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 /*
233348Sdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
243034Sdougm  * Use is subject to license terms.
253034Sdougm  */
263034Sdougm 
273034Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
283034Sdougm 
293034Sdougm #include <sys/types.h>
303034Sdougm #include <sys/stat.h>
313034Sdougm #include <fcntl.h>
323034Sdougm #include <stdlib.h>
333034Sdougm #include <stdio.h>
343034Sdougm #include <string.h>
353034Sdougm #include <ctype.h>
363034Sdougm #include <unistd.h>
373034Sdougm #include <getopt.h>
383034Sdougm #include <utmpx.h>
393034Sdougm #include <pwd.h>
403034Sdougm #include <auth_attr.h>
413034Sdougm #include <secdb.h>
423034Sdougm #include <sys/param.h>
433034Sdougm #include <sys/stat.h>
443034Sdougm #include <errno.h>
453034Sdougm 
463034Sdougm #include <libshare.h>
473034Sdougm #include "sharemgr.h"
483034Sdougm #include <libscf.h>
493034Sdougm #include <libxml/tree.h>
503034Sdougm #include <libintl.h>
515331Samw #include <assert.h>
525331Samw #include <iconv.h>
535331Samw #include <langinfo.h>
545331Samw #include <dirent.h>
553034Sdougm 
563034Sdougm static char *sa_get_usage(sa_usage_t);
573034Sdougm 
583034Sdougm /*
593034Sdougm  * Implementation of the common sub-commands supported by sharemgr.
603034Sdougm  * A number of helper functions are also included.
613034Sdougm  */
623034Sdougm 
633034Sdougm /*
643034Sdougm  * has_protocol(group, proto)
653034Sdougm  *	If the group has an optionset with the specified protocol,
663034Sdougm  *	return true (1) otherwise false (0).
673034Sdougm  */
683034Sdougm static int
693034Sdougm has_protocol(sa_group_t group, char *protocol)
703034Sdougm {
713034Sdougm 	sa_optionset_t optionset;
723034Sdougm 	int result = 0;
733034Sdougm 
743034Sdougm 	optionset = sa_get_optionset(group, protocol);
753034Sdougm 	if (optionset != NULL) {
764653Sdougm 		result++;
773034Sdougm 	}
783034Sdougm 	return (result);
793034Sdougm }
803034Sdougm 
813034Sdougm /*
825331Samw  * validresource(name)
835331Samw  *
845331Samw  * Check that name only has valid characters in it. The current valid
855331Samw  * set are the printable characters but not including:
865331Samw  *	" / \ [ ] : | < > + ; , ? * = \t
875331Samw  * Note that space is included and there is a maximum length.
885331Samw  */
895331Samw static int
905331Samw validresource(const char *name)
915331Samw {
925331Samw 	const char *cp;
935331Samw 	size_t len;
945331Samw 
955331Samw 	if (name == NULL)
965331Samw 		return (B_FALSE);
975331Samw 
985331Samw 	len = strlen(name);
995331Samw 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
1005331Samw 		return (B_FALSE);
1015331Samw 
1025331Samw 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
1035331Samw 		return (B_FALSE);
1045331Samw 	}
1055331Samw 
1065331Samw 	for (cp = name; *cp != '\0'; cp++)
1075331Samw 		if (iscntrl(*cp))
1085331Samw 			return (B_FALSE);
1095331Samw 
1105331Samw 	return (B_TRUE);
1115331Samw }
1125331Samw 
1135331Samw /*
1145331Samw  * conv_to_utf8(input)
1155331Samw  *
1165331Samw  * Convert the input string to utf8 from the current locale.  If the
1175331Samw  * conversion fails, use the current locale, it is likely close
1185331Samw  * enough. For example, the "C" locale is a subset of utf-8. The
1195331Samw  * return value may be a new string or the original input string.
1205331Samw  */
1215331Samw 
1225331Samw static char *
1235331Samw conv_to_utf8(char *input)
1245331Samw {
1255331Samw 	iconv_t cd;
126*5521Sas200622 	char *inval = input;
1275331Samw 	char *output = input;
1285331Samw 	char *outleft;
1295331Samw 	char *curlocale;
1305331Samw 	size_t bytesleft;
1315331Samw 	size_t size;
1325331Samw 	size_t osize;
1335331Samw 	static int warned = 0;
1345331Samw 
1355331Samw 	curlocale = nl_langinfo(CODESET);
1365331Samw 	if (curlocale == NULL)
1375331Samw 		curlocale = "C";
1385331Samw 	cd = iconv_open("UTF-8", curlocale);
1395331Samw 	if (cd != NULL && cd != (iconv_t)-1) {
1405331Samw 		size = strlen(input);
1415331Samw 		/* Assume worst case of characters expanding to 4 bytes. */
1425331Samw 		bytesleft = size * 4;
1435331Samw 		output = calloc(bytesleft, 1);
1445331Samw 		if (output != NULL) {
1455331Samw 			outleft = output;
146*5521Sas200622 			/* inval can be modified on return */
147*5521Sas200622 			osize = iconv(cd, (const char **)&inval, &size,
1485331Samw 			    &outleft, &bytesleft);
1495331Samw 			if (osize == (size_t)-1 || size != 0) {
1505331Samw 				free(output);
1515331Samw 				output = input;
1525331Samw 			}
153*5521Sas200622 		} else {
154*5521Sas200622 			/* Need to return something. */
155*5521Sas200622 			output = input;
1565331Samw 		}
1575331Samw 		(void) iconv_close(cd);
1585331Samw 	} else {
1595331Samw 		if (!warned)
1605331Samw 			(void) fprintf(stderr,
1615331Samw 			    gettext("Cannot convert to UTF-8 from %s\n"),
1625331Samw 			    curlocale ? curlocale : gettext("unknown"));
1635331Samw 		warned = 1;
1645331Samw 	}
1655331Samw 	return (output);
1665331Samw }
1675331Samw 
1685331Samw /*
1695331Samw  * conv_from(input)
1705331Samw  *
1715331Samw  * Convert the input string from utf8 to current locale.  If the
1725331Samw  * conversion isn't supported, just use as is. The return value may be
1735331Samw  * a new string or the original input string.
1745331Samw  */
1755331Samw 
1765331Samw static char *
1775331Samw conv_from_utf8(char *input)
1785331Samw {
1795331Samw 	iconv_t cd;
1805331Samw 	char *output = input;
181*5521Sas200622 	char *inval = input;
1825331Samw 	char *outleft;
1835331Samw 	char *curlocale;
1845331Samw 	size_t bytesleft;
1855331Samw 	size_t size;
1865331Samw 	size_t osize;
1875331Samw 	static int warned = 0;
1885331Samw 
1895331Samw 	curlocale = nl_langinfo(CODESET);
1905331Samw 	if (curlocale == NULL)
1915331Samw 		curlocale = "C";
1925331Samw 	cd = iconv_open(curlocale, "UTF-8");
1935331Samw 	if (cd != NULL && cd != (iconv_t)-1) {
1945331Samw 		size = strlen(input);
1955331Samw 		/* Assume worst case of characters expanding to 4 bytes. */
1965331Samw 		bytesleft = size * 4;
1975331Samw 		output = calloc(bytesleft, 1);
1985331Samw 		if (output != NULL) {
1995331Samw 			outleft = output;
200*5521Sas200622 			osize = iconv(cd, (const char **)&inval, &size,
2015331Samw 			    &outleft, &bytesleft);
202*5521Sas200622 			if (osize == (size_t)-1 || size != 0)
2035331Samw 				output = input;
204*5521Sas200622 		} else {
205*5521Sas200622 			/* Need to return something. */
206*5521Sas200622 			output = input;
2075331Samw 		}
2085331Samw 		(void) iconv_close(cd);
2095331Samw 	} else {
2105331Samw 		if (!warned)
2115331Samw 			(void) fprintf(stderr,
2125331Samw 			    gettext("Cannot convert to %s from UTF-8\n"),
2135331Samw 			    curlocale ? curlocale : gettext("unknown"));
2145331Samw 		warned = 1;
2155331Samw 	}
2165331Samw 	return (output);
2175331Samw }
2185331Samw 
2195331Samw static void
2205331Samw print_rsrc_desc(char *resource)
2215331Samw {
2225331Samw 	char *description;
2235331Samw 	char *desc;
2245331Samw 
2255331Samw 	description = sa_get_resource_description(resource);
2265331Samw 	if (description != NULL) {
2275331Samw 		desc = conv_from_utf8(description);
2285331Samw 		if (desc != description) {
2295331Samw 			sa_free_share_description(description);
2305331Samw 			description = desc;
2315331Samw 		}
2325331Samw 		(void) printf("\t\"%s\"", description);
2335331Samw 		sa_free_share_description(description);
2345331Samw 	}
2355331Samw }
2365331Samw 
2375331Samw static int
2385331Samw set_share_desc(sa_share_t share, char *description)
2395331Samw {
2405331Samw 	char *desc;
2415331Samw 	int ret;
2425331Samw 
2435331Samw 	desc = conv_to_utf8(description);
2445331Samw 	ret = sa_set_share_description(share, desc);
2455331Samw 	if (description != desc)
2465331Samw 		sa_free_share_description(desc);
2475331Samw 	return (ret);
2485331Samw }
2495331Samw 
2505331Samw /*
2515331Samw  * add_list(list, item, data, proto)
2525331Samw  *	Adds a new list member that points holds item in the list.
2533034Sdougm  *	If list is NULL, it starts a new list.  The function returns
2543034Sdougm  *	the first member of the list.
2553034Sdougm  */
2563034Sdougm struct list *
2575331Samw add_list(struct list *listp, void *item, void *data, char *proto)
2583034Sdougm {
2593034Sdougm 	struct list *new, *tmp;
2603034Sdougm 
2613034Sdougm 	new = malloc(sizeof (struct list));
2623034Sdougm 	if (new != NULL) {
2634653Sdougm 		new->next = NULL;
2644653Sdougm 		new->item = item;
2654653Sdougm 		new->itemdata = data;
2665331Samw 		new->proto = proto;
2673034Sdougm 	} else {
2684653Sdougm 		return (listp);
2693034Sdougm 	}
2703034Sdougm 
2713034Sdougm 	if (listp == NULL)
2724653Sdougm 		return (new);
2733034Sdougm 
2743034Sdougm 	for (tmp = listp; tmp->next != NULL; tmp = tmp->next) {
2753034Sdougm 		/* get to end of list */
2763034Sdougm 	}
2773034Sdougm 	tmp->next = new;
2783034Sdougm 	return (listp);
2793034Sdougm }
2803034Sdougm 
2813034Sdougm /*
2823034Sdougm  * free_list(list)
2833034Sdougm  *	Given a list, free all the members of the list;
2843034Sdougm  */
2853034Sdougm static void
2863034Sdougm free_list(struct list *listp)
2873034Sdougm {
2883034Sdougm 	struct list *tmp;
2893034Sdougm 	while (listp != NULL) {
2904653Sdougm 		tmp = listp;
2914653Sdougm 		listp = listp->next;
2924653Sdougm 		free(tmp);
2933034Sdougm 	}
2943034Sdougm }
2953034Sdougm 
2963034Sdougm /*
2973034Sdougm  * check_authorization(instname, which)
2983034Sdougm  *
2993034Sdougm  * Checks to see if the specific type of authorization in which is
3003034Sdougm  * enabled for the user in this SMF service instance.
3013034Sdougm  */
3023034Sdougm 
3033034Sdougm static int
3043034Sdougm check_authorization(char *instname, int which)
3053034Sdougm {
3063034Sdougm 	scf_handle_t *handle = NULL;
3073034Sdougm 	scf_simple_prop_t *prop = NULL;
3083034Sdougm 	char svcstring[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
3093034Sdougm 	char *authstr = NULL;
3103034Sdougm 	ssize_t numauths;
3114653Sdougm 	int ret = B_TRUE;
3123034Sdougm 	uid_t uid;
3133034Sdougm 	struct passwd *pw = NULL;
3143034Sdougm 
3153034Sdougm 	uid = getuid();
3163034Sdougm 	pw = getpwuid(uid);
3174653Sdougm 	if (pw == NULL) {
3184653Sdougm 		ret = B_FALSE;
3194653Sdougm 	} else {
3204653Sdougm 		/*
3214653Sdougm 		 * Since names are restricted to SA_MAX_NAME_LEN won't
3224653Sdougm 		 * overflow.
3234653Sdougm 		 */
3244653Sdougm 		(void) snprintf(svcstring, sizeof (svcstring), "%s:%s",
3254653Sdougm 		    SA_SVC_FMRI_BASE, instname);
3264653Sdougm 		handle = scf_handle_create(SCF_VERSION);
3274653Sdougm 		if (handle != NULL) {
3284653Sdougm 			if (scf_handle_bind(handle) == 0) {
3294653Sdougm 				switch (which) {
3304653Sdougm 				case SVC_SET:
3314653Sdougm 					prop = scf_simple_prop_get(handle,
3324653Sdougm 					    svcstring, "general",
3334653Sdougm 					    SVC_AUTH_VALUE);
3344653Sdougm 					break;
3354653Sdougm 				case SVC_ACTION:
3364653Sdougm 					prop = scf_simple_prop_get(handle,
3374653Sdougm 					    svcstring, "general",
3384653Sdougm 					    SVC_AUTH_ACTION);
3394653Sdougm 					break;
3404653Sdougm 				}
3414653Sdougm 			}
3423034Sdougm 		}
3433034Sdougm 	}
3443034Sdougm 	/* make sure we have an authorization string property */
3453034Sdougm 	if (prop != NULL) {
3464653Sdougm 		int i;
3474653Sdougm 		numauths = scf_simple_prop_numvalues(prop);
3484653Sdougm 		for (ret = 0, i = 0; i < numauths; i++) {
3494653Sdougm 			authstr = scf_simple_prop_next_astring(prop);
3504653Sdougm 			if (authstr != NULL) {
3514653Sdougm 				/* check if this user has one of the strings */
3524653Sdougm 				if (chkauthattr(authstr, pw->pw_name)) {
3534653Sdougm 					ret = 1;
3544653Sdougm 					break;
3554653Sdougm 				}
3564653Sdougm 			}
3573034Sdougm 		}
3584653Sdougm 		endauthattr();
3594653Sdougm 		scf_simple_prop_free(prop);
3603034Sdougm 	} else {
3614653Sdougm 		/* no authorization string defined */
3624653Sdougm 		ret = 0;
3633034Sdougm 	}
3643034Sdougm 	if (handle != NULL)
3654653Sdougm 		scf_handle_destroy(handle);
3663034Sdougm 	return (ret);
3673034Sdougm }
3683034Sdougm 
3693034Sdougm /*
3703034Sdougm  * check_authorizations(instname, flags)
3713034Sdougm  *
3723034Sdougm  * check all the needed authorizations for the user in this service
3733034Sdougm  * instance. Return value of 1(true) or 0(false) indicates whether
3743034Sdougm  * there are authorizations for the user or not.
3753034Sdougm  */
3763034Sdougm 
3773034Sdougm static int
3783034Sdougm check_authorizations(char *instname, int flags)
3793034Sdougm {
3803034Sdougm 	int ret1 = 0;
3813034Sdougm 	int ret2 = 0;
3823034Sdougm 	int ret;
3833034Sdougm 
3843034Sdougm 	if (flags & SVC_SET)
3854653Sdougm 		ret1 = check_authorization(instname, SVC_SET);
3863034Sdougm 	if (flags & SVC_ACTION)
3874653Sdougm 		ret2 = check_authorization(instname, SVC_ACTION);
3883034Sdougm 	switch (flags) {
3893034Sdougm 	case SVC_ACTION:
3904653Sdougm 		ret = ret2;
3914653Sdougm 		break;
3923034Sdougm 	case SVC_SET:
3934653Sdougm 		ret = ret1;
3944653Sdougm 		break;
3953034Sdougm 	case SVC_ACTION|SVC_SET:
3964653Sdougm 		ret = ret1 & ret2;
3974653Sdougm 		break;
3983034Sdougm 	default:
3994653Sdougm 		/* if not flags set, we assume we don't need authorizations */
4004653Sdougm 		ret = 1;
4013034Sdougm 	}
4023034Sdougm 	return (ret);
4033034Sdougm }
4043034Sdougm 
4053034Sdougm /*
4065331Samw  * notify_or_enable_share(share, protocol)
4075331Samw  *
4085331Samw  * Since some protocols don't want an "enable" when properties change,
4095331Samw  * this function will use the protocol specific notify function
4105331Samw  * first. If that fails, it will then attempt to use the
4115331Samw  * sa_enable_share().  "protocol" is the protocol that was specified
4125331Samw  * on the command line.
4135331Samw  */
4145331Samw static void
4155331Samw notify_or_enable_share(sa_share_t share, char *protocol)
4165331Samw {
4175331Samw 	sa_group_t group;
4185331Samw 	sa_optionset_t opt;
4195331Samw 	int ret = SA_OK;
4205331Samw 	char *path;
4215331Samw 	char *groupproto;
4225331Samw 	sa_share_t parent = share;
4235331Samw 
4245331Samw 	/* If really a resource, get parent share */
4255331Samw 	if (!sa_is_share(share)) {
4265331Samw 		parent = sa_get_resource_parent((sa_resource_t)share);
4275331Samw 	}
4285331Samw 
4295331Samw 	/*
4305331Samw 	 * Now that we've got a share in "parent", make sure it has a path.
4315331Samw 	 */
4325331Samw 	path = sa_get_share_attr(parent, "path");
4335331Samw 	if (path == NULL)
4345331Samw 		return;
4355331Samw 
4365331Samw 	group = sa_get_parent_group(parent);
4375331Samw 
4385331Samw 	if (group == NULL) {
4395331Samw 		sa_free_attr_string(path);
4405331Samw 		return;
4415331Samw 	}
4425331Samw 	for (opt = sa_get_optionset(group, NULL);
4435331Samw 	    opt != NULL;
4445331Samw 	    opt = sa_get_next_optionset(opt)) {
4455331Samw 		groupproto = sa_get_optionset_attr(opt, "type");
4465331Samw 		if (groupproto == NULL ||
4475331Samw 		    (protocol != NULL && strcmp(groupproto, protocol) != 0)) {
4485331Samw 			sa_free_attr_string(groupproto);
4495331Samw 			continue;
4505331Samw 		}
4515331Samw 		if (sa_is_share(share)) {
4525331Samw 			if ((ret = sa_proto_change_notify(share,
4535331Samw 			    groupproto)) != SA_OK) {
4545331Samw 				ret = sa_enable_share(share, groupproto);
4555331Samw 				if (ret != SA_OK) {
4565331Samw 					(void) printf(
4575331Samw 					    gettext("Could not reenable"
4585331Samw 					    " share %s: %s\n"),
4595331Samw 					    path, sa_errorstr(ret));
4605331Samw 				}
4615331Samw 			}
4625331Samw 		} else {
4635331Samw 			/* Must be a resource */
4645331Samw 			if ((ret = sa_proto_notify_resource(share,
4655331Samw 			    groupproto)) != SA_OK) {
4665331Samw 				ret = sa_enable_resource(share, groupproto);
4675331Samw 				if (ret != SA_OK) {
4685331Samw 					(void) printf(
4695331Samw 					    gettext("Could not "
4705331Samw 					    "reenable resource %s: "
4715331Samw 					    "%s\n"), path,
4725331Samw 					    sa_errorstr(ret));
4735331Samw 				}
4745331Samw 			}
4755331Samw 		}
4765331Samw 		sa_free_attr_string(groupproto);
4775331Samw 	}
4785331Samw 	sa_free_attr_string(path);
4795331Samw }
4805331Samw 
4815331Samw /*
4825331Samw  * enable_group(group, updateproto, notify, proto)
4833082Sdougm  *
4843082Sdougm  * enable all the shares in the specified group. This is a helper for
4853082Sdougm  * enable_all_groups in order to simplify regular and subgroup (zfs)
4865331Samw  * enabling. Group has already been checked for non-NULL. If notify
4875331Samw  * is non-zero, attempt to use the notify interface rather than
4885331Samw  * enable.
4893082Sdougm  */
4903082Sdougm static void
4915331Samw enable_group(sa_group_t group, char *updateproto, int notify, char *proto)
4923082Sdougm {
4933082Sdougm 	sa_share_t share;
4943082Sdougm 
4953082Sdougm 	for (share = sa_get_share(group, NULL);
4963082Sdougm 	    share != NULL;
4973082Sdougm 	    share = sa_get_next_share(share)) {
4984653Sdougm 		if (updateproto != NULL)
4994653Sdougm 			(void) sa_update_legacy(share, updateproto);
5005331Samw 		if (notify)
5015331Samw 			notify_or_enable_share(share, proto);
5025331Samw 		else
5035331Samw 			(void) sa_enable_share(share, proto);
5043082Sdougm 	}
5053082Sdougm }
5063082Sdougm 
5073082Sdougm /*
5084241Sdougm  * isenabled(group)
5094241Sdougm  *
5104241Sdougm  * Returns B_TRUE if the group is enabled or B_FALSE if it isn't.
5114241Sdougm  * Moved to separate function to reduce clutter in the code.
5124241Sdougm  */
5134241Sdougm 
5144241Sdougm static int
5154241Sdougm isenabled(sa_group_t group)
5164241Sdougm {
5174241Sdougm 	char *state;
5184241Sdougm 	int ret = B_FALSE;
5194241Sdougm 
5204241Sdougm 	if (group != NULL) {
5214653Sdougm 		state = sa_get_group_attr(group, "state");
5224653Sdougm 		if (state != NULL) {
5235331Samw 
5244653Sdougm 			if (strcmp(state, "enabled") == 0)
5254653Sdougm 				ret = B_TRUE;
5264653Sdougm 			sa_free_attr_string(state);
5274653Sdougm 		}
5284241Sdougm 	}
5294241Sdougm 	return (ret);
5304241Sdougm }
5314241Sdougm 
5324241Sdougm /*
5333082Sdougm  * enable_all_groups(list, setstate, online, updateproto)
5345331Samw  *
5355331Samw  * Given a list of groups, enable each one found.  If updateproto is
5365331Samw  * not NULL, then update all the shares for the protocol that was
5375331Samw  * passed in. If enable is non-zero, tell enable_group to try the
5385331Samw  * notify interface since this is a property change.
5393034Sdougm  */
5403034Sdougm static int
5413910Sdougm enable_all_groups(sa_handle_t handle, struct list *work, int setstate,
5425331Samw     int online, char *updateproto, int enable)
5433034Sdougm {
5444241Sdougm 	int ret;
5453034Sdougm 	char instance[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
5463034Sdougm 	char *state;
5473034Sdougm 	char *name;
5483034Sdougm 	char *zfs = NULL;
5493034Sdougm 	sa_group_t group;
5503082Sdougm 	sa_group_t subgroup;
5513034Sdougm 
5524241Sdougm 	for (ret = SA_OK; work != NULL;	work = work->next) {
5534653Sdougm 		group = (sa_group_t)work->item;
5544241Sdougm 
5554241Sdougm 		/*
5564241Sdougm 		 * If setstate == TRUE, then make sure to set
5574241Sdougm 		 * enabled. This needs to be done here in order for
5584241Sdougm 		 * the isenabled check to succeed on a newly enabled
5594241Sdougm 		 * group.
5604241Sdougm 		 */
5614653Sdougm 		if (setstate == B_TRUE) {
5624653Sdougm 			ret = sa_set_group_attr(group, "state",	"enabled");
5634653Sdougm 			if (ret != SA_OK)
5644653Sdougm 				break;
5654653Sdougm 		}
5664241Sdougm 
5674241Sdougm 		/*
5684241Sdougm 		 * Check to see if group is enabled. If it isn't, skip
5694241Sdougm 		 * the rest.  We don't want shares starting if the
5704241Sdougm 		 * group is disabled. The properties may have been
5714241Sdougm 		 * updated, but there won't be a change until the
5724241Sdougm 		 * group is enabled.
5734241Sdougm 		 */
5744653Sdougm 		if (!isenabled(group))
5754653Sdougm 			continue;
5764653Sdougm 
5774653Sdougm 		/* if itemdata != NULL then a single share */
5784653Sdougm 		if (work->itemdata != NULL) {
5795331Samw 			if (enable) {
5805331Samw 				if (work->itemdata != NULL)
5815331Samw 					notify_or_enable_share(work->itemdata,
5825331Samw 					    updateproto);
5835331Samw 				else
5845331Samw 					ret = SA_CONFIG_ERR;
5855331Samw 			} else {
5865331Samw 				if (sa_is_share(work->itemdata)) {
5875331Samw 					ret = sa_enable_share(
5885331Samw 					    (sa_share_t)work->itemdata,
5895331Samw 					    updateproto);
5905331Samw 				} else {
5915331Samw 					ret = sa_enable_resource(
5925331Samw 					    (sa_resource_t)work->itemdata,
5935331Samw 					    updateproto);
5945331Samw 				}
5955331Samw 			}
5963034Sdougm 		}
5974653Sdougm 		if (ret != SA_OK)
5984653Sdougm 			break;
5994653Sdougm 
6004653Sdougm 		/* if itemdata == NULL then the whole group */
6014653Sdougm 		if (work->itemdata == NULL) {
6024653Sdougm 			zfs = sa_get_group_attr(group, "zfs");
6034653Sdougm 			/*
6045331Samw 			 * If the share is managed by ZFS, don't
6054653Sdougm 			 * update any of the protocols since ZFS is
6065331Samw 			 * handling this.  Updateproto will contain
6074653Sdougm 			 * the name of the protocol that we want to
6084653Sdougm 			 * update legacy files for.
6094653Sdougm 			 */
6105331Samw 			enable_group(group, zfs == NULL ? updateproto : NULL,
6115331Samw 			    enable, work->proto);
6124653Sdougm 			for (subgroup = sa_get_sub_group(group);
6134653Sdougm 			    subgroup != NULL;
6144653Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
6154653Sdougm 				/* never update legacy for ZFS subgroups */
6165331Samw 				enable_group(subgroup, NULL, enable,
6175331Samw 				    work->proto);
6183034Sdougm 			}
6193034Sdougm 		}
6204653Sdougm 		if (online) {
6214653Sdougm 			zfs = sa_get_group_attr(group, "zfs");
6224653Sdougm 			name = sa_get_group_attr(group, "name");
6234653Sdougm 			if (name != NULL) {
6244653Sdougm 				if (zfs == NULL) {
6254653Sdougm 					(void) snprintf(instance,
6264653Sdougm 					    sizeof (instance), "%s:%s",
6274653Sdougm 					    SA_SVC_FMRI_BASE, name);
6284653Sdougm 					state = smf_get_state(instance);
6294653Sdougm 					if (state == NULL ||
6304653Sdougm 					    strcmp(state, "online") != 0) {
6314653Sdougm 						(void) smf_enable_instance(
6324653Sdougm 						    instance, 0);
6334653Sdougm 						free(state);
6344653Sdougm 					}
6354653Sdougm 				} else {
6364653Sdougm 					sa_free_attr_string(zfs);
6374653Sdougm 					zfs = NULL;
6384653Sdougm 				}
6394653Sdougm 				if (name != NULL)
6404653Sdougm 					sa_free_attr_string(name);
6414653Sdougm 			}
6424653Sdougm 		}
6433034Sdougm 	}
6443034Sdougm 	if (ret == SA_OK) {
6454653Sdougm 		ret = sa_update_config(handle);
6463034Sdougm 	}
6473034Sdougm 	return (ret);
6483034Sdougm }
6493034Sdougm 
6503034Sdougm /*
6513034Sdougm  * chk_opt(optlistp, security, proto)
6523034Sdougm  *
6533034Sdougm  * Do a sanity check on the optlist provided for the protocol.  This
6543034Sdougm  * is a syntax check and verification that the property is either a
6553034Sdougm  * general or specific to a names optionset.
6563034Sdougm  */
6573034Sdougm 
6583034Sdougm static int
6593034Sdougm chk_opt(struct options *optlistp, int security, char *proto)
6603034Sdougm {
6613034Sdougm 	struct options *optlist;
6623034Sdougm 	char *sep = "";
6633034Sdougm 	int notfirst = 0;
6643034Sdougm 	int ret;
6653034Sdougm 
6663034Sdougm 	for (optlist = optlistp; optlist != NULL; optlist = optlist->next) {
6674653Sdougm 		char *optname;
6684653Sdougm 
6694653Sdougm 		optname = optlist->optname;
6704653Sdougm 		ret = OPT_ADD_OK;
6714653Sdougm 		/* extract property/value pair */
6724653Sdougm 		if (sa_is_security(optname, proto)) {
6734653Sdougm 			if (!security)
6744653Sdougm 				ret = OPT_ADD_SECURITY;
6754653Sdougm 		} else {
6764653Sdougm 			if (security)
6774653Sdougm 				ret = OPT_ADD_PROPERTY;
6784653Sdougm 		}
6794653Sdougm 		if (ret != OPT_ADD_OK) {
6804653Sdougm 			if (notfirst == 0)
6814653Sdougm 				(void) printf(
6824653Sdougm 				    gettext("Property syntax error: "));
6834653Sdougm 			switch (ret) {
6844653Sdougm 			case OPT_ADD_SYNTAX:
6854653Sdougm 				(void) printf(gettext("%ssyntax error: %s"),
6863034Sdougm 				    sep, optname);
6874653Sdougm 				sep = ", ";
6884653Sdougm 				break;
6894653Sdougm 			case OPT_ADD_SECURITY:
6904653Sdougm 				(void) printf(gettext("%s%s requires -S"),
6913034Sdougm 				    optname, sep);
6924653Sdougm 				sep = ", ";
6934653Sdougm 				break;
6944653Sdougm 			case OPT_ADD_PROPERTY:
6954653Sdougm 				(void) printf(
6964653Sdougm 				    gettext("%s%s not supported with -S"),
6973034Sdougm 				    optname, sep);
6984653Sdougm 				sep = ", ";
6994653Sdougm 				break;
7004653Sdougm 			}
7014653Sdougm 			notfirst++;
7023034Sdougm 		}
7033034Sdougm 	}
7043034Sdougm 	if (notfirst) {
7054653Sdougm 		(void) printf("\n");
7064653Sdougm 		ret = SA_SYNTAX_ERR;
7073034Sdougm 	}
7083034Sdougm 	return (ret);
7093034Sdougm }
7103034Sdougm 
7113034Sdougm /*
7123034Sdougm  * free_opt(optlist)
7133034Sdougm  *	Free the specified option list.
7143034Sdougm  */
7153034Sdougm static void
7163034Sdougm free_opt(struct options *optlist)
7173034Sdougm {
7183034Sdougm 	struct options *nextopt;
7193034Sdougm 	while (optlist != NULL) {
7203034Sdougm 		nextopt = optlist->next;
7213034Sdougm 		free(optlist);
7223034Sdougm 		optlist = nextopt;
7233034Sdougm 	}
7243034Sdougm }
7253034Sdougm 
7263034Sdougm /*
7273034Sdougm  * check property list for valid properties
7283034Sdougm  * A null value is a remove which is always valid.
7293034Sdougm  */
7303034Sdougm static int
7313034Sdougm valid_options(struct options *optlist, char *proto, void *object, char *sec)
7323034Sdougm {
7333034Sdougm 	int ret = SA_OK;
7343034Sdougm 	struct options *cur;
7353034Sdougm 	sa_property_t prop;
7363034Sdougm 	sa_optionset_t parent = NULL;
7373034Sdougm 
7383034Sdougm 	if (object != NULL) {
7394653Sdougm 		if (sec == NULL)
7404653Sdougm 			parent = sa_get_optionset(object, proto);
7414653Sdougm 		else
7424653Sdougm 			parent = sa_get_security(object, sec, proto);
7433034Sdougm 	}
7443034Sdougm 
7453034Sdougm 	for (cur = optlist; cur != NULL; cur = cur->next) {
7464653Sdougm 		if (cur->optvalue == NULL)
7474653Sdougm 			continue;
7483034Sdougm 		prop = sa_create_property(cur->optname, cur->optvalue);
7493034Sdougm 		if (prop == NULL)
7504653Sdougm 			ret = SA_NO_MEMORY;
7513034Sdougm 		if (ret != SA_OK ||
7523034Sdougm 		    (ret = sa_valid_property(parent, proto, prop)) != SA_OK) {
7534653Sdougm 			(void) printf(
7544653Sdougm 			    gettext("Could not add property %s: %s\n"),
7554653Sdougm 			    cur->optname, sa_errorstr(ret));
7563034Sdougm 		}
7573034Sdougm 		(void) sa_remove_property(prop);
7583034Sdougm 	}
7593034Sdougm 	return (ret);
7603034Sdougm }
7613034Sdougm 
7623034Sdougm /*
7633034Sdougm  * add_optionset(group, optlist, protocol, *err)
7643034Sdougm  *	Add the options in optlist to an optionset and then add the optionset
7653034Sdougm  *	to the group.
7663034Sdougm  *
7673034Sdougm  *	The return value indicates if there was a "change" while errors are
7683034Sdougm  *	returned via the *err parameters.
7693034Sdougm  */
7703034Sdougm static int
7713034Sdougm add_optionset(sa_group_t group, struct options *optlist, char *proto, int *err)
7723034Sdougm {
7733034Sdougm 	sa_optionset_t optionset;
7743034Sdougm 	int ret = SA_OK;
7755331Samw 	int result = B_FALSE;
7763034Sdougm 
7773034Sdougm 	optionset = sa_get_optionset(group, proto);
7783034Sdougm 	if (optionset == NULL) {
7794653Sdougm 		optionset = sa_create_optionset(group, proto);
7805331Samw 		if (optionset == NULL)
7815331Samw 			ret = SA_NO_MEMORY;
7825331Samw 		result = B_TRUE; /* adding a protocol is a change */
7833034Sdougm 	}
7844653Sdougm 	if (optionset == NULL) {
7854653Sdougm 		ret = SA_NO_MEMORY;
7864653Sdougm 		goto out;
7874653Sdougm 	}
7884653Sdougm 	while (optlist != NULL) {
7893034Sdougm 		sa_property_t prop;
7903034Sdougm 		prop = sa_get_property(optionset, optlist->optname);
7913034Sdougm 		if (prop == NULL) {
7923034Sdougm 			/*
7933034Sdougm 			 * add the property, but only if it is
7943034Sdougm 			 * a non-NULL or non-zero length value
7953034Sdougm 			 */
7964653Sdougm 			if (optlist->optvalue != NULL) {
7974653Sdougm 				prop = sa_create_property(optlist->optname,
7984653Sdougm 				    optlist->optvalue);
7994653Sdougm 				if (prop != NULL) {
8004653Sdougm 					ret = sa_valid_property(optionset,
8014653Sdougm 					    proto, prop);
8024653Sdougm 					if (ret != SA_OK) {
8034653Sdougm 						(void) sa_remove_property(prop);
8044653Sdougm 						(void) printf(gettext("Could "
8054653Sdougm 						    "not add property "
8064653Sdougm 						    "%s: %s\n"),
8074653Sdougm 						    optlist->optname,
8084653Sdougm 						    sa_errorstr(ret));
8094653Sdougm 					}
8104653Sdougm 				}
8114653Sdougm 				if (ret == SA_OK) {
8124653Sdougm 					ret = sa_add_property(optionset, prop);
8134653Sdougm 					if (ret != SA_OK) {
8144653Sdougm 						(void) printf(gettext(
8154653Sdougm 						    "Could not add property "
8164653Sdougm 						    "%s: %s\n"),
8174653Sdougm 						    optlist->optname,
8184653Sdougm 						    sa_errorstr(ret));
8194653Sdougm 					} else {
8204653Sdougm 						/* there was a change */
8215331Samw 						result = B_TRUE;
8224653Sdougm 					}
8234653Sdougm 				}
8243034Sdougm 			}
8254653Sdougm 		} else {
8264653Sdougm 			ret = sa_update_property(prop, optlist->optvalue);
8274653Sdougm 			/* should check to see if value changed */
8284653Sdougm 			if (ret != SA_OK) {
8294653Sdougm 				(void) printf(gettext("Could not update "
8304653Sdougm 				    "property %s: %s\n"), optlist->optname,
8314653Sdougm 				    sa_errorstr(ret));
8324653Sdougm 			} else {
8335331Samw 				result = B_TRUE;
8343034Sdougm 			}
8353034Sdougm 		}
8363034Sdougm 		optlist = optlist->next;
8373034Sdougm 	}
8384653Sdougm 	ret = sa_commit_properties(optionset, 0);
8394653Sdougm 
8404653Sdougm out:
8413034Sdougm 	if (err != NULL)
8424653Sdougm 		*err = ret;
8433034Sdougm 	return (result);
8443034Sdougm }
8453034Sdougm 
8463034Sdougm /*
8475331Samw  * resource_compliant(group)
8485331Samw  *
8495331Samw  * Go through all the shares in the group. Assume compliant, but if
8505331Samw  * any share doesn't have at least one resource name, it isn't
8515331Samw  * compliant.
8525331Samw  */
8535331Samw static int
8545331Samw resource_compliant(sa_group_t group)
8555331Samw {
8565331Samw 	sa_share_t share;
8575331Samw 
8585331Samw 	for (share = sa_get_share(group, NULL); share != NULL;
8595331Samw 	    share = sa_get_next_share(share)) {
8605331Samw 		if (sa_get_share_resource(share, NULL) == NULL) {
8615331Samw 			return (B_FALSE);
8625331Samw 		}
8635331Samw 	}
8645331Samw 	return (B_TRUE);
8655331Samw }
8665331Samw 
8675331Samw /*
8685331Samw  * fix_path(path)
8695331Samw  *
8705331Samw  * change all illegal characters to something else.  For now, all get
8715331Samw  * converted to '_' and the leading '/' is stripped off. This is used
8725331Samw  * to construct an resource name (SMB share name) that is valid.
8735331Samw  * Caller must pass a valid path.
8745331Samw  */
8755331Samw static void
8765331Samw fix_path(char *path)
8775331Samw {
8785331Samw 	char *cp;
8795331Samw 	size_t len;
8805331Samw 
8815331Samw 	assert(path != NULL);
8825331Samw 
8835331Samw 	/* make sure we are appropriate length */
8845331Samw 	cp = path + 1; /* skip leading slash */
8855331Samw 	while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) {
8865331Samw 		cp = strchr(cp, '/');
8875331Samw 		if (cp != NULL)
8885331Samw 			cp++;
8895331Samw 	}
8905331Samw 	/* two cases - cp == NULL and cp is substring of path */
8915331Samw 	if (cp == NULL) {
8925331Samw 		/* just take last SA_MAX_RESOURCE_NAME chars */
8935331Samw 		len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME;
8945331Samw 		(void) memmove(path, path + len, SA_MAX_RESOURCE_NAME);
8955331Samw 		path[SA_MAX_RESOURCE_NAME] = '\0';
8965331Samw 	} else {
8975331Samw 		len = strlen(cp) + 1;
8985331Samw 		(void) memmove(path, cp, len);
8995331Samw 	}
9005331Samw 
9015331Samw 	/*
9025331Samw 	 * Don't want any of the characters that are not allowed
9035331Samw 	 * in and SMB share name. Replace them with '_'.
9045331Samw 	 */
9055331Samw 	while (*path) {
9065331Samw 		switch (*path) {
9075331Samw 		case '/':
9085331Samw 		case '"':
9095331Samw 		case '\\':
9105331Samw 		case '[':
9115331Samw 		case ']':
9125331Samw 		case ':':
9135331Samw 		case '|':
9145331Samw 		case '<':
9155331Samw 		case '>':
9165331Samw 		case '+':
9175331Samw 		case ';':
9185331Samw 		case ',':
9195331Samw 		case '?':
9205331Samw 		case '*':
9215331Samw 		case '=':
9225331Samw 		case '\t':
9235331Samw 			*path = '_';
9245331Samw 			break;
9255331Samw 		}
9265331Samw 		path++;
9275331Samw 	}
9285331Samw }
9295331Samw 
9305331Samw /*
9315331Samw  * name_adjust(path, count)
9325331Samw  *
9335331Samw  * Add a ~<count> in place of last few characters. The total number of
9345331Samw  * characters is dependent on count.
9355331Samw  */
9365331Samw #define	MAX_MANGLE_NUMBER	10000
9375331Samw 
9385331Samw static int
9395331Samw name_adjust(char *path, int count)
9405331Samw {
9415331Samw 	size_t len;
9425331Samw 
9435331Samw 	len = strlen(path) - 2;
9445331Samw 	if (count > 10)
9455331Samw 		len--;
9465331Samw 	if (count > 100)
9475331Samw 		len--;
9485331Samw 	if (count > 1000)
9495331Samw 		len--;
9505331Samw 	if (len > 0)
9515331Samw 		(void) sprintf(path + len, "~%d", count);
9525331Samw 	else
9535331Samw 		return (SA_BAD_VALUE);
9545331Samw 
9555331Samw 	return (SA_OK);
9565331Samw }
9575331Samw 
9585331Samw /*
9595331Samw  * make_resources(group)
9605331Samw  *
9615331Samw  * Go through all the shares in the group and make them have resource
9625331Samw  * names.
9635331Samw  */
9645331Samw static void
9655331Samw make_resources(sa_group_t group)
9665331Samw {
9675331Samw 	sa_share_t share;
9685331Samw 	int count;
9695331Samw 	int err = SA_OK;
9705331Samw 
9715331Samw 	for (share = sa_get_share(group, NULL); share != NULL;
9725331Samw 	    share = sa_get_next_share(share)) {
9735331Samw 		/* Skip those with resources */
9745331Samw 		if (sa_get_share_resource(share, NULL) == NULL) {
9755331Samw 			char *path;
9765331Samw 			path = sa_get_share_attr(share, "path");
9775331Samw 			if (path == NULL)
9785331Samw 				continue;
9795331Samw 			fix_path(path);
9805331Samw 			count = 0;	/* reset for next resource */
9815331Samw 			while (sa_add_resource(share, path,
9825331Samw 			    SA_SHARE_PERMANENT, &err) == NULL &&
9835331Samw 			    err == SA_DUPLICATE_NAME) {
9845331Samw 				int ret;
9855331Samw 				ret = name_adjust(path, count);
9865331Samw 				count++;
9875331Samw 				if (ret != SA_OK ||
9885331Samw 				    count >= MAX_MANGLE_NUMBER) {
9895331Samw 					(void) printf(gettext(
9905331Samw 					    "Cannot create resource name for"
9915331Samw 					    " path: %s\n"), path);
9925331Samw 					break;
9935331Samw 				}
9945331Samw 			}
9955331Samw 			sa_free_attr_string(path);
9965331Samw 		}
9975331Samw 	}
9985331Samw }
9995331Samw 
10005331Samw /*
10013034Sdougm  * sa_create(flags, argc, argv)
10023034Sdougm  *	create a new group
10033034Sdougm  *	this may or may not have a protocol associated with it.
10043034Sdougm  *	No protocol means "all" protocols in this case.
10053034Sdougm  */
10063034Sdougm static int
10073910Sdougm sa_create(sa_handle_t handle, int flags, int argc, char *argv[])
10083034Sdougm {
10093034Sdougm 	char *groupname;
10103034Sdougm 
10113034Sdougm 	sa_group_t group;
10125331Samw 	int force = 0;
10133034Sdougm 	int verbose = 0;
10143034Sdougm 	int dryrun = 0;
10153034Sdougm 	int c;
10163034Sdougm 	char *protocol = NULL;
10173034Sdougm 	int ret = SA_OK;
10183034Sdougm 	struct options *optlist = NULL;
10193034Sdougm 	int err = 0;
10203034Sdougm 	int auth;
10213034Sdougm 
10225331Samw 	while ((c = getopt(argc, argv, "?fhvnP:p:")) != EOF) {
10234653Sdougm 		switch (c) {
10245331Samw 		case 'f':
10255331Samw 			force++;
10265331Samw 			break;
10274653Sdougm 		case 'v':
10284653Sdougm 			verbose++;
10294653Sdougm 			break;
10304653Sdougm 		case 'n':
10314653Sdougm 			dryrun++;
10324653Sdougm 			break;
10334653Sdougm 		case 'P':
10345331Samw 			if (protocol != NULL) {
10355331Samw 				(void) printf(gettext("Specifying "
10365331Samw 				    "multiple protocols "
10375331Samw 				    "not supported: %s\n"), protocol);
10385331Samw 				return (SA_SYNTAX_ERR);
10395331Samw 			}
10404653Sdougm 			protocol = optarg;
10414653Sdougm 			if (sa_valid_protocol(protocol))
10424653Sdougm 				break;
10434653Sdougm 			(void) printf(gettext(
10444653Sdougm 			    "Invalid protocol specified: %s\n"), protocol);
10454653Sdougm 			return (SA_INVALID_PROTOCOL);
10464653Sdougm 			break;
10474653Sdougm 		case 'p':
10484653Sdougm 			ret = add_opt(&optlist, optarg, 0);
10494653Sdougm 			switch (ret) {
10504653Sdougm 			case OPT_ADD_SYNTAX:
10514653Sdougm 				(void) printf(gettext(
10524653Sdougm 				    "Property syntax error for property: %s\n"),
10534653Sdougm 				    optarg);
10544653Sdougm 				return (SA_SYNTAX_ERR);
10554653Sdougm 			case OPT_ADD_SECURITY:
10564653Sdougm 				(void) printf(gettext(
10574653Sdougm 				    "Security properties need "
10584653Sdougm 				    "to be set with set-security: %s\n"),
10594653Sdougm 				    optarg);
10604653Sdougm 				return (SA_SYNTAX_ERR);
10614653Sdougm 			default:
10624653Sdougm 				break;
10634653Sdougm 			}
10644653Sdougm 			break;
10654653Sdougm 		default:
10664653Sdougm 		case 'h':
10674653Sdougm 		case '?':
10684653Sdougm 			(void) printf(gettext("usage: %s\n"),
10694653Sdougm 			    sa_get_usage(USAGE_CREATE));
10704653Sdougm 			return (0);
10713034Sdougm 		}
10723034Sdougm 	}
10733034Sdougm 
10743034Sdougm 	if (optind >= argc) {
10754653Sdougm 		(void) printf(gettext("usage: %s\n"),
10764653Sdougm 		    sa_get_usage(USAGE_CREATE));
10774653Sdougm 		(void) printf(gettext("\tgroup must be specified.\n"));
10784653Sdougm 		return (SA_BAD_PATH);
10793034Sdougm 	}
10803034Sdougm 
10813034Sdougm 	if ((optind + 1) < argc) {
10824653Sdougm 		(void) printf(gettext("usage: %s\n"),
10834653Sdougm 		    sa_get_usage(USAGE_CREATE));
10844653Sdougm 		(void) printf(gettext("\textraneous group(s) at end\n"));
10854653Sdougm 		return (SA_SYNTAX_ERR);
10863034Sdougm 	}
10873034Sdougm 
10883034Sdougm 	if (protocol == NULL && optlist != NULL) {
10894653Sdougm 		/* lookup default protocol */
10904653Sdougm 		(void) printf(gettext("usage: %s\n"),
10914653Sdougm 		    sa_get_usage(USAGE_CREATE));
10924653Sdougm 		(void) printf(gettext("\tprotocol must be specified "
10934653Sdougm 		    "with properties\n"));
10944653Sdougm 		return (SA_INVALID_PROTOCOL);
10953034Sdougm 	}
10963034Sdougm 
10973034Sdougm 	if (optlist != NULL)
10984653Sdougm 		ret = chk_opt(optlist, 0, protocol);
10993034Sdougm 	if (ret == OPT_ADD_SECURITY) {
11004653Sdougm 		(void) printf(gettext("Security properties not "
11014653Sdougm 		    "supported with create\n"));
11024653Sdougm 		return (SA_SYNTAX_ERR);
11033034Sdougm 	}
11043034Sdougm 
11053034Sdougm 	/*
11064653Sdougm 	 * If a group already exists, we can only add a new protocol
11073034Sdougm 	 * to it and not create a new one or add the same protocol
11083034Sdougm 	 * again.
11093034Sdougm 	 */
11103034Sdougm 
11113034Sdougm 	groupname = argv[optind];
11123034Sdougm 
11133034Sdougm 	auth = check_authorizations(groupname, flags);
11143034Sdougm 
11153910Sdougm 	group = sa_get_group(handle, groupname);
11163034Sdougm 	if (group != NULL) {
11174653Sdougm 		/* group exists so must be a protocol add */
11184653Sdougm 		if (protocol != NULL) {
11194653Sdougm 			if (has_protocol(group, protocol)) {
11204653Sdougm 				(void) printf(gettext(
11214653Sdougm 				    "Group \"%s\" already exists"
11224653Sdougm 				    " with protocol %s\n"), groupname,
11234653Sdougm 				    protocol);
11244653Sdougm 				ret = SA_DUPLICATE_NAME;
11254653Sdougm 			}
11264653Sdougm 		} else {
11274653Sdougm 			/* must add new protocol */
11284653Sdougm 			(void) printf(gettext(
11294653Sdougm 			    "Group already exists and no protocol "
11304653Sdougm 			    "specified.\n"));
11314653Sdougm 			ret = SA_DUPLICATE_NAME;
11323034Sdougm 		}
11333034Sdougm 	} else {
11343034Sdougm 		/*
11353034Sdougm 		 * is it a valid name? Must comply with SMF instance
11363034Sdougm 		 * name restrictions.
11373034Sdougm 		 */
11384653Sdougm 		if (!sa_valid_group_name(groupname)) {
11394653Sdougm 			ret = SA_INVALID_NAME;
11404653Sdougm 			(void) printf(gettext("Invalid group name: %s\n"),
11414653Sdougm 			    groupname);
11424653Sdougm 		}
11433034Sdougm 	}
11443034Sdougm 	if (ret == SA_OK) {
11454653Sdougm 		/* check protocol vs optlist */
11464653Sdougm 		if (optlist != NULL) {
11474653Sdougm 			/* check options, if any, for validity */
11484653Sdougm 			ret = valid_options(optlist, protocol, group, NULL);
11494653Sdougm 		}
11503034Sdougm 	}
11513034Sdougm 	if (ret == SA_OK && !dryrun) {
11524653Sdougm 		if (group == NULL) {
11534653Sdougm 			group = sa_create_group(handle, (char *)groupname,
11544653Sdougm 			    &err);
11553034Sdougm 		}
11564653Sdougm 		if (group != NULL) {
11574653Sdougm 			sa_optionset_t optionset;
11585331Samw 			/*
11595331Samw 			 * First check to see if the new protocol is one that
11605331Samw 			 * requires resource names and make sure we are
11615331Samw 			 * compliant before proceeding.
11625331Samw 			 */
11635331Samw 			if (protocol != NULL) {
11645331Samw 				uint64_t features;
11655331Samw 
11665331Samw 				features = sa_proto_get_featureset(protocol);
11675331Samw 				if ((features & SA_FEATURE_RESOURCE) &&
11685331Samw 				    !resource_compliant(group)) {
11695331Samw 					if (force) {
11705331Samw 						make_resources(group);
11715331Samw 					} else {
11725331Samw 						ret = SA_RESOURCE_REQUIRED;
11735331Samw 						(void) printf(
11745331Samw 						    gettext("Protocol "
11755331Samw 						    "requires resource "
11765331Samw 						    "names to be "
11775331Samw 						    "set: %s\n"),
11785331Samw 						    protocol);
11795331Samw 						goto err;
11805331Samw 					}
11815331Samw 				}
11825331Samw 			}
11834653Sdougm 			if (optlist != NULL) {
11844653Sdougm 				(void) add_optionset(group, optlist, protocol,
11854653Sdougm 				    &ret);
11864653Sdougm 			} else if (protocol != NULL) {
11874653Sdougm 				optionset = sa_create_optionset(group,
11884653Sdougm 				    protocol);
11894653Sdougm 				if (optionset == NULL)
11904653Sdougm 					ret = SA_NO_MEMORY;
11914653Sdougm 			} else if (protocol == NULL) {
11924653Sdougm 				char **protolist;
11934653Sdougm 				int numprotos, i;
11944653Sdougm 				numprotos = sa_get_protocols(&protolist);
11954653Sdougm 				for (i = 0; i < numprotos; i++) {
11964653Sdougm 					optionset = sa_create_optionset(group,
11974653Sdougm 					    protolist[i]);
11984653Sdougm 				}
11994653Sdougm 				if (protolist != NULL)
12004653Sdougm 					free(protolist);
12014653Sdougm 			}
12023034Sdougm 			/*
12034653Sdougm 			 * We have a group and legal additions
12043034Sdougm 			 */
12054653Sdougm 			if (ret == SA_OK) {
12064653Sdougm 				/*
12074653Sdougm 				 * Commit to configuration for protocols that
12084653Sdougm 				 * need to do block updates. For NFS, this
12094653Sdougm 				 * doesn't do anything but it will be run for
12104653Sdougm 				 * all protocols that implement the
12114653Sdougm 				 * appropriate plugin.
12124653Sdougm 				 */
12134653Sdougm 				ret = sa_update_config(handle);
12144653Sdougm 			} else {
12154653Sdougm 				if (group != NULL)
12164653Sdougm 					(void) sa_remove_group(group);
12174653Sdougm 			}
12183034Sdougm 		} else {
12194653Sdougm 			ret = err;
12204653Sdougm 			(void) printf(gettext("Could not create group: %s\n"),
12214653Sdougm 			    sa_errorstr(ret));
12223034Sdougm 		}
12233034Sdougm 	}
12243034Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose) {
12254653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
12264653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
12274653Sdougm 		ret = SA_NO_PERMISSION;
12283034Sdougm 	}
12295331Samw err:
12303034Sdougm 	free_opt(optlist);
12313034Sdougm 	return (ret);
12323034Sdougm }
12333034Sdougm 
12343034Sdougm /*
12353034Sdougm  * group_status(group)
12363034Sdougm  *
12373034Sdougm  * return the current status (enabled/disabled) of the group.
12383034Sdougm  */
12393034Sdougm 
12403034Sdougm static char *
12413034Sdougm group_status(sa_group_t group)
12423034Sdougm {
12433034Sdougm 	char *state;
12443034Sdougm 	int enabled = 0;
12453034Sdougm 
12463034Sdougm 	state = sa_get_group_attr(group, "state");
12473034Sdougm 	if (state != NULL) {
12484653Sdougm 		if (strcmp(state, "enabled") == 0) {
12494653Sdougm 			enabled = 1;
12504653Sdougm 		}
12514653Sdougm 		sa_free_attr_string(state);
12523034Sdougm 	}
12534255Sdougm 	return (enabled ? "enabled" : "disabled");
12543034Sdougm }
12553034Sdougm 
12563034Sdougm /*
12573034Sdougm  * sa_delete(flags, argc, argv)
12583034Sdougm  *
12593034Sdougm  *	Delete a group.
12603034Sdougm  */
12613034Sdougm 
12623034Sdougm static int
12633910Sdougm sa_delete(sa_handle_t handle, int flags, int argc, char *argv[])
12643034Sdougm {
12653034Sdougm 	char *groupname;
12663034Sdougm 	sa_group_t group;
12673034Sdougm 	sa_share_t share;
12683034Sdougm 	int verbose = 0;
12693034Sdougm 	int dryrun = 0;
12703034Sdougm 	int force = 0;
12713034Sdougm 	int c;
12723034Sdougm 	char *protocol = NULL;
12733034Sdougm 	char *sectype = NULL;
12743034Sdougm 	int ret = SA_OK;
12753034Sdougm 	int auth;
12763034Sdougm 
12773034Sdougm 	while ((c = getopt(argc, argv, "?hvnP:fS:")) != EOF) {
12784653Sdougm 		switch (c) {
12794653Sdougm 		case 'v':
12804653Sdougm 			verbose++;
12814653Sdougm 			break;
12824653Sdougm 		case 'n':
12834653Sdougm 			dryrun++;
12844653Sdougm 			break;
12854653Sdougm 		case 'P':
12865331Samw 			if (protocol != NULL) {
12875331Samw 				(void) printf(gettext("Specifying "
12885331Samw 				    "multiple protocols "
12895331Samw 				    "not supported: %s\n"), protocol);
12905331Samw 				return (SA_SYNTAX_ERR);
12915331Samw 			}
12924653Sdougm 			protocol = optarg;
12934653Sdougm 			if (!sa_valid_protocol(protocol)) {
12944653Sdougm 				(void) printf(gettext("Invalid protocol "
12955331Samw 				    "specified: %s\n"), protocol);
12964653Sdougm 				return (SA_INVALID_PROTOCOL);
12974653Sdougm 			}
12984653Sdougm 			break;
12994653Sdougm 		case 'S':
13005331Samw 			if (sectype != NULL) {
13015331Samw 				(void) printf(gettext("Specifying "
13025331Samw 				    "multiple property "
13035331Samw 				    "spaces not supported: %s\n"), sectype);
13045331Samw 				return (SA_SYNTAX_ERR);
13055331Samw 			}
13064653Sdougm 			sectype = optarg;
13074653Sdougm 			break;
13084653Sdougm 		case 'f':
13094653Sdougm 			force++;
13104653Sdougm 			break;
13114653Sdougm 		default:
13124653Sdougm 		case 'h':
13134653Sdougm 		case '?':
13144653Sdougm 			(void) printf(gettext("usage: %s\n"),
13154653Sdougm 			    sa_get_usage(USAGE_DELETE));
13164653Sdougm 			return (0);
13173034Sdougm 		}
13183034Sdougm 	}
13193034Sdougm 
13203034Sdougm 	if (optind >= argc) {
13214653Sdougm 		(void) printf(gettext("usage: %s\n"),
13224653Sdougm 		    sa_get_usage(USAGE_DELETE));
13234653Sdougm 		(void) printf(gettext("\tgroup must be specified.\n"));
13244653Sdougm 		return (SA_SYNTAX_ERR);
13253034Sdougm 	}
13263034Sdougm 
13273034Sdougm 	if ((optind + 1) < argc) {
13284653Sdougm 		(void) printf(gettext("usage: %s\n"),
13294653Sdougm 		    sa_get_usage(USAGE_DELETE));
13304653Sdougm 		(void) printf(gettext("\textraneous group(s) at end\n"));
13314653Sdougm 		return (SA_SYNTAX_ERR);
13323034Sdougm 	}
13333034Sdougm 
13343034Sdougm 	if (sectype != NULL && protocol == NULL) {
13354653Sdougm 		(void) printf(gettext("usage: %s\n"),
13364653Sdougm 		    sa_get_usage(USAGE_DELETE));
13374653Sdougm 		(void) printf(gettext("\tsecurity requires protocol to be "
13384653Sdougm 		    "specified.\n"));
13394653Sdougm 		return (SA_SYNTAX_ERR);
13403034Sdougm 	}
13413034Sdougm 
13423034Sdougm 	/*
13433034Sdougm 	 * Determine if the group already exists since it must in
13443034Sdougm 	 * order to be removed.
13453034Sdougm 	 *
13463034Sdougm 	 * We can delete when:
13473034Sdougm 	 *
13483034Sdougm 	 *	- group is empty
13493034Sdougm 	 *	- force flag is set
13503034Sdougm 	 *	- if protocol specified, only delete the protocol
13513034Sdougm 	 */
13523034Sdougm 
13533034Sdougm 	groupname = argv[optind];
13543910Sdougm 	group = sa_get_group(handle, groupname);
13553034Sdougm 	if (group == NULL) {
13563034Sdougm 		ret = SA_NO_SUCH_GROUP;
13574653Sdougm 		goto done;
13584653Sdougm 	}
13594653Sdougm 	auth = check_authorizations(groupname, flags);
13604653Sdougm 	if (protocol == NULL) {
13613034Sdougm 		share = sa_get_share(group, NULL);
13623034Sdougm 		if (share != NULL)
13634653Sdougm 			ret = SA_BUSY;
13643034Sdougm 		if (share == NULL || (share != NULL && force == 1)) {
13654653Sdougm 			ret = SA_OK;
13664653Sdougm 			if (!dryrun) {
13674653Sdougm 				while (share != NULL) {
13684653Sdougm 					sa_share_t next_share;
13694653Sdougm 					next_share = sa_get_next_share(share);
13704653Sdougm 					/*
13714653Sdougm 					 * need to do the disable of
13724653Sdougm 					 * each share, but don't
13734653Sdougm 					 * actually do anything on a
13744653Sdougm 					 * dryrun.
13754653Sdougm 					 */
13764653Sdougm 					ret = sa_disable_share(share, NULL);
13774653Sdougm 					ret = sa_remove_share(share);
13784653Sdougm 					share = next_share;
13794653Sdougm 				}
13804653Sdougm 				ret = sa_remove_group(group);
13813034Sdougm 			}
13823034Sdougm 		}
13834653Sdougm 		/* Commit to configuration if not a dryrun */
13843034Sdougm 		if (!dryrun && ret == SA_OK) {
13854653Sdougm 			ret = sa_update_config(handle);
13863034Sdougm 		}
13874653Sdougm 	} else {
13883034Sdougm 		/* a protocol delete */
13893034Sdougm 		sa_optionset_t optionset;
13903034Sdougm 		sa_security_t security;
13915331Samw 		if (sectype != NULL) {
13924653Sdougm 			/* only delete specified security */
13934653Sdougm 			security = sa_get_security(group, sectype, protocol);
13944653Sdougm 			if (security != NULL && !dryrun)
13954653Sdougm 				ret = sa_destroy_security(security);
13964653Sdougm 			else
13974653Sdougm 				ret = SA_INVALID_PROTOCOL;
13983034Sdougm 		} else {
13994653Sdougm 			optionset = sa_get_optionset(group, protocol);
14004653Sdougm 			if (optionset != NULL && !dryrun) {
14014653Sdougm 				/*
14024653Sdougm 				 * have an optionset with
14034653Sdougm 				 * protocol to delete
14044653Sdougm 				 */
14054653Sdougm 				ret = sa_destroy_optionset(optionset);
14064653Sdougm 				/*
14074653Sdougm 				 * Now find all security sets
14084653Sdougm 				 * for the protocol and remove
14094653Sdougm 				 * them. Don't remove other
14104653Sdougm 				 * protocols.
14114653Sdougm 				 */
14124653Sdougm 				for (security =
14134653Sdougm 				    sa_get_security(group, NULL, NULL);
14144653Sdougm 				    ret == SA_OK && security != NULL;
14154653Sdougm 				    security = sa_get_next_security(security)) {
14164653Sdougm 					char *secprot;
14174653Sdougm 					secprot = sa_get_security_attr(security,
14184653Sdougm 					    "type");
14194653Sdougm 					if (secprot != NULL &&
14204653Sdougm 					    strcmp(secprot, protocol) == 0)
14214653Sdougm 						ret = sa_destroy_security(
14224653Sdougm 						    security);
14234653Sdougm 					if (secprot != NULL)
14244653Sdougm 						sa_free_attr_string(secprot);
14254653Sdougm 				}
14264653Sdougm 			} else {
14274653Sdougm 				if (!dryrun)
14284653Sdougm 					ret = SA_INVALID_PROTOCOL;
14293034Sdougm 			}
14303034Sdougm 		}
14315331Samw 		/*
14325331Samw 		 * With the protocol items removed, make sure that all
14335331Samw 		 * the shares are updated in the legacy files, if
14345331Samw 		 * necessary.
14355331Samw 		 */
14365331Samw 		for (share = sa_get_share(group, NULL);
14375331Samw 		    share != NULL;
14385331Samw 		    share = sa_get_next_share(share)) {
14395331Samw 			(void) sa_delete_legacy(share, protocol);
14405331Samw 		}
14413034Sdougm 	}
14424653Sdougm 
14434653Sdougm done:
14443034Sdougm 	if (ret != SA_OK) {
14454653Sdougm 		(void) printf(gettext("Could not delete group: %s\n"),
14464653Sdougm 		    sa_errorstr(ret));
14473034Sdougm 	} else if (dryrun && !auth && verbose) {
14484653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
14494653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
14503034Sdougm 	}
14513034Sdougm 	return (ret);
14523034Sdougm }
14533034Sdougm 
14543034Sdougm /*
14553034Sdougm  * strndupr(*buff, str, buffsize)
14563034Sdougm  *
14573034Sdougm  * used with small strings to duplicate and possibly increase the
14583034Sdougm  * buffer size of a string.
14593034Sdougm  */
14603034Sdougm static char *
14613034Sdougm strndupr(char *buff, char *str, int *buffsize)
14623034Sdougm {
14633034Sdougm 	int limit;
14643034Sdougm 	char *orig_buff = buff;
14653034Sdougm 
14663034Sdougm 	if (buff == NULL) {
14674653Sdougm 		buff = (char *)malloc(64);
14684653Sdougm 		if (buff == NULL)
14694653Sdougm 			return (NULL);
14704653Sdougm 		*buffsize = 64;
14714653Sdougm 		buff[0] = '\0';
14723034Sdougm 	}
14733034Sdougm 	limit = strlen(buff) + strlen(str) + 1;
14743034Sdougm 	if (limit > *buffsize) {
14754653Sdougm 		limit = *buffsize = *buffsize + ((limit / 64) + 64);
14764653Sdougm 		buff = realloc(buff, limit);
14773034Sdougm 	}
14783034Sdougm 	if (buff != NULL) {
14794653Sdougm 		(void) strcat(buff, str);
14803034Sdougm 	} else {
14814653Sdougm 		/* if it fails, fail it hard */
14824653Sdougm 		if (orig_buff != NULL)
14834653Sdougm 			free(orig_buff);
14843034Sdougm 	}
14853034Sdougm 	return (buff);
14863034Sdougm }
14873034Sdougm 
14883034Sdougm /*
14893034Sdougm  * group_proto(group)
14903034Sdougm  *
14913034Sdougm  * return a string of all the protocols (space separated) associated
14923034Sdougm  * with this group.
14933034Sdougm  */
14943034Sdougm 
14953034Sdougm static char *
14963034Sdougm group_proto(sa_group_t group)
14973034Sdougm {
14983034Sdougm 	sa_optionset_t optionset;
14993034Sdougm 	char *proto;
15003034Sdougm 	char *buff = NULL;
15013034Sdougm 	int buffsize = 0;
15023034Sdougm 	int addspace = 0;
15033034Sdougm 	/*
15043034Sdougm 	 * get the protocol list by finding the optionsets on this
15053034Sdougm 	 * group and extracting the type value. The initial call to
15063034Sdougm 	 * strndupr() initailizes buff.
15073034Sdougm 	 */
15083034Sdougm 	buff = strndupr(buff, "", &buffsize);
15093034Sdougm 	if (buff != NULL) {
15104653Sdougm 		for (optionset = sa_get_optionset(group, NULL);
15114653Sdougm 		    optionset != NULL && buff != NULL;
15124653Sdougm 		    optionset = sa_get_next_optionset(optionset)) {
15134653Sdougm 			/*
15144653Sdougm 			 * extract out the protocol type from this optionset
15154653Sdougm 			 * and append it to the buffer "buff". strndupr() will
15164653Sdougm 			 * reallocate space as necessay.
15174653Sdougm 			 */
15184653Sdougm 			proto = sa_get_optionset_attr(optionset, "type");
15194653Sdougm 			if (proto != NULL) {
15204653Sdougm 				if (addspace++)
15214653Sdougm 					buff = strndupr(buff, " ", &buffsize);
15224653Sdougm 				buff = strndupr(buff, proto, &buffsize);
15234653Sdougm 				sa_free_attr_string(proto);
15244653Sdougm 			}
15253034Sdougm 		}
15263034Sdougm 	}
15273034Sdougm 	return (buff);
15283034Sdougm }
15293034Sdougm 
15303034Sdougm /*
15313034Sdougm  * sa_list(flags, argc, argv)
15323034Sdougm  *
15333034Sdougm  * implements the "list" subcommand to list groups and optionally
15343034Sdougm  * their state and protocols.
15353034Sdougm  */
15363034Sdougm 
15373034Sdougm static int
15383910Sdougm sa_list(sa_handle_t handle, int flags, int argc, char *argv[])
15393034Sdougm {
15403034Sdougm 	sa_group_t group;
15413034Sdougm 	int verbose = 0;
15423034Sdougm 	int c;
15433034Sdougm 	char *protocol = NULL;
15445331Samw #ifdef lint
15455331Samw 	flags = flags;
15465331Samw #endif
15473034Sdougm 
15483034Sdougm 	while ((c = getopt(argc, argv, "?hvP:")) != EOF) {
15494653Sdougm 		switch (c) {
15504653Sdougm 		case 'v':
15514653Sdougm 			verbose++;
15524653Sdougm 			break;
15534653Sdougm 		case 'P':
15545331Samw 			if (protocol != NULL) {
15555331Samw 				(void) printf(gettext(
15565331Samw 				    "Specifying multiple protocols "
15575331Samw 				    "not supported: %s\n"),
15585331Samw 				    protocol);
15595331Samw 				return (SA_SYNTAX_ERR);
15605331Samw 			}
15614653Sdougm 			protocol = optarg;
15624653Sdougm 			if (!sa_valid_protocol(protocol)) {
15634653Sdougm 				(void) printf(gettext(
15644653Sdougm 				    "Invalid protocol specified: %s\n"),
15654653Sdougm 				    protocol);
15664653Sdougm 				return (SA_INVALID_PROTOCOL);
15674653Sdougm 			}
15684653Sdougm 			break;
15694653Sdougm 		default:
15704653Sdougm 		case 'h':
15714653Sdougm 		case '?':
15724653Sdougm 			(void) printf(gettext("usage: %s\n"),
15734653Sdougm 			    sa_get_usage(USAGE_LIST));
15744653Sdougm 			return (0);
15753034Sdougm 		}
15763034Sdougm 	}
15773034Sdougm 
15784653Sdougm 	for (group = sa_get_group(handle, NULL);
15794653Sdougm 	    group != NULL;
15803034Sdougm 	    group = sa_get_next_group(group)) {
15814653Sdougm 		char *name;
15824653Sdougm 		char *proto;
15834653Sdougm 		if (protocol == NULL || has_protocol(group, protocol)) {
15844653Sdougm 			name = sa_get_group_attr(group, "name");
15854653Sdougm 			if (name != NULL && (verbose > 1 || name[0] != '#')) {
15864653Sdougm 				(void) printf("%s", (char *)name);
15874653Sdougm 				if (verbose) {
15884653Sdougm 					/*
15894653Sdougm 					 * Need the list of protocols
15904653Sdougm 					 * and current status once
15914653Sdougm 					 * available. We do want to
15924653Sdougm 					 * translate the
15934653Sdougm 					 * enabled/disabled text here.
15944653Sdougm 					 */
15954653Sdougm 					(void) printf("\t%s", isenabled(group) ?
15964653Sdougm 					    gettext("enabled") :
15974653Sdougm 					    gettext("disabled"));
15984653Sdougm 					proto = group_proto(group);
15994653Sdougm 					if (proto != NULL) {
16004653Sdougm 						(void) printf("\t%s",
16014653Sdougm 						    (char *)proto);
16024653Sdougm 						free(proto);
16034653Sdougm 					}
16044653Sdougm 				}
16054653Sdougm 				(void) printf("\n");
16063034Sdougm 			}
16074653Sdougm 			if (name != NULL)
16084653Sdougm 				sa_free_attr_string(name);
16093034Sdougm 		}
16103034Sdougm 	}
16113034Sdougm 	return (0);
16123034Sdougm }
16133034Sdougm 
16143034Sdougm /*
16153034Sdougm  * out_properties(optionset, proto, sec)
16163034Sdougm  *
16173034Sdougm  * Format the properties and encode the protocol and optional named
16183034Sdougm  * optionset into the string.
16193034Sdougm  *
16203034Sdougm  * format is protocol[:name]=(property-list)
16213034Sdougm  */
16223034Sdougm 
16233034Sdougm static void
16243034Sdougm out_properties(sa_optionset_t optionset, char *proto, char *sec)
16253034Sdougm {
16263034Sdougm 	char *type;
16273034Sdougm 	char *value;
16283034Sdougm 	int spacer;
16293034Sdougm 	sa_property_t prop;
16303034Sdougm 
16314653Sdougm 	if (sec == NULL)
16324653Sdougm 		(void) printf(" %s=(", proto ? proto : gettext("all"));
16334653Sdougm 	else
16344653Sdougm 		(void) printf(" %s:%s=(", proto ? proto : gettext("all"), sec);
16353034Sdougm 
16363034Sdougm 	for (spacer = 0, prop = sa_get_property(optionset, NULL);
16374653Sdougm 	    prop != NULL;
16384653Sdougm 	    prop = sa_get_next_property(prop)) {
16393034Sdougm 
16403034Sdougm 		/*
16413034Sdougm 		 * extract the property name/value and output with
16423034Sdougm 		 * appropriate spacing. I.e. no prefixed space the
16433034Sdougm 		 * first time through but a space on subsequent
16443034Sdougm 		 * properties.
16453034Sdougm 		 */
16464653Sdougm 		type = sa_get_property_attr(prop, "type");
16474653Sdougm 		value = sa_get_property_attr(prop, "value");
16484653Sdougm 		if (type != NULL) {
16494653Sdougm 			(void) printf("%s%s=", spacer ? " " : "",	type);
16504653Sdougm 			spacer = 1;
16514653Sdougm 			if (value != NULL)
16524653Sdougm 				(void) printf("\"%s\"", value);
16534653Sdougm 			else
16544653Sdougm 				(void) printf("\"\"");
16554653Sdougm 		}
16564653Sdougm 		if (type != NULL)
16574653Sdougm 			sa_free_attr_string(type);
16583034Sdougm 		if (value != NULL)
16594653Sdougm 			sa_free_attr_string(value);
16603034Sdougm 	}
16613034Sdougm 	(void) printf(")");
16623034Sdougm }
16633034Sdougm 
16643034Sdougm /*
16653034Sdougm  * show_properties(group, protocol, prefix)
16663034Sdougm  *
16673034Sdougm  * print the properties for a group. If protocol is NULL, do all
16683034Sdougm  * protocols otherwise only the specified protocol. All security
16693034Sdougm  * (named groups specific to the protocol) are included.
16703034Sdougm  *
16713034Sdougm  * The "prefix" is always applied. The caller knows whether it wants
16723034Sdougm  * some type of prefix string (white space) or not.  Once the prefix
16733034Sdougm  * has been output, it is reduced to the zero length string for the
16743034Sdougm  * remainder of the property output.
16753034Sdougm  */
16763034Sdougm 
16773034Sdougm static void
16783034Sdougm show_properties(sa_group_t group, char *protocol, char *prefix)
16793034Sdougm {
16803034Sdougm 	sa_optionset_t optionset;
16813034Sdougm 	sa_security_t security;
16823034Sdougm 	char *value;
16833034Sdougm 	char *secvalue;
16843034Sdougm 
16853034Sdougm 	if (protocol != NULL) {
16864653Sdougm 		optionset = sa_get_optionset(group, protocol);
16874653Sdougm 		if (optionset != NULL) {
16884653Sdougm 			(void) printf("%s", prefix);
16894653Sdougm 			prefix = "";
16904653Sdougm 			out_properties(optionset, protocol, NULL);
16914653Sdougm 		}
16924653Sdougm 		security = sa_get_security(group, protocol, NULL);
16934653Sdougm 		if (security != NULL) {
16944653Sdougm 			(void) printf("%s", prefix);
16954653Sdougm 			prefix = "";
16964653Sdougm 			out_properties(security, protocol, NULL);
16974653Sdougm 		}
16983034Sdougm 	} else {
16994653Sdougm 		for (optionset = sa_get_optionset(group, protocol);
17004653Sdougm 		    optionset != NULL;
17014653Sdougm 		    optionset = sa_get_next_optionset(optionset)) {
17024653Sdougm 
17034653Sdougm 			value = sa_get_optionset_attr(optionset, "type");
17044653Sdougm 			(void) printf("%s", prefix);
17054653Sdougm 			prefix = "";
17064653Sdougm 			out_properties(optionset, value, 0);
17074653Sdougm 			if (value != NULL)
17084653Sdougm 				sa_free_attr_string(value);
17094653Sdougm 		}
17104653Sdougm 		for (security = sa_get_security(group, NULL, protocol);
17114653Sdougm 		    security != NULL;
17124653Sdougm 		    security = sa_get_next_security(security)) {
17134653Sdougm 
17144653Sdougm 			value = sa_get_security_attr(security, "type");
17154653Sdougm 			secvalue = sa_get_security_attr(security, "sectype");
17164653Sdougm 			(void) printf("%s", prefix);
17174653Sdougm 			prefix = "";
17184653Sdougm 			out_properties(security, value, secvalue);
17194653Sdougm 			if (value != NULL)
17204653Sdougm 				sa_free_attr_string(value);
17214653Sdougm 			if (secvalue != NULL)
17224653Sdougm 				sa_free_attr_string(secvalue);
17234653Sdougm 		}
17243034Sdougm 	}
17253034Sdougm }
17263034Sdougm 
17273034Sdougm /*
17285331Samw  * get_resource(share)
17295331Samw  *
17305331Samw  * Get the first resource name, if any, and fix string to be in
17315331Samw  * current locale and have quotes if it has embedded spaces.  Return
17325331Samw  * an attr string that must be freed.
17335331Samw  */
17345331Samw 
17355331Samw static char *
17365331Samw get_resource(sa_share_t share)
17375331Samw {
17385331Samw 	sa_resource_t resource;
17395331Samw 	char *resstring = NULL;
17405331Samw 	char *retstring;
17415331Samw 
17425331Samw 	if ((resource = sa_get_share_resource(share, NULL)) != NULL) {
17435331Samw 		resstring = sa_get_resource_attr(resource, "name");
17445331Samw 		if (resstring != NULL) {
17455331Samw 			char *cp;
17465331Samw 			int len;
17475331Samw 
17485331Samw 			retstring = conv_from_utf8(resstring);
17495331Samw 			if (retstring != resstring) {
17505331Samw 				sa_free_attr_string(resstring);
17515331Samw 				resstring = retstring;
17525331Samw 			}
17535331Samw 			if (strpbrk(resstring, " ") != NULL) {
17545331Samw 				/* account for quotes */
17555331Samw 				len = strlen(resstring) + 3;
17565331Samw 				cp = calloc(len, sizeof (char));
17575331Samw 				if (cp != NULL) {
17585331Samw 					(void) snprintf(cp, len,
17595331Samw 					    "\"%s\"", resstring);
17605331Samw 					sa_free_attr_string(resstring);
17615331Samw 					resstring = cp;
17625331Samw 				} else {
17635331Samw 					sa_free_attr_string(resstring);
17645331Samw 					resstring = NULL;
17655331Samw 				}
17665331Samw 			}
17675331Samw 		}
17685331Samw 	}
17695331Samw 	return (resstring);
17705331Samw }
17715331Samw 
17725331Samw /*
17735331Samw  * has_resource_with_opt(share)
17745331Samw  *
17755331Samw  * Check to see if the share has any resource names with optionsets
17765331Samw  * set. Also indicate if multiple resource names since the syntax
17775331Samw  * would be about the same.
17785331Samw  */
17795331Samw static int
17805331Samw has_resource_with_opt(sa_share_t share)
17815331Samw {
17825331Samw 	sa_resource_t resource;
17835331Samw 	int ret = B_FALSE;
17845331Samw 
17855331Samw 	for (resource = sa_get_share_resource(share, NULL);
17865331Samw 	    resource != NULL;
17875331Samw 	    resource = sa_get_next_resource(resource)) {
17885331Samw 
17895331Samw 		if (sa_get_optionset(resource, NULL) != NULL) {
17905331Samw 			ret = B_TRUE;
17915331Samw 			break;
17925331Samw 		}
17935331Samw 	}
17945331Samw 	return (ret);
17955331Samw }
17965331Samw 
17975331Samw /*
17985331Samw  * has_multiple_resource(share)
17995331Samw  *
18005331Samw  * Check to see if the share has any resource names with optionsets
18015331Samw  * set. Also indicate if multiple resource names since the syntax
18025331Samw  * would be about the same.
18035331Samw  */
18045331Samw static int
18055331Samw has_multiple_resource(sa_share_t share)
18065331Samw {
18075331Samw 	sa_resource_t resource;
18085331Samw 	int num;
18095331Samw 
18105331Samw 	for (num = 0, resource = sa_get_share_resource(share, NULL);
18115331Samw 	    resource != NULL;
18125331Samw 	    resource = sa_get_next_resource(resource)) {
18135331Samw 		num++;
18145331Samw 		if (num > 1)
18155331Samw 			return (B_TRUE);
18165331Samw 	}
18175331Samw 	return (B_FALSE);
18185331Samw }
18195331Samw 
18205331Samw /*
18215331Samw  * show_share(share, verbose, properties, proto, iszfs, sharepath)
18225331Samw  *
18235331Samw  * print out the share information. With the addition of resource as a
18245331Samw  * full object that can have multiple instances below the share, we
18255331Samw  * need to display that as well.
18265331Samw  */
18275331Samw 
18285331Samw static void
18295331Samw show_share(sa_share_t share, int verbose, int properties, char *proto,
18305331Samw     int iszfs, char *sharepath)
18315331Samw {
18325331Samw 	char *drive;
18335331Samw 	char *exclude;
18345331Samw 	sa_resource_t resource = NULL;
18355331Samw 	char *description;
18365331Samw 	char *desc;
18375331Samw 	char *rsrcname;
18385331Samw 	int rsrcwithopt;
18395331Samw 	int multiple;
18405331Samw 	char *type;
18415331Samw 
18425331Samw 	rsrcwithopt = has_resource_with_opt(share);
18435331Samw 
18445331Samw 	if (verbose || (properties && rsrcwithopt)) {
18455331Samw 		/* First, indicate if transient */
18465331Samw 		type = sa_get_share_attr(share, "type");
18475331Samw 		if (type != NULL && !iszfs && verbose &&
18485331Samw 		    strcmp(type, "transient") == 0)
18495331Samw 			(void) printf("\t* ");
18505331Samw 		else
18515331Samw 			(void) printf("\t  ");
18525331Samw 
18535331Samw 		if (type != NULL)
18545331Samw 			sa_free_attr_string(type);
18555331Samw 
18565331Samw 		/*
18575331Samw 		 * If we came in with verbose, we want to handle the case of
18585331Samw 		 * multiple resources as though they had properties set.
18595331Samw 		 */
18605331Samw 		multiple = has_multiple_resource(share);
18615331Samw 
18625331Samw 		/* Next, if not multiple follow old model */
18635331Samw 		if (!multiple && !rsrcwithopt) {
18645331Samw 			rsrcname = get_resource(share);
18655331Samw 			if (rsrcname != NULL && strlen(rsrcname) > 0) {
18665331Samw 				(void) printf("%s=%s", rsrcname, sharepath);
18675331Samw 			} else {
18685331Samw 				(void) printf("%s", sharepath);
18695331Samw 			}
18705331Samw 			if (rsrcname != NULL)
18715331Samw 				sa_free_attr_string(rsrcname);
18725331Samw 		} else {
18735331Samw 			/* Treat as simple and then resources come later */
18745331Samw 			(void) printf("%s", sharepath);
18755331Samw 		}
18765331Samw 		drive = sa_get_share_attr(share, "drive-letter");
18775331Samw 		if (drive != NULL) {
18785331Samw 			if (strlen(drive) > 0)
18795331Samw 				(void) printf(gettext("\tdrive-letter=\"%s:\""),
18805331Samw 				    drive);
18815331Samw 			sa_free_attr_string(drive);
18825331Samw 		}
18835331Samw 		if (properties)
18845331Samw 			show_properties(share, proto, "\t");
18855331Samw 		exclude = sa_get_share_attr(share, "exclude");
18865331Samw 		if (exclude != NULL) {
18875331Samw 			(void) printf(gettext("\tnot-shared-with=[%s]"),
18885331Samw 			    exclude);
18895331Samw 			sa_free_attr_string(exclude);
18905331Samw 		}
18915331Samw 		description = sa_get_share_description(share);
18925331Samw 		if (description != NULL) {
18935331Samw 			if (strlen(description) > 0) {
18945331Samw 				desc = conv_from_utf8(description);
18955331Samw 				if (desc != description) {
18965331Samw 					sa_free_share_description(description);
18975331Samw 					description = desc;
18985331Samw 				}
18995331Samw 				(void) printf("\t\"%s\"", description);
19005331Samw 			}
19015331Samw 			sa_free_share_description(description);
19025331Samw 		}
19035331Samw 
19045331Samw 		/*
19055331Samw 		 * If there are resource names with options, show them
19065331Samw 		 * here, with one line per resource. Resource specific
19075331Samw 		 * options are at the end of the line followed by
19085331Samw 		 * description, if any.
19095331Samw 		 */
19105331Samw 		if (rsrcwithopt || multiple) {
19115331Samw 			for (resource = sa_get_share_resource(share, NULL);
19125331Samw 			    resource != NULL;
19135331Samw 			    resource = sa_get_next_resource(resource)) {
19145331Samw 				int has_space;
19155331Samw 				char *rsrc;
19165331Samw 
19175331Samw 				(void) printf("\n\t\t  ");
19185331Samw 				rsrcname = sa_get_resource_attr(resource,
19195331Samw 				    "name");
19205331Samw 				if (rsrcname == NULL)
19215331Samw 					continue;
19225331Samw 
19235331Samw 				rsrc = conv_from_utf8(rsrcname);
19245331Samw 				has_space = strpbrk(rsrc, " ") != NULL;
19255331Samw 
19265331Samw 				if (has_space)
19275331Samw 					(void) printf("\"%s\"=%s", rsrc,
19285331Samw 					    sharepath);
19295331Samw 				else
19305331Samw 					(void) printf("%s=%s", rsrc,
19315331Samw 					    sharepath);
19325331Samw 				if (rsrc != rsrcname)
19335331Samw 					sa_free_attr_string(rsrc);
19345331Samw 				sa_free_attr_string(rsrcname);
19355331Samw 				if (properties || rsrcwithopt)
19365331Samw 					show_properties(resource, proto, "\t");
19375331Samw 
19385331Samw 				/* Get description string if any */
19395331Samw 				print_rsrc_desc(resource);
19405331Samw 			}
19415331Samw 		}
19425331Samw 	} else {
19435331Samw 		(void) printf("\t  %s", sharepath);
19445331Samw 		if (properties)
19455331Samw 			show_properties(share, proto, "\t");
19465331Samw 	}
19475331Samw 	(void) printf("\n");
19485331Samw }
19495331Samw 
19505331Samw /*
19513034Sdougm  * show_group(group, verbose, properties, proto, subgroup)
19523034Sdougm  *
19533034Sdougm  * helper function to show the contents of a group.
19543034Sdougm  */
19553034Sdougm 
19563034Sdougm static void
19573034Sdougm show_group(sa_group_t group, int verbose, int properties, char *proto,
19585331Samw     char *subgroup)
19593034Sdougm {
19603034Sdougm 	sa_share_t share;
19613034Sdougm 	char *groupname;
19623034Sdougm 	char *zfs = NULL;
19633034Sdougm 	int iszfs = 0;
19645331Samw 	char *sharepath;
19653034Sdougm 
19663034Sdougm 	groupname = sa_get_group_attr(group, "name");
19673034Sdougm 	if (groupname != NULL) {
19684653Sdougm 		if (proto != NULL && !has_protocol(group, proto)) {
19694653Sdougm 			sa_free_attr_string(groupname);
19704653Sdougm 			return;
19714653Sdougm 		}
19723034Sdougm 		/*
19733034Sdougm 		 * check to see if the group is managed by ZFS. If
19743034Sdougm 		 * there is an attribute, then it is. A non-NULL zfs
19753034Sdougm 		 * variable will trigger the different way to display
19763034Sdougm 		 * and will remove the transient property indicator
19773034Sdougm 		 * from the output.
19783034Sdougm 		 */
19794653Sdougm 		zfs = sa_get_group_attr(group, "zfs");
19804653Sdougm 		if (zfs != NULL) {
19814653Sdougm 			iszfs = 1;
19824653Sdougm 			sa_free_attr_string(zfs);
19833034Sdougm 		}
19844653Sdougm 		share = sa_get_share(group, NULL);
19854653Sdougm 		if (subgroup == NULL)
19864653Sdougm 			(void) printf("%s", groupname);
19874653Sdougm 		else
19884653Sdougm 			(void) printf("    %s/%s", subgroup, groupname);
19894653Sdougm 		if (properties)
19904653Sdougm 			show_properties(group, proto, "");
19914653Sdougm 		(void) printf("\n");
19924653Sdougm 		if (strcmp(groupname, "zfs") == 0) {
19934653Sdougm 			sa_group_t zgroup;
19944653Sdougm 
19954653Sdougm 			for (zgroup = sa_get_sub_group(group);
19964653Sdougm 			    zgroup != NULL;
19974653Sdougm 			    zgroup = sa_get_next_group(zgroup)) {
19984653Sdougm 				show_group(zgroup, verbose, properties, proto,
19994653Sdougm 				    "zfs");
20004653Sdougm 			}
20014653Sdougm 			sa_free_attr_string(groupname);
20024653Sdougm 			return;
20034653Sdougm 		}
20043034Sdougm 		/*
20054653Sdougm 		 * Have a group, so list the contents. Resource and
20063034Sdougm 		 * description are only listed if verbose is set.
20073034Sdougm 		 */
20084653Sdougm 		for (share = sa_get_share(group, NULL);
20094653Sdougm 		    share != NULL;
20104653Sdougm 		    share = sa_get_next_share(share)) {
20114653Sdougm 			sharepath = sa_get_share_attr(share, "path");
20124653Sdougm 			if (sharepath != NULL) {
20135331Samw 				show_share(share, verbose, properties, proto,
20145331Samw 				    iszfs, sharepath);
20154653Sdougm 				sa_free_attr_string(sharepath);
20163034Sdougm 			}
20173034Sdougm 		}
20183034Sdougm 	}
20193034Sdougm 	if (groupname != NULL) {
20203034Sdougm 		sa_free_attr_string(groupname);
20213034Sdougm 	}
20223034Sdougm }
20233034Sdougm 
20243034Sdougm /*
20253034Sdougm  * show_group_xml_init()
20263034Sdougm  *
20273034Sdougm  * Create an XML document that will be used to display config info via
20283034Sdougm  * XML format.
20293034Sdougm  */
20303034Sdougm 
20313034Sdougm xmlDocPtr
20323034Sdougm show_group_xml_init()
20333034Sdougm {
20343034Sdougm 	xmlDocPtr doc;
20353034Sdougm 	xmlNodePtr root;
20363034Sdougm 
20373034Sdougm 	doc = xmlNewDoc((xmlChar *)"1.0");
20383034Sdougm 	if (doc != NULL) {
20394653Sdougm 		root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
20404653Sdougm 		if (root != NULL)
20414653Sdougm 			xmlDocSetRootElement(doc, root);
20423034Sdougm 	}
20433034Sdougm 	return (doc);
20443034Sdougm }
20453034Sdougm 
20463034Sdougm /*
20473034Sdougm  * show_group_xml(doc, group)
20483034Sdougm  *
20493034Sdougm  * Copy the group info into the XML doc.
20503034Sdougm  */
20513034Sdougm 
20523034Sdougm static void
20533034Sdougm show_group_xml(xmlDocPtr doc, sa_group_t group)
20543034Sdougm {
20553034Sdougm 	xmlNodePtr node;
20563034Sdougm 	xmlNodePtr root;
20573034Sdougm 
20583034Sdougm 	root = xmlDocGetRootElement(doc);
20593034Sdougm 	node = xmlCopyNode((xmlNodePtr)group, 1);
20603034Sdougm 	if (node != NULL && root != NULL) {
20614653Sdougm 		xmlAddChild(root, node);
20623034Sdougm 		/*
20633034Sdougm 		 * In the future, we may have interally used tags that
20643034Sdougm 		 * should not appear in the XML output. Remove
20653034Sdougm 		 * anything we don't want to show here.
20663034Sdougm 		 */
20673034Sdougm 	}
20683034Sdougm }
20693034Sdougm 
20703034Sdougm /*
20713034Sdougm  * sa_show(flags, argc, argv)
20723034Sdougm  *
20733034Sdougm  * Implements the show subcommand.
20743034Sdougm  */
20753034Sdougm 
20763034Sdougm int
20773910Sdougm sa_show(sa_handle_t handle, int flags, int argc, char *argv[])
20783034Sdougm {
20793034Sdougm 	sa_group_t group;
20803034Sdougm 	int verbose = 0;
20813034Sdougm 	int properties = 0;
20823034Sdougm 	int c;
20833034Sdougm 	int ret = SA_OK;
20843034Sdougm 	char *protocol = NULL;
20853034Sdougm 	int xml = 0;
20863034Sdougm 	xmlDocPtr doc;
20875331Samw #ifdef lint
20885331Samw 	flags = flags;
20895331Samw #endif
20903034Sdougm 
20913034Sdougm 	while ((c = getopt(argc, argv, "?hvP:px")) !=	EOF) {
20924653Sdougm 		switch (c) {
20934653Sdougm 		case 'v':
20944653Sdougm 			verbose++;
20954653Sdougm 			break;
20964653Sdougm 		case 'p':
20974653Sdougm 			properties++;
20984653Sdougm 			break;
20994653Sdougm 		case 'P':
21005331Samw 			if (protocol != NULL) {
21015331Samw 				(void) printf(gettext(
21025331Samw 				    "Specifying multiple protocols "
21035331Samw 				    "not supported: %s\n"),
21045331Samw 				    protocol);
21055331Samw 				return (SA_SYNTAX_ERR);
21065331Samw 			}
21074653Sdougm 			protocol = optarg;
21084653Sdougm 			if (!sa_valid_protocol(protocol)) {
21094653Sdougm 				(void) printf(gettext(
21104653Sdougm 				    "Invalid protocol specified: %s\n"),
21114653Sdougm 				    protocol);
21124653Sdougm 				return (SA_INVALID_PROTOCOL);
21134653Sdougm 			}
21144653Sdougm 			break;
21154653Sdougm 		case 'x':
21164653Sdougm 			xml++;
21174653Sdougm 			break;
21184653Sdougm 		default:
21194653Sdougm 		case 'h':
21204653Sdougm 		case '?':
21214653Sdougm 			(void) printf(gettext("usage: %s\n"),
21224653Sdougm 			    sa_get_usage(USAGE_SHOW));
21234653Sdougm 			return (0);
21243034Sdougm 		}
21253034Sdougm 	}
21263034Sdougm 
21273034Sdougm 	if (xml) {
21284653Sdougm 		doc = show_group_xml_init();
21294653Sdougm 		if (doc == NULL)
21304653Sdougm 			ret = SA_NO_MEMORY;
21313034Sdougm 	}
21323034Sdougm 
21333034Sdougm 	if (optind == argc) {
21344653Sdougm 		/* No group specified so go through them all */
21354653Sdougm 		for (group = sa_get_group(handle, NULL);
21364653Sdougm 		    group != NULL;
21374653Sdougm 		    group = sa_get_next_group(group)) {
21384653Sdougm 			/*
21394653Sdougm 			 * Have a group so check if one we want and then list
21404653Sdougm 			 * contents with appropriate options.
21414653Sdougm 			 */
21424653Sdougm 			if (xml)
21434653Sdougm 				show_group_xml(doc, group);
21444653Sdougm 			else
21454653Sdougm 				show_group(group, verbose, properties, protocol,
21464653Sdougm 				    NULL);
21474653Sdougm 		}
21483034Sdougm 	} else {
21494653Sdougm 		/* Have a specified list of groups */
21504653Sdougm 		for (; optind < argc; optind++) {
21514653Sdougm 			group = sa_get_group(handle, argv[optind]);
21524653Sdougm 			if (group != NULL) {
21534653Sdougm 				if (xml)
21544653Sdougm 					show_group_xml(doc, group);
21554653Sdougm 				else
21564653Sdougm 					show_group(group, verbose, properties,
21574653Sdougm 					    protocol, NULL);
21584653Sdougm 			} else {
21594653Sdougm 				(void) printf(gettext("%s: not found\n"),
21604653Sdougm 				    argv[optind]);
21614653Sdougm 				ret = SA_NO_SUCH_GROUP;
21624653Sdougm 			}
21633034Sdougm 		}
21643034Sdougm 	}
21653034Sdougm 	if (xml && ret == SA_OK) {
21664653Sdougm 		xmlDocFormatDump(stdout, doc, 1);
21674653Sdougm 		xmlFreeDoc(doc);
21683034Sdougm 	}
21693034Sdougm 	return (ret);
21703034Sdougm 
21713034Sdougm }
21723034Sdougm 
21733034Sdougm /*
21743034Sdougm  * enable_share(group, share, update_legacy)
21753034Sdougm  *
21763034Sdougm  * helper function to enable a share if the group is enabled.
21773034Sdougm  */
21783034Sdougm 
21793034Sdougm static int
21803910Sdougm enable_share(sa_handle_t handle, sa_group_t group, sa_share_t share,
21815331Samw     int update_legacy)
21823034Sdougm {
21833034Sdougm 	char *value;
21843034Sdougm 	int enabled;
21853034Sdougm 	sa_optionset_t optionset;
21865331Samw 	int err;
21873034Sdougm 	int ret = SA_OK;
21883034Sdougm 	char *zfs = NULL;
21893034Sdougm 	int iszfs = 0;
21905331Samw 	int isshare;
21913034Sdougm 
21923034Sdougm 	/*
21933034Sdougm 	 * need to enable this share if the group is enabled but not
21943034Sdougm 	 * otherwise. The enable is also done on each protocol
21953034Sdougm 	 * represented in the group.
21963034Sdougm 	 */
21973034Sdougm 	value = sa_get_group_attr(group, "state");
21983034Sdougm 	enabled = value != NULL && strcmp(value, "enabled") == 0;
21993034Sdougm 	if (value != NULL)
22004653Sdougm 		sa_free_attr_string(value);
22013034Sdougm 	/* remove legacy config if necessary */
22023034Sdougm 	if (update_legacy)
22035331Samw 		ret = sa_delete_legacy(share, NULL);
22043034Sdougm 	zfs = sa_get_group_attr(group, "zfs");
22053034Sdougm 	if (zfs != NULL) {
22064653Sdougm 		iszfs++;
22074653Sdougm 		sa_free_attr_string(zfs);
22083034Sdougm 	}
22093034Sdougm 
22103034Sdougm 	/*
22113034Sdougm 	 * Step through each optionset at the group level and
22123034Sdougm 	 * enable the share based on the protocol type. This
22133034Sdougm 	 * works because protocols must be set on the group
22143034Sdougm 	 * for the protocol to be enabled.
22153034Sdougm 	 */
22165331Samw 	isshare = sa_is_share(share);
22173034Sdougm 	for (optionset = sa_get_optionset(group, NULL);
22183034Sdougm 	    optionset != NULL && ret == SA_OK;
22193034Sdougm 	    optionset = sa_get_next_optionset(optionset)) {
22204653Sdougm 		value = sa_get_optionset_attr(optionset, "type");
22214653Sdougm 		if (value != NULL) {
22225331Samw 			if (enabled) {
22235331Samw 				if (isshare) {
22245331Samw 					err = sa_enable_share(share, value);
22255331Samw 				} else {
22265331Samw 					err = sa_enable_resource(share, value);
22275331Samw 					if (err == SA_NOT_SUPPORTED) {
22285331Samw 						sa_share_t parent;
22295331Samw 						parent = sa_get_resource_parent(
22305331Samw 						    share);
22315331Samw 						if (parent != NULL)
22325331Samw 							err = sa_enable_share(
22335331Samw 							    parent, value);
22345331Samw 					}
22355331Samw 				}
22365331Samw 				if (err != SA_OK) {
22375331Samw 					ret = err;
22385331Samw 					(void) printf(gettext(
22395331Samw 					    "Failed to enable share for "
22405331Samw 					    "\"%s\": %s\n"),
22415331Samw 					    value, sa_errorstr(ret));
22425331Samw 				}
22435331Samw 			}
22445331Samw 			/*
22455331Samw 			 * If we want to update the legacy, use a copy of
22465331Samw 			 * share so we can avoid breaking the loop we are in
22475331Samw 			 * since we might also need to go up the tree to the
22485331Samw 			 * parent.
22495331Samw 			 */
22505331Samw 			if (update_legacy && !iszfs) {
22515331Samw 				sa_share_t update = share;
22525331Samw 				if (!sa_is_share(share)) {
22535331Samw 					update = sa_get_resource_parent(share);
22545331Samw 				}
22555331Samw 				(void) sa_update_legacy(update, value);
22565331Samw 			}
22574653Sdougm 			sa_free_attr_string(value);
22584653Sdougm 		}
22593034Sdougm 	}
22603034Sdougm 	if (ret == SA_OK)
22614653Sdougm 		(void) sa_update_config(handle);
22623034Sdougm 	return (ret);
22633034Sdougm }
22643034Sdougm 
22653034Sdougm /*
22665331Samw  * sa_require_resource(group)
22675331Samw  *
22685331Samw  * if any of the defined protocols on the group require resource
22695331Samw  * names, then all shares must have them.
22705331Samw  */
22715331Samw 
22725331Samw static int
22735331Samw sa_require_resource(sa_group_t group)
22745331Samw {
22755331Samw 	sa_optionset_t optionset;
22765331Samw 
22775331Samw 	for (optionset = sa_get_optionset(group, NULL);
22785331Samw 	    optionset != NULL;
22795331Samw 	    optionset = sa_get_next_optionset(optionset)) {
22805331Samw 		char *proto;
22815331Samw 
22825331Samw 		proto = sa_get_optionset_attr(optionset, "type");
22835331Samw 		if (proto != NULL) {
22845331Samw 			uint64_t features;
22855331Samw 
22865331Samw 			features = sa_proto_get_featureset(proto);
22875331Samw 			if (features & SA_FEATURE_RESOURCE) {
22885331Samw 				sa_free_attr_string(proto);
22895331Samw 				return (B_TRUE);
22905331Samw 			}
22915331Samw 			sa_free_attr_string(proto);
22925331Samw 		}
22935331Samw 	}
22945331Samw 	return (B_FALSE);
22955331Samw }
22965331Samw 
22975331Samw /*
22983034Sdougm  * sa_addshare(flags, argc, argv)
22993034Sdougm  *
23003034Sdougm  * implements add-share subcommand.
23013034Sdougm  */
23023034Sdougm 
23035331Samw static int
23043910Sdougm sa_addshare(sa_handle_t handle, int flags, int argc, char *argv[])
23053034Sdougm {
23063034Sdougm 	int verbose = 0;
23073034Sdougm 	int dryrun = 0;
23083034Sdougm 	int c;
23093034Sdougm 	int ret = SA_OK;
23103034Sdougm 	sa_group_t group;
23113034Sdougm 	sa_share_t share;
23125331Samw 	sa_resource_t resource = NULL;
23133034Sdougm 	char *sharepath = NULL;
23143034Sdougm 	char *description = NULL;
23155331Samw 	char *rsrcname = NULL;
23165331Samw 	char *rsrc = NULL;
23173034Sdougm 	int persist = SA_SHARE_PERMANENT; /* default to persist */
23183034Sdougm 	int auth;
23193034Sdougm 	char dir[MAXPATHLEN];
23203034Sdougm 
23213034Sdougm 	while ((c = getopt(argc, argv, "?hvns:d:r:t")) != EOF) {
23224653Sdougm 		switch (c) {
23234653Sdougm 		case 'n':
23244653Sdougm 			dryrun++;
23254653Sdougm 			break;
23264653Sdougm 		case 'v':
23274653Sdougm 			verbose++;
23284653Sdougm 			break;
23294653Sdougm 		case 'd':
23304653Sdougm 			description = optarg;
23314653Sdougm 			break;
23324653Sdougm 		case 'r':
23335331Samw 			if (rsrcname != NULL) {
23345331Samw 				(void) printf(gettext("Adding multiple "
23355331Samw 				    "resource names not"
23365331Samw 				    " supported\n"));
23375331Samw 				return (SA_SYNTAX_ERR);
23385331Samw 			}
23395331Samw 			rsrcname = optarg;
23404653Sdougm 			break;
23414653Sdougm 		case 's':
23424653Sdougm 			/*
23434653Sdougm 			 * Save share path into group. Currently limit
23444653Sdougm 			 * to one share per command.
23454653Sdougm 			 */
23464653Sdougm 			if (sharepath != NULL) {
23474653Sdougm 				(void) printf(gettext(
23484653Sdougm 				    "Adding multiple shares not supported\n"));
23495331Samw 				return (SA_SYNTAX_ERR);
23504653Sdougm 			}
23514653Sdougm 			sharepath = optarg;
23524653Sdougm 			break;
23534653Sdougm 		case 't':
23544653Sdougm 			persist = SA_SHARE_TRANSIENT;
23554653Sdougm 			break;
23564653Sdougm 		default:
23574653Sdougm 		case 'h':
23584653Sdougm 		case '?':
23594653Sdougm 			(void) printf(gettext("usage: %s\n"),
23604653Sdougm 			    sa_get_usage(USAGE_ADD_SHARE));
23614653Sdougm 			return (0);
23623034Sdougm 		}
23633034Sdougm 	}
23643034Sdougm 
23653034Sdougm 	if (optind >= argc) {
23664653Sdougm 		(void) printf(gettext("usage: %s\n"),
23674653Sdougm 		    sa_get_usage(USAGE_ADD_SHARE));
23684653Sdougm 		if (dryrun || sharepath != NULL || description != NULL ||
23695331Samw 		    rsrcname != NULL || verbose || persist) {
23704653Sdougm 			(void) printf(gettext("\tgroup must be specified\n"));
23714653Sdougm 			ret = SA_NO_SUCH_GROUP;
23724653Sdougm 		} else {
23734653Sdougm 			ret = SA_OK;
23744653Sdougm 		}
23753034Sdougm 	} else {
23764653Sdougm 		if (sharepath == NULL) {
23774653Sdougm 			(void) printf(gettext("usage: %s\n"),
23784653Sdougm 			    sa_get_usage(USAGE_ADD_SHARE));
23794653Sdougm 			(void) printf(gettext(
23804653Sdougm 			    "\t-s sharepath must be specified\n"));
23815331Samw 			ret = SA_BAD_PATH;
23824653Sdougm 		}
23835331Samw 		if (ret == SA_OK) {
23845331Samw 			if (realpath(sharepath, dir) == NULL) {
23855331Samw 				ret = SA_BAD_PATH;
23865331Samw 				(void) printf(gettext("Path "
23875331Samw 				    "is not valid: %s\n"),
23885331Samw 				    sharepath);
23895331Samw 			} else {
23905331Samw 				sharepath = dir;
23915331Samw 			}
23923034Sdougm 		}
23935331Samw 		if (ret == SA_OK && rsrcname != NULL) {
23945331Samw 			/* check for valid syntax */
23955331Samw 			if (validresource(rsrcname)) {
23965331Samw 				rsrc = conv_to_utf8(rsrcname);
23975331Samw 				resource = sa_find_resource(handle, rsrc);
23985331Samw 				if (resource != NULL) {
23995331Samw 					/*
24005331Samw 					 * Resource names must be
24015331Samw 					 * unique in the system
24025331Samw 					 */
24035331Samw 					ret = SA_DUPLICATE_NAME;
24045331Samw 					(void) printf(gettext("usage: %s\n"),
24055331Samw 					    sa_get_usage(USAGE_ADD_SHARE));
24065331Samw 					(void) printf(gettext(
24075331Samw 					    "\tresource names must be unique "
24085331Samw 					    "in the system\n"));
24095331Samw 				}
24105331Samw 			} else {
24115331Samw 				(void) printf(gettext("usage: %s\n"),
24125331Samw 				    sa_get_usage(USAGE_ADD_SHARE));
24135331Samw 				(void) printf(gettext(
24145331Samw 				    "\tresource names use restricted "
24155331Samw 				    "character set\n"));
24165331Samw 				ret = SA_INVALID_NAME;
24175331Samw 			}
24183034Sdougm 		}
24195331Samw 
24205331Samw 		if (ret != SA_OK) {
24215331Samw 			if (rsrc != NULL && rsrcname != rsrc)
24225331Samw 				sa_free_attr_string(rsrc);
24235331Samw 			return (ret);
24244653Sdougm 		}
24255331Samw 
24264653Sdougm 		share = sa_find_share(handle, sharepath);
24274653Sdougm 		if (share != NULL) {
24285331Samw 			if (rsrcname == NULL) {
24295331Samw 				/*
24305331Samw 				 * Can only have a duplicate share if a new
24315331Samw 				 * resource name is being added.
24325331Samw 				 */
24335331Samw 				ret = SA_DUPLICATE_NAME;
24345331Samw 				(void) printf(gettext("Share path already "
24355331Samw 				    "shared: %s\n"), sharepath);
24365331Samw 			}
24375331Samw 		}
24385331Samw 		if (ret != SA_OK)
24395331Samw 			return (ret);
24405331Samw 
24415331Samw 		group = sa_get_group(handle, argv[optind]);
24425331Samw 		if (group != NULL) {
24435331Samw 			if (sa_require_resource(group) == B_TRUE &&
24445331Samw 			    rsrcname == NULL) {
24455331Samw 				(void) printf(gettext(
24465331Samw 				    "Resource name is required "
24475331Samw 				    "by at least one enabled protocol "
24485331Samw 				    "in group\n"));
24495331Samw 				return (SA_RESOURCE_REQUIRED);
24505331Samw 			}
24515331Samw 			if (share == NULL && ret == SA_OK) {
24525331Samw 				if (dryrun)
24535331Samw 					ret = sa_check_path(group, sharepath,
24545331Samw 					    SA_CHECK_NORMAL);
24555331Samw 				else
24565331Samw 					share = sa_add_share(group, sharepath,
24575331Samw 					    persist, &ret);
24585331Samw 			}
24595331Samw 			/*
24605331Samw 			 * Make sure this isn't an attempt to put a resourced
24615331Samw 			 * share into a different group than it already is in.
24625331Samw 			 */
24635331Samw 			if (share != NULL) {
24645331Samw 				sa_group_t parent;
24655331Samw 				parent = sa_get_parent_group(share);
24665331Samw 				if (parent != group) {
24675331Samw 					ret = SA_DUPLICATE_NAME;
24684653Sdougm 					(void) printf(gettext(
24694653Sdougm 					    "Share path already "
24705331Samw 					    "shared: %s\n"), sharepath);
24714653Sdougm 				}
24723034Sdougm 			}
24733034Sdougm 			if (!dryrun && share == NULL) {
24744653Sdougm 				(void) printf(gettext(
24754653Sdougm 				    "Could not add share: %s\n"),
24764653Sdougm 				    sa_errorstr(ret));
24773034Sdougm 			} else {
24785331Samw 				auth = check_authorizations(argv[optind],
24795331Samw 				    flags);
24804653Sdougm 				if (!dryrun && ret == SA_OK) {
24815331Samw 					if (rsrcname != NULL) {
24825331Samw 						resource = sa_add_resource(
24835331Samw 						    share,
24845331Samw 						    rsrc,
24855331Samw 						    SA_SHARE_PERMANENT,
24865331Samw 						    &ret);
24874653Sdougm 					}
24884653Sdougm 					if (ret == SA_OK &&
24894653Sdougm 					    description != NULL) {
24905331Samw 						if (description != NULL) {
24915331Samw 							ret =
24925331Samw 							    set_share_desc(
24935331Samw 							    share,
24945331Samw 							    description);
24955331Samw 						}
24964653Sdougm 					}
24974653Sdougm 					if (ret == SA_OK) {
24985331Samw 						/* now enable the share(s) */
24995331Samw 						if (resource != NULL) {
25005331Samw 							ret = enable_share(
25015331Samw 							    handle,
25025331Samw 							    group,
25035331Samw 							    resource,
25045331Samw 							    1);
25055331Samw 						} else {
25065331Samw 							ret = enable_share(
25075331Samw 							    handle,
25085331Samw 							    group,
25095331Samw 							    share,
25105331Samw 							    1);
25115331Samw 						}
25124653Sdougm 						ret = sa_update_config(handle);
25134653Sdougm 					}
25144653Sdougm 					switch (ret) {
25154653Sdougm 					case SA_DUPLICATE_NAME:
25164653Sdougm 						(void) printf(gettext(
25174653Sdougm 						    "Resource name in"
25185331Samw 						    "use: %s\n"),
25195331Samw 						    rsrcname);
25204653Sdougm 						break;
25214653Sdougm 					default:
25225331Samw 						(void) printf(gettext(
25235331Samw 						    "Could not set "
25244653Sdougm 						    "attribute: %s\n"),
25254653Sdougm 						    sa_errorstr(ret));
25264653Sdougm 						break;
25274653Sdougm 					case SA_OK:
25284653Sdougm 						break;
25294653Sdougm 					}
25305331Samw 				} else if (dryrun && ret == SA_OK &&
25315331Samw 				    !auth && verbose) {
25324653Sdougm 					(void) printf(gettext(
25334653Sdougm 					    "Command would fail: %s\n"),
25344653Sdougm 					    sa_errorstr(SA_NO_PERMISSION));
25354653Sdougm 					ret = SA_NO_PERMISSION;
25363034Sdougm 				}
25373034Sdougm 			}
25385331Samw 		} else {
25395331Samw 			switch (ret) {
25405331Samw 			default:
25415331Samw 				(void) printf(gettext(
25425331Samw 				    "Group \"%s\" not found\n"), argv[optind]);
25435331Samw 				ret = SA_NO_SUCH_GROUP;
25445331Samw 				break;
25455331Samw 			case SA_BAD_PATH:
25465331Samw 			case SA_DUPLICATE_NAME:
25475331Samw 				break;
25485331Samw 			}
25493034Sdougm 		}
25503034Sdougm 	}
25513034Sdougm 	return (ret);
25523034Sdougm }
25533034Sdougm 
25543034Sdougm /*
25553034Sdougm  * sa_moveshare(flags, argc, argv)
25563034Sdougm  *
25573034Sdougm  * implements move-share subcommand.
25583034Sdougm  */
25593034Sdougm 
25603034Sdougm int
25613910Sdougm sa_moveshare(sa_handle_t handle, int flags, int argc, char *argv[])
25623034Sdougm {
25633034Sdougm 	int verbose = 0;
25643034Sdougm 	int dryrun = 0;
25653034Sdougm 	int c;
25663034Sdougm 	int ret = SA_OK;
25673034Sdougm 	sa_group_t group;
25683034Sdougm 	sa_share_t share;
25695331Samw 	char *rsrcname = NULL;
25703034Sdougm 	char *sharepath = NULL;
25713034Sdougm 	int authsrc = 0, authdst = 0;
25723034Sdougm 
25735331Samw 	while ((c = getopt(argc, argv, "?hvnr:s:")) != EOF) {
25744653Sdougm 		switch (c) {
25754653Sdougm 		case 'n':
25764653Sdougm 			dryrun++;
25774653Sdougm 			break;
25784653Sdougm 		case 'v':
25794653Sdougm 			verbose++;
25804653Sdougm 			break;
25815331Samw 		case 'r':
25825331Samw 			if (rsrcname != NULL) {
25835331Samw 				(void) printf(gettext(
25845331Samw 				    "Moving multiple resource names not"
25855331Samw 				    " supported\n"));
25865331Samw 				return (SA_SYNTAX_ERR);
25875331Samw 			}
25885331Samw 			rsrcname = optarg;
25895331Samw 			break;
25904653Sdougm 		case 's':
25914653Sdougm 			/*
25924653Sdougm 			 * Remove share path from group. Currently limit
25934653Sdougm 			 * to one share per command.
25944653Sdougm 			 */
25954653Sdougm 			if (sharepath != NULL) {
25964653Sdougm 				(void) printf(gettext("Moving multiple shares"
25975331Samw 				    " not supported\n"));
25985331Samw 				return (SA_SYNTAX_ERR);
25994653Sdougm 			}
26004653Sdougm 			sharepath = optarg;
26014653Sdougm 			break;
26024653Sdougm 		default:
26034653Sdougm 		case 'h':
26044653Sdougm 		case '?':
26054653Sdougm 			(void) printf(gettext("usage: %s\n"),
26064653Sdougm 			    sa_get_usage(USAGE_MOVE_SHARE));
26074653Sdougm 			return (0);
26083034Sdougm 		}
26093034Sdougm 	}
26103034Sdougm 
26113034Sdougm 	if (optind >= argc || sharepath == NULL) {
26125331Samw 		(void) printf(gettext("usage: %s\n"),
26135331Samw 		    sa_get_usage(USAGE_MOVE_SHARE));
26145331Samw 		if (dryrun || verbose || sharepath != NULL) {
26155331Samw 			(void) printf(gettext("\tgroup must be specified\n"));
26165331Samw 			ret = SA_NO_SUCH_GROUP;
26175331Samw 		} else {
26185331Samw 			if (sharepath == NULL) {
26195331Samw 				ret = SA_SYNTAX_ERR;
26204653Sdougm 				(void) printf(gettext(
26215331Samw 				    "\tsharepath must be specified\n"));
26224653Sdougm 			} else {
26235331Samw 				ret = SA_OK;
26244653Sdougm 			}
26255331Samw 		}
26264653Sdougm 	} else {
26274653Sdougm 		sa_group_t parent;
26284653Sdougm 		char *zfsold;
26294653Sdougm 		char *zfsnew;
26304653Sdougm 
26313034Sdougm 		if (sharepath == NULL) {
26324653Sdougm 			(void) printf(gettext(
26334653Sdougm 			    "sharepath must be specified with the -s "
26344653Sdougm 			    "option\n"));
26354653Sdougm 			return (SA_BAD_PATH);
26364653Sdougm 		}
26373910Sdougm 		group = sa_get_group(handle, argv[optind]);
26384653Sdougm 		if (group == NULL) {
26394653Sdougm 			(void) printf(gettext("Group \"%s\" not found\n"),
26404653Sdougm 			    argv[optind]);
26414653Sdougm 			return (SA_NO_SUCH_GROUP);
26424653Sdougm 		}
26434653Sdougm 		share = sa_find_share(handle, sharepath);
26444653Sdougm 		authdst = check_authorizations(argv[optind], flags);
26454653Sdougm 		if (share == NULL) {
26463034Sdougm 			(void) printf(gettext("Share not found: %s\n"),
26474653Sdougm 			    sharepath);
26484653Sdougm 			return (SA_NO_SUCH_PATH);
26494653Sdougm 		}
26504653Sdougm 
26514653Sdougm 		parent = sa_get_parent_group(share);
26524653Sdougm 		if (parent != NULL) {
26534653Sdougm 			char *pname;
26544653Sdougm 			pname = sa_get_group_attr(parent, "name");
26554653Sdougm 			if (pname != NULL) {
26563034Sdougm 				authsrc = check_authorizations(pname, flags);
26573034Sdougm 				sa_free_attr_string(pname);
26584653Sdougm 			}
26594653Sdougm 			zfsold = sa_get_group_attr(parent, "zfs");
26604653Sdougm 			zfsnew = sa_get_group_attr(group, "zfs");
26614653Sdougm 			if ((zfsold != NULL && zfsnew == NULL) ||
26624653Sdougm 			    (zfsold == NULL && zfsnew != NULL)) {
26633034Sdougm 				ret = SA_NOT_ALLOWED;
26643034Sdougm 			}
26654653Sdougm 			if (zfsold != NULL)
26664653Sdougm 				sa_free_attr_string(zfsold);
26674653Sdougm 			if (zfsnew != NULL)
26684653Sdougm 				sa_free_attr_string(zfsnew);
26694653Sdougm 		}
26704653Sdougm 
26714653Sdougm 		if (ret == SA_OK && parent != group && !dryrun) {
26724653Sdougm 			char *oldstate;
26734653Sdougm 			/*
26744653Sdougm 			 * Note that the share may need to be
26755331Samw 			 * "unshared" if the new group is disabled and
26765331Samw 			 * the old was enabled or it may need to be
26775331Samw 			 * share to update if the new group is
26785331Samw 			 * enabled. We disable before the move and
26795331Samw 			 * will have to enable after the move in order
26805331Samw 			 * to cleanup entries for protocols that
26815331Samw 			 * aren't in the new group.
26824653Sdougm 			 */
26834653Sdougm 			oldstate = sa_get_group_attr(parent, "state");
26844653Sdougm 
26854653Sdougm 			/* enable_share determines what to do */
26865331Samw 			if (strcmp(oldstate, "enabled") == 0)
26873034Sdougm 				(void) sa_disable_share(share, NULL);
26885331Samw 
26894653Sdougm 			if (oldstate != NULL)
26903034Sdougm 				sa_free_attr_string(oldstate);
26913034Sdougm 		}
26924653Sdougm 
26935331Samw 		if (!dryrun && ret == SA_OK)
26945331Samw 			ret = sa_move_share(group, share);
26955331Samw 
26965331Samw 		/*
26975331Samw 		 * Reenable and update any config information.
26985331Samw 		 */
26995331Samw 		if (ret == SA_OK && parent != group && !dryrun) {
27005331Samw 			ret = sa_update_config(handle);
27015331Samw 
27025331Samw 			(void) enable_share(handle, group, share, 1);
27035331Samw 		}
27045331Samw 
27054653Sdougm 		if (ret != SA_OK)
27064653Sdougm 			(void) printf(gettext("Could not move share: %s\n"),
27074653Sdougm 			    sa_errorstr(ret));
27084653Sdougm 
27094653Sdougm 		if (dryrun && ret == SA_OK && !(authsrc & authdst) &&
27104653Sdougm 		    verbose) {
27114653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
27124653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
27134653Sdougm 		}
27143034Sdougm 	}
27153034Sdougm 	return (ret);
27163034Sdougm }
27173034Sdougm 
27183034Sdougm /*
27193034Sdougm  * sa_removeshare(flags, argc, argv)
27203034Sdougm  *
27213034Sdougm  * implements remove-share subcommand.
27223034Sdougm  */
27233034Sdougm 
27243034Sdougm int
27253910Sdougm sa_removeshare(sa_handle_t handle, int flags, int argc, char *argv[])
27263034Sdougm {
27273034Sdougm 	int verbose = 0;
27283034Sdougm 	int dryrun = 0;
27293034Sdougm 	int force = 0;
27303034Sdougm 	int c;
27313034Sdougm 	int ret = SA_OK;
27323034Sdougm 	sa_group_t group;
27335331Samw 	sa_resource_t resource = NULL;
27345331Samw 	sa_share_t share = NULL;
27355331Samw 	char *rsrcname = NULL;
27363034Sdougm 	char *sharepath = NULL;
27373034Sdougm 	char dir[MAXPATHLEN];
27383034Sdougm 	int auth;
27393034Sdougm 
27405331Samw 	while ((c = getopt(argc, argv, "?hfnr:s:v")) != EOF) {
27414653Sdougm 		switch (c) {
27424653Sdougm 		case 'n':
27434653Sdougm 			dryrun++;
27444653Sdougm 			break;
27454653Sdougm 		case 'v':
27464653Sdougm 			verbose++;
27474653Sdougm 			break;
27484653Sdougm 		case 'f':
27494653Sdougm 			force++;
27504653Sdougm 			break;
27514653Sdougm 		case 's':
27524653Sdougm 			/*
27534653Sdougm 			 * Remove share path from group. Currently limit
27544653Sdougm 			 * to one share per command.
27554653Sdougm 			 */
27564653Sdougm 			if (sharepath != NULL) {
27574653Sdougm 				(void) printf(gettext(
27584653Sdougm 				    "Removing multiple shares not "
27593034Sdougm 				    "supported\n"));
27604653Sdougm 				return (SA_SYNTAX_ERR);
27614653Sdougm 			}
27624653Sdougm 			sharepath = optarg;
27634653Sdougm 			break;
27645331Samw 		case 'r':
27655331Samw 			/*
27665331Samw 			 * Remove share from group if last resource or remove
27675331Samw 			 * resource from share if multiple resources.
27685331Samw 			 */
27695331Samw 			if (rsrcname != NULL) {
27705331Samw 				(void) printf(gettext(
27715331Samw 				    "Removing multiple resource names not "
27725331Samw 				    "supported\n"));
27735331Samw 				return (SA_SYNTAX_ERR);
27745331Samw 			}
27755331Samw 			rsrcname = optarg;
27765331Samw 			break;
27774653Sdougm 		default:
27784653Sdougm 		case 'h':
27794653Sdougm 		case '?':
27804653Sdougm 			(void) printf(gettext("usage: %s\n"),
27814653Sdougm 			    sa_get_usage(USAGE_REMOVE_SHARE));
27824653Sdougm 			return (0);
27833034Sdougm 		}
27843034Sdougm 	}
27853034Sdougm 
27865331Samw 	if (optind >= argc || (rsrcname == NULL && sharepath == NULL)) {
27875331Samw 		if (sharepath == NULL && rsrcname == NULL) {
27883034Sdougm 			(void) printf(gettext("usage: %s\n"),
27894653Sdougm 			    sa_get_usage(USAGE_REMOVE_SHARE));
27905331Samw 			(void) printf(gettext("\t-s sharepath or -r resource"
27915331Samw 			    " must be specified\n"));
27924653Sdougm 			ret = SA_BAD_PATH;
27934653Sdougm 		} else {
27944653Sdougm 			ret = SA_OK;
27954653Sdougm 		}
27963034Sdougm 	}
27974653Sdougm 	if (ret != SA_OK) {
27984653Sdougm 		return (ret);
27994653Sdougm 	}
28004653Sdougm 
28014653Sdougm 	if (optind < argc) {
28023034Sdougm 		if ((optind + 1) < argc) {
28034653Sdougm 			(void) printf(gettext("Extraneous group(s) at end of "
28044653Sdougm 			    "command\n"));
28054653Sdougm 			ret = SA_SYNTAX_ERR;
28063034Sdougm 		} else {
28074653Sdougm 			group = sa_get_group(handle, argv[optind]);
28084653Sdougm 			if (group == NULL) {
28094653Sdougm 				(void) printf(gettext(
28104653Sdougm 				    "Group \"%s\" not found\n"), argv[optind]);
28114653Sdougm 				ret = SA_NO_SUCH_GROUP;
28124653Sdougm 			}
28133034Sdougm 		}
28144653Sdougm 	} else {
28153034Sdougm 		group = NULL;
28164653Sdougm 	}
28174653Sdougm 
28185331Samw 	if (rsrcname != NULL) {
28195331Samw 		resource = sa_find_resource(handle, rsrcname);
28205331Samw 		if (resource == NULL) {
28215331Samw 			ret = SA_NO_SUCH_RESOURCE;
28225331Samw 			(void) printf(gettext(
28235331Samw 			    "Resource name not found for share: %s\n"),
28245331Samw 			    rsrcname);
28255331Samw 		}
28265331Samw 	}
28275331Samw 
28284653Sdougm 	/*
28294653Sdougm 	 * Lookup the path in the internal configuration. Care
28304653Sdougm 	 * must be taken to handle the case where the
28314653Sdougm 	 * underlying path has been removed since we need to
28324653Sdougm 	 * be able to deal with that as well.
28334653Sdougm 	 */
28344653Sdougm 	if (ret == SA_OK) {
28355331Samw 		if (sharepath != NULL) {
28365331Samw 			if (group != NULL)
28375331Samw 				share = sa_get_share(group, sharepath);
28385331Samw 			else
28395331Samw 				share = sa_find_share(handle, sharepath);
28405331Samw 		}
28415331Samw 
28425331Samw 		if (resource != NULL) {
28435331Samw 			sa_share_t rsrcshare;
28445331Samw 			rsrcshare = sa_get_resource_parent(resource);
28455331Samw 			if (share == NULL)
28465331Samw 				share = rsrcshare;
28475331Samw 			else if (share != rsrcshare) {
28485331Samw 				ret = SA_NO_SUCH_RESOURCE;
28495331Samw 				(void) printf(gettext(
28505331Samw 				    "Bad resource name for share: %s\n"),
28515331Samw 				    rsrcname);
28525331Samw 				share = NULL;
28535331Samw 			}
28545331Samw 		}
28555331Samw 
28563663Sdougm 		/*
28573663Sdougm 		 * If we didn't find the share with the provided path,
28583663Sdougm 		 * it may be a symlink so attempt to resolve it using
28593663Sdougm 		 * realpath and try again. Realpath will resolve any
28603663Sdougm 		 * symlinks and place them in "dir". Note that
28613663Sdougm 		 * sharepath is only used for the lookup the first
28623663Sdougm 		 * time and later for error messages. dir will be used
28633663Sdougm 		 * on the second attempt. Once a share is found, all
28643663Sdougm 		 * operations are based off of the share variable.
28653663Sdougm 		 */
28663663Sdougm 		if (share == NULL) {
28674653Sdougm 			if (realpath(sharepath, dir) == NULL) {
28684653Sdougm 				ret = SA_BAD_PATH;
28694653Sdougm 				(void) printf(gettext(
28704653Sdougm 				    "Path is not valid: %s\n"), sharepath);
28714653Sdougm 			} else {
28724653Sdougm 				if (group != NULL)
28734653Sdougm 					share = sa_get_share(group, dir);
28744653Sdougm 				else
28754653Sdougm 					share = sa_find_share(handle, dir);
28764653Sdougm 			}
28773663Sdougm 		}
28784653Sdougm 	}
28794653Sdougm 
28804653Sdougm 	/*
28814653Sdougm 	 * If there hasn't been an error, there was likely a
28824653Sdougm 	 * path found. If not, give the appropriate error
28834653Sdougm 	 * message and set the return error. If it was found,
28844653Sdougm 	 * then disable the share and then remove it from the
28854653Sdougm 	 * configuration.
28864653Sdougm 	 */
28874653Sdougm 	if (ret != SA_OK) {
28884653Sdougm 		return (ret);
28894653Sdougm 	}
28904653Sdougm 	if (share == NULL) {
28914653Sdougm 		if (group != NULL)
28923034Sdougm 			(void) printf(gettext("Share not found in group %s:"
28934653Sdougm 			    " %s\n"), argv[optind], sharepath);
28944653Sdougm 		else
28953034Sdougm 			(void) printf(gettext("Share not found: %s\n"),
28964653Sdougm 			    sharepath);
28975331Samw 		ret = SA_NO_SUCH_PATH;
28984653Sdougm 	} else {
28994653Sdougm 		if (group == NULL)
29003034Sdougm 			group = sa_get_parent_group(share);
29014653Sdougm 		if (!dryrun) {
29023034Sdougm 			if (ret == SA_OK) {
29035331Samw 				if (resource != NULL)
29045331Samw 					ret = sa_disable_resource(resource,
29055331Samw 					    NULL);
29065331Samw 				else
29075331Samw 					ret = sa_disable_share(share, NULL);
29083034Sdougm 				/*
29094653Sdougm 				 * We don't care if it fails since it
29103663Sdougm 				 * could be disabled already. Some
29113663Sdougm 				 * unexpected errors could occur that
29123663Sdougm 				 * prevent removal, so also check for
29133663Sdougm 				 * force being set.
29143034Sdougm 				 */
29155331Samw 				if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
29165331Samw 				    ret == SA_NOT_SUPPORTED ||
29175331Samw 				    ret == SA_SYSTEM_ERR || force) &&
29185331Samw 				    resource == NULL)
29195331Samw 					ret = sa_remove_share(share);
29205331Samw 
29215331Samw 				if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
29224653Sdougm 				    ret == SA_NOT_SUPPORTED ||
29235331Samw 				    ret == SA_SYSTEM_ERR || force) &&
29245331Samw 				    resource != NULL) {
29255331Samw 					ret = sa_remove_resource(resource);
29265331Samw 					if (ret == SA_OK) {
29275331Samw 						/*
29285331Samw 						 * If this was the
29295331Samw 						 * last one, remove
29305331Samw 						 * the share as well.
29315331Samw 						 */
29325331Samw 						resource =
29335331Samw 						    sa_get_share_resource(
29345331Samw 						    share, NULL);
29355331Samw 						if (resource == NULL)
29365331Samw 							ret = sa_remove_share(
29375331Samw 							    share);
29385331Samw 					}
29394653Sdougm 				}
29404653Sdougm 				if (ret == SA_OK)
29414653Sdougm 					ret = sa_update_config(handle);
29423034Sdougm 			}
29434653Sdougm 			if (ret != SA_OK)
29445331Samw 				(void) printf(gettext("Could not remove share:"
29455331Samw 				    " %s\n"), sa_errorstr(ret));
29464653Sdougm 		} else if (ret == SA_OK) {
29473034Sdougm 			char *pname;
29483034Sdougm 			pname = sa_get_group_attr(group, "name");
29493034Sdougm 			if (pname != NULL) {
29504653Sdougm 				auth = check_authorizations(pname, flags);
29514653Sdougm 				sa_free_attr_string(pname);
29523034Sdougm 			}
29533034Sdougm 			if (!auth && verbose) {
29544653Sdougm 				(void) printf(gettext(
29554653Sdougm 				    "Command would fail: %s\n"),
29564653Sdougm 				    sa_errorstr(SA_NO_PERMISSION));
29573034Sdougm 			}
29583034Sdougm 		}
29593034Sdougm 	}
29603034Sdougm 	return (ret);
29613034Sdougm }
29623034Sdougm 
29633034Sdougm /*
29643034Sdougm  * sa_set_share(flags, argc, argv)
29653034Sdougm  *
29663034Sdougm  * implements set-share subcommand.
29673034Sdougm  */
29683034Sdougm 
29693034Sdougm int
29703910Sdougm sa_set_share(sa_handle_t handle, int flags, int argc, char *argv[])
29713034Sdougm {
29723034Sdougm 	int dryrun = 0;
29733034Sdougm 	int c;
29743034Sdougm 	int ret = SA_OK;
29753034Sdougm 	sa_group_t group, sharegroup;
29763034Sdougm 	sa_share_t share;
29775331Samw 	sa_resource_t resource = NULL;
29783034Sdougm 	char *sharepath = NULL;
29793034Sdougm 	char *description = NULL;
29805331Samw 	char *desc;
29815331Samw 	char *rsrcname = NULL;
29825331Samw 	char *rsrc = NULL;
29835331Samw 	char *newname = NULL;
29845331Samw 	char *newrsrc;
29855331Samw 	char *groupname = NULL;
29863034Sdougm 	int auth;
29873034Sdougm 	int verbose = 0;
29883034Sdougm 
29893034Sdougm 	while ((c = getopt(argc, argv, "?hnd:r:s:")) != EOF) {
29904653Sdougm 		switch (c) {
29914653Sdougm 		case 'n':
29924653Sdougm 			dryrun++;
29934653Sdougm 			break;
29944653Sdougm 		case 'd':
29954653Sdougm 			description = optarg;
29964653Sdougm 			break;
29974653Sdougm 		case 'v':
29984653Sdougm 			verbose++;
29994653Sdougm 			break;
30005331Samw 		case 'r':
30015331Samw 			/*
30025331Samw 			 * Update share by resource name
30035331Samw 			 */
30045331Samw 			if (rsrcname != NULL) {
30055331Samw 				(void) printf(gettext(
30065331Samw 				    "Updating multiple resource names not "
30075331Samw 				    "supported\n"));
30085331Samw 				return (SA_SYNTAX_ERR);
30095331Samw 			}
30105331Samw 			rsrcname = optarg;
30115331Samw 			break;
30124653Sdougm 		case 's':
30134653Sdougm 			/*
30144653Sdougm 			 * Save share path into group. Currently limit
30154653Sdougm 			 * to one share per command.
30164653Sdougm 			 */
30174653Sdougm 			if (sharepath != NULL) {
30184653Sdougm 				(void) printf(gettext(
30194653Sdougm 				    "Updating multiple shares not "
30203034Sdougm 				    "supported\n"));
30215331Samw 				return (SA_SYNTAX_ERR);
30224653Sdougm 			}
30234653Sdougm 			sharepath = optarg;
30244653Sdougm 			break;
30254653Sdougm 		default:
30264653Sdougm 		case 'h':
30274653Sdougm 		case '?':
30284653Sdougm 			(void) printf(gettext("usage: %s\n"),
30294653Sdougm 			    sa_get_usage(USAGE_SET_SHARE));
30304653Sdougm 			return (SA_OK);
30313034Sdougm 		}
30323034Sdougm 	}
30334653Sdougm 
30345331Samw 	if (optind >= argc && sharepath == NULL && rsrcname == NULL) {
30354653Sdougm 		if (sharepath == NULL) {
30364653Sdougm 			(void) printf(gettext("usage: %s\n"),
30374653Sdougm 			    sa_get_usage(USAGE_SET_SHARE));
30384653Sdougm 			(void) printf(gettext("\tgroup must be specified\n"));
30394653Sdougm 			ret = SA_BAD_PATH;
30404653Sdougm 		} else {
30414653Sdougm 			ret = SA_OK;
30424653Sdougm 		}
30433034Sdougm 	}
30443034Sdougm 	if ((optind + 1) < argc) {
30454653Sdougm 		(void) printf(gettext("usage: %s\n"),
30464653Sdougm 		    sa_get_usage(USAGE_SET_SHARE));
30474653Sdougm 		(void) printf(gettext("\tExtraneous group(s) at end\n"));
30484653Sdougm 		ret = SA_SYNTAX_ERR;
30493034Sdougm 	}
30504653Sdougm 
30515331Samw 	/*
30525331Samw 	 * Must have at least one of sharepath and rsrcrname.
30535331Samw 	 * It is a syntax error to be missing both.
30545331Samw 	 */
30555331Samw 	if (sharepath == NULL && rsrcname == NULL) {
30565331Samw 		(void) printf(gettext("usage: %s\n"),
30575331Samw 		    sa_get_usage(USAGE_SET_SHARE));
30585331Samw 		ret = SA_SYNTAX_ERR;
30595331Samw 	}
30605331Samw 
30614653Sdougm 	if (ret != SA_OK)
30624653Sdougm 		return (ret);
30634653Sdougm 
30644653Sdougm 	if (optind < argc) {
30653034Sdougm 		groupname = argv[optind];
30663910Sdougm 		group = sa_get_group(handle, groupname);
30674653Sdougm 	} else {
30683034Sdougm 		group = NULL;
30693034Sdougm 		groupname = NULL;
30704653Sdougm 	}
30715331Samw 	if (rsrcname != NULL) {
30725331Samw 		/*
30735331Samw 		 * If rsrcname exists, split rename syntax and then
30745331Samw 		 * convert to utf 8 if no errors.
30755331Samw 		 */
30765331Samw 		newname = strchr(rsrcname, '=');
30775331Samw 		if (newname != NULL) {
30785331Samw 			*newname++ = '\0';
30795331Samw 		}
30805331Samw 		if (!validresource(rsrcname)) {
30815331Samw 			ret = SA_INVALID_NAME;
30825331Samw 			(void) printf(gettext("Invalid resource name: "
30835331Samw 			    "\"%s\"\n"), rsrcname);
30845331Samw 		} else {
30855331Samw 			rsrc = conv_to_utf8(rsrcname);
30865331Samw 		}
30875331Samw 		if (newname != NULL) {
30885331Samw 			if (!validresource(newname)) {
30895331Samw 				ret = SA_INVALID_NAME;
30905331Samw 				(void) printf(gettext("Invalid resource name: "
30915331Samw 				    "%s\n"), newname);
30925331Samw 			} else {
30935331Samw 				newrsrc = conv_to_utf8(newname);
30945331Samw 			}
30955331Samw 		}
30965331Samw 	}
30975331Samw 
30985331Samw 	if (ret != SA_OK) {
30995331Samw 		if (rsrcname != NULL && rsrcname != rsrc)
31005331Samw 			sa_free_attr_string(rsrc);
31015331Samw 		if (newname != NULL && newname != newrsrc)
31025331Samw 			sa_free_attr_string(newrsrc);
31035331Samw 		return (ret);
31045331Samw 	}
31055331Samw 
31065331Samw 	if (sharepath != NULL) {
31075331Samw 		share = sa_find_share(handle, sharepath);
31085331Samw 	} else if (rsrcname != NULL) {
31095331Samw 		resource = sa_find_resource(handle, rsrc);
31105331Samw 		if (resource != NULL) {
31115331Samw 			share = sa_get_resource_parent(resource);
31125331Samw 		}
31135331Samw 	}
31145331Samw 	if (share != NULL) {
31155331Samw 		sharegroup = sa_get_parent_group(share);
31165331Samw 		if (group != NULL && group != sharegroup) {
31175331Samw 			(void) printf(gettext("Group \"%s\" does not contain "
31185331Samw 			    "share %s\n"),
31195331Samw 			    argv[optind], sharepath);
31205331Samw 			ret = SA_BAD_PATH;
31215331Samw 		} else {
31225331Samw 			int delgroupname = 0;
31235331Samw 			if (groupname == NULL) {
31245331Samw 				groupname = sa_get_group_attr(sharegroup,
31255331Samw 				    "name");
31265331Samw 				delgroupname = 1;
31275331Samw 			}
31285331Samw 			if (groupname != NULL) {
31295331Samw 				auth = check_authorizations(groupname, flags);
31305331Samw 				if (delgroupname) {
31315331Samw 					sa_free_attr_string(groupname);
31325331Samw 					groupname = NULL;
31335331Samw 				}
31345331Samw 			} else {
31355331Samw 				ret = SA_NO_MEMORY;
31365331Samw 			}
31375331Samw 			if (rsrcname != NULL) {
31385331Samw 				resource = sa_find_resource(handle, rsrc);
31395331Samw 				if (!dryrun) {
31405331Samw 					if (newname != NULL &&
31415331Samw 					    resource != NULL)
31425331Samw 						ret = sa_rename_resource(
31435331Samw 						    resource, newrsrc);
31445331Samw 					else if (newname != NULL)
31455331Samw 						ret = SA_NO_SUCH_RESOURCE;
31465331Samw 					if (newname != NULL &&
31475331Samw 					    newname != newrsrc)
31485331Samw 						sa_free_attr_string(newrsrc);
31495331Samw 				}
31505331Samw 				if (rsrc != rsrcname)
31515331Samw 					sa_free_attr_string(rsrc);
31525331Samw 			}
31535331Samw 
31545331Samw 			/*
31555331Samw 			 * If the user has set a description, it will be
31565331Samw 			 * on the resource if -r was used otherwise it
31575331Samw 			 * must be on the share.
31585331Samw 			 */
31595331Samw 			if (ret == SA_OK && description != NULL) {
31605331Samw 				desc = conv_to_utf8(description);
31615331Samw 				if (resource != NULL)
31625331Samw 					ret = sa_set_resource_description(
31635331Samw 					    resource, desc);
31645331Samw 				else
31655331Samw 					ret = sa_set_share_description(share,
31665331Samw 					    desc);
31675331Samw 				if (desc != description)
31685331Samw 					sa_free_share_description(desc);
31695331Samw 			}
31705331Samw 		}
31715331Samw 		if (!dryrun && ret == SA_OK) {
31725331Samw 			if (resource != NULL)
31735331Samw 				(void) sa_enable_resource(resource, NULL);
31745331Samw 			ret = sa_update_config(handle);
31755331Samw 		}
31765331Samw 		switch (ret) {
31775331Samw 		case SA_DUPLICATE_NAME:
31785331Samw 			(void) printf(gettext("Resource name in use: %s\n"),
31795331Samw 			    rsrcname);
31805331Samw 			break;
31815331Samw 		default:
31825331Samw 			(void) printf(gettext("Could not set: %s\n"),
31835331Samw 			    sa_errorstr(ret));
31845331Samw 			break;
31855331Samw 		case SA_OK:
31865331Samw 			if (dryrun && !auth && verbose) {
31875331Samw 				(void) printf(gettext(
31885331Samw 				    "Command would fail: %s\n"),
31895331Samw 				    sa_errorstr(SA_NO_PERMISSION));
31905331Samw 			}
31915331Samw 			break;
31925331Samw 		}
31935331Samw 	} else {
31944653Sdougm 		(void) printf(gettext("Share path \"%s\" not found\n"),
31954653Sdougm 		    sharepath);
31965331Samw 		ret = SA_NO_SUCH_PATH;
31973034Sdougm 	}
31984653Sdougm 
31993034Sdougm 	return (ret);
32003034Sdougm }
32013034Sdougm 
32023034Sdougm /*
32033034Sdougm  * add_security(group, sectype, optlist, proto, *err)
32043034Sdougm  *
32053034Sdougm  * Helper function to add a security option (named optionset) to the
32063034Sdougm  * group.
32073034Sdougm  */
32083034Sdougm 
32093034Sdougm static int
32103034Sdougm add_security(sa_group_t group, char *sectype,
32115331Samw     struct options *optlist, char *proto, int *err)
32123034Sdougm {
32133034Sdougm 	sa_security_t security;
32143034Sdougm 	int ret = SA_OK;
32153034Sdougm 	int result = 0;
32163034Sdougm 
32173034Sdougm 	sectype = sa_proto_space_alias(proto, sectype);
32183034Sdougm 	security = sa_get_security(group, sectype, proto);
32194653Sdougm 	if (security == NULL)
32204653Sdougm 		security = sa_create_security(group, sectype, proto);
32214653Sdougm 
32223034Sdougm 	if (sectype != NULL)
32234653Sdougm 		sa_free_attr_string(sectype);
32244653Sdougm 
32254653Sdougm 	if (security == NULL)
32264653Sdougm 		return (ret);
32274653Sdougm 
32284653Sdougm 	while (optlist != NULL) {
32293034Sdougm 		sa_property_t prop;
32303034Sdougm 		prop = sa_get_property(security, optlist->optname);
32313034Sdougm 		if (prop == NULL) {
32323034Sdougm 			/*
32334653Sdougm 			 * Add the property, but only if it is
32343034Sdougm 			 * a non-NULL or non-zero length value
32353034Sdougm 			 */
32364653Sdougm 			if (optlist->optvalue != NULL) {
32374653Sdougm 				prop = sa_create_property(optlist->optname,
32384653Sdougm 				    optlist->optvalue);
32394653Sdougm 				if (prop != NULL) {
32405331Samw 					ret = sa_valid_property(security,
32415331Samw 					    proto, prop);
32424653Sdougm 					if (ret != SA_OK) {
32434653Sdougm 						(void) sa_remove_property(prop);
32444653Sdougm 						(void) printf(gettext(
32454653Sdougm 						    "Could not add "
32464653Sdougm 						    "property %s: %s\n"),
32474653Sdougm 						    optlist->optname,
32484653Sdougm 						    sa_errorstr(ret));
32494653Sdougm 					}
32504653Sdougm 					if (ret == SA_OK) {
32514653Sdougm 						ret = sa_add_property(security,
32524653Sdougm 						    prop);
32534653Sdougm 						if (ret != SA_OK) {
32544653Sdougm 							(void) printf(gettext(
32554653Sdougm 							    "Could not add "
32565331Samw 							    "property (%s=%s):"
32575331Samw 							    " %s\n"),
32584653Sdougm 							    optlist->optname,
32594653Sdougm 							    optlist->optvalue,
32604653Sdougm 							    sa_errorstr(ret));
32614653Sdougm 						} else {
32624653Sdougm 							result = 1;
32634653Sdougm 						}
32644653Sdougm 					}
32653034Sdougm 				}
32663034Sdougm 			}
32673034Sdougm 		} else {
32684653Sdougm 			ret = sa_update_property(prop, optlist->optvalue);
32694653Sdougm 			result = 1; /* should check if really changed */
32703034Sdougm 		}
32713034Sdougm 		optlist = optlist->next;
32724653Sdougm 	}
32734653Sdougm 	/*
32744653Sdougm 	 * When done, properties may have all been removed but
32754653Sdougm 	 * we need to keep the security type itself until
32764653Sdougm 	 * explicitly removed.
32774653Sdougm 	 */
32784653Sdougm 	if (result)
32793034Sdougm 		ret = sa_commit_properties(security, 0);
32803034Sdougm 	*err = ret;
32813034Sdougm 	return (result);
32823034Sdougm }
32833034Sdougm 
32843034Sdougm /*
32855089Sdougm  * zfscheck(group, share)
32865089Sdougm  *
32875089Sdougm  * For the special case where a share was provided, make sure it is a
32885089Sdougm  * compatible path for a ZFS property change.  The only path
32895089Sdougm  * acceptable is the path that defines the zfs sub-group (dataset with
32905089Sdougm  * the sharenfs property set) and not one of the paths that inherited
32915089Sdougm  * the NFS properties. Returns SA_OK if it is usable and
32925089Sdougm  * SA_NOT_ALLOWED if it isn't.
32935089Sdougm  *
32945089Sdougm  * If group is not a ZFS group/subgroup, we assume OK since the check
32955089Sdougm  * on return will catch errors for those cases.  What we are looking
32965089Sdougm  * for here is that the group is ZFS and the share is not the defining
32975089Sdougm  * share.  All else is SA_OK.
32985089Sdougm  */
32995089Sdougm 
33005089Sdougm static int
33015089Sdougm zfscheck(sa_group_t group, sa_share_t share)
33025089Sdougm {
33035089Sdougm 	int ret = SA_OK;
33045089Sdougm 	char *attr;
33055089Sdougm 
33065089Sdougm 	if (sa_group_is_zfs(group)) {
33075089Sdougm 		/*
33085089Sdougm 		 * The group is a ZFS group.  Does the share represent
33095089Sdougm 		 * the dataset that defined the group? It is only OK
33105089Sdougm 		 * if the attribute "subgroup" exists on the share and
33115089Sdougm 		 * has a value of "true".
33125089Sdougm 		 */
33135089Sdougm 
33145089Sdougm 		ret = SA_NOT_ALLOWED;
33155089Sdougm 		attr = sa_get_share_attr(share, "subgroup");
33165089Sdougm 		if (attr != NULL) {
33175089Sdougm 			if (strcmp(attr, "true") == 0)
33185089Sdougm 				ret = SA_OK;
33195089Sdougm 			sa_free_attr_string(attr);
33205089Sdougm 		}
33215089Sdougm 	}
33225089Sdougm 	return (ret);
33235089Sdougm }
33245089Sdougm 
33255089Sdougm /*
33265331Samw  * basic_set(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
33273034Sdougm  *
33283034Sdougm  * This function implements "set" when a name space (-S) is not
33293034Sdougm  * specified. It is a basic set. Options and other CLI parsing has
33303034Sdougm  * already been done.
33315331Samw  *
33325331Samw  * "rsrcname" is a "resource name". If it is non-NULL, it must match
33335331Samw  * the sharepath if present or group if present, otherwise it is used
33345331Samw  * to set options.
33355331Samw  *
33365331Samw  * Resource names may take options if the protocol supports it. If the
33375331Samw  * protocol doesn't support resource level options, rsrcname is just
33385331Samw  * an alias for the share.
33393034Sdougm  */
33403034Sdougm 
33413034Sdougm static int
33423910Sdougm basic_set(sa_handle_t handle, char *groupname, struct options *optlist,
33435331Samw     char *protocol, char *sharepath, char *rsrcname, int dryrun)
33443034Sdougm {
33453034Sdougm 	sa_group_t group;
33463034Sdougm 	int ret = SA_OK;
33473034Sdougm 	int change = 0;
33483034Sdougm 	struct list *worklist = NULL;
33493034Sdougm 
33503910Sdougm 	group = sa_get_group(handle, groupname);
33513034Sdougm 	if (group != NULL) {
33524653Sdougm 		sa_share_t share = NULL;
33535331Samw 		sa_resource_t resource = NULL;
33545331Samw 
33555331Samw 		/*
33565331Samw 		 * If there is a sharepath, make sure it belongs to
33575331Samw 		 * the group.
33585331Samw 		 */
33594653Sdougm 		if (sharepath != NULL) {
33604653Sdougm 			share = sa_get_share(group, sharepath);
33614653Sdougm 			if (share == NULL) {
33624653Sdougm 				(void) printf(gettext(
33634653Sdougm 				    "Share does not exist in group %s\n"),
33644653Sdougm 				    groupname, sharepath);
33654653Sdougm 				ret = SA_NO_SUCH_PATH;
33665089Sdougm 			} else {
33675089Sdougm 				/* if ZFS and OK, then only group */
33685089Sdougm 				ret = zfscheck(group, share);
33695089Sdougm 				if (ret == SA_OK &&
33705089Sdougm 				    sa_group_is_zfs(group))
33715089Sdougm 					share = NULL;
33725089Sdougm 				if (ret == SA_NOT_ALLOWED)
33735089Sdougm 					(void) printf(gettext(
33745089Sdougm 					    "Properties on ZFS group shares "
33755089Sdougm 					    "not supported: %s\n"), sharepath);
33764653Sdougm 			}
33773034Sdougm 		}
33785331Samw 
33795331Samw 		/*
33805331Samw 		 * If a resource name exists, make sure it belongs to
33815331Samw 		 * the share if present else it belongs to the
33825331Samw 		 * group. Also check the protocol to see if it
33835331Samw 		 * supports resource level properties or not. If not,
33845331Samw 		 * use share only.
33855331Samw 		 */
33865331Samw 		if (rsrcname != NULL) {
33875331Samw 			if (share != NULL) {
33885331Samw 				resource = sa_get_share_resource(share,
33895331Samw 				    rsrcname);
33905331Samw 				if (resource == NULL)
33915331Samw 					ret = SA_NO_SUCH_RESOURCE;
33925331Samw 			} else {
33935331Samw 				resource = sa_get_resource(group, rsrcname);
33945331Samw 				if (resource != NULL)
33955331Samw 					share = sa_get_resource_parent(
33965331Samw 					    resource);
33975331Samw 				else
33985331Samw 					ret = SA_NO_SUCH_RESOURCE;
33995331Samw 			}
34005331Samw 			if (ret == SA_OK && resource != NULL) {
34015331Samw 				uint64_t features;
34025331Samw 				/*
34035331Samw 				 * Check to see if the resource can take
34045331Samw 				 * properties. If so, stick the resource into
34055331Samw 				 * "share" so it will all just work.
34065331Samw 				 */
34075331Samw 				features = sa_proto_get_featureset(protocol);
34085331Samw 				if (features & SA_FEATURE_RESOURCE)
34095331Samw 					share = (sa_share_t)resource;
34105331Samw 			}
34115331Samw 		}
34125331Samw 
34134653Sdougm 		if (ret == SA_OK) {
34144653Sdougm 			/* group must exist */
34154653Sdougm 			ret = valid_options(optlist, protocol,
34164653Sdougm 			    share == NULL ? group : share, NULL);
34174653Sdougm 			if (ret == SA_OK && !dryrun) {
34184653Sdougm 				if (share != NULL)
34194653Sdougm 					change |= add_optionset(share, optlist,
34204653Sdougm 					    protocol, &ret);
34214653Sdougm 				else
34224653Sdougm 					change |= add_optionset(group, optlist,
34234653Sdougm 					    protocol, &ret);
34244653Sdougm 				if (ret == SA_OK && change)
34254653Sdougm 					worklist = add_list(worklist, group,
34265331Samw 					    share, protocol);
34274653Sdougm 			}
34283034Sdougm 		}
34294653Sdougm 		free_opt(optlist);
34303034Sdougm 	} else {
34313034Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
34323034Sdougm 		ret = SA_NO_SUCH_GROUP;
34333034Sdougm 	}
34343034Sdougm 	/*
34353034Sdougm 	 * we have a group and potentially legal additions
34363034Sdougm 	 */
34373034Sdougm 
34384653Sdougm 	/*
34394653Sdougm 	 * Commit to configuration if not a dryrunp and properties
34404653Sdougm 	 * have changed.
34414653Sdougm 	 */
34424653Sdougm 	if (!dryrun && ret == SA_OK && change && worklist != NULL)
34433034Sdougm 		/* properties changed, so update all shares */
34445331Samw 		(void) enable_all_groups(handle, worklist, 0, 0, protocol,
34455331Samw 		    B_TRUE);
34464653Sdougm 
34473034Sdougm 	if (worklist != NULL)
34484653Sdougm 		free_list(worklist);
34493034Sdougm 	return (ret);
34503034Sdougm }
34513034Sdougm 
34523034Sdougm /*
34533034Sdougm  * space_set(groupname, optlist, protocol, sharepath, dryrun)
34543034Sdougm  *
34553034Sdougm  * This function implements "set" when a name space (-S) is
34563034Sdougm  * specified. It is a namespace set. Options and other CLI parsing has
34573034Sdougm  * already been done.
34583034Sdougm  */
34593034Sdougm 
34603034Sdougm static int
34613910Sdougm space_set(sa_handle_t handle, char *groupname, struct options *optlist,
34625331Samw     char *protocol, char *sharepath, int dryrun, char *sectype)
34633034Sdougm {
34643034Sdougm 	sa_group_t group;
34653034Sdougm 	int ret = SA_OK;
34663034Sdougm 	int change = 0;
34673034Sdougm 	struct list *worklist = NULL;
34683034Sdougm 
34693034Sdougm 	/*
34703034Sdougm 	 * make sure protcol and sectype are valid
34713034Sdougm 	 */
34723034Sdougm 
34733034Sdougm 	if (sa_proto_valid_space(protocol, sectype) == 0) {
34744653Sdougm 		(void) printf(gettext("Option space \"%s\" not valid "
34754653Sdougm 		    "for protocol.\n"), sectype);
34764653Sdougm 		return (SA_INVALID_SECURITY);
34773034Sdougm 	}
34783034Sdougm 
34793910Sdougm 	group = sa_get_group(handle, groupname);
34803034Sdougm 	if (group != NULL) {
34814653Sdougm 		sa_share_t share = NULL;
34824653Sdougm 		if (sharepath != NULL) {
34834653Sdougm 			share = sa_get_share(group, sharepath);
34844653Sdougm 			if (share == NULL) {
34854653Sdougm 				(void) printf(gettext(
34864653Sdougm 				    "Share does not exist in group %s\n"),
34874653Sdougm 				    groupname, sharepath);
34884653Sdougm 				ret = SA_NO_SUCH_PATH;
34895089Sdougm 			} else {
34905089Sdougm 				/* if ZFS and OK, then only group */
34915089Sdougm 				ret = zfscheck(group, share);
34925089Sdougm 				if (ret == SA_OK &&
34935089Sdougm 				    sa_group_is_zfs(group))
34945089Sdougm 					share = NULL;
34955089Sdougm 				if (ret == SA_NOT_ALLOWED)
34965089Sdougm 					(void) printf(gettext(
34975089Sdougm 					    "Properties on ZFS group shares "
34985089Sdougm 					    "not supported: %s\n"), sharepath);
34994653Sdougm 			}
35003034Sdougm 		}
35014653Sdougm 		if (ret == SA_OK) {
35024653Sdougm 			/* group must exist */
35034653Sdougm 			ret = valid_options(optlist, protocol,
35044653Sdougm 			    share == NULL ? group : share, sectype);
35054653Sdougm 			if (ret == SA_OK && !dryrun) {
35064653Sdougm 				if (share != NULL)
35074653Sdougm 					change = add_security(share, sectype,
35084653Sdougm 					    optlist, protocol, &ret);
35094653Sdougm 				else
35104653Sdougm 					change = add_security(group, sectype,
35114653Sdougm 					    optlist, protocol, &ret);
35124653Sdougm 				if (ret != SA_OK)
35134653Sdougm 					(void) printf(gettext(
35144653Sdougm 					    "Could not set property: %s\n"),
35154653Sdougm 					    sa_errorstr(ret));
35164653Sdougm 			}
35174653Sdougm 			if (ret == SA_OK && change)
35185331Samw 				worklist = add_list(worklist, group, share,
35195331Samw 				    protocol);
35203034Sdougm 		}
35214653Sdougm 		free_opt(optlist);
35223034Sdougm 	} else {
35233034Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
35243034Sdougm 		ret = SA_NO_SUCH_GROUP;
35253034Sdougm 	}
35265331Samw 
35273034Sdougm 	/*
35285331Samw 	 * We have a group and potentially legal additions.
35293034Sdougm 	 */
35303034Sdougm 
35314653Sdougm 	/* Commit to configuration if not a dryrun */
35323034Sdougm 	if (!dryrun && ret == 0) {
35334653Sdougm 		if (change && worklist != NULL) {
35344653Sdougm 			/* properties changed, so update all shares */
35354653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
35365331Samw 			    protocol, B_TRUE);
35374653Sdougm 		}
35384653Sdougm 		ret = sa_update_config(handle);
35393034Sdougm 	}
35403034Sdougm 	if (worklist != NULL)
35414653Sdougm 		free_list(worklist);
35423034Sdougm 	return (ret);
35433034Sdougm }
35443034Sdougm 
35453034Sdougm /*
35463034Sdougm  * sa_set(flags, argc, argv)
35473034Sdougm  *
35483034Sdougm  * Implements the set subcommand. It keys off of -S to determine which
35493034Sdougm  * set of operations to actually do.
35503034Sdougm  */
35513034Sdougm 
35523034Sdougm int
35533910Sdougm sa_set(sa_handle_t handle, int flags, int argc, char *argv[])
35543034Sdougm {
35553034Sdougm 	char *groupname;
35563034Sdougm 	int verbose = 0;
35573034Sdougm 	int dryrun = 0;
35583034Sdougm 	int c;
35593034Sdougm 	char *protocol = NULL;
35603034Sdougm 	int ret = SA_OK;
35613034Sdougm 	struct options *optlist = NULL;
35625331Samw 	char *rsrcname = NULL;
35633034Sdougm 	char *sharepath = NULL;
35643034Sdougm 	char *optset = NULL;
35653034Sdougm 	int auth;
35663034Sdougm 
35675331Samw 	while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
35684653Sdougm 		switch (c) {
35694653Sdougm 		case 'v':
35704653Sdougm 			verbose++;
35714653Sdougm 			break;
35724653Sdougm 		case 'n':
35734653Sdougm 			dryrun++;
35744653Sdougm 			break;
35754653Sdougm 		case 'P':
35765331Samw 			if (protocol != NULL) {
35775331Samw 				(void) printf(gettext(
35785331Samw 				    "Specifying multiple protocols "
35795331Samw 				    "not supported: %s\n"), protocol);
35805331Samw 				return (SA_SYNTAX_ERR);
35815331Samw 			}
35824653Sdougm 			protocol = optarg;
35834653Sdougm 			if (!sa_valid_protocol(protocol)) {
35844653Sdougm 				(void) printf(gettext(
35854653Sdougm 				    "Invalid protocol specified: %s\n"),
35864653Sdougm 				    protocol);
35874653Sdougm 				return (SA_INVALID_PROTOCOL);
35884653Sdougm 			}
35894653Sdougm 			break;
35904653Sdougm 		case 'p':
35914653Sdougm 			ret = add_opt(&optlist, optarg, 0);
35924653Sdougm 			switch (ret) {
35934653Sdougm 			case OPT_ADD_SYNTAX:
35944653Sdougm 				(void) printf(gettext("Property syntax error:"
35954653Sdougm 				    " %s\n"), optarg);
35964653Sdougm 				return (SA_SYNTAX_ERR);
35974653Sdougm 			case OPT_ADD_MEMORY:
35984653Sdougm 				(void) printf(gettext("No memory to set "
35994653Sdougm 				    "property: %s\n"), optarg);
36004653Sdougm 				return (SA_NO_MEMORY);
36014653Sdougm 			default:
36024653Sdougm 				break;
36034653Sdougm 			}
36044653Sdougm 			break;
36055331Samw 		case 'r':
36065331Samw 			if (rsrcname != NULL) {
36075331Samw 				(void) printf(gettext(
36085331Samw 				    "Setting multiple resource names not"
36095331Samw 				    " supported\n"));
36105331Samw 				return (SA_SYNTAX_ERR);
36115331Samw 			}
36125331Samw 			rsrcname = optarg;
36135331Samw 			break;
36144653Sdougm 		case 's':
36155331Samw 			if (sharepath != NULL) {
36165331Samw 				(void) printf(gettext(
36175331Samw 				    "Setting multiple shares not supported\n"));
36185331Samw 				return (SA_SYNTAX_ERR);
36195331Samw 			}
36204653Sdougm 			sharepath = optarg;
36214653Sdougm 			break;
36224653Sdougm 		case 'S':
36235331Samw 			if (optset != NULL) {
36245331Samw 				(void) printf(gettext(
36255331Samw 				    "Specifying multiple property "
36265331Samw 				    "spaces not supported: %s\n"), optset);
36275331Samw 				return (SA_SYNTAX_ERR);
36285331Samw 			}
36294653Sdougm 			optset = optarg;
36304653Sdougm 			break;
36314653Sdougm 		default:
36324653Sdougm 		case 'h':
36334653Sdougm 		case '?':
36344653Sdougm 			(void) printf(gettext("usage: %s\n"),
36354653Sdougm 			    sa_get_usage(USAGE_SET));
36364653Sdougm 			return (SA_OK);
36373034Sdougm 		}
36383034Sdougm 	}
36393034Sdougm 
36403034Sdougm 	if (optlist != NULL)
36414653Sdougm 		ret = chk_opt(optlist, optset != NULL, protocol);
36423034Sdougm 
36433034Sdougm 	if (optind >= argc || (optlist == NULL && optset == NULL) ||
36444653Sdougm 	    protocol == NULL || ret != OPT_ADD_OK) {
36454653Sdougm 		char *sep = "\t";
36464653Sdougm 
36474653Sdougm 		(void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET));
36484653Sdougm 		if (optind >= argc) {
36494653Sdougm 			(void) printf(gettext("%sgroup must be specified"),
36504653Sdougm 			    sep);
36514653Sdougm 			sep = ", ";
36524653Sdougm 		}
36534653Sdougm 		if (optlist == NULL) {
36544653Sdougm 			(void) printf(gettext("%sat least one property must be"
36554653Sdougm 			    " specified"), sep);
36564653Sdougm 			sep = ", ";
36574653Sdougm 		}
36584653Sdougm 		if (protocol == NULL) {
36594653Sdougm 			(void) printf(gettext("%sprotocol must be specified"),
36604653Sdougm 			    sep);
36614653Sdougm 			sep = ", ";
36624653Sdougm 		}
36634653Sdougm 		(void) printf("\n");
36644653Sdougm 		ret = SA_SYNTAX_ERR;
36653034Sdougm 	} else {
36663034Sdougm 		/*
36675089Sdougm 		 * Group already exists so we can proceed after a few
36685089Sdougm 		 * additional checks related to ZFS handling.
36693034Sdougm 		 */
36703034Sdougm 
36714653Sdougm 		groupname = argv[optind];
36725089Sdougm 		if (strcmp(groupname, "zfs") == 0) {
36735089Sdougm 			(void) printf(gettext("Changing properties for group "
36745089Sdougm 			    "\"zfs\" not allowed\n"));
36755089Sdougm 			return (SA_NOT_ALLOWED);
36765089Sdougm 		}
36775089Sdougm 
36784653Sdougm 		auth = check_authorizations(groupname, flags);
36794653Sdougm 		if (optset == NULL)
36804653Sdougm 			ret = basic_set(handle, groupname, optlist, protocol,
36815331Samw 			    sharepath, rsrcname, dryrun);
36824653Sdougm 		else
36834653Sdougm 			ret = space_set(handle, groupname, optlist, protocol,
36844653Sdougm 			    sharepath, dryrun, optset);
36854653Sdougm 		if (dryrun && ret == SA_OK && !auth && verbose) {
36864653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
36874653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
36884653Sdougm 		}
36893034Sdougm 	}
36903034Sdougm 	return (ret);
36913034Sdougm }
36923034Sdougm 
36933034Sdougm /*
36943034Sdougm  * remove_options(group, optlist, proto, *err)
36953034Sdougm  *
36964653Sdougm  * Helper function to actually remove options from a group after all
36973034Sdougm  * preprocessing is done.
36983034Sdougm  */
36993034Sdougm 
37003034Sdougm static int
37013034Sdougm remove_options(sa_group_t group, struct options *optlist,
37025331Samw     char *proto, int *err)
37033034Sdougm {
37043034Sdougm 	struct options *cur;
37053034Sdougm 	sa_optionset_t optionset;
37063034Sdougm 	sa_property_t prop;
37073034Sdougm 	int change = 0;
37083034Sdougm 	int ret = SA_OK;
37093034Sdougm 
37103034Sdougm 	optionset = sa_get_optionset(group, proto);
37113034Sdougm 	if (optionset != NULL) {
37124653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
37134653Sdougm 			prop = sa_get_property(optionset, cur->optname);
37144653Sdougm 			if (prop != NULL) {
37154653Sdougm 				ret = sa_remove_property(prop);
37164653Sdougm 				if (ret != SA_OK)
37174653Sdougm 					break;
37184653Sdougm 				change = 1;
37194653Sdougm 			}
37203034Sdougm 		}
37213034Sdougm 	}
37223034Sdougm 	if (ret == SA_OK && change)
37234653Sdougm 		ret = sa_commit_properties(optionset, 0);
37243034Sdougm 
37253034Sdougm 	if (err != NULL)
37264653Sdougm 		*err = ret;
37273034Sdougm 	return (change);
37283034Sdougm }
37293034Sdougm 
37303034Sdougm /*
37313034Sdougm  * valid_unset(group, optlist, proto)
37323034Sdougm  *
37333034Sdougm  * Sanity check the optlist to make sure they can be removed. Issue an
37343034Sdougm  * error if a property doesn't exist.
37353034Sdougm  */
37363034Sdougm 
37373034Sdougm static int
37383034Sdougm valid_unset(sa_group_t group, struct options *optlist, char *proto)
37393034Sdougm {
37403034Sdougm 	struct options *cur;
37413034Sdougm 	sa_optionset_t optionset;
37423034Sdougm 	sa_property_t prop;
37433034Sdougm 	int ret = SA_OK;
37443034Sdougm 
37453034Sdougm 	optionset = sa_get_optionset(group, proto);
37463034Sdougm 	if (optionset != NULL) {
37474653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
37484653Sdougm 			prop = sa_get_property(optionset, cur->optname);
37494653Sdougm 			if (prop == NULL) {
37504653Sdougm 				(void) printf(gettext(
37514653Sdougm 				    "Could not unset property %s: not set\n"),
37524653Sdougm 				    cur->optname);
37534653Sdougm 				ret = SA_NO_SUCH_PROP;
37544653Sdougm 			}
37553034Sdougm 		}
37563034Sdougm 	}
37573034Sdougm 	return (ret);
37583034Sdougm }
37593034Sdougm 
37603034Sdougm /*
37613034Sdougm  * valid_unset_security(group, optlist, proto)
37623034Sdougm  *
37633034Sdougm  * Sanity check the optlist to make sure they can be removed. Issue an
37643034Sdougm  * error if a property doesn't exist.
37653034Sdougm  */
37663034Sdougm 
37673034Sdougm static int
37683034Sdougm valid_unset_security(sa_group_t group, struct options *optlist, char *proto,
37695331Samw     char *sectype)
37703034Sdougm {
37713034Sdougm 	struct options *cur;
37723034Sdougm 	sa_security_t security;
37733034Sdougm 	sa_property_t prop;
37743034Sdougm 	int ret = SA_OK;
37753034Sdougm 	char *sec;
37763034Sdougm 
37773034Sdougm 	sec = sa_proto_space_alias(proto, sectype);
37783034Sdougm 	security = sa_get_security(group, sec, proto);
37793034Sdougm 	if (security != NULL) {
37804653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
37814653Sdougm 			prop = sa_get_property(security, cur->optname);
37824653Sdougm 			if (prop == NULL) {
37834653Sdougm 				(void) printf(gettext(
37844653Sdougm 				    "Could not unset property %s: not set\n"),
37854653Sdougm 				    cur->optname);
37864653Sdougm 				ret = SA_NO_SUCH_PROP;
37874653Sdougm 			}
37883034Sdougm 		}
37893034Sdougm 	} else {
37904653Sdougm 		(void) printf(gettext(
37914653Sdougm 		    "Could not unset %s: space not defined\n"), sectype);
37924653Sdougm 		ret = SA_NO_SUCH_SECURITY;
37933034Sdougm 	}
37943034Sdougm 	if (sec != NULL)
37954653Sdougm 		sa_free_attr_string(sec);
37963034Sdougm 	return (ret);
37973034Sdougm }
37983034Sdougm 
37993034Sdougm /*
38003034Sdougm  * remove_security(group, optlist, proto)
38013034Sdougm  *
38023034Sdougm  * Remove the properties since they were checked as valid.
38033034Sdougm  */
38043034Sdougm 
38053034Sdougm static int
38063034Sdougm remove_security(sa_group_t group, char *sectype,
38075331Samw     struct options *optlist, char *proto, int *err)
38083034Sdougm {
38093034Sdougm 	sa_security_t security;
38103034Sdougm 	int ret = SA_OK;
38113034Sdougm 	int change = 0;
38123034Sdougm 
38133034Sdougm 	sectype = sa_proto_space_alias(proto, sectype);
38143034Sdougm 	security = sa_get_security(group, sectype, proto);
38153034Sdougm 	if (sectype != NULL)
38164653Sdougm 		sa_free_attr_string(sectype);
38173034Sdougm 
38183034Sdougm 	if (security != NULL) {
38194653Sdougm 		while (optlist != NULL) {
38204653Sdougm 			sa_property_t prop;
38214653Sdougm 			prop = sa_get_property(security, optlist->optname);
38224653Sdougm 			if (prop != NULL) {
38234653Sdougm 				ret = sa_remove_property(prop);
38244653Sdougm 				if (ret != SA_OK)
38254653Sdougm 					break;
38264653Sdougm 				change = 1;
38274653Sdougm 			}
38284653Sdougm 			optlist = optlist->next;
38293034Sdougm 		}
38303034Sdougm 		/*
38313034Sdougm 		 * when done, properties may have all been removed but
38323034Sdougm 		 * we need to keep the security type itself until
38333034Sdougm 		 * explicitly removed.
38343034Sdougm 		 */
38354653Sdougm 		if (ret == SA_OK && change)
38364653Sdougm 			ret = sa_commit_properties(security, 0);
38373034Sdougm 	} else {
38384653Sdougm 		ret = SA_NO_SUCH_PROP;
38393034Sdougm 	}
38403034Sdougm 	if (err != NULL)
38414653Sdougm 		*err = ret;
38423034Sdougm 	return (change);
38433034Sdougm }
38443034Sdougm 
38453034Sdougm /*
38465331Samw  * basic_unset(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
38473034Sdougm  *
38484653Sdougm  * Unset non-named optionset properties.
38493034Sdougm  */
38503034Sdougm 
38513034Sdougm static int
38523910Sdougm basic_unset(sa_handle_t handle, char *groupname, struct options *optlist,
38535331Samw     char *protocol, char *sharepath, char *rsrcname, int dryrun)
38543034Sdougm {
38553034Sdougm 	sa_group_t group;
38563034Sdougm 	int ret = SA_OK;
38573034Sdougm 	int change = 0;
38583034Sdougm 	struct list *worklist = NULL;
38594653Sdougm 	sa_share_t share = NULL;
38605331Samw 	sa_resource_t resource = NULL;
38613034Sdougm 
38623910Sdougm 	group = sa_get_group(handle, groupname);
38634653Sdougm 	if (group == NULL)
38644653Sdougm 		return (ret);
38654653Sdougm 
38665331Samw 	/*
38675331Samw 	 * If there is a sharepath, make sure it belongs to
38685331Samw 	 * the group.
38695331Samw 	 */
38704653Sdougm 	if (sharepath != NULL) {
38713034Sdougm 		share = sa_get_share(group, sharepath);
38723034Sdougm 		if (share == NULL) {
38734653Sdougm 			(void) printf(gettext(
38744653Sdougm 			    "Share does not exist in group %s\n"),
38754653Sdougm 			    groupname, sharepath);
38764653Sdougm 			ret = SA_NO_SUCH_PATH;
38773034Sdougm 		}
38784653Sdougm 	}
38795331Samw 	/*
38805331Samw 	 * If a resource name exists, make sure it belongs to
38815331Samw 	 * the share if present else it belongs to the
38825331Samw 	 * group. Also check the protocol to see if it
38835331Samw 	 * supports resource level properties or not. If not,
38845331Samw 	 * use share only.
38855331Samw 	 */
38865331Samw 	if (rsrcname != NULL) {
38875331Samw 		if (share != NULL) {
38885331Samw 			resource = sa_get_share_resource(share, rsrcname);
38895331Samw 			if (resource == NULL)
38905331Samw 				ret = SA_NO_SUCH_RESOURCE;
38915331Samw 		} else {
38925331Samw 			resource = sa_get_resource(group, rsrcname);
38935331Samw 			if (resource != NULL) {
38945331Samw 				share = sa_get_resource_parent(resource);
38955331Samw 			} else {
38965331Samw 				ret = SA_NO_SUCH_RESOURCE;
38975331Samw 			}
38985331Samw 		}
38995331Samw 		if (ret == SA_OK && resource != NULL) {
39005331Samw 			uint64_t features;
39015331Samw 			/*
39025331Samw 			 * Check to see if the resource can take
39035331Samw 			 * properties. If so, stick the resource into
39045331Samw 			 * "share" so it will all just work.
39055331Samw 			 */
39065331Samw 			features = sa_proto_get_featureset(protocol);
39075331Samw 			if (features & SA_FEATURE_RESOURCE)
39085331Samw 				share = (sa_share_t)resource;
39095331Samw 		}
39105331Samw 	}
39115331Samw 
39124653Sdougm 	if (ret == SA_OK) {
39133034Sdougm 		/* group must exist */
39143034Sdougm 		ret = valid_unset(share != NULL ? share : group,
39154653Sdougm 		    optlist, protocol);
39163034Sdougm 		if (ret == SA_OK && !dryrun) {
39174653Sdougm 			if (share != NULL) {
39184653Sdougm 				sa_optionset_t optionset;
39194653Sdougm 				sa_property_t prop;
39204653Sdougm 				change |= remove_options(share, optlist,
39214653Sdougm 				    protocol, &ret);
39224653Sdougm 				/*
39234653Sdougm 				 * If a share optionset is
39244653Sdougm 				 * empty, remove it.
39254653Sdougm 				 */
39264653Sdougm 				optionset = sa_get_optionset((sa_share_t)share,
39274653Sdougm 				    protocol);
39284653Sdougm 				if (optionset != NULL) {
39294653Sdougm 					prop = sa_get_property(optionset, NULL);
39304653Sdougm 					if (prop == NULL)
39314653Sdougm 						(void) sa_destroy_optionset(
39324653Sdougm 						    optionset);
39334653Sdougm 				}
39344653Sdougm 			} else {
39354653Sdougm 				change |= remove_options(group,
39364653Sdougm 				    optlist, protocol, &ret);
39373034Sdougm 			}
39384653Sdougm 			if (ret == SA_OK && change)
39395331Samw 				worklist = add_list(worklist, group, share,
39405331Samw 				    protocol);
39414653Sdougm 			if (ret != SA_OK)
39424653Sdougm 				(void) printf(gettext(
39434653Sdougm 				    "Could not remove properties: "
39444653Sdougm 				    "%s\n"), sa_errorstr(ret));
39453034Sdougm 		}
39464653Sdougm 	} else {
39475331Samw 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
39483034Sdougm 		ret = SA_NO_SUCH_GROUP;
39493034Sdougm 	}
39504653Sdougm 	free_opt(optlist);
39513034Sdougm 
39523034Sdougm 	/*
39534653Sdougm 	 * We have a group and potentially legal additions
39544653Sdougm 	 *
39554653Sdougm 	 * Commit to configuration if not a dryrun
39563034Sdougm 	 */
39573034Sdougm 	if (!dryrun && ret == SA_OK) {
39584653Sdougm 		if (change && worklist != NULL) {
39594653Sdougm 			/* properties changed, so update all shares */
39604653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
39615331Samw 			    protocol, B_TRUE);
39624653Sdougm 		}
39633034Sdougm 	}
39643034Sdougm 	if (worklist != NULL)
39654653Sdougm 		free_list(worklist);
39663034Sdougm 	return (ret);
39673034Sdougm }
39683034Sdougm 
39693034Sdougm /*
39703034Sdougm  * space_unset(groupname, optlist, protocol, sharepath, dryrun)
39713034Sdougm  *
39724653Sdougm  * Unset named optionset properties.
39733034Sdougm  */
39743034Sdougm static int
39753910Sdougm space_unset(sa_handle_t handle, char *groupname, struct options *optlist,
39765331Samw     char *protocol, char *sharepath, int dryrun, char *sectype)
39773034Sdougm {
39783034Sdougm 	sa_group_t group;
39793034Sdougm 	int ret = SA_OK;
39803034Sdougm 	int change = 0;
39813034Sdougm 	struct list *worklist = NULL;
39824653Sdougm 	sa_share_t share = NULL;
39833034Sdougm 
39843910Sdougm 	group = sa_get_group(handle, groupname);
39854653Sdougm 	if (group == NULL) {
39864653Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
39874653Sdougm 		return (SA_NO_SUCH_GROUP);
39884653Sdougm 	}
39894653Sdougm 	if (sharepath != NULL) {
39903034Sdougm 		share = sa_get_share(group, sharepath);
39913034Sdougm 		if (share == NULL) {
39924653Sdougm 			(void) printf(gettext(
39934653Sdougm 			    "Share does not exist in group %s\n"),
39944653Sdougm 			    groupname, sharepath);
39954653Sdougm 			return (SA_NO_SUCH_PATH);
39963034Sdougm 		}
39974653Sdougm 	}
39985331Samw 	ret = valid_unset_security(share != NULL ? share : group,
39995331Samw 	    optlist, protocol, sectype);
40004653Sdougm 
40014653Sdougm 	if (ret == SA_OK && !dryrun) {
40024653Sdougm 		if (optlist != NULL) {
40033034Sdougm 			if (share != NULL) {
40044653Sdougm 				sa_security_t optionset;
40054653Sdougm 				sa_property_t prop;
40064653Sdougm 				change = remove_security(share,
40074653Sdougm 				    sectype, optlist, protocol, &ret);
40084653Sdougm 
40094653Sdougm 				/* If a share security is empty, remove it */
40104653Sdougm 				optionset = sa_get_security((sa_group_t)share,
40114653Sdougm 				    sectype, protocol);
40124653Sdougm 				if (optionset != NULL) {
40134653Sdougm 					prop = sa_get_property(optionset,
40144653Sdougm 					    NULL);
40154653Sdougm 					if (prop == NULL)
40164653Sdougm 						ret = sa_destroy_security(
40174653Sdougm 						    optionset);
40184653Sdougm 				}
40193034Sdougm 			} else {
40204653Sdougm 				change = remove_security(group, sectype,
40214653Sdougm 				    optlist, protocol, &ret);
40223034Sdougm 			}
40234653Sdougm 		} else {
40243034Sdougm 			sa_security_t security;
40253034Sdougm 			char *sec;
40263034Sdougm 			sec = sa_proto_space_alias(protocol, sectype);
40273034Sdougm 			security = sa_get_security(group, sec, protocol);
40283034Sdougm 			if (sec != NULL)
40294653Sdougm 				sa_free_attr_string(sec);
40303034Sdougm 			if (security != NULL) {
40314653Sdougm 				ret = sa_destroy_security(security);
40324653Sdougm 				if (ret == SA_OK)
40334653Sdougm 					change = 1;
40343034Sdougm 			} else {
40354653Sdougm 				ret = SA_NO_SUCH_PROP;
40363034Sdougm 			}
40374653Sdougm 		}
40384653Sdougm 		if (ret != SA_OK)
40393034Sdougm 			(void) printf(gettext("Could not unset property: %s\n"),
40404653Sdougm 			    sa_errorstr(ret));
40413034Sdougm 	}
40424653Sdougm 
40434653Sdougm 	if (ret == SA_OK && change)
40445331Samw 		worklist = add_list(worklist, group, 0, protocol);
40454653Sdougm 
40463034Sdougm 	free_opt(optlist);
40473034Sdougm 	/*
40484653Sdougm 	 * We have a group and potentially legal additions
40493034Sdougm 	 */
40503034Sdougm 
40514653Sdougm 	/* Commit to configuration if not a dryrun */
40523034Sdougm 	if (!dryrun && ret == 0) {
40533034Sdougm 		/* properties changed, so update all shares */
40544653Sdougm 		if (change && worklist != NULL)
40554653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
40565331Samw 			    protocol, B_TRUE);
40574653Sdougm 		ret = sa_update_config(handle);
40583034Sdougm 	}
40593034Sdougm 	if (worklist != NULL)
40604653Sdougm 		free_list(worklist);
40613034Sdougm 	return (ret);
40623034Sdougm }
40633034Sdougm 
40643034Sdougm /*
40653034Sdougm  * sa_unset(flags, argc, argv)
40663034Sdougm  *
40674653Sdougm  * Implements the unset subcommand. Parsing done here and then basic
40683034Sdougm  * or space versions of the real code are called.
40693034Sdougm  */
40703034Sdougm 
40713034Sdougm int
40723910Sdougm sa_unset(sa_handle_t handle, int flags, int argc, char *argv[])
40733034Sdougm {
40743034Sdougm 	char *groupname;
40753034Sdougm 	int verbose = 0;
40763034Sdougm 	int dryrun = 0;
40773034Sdougm 	int c;
40783034Sdougm 	char *protocol = NULL;
40793034Sdougm 	int ret = SA_OK;
40803034Sdougm 	struct options *optlist = NULL;
40815331Samw 	char *rsrcname = NULL;
40823034Sdougm 	char *sharepath = NULL;
40833034Sdougm 	char *optset = NULL;
40843034Sdougm 	int auth;
40853034Sdougm 
40865331Samw 	while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
40874653Sdougm 		switch (c) {
40884653Sdougm 		case 'v':
40894653Sdougm 			verbose++;
40904653Sdougm 			break;
40914653Sdougm 		case 'n':
40924653Sdougm 			dryrun++;
40934653Sdougm 			break;
40944653Sdougm 		case 'P':
40955331Samw 			if (protocol != NULL) {
40965331Samw 				(void) printf(gettext(
40975331Samw 				    "Specifying multiple protocols "
40985331Samw 				    "not supported: %s\n"), protocol);
40995331Samw 				return (SA_SYNTAX_ERR);
41005331Samw 			}
41014653Sdougm 			protocol = optarg;
41024653Sdougm 			if (!sa_valid_protocol(protocol)) {
41034653Sdougm 				(void) printf(gettext(
41044653Sdougm 				    "Invalid protocol specified: %s\n"),
41054653Sdougm 				    protocol);
41064653Sdougm 				return (SA_INVALID_PROTOCOL);
41074653Sdougm 			}
41084653Sdougm 			break;
41094653Sdougm 		case 'p':
41104653Sdougm 			ret = add_opt(&optlist, optarg, 1);
41114653Sdougm 			switch (ret) {
41124653Sdougm 			case OPT_ADD_SYNTAX:
41134653Sdougm 				(void) printf(gettext("Property syntax error "
41144653Sdougm 				    "for property %s\n"), optarg);
41154653Sdougm 				return (SA_SYNTAX_ERR);
41164653Sdougm 
41174653Sdougm 			case OPT_ADD_PROPERTY:
41184653Sdougm 				(void) printf(gettext("Properties need to be "
41194653Sdougm 				    "set with set command: %s\n"), optarg);
41204653Sdougm 				return (SA_SYNTAX_ERR);
41214653Sdougm 
41224653Sdougm 			default:
41234653Sdougm 				break;
41244653Sdougm 			}
41254653Sdougm 			break;
41265331Samw 		case 'r':
41275331Samw 			/*
41285331Samw 			 * Unset properties on resource if applicable or on
41295331Samw 			 * share if resource for this protocol doesn't use
41305331Samw 			 * resources.
41315331Samw 			 */
41325331Samw 			if (rsrcname != NULL) {
41335331Samw 				(void) printf(gettext(
41345331Samw 				    "Unsetting multiple resource "
41355331Samw 				    "names not supported\n"));
41365331Samw 				return (SA_SYNTAX_ERR);
41375331Samw 			}
41385331Samw 			rsrcname = optarg;
41395331Samw 			break;
41404653Sdougm 		case 's':
41415331Samw 			if (sharepath != NULL) {
41425331Samw 				(void) printf(gettext(
41435331Samw 				    "Adding multiple shares not supported\n"));
41445331Samw 				return (SA_SYNTAX_ERR);
41455331Samw 			}
41464653Sdougm 			sharepath = optarg;
41474653Sdougm 			break;
41484653Sdougm 		case 'S':
41495331Samw 			if (optset != NULL) {
41505331Samw 				(void) printf(gettext(
41515331Samw 				    "Specifying multiple property "
41525331Samw 				    "spaces not supported: %s\n"), optset);
41535331Samw 				return (SA_SYNTAX_ERR);
41545331Samw 			}
41554653Sdougm 			optset = optarg;
41564653Sdougm 			break;
41574653Sdougm 		default:
41584653Sdougm 		case 'h':
41594653Sdougm 		case '?':
41604653Sdougm 			(void) printf(gettext("usage: %s\n"),
41614653Sdougm 			    sa_get_usage(USAGE_UNSET));
41624653Sdougm 			return (SA_OK);
41633034Sdougm 		}
41643034Sdougm 	}
41653034Sdougm 
41663034Sdougm 	if (optlist != NULL)
41674653Sdougm 		ret = chk_opt(optlist, optset != NULL, protocol);
41683034Sdougm 
41693034Sdougm 	if (optind >= argc || (optlist == NULL && optset == NULL) ||
41703034Sdougm 	    protocol == NULL) {
41714653Sdougm 		char *sep = "\t";
41724653Sdougm 		(void) printf(gettext("usage: %s\n"),
41734653Sdougm 		    sa_get_usage(USAGE_UNSET));
41744653Sdougm 		if (optind >= argc) {
41754653Sdougm 			(void) printf(gettext("%sgroup must be specified"),
41764653Sdougm 			    sep);
41774653Sdougm 			sep = ", ";
41784653Sdougm 		}
41794653Sdougm 		if (optlist == NULL) {
41804653Sdougm 			(void) printf(gettext("%sat least one property must "
41814653Sdougm 			    "be specified"), sep);
41824653Sdougm 			sep = ", ";
41834653Sdougm 		}
41844653Sdougm 		if (protocol == NULL) {
41854653Sdougm 			(void) printf(gettext("%sprotocol must be specified"),
41864653Sdougm 			    sep);
41874653Sdougm 			sep = ", ";
41884653Sdougm 		}
41894653Sdougm 		(void) printf("\n");
41904653Sdougm 		ret = SA_SYNTAX_ERR;
41913034Sdougm 	} else {
41923034Sdougm 
41933034Sdougm 		/*
41944653Sdougm 		 * If a group already exists, we can only add a new
41953034Sdougm 		 * protocol to it and not create a new one or add the
41963034Sdougm 		 * same protocol again.
41973034Sdougm 		 */
41983034Sdougm 
41994653Sdougm 		groupname = argv[optind];
42004653Sdougm 		auth = check_authorizations(groupname, flags);
42014653Sdougm 		if (optset == NULL)
42024653Sdougm 			ret = basic_unset(handle, groupname, optlist, protocol,
42035331Samw 			    sharepath, rsrcname, dryrun);
42044653Sdougm 		else
42054653Sdougm 			ret = space_unset(handle, groupname, optlist, protocol,
42064653Sdougm 			    sharepath, dryrun, optset);
42074653Sdougm 
42084653Sdougm 		if (dryrun && ret == SA_OK && !auth && verbose)
42094653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
42104653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
42113034Sdougm 	}
42123034Sdougm 	return (ret);
42133034Sdougm }
42143034Sdougm 
42153034Sdougm /*
42163034Sdougm  * sa_enable_group(flags, argc, argv)
42173034Sdougm  *
42183034Sdougm  * Implements the enable subcommand
42193034Sdougm  */
42203034Sdougm 
42213034Sdougm int
42223910Sdougm sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[])
42233034Sdougm {
42243034Sdougm 	int verbose = 0;
42253034Sdougm 	int dryrun = 0;
42263034Sdougm 	int all = 0;
42273034Sdougm 	int c;
42283034Sdougm 	int ret = SA_OK;
42293034Sdougm 	char *protocol = NULL;
42303034Sdougm 	char *state;
42313034Sdougm 	struct list *worklist = NULL;
42323034Sdougm 	int auth = 1;
42334653Sdougm 	sa_group_t group;
42343034Sdougm 
42353034Sdougm 	while ((c = getopt(argc, argv, "?havnP:")) != EOF) {
42364653Sdougm 		switch (c) {
42374653Sdougm 		case 'a':
42384653Sdougm 			all = 1;
42394653Sdougm 			break;
42404653Sdougm 		case 'n':
42414653Sdougm 			dryrun++;
42424653Sdougm 			break;
42434653Sdougm 		case 'P':
42445331Samw 			if (protocol != NULL) {
42455331Samw 				(void) printf(gettext(
42465331Samw 				    "Specifying multiple protocols "
42475331Samw 				    "not supported: %s\n"), protocol);
42485331Samw 				return (SA_SYNTAX_ERR);
42495331Samw 			}
42504653Sdougm 			protocol = optarg;
42514653Sdougm 			if (!sa_valid_protocol(protocol)) {
42524653Sdougm 				(void) printf(gettext(
42534653Sdougm 				    "Invalid protocol specified: %s\n"),
42543034Sdougm 				    protocol);
42554653Sdougm 				return (SA_INVALID_PROTOCOL);
42564653Sdougm 			}
42574653Sdougm 			break;
42584653Sdougm 		case 'v':
42594653Sdougm 			verbose++;
42604653Sdougm 			break;
42614653Sdougm 		default:
42624653Sdougm 		case 'h':
42634653Sdougm 		case '?':
42644653Sdougm 			(void) printf(gettext("usage: %s\n"),
42654653Sdougm 			    sa_get_usage(USAGE_ENABLE));
42664653Sdougm 			return (0);
42673034Sdougm 		}
42683034Sdougm 	}
42693034Sdougm 
42703034Sdougm 	if (optind == argc && !all) {
42714653Sdougm 		(void) printf(gettext("usage: %s\n"),
42724653Sdougm 		    sa_get_usage(USAGE_ENABLE));
42734653Sdougm 		(void) printf(gettext("\tmust specify group\n"));
42744653Sdougm 		return (SA_NO_SUCH_PATH);
42754653Sdougm 	}
42764653Sdougm 	if (!all) {
42773034Sdougm 		while (optind < argc) {
42784653Sdougm 			group = sa_get_group(handle, argv[optind]);
42794653Sdougm 			if (group != NULL) {
42804653Sdougm 				auth &= check_authorizations(argv[optind],
42814653Sdougm 				    flags);
42824653Sdougm 				state = sa_get_group_attr(group, "state");
42834653Sdougm 				if (state != NULL &&
42844653Sdougm 				    strcmp(state, "enabled") == 0) {
42854653Sdougm 					/* already enabled */
42864653Sdougm 					if (verbose)
42874653Sdougm 						(void) printf(gettext(
42884653Sdougm 						    "Group \"%s\" is already "
42894653Sdougm 						    "enabled\n"),
42904653Sdougm 						    argv[optind]);
42914653Sdougm 					ret = SA_BUSY; /* already enabled */
42924653Sdougm 				} else {
42934653Sdougm 					worklist = add_list(worklist, group,
42945331Samw 					    0, protocol);
42954653Sdougm 					if (verbose)
42964653Sdougm 						(void) printf(gettext(
42974653Sdougm 						    "Enabling group \"%s\"\n"),
42984653Sdougm 						    argv[optind]);
42994653Sdougm 				}
43004653Sdougm 				if (state != NULL)
43014653Sdougm 					sa_free_attr_string(state);
43023034Sdougm 			} else {
43034653Sdougm 				ret = SA_NO_SUCH_GROUP;
43043034Sdougm 			}
43054653Sdougm 			optind++;
43063034Sdougm 		}
43074653Sdougm 	} else {
43084653Sdougm 		for (group = sa_get_group(handle, NULL);
43094653Sdougm 		    group != NULL;
43103034Sdougm 		    group = sa_get_next_group(group)) {
43115331Samw 			worklist = add_list(worklist, group, 0, protocol);
43123034Sdougm 		}
43134653Sdougm 	}
43144653Sdougm 	if (!dryrun && ret == SA_OK)
43155331Samw 		ret = enable_all_groups(handle, worklist, 1, 0, NULL, B_FALSE);
43164653Sdougm 
43174653Sdougm 	if (ret != SA_OK && ret != SA_BUSY)
43183034Sdougm 		(void) printf(gettext("Could not enable group: %s\n"),
43194653Sdougm 		    sa_errorstr(ret));
43204653Sdougm 	if (ret == SA_BUSY)
43213034Sdougm 		ret = SA_OK;
43224653Sdougm 
43233034Sdougm 	if (worklist != NULL)
43244653Sdougm 		free_list(worklist);
43253034Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose) {
43264653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
43274653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
43283034Sdougm 	}
43293034Sdougm 	return (ret);
43303034Sdougm }
43313034Sdougm 
43323034Sdougm /*
43335331Samw  * disable_group(group, proto)
43343034Sdougm  *
43355331Samw  * Disable all the shares in the specified group.. This is a helper
43365331Samw  * for disable_all_groups in order to simplify regular and subgroup
43375331Samw  * (zfs) disabling. Group has already been checked for non-NULL.
43383034Sdougm  */
43393034Sdougm 
43403034Sdougm static int
43415331Samw disable_group(sa_group_t group, char *proto)
43423034Sdougm {
43433034Sdougm 	sa_share_t share;
43443034Sdougm 	int ret = SA_OK;
43453034Sdougm 
43465331Samw 	/*
43475331Samw 	 * If the protocol isn't enabled, skip it and treat as
43485331Samw 	 * successful.
43495331Samw 	 */
43505331Samw 	if (!has_protocol(group, proto))
43515331Samw 		return (ret);
43525331Samw 
43533034Sdougm 	for (share = sa_get_share(group, NULL);
43543034Sdougm 	    share != NULL && ret == SA_OK;
43553034Sdougm 	    share = sa_get_next_share(share)) {
43565331Samw 		ret = sa_disable_share(share, proto);
43574653Sdougm 		if (ret == SA_NO_SUCH_PATH) {
43584653Sdougm 			/*
43594653Sdougm 			 * this is OK since the path is gone. we can't
43604653Sdougm 			 * re-share it anyway so no error.
43614653Sdougm 			 */
43624653Sdougm 			ret = SA_OK;
43634653Sdougm 		}
43643034Sdougm 	}
43653034Sdougm 	return (ret);
43663034Sdougm }
43673034Sdougm 
43683034Sdougm /*
43693034Sdougm  * disable_all_groups(work, setstate)
43703034Sdougm  *
43713034Sdougm  * helper function that disables the shares in the list of groups
43723034Sdougm  * provided. It optionally marks the group as disabled. Used by both
43733034Sdougm  * enable and start subcommands.
43743034Sdougm  */
43753034Sdougm 
43763034Sdougm static int
43773910Sdougm disable_all_groups(sa_handle_t handle, struct list *work, int setstate)
43783034Sdougm {
43793034Sdougm 	int ret = SA_OK;
43803034Sdougm 	sa_group_t subgroup, group;
43813034Sdougm 
43823034Sdougm 	while (work != NULL && ret == SA_OK) {
43834653Sdougm 		group = (sa_group_t)work->item;
43844653Sdougm 		if (setstate)
43854653Sdougm 			ret = sa_set_group_attr(group, "state", "disabled");
43864653Sdougm 		if (ret == SA_OK) {
43874653Sdougm 			char *name;
43884653Sdougm 			name = sa_get_group_attr(group, "name");
43894653Sdougm 			if (name != NULL && strcmp(name, "zfs") == 0) {
43904653Sdougm 				/* need to get the sub-groups for stopping */
43914653Sdougm 				for (subgroup = sa_get_sub_group(group);
43924653Sdougm 				    subgroup != NULL;
43934653Sdougm 				    subgroup = sa_get_next_group(subgroup)) {
43945331Samw 					ret = disable_group(subgroup,
43955331Samw 					    work->proto);
43964653Sdougm 				}
43974653Sdougm 			} else {
43985331Samw 				ret = disable_group(group, work->proto);
43994653Sdougm 			}
44004653Sdougm 			/*
44014653Sdougm 			 * We don't want to "disable" since it won't come
44024653Sdougm 			 * up after a reboot.  The SMF framework should do
44034653Sdougm 			 * the right thing. On enable we do want to do
44044653Sdougm 			 * something.
44054653Sdougm 			 */
44063034Sdougm 		}
44074653Sdougm 		work = work->next;
44083034Sdougm 	}
44093034Sdougm 	if (ret == SA_OK)
44104653Sdougm 		ret = sa_update_config(handle);
44113034Sdougm 	return (ret);
44123034Sdougm }
44133034Sdougm 
44143034Sdougm /*
44153034Sdougm  * sa_disable_group(flags, argc, argv)
44163034Sdougm  *
44173034Sdougm  * Implements the disable subcommand
44183034Sdougm  */
44193034Sdougm 
44203034Sdougm int
44213910Sdougm sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[])
44223034Sdougm {
44233034Sdougm 	int verbose = 0;
44243034Sdougm 	int dryrun = 0;
44253034Sdougm 	int all = 0;
44263034Sdougm 	int c;
44273034Sdougm 	int ret = SA_OK;
44285331Samw 	char *protocol = NULL;
44293034Sdougm 	char *state;
44303034Sdougm 	struct list *worklist = NULL;
44314653Sdougm 	sa_group_t group;
44323034Sdougm 	int auth = 1;
44333034Sdougm 
44343034Sdougm 	while ((c = getopt(argc, argv, "?havn")) != EOF) {
44354653Sdougm 		switch (c) {
44364653Sdougm 		case 'a':
44374653Sdougm 			all = 1;
44384653Sdougm 			break;
44394653Sdougm 		case 'n':
44404653Sdougm 			dryrun++;
44414653Sdougm 			break;
44424653Sdougm 		case 'P':
44435331Samw 			if (protocol != NULL) {
44445331Samw 				(void) printf(gettext(
44455331Samw 				    "Specifying multiple protocols "
44465331Samw 				    "not supported: %s\n"), protocol);
44475331Samw 				return (SA_SYNTAX_ERR);
44485331Samw 			}
44494653Sdougm 			protocol = optarg;
44504653Sdougm 			if (!sa_valid_protocol(protocol)) {
44514653Sdougm 				(void) printf(gettext(
44524653Sdougm 				    "Invalid protocol specified: %s\n"),
44534653Sdougm 				    protocol);
44544653Sdougm 				return (SA_INVALID_PROTOCOL);
44554653Sdougm 			}
44564653Sdougm 			break;
44574653Sdougm 		case 'v':
44584653Sdougm 			verbose++;
44594653Sdougm 			break;
44604653Sdougm 		default:
44614653Sdougm 		case 'h':
44624653Sdougm 		case '?':
44634653Sdougm 			(void) printf(gettext("usage: %s\n"),
44644653Sdougm 			    sa_get_usage(USAGE_DISABLE));
44654653Sdougm 			return (0);
44663034Sdougm 		}
44673034Sdougm 	}
44683034Sdougm 
44693034Sdougm 	if (optind == argc && !all) {
44703034Sdougm 		(void) printf(gettext("usage: %s\n"),
44714653Sdougm 		    sa_get_usage(USAGE_DISABLE));
44723034Sdougm 		(void) printf(gettext("\tmust specify group\n"));
44734653Sdougm 		return (SA_NO_SUCH_PATH);
44744653Sdougm 	}
44754653Sdougm 	if (!all) {
44764653Sdougm 		while (optind < argc) {
44773910Sdougm 			group = sa_get_group(handle, argv[optind]);
44783034Sdougm 			if (group != NULL) {
44794653Sdougm 				auth &= check_authorizations(argv[optind],
44804653Sdougm 				    flags);
44814653Sdougm 				state = sa_get_group_attr(group, "state");
44824653Sdougm 				if (state == NULL ||
44834653Sdougm 				    strcmp(state, "disabled") == 0) {
44844653Sdougm 					/* already disabled */
44854653Sdougm 					if (verbose)
44864653Sdougm 						(void) printf(gettext(
44874653Sdougm 						    "Group \"%s\" is "
44884653Sdougm 						    "already disabled\n"),
44894653Sdougm 						    argv[optind]);
44905331Samw 					ret = SA_BUSY; /* already disabled */
44914653Sdougm 				} else {
44925331Samw 					worklist = add_list(worklist, group, 0,
44935331Samw 					    protocol);
44944653Sdougm 					if (verbose)
44954653Sdougm 						(void) printf(gettext(
44964653Sdougm 						    "Disabling group "
44974653Sdougm 						    "\"%s\"\n"), argv[optind]);
44984653Sdougm 				}
44994653Sdougm 				if (state != NULL)
45004653Sdougm 					sa_free_attr_string(state);
45013034Sdougm 			} else {
45024653Sdougm 				ret = SA_NO_SUCH_GROUP;
45033034Sdougm 			}
45043034Sdougm 			optind++;
45054653Sdougm 		}
45064653Sdougm 	} else {
45074653Sdougm 		for (group = sa_get_group(handle, NULL);
45084653Sdougm 		    group != NULL;
45094653Sdougm 		    group = sa_get_next_group(group))
45105331Samw 			worklist = add_list(worklist, group, 0, protocol);
45113034Sdougm 	}
45124653Sdougm 
45134653Sdougm 	if (ret == SA_OK && !dryrun)
45144653Sdougm 		ret = disable_all_groups(handle, worklist, 1);
45154653Sdougm 	if (ret != SA_OK && ret != SA_BUSY)
45164653Sdougm 		(void) printf(gettext("Could not disable group: %s\n"),
45174653Sdougm 		    sa_errorstr(ret));
45184653Sdougm 	if (ret == SA_BUSY)
45194653Sdougm 		ret = SA_OK;
45203034Sdougm 	if (worklist != NULL)
45214653Sdougm 		free_list(worklist);
45224653Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose)
45234653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
45244653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
45253034Sdougm 	return (ret);
45263034Sdougm }
45273034Sdougm 
45283034Sdougm /*
45293034Sdougm  * sa_start_group(flags, argc, argv)
45303034Sdougm  *
45313034Sdougm  * Implements the start command.
45323034Sdougm  * This is similar to enable except it doesn't change the state
45333034Sdougm  * of the group(s) and only enables shares if the group is already
45343034Sdougm  * enabled.
45353034Sdougm  */
45365331Samw 
45373034Sdougm int
45383910Sdougm sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[])
45393034Sdougm {
45403034Sdougm 	int verbose = 0;
45413034Sdougm 	int all = 0;
45423034Sdougm 	int c;
45433034Sdougm 	int ret = SMF_EXIT_OK;
45443034Sdougm 	char *protocol = NULL;
45453034Sdougm 	char *state;
45463034Sdougm 	struct list *worklist = NULL;
45474653Sdougm 	sa_group_t group;
45485331Samw #ifdef lint
45495331Samw 	flags = flags;
45505331Samw #endif
45513034Sdougm 
45523034Sdougm 	while ((c = getopt(argc, argv, "?havP:")) != EOF) {
45534653Sdougm 		switch (c) {
45544653Sdougm 		case 'a':
45554653Sdougm 			all = 1;
45564653Sdougm 			break;
45574653Sdougm 		case 'P':
45585331Samw 			if (protocol != NULL) {
45595331Samw 				(void) printf(gettext(
45605331Samw 				    "Specifying multiple protocols "
45615331Samw 				    "not supported: %s\n"), protocol);
45625331Samw 				return (SA_SYNTAX_ERR);
45635331Samw 			}
45644653Sdougm 			protocol = optarg;
45654653Sdougm 			if (!sa_valid_protocol(protocol)) {
45664653Sdougm 				(void) printf(gettext(
45674653Sdougm 				    "Invalid protocol specified: %s\n"),
45683034Sdougm 				    protocol);
45694653Sdougm 				return (SA_INVALID_PROTOCOL);
45704653Sdougm 			}
45714653Sdougm 			break;
45724653Sdougm 		case 'v':
45734653Sdougm 			verbose++;
45744653Sdougm 			break;
45754653Sdougm 		default:
45764653Sdougm 		case 'h':
45774653Sdougm 		case '?':
45784653Sdougm 			(void) printf(gettext("usage: %s\n"),
45794653Sdougm 			    sa_get_usage(USAGE_START));
45804653Sdougm 			return (SA_OK);
45813034Sdougm 		}
45823034Sdougm 	}
45833034Sdougm 
45843034Sdougm 	if (optind == argc && !all) {
45853034Sdougm 		(void) printf(gettext("usage: %s\n"),
45864653Sdougm 		    sa_get_usage(USAGE_START));
45874653Sdougm 		return (SMF_EXIT_ERR_FATAL);
45884653Sdougm 	}
45894653Sdougm 
45904653Sdougm 	if (!all) {
45914653Sdougm 		while (optind < argc) {
45923910Sdougm 			group = sa_get_group(handle, argv[optind]);
45933034Sdougm 			if (group != NULL) {
45944653Sdougm 				state = sa_get_group_attr(group, "state");
45954653Sdougm 				if (state == NULL ||
45964653Sdougm 				    strcmp(state, "enabled") == 0) {
45975331Samw 					worklist = add_list(worklist, group, 0,
45985331Samw 					    protocol);
45994653Sdougm 					if (verbose)
46004653Sdougm 						(void) printf(gettext(
46014653Sdougm 						    "Starting group \"%s\"\n"),
46024653Sdougm 						    argv[optind]);
46034653Sdougm 				} else {
46044653Sdougm 					/*
46054653Sdougm 					 * Determine if there are any
46065331Samw 					 * protocols.  If there aren't any,
46074653Sdougm 					 * then there isn't anything to do in
46084653Sdougm 					 * any case so no error.
46094653Sdougm 					 */
46104653Sdougm 					if (sa_get_optionset(group,
46114653Sdougm 					    protocol) != NULL) {
46124653Sdougm 						ret = SMF_EXIT_OK;
46134653Sdougm 					}
46143034Sdougm 				}
46154653Sdougm 				if (state != NULL)
46164653Sdougm 					sa_free_attr_string(state);
46173034Sdougm 			}
46183034Sdougm 			optind++;
46194653Sdougm 		}
46204653Sdougm 	} else {
46215331Samw 		for (group = sa_get_group(handle, NULL);
46225331Samw 		    group != NULL;
46234653Sdougm 		    group = sa_get_next_group(group)) {
46243034Sdougm 			state = sa_get_group_attr(group, "state");
46253034Sdougm 			if (state == NULL || strcmp(state, "enabled") == 0)
46265331Samw 				worklist = add_list(worklist, group, 0,
46275331Samw 				    protocol);
46283034Sdougm 			if (state != NULL)
46294653Sdougm 				sa_free_attr_string(state);
46303034Sdougm 		}
46313034Sdougm 	}
46324653Sdougm 
46335331Samw 	(void) enable_all_groups(handle, worklist, 0, 1, protocol, B_FALSE);
46344653Sdougm 
46353034Sdougm 	if (worklist != NULL)
46364653Sdougm 		free_list(worklist);
46373034Sdougm 	return (ret);
46383034Sdougm }
46393034Sdougm 
46403034Sdougm /*
46413034Sdougm  * sa_stop_group(flags, argc, argv)
46423034Sdougm  *
46433034Sdougm  * Implements the stop command.
46443034Sdougm  * This is similar to disable except it doesn't change the state
46453034Sdougm  * of the group(s) and only disables shares if the group is already
46463034Sdougm  * enabled.
46473034Sdougm  */
46483034Sdougm int
46493910Sdougm sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[])
46503034Sdougm {
46513034Sdougm 	int verbose = 0;
46523034Sdougm 	int all = 0;
46533034Sdougm 	int c;
46543034Sdougm 	int ret = SMF_EXIT_OK;
46553034Sdougm 	char *protocol = NULL;
46563034Sdougm 	char *state;
46573034Sdougm 	struct list *worklist = NULL;
46584653Sdougm 	sa_group_t group;
46595331Samw #ifdef lint
46605331Samw 	flags = flags;
46615331Samw #endif
46623034Sdougm 
46633034Sdougm 	while ((c = getopt(argc, argv, "?havP:")) != EOF) {
46644653Sdougm 		switch (c) {
46654653Sdougm 		case 'a':
46664653Sdougm 			all = 1;
46674653Sdougm 			break;
46684653Sdougm 		case 'P':
46695331Samw 			if (protocol != NULL) {
46705331Samw 				(void) printf(gettext(
46715331Samw 				    "Specifying multiple protocols "
46725331Samw 				    "not supported: %s\n"), protocol);
46735331Samw 				return (SA_SYNTAX_ERR);
46745331Samw 			}
46754653Sdougm 			protocol = optarg;
46764653Sdougm 			if (!sa_valid_protocol(protocol)) {
46774653Sdougm 				(void) printf(gettext(
46784653Sdougm 				    "Invalid protocol specified: %s\n"),
46794653Sdougm 				    protocol);
46804653Sdougm 				return (SA_INVALID_PROTOCOL);
46814653Sdougm 			}
46824653Sdougm 			break;
46834653Sdougm 		case 'v':
46844653Sdougm 			verbose++;
46854653Sdougm 			break;
46864653Sdougm 		default:
46874653Sdougm 		case 'h':
46884653Sdougm 		case '?':
46894653Sdougm 			(void) printf(gettext("usage: %s\n"),
46904653Sdougm 			    sa_get_usage(USAGE_STOP));
46914653Sdougm 			return (0);
46923034Sdougm 		}
46933034Sdougm 	}
46943034Sdougm 
46953034Sdougm 	if (optind == argc && !all) {
46964653Sdougm 		(void) printf(gettext("usage: %s\n"),
46974653Sdougm 		    sa_get_usage(USAGE_STOP));
46984653Sdougm 		return (SMF_EXIT_ERR_FATAL);
46994653Sdougm 	} else if (!all) {
47004653Sdougm 		while (optind < argc) {
47013910Sdougm 			group = sa_get_group(handle, argv[optind]);
47023034Sdougm 			if (group != NULL) {
47034653Sdougm 				state = sa_get_group_attr(group, "state");
47044653Sdougm 				if (state == NULL ||
47054653Sdougm 				    strcmp(state, "enabled") == 0) {
47065331Samw 					worklist = add_list(worklist, group, 0,
47075331Samw 					    protocol);
47084653Sdougm 					if (verbose)
47094653Sdougm 						(void) printf(gettext(
47104653Sdougm 						    "Stopping group \"%s\"\n"),
47114653Sdougm 						    argv[optind]);
47124653Sdougm 				} else {
47134653Sdougm 					ret = SMF_EXIT_OK;
47144653Sdougm 				}
47154653Sdougm 				if (state != NULL)
47164653Sdougm 					sa_free_attr_string(state);
47173034Sdougm 			}
47183034Sdougm 			optind++;
47194653Sdougm 		}
47204653Sdougm 	} else {
47215331Samw 		for (group = sa_get_group(handle, NULL);
47225331Samw 		    group != NULL;
47234653Sdougm 		    group = sa_get_next_group(group)) {
47243034Sdougm 			state = sa_get_group_attr(group, "state");
47253034Sdougm 			if (state == NULL || strcmp(state, "enabled") == 0)
47265331Samw 				worklist = add_list(worklist, group, 0,
47275331Samw 				    protocol);
47283034Sdougm 			if (state != NULL)
47294653Sdougm 				sa_free_attr_string(state);
47303034Sdougm 		}
47313034Sdougm 	}
47324653Sdougm 	(void) disable_all_groups(handle, worklist, 0);
47334653Sdougm 	ret = sa_update_config(handle);
47344653Sdougm 
47353034Sdougm 	if (worklist != NULL)
47364653Sdougm 		free_list(worklist);
47373034Sdougm 	return (ret);
47383034Sdougm }
47393034Sdougm 
47403034Sdougm /*
47413034Sdougm  * remove_all_options(share, proto)
47423034Sdougm  *
47433034Sdougm  * Removes all options on a share.
47443034Sdougm  */
47453034Sdougm 
47463034Sdougm static void
47473034Sdougm remove_all_options(sa_share_t share, char *proto)
47483034Sdougm {
47493034Sdougm 	sa_optionset_t optionset;
47503034Sdougm 	sa_security_t security;
47513034Sdougm 	sa_security_t prevsec = NULL;
47523034Sdougm 
47533034Sdougm 	optionset = sa_get_optionset(share, proto);
47543034Sdougm 	if (optionset != NULL)
47554653Sdougm 		(void) sa_destroy_optionset(optionset);
47563034Sdougm 	for (security = sa_get_security(share, NULL, NULL);
47573034Sdougm 	    security != NULL;
47583034Sdougm 	    security = sa_get_next_security(security)) {
47594653Sdougm 		char *type;
47603034Sdougm 		/*
47614653Sdougm 		 * We walk through the list.  prevsec keeps the
47623034Sdougm 		 * previous security so we can delete it without
47633034Sdougm 		 * destroying the list.
47643034Sdougm 		 */
47654653Sdougm 		if (prevsec != NULL) {
47664653Sdougm 			/* remove the previously seen security */
47674653Sdougm 			(void) sa_destroy_security(prevsec);
47684653Sdougm 			/* set to NULL so we don't try multiple times */
47694653Sdougm 			prevsec = NULL;
47704653Sdougm 		}
47714653Sdougm 		type = sa_get_security_attr(security, "type");
47724653Sdougm 		if (type != NULL) {
47734653Sdougm 			/*
47744653Sdougm 			 * if the security matches the specified protocol, we
47754653Sdougm 			 * want to remove it. prevsec holds it until either
47764653Sdougm 			 * the next pass or we fall out of the loop.
47774653Sdougm 			 */
47784653Sdougm 			if (strcmp(type, proto) == 0)
47794653Sdougm 				prevsec = security;
47804653Sdougm 			sa_free_attr_string(type);
47814653Sdougm 		}
47823034Sdougm 	}
47833034Sdougm 	/* in case there is one left */
47843034Sdougm 	if (prevsec != NULL)
47854653Sdougm 		(void) sa_destroy_security(prevsec);
47863034Sdougm }
47873034Sdougm 
47883034Sdougm 
47893034Sdougm /*
47903034Sdougm  * for legacy support, we need to handle the old syntax. This is what
47913034Sdougm  * we get if sharemgr is called with the name "share" rather than
47923034Sdougm  * sharemgr.
47933034Sdougm  */
47943034Sdougm 
47953034Sdougm static int
47963034Sdougm format_legacy_path(char *buff, int buffsize, char *proto, char *cmd)
47973034Sdougm {
47983034Sdougm 	int err;
47993034Sdougm 
48003034Sdougm 	err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd);
48013034Sdougm 	if (err > buffsize)
48024653Sdougm 		return (-1);
48033034Sdougm 	return (0);
48043034Sdougm }
48053034Sdougm 
48063034Sdougm 
48073034Sdougm /*
48083034Sdougm  * check_legacy_cmd(proto, cmd)
48093034Sdougm  *
48103034Sdougm  * Check to see if the cmd exists in /usr/lib/fs/<proto>/<cmd> and is
48113034Sdougm  * executable.
48123034Sdougm  */
48133034Sdougm 
48143034Sdougm static int
48153034Sdougm check_legacy_cmd(char *path)
48163034Sdougm {
48173034Sdougm 	struct stat st;
48183034Sdougm 	int ret = 0;
48193034Sdougm 
48203034Sdougm 	if (stat(path, &st) == 0) {
48214653Sdougm 		if (S_ISREG(st.st_mode) &&
48224653Sdougm 		    st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
48234653Sdougm 			ret = 1;
48243034Sdougm 	}
48253034Sdougm 	return (ret);
48263034Sdougm }
48273034Sdougm 
48283034Sdougm /*
48293034Sdougm  * run_legacy_command(proto, cmd, argv)
48303034Sdougm  *
48314653Sdougm  * We know the command exists, so attempt to execute it with all the
48323034Sdougm  * arguments. This implements full legacy share support for those
48333034Sdougm  * protocols that don't have plugin providers.
48343034Sdougm  */
48353034Sdougm 
48363034Sdougm static int
48373034Sdougm run_legacy_command(char *path, char *argv[])
48383034Sdougm {
48393034Sdougm 	int ret;
48403034Sdougm 
48413034Sdougm 	ret = execv(path, argv);
48423034Sdougm 	if (ret < 0) {
48434653Sdougm 		switch (errno) {
48444653Sdougm 		case EACCES:
48454653Sdougm 			ret = SA_NO_PERMISSION;
48464653Sdougm 			break;
48474653Sdougm 		default:
48484653Sdougm 			ret = SA_SYSTEM_ERR;
48494653Sdougm 			break;
48504653Sdougm 		}
48513034Sdougm 	}
48523034Sdougm 	return (ret);
48533034Sdougm }
48543034Sdougm 
48553034Sdougm /*
48563348Sdougm  * out_share(out, group, proto)
48573034Sdougm  *
48583034Sdougm  * Display the share information in the format that the "share"
48593034Sdougm  * command has traditionally used.
48603034Sdougm  */
48613034Sdougm 
48623034Sdougm static void
48633348Sdougm out_share(FILE *out, sa_group_t group, char *proto)
48643034Sdougm {
48653034Sdougm 	sa_share_t share;
48663034Sdougm 	char resfmt[128];
48675331Samw 	char *defprop;
48685331Samw 
48695331Samw 	/*
48705331Samw 	 * The original share command defaulted to displaying NFS
48715331Samw 	 * shares or allowed a protocol to be specified. We want to
48725331Samw 	 * skip those shares that are not the specified protocol.
48735331Samw 	 */
48745331Samw 	if (proto != NULL && sa_get_optionset(group, proto) == NULL)
48755331Samw 		return;
48765331Samw 
48775331Samw 	if (proto == NULL)
48785331Samw 		proto = "nfs";
48795331Samw 
48805331Samw 	/*
48815331Samw 	 * get the default property string.  NFS uses "rw" but
48825331Samw 	 * everything else will use "".
48835331Samw 	 */
48845331Samw 	if (proto != NULL && strcmp(proto, "nfs") != 0)
48855331Samw 		defprop = "\"\"";
48865331Samw 	else
48875331Samw 		defprop = "rw";
48883034Sdougm 
48894653Sdougm 	for (share = sa_get_share(group, NULL);
48904653Sdougm 	    share != NULL;
48914653Sdougm 	    share = sa_get_next_share(share)) {
48924653Sdougm 		char *path;
48934653Sdougm 		char *type;
48944653Sdougm 		char *resource;
48954653Sdougm 		char *description;
48964653Sdougm 		char *groupname;
48974653Sdougm 		char *sharedstate;
48984653Sdougm 		int shared = 1;
48994653Sdougm 		char *soptions;
49005331Samw 		char shareopts[MAXNAMLEN];
49014653Sdougm 
49024653Sdougm 		sharedstate = sa_get_share_attr(share, "shared");
49034653Sdougm 		path = sa_get_share_attr(share, "path");
49044653Sdougm 		type = sa_get_share_attr(share, "type");
49055331Samw 		resource = get_resource(share);
49064653Sdougm 		groupname = sa_get_group_attr(group, "name");
49074653Sdougm 
49084653Sdougm 		if (groupname != NULL && strcmp(groupname, "default") == 0) {
49094653Sdougm 			sa_free_attr_string(groupname);
49104653Sdougm 			groupname = NULL;
49114653Sdougm 		}
49124653Sdougm 		description = sa_get_share_description(share);
49134653Sdougm 
49145331Samw 		/*
49155331Samw 		 * Want the sharetab version if it exists, defaulting
49165331Samw 		 * to NFS if no protocol specified.
49175331Samw 		 */
49185331Samw 		(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", proto);
49195331Samw 		soptions = sa_get_share_attr(share, shareopts);
49204653Sdougm 
49214653Sdougm 		if (sharedstate == NULL)
49224653Sdougm 			shared = 0;
49234653Sdougm 
49244653Sdougm 		if (soptions == NULL)
49254653Sdougm 			soptions = sa_proto_legacy_format(proto, share, 1);
49264653Sdougm 
49274653Sdougm 		if (shared) {
49284653Sdougm 			/* only active shares go here */
49294653Sdougm 			(void) snprintf(resfmt, sizeof (resfmt), "%s%s%s",
49304653Sdougm 			    resource != NULL ? resource : "-",
49314653Sdougm 			    groupname != NULL ? "@" : "",
49324653Sdougm 			    groupname != NULL ? groupname : "");
49334653Sdougm 			(void) fprintf(out, "%-14.14s  %s   %s   \"%s\"  \n",
49344653Sdougm 			    resfmt, path,
49354653Sdougm 			    (soptions != NULL && strlen(soptions) > 0) ?
49365331Samw 			    soptions : defprop,
49374653Sdougm 			    (description != NULL) ? description : "");
49384653Sdougm 		}
49394653Sdougm 
49404653Sdougm 		if (path != NULL)
49414653Sdougm 			sa_free_attr_string(path);
49424653Sdougm 		if (type != NULL)
49434653Sdougm 			sa_free_attr_string(type);
49444653Sdougm 		if (resource != NULL)
49454653Sdougm 			sa_free_attr_string(resource);
49464653Sdougm 		if (groupname != NULL)
49474653Sdougm 			sa_free_attr_string(groupname);
49484653Sdougm 		if (description != NULL)
49494653Sdougm 			sa_free_share_description(description);
49504653Sdougm 		if (sharedstate != NULL)
49514653Sdougm 			sa_free_attr_string(sharedstate);
49524653Sdougm 		if (soptions != NULL)
49534653Sdougm 			sa_format_free(soptions);
49543034Sdougm 	}
49553034Sdougm }
49563034Sdougm 
49573034Sdougm /*
49583034Sdougm  * output_legacy_file(out, proto)
49593034Sdougm  *
49603034Sdougm  * Walk all of the groups for the specified protocol and call
49613034Sdougm  * out_share() to format and write in the format displayed by the
49623034Sdougm  * "share" command with no arguments.
49633034Sdougm  */
49643034Sdougm 
49653034Sdougm static void
49663910Sdougm output_legacy_file(FILE *out, char *proto, sa_handle_t handle)
49673034Sdougm {
49683034Sdougm 	sa_group_t group;
49693034Sdougm 
49705331Samw 	for (group = sa_get_group(handle, NULL);
49715331Samw 	    group != NULL;
49724653Sdougm 	    group = sa_get_next_group(group)) {
49734653Sdougm 		char *zfs;
49743034Sdougm 
49753034Sdougm 		/*
49765331Samw 		 * Go through all the groups and ZFS
49775331Samw 		 * sub-groups. out_share() will format the shares in
49785331Samw 		 * the group appropriately.
49793034Sdougm 		 */
49803034Sdougm 
49814653Sdougm 		zfs = sa_get_group_attr(group, "zfs");
49824653Sdougm 		if (zfs != NULL) {
49834653Sdougm 			sa_group_t zgroup;
49844653Sdougm 			sa_free_attr_string(zfs);
49854653Sdougm 			for (zgroup = sa_get_sub_group(group);
49864653Sdougm 			    zgroup != NULL;
49874653Sdougm 			    zgroup = sa_get_next_group(zgroup)) {
49884653Sdougm 
49894653Sdougm 				/* got a group, so display it */
49904653Sdougm 				out_share(out, zgroup, proto);
49914653Sdougm 			}
49924653Sdougm 		} else {
49934653Sdougm 			out_share(out, group, proto);
49943034Sdougm 		}
49953034Sdougm 	}
49963034Sdougm }
49973034Sdougm 
49983034Sdougm int
49993910Sdougm sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[])
50003034Sdougm {
50013034Sdougm 	char *protocol = "nfs";
50023034Sdougm 	char *options = NULL;
50033034Sdougm 	char *description = NULL;
50043034Sdougm 	char *groupname = NULL;
50053034Sdougm 	char *sharepath = NULL;
50063034Sdougm 	char *resource = NULL;
50073034Sdougm 	char *groupstatus = NULL;
50083034Sdougm 	int persist = SA_SHARE_TRANSIENT;
50093034Sdougm 	int argsused = 0;
50103034Sdougm 	int c;
50113034Sdougm 	int ret = SA_OK;
50123034Sdougm 	int zfs = 0;
50133034Sdougm 	int true_legacy = 0;
50143034Sdougm 	int curtype = SA_SHARE_TRANSIENT;
50153034Sdougm 	char cmd[MAXPATHLEN];
50164653Sdougm 	sa_group_t group = NULL;
50175331Samw 	sa_resource_t rsrc = NULL;
50184653Sdougm 	sa_share_t share;
50194653Sdougm 	char dir[MAXPATHLEN];
50205331Samw 	uint64_t features;
50215331Samw #ifdef lint
50225331Samw 	flags = flags;
50235331Samw #endif
50243034Sdougm 
50253034Sdougm 	while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) {
50264653Sdougm 		switch (c) {
50274653Sdougm 		case 'd':
50284653Sdougm 			description = optarg;
50294653Sdougm 			argsused++;
50304653Sdougm 			break;
50314653Sdougm 		case 'F':
50324653Sdougm 			protocol = optarg;
50334653Sdougm 			if (!sa_valid_protocol(protocol)) {
50344653Sdougm 				if (format_legacy_path(cmd, MAXPATHLEN,
50354653Sdougm 				    protocol, "share") == 0 &&
50364653Sdougm 				    check_legacy_cmd(cmd)) {
50374653Sdougm 					true_legacy++;
50384653Sdougm 				} else {
50394653Sdougm 					(void) fprintf(stderr, gettext(
50404653Sdougm 					    "Invalid protocol specified: "
50414653Sdougm 					    "%s\n"), protocol);
50424653Sdougm 					return (SA_INVALID_PROTOCOL);
50434653Sdougm 				}
50444653Sdougm 			}
50454653Sdougm 			break;
50464653Sdougm 		case 'o':
50474653Sdougm 			options = optarg;
50484653Sdougm 			argsused++;
50494653Sdougm 			break;
50504653Sdougm 		case 'p':
50514653Sdougm 			persist = SA_SHARE_PERMANENT;
50524653Sdougm 			argsused++;
50534653Sdougm 			break;
50544653Sdougm 		case 'h':
50554653Sdougm 		case '?':
50564653Sdougm 		default:
50574653Sdougm 			(void) fprintf(stderr, gettext("usage: %s\n"),
50584653Sdougm 			    sa_get_usage(USAGE_SHARE));
50594653Sdougm 			return (SA_OK);
50603034Sdougm 		}
50614653Sdougm 	}
50624653Sdougm 
50634653Sdougm 	/* Have the info so construct what is needed */
50644653Sdougm 	if (!argsused && optind == argc) {
50654653Sdougm 		/* display current info in share format */
50665331Samw 		(void) output_legacy_file(stdout, protocol, handle);
50674653Sdougm 		return (ret);
50683034Sdougm 	}
50693034Sdougm 
50704653Sdougm 	/* We are modifying the configuration */
50714653Sdougm 	if (optind == argc) {
50723034Sdougm 		(void) fprintf(stderr, gettext("usage: %s\n"),
50734653Sdougm 		    sa_get_usage(USAGE_SHARE));
50743034Sdougm 		return (SA_LEGACY_ERR);
50754653Sdougm 	}
50764653Sdougm 	if (true_legacy) {
50774653Sdougm 		/* If still using legacy share/unshare, exec it */
50783034Sdougm 		ret = run_legacy_command(cmd, argv);
50793034Sdougm 		return (ret);
50804653Sdougm 	}
50814653Sdougm 
50824653Sdougm 	sharepath = argv[optind++];
50834653Sdougm 	if (optind < argc) {
50843034Sdougm 		resource = argv[optind];
50853034Sdougm 		groupname = strchr(resource, '@');
50863034Sdougm 		if (groupname != NULL)
50874653Sdougm 			*groupname++ = '\0';
50884653Sdougm 	}
50894653Sdougm 	if (realpath(sharepath, dir) == NULL)
50903034Sdougm 		ret = SA_BAD_PATH;
50914653Sdougm 	else
50923034Sdougm 		sharepath = dir;
50934653Sdougm 	if (ret == SA_OK)
50943910Sdougm 		share = sa_find_share(handle, sharepath);
50954653Sdougm 	else
50963034Sdougm 		share = NULL;
50974653Sdougm 
50985331Samw 	features = sa_proto_get_featureset(protocol);
50995331Samw 
51004653Sdougm 	if (groupname != NULL) {
51014653Sdougm 		ret = SA_NOT_ALLOWED;
51024653Sdougm 	} else if (ret == SA_OK) {
51035331Samw 		char *legacygroup;
51043034Sdougm 		/*
51054653Sdougm 		 * The legacy group is always present and zfs groups
51063034Sdougm 		 * come and go.  zfs shares may be in sub-groups and
51073034Sdougm 		 * the zfs share will already be in that group so it
51085331Samw 		 * isn't an error. If the protocol is "smb", the group
51095331Samw 		 * "smb" is used when "default" would otherwise be
51105331Samw 		 * used.  "default" is NFS only and "smb" is SMB only.
51113034Sdougm 		 */
51125331Samw 		if (strcmp(protocol, "smb") == 0)
51135331Samw 			legacygroup = "smb";
51145331Samw 		else
51155331Samw 			legacygroup = "default";
51165331Samw 
51173034Sdougm 		/*
51184653Sdougm 		 * If the share exists (not NULL), then make sure it
51194653Sdougm 		 * is one we want to handle by getting the parent
51204653Sdougm 		 * group.
51213034Sdougm 		 */
51225331Samw 		if (share != NULL) {
51234653Sdougm 			group = sa_get_parent_group(share);
51245331Samw 		} else {
51254653Sdougm 			group = sa_get_group(handle, legacygroup);
51265331Samw 			if (group == NULL && strcmp(legacygroup, "smb") == 0) {
51275331Samw 				/*
51285331Samw 				 * This group may not exist, so create
51295331Samw 				 * as necessary. It only contains the
51305331Samw 				 * "smb" protocol.
51315331Samw 				 */
51325331Samw 				group = sa_create_group(handle, legacygroup,
51335331Samw 				    &ret);
51345331Samw 				if (group != NULL)
51355331Samw 					(void) sa_create_optionset(group,
51365331Samw 					    protocol);
51375331Samw 			}
51385331Samw 		}
51395331Samw 
51405331Samw 		if (group == NULL) {
51415331Samw 			ret = SA_SYSTEM_ERR;
51425331Samw 			goto err;
51435331Samw 		}
51445331Samw 
51455331Samw 		groupstatus = group_status(group);
51465331Samw 		if (share == NULL) {
51475331Samw 			share = sa_add_share(group, sharepath,
51485331Samw 			    persist, &ret);
51495331Samw 			if (share == NULL &&
51505331Samw 			    ret == SA_DUPLICATE_NAME) {
51515331Samw 				/*
51525331Samw 				 * Could be a ZFS path being started
51535331Samw 				 */
51545331Samw 				if (sa_zfs_is_shared(handle,
51555331Samw 				    sharepath)) {
51565331Samw 					ret = SA_OK;
51575331Samw 					group = sa_get_group(handle,
51585331Samw 					    "zfs");
51595331Samw 					if (group == NULL) {
51605331Samw 						/*
51615331Samw 						 * This shouldn't
51625331Samw 						 * happen.
51635331Samw 						 */
51645331Samw 						ret = SA_CONFIG_ERR;
51655331Samw 					} else {
51665331Samw 						share = sa_add_share(
51675331Samw 						    group, sharepath,
51685331Samw 						    persist, &ret);
51694653Sdougm 					}
51703034Sdougm 				}
51715331Samw 			}
51725331Samw 		} else {
51735331Samw 			char *type;
51745331Samw 			/*
51755331Samw 			 * May want to change persist state, but the
51765331Samw 			 * important thing is to change options. We
51775331Samw 			 * need to change them regardless of the
51785331Samw 			 * source.
51795331Samw 			 */
51805331Samw 
51815331Samw 			if (sa_zfs_is_shared(handle, sharepath)) {
51825331Samw 				zfs = 1;
51835331Samw 			}
51845331Samw 			remove_all_options(share, protocol);
51855331Samw 			type = sa_get_share_attr(share, "type");
51865331Samw 			if (type != NULL &&
51875331Samw 			    strcmp(type, "transient") != 0) {
51885331Samw 				curtype = SA_SHARE_PERMANENT;
51895331Samw 			}
51905331Samw 			if (type != NULL)
51915331Samw 				sa_free_attr_string(type);
51925331Samw 			if (curtype != persist) {
51935331Samw 				(void) sa_set_share_attr(share, "type",
51945331Samw 				    persist == SA_SHARE_PERMANENT ?
51955331Samw 				    "persist" : "transient");
51965331Samw 			}
51975331Samw 		}
51985331Samw 
51995331Samw 		/*
52005331Samw 		 * If there is a resource name, we may
52015331Samw 		 * actually care about it if this is share for
52025331Samw 		 * a protocol that uses resource level sharing
52035331Samw 		 * (SMB). We need to find the resource and, if
52045331Samw 		 * it exists, make sure it belongs to the
52055331Samw 		 * current share. If it doesn't exist, attempt
52065331Samw 		 * to create it.
52075331Samw 		 */
52085331Samw 
52095331Samw 		if (ret == SA_OK && resource != NULL) {
52105331Samw 			rsrc = sa_find_resource(handle, resource);
52115331Samw 			if (rsrc != NULL) {
52125331Samw 				if (share != sa_get_resource_parent(rsrc))
52135331Samw 					ret = SA_DUPLICATE_NAME;
52145331Samw 				} else {
52155331Samw 					rsrc = sa_add_resource(share, resource,
52165331Samw 					    persist, &ret);
52173034Sdougm 				}
52185331Samw 				if (features & SA_FEATURE_RESOURCE)
52195331Samw 					share = rsrc;
52203108Sdougm 			}
52215331Samw 
52224653Sdougm 			/* Have a group to hold this share path */
52234653Sdougm 			if (ret == SA_OK && options != NULL &&
52244653Sdougm 			    strlen(options) > 0) {
52254653Sdougm 				ret = sa_parse_legacy_options(share,
52264653Sdougm 				    options,
52274653Sdougm 				    protocol);
52283034Sdougm 			}
52294653Sdougm 			if (!zfs) {
52304653Sdougm 				/*
52315331Samw 				 * ZFS shares never have a description
52325331Samw 				 * and we can't store the values so
52335331Samw 				 * don't try.
52344653Sdougm 				 */
52354653Sdougm 				if (ret == SA_OK && description != NULL)
52364653Sdougm 					ret = sa_set_share_description(share,
52374653Sdougm 					    description);
52383034Sdougm 			}
52395331Samw 			if (ret == SA_OK &&
52405331Samw 			    strcmp(groupstatus, "enabled") == 0) {
52415331Samw 				if (rsrc != share)
52424653Sdougm 					ret = sa_enable_share(share, protocol);
52435331Samw 				else
52445331Samw 					ret = sa_enable_resource(rsrc,
52455331Samw 					    protocol);
52464653Sdougm 				if (ret == SA_OK &&
52474653Sdougm 				    persist == SA_SHARE_PERMANENT) {
52484653Sdougm 					(void) sa_update_legacy(share,
52494653Sdougm 					    protocol);
52504653Sdougm 				}
52514653Sdougm 				if (ret == SA_OK)
52524653Sdougm 					ret = sa_update_config(handle);
52534653Sdougm 			}
52543034Sdougm 	}
52555331Samw err:
52563034Sdougm 	if (ret != SA_OK) {
52574653Sdougm 		(void) fprintf(stderr, gettext("Could not share: %s: %s\n"),
52584653Sdougm 		    sharepath, sa_errorstr(ret));
52594653Sdougm 		ret = SA_LEGACY_ERR;
52603034Sdougm 	}
52613034Sdougm 	return (ret);
52623034Sdougm }
52633034Sdougm 
52643034Sdougm /*
52653034Sdougm  * sa_legacy_unshare(flags, argc, argv)
52663034Sdougm  *
52673034Sdougm  * Implements the original unshare command.
52683034Sdougm  */
52693034Sdougm int
52703910Sdougm sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[])
52713034Sdougm {
52723034Sdougm 	char *protocol = "nfs"; /* for now */
52733034Sdougm 	char *options = NULL;
52743034Sdougm 	char *sharepath = NULL;
52753034Sdougm 	int persist = SA_SHARE_TRANSIENT;
52763034Sdougm 	int argsused = 0;
52773034Sdougm 	int c;
52783034Sdougm 	int ret = SA_OK;
52793034Sdougm 	int true_legacy = 0;
52805331Samw 	uint64_t features = 0;
52815331Samw 	sa_resource_t resource = NULL;
52823034Sdougm 	char cmd[MAXPATHLEN];
52835331Samw #ifdef lint
52845331Samw 	flags = flags;
52855331Samw 	options = options;
52865331Samw #endif
52873034Sdougm 
52883034Sdougm 	while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) {
52894653Sdougm 		switch (c) {
52904653Sdougm 		case 'h':
52914653Sdougm 		case '?':
52924653Sdougm 			break;
52934653Sdougm 		case 'F':
52944653Sdougm 			protocol = optarg;
52954653Sdougm 			if (!sa_valid_protocol(protocol)) {
52964653Sdougm 				if (format_legacy_path(cmd, MAXPATHLEN,
52974653Sdougm 				    protocol, "unshare") == 0 &&
52984653Sdougm 				    check_legacy_cmd(cmd)) {
52994653Sdougm 					true_legacy++;
53004653Sdougm 				} else {
53014653Sdougm 					(void) printf(gettext(
53024653Sdougm 					    "Invalid file system name\n"));
53034653Sdougm 					return (SA_INVALID_PROTOCOL);
53044653Sdougm 				}
53054653Sdougm 			}
53064653Sdougm 			break;
53074653Sdougm 		case 'o':
53084653Sdougm 			options = optarg;
53094653Sdougm 			argsused++;
53104653Sdougm 			break;
53114653Sdougm 		case 'p':
53124653Sdougm 			persist = SA_SHARE_PERMANENT;
53134653Sdougm 			argsused++;
53144653Sdougm 			break;
53154653Sdougm 		default:
53164653Sdougm 			(void) printf(gettext("usage: %s\n"),
53174653Sdougm 			    sa_get_usage(USAGE_UNSHARE));
53184653Sdougm 			return (SA_OK);
53193034Sdougm 		}
53203034Sdougm 	}
53213034Sdougm 
53224653Sdougm 	/* Have the info so construct what is needed */
53234653Sdougm 	if (optind == argc || (optind + 1) < argc || options != NULL) {
53244653Sdougm 		ret = SA_SYNTAX_ERR;
53253034Sdougm 	} else {
53264653Sdougm 		sa_share_t share;
53274653Sdougm 		char dir[MAXPATHLEN];
53284653Sdougm 		if (true_legacy) {
53294653Sdougm 			/* if still using legacy share/unshare, exec it */
53304653Sdougm 			ret = run_legacy_command(cmd, argv);
53314653Sdougm 			return (ret);
53324653Sdougm 		}
53333663Sdougm 		/*
53343663Sdougm 		 * Find the path in the internal configuration. If it
53353663Sdougm 		 * isn't found, attempt to resolve the path via
53363663Sdougm 		 * realpath() and try again.
53373663Sdougm 		 */
53384653Sdougm 		sharepath = argv[optind++];
53394653Sdougm 		share = sa_find_share(handle, sharepath);
53404653Sdougm 		if (share == NULL) {
53414653Sdougm 			if (realpath(sharepath, dir) == NULL) {
53424653Sdougm 				ret = SA_NO_SUCH_PATH;
53434653Sdougm 			} else {
53444653Sdougm 				share = sa_find_share(handle, dir);
53454653Sdougm 			}
53463663Sdougm 		}
53475331Samw 		if (share == NULL) {
53485331Samw 			/* Could be a resource name so check that next */
53495331Samw 			features = sa_proto_get_featureset(protocol);
53505331Samw 			resource = sa_find_resource(handle, sharepath);
53515331Samw 			if (resource != NULL) {
53525331Samw 				share = sa_get_resource_parent(resource);
53535331Samw 				if (features & SA_FEATURE_RESOURCE)
53545331Samw 					(void) sa_disable_resource(resource,
53555331Samw 					    protocol);
53565331Samw 				if (persist == SA_SHARE_PERMANENT) {
53575331Samw 					ret = sa_remove_resource(resource);
53585331Samw 					if (ret == SA_OK)
53595331Samw 						ret = sa_update_config(handle);
53605331Samw 				}
53615331Samw 				/*
53625331Samw 				 * If we still have a resource on the
53635331Samw 				 * share, we don't disable the share
53645331Samw 				 * itself. IF there aren't anymore, we
53655331Samw 				 * need to remove the share. The
53665331Samw 				 * removal will be done in the next
53675331Samw 				 * section if appropriate.
53685331Samw 				 */
53695331Samw 				resource = sa_get_share_resource(share, NULL);
53705331Samw 				if (resource != NULL)
53715331Samw 					share = NULL;
53725331Samw 			} else if (ret == SA_OK) {
53735331Samw 				/* Didn't find path and no  resource */
53745331Samw 				ret = SA_BAD_PATH;
53755331Samw 			}
53765331Samw 		}
53775331Samw 		if (share != NULL && resource == NULL) {
53784653Sdougm 			ret = sa_disable_share(share, protocol);
53794653Sdougm 			/*
53804653Sdougm 			 * Errors are ok and removal should still occur. The
53814653Sdougm 			 * legacy unshare is more forgiving of errors than the
53824653Sdougm 			 * remove-share subcommand which may need the force
53834653Sdougm 			 * flag set for some error conditions. That is, the
53844653Sdougm 			 * "unshare" command will always unshare if it can
53854653Sdougm 			 * while "remove-share" might require the force option.
53864653Sdougm 			 */
53874653Sdougm 			if (persist == SA_SHARE_PERMANENT) {
53884653Sdougm 				ret = sa_remove_share(share);
53894653Sdougm 				if (ret == SA_OK)
53904653Sdougm 					ret = sa_update_config(handle);
53914653Sdougm 			}
53925331Samw 		} else if (ret == SA_OK && share == NULL && resource == NULL) {
53935331Samw 			/*
53945331Samw 			 * If both share and resource are NULL, then
53955331Samw 			 * share not found. If one or the other was
53965331Samw 			 * found or there was an earlier error, we
53975331Samw 			 * assume it was handled earlier.
53985331Samw 			 */
53994653Sdougm 			ret = SA_NOT_SHARED;
54003663Sdougm 		}
54013034Sdougm 	}
54023034Sdougm 	switch (ret) {
54033034Sdougm 	default:
54044653Sdougm 		(void) printf("%s: %s\n", sharepath, sa_errorstr(ret));
54054653Sdougm 		ret = SA_LEGACY_ERR;
54064653Sdougm 		break;
54073034Sdougm 	case SA_SYNTAX_ERR:
54084653Sdougm 		(void) printf(gettext("usage: %s\n"),
54094653Sdougm 		    sa_get_usage(USAGE_UNSHARE));
54104653Sdougm 		break;
54113034Sdougm 	case SA_OK:
54124653Sdougm 		break;
54133034Sdougm 	}
54143034Sdougm 	return (ret);
54153034Sdougm }
54163034Sdougm 
54173034Sdougm /*
54184653Sdougm  * Common commands that implement the sub-commands used by all
54195331Samw  * protocols. The entries are found via the lookup command
54203034Sdougm  */
54213034Sdougm 
54223034Sdougm static sa_command_t commands[] = {
54233034Sdougm 	{"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET},
54243034Sdougm 	{"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION},
54253034Sdougm 	{"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION},
54263034Sdougm 	{"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION},
54273034Sdougm 	{"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION},
54283034Sdougm 	{"list", 0, sa_list, USAGE_LIST},
54293034Sdougm 	{"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET},
54303034Sdougm 	{"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET},
54313034Sdougm 	{"set", 0, sa_set, USAGE_SET, SVC_SET},
54323034Sdougm 	{"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET},
54333034Sdougm 	{"show", 0, sa_show, USAGE_SHOW},
54343034Sdougm 	{"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION},
54353034Sdougm 	{"start", CMD_NODISPLAY, sa_start_group, USAGE_START,
54365331Samw 	    SVC_SET|SVC_ACTION},
54373034Sdougm 	{"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION},
54383034Sdougm 	{"unset", 0, sa_unset, USAGE_UNSET, SVC_SET},
54393034Sdougm 	{"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION},
54403034Sdougm 	{NULL, 0, NULL, NULL}
54413034Sdougm };
54423034Sdougm 
54433034Sdougm static char *
54443034Sdougm sa_get_usage(sa_usage_t index)
54453034Sdougm {
54463034Sdougm 	char *ret = NULL;
54473034Sdougm 	switch (index) {
54483034Sdougm 	case USAGE_ADD_SHARE:
54494653Sdougm 		ret = gettext("add-share [-nth] [-r resource-name] "
54504653Sdougm 		    "[-d \"description text\"] -s sharepath group");
54514653Sdougm 		break;
54523034Sdougm 	case USAGE_CREATE:
54534653Sdougm 		ret = gettext(
54544653Sdougm 		    "create [-nvh] [-P proto [-p property=value]] group");
54554653Sdougm 		break;
54563034Sdougm 	case USAGE_DELETE:
54574653Sdougm 		ret = gettext("delete [-nvh] [-P proto] [-f] group");
54584653Sdougm 		break;
54593034Sdougm 	case USAGE_DISABLE:
54604653Sdougm 		ret = gettext("disable [-nvh] {-a | group ...}");
54614653Sdougm 		break;
54623034Sdougm 	case USAGE_ENABLE:
54634653Sdougm 		ret = gettext("enable [-nvh] {-a | group ...}");
54644653Sdougm 		break;
54653034Sdougm 	case USAGE_LIST:
54664653Sdougm 		ret = gettext("list [-vh] [-P proto]");
54674653Sdougm 		break;
54683034Sdougm 	case USAGE_MOVE_SHARE:
54694653Sdougm 		ret = gettext(
54704653Sdougm 		    "move-share [-nvh] -s sharepath destination-group");
54714653Sdougm 		break;
54723034Sdougm 	case USAGE_REMOVE_SHARE:
54735331Samw 		ret = gettext(
54745331Samw 		    "remove-share [-fnvh] {-s sharepath | -r resource} "
54755331Samw 		    "group");
54764653Sdougm 		break;
54773034Sdougm 	case USAGE_SET:
54784653Sdougm 		ret = gettext("set [-nvh] -P proto [-S optspace] "
54795331Samw 		    "[-p property=value]* [-s sharepath] [-r resource]] "
54805331Samw 		    "group");
54814653Sdougm 		break;
54823034Sdougm 	case USAGE_SET_SECURITY:
54834653Sdougm 		ret = gettext("set-security [-nvh] -P proto -S security-type "
54844653Sdougm 		    "[-p property=value]* group");
54854653Sdougm 		break;
54863034Sdougm 	case USAGE_SET_SHARE:
54874653Sdougm 		ret = gettext("set-share [-nh] [-r resource] "
54884653Sdougm 		    "[-d \"description text\"] -s sharepath group");
54894653Sdougm 		break;
54903034Sdougm 	case USAGE_SHOW:
54914653Sdougm 		ret = gettext("show [-pvxh] [-P proto] [group ...]");
54924653Sdougm 		break;
54933034Sdougm 	case USAGE_SHARE:
54944653Sdougm 		ret = gettext("share [-F fstype] [-p] [-o optionlist]"
54954653Sdougm 		    "[-d description] [pathname [resourcename]]");
54964653Sdougm 		break;
54973034Sdougm 	case USAGE_START:
54984653Sdougm 		ret = gettext("start [-vh] [-P proto] {-a | group ...}");
54994653Sdougm 		break;
55003034Sdougm 	case USAGE_STOP:
55014653Sdougm 		ret = gettext("stop [-vh] [-P proto] {-a | group ...}");
55024653Sdougm 		break;
55033034Sdougm 	case USAGE_UNSET:
55044653Sdougm 		ret = gettext("unset [-nvh] -P proto [-S optspace] "
55054653Sdougm 		    "[-p property]* group");
55064653Sdougm 		break;
55073034Sdougm 	case USAGE_UNSET_SECURITY:
55085331Samw 		ret = gettext("unset-security [-nvh] -P proto "
55095331Samw 		    "-S security-type [-p property]* group");
55104653Sdougm 		break;
55113034Sdougm 	case USAGE_UNSHARE:
55124653Sdougm 		ret = gettext(
55135331Samw 		    "unshare [-F fstype] [-p] [-o optionlist] sharepath");
55144653Sdougm 		break;
55153034Sdougm 	}
55163034Sdougm 	return (ret);
55173034Sdougm }
55183034Sdougm 
55193034Sdougm /*
55203034Sdougm  * sa_lookup(cmd, proto)
55213034Sdougm  *
55223034Sdougm  * Lookup the sub-command. proto isn't currently used, but it may
55233034Sdougm  * eventually provide a way to provide protocol specific sub-commands.
55243034Sdougm  */
55253034Sdougm sa_command_t *
55263034Sdougm sa_lookup(char *cmd, char *proto)
55273034Sdougm {
55283034Sdougm 	int i;
55293034Sdougm 	size_t len;
55305331Samw #ifdef lint
55315331Samw 	proto = proto;
55325331Samw #endif
55333034Sdougm 
55343034Sdougm 	len = strlen(cmd);
55353034Sdougm 	for (i = 0; commands[i].cmdname != NULL; i++) {
55364653Sdougm 		if (strncmp(cmd, commands[i].cmdname, len) == 0)
55374653Sdougm 			return (&commands[i]);
55383034Sdougm 	}
55393034Sdougm 	return (NULL);
55403034Sdougm }
55413034Sdougm 
55423034Sdougm void
55433034Sdougm sub_command_help(char *proto)
55443034Sdougm {
55453034Sdougm 	int i;
55465331Samw #ifdef lint
55475331Samw 	proto = proto;
55485331Samw #endif
55493034Sdougm 
55503034Sdougm 	(void) printf(gettext("\tsub-commands:\n"));
55513034Sdougm 	for (i = 0; commands[i].cmdname != NULL; i++) {
55524653Sdougm 		if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY)))
55534653Sdougm 			(void) printf("\t%s\n",
55544653Sdougm 			    sa_get_usage((sa_usage_t)commands[i].cmdidx));
55553034Sdougm 	}
55563034Sdougm }
5557