xref: /onnv-gate/usr/src/cmd/dfs.cmds/sharemgr/commands.c (revision 11415:2d2e5f652b7c)
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 /*
23*11415SSurya.Prakki@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
243034Sdougm  * Use is subject to license terms.
253034Sdougm  */
263034Sdougm 
273034Sdougm #include <sys/types.h>
283034Sdougm #include <sys/stat.h>
293034Sdougm #include <fcntl.h>
303034Sdougm #include <stdlib.h>
313034Sdougm #include <stdio.h>
323034Sdougm #include <string.h>
333034Sdougm #include <ctype.h>
343034Sdougm #include <unistd.h>
353034Sdougm #include <getopt.h>
363034Sdougm #include <utmpx.h>
373034Sdougm #include <pwd.h>
383034Sdougm #include <auth_attr.h>
393034Sdougm #include <secdb.h>
403034Sdougm #include <sys/param.h>
413034Sdougm #include <sys/stat.h>
423034Sdougm #include <errno.h>
433034Sdougm 
443034Sdougm #include <libshare.h>
453034Sdougm #include "sharemgr.h"
463034Sdougm #include <libscf.h>
473034Sdougm #include <libxml/tree.h>
483034Sdougm #include <libintl.h>
495331Samw #include <assert.h>
505331Samw #include <iconv.h>
515331Samw #include <langinfo.h>
525331Samw #include <dirent.h>
533034Sdougm 
543034Sdougm static char *sa_get_usage(sa_usage_t);
553034Sdougm 
563034Sdougm /*
573034Sdougm  * Implementation of the common sub-commands supported by sharemgr.
583034Sdougm  * A number of helper functions are also included.
593034Sdougm  */
603034Sdougm 
613034Sdougm /*
623034Sdougm  * has_protocol(group, proto)
633034Sdougm  *	If the group has an optionset with the specified protocol,
643034Sdougm  *	return true (1) otherwise false (0).
653034Sdougm  */
663034Sdougm static int
has_protocol(sa_group_t group,char * protocol)673034Sdougm has_protocol(sa_group_t group, char *protocol)
683034Sdougm {
693034Sdougm 	sa_optionset_t optionset;
703034Sdougm 	int result = 0;
713034Sdougm 
723034Sdougm 	optionset = sa_get_optionset(group, protocol);
733034Sdougm 	if (optionset != NULL) {
744653Sdougm 		result++;
753034Sdougm 	}
763034Sdougm 	return (result);
773034Sdougm }
783034Sdougm 
793034Sdougm /*
805331Samw  * validresource(name)
815331Samw  *
825331Samw  * Check that name only has valid characters in it. The current valid
835331Samw  * set are the printable characters but not including:
845331Samw  *	" / \ [ ] : | < > + ; , ? * = \t
855331Samw  * Note that space is included and there is a maximum length.
865331Samw  */
875331Samw static int
validresource(const char * name)885331Samw validresource(const char *name)
895331Samw {
905331Samw 	const char *cp;
915331Samw 	size_t len;
925331Samw 
935331Samw 	if (name == NULL)
945331Samw 		return (B_FALSE);
955331Samw 
965331Samw 	len = strlen(name);
975331Samw 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
985331Samw 		return (B_FALSE);
995331Samw 
1005331Samw 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
1015331Samw 		return (B_FALSE);
1025331Samw 	}
1035331Samw 
1045331Samw 	for (cp = name; *cp != '\0'; cp++)
1055331Samw 		if (iscntrl(*cp))
1065331Samw 			return (B_FALSE);
1075331Samw 
1085331Samw 	return (B_TRUE);
1095331Samw }
1105331Samw 
1115331Samw /*
1125331Samw  * conv_to_utf8(input)
1135331Samw  *
1145331Samw  * Convert the input string to utf8 from the current locale.  If the
1155331Samw  * conversion fails, use the current locale, it is likely close
1165331Samw  * enough. For example, the "C" locale is a subset of utf-8. The
1175331Samw  * return value may be a new string or the original input string.
1185331Samw  */
1195331Samw 
1205331Samw static char *
conv_to_utf8(char * input)1215331Samw conv_to_utf8(char *input)
1225331Samw {
1235331Samw 	iconv_t cd;
1245521Sas200622 	char *inval = input;
1255331Samw 	char *output = input;
1265331Samw 	char *outleft;
1275331Samw 	char *curlocale;
1285331Samw 	size_t bytesleft;
1295331Samw 	size_t size;
1305331Samw 	size_t osize;
1315331Samw 	static int warned = 0;
1325331Samw 
1335331Samw 	curlocale = nl_langinfo(CODESET);
1345331Samw 	if (curlocale == NULL)
1355331Samw 		curlocale = "C";
1365331Samw 	cd = iconv_open("UTF-8", curlocale);
1375331Samw 	if (cd != NULL && cd != (iconv_t)-1) {
1385331Samw 		size = strlen(input);
1395331Samw 		/* Assume worst case of characters expanding to 4 bytes. */
1405331Samw 		bytesleft = size * 4;
1415331Samw 		output = calloc(bytesleft, 1);
1425331Samw 		if (output != NULL) {
1435331Samw 			outleft = output;
1445521Sas200622 			/* inval can be modified on return */
1455521Sas200622 			osize = iconv(cd, (const char **)&inval, &size,
1465331Samw 			    &outleft, &bytesleft);
1475331Samw 			if (osize == (size_t)-1 || size != 0) {
1485331Samw 				free(output);
1495331Samw 				output = input;
1505331Samw 			}
1515521Sas200622 		} else {
1525521Sas200622 			/* Need to return something. */
1535521Sas200622 			output = input;
1545331Samw 		}
1555331Samw 		(void) iconv_close(cd);
1565331Samw 	} else {
1575331Samw 		if (!warned)
1585331Samw 			(void) fprintf(stderr,
1595331Samw 			    gettext("Cannot convert to UTF-8 from %s\n"),
1605331Samw 			    curlocale ? curlocale : gettext("unknown"));
1615331Samw 		warned = 1;
1625331Samw 	}
1635331Samw 	return (output);
1645331Samw }
1655331Samw 
1665331Samw /*
1675331Samw  * conv_from(input)
1685331Samw  *
1695331Samw  * Convert the input string from utf8 to current locale.  If the
1705331Samw  * conversion isn't supported, just use as is. The return value may be
1715331Samw  * a new string or the original input string.
1725331Samw  */
1735331Samw 
1745331Samw static char *
conv_from_utf8(char * input)1755331Samw conv_from_utf8(char *input)
1765331Samw {
1775331Samw 	iconv_t cd;
1785331Samw 	char *output = input;
1795521Sas200622 	char *inval = input;
1805331Samw 	char *outleft;
1815331Samw 	char *curlocale;
1825331Samw 	size_t bytesleft;
1835331Samw 	size_t size;
1845331Samw 	size_t osize;
1855331Samw 	static int warned = 0;
1865331Samw 
1875331Samw 	curlocale = nl_langinfo(CODESET);
1885331Samw 	if (curlocale == NULL)
1895331Samw 		curlocale = "C";
1905331Samw 	cd = iconv_open(curlocale, "UTF-8");
1915331Samw 	if (cd != NULL && cd != (iconv_t)-1) {
1925331Samw 		size = strlen(input);
1935331Samw 		/* Assume worst case of characters expanding to 4 bytes. */
1945331Samw 		bytesleft = size * 4;
1955331Samw 		output = calloc(bytesleft, 1);
1965331Samw 		if (output != NULL) {
1975331Samw 			outleft = output;
1985521Sas200622 			osize = iconv(cd, (const char **)&inval, &size,
1995331Samw 			    &outleft, &bytesleft);
2005521Sas200622 			if (osize == (size_t)-1 || size != 0)
2015331Samw 				output = input;
2025521Sas200622 		} else {
2035521Sas200622 			/* Need to return something. */
2045521Sas200622 			output = input;
2055331Samw 		}
2065331Samw 		(void) iconv_close(cd);
2075331Samw 	} else {
2085331Samw 		if (!warned)
2095331Samw 			(void) fprintf(stderr,
2105331Samw 			    gettext("Cannot convert to %s from UTF-8\n"),
2115331Samw 			    curlocale ? curlocale : gettext("unknown"));
2125331Samw 		warned = 1;
2135331Samw 	}
2145331Samw 	return (output);
2155331Samw }
2165331Samw 
2175885Sdougm /*
2185885Sdougm  * print_rsrc_desc(resource, sharedesc)
2195885Sdougm  *
2205885Sdougm  * Print the resource description string after converting from UTF8 to
2215885Sdougm  * the current locale. If sharedesc is not NULL and there is no
2225885Sdougm  * description on the resource, use sharedesc. sharedesc will already
2235885Sdougm  * be converted to UTF8.
2245885Sdougm  */
2255885Sdougm 
2265331Samw static void
print_rsrc_desc(sa_resource_t resource,char * sharedesc)2275885Sdougm print_rsrc_desc(sa_resource_t resource, char *sharedesc)
2285331Samw {
2295331Samw 	char *description;
2305331Samw 	char *desc;
2315331Samw 
2325885Sdougm 	if (resource == NULL)
2335885Sdougm 		return;
2345885Sdougm 
2355331Samw 	description = sa_get_resource_description(resource);
2365331Samw 	if (description != NULL) {
2375331Samw 		desc = conv_from_utf8(description);
2385331Samw 		if (desc != description) {
2395331Samw 			sa_free_share_description(description);
2405331Samw 			description = desc;
2415331Samw 		}
2425885Sdougm 	} else if (sharedesc != NULL) {
2435885Sdougm 		description = strdup(sharedesc);
2445885Sdougm 	}
2455885Sdougm 	if (description != NULL) {
2465331Samw 		(void) printf("\t\"%s\"", description);
2475331Samw 		sa_free_share_description(description);
2485331Samw 	}
2495331Samw }
2505331Samw 
2515885Sdougm /*
2525885Sdougm  * set_resource_desc(share, description)
2535885Sdougm  *
2545885Sdougm  * Set the share description value after converting the description
2555885Sdougm  * string to UTF8 from the current locale.
2565885Sdougm  */
2575885Sdougm 
2585885Sdougm static int
set_resource_desc(sa_share_t share,char * description)2595885Sdougm set_resource_desc(sa_share_t share, char *description)
2605885Sdougm {
2615885Sdougm 	char *desc;
2625885Sdougm 	int ret;
2635885Sdougm 
2645885Sdougm 	desc = conv_to_utf8(description);
2655885Sdougm 	ret = sa_set_resource_description(share, desc);
2665885Sdougm 	if (description != desc)
2675885Sdougm 		sa_free_share_description(desc);
2685885Sdougm 	return (ret);
2695885Sdougm }
2705885Sdougm 
2715885Sdougm /*
2725885Sdougm  * set_share_desc(share, description)
2735885Sdougm  *
2745885Sdougm  * Set the resource description value after converting the description
2755885Sdougm  * string to UTF8 from the current locale.
2765885Sdougm  */
2775885Sdougm 
2785331Samw static int
set_share_desc(sa_share_t share,char * description)2795331Samw set_share_desc(sa_share_t share, char *description)
2805331Samw {
2815331Samw 	char *desc;
2825331Samw 	int ret;
2835331Samw 
2845331Samw 	desc = conv_to_utf8(description);
2855331Samw 	ret = sa_set_share_description(share, desc);
2865331Samw 	if (description != desc)
2875331Samw 		sa_free_share_description(desc);
2885331Samw 	return (ret);
2895331Samw }
2905331Samw 
2915331Samw /*
2925331Samw  * add_list(list, item, data, proto)
2935331Samw  *	Adds a new list member that points holds item in the list.
2943034Sdougm  *	If list is NULL, it starts a new list.  The function returns
2953034Sdougm  *	the first member of the list.
2963034Sdougm  */
2973034Sdougm struct list *
add_list(struct list * listp,void * item,void * data,char * proto)2985331Samw add_list(struct list *listp, void *item, void *data, char *proto)
2993034Sdougm {
3003034Sdougm 	struct list *new, *tmp;
3013034Sdougm 
3023034Sdougm 	new = malloc(sizeof (struct list));
3033034Sdougm 	if (new != NULL) {
3044653Sdougm 		new->next = NULL;
3054653Sdougm 		new->item = item;
3064653Sdougm 		new->itemdata = data;
3075331Samw 		new->proto = proto;
3083034Sdougm 	} else {
3094653Sdougm 		return (listp);
3103034Sdougm 	}
3113034Sdougm 
3123034Sdougm 	if (listp == NULL)
3134653Sdougm 		return (new);
3143034Sdougm 
3153034Sdougm 	for (tmp = listp; tmp->next != NULL; tmp = tmp->next) {
3163034Sdougm 		/* get to end of list */
3173034Sdougm 	}
3183034Sdougm 	tmp->next = new;
3193034Sdougm 	return (listp);
3203034Sdougm }
3213034Sdougm 
3223034Sdougm /*
3233034Sdougm  * free_list(list)
3243034Sdougm  *	Given a list, free all the members of the list;
3253034Sdougm  */
3263034Sdougm static void
free_list(struct list * listp)3273034Sdougm free_list(struct list *listp)
3283034Sdougm {
3293034Sdougm 	struct list *tmp;
3303034Sdougm 	while (listp != NULL) {
3314653Sdougm 		tmp = listp;
3324653Sdougm 		listp = listp->next;
3334653Sdougm 		free(tmp);
3343034Sdougm 	}
3353034Sdougm }
3363034Sdougm 
3373034Sdougm /*
3383034Sdougm  * check_authorization(instname, which)
3393034Sdougm  *
3403034Sdougm  * Checks to see if the specific type of authorization in which is
3413034Sdougm  * enabled for the user in this SMF service instance.
3423034Sdougm  */
3433034Sdougm 
3443034Sdougm static int
check_authorization(char * instname,int which)3453034Sdougm check_authorization(char *instname, int which)
3463034Sdougm {
3473034Sdougm 	scf_handle_t *handle = NULL;
3483034Sdougm 	scf_simple_prop_t *prop = NULL;
3493034Sdougm 	char svcstring[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
3503034Sdougm 	char *authstr = NULL;
3513034Sdougm 	ssize_t numauths;
3524653Sdougm 	int ret = B_TRUE;
3533034Sdougm 	uid_t uid;
3543034Sdougm 	struct passwd *pw = NULL;
3553034Sdougm 
3563034Sdougm 	uid = getuid();
3573034Sdougm 	pw = getpwuid(uid);
3584653Sdougm 	if (pw == NULL) {
3594653Sdougm 		ret = B_FALSE;
3604653Sdougm 	} else {
3614653Sdougm 		/*
3624653Sdougm 		 * Since names are restricted to SA_MAX_NAME_LEN won't
3634653Sdougm 		 * overflow.
3644653Sdougm 		 */
3654653Sdougm 		(void) snprintf(svcstring, sizeof (svcstring), "%s:%s",
3664653Sdougm 		    SA_SVC_FMRI_BASE, instname);
3674653Sdougm 		handle = scf_handle_create(SCF_VERSION);
3684653Sdougm 		if (handle != NULL) {
3694653Sdougm 			if (scf_handle_bind(handle) == 0) {
3704653Sdougm 				switch (which) {
3714653Sdougm 				case SVC_SET:
3724653Sdougm 					prop = scf_simple_prop_get(handle,
3734653Sdougm 					    svcstring, "general",
3744653Sdougm 					    SVC_AUTH_VALUE);
3754653Sdougm 					break;
3764653Sdougm 				case SVC_ACTION:
3774653Sdougm 					prop = scf_simple_prop_get(handle,
3784653Sdougm 					    svcstring, "general",
3794653Sdougm 					    SVC_AUTH_ACTION);
3804653Sdougm 					break;
3814653Sdougm 				}
3824653Sdougm 			}
3833034Sdougm 		}
3843034Sdougm 	}
3853034Sdougm 	/* make sure we have an authorization string property */
3863034Sdougm 	if (prop != NULL) {
3874653Sdougm 		int i;
3884653Sdougm 		numauths = scf_simple_prop_numvalues(prop);
3894653Sdougm 		for (ret = 0, i = 0; i < numauths; i++) {
3904653Sdougm 			authstr = scf_simple_prop_next_astring(prop);
3914653Sdougm 			if (authstr != NULL) {
3924653Sdougm 				/* check if this user has one of the strings */
3934653Sdougm 				if (chkauthattr(authstr, pw->pw_name)) {
3944653Sdougm 					ret = 1;
3954653Sdougm 					break;
3964653Sdougm 				}
3974653Sdougm 			}
3983034Sdougm 		}
3994653Sdougm 		endauthattr();
4004653Sdougm 		scf_simple_prop_free(prop);
4013034Sdougm 	} else {
4024653Sdougm 		/* no authorization string defined */
4034653Sdougm 		ret = 0;
4043034Sdougm 	}
4053034Sdougm 	if (handle != NULL)
4064653Sdougm 		scf_handle_destroy(handle);
4073034Sdougm 	return (ret);
4083034Sdougm }
4093034Sdougm 
4103034Sdougm /*
4113034Sdougm  * check_authorizations(instname, flags)
4123034Sdougm  *
4133034Sdougm  * check all the needed authorizations for the user in this service
4143034Sdougm  * instance. Return value of 1(true) or 0(false) indicates whether
4153034Sdougm  * there are authorizations for the user or not.
4163034Sdougm  */
4173034Sdougm 
4183034Sdougm static int
check_authorizations(char * instname,int flags)4193034Sdougm check_authorizations(char *instname, int flags)
4203034Sdougm {
4213034Sdougm 	int ret1 = 0;
4223034Sdougm 	int ret2 = 0;
4233034Sdougm 	int ret;
4243034Sdougm 
4253034Sdougm 	if (flags & SVC_SET)
4264653Sdougm 		ret1 = check_authorization(instname, SVC_SET);
4273034Sdougm 	if (flags & SVC_ACTION)
4284653Sdougm 		ret2 = check_authorization(instname, SVC_ACTION);
4293034Sdougm 	switch (flags) {
4303034Sdougm 	case SVC_ACTION:
4314653Sdougm 		ret = ret2;
4324653Sdougm 		break;
4333034Sdougm 	case SVC_SET:
4344653Sdougm 		ret = ret1;
4354653Sdougm 		break;
4363034Sdougm 	case SVC_ACTION|SVC_SET:
4374653Sdougm 		ret = ret1 & ret2;
4384653Sdougm 		break;
4393034Sdougm 	default:
4404653Sdougm 		/* if not flags set, we assume we don't need authorizations */
4414653Sdougm 		ret = 1;
4423034Sdougm 	}
4433034Sdougm 	return (ret);
4443034Sdougm }
4453034Sdougm 
4463034Sdougm /*
4475331Samw  * notify_or_enable_share(share, protocol)
4485331Samw  *
4495331Samw  * Since some protocols don't want an "enable" when properties change,
4505331Samw  * this function will use the protocol specific notify function
4515331Samw  * first. If that fails, it will then attempt to use the
4525331Samw  * sa_enable_share().  "protocol" is the protocol that was specified
4535331Samw  * on the command line.
4545331Samw  */
4555331Samw static void
notify_or_enable_share(sa_share_t share,char * protocol)4565331Samw notify_or_enable_share(sa_share_t share, char *protocol)
4575331Samw {
4585331Samw 	sa_group_t group;
4595331Samw 	sa_optionset_t opt;
4605331Samw 	int ret = SA_OK;
4615331Samw 	char *path;
4625331Samw 	char *groupproto;
4635331Samw 	sa_share_t parent = share;
4645331Samw 
4655331Samw 	/* If really a resource, get parent share */
4665331Samw 	if (!sa_is_share(share)) {
4675331Samw 		parent = sa_get_resource_parent((sa_resource_t)share);
4685331Samw 	}
4695331Samw 
4705331Samw 	/*
4715331Samw 	 * Now that we've got a share in "parent", make sure it has a path.
4725331Samw 	 */
4735331Samw 	path = sa_get_share_attr(parent, "path");
4745331Samw 	if (path == NULL)
4755331Samw 		return;
4765331Samw 
4775331Samw 	group = sa_get_parent_group(parent);
4785331Samw 
4795331Samw 	if (group == NULL) {
4805331Samw 		sa_free_attr_string(path);
4815331Samw 		return;
4825331Samw 	}
4835331Samw 	for (opt = sa_get_optionset(group, NULL);
4845331Samw 	    opt != NULL;
4855331Samw 	    opt = sa_get_next_optionset(opt)) {
4865331Samw 		groupproto = sa_get_optionset_attr(opt, "type");
4875331Samw 		if (groupproto == NULL ||
4885331Samw 		    (protocol != NULL && strcmp(groupproto, protocol) != 0)) {
48911337SWilliam.Krier@Sun.COM 			if (groupproto != NULL)
49011337SWilliam.Krier@Sun.COM 				sa_free_attr_string(groupproto);
4915331Samw 			continue;
4925331Samw 		}
4935331Samw 		if (sa_is_share(share)) {
4945331Samw 			if ((ret = sa_proto_change_notify(share,
4955331Samw 			    groupproto)) != SA_OK) {
4965331Samw 				ret = sa_enable_share(share, groupproto);
4975331Samw 				if (ret != SA_OK) {
4985331Samw 					(void) printf(
4995331Samw 					    gettext("Could not reenable"
5005331Samw 					    " share %s: %s\n"),
5015331Samw 					    path, sa_errorstr(ret));
5025331Samw 				}
5035331Samw 			}
5045331Samw 		} else {
5055331Samw 			/* Must be a resource */
5065331Samw 			if ((ret = sa_proto_notify_resource(share,
5075331Samw 			    groupproto)) != SA_OK) {
5085331Samw 				ret = sa_enable_resource(share, groupproto);
5095331Samw 				if (ret != SA_OK) {
5105331Samw 					(void) printf(
5115331Samw 					    gettext("Could not "
5125331Samw 					    "reenable resource %s: "
5135331Samw 					    "%s\n"), path,
5145331Samw 					    sa_errorstr(ret));
5155331Samw 				}
5165331Samw 			}
5175331Samw 		}
5185331Samw 		sa_free_attr_string(groupproto);
5195331Samw 	}
5205331Samw 	sa_free_attr_string(path);
5215331Samw }
5225331Samw 
5235331Samw /*
5245331Samw  * enable_group(group, updateproto, notify, proto)
5253082Sdougm  *
5263082Sdougm  * enable all the shares in the specified group. This is a helper for
5273082Sdougm  * enable_all_groups in order to simplify regular and subgroup (zfs)
5285331Samw  * enabling. Group has already been checked for non-NULL. If notify
5295331Samw  * is non-zero, attempt to use the notify interface rather than
5305331Samw  * enable.
5313082Sdougm  */
5323082Sdougm static void
enable_group(sa_group_t group,char * updateproto,int notify,char * proto)5335331Samw enable_group(sa_group_t group, char *updateproto, int notify, char *proto)
5343082Sdougm {
5353082Sdougm 	sa_share_t share;
5363082Sdougm 
5373082Sdougm 	for (share = sa_get_share(group, NULL);
5383082Sdougm 	    share != NULL;
5393082Sdougm 	    share = sa_get_next_share(share)) {
5404653Sdougm 		if (updateproto != NULL)
5414653Sdougm 			(void) sa_update_legacy(share, updateproto);
5425331Samw 		if (notify)
5435331Samw 			notify_or_enable_share(share, proto);
5445331Samw 		else
5455331Samw 			(void) sa_enable_share(share, proto);
5463082Sdougm 	}
5473082Sdougm }
5483082Sdougm 
5493082Sdougm /*
5504241Sdougm  * isenabled(group)
5514241Sdougm  *
5524241Sdougm  * Returns B_TRUE if the group is enabled or B_FALSE if it isn't.
5534241Sdougm  * Moved to separate function to reduce clutter in the code.
5544241Sdougm  */
5554241Sdougm 
5564241Sdougm static int
isenabled(sa_group_t group)5574241Sdougm isenabled(sa_group_t group)
5584241Sdougm {
5594241Sdougm 	char *state;
5604241Sdougm 	int ret = B_FALSE;
5614241Sdougm 
5624241Sdougm 	if (group != NULL) {
5634653Sdougm 		state = sa_get_group_attr(group, "state");
5644653Sdougm 		if (state != NULL) {
5655331Samw 
5664653Sdougm 			if (strcmp(state, "enabled") == 0)
5674653Sdougm 				ret = B_TRUE;
5684653Sdougm 			sa_free_attr_string(state);
5694653Sdougm 		}
5704241Sdougm 	}
5714241Sdougm 	return (ret);
5724241Sdougm }
5734241Sdougm 
5744241Sdougm /*
5753082Sdougm  * enable_all_groups(list, setstate, online, updateproto)
5765331Samw  *
5775331Samw  * Given a list of groups, enable each one found.  If updateproto is
5785331Samw  * not NULL, then update all the shares for the protocol that was
5795331Samw  * passed in. If enable is non-zero, tell enable_group to try the
5805331Samw  * notify interface since this is a property change.
5813034Sdougm  */
5823034Sdougm static int
enable_all_groups(sa_handle_t handle,struct list * work,int setstate,int online,char * updateproto,int enable)5833910Sdougm enable_all_groups(sa_handle_t handle, struct list *work, int setstate,
5845331Samw     int online, char *updateproto, int enable)
5853034Sdougm {
5864241Sdougm 	int ret;
5873034Sdougm 	char instance[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
5883034Sdougm 	char *state;
5893034Sdougm 	char *name;
5903034Sdougm 	char *zfs = NULL;
5913034Sdougm 	sa_group_t group;
5923082Sdougm 	sa_group_t subgroup;
5933034Sdougm 
5944241Sdougm 	for (ret = SA_OK; work != NULL;	work = work->next) {
5954653Sdougm 		group = (sa_group_t)work->item;
5964241Sdougm 
5974241Sdougm 		/*
5984241Sdougm 		 * If setstate == TRUE, then make sure to set
5994241Sdougm 		 * enabled. This needs to be done here in order for
6004241Sdougm 		 * the isenabled check to succeed on a newly enabled
6014241Sdougm 		 * group.
6024241Sdougm 		 */
6034653Sdougm 		if (setstate == B_TRUE) {
6044653Sdougm 			ret = sa_set_group_attr(group, "state",	"enabled");
6054653Sdougm 			if (ret != SA_OK)
6064653Sdougm 				break;
6074653Sdougm 		}
6084241Sdougm 
6094241Sdougm 		/*
6104241Sdougm 		 * Check to see if group is enabled. If it isn't, skip
6114241Sdougm 		 * the rest.  We don't want shares starting if the
6124241Sdougm 		 * group is disabled. The properties may have been
6134241Sdougm 		 * updated, but there won't be a change until the
6144241Sdougm 		 * group is enabled.
6154241Sdougm 		 */
6164653Sdougm 		if (!isenabled(group))
6174653Sdougm 			continue;
6184653Sdougm 
6194653Sdougm 		/* if itemdata != NULL then a single share */
6204653Sdougm 		if (work->itemdata != NULL) {
6215331Samw 			if (enable) {
6225331Samw 				if (work->itemdata != NULL)
6235331Samw 					notify_or_enable_share(work->itemdata,
6245331Samw 					    updateproto);
6255331Samw 				else
6265331Samw 					ret = SA_CONFIG_ERR;
6275331Samw 			} else {
6285331Samw 				if (sa_is_share(work->itemdata)) {
6295331Samw 					ret = sa_enable_share(
6305331Samw 					    (sa_share_t)work->itemdata,
6315331Samw 					    updateproto);
6325331Samw 				} else {
6335331Samw 					ret = sa_enable_resource(
6345331Samw 					    (sa_resource_t)work->itemdata,
6355331Samw 					    updateproto);
6365331Samw 				}
6375331Samw 			}
6383034Sdougm 		}
6394653Sdougm 		if (ret != SA_OK)
6404653Sdougm 			break;
6414653Sdougm 
6424653Sdougm 		/* if itemdata == NULL then the whole group */
6434653Sdougm 		if (work->itemdata == NULL) {
6444653Sdougm 			zfs = sa_get_group_attr(group, "zfs");
6454653Sdougm 			/*
6465331Samw 			 * If the share is managed by ZFS, don't
6474653Sdougm 			 * update any of the protocols since ZFS is
6485331Samw 			 * handling this.  Updateproto will contain
6494653Sdougm 			 * the name of the protocol that we want to
6504653Sdougm 			 * update legacy files for.
6514653Sdougm 			 */
6525331Samw 			enable_group(group, zfs == NULL ? updateproto : NULL,
6535331Samw 			    enable, work->proto);
65411337SWilliam.Krier@Sun.COM 			if (zfs != NULL)
65511337SWilliam.Krier@Sun.COM 				sa_free_attr_string(zfs);
65611337SWilliam.Krier@Sun.COM 
6574653Sdougm 			for (subgroup = sa_get_sub_group(group);
6584653Sdougm 			    subgroup != NULL;
6594653Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
6604653Sdougm 				/* never update legacy for ZFS subgroups */
6615331Samw 				enable_group(subgroup, NULL, enable,
6625331Samw 				    work->proto);
6633034Sdougm 			}
6643034Sdougm 		}
6654653Sdougm 		if (online) {
6664653Sdougm 			zfs = sa_get_group_attr(group, "zfs");
6674653Sdougm 			name = sa_get_group_attr(group, "name");
6684653Sdougm 			if (name != NULL) {
6694653Sdougm 				if (zfs == NULL) {
6704653Sdougm 					(void) snprintf(instance,
6714653Sdougm 					    sizeof (instance), "%s:%s",
6724653Sdougm 					    SA_SVC_FMRI_BASE, name);
6734653Sdougm 					state = smf_get_state(instance);
6744653Sdougm 					if (state == NULL ||
6754653Sdougm 					    strcmp(state, "online") != 0) {
6764653Sdougm 						(void) smf_enable_instance(
6774653Sdougm 						    instance, 0);
6784653Sdougm 						free(state);
6794653Sdougm 					}
6804653Sdougm 				} else {
6814653Sdougm 					sa_free_attr_string(zfs);
6824653Sdougm 					zfs = NULL;
6834653Sdougm 				}
6844653Sdougm 				if (name != NULL)
6854653Sdougm 					sa_free_attr_string(name);
6864653Sdougm 			}
6874653Sdougm 		}
6883034Sdougm 	}
6893034Sdougm 	if (ret == SA_OK) {
6904653Sdougm 		ret = sa_update_config(handle);
6913034Sdougm 	}
6923034Sdougm 	return (ret);
6933034Sdougm }
6943034Sdougm 
6953034Sdougm /*
6963034Sdougm  * chk_opt(optlistp, security, proto)
6973034Sdougm  *
6983034Sdougm  * Do a sanity check on the optlist provided for the protocol.  This
6993034Sdougm  * is a syntax check and verification that the property is either a
7003034Sdougm  * general or specific to a names optionset.
7013034Sdougm  */
7023034Sdougm 
7033034Sdougm static int
chk_opt(struct options * optlistp,int security,char * proto)7043034Sdougm chk_opt(struct options *optlistp, int security, char *proto)
7053034Sdougm {
7063034Sdougm 	struct options *optlist;
7073034Sdougm 	char *sep = "";
7083034Sdougm 	int notfirst = 0;
7093034Sdougm 	int ret;
7103034Sdougm 
7113034Sdougm 	for (optlist = optlistp; optlist != NULL; optlist = optlist->next) {
7124653Sdougm 		char *optname;
7134653Sdougm 
7144653Sdougm 		optname = optlist->optname;
7154653Sdougm 		ret = OPT_ADD_OK;
7164653Sdougm 		/* extract property/value pair */
7174653Sdougm 		if (sa_is_security(optname, proto)) {
7184653Sdougm 			if (!security)
7194653Sdougm 				ret = OPT_ADD_SECURITY;
7204653Sdougm 		} else {
7214653Sdougm 			if (security)
7224653Sdougm 				ret = OPT_ADD_PROPERTY;
7234653Sdougm 		}
7244653Sdougm 		if (ret != OPT_ADD_OK) {
7254653Sdougm 			if (notfirst == 0)
7264653Sdougm 				(void) printf(
7274653Sdougm 				    gettext("Property syntax error: "));
7284653Sdougm 			switch (ret) {
7294653Sdougm 			case OPT_ADD_SYNTAX:
7304653Sdougm 				(void) printf(gettext("%ssyntax error: %s"),
7313034Sdougm 				    sep, optname);
7324653Sdougm 				sep = ", ";
7334653Sdougm 				break;
7344653Sdougm 			case OPT_ADD_SECURITY:
7354653Sdougm 				(void) printf(gettext("%s%s requires -S"),
7363034Sdougm 				    optname, sep);
7374653Sdougm 				sep = ", ";
7384653Sdougm 				break;
7394653Sdougm 			case OPT_ADD_PROPERTY:
7404653Sdougm 				(void) printf(
7414653Sdougm 				    gettext("%s%s not supported with -S"),
7423034Sdougm 				    optname, sep);
7434653Sdougm 				sep = ", ";
7444653Sdougm 				break;
7454653Sdougm 			}
7464653Sdougm 			notfirst++;
7473034Sdougm 		}
7483034Sdougm 	}
7493034Sdougm 	if (notfirst) {
7504653Sdougm 		(void) printf("\n");
7514653Sdougm 		ret = SA_SYNTAX_ERR;
7523034Sdougm 	}
7533034Sdougm 	return (ret);
7543034Sdougm }
7553034Sdougm 
7563034Sdougm /*
7573034Sdougm  * free_opt(optlist)
7583034Sdougm  *	Free the specified option list.
7593034Sdougm  */
7603034Sdougm static void
free_opt(struct options * optlist)7613034Sdougm free_opt(struct options *optlist)
7623034Sdougm {
7633034Sdougm 	struct options *nextopt;
7643034Sdougm 	while (optlist != NULL) {
7653034Sdougm 		nextopt = optlist->next;
7663034Sdougm 		free(optlist);
7673034Sdougm 		optlist = nextopt;
7683034Sdougm 	}
7693034Sdougm }
7703034Sdougm 
7713034Sdougm /*
7723034Sdougm  * check property list for valid properties
7733034Sdougm  * A null value is a remove which is always valid.
7743034Sdougm  */
7753034Sdougm static int
valid_options(sa_handle_t handle,struct options * optlist,char * proto,void * object,char * sec)7766214Sdougm valid_options(sa_handle_t handle, struct options *optlist, char *proto,
7776214Sdougm     void *object, char *sec)
7783034Sdougm {
7793034Sdougm 	int ret = SA_OK;
7803034Sdougm 	struct options *cur;
7813034Sdougm 	sa_property_t prop;
7823034Sdougm 	sa_optionset_t parent = NULL;
7833034Sdougm 
7843034Sdougm 	if (object != NULL) {
7854653Sdougm 		if (sec == NULL)
7864653Sdougm 			parent = sa_get_optionset(object, proto);
7874653Sdougm 		else
7884653Sdougm 			parent = sa_get_security(object, sec, proto);
7893034Sdougm 	}
7903034Sdougm 
7913034Sdougm 	for (cur = optlist; cur != NULL; cur = cur->next) {
7924653Sdougm 		if (cur->optvalue == NULL)
7934653Sdougm 			continue;
7943034Sdougm 		prop = sa_create_property(cur->optname, cur->optvalue);
7953034Sdougm 		if (prop == NULL)
7964653Sdougm 			ret = SA_NO_MEMORY;
7973034Sdougm 		if (ret != SA_OK ||
7986214Sdougm 		    (ret = sa_valid_property(handle, parent, proto, prop)) !=
7996214Sdougm 		    SA_OK) {
8004653Sdougm 			(void) printf(
8014653Sdougm 			    gettext("Could not add property %s: %s\n"),
8024653Sdougm 			    cur->optname, sa_errorstr(ret));
8033034Sdougm 		}
8043034Sdougm 		(void) sa_remove_property(prop);
8053034Sdougm 	}
8063034Sdougm 	return (ret);
8073034Sdougm }
8083034Sdougm 
8093034Sdougm /*
8103034Sdougm  * add_optionset(group, optlist, protocol, *err)
8113034Sdougm  *	Add the options in optlist to an optionset and then add the optionset
8123034Sdougm  *	to the group.
8133034Sdougm  *
8143034Sdougm  *	The return value indicates if there was a "change" while errors are
8153034Sdougm  *	returned via the *err parameters.
8163034Sdougm  */
8173034Sdougm static int
add_optionset(sa_group_t group,struct options * optlist,char * proto,int * err)8183034Sdougm add_optionset(sa_group_t group, struct options *optlist, char *proto, int *err)
8193034Sdougm {
8203034Sdougm 	sa_optionset_t optionset;
8213034Sdougm 	int ret = SA_OK;
8225331Samw 	int result = B_FALSE;
8236214Sdougm 	sa_handle_t handle;
8243034Sdougm 
8253034Sdougm 	optionset = sa_get_optionset(group, proto);
8263034Sdougm 	if (optionset == NULL) {
8274653Sdougm 		optionset = sa_create_optionset(group, proto);
8285331Samw 		if (optionset == NULL)
8295331Samw 			ret = SA_NO_MEMORY;
8305331Samw 		result = B_TRUE; /* adding a protocol is a change */
8313034Sdougm 	}
8324653Sdougm 	if (optionset == NULL) {
8334653Sdougm 		ret = SA_NO_MEMORY;
8344653Sdougm 		goto out;
8354653Sdougm 	}
8366214Sdougm 	handle = sa_find_group_handle(group);
8376214Sdougm 	if (handle == NULL) {
8386214Sdougm 		ret = SA_CONFIG_ERR;
8396214Sdougm 		goto out;
8406214Sdougm 	}
8414653Sdougm 	while (optlist != NULL) {
8423034Sdougm 		sa_property_t prop;
8433034Sdougm 		prop = sa_get_property(optionset, optlist->optname);
8443034Sdougm 		if (prop == NULL) {
8453034Sdougm 			/*
8463034Sdougm 			 * add the property, but only if it is
8473034Sdougm 			 * a non-NULL or non-zero length value
8483034Sdougm 			 */
8494653Sdougm 			if (optlist->optvalue != NULL) {
8504653Sdougm 				prop = sa_create_property(optlist->optname,
8514653Sdougm 				    optlist->optvalue);
8524653Sdougm 				if (prop != NULL) {
8536214Sdougm 					ret = sa_valid_property(handle,
8546214Sdougm 					    optionset, proto, prop);
8554653Sdougm 					if (ret != SA_OK) {
8564653Sdougm 						(void) sa_remove_property(prop);
8574653Sdougm 						(void) printf(gettext("Could "
8584653Sdougm 						    "not add property "
8594653Sdougm 						    "%s: %s\n"),
8604653Sdougm 						    optlist->optname,
8614653Sdougm 						    sa_errorstr(ret));
8624653Sdougm 					}
8634653Sdougm 				}
8644653Sdougm 				if (ret == SA_OK) {
8654653Sdougm 					ret = sa_add_property(optionset, prop);
8664653Sdougm 					if (ret != SA_OK) {
8674653Sdougm 						(void) printf(gettext(
8684653Sdougm 						    "Could not add property "
8694653Sdougm 						    "%s: %s\n"),
8704653Sdougm 						    optlist->optname,
8714653Sdougm 						    sa_errorstr(ret));
8724653Sdougm 					} else {
8734653Sdougm 						/* there was a change */
8745331Samw 						result = B_TRUE;
8754653Sdougm 					}
8764653Sdougm 				}
8773034Sdougm 			}
8784653Sdougm 		} else {
8794653Sdougm 			ret = sa_update_property(prop, optlist->optvalue);
8804653Sdougm 			/* should check to see if value changed */
8814653Sdougm 			if (ret != SA_OK) {
8824653Sdougm 				(void) printf(gettext("Could not update "
8834653Sdougm 				    "property %s: %s\n"), optlist->optname,
8844653Sdougm 				    sa_errorstr(ret));
8854653Sdougm 			} else {
8865331Samw 				result = B_TRUE;
8873034Sdougm 			}
8883034Sdougm 		}
8893034Sdougm 		optlist = optlist->next;
8903034Sdougm 	}
8914653Sdougm 	ret = sa_commit_properties(optionset, 0);
8924653Sdougm 
8934653Sdougm out:
8943034Sdougm 	if (err != NULL)
8954653Sdougm 		*err = ret;
8963034Sdougm 	return (result);
8973034Sdougm }
8983034Sdougm 
8993034Sdougm /*
9005331Samw  * resource_compliant(group)
9015331Samw  *
9025331Samw  * Go through all the shares in the group. Assume compliant, but if
9035331Samw  * any share doesn't have at least one resource name, it isn't
9045331Samw  * compliant.
9055331Samw  */
9065331Samw static int
resource_compliant(sa_group_t group)9075331Samw resource_compliant(sa_group_t group)
9085331Samw {
9095331Samw 	sa_share_t share;
9105331Samw 
9115331Samw 	for (share = sa_get_share(group, NULL); share != NULL;
9125331Samw 	    share = sa_get_next_share(share)) {
9135331Samw 		if (sa_get_share_resource(share, NULL) == NULL) {
9145331Samw 			return (B_FALSE);
9155331Samw 		}
9165331Samw 	}
9175331Samw 	return (B_TRUE);
9185331Samw }
9195331Samw 
9205331Samw /*
9215331Samw  * fix_path(path)
9225331Samw  *
9235331Samw  * change all illegal characters to something else.  For now, all get
9245331Samw  * converted to '_' and the leading '/' is stripped off. This is used
9255331Samw  * to construct an resource name (SMB share name) that is valid.
9265331Samw  * Caller must pass a valid path.
9275331Samw  */
9285331Samw static void
fix_path(char * path)9295331Samw fix_path(char *path)
9305331Samw {
9315331Samw 	char *cp;
9325331Samw 	size_t len;
9335331Samw 
9345331Samw 	assert(path != NULL);
9355331Samw 
9365331Samw 	/* make sure we are appropriate length */
9375331Samw 	cp = path + 1; /* skip leading slash */
9385331Samw 	while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) {
9395331Samw 		cp = strchr(cp, '/');
9405331Samw 		if (cp != NULL)
9415331Samw 			cp++;
9425331Samw 	}
9435331Samw 	/* two cases - cp == NULL and cp is substring of path */
9445331Samw 	if (cp == NULL) {
9455331Samw 		/* just take last SA_MAX_RESOURCE_NAME chars */
9465331Samw 		len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME;
9475331Samw 		(void) memmove(path, path + len, SA_MAX_RESOURCE_NAME);
9485331Samw 		path[SA_MAX_RESOURCE_NAME] = '\0';
9495331Samw 	} else {
9505331Samw 		len = strlen(cp) + 1;
9515331Samw 		(void) memmove(path, cp, len);
9525331Samw 	}
9535331Samw 
9545331Samw 	/*
9555331Samw 	 * Don't want any of the characters that are not allowed
9565331Samw 	 * in and SMB share name. Replace them with '_'.
9575331Samw 	 */
9585331Samw 	while (*path) {
9595331Samw 		switch (*path) {
9605331Samw 		case '/':
9615331Samw 		case '"':
9625331Samw 		case '\\':
9635331Samw 		case '[':
9645331Samw 		case ']':
9655331Samw 		case ':':
9665331Samw 		case '|':
9675331Samw 		case '<':
9685331Samw 		case '>':
9695331Samw 		case '+':
9705331Samw 		case ';':
9715331Samw 		case ',':
9725331Samw 		case '?':
9735331Samw 		case '*':
9745331Samw 		case '=':
9755331Samw 		case '\t':
9765331Samw 			*path = '_';
9775331Samw 			break;
9785331Samw 		}
9795331Samw 		path++;
9805331Samw 	}
9815331Samw }
9825331Samw 
9835331Samw /*
9845331Samw  * name_adjust(path, count)
9855331Samw  *
9865331Samw  * Add a ~<count> in place of last few characters. The total number of
9875331Samw  * characters is dependent on count.
9885331Samw  */
9895331Samw #define	MAX_MANGLE_NUMBER	10000
9905331Samw 
9915331Samw static int
name_adjust(char * path,int count)9925331Samw name_adjust(char *path, int count)
9935331Samw {
9945331Samw 	size_t len;
9955331Samw 
9965331Samw 	len = strlen(path) - 2;
9975331Samw 	if (count > 10)
9985331Samw 		len--;
9995331Samw 	if (count > 100)
10005331Samw 		len--;
10015331Samw 	if (count > 1000)
10025331Samw 		len--;
10035331Samw 	if (len > 0)
10045331Samw 		(void) sprintf(path + len, "~%d", count);
10055331Samw 	else
10065331Samw 		return (SA_BAD_VALUE);
10075331Samw 
10085331Samw 	return (SA_OK);
10095331Samw }
10105331Samw 
10115331Samw /*
10125331Samw  * make_resources(group)
10135331Samw  *
10145331Samw  * Go through all the shares in the group and make them have resource
10155331Samw  * names.
10165331Samw  */
10175331Samw static void
make_resources(sa_group_t group)10185331Samw make_resources(sa_group_t group)
10195331Samw {
10205331Samw 	sa_share_t share;
10215331Samw 	int count;
10225331Samw 	int err = SA_OK;
10235331Samw 
10245331Samw 	for (share = sa_get_share(group, NULL); share != NULL;
10255331Samw 	    share = sa_get_next_share(share)) {
10265331Samw 		/* Skip those with resources */
10275331Samw 		if (sa_get_share_resource(share, NULL) == NULL) {
10285331Samw 			char *path;
10295331Samw 			path = sa_get_share_attr(share, "path");
10305331Samw 			if (path == NULL)
10315331Samw 				continue;
10325331Samw 			fix_path(path);
10335331Samw 			count = 0;	/* reset for next resource */
10345331Samw 			while (sa_add_resource(share, path,
10355331Samw 			    SA_SHARE_PERMANENT, &err) == NULL &&
10365331Samw 			    err == SA_DUPLICATE_NAME) {
10375331Samw 				int ret;
10385331Samw 				ret = name_adjust(path, count);
10395331Samw 				count++;
10405331Samw 				if (ret != SA_OK ||
10415331Samw 				    count >= MAX_MANGLE_NUMBER) {
10425331Samw 					(void) printf(gettext(
10435331Samw 					    "Cannot create resource name for"
10445331Samw 					    " path: %s\n"), path);
10455331Samw 					break;
10465331Samw 				}
10475331Samw 			}
10485331Samw 			sa_free_attr_string(path);
10495331Samw 		}
10505331Samw 	}
10515331Samw }
10525331Samw 
10535331Samw /*
10546088Sdougm  * check_valid_group(group, protocol)
10556088Sdougm  *
10566088Sdougm  * Check to see that the group should have the protocol added (if
10576088Sdougm  * there is one specified).
10586088Sdougm  */
10596088Sdougm 
10606088Sdougm static int
check_valid_group(sa_group_t group,char * groupname,char * protocol)10616088Sdougm check_valid_group(sa_group_t group, char *groupname, char *protocol)
10626088Sdougm {
10636088Sdougm 
10646088Sdougm 	if (protocol != NULL) {
10656088Sdougm 		if (has_protocol(group, protocol)) {
10666088Sdougm 			(void) printf(gettext(
10676088Sdougm 			    "Group \"%s\" already exists"
10686088Sdougm 			    " with protocol %s\n"), groupname,
10696088Sdougm 			    protocol);
10706088Sdougm 			return (SA_DUPLICATE_NAME);
10716088Sdougm 		} else if (strcmp(groupname, "default") == 0 &&
10726088Sdougm 		    strcmp(protocol, "nfs") != 0) {
10736088Sdougm 			(void) printf(gettext(
10746088Sdougm 			    "Group \"%s\" only allows protocol "
10756088Sdougm 			    "\"%s\"\n"), groupname, "nfs");
10766088Sdougm 			return (SA_INVALID_PROTOCOL);
10776088Sdougm 		}
10786088Sdougm 	} else {
10796088Sdougm 		/* must add new protocol */
10806088Sdougm 		(void) printf(gettext(
10816088Sdougm 		    "Group already exists and no protocol "
10826088Sdougm 		    "specified.\n"));
10836088Sdougm 		return (SA_DUPLICATE_NAME);
10846088Sdougm 	}
10856088Sdougm 	return (SA_OK);
10866088Sdougm }
10876088Sdougm 
10886088Sdougm /*
10896088Sdougm  * enforce_featureset(group, protocol, dryrun, force)
10906088Sdougm  *
10916088Sdougm  * Check the protocol featureset against the group and enforce any
10926088Sdougm  * rules that might be imposed.
10936088Sdougm  */
10946088Sdougm 
10956088Sdougm static int
enforce_featureset(sa_group_t group,char * protocol,boolean_t dryrun,boolean_t force)10966088Sdougm enforce_featureset(sa_group_t group, char *protocol, boolean_t dryrun,
10976088Sdougm     boolean_t force)
10986088Sdougm {
10996088Sdougm 	uint64_t features;
11006088Sdougm 
11016088Sdougm 	if (protocol == NULL)
11026088Sdougm 		return (SA_OK);
11036088Sdougm 
11046088Sdougm 	/*
11056088Sdougm 	 * First check to see if specified protocol is one we want to
11066088Sdougm 	 * allow on a group. Only server protocols are allowed here.
11076088Sdougm 	 */
11086088Sdougm 	features = sa_proto_get_featureset(protocol);
11096088Sdougm 	if (!(features & SA_FEATURE_SERVER)) {
11106088Sdougm 		(void) printf(
11116088Sdougm 		    gettext("Protocol \"%s\" not supported.\n"), protocol);
11126088Sdougm 		return (SA_INVALID_PROTOCOL);
11136088Sdougm 	}
11146088Sdougm 
11156088Sdougm 	/*
11166088Sdougm 	 * Check to see if the new protocol is one that requires
11176088Sdougm 	 * resource names and make sure we are compliant before
11186088Sdougm 	 * proceeding.
11196088Sdougm 	 */
11206088Sdougm 	if ((features & SA_FEATURE_RESOURCE) &&
11216088Sdougm 	    !resource_compliant(group)) {
11226088Sdougm 		if (force && !dryrun) {
11236088Sdougm 			make_resources(group);
11246088Sdougm 		} else {
11256088Sdougm 			(void) printf(
11266088Sdougm 			    gettext("Protocol requires resource names to be "
11276088Sdougm 			    "set: %s\n"), protocol);
11286088Sdougm 			return (SA_RESOURCE_REQUIRED);
11296088Sdougm 		}
11306088Sdougm 	}
11316088Sdougm 	return (SA_OK);
11326088Sdougm }
11336088Sdougm 
11346088Sdougm /*
11356088Sdougm  * set_all_protocols(group)
11366088Sdougm  *
11376088Sdougm  * Get the list of all protocols and add all server protocols to the
11386088Sdougm  * group.
11396088Sdougm  */
11406088Sdougm 
11416088Sdougm static int
set_all_protocols(sa_group_t group)11426088Sdougm set_all_protocols(sa_group_t group)
11436088Sdougm {
11446088Sdougm 	char **protolist;
11456088Sdougm 	int numprotos, i;
11466088Sdougm 	uint64_t features;
11476088Sdougm 	sa_optionset_t optionset;
11486088Sdougm 	int ret = SA_OK;
11496088Sdougm 
11506088Sdougm 	/*
11516088Sdougm 	 * Now make sure we really want to put this protocol on a
11526088Sdougm 	 * group. Only server protocols can go here.
11536088Sdougm 	 */
11546088Sdougm 	numprotos = sa_get_protocols(&protolist);
11556088Sdougm 	for (i = 0; i < numprotos; i++) {
11566088Sdougm 		features = sa_proto_get_featureset(protolist[i]);
11576088Sdougm 		if (features & SA_FEATURE_SERVER) {
11586088Sdougm 			optionset = sa_create_optionset(group, protolist[i]);
11596088Sdougm 			if (optionset == NULL) {
11606088Sdougm 				ret = SA_NO_MEMORY;
11616088Sdougm 				break;
11626088Sdougm 			}
11636088Sdougm 		}
11646088Sdougm 	}
11656088Sdougm 
11666088Sdougm 	if (protolist != NULL)
11676088Sdougm 		free(protolist);
11686088Sdougm 
11696088Sdougm 	return (ret);
11706088Sdougm }
11716088Sdougm 
11726088Sdougm /*
11733034Sdougm  * sa_create(flags, argc, argv)
11743034Sdougm  *	create a new group
11753034Sdougm  *	this may or may not have a protocol associated with it.
11763034Sdougm  *	No protocol means "all" protocols in this case.
11773034Sdougm  */
11783034Sdougm static int
sa_create(sa_handle_t handle,int flags,int argc,char * argv[])11793910Sdougm sa_create(sa_handle_t handle, int flags, int argc, char *argv[])
11803034Sdougm {
11813034Sdougm 	char *groupname;
11823034Sdougm 
11833034Sdougm 	sa_group_t group;
11846088Sdougm 	boolean_t force = B_FALSE;
11856088Sdougm 	boolean_t verbose = B_FALSE;
11866088Sdougm 	boolean_t dryrun = B_FALSE;
11873034Sdougm 	int c;
11883034Sdougm 	char *protocol = NULL;
11893034Sdougm 	int ret = SA_OK;
11903034Sdougm 	struct options *optlist = NULL;
11916019Sdougm 	int err = SA_OK;
11923034Sdougm 	int auth;
11936088Sdougm 	boolean_t created = B_FALSE;
11943034Sdougm 
11955331Samw 	while ((c = getopt(argc, argv, "?fhvnP:p:")) != EOF) {
11964653Sdougm 		switch (c) {
11975331Samw 		case 'f':
11986088Sdougm 			force = B_TRUE;
11995331Samw 			break;
12004653Sdougm 		case 'v':
12016088Sdougm 			verbose = B_TRUE;
12024653Sdougm 			break;
12034653Sdougm 		case 'n':
12046088Sdougm 			dryrun = B_TRUE;
12054653Sdougm 			break;
12064653Sdougm 		case 'P':
12075331Samw 			if (protocol != NULL) {
12085331Samw 				(void) printf(gettext("Specifying "
12095331Samw 				    "multiple protocols "
12105331Samw 				    "not supported: %s\n"), protocol);
12115331Samw 				return (SA_SYNTAX_ERR);
12125331Samw 			}
12134653Sdougm 			protocol = optarg;
12144653Sdougm 			if (sa_valid_protocol(protocol))
12154653Sdougm 				break;
12164653Sdougm 			(void) printf(gettext(
12174653Sdougm 			    "Invalid protocol specified: %s\n"), protocol);
12184653Sdougm 			return (SA_INVALID_PROTOCOL);
12194653Sdougm 			break;
12204653Sdougm 		case 'p':
12214653Sdougm 			ret = add_opt(&optlist, optarg, 0);
12224653Sdougm 			switch (ret) {
12234653Sdougm 			case OPT_ADD_SYNTAX:
12244653Sdougm 				(void) printf(gettext(
12254653Sdougm 				    "Property syntax error for property: %s\n"),
12264653Sdougm 				    optarg);
12274653Sdougm 				return (SA_SYNTAX_ERR);
12284653Sdougm 			case OPT_ADD_SECURITY:
12294653Sdougm 				(void) printf(gettext(
12304653Sdougm 				    "Security properties need "
12314653Sdougm 				    "to be set with set-security: %s\n"),
12324653Sdougm 				    optarg);
12334653Sdougm 				return (SA_SYNTAX_ERR);
12344653Sdougm 			default:
12354653Sdougm 				break;
12364653Sdougm 			}
12374653Sdougm 			break;
12386019Sdougm 		case 'h':
12396019Sdougm 			/* optopt on valid arg isn't defined */
12406019Sdougm 			optopt = c;
12416019Sdougm 			/*FALLTHROUGH*/
12426019Sdougm 		case '?':
12434653Sdougm 		default:
12446019Sdougm 			/*
12456019Sdougm 			 * Since a bad option gets to here, sort it
12466019Sdougm 			 * out and return a syntax error return value
12476019Sdougm 			 * if necessary.
12486019Sdougm 			 */
12496019Sdougm 			switch (optopt) {
12506019Sdougm 			default:
12516019Sdougm 				err = SA_SYNTAX_ERR;
12526019Sdougm 				break;
12536019Sdougm 			case 'h':
12546019Sdougm 			case '?':
12556019Sdougm 				break;
12566019Sdougm 			}
12574653Sdougm 			(void) printf(gettext("usage: %s\n"),
12584653Sdougm 			    sa_get_usage(USAGE_CREATE));
12596019Sdougm 			return (err);
12603034Sdougm 		}
12613034Sdougm 	}
12623034Sdougm 
12633034Sdougm 	if (optind >= argc) {
12644653Sdougm 		(void) printf(gettext("usage: %s\n"),
12654653Sdougm 		    sa_get_usage(USAGE_CREATE));
12664653Sdougm 		(void) printf(gettext("\tgroup must be specified.\n"));
12674653Sdougm 		return (SA_BAD_PATH);
12683034Sdougm 	}
12693034Sdougm 
12703034Sdougm 	if ((optind + 1) < argc) {
12714653Sdougm 		(void) printf(gettext("usage: %s\n"),
12724653Sdougm 		    sa_get_usage(USAGE_CREATE));
12734653Sdougm 		(void) printf(gettext("\textraneous group(s) at end\n"));
12744653Sdougm 		return (SA_SYNTAX_ERR);
12753034Sdougm 	}
12763034Sdougm 
12773034Sdougm 	if (protocol == NULL && optlist != NULL) {
12784653Sdougm 		/* lookup default protocol */
12794653Sdougm 		(void) printf(gettext("usage: %s\n"),
12804653Sdougm 		    sa_get_usage(USAGE_CREATE));
12814653Sdougm 		(void) printf(gettext("\tprotocol must be specified "
12824653Sdougm 		    "with properties\n"));
12834653Sdougm 		return (SA_INVALID_PROTOCOL);
12843034Sdougm 	}
12853034Sdougm 
12863034Sdougm 	if (optlist != NULL)
12874653Sdougm 		ret = chk_opt(optlist, 0, protocol);
12883034Sdougm 	if (ret == OPT_ADD_SECURITY) {
12894653Sdougm 		(void) printf(gettext("Security properties not "
12904653Sdougm 		    "supported with create\n"));
12914653Sdougm 		return (SA_SYNTAX_ERR);
12923034Sdougm 	}
12933034Sdougm 
12943034Sdougm 	/*
12954653Sdougm 	 * If a group already exists, we can only add a new protocol
12963034Sdougm 	 * to it and not create a new one or add the same protocol
12973034Sdougm 	 * again.
12983034Sdougm 	 */
12993034Sdougm 
13003034Sdougm 	groupname = argv[optind];
13013034Sdougm 
13023034Sdougm 	auth = check_authorizations(groupname, flags);
13033034Sdougm 
13043910Sdougm 	group = sa_get_group(handle, groupname);
13053034Sdougm 	if (group != NULL) {
13064653Sdougm 		/* group exists so must be a protocol add */
13076088Sdougm 		ret = check_valid_group(group, groupname, protocol);
13083034Sdougm 	} else {
13093034Sdougm 		/*
13103034Sdougm 		 * is it a valid name? Must comply with SMF instance
13113034Sdougm 		 * name restrictions.
13123034Sdougm 		 */
13134653Sdougm 		if (!sa_valid_group_name(groupname)) {
13144653Sdougm 			ret = SA_INVALID_NAME;
13154653Sdougm 			(void) printf(gettext("Invalid group name: %s\n"),
13164653Sdougm 			    groupname);
13174653Sdougm 		}
13183034Sdougm 	}
13193034Sdougm 	if (ret == SA_OK) {
13204653Sdougm 		/* check protocol vs optlist */
13214653Sdougm 		if (optlist != NULL) {
13224653Sdougm 			/* check options, if any, for validity */
13236214Sdougm 			ret = valid_options(handle, optlist, protocol,
13246214Sdougm 			    group, NULL);
13254653Sdougm 		}
13263034Sdougm 	}
13273034Sdougm 	if (ret == SA_OK && !dryrun) {
13284653Sdougm 		if (group == NULL) {
13294653Sdougm 			group = sa_create_group(handle, (char *)groupname,
13304653Sdougm 			    &err);
13316088Sdougm 			created = B_TRUE;
13323034Sdougm 		}
13334653Sdougm 		if (group != NULL) {
13344653Sdougm 			sa_optionset_t optionset;
13356088Sdougm 
13365331Samw 			/*
13376088Sdougm 			 * Check group and protocol against featureset
13386088Sdougm 			 * requirements.
13395331Samw 			 */
13406088Sdougm 			ret = enforce_featureset(group, protocol,
13416088Sdougm 			    dryrun, force);
13426088Sdougm 			if (ret != SA_OK)
13436088Sdougm 				goto err;
13446088Sdougm 
13456088Sdougm 			/*
13466088Sdougm 			 * So far so good. Now add the required
13476088Sdougm 			 * optionset(s) to the group.
13486088Sdougm 			 */
13494653Sdougm 			if (optlist != NULL) {
13504653Sdougm 				(void) add_optionset(group, optlist, protocol,
13514653Sdougm 				    &ret);
13524653Sdougm 			} else if (protocol != NULL) {
13534653Sdougm 				optionset = sa_create_optionset(group,
13544653Sdougm 				    protocol);
13554653Sdougm 				if (optionset == NULL)
13564653Sdougm 					ret = SA_NO_MEMORY;
13574653Sdougm 			} else if (protocol == NULL) {
13586088Sdougm 				/* default group create so add all protocols */
13596088Sdougm 				ret = set_all_protocols(group);
13604653Sdougm 			}
13613034Sdougm 			/*
13624653Sdougm 			 * We have a group and legal additions
13633034Sdougm 			 */
13644653Sdougm 			if (ret == SA_OK) {
13654653Sdougm 				/*
13664653Sdougm 				 * Commit to configuration for protocols that
13674653Sdougm 				 * need to do block updates. For NFS, this
13684653Sdougm 				 * doesn't do anything but it will be run for
13694653Sdougm 				 * all protocols that implement the
13704653Sdougm 				 * appropriate plugin.
13714653Sdougm 				 */
13724653Sdougm 				ret = sa_update_config(handle);
13734653Sdougm 			} else {
13744653Sdougm 				if (group != NULL)
13754653Sdougm 					(void) sa_remove_group(group);
13764653Sdougm 			}
13773034Sdougm 		} else {
13784653Sdougm 			ret = err;
13794653Sdougm 			(void) printf(gettext("Could not create group: %s\n"),
13804653Sdougm 			    sa_errorstr(ret));
13813034Sdougm 		}
13823034Sdougm 	}
13833034Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose) {
13844653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
13854653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
13864653Sdougm 		ret = SA_NO_PERMISSION;
13873034Sdougm 	}
13885331Samw err:
13896088Sdougm 	if (ret != SA_OK && created)
13906088Sdougm 		ret = sa_remove_group(group);
13916088Sdougm 
13923034Sdougm 	free_opt(optlist);
13933034Sdougm 	return (ret);
13943034Sdougm }
13953034Sdougm 
13963034Sdougm /*
13973034Sdougm  * group_status(group)
13983034Sdougm  *
13993034Sdougm  * return the current status (enabled/disabled) of the group.
14003034Sdougm  */
14013034Sdougm 
14023034Sdougm static char *
group_status(sa_group_t group)14033034Sdougm group_status(sa_group_t group)
14043034Sdougm {
14053034Sdougm 	char *state;
14063034Sdougm 	int enabled = 0;
14073034Sdougm 
14083034Sdougm 	state = sa_get_group_attr(group, "state");
14093034Sdougm 	if (state != NULL) {
14104653Sdougm 		if (strcmp(state, "enabled") == 0) {
14114653Sdougm 			enabled = 1;
14124653Sdougm 		}
14134653Sdougm 		sa_free_attr_string(state);
14143034Sdougm 	}
14154255Sdougm 	return (enabled ? "enabled" : "disabled");
14163034Sdougm }
14173034Sdougm 
14183034Sdougm /*
14193034Sdougm  * sa_delete(flags, argc, argv)
14203034Sdougm  *
14213034Sdougm  *	Delete a group.
14223034Sdougm  */
14233034Sdougm 
14243034Sdougm static int
sa_delete(sa_handle_t handle,int flags,int argc,char * argv[])14253910Sdougm sa_delete(sa_handle_t handle, int flags, int argc, char *argv[])
14263034Sdougm {
14273034Sdougm 	char *groupname;
14283034Sdougm 	sa_group_t group;
14293034Sdougm 	sa_share_t share;
14303034Sdougm 	int verbose = 0;
14313034Sdougm 	int dryrun = 0;
14323034Sdougm 	int force = 0;
14333034Sdougm 	int c;
14343034Sdougm 	char *protocol = NULL;
14353034Sdougm 	char *sectype = NULL;
14363034Sdougm 	int ret = SA_OK;
14373034Sdougm 	int auth;
14383034Sdougm 
14393034Sdougm 	while ((c = getopt(argc, argv, "?hvnP:fS:")) != EOF) {
14404653Sdougm 		switch (c) {
14414653Sdougm 		case 'v':
14424653Sdougm 			verbose++;
14434653Sdougm 			break;
14444653Sdougm 		case 'n':
14454653Sdougm 			dryrun++;
14464653Sdougm 			break;
14474653Sdougm 		case 'P':
14485331Samw 			if (protocol != NULL) {
14495331Samw 				(void) printf(gettext("Specifying "
14505331Samw 				    "multiple protocols "
14515331Samw 				    "not supported: %s\n"), protocol);
14525331Samw 				return (SA_SYNTAX_ERR);
14535331Samw 			}
14544653Sdougm 			protocol = optarg;
14554653Sdougm 			if (!sa_valid_protocol(protocol)) {
14564653Sdougm 				(void) printf(gettext("Invalid protocol "
14575331Samw 				    "specified: %s\n"), protocol);
14584653Sdougm 				return (SA_INVALID_PROTOCOL);
14594653Sdougm 			}
14604653Sdougm 			break;
14614653Sdougm 		case 'S':
14625331Samw 			if (sectype != NULL) {
14635331Samw 				(void) printf(gettext("Specifying "
14645331Samw 				    "multiple property "
14655331Samw 				    "spaces not supported: %s\n"), sectype);
14665331Samw 				return (SA_SYNTAX_ERR);
14675331Samw 			}
14684653Sdougm 			sectype = optarg;
14694653Sdougm 			break;
14704653Sdougm 		case 'f':
14714653Sdougm 			force++;
14724653Sdougm 			break;
14736019Sdougm 		case 'h':
14746019Sdougm 			/* optopt on valid arg isn't defined */
14756019Sdougm 			optopt = c;
14766019Sdougm 			/*FALLTHROUGH*/
14776019Sdougm 		case '?':
14784653Sdougm 		default:
14796019Sdougm 			/*
14806019Sdougm 			 * Since a bad option gets to here, sort it
14816019Sdougm 			 * out and return a syntax error return value
14826019Sdougm 			 * if necessary.
14836019Sdougm 			 */
14846019Sdougm 			switch (optopt) {
14856019Sdougm 			default:
14866019Sdougm 				ret = SA_SYNTAX_ERR;
14876019Sdougm 				break;
14886019Sdougm 			case 'h':
14896019Sdougm 			case '?':
14906019Sdougm 				break;
14916019Sdougm 			}
14924653Sdougm 			(void) printf(gettext("usage: %s\n"),
14934653Sdougm 			    sa_get_usage(USAGE_DELETE));
14946019Sdougm 			return (ret);
14953034Sdougm 		}
14963034Sdougm 	}
14973034Sdougm 
14983034Sdougm 	if (optind >= argc) {
14994653Sdougm 		(void) printf(gettext("usage: %s\n"),
15004653Sdougm 		    sa_get_usage(USAGE_DELETE));
15014653Sdougm 		(void) printf(gettext("\tgroup must be specified.\n"));
15024653Sdougm 		return (SA_SYNTAX_ERR);
15033034Sdougm 	}
15043034Sdougm 
15053034Sdougm 	if ((optind + 1) < argc) {
15064653Sdougm 		(void) printf(gettext("usage: %s\n"),
15074653Sdougm 		    sa_get_usage(USAGE_DELETE));
15084653Sdougm 		(void) printf(gettext("\textraneous group(s) at end\n"));
15094653Sdougm 		return (SA_SYNTAX_ERR);
15103034Sdougm 	}
15113034Sdougm 
15123034Sdougm 	if (sectype != NULL && protocol == NULL) {
15134653Sdougm 		(void) printf(gettext("usage: %s\n"),
15144653Sdougm 		    sa_get_usage(USAGE_DELETE));
15154653Sdougm 		(void) printf(gettext("\tsecurity requires protocol to be "
15164653Sdougm 		    "specified.\n"));
15174653Sdougm 		return (SA_SYNTAX_ERR);
15183034Sdougm 	}
15193034Sdougm 
15203034Sdougm 	/*
15213034Sdougm 	 * Determine if the group already exists since it must in
15223034Sdougm 	 * order to be removed.
15233034Sdougm 	 *
15243034Sdougm 	 * We can delete when:
15253034Sdougm 	 *
15263034Sdougm 	 *	- group is empty
15273034Sdougm 	 *	- force flag is set
15283034Sdougm 	 *	- if protocol specified, only delete the protocol
15293034Sdougm 	 */
15303034Sdougm 
15313034Sdougm 	groupname = argv[optind];
15323910Sdougm 	group = sa_get_group(handle, groupname);
15333034Sdougm 	if (group == NULL) {
15343034Sdougm 		ret = SA_NO_SUCH_GROUP;
15354653Sdougm 		goto done;
15364653Sdougm 	}
15374653Sdougm 	auth = check_authorizations(groupname, flags);
15384653Sdougm 	if (protocol == NULL) {
15393034Sdougm 		share = sa_get_share(group, NULL);
15403034Sdougm 		if (share != NULL)
15414653Sdougm 			ret = SA_BUSY;
15423034Sdougm 		if (share == NULL || (share != NULL && force == 1)) {
15434653Sdougm 			ret = SA_OK;
15444653Sdougm 			if (!dryrun) {
15454653Sdougm 				while (share != NULL) {
15464653Sdougm 					sa_share_t next_share;
15474653Sdougm 					next_share = sa_get_next_share(share);
15484653Sdougm 					/*
15494653Sdougm 					 * need to do the disable of
15504653Sdougm 					 * each share, but don't
15514653Sdougm 					 * actually do anything on a
15524653Sdougm 					 * dryrun.
15534653Sdougm 					 */
15544653Sdougm 					ret = sa_disable_share(share, NULL);
15554653Sdougm 					ret = sa_remove_share(share);
15564653Sdougm 					share = next_share;
15574653Sdougm 				}
15584653Sdougm 				ret = sa_remove_group(group);
15593034Sdougm 			}
15603034Sdougm 		}
15614653Sdougm 		/* Commit to configuration if not a dryrun */
15623034Sdougm 		if (!dryrun && ret == SA_OK) {
15634653Sdougm 			ret = sa_update_config(handle);
15643034Sdougm 		}
15654653Sdougm 	} else {
15663034Sdougm 		/* a protocol delete */
15673034Sdougm 		sa_optionset_t optionset;
15683034Sdougm 		sa_security_t security;
15695331Samw 		if (sectype != NULL) {
15704653Sdougm 			/* only delete specified security */
15714653Sdougm 			security = sa_get_security(group, sectype, protocol);
15724653Sdougm 			if (security != NULL && !dryrun)
15734653Sdougm 				ret = sa_destroy_security(security);
15744653Sdougm 			else
15754653Sdougm 				ret = SA_INVALID_PROTOCOL;
15763034Sdougm 		} else {
15774653Sdougm 			optionset = sa_get_optionset(group, protocol);
15784653Sdougm 			if (optionset != NULL && !dryrun) {
15794653Sdougm 				/*
15804653Sdougm 				 * have an optionset with
15814653Sdougm 				 * protocol to delete
15824653Sdougm 				 */
15834653Sdougm 				ret = sa_destroy_optionset(optionset);
15844653Sdougm 				/*
15854653Sdougm 				 * Now find all security sets
15864653Sdougm 				 * for the protocol and remove
15874653Sdougm 				 * them. Don't remove other
15884653Sdougm 				 * protocols.
15894653Sdougm 				 */
15904653Sdougm 				for (security =
15914653Sdougm 				    sa_get_security(group, NULL, NULL);
15924653Sdougm 				    ret == SA_OK && security != NULL;
15934653Sdougm 				    security = sa_get_next_security(security)) {
15944653Sdougm 					char *secprot;
15954653Sdougm 					secprot = sa_get_security_attr(security,
15964653Sdougm 					    "type");
15974653Sdougm 					if (secprot != NULL &&
15984653Sdougm 					    strcmp(secprot, protocol) == 0)
15994653Sdougm 						ret = sa_destroy_security(
16004653Sdougm 						    security);
16014653Sdougm 					if (secprot != NULL)
16024653Sdougm 						sa_free_attr_string(secprot);
16034653Sdougm 				}
16044653Sdougm 			} else {
16054653Sdougm 				if (!dryrun)
16064653Sdougm 					ret = SA_INVALID_PROTOCOL;
16073034Sdougm 			}
16083034Sdougm 		}
16095331Samw 		/*
16105331Samw 		 * With the protocol items removed, make sure that all
16115331Samw 		 * the shares are updated in the legacy files, if
16125331Samw 		 * necessary.
16135331Samw 		 */
16145331Samw 		for (share = sa_get_share(group, NULL);
16155331Samw 		    share != NULL;
16165331Samw 		    share = sa_get_next_share(share)) {
16175331Samw 			(void) sa_delete_legacy(share, protocol);
16185331Samw 		}
16193034Sdougm 	}
16204653Sdougm 
16214653Sdougm done:
16223034Sdougm 	if (ret != SA_OK) {
16234653Sdougm 		(void) printf(gettext("Could not delete group: %s\n"),
16244653Sdougm 		    sa_errorstr(ret));
16253034Sdougm 	} else if (dryrun && !auth && verbose) {
16264653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
16274653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
16283034Sdougm 	}
16293034Sdougm 	return (ret);
16303034Sdougm }
16313034Sdougm 
16323034Sdougm /*
16333034Sdougm  * strndupr(*buff, str, buffsize)
16343034Sdougm  *
16353034Sdougm  * used with small strings to duplicate and possibly increase the
16363034Sdougm  * buffer size of a string.
16373034Sdougm  */
16383034Sdougm static char *
strndupr(char * buff,char * str,int * buffsize)16393034Sdougm strndupr(char *buff, char *str, int *buffsize)
16403034Sdougm {
16413034Sdougm 	int limit;
16423034Sdougm 	char *orig_buff = buff;
16433034Sdougm 
16443034Sdougm 	if (buff == NULL) {
16454653Sdougm 		buff = (char *)malloc(64);
16464653Sdougm 		if (buff == NULL)
16474653Sdougm 			return (NULL);
16484653Sdougm 		*buffsize = 64;
16494653Sdougm 		buff[0] = '\0';
16503034Sdougm 	}
16513034Sdougm 	limit = strlen(buff) + strlen(str) + 1;
16523034Sdougm 	if (limit > *buffsize) {
16534653Sdougm 		limit = *buffsize = *buffsize + ((limit / 64) + 64);
16544653Sdougm 		buff = realloc(buff, limit);
16553034Sdougm 	}
16563034Sdougm 	if (buff != NULL) {
16574653Sdougm 		(void) strcat(buff, str);
16583034Sdougm 	} else {
16594653Sdougm 		/* if it fails, fail it hard */
16604653Sdougm 		if (orig_buff != NULL)
16614653Sdougm 			free(orig_buff);
16623034Sdougm 	}
16633034Sdougm 	return (buff);
16643034Sdougm }
16653034Sdougm 
16663034Sdougm /*
16673034Sdougm  * group_proto(group)
16683034Sdougm  *
16693034Sdougm  * return a string of all the protocols (space separated) associated
16703034Sdougm  * with this group.
16713034Sdougm  */
16723034Sdougm 
16733034Sdougm static char *
group_proto(sa_group_t group)16743034Sdougm group_proto(sa_group_t group)
16753034Sdougm {
16763034Sdougm 	sa_optionset_t optionset;
16773034Sdougm 	char *proto;
16783034Sdougm 	char *buff = NULL;
16793034Sdougm 	int buffsize = 0;
16803034Sdougm 	int addspace = 0;
16813034Sdougm 	/*
16823034Sdougm 	 * get the protocol list by finding the optionsets on this
16833034Sdougm 	 * group and extracting the type value. The initial call to
16843034Sdougm 	 * strndupr() initailizes buff.
16853034Sdougm 	 */
16863034Sdougm 	buff = strndupr(buff, "", &buffsize);
16873034Sdougm 	if (buff != NULL) {
16884653Sdougm 		for (optionset = sa_get_optionset(group, NULL);
16894653Sdougm 		    optionset != NULL && buff != NULL;
16904653Sdougm 		    optionset = sa_get_next_optionset(optionset)) {
16914653Sdougm 			/*
16924653Sdougm 			 * extract out the protocol type from this optionset
16934653Sdougm 			 * and append it to the buffer "buff". strndupr() will
16944653Sdougm 			 * reallocate space as necessay.
16954653Sdougm 			 */
16964653Sdougm 			proto = sa_get_optionset_attr(optionset, "type");
16974653Sdougm 			if (proto != NULL) {
16984653Sdougm 				if (addspace++)
16994653Sdougm 					buff = strndupr(buff, " ", &buffsize);
17004653Sdougm 				buff = strndupr(buff, proto, &buffsize);
17014653Sdougm 				sa_free_attr_string(proto);
17024653Sdougm 			}
17033034Sdougm 		}
17043034Sdougm 	}
17053034Sdougm 	return (buff);
17063034Sdougm }
17073034Sdougm 
17083034Sdougm /*
17093034Sdougm  * sa_list(flags, argc, argv)
17103034Sdougm  *
17113034Sdougm  * implements the "list" subcommand to list groups and optionally
17123034Sdougm  * their state and protocols.
17133034Sdougm  */
17143034Sdougm 
17153034Sdougm static int
sa_list(sa_handle_t handle,int flags,int argc,char * argv[])17163910Sdougm sa_list(sa_handle_t handle, int flags, int argc, char *argv[])
17173034Sdougm {
17183034Sdougm 	sa_group_t group;
17193034Sdougm 	int verbose = 0;
17203034Sdougm 	int c;
17213034Sdougm 	char *protocol = NULL;
17226019Sdougm 	int ret = SA_OK;
17235331Samw #ifdef lint
17245331Samw 	flags = flags;
17255331Samw #endif
17263034Sdougm 
17273034Sdougm 	while ((c = getopt(argc, argv, "?hvP:")) != EOF) {
17284653Sdougm 		switch (c) {
17294653Sdougm 		case 'v':
17304653Sdougm 			verbose++;
17314653Sdougm 			break;
17324653Sdougm 		case 'P':
17335331Samw 			if (protocol != NULL) {
17345331Samw 				(void) printf(gettext(
17355331Samw 				    "Specifying multiple protocols "
17365331Samw 				    "not supported: %s\n"),
17375331Samw 				    protocol);
17385331Samw 				return (SA_SYNTAX_ERR);
17395331Samw 			}
17404653Sdougm 			protocol = optarg;
17414653Sdougm 			if (!sa_valid_protocol(protocol)) {
17424653Sdougm 				(void) printf(gettext(
17434653Sdougm 				    "Invalid protocol specified: %s\n"),
17444653Sdougm 				    protocol);
17454653Sdougm 				return (SA_INVALID_PROTOCOL);
17464653Sdougm 			}
17474653Sdougm 			break;
17486019Sdougm 		case 'h':
17496019Sdougm 			/* optopt on valid arg isn't defined */
17506019Sdougm 			optopt = c;
17516019Sdougm 			/*FALLTHROUGH*/
17526019Sdougm 		case '?':
17534653Sdougm 		default:
17546019Sdougm 			/*
17556019Sdougm 			 * Since a bad option gets to here, sort it
17566019Sdougm 			 * out and return a syntax error return value
17576019Sdougm 			 * if necessary.
17586019Sdougm 			 */
17596019Sdougm 			switch (optopt) {
17606019Sdougm 			default:
17616019Sdougm 				ret = SA_SYNTAX_ERR;
17626019Sdougm 				break;
17636019Sdougm 			case 'h':
17646019Sdougm 			case '?':
17656019Sdougm 				break;
17666019Sdougm 			}
17674653Sdougm 			(void) printf(gettext("usage: %s\n"),
17684653Sdougm 			    sa_get_usage(USAGE_LIST));
17696019Sdougm 				return (ret);
17703034Sdougm 		}
17713034Sdougm 	}
17723034Sdougm 
17735885Sdougm 	if (optind != argc) {
17745885Sdougm 		(void) printf(gettext("usage: %s\n"),
17755885Sdougm 		    sa_get_usage(USAGE_LIST));
17765885Sdougm 		return (SA_SYNTAX_ERR);
17775885Sdougm 	}
17785885Sdougm 
17794653Sdougm 	for (group = sa_get_group(handle, NULL);
17804653Sdougm 	    group != NULL;
17813034Sdougm 	    group = sa_get_next_group(group)) {
17824653Sdougm 		char *name;
17834653Sdougm 		char *proto;
17844653Sdougm 		if (protocol == NULL || has_protocol(group, protocol)) {
17854653Sdougm 			name = sa_get_group_attr(group, "name");
17864653Sdougm 			if (name != NULL && (verbose > 1 || name[0] != '#')) {
17874653Sdougm 				(void) printf("%s", (char *)name);
17884653Sdougm 				if (verbose) {
17894653Sdougm 					/*
17904653Sdougm 					 * Need the list of protocols
17914653Sdougm 					 * and current status once
17924653Sdougm 					 * available. We do want to
17934653Sdougm 					 * translate the
17944653Sdougm 					 * enabled/disabled text here.
17954653Sdougm 					 */
17964653Sdougm 					(void) printf("\t%s", isenabled(group) ?
17974653Sdougm 					    gettext("enabled") :
17984653Sdougm 					    gettext("disabled"));
17994653Sdougm 					proto = group_proto(group);
18004653Sdougm 					if (proto != NULL) {
18014653Sdougm 						(void) printf("\t%s",
18024653Sdougm 						    (char *)proto);
18034653Sdougm 						free(proto);
18044653Sdougm 					}
18054653Sdougm 				}
18064653Sdougm 				(void) printf("\n");
18073034Sdougm 			}
18084653Sdougm 			if (name != NULL)
18094653Sdougm 				sa_free_attr_string(name);
18103034Sdougm 		}
18113034Sdougm 	}
18123034Sdougm 	return (0);
18133034Sdougm }
18143034Sdougm 
18153034Sdougm /*
18163034Sdougm  * out_properties(optionset, proto, sec)
18173034Sdougm  *
18183034Sdougm  * Format the properties and encode the protocol and optional named
18193034Sdougm  * optionset into the string.
18203034Sdougm  *
18213034Sdougm  * format is protocol[:name]=(property-list)
18223034Sdougm  */
18233034Sdougm 
18243034Sdougm static void
out_properties(sa_optionset_t optionset,char * proto,char * sec)18253034Sdougm out_properties(sa_optionset_t optionset, char *proto, char *sec)
18263034Sdougm {
18273034Sdougm 	char *type;
18283034Sdougm 	char *value;
18293034Sdougm 	int spacer;
18303034Sdougm 	sa_property_t prop;
18313034Sdougm 
18324653Sdougm 	if (sec == NULL)
18334653Sdougm 		(void) printf(" %s=(", proto ? proto : gettext("all"));
18344653Sdougm 	else
18354653Sdougm 		(void) printf(" %s:%s=(", proto ? proto : gettext("all"), sec);
18363034Sdougm 
18373034Sdougm 	for (spacer = 0, prop = sa_get_property(optionset, NULL);
18384653Sdougm 	    prop != NULL;
18394653Sdougm 	    prop = sa_get_next_property(prop)) {
18403034Sdougm 
18413034Sdougm 		/*
18423034Sdougm 		 * extract the property name/value and output with
18433034Sdougm 		 * appropriate spacing. I.e. no prefixed space the
18443034Sdougm 		 * first time through but a space on subsequent
18453034Sdougm 		 * properties.
18463034Sdougm 		 */
18474653Sdougm 		type = sa_get_property_attr(prop, "type");
18484653Sdougm 		value = sa_get_property_attr(prop, "value");
18494653Sdougm 		if (type != NULL) {
18504653Sdougm 			(void) printf("%s%s=", spacer ? " " : "",	type);
18514653Sdougm 			spacer = 1;
18524653Sdougm 			if (value != NULL)
18534653Sdougm 				(void) printf("\"%s\"", value);
18544653Sdougm 			else
18554653Sdougm 				(void) printf("\"\"");
18564653Sdougm 		}
18574653Sdougm 		if (type != NULL)
18584653Sdougm 			sa_free_attr_string(type);
18593034Sdougm 		if (value != NULL)
18604653Sdougm 			sa_free_attr_string(value);
18613034Sdougm 	}
18623034Sdougm 	(void) printf(")");
18633034Sdougm }
18643034Sdougm 
18653034Sdougm /*
18663034Sdougm  * show_properties(group, protocol, prefix)
18673034Sdougm  *
18683034Sdougm  * print the properties for a group. If protocol is NULL, do all
18693034Sdougm  * protocols otherwise only the specified protocol. All security
18703034Sdougm  * (named groups specific to the protocol) are included.
18713034Sdougm  *
18723034Sdougm  * The "prefix" is always applied. The caller knows whether it wants
18733034Sdougm  * some type of prefix string (white space) or not.  Once the prefix
18743034Sdougm  * has been output, it is reduced to the zero length string for the
18753034Sdougm  * remainder of the property output.
18763034Sdougm  */
18773034Sdougm 
18783034Sdougm static void
show_properties(sa_group_t group,char * protocol,char * prefix)18793034Sdougm show_properties(sa_group_t group, char *protocol, char *prefix)
18803034Sdougm {
18813034Sdougm 	sa_optionset_t optionset;
18823034Sdougm 	sa_security_t security;
18833034Sdougm 	char *value;
18843034Sdougm 	char *secvalue;
18853034Sdougm 
18863034Sdougm 	if (protocol != NULL) {
18874653Sdougm 		optionset = sa_get_optionset(group, protocol);
18884653Sdougm 		if (optionset != NULL) {
18894653Sdougm 			(void) printf("%s", prefix);
18904653Sdougm 			prefix = "";
18914653Sdougm 			out_properties(optionset, protocol, NULL);
18924653Sdougm 		}
18934653Sdougm 		security = sa_get_security(group, protocol, NULL);
18944653Sdougm 		if (security != NULL) {
18954653Sdougm 			(void) printf("%s", prefix);
18964653Sdougm 			prefix = "";
18974653Sdougm 			out_properties(security, protocol, NULL);
18984653Sdougm 		}
18993034Sdougm 	} else {
19004653Sdougm 		for (optionset = sa_get_optionset(group, protocol);
19014653Sdougm 		    optionset != NULL;
19024653Sdougm 		    optionset = sa_get_next_optionset(optionset)) {
19034653Sdougm 
19044653Sdougm 			value = sa_get_optionset_attr(optionset, "type");
19054653Sdougm 			(void) printf("%s", prefix);
19064653Sdougm 			prefix = "";
19074653Sdougm 			out_properties(optionset, value, 0);
19084653Sdougm 			if (value != NULL)
19094653Sdougm 				sa_free_attr_string(value);
19104653Sdougm 		}
19114653Sdougm 		for (security = sa_get_security(group, NULL, protocol);
19124653Sdougm 		    security != NULL;
19134653Sdougm 		    security = sa_get_next_security(security)) {
19144653Sdougm 
19154653Sdougm 			value = sa_get_security_attr(security, "type");
19164653Sdougm 			secvalue = sa_get_security_attr(security, "sectype");
19174653Sdougm 			(void) printf("%s", prefix);
19184653Sdougm 			prefix = "";
19194653Sdougm 			out_properties(security, value, secvalue);
19204653Sdougm 			if (value != NULL)
19214653Sdougm 				sa_free_attr_string(value);
19224653Sdougm 			if (secvalue != NULL)
19234653Sdougm 				sa_free_attr_string(secvalue);
19244653Sdougm 		}
19253034Sdougm 	}
19263034Sdougm }
19273034Sdougm 
19283034Sdougm /*
19295331Samw  * get_resource(share)
19305331Samw  *
19315331Samw  * Get the first resource name, if any, and fix string to be in
19325331Samw  * current locale and have quotes if it has embedded spaces.  Return
19335331Samw  * an attr string that must be freed.
19345331Samw  */
19355331Samw 
19365331Samw static char *
get_resource(sa_share_t share)19375331Samw get_resource(sa_share_t share)
19385331Samw {
19395331Samw 	sa_resource_t resource;
19405331Samw 	char *resstring = NULL;
19415331Samw 	char *retstring;
19425331Samw 
19435331Samw 	if ((resource = sa_get_share_resource(share, NULL)) != NULL) {
19445331Samw 		resstring = sa_get_resource_attr(resource, "name");
19455331Samw 		if (resstring != NULL) {
19465331Samw 			char *cp;
19475331Samw 			int len;
19485331Samw 
19495331Samw 			retstring = conv_from_utf8(resstring);
19505331Samw 			if (retstring != resstring) {
19515331Samw 				sa_free_attr_string(resstring);
19525331Samw 				resstring = retstring;
19535331Samw 			}
19545331Samw 			if (strpbrk(resstring, " ") != NULL) {
19555331Samw 				/* account for quotes */
19565331Samw 				len = strlen(resstring) + 3;
19575331Samw 				cp = calloc(len, sizeof (char));
19585331Samw 				if (cp != NULL) {
19595331Samw 					(void) snprintf(cp, len,
19605331Samw 					    "\"%s\"", resstring);
19615331Samw 					sa_free_attr_string(resstring);
19625331Samw 					resstring = cp;
19635331Samw 				} else {
19645331Samw 					sa_free_attr_string(resstring);
19655331Samw 					resstring = NULL;
19665331Samw 				}
19675331Samw 			}
19685331Samw 		}
19695331Samw 	}
19705331Samw 	return (resstring);
19715331Samw }
19725331Samw 
19735331Samw /*
19745331Samw  * has_resource_with_opt(share)
19755331Samw  *
19765331Samw  * Check to see if the share has any resource names with optionsets
19775331Samw  * set. Also indicate if multiple resource names since the syntax
19785331Samw  * would be about the same.
19795331Samw  */
19805331Samw static int
has_resource_with_opt(sa_share_t share)19815331Samw has_resource_with_opt(sa_share_t share)
19825331Samw {
19835331Samw 	sa_resource_t resource;
19845331Samw 	int ret = B_FALSE;
19855331Samw 
19865331Samw 	for (resource = sa_get_share_resource(share, NULL);
19875331Samw 	    resource != NULL;
19885331Samw 	    resource = sa_get_next_resource(resource)) {
19895331Samw 
19905331Samw 		if (sa_get_optionset(resource, NULL) != NULL) {
19915331Samw 			ret = B_TRUE;
19925331Samw 			break;
19935331Samw 		}
19945331Samw 	}
19955331Samw 	return (ret);
19965331Samw }
19975331Samw 
19985331Samw /*
19995331Samw  * has_multiple_resource(share)
20005331Samw  *
20015885Sdougm  * Check to see if the share has multiple resource names since
20025885Sdougm  * the syntax would be about the same.
20035331Samw  */
20045885Sdougm static boolean_t
has_multiple_resource(sa_share_t share)20055331Samw has_multiple_resource(sa_share_t share)
20065331Samw {
20075331Samw 	sa_resource_t resource;
20085331Samw 	int num;
20095331Samw 
20105331Samw 	for (num = 0, resource = sa_get_share_resource(share, NULL);
20115331Samw 	    resource != NULL;
20125331Samw 	    resource = sa_get_next_resource(resource)) {
20135331Samw 		num++;
20145331Samw 		if (num > 1)
20155331Samw 			return (B_TRUE);
20165331Samw 	}
20175331Samw 	return (B_FALSE);
20185331Samw }
20195331Samw 
20205331Samw /*
20215331Samw  * show_share(share, verbose, properties, proto, iszfs, sharepath)
20225331Samw  *
20235331Samw  * print out the share information. With the addition of resource as a
20245331Samw  * full object that can have multiple instances below the share, we
20255331Samw  * need to display that as well.
20265331Samw  */
20275331Samw 
20285331Samw static void
show_share(sa_share_t share,int verbose,int properties,char * proto,int iszfs,char * sharepath)20295331Samw show_share(sa_share_t share, int verbose, int properties, char *proto,
20305331Samw     int iszfs, char *sharepath)
20315331Samw {
20325331Samw 	char *drive;
20335331Samw 	char *exclude;
20345331Samw 	sa_resource_t resource = NULL;
20355331Samw 	char *description;
20365331Samw 	char *rsrcname;
20375331Samw 	int rsrcwithopt;
20385885Sdougm 	boolean_t multiple;
20395331Samw 	char *type;
20405331Samw 
20415331Samw 	rsrcwithopt = has_resource_with_opt(share);
20425331Samw 
20435331Samw 	if (verbose || (properties && rsrcwithopt)) {
20445331Samw 		/* First, indicate if transient */
20455331Samw 		type = sa_get_share_attr(share, "type");
20465331Samw 		if (type != NULL && !iszfs && verbose &&
20475331Samw 		    strcmp(type, "transient") == 0)
20485331Samw 			(void) printf("\t* ");
20495331Samw 		else
20505331Samw 			(void) printf("\t  ");
20515331Samw 
20525331Samw 		if (type != NULL)
20535331Samw 			sa_free_attr_string(type);
20545331Samw 
20555331Samw 		/*
20565331Samw 		 * If we came in with verbose, we want to handle the case of
20575331Samw 		 * multiple resources as though they had properties set.
20585331Samw 		 */
20595331Samw 		multiple = has_multiple_resource(share);
20605331Samw 
20615885Sdougm 		/*
20625885Sdougm 		 * if there is a description on the share and there
20635885Sdougm 		 * are resources, treat as multiple resources in order
20645885Sdougm 		 * to get all descriptions displayed.
20655885Sdougm 		 */
20665885Sdougm 		description = sa_get_share_description(share);
20675885Sdougm 		resource = sa_get_share_resource(share, NULL);
20685885Sdougm 
20695885Sdougm 		if (description != NULL && resource != NULL)
20705885Sdougm 			multiple = B_TRUE;
20715885Sdougm 
20725331Samw 		/* Next, if not multiple follow old model */
20735331Samw 		if (!multiple && !rsrcwithopt) {
20745331Samw 			rsrcname = get_resource(share);
20755331Samw 			if (rsrcname != NULL && strlen(rsrcname) > 0) {
20765331Samw 				(void) printf("%s=%s", rsrcname, sharepath);
20775331Samw 			} else {
20785331Samw 				(void) printf("%s", sharepath);
20795331Samw 			}
20805331Samw 			if (rsrcname != NULL)
20815331Samw 				sa_free_attr_string(rsrcname);
20825885Sdougm 			/* Print the description string if there is one. */
20835885Sdougm 			print_rsrc_desc(resource, description);
20845331Samw 		} else {
20855331Samw 			/* Treat as simple and then resources come later */
20865331Samw 			(void) printf("%s", sharepath);
20875331Samw 		}
20885331Samw 		drive = sa_get_share_attr(share, "drive-letter");
20895331Samw 		if (drive != NULL) {
20905331Samw 			if (strlen(drive) > 0)
20915331Samw 				(void) printf(gettext("\tdrive-letter=\"%s:\""),
20925331Samw 				    drive);
20935331Samw 			sa_free_attr_string(drive);
20945331Samw 		}
20955331Samw 		if (properties)
20965331Samw 			show_properties(share, proto, "\t");
20975331Samw 		exclude = sa_get_share_attr(share, "exclude");
20985331Samw 		if (exclude != NULL) {
20995331Samw 			(void) printf(gettext("\tnot-shared-with=[%s]"),
21005331Samw 			    exclude);
21015331Samw 			sa_free_attr_string(exclude);
21025331Samw 		}
21035885Sdougm 
21045331Samw 		if (description != NULL) {
21055885Sdougm 			print_rsrc_desc((sa_resource_t)share, description);
21065331Samw 		}
21075331Samw 		/*
21085331Samw 		 * If there are resource names with options, show them
21095331Samw 		 * here, with one line per resource. Resource specific
21105331Samw 		 * options are at the end of the line followed by
21115331Samw 		 * description, if any.
21125331Samw 		 */
21135331Samw 		if (rsrcwithopt || multiple) {
21145331Samw 			for (resource = sa_get_share_resource(share, NULL);
21155331Samw 			    resource != NULL;
21165331Samw 			    resource = sa_get_next_resource(resource)) {
21175331Samw 				int has_space;
21185331Samw 				char *rsrc;
21195331Samw 
21205331Samw 				(void) printf("\n\t\t  ");
21215331Samw 				rsrcname = sa_get_resource_attr(resource,
21225331Samw 				    "name");
21235331Samw 				if (rsrcname == NULL)
21245331Samw 					continue;
21255331Samw 
21265331Samw 				rsrc = conv_from_utf8(rsrcname);
21275331Samw 				has_space = strpbrk(rsrc, " ") != NULL;
21285331Samw 
21295331Samw 				if (has_space)
21305331Samw 					(void) printf("\"%s\"=%s", rsrc,
21315331Samw 					    sharepath);
21325331Samw 				else
21335331Samw 					(void) printf("%s=%s", rsrc,
21345331Samw 					    sharepath);
21355331Samw 				if (rsrc != rsrcname)
21365331Samw 					sa_free_attr_string(rsrc);
21375331Samw 				sa_free_attr_string(rsrcname);
21385331Samw 				if (properties || rsrcwithopt)
21395331Samw 					show_properties(resource, proto, "\t");
21405331Samw 
21415331Samw 				/* Get description string if any */
21425885Sdougm 				print_rsrc_desc(resource, description);
21435331Samw 			}
21445331Samw 		}
21455885Sdougm 		if (description != NULL)
21465885Sdougm 			sa_free_share_description(description);
21475331Samw 	} else {
21485331Samw 		(void) printf("\t  %s", sharepath);
21495331Samw 		if (properties)
21505331Samw 			show_properties(share, proto, "\t");
21515331Samw 	}
21525331Samw 	(void) printf("\n");
21535331Samw }
21545331Samw 
21555331Samw /*
21563034Sdougm  * show_group(group, verbose, properties, proto, subgroup)
21573034Sdougm  *
21583034Sdougm  * helper function to show the contents of a group.
21593034Sdougm  */
21603034Sdougm 
21613034Sdougm static void
show_group(sa_group_t group,int verbose,int properties,char * proto,char * subgroup)21623034Sdougm show_group(sa_group_t group, int verbose, int properties, char *proto,
21635331Samw     char *subgroup)
21643034Sdougm {
21653034Sdougm 	sa_share_t share;
21663034Sdougm 	char *groupname;
21673034Sdougm 	char *zfs = NULL;
21683034Sdougm 	int iszfs = 0;
21695331Samw 	char *sharepath;
21703034Sdougm 
21713034Sdougm 	groupname = sa_get_group_attr(group, "name");
21723034Sdougm 	if (groupname != NULL) {
21734653Sdougm 		if (proto != NULL && !has_protocol(group, proto)) {
21744653Sdougm 			sa_free_attr_string(groupname);
21754653Sdougm 			return;
21764653Sdougm 		}
21773034Sdougm 		/*
21783034Sdougm 		 * check to see if the group is managed by ZFS. If
21793034Sdougm 		 * there is an attribute, then it is. A non-NULL zfs
21803034Sdougm 		 * variable will trigger the different way to display
21813034Sdougm 		 * and will remove the transient property indicator
21823034Sdougm 		 * from the output.
21833034Sdougm 		 */
21844653Sdougm 		zfs = sa_get_group_attr(group, "zfs");
21854653Sdougm 		if (zfs != NULL) {
21864653Sdougm 			iszfs = 1;
21874653Sdougm 			sa_free_attr_string(zfs);
21883034Sdougm 		}
21894653Sdougm 		share = sa_get_share(group, NULL);
21904653Sdougm 		if (subgroup == NULL)
21914653Sdougm 			(void) printf("%s", groupname);
21924653Sdougm 		else
21934653Sdougm 			(void) printf("    %s/%s", subgroup, groupname);
21944653Sdougm 		if (properties)
21954653Sdougm 			show_properties(group, proto, "");
21964653Sdougm 		(void) printf("\n");
21974653Sdougm 		if (strcmp(groupname, "zfs") == 0) {
21984653Sdougm 			sa_group_t zgroup;
21994653Sdougm 
22004653Sdougm 			for (zgroup = sa_get_sub_group(group);
22014653Sdougm 			    zgroup != NULL;
22024653Sdougm 			    zgroup = sa_get_next_group(zgroup)) {
22034653Sdougm 				show_group(zgroup, verbose, properties, proto,
22044653Sdougm 				    "zfs");
22054653Sdougm 			}
22064653Sdougm 			sa_free_attr_string(groupname);
22074653Sdougm 			return;
22084653Sdougm 		}
22093034Sdougm 		/*
22104653Sdougm 		 * Have a group, so list the contents. Resource and
22113034Sdougm 		 * description are only listed if verbose is set.
22123034Sdougm 		 */
22134653Sdougm 		for (share = sa_get_share(group, NULL);
22144653Sdougm 		    share != NULL;
22154653Sdougm 		    share = sa_get_next_share(share)) {
22164653Sdougm 			sharepath = sa_get_share_attr(share, "path");
22174653Sdougm 			if (sharepath != NULL) {
22185331Samw 				show_share(share, verbose, properties, proto,
22195331Samw 				    iszfs, sharepath);
22204653Sdougm 				sa_free_attr_string(sharepath);
22213034Sdougm 			}
22223034Sdougm 		}
22233034Sdougm 	}
22243034Sdougm 	if (groupname != NULL) {
22253034Sdougm 		sa_free_attr_string(groupname);
22263034Sdougm 	}
22273034Sdougm }
22283034Sdougm 
22293034Sdougm /*
22303034Sdougm  * show_group_xml_init()
22313034Sdougm  *
22323034Sdougm  * Create an XML document that will be used to display config info via
22333034Sdougm  * XML format.
22343034Sdougm  */
22353034Sdougm 
22363034Sdougm xmlDocPtr
show_group_xml_init()22373034Sdougm show_group_xml_init()
22383034Sdougm {
22393034Sdougm 	xmlDocPtr doc;
22403034Sdougm 	xmlNodePtr root;
22413034Sdougm 
22423034Sdougm 	doc = xmlNewDoc((xmlChar *)"1.0");
22433034Sdougm 	if (doc != NULL) {
22444653Sdougm 		root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
22454653Sdougm 		if (root != NULL)
2246*11415SSurya.Prakki@Sun.COM 			(void) xmlDocSetRootElement(doc, root);
22473034Sdougm 	}
22483034Sdougm 	return (doc);
22493034Sdougm }
22503034Sdougm 
22513034Sdougm /*
22523034Sdougm  * show_group_xml(doc, group)
22533034Sdougm  *
22543034Sdougm  * Copy the group info into the XML doc.
22553034Sdougm  */
22563034Sdougm 
22573034Sdougm static void
show_group_xml(xmlDocPtr doc,sa_group_t group)22583034Sdougm show_group_xml(xmlDocPtr doc, sa_group_t group)
22593034Sdougm {
22603034Sdougm 	xmlNodePtr node;
22613034Sdougm 	xmlNodePtr root;
22623034Sdougm 
22633034Sdougm 	root = xmlDocGetRootElement(doc);
22643034Sdougm 	node = xmlCopyNode((xmlNodePtr)group, 1);
22653034Sdougm 	if (node != NULL && root != NULL) {
2266*11415SSurya.Prakki@Sun.COM 		(void) xmlAddChild(root, node);
22673034Sdougm 		/*
22683034Sdougm 		 * In the future, we may have interally used tags that
22693034Sdougm 		 * should not appear in the XML output. Remove
22703034Sdougm 		 * anything we don't want to show here.
22713034Sdougm 		 */
22723034Sdougm 	}
22733034Sdougm }
22743034Sdougm 
22753034Sdougm /*
22763034Sdougm  * sa_show(flags, argc, argv)
22773034Sdougm  *
22783034Sdougm  * Implements the show subcommand.
22793034Sdougm  */
22803034Sdougm 
22813034Sdougm int
sa_show(sa_handle_t handle,int flags,int argc,char * argv[])22823910Sdougm sa_show(sa_handle_t handle, int flags, int argc, char *argv[])
22833034Sdougm {
22843034Sdougm 	sa_group_t group;
22853034Sdougm 	int verbose = 0;
22863034Sdougm 	int properties = 0;
22873034Sdougm 	int c;
22883034Sdougm 	int ret = SA_OK;
22893034Sdougm 	char *protocol = NULL;
22903034Sdougm 	int xml = 0;
22913034Sdougm 	xmlDocPtr doc;
22925331Samw #ifdef lint
22935331Samw 	flags = flags;
22945331Samw #endif
22953034Sdougm 
22963034Sdougm 	while ((c = getopt(argc, argv, "?hvP:px")) !=	EOF) {
22974653Sdougm 		switch (c) {
22984653Sdougm 		case 'v':
22994653Sdougm 			verbose++;
23004653Sdougm 			break;
23014653Sdougm 		case 'p':
23024653Sdougm 			properties++;
23034653Sdougm 			break;
23044653Sdougm 		case 'P':
23055331Samw 			if (protocol != NULL) {
23065331Samw 				(void) printf(gettext(
23075331Samw 				    "Specifying multiple protocols "
23085331Samw 				    "not supported: %s\n"),
23095331Samw 				    protocol);
23105331Samw 				return (SA_SYNTAX_ERR);
23115331Samw 			}
23124653Sdougm 			protocol = optarg;
23134653Sdougm 			if (!sa_valid_protocol(protocol)) {
23144653Sdougm 				(void) printf(gettext(
23154653Sdougm 				    "Invalid protocol specified: %s\n"),
23164653Sdougm 				    protocol);
23174653Sdougm 				return (SA_INVALID_PROTOCOL);
23184653Sdougm 			}
23194653Sdougm 			break;
23204653Sdougm 		case 'x':
23214653Sdougm 			xml++;
23224653Sdougm 			break;
23236019Sdougm 		case 'h':
23246019Sdougm 			/* optopt on valid arg isn't defined */
23256019Sdougm 			optopt = c;
23266019Sdougm 			/*FALLTHROUGH*/
23276019Sdougm 		case '?':
23284653Sdougm 		default:
23296019Sdougm 			/*
23306019Sdougm 			 * Since a bad option gets to here, sort it
23316019Sdougm 			 * out and return a syntax error return value
23326019Sdougm 			 * if necessary.
23336019Sdougm 			 */
23346019Sdougm 			switch (optopt) {
23356019Sdougm 			default:
23366019Sdougm 				ret = SA_SYNTAX_ERR;
23376019Sdougm 				break;
23386019Sdougm 			case 'h':
23396019Sdougm 			case '?':
23406019Sdougm 				break;
23416019Sdougm 			}
23424653Sdougm 			(void) printf(gettext("usage: %s\n"),
23434653Sdougm 			    sa_get_usage(USAGE_SHOW));
23446019Sdougm 			return (ret);
23453034Sdougm 		}
23463034Sdougm 	}
23473034Sdougm 
23483034Sdougm 	if (xml) {
23494653Sdougm 		doc = show_group_xml_init();
23504653Sdougm 		if (doc == NULL)
23514653Sdougm 			ret = SA_NO_MEMORY;
23523034Sdougm 	}
23533034Sdougm 
23543034Sdougm 	if (optind == argc) {
23554653Sdougm 		/* No group specified so go through them all */
23564653Sdougm 		for (group = sa_get_group(handle, NULL);
23574653Sdougm 		    group != NULL;
23584653Sdougm 		    group = sa_get_next_group(group)) {
23594653Sdougm 			/*
23604653Sdougm 			 * Have a group so check if one we want and then list
23614653Sdougm 			 * contents with appropriate options.
23624653Sdougm 			 */
23634653Sdougm 			if (xml)
23644653Sdougm 				show_group_xml(doc, group);
23654653Sdougm 			else
23664653Sdougm 				show_group(group, verbose, properties, protocol,
23674653Sdougm 				    NULL);
23684653Sdougm 		}
23693034Sdougm 	} else {
23704653Sdougm 		/* Have a specified list of groups */
23714653Sdougm 		for (; optind < argc; optind++) {
23724653Sdougm 			group = sa_get_group(handle, argv[optind]);
23734653Sdougm 			if (group != NULL) {
23744653Sdougm 				if (xml)
23754653Sdougm 					show_group_xml(doc, group);
23764653Sdougm 				else
23774653Sdougm 					show_group(group, verbose, properties,
23784653Sdougm 					    protocol, NULL);
23794653Sdougm 			} else {
23804653Sdougm 				(void) printf(gettext("%s: not found\n"),
23814653Sdougm 				    argv[optind]);
23824653Sdougm 				ret = SA_NO_SUCH_GROUP;
23834653Sdougm 			}
23843034Sdougm 		}
23853034Sdougm 	}
23863034Sdougm 	if (xml && ret == SA_OK) {
2387*11415SSurya.Prakki@Sun.COM 		(void) xmlDocFormatDump(stdout, doc, 1);
23884653Sdougm 		xmlFreeDoc(doc);
23893034Sdougm 	}
23903034Sdougm 	return (ret);
23913034Sdougm 
23923034Sdougm }
23933034Sdougm 
23943034Sdougm /*
23953034Sdougm  * enable_share(group, share, update_legacy)
23963034Sdougm  *
23973034Sdougm  * helper function to enable a share if the group is enabled.
23983034Sdougm  */
23993034Sdougm 
24003034Sdougm static int
enable_share(sa_handle_t handle,sa_group_t group,sa_share_t share,int update_legacy)24013910Sdougm enable_share(sa_handle_t handle, sa_group_t group, sa_share_t share,
24025331Samw     int update_legacy)
24033034Sdougm {
24043034Sdougm 	char *value;
24053034Sdougm 	int enabled;
24063034Sdougm 	sa_optionset_t optionset;
24075331Samw 	int err;
24083034Sdougm 	int ret = SA_OK;
24093034Sdougm 	char *zfs = NULL;
24103034Sdougm 	int iszfs = 0;
24115331Samw 	int isshare;
24123034Sdougm 
24133034Sdougm 	/*
24143034Sdougm 	 * need to enable this share if the group is enabled but not
24153034Sdougm 	 * otherwise. The enable is also done on each protocol
24163034Sdougm 	 * represented in the group.
24173034Sdougm 	 */
24183034Sdougm 	value = sa_get_group_attr(group, "state");
24193034Sdougm 	enabled = value != NULL && strcmp(value, "enabled") == 0;
24203034Sdougm 	if (value != NULL)
24214653Sdougm 		sa_free_attr_string(value);
24223034Sdougm 	/* remove legacy config if necessary */
24233034Sdougm 	if (update_legacy)
24245331Samw 		ret = sa_delete_legacy(share, NULL);
24253034Sdougm 	zfs = sa_get_group_attr(group, "zfs");
24263034Sdougm 	if (zfs != NULL) {
24274653Sdougm 		iszfs++;
24284653Sdougm 		sa_free_attr_string(zfs);
24293034Sdougm 	}
24303034Sdougm 
24313034Sdougm 	/*
24323034Sdougm 	 * Step through each optionset at the group level and
24333034Sdougm 	 * enable the share based on the protocol type. This
24343034Sdougm 	 * works because protocols must be set on the group
24353034Sdougm 	 * for the protocol to be enabled.
24363034Sdougm 	 */
24375331Samw 	isshare = sa_is_share(share);
24383034Sdougm 	for (optionset = sa_get_optionset(group, NULL);
24393034Sdougm 	    optionset != NULL && ret == SA_OK;
24403034Sdougm 	    optionset = sa_get_next_optionset(optionset)) {
24414653Sdougm 		value = sa_get_optionset_attr(optionset, "type");
24424653Sdougm 		if (value != NULL) {
24435331Samw 			if (enabled) {
24445331Samw 				if (isshare) {
24455331Samw 					err = sa_enable_share(share, value);
24465331Samw 				} else {
24475331Samw 					err = sa_enable_resource(share, value);
24485331Samw 					if (err == SA_NOT_SUPPORTED) {
24495331Samw 						sa_share_t parent;
24505331Samw 						parent = sa_get_resource_parent(
24515331Samw 						    share);
24525331Samw 						if (parent != NULL)
24535331Samw 							err = sa_enable_share(
24545331Samw 							    parent, value);
24555331Samw 					}
24565331Samw 				}
24575331Samw 				if (err != SA_OK) {
24585331Samw 					ret = err;
24595331Samw 					(void) printf(gettext(
24605331Samw 					    "Failed to enable share for "
24615331Samw 					    "\"%s\": %s\n"),
24625331Samw 					    value, sa_errorstr(ret));
24635331Samw 				}
24645331Samw 			}
24655331Samw 			/*
24665331Samw 			 * If we want to update the legacy, use a copy of
24675331Samw 			 * share so we can avoid breaking the loop we are in
24685331Samw 			 * since we might also need to go up the tree to the
24695331Samw 			 * parent.
24705331Samw 			 */
24715331Samw 			if (update_legacy && !iszfs) {
24725331Samw 				sa_share_t update = share;
24735331Samw 				if (!sa_is_share(share)) {
24745331Samw 					update = sa_get_resource_parent(share);
24755331Samw 				}
24765331Samw 				(void) sa_update_legacy(update, value);
24775331Samw 			}
24784653Sdougm 			sa_free_attr_string(value);
24794653Sdougm 		}
24803034Sdougm 	}
24813034Sdougm 	if (ret == SA_OK)
24824653Sdougm 		(void) sa_update_config(handle);
24833034Sdougm 	return (ret);
24843034Sdougm }
24853034Sdougm 
24863034Sdougm /*
24875331Samw  * sa_require_resource(group)
24885331Samw  *
24895331Samw  * if any of the defined protocols on the group require resource
24905331Samw  * names, then all shares must have them.
24915331Samw  */
24925331Samw 
24935331Samw static int
sa_require_resource(sa_group_t group)24945331Samw sa_require_resource(sa_group_t group)
24955331Samw {
24965331Samw 	sa_optionset_t optionset;
24975331Samw 
24985331Samw 	for (optionset = sa_get_optionset(group, NULL);
24995331Samw 	    optionset != NULL;
25005331Samw 	    optionset = sa_get_next_optionset(optionset)) {
25015331Samw 		char *proto;
25025331Samw 
25035331Samw 		proto = sa_get_optionset_attr(optionset, "type");
25045331Samw 		if (proto != NULL) {
25055331Samw 			uint64_t features;
25065331Samw 
25075331Samw 			features = sa_proto_get_featureset(proto);
25085331Samw 			if (features & SA_FEATURE_RESOURCE) {
25095331Samw 				sa_free_attr_string(proto);
25105331Samw 				return (B_TRUE);
25115331Samw 			}
25125331Samw 			sa_free_attr_string(proto);
25135331Samw 		}
25145331Samw 	}
25155331Samw 	return (B_FALSE);
25165331Samw }
25175331Samw 
25185331Samw /*
25193034Sdougm  * sa_addshare(flags, argc, argv)
25203034Sdougm  *
25213034Sdougm  * implements add-share subcommand.
25223034Sdougm  */
25233034Sdougm 
25245331Samw static int
sa_addshare(sa_handle_t handle,int flags,int argc,char * argv[])25253910Sdougm sa_addshare(sa_handle_t handle, int flags, int argc, char *argv[])
25263034Sdougm {
25273034Sdougm 	int verbose = 0;
25283034Sdougm 	int dryrun = 0;
25293034Sdougm 	int c;
25303034Sdougm 	int ret = SA_OK;
25313034Sdougm 	sa_group_t group;
25323034Sdougm 	sa_share_t share;
25335331Samw 	sa_resource_t resource = NULL;
25343034Sdougm 	char *sharepath = NULL;
25353034Sdougm 	char *description = NULL;
25365331Samw 	char *rsrcname = NULL;
25375331Samw 	char *rsrc = NULL;
25383034Sdougm 	int persist = SA_SHARE_PERMANENT; /* default to persist */
25393034Sdougm 	int auth;
25403034Sdougm 	char dir[MAXPATHLEN];
25413034Sdougm 
25423034Sdougm 	while ((c = getopt(argc, argv, "?hvns:d:r:t")) != EOF) {
25434653Sdougm 		switch (c) {
25444653Sdougm 		case 'n':
25454653Sdougm 			dryrun++;
25464653Sdougm 			break;
25474653Sdougm 		case 'v':
25484653Sdougm 			verbose++;
25494653Sdougm 			break;
25504653Sdougm 		case 'd':
25514653Sdougm 			description = optarg;
25524653Sdougm 			break;
25534653Sdougm 		case 'r':
25545331Samw 			if (rsrcname != NULL) {
25555331Samw 				(void) printf(gettext("Adding multiple "
25565331Samw 				    "resource names not"
25575331Samw 				    " supported\n"));
25585331Samw 				return (SA_SYNTAX_ERR);
25595331Samw 			}
25605331Samw 			rsrcname = optarg;
25614653Sdougm 			break;
25624653Sdougm 		case 's':
25634653Sdougm 			/*
25644653Sdougm 			 * Save share path into group. Currently limit
25654653Sdougm 			 * to one share per command.
25664653Sdougm 			 */
25674653Sdougm 			if (sharepath != NULL) {
25684653Sdougm 				(void) printf(gettext(
25694653Sdougm 				    "Adding multiple shares not supported\n"));
25705331Samw 				return (SA_SYNTAX_ERR);
25714653Sdougm 			}
25724653Sdougm 			sharepath = optarg;
25734653Sdougm 			break;
25744653Sdougm 		case 't':
25754653Sdougm 			persist = SA_SHARE_TRANSIENT;
25764653Sdougm 			break;
25776019Sdougm 		case 'h':
25786019Sdougm 			/* optopt on valid arg isn't defined */
25796019Sdougm 			optopt = c;
25806019Sdougm 			/*FALLTHROUGH*/
25816019Sdougm 		case '?':
25824653Sdougm 		default:
25836019Sdougm 			/*
25846019Sdougm 			 * Since a bad option gets to here, sort it
25856019Sdougm 			 * out and return a syntax error return value
25866019Sdougm 			 * if necessary.
25876019Sdougm 			 */
25886019Sdougm 			switch (optopt) {
25896019Sdougm 			default:
25906019Sdougm 				ret = SA_SYNTAX_ERR;
25916019Sdougm 				break;
25926019Sdougm 			case 'h':
25936019Sdougm 			case '?':
25946019Sdougm 				break;
25956019Sdougm 			}
25964653Sdougm 			(void) printf(gettext("usage: %s\n"),
25974653Sdougm 			    sa_get_usage(USAGE_ADD_SHARE));
25986019Sdougm 			return (ret);
25993034Sdougm 		}
26003034Sdougm 	}
26013034Sdougm 
26023034Sdougm 	if (optind >= argc) {
26034653Sdougm 		(void) printf(gettext("usage: %s\n"),
26044653Sdougm 		    sa_get_usage(USAGE_ADD_SHARE));
26054653Sdougm 		if (dryrun || sharepath != NULL || description != NULL ||
26065331Samw 		    rsrcname != NULL || verbose || persist) {
26074653Sdougm 			(void) printf(gettext("\tgroup must be specified\n"));
26084653Sdougm 			ret = SA_NO_SUCH_GROUP;
26094653Sdougm 		} else {
26104653Sdougm 			ret = SA_OK;
26114653Sdougm 		}
26123034Sdougm 	} else {
26134653Sdougm 		if (sharepath == NULL) {
26144653Sdougm 			(void) printf(gettext("usage: %s\n"),
26154653Sdougm 			    sa_get_usage(USAGE_ADD_SHARE));
26164653Sdougm 			(void) printf(gettext(
26174653Sdougm 			    "\t-s sharepath must be specified\n"));
26185331Samw 			ret = SA_BAD_PATH;
26194653Sdougm 		}
26205331Samw 		if (ret == SA_OK) {
26215331Samw 			if (realpath(sharepath, dir) == NULL) {
26225331Samw 				ret = SA_BAD_PATH;
26235331Samw 				(void) printf(gettext("Path "
26245331Samw 				    "is not valid: %s\n"),
26255331Samw 				    sharepath);
26265331Samw 			} else {
26275331Samw 				sharepath = dir;
26285331Samw 			}
26293034Sdougm 		}
26305331Samw 		if (ret == SA_OK && rsrcname != NULL) {
26315331Samw 			/* check for valid syntax */
26325331Samw 			if (validresource(rsrcname)) {
26335331Samw 				rsrc = conv_to_utf8(rsrcname);
26345331Samw 				resource = sa_find_resource(handle, rsrc);
26355331Samw 				if (resource != NULL) {
26365331Samw 					/*
26375331Samw 					 * Resource names must be
26385331Samw 					 * unique in the system
26395331Samw 					 */
26405331Samw 					ret = SA_DUPLICATE_NAME;
26415331Samw 					(void) printf(gettext("usage: %s\n"),
26425331Samw 					    sa_get_usage(USAGE_ADD_SHARE));
26435331Samw 					(void) printf(gettext(
26445331Samw 					    "\tresource names must be unique "
26455331Samw 					    "in the system\n"));
26465331Samw 				}
26475331Samw 			} else {
26485331Samw 				(void) printf(gettext("usage: %s\n"),
26495331Samw 				    sa_get_usage(USAGE_ADD_SHARE));
26505331Samw 				(void) printf(gettext(
26515331Samw 				    "\tresource names use restricted "
26525331Samw 				    "character set\n"));
26535331Samw 				ret = SA_INVALID_NAME;
26545331Samw 			}
26553034Sdougm 		}
26565331Samw 
26575331Samw 		if (ret != SA_OK) {
26585331Samw 			if (rsrc != NULL && rsrcname != rsrc)
26595331Samw 				sa_free_attr_string(rsrc);
26605331Samw 			return (ret);
26614653Sdougm 		}
26625331Samw 
26634653Sdougm 		share = sa_find_share(handle, sharepath);
26644653Sdougm 		if (share != NULL) {
26655331Samw 			if (rsrcname == NULL) {
26665331Samw 				/*
26675331Samw 				 * Can only have a duplicate share if a new
26685331Samw 				 * resource name is being added.
26695331Samw 				 */
26705331Samw 				ret = SA_DUPLICATE_NAME;
26715331Samw 				(void) printf(gettext("Share path already "
26725331Samw 				    "shared: %s\n"), sharepath);
26735331Samw 			}
26745331Samw 		}
26755331Samw 		if (ret != SA_OK)
26765331Samw 			return (ret);
26775331Samw 
26785331Samw 		group = sa_get_group(handle, argv[optind]);
26795331Samw 		if (group != NULL) {
26805331Samw 			if (sa_require_resource(group) == B_TRUE &&
26815331Samw 			    rsrcname == NULL) {
26825331Samw 				(void) printf(gettext(
26835331Samw 				    "Resource name is required "
26845331Samw 				    "by at least one enabled protocol "
26855331Samw 				    "in group\n"));
26865331Samw 				return (SA_RESOURCE_REQUIRED);
26875331Samw 			}
26885331Samw 			if (share == NULL && ret == SA_OK) {
26895331Samw 				if (dryrun)
26905331Samw 					ret = sa_check_path(group, sharepath,
26915331Samw 					    SA_CHECK_NORMAL);
26925331Samw 				else
26935331Samw 					share = sa_add_share(group, sharepath,
26945331Samw 					    persist, &ret);
26955331Samw 			}
26965331Samw 			/*
26975331Samw 			 * Make sure this isn't an attempt to put a resourced
26985331Samw 			 * share into a different group than it already is in.
26995331Samw 			 */
27005331Samw 			if (share != NULL) {
27015331Samw 				sa_group_t parent;
27025331Samw 				parent = sa_get_parent_group(share);
27035331Samw 				if (parent != group) {
27045331Samw 					ret = SA_DUPLICATE_NAME;
27054653Sdougm 					(void) printf(gettext(
27064653Sdougm 					    "Share path already "
27075331Samw 					    "shared: %s\n"), sharepath);
27084653Sdougm 				}
27093034Sdougm 			}
27103034Sdougm 			if (!dryrun && share == NULL) {
27114653Sdougm 				(void) printf(gettext(
27124653Sdougm 				    "Could not add share: %s\n"),
27134653Sdougm 				    sa_errorstr(ret));
27143034Sdougm 			} else {
27155331Samw 				auth = check_authorizations(argv[optind],
27165331Samw 				    flags);
27174653Sdougm 				if (!dryrun && ret == SA_OK) {
27185331Samw 					if (rsrcname != NULL) {
27195331Samw 						resource = sa_add_resource(
27205331Samw 						    share,
27215331Samw 						    rsrc,
27225331Samw 						    SA_SHARE_PERMANENT,
27235331Samw 						    &ret);
27244653Sdougm 					}
27254653Sdougm 					if (ret == SA_OK &&
27264653Sdougm 					    description != NULL) {
27275885Sdougm 						if (resource != NULL)
27285885Sdougm 							ret =
27295885Sdougm 							    set_resource_desc(
27305885Sdougm 							    resource,
27315885Sdougm 							    description);
27325885Sdougm 						else
27335331Samw 							ret =
27345331Samw 							    set_share_desc(
27355331Samw 							    share,
27365331Samw 							    description);
27374653Sdougm 					}
27384653Sdougm 					if (ret == SA_OK) {
27395331Samw 						/* now enable the share(s) */
27405331Samw 						if (resource != NULL) {
27415331Samw 							ret = enable_share(
27425331Samw 							    handle,
27435331Samw 							    group,
27445331Samw 							    resource,
27455331Samw 							    1);
27465331Samw 						} else {
27475331Samw 							ret = enable_share(
27485331Samw 							    handle,
27495331Samw 							    group,
27505331Samw 							    share,
27515331Samw 							    1);
27525331Samw 						}
27534653Sdougm 						ret = sa_update_config(handle);
27544653Sdougm 					}
27554653Sdougm 					switch (ret) {
27564653Sdougm 					case SA_DUPLICATE_NAME:
27574653Sdougm 						(void) printf(gettext(
27584653Sdougm 						    "Resource name in"
27595331Samw 						    "use: %s\n"),
27605331Samw 						    rsrcname);
27614653Sdougm 						break;
27624653Sdougm 					default:
27635331Samw 						(void) printf(gettext(
27645331Samw 						    "Could not set "
27654653Sdougm 						    "attribute: %s\n"),
27664653Sdougm 						    sa_errorstr(ret));
27674653Sdougm 						break;
27684653Sdougm 					case SA_OK:
27694653Sdougm 						break;
27704653Sdougm 					}
27715331Samw 				} else if (dryrun && ret == SA_OK &&
27725331Samw 				    !auth && verbose) {
27734653Sdougm 					(void) printf(gettext(
27744653Sdougm 					    "Command would fail: %s\n"),
27754653Sdougm 					    sa_errorstr(SA_NO_PERMISSION));
27764653Sdougm 					ret = SA_NO_PERMISSION;
27773034Sdougm 				}
27783034Sdougm 			}
27795331Samw 		} else {
27805331Samw 			switch (ret) {
27815331Samw 			default:
27825331Samw 				(void) printf(gettext(
27835331Samw 				    "Group \"%s\" not found\n"), argv[optind]);
27845331Samw 				ret = SA_NO_SUCH_GROUP;
27855331Samw 				break;
27865331Samw 			case SA_BAD_PATH:
27875331Samw 			case SA_DUPLICATE_NAME:
27885331Samw 				break;
27895331Samw 			}
27903034Sdougm 		}
27913034Sdougm 	}
27923034Sdougm 	return (ret);
27933034Sdougm }
27943034Sdougm 
27953034Sdougm /*
27963034Sdougm  * sa_moveshare(flags, argc, argv)
27973034Sdougm  *
27983034Sdougm  * implements move-share subcommand.
27993034Sdougm  */
28003034Sdougm 
28013034Sdougm int
sa_moveshare(sa_handle_t handle,int flags,int argc,char * argv[])28023910Sdougm sa_moveshare(sa_handle_t handle, int flags, int argc, char *argv[])
28033034Sdougm {
28043034Sdougm 	int verbose = 0;
28053034Sdougm 	int dryrun = 0;
28063034Sdougm 	int c;
28073034Sdougm 	int ret = SA_OK;
28083034Sdougm 	sa_group_t group;
28093034Sdougm 	sa_share_t share;
28105331Samw 	char *rsrcname = NULL;
28113034Sdougm 	char *sharepath = NULL;
28123034Sdougm 	int authsrc = 0, authdst = 0;
28135885Sdougm 	char dir[MAXPATHLEN];
28143034Sdougm 
28155331Samw 	while ((c = getopt(argc, argv, "?hvnr:s:")) != EOF) {
28164653Sdougm 		switch (c) {
28174653Sdougm 		case 'n':
28184653Sdougm 			dryrun++;
28194653Sdougm 			break;
28204653Sdougm 		case 'v':
28214653Sdougm 			verbose++;
28224653Sdougm 			break;
28235331Samw 		case 'r':
28245331Samw 			if (rsrcname != NULL) {
28255331Samw 				(void) printf(gettext(
28265331Samw 				    "Moving multiple resource names not"
28275331Samw 				    " supported\n"));
28285331Samw 				return (SA_SYNTAX_ERR);
28295331Samw 			}
28305331Samw 			rsrcname = optarg;
28315331Samw 			break;
28324653Sdougm 		case 's':
28334653Sdougm 			/*
28344653Sdougm 			 * Remove share path from group. Currently limit
28354653Sdougm 			 * to one share per command.
28364653Sdougm 			 */
28374653Sdougm 			if (sharepath != NULL) {
28384653Sdougm 				(void) printf(gettext("Moving multiple shares"
28395331Samw 				    " not supported\n"));
28405331Samw 				return (SA_SYNTAX_ERR);
28414653Sdougm 			}
28424653Sdougm 			sharepath = optarg;
28434653Sdougm 			break;
28446019Sdougm 		case 'h':
28456019Sdougm 			/* optopt on valid arg isn't defined */
28466019Sdougm 			optopt = c;
28476019Sdougm 			/*FALLTHROUGH*/
28486019Sdougm 		case '?':
28494653Sdougm 		default:
28506019Sdougm 			/*
28516019Sdougm 			 * Since a bad option gets to here, sort it
28526019Sdougm 			 * out and return a syntax error return value
28536019Sdougm 			 * if necessary.
28546019Sdougm 			 */
28556019Sdougm 			switch (optopt) {
28566019Sdougm 			default:
28576019Sdougm 				ret = SA_SYNTAX_ERR;
28586019Sdougm 				break;
28596019Sdougm 			case 'h':
28606019Sdougm 			case '?':
28616019Sdougm 				break;
28626019Sdougm 			}
28634653Sdougm 			(void) printf(gettext("usage: %s\n"),
28644653Sdougm 			    sa_get_usage(USAGE_MOVE_SHARE));
28656019Sdougm 			return (ret);
28663034Sdougm 		}
28673034Sdougm 	}
28683034Sdougm 
28693034Sdougm 	if (optind >= argc || sharepath == NULL) {
28705331Samw 		(void) printf(gettext("usage: %s\n"),
28715331Samw 		    sa_get_usage(USAGE_MOVE_SHARE));
28725331Samw 		if (dryrun || verbose || sharepath != NULL) {
28735331Samw 			(void) printf(gettext("\tgroup must be specified\n"));
28745331Samw 			ret = SA_NO_SUCH_GROUP;
28755331Samw 		} else {
28765331Samw 			if (sharepath == NULL) {
28775331Samw 				ret = SA_SYNTAX_ERR;
28784653Sdougm 				(void) printf(gettext(
28795331Samw 				    "\tsharepath must be specified\n"));
28804653Sdougm 			} else {
28815331Samw 				ret = SA_OK;
28824653Sdougm 			}
28835331Samw 		}
28844653Sdougm 	} else {
28854653Sdougm 		sa_group_t parent;
28864653Sdougm 		char *zfsold;
28874653Sdougm 		char *zfsnew;
28884653Sdougm 
28893034Sdougm 		if (sharepath == NULL) {
28904653Sdougm 			(void) printf(gettext(
28914653Sdougm 			    "sharepath must be specified with the -s "
28924653Sdougm 			    "option\n"));
28934653Sdougm 			return (SA_BAD_PATH);
28944653Sdougm 		}
28953910Sdougm 		group = sa_get_group(handle, argv[optind]);
28964653Sdougm 		if (group == NULL) {
28974653Sdougm 			(void) printf(gettext("Group \"%s\" not found\n"),
28984653Sdougm 			    argv[optind]);
28994653Sdougm 			return (SA_NO_SUCH_GROUP);
29004653Sdougm 		}
29014653Sdougm 		share = sa_find_share(handle, sharepath);
29025885Sdougm 		/*
29035885Sdougm 		 * If a share wasn't found, it may have been a symlink
29045885Sdougm 		 * or has a trailing '/'. Try again after resolving
29055885Sdougm 		 * with realpath().
29065885Sdougm 		 */
29075885Sdougm 		if (share == NULL) {
29085885Sdougm 			if (realpath(sharepath, dir) == NULL) {
29095885Sdougm 				(void) printf(gettext("Path "
29105885Sdougm 				    "is not valid: %s\n"),
29115885Sdougm 				    sharepath);
29125885Sdougm 				return (SA_BAD_PATH);
29135885Sdougm 			}
29145885Sdougm 			sharepath = dir;
29155885Sdougm 			share = sa_find_share(handle, sharepath);
29165885Sdougm 		}
29174653Sdougm 		if (share == NULL) {
29183034Sdougm 			(void) printf(gettext("Share not found: %s\n"),
29194653Sdougm 			    sharepath);
29204653Sdougm 			return (SA_NO_SUCH_PATH);
29214653Sdougm 		}
29225885Sdougm 		authdst = check_authorizations(argv[optind], flags);
29234653Sdougm 
29244653Sdougm 		parent = sa_get_parent_group(share);
29254653Sdougm 		if (parent != NULL) {
29264653Sdougm 			char *pname;
29274653Sdougm 			pname = sa_get_group_attr(parent, "name");
29284653Sdougm 			if (pname != NULL) {
29293034Sdougm 				authsrc = check_authorizations(pname, flags);
29303034Sdougm 				sa_free_attr_string(pname);
29314653Sdougm 			}
29324653Sdougm 			zfsold = sa_get_group_attr(parent, "zfs");
29334653Sdougm 			zfsnew = sa_get_group_attr(group, "zfs");
29344653Sdougm 			if ((zfsold != NULL && zfsnew == NULL) ||
29354653Sdougm 			    (zfsold == NULL && zfsnew != NULL)) {
29363034Sdougm 				ret = SA_NOT_ALLOWED;
29373034Sdougm 			}
29384653Sdougm 			if (zfsold != NULL)
29394653Sdougm 				sa_free_attr_string(zfsold);
29404653Sdougm 			if (zfsnew != NULL)
29414653Sdougm 				sa_free_attr_string(zfsnew);
29424653Sdougm 		}
29434653Sdougm 
29444653Sdougm 		if (ret == SA_OK && parent != group && !dryrun) {
29454653Sdougm 			char *oldstate;
29464653Sdougm 			/*
29474653Sdougm 			 * Note that the share may need to be
29485331Samw 			 * "unshared" if the new group is disabled and
29495331Samw 			 * the old was enabled or it may need to be
29505331Samw 			 * share to update if the new group is
29515331Samw 			 * enabled. We disable before the move and
29525331Samw 			 * will have to enable after the move in order
29535331Samw 			 * to cleanup entries for protocols that
29545331Samw 			 * aren't in the new group.
29554653Sdougm 			 */
29564653Sdougm 			oldstate = sa_get_group_attr(parent, "state");
295711337SWilliam.Krier@Sun.COM 			if (oldstate != NULL) {
295811337SWilliam.Krier@Sun.COM 				/* enable_share determines what to do */
295911337SWilliam.Krier@Sun.COM 				if (strcmp(oldstate, "enabled") == 0)
296011337SWilliam.Krier@Sun.COM 					(void) sa_disable_share(share, NULL);
29613034Sdougm 				sa_free_attr_string(oldstate);
296211337SWilliam.Krier@Sun.COM 			}
29633034Sdougm 		}
29644653Sdougm 
29655331Samw 		if (!dryrun && ret == SA_OK)
29665331Samw 			ret = sa_move_share(group, share);
29675331Samw 
29685331Samw 		/*
29695331Samw 		 * Reenable and update any config information.
29705331Samw 		 */
29715331Samw 		if (ret == SA_OK && parent != group && !dryrun) {
29725331Samw 			ret = sa_update_config(handle);
29735331Samw 
29745331Samw 			(void) enable_share(handle, group, share, 1);
29755331Samw 		}
29765331Samw 
29774653Sdougm 		if (ret != SA_OK)
29784653Sdougm 			(void) printf(gettext("Could not move share: %s\n"),
29794653Sdougm 			    sa_errorstr(ret));
29804653Sdougm 
29814653Sdougm 		if (dryrun && ret == SA_OK && !(authsrc & authdst) &&
29824653Sdougm 		    verbose) {
29834653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
29844653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
29854653Sdougm 		}
29863034Sdougm 	}
29873034Sdougm 	return (ret);
29883034Sdougm }
29893034Sdougm 
29903034Sdougm /*
29913034Sdougm  * sa_removeshare(flags, argc, argv)
29923034Sdougm  *
29933034Sdougm  * implements remove-share subcommand.
29943034Sdougm  */
29953034Sdougm 
29963034Sdougm int
sa_removeshare(sa_handle_t handle,int flags,int argc,char * argv[])29973910Sdougm sa_removeshare(sa_handle_t handle, int flags, int argc, char *argv[])
29983034Sdougm {
29993034Sdougm 	int verbose = 0;
30003034Sdougm 	int dryrun = 0;
30013034Sdougm 	int force = 0;
30023034Sdougm 	int c;
30033034Sdougm 	int ret = SA_OK;
30043034Sdougm 	sa_group_t group;
30055331Samw 	sa_resource_t resource = NULL;
30065331Samw 	sa_share_t share = NULL;
30075331Samw 	char *rsrcname = NULL;
30083034Sdougm 	char *sharepath = NULL;
30093034Sdougm 	char dir[MAXPATHLEN];
30103034Sdougm 	int auth;
30113034Sdougm 
30125331Samw 	while ((c = getopt(argc, argv, "?hfnr:s:v")) != EOF) {
30134653Sdougm 		switch (c) {
30144653Sdougm 		case 'n':
30154653Sdougm 			dryrun++;
30164653Sdougm 			break;
30174653Sdougm 		case 'v':
30184653Sdougm 			verbose++;
30194653Sdougm 			break;
30204653Sdougm 		case 'f':
30214653Sdougm 			force++;
30224653Sdougm 			break;
30234653Sdougm 		case 's':
30244653Sdougm 			/*
30254653Sdougm 			 * Remove share path from group. Currently limit
30264653Sdougm 			 * to one share per command.
30274653Sdougm 			 */
30284653Sdougm 			if (sharepath != NULL) {
30294653Sdougm 				(void) printf(gettext(
30304653Sdougm 				    "Removing multiple shares not "
30313034Sdougm 				    "supported\n"));
30324653Sdougm 				return (SA_SYNTAX_ERR);
30334653Sdougm 			}
30344653Sdougm 			sharepath = optarg;
30354653Sdougm 			break;
30365331Samw 		case 'r':
30375331Samw 			/*
30385331Samw 			 * Remove share from group if last resource or remove
30395331Samw 			 * resource from share if multiple resources.
30405331Samw 			 */
30415331Samw 			if (rsrcname != NULL) {
30425331Samw 				(void) printf(gettext(
30435331Samw 				    "Removing multiple resource names not "
30445331Samw 				    "supported\n"));
30455331Samw 				return (SA_SYNTAX_ERR);
30465331Samw 			}
30475331Samw 			rsrcname = optarg;
30485331Samw 			break;
30496019Sdougm 		case 'h':
30506019Sdougm 			/* optopt on valid arg isn't defined */
30516019Sdougm 			optopt = c;
30526019Sdougm 			/*FALLTHROUGH*/
30536019Sdougm 		case '?':
30544653Sdougm 		default:
30556019Sdougm 			/*
30566019Sdougm 			 * Since a bad option gets to here, sort it
30576019Sdougm 			 * out and return a syntax error return value
30586019Sdougm 			 * if necessary.
30596019Sdougm 			 */
30606019Sdougm 			switch (optopt) {
30616019Sdougm 			default:
30626019Sdougm 				ret = SA_SYNTAX_ERR;
30636019Sdougm 				break;
30646019Sdougm 			case 'h':
30656019Sdougm 			case '?':
30666019Sdougm 				break;
30676019Sdougm 			}
30684653Sdougm 			(void) printf(gettext("usage: %s\n"),
30694653Sdougm 			    sa_get_usage(USAGE_REMOVE_SHARE));
30706019Sdougm 			return (ret);
30713034Sdougm 		}
30723034Sdougm 	}
30733034Sdougm 
30745331Samw 	if (optind >= argc || (rsrcname == NULL && sharepath == NULL)) {
30755331Samw 		if (sharepath == NULL && rsrcname == NULL) {
30763034Sdougm 			(void) printf(gettext("usage: %s\n"),
30774653Sdougm 			    sa_get_usage(USAGE_REMOVE_SHARE));
30785331Samw 			(void) printf(gettext("\t-s sharepath or -r resource"
30795331Samw 			    " must be specified\n"));
30804653Sdougm 			ret = SA_BAD_PATH;
30814653Sdougm 		} else {
30824653Sdougm 			ret = SA_OK;
30834653Sdougm 		}
30843034Sdougm 	}
30854653Sdougm 	if (ret != SA_OK) {
30864653Sdougm 		return (ret);
30874653Sdougm 	}
30884653Sdougm 
30894653Sdougm 	if (optind < argc) {
30903034Sdougm 		if ((optind + 1) < argc) {
30914653Sdougm 			(void) printf(gettext("Extraneous group(s) at end of "
30924653Sdougm 			    "command\n"));
30934653Sdougm 			ret = SA_SYNTAX_ERR;
30943034Sdougm 		} else {
30954653Sdougm 			group = sa_get_group(handle, argv[optind]);
30964653Sdougm 			if (group == NULL) {
30974653Sdougm 				(void) printf(gettext(
30984653Sdougm 				    "Group \"%s\" not found\n"), argv[optind]);
30994653Sdougm 				ret = SA_NO_SUCH_GROUP;
31004653Sdougm 			}
31013034Sdougm 		}
31024653Sdougm 	} else {
31033034Sdougm 		group = NULL;
31044653Sdougm 	}
31054653Sdougm 
31065331Samw 	if (rsrcname != NULL) {
31075331Samw 		resource = sa_find_resource(handle, rsrcname);
31085331Samw 		if (resource == NULL) {
31095331Samw 			ret = SA_NO_SUCH_RESOURCE;
31105331Samw 			(void) printf(gettext(
31115331Samw 			    "Resource name not found for share: %s\n"),
31125331Samw 			    rsrcname);
31135331Samw 		}
31145331Samw 	}
31155331Samw 
31164653Sdougm 	/*
31174653Sdougm 	 * Lookup the path in the internal configuration. Care
31184653Sdougm 	 * must be taken to handle the case where the
31194653Sdougm 	 * underlying path has been removed since we need to
31204653Sdougm 	 * be able to deal with that as well.
31214653Sdougm 	 */
31224653Sdougm 	if (ret == SA_OK) {
31235331Samw 		if (sharepath != NULL) {
31245331Samw 			if (group != NULL)
31255331Samw 				share = sa_get_share(group, sharepath);
31265331Samw 			else
31275331Samw 				share = sa_find_share(handle, sharepath);
31285331Samw 		}
31295331Samw 
31305331Samw 		if (resource != NULL) {
31315331Samw 			sa_share_t rsrcshare;
31325331Samw 			rsrcshare = sa_get_resource_parent(resource);
31335331Samw 			if (share == NULL)
31345331Samw 				share = rsrcshare;
31355331Samw 			else if (share != rsrcshare) {
31365331Samw 				ret = SA_NO_SUCH_RESOURCE;
31375331Samw 				(void) printf(gettext(
31385331Samw 				    "Bad resource name for share: %s\n"),
31395331Samw 				    rsrcname);
31405331Samw 				share = NULL;
31415331Samw 			}
31425331Samw 		}
31435331Samw 
31443663Sdougm 		/*
31453663Sdougm 		 * If we didn't find the share with the provided path,
31463663Sdougm 		 * it may be a symlink so attempt to resolve it using
31473663Sdougm 		 * realpath and try again. Realpath will resolve any
31483663Sdougm 		 * symlinks and place them in "dir". Note that
31493663Sdougm 		 * sharepath is only used for the lookup the first
31503663Sdougm 		 * time and later for error messages. dir will be used
31513663Sdougm 		 * on the second attempt. Once a share is found, all
31523663Sdougm 		 * operations are based off of the share variable.
31533663Sdougm 		 */
31543663Sdougm 		if (share == NULL) {
31554653Sdougm 			if (realpath(sharepath, dir) == NULL) {
31564653Sdougm 				ret = SA_BAD_PATH;
31574653Sdougm 				(void) printf(gettext(
31584653Sdougm 				    "Path is not valid: %s\n"), sharepath);
31594653Sdougm 			} else {
31604653Sdougm 				if (group != NULL)
31614653Sdougm 					share = sa_get_share(group, dir);
31624653Sdougm 				else
31634653Sdougm 					share = sa_find_share(handle, dir);
31644653Sdougm 			}
31653663Sdougm 		}
31664653Sdougm 	}
31674653Sdougm 
31684653Sdougm 	/*
31694653Sdougm 	 * If there hasn't been an error, there was likely a
31704653Sdougm 	 * path found. If not, give the appropriate error
31714653Sdougm 	 * message and set the return error. If it was found,
31724653Sdougm 	 * then disable the share and then remove it from the
31734653Sdougm 	 * configuration.
31744653Sdougm 	 */
31754653Sdougm 	if (ret != SA_OK) {
31764653Sdougm 		return (ret);
31774653Sdougm 	}
31784653Sdougm 	if (share == NULL) {
31794653Sdougm 		if (group != NULL)
31803034Sdougm 			(void) printf(gettext("Share not found in group %s:"
31814653Sdougm 			    " %s\n"), argv[optind], sharepath);
31824653Sdougm 		else
31833034Sdougm 			(void) printf(gettext("Share not found: %s\n"),
31844653Sdougm 			    sharepath);
31855331Samw 		ret = SA_NO_SUCH_PATH;
31864653Sdougm 	} else {
31874653Sdougm 		if (group == NULL)
31883034Sdougm 			group = sa_get_parent_group(share);
31894653Sdougm 		if (!dryrun) {
31903034Sdougm 			if (ret == SA_OK) {
31915331Samw 				if (resource != NULL)
31925331Samw 					ret = sa_disable_resource(resource,
31935331Samw 					    NULL);
31945331Samw 				else
31955331Samw 					ret = sa_disable_share(share, NULL);
31963034Sdougm 				/*
31974653Sdougm 				 * We don't care if it fails since it
31983663Sdougm 				 * could be disabled already. Some
31993663Sdougm 				 * unexpected errors could occur that
32003663Sdougm 				 * prevent removal, so also check for
32013663Sdougm 				 * force being set.
32023034Sdougm 				 */
32035331Samw 				if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
32045331Samw 				    ret == SA_NOT_SUPPORTED ||
32055331Samw 				    ret == SA_SYSTEM_ERR || force) &&
32065331Samw 				    resource == NULL)
32075331Samw 					ret = sa_remove_share(share);
32085331Samw 
32095331Samw 				if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
32104653Sdougm 				    ret == SA_NOT_SUPPORTED ||
32115331Samw 				    ret == SA_SYSTEM_ERR || force) &&
32125331Samw 				    resource != NULL) {
32135331Samw 					ret = sa_remove_resource(resource);
32145331Samw 					if (ret == SA_OK) {
32155331Samw 						/*
32165331Samw 						 * If this was the
32175331Samw 						 * last one, remove
32185331Samw 						 * the share as well.
32195331Samw 						 */
32205331Samw 						resource =
32215331Samw 						    sa_get_share_resource(
32225331Samw 						    share, NULL);
32235331Samw 						if (resource == NULL)
32245331Samw 							ret = sa_remove_share(
32255331Samw 							    share);
32265331Samw 					}
32274653Sdougm 				}
32284653Sdougm 				if (ret == SA_OK)
32294653Sdougm 					ret = sa_update_config(handle);
32303034Sdougm 			}
32314653Sdougm 			if (ret != SA_OK)
32325331Samw 				(void) printf(gettext("Could not remove share:"
32335331Samw 				    " %s\n"), sa_errorstr(ret));
32344653Sdougm 		} else if (ret == SA_OK) {
32353034Sdougm 			char *pname;
32363034Sdougm 			pname = sa_get_group_attr(group, "name");
32373034Sdougm 			if (pname != NULL) {
32384653Sdougm 				auth = check_authorizations(pname, flags);
32394653Sdougm 				sa_free_attr_string(pname);
32403034Sdougm 			}
32413034Sdougm 			if (!auth && verbose) {
32424653Sdougm 				(void) printf(gettext(
32434653Sdougm 				    "Command would fail: %s\n"),
32444653Sdougm 				    sa_errorstr(SA_NO_PERMISSION));
32453034Sdougm 			}
32463034Sdougm 		}
32473034Sdougm 	}
32483034Sdougm 	return (ret);
32493034Sdougm }
32503034Sdougm 
32513034Sdougm /*
32523034Sdougm  * sa_set_share(flags, argc, argv)
32533034Sdougm  *
32543034Sdougm  * implements set-share subcommand.
32553034Sdougm  */
32563034Sdougm 
32573034Sdougm int
sa_set_share(sa_handle_t handle,int flags,int argc,char * argv[])32583910Sdougm sa_set_share(sa_handle_t handle, int flags, int argc, char *argv[])
32593034Sdougm {
32603034Sdougm 	int dryrun = 0;
32613034Sdougm 	int c;
32623034Sdougm 	int ret = SA_OK;
32633034Sdougm 	sa_group_t group, sharegroup;
32645772Sas200622 	sa_share_t share = NULL;
32655331Samw 	sa_resource_t resource = NULL;
32663034Sdougm 	char *sharepath = NULL;
32673034Sdougm 	char *description = NULL;
32685331Samw 	char *rsrcname = NULL;
32695331Samw 	char *rsrc = NULL;
32705331Samw 	char *newname = NULL;
32715331Samw 	char *newrsrc;
32725331Samw 	char *groupname = NULL;
32733034Sdougm 	int auth;
32743034Sdougm 	int verbose = 0;
32753034Sdougm 
32763034Sdougm 	while ((c = getopt(argc, argv, "?hnd:r:s:")) != EOF) {
32774653Sdougm 		switch (c) {
32784653Sdougm 		case 'n':
32794653Sdougm 			dryrun++;
32804653Sdougm 			break;
32814653Sdougm 		case 'd':
32824653Sdougm 			description = optarg;
32834653Sdougm 			break;
32844653Sdougm 		case 'v':
32854653Sdougm 			verbose++;
32864653Sdougm 			break;
32875331Samw 		case 'r':
32885331Samw 			/*
32895331Samw 			 * Update share by resource name
32905331Samw 			 */
32915331Samw 			if (rsrcname != NULL) {
32925331Samw 				(void) printf(gettext(
32935331Samw 				    "Updating multiple resource names not "
32945331Samw 				    "supported\n"));
32955331Samw 				return (SA_SYNTAX_ERR);
32965331Samw 			}
32975331Samw 			rsrcname = optarg;
32985331Samw 			break;
32994653Sdougm 		case 's':
33004653Sdougm 			/*
33014653Sdougm 			 * Save share path into group. Currently limit
33024653Sdougm 			 * to one share per command.
33034653Sdougm 			 */
33044653Sdougm 			if (sharepath != NULL) {
33054653Sdougm 				(void) printf(gettext(
33064653Sdougm 				    "Updating multiple shares not "
33073034Sdougm 				    "supported\n"));
33085331Samw 				return (SA_SYNTAX_ERR);
33094653Sdougm 			}
33104653Sdougm 			sharepath = optarg;
33114653Sdougm 			break;
33126019Sdougm 		case 'h':
33136019Sdougm 			/* optopt on valid arg isn't defined */
33146019Sdougm 			optopt = c;
33156019Sdougm 			/*FALLTHROUGH*/
33166019Sdougm 		case '?':
33174653Sdougm 		default:
33186019Sdougm 			/*
33196019Sdougm 			 * Since a bad option gets to here, sort it
33206019Sdougm 			 * out and return a syntax error return value
33216019Sdougm 			 * if necessary.
33226019Sdougm 			 */
33236019Sdougm 			switch (optopt) {
33246019Sdougm 			default:
33256019Sdougm 				ret = SA_SYNTAX_ERR;
33266019Sdougm 				break;
33276019Sdougm 			case 'h':
33286019Sdougm 			case '?':
33296019Sdougm 				break;
33306019Sdougm 			}
33314653Sdougm 			(void) printf(gettext("usage: %s\n"),
33324653Sdougm 			    sa_get_usage(USAGE_SET_SHARE));
33336019Sdougm 			return (ret);
33343034Sdougm 		}
33353034Sdougm 	}
33364653Sdougm 
33375331Samw 	if (optind >= argc && sharepath == NULL && rsrcname == NULL) {
33384653Sdougm 		if (sharepath == NULL) {
33394653Sdougm 			(void) printf(gettext("usage: %s\n"),
33404653Sdougm 			    sa_get_usage(USAGE_SET_SHARE));
33414653Sdougm 			(void) printf(gettext("\tgroup must be specified\n"));
33424653Sdougm 			ret = SA_BAD_PATH;
33434653Sdougm 		} else {
33444653Sdougm 			ret = SA_OK;
33454653Sdougm 		}
33463034Sdougm 	}
33473034Sdougm 	if ((optind + 1) < argc) {
33484653Sdougm 		(void) printf(gettext("usage: %s\n"),
33494653Sdougm 		    sa_get_usage(USAGE_SET_SHARE));
33504653Sdougm 		(void) printf(gettext("\tExtraneous group(s) at end\n"));
33514653Sdougm 		ret = SA_SYNTAX_ERR;
33523034Sdougm 	}
33534653Sdougm 
33545331Samw 	/*
33555331Samw 	 * Must have at least one of sharepath and rsrcrname.
33565331Samw 	 * It is a syntax error to be missing both.
33575331Samw 	 */
33585331Samw 	if (sharepath == NULL && rsrcname == NULL) {
33595331Samw 		(void) printf(gettext("usage: %s\n"),
33605331Samw 		    sa_get_usage(USAGE_SET_SHARE));
33615331Samw 		ret = SA_SYNTAX_ERR;
33625331Samw 	}
33635331Samw 
33644653Sdougm 	if (ret != SA_OK)
33654653Sdougm 		return (ret);
33664653Sdougm 
33674653Sdougm 	if (optind < argc) {
33683034Sdougm 		groupname = argv[optind];
33693910Sdougm 		group = sa_get_group(handle, groupname);
33704653Sdougm 	} else {
33713034Sdougm 		group = NULL;
33723034Sdougm 		groupname = NULL;
33734653Sdougm 	}
33745331Samw 	if (rsrcname != NULL) {
33755331Samw 		/*
33765331Samw 		 * If rsrcname exists, split rename syntax and then
33775331Samw 		 * convert to utf 8 if no errors.
33785331Samw 		 */
33795331Samw 		newname = strchr(rsrcname, '=');
33805331Samw 		if (newname != NULL) {
33815331Samw 			*newname++ = '\0';
33825331Samw 		}
33835331Samw 		if (!validresource(rsrcname)) {
33845331Samw 			ret = SA_INVALID_NAME;
33855331Samw 			(void) printf(gettext("Invalid resource name: "
33865331Samw 			    "\"%s\"\n"), rsrcname);
33875331Samw 		} else {
33885331Samw 			rsrc = conv_to_utf8(rsrcname);
33895331Samw 		}
33905331Samw 		if (newname != NULL) {
33915331Samw 			if (!validresource(newname)) {
33925331Samw 				ret = SA_INVALID_NAME;
33935331Samw 				(void) printf(gettext("Invalid resource name: "
33945331Samw 				    "%s\n"), newname);
33957637SDoug.McCallum@Sun.COM 				newname = NULL;
33965331Samw 			} else {
33975331Samw 				newrsrc = conv_to_utf8(newname);
33985331Samw 			}
33995331Samw 		}
34005331Samw 	}
34015331Samw 
34025331Samw 	if (ret != SA_OK) {
34035331Samw 		if (rsrcname != NULL && rsrcname != rsrc)
34045331Samw 			sa_free_attr_string(rsrc);
34055331Samw 		if (newname != NULL && newname != newrsrc)
34065331Samw 			sa_free_attr_string(newrsrc);
34075331Samw 		return (ret);
34085331Samw 	}
34095331Samw 
34105331Samw 	if (sharepath != NULL) {
34115331Samw 		share = sa_find_share(handle, sharepath);
34125331Samw 	} else if (rsrcname != NULL) {
34135331Samw 		resource = sa_find_resource(handle, rsrc);
34145772Sas200622 		if (resource != NULL)
34155331Samw 			share = sa_get_resource_parent(resource);
34165772Sas200622 		else
34175772Sas200622 			ret = SA_NO_SUCH_RESOURCE;
34185331Samw 	}
34195331Samw 	if (share != NULL) {
34205331Samw 		sharegroup = sa_get_parent_group(share);
34215331Samw 		if (group != NULL && group != sharegroup) {
34225331Samw 			(void) printf(gettext("Group \"%s\" does not contain "
34235331Samw 			    "share %s\n"),
34245331Samw 			    argv[optind], sharepath);
34255331Samw 			ret = SA_BAD_PATH;
34265331Samw 		} else {
34275331Samw 			int delgroupname = 0;
34285331Samw 			if (groupname == NULL) {
34295331Samw 				groupname = sa_get_group_attr(sharegroup,
34305331Samw 				    "name");
34315331Samw 				delgroupname = 1;
34325331Samw 			}
34335331Samw 			if (groupname != NULL) {
34345331Samw 				auth = check_authorizations(groupname, flags);
34355331Samw 				if (delgroupname) {
34365331Samw 					sa_free_attr_string(groupname);
34375331Samw 					groupname = NULL;
34385331Samw 				}
34395331Samw 			} else {
34405331Samw 				ret = SA_NO_MEMORY;
34415331Samw 			}
34425331Samw 			if (rsrcname != NULL) {
34435331Samw 				resource = sa_find_resource(handle, rsrc);
34445331Samw 				if (!dryrun) {
34455331Samw 					if (newname != NULL &&
34465331Samw 					    resource != NULL)
34475331Samw 						ret = sa_rename_resource(
34485331Samw 						    resource, newrsrc);
34495331Samw 					else if (newname != NULL)
34505331Samw 						ret = SA_NO_SUCH_RESOURCE;
34515331Samw 					if (newname != NULL &&
34525331Samw 					    newname != newrsrc)
34535331Samw 						sa_free_attr_string(newrsrc);
34545331Samw 				}
34555331Samw 				if (rsrc != rsrcname)
34565331Samw 					sa_free_attr_string(rsrc);
34575331Samw 			}
34585331Samw 
34595331Samw 			/*
34605331Samw 			 * If the user has set a description, it will be
34615331Samw 			 * on the resource if -r was used otherwise it
34625331Samw 			 * must be on the share.
34635331Samw 			 */
34645967Scp160787 			if (!dryrun && ret == SA_OK && description != NULL) {
34655967Scp160787 				char *desc;
34665967Scp160787 				desc = conv_to_utf8(description);
34675331Samw 				if (resource != NULL)
34685967Scp160787 					ret = sa_set_resource_description(
34695967Scp160787 					    resource, desc);
34705331Samw 				else
34715967Scp160787 					ret = sa_set_share_description(share,
34725967Scp160787 					    desc);
34735967Scp160787 				if (desc != description)
34745967Scp160787 					sa_free_share_description(desc);
34755331Samw 			}
34765331Samw 		}
34775331Samw 		if (!dryrun && ret == SA_OK) {
34785331Samw 			if (resource != NULL)
34795331Samw 				(void) sa_enable_resource(resource, NULL);
34805331Samw 			ret = sa_update_config(handle);
34815331Samw 		}
34825331Samw 		switch (ret) {
34835331Samw 		case SA_DUPLICATE_NAME:
34845331Samw 			(void) printf(gettext("Resource name in use: %s\n"),
34855331Samw 			    rsrcname);
34865331Samw 			break;
34875331Samw 		default:
34885331Samw 			(void) printf(gettext("Could not set: %s\n"),
34895331Samw 			    sa_errorstr(ret));
34905331Samw 			break;
34915331Samw 		case SA_OK:
34925331Samw 			if (dryrun && !auth && verbose) {
34935331Samw 				(void) printf(gettext(
34945331Samw 				    "Command would fail: %s\n"),
34955331Samw 				    sa_errorstr(SA_NO_PERMISSION));
34965331Samw 			}
34975331Samw 			break;
34985331Samw 		}
34995331Samw 	} else {
35005772Sas200622 		switch (ret) {
35015772Sas200622 		case SA_NO_SUCH_RESOURCE:
35025772Sas200622 			(void) printf(gettext("Resource \"%s\" not found\n"),
35035772Sas200622 			    rsrcname);
35045772Sas200622 			break;
35055772Sas200622 		default:
35065772Sas200622 			if (sharepath != NULL) {
35075772Sas200622 				(void) printf(
35085772Sas200622 				    gettext("Share path \"%s\" not found\n"),
35095772Sas200622 				    sharepath);
35105772Sas200622 				ret = SA_NO_SUCH_PATH;
35115772Sas200622 			} else {
35125772Sas200622 				(void) printf(gettext("Set failed: %s\n"),
35135772Sas200622 				    sa_errorstr(ret));
35145772Sas200622 			}
35155772Sas200622 		}
35163034Sdougm 	}
35174653Sdougm 
35183034Sdougm 	return (ret);
35193034Sdougm }
35203034Sdougm 
35213034Sdougm /*
35223034Sdougm  * add_security(group, sectype, optlist, proto, *err)
35233034Sdougm  *
35243034Sdougm  * Helper function to add a security option (named optionset) to the
35253034Sdougm  * group.
35263034Sdougm  */
35273034Sdougm 
35283034Sdougm static int
add_security(sa_group_t group,char * sectype,struct options * optlist,char * proto,int * err)35293034Sdougm add_security(sa_group_t group, char *sectype,
35305331Samw     struct options *optlist, char *proto, int *err)
35313034Sdougm {
35323034Sdougm 	sa_security_t security;
35333034Sdougm 	int ret = SA_OK;
35343034Sdougm 	int result = 0;
35356214Sdougm 	sa_handle_t handle;
35363034Sdougm 
35373034Sdougm 	sectype = sa_proto_space_alias(proto, sectype);
35383034Sdougm 	security = sa_get_security(group, sectype, proto);
35394653Sdougm 	if (security == NULL)
35404653Sdougm 		security = sa_create_security(group, sectype, proto);
35414653Sdougm 
35423034Sdougm 	if (sectype != NULL)
35434653Sdougm 		sa_free_attr_string(sectype);
35444653Sdougm 
35454653Sdougm 	if (security == NULL)
35466214Sdougm 		goto done;
35476214Sdougm 
35486214Sdougm 	handle = sa_find_group_handle(group);
35496214Sdougm 	if (handle == NULL) {
35506214Sdougm 		ret = SA_CONFIG_ERR;
35516214Sdougm 		goto done;
35526214Sdougm 	}
35534653Sdougm 	while (optlist != NULL) {
35543034Sdougm 		sa_property_t prop;
35553034Sdougm 		prop = sa_get_property(security, optlist->optname);
35563034Sdougm 		if (prop == NULL) {
35573034Sdougm 			/*
35584653Sdougm 			 * Add the property, but only if it is
35593034Sdougm 			 * a non-NULL or non-zero length value
35603034Sdougm 			 */
35614653Sdougm 			if (optlist->optvalue != NULL) {
35624653Sdougm 				prop = sa_create_property(optlist->optname,
35634653Sdougm 				    optlist->optvalue);
35644653Sdougm 				if (prop != NULL) {
35656214Sdougm 					ret = sa_valid_property(handle,
35666214Sdougm 					    security, proto, prop);
35674653Sdougm 					if (ret != SA_OK) {
35684653Sdougm 						(void) sa_remove_property(prop);
35694653Sdougm 						(void) printf(gettext(
35704653Sdougm 						    "Could not add "
35714653Sdougm 						    "property %s: %s\n"),
35724653Sdougm 						    optlist->optname,
35734653Sdougm 						    sa_errorstr(ret));
35744653Sdougm 					}
35754653Sdougm 					if (ret == SA_OK) {
35764653Sdougm 						ret = sa_add_property(security,
35774653Sdougm 						    prop);
35784653Sdougm 						if (ret != SA_OK) {
35794653Sdougm 							(void) printf(gettext(
35804653Sdougm 							    "Could not add "
35815331Samw 							    "property (%s=%s):"
35825331Samw 							    " %s\n"),
35834653Sdougm 							    optlist->optname,
35844653Sdougm 							    optlist->optvalue,
35854653Sdougm 							    sa_errorstr(ret));
35864653Sdougm 						} else {
35874653Sdougm 							result = 1;
35884653Sdougm 						}
35894653Sdougm 					}
35903034Sdougm 				}
35913034Sdougm 			}
35923034Sdougm 		} else {
35934653Sdougm 			ret = sa_update_property(prop, optlist->optvalue);
35944653Sdougm 			result = 1; /* should check if really changed */
35953034Sdougm 		}
35963034Sdougm 		optlist = optlist->next;
35974653Sdougm 	}
35984653Sdougm 	/*
35994653Sdougm 	 * When done, properties may have all been removed but
36004653Sdougm 	 * we need to keep the security type itself until
36014653Sdougm 	 * explicitly removed.
36024653Sdougm 	 */
36034653Sdougm 	if (result)
36043034Sdougm 		ret = sa_commit_properties(security, 0);
36056214Sdougm done:
36063034Sdougm 	*err = ret;
36073034Sdougm 	return (result);
36083034Sdougm }
36093034Sdougm 
36103034Sdougm /*
36115089Sdougm  * zfscheck(group, share)
36125089Sdougm  *
36135089Sdougm  * For the special case where a share was provided, make sure it is a
36145089Sdougm  * compatible path for a ZFS property change.  The only path
36155089Sdougm  * acceptable is the path that defines the zfs sub-group (dataset with
36165089Sdougm  * the sharenfs property set) and not one of the paths that inherited
36175089Sdougm  * the NFS properties. Returns SA_OK if it is usable and
36185089Sdougm  * SA_NOT_ALLOWED if it isn't.
36195089Sdougm  *
36205089Sdougm  * If group is not a ZFS group/subgroup, we assume OK since the check
36215089Sdougm  * on return will catch errors for those cases.  What we are looking
36225089Sdougm  * for here is that the group is ZFS and the share is not the defining
36235089Sdougm  * share.  All else is SA_OK.
36245089Sdougm  */
36255089Sdougm 
36265089Sdougm static int
zfscheck(sa_group_t group,sa_share_t share)36275089Sdougm zfscheck(sa_group_t group, sa_share_t share)
36285089Sdougm {
36295089Sdougm 	int ret = SA_OK;
36305089Sdougm 	char *attr;
36315089Sdougm 
36325089Sdougm 	if (sa_group_is_zfs(group)) {
36335089Sdougm 		/*
36345089Sdougm 		 * The group is a ZFS group.  Does the share represent
36355089Sdougm 		 * the dataset that defined the group? It is only OK
36365089Sdougm 		 * if the attribute "subgroup" exists on the share and
36375089Sdougm 		 * has a value of "true".
36385089Sdougm 		 */
36395089Sdougm 
36405089Sdougm 		ret = SA_NOT_ALLOWED;
36415089Sdougm 		attr = sa_get_share_attr(share, "subgroup");
36425089Sdougm 		if (attr != NULL) {
36435089Sdougm 			if (strcmp(attr, "true") == 0)
36445089Sdougm 				ret = SA_OK;
36455089Sdougm 			sa_free_attr_string(attr);
36465089Sdougm 		}
36475089Sdougm 	}
36485089Sdougm 	return (ret);
36495089Sdougm }
36505089Sdougm 
36515089Sdougm /*
36525331Samw  * basic_set(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
36533034Sdougm  *
36543034Sdougm  * This function implements "set" when a name space (-S) is not
36553034Sdougm  * specified. It is a basic set. Options and other CLI parsing has
36563034Sdougm  * already been done.
36575331Samw  *
36585331Samw  * "rsrcname" is a "resource name". If it is non-NULL, it must match
36595331Samw  * the sharepath if present or group if present, otherwise it is used
36605331Samw  * to set options.
36615331Samw  *
36625331Samw  * Resource names may take options if the protocol supports it. If the
36635331Samw  * protocol doesn't support resource level options, rsrcname is just
36645331Samw  * an alias for the share.
36653034Sdougm  */
36663034Sdougm 
36673034Sdougm static int
basic_set(sa_handle_t handle,char * groupname,struct options * optlist,char * protocol,char * sharepath,char * rsrcname,int dryrun)36683910Sdougm basic_set(sa_handle_t handle, char *groupname, struct options *optlist,
36695331Samw     char *protocol, char *sharepath, char *rsrcname, int dryrun)
36703034Sdougm {
36713034Sdougm 	sa_group_t group;
36723034Sdougm 	int ret = SA_OK;
36733034Sdougm 	int change = 0;
36743034Sdougm 	struct list *worklist = NULL;
36753034Sdougm 
36763910Sdougm 	group = sa_get_group(handle, groupname);
36773034Sdougm 	if (group != NULL) {
36784653Sdougm 		sa_share_t share = NULL;
36795331Samw 		sa_resource_t resource = NULL;
36805331Samw 
36815331Samw 		/*
36825331Samw 		 * If there is a sharepath, make sure it belongs to
36835331Samw 		 * the group.
36845331Samw 		 */
36854653Sdougm 		if (sharepath != NULL) {
36864653Sdougm 			share = sa_get_share(group, sharepath);
36874653Sdougm 			if (share == NULL) {
36884653Sdougm 				(void) printf(gettext(
36894653Sdougm 				    "Share does not exist in group %s\n"),
36904653Sdougm 				    groupname, sharepath);
36914653Sdougm 				ret = SA_NO_SUCH_PATH;
36925089Sdougm 			} else {
36935089Sdougm 				/* if ZFS and OK, then only group */
36945089Sdougm 				ret = zfscheck(group, share);
36955089Sdougm 				if (ret == SA_OK &&
36965089Sdougm 				    sa_group_is_zfs(group))
36975089Sdougm 					share = NULL;
36985089Sdougm 				if (ret == SA_NOT_ALLOWED)
36995089Sdougm 					(void) printf(gettext(
37005089Sdougm 					    "Properties on ZFS group shares "
37015089Sdougm 					    "not supported: %s\n"), sharepath);
37024653Sdougm 			}
37033034Sdougm 		}
37045331Samw 
37055331Samw 		/*
37065331Samw 		 * If a resource name exists, make sure it belongs to
37075331Samw 		 * the share if present else it belongs to the
37085331Samw 		 * group. Also check the protocol to see if it
37095331Samw 		 * supports resource level properties or not. If not,
37105331Samw 		 * use share only.
37115331Samw 		 */
37125331Samw 		if (rsrcname != NULL) {
37135331Samw 			if (share != NULL) {
37145331Samw 				resource = sa_get_share_resource(share,
37155331Samw 				    rsrcname);
37165331Samw 				if (resource == NULL)
37175331Samw 					ret = SA_NO_SUCH_RESOURCE;
37185331Samw 			} else {
37195331Samw 				resource = sa_get_resource(group, rsrcname);
37205331Samw 				if (resource != NULL)
37215331Samw 					share = sa_get_resource_parent(
37225331Samw 					    resource);
37235331Samw 				else
37245331Samw 					ret = SA_NO_SUCH_RESOURCE;
37255331Samw 			}
37265331Samw 			if (ret == SA_OK && resource != NULL) {
37275331Samw 				uint64_t features;
37285331Samw 				/*
37295331Samw 				 * Check to see if the resource can take
37305331Samw 				 * properties. If so, stick the resource into
37315331Samw 				 * "share" so it will all just work.
37325331Samw 				 */
37335331Samw 				features = sa_proto_get_featureset(protocol);
37345331Samw 				if (features & SA_FEATURE_RESOURCE)
37355331Samw 					share = (sa_share_t)resource;
37365331Samw 			}
37375331Samw 		}
37385331Samw 
37394653Sdougm 		if (ret == SA_OK) {
37404653Sdougm 			/* group must exist */
37416214Sdougm 			ret = valid_options(handle, optlist, protocol,
37424653Sdougm 			    share == NULL ? group : share, NULL);
37434653Sdougm 			if (ret == SA_OK && !dryrun) {
37444653Sdougm 				if (share != NULL)
37454653Sdougm 					change |= add_optionset(share, optlist,
37464653Sdougm 					    protocol, &ret);
37474653Sdougm 				else
37484653Sdougm 					change |= add_optionset(group, optlist,
37494653Sdougm 					    protocol, &ret);
37504653Sdougm 				if (ret == SA_OK && change)
37514653Sdougm 					worklist = add_list(worklist, group,
37525331Samw 					    share, protocol);
37534653Sdougm 			}
37543034Sdougm 		}
37554653Sdougm 		free_opt(optlist);
37563034Sdougm 	} else {
37573034Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
37583034Sdougm 		ret = SA_NO_SUCH_GROUP;
37593034Sdougm 	}
37603034Sdougm 	/*
37613034Sdougm 	 * we have a group and potentially legal additions
37623034Sdougm 	 */
37633034Sdougm 
37644653Sdougm 	/*
37654653Sdougm 	 * Commit to configuration if not a dryrunp and properties
37664653Sdougm 	 * have changed.
37674653Sdougm 	 */
37684653Sdougm 	if (!dryrun && ret == SA_OK && change && worklist != NULL)
37693034Sdougm 		/* properties changed, so update all shares */
37705331Samw 		(void) enable_all_groups(handle, worklist, 0, 0, protocol,
37715331Samw 		    B_TRUE);
37724653Sdougm 
37733034Sdougm 	if (worklist != NULL)
37744653Sdougm 		free_list(worklist);
37753034Sdougm 	return (ret);
37763034Sdougm }
37773034Sdougm 
37783034Sdougm /*
37793034Sdougm  * space_set(groupname, optlist, protocol, sharepath, dryrun)
37803034Sdougm  *
37813034Sdougm  * This function implements "set" when a name space (-S) is
37823034Sdougm  * specified. It is a namespace set. Options and other CLI parsing has
37833034Sdougm  * already been done.
37843034Sdougm  */
37853034Sdougm 
37863034Sdougm static int
space_set(sa_handle_t handle,char * groupname,struct options * optlist,char * protocol,char * sharepath,int dryrun,char * sectype)37873910Sdougm space_set(sa_handle_t handle, char *groupname, struct options *optlist,
37885331Samw     char *protocol, char *sharepath, int dryrun, char *sectype)
37893034Sdougm {
37903034Sdougm 	sa_group_t group;
37913034Sdougm 	int ret = SA_OK;
37923034Sdougm 	int change = 0;
37933034Sdougm 	struct list *worklist = NULL;
37943034Sdougm 
37953034Sdougm 	/*
37963034Sdougm 	 * make sure protcol and sectype are valid
37973034Sdougm 	 */
37983034Sdougm 
37993034Sdougm 	if (sa_proto_valid_space(protocol, sectype) == 0) {
38004653Sdougm 		(void) printf(gettext("Option space \"%s\" not valid "
38014653Sdougm 		    "for protocol.\n"), sectype);
38024653Sdougm 		return (SA_INVALID_SECURITY);
38033034Sdougm 	}
38043034Sdougm 
38053910Sdougm 	group = sa_get_group(handle, groupname);
38063034Sdougm 	if (group != NULL) {
38074653Sdougm 		sa_share_t share = NULL;
38084653Sdougm 		if (sharepath != NULL) {
38094653Sdougm 			share = sa_get_share(group, sharepath);
38104653Sdougm 			if (share == NULL) {
38114653Sdougm 				(void) printf(gettext(
38124653Sdougm 				    "Share does not exist in group %s\n"),
38134653Sdougm 				    groupname, sharepath);
38144653Sdougm 				ret = SA_NO_SUCH_PATH;
38155089Sdougm 			} else {
38165089Sdougm 				/* if ZFS and OK, then only group */
38175089Sdougm 				ret = zfscheck(group, share);
38185089Sdougm 				if (ret == SA_OK &&
38195089Sdougm 				    sa_group_is_zfs(group))
38205089Sdougm 					share = NULL;
38215089Sdougm 				if (ret == SA_NOT_ALLOWED)
38225089Sdougm 					(void) printf(gettext(
38235089Sdougm 					    "Properties on ZFS group shares "
38245089Sdougm 					    "not supported: %s\n"), sharepath);
38254653Sdougm 			}
38263034Sdougm 		}
38274653Sdougm 		if (ret == SA_OK) {
38284653Sdougm 			/* group must exist */
38296214Sdougm 			ret = valid_options(handle, optlist, protocol,
38304653Sdougm 			    share == NULL ? group : share, sectype);
38314653Sdougm 			if (ret == SA_OK && !dryrun) {
38324653Sdougm 				if (share != NULL)
38334653Sdougm 					change = add_security(share, sectype,
38344653Sdougm 					    optlist, protocol, &ret);
38354653Sdougm 				else
38364653Sdougm 					change = add_security(group, sectype,
38374653Sdougm 					    optlist, protocol, &ret);
38384653Sdougm 				if (ret != SA_OK)
38394653Sdougm 					(void) printf(gettext(
38404653Sdougm 					    "Could not set property: %s\n"),
38414653Sdougm 					    sa_errorstr(ret));
38424653Sdougm 			}
38434653Sdougm 			if (ret == SA_OK && change)
38445331Samw 				worklist = add_list(worklist, group, share,
38455331Samw 				    protocol);
38463034Sdougm 		}
38474653Sdougm 		free_opt(optlist);
38483034Sdougm 	} else {
38493034Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
38503034Sdougm 		ret = SA_NO_SUCH_GROUP;
38513034Sdougm 	}
38525331Samw 
38533034Sdougm 	/*
38545331Samw 	 * We have a group and potentially legal additions.
38553034Sdougm 	 */
38563034Sdougm 
38574653Sdougm 	/* Commit to configuration if not a dryrun */
38583034Sdougm 	if (!dryrun && ret == 0) {
38594653Sdougm 		if (change && worklist != NULL) {
38604653Sdougm 			/* properties changed, so update all shares */
38614653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
38625331Samw 			    protocol, B_TRUE);
38634653Sdougm 		}
38644653Sdougm 		ret = sa_update_config(handle);
38653034Sdougm 	}
38663034Sdougm 	if (worklist != NULL)
38674653Sdougm 		free_list(worklist);
38683034Sdougm 	return (ret);
38693034Sdougm }
38703034Sdougm 
38713034Sdougm /*
38723034Sdougm  * sa_set(flags, argc, argv)
38733034Sdougm  *
38743034Sdougm  * Implements the set subcommand. It keys off of -S to determine which
38753034Sdougm  * set of operations to actually do.
38763034Sdougm  */
38773034Sdougm 
38783034Sdougm int
sa_set(sa_handle_t handle,int flags,int argc,char * argv[])38793910Sdougm sa_set(sa_handle_t handle, int flags, int argc, char *argv[])
38803034Sdougm {
38813034Sdougm 	char *groupname;
38823034Sdougm 	int verbose = 0;
38833034Sdougm 	int dryrun = 0;
38843034Sdougm 	int c;
38853034Sdougm 	char *protocol = NULL;
38863034Sdougm 	int ret = SA_OK;
38873034Sdougm 	struct options *optlist = NULL;
38885331Samw 	char *rsrcname = NULL;
38893034Sdougm 	char *sharepath = NULL;
38903034Sdougm 	char *optset = NULL;
38913034Sdougm 	int auth;
38923034Sdougm 
38935331Samw 	while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
38944653Sdougm 		switch (c) {
38954653Sdougm 		case 'v':
38964653Sdougm 			verbose++;
38974653Sdougm 			break;
38984653Sdougm 		case 'n':
38994653Sdougm 			dryrun++;
39004653Sdougm 			break;
39014653Sdougm 		case 'P':
39025331Samw 			if (protocol != NULL) {
39035331Samw 				(void) printf(gettext(
39045331Samw 				    "Specifying multiple protocols "
39055331Samw 				    "not supported: %s\n"), protocol);
39065331Samw 				return (SA_SYNTAX_ERR);
39075331Samw 			}
39084653Sdougm 			protocol = optarg;
39094653Sdougm 			if (!sa_valid_protocol(protocol)) {
39104653Sdougm 				(void) printf(gettext(
39114653Sdougm 				    "Invalid protocol specified: %s\n"),
39124653Sdougm 				    protocol);
39134653Sdougm 				return (SA_INVALID_PROTOCOL);
39144653Sdougm 			}
39154653Sdougm 			break;
39164653Sdougm 		case 'p':
39174653Sdougm 			ret = add_opt(&optlist, optarg, 0);
39184653Sdougm 			switch (ret) {
39194653Sdougm 			case OPT_ADD_SYNTAX:
39204653Sdougm 				(void) printf(gettext("Property syntax error:"
39214653Sdougm 				    " %s\n"), optarg);
39224653Sdougm 				return (SA_SYNTAX_ERR);
39234653Sdougm 			case OPT_ADD_MEMORY:
39244653Sdougm 				(void) printf(gettext("No memory to set "
39254653Sdougm 				    "property: %s\n"), optarg);
39264653Sdougm 				return (SA_NO_MEMORY);
39274653Sdougm 			default:
39284653Sdougm 				break;
39294653Sdougm 			}
39304653Sdougm 			break;
39315331Samw 		case 'r':
39325331Samw 			if (rsrcname != NULL) {
39335331Samw 				(void) printf(gettext(
39345331Samw 				    "Setting multiple resource names not"
39355331Samw 				    " supported\n"));
39365331Samw 				return (SA_SYNTAX_ERR);
39375331Samw 			}
39385331Samw 			rsrcname = optarg;
39395331Samw 			break;
39404653Sdougm 		case 's':
39415331Samw 			if (sharepath != NULL) {
39425331Samw 				(void) printf(gettext(
39435331Samw 				    "Setting multiple shares not supported\n"));
39445331Samw 				return (SA_SYNTAX_ERR);
39455331Samw 			}
39464653Sdougm 			sharepath = optarg;
39474653Sdougm 			break;
39484653Sdougm 		case 'S':
39495331Samw 			if (optset != NULL) {
39505331Samw 				(void) printf(gettext(
39515331Samw 				    "Specifying multiple property "
39525331Samw 				    "spaces not supported: %s\n"), optset);
39535331Samw 				return (SA_SYNTAX_ERR);
39545331Samw 			}
39554653Sdougm 			optset = optarg;
39564653Sdougm 			break;
39576019Sdougm 		case 'h':
39586019Sdougm 			/* optopt on valid arg isn't defined */
39596019Sdougm 			optopt = c;
39606019Sdougm 			/*FALLTHROUGH*/
39616019Sdougm 		case '?':
39624653Sdougm 		default:
39636019Sdougm 			/*
39646019Sdougm 			 * Since a bad option gets to here, sort it
39656019Sdougm 			 * out and return a syntax error return value
39666019Sdougm 			 * if necessary.
39676019Sdougm 			 */
39686019Sdougm 			switch (optopt) {
39696019Sdougm 			default:
39706019Sdougm 				ret = SA_SYNTAX_ERR;
39716019Sdougm 				break;
39726019Sdougm 			case 'h':
39736019Sdougm 			case '?':
39746019Sdougm 				break;
39756019Sdougm 			}
39764653Sdougm 			(void) printf(gettext("usage: %s\n"),
39774653Sdougm 			    sa_get_usage(USAGE_SET));
39786019Sdougm 			return (ret);
39793034Sdougm 		}
39803034Sdougm 	}
39813034Sdougm 
39823034Sdougm 	if (optlist != NULL)
39834653Sdougm 		ret = chk_opt(optlist, optset != NULL, protocol);
39843034Sdougm 
39853034Sdougm 	if (optind >= argc || (optlist == NULL && optset == NULL) ||
39864653Sdougm 	    protocol == NULL || ret != OPT_ADD_OK) {
39874653Sdougm 		char *sep = "\t";
39884653Sdougm 
39894653Sdougm 		(void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET));
39904653Sdougm 		if (optind >= argc) {
39914653Sdougm 			(void) printf(gettext("%sgroup must be specified"),
39924653Sdougm 			    sep);
39934653Sdougm 			sep = ", ";
39944653Sdougm 		}
39954653Sdougm 		if (optlist == NULL) {
39964653Sdougm 			(void) printf(gettext("%sat least one property must be"
39974653Sdougm 			    " specified"), sep);
39984653Sdougm 			sep = ", ";
39994653Sdougm 		}
40004653Sdougm 		if (protocol == NULL) {
40014653Sdougm 			(void) printf(gettext("%sprotocol must be specified"),
40024653Sdougm 			    sep);
40034653Sdougm 			sep = ", ";
40044653Sdougm 		}
40054653Sdougm 		(void) printf("\n");
40064653Sdougm 		ret = SA_SYNTAX_ERR;
40073034Sdougm 	} else {
40083034Sdougm 		/*
40095089Sdougm 		 * Group already exists so we can proceed after a few
40105089Sdougm 		 * additional checks related to ZFS handling.
40113034Sdougm 		 */
40123034Sdougm 
40134653Sdougm 		groupname = argv[optind];
40145089Sdougm 		if (strcmp(groupname, "zfs") == 0) {
40155089Sdougm 			(void) printf(gettext("Changing properties for group "
40165089Sdougm 			    "\"zfs\" not allowed\n"));
40175089Sdougm 			return (SA_NOT_ALLOWED);
40185089Sdougm 		}
40195089Sdougm 
40204653Sdougm 		auth = check_authorizations(groupname, flags);
40214653Sdougm 		if (optset == NULL)
40224653Sdougm 			ret = basic_set(handle, groupname, optlist, protocol,
40235331Samw 			    sharepath, rsrcname, dryrun);
40244653Sdougm 		else
40254653Sdougm 			ret = space_set(handle, groupname, optlist, protocol,
40264653Sdougm 			    sharepath, dryrun, optset);
40274653Sdougm 		if (dryrun && ret == SA_OK && !auth && verbose) {
40284653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
40294653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
40304653Sdougm 		}
40313034Sdougm 	}
40323034Sdougm 	return (ret);
40333034Sdougm }
40343034Sdougm 
40353034Sdougm /*
40363034Sdougm  * remove_options(group, optlist, proto, *err)
40373034Sdougm  *
40384653Sdougm  * Helper function to actually remove options from a group after all
40393034Sdougm  * preprocessing is done.
40403034Sdougm  */
40413034Sdougm 
40423034Sdougm static int
remove_options(sa_group_t group,struct options * optlist,char * proto,int * err)40433034Sdougm remove_options(sa_group_t group, struct options *optlist,
40445331Samw     char *proto, int *err)
40453034Sdougm {
40463034Sdougm 	struct options *cur;
40473034Sdougm 	sa_optionset_t optionset;
40483034Sdougm 	sa_property_t prop;
40493034Sdougm 	int change = 0;
40503034Sdougm 	int ret = SA_OK;
40513034Sdougm 
40523034Sdougm 	optionset = sa_get_optionset(group, proto);
40533034Sdougm 	if (optionset != NULL) {
40544653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
40554653Sdougm 			prop = sa_get_property(optionset, cur->optname);
40564653Sdougm 			if (prop != NULL) {
40574653Sdougm 				ret = sa_remove_property(prop);
40584653Sdougm 				if (ret != SA_OK)
40594653Sdougm 					break;
40604653Sdougm 				change = 1;
40614653Sdougm 			}
40623034Sdougm 		}
40633034Sdougm 	}
40643034Sdougm 	if (ret == SA_OK && change)
40654653Sdougm 		ret = sa_commit_properties(optionset, 0);
40663034Sdougm 
40673034Sdougm 	if (err != NULL)
40684653Sdougm 		*err = ret;
40693034Sdougm 	return (change);
40703034Sdougm }
40713034Sdougm 
40723034Sdougm /*
40733034Sdougm  * valid_unset(group, optlist, proto)
40743034Sdougm  *
40753034Sdougm  * Sanity check the optlist to make sure they can be removed. Issue an
40763034Sdougm  * error if a property doesn't exist.
40773034Sdougm  */
40783034Sdougm 
40793034Sdougm static int
valid_unset(sa_group_t group,struct options * optlist,char * proto)40803034Sdougm valid_unset(sa_group_t group, struct options *optlist, char *proto)
40813034Sdougm {
40823034Sdougm 	struct options *cur;
40833034Sdougm 	sa_optionset_t optionset;
40843034Sdougm 	sa_property_t prop;
40853034Sdougm 	int ret = SA_OK;
40863034Sdougm 
40873034Sdougm 	optionset = sa_get_optionset(group, proto);
40883034Sdougm 	if (optionset != NULL) {
40894653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
40904653Sdougm 			prop = sa_get_property(optionset, cur->optname);
40914653Sdougm 			if (prop == NULL) {
40924653Sdougm 				(void) printf(gettext(
40934653Sdougm 				    "Could not unset property %s: not set\n"),
40944653Sdougm 				    cur->optname);
40954653Sdougm 				ret = SA_NO_SUCH_PROP;
40964653Sdougm 			}
40973034Sdougm 		}
40983034Sdougm 	}
40993034Sdougm 	return (ret);
41003034Sdougm }
41013034Sdougm 
41023034Sdougm /*
41033034Sdougm  * valid_unset_security(group, optlist, proto)
41043034Sdougm  *
41053034Sdougm  * Sanity check the optlist to make sure they can be removed. Issue an
41063034Sdougm  * error if a property doesn't exist.
41073034Sdougm  */
41083034Sdougm 
41093034Sdougm static int
valid_unset_security(sa_group_t group,struct options * optlist,char * proto,char * sectype)41103034Sdougm valid_unset_security(sa_group_t group, struct options *optlist, char *proto,
41115331Samw     char *sectype)
41123034Sdougm {
41133034Sdougm 	struct options *cur;
41143034Sdougm 	sa_security_t security;
41153034Sdougm 	sa_property_t prop;
41163034Sdougm 	int ret = SA_OK;
41173034Sdougm 	char *sec;
41183034Sdougm 
41193034Sdougm 	sec = sa_proto_space_alias(proto, sectype);
41203034Sdougm 	security = sa_get_security(group, sec, proto);
41213034Sdougm 	if (security != NULL) {
41224653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
41234653Sdougm 			prop = sa_get_property(security, cur->optname);
41244653Sdougm 			if (prop == NULL) {
41254653Sdougm 				(void) printf(gettext(
41264653Sdougm 				    "Could not unset property %s: not set\n"),
41274653Sdougm 				    cur->optname);
41284653Sdougm 				ret = SA_NO_SUCH_PROP;
41294653Sdougm 			}
41303034Sdougm 		}
41313034Sdougm 	} else {
41324653Sdougm 		(void) printf(gettext(
41334653Sdougm 		    "Could not unset %s: space not defined\n"), sectype);
41344653Sdougm 		ret = SA_NO_SUCH_SECURITY;
41353034Sdougm 	}
41363034Sdougm 	if (sec != NULL)
41374653Sdougm 		sa_free_attr_string(sec);
41383034Sdougm 	return (ret);
41393034Sdougm }
41403034Sdougm 
41413034Sdougm /*
41423034Sdougm  * remove_security(group, optlist, proto)
41433034Sdougm  *
41443034Sdougm  * Remove the properties since they were checked as valid.
41453034Sdougm  */
41463034Sdougm 
41473034Sdougm static int
remove_security(sa_group_t group,char * sectype,struct options * optlist,char * proto,int * err)41483034Sdougm remove_security(sa_group_t group, char *sectype,
41495331Samw     struct options *optlist, char *proto, int *err)
41503034Sdougm {
41513034Sdougm 	sa_security_t security;
41523034Sdougm 	int ret = SA_OK;
41533034Sdougm 	int change = 0;
41543034Sdougm 
41553034Sdougm 	sectype = sa_proto_space_alias(proto, sectype);
41563034Sdougm 	security = sa_get_security(group, sectype, proto);
41573034Sdougm 	if (sectype != NULL)
41584653Sdougm 		sa_free_attr_string(sectype);
41593034Sdougm 
41603034Sdougm 	if (security != NULL) {
41614653Sdougm 		while (optlist != NULL) {
41624653Sdougm 			sa_property_t prop;
41634653Sdougm 			prop = sa_get_property(security, optlist->optname);
41644653Sdougm 			if (prop != NULL) {
41654653Sdougm 				ret = sa_remove_property(prop);
41664653Sdougm 				if (ret != SA_OK)
41674653Sdougm 					break;
41684653Sdougm 				change = 1;
41694653Sdougm 			}
41704653Sdougm 			optlist = optlist->next;
41713034Sdougm 		}
41723034Sdougm 		/*
41733034Sdougm 		 * when done, properties may have all been removed but
41743034Sdougm 		 * we need to keep the security type itself until
41753034Sdougm 		 * explicitly removed.
41763034Sdougm 		 */
41774653Sdougm 		if (ret == SA_OK && change)
41784653Sdougm 			ret = sa_commit_properties(security, 0);
41793034Sdougm 	} else {
41804653Sdougm 		ret = SA_NO_SUCH_PROP;
41813034Sdougm 	}
41823034Sdougm 	if (err != NULL)
41834653Sdougm 		*err = ret;
41843034Sdougm 	return (change);
41853034Sdougm }
41863034Sdougm 
41873034Sdougm /*
41885331Samw  * basic_unset(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
41893034Sdougm  *
41904653Sdougm  * Unset non-named optionset properties.
41913034Sdougm  */
41923034Sdougm 
41933034Sdougm static int
basic_unset(sa_handle_t handle,char * groupname,struct options * optlist,char * protocol,char * sharepath,char * rsrcname,int dryrun)41943910Sdougm basic_unset(sa_handle_t handle, char *groupname, struct options *optlist,
41955331Samw     char *protocol, char *sharepath, char *rsrcname, int dryrun)
41963034Sdougm {
41973034Sdougm 	sa_group_t group;
41983034Sdougm 	int ret = SA_OK;
41993034Sdougm 	int change = 0;
42003034Sdougm 	struct list *worklist = NULL;
42014653Sdougm 	sa_share_t share = NULL;
42025331Samw 	sa_resource_t resource = NULL;
42033034Sdougm 
42043910Sdougm 	group = sa_get_group(handle, groupname);
42054653Sdougm 	if (group == NULL)
42064653Sdougm 		return (ret);
42074653Sdougm 
42085331Samw 	/*
42095331Samw 	 * If there is a sharepath, make sure it belongs to
42105331Samw 	 * the group.
42115331Samw 	 */
42124653Sdougm 	if (sharepath != NULL) {
42133034Sdougm 		share = sa_get_share(group, sharepath);
42143034Sdougm 		if (share == NULL) {
42154653Sdougm 			(void) printf(gettext(
42164653Sdougm 			    "Share does not exist in group %s\n"),
42174653Sdougm 			    groupname, sharepath);
42184653Sdougm 			ret = SA_NO_SUCH_PATH;
42193034Sdougm 		}
42204653Sdougm 	}
42215331Samw 	/*
42225331Samw 	 * If a resource name exists, make sure it belongs to
42235331Samw 	 * the share if present else it belongs to the
42245331Samw 	 * group. Also check the protocol to see if it
42255331Samw 	 * supports resource level properties or not. If not,
42265331Samw 	 * use share only.
42275331Samw 	 */
42285331Samw 	if (rsrcname != NULL) {
42295331Samw 		if (share != NULL) {
42305331Samw 			resource = sa_get_share_resource(share, rsrcname);
42315331Samw 			if (resource == NULL)
42325331Samw 				ret = SA_NO_SUCH_RESOURCE;
42335331Samw 		} else {
42345331Samw 			resource = sa_get_resource(group, rsrcname);
42355331Samw 			if (resource != NULL) {
42365331Samw 				share = sa_get_resource_parent(resource);
42375331Samw 			} else {
42385331Samw 				ret = SA_NO_SUCH_RESOURCE;
42395331Samw 			}
42405331Samw 		}
42415331Samw 		if (ret == SA_OK && resource != NULL) {
42425331Samw 			uint64_t features;
42435331Samw 			/*
42445331Samw 			 * Check to see if the resource can take
42455331Samw 			 * properties. If so, stick the resource into
42465331Samw 			 * "share" so it will all just work.
42475331Samw 			 */
42485331Samw 			features = sa_proto_get_featureset(protocol);
42495331Samw 			if (features & SA_FEATURE_RESOURCE)
42505331Samw 				share = (sa_share_t)resource;
42515331Samw 		}
42525331Samw 	}
42535331Samw 
42544653Sdougm 	if (ret == SA_OK) {
42553034Sdougm 		/* group must exist */
42563034Sdougm 		ret = valid_unset(share != NULL ? share : group,
42574653Sdougm 		    optlist, protocol);
42583034Sdougm 		if (ret == SA_OK && !dryrun) {
42594653Sdougm 			if (share != NULL) {
42604653Sdougm 				sa_optionset_t optionset;
42614653Sdougm 				sa_property_t prop;
42624653Sdougm 				change |= remove_options(share, optlist,
42634653Sdougm 				    protocol, &ret);
42644653Sdougm 				/*
42654653Sdougm 				 * If a share optionset is
42664653Sdougm 				 * empty, remove it.
42674653Sdougm 				 */
42684653Sdougm 				optionset = sa_get_optionset((sa_share_t)share,
42694653Sdougm 				    protocol);
42704653Sdougm 				if (optionset != NULL) {
42714653Sdougm 					prop = sa_get_property(optionset, NULL);
42724653Sdougm 					if (prop == NULL)
42734653Sdougm 						(void) sa_destroy_optionset(
42744653Sdougm 						    optionset);
42754653Sdougm 				}
42764653Sdougm 			} else {
42774653Sdougm 				change |= remove_options(group,
42784653Sdougm 				    optlist, protocol, &ret);
42793034Sdougm 			}
42804653Sdougm 			if (ret == SA_OK && change)
42815331Samw 				worklist = add_list(worklist, group, share,
42825331Samw 				    protocol);
42834653Sdougm 			if (ret != SA_OK)
42844653Sdougm 				(void) printf(gettext(
42854653Sdougm 				    "Could not remove properties: "
42864653Sdougm 				    "%s\n"), sa_errorstr(ret));
42873034Sdougm 		}
42884653Sdougm 	} else {
42895331Samw 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
42903034Sdougm 		ret = SA_NO_SUCH_GROUP;
42913034Sdougm 	}
42924653Sdougm 	free_opt(optlist);
42933034Sdougm 
42943034Sdougm 	/*
42954653Sdougm 	 * We have a group and potentially legal additions
42964653Sdougm 	 *
42974653Sdougm 	 * Commit to configuration if not a dryrun
42983034Sdougm 	 */
42993034Sdougm 	if (!dryrun && ret == SA_OK) {
43004653Sdougm 		if (change && worklist != NULL) {
43014653Sdougm 			/* properties changed, so update all shares */
43024653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
43035331Samw 			    protocol, B_TRUE);
43044653Sdougm 		}
43053034Sdougm 	}
43063034Sdougm 	if (worklist != NULL)
43074653Sdougm 		free_list(worklist);
43083034Sdougm 	return (ret);
43093034Sdougm }
43103034Sdougm 
43113034Sdougm /*
43123034Sdougm  * space_unset(groupname, optlist, protocol, sharepath, dryrun)
43133034Sdougm  *
43144653Sdougm  * Unset named optionset properties.
43153034Sdougm  */
43163034Sdougm static int
space_unset(sa_handle_t handle,char * groupname,struct options * optlist,char * protocol,char * sharepath,int dryrun,char * sectype)43173910Sdougm space_unset(sa_handle_t handle, char *groupname, struct options *optlist,
43185331Samw     char *protocol, char *sharepath, int dryrun, char *sectype)
43193034Sdougm {
43203034Sdougm 	sa_group_t group;
43213034Sdougm 	int ret = SA_OK;
43223034Sdougm 	int change = 0;
43233034Sdougm 	struct list *worklist = NULL;
43244653Sdougm 	sa_share_t share = NULL;
43253034Sdougm 
43263910Sdougm 	group = sa_get_group(handle, groupname);
43274653Sdougm 	if (group == NULL) {
43284653Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
43294653Sdougm 		return (SA_NO_SUCH_GROUP);
43304653Sdougm 	}
43314653Sdougm 	if (sharepath != NULL) {
43323034Sdougm 		share = sa_get_share(group, sharepath);
43333034Sdougm 		if (share == NULL) {
43344653Sdougm 			(void) printf(gettext(
43354653Sdougm 			    "Share does not exist in group %s\n"),
43364653Sdougm 			    groupname, sharepath);
43374653Sdougm 			return (SA_NO_SUCH_PATH);
43383034Sdougm 		}
43394653Sdougm 	}
43405331Samw 	ret = valid_unset_security(share != NULL ? share : group,
43415331Samw 	    optlist, protocol, sectype);
43424653Sdougm 
43434653Sdougm 	if (ret == SA_OK && !dryrun) {
43444653Sdougm 		if (optlist != NULL) {
43453034Sdougm 			if (share != NULL) {
43464653Sdougm 				sa_security_t optionset;
43474653Sdougm 				sa_property_t prop;
43484653Sdougm 				change = remove_security(share,
43494653Sdougm 				    sectype, optlist, protocol, &ret);
43504653Sdougm 
43514653Sdougm 				/* If a share security is empty, remove it */
43524653Sdougm 				optionset = sa_get_security((sa_group_t)share,
43534653Sdougm 				    sectype, protocol);
43544653Sdougm 				if (optionset != NULL) {
43554653Sdougm 					prop = sa_get_property(optionset,
43564653Sdougm 					    NULL);
43574653Sdougm 					if (prop == NULL)
43584653Sdougm 						ret = sa_destroy_security(
43594653Sdougm 						    optionset);
43604653Sdougm 				}
43613034Sdougm 			} else {
43624653Sdougm 				change = remove_security(group, sectype,
43634653Sdougm 				    optlist, protocol, &ret);
43643034Sdougm 			}
43654653Sdougm 		} else {
43663034Sdougm 			sa_security_t security;
43673034Sdougm 			char *sec;
43683034Sdougm 			sec = sa_proto_space_alias(protocol, sectype);
43693034Sdougm 			security = sa_get_security(group, sec, protocol);
43703034Sdougm 			if (sec != NULL)
43714653Sdougm 				sa_free_attr_string(sec);
43723034Sdougm 			if (security != NULL) {
43734653Sdougm 				ret = sa_destroy_security(security);
43744653Sdougm 				if (ret == SA_OK)
43754653Sdougm 					change = 1;
43763034Sdougm 			} else {
43774653Sdougm 				ret = SA_NO_SUCH_PROP;
43783034Sdougm 			}
43794653Sdougm 		}
43804653Sdougm 		if (ret != SA_OK)
43813034Sdougm 			(void) printf(gettext("Could not unset property: %s\n"),
43824653Sdougm 			    sa_errorstr(ret));
43833034Sdougm 	}
43844653Sdougm 
43854653Sdougm 	if (ret == SA_OK && change)
43865331Samw 		worklist = add_list(worklist, group, 0, protocol);
43874653Sdougm 
43883034Sdougm 	free_opt(optlist);
43893034Sdougm 	/*
43904653Sdougm 	 * We have a group and potentially legal additions
43913034Sdougm 	 */
43923034Sdougm 
43934653Sdougm 	/* Commit to configuration if not a dryrun */
43943034Sdougm 	if (!dryrun && ret == 0) {
43953034Sdougm 		/* properties changed, so update all shares */
43964653Sdougm 		if (change && worklist != NULL)
43974653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
43985331Samw 			    protocol, B_TRUE);
43994653Sdougm 		ret = sa_update_config(handle);
44003034Sdougm 	}
44013034Sdougm 	if (worklist != NULL)
44024653Sdougm 		free_list(worklist);
44033034Sdougm 	return (ret);
44043034Sdougm }
44053034Sdougm 
44063034Sdougm /*
44073034Sdougm  * sa_unset(flags, argc, argv)
44083034Sdougm  *
44094653Sdougm  * Implements the unset subcommand. Parsing done here and then basic
44103034Sdougm  * or space versions of the real code are called.
44113034Sdougm  */
44123034Sdougm 
44133034Sdougm int
sa_unset(sa_handle_t handle,int flags,int argc,char * argv[])44143910Sdougm sa_unset(sa_handle_t handle, int flags, int argc, char *argv[])
44153034Sdougm {
44163034Sdougm 	char *groupname;
44173034Sdougm 	int verbose = 0;
44183034Sdougm 	int dryrun = 0;
44193034Sdougm 	int c;
44203034Sdougm 	char *protocol = NULL;
44213034Sdougm 	int ret = SA_OK;
44223034Sdougm 	struct options *optlist = NULL;
44235331Samw 	char *rsrcname = NULL;
44243034Sdougm 	char *sharepath = NULL;
44253034Sdougm 	char *optset = NULL;
44263034Sdougm 	int auth;
44273034Sdougm 
44285331Samw 	while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
44294653Sdougm 		switch (c) {
44304653Sdougm 		case 'v':
44314653Sdougm 			verbose++;
44324653Sdougm 			break;
44334653Sdougm 		case 'n':
44344653Sdougm 			dryrun++;
44354653Sdougm 			break;
44364653Sdougm 		case 'P':
44375331Samw 			if (protocol != NULL) {
44385331Samw 				(void) printf(gettext(
44395331Samw 				    "Specifying multiple protocols "
44405331Samw 				    "not supported: %s\n"), protocol);
44415331Samw 				return (SA_SYNTAX_ERR);
44425331Samw 			}
44434653Sdougm 			protocol = optarg;
44444653Sdougm 			if (!sa_valid_protocol(protocol)) {
44454653Sdougm 				(void) printf(gettext(
44464653Sdougm 				    "Invalid protocol specified: %s\n"),
44474653Sdougm 				    protocol);
44484653Sdougm 				return (SA_INVALID_PROTOCOL);
44494653Sdougm 			}
44504653Sdougm 			break;
44514653Sdougm 		case 'p':
44524653Sdougm 			ret = add_opt(&optlist, optarg, 1);
44534653Sdougm 			switch (ret) {
44544653Sdougm 			case OPT_ADD_SYNTAX:
44554653Sdougm 				(void) printf(gettext("Property syntax error "
44564653Sdougm 				    "for property %s\n"), optarg);
44574653Sdougm 				return (SA_SYNTAX_ERR);
44584653Sdougm 
44594653Sdougm 			case OPT_ADD_PROPERTY:
44604653Sdougm 				(void) printf(gettext("Properties need to be "
44614653Sdougm 				    "set with set command: %s\n"), optarg);
44624653Sdougm 				return (SA_SYNTAX_ERR);
44634653Sdougm 
44644653Sdougm 			default:
44654653Sdougm 				break;
44664653Sdougm 			}
44674653Sdougm 			break;
44685331Samw 		case 'r':
44695331Samw 			/*
44705331Samw 			 * Unset properties on resource if applicable or on
44715331Samw 			 * share if resource for this protocol doesn't use
44725331Samw 			 * resources.
44735331Samw 			 */
44745331Samw 			if (rsrcname != NULL) {
44755331Samw 				(void) printf(gettext(
44765331Samw 				    "Unsetting multiple resource "
44775331Samw 				    "names not supported\n"));
44785331Samw 				return (SA_SYNTAX_ERR);
44795331Samw 			}
44805331Samw 			rsrcname = optarg;
44815331Samw 			break;
44824653Sdougm 		case 's':
44835331Samw 			if (sharepath != NULL) {
44845331Samw 				(void) printf(gettext(
44855331Samw 				    "Adding multiple shares not supported\n"));
44865331Samw 				return (SA_SYNTAX_ERR);
44875331Samw 			}
44884653Sdougm 			sharepath = optarg;
44894653Sdougm 			break;
44904653Sdougm 		case 'S':
44915331Samw 			if (optset != NULL) {
44925331Samw 				(void) printf(gettext(
44935331Samw 				    "Specifying multiple property "
44945331Samw 				    "spaces not supported: %s\n"), optset);
44955331Samw 				return (SA_SYNTAX_ERR);
44965331Samw 			}
44974653Sdougm 			optset = optarg;
44984653Sdougm 			break;
44996019Sdougm 		case 'h':
45006019Sdougm 			/* optopt on valid arg isn't defined */
45016019Sdougm 			optopt = c;
45026019Sdougm 			/*FALLTHROUGH*/
45036019Sdougm 		case '?':
45044653Sdougm 		default:
45056019Sdougm 			/*
45066019Sdougm 			 * Since a bad option gets to here, sort it
45076019Sdougm 			 * out and return a syntax error return value
45086019Sdougm 			 * if necessary.
45096019Sdougm 			 */
45106019Sdougm 			switch (optopt) {
45116019Sdougm 			default:
45126019Sdougm 				ret = SA_SYNTAX_ERR;
45136019Sdougm 				break;
45146019Sdougm 			case 'h':
45156019Sdougm 			case '?':
45166019Sdougm 				break;
45176019Sdougm 			}
45184653Sdougm 			(void) printf(gettext("usage: %s\n"),
45194653Sdougm 			    sa_get_usage(USAGE_UNSET));
45206019Sdougm 			return (ret);
45213034Sdougm 		}
45223034Sdougm 	}
45233034Sdougm 
45243034Sdougm 	if (optlist != NULL)
45254653Sdougm 		ret = chk_opt(optlist, optset != NULL, protocol);
45263034Sdougm 
45273034Sdougm 	if (optind >= argc || (optlist == NULL && optset == NULL) ||
45283034Sdougm 	    protocol == NULL) {
45294653Sdougm 		char *sep = "\t";
45304653Sdougm 		(void) printf(gettext("usage: %s\n"),
45314653Sdougm 		    sa_get_usage(USAGE_UNSET));
45324653Sdougm 		if (optind >= argc) {
45334653Sdougm 			(void) printf(gettext("%sgroup must be specified"),
45344653Sdougm 			    sep);
45354653Sdougm 			sep = ", ";
45364653Sdougm 		}
45374653Sdougm 		if (optlist == NULL) {
45384653Sdougm 			(void) printf(gettext("%sat least one property must "
45394653Sdougm 			    "be specified"), sep);
45404653Sdougm 			sep = ", ";
45414653Sdougm 		}
45424653Sdougm 		if (protocol == NULL) {
45434653Sdougm 			(void) printf(gettext("%sprotocol must be specified"),
45444653Sdougm 			    sep);
45454653Sdougm 			sep = ", ";
45464653Sdougm 		}
45474653Sdougm 		(void) printf("\n");
45484653Sdougm 		ret = SA_SYNTAX_ERR;
45493034Sdougm 	} else {
45503034Sdougm 
45513034Sdougm 		/*
45524653Sdougm 		 * If a group already exists, we can only add a new
45533034Sdougm 		 * protocol to it and not create a new one or add the
45543034Sdougm 		 * same protocol again.
45553034Sdougm 		 */
45563034Sdougm 
45574653Sdougm 		groupname = argv[optind];
45584653Sdougm 		auth = check_authorizations(groupname, flags);
45594653Sdougm 		if (optset == NULL)
45604653Sdougm 			ret = basic_unset(handle, groupname, optlist, protocol,
45615331Samw 			    sharepath, rsrcname, dryrun);
45624653Sdougm 		else
45634653Sdougm 			ret = space_unset(handle, groupname, optlist, protocol,
45644653Sdougm 			    sharepath, dryrun, optset);
45654653Sdougm 
45664653Sdougm 		if (dryrun && ret == SA_OK && !auth && verbose)
45674653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
45684653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
45693034Sdougm 	}
45703034Sdougm 	return (ret);
45713034Sdougm }
45723034Sdougm 
45733034Sdougm /*
45743034Sdougm  * sa_enable_group(flags, argc, argv)
45753034Sdougm  *
45763034Sdougm  * Implements the enable subcommand
45773034Sdougm  */
45783034Sdougm 
45793034Sdougm int
sa_enable_group(sa_handle_t handle,int flags,int argc,char * argv[])45803910Sdougm sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[])
45813034Sdougm {
45823034Sdougm 	int verbose = 0;
45833034Sdougm 	int dryrun = 0;
45843034Sdougm 	int all = 0;
45853034Sdougm 	int c;
45863034Sdougm 	int ret = SA_OK;
45873034Sdougm 	char *protocol = NULL;
45883034Sdougm 	char *state;
45893034Sdougm 	struct list *worklist = NULL;
45903034Sdougm 	int auth = 1;
45914653Sdougm 	sa_group_t group;
45923034Sdougm 
45933034Sdougm 	while ((c = getopt(argc, argv, "?havnP:")) != EOF) {
45944653Sdougm 		switch (c) {
45954653Sdougm 		case 'a':
45964653Sdougm 			all = 1;
45974653Sdougm 			break;
45984653Sdougm 		case 'n':
45994653Sdougm 			dryrun++;
46004653Sdougm 			break;
46014653Sdougm 		case 'P':
46025331Samw 			if (protocol != NULL) {
46035331Samw 				(void) printf(gettext(
46045331Samw 				    "Specifying multiple protocols "
46055331Samw 				    "not supported: %s\n"), protocol);
46065331Samw 				return (SA_SYNTAX_ERR);
46075331Samw 			}
46084653Sdougm 			protocol = optarg;
46094653Sdougm 			if (!sa_valid_protocol(protocol)) {
46104653Sdougm 				(void) printf(gettext(
46114653Sdougm 				    "Invalid protocol specified: %s\n"),
46123034Sdougm 				    protocol);
46134653Sdougm 				return (SA_INVALID_PROTOCOL);
46144653Sdougm 			}
46154653Sdougm 			break;
46164653Sdougm 		case 'v':
46174653Sdougm 			verbose++;
46184653Sdougm 			break;
46194653Sdougm 		case 'h':
46206019Sdougm 			/* optopt on valid arg isn't defined */
46216019Sdougm 			optopt = c;
46226019Sdougm 			/*FALLTHROUGH*/
46234653Sdougm 		case '?':
46246019Sdougm 		default:
46256019Sdougm 			/*
46266019Sdougm 			 * Since a bad option gets to here, sort it
46276019Sdougm 			 * out and return a syntax error return value
46286019Sdougm 			 * if necessary.
46296019Sdougm 			 */
46306019Sdougm 			switch (optopt) {
46316019Sdougm 			default:
46326019Sdougm 				ret = SA_SYNTAX_ERR;
46336019Sdougm 				break;
46346019Sdougm 			case 'h':
46356019Sdougm 			case '?':
46366019Sdougm 				(void) printf(gettext("usage: %s\n"),
46376019Sdougm 				    sa_get_usage(USAGE_ENABLE));
46386019Sdougm 				return (ret);
46396019Sdougm 			}
46403034Sdougm 		}
46413034Sdougm 	}
46423034Sdougm 
46433034Sdougm 	if (optind == argc && !all) {
46444653Sdougm 		(void) printf(gettext("usage: %s\n"),
46454653Sdougm 		    sa_get_usage(USAGE_ENABLE));
46464653Sdougm 		(void) printf(gettext("\tmust specify group\n"));
46474653Sdougm 		return (SA_NO_SUCH_PATH);
46484653Sdougm 	}
46494653Sdougm 	if (!all) {
46503034Sdougm 		while (optind < argc) {
46514653Sdougm 			group = sa_get_group(handle, argv[optind]);
46524653Sdougm 			if (group != NULL) {
46534653Sdougm 				auth &= check_authorizations(argv[optind],
46544653Sdougm 				    flags);
46554653Sdougm 				state = sa_get_group_attr(group, "state");
46564653Sdougm 				if (state != NULL &&
46574653Sdougm 				    strcmp(state, "enabled") == 0) {
46584653Sdougm 					/* already enabled */
46594653Sdougm 					if (verbose)
46604653Sdougm 						(void) printf(gettext(
46614653Sdougm 						    "Group \"%s\" is already "
46624653Sdougm 						    "enabled\n"),
46634653Sdougm 						    argv[optind]);
46644653Sdougm 					ret = SA_BUSY; /* already enabled */
46654653Sdougm 				} else {
46664653Sdougm 					worklist = add_list(worklist, group,
46675331Samw 					    0, protocol);
46684653Sdougm 					if (verbose)
46694653Sdougm 						(void) printf(gettext(
46704653Sdougm 						    "Enabling group \"%s\"\n"),
46714653Sdougm 						    argv[optind]);
46724653Sdougm 				}
46734653Sdougm 				if (state != NULL)
46744653Sdougm 					sa_free_attr_string(state);
46753034Sdougm 			} else {
46764653Sdougm 				ret = SA_NO_SUCH_GROUP;
46773034Sdougm 			}
46784653Sdougm 			optind++;
46793034Sdougm 		}
46804653Sdougm 	} else {
46814653Sdougm 		for (group = sa_get_group(handle, NULL);
46824653Sdougm 		    group != NULL;
46833034Sdougm 		    group = sa_get_next_group(group)) {
46845331Samw 			worklist = add_list(worklist, group, 0, protocol);
46853034Sdougm 		}
46864653Sdougm 	}
46874653Sdougm 	if (!dryrun && ret == SA_OK)
46885331Samw 		ret = enable_all_groups(handle, worklist, 1, 0, NULL, B_FALSE);
46894653Sdougm 
46904653Sdougm 	if (ret != SA_OK && ret != SA_BUSY)
46913034Sdougm 		(void) printf(gettext("Could not enable group: %s\n"),
46924653Sdougm 		    sa_errorstr(ret));
46934653Sdougm 	if (ret == SA_BUSY)
46943034Sdougm 		ret = SA_OK;
46954653Sdougm 
46963034Sdougm 	if (worklist != NULL)
46974653Sdougm 		free_list(worklist);
46983034Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose) {
46994653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
47004653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
47013034Sdougm 	}
47023034Sdougm 	return (ret);
47033034Sdougm }
47043034Sdougm 
47053034Sdougm /*
47065331Samw  * disable_group(group, proto)
47073034Sdougm  *
47085331Samw  * Disable all the shares in the specified group.. This is a helper
47095331Samw  * for disable_all_groups in order to simplify regular and subgroup
47105331Samw  * (zfs) disabling. Group has already been checked for non-NULL.
47113034Sdougm  */
47123034Sdougm 
47133034Sdougm static int
disable_group(sa_group_t group,char * proto)47145331Samw disable_group(sa_group_t group, char *proto)
47153034Sdougm {
47163034Sdougm 	sa_share_t share;
47173034Sdougm 	int ret = SA_OK;
47183034Sdougm 
47195331Samw 	/*
47205331Samw 	 * If the protocol isn't enabled, skip it and treat as
47215331Samw 	 * successful.
47225331Samw 	 */
47235331Samw 	if (!has_protocol(group, proto))
47245331Samw 		return (ret);
47255331Samw 
47263034Sdougm 	for (share = sa_get_share(group, NULL);
47273034Sdougm 	    share != NULL && ret == SA_OK;
47283034Sdougm 	    share = sa_get_next_share(share)) {
47295331Samw 		ret = sa_disable_share(share, proto);
47304653Sdougm 		if (ret == SA_NO_SUCH_PATH) {
47314653Sdougm 			/*
47324653Sdougm 			 * this is OK since the path is gone. we can't
47334653Sdougm 			 * re-share it anyway so no error.
47344653Sdougm 			 */
47354653Sdougm 			ret = SA_OK;
47364653Sdougm 		}
47373034Sdougm 	}
47383034Sdougm 	return (ret);
47393034Sdougm }
47403034Sdougm 
47413034Sdougm /*
47423034Sdougm  * disable_all_groups(work, setstate)
47433034Sdougm  *
47443034Sdougm  * helper function that disables the shares in the list of groups
47453034Sdougm  * provided. It optionally marks the group as disabled. Used by both
47463034Sdougm  * enable and start subcommands.
47473034Sdougm  */
47483034Sdougm 
47493034Sdougm static int
disable_all_groups(sa_handle_t handle,struct list * work,int setstate)47503910Sdougm disable_all_groups(sa_handle_t handle, struct list *work, int setstate)
47513034Sdougm {
47523034Sdougm 	int ret = SA_OK;
47533034Sdougm 	sa_group_t subgroup, group;
47543034Sdougm 
47553034Sdougm 	while (work != NULL && ret == SA_OK) {
47564653Sdougm 		group = (sa_group_t)work->item;
47574653Sdougm 		if (setstate)
47584653Sdougm 			ret = sa_set_group_attr(group, "state", "disabled");
47594653Sdougm 		if (ret == SA_OK) {
47604653Sdougm 			char *name;
47614653Sdougm 			name = sa_get_group_attr(group, "name");
47624653Sdougm 			if (name != NULL && strcmp(name, "zfs") == 0) {
47634653Sdougm 				/* need to get the sub-groups for stopping */
47644653Sdougm 				for (subgroup = sa_get_sub_group(group);
47654653Sdougm 				    subgroup != NULL;
47664653Sdougm 				    subgroup = sa_get_next_group(subgroup)) {
47675331Samw 					ret = disable_group(subgroup,
47685331Samw 					    work->proto);
47694653Sdougm 				}
47704653Sdougm 			} else {
47715331Samw 				ret = disable_group(group, work->proto);
47724653Sdougm 			}
477311337SWilliam.Krier@Sun.COM 			if (name != NULL)
477411337SWilliam.Krier@Sun.COM 				sa_free_attr_string(name);
47754653Sdougm 			/*
47764653Sdougm 			 * We don't want to "disable" since it won't come
47774653Sdougm 			 * up after a reboot.  The SMF framework should do
47784653Sdougm 			 * the right thing. On enable we do want to do
47794653Sdougm 			 * something.
47804653Sdougm 			 */
47813034Sdougm 		}
47824653Sdougm 		work = work->next;
47833034Sdougm 	}
47843034Sdougm 	if (ret == SA_OK)
47854653Sdougm 		ret = sa_update_config(handle);
47863034Sdougm 	return (ret);
47873034Sdougm }
47883034Sdougm 
47893034Sdougm /*
47903034Sdougm  * sa_disable_group(flags, argc, argv)
47913034Sdougm  *
47923034Sdougm  * Implements the disable subcommand
47933034Sdougm  */
47943034Sdougm 
47953034Sdougm int
sa_disable_group(sa_handle_t handle,int flags,int argc,char * argv[])47963910Sdougm sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[])
47973034Sdougm {
47983034Sdougm 	int verbose = 0;
47993034Sdougm 	int dryrun = 0;
48003034Sdougm 	int all = 0;
48013034Sdougm 	int c;
48023034Sdougm 	int ret = SA_OK;
48035331Samw 	char *protocol = NULL;
48043034Sdougm 	char *state;
48053034Sdougm 	struct list *worklist = NULL;
48064653Sdougm 	sa_group_t group;
48073034Sdougm 	int auth = 1;
48083034Sdougm 
48093034Sdougm 	while ((c = getopt(argc, argv, "?havn")) != EOF) {
48104653Sdougm 		switch (c) {
48114653Sdougm 		case 'a':
48124653Sdougm 			all = 1;
48134653Sdougm 			break;
48144653Sdougm 		case 'n':
48154653Sdougm 			dryrun++;
48164653Sdougm 			break;
48174653Sdougm 		case 'P':
48185331Samw 			if (protocol != NULL) {
48195331Samw 				(void) printf(gettext(
48205331Samw 				    "Specifying multiple protocols "
48215331Samw 				    "not supported: %s\n"), protocol);
48225331Samw 				return (SA_SYNTAX_ERR);
48235331Samw 			}
48244653Sdougm 			protocol = optarg;
48254653Sdougm 			if (!sa_valid_protocol(protocol)) {
48264653Sdougm 				(void) printf(gettext(
48274653Sdougm 				    "Invalid protocol specified: %s\n"),
48284653Sdougm 				    protocol);
48294653Sdougm 				return (SA_INVALID_PROTOCOL);
48304653Sdougm 			}
48314653Sdougm 			break;
48324653Sdougm 		case 'v':
48334653Sdougm 			verbose++;
48344653Sdougm 			break;
48356019Sdougm 		case 'h':
48366019Sdougm 			/* optopt on valid arg isn't defined */
48376019Sdougm 			optopt = c;
48386019Sdougm 			/*FALLTHROUGH*/
48396019Sdougm 		case '?':
48404653Sdougm 		default:
48416019Sdougm 			/*
48426019Sdougm 			 * Since a bad option gets to here, sort it
48436019Sdougm 			 * out and return a syntax error return value
48446019Sdougm 			 * if necessary.
48456019Sdougm 			 */
48466019Sdougm 			switch (optopt) {
48476019Sdougm 			default:
48486019Sdougm 				ret = SA_SYNTAX_ERR;
48496019Sdougm 				break;
48506019Sdougm 			case 'h':
48516019Sdougm 			case '?':
48526019Sdougm 				break;
48536019Sdougm 			}
48544653Sdougm 			(void) printf(gettext("usage: %s\n"),
48554653Sdougm 			    sa_get_usage(USAGE_DISABLE));
48566019Sdougm 			return (ret);
48573034Sdougm 		}
48583034Sdougm 	}
48593034Sdougm 
48603034Sdougm 	if (optind == argc && !all) {
48613034Sdougm 		(void) printf(gettext("usage: %s\n"),
48624653Sdougm 		    sa_get_usage(USAGE_DISABLE));
48633034Sdougm 		(void) printf(gettext("\tmust specify group\n"));
48644653Sdougm 		return (SA_NO_SUCH_PATH);
48654653Sdougm 	}
48664653Sdougm 	if (!all) {
48674653Sdougm 		while (optind < argc) {
48683910Sdougm 			group = sa_get_group(handle, argv[optind]);
48693034Sdougm 			if (group != NULL) {
48704653Sdougm 				auth &= check_authorizations(argv[optind],
48714653Sdougm 				    flags);
48724653Sdougm 				state = sa_get_group_attr(group, "state");
48734653Sdougm 				if (state == NULL ||
48744653Sdougm 				    strcmp(state, "disabled") == 0) {
48754653Sdougm 					/* already disabled */
48764653Sdougm 					if (verbose)
48774653Sdougm 						(void) printf(gettext(
48784653Sdougm 						    "Group \"%s\" is "
48794653Sdougm 						    "already disabled\n"),
48804653Sdougm 						    argv[optind]);
48815331Samw 					ret = SA_BUSY; /* already disabled */
48824653Sdougm 				} else {
48835331Samw 					worklist = add_list(worklist, group, 0,
48845331Samw 					    protocol);
48854653Sdougm 					if (verbose)
48864653Sdougm 						(void) printf(gettext(
48874653Sdougm 						    "Disabling group "
48884653Sdougm 						    "\"%s\"\n"), argv[optind]);
48894653Sdougm 				}
48904653Sdougm 				if (state != NULL)
48914653Sdougm 					sa_free_attr_string(state);
48923034Sdougm 			} else {
48934653Sdougm 				ret = SA_NO_SUCH_GROUP;
48943034Sdougm 			}
48953034Sdougm 			optind++;
48964653Sdougm 		}
48974653Sdougm 	} else {
48984653Sdougm 		for (group = sa_get_group(handle, NULL);
48994653Sdougm 		    group != NULL;
49004653Sdougm 		    group = sa_get_next_group(group))
49015331Samw 			worklist = add_list(worklist, group, 0, protocol);
49023034Sdougm 	}
49034653Sdougm 
49044653Sdougm 	if (ret == SA_OK && !dryrun)
49054653Sdougm 		ret = disable_all_groups(handle, worklist, 1);
49064653Sdougm 	if (ret != SA_OK && ret != SA_BUSY)
49074653Sdougm 		(void) printf(gettext("Could not disable group: %s\n"),
49084653Sdougm 		    sa_errorstr(ret));
49094653Sdougm 	if (ret == SA_BUSY)
49104653Sdougm 		ret = SA_OK;
49113034Sdougm 	if (worklist != NULL)
49124653Sdougm 		free_list(worklist);
49134653Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose)
49144653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
49154653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
49163034Sdougm 	return (ret);
49173034Sdougm }
49183034Sdougm 
49193034Sdougm /*
49203034Sdougm  * sa_start_group(flags, argc, argv)
49213034Sdougm  *
49223034Sdougm  * Implements the start command.
49233034Sdougm  * This is similar to enable except it doesn't change the state
49243034Sdougm  * of the group(s) and only enables shares if the group is already
49253034Sdougm  * enabled.
49263034Sdougm  */
49275331Samw 
49283034Sdougm int
sa_start_group(sa_handle_t handle,int flags,int argc,char * argv[])49293910Sdougm sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[])
49303034Sdougm {
49313034Sdougm 	int verbose = 0;
49323034Sdougm 	int all = 0;
49333034Sdougm 	int c;
49343034Sdougm 	int ret = SMF_EXIT_OK;
49353034Sdougm 	char *protocol = NULL;
49363034Sdougm 	char *state;
49373034Sdougm 	struct list *worklist = NULL;
49384653Sdougm 	sa_group_t group;
49395331Samw #ifdef lint
49405331Samw 	flags = flags;
49415331Samw #endif
49423034Sdougm 
49433034Sdougm 	while ((c = getopt(argc, argv, "?havP:")) != EOF) {
49444653Sdougm 		switch (c) {
49454653Sdougm 		case 'a':
49464653Sdougm 			all = 1;
49474653Sdougm 			break;
49484653Sdougm 		case 'P':
49495331Samw 			if (protocol != NULL) {
49505331Samw 				(void) printf(gettext(
49515331Samw 				    "Specifying multiple protocols "
49525331Samw 				    "not supported: %s\n"), protocol);
49535331Samw 				return (SA_SYNTAX_ERR);
49545331Samw 			}
49554653Sdougm 			protocol = optarg;
49564653Sdougm 			if (!sa_valid_protocol(protocol)) {
49574653Sdougm 				(void) printf(gettext(
49584653Sdougm 				    "Invalid protocol specified: %s\n"),
49593034Sdougm 				    protocol);
49604653Sdougm 				return (SA_INVALID_PROTOCOL);
49614653Sdougm 			}
49624653Sdougm 			break;
49634653Sdougm 		case 'v':
49644653Sdougm 			verbose++;
49654653Sdougm 			break;
49666019Sdougm 		case 'h':
49676019Sdougm 			/* optopt on valid arg isn't defined */
49686019Sdougm 			optopt = c;
49696019Sdougm 			/*FALLTHROUGH*/
49706019Sdougm 		case '?':
49714653Sdougm 		default:
49726019Sdougm 			/*
49736019Sdougm 			 * Since a bad option gets to here, sort it
49746019Sdougm 			 * out and return a syntax error return value
49756019Sdougm 			 * if necessary.
49766019Sdougm 			 */
49776019Sdougm 			ret = SA_OK;
49786019Sdougm 			switch (optopt) {
49796019Sdougm 			default:
49806019Sdougm 				ret = SA_SYNTAX_ERR;
49816019Sdougm 				break;
49826019Sdougm 			case 'h':
49836019Sdougm 			case '?':
49846019Sdougm 				break;
49856019Sdougm 			}
49864653Sdougm 			(void) printf(gettext("usage: %s\n"),
49874653Sdougm 			    sa_get_usage(USAGE_START));
49886019Sdougm 			return (ret);
49893034Sdougm 		}
49903034Sdougm 	}
49913034Sdougm 
49923034Sdougm 	if (optind == argc && !all) {
49933034Sdougm 		(void) printf(gettext("usage: %s\n"),
49944653Sdougm 		    sa_get_usage(USAGE_START));
49954653Sdougm 		return (SMF_EXIT_ERR_FATAL);
49964653Sdougm 	}
49974653Sdougm 
49984653Sdougm 	if (!all) {
49994653Sdougm 		while (optind < argc) {
50003910Sdougm 			group = sa_get_group(handle, argv[optind]);
50013034Sdougm 			if (group != NULL) {
50024653Sdougm 				state = sa_get_group_attr(group, "state");
50034653Sdougm 				if (state == NULL ||
50044653Sdougm 				    strcmp(state, "enabled") == 0) {
50055331Samw 					worklist = add_list(worklist, group, 0,
50065331Samw 					    protocol);
50074653Sdougm 					if (verbose)
50084653Sdougm 						(void) printf(gettext(
50094653Sdougm 						    "Starting group \"%s\"\n"),
50104653Sdougm 						    argv[optind]);
50114653Sdougm 				} else {
50124653Sdougm 					/*
50134653Sdougm 					 * Determine if there are any
50145331Samw 					 * protocols.  If there aren't any,
50154653Sdougm 					 * then there isn't anything to do in
50164653Sdougm 					 * any case so no error.
50174653Sdougm 					 */
50184653Sdougm 					if (sa_get_optionset(group,
50194653Sdougm 					    protocol) != NULL) {
50204653Sdougm 						ret = SMF_EXIT_OK;
50214653Sdougm 					}
50223034Sdougm 				}
50234653Sdougm 				if (state != NULL)
50244653Sdougm 					sa_free_attr_string(state);
50253034Sdougm 			}
50263034Sdougm 			optind++;
50274653Sdougm 		}
50284653Sdougm 	} else {
50295331Samw 		for (group = sa_get_group(handle, NULL);
50305331Samw 		    group != NULL;
50314653Sdougm 		    group = sa_get_next_group(group)) {
50323034Sdougm 			state = sa_get_group_attr(group, "state");
50333034Sdougm 			if (state == NULL || strcmp(state, "enabled") == 0)
50345331Samw 				worklist = add_list(worklist, group, 0,
50355331Samw 				    protocol);
50363034Sdougm 			if (state != NULL)
50374653Sdougm 				sa_free_attr_string(state);
50383034Sdougm 		}
50393034Sdougm 	}
50404653Sdougm 
50415331Samw 	(void) enable_all_groups(handle, worklist, 0, 1, protocol, B_FALSE);
50424653Sdougm 
50433034Sdougm 	if (worklist != NULL)
50444653Sdougm 		free_list(worklist);
50453034Sdougm 	return (ret);
50463034Sdougm }
50473034Sdougm 
50483034Sdougm /*
50493034Sdougm  * sa_stop_group(flags, argc, argv)
50503034Sdougm  *
50513034Sdougm  * Implements the stop command.
50523034Sdougm  * This is similar to disable except it doesn't change the state
50533034Sdougm  * of the group(s) and only disables shares if the group is already
50543034Sdougm  * enabled.
50553034Sdougm  */
50563034Sdougm int
sa_stop_group(sa_handle_t handle,int flags,int argc,char * argv[])50573910Sdougm sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[])
50583034Sdougm {
50593034Sdougm 	int verbose = 0;
50603034Sdougm 	int all = 0;
50613034Sdougm 	int c;
50623034Sdougm 	int ret = SMF_EXIT_OK;
50633034Sdougm 	char *protocol = NULL;
50643034Sdougm 	char *state;
50653034Sdougm 	struct list *worklist = NULL;
50664653Sdougm 	sa_group_t group;
50675331Samw #ifdef lint
50685331Samw 	flags = flags;
50695331Samw #endif
50703034Sdougm 
50713034Sdougm 	while ((c = getopt(argc, argv, "?havP:")) != EOF) {
50724653Sdougm 		switch (c) {
50734653Sdougm 		case 'a':
50744653Sdougm 			all = 1;
50754653Sdougm 			break;
50764653Sdougm 		case 'P':
50775331Samw 			if (protocol != NULL) {
50785331Samw 				(void) printf(gettext(
50795331Samw 				    "Specifying multiple protocols "
50805331Samw 				    "not supported: %s\n"), protocol);
50815331Samw 				return (SA_SYNTAX_ERR);
50825331Samw 			}
50834653Sdougm 			protocol = optarg;
50844653Sdougm 			if (!sa_valid_protocol(protocol)) {
50854653Sdougm 				(void) printf(gettext(
50864653Sdougm 				    "Invalid protocol specified: %s\n"),
50874653Sdougm 				    protocol);
50884653Sdougm 				return (SA_INVALID_PROTOCOL);
50894653Sdougm 			}
50904653Sdougm 			break;
50914653Sdougm 		case 'v':
50924653Sdougm 			verbose++;
50934653Sdougm 			break;
50946019Sdougm 		case 'h':
50956019Sdougm 			/* optopt on valid arg isn't defined */
50966019Sdougm 			optopt = c;
50976019Sdougm 			/*FALLTHROUGH*/
50986019Sdougm 		case '?':
50994653Sdougm 		default:
51006019Sdougm 			/*
51016019Sdougm 			 * Since a bad option gets to here, sort it
51026019Sdougm 			 * out and return a syntax error return value
51036019Sdougm 			 * if necessary.
51046019Sdougm 			 */
51056019Sdougm 			ret = SA_OK;
51066019Sdougm 			switch (optopt) {
51076019Sdougm 			default:
51086019Sdougm 				ret = SA_SYNTAX_ERR;
51096019Sdougm 				break;
51106019Sdougm 			case 'h':
51116019Sdougm 			case '?':
51126019Sdougm 				break;
51136019Sdougm 			}
51144653Sdougm 			(void) printf(gettext("usage: %s\n"),
51154653Sdougm 			    sa_get_usage(USAGE_STOP));
51166019Sdougm 			return (ret);
51173034Sdougm 		}
51183034Sdougm 	}
51193034Sdougm 
51203034Sdougm 	if (optind == argc && !all) {
51214653Sdougm 		(void) printf(gettext("usage: %s\n"),
51224653Sdougm 		    sa_get_usage(USAGE_STOP));
51234653Sdougm 		return (SMF_EXIT_ERR_FATAL);
51244653Sdougm 	} else if (!all) {
51254653Sdougm 		while (optind < argc) {
51263910Sdougm 			group = sa_get_group(handle, argv[optind]);
51273034Sdougm 			if (group != NULL) {
51284653Sdougm 				state = sa_get_group_attr(group, "state");
51294653Sdougm 				if (state == NULL ||
51304653Sdougm 				    strcmp(state, "enabled") == 0) {
51315331Samw 					worklist = add_list(worklist, group, 0,
51325331Samw 					    protocol);
51334653Sdougm 					if (verbose)
51344653Sdougm 						(void) printf(gettext(
51354653Sdougm 						    "Stopping group \"%s\"\n"),
51364653Sdougm 						    argv[optind]);
51374653Sdougm 				} else {
51384653Sdougm 					ret = SMF_EXIT_OK;
51394653Sdougm 				}
51404653Sdougm 				if (state != NULL)
51414653Sdougm 					sa_free_attr_string(state);
51423034Sdougm 			}
51433034Sdougm 			optind++;
51444653Sdougm 		}
51454653Sdougm 	} else {
51465331Samw 		for (group = sa_get_group(handle, NULL);
51475331Samw 		    group != NULL;
51484653Sdougm 		    group = sa_get_next_group(group)) {
51493034Sdougm 			state = sa_get_group_attr(group, "state");
51503034Sdougm 			if (state == NULL || strcmp(state, "enabled") == 0)
51515331Samw 				worklist = add_list(worklist, group, 0,
51525331Samw 				    protocol);
51533034Sdougm 			if (state != NULL)
51544653Sdougm 				sa_free_attr_string(state);
51553034Sdougm 		}
51563034Sdougm 	}
51574653Sdougm 	(void) disable_all_groups(handle, worklist, 0);
51584653Sdougm 	ret = sa_update_config(handle);
51594653Sdougm 
51603034Sdougm 	if (worklist != NULL)
51614653Sdougm 		free_list(worklist);
51623034Sdougm 	return (ret);
51633034Sdougm }
51643034Sdougm 
51653034Sdougm /*
51663034Sdougm  * remove_all_options(share, proto)
51673034Sdougm  *
51683034Sdougm  * Removes all options on a share.
51693034Sdougm  */
51703034Sdougm 
51713034Sdougm static void
remove_all_options(sa_share_t share,char * proto)51723034Sdougm remove_all_options(sa_share_t share, char *proto)
51733034Sdougm {
51743034Sdougm 	sa_optionset_t optionset;
51753034Sdougm 	sa_security_t security;
51763034Sdougm 	sa_security_t prevsec = NULL;
51773034Sdougm 
51783034Sdougm 	optionset = sa_get_optionset(share, proto);
51793034Sdougm 	if (optionset != NULL)
51804653Sdougm 		(void) sa_destroy_optionset(optionset);
51813034Sdougm 	for (security = sa_get_security(share, NULL, NULL);
51823034Sdougm 	    security != NULL;
51833034Sdougm 	    security = sa_get_next_security(security)) {
51844653Sdougm 		char *type;
51853034Sdougm 		/*
51864653Sdougm 		 * We walk through the list.  prevsec keeps the
51873034Sdougm 		 * previous security so we can delete it without
51883034Sdougm 		 * destroying the list.
51893034Sdougm 		 */
51904653Sdougm 		if (prevsec != NULL) {
51914653Sdougm 			/* remove the previously seen security */
51924653Sdougm 			(void) sa_destroy_security(prevsec);
51934653Sdougm 			/* set to NULL so we don't try multiple times */
51944653Sdougm 			prevsec = NULL;
51954653Sdougm 		}
51964653Sdougm 		type = sa_get_security_attr(security, "type");
51974653Sdougm 		if (type != NULL) {
51984653Sdougm 			/*
51994653Sdougm 			 * if the security matches the specified protocol, we
52004653Sdougm 			 * want to remove it. prevsec holds it until either
52014653Sdougm 			 * the next pass or we fall out of the loop.
52024653Sdougm 			 */
52034653Sdougm 			if (strcmp(type, proto) == 0)
52044653Sdougm 				prevsec = security;
52054653Sdougm 			sa_free_attr_string(type);
52064653Sdougm 		}
52073034Sdougm 	}
52083034Sdougm 	/* in case there is one left */
52093034Sdougm 	if (prevsec != NULL)
52104653Sdougm 		(void) sa_destroy_security(prevsec);
52113034Sdougm }
52123034Sdougm 
52133034Sdougm 
52143034Sdougm /*
52153034Sdougm  * for legacy support, we need to handle the old syntax. This is what
52163034Sdougm  * we get if sharemgr is called with the name "share" rather than
52173034Sdougm  * sharemgr.
52183034Sdougm  */
52193034Sdougm 
52203034Sdougm static int
format_legacy_path(char * buff,int buffsize,char * proto,char * cmd)52213034Sdougm format_legacy_path(char *buff, int buffsize, char *proto, char *cmd)
52223034Sdougm {
52233034Sdougm 	int err;
52243034Sdougm 
52253034Sdougm 	err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd);
52263034Sdougm 	if (err > buffsize)
52274653Sdougm 		return (-1);
52283034Sdougm 	return (0);
52293034Sdougm }
52303034Sdougm 
52313034Sdougm 
52323034Sdougm /*
52333034Sdougm  * check_legacy_cmd(proto, cmd)
52343034Sdougm  *
52353034Sdougm  * Check to see if the cmd exists in /usr/lib/fs/<proto>/<cmd> and is
52363034Sdougm  * executable.
52373034Sdougm  */
52383034Sdougm 
52393034Sdougm static int
check_legacy_cmd(char * path)52403034Sdougm check_legacy_cmd(char *path)
52413034Sdougm {
52423034Sdougm 	struct stat st;
52433034Sdougm 	int ret = 0;
52443034Sdougm 
52453034Sdougm 	if (stat(path, &st) == 0) {
52464653Sdougm 		if (S_ISREG(st.st_mode) &&
52474653Sdougm 		    st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
52484653Sdougm 			ret = 1;
52493034Sdougm 	}
52503034Sdougm 	return (ret);
52513034Sdougm }
52523034Sdougm 
52533034Sdougm /*
52543034Sdougm  * run_legacy_command(proto, cmd, argv)
52553034Sdougm  *
52564653Sdougm  * We know the command exists, so attempt to execute it with all the
52573034Sdougm  * arguments. This implements full legacy share support for those
52583034Sdougm  * protocols that don't have plugin providers.
52593034Sdougm  */
52603034Sdougm 
52613034Sdougm static int
run_legacy_command(char * path,char * argv[])52623034Sdougm run_legacy_command(char *path, char *argv[])
52633034Sdougm {
52643034Sdougm 	int ret;
52653034Sdougm 
52663034Sdougm 	ret = execv(path, argv);
52673034Sdougm 	if (ret < 0) {
52684653Sdougm 		switch (errno) {
52694653Sdougm 		case EACCES:
52704653Sdougm 			ret = SA_NO_PERMISSION;
52714653Sdougm 			break;
52724653Sdougm 		default:
52734653Sdougm 			ret = SA_SYSTEM_ERR;
52744653Sdougm 			break;
52754653Sdougm 		}
52763034Sdougm 	}
52773034Sdougm 	return (ret);
52783034Sdougm }
52793034Sdougm 
52803034Sdougm /*
52813348Sdougm  * out_share(out, group, proto)
52823034Sdougm  *
52833034Sdougm  * Display the share information in the format that the "share"
52843034Sdougm  * command has traditionally used.
52853034Sdougm  */
52863034Sdougm 
52873034Sdougm static void
out_share(FILE * out,sa_group_t group,char * proto)52883348Sdougm out_share(FILE *out, sa_group_t group, char *proto)
52893034Sdougm {
52903034Sdougm 	sa_share_t share;
52913034Sdougm 	char resfmt[128];
52925331Samw 	char *defprop;
52935331Samw 
52945331Samw 	/*
52955331Samw 	 * The original share command defaulted to displaying NFS
52965331Samw 	 * shares or allowed a protocol to be specified. We want to
52975331Samw 	 * skip those shares that are not the specified protocol.
52985331Samw 	 */
52995331Samw 	if (proto != NULL && sa_get_optionset(group, proto) == NULL)
53005331Samw 		return;
53015331Samw 
53025331Samw 	if (proto == NULL)
53035331Samw 		proto = "nfs";
53045331Samw 
53055331Samw 	/*
53065331Samw 	 * get the default property string.  NFS uses "rw" but
53075331Samw 	 * everything else will use "".
53085331Samw 	 */
53095331Samw 	if (proto != NULL && strcmp(proto, "nfs") != 0)
53105331Samw 		defprop = "\"\"";
53115331Samw 	else
53125331Samw 		defprop = "rw";
53133034Sdougm 
53144653Sdougm 	for (share = sa_get_share(group, NULL);
53154653Sdougm 	    share != NULL;
53164653Sdougm 	    share = sa_get_next_share(share)) {
53174653Sdougm 		char *path;
53184653Sdougm 		char *type;
53194653Sdougm 		char *resource;
53204653Sdougm 		char *description;
53214653Sdougm 		char *groupname;
53224653Sdougm 		char *sharedstate;
53234653Sdougm 		int shared = 1;
53244653Sdougm 		char *soptions;
53255331Samw 		char shareopts[MAXNAMLEN];
53264653Sdougm 
53274653Sdougm 		sharedstate = sa_get_share_attr(share, "shared");
53284653Sdougm 		path = sa_get_share_attr(share, "path");
53294653Sdougm 		type = sa_get_share_attr(share, "type");
53305331Samw 		resource = get_resource(share);
53314653Sdougm 		groupname = sa_get_group_attr(group, "name");
53324653Sdougm 
53334653Sdougm 		if (groupname != NULL && strcmp(groupname, "default") == 0) {
53344653Sdougm 			sa_free_attr_string(groupname);
53354653Sdougm 			groupname = NULL;
53364653Sdougm 		}
53374653Sdougm 		description = sa_get_share_description(share);
53384653Sdougm 
53395331Samw 		/*
53405331Samw 		 * Want the sharetab version if it exists, defaulting
53415331Samw 		 * to NFS if no protocol specified.
53425331Samw 		 */
53435331Samw 		(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", proto);
53445331Samw 		soptions = sa_get_share_attr(share, shareopts);
53454653Sdougm 
53464653Sdougm 		if (sharedstate == NULL)
53474653Sdougm 			shared = 0;
53484653Sdougm 
53494653Sdougm 		if (soptions == NULL)
53504653Sdougm 			soptions = sa_proto_legacy_format(proto, share, 1);
53514653Sdougm 
53524653Sdougm 		if (shared) {
53534653Sdougm 			/* only active shares go here */
53544653Sdougm 			(void) snprintf(resfmt, sizeof (resfmt), "%s%s%s",
53554653Sdougm 			    resource != NULL ? resource : "-",
53564653Sdougm 			    groupname != NULL ? "@" : "",
53574653Sdougm 			    groupname != NULL ? groupname : "");
53584653Sdougm 			(void) fprintf(out, "%-14.14s  %s   %s   \"%s\"  \n",
535911337SWilliam.Krier@Sun.COM 			    resfmt, (path != NULL) ? path : "",
53604653Sdougm 			    (soptions != NULL && strlen(soptions) > 0) ?
53615331Samw 			    soptions : defprop,
53624653Sdougm 			    (description != NULL) ? description : "");
53634653Sdougm 		}
53644653Sdougm 
53654653Sdougm 		if (path != NULL)
53664653Sdougm 			sa_free_attr_string(path);
53674653Sdougm 		if (type != NULL)
53684653Sdougm 			sa_free_attr_string(type);
53694653Sdougm 		if (resource != NULL)
53704653Sdougm 			sa_free_attr_string(resource);
53714653Sdougm 		if (groupname != NULL)
53724653Sdougm 			sa_free_attr_string(groupname);
53734653Sdougm 		if (description != NULL)
53744653Sdougm 			sa_free_share_description(description);
53754653Sdougm 		if (sharedstate != NULL)
53764653Sdougm 			sa_free_attr_string(sharedstate);
53774653Sdougm 		if (soptions != NULL)
53784653Sdougm 			sa_format_free(soptions);
53793034Sdougm 	}
53803034Sdougm }
53813034Sdougm 
53823034Sdougm /*
53833034Sdougm  * output_legacy_file(out, proto)
53843034Sdougm  *
53853034Sdougm  * Walk all of the groups for the specified protocol and call
53863034Sdougm  * out_share() to format and write in the format displayed by the
53873034Sdougm  * "share" command with no arguments.
53883034Sdougm  */
53893034Sdougm 
53903034Sdougm static void
output_legacy_file(FILE * out,char * proto,sa_handle_t handle)53913910Sdougm output_legacy_file(FILE *out, char *proto, sa_handle_t handle)
53923034Sdougm {
53933034Sdougm 	sa_group_t group;
53943034Sdougm 
53955331Samw 	for (group = sa_get_group(handle, NULL);
53965331Samw 	    group != NULL;
53974653Sdougm 	    group = sa_get_next_group(group)) {
53984653Sdougm 		char *zfs;
53993034Sdougm 
54003034Sdougm 		/*
54015331Samw 		 * Go through all the groups and ZFS
54025331Samw 		 * sub-groups. out_share() will format the shares in
54035331Samw 		 * the group appropriately.
54043034Sdougm 		 */
54053034Sdougm 
54064653Sdougm 		zfs = sa_get_group_attr(group, "zfs");
54074653Sdougm 		if (zfs != NULL) {
54084653Sdougm 			sa_group_t zgroup;
54094653Sdougm 			sa_free_attr_string(zfs);
54104653Sdougm 			for (zgroup = sa_get_sub_group(group);
54114653Sdougm 			    zgroup != NULL;
54124653Sdougm 			    zgroup = sa_get_next_group(zgroup)) {
54134653Sdougm 
54144653Sdougm 				/* got a group, so display it */
54154653Sdougm 				out_share(out, zgroup, proto);
54164653Sdougm 			}
54174653Sdougm 		} else {
54184653Sdougm 			out_share(out, group, proto);
54193034Sdougm 		}
54203034Sdougm 	}
54213034Sdougm }
54223034Sdougm 
54233034Sdougm int
sa_legacy_share(sa_handle_t handle,int flags,int argc,char * argv[])54243910Sdougm sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[])
54253034Sdougm {
54263034Sdougm 	char *protocol = "nfs";
54273034Sdougm 	char *options = NULL;
54283034Sdougm 	char *description = NULL;
54293034Sdougm 	char *groupname = NULL;
54303034Sdougm 	char *sharepath = NULL;
54313034Sdougm 	char *resource = NULL;
54323034Sdougm 	char *groupstatus = NULL;
54333034Sdougm 	int persist = SA_SHARE_TRANSIENT;
54343034Sdougm 	int argsused = 0;
54353034Sdougm 	int c;
54363034Sdougm 	int ret = SA_OK;
54373034Sdougm 	int zfs = 0;
54383034Sdougm 	int true_legacy = 0;
54393034Sdougm 	int curtype = SA_SHARE_TRANSIENT;
54403034Sdougm 	char cmd[MAXPATHLEN];
54414653Sdougm 	sa_group_t group = NULL;
54425331Samw 	sa_resource_t rsrc = NULL;
54434653Sdougm 	sa_share_t share;
54444653Sdougm 	char dir[MAXPATHLEN];
54455331Samw 	uint64_t features;
54465331Samw #ifdef lint
54475331Samw 	flags = flags;
54485331Samw #endif
54493034Sdougm 
54503034Sdougm 	while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) {
54514653Sdougm 		switch (c) {
54524653Sdougm 		case 'd':
54534653Sdougm 			description = optarg;
54544653Sdougm 			argsused++;
54554653Sdougm 			break;
54564653Sdougm 		case 'F':
54574653Sdougm 			protocol = optarg;
54584653Sdougm 			if (!sa_valid_protocol(protocol)) {
54594653Sdougm 				if (format_legacy_path(cmd, MAXPATHLEN,
54604653Sdougm 				    protocol, "share") == 0 &&
54614653Sdougm 				    check_legacy_cmd(cmd)) {
54624653Sdougm 					true_legacy++;
54634653Sdougm 				} else {
54644653Sdougm 					(void) fprintf(stderr, gettext(
54654653Sdougm 					    "Invalid protocol specified: "
54664653Sdougm 					    "%s\n"), protocol);
54674653Sdougm 					return (SA_INVALID_PROTOCOL);
54684653Sdougm 				}
54694653Sdougm 			}
54704653Sdougm 			break;
54714653Sdougm 		case 'o':
54724653Sdougm 			options = optarg;
54734653Sdougm 			argsused++;
54744653Sdougm 			break;
54754653Sdougm 		case 'p':
54764653Sdougm 			persist = SA_SHARE_PERMANENT;
54774653Sdougm 			argsused++;
54784653Sdougm 			break;
54794653Sdougm 		case 'h':
54806019Sdougm 			/* optopt on valid arg isn't defined */
54816019Sdougm 			optopt = c;
54826019Sdougm 			/*FALLTHROUGH*/
54834653Sdougm 		case '?':
54844653Sdougm 		default:
54856019Sdougm 			/*
54866019Sdougm 			 * Since a bad option gets to here, sort it
54876019Sdougm 			 * out and return a syntax error return value
54886019Sdougm 			 * if necessary.
54896019Sdougm 			 */
54906019Sdougm 			switch (optopt) {
54916019Sdougm 			default:
54926019Sdougm 				ret = SA_LEGACY_ERR;
54936019Sdougm 				break;
54946019Sdougm 			case 'h':
54956019Sdougm 			case '?':
54966019Sdougm 				break;
54976019Sdougm 			}
54984653Sdougm 			(void) fprintf(stderr, gettext("usage: %s\n"),
54994653Sdougm 			    sa_get_usage(USAGE_SHARE));
55006019Sdougm 			return (ret);
55013034Sdougm 		}
55024653Sdougm 	}
55034653Sdougm 
55044653Sdougm 	/* Have the info so construct what is needed */
55054653Sdougm 	if (!argsused && optind == argc) {
55064653Sdougm 		/* display current info in share format */
55075331Samw 		(void) output_legacy_file(stdout, protocol, handle);
55084653Sdougm 		return (ret);
55093034Sdougm 	}
55103034Sdougm 
55114653Sdougm 	/* We are modifying the configuration */
55124653Sdougm 	if (optind == argc) {
55133034Sdougm 		(void) fprintf(stderr, gettext("usage: %s\n"),
55144653Sdougm 		    sa_get_usage(USAGE_SHARE));
55153034Sdougm 		return (SA_LEGACY_ERR);
55164653Sdougm 	}
55174653Sdougm 	if (true_legacy) {
55184653Sdougm 		/* If still using legacy share/unshare, exec it */
55193034Sdougm 		ret = run_legacy_command(cmd, argv);
55203034Sdougm 		return (ret);
55214653Sdougm 	}
55224653Sdougm 
55234653Sdougm 	sharepath = argv[optind++];
55244653Sdougm 	if (optind < argc) {
55253034Sdougm 		resource = argv[optind];
55263034Sdougm 		groupname = strchr(resource, '@');
55273034Sdougm 		if (groupname != NULL)
55284653Sdougm 			*groupname++ = '\0';
55294653Sdougm 	}
55304653Sdougm 	if (realpath(sharepath, dir) == NULL)
55313034Sdougm 		ret = SA_BAD_PATH;
55324653Sdougm 	else
55333034Sdougm 		sharepath = dir;
55344653Sdougm 	if (ret == SA_OK)
55353910Sdougm 		share = sa_find_share(handle, sharepath);
55364653Sdougm 	else
55373034Sdougm 		share = NULL;
55384653Sdougm 
55395331Samw 	features = sa_proto_get_featureset(protocol);
55405331Samw 
55414653Sdougm 	if (groupname != NULL) {
55424653Sdougm 		ret = SA_NOT_ALLOWED;
55434653Sdougm 	} else if (ret == SA_OK) {
55445331Samw 		char *legacygroup;
55453034Sdougm 		/*
55464653Sdougm 		 * The legacy group is always present and zfs groups
55473034Sdougm 		 * come and go.  zfs shares may be in sub-groups and
55483034Sdougm 		 * the zfs share will already be in that group so it
55495331Samw 		 * isn't an error. If the protocol is "smb", the group
55505331Samw 		 * "smb" is used when "default" would otherwise be
55515331Samw 		 * used.  "default" is NFS only and "smb" is SMB only.
55523034Sdougm 		 */
55535331Samw 		if (strcmp(protocol, "smb") == 0)
55545331Samw 			legacygroup = "smb";
55555331Samw 		else
55565331Samw 			legacygroup = "default";
55575331Samw 
55583034Sdougm 		/*
55594653Sdougm 		 * If the share exists (not NULL), then make sure it
55604653Sdougm 		 * is one we want to handle by getting the parent
55614653Sdougm 		 * group.
55623034Sdougm 		 */
55635331Samw 		if (share != NULL) {
55644653Sdougm 			group = sa_get_parent_group(share);
55655331Samw 		} else {
55664653Sdougm 			group = sa_get_group(handle, legacygroup);
55675331Samw 			if (group == NULL && strcmp(legacygroup, "smb") == 0) {
55685331Samw 				/*
55695331Samw 				 * This group may not exist, so create
55705331Samw 				 * as necessary. It only contains the
55715331Samw 				 * "smb" protocol.
55725331Samw 				 */
55735331Samw 				group = sa_create_group(handle, legacygroup,
55745331Samw 				    &ret);
55755331Samw 				if (group != NULL)
55765331Samw 					(void) sa_create_optionset(group,
55775331Samw 					    protocol);
55785331Samw 			}
55795331Samw 		}
55805331Samw 
55815331Samw 		if (group == NULL) {
55825331Samw 			ret = SA_SYSTEM_ERR;
55835331Samw 			goto err;
55845331Samw 		}
55855331Samw 
55865331Samw 		groupstatus = group_status(group);
55875331Samw 		if (share == NULL) {
55885331Samw 			share = sa_add_share(group, sharepath,
55895331Samw 			    persist, &ret);
55905331Samw 			if (share == NULL &&
55915331Samw 			    ret == SA_DUPLICATE_NAME) {
55925331Samw 				/*
55935331Samw 				 * Could be a ZFS path being started
55945331Samw 				 */
55955331Samw 				if (sa_zfs_is_shared(handle,
55965331Samw 				    sharepath)) {
55975331Samw 					ret = SA_OK;
55985331Samw 					group = sa_get_group(handle,
55995331Samw 					    "zfs");
56005331Samw 					if (group == NULL) {
56015331Samw 						/*
56025331Samw 						 * This shouldn't
56035331Samw 						 * happen.
56045331Samw 						 */
56055331Samw 						ret = SA_CONFIG_ERR;
56065331Samw 					} else {
56075331Samw 						share = sa_add_share(
56085331Samw 						    group, sharepath,
56095331Samw 						    persist, &ret);
56104653Sdougm 					}
56113034Sdougm 				}
56125331Samw 			}
56135331Samw 		} else {
56145331Samw 			char *type;
56155331Samw 			/*
56165331Samw 			 * May want to change persist state, but the
56175331Samw 			 * important thing is to change options. We
56185331Samw 			 * need to change them regardless of the
56195331Samw 			 * source.
56205331Samw 			 */
56215331Samw 
56225331Samw 			if (sa_zfs_is_shared(handle, sharepath)) {
56235331Samw 				zfs = 1;
56245331Samw 			}
56255331Samw 			remove_all_options(share, protocol);
56265331Samw 			type = sa_get_share_attr(share, "type");
56275331Samw 			if (type != NULL &&
56285331Samw 			    strcmp(type, "transient") != 0) {
56295331Samw 				curtype = SA_SHARE_PERMANENT;
56305331Samw 			}
56315331Samw 			if (type != NULL)
56325331Samw 				sa_free_attr_string(type);
56335331Samw 			if (curtype != persist) {
56345331Samw 				(void) sa_set_share_attr(share, "type",
56355331Samw 				    persist == SA_SHARE_PERMANENT ?
56365331Samw 				    "persist" : "transient");
56375331Samw 			}
56385331Samw 		}
56395331Samw 
56405331Samw 		/*
56415331Samw 		 * If there is a resource name, we may
56425331Samw 		 * actually care about it if this is share for
56435331Samw 		 * a protocol that uses resource level sharing
56445331Samw 		 * (SMB). We need to find the resource and, if
56455331Samw 		 * it exists, make sure it belongs to the
56465331Samw 		 * current share. If it doesn't exist, attempt
56475331Samw 		 * to create it.
56485331Samw 		 */
56495331Samw 
56505331Samw 		if (ret == SA_OK && resource != NULL) {
56515331Samw 			rsrc = sa_find_resource(handle, resource);
56525331Samw 			if (rsrc != NULL) {
56535331Samw 				if (share != sa_get_resource_parent(rsrc))
56545331Samw 					ret = SA_DUPLICATE_NAME;
56555331Samw 				} else {
56565331Samw 					rsrc = sa_add_resource(share, resource,
56575331Samw 					    persist, &ret);
56583034Sdougm 				}
56595331Samw 				if (features & SA_FEATURE_RESOURCE)
56605331Samw 					share = rsrc;
56613108Sdougm 			}
56625331Samw 
56634653Sdougm 			/* Have a group to hold this share path */
56644653Sdougm 			if (ret == SA_OK && options != NULL &&
56654653Sdougm 			    strlen(options) > 0) {
56664653Sdougm 				ret = sa_parse_legacy_options(share,
56674653Sdougm 				    options,
56684653Sdougm 				    protocol);
56693034Sdougm 			}
56704653Sdougm 			if (!zfs) {
56714653Sdougm 				/*
56725331Samw 				 * ZFS shares never have a description
56735331Samw 				 * and we can't store the values so
56745331Samw 				 * don't try.
56754653Sdougm 				 */
56764653Sdougm 				if (ret == SA_OK && description != NULL)
56774653Sdougm 					ret = sa_set_share_description(share,
56784653Sdougm 					    description);
56793034Sdougm 			}
56805331Samw 			if (ret == SA_OK &&
56815331Samw 			    strcmp(groupstatus, "enabled") == 0) {
56825331Samw 				if (rsrc != share)
56834653Sdougm 					ret = sa_enable_share(share, protocol);
56845331Samw 				else
56855331Samw 					ret = sa_enable_resource(rsrc,
56865331Samw 					    protocol);
56874653Sdougm 				if (ret == SA_OK &&
56884653Sdougm 				    persist == SA_SHARE_PERMANENT) {
56894653Sdougm 					(void) sa_update_legacy(share,
56904653Sdougm 					    protocol);
56914653Sdougm 				}
56924653Sdougm 				if (ret == SA_OK)
56934653Sdougm 					ret = sa_update_config(handle);
56944653Sdougm 			}
56953034Sdougm 	}
56965331Samw err:
56973034Sdougm 	if (ret != SA_OK) {
56984653Sdougm 		(void) fprintf(stderr, gettext("Could not share: %s: %s\n"),
56994653Sdougm 		    sharepath, sa_errorstr(ret));
57004653Sdougm 		ret = SA_LEGACY_ERR;
57013034Sdougm 	}
57023034Sdougm 	return (ret);
57033034Sdougm }
57043034Sdougm 
57053034Sdougm /*
57063034Sdougm  * sa_legacy_unshare(flags, argc, argv)
57073034Sdougm  *
57083034Sdougm  * Implements the original unshare command.
57093034Sdougm  */
57103034Sdougm int
sa_legacy_unshare(sa_handle_t handle,int flags,int argc,char * argv[])57113910Sdougm sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[])
57123034Sdougm {
57133034Sdougm 	char *protocol = "nfs"; /* for now */
57143034Sdougm 	char *options = NULL;
57153034Sdougm 	char *sharepath = NULL;
57163034Sdougm 	int persist = SA_SHARE_TRANSIENT;
57173034Sdougm 	int argsused = 0;
57183034Sdougm 	int c;
57193034Sdougm 	int ret = SA_OK;
57203034Sdougm 	int true_legacy = 0;
57215331Samw 	uint64_t features = 0;
57225331Samw 	sa_resource_t resource = NULL;
57233034Sdougm 	char cmd[MAXPATHLEN];
57245331Samw #ifdef lint
57255331Samw 	flags = flags;
57265331Samw 	options = options;
57275331Samw #endif
57283034Sdougm 
57293034Sdougm 	while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) {
57304653Sdougm 		switch (c) {
57314653Sdougm 		case 'F':
57324653Sdougm 			protocol = optarg;
57334653Sdougm 			if (!sa_valid_protocol(protocol)) {
57344653Sdougm 				if (format_legacy_path(cmd, MAXPATHLEN,
57354653Sdougm 				    protocol, "unshare") == 0 &&
57364653Sdougm 				    check_legacy_cmd(cmd)) {
57374653Sdougm 					true_legacy++;
57384653Sdougm 				} else {
57394653Sdougm 					(void) printf(gettext(
57404653Sdougm 					    "Invalid file system name\n"));
57414653Sdougm 					return (SA_INVALID_PROTOCOL);
57424653Sdougm 				}
57434653Sdougm 			}
57444653Sdougm 			break;
57454653Sdougm 		case 'o':
57464653Sdougm 			options = optarg;
57474653Sdougm 			argsused++;
57484653Sdougm 			break;
57494653Sdougm 		case 'p':
57504653Sdougm 			persist = SA_SHARE_PERMANENT;
57514653Sdougm 			argsused++;
57524653Sdougm 			break;
57536019Sdougm 		case 'h':
57546019Sdougm 			/* optopt on valid arg isn't defined */
57556019Sdougm 			optopt = c;
57566019Sdougm 			/*FALLTHROUGH*/
57576019Sdougm 		case '?':
57584653Sdougm 		default:
57596019Sdougm 			/*
57606019Sdougm 			 * Since a bad option gets to here, sort it
57616019Sdougm 			 * out and return a syntax error return value
57626019Sdougm 			 * if necessary.
57636019Sdougm 			 */
57646019Sdougm 			switch (optopt) {
57656019Sdougm 			default:
57666019Sdougm 				ret = SA_LEGACY_ERR;
57676019Sdougm 				break;
57686019Sdougm 			case 'h':
57696019Sdougm 			case '?':
57706019Sdougm 				break;
57716019Sdougm 			}
57724653Sdougm 			(void) printf(gettext("usage: %s\n"),
57734653Sdougm 			    sa_get_usage(USAGE_UNSHARE));
57746019Sdougm 			return (ret);
57753034Sdougm 		}
57763034Sdougm 	}
57773034Sdougm 
57784653Sdougm 	/* Have the info so construct what is needed */
57794653Sdougm 	if (optind == argc || (optind + 1) < argc || options != NULL) {
57804653Sdougm 		ret = SA_SYNTAX_ERR;
57813034Sdougm 	} else {
57824653Sdougm 		sa_share_t share;
57834653Sdougm 		char dir[MAXPATHLEN];
57844653Sdougm 		if (true_legacy) {
57854653Sdougm 			/* if still using legacy share/unshare, exec it */
57864653Sdougm 			ret = run_legacy_command(cmd, argv);
57874653Sdougm 			return (ret);
57884653Sdougm 		}
57893663Sdougm 		/*
57903663Sdougm 		 * Find the path in the internal configuration. If it
57913663Sdougm 		 * isn't found, attempt to resolve the path via
57923663Sdougm 		 * realpath() and try again.
57933663Sdougm 		 */
57944653Sdougm 		sharepath = argv[optind++];
57954653Sdougm 		share = sa_find_share(handle, sharepath);
57964653Sdougm 		if (share == NULL) {
57974653Sdougm 			if (realpath(sharepath, dir) == NULL) {
57984653Sdougm 				ret = SA_NO_SUCH_PATH;
57994653Sdougm 			} else {
58004653Sdougm 				share = sa_find_share(handle, dir);
58014653Sdougm 			}
58023663Sdougm 		}
58035331Samw 		if (share == NULL) {
58045331Samw 			/* Could be a resource name so check that next */
58055331Samw 			features = sa_proto_get_featureset(protocol);
58065331Samw 			resource = sa_find_resource(handle, sharepath);
58075331Samw 			if (resource != NULL) {
58085331Samw 				share = sa_get_resource_parent(resource);
58095331Samw 				if (features & SA_FEATURE_RESOURCE)
58105331Samw 					(void) sa_disable_resource(resource,
58115331Samw 					    protocol);
58125331Samw 				if (persist == SA_SHARE_PERMANENT) {
58135331Samw 					ret = sa_remove_resource(resource);
58145331Samw 					if (ret == SA_OK)
58155331Samw 						ret = sa_update_config(handle);
58165331Samw 				}
58175331Samw 				/*
58185331Samw 				 * If we still have a resource on the
58195331Samw 				 * share, we don't disable the share
58205331Samw 				 * itself. IF there aren't anymore, we
58215331Samw 				 * need to remove the share. The
58225331Samw 				 * removal will be done in the next
58235331Samw 				 * section if appropriate.
58245331Samw 				 */
58255331Samw 				resource = sa_get_share_resource(share, NULL);
58265331Samw 				if (resource != NULL)
58275331Samw 					share = NULL;
58285331Samw 			} else if (ret == SA_OK) {
58295331Samw 				/* Didn't find path and no  resource */
58305331Samw 				ret = SA_BAD_PATH;
58315331Samw 			}
58325331Samw 		}
58335331Samw 		if (share != NULL && resource == NULL) {
58344653Sdougm 			ret = sa_disable_share(share, protocol);
58354653Sdougm 			/*
58364653Sdougm 			 * Errors are ok and removal should still occur. The
58374653Sdougm 			 * legacy unshare is more forgiving of errors than the
58384653Sdougm 			 * remove-share subcommand which may need the force
58394653Sdougm 			 * flag set for some error conditions. That is, the
58404653Sdougm 			 * "unshare" command will always unshare if it can
58414653Sdougm 			 * while "remove-share" might require the force option.
58424653Sdougm 			 */
58434653Sdougm 			if (persist == SA_SHARE_PERMANENT) {
58444653Sdougm 				ret = sa_remove_share(share);
58454653Sdougm 				if (ret == SA_OK)
58464653Sdougm 					ret = sa_update_config(handle);
58474653Sdougm 			}
58485331Samw 		} else if (ret == SA_OK && share == NULL && resource == NULL) {
58495331Samw 			/*
58505331Samw 			 * If both share and resource are NULL, then
58515331Samw 			 * share not found. If one or the other was
58525331Samw 			 * found or there was an earlier error, we
58535331Samw 			 * assume it was handled earlier.
58545331Samw 			 */
58554653Sdougm 			ret = SA_NOT_SHARED;
58563663Sdougm 		}
58573034Sdougm 	}
58583034Sdougm 	switch (ret) {
58593034Sdougm 	default:
58604653Sdougm 		(void) printf("%s: %s\n", sharepath, sa_errorstr(ret));
58614653Sdougm 		ret = SA_LEGACY_ERR;
58624653Sdougm 		break;
58633034Sdougm 	case SA_SYNTAX_ERR:
58644653Sdougm 		(void) printf(gettext("usage: %s\n"),
58654653Sdougm 		    sa_get_usage(USAGE_UNSHARE));
58664653Sdougm 		break;
58673034Sdougm 	case SA_OK:
58684653Sdougm 		break;
58693034Sdougm 	}
58703034Sdougm 	return (ret);
58713034Sdougm }
58723034Sdougm 
58733034Sdougm /*
58744653Sdougm  * Common commands that implement the sub-commands used by all
58755331Samw  * protocols. The entries are found via the lookup command
58763034Sdougm  */
58773034Sdougm 
58783034Sdougm static sa_command_t commands[] = {
58793034Sdougm 	{"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET},
58803034Sdougm 	{"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION},
58813034Sdougm 	{"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION},
58823034Sdougm 	{"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION},
58833034Sdougm 	{"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION},
58843034Sdougm 	{"list", 0, sa_list, USAGE_LIST},
58853034Sdougm 	{"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET},
58863034Sdougm 	{"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET},
58873034Sdougm 	{"set", 0, sa_set, USAGE_SET, SVC_SET},
58883034Sdougm 	{"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET},
58893034Sdougm 	{"show", 0, sa_show, USAGE_SHOW},
58903034Sdougm 	{"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION},
58913034Sdougm 	{"start", CMD_NODISPLAY, sa_start_group, USAGE_START,
58925331Samw 	    SVC_SET|SVC_ACTION},
58933034Sdougm 	{"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION},
58943034Sdougm 	{"unset", 0, sa_unset, USAGE_UNSET, SVC_SET},
58953034Sdougm 	{"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION},
58963034Sdougm 	{NULL, 0, NULL, NULL}
58973034Sdougm };
58983034Sdougm 
58993034Sdougm static char *
sa_get_usage(sa_usage_t index)59003034Sdougm sa_get_usage(sa_usage_t index)
59013034Sdougm {
59023034Sdougm 	char *ret = NULL;
59033034Sdougm 	switch (index) {
59043034Sdougm 	case USAGE_ADD_SHARE:
59054653Sdougm 		ret = gettext("add-share [-nth] [-r resource-name] "
59064653Sdougm 		    "[-d \"description text\"] -s sharepath group");
59074653Sdougm 		break;
59083034Sdougm 	case USAGE_CREATE:
59094653Sdougm 		ret = gettext(
59104653Sdougm 		    "create [-nvh] [-P proto [-p property=value]] group");
59114653Sdougm 		break;
59123034Sdougm 	case USAGE_DELETE:
59134653Sdougm 		ret = gettext("delete [-nvh] [-P proto] [-f] group");
59144653Sdougm 		break;
59153034Sdougm 	case USAGE_DISABLE:
59164653Sdougm 		ret = gettext("disable [-nvh] {-a | group ...}");
59174653Sdougm 		break;
59183034Sdougm 	case USAGE_ENABLE:
59194653Sdougm 		ret = gettext("enable [-nvh] {-a | group ...}");
59204653Sdougm 		break;
59213034Sdougm 	case USAGE_LIST:
59224653Sdougm 		ret = gettext("list [-vh] [-P proto]");
59234653Sdougm 		break;
59243034Sdougm 	case USAGE_MOVE_SHARE:
59254653Sdougm 		ret = gettext(
59264653Sdougm 		    "move-share [-nvh] -s sharepath destination-group");
59274653Sdougm 		break;
59283034Sdougm 	case USAGE_REMOVE_SHARE:
59295331Samw 		ret = gettext(
59305331Samw 		    "remove-share [-fnvh] {-s sharepath | -r resource} "
59315331Samw 		    "group");
59324653Sdougm 		break;
59333034Sdougm 	case USAGE_SET:
59344653Sdougm 		ret = gettext("set [-nvh] -P proto [-S optspace] "
59355331Samw 		    "[-p property=value]* [-s sharepath] [-r resource]] "
59365331Samw 		    "group");
59374653Sdougm 		break;
59383034Sdougm 	case USAGE_SET_SECURITY:
59394653Sdougm 		ret = gettext("set-security [-nvh] -P proto -S security-type "
59404653Sdougm 		    "[-p property=value]* group");
59414653Sdougm 		break;
59423034Sdougm 	case USAGE_SET_SHARE:
59434653Sdougm 		ret = gettext("set-share [-nh] [-r resource] "
59444653Sdougm 		    "[-d \"description text\"] -s sharepath group");
59454653Sdougm 		break;
59463034Sdougm 	case USAGE_SHOW:
59474653Sdougm 		ret = gettext("show [-pvxh] [-P proto] [group ...]");
59484653Sdougm 		break;
59493034Sdougm 	case USAGE_SHARE:
59504653Sdougm 		ret = gettext("share [-F fstype] [-p] [-o optionlist]"
59514653Sdougm 		    "[-d description] [pathname [resourcename]]");
59524653Sdougm 		break;
59533034Sdougm 	case USAGE_START:
59544653Sdougm 		ret = gettext("start [-vh] [-P proto] {-a | group ...}");
59554653Sdougm 		break;
59563034Sdougm 	case USAGE_STOP:
59574653Sdougm 		ret = gettext("stop [-vh] [-P proto] {-a | group ...}");
59584653Sdougm 		break;
59593034Sdougm 	case USAGE_UNSET:
59604653Sdougm 		ret = gettext("unset [-nvh] -P proto [-S optspace] "
59614653Sdougm 		    "[-p property]* group");
59624653Sdougm 		break;
59633034Sdougm 	case USAGE_UNSET_SECURITY:
59645331Samw 		ret = gettext("unset-security [-nvh] -P proto "
59655331Samw 		    "-S security-type [-p property]* group");
59664653Sdougm 		break;
59673034Sdougm 	case USAGE_UNSHARE:
59684653Sdougm 		ret = gettext(
59695331Samw 		    "unshare [-F fstype] [-p] [-o optionlist] sharepath");
59704653Sdougm 		break;
59713034Sdougm 	}
59723034Sdougm 	return (ret);
59733034Sdougm }
59743034Sdougm 
59753034Sdougm /*
59763034Sdougm  * sa_lookup(cmd, proto)
59773034Sdougm  *
59783034Sdougm  * Lookup the sub-command. proto isn't currently used, but it may
59793034Sdougm  * eventually provide a way to provide protocol specific sub-commands.
59803034Sdougm  */
59813034Sdougm sa_command_t *
sa_lookup(char * cmd,char * proto)59823034Sdougm sa_lookup(char *cmd, char *proto)
59833034Sdougm {
59843034Sdougm 	int i;
59853034Sdougm 	size_t len;
59865331Samw #ifdef lint
59875331Samw 	proto = proto;
59885331Samw #endif
59893034Sdougm 
59903034Sdougm 	len = strlen(cmd);
59913034Sdougm 	for (i = 0; commands[i].cmdname != NULL; i++) {
59924653Sdougm 		if (strncmp(cmd, commands[i].cmdname, len) == 0)
59934653Sdougm 			return (&commands[i]);
59943034Sdougm 	}
59953034Sdougm 	return (NULL);
59963034Sdougm }
59973034Sdougm 
59983034Sdougm void
sub_command_help(char * proto)59993034Sdougm sub_command_help(char *proto)
60003034Sdougm {
60013034Sdougm 	int i;
60025331Samw #ifdef lint
60035331Samw 	proto = proto;
60045331Samw #endif
60053034Sdougm 
60063034Sdougm 	(void) printf(gettext("\tsub-commands:\n"));
60073034Sdougm 	for (i = 0; commands[i].cmdname != NULL; i++) {
60084653Sdougm 		if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY)))
60094653Sdougm 			(void) printf("\t%s\n",
60104653Sdougm 			    sa_get_usage((sa_usage_t)commands[i].cmdidx));
60113034Sdougm 	}
60123034Sdougm }
6013