13034Sdougm /* 23034Sdougm * CDDL HEADER START 33034Sdougm * 43034Sdougm * The contents of this file are subject to the terms of the 53034Sdougm * Common Development and Distribution License (the "License"). 63034Sdougm * You may not use this file except in compliance with the License. 73034Sdougm * 83034Sdougm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 93034Sdougm * or http://www.opensolaris.org/os/licensing. 103034Sdougm * See the License for the specific language governing permissions 113034Sdougm * and limitations under the License. 123034Sdougm * 133034Sdougm * When distributing Covered Code, include this CDDL HEADER in each 143034Sdougm * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 153034Sdougm * If applicable, add the following below this CDDL HEADER, with the 163034Sdougm * fields enclosed by brackets "[]" replaced with your own identifying 173034Sdougm * information: Portions Copyright [yyyy] [name of copyright owner] 183034Sdougm * 193034Sdougm * CDDL HEADER END 203034Sdougm */ 213034Sdougm 223034Sdougm /* 235772Sas200622 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 243034Sdougm * Use is subject to license terms. 253034Sdougm */ 263034Sdougm 273034Sdougm #pragma ident "%Z%%M% %I% %E% SMI" 283034Sdougm 293034Sdougm #include <sys/types.h> 303034Sdougm #include <sys/stat.h> 313034Sdougm #include <fcntl.h> 323034Sdougm #include <stdlib.h> 333034Sdougm #include <stdio.h> 343034Sdougm #include <string.h> 353034Sdougm #include <ctype.h> 363034Sdougm #include <unistd.h> 373034Sdougm #include <getopt.h> 383034Sdougm #include <utmpx.h> 393034Sdougm #include <pwd.h> 403034Sdougm #include <auth_attr.h> 413034Sdougm #include <secdb.h> 423034Sdougm #include <sys/param.h> 433034Sdougm #include <sys/stat.h> 443034Sdougm #include <errno.h> 453034Sdougm 463034Sdougm #include <libshare.h> 473034Sdougm #include "sharemgr.h" 483034Sdougm #include <libscf.h> 493034Sdougm #include <libxml/tree.h> 503034Sdougm #include <libintl.h> 515331Samw #include <assert.h> 525331Samw #include <iconv.h> 535331Samw #include <langinfo.h> 545331Samw #include <dirent.h> 553034Sdougm 563034Sdougm static char *sa_get_usage(sa_usage_t); 573034Sdougm 583034Sdougm /* 593034Sdougm * Implementation of the common sub-commands supported by sharemgr. 603034Sdougm * A number of helper functions are also included. 613034Sdougm */ 623034Sdougm 633034Sdougm /* 643034Sdougm * has_protocol(group, proto) 653034Sdougm * If the group has an optionset with the specified protocol, 663034Sdougm * return true (1) otherwise false (0). 673034Sdougm */ 683034Sdougm static int 693034Sdougm has_protocol(sa_group_t group, char *protocol) 703034Sdougm { 713034Sdougm sa_optionset_t optionset; 723034Sdougm int result = 0; 733034Sdougm 743034Sdougm optionset = sa_get_optionset(group, protocol); 753034Sdougm if (optionset != NULL) { 764653Sdougm result++; 773034Sdougm } 783034Sdougm return (result); 793034Sdougm } 803034Sdougm 813034Sdougm /* 825331Samw * validresource(name) 835331Samw * 845331Samw * Check that name only has valid characters in it. The current valid 855331Samw * set are the printable characters but not including: 865331Samw * " / \ [ ] : | < > + ; , ? * = \t 875331Samw * Note that space is included and there is a maximum length. 885331Samw */ 895331Samw static int 905331Samw validresource(const char *name) 915331Samw { 925331Samw const char *cp; 935331Samw size_t len; 945331Samw 955331Samw if (name == NULL) 965331Samw return (B_FALSE); 975331Samw 985331Samw len = strlen(name); 995331Samw if (len == 0 || len > SA_MAX_RESOURCE_NAME) 1005331Samw return (B_FALSE); 1015331Samw 1025331Samw if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) { 1035331Samw return (B_FALSE); 1045331Samw } 1055331Samw 1065331Samw for (cp = name; *cp != '\0'; cp++) 1075331Samw if (iscntrl(*cp)) 1085331Samw return (B_FALSE); 1095331Samw 1105331Samw return (B_TRUE); 1115331Samw } 1125331Samw 1135331Samw /* 1145331Samw * conv_to_utf8(input) 1155331Samw * 1165331Samw * Convert the input string to utf8 from the current locale. If the 1175331Samw * conversion fails, use the current locale, it is likely close 1185331Samw * enough. For example, the "C" locale is a subset of utf-8. The 1195331Samw * return value may be a new string or the original input string. 1205331Samw */ 1215331Samw 1225331Samw static char * 1235331Samw conv_to_utf8(char *input) 1245331Samw { 1255331Samw iconv_t cd; 1265521Sas200622 char *inval = input; 1275331Samw char *output = input; 1285331Samw char *outleft; 1295331Samw char *curlocale; 1305331Samw size_t bytesleft; 1315331Samw size_t size; 1325331Samw size_t osize; 1335331Samw static int warned = 0; 1345331Samw 1355331Samw curlocale = nl_langinfo(CODESET); 1365331Samw if (curlocale == NULL) 1375331Samw curlocale = "C"; 1385331Samw cd = iconv_open("UTF-8", curlocale); 1395331Samw if (cd != NULL && cd != (iconv_t)-1) { 1405331Samw size = strlen(input); 1415331Samw /* Assume worst case of characters expanding to 4 bytes. */ 1425331Samw bytesleft = size * 4; 1435331Samw output = calloc(bytesleft, 1); 1445331Samw if (output != NULL) { 1455331Samw outleft = output; 1465521Sas200622 /* inval can be modified on return */ 1475521Sas200622 osize = iconv(cd, (const char **)&inval, &size, 1485331Samw &outleft, &bytesleft); 1495331Samw if (osize == (size_t)-1 || size != 0) { 1505331Samw free(output); 1515331Samw output = input; 1525331Samw } 1535521Sas200622 } else { 1545521Sas200622 /* Need to return something. */ 1555521Sas200622 output = input; 1565331Samw } 1575331Samw (void) iconv_close(cd); 1585331Samw } else { 1595331Samw if (!warned) 1605331Samw (void) fprintf(stderr, 1615331Samw gettext("Cannot convert to UTF-8 from %s\n"), 1625331Samw curlocale ? curlocale : gettext("unknown")); 1635331Samw warned = 1; 1645331Samw } 1655331Samw return (output); 1665331Samw } 1675331Samw 1685331Samw /* 1695331Samw * conv_from(input) 1705331Samw * 1715331Samw * Convert the input string from utf8 to current locale. If the 1725331Samw * conversion isn't supported, just use as is. The return value may be 1735331Samw * a new string or the original input string. 1745331Samw */ 1755331Samw 1765331Samw static char * 1775331Samw conv_from_utf8(char *input) 1785331Samw { 1795331Samw iconv_t cd; 1805331Samw char *output = input; 1815521Sas200622 char *inval = input; 1825331Samw char *outleft; 1835331Samw char *curlocale; 1845331Samw size_t bytesleft; 1855331Samw size_t size; 1865331Samw size_t osize; 1875331Samw static int warned = 0; 1885331Samw 1895331Samw curlocale = nl_langinfo(CODESET); 1905331Samw if (curlocale == NULL) 1915331Samw curlocale = "C"; 1925331Samw cd = iconv_open(curlocale, "UTF-8"); 1935331Samw if (cd != NULL && cd != (iconv_t)-1) { 1945331Samw size = strlen(input); 1955331Samw /* Assume worst case of characters expanding to 4 bytes. */ 1965331Samw bytesleft = size * 4; 1975331Samw output = calloc(bytesleft, 1); 1985331Samw if (output != NULL) { 1995331Samw outleft = output; 2005521Sas200622 osize = iconv(cd, (const char **)&inval, &size, 2015331Samw &outleft, &bytesleft); 2025521Sas200622 if (osize == (size_t)-1 || size != 0) 2035331Samw output = input; 2045521Sas200622 } else { 2055521Sas200622 /* Need to return something. */ 2065521Sas200622 output = input; 2075331Samw } 2085331Samw (void) iconv_close(cd); 2095331Samw } else { 2105331Samw if (!warned) 2115331Samw (void) fprintf(stderr, 2125331Samw gettext("Cannot convert to %s from UTF-8\n"), 2135331Samw curlocale ? curlocale : gettext("unknown")); 2145331Samw warned = 1; 2155331Samw } 2165331Samw return (output); 2175331Samw } 2185331Samw 2195885Sdougm /* 2205885Sdougm * print_rsrc_desc(resource, sharedesc) 2215885Sdougm * 2225885Sdougm * Print the resource description string after converting from UTF8 to 2235885Sdougm * the current locale. If sharedesc is not NULL and there is no 2245885Sdougm * description on the resource, use sharedesc. sharedesc will already 2255885Sdougm * be converted to UTF8. 2265885Sdougm */ 2275885Sdougm 2285331Samw static void 2295885Sdougm print_rsrc_desc(sa_resource_t resource, char *sharedesc) 2305331Samw { 2315331Samw char *description; 2325331Samw char *desc; 2335331Samw 2345885Sdougm if (resource == NULL) 2355885Sdougm return; 2365885Sdougm 2375331Samw description = sa_get_resource_description(resource); 2385331Samw if (description != NULL) { 2395331Samw desc = conv_from_utf8(description); 2405331Samw if (desc != description) { 2415331Samw sa_free_share_description(description); 2425331Samw description = desc; 2435331Samw } 2445885Sdougm } else if (sharedesc != NULL) { 2455885Sdougm description = strdup(sharedesc); 2465885Sdougm } 2475885Sdougm if (description != NULL) { 2485331Samw (void) printf("\t\"%s\"", description); 2495331Samw sa_free_share_description(description); 2505331Samw } 2515331Samw } 2525331Samw 2535885Sdougm /* 2545885Sdougm * set_resource_desc(share, description) 2555885Sdougm * 2565885Sdougm * Set the share description value after converting the description 2575885Sdougm * string to UTF8 from the current locale. 2585885Sdougm */ 2595885Sdougm 2605885Sdougm static int 2615885Sdougm set_resource_desc(sa_share_t share, char *description) 2625885Sdougm { 2635885Sdougm char *desc; 2645885Sdougm int ret; 2655885Sdougm 2665885Sdougm desc = conv_to_utf8(description); 2675885Sdougm ret = sa_set_resource_description(share, desc); 2685885Sdougm if (description != desc) 2695885Sdougm sa_free_share_description(desc); 2705885Sdougm return (ret); 2715885Sdougm } 2725885Sdougm 2735885Sdougm /* 2745885Sdougm * set_share_desc(share, description) 2755885Sdougm * 2765885Sdougm * Set the resource description value after converting the description 2775885Sdougm * string to UTF8 from the current locale. 2785885Sdougm */ 2795885Sdougm 2805331Samw static int 2815331Samw set_share_desc(sa_share_t share, char *description) 2825331Samw { 2835331Samw char *desc; 2845331Samw int ret; 2855331Samw 2865331Samw desc = conv_to_utf8(description); 2875331Samw ret = sa_set_share_description(share, desc); 2885331Samw if (description != desc) 2895331Samw sa_free_share_description(desc); 2905331Samw return (ret); 2915331Samw } 2925331Samw 2935331Samw /* 2945331Samw * add_list(list, item, data, proto) 2955331Samw * Adds a new list member that points holds item in the list. 2963034Sdougm * If list is NULL, it starts a new list. The function returns 2973034Sdougm * the first member of the list. 2983034Sdougm */ 2993034Sdougm struct list * 3005331Samw add_list(struct list *listp, void *item, void *data, char *proto) 3013034Sdougm { 3023034Sdougm struct list *new, *tmp; 3033034Sdougm 3043034Sdougm new = malloc(sizeof (struct list)); 3053034Sdougm if (new != NULL) { 3064653Sdougm new->next = NULL; 3074653Sdougm new->item = item; 3084653Sdougm new->itemdata = data; 3095331Samw new->proto = proto; 3103034Sdougm } else { 3114653Sdougm return (listp); 3123034Sdougm } 3133034Sdougm 3143034Sdougm if (listp == NULL) 3154653Sdougm return (new); 3163034Sdougm 3173034Sdougm for (tmp = listp; tmp->next != NULL; tmp = tmp->next) { 3183034Sdougm /* get to end of list */ 3193034Sdougm } 3203034Sdougm tmp->next = new; 3213034Sdougm return (listp); 3223034Sdougm } 3233034Sdougm 3243034Sdougm /* 3253034Sdougm * free_list(list) 3263034Sdougm * Given a list, free all the members of the list; 3273034Sdougm */ 3283034Sdougm static void 3293034Sdougm free_list(struct list *listp) 3303034Sdougm { 3313034Sdougm struct list *tmp; 3323034Sdougm while (listp != NULL) { 3334653Sdougm tmp = listp; 3344653Sdougm listp = listp->next; 3354653Sdougm free(tmp); 3363034Sdougm } 3373034Sdougm } 3383034Sdougm 3393034Sdougm /* 3403034Sdougm * check_authorization(instname, which) 3413034Sdougm * 3423034Sdougm * Checks to see if the specific type of authorization in which is 3433034Sdougm * enabled for the user in this SMF service instance. 3443034Sdougm */ 3453034Sdougm 3463034Sdougm static int 3473034Sdougm check_authorization(char *instname, int which) 3483034Sdougm { 3493034Sdougm scf_handle_t *handle = NULL; 3503034Sdougm scf_simple_prop_t *prop = NULL; 3513034Sdougm char svcstring[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1]; 3523034Sdougm char *authstr = NULL; 3533034Sdougm ssize_t numauths; 3544653Sdougm int ret = B_TRUE; 3553034Sdougm uid_t uid; 3563034Sdougm struct passwd *pw = NULL; 3573034Sdougm 3583034Sdougm uid = getuid(); 3593034Sdougm pw = getpwuid(uid); 3604653Sdougm if (pw == NULL) { 3614653Sdougm ret = B_FALSE; 3624653Sdougm } else { 3634653Sdougm /* 3644653Sdougm * Since names are restricted to SA_MAX_NAME_LEN won't 3654653Sdougm * overflow. 3664653Sdougm */ 3674653Sdougm (void) snprintf(svcstring, sizeof (svcstring), "%s:%s", 3684653Sdougm SA_SVC_FMRI_BASE, instname); 3694653Sdougm handle = scf_handle_create(SCF_VERSION); 3704653Sdougm if (handle != NULL) { 3714653Sdougm if (scf_handle_bind(handle) == 0) { 3724653Sdougm switch (which) { 3734653Sdougm case SVC_SET: 3744653Sdougm prop = scf_simple_prop_get(handle, 3754653Sdougm svcstring, "general", 3764653Sdougm SVC_AUTH_VALUE); 3774653Sdougm break; 3784653Sdougm case SVC_ACTION: 3794653Sdougm prop = scf_simple_prop_get(handle, 3804653Sdougm svcstring, "general", 3814653Sdougm SVC_AUTH_ACTION); 3824653Sdougm break; 3834653Sdougm } 3844653Sdougm } 3853034Sdougm } 3863034Sdougm } 3873034Sdougm /* make sure we have an authorization string property */ 3883034Sdougm if (prop != NULL) { 3894653Sdougm int i; 3904653Sdougm numauths = scf_simple_prop_numvalues(prop); 3914653Sdougm for (ret = 0, i = 0; i < numauths; i++) { 3924653Sdougm authstr = scf_simple_prop_next_astring(prop); 3934653Sdougm if (authstr != NULL) { 3944653Sdougm /* check if this user has one of the strings */ 3954653Sdougm if (chkauthattr(authstr, pw->pw_name)) { 3964653Sdougm ret = 1; 3974653Sdougm break; 3984653Sdougm } 3994653Sdougm } 4003034Sdougm } 4014653Sdougm endauthattr(); 4024653Sdougm scf_simple_prop_free(prop); 4033034Sdougm } else { 4044653Sdougm /* no authorization string defined */ 4054653Sdougm ret = 0; 4063034Sdougm } 4073034Sdougm if (handle != NULL) 4084653Sdougm scf_handle_destroy(handle); 4093034Sdougm return (ret); 4103034Sdougm } 4113034Sdougm 4123034Sdougm /* 4133034Sdougm * check_authorizations(instname, flags) 4143034Sdougm * 4153034Sdougm * check all the needed authorizations for the user in this service 4163034Sdougm * instance. Return value of 1(true) or 0(false) indicates whether 4173034Sdougm * there are authorizations for the user or not. 4183034Sdougm */ 4193034Sdougm 4203034Sdougm static int 4213034Sdougm check_authorizations(char *instname, int flags) 4223034Sdougm { 4233034Sdougm int ret1 = 0; 4243034Sdougm int ret2 = 0; 4253034Sdougm int ret; 4263034Sdougm 4273034Sdougm if (flags & SVC_SET) 4284653Sdougm ret1 = check_authorization(instname, SVC_SET); 4293034Sdougm if (flags & SVC_ACTION) 4304653Sdougm ret2 = check_authorization(instname, SVC_ACTION); 4313034Sdougm switch (flags) { 4323034Sdougm case SVC_ACTION: 4334653Sdougm ret = ret2; 4344653Sdougm break; 4353034Sdougm case SVC_SET: 4364653Sdougm ret = ret1; 4374653Sdougm break; 4383034Sdougm case SVC_ACTION|SVC_SET: 4394653Sdougm ret = ret1 & ret2; 4404653Sdougm break; 4413034Sdougm default: 4424653Sdougm /* if not flags set, we assume we don't need authorizations */ 4434653Sdougm ret = 1; 4443034Sdougm } 4453034Sdougm return (ret); 4463034Sdougm } 4473034Sdougm 4483034Sdougm /* 4495331Samw * notify_or_enable_share(share, protocol) 4505331Samw * 4515331Samw * Since some protocols don't want an "enable" when properties change, 4525331Samw * this function will use the protocol specific notify function 4535331Samw * first. If that fails, it will then attempt to use the 4545331Samw * sa_enable_share(). "protocol" is the protocol that was specified 4555331Samw * on the command line. 4565331Samw */ 4575331Samw static void 4585331Samw notify_or_enable_share(sa_share_t share, char *protocol) 4595331Samw { 4605331Samw sa_group_t group; 4615331Samw sa_optionset_t opt; 4625331Samw int ret = SA_OK; 4635331Samw char *path; 4645331Samw char *groupproto; 4655331Samw sa_share_t parent = share; 4665331Samw 4675331Samw /* If really a resource, get parent share */ 4685331Samw if (!sa_is_share(share)) { 4695331Samw parent = sa_get_resource_parent((sa_resource_t)share); 4705331Samw } 4715331Samw 4725331Samw /* 4735331Samw * Now that we've got a share in "parent", make sure it has a path. 4745331Samw */ 4755331Samw path = sa_get_share_attr(parent, "path"); 4765331Samw if (path == NULL) 4775331Samw return; 4785331Samw 4795331Samw group = sa_get_parent_group(parent); 4805331Samw 4815331Samw if (group == NULL) { 4825331Samw sa_free_attr_string(path); 4835331Samw return; 4845331Samw } 4855331Samw for (opt = sa_get_optionset(group, NULL); 4865331Samw opt != NULL; 4875331Samw opt = sa_get_next_optionset(opt)) { 4885331Samw groupproto = sa_get_optionset_attr(opt, "type"); 4895331Samw if (groupproto == NULL || 4905331Samw (protocol != NULL && strcmp(groupproto, protocol) != 0)) { 4915331Samw sa_free_attr_string(groupproto); 4925331Samw continue; 4935331Samw } 4945331Samw if (sa_is_share(share)) { 4955331Samw if ((ret = sa_proto_change_notify(share, 4965331Samw groupproto)) != SA_OK) { 4975331Samw ret = sa_enable_share(share, groupproto); 4985331Samw if (ret != SA_OK) { 4995331Samw (void) printf( 5005331Samw gettext("Could not reenable" 5015331Samw " share %s: %s\n"), 5025331Samw path, sa_errorstr(ret)); 5035331Samw } 5045331Samw } 5055331Samw } else { 5065331Samw /* Must be a resource */ 5075331Samw if ((ret = sa_proto_notify_resource(share, 5085331Samw groupproto)) != SA_OK) { 5095331Samw ret = sa_enable_resource(share, groupproto); 5105331Samw if (ret != SA_OK) { 5115331Samw (void) printf( 5125331Samw gettext("Could not " 5135331Samw "reenable resource %s: " 5145331Samw "%s\n"), path, 5155331Samw sa_errorstr(ret)); 5165331Samw } 5175331Samw } 5185331Samw } 5195331Samw sa_free_attr_string(groupproto); 5205331Samw } 5215331Samw sa_free_attr_string(path); 5225331Samw } 5235331Samw 5245331Samw /* 5255331Samw * enable_group(group, updateproto, notify, proto) 5263082Sdougm * 5273082Sdougm * enable all the shares in the specified group. This is a helper for 5283082Sdougm * enable_all_groups in order to simplify regular and subgroup (zfs) 5295331Samw * enabling. Group has already been checked for non-NULL. If notify 5305331Samw * is non-zero, attempt to use the notify interface rather than 5315331Samw * enable. 5323082Sdougm */ 5333082Sdougm static void 5345331Samw enable_group(sa_group_t group, char *updateproto, int notify, char *proto) 5353082Sdougm { 5363082Sdougm sa_share_t share; 5373082Sdougm 5383082Sdougm for (share = sa_get_share(group, NULL); 5393082Sdougm share != NULL; 5403082Sdougm share = sa_get_next_share(share)) { 5414653Sdougm if (updateproto != NULL) 5424653Sdougm (void) sa_update_legacy(share, updateproto); 5435331Samw if (notify) 5445331Samw notify_or_enable_share(share, proto); 5455331Samw else 5465331Samw (void) sa_enable_share(share, proto); 5473082Sdougm } 5483082Sdougm } 5493082Sdougm 5503082Sdougm /* 5514241Sdougm * isenabled(group) 5524241Sdougm * 5534241Sdougm * Returns B_TRUE if the group is enabled or B_FALSE if it isn't. 5544241Sdougm * Moved to separate function to reduce clutter in the code. 5554241Sdougm */ 5564241Sdougm 5574241Sdougm static int 5584241Sdougm isenabled(sa_group_t group) 5594241Sdougm { 5604241Sdougm char *state; 5614241Sdougm int ret = B_FALSE; 5624241Sdougm 5634241Sdougm if (group != NULL) { 5644653Sdougm state = sa_get_group_attr(group, "state"); 5654653Sdougm if (state != NULL) { 5665331Samw 5674653Sdougm if (strcmp(state, "enabled") == 0) 5684653Sdougm ret = B_TRUE; 5694653Sdougm sa_free_attr_string(state); 5704653Sdougm } 5714241Sdougm } 5724241Sdougm return (ret); 5734241Sdougm } 5744241Sdougm 5754241Sdougm /* 5763082Sdougm * enable_all_groups(list, setstate, online, updateproto) 5775331Samw * 5785331Samw * Given a list of groups, enable each one found. If updateproto is 5795331Samw * not NULL, then update all the shares for the protocol that was 5805331Samw * passed in. If enable is non-zero, tell enable_group to try the 5815331Samw * notify interface since this is a property change. 5823034Sdougm */ 5833034Sdougm static int 5843910Sdougm enable_all_groups(sa_handle_t handle, struct list *work, int setstate, 5855331Samw int online, char *updateproto, int enable) 5863034Sdougm { 5874241Sdougm int ret; 5883034Sdougm char instance[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1]; 5893034Sdougm char *state; 5903034Sdougm char *name; 5913034Sdougm char *zfs = NULL; 5923034Sdougm sa_group_t group; 5933082Sdougm sa_group_t subgroup; 5943034Sdougm 5954241Sdougm for (ret = SA_OK; work != NULL; work = work->next) { 5964653Sdougm group = (sa_group_t)work->item; 5974241Sdougm 5984241Sdougm /* 5994241Sdougm * If setstate == TRUE, then make sure to set 6004241Sdougm * enabled. This needs to be done here in order for 6014241Sdougm * the isenabled check to succeed on a newly enabled 6024241Sdougm * group. 6034241Sdougm */ 6044653Sdougm if (setstate == B_TRUE) { 6054653Sdougm ret = sa_set_group_attr(group, "state", "enabled"); 6064653Sdougm if (ret != SA_OK) 6074653Sdougm break; 6084653Sdougm } 6094241Sdougm 6104241Sdougm /* 6114241Sdougm * Check to see if group is enabled. If it isn't, skip 6124241Sdougm * the rest. We don't want shares starting if the 6134241Sdougm * group is disabled. The properties may have been 6144241Sdougm * updated, but there won't be a change until the 6154241Sdougm * group is enabled. 6164241Sdougm */ 6174653Sdougm if (!isenabled(group)) 6184653Sdougm continue; 6194653Sdougm 6204653Sdougm /* if itemdata != NULL then a single share */ 6214653Sdougm if (work->itemdata != NULL) { 6225331Samw if (enable) { 6235331Samw if (work->itemdata != NULL) 6245331Samw notify_or_enable_share(work->itemdata, 6255331Samw updateproto); 6265331Samw else 6275331Samw ret = SA_CONFIG_ERR; 6285331Samw } else { 6295331Samw if (sa_is_share(work->itemdata)) { 6305331Samw ret = sa_enable_share( 6315331Samw (sa_share_t)work->itemdata, 6325331Samw updateproto); 6335331Samw } else { 6345331Samw ret = sa_enable_resource( 6355331Samw (sa_resource_t)work->itemdata, 6365331Samw updateproto); 6375331Samw } 6385331Samw } 6393034Sdougm } 6404653Sdougm if (ret != SA_OK) 6414653Sdougm break; 6424653Sdougm 6434653Sdougm /* if itemdata == NULL then the whole group */ 6444653Sdougm if (work->itemdata == NULL) { 6454653Sdougm zfs = sa_get_group_attr(group, "zfs"); 6464653Sdougm /* 6475331Samw * If the share is managed by ZFS, don't 6484653Sdougm * update any of the protocols since ZFS is 6495331Samw * handling this. Updateproto will contain 6504653Sdougm * the name of the protocol that we want to 6514653Sdougm * update legacy files for. 6524653Sdougm */ 6535331Samw enable_group(group, zfs == NULL ? updateproto : NULL, 6545331Samw enable, work->proto); 6554653Sdougm for (subgroup = sa_get_sub_group(group); 6564653Sdougm subgroup != NULL; 6574653Sdougm subgroup = sa_get_next_group(subgroup)) { 6584653Sdougm /* never update legacy for ZFS subgroups */ 6595331Samw enable_group(subgroup, NULL, enable, 6605331Samw work->proto); 6613034Sdougm } 6623034Sdougm } 6634653Sdougm if (online) { 6644653Sdougm zfs = sa_get_group_attr(group, "zfs"); 6654653Sdougm name = sa_get_group_attr(group, "name"); 6664653Sdougm if (name != NULL) { 6674653Sdougm if (zfs == NULL) { 6684653Sdougm (void) snprintf(instance, 6694653Sdougm sizeof (instance), "%s:%s", 6704653Sdougm SA_SVC_FMRI_BASE, name); 6714653Sdougm state = smf_get_state(instance); 6724653Sdougm if (state == NULL || 6734653Sdougm strcmp(state, "online") != 0) { 6744653Sdougm (void) smf_enable_instance( 6754653Sdougm instance, 0); 6764653Sdougm free(state); 6774653Sdougm } 6784653Sdougm } else { 6794653Sdougm sa_free_attr_string(zfs); 6804653Sdougm zfs = NULL; 6814653Sdougm } 6824653Sdougm if (name != NULL) 6834653Sdougm sa_free_attr_string(name); 6844653Sdougm } 6854653Sdougm } 6863034Sdougm } 6873034Sdougm if (ret == SA_OK) { 6884653Sdougm ret = sa_update_config(handle); 6893034Sdougm } 6903034Sdougm return (ret); 6913034Sdougm } 6923034Sdougm 6933034Sdougm /* 6943034Sdougm * chk_opt(optlistp, security, proto) 6953034Sdougm * 6963034Sdougm * Do a sanity check on the optlist provided for the protocol. This 6973034Sdougm * is a syntax check and verification that the property is either a 6983034Sdougm * general or specific to a names optionset. 6993034Sdougm */ 7003034Sdougm 7013034Sdougm static int 7023034Sdougm chk_opt(struct options *optlistp, int security, char *proto) 7033034Sdougm { 7043034Sdougm struct options *optlist; 7053034Sdougm char *sep = ""; 7063034Sdougm int notfirst = 0; 7073034Sdougm int ret; 7083034Sdougm 7093034Sdougm for (optlist = optlistp; optlist != NULL; optlist = optlist->next) { 7104653Sdougm char *optname; 7114653Sdougm 7124653Sdougm optname = optlist->optname; 7134653Sdougm ret = OPT_ADD_OK; 7144653Sdougm /* extract property/value pair */ 7154653Sdougm if (sa_is_security(optname, proto)) { 7164653Sdougm if (!security) 7174653Sdougm ret = OPT_ADD_SECURITY; 7184653Sdougm } else { 7194653Sdougm if (security) 7204653Sdougm ret = OPT_ADD_PROPERTY; 7214653Sdougm } 7224653Sdougm if (ret != OPT_ADD_OK) { 7234653Sdougm if (notfirst == 0) 7244653Sdougm (void) printf( 7254653Sdougm gettext("Property syntax error: ")); 7264653Sdougm switch (ret) { 7274653Sdougm case OPT_ADD_SYNTAX: 7284653Sdougm (void) printf(gettext("%ssyntax error: %s"), 7293034Sdougm sep, optname); 7304653Sdougm sep = ", "; 7314653Sdougm break; 7324653Sdougm case OPT_ADD_SECURITY: 7334653Sdougm (void) printf(gettext("%s%s requires -S"), 7343034Sdougm optname, sep); 7354653Sdougm sep = ", "; 7364653Sdougm break; 7374653Sdougm case OPT_ADD_PROPERTY: 7384653Sdougm (void) printf( 7394653Sdougm gettext("%s%s not supported with -S"), 7403034Sdougm optname, sep); 7414653Sdougm sep = ", "; 7424653Sdougm break; 7434653Sdougm } 7444653Sdougm notfirst++; 7453034Sdougm } 7463034Sdougm } 7473034Sdougm if (notfirst) { 7484653Sdougm (void) printf("\n"); 7494653Sdougm ret = SA_SYNTAX_ERR; 7503034Sdougm } 7513034Sdougm return (ret); 7523034Sdougm } 7533034Sdougm 7543034Sdougm /* 7553034Sdougm * free_opt(optlist) 7563034Sdougm * Free the specified option list. 7573034Sdougm */ 7583034Sdougm static void 7593034Sdougm free_opt(struct options *optlist) 7603034Sdougm { 7613034Sdougm struct options *nextopt; 7623034Sdougm while (optlist != NULL) { 7633034Sdougm nextopt = optlist->next; 7643034Sdougm free(optlist); 7653034Sdougm optlist = nextopt; 7663034Sdougm } 7673034Sdougm } 7683034Sdougm 7693034Sdougm /* 7703034Sdougm * check property list for valid properties 7713034Sdougm * A null value is a remove which is always valid. 7723034Sdougm */ 7733034Sdougm static int 7743034Sdougm valid_options(struct options *optlist, char *proto, void *object, char *sec) 7753034Sdougm { 7763034Sdougm int ret = SA_OK; 7773034Sdougm struct options *cur; 7783034Sdougm sa_property_t prop; 7793034Sdougm sa_optionset_t parent = NULL; 7803034Sdougm 7813034Sdougm if (object != NULL) { 7824653Sdougm if (sec == NULL) 7834653Sdougm parent = sa_get_optionset(object, proto); 7844653Sdougm else 7854653Sdougm parent = sa_get_security(object, sec, proto); 7863034Sdougm } 7873034Sdougm 7883034Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 7894653Sdougm if (cur->optvalue == NULL) 7904653Sdougm continue; 7913034Sdougm prop = sa_create_property(cur->optname, cur->optvalue); 7923034Sdougm if (prop == NULL) 7934653Sdougm ret = SA_NO_MEMORY; 7943034Sdougm if (ret != SA_OK || 7953034Sdougm (ret = sa_valid_property(parent, proto, prop)) != SA_OK) { 7964653Sdougm (void) printf( 7974653Sdougm gettext("Could not add property %s: %s\n"), 7984653Sdougm cur->optname, sa_errorstr(ret)); 7993034Sdougm } 8003034Sdougm (void) sa_remove_property(prop); 8013034Sdougm } 8023034Sdougm return (ret); 8033034Sdougm } 8043034Sdougm 8053034Sdougm /* 8063034Sdougm * add_optionset(group, optlist, protocol, *err) 8073034Sdougm * Add the options in optlist to an optionset and then add the optionset 8083034Sdougm * to the group. 8093034Sdougm * 8103034Sdougm * The return value indicates if there was a "change" while errors are 8113034Sdougm * returned via the *err parameters. 8123034Sdougm */ 8133034Sdougm static int 8143034Sdougm add_optionset(sa_group_t group, struct options *optlist, char *proto, int *err) 8153034Sdougm { 8163034Sdougm sa_optionset_t optionset; 8173034Sdougm int ret = SA_OK; 8185331Samw int result = B_FALSE; 8193034Sdougm 8203034Sdougm optionset = sa_get_optionset(group, proto); 8213034Sdougm if (optionset == NULL) { 8224653Sdougm optionset = sa_create_optionset(group, proto); 8235331Samw if (optionset == NULL) 8245331Samw ret = SA_NO_MEMORY; 8255331Samw result = B_TRUE; /* adding a protocol is a change */ 8263034Sdougm } 8274653Sdougm if (optionset == NULL) { 8284653Sdougm ret = SA_NO_MEMORY; 8294653Sdougm goto out; 8304653Sdougm } 8314653Sdougm while (optlist != NULL) { 8323034Sdougm sa_property_t prop; 8333034Sdougm prop = sa_get_property(optionset, optlist->optname); 8343034Sdougm if (prop == NULL) { 8353034Sdougm /* 8363034Sdougm * add the property, but only if it is 8373034Sdougm * a non-NULL or non-zero length value 8383034Sdougm */ 8394653Sdougm if (optlist->optvalue != NULL) { 8404653Sdougm prop = sa_create_property(optlist->optname, 8414653Sdougm optlist->optvalue); 8424653Sdougm if (prop != NULL) { 8434653Sdougm ret = sa_valid_property(optionset, 8444653Sdougm proto, prop); 8454653Sdougm if (ret != SA_OK) { 8464653Sdougm (void) sa_remove_property(prop); 8474653Sdougm (void) printf(gettext("Could " 8484653Sdougm "not add property " 8494653Sdougm "%s: %s\n"), 8504653Sdougm optlist->optname, 8514653Sdougm sa_errorstr(ret)); 8524653Sdougm } 8534653Sdougm } 8544653Sdougm if (ret == SA_OK) { 8554653Sdougm ret = sa_add_property(optionset, prop); 8564653Sdougm if (ret != SA_OK) { 8574653Sdougm (void) printf(gettext( 8584653Sdougm "Could not add property " 8594653Sdougm "%s: %s\n"), 8604653Sdougm optlist->optname, 8614653Sdougm sa_errorstr(ret)); 8624653Sdougm } else { 8634653Sdougm /* there was a change */ 8645331Samw result = B_TRUE; 8654653Sdougm } 8664653Sdougm } 8673034Sdougm } 8684653Sdougm } else { 8694653Sdougm ret = sa_update_property(prop, optlist->optvalue); 8704653Sdougm /* should check to see if value changed */ 8714653Sdougm if (ret != SA_OK) { 8724653Sdougm (void) printf(gettext("Could not update " 8734653Sdougm "property %s: %s\n"), optlist->optname, 8744653Sdougm sa_errorstr(ret)); 8754653Sdougm } else { 8765331Samw result = B_TRUE; 8773034Sdougm } 8783034Sdougm } 8793034Sdougm optlist = optlist->next; 8803034Sdougm } 8814653Sdougm ret = sa_commit_properties(optionset, 0); 8824653Sdougm 8834653Sdougm out: 8843034Sdougm if (err != NULL) 8854653Sdougm *err = ret; 8863034Sdougm return (result); 8873034Sdougm } 8883034Sdougm 8893034Sdougm /* 8905331Samw * resource_compliant(group) 8915331Samw * 8925331Samw * Go through all the shares in the group. Assume compliant, but if 8935331Samw * any share doesn't have at least one resource name, it isn't 8945331Samw * compliant. 8955331Samw */ 8965331Samw static int 8975331Samw resource_compliant(sa_group_t group) 8985331Samw { 8995331Samw sa_share_t share; 9005331Samw 9015331Samw for (share = sa_get_share(group, NULL); share != NULL; 9025331Samw share = sa_get_next_share(share)) { 9035331Samw if (sa_get_share_resource(share, NULL) == NULL) { 9045331Samw return (B_FALSE); 9055331Samw } 9065331Samw } 9075331Samw return (B_TRUE); 9085331Samw } 9095331Samw 9105331Samw /* 9115331Samw * fix_path(path) 9125331Samw * 9135331Samw * change all illegal characters to something else. For now, all get 9145331Samw * converted to '_' and the leading '/' is stripped off. This is used 9155331Samw * to construct an resource name (SMB share name) that is valid. 9165331Samw * Caller must pass a valid path. 9175331Samw */ 9185331Samw static void 9195331Samw fix_path(char *path) 9205331Samw { 9215331Samw char *cp; 9225331Samw size_t len; 9235331Samw 9245331Samw assert(path != NULL); 9255331Samw 9265331Samw /* make sure we are appropriate length */ 9275331Samw cp = path + 1; /* skip leading slash */ 9285331Samw while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) { 9295331Samw cp = strchr(cp, '/'); 9305331Samw if (cp != NULL) 9315331Samw cp++; 9325331Samw } 9335331Samw /* two cases - cp == NULL and cp is substring of path */ 9345331Samw if (cp == NULL) { 9355331Samw /* just take last SA_MAX_RESOURCE_NAME chars */ 9365331Samw len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME; 9375331Samw (void) memmove(path, path + len, SA_MAX_RESOURCE_NAME); 9385331Samw path[SA_MAX_RESOURCE_NAME] = '\0'; 9395331Samw } else { 9405331Samw len = strlen(cp) + 1; 9415331Samw (void) memmove(path, cp, len); 9425331Samw } 9435331Samw 9445331Samw /* 9455331Samw * Don't want any of the characters that are not allowed 9465331Samw * in and SMB share name. Replace them with '_'. 9475331Samw */ 9485331Samw while (*path) { 9495331Samw switch (*path) { 9505331Samw case '/': 9515331Samw case '"': 9525331Samw case '\\': 9535331Samw case '[': 9545331Samw case ']': 9555331Samw case ':': 9565331Samw case '|': 9575331Samw case '<': 9585331Samw case '>': 9595331Samw case '+': 9605331Samw case ';': 9615331Samw case ',': 9625331Samw case '?': 9635331Samw case '*': 9645331Samw case '=': 9655331Samw case '\t': 9665331Samw *path = '_'; 9675331Samw break; 9685331Samw } 9695331Samw path++; 9705331Samw } 9715331Samw } 9725331Samw 9735331Samw /* 9745331Samw * name_adjust(path, count) 9755331Samw * 9765331Samw * Add a ~<count> in place of last few characters. The total number of 9775331Samw * characters is dependent on count. 9785331Samw */ 9795331Samw #define MAX_MANGLE_NUMBER 10000 9805331Samw 9815331Samw static int 9825331Samw name_adjust(char *path, int count) 9835331Samw { 9845331Samw size_t len; 9855331Samw 9865331Samw len = strlen(path) - 2; 9875331Samw if (count > 10) 9885331Samw len--; 9895331Samw if (count > 100) 9905331Samw len--; 9915331Samw if (count > 1000) 9925331Samw len--; 9935331Samw if (len > 0) 9945331Samw (void) sprintf(path + len, "~%d", count); 9955331Samw else 9965331Samw return (SA_BAD_VALUE); 9975331Samw 9985331Samw return (SA_OK); 9995331Samw } 10005331Samw 10015331Samw /* 10025331Samw * make_resources(group) 10035331Samw * 10045331Samw * Go through all the shares in the group and make them have resource 10055331Samw * names. 10065331Samw */ 10075331Samw static void 10085331Samw make_resources(sa_group_t group) 10095331Samw { 10105331Samw sa_share_t share; 10115331Samw int count; 10125331Samw int err = SA_OK; 10135331Samw 10145331Samw for (share = sa_get_share(group, NULL); share != NULL; 10155331Samw share = sa_get_next_share(share)) { 10165331Samw /* Skip those with resources */ 10175331Samw if (sa_get_share_resource(share, NULL) == NULL) { 10185331Samw char *path; 10195331Samw path = sa_get_share_attr(share, "path"); 10205331Samw if (path == NULL) 10215331Samw continue; 10225331Samw fix_path(path); 10235331Samw count = 0; /* reset for next resource */ 10245331Samw while (sa_add_resource(share, path, 10255331Samw SA_SHARE_PERMANENT, &err) == NULL && 10265331Samw err == SA_DUPLICATE_NAME) { 10275331Samw int ret; 10285331Samw ret = name_adjust(path, count); 10295331Samw count++; 10305331Samw if (ret != SA_OK || 10315331Samw count >= MAX_MANGLE_NUMBER) { 10325331Samw (void) printf(gettext( 10335331Samw "Cannot create resource name for" 10345331Samw " path: %s\n"), path); 10355331Samw break; 10365331Samw } 10375331Samw } 10385331Samw sa_free_attr_string(path); 10395331Samw } 10405331Samw } 10415331Samw } 10425331Samw 10435331Samw /* 10443034Sdougm * sa_create(flags, argc, argv) 10453034Sdougm * create a new group 10463034Sdougm * this may or may not have a protocol associated with it. 10473034Sdougm * No protocol means "all" protocols in this case. 10483034Sdougm */ 10493034Sdougm static int 10503910Sdougm sa_create(sa_handle_t handle, int flags, int argc, char *argv[]) 10513034Sdougm { 10523034Sdougm char *groupname; 10533034Sdougm 10543034Sdougm sa_group_t group; 10555331Samw int force = 0; 10563034Sdougm int verbose = 0; 10573034Sdougm int dryrun = 0; 10583034Sdougm int c; 10593034Sdougm char *protocol = NULL; 10603034Sdougm int ret = SA_OK; 10613034Sdougm struct options *optlist = NULL; 10623034Sdougm int err = 0; 10633034Sdougm int auth; 10643034Sdougm 10655331Samw while ((c = getopt(argc, argv, "?fhvnP:p:")) != EOF) { 10664653Sdougm switch (c) { 10675331Samw case 'f': 10685331Samw force++; 10695331Samw break; 10704653Sdougm case 'v': 10714653Sdougm verbose++; 10724653Sdougm break; 10734653Sdougm case 'n': 10744653Sdougm dryrun++; 10754653Sdougm break; 10764653Sdougm case 'P': 10775331Samw if (protocol != NULL) { 10785331Samw (void) printf(gettext("Specifying " 10795331Samw "multiple protocols " 10805331Samw "not supported: %s\n"), protocol); 10815331Samw return (SA_SYNTAX_ERR); 10825331Samw } 10834653Sdougm protocol = optarg; 10844653Sdougm if (sa_valid_protocol(protocol)) 10854653Sdougm break; 10864653Sdougm (void) printf(gettext( 10874653Sdougm "Invalid protocol specified: %s\n"), protocol); 10884653Sdougm return (SA_INVALID_PROTOCOL); 10894653Sdougm break; 10904653Sdougm case 'p': 10914653Sdougm ret = add_opt(&optlist, optarg, 0); 10924653Sdougm switch (ret) { 10934653Sdougm case OPT_ADD_SYNTAX: 10944653Sdougm (void) printf(gettext( 10954653Sdougm "Property syntax error for property: %s\n"), 10964653Sdougm optarg); 10974653Sdougm return (SA_SYNTAX_ERR); 10984653Sdougm case OPT_ADD_SECURITY: 10994653Sdougm (void) printf(gettext( 11004653Sdougm "Security properties need " 11014653Sdougm "to be set with set-security: %s\n"), 11024653Sdougm optarg); 11034653Sdougm return (SA_SYNTAX_ERR); 11044653Sdougm default: 11054653Sdougm break; 11064653Sdougm } 11074653Sdougm break; 11084653Sdougm default: 11094653Sdougm case 'h': 11104653Sdougm case '?': 11114653Sdougm (void) printf(gettext("usage: %s\n"), 11124653Sdougm sa_get_usage(USAGE_CREATE)); 11134653Sdougm return (0); 11143034Sdougm } 11153034Sdougm } 11163034Sdougm 11173034Sdougm if (optind >= argc) { 11184653Sdougm (void) printf(gettext("usage: %s\n"), 11194653Sdougm sa_get_usage(USAGE_CREATE)); 11204653Sdougm (void) printf(gettext("\tgroup must be specified.\n")); 11214653Sdougm return (SA_BAD_PATH); 11223034Sdougm } 11233034Sdougm 11243034Sdougm if ((optind + 1) < argc) { 11254653Sdougm (void) printf(gettext("usage: %s\n"), 11264653Sdougm sa_get_usage(USAGE_CREATE)); 11274653Sdougm (void) printf(gettext("\textraneous group(s) at end\n")); 11284653Sdougm return (SA_SYNTAX_ERR); 11293034Sdougm } 11303034Sdougm 11313034Sdougm if (protocol == NULL && optlist != NULL) { 11324653Sdougm /* lookup default protocol */ 11334653Sdougm (void) printf(gettext("usage: %s\n"), 11344653Sdougm sa_get_usage(USAGE_CREATE)); 11354653Sdougm (void) printf(gettext("\tprotocol must be specified " 11364653Sdougm "with properties\n")); 11374653Sdougm return (SA_INVALID_PROTOCOL); 11383034Sdougm } 11393034Sdougm 11403034Sdougm if (optlist != NULL) 11414653Sdougm ret = chk_opt(optlist, 0, protocol); 11423034Sdougm if (ret == OPT_ADD_SECURITY) { 11434653Sdougm (void) printf(gettext("Security properties not " 11444653Sdougm "supported with create\n")); 11454653Sdougm return (SA_SYNTAX_ERR); 11463034Sdougm } 11473034Sdougm 11483034Sdougm /* 11494653Sdougm * If a group already exists, we can only add a new protocol 11503034Sdougm * to it and not create a new one or add the same protocol 11513034Sdougm * again. 11523034Sdougm */ 11533034Sdougm 11543034Sdougm groupname = argv[optind]; 11553034Sdougm 11563034Sdougm auth = check_authorizations(groupname, flags); 11573034Sdougm 11583910Sdougm group = sa_get_group(handle, groupname); 11593034Sdougm if (group != NULL) { 11604653Sdougm /* group exists so must be a protocol add */ 11614653Sdougm if (protocol != NULL) { 11624653Sdougm if (has_protocol(group, protocol)) { 11634653Sdougm (void) printf(gettext( 11644653Sdougm "Group \"%s\" already exists" 11654653Sdougm " with protocol %s\n"), groupname, 11664653Sdougm protocol); 11674653Sdougm ret = SA_DUPLICATE_NAME; 1168*5997Sdougm } else if (strcmp(groupname, "default") == 0 && 1169*5997Sdougm strcmp(protocol, "nfs") != 0) { 1170*5997Sdougm (void) printf(gettext( 1171*5997Sdougm "Group \"%s\" only allows protocol " 1172*5997Sdougm "\"%s\"\n"), groupname, "nfs"); 1173*5997Sdougm ret = SA_INVALID_PROTOCOL; 11744653Sdougm } 11754653Sdougm } else { 11764653Sdougm /* must add new protocol */ 11774653Sdougm (void) printf(gettext( 11784653Sdougm "Group already exists and no protocol " 11794653Sdougm "specified.\n")); 11804653Sdougm ret = SA_DUPLICATE_NAME; 11813034Sdougm } 11823034Sdougm } else { 11833034Sdougm /* 11843034Sdougm * is it a valid name? Must comply with SMF instance 11853034Sdougm * name restrictions. 11863034Sdougm */ 11874653Sdougm if (!sa_valid_group_name(groupname)) { 11884653Sdougm ret = SA_INVALID_NAME; 11894653Sdougm (void) printf(gettext("Invalid group name: %s\n"), 11904653Sdougm groupname); 11914653Sdougm } 11923034Sdougm } 11933034Sdougm if (ret == SA_OK) { 11944653Sdougm /* check protocol vs optlist */ 11954653Sdougm if (optlist != NULL) { 11964653Sdougm /* check options, if any, for validity */ 11974653Sdougm ret = valid_options(optlist, protocol, group, NULL); 11984653Sdougm } 11993034Sdougm } 12003034Sdougm if (ret == SA_OK && !dryrun) { 12014653Sdougm if (group == NULL) { 12024653Sdougm group = sa_create_group(handle, (char *)groupname, 12034653Sdougm &err); 12043034Sdougm } 12054653Sdougm if (group != NULL) { 12064653Sdougm sa_optionset_t optionset; 12075331Samw /* 12085331Samw * First check to see if the new protocol is one that 12095331Samw * requires resource names and make sure we are 12105331Samw * compliant before proceeding. 12115331Samw */ 12125331Samw if (protocol != NULL) { 12135331Samw uint64_t features; 12145331Samw 12155331Samw features = sa_proto_get_featureset(protocol); 12165331Samw if ((features & SA_FEATURE_RESOURCE) && 12175331Samw !resource_compliant(group)) { 12185331Samw if (force) { 12195331Samw make_resources(group); 12205331Samw } else { 12215331Samw ret = SA_RESOURCE_REQUIRED; 12225331Samw (void) printf( 12235331Samw gettext("Protocol " 12245331Samw "requires resource " 12255331Samw "names to be " 12265331Samw "set: %s\n"), 12275331Samw protocol); 12285331Samw goto err; 12295331Samw } 12305331Samw } 12315331Samw } 12324653Sdougm if (optlist != NULL) { 12334653Sdougm (void) add_optionset(group, optlist, protocol, 12344653Sdougm &ret); 12354653Sdougm } else if (protocol != NULL) { 12364653Sdougm optionset = sa_create_optionset(group, 12374653Sdougm protocol); 12384653Sdougm if (optionset == NULL) 12394653Sdougm ret = SA_NO_MEMORY; 12404653Sdougm } else if (protocol == NULL) { 12414653Sdougm char **protolist; 12424653Sdougm int numprotos, i; 12434653Sdougm numprotos = sa_get_protocols(&protolist); 12444653Sdougm for (i = 0; i < numprotos; i++) { 12454653Sdougm optionset = sa_create_optionset(group, 12464653Sdougm protolist[i]); 12474653Sdougm } 12484653Sdougm if (protolist != NULL) 12494653Sdougm free(protolist); 12504653Sdougm } 12513034Sdougm /* 12524653Sdougm * We have a group and legal additions 12533034Sdougm */ 12544653Sdougm if (ret == SA_OK) { 12554653Sdougm /* 12564653Sdougm * Commit to configuration for protocols that 12574653Sdougm * need to do block updates. For NFS, this 12584653Sdougm * doesn't do anything but it will be run for 12594653Sdougm * all protocols that implement the 12604653Sdougm * appropriate plugin. 12614653Sdougm */ 12624653Sdougm ret = sa_update_config(handle); 12634653Sdougm } else { 12644653Sdougm if (group != NULL) 12654653Sdougm (void) sa_remove_group(group); 12664653Sdougm } 12673034Sdougm } else { 12684653Sdougm ret = err; 12694653Sdougm (void) printf(gettext("Could not create group: %s\n"), 12704653Sdougm sa_errorstr(ret)); 12713034Sdougm } 12723034Sdougm } 12733034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 12744653Sdougm (void) printf(gettext("Command would fail: %s\n"), 12754653Sdougm sa_errorstr(SA_NO_PERMISSION)); 12764653Sdougm ret = SA_NO_PERMISSION; 12773034Sdougm } 12785331Samw err: 12793034Sdougm free_opt(optlist); 12803034Sdougm return (ret); 12813034Sdougm } 12823034Sdougm 12833034Sdougm /* 12843034Sdougm * group_status(group) 12853034Sdougm * 12863034Sdougm * return the current status (enabled/disabled) of the group. 12873034Sdougm */ 12883034Sdougm 12893034Sdougm static char * 12903034Sdougm group_status(sa_group_t group) 12913034Sdougm { 12923034Sdougm char *state; 12933034Sdougm int enabled = 0; 12943034Sdougm 12953034Sdougm state = sa_get_group_attr(group, "state"); 12963034Sdougm if (state != NULL) { 12974653Sdougm if (strcmp(state, "enabled") == 0) { 12984653Sdougm enabled = 1; 12994653Sdougm } 13004653Sdougm sa_free_attr_string(state); 13013034Sdougm } 13024255Sdougm return (enabled ? "enabled" : "disabled"); 13033034Sdougm } 13043034Sdougm 13053034Sdougm /* 13063034Sdougm * sa_delete(flags, argc, argv) 13073034Sdougm * 13083034Sdougm * Delete a group. 13093034Sdougm */ 13103034Sdougm 13113034Sdougm static int 13123910Sdougm sa_delete(sa_handle_t handle, int flags, int argc, char *argv[]) 13133034Sdougm { 13143034Sdougm char *groupname; 13153034Sdougm sa_group_t group; 13163034Sdougm sa_share_t share; 13173034Sdougm int verbose = 0; 13183034Sdougm int dryrun = 0; 13193034Sdougm int force = 0; 13203034Sdougm int c; 13213034Sdougm char *protocol = NULL; 13223034Sdougm char *sectype = NULL; 13233034Sdougm int ret = SA_OK; 13243034Sdougm int auth; 13253034Sdougm 13263034Sdougm while ((c = getopt(argc, argv, "?hvnP:fS:")) != EOF) { 13274653Sdougm switch (c) { 13284653Sdougm case 'v': 13294653Sdougm verbose++; 13304653Sdougm break; 13314653Sdougm case 'n': 13324653Sdougm dryrun++; 13334653Sdougm break; 13344653Sdougm case 'P': 13355331Samw if (protocol != NULL) { 13365331Samw (void) printf(gettext("Specifying " 13375331Samw "multiple protocols " 13385331Samw "not supported: %s\n"), protocol); 13395331Samw return (SA_SYNTAX_ERR); 13405331Samw } 13414653Sdougm protocol = optarg; 13424653Sdougm if (!sa_valid_protocol(protocol)) { 13434653Sdougm (void) printf(gettext("Invalid protocol " 13445331Samw "specified: %s\n"), protocol); 13454653Sdougm return (SA_INVALID_PROTOCOL); 13464653Sdougm } 13474653Sdougm break; 13484653Sdougm case 'S': 13495331Samw if (sectype != NULL) { 13505331Samw (void) printf(gettext("Specifying " 13515331Samw "multiple property " 13525331Samw "spaces not supported: %s\n"), sectype); 13535331Samw return (SA_SYNTAX_ERR); 13545331Samw } 13554653Sdougm sectype = optarg; 13564653Sdougm break; 13574653Sdougm case 'f': 13584653Sdougm force++; 13594653Sdougm break; 13604653Sdougm default: 13614653Sdougm case 'h': 13624653Sdougm case '?': 13634653Sdougm (void) printf(gettext("usage: %s\n"), 13644653Sdougm sa_get_usage(USAGE_DELETE)); 13654653Sdougm return (0); 13663034Sdougm } 13673034Sdougm } 13683034Sdougm 13693034Sdougm if (optind >= argc) { 13704653Sdougm (void) printf(gettext("usage: %s\n"), 13714653Sdougm sa_get_usage(USAGE_DELETE)); 13724653Sdougm (void) printf(gettext("\tgroup must be specified.\n")); 13734653Sdougm return (SA_SYNTAX_ERR); 13743034Sdougm } 13753034Sdougm 13763034Sdougm if ((optind + 1) < argc) { 13774653Sdougm (void) printf(gettext("usage: %s\n"), 13784653Sdougm sa_get_usage(USAGE_DELETE)); 13794653Sdougm (void) printf(gettext("\textraneous group(s) at end\n")); 13804653Sdougm return (SA_SYNTAX_ERR); 13813034Sdougm } 13823034Sdougm 13833034Sdougm if (sectype != NULL && protocol == NULL) { 13844653Sdougm (void) printf(gettext("usage: %s\n"), 13854653Sdougm sa_get_usage(USAGE_DELETE)); 13864653Sdougm (void) printf(gettext("\tsecurity requires protocol to be " 13874653Sdougm "specified.\n")); 13884653Sdougm return (SA_SYNTAX_ERR); 13893034Sdougm } 13903034Sdougm 13913034Sdougm /* 13923034Sdougm * Determine if the group already exists since it must in 13933034Sdougm * order to be removed. 13943034Sdougm * 13953034Sdougm * We can delete when: 13963034Sdougm * 13973034Sdougm * - group is empty 13983034Sdougm * - force flag is set 13993034Sdougm * - if protocol specified, only delete the protocol 14003034Sdougm */ 14013034Sdougm 14023034Sdougm groupname = argv[optind]; 14033910Sdougm group = sa_get_group(handle, groupname); 14043034Sdougm if (group == NULL) { 14053034Sdougm ret = SA_NO_SUCH_GROUP; 14064653Sdougm goto done; 14074653Sdougm } 14084653Sdougm auth = check_authorizations(groupname, flags); 14094653Sdougm if (protocol == NULL) { 14103034Sdougm share = sa_get_share(group, NULL); 14113034Sdougm if (share != NULL) 14124653Sdougm ret = SA_BUSY; 14133034Sdougm if (share == NULL || (share != NULL && force == 1)) { 14144653Sdougm ret = SA_OK; 14154653Sdougm if (!dryrun) { 14164653Sdougm while (share != NULL) { 14174653Sdougm sa_share_t next_share; 14184653Sdougm next_share = sa_get_next_share(share); 14194653Sdougm /* 14204653Sdougm * need to do the disable of 14214653Sdougm * each share, but don't 14224653Sdougm * actually do anything on a 14234653Sdougm * dryrun. 14244653Sdougm */ 14254653Sdougm ret = sa_disable_share(share, NULL); 14264653Sdougm ret = sa_remove_share(share); 14274653Sdougm share = next_share; 14284653Sdougm } 14294653Sdougm ret = sa_remove_group(group); 14303034Sdougm } 14313034Sdougm } 14324653Sdougm /* Commit to configuration if not a dryrun */ 14333034Sdougm if (!dryrun && ret == SA_OK) { 14344653Sdougm ret = sa_update_config(handle); 14353034Sdougm } 14364653Sdougm } else { 14373034Sdougm /* a protocol delete */ 14383034Sdougm sa_optionset_t optionset; 14393034Sdougm sa_security_t security; 14405331Samw if (sectype != NULL) { 14414653Sdougm /* only delete specified security */ 14424653Sdougm security = sa_get_security(group, sectype, protocol); 14434653Sdougm if (security != NULL && !dryrun) 14444653Sdougm ret = sa_destroy_security(security); 14454653Sdougm else 14464653Sdougm ret = SA_INVALID_PROTOCOL; 14473034Sdougm } else { 14484653Sdougm optionset = sa_get_optionset(group, protocol); 14494653Sdougm if (optionset != NULL && !dryrun) { 14504653Sdougm /* 14514653Sdougm * have an optionset with 14524653Sdougm * protocol to delete 14534653Sdougm */ 14544653Sdougm ret = sa_destroy_optionset(optionset); 14554653Sdougm /* 14564653Sdougm * Now find all security sets 14574653Sdougm * for the protocol and remove 14584653Sdougm * them. Don't remove other 14594653Sdougm * protocols. 14604653Sdougm */ 14614653Sdougm for (security = 14624653Sdougm sa_get_security(group, NULL, NULL); 14634653Sdougm ret == SA_OK && security != NULL; 14644653Sdougm security = sa_get_next_security(security)) { 14654653Sdougm char *secprot; 14664653Sdougm secprot = sa_get_security_attr(security, 14674653Sdougm "type"); 14684653Sdougm if (secprot != NULL && 14694653Sdougm strcmp(secprot, protocol) == 0) 14704653Sdougm ret = sa_destroy_security( 14714653Sdougm security); 14724653Sdougm if (secprot != NULL) 14734653Sdougm sa_free_attr_string(secprot); 14744653Sdougm } 14754653Sdougm } else { 14764653Sdougm if (!dryrun) 14774653Sdougm ret = SA_INVALID_PROTOCOL; 14783034Sdougm } 14793034Sdougm } 14805331Samw /* 14815331Samw * With the protocol items removed, make sure that all 14825331Samw * the shares are updated in the legacy files, if 14835331Samw * necessary. 14845331Samw */ 14855331Samw for (share = sa_get_share(group, NULL); 14865331Samw share != NULL; 14875331Samw share = sa_get_next_share(share)) { 14885331Samw (void) sa_delete_legacy(share, protocol); 14895331Samw } 14903034Sdougm } 14914653Sdougm 14924653Sdougm done: 14933034Sdougm if (ret != SA_OK) { 14944653Sdougm (void) printf(gettext("Could not delete group: %s\n"), 14954653Sdougm sa_errorstr(ret)); 14963034Sdougm } else if (dryrun && !auth && verbose) { 14974653Sdougm (void) printf(gettext("Command would fail: %s\n"), 14984653Sdougm sa_errorstr(SA_NO_PERMISSION)); 14993034Sdougm } 15003034Sdougm return (ret); 15013034Sdougm } 15023034Sdougm 15033034Sdougm /* 15043034Sdougm * strndupr(*buff, str, buffsize) 15053034Sdougm * 15063034Sdougm * used with small strings to duplicate and possibly increase the 15073034Sdougm * buffer size of a string. 15083034Sdougm */ 15093034Sdougm static char * 15103034Sdougm strndupr(char *buff, char *str, int *buffsize) 15113034Sdougm { 15123034Sdougm int limit; 15133034Sdougm char *orig_buff = buff; 15143034Sdougm 15153034Sdougm if (buff == NULL) { 15164653Sdougm buff = (char *)malloc(64); 15174653Sdougm if (buff == NULL) 15184653Sdougm return (NULL); 15194653Sdougm *buffsize = 64; 15204653Sdougm buff[0] = '\0'; 15213034Sdougm } 15223034Sdougm limit = strlen(buff) + strlen(str) + 1; 15233034Sdougm if (limit > *buffsize) { 15244653Sdougm limit = *buffsize = *buffsize + ((limit / 64) + 64); 15254653Sdougm buff = realloc(buff, limit); 15263034Sdougm } 15273034Sdougm if (buff != NULL) { 15284653Sdougm (void) strcat(buff, str); 15293034Sdougm } else { 15304653Sdougm /* if it fails, fail it hard */ 15314653Sdougm if (orig_buff != NULL) 15324653Sdougm free(orig_buff); 15333034Sdougm } 15343034Sdougm return (buff); 15353034Sdougm } 15363034Sdougm 15373034Sdougm /* 15383034Sdougm * group_proto(group) 15393034Sdougm * 15403034Sdougm * return a string of all the protocols (space separated) associated 15413034Sdougm * with this group. 15423034Sdougm */ 15433034Sdougm 15443034Sdougm static char * 15453034Sdougm group_proto(sa_group_t group) 15463034Sdougm { 15473034Sdougm sa_optionset_t optionset; 15483034Sdougm char *proto; 15493034Sdougm char *buff = NULL; 15503034Sdougm int buffsize = 0; 15513034Sdougm int addspace = 0; 15523034Sdougm /* 15533034Sdougm * get the protocol list by finding the optionsets on this 15543034Sdougm * group and extracting the type value. The initial call to 15553034Sdougm * strndupr() initailizes buff. 15563034Sdougm */ 15573034Sdougm buff = strndupr(buff, "", &buffsize); 15583034Sdougm if (buff != NULL) { 15594653Sdougm for (optionset = sa_get_optionset(group, NULL); 15604653Sdougm optionset != NULL && buff != NULL; 15614653Sdougm optionset = sa_get_next_optionset(optionset)) { 15624653Sdougm /* 15634653Sdougm * extract out the protocol type from this optionset 15644653Sdougm * and append it to the buffer "buff". strndupr() will 15654653Sdougm * reallocate space as necessay. 15664653Sdougm */ 15674653Sdougm proto = sa_get_optionset_attr(optionset, "type"); 15684653Sdougm if (proto != NULL) { 15694653Sdougm if (addspace++) 15704653Sdougm buff = strndupr(buff, " ", &buffsize); 15714653Sdougm buff = strndupr(buff, proto, &buffsize); 15724653Sdougm sa_free_attr_string(proto); 15734653Sdougm } 15743034Sdougm } 15753034Sdougm } 15763034Sdougm return (buff); 15773034Sdougm } 15783034Sdougm 15793034Sdougm /* 15803034Sdougm * sa_list(flags, argc, argv) 15813034Sdougm * 15823034Sdougm * implements the "list" subcommand to list groups and optionally 15833034Sdougm * their state and protocols. 15843034Sdougm */ 15853034Sdougm 15863034Sdougm static int 15873910Sdougm sa_list(sa_handle_t handle, int flags, int argc, char *argv[]) 15883034Sdougm { 15893034Sdougm sa_group_t group; 15903034Sdougm int verbose = 0; 15913034Sdougm int c; 15923034Sdougm char *protocol = NULL; 15935331Samw #ifdef lint 15945331Samw flags = flags; 15955331Samw #endif 15963034Sdougm 15973034Sdougm while ((c = getopt(argc, argv, "?hvP:")) != EOF) { 15984653Sdougm switch (c) { 15994653Sdougm case 'v': 16004653Sdougm verbose++; 16014653Sdougm break; 16024653Sdougm case 'P': 16035331Samw if (protocol != NULL) { 16045331Samw (void) printf(gettext( 16055331Samw "Specifying multiple protocols " 16065331Samw "not supported: %s\n"), 16075331Samw protocol); 16085331Samw return (SA_SYNTAX_ERR); 16095331Samw } 16104653Sdougm protocol = optarg; 16114653Sdougm if (!sa_valid_protocol(protocol)) { 16124653Sdougm (void) printf(gettext( 16134653Sdougm "Invalid protocol specified: %s\n"), 16144653Sdougm protocol); 16154653Sdougm return (SA_INVALID_PROTOCOL); 16164653Sdougm } 16174653Sdougm break; 16184653Sdougm default: 16194653Sdougm case 'h': 16204653Sdougm case '?': 16214653Sdougm (void) printf(gettext("usage: %s\n"), 16224653Sdougm sa_get_usage(USAGE_LIST)); 16234653Sdougm return (0); 16243034Sdougm } 16253034Sdougm } 16263034Sdougm 16275885Sdougm if (optind != argc) { 16285885Sdougm (void) printf(gettext("usage: %s\n"), 16295885Sdougm sa_get_usage(USAGE_LIST)); 16305885Sdougm return (SA_SYNTAX_ERR); 16315885Sdougm } 16325885Sdougm 16334653Sdougm for (group = sa_get_group(handle, NULL); 16344653Sdougm group != NULL; 16353034Sdougm group = sa_get_next_group(group)) { 16364653Sdougm char *name; 16374653Sdougm char *proto; 16384653Sdougm if (protocol == NULL || has_protocol(group, protocol)) { 16394653Sdougm name = sa_get_group_attr(group, "name"); 16404653Sdougm if (name != NULL && (verbose > 1 || name[0] != '#')) { 16414653Sdougm (void) printf("%s", (char *)name); 16424653Sdougm if (verbose) { 16434653Sdougm /* 16444653Sdougm * Need the list of protocols 16454653Sdougm * and current status once 16464653Sdougm * available. We do want to 16474653Sdougm * translate the 16484653Sdougm * enabled/disabled text here. 16494653Sdougm */ 16504653Sdougm (void) printf("\t%s", isenabled(group) ? 16514653Sdougm gettext("enabled") : 16524653Sdougm gettext("disabled")); 16534653Sdougm proto = group_proto(group); 16544653Sdougm if (proto != NULL) { 16554653Sdougm (void) printf("\t%s", 16564653Sdougm (char *)proto); 16574653Sdougm free(proto); 16584653Sdougm } 16594653Sdougm } 16604653Sdougm (void) printf("\n"); 16613034Sdougm } 16624653Sdougm if (name != NULL) 16634653Sdougm sa_free_attr_string(name); 16643034Sdougm } 16653034Sdougm } 16663034Sdougm return (0); 16673034Sdougm } 16683034Sdougm 16693034Sdougm /* 16703034Sdougm * out_properties(optionset, proto, sec) 16713034Sdougm * 16723034Sdougm * Format the properties and encode the protocol and optional named 16733034Sdougm * optionset into the string. 16743034Sdougm * 16753034Sdougm * format is protocol[:name]=(property-list) 16763034Sdougm */ 16773034Sdougm 16783034Sdougm static void 16793034Sdougm out_properties(sa_optionset_t optionset, char *proto, char *sec) 16803034Sdougm { 16813034Sdougm char *type; 16823034Sdougm char *value; 16833034Sdougm int spacer; 16843034Sdougm sa_property_t prop; 16853034Sdougm 16864653Sdougm if (sec == NULL) 16874653Sdougm (void) printf(" %s=(", proto ? proto : gettext("all")); 16884653Sdougm else 16894653Sdougm (void) printf(" %s:%s=(", proto ? proto : gettext("all"), sec); 16903034Sdougm 16913034Sdougm for (spacer = 0, prop = sa_get_property(optionset, NULL); 16924653Sdougm prop != NULL; 16934653Sdougm prop = sa_get_next_property(prop)) { 16943034Sdougm 16953034Sdougm /* 16963034Sdougm * extract the property name/value and output with 16973034Sdougm * appropriate spacing. I.e. no prefixed space the 16983034Sdougm * first time through but a space on subsequent 16993034Sdougm * properties. 17003034Sdougm */ 17014653Sdougm type = sa_get_property_attr(prop, "type"); 17024653Sdougm value = sa_get_property_attr(prop, "value"); 17034653Sdougm if (type != NULL) { 17044653Sdougm (void) printf("%s%s=", spacer ? " " : "", type); 17054653Sdougm spacer = 1; 17064653Sdougm if (value != NULL) 17074653Sdougm (void) printf("\"%s\"", value); 17084653Sdougm else 17094653Sdougm (void) printf("\"\""); 17104653Sdougm } 17114653Sdougm if (type != NULL) 17124653Sdougm sa_free_attr_string(type); 17133034Sdougm if (value != NULL) 17144653Sdougm sa_free_attr_string(value); 17153034Sdougm } 17163034Sdougm (void) printf(")"); 17173034Sdougm } 17183034Sdougm 17193034Sdougm /* 17203034Sdougm * show_properties(group, protocol, prefix) 17213034Sdougm * 17223034Sdougm * print the properties for a group. If protocol is NULL, do all 17233034Sdougm * protocols otherwise only the specified protocol. All security 17243034Sdougm * (named groups specific to the protocol) are included. 17253034Sdougm * 17263034Sdougm * The "prefix" is always applied. The caller knows whether it wants 17273034Sdougm * some type of prefix string (white space) or not. Once the prefix 17283034Sdougm * has been output, it is reduced to the zero length string for the 17293034Sdougm * remainder of the property output. 17303034Sdougm */ 17313034Sdougm 17323034Sdougm static void 17333034Sdougm show_properties(sa_group_t group, char *protocol, char *prefix) 17343034Sdougm { 17353034Sdougm sa_optionset_t optionset; 17363034Sdougm sa_security_t security; 17373034Sdougm char *value; 17383034Sdougm char *secvalue; 17393034Sdougm 17403034Sdougm if (protocol != NULL) { 17414653Sdougm optionset = sa_get_optionset(group, protocol); 17424653Sdougm if (optionset != NULL) { 17434653Sdougm (void) printf("%s", prefix); 17444653Sdougm prefix = ""; 17454653Sdougm out_properties(optionset, protocol, NULL); 17464653Sdougm } 17474653Sdougm security = sa_get_security(group, protocol, NULL); 17484653Sdougm if (security != NULL) { 17494653Sdougm (void) printf("%s", prefix); 17504653Sdougm prefix = ""; 17514653Sdougm out_properties(security, protocol, NULL); 17524653Sdougm } 17533034Sdougm } else { 17544653Sdougm for (optionset = sa_get_optionset(group, protocol); 17554653Sdougm optionset != NULL; 17564653Sdougm optionset = sa_get_next_optionset(optionset)) { 17574653Sdougm 17584653Sdougm value = sa_get_optionset_attr(optionset, "type"); 17594653Sdougm (void) printf("%s", prefix); 17604653Sdougm prefix = ""; 17614653Sdougm out_properties(optionset, value, 0); 17624653Sdougm if (value != NULL) 17634653Sdougm sa_free_attr_string(value); 17644653Sdougm } 17654653Sdougm for (security = sa_get_security(group, NULL, protocol); 17664653Sdougm security != NULL; 17674653Sdougm security = sa_get_next_security(security)) { 17684653Sdougm 17694653Sdougm value = sa_get_security_attr(security, "type"); 17704653Sdougm secvalue = sa_get_security_attr(security, "sectype"); 17714653Sdougm (void) printf("%s", prefix); 17724653Sdougm prefix = ""; 17734653Sdougm out_properties(security, value, secvalue); 17744653Sdougm if (value != NULL) 17754653Sdougm sa_free_attr_string(value); 17764653Sdougm if (secvalue != NULL) 17774653Sdougm sa_free_attr_string(secvalue); 17784653Sdougm } 17793034Sdougm } 17803034Sdougm } 17813034Sdougm 17823034Sdougm /* 17835331Samw * get_resource(share) 17845331Samw * 17855331Samw * Get the first resource name, if any, and fix string to be in 17865331Samw * current locale and have quotes if it has embedded spaces. Return 17875331Samw * an attr string that must be freed. 17885331Samw */ 17895331Samw 17905331Samw static char * 17915331Samw get_resource(sa_share_t share) 17925331Samw { 17935331Samw sa_resource_t resource; 17945331Samw char *resstring = NULL; 17955331Samw char *retstring; 17965331Samw 17975331Samw if ((resource = sa_get_share_resource(share, NULL)) != NULL) { 17985331Samw resstring = sa_get_resource_attr(resource, "name"); 17995331Samw if (resstring != NULL) { 18005331Samw char *cp; 18015331Samw int len; 18025331Samw 18035331Samw retstring = conv_from_utf8(resstring); 18045331Samw if (retstring != resstring) { 18055331Samw sa_free_attr_string(resstring); 18065331Samw resstring = retstring; 18075331Samw } 18085331Samw if (strpbrk(resstring, " ") != NULL) { 18095331Samw /* account for quotes */ 18105331Samw len = strlen(resstring) + 3; 18115331Samw cp = calloc(len, sizeof (char)); 18125331Samw if (cp != NULL) { 18135331Samw (void) snprintf(cp, len, 18145331Samw "\"%s\"", resstring); 18155331Samw sa_free_attr_string(resstring); 18165331Samw resstring = cp; 18175331Samw } else { 18185331Samw sa_free_attr_string(resstring); 18195331Samw resstring = NULL; 18205331Samw } 18215331Samw } 18225331Samw } 18235331Samw } 18245331Samw return (resstring); 18255331Samw } 18265331Samw 18275331Samw /* 18285331Samw * has_resource_with_opt(share) 18295331Samw * 18305331Samw * Check to see if the share has any resource names with optionsets 18315331Samw * set. Also indicate if multiple resource names since the syntax 18325331Samw * would be about the same. 18335331Samw */ 18345331Samw static int 18355331Samw has_resource_with_opt(sa_share_t share) 18365331Samw { 18375331Samw sa_resource_t resource; 18385331Samw int ret = B_FALSE; 18395331Samw 18405331Samw for (resource = sa_get_share_resource(share, NULL); 18415331Samw resource != NULL; 18425331Samw resource = sa_get_next_resource(resource)) { 18435331Samw 18445331Samw if (sa_get_optionset(resource, NULL) != NULL) { 18455331Samw ret = B_TRUE; 18465331Samw break; 18475331Samw } 18485331Samw } 18495331Samw return (ret); 18505331Samw } 18515331Samw 18525331Samw /* 18535331Samw * has_multiple_resource(share) 18545331Samw * 18555885Sdougm * Check to see if the share has multiple resource names since 18565885Sdougm * the syntax would be about the same. 18575331Samw */ 18585885Sdougm static boolean_t 18595331Samw has_multiple_resource(sa_share_t share) 18605331Samw { 18615331Samw sa_resource_t resource; 18625331Samw int num; 18635331Samw 18645331Samw for (num = 0, resource = sa_get_share_resource(share, NULL); 18655331Samw resource != NULL; 18665331Samw resource = sa_get_next_resource(resource)) { 18675331Samw num++; 18685331Samw if (num > 1) 18695331Samw return (B_TRUE); 18705331Samw } 18715331Samw return (B_FALSE); 18725331Samw } 18735331Samw 18745331Samw /* 18755331Samw * show_share(share, verbose, properties, proto, iszfs, sharepath) 18765331Samw * 18775331Samw * print out the share information. With the addition of resource as a 18785331Samw * full object that can have multiple instances below the share, we 18795331Samw * need to display that as well. 18805331Samw */ 18815331Samw 18825331Samw static void 18835331Samw show_share(sa_share_t share, int verbose, int properties, char *proto, 18845331Samw int iszfs, char *sharepath) 18855331Samw { 18865331Samw char *drive; 18875331Samw char *exclude; 18885331Samw sa_resource_t resource = NULL; 18895331Samw char *description; 18905331Samw char *rsrcname; 18915331Samw int rsrcwithopt; 18925885Sdougm boolean_t multiple; 18935331Samw char *type; 18945331Samw 18955331Samw rsrcwithopt = has_resource_with_opt(share); 18965331Samw 18975331Samw if (verbose || (properties && rsrcwithopt)) { 18985331Samw /* First, indicate if transient */ 18995331Samw type = sa_get_share_attr(share, "type"); 19005331Samw if (type != NULL && !iszfs && verbose && 19015331Samw strcmp(type, "transient") == 0) 19025331Samw (void) printf("\t* "); 19035331Samw else 19045331Samw (void) printf("\t "); 19055331Samw 19065331Samw if (type != NULL) 19075331Samw sa_free_attr_string(type); 19085331Samw 19095331Samw /* 19105331Samw * If we came in with verbose, we want to handle the case of 19115331Samw * multiple resources as though they had properties set. 19125331Samw */ 19135331Samw multiple = has_multiple_resource(share); 19145331Samw 19155885Sdougm /* 19165885Sdougm * if there is a description on the share and there 19175885Sdougm * are resources, treat as multiple resources in order 19185885Sdougm * to get all descriptions displayed. 19195885Sdougm */ 19205885Sdougm description = sa_get_share_description(share); 19215885Sdougm resource = sa_get_share_resource(share, NULL); 19225885Sdougm 19235885Sdougm if (description != NULL && resource != NULL) 19245885Sdougm multiple = B_TRUE; 19255885Sdougm 19265331Samw /* Next, if not multiple follow old model */ 19275331Samw if (!multiple && !rsrcwithopt) { 19285331Samw rsrcname = get_resource(share); 19295331Samw if (rsrcname != NULL && strlen(rsrcname) > 0) { 19305331Samw (void) printf("%s=%s", rsrcname, sharepath); 19315331Samw } else { 19325331Samw (void) printf("%s", sharepath); 19335331Samw } 19345331Samw if (rsrcname != NULL) 19355331Samw sa_free_attr_string(rsrcname); 19365885Sdougm /* Print the description string if there is one. */ 19375885Sdougm print_rsrc_desc(resource, description); 19385331Samw } else { 19395331Samw /* Treat as simple and then resources come later */ 19405331Samw (void) printf("%s", sharepath); 19415331Samw } 19425331Samw drive = sa_get_share_attr(share, "drive-letter"); 19435331Samw if (drive != NULL) { 19445331Samw if (strlen(drive) > 0) 19455331Samw (void) printf(gettext("\tdrive-letter=\"%s:\""), 19465331Samw drive); 19475331Samw sa_free_attr_string(drive); 19485331Samw } 19495331Samw if (properties) 19505331Samw show_properties(share, proto, "\t"); 19515331Samw exclude = sa_get_share_attr(share, "exclude"); 19525331Samw if (exclude != NULL) { 19535331Samw (void) printf(gettext("\tnot-shared-with=[%s]"), 19545331Samw exclude); 19555331Samw sa_free_attr_string(exclude); 19565331Samw } 19575885Sdougm 19585331Samw if (description != NULL) { 19595885Sdougm print_rsrc_desc((sa_resource_t)share, description); 19605331Samw } 19615331Samw /* 19625331Samw * If there are resource names with options, show them 19635331Samw * here, with one line per resource. Resource specific 19645331Samw * options are at the end of the line followed by 19655331Samw * description, if any. 19665331Samw */ 19675331Samw if (rsrcwithopt || multiple) { 19685331Samw for (resource = sa_get_share_resource(share, NULL); 19695331Samw resource != NULL; 19705331Samw resource = sa_get_next_resource(resource)) { 19715331Samw int has_space; 19725331Samw char *rsrc; 19735331Samw 19745331Samw (void) printf("\n\t\t "); 19755331Samw rsrcname = sa_get_resource_attr(resource, 19765331Samw "name"); 19775331Samw if (rsrcname == NULL) 19785331Samw continue; 19795331Samw 19805331Samw rsrc = conv_from_utf8(rsrcname); 19815331Samw has_space = strpbrk(rsrc, " ") != NULL; 19825331Samw 19835331Samw if (has_space) 19845331Samw (void) printf("\"%s\"=%s", rsrc, 19855331Samw sharepath); 19865331Samw else 19875331Samw (void) printf("%s=%s", rsrc, 19885331Samw sharepath); 19895331Samw if (rsrc != rsrcname) 19905331Samw sa_free_attr_string(rsrc); 19915331Samw sa_free_attr_string(rsrcname); 19925331Samw if (properties || rsrcwithopt) 19935331Samw show_properties(resource, proto, "\t"); 19945331Samw 19955331Samw /* Get description string if any */ 19965885Sdougm print_rsrc_desc(resource, description); 19975331Samw } 19985331Samw } 19995885Sdougm if (description != NULL) 20005885Sdougm sa_free_share_description(description); 20015331Samw } else { 20025331Samw (void) printf("\t %s", sharepath); 20035331Samw if (properties) 20045331Samw show_properties(share, proto, "\t"); 20055331Samw } 20065331Samw (void) printf("\n"); 20075331Samw } 20085331Samw 20095331Samw /* 20103034Sdougm * show_group(group, verbose, properties, proto, subgroup) 20113034Sdougm * 20123034Sdougm * helper function to show the contents of a group. 20133034Sdougm */ 20143034Sdougm 20153034Sdougm static void 20163034Sdougm show_group(sa_group_t group, int verbose, int properties, char *proto, 20175331Samw char *subgroup) 20183034Sdougm { 20193034Sdougm sa_share_t share; 20203034Sdougm char *groupname; 20213034Sdougm char *zfs = NULL; 20223034Sdougm int iszfs = 0; 20235331Samw char *sharepath; 20243034Sdougm 20253034Sdougm groupname = sa_get_group_attr(group, "name"); 20263034Sdougm if (groupname != NULL) { 20274653Sdougm if (proto != NULL && !has_protocol(group, proto)) { 20284653Sdougm sa_free_attr_string(groupname); 20294653Sdougm return; 20304653Sdougm } 20313034Sdougm /* 20323034Sdougm * check to see if the group is managed by ZFS. If 20333034Sdougm * there is an attribute, then it is. A non-NULL zfs 20343034Sdougm * variable will trigger the different way to display 20353034Sdougm * and will remove the transient property indicator 20363034Sdougm * from the output. 20373034Sdougm */ 20384653Sdougm zfs = sa_get_group_attr(group, "zfs"); 20394653Sdougm if (zfs != NULL) { 20404653Sdougm iszfs = 1; 20414653Sdougm sa_free_attr_string(zfs); 20423034Sdougm } 20434653Sdougm share = sa_get_share(group, NULL); 20444653Sdougm if (subgroup == NULL) 20454653Sdougm (void) printf("%s", groupname); 20464653Sdougm else 20474653Sdougm (void) printf(" %s/%s", subgroup, groupname); 20484653Sdougm if (properties) 20494653Sdougm show_properties(group, proto, ""); 20504653Sdougm (void) printf("\n"); 20514653Sdougm if (strcmp(groupname, "zfs") == 0) { 20524653Sdougm sa_group_t zgroup; 20534653Sdougm 20544653Sdougm for (zgroup = sa_get_sub_group(group); 20554653Sdougm zgroup != NULL; 20564653Sdougm zgroup = sa_get_next_group(zgroup)) { 20574653Sdougm show_group(zgroup, verbose, properties, proto, 20584653Sdougm "zfs"); 20594653Sdougm } 20604653Sdougm sa_free_attr_string(groupname); 20614653Sdougm return; 20624653Sdougm } 20633034Sdougm /* 20644653Sdougm * Have a group, so list the contents. Resource and 20653034Sdougm * description are only listed if verbose is set. 20663034Sdougm */ 20674653Sdougm for (share = sa_get_share(group, NULL); 20684653Sdougm share != NULL; 20694653Sdougm share = sa_get_next_share(share)) { 20704653Sdougm sharepath = sa_get_share_attr(share, "path"); 20714653Sdougm if (sharepath != NULL) { 20725331Samw show_share(share, verbose, properties, proto, 20735331Samw iszfs, sharepath); 20744653Sdougm sa_free_attr_string(sharepath); 20753034Sdougm } 20763034Sdougm } 20773034Sdougm } 20783034Sdougm if (groupname != NULL) { 20793034Sdougm sa_free_attr_string(groupname); 20803034Sdougm } 20813034Sdougm } 20823034Sdougm 20833034Sdougm /* 20843034Sdougm * show_group_xml_init() 20853034Sdougm * 20863034Sdougm * Create an XML document that will be used to display config info via 20873034Sdougm * XML format. 20883034Sdougm */ 20893034Sdougm 20903034Sdougm xmlDocPtr 20913034Sdougm show_group_xml_init() 20923034Sdougm { 20933034Sdougm xmlDocPtr doc; 20943034Sdougm xmlNodePtr root; 20953034Sdougm 20963034Sdougm doc = xmlNewDoc((xmlChar *)"1.0"); 20973034Sdougm if (doc != NULL) { 20984653Sdougm root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 20994653Sdougm if (root != NULL) 21004653Sdougm xmlDocSetRootElement(doc, root); 21013034Sdougm } 21023034Sdougm return (doc); 21033034Sdougm } 21043034Sdougm 21053034Sdougm /* 21063034Sdougm * show_group_xml(doc, group) 21073034Sdougm * 21083034Sdougm * Copy the group info into the XML doc. 21093034Sdougm */ 21103034Sdougm 21113034Sdougm static void 21123034Sdougm show_group_xml(xmlDocPtr doc, sa_group_t group) 21133034Sdougm { 21143034Sdougm xmlNodePtr node; 21153034Sdougm xmlNodePtr root; 21163034Sdougm 21173034Sdougm root = xmlDocGetRootElement(doc); 21183034Sdougm node = xmlCopyNode((xmlNodePtr)group, 1); 21193034Sdougm if (node != NULL && root != NULL) { 21204653Sdougm xmlAddChild(root, node); 21213034Sdougm /* 21223034Sdougm * In the future, we may have interally used tags that 21233034Sdougm * should not appear in the XML output. Remove 21243034Sdougm * anything we don't want to show here. 21253034Sdougm */ 21263034Sdougm } 21273034Sdougm } 21283034Sdougm 21293034Sdougm /* 21303034Sdougm * sa_show(flags, argc, argv) 21313034Sdougm * 21323034Sdougm * Implements the show subcommand. 21333034Sdougm */ 21343034Sdougm 21353034Sdougm int 21363910Sdougm sa_show(sa_handle_t handle, int flags, int argc, char *argv[]) 21373034Sdougm { 21383034Sdougm sa_group_t group; 21393034Sdougm int verbose = 0; 21403034Sdougm int properties = 0; 21413034Sdougm int c; 21423034Sdougm int ret = SA_OK; 21433034Sdougm char *protocol = NULL; 21443034Sdougm int xml = 0; 21453034Sdougm xmlDocPtr doc; 21465331Samw #ifdef lint 21475331Samw flags = flags; 21485331Samw #endif 21493034Sdougm 21503034Sdougm while ((c = getopt(argc, argv, "?hvP:px")) != EOF) { 21514653Sdougm switch (c) { 21524653Sdougm case 'v': 21534653Sdougm verbose++; 21544653Sdougm break; 21554653Sdougm case 'p': 21564653Sdougm properties++; 21574653Sdougm break; 21584653Sdougm case 'P': 21595331Samw if (protocol != NULL) { 21605331Samw (void) printf(gettext( 21615331Samw "Specifying multiple protocols " 21625331Samw "not supported: %s\n"), 21635331Samw protocol); 21645331Samw return (SA_SYNTAX_ERR); 21655331Samw } 21664653Sdougm protocol = optarg; 21674653Sdougm if (!sa_valid_protocol(protocol)) { 21684653Sdougm (void) printf(gettext( 21694653Sdougm "Invalid protocol specified: %s\n"), 21704653Sdougm protocol); 21714653Sdougm return (SA_INVALID_PROTOCOL); 21724653Sdougm } 21734653Sdougm break; 21744653Sdougm case 'x': 21754653Sdougm xml++; 21764653Sdougm break; 21774653Sdougm default: 21784653Sdougm case 'h': 21794653Sdougm case '?': 21804653Sdougm (void) printf(gettext("usage: %s\n"), 21814653Sdougm sa_get_usage(USAGE_SHOW)); 21824653Sdougm return (0); 21833034Sdougm } 21843034Sdougm } 21853034Sdougm 21863034Sdougm if (xml) { 21874653Sdougm doc = show_group_xml_init(); 21884653Sdougm if (doc == NULL) 21894653Sdougm ret = SA_NO_MEMORY; 21903034Sdougm } 21913034Sdougm 21923034Sdougm if (optind == argc) { 21934653Sdougm /* No group specified so go through them all */ 21944653Sdougm for (group = sa_get_group(handle, NULL); 21954653Sdougm group != NULL; 21964653Sdougm group = sa_get_next_group(group)) { 21974653Sdougm /* 21984653Sdougm * Have a group so check if one we want and then list 21994653Sdougm * contents with appropriate options. 22004653Sdougm */ 22014653Sdougm if (xml) 22024653Sdougm show_group_xml(doc, group); 22034653Sdougm else 22044653Sdougm show_group(group, verbose, properties, protocol, 22054653Sdougm NULL); 22064653Sdougm } 22073034Sdougm } else { 22084653Sdougm /* Have a specified list of groups */ 22094653Sdougm for (; optind < argc; optind++) { 22104653Sdougm group = sa_get_group(handle, argv[optind]); 22114653Sdougm if (group != NULL) { 22124653Sdougm if (xml) 22134653Sdougm show_group_xml(doc, group); 22144653Sdougm else 22154653Sdougm show_group(group, verbose, properties, 22164653Sdougm protocol, NULL); 22174653Sdougm } else { 22184653Sdougm (void) printf(gettext("%s: not found\n"), 22194653Sdougm argv[optind]); 22204653Sdougm ret = SA_NO_SUCH_GROUP; 22214653Sdougm } 22223034Sdougm } 22233034Sdougm } 22243034Sdougm if (xml && ret == SA_OK) { 22254653Sdougm xmlDocFormatDump(stdout, doc, 1); 22264653Sdougm xmlFreeDoc(doc); 22273034Sdougm } 22283034Sdougm return (ret); 22293034Sdougm 22303034Sdougm } 22313034Sdougm 22323034Sdougm /* 22333034Sdougm * enable_share(group, share, update_legacy) 22343034Sdougm * 22353034Sdougm * helper function to enable a share if the group is enabled. 22363034Sdougm */ 22373034Sdougm 22383034Sdougm static int 22393910Sdougm enable_share(sa_handle_t handle, sa_group_t group, sa_share_t share, 22405331Samw int update_legacy) 22413034Sdougm { 22423034Sdougm char *value; 22433034Sdougm int enabled; 22443034Sdougm sa_optionset_t optionset; 22455331Samw int err; 22463034Sdougm int ret = SA_OK; 22473034Sdougm char *zfs = NULL; 22483034Sdougm int iszfs = 0; 22495331Samw int isshare; 22503034Sdougm 22513034Sdougm /* 22523034Sdougm * need to enable this share if the group is enabled but not 22533034Sdougm * otherwise. The enable is also done on each protocol 22543034Sdougm * represented in the group. 22553034Sdougm */ 22563034Sdougm value = sa_get_group_attr(group, "state"); 22573034Sdougm enabled = value != NULL && strcmp(value, "enabled") == 0; 22583034Sdougm if (value != NULL) 22594653Sdougm sa_free_attr_string(value); 22603034Sdougm /* remove legacy config if necessary */ 22613034Sdougm if (update_legacy) 22625331Samw ret = sa_delete_legacy(share, NULL); 22633034Sdougm zfs = sa_get_group_attr(group, "zfs"); 22643034Sdougm if (zfs != NULL) { 22654653Sdougm iszfs++; 22664653Sdougm sa_free_attr_string(zfs); 22673034Sdougm } 22683034Sdougm 22693034Sdougm /* 22703034Sdougm * Step through each optionset at the group level and 22713034Sdougm * enable the share based on the protocol type. This 22723034Sdougm * works because protocols must be set on the group 22733034Sdougm * for the protocol to be enabled. 22743034Sdougm */ 22755331Samw isshare = sa_is_share(share); 22763034Sdougm for (optionset = sa_get_optionset(group, NULL); 22773034Sdougm optionset != NULL && ret == SA_OK; 22783034Sdougm optionset = sa_get_next_optionset(optionset)) { 22794653Sdougm value = sa_get_optionset_attr(optionset, "type"); 22804653Sdougm if (value != NULL) { 22815331Samw if (enabled) { 22825331Samw if (isshare) { 22835331Samw err = sa_enable_share(share, value); 22845331Samw } else { 22855331Samw err = sa_enable_resource(share, value); 22865331Samw if (err == SA_NOT_SUPPORTED) { 22875331Samw sa_share_t parent; 22885331Samw parent = sa_get_resource_parent( 22895331Samw share); 22905331Samw if (parent != NULL) 22915331Samw err = sa_enable_share( 22925331Samw parent, value); 22935331Samw } 22945331Samw } 22955331Samw if (err != SA_OK) { 22965331Samw ret = err; 22975331Samw (void) printf(gettext( 22985331Samw "Failed to enable share for " 22995331Samw "\"%s\": %s\n"), 23005331Samw value, sa_errorstr(ret)); 23015331Samw } 23025331Samw } 23035331Samw /* 23045331Samw * If we want to update the legacy, use a copy of 23055331Samw * share so we can avoid breaking the loop we are in 23065331Samw * since we might also need to go up the tree to the 23075331Samw * parent. 23085331Samw */ 23095331Samw if (update_legacy && !iszfs) { 23105331Samw sa_share_t update = share; 23115331Samw if (!sa_is_share(share)) { 23125331Samw update = sa_get_resource_parent(share); 23135331Samw } 23145331Samw (void) sa_update_legacy(update, value); 23155331Samw } 23164653Sdougm sa_free_attr_string(value); 23174653Sdougm } 23183034Sdougm } 23193034Sdougm if (ret == SA_OK) 23204653Sdougm (void) sa_update_config(handle); 23213034Sdougm return (ret); 23223034Sdougm } 23233034Sdougm 23243034Sdougm /* 23255331Samw * sa_require_resource(group) 23265331Samw * 23275331Samw * if any of the defined protocols on the group require resource 23285331Samw * names, then all shares must have them. 23295331Samw */ 23305331Samw 23315331Samw static int 23325331Samw sa_require_resource(sa_group_t group) 23335331Samw { 23345331Samw sa_optionset_t optionset; 23355331Samw 23365331Samw for (optionset = sa_get_optionset(group, NULL); 23375331Samw optionset != NULL; 23385331Samw optionset = sa_get_next_optionset(optionset)) { 23395331Samw char *proto; 23405331Samw 23415331Samw proto = sa_get_optionset_attr(optionset, "type"); 23425331Samw if (proto != NULL) { 23435331Samw uint64_t features; 23445331Samw 23455331Samw features = sa_proto_get_featureset(proto); 23465331Samw if (features & SA_FEATURE_RESOURCE) { 23475331Samw sa_free_attr_string(proto); 23485331Samw return (B_TRUE); 23495331Samw } 23505331Samw sa_free_attr_string(proto); 23515331Samw } 23525331Samw } 23535331Samw return (B_FALSE); 23545331Samw } 23555331Samw 23565331Samw /* 23573034Sdougm * sa_addshare(flags, argc, argv) 23583034Sdougm * 23593034Sdougm * implements add-share subcommand. 23603034Sdougm */ 23613034Sdougm 23625331Samw static int 23633910Sdougm sa_addshare(sa_handle_t handle, int flags, int argc, char *argv[]) 23643034Sdougm { 23653034Sdougm int verbose = 0; 23663034Sdougm int dryrun = 0; 23673034Sdougm int c; 23683034Sdougm int ret = SA_OK; 23693034Sdougm sa_group_t group; 23703034Sdougm sa_share_t share; 23715331Samw sa_resource_t resource = NULL; 23723034Sdougm char *sharepath = NULL; 23733034Sdougm char *description = NULL; 23745331Samw char *rsrcname = NULL; 23755331Samw char *rsrc = NULL; 23763034Sdougm int persist = SA_SHARE_PERMANENT; /* default to persist */ 23773034Sdougm int auth; 23783034Sdougm char dir[MAXPATHLEN]; 23793034Sdougm 23803034Sdougm while ((c = getopt(argc, argv, "?hvns:d:r:t")) != EOF) { 23814653Sdougm switch (c) { 23824653Sdougm case 'n': 23834653Sdougm dryrun++; 23844653Sdougm break; 23854653Sdougm case 'v': 23864653Sdougm verbose++; 23874653Sdougm break; 23884653Sdougm case 'd': 23894653Sdougm description = optarg; 23904653Sdougm break; 23914653Sdougm case 'r': 23925331Samw if (rsrcname != NULL) { 23935331Samw (void) printf(gettext("Adding multiple " 23945331Samw "resource names not" 23955331Samw " supported\n")); 23965331Samw return (SA_SYNTAX_ERR); 23975331Samw } 23985331Samw rsrcname = optarg; 23994653Sdougm break; 24004653Sdougm case 's': 24014653Sdougm /* 24024653Sdougm * Save share path into group. Currently limit 24034653Sdougm * to one share per command. 24044653Sdougm */ 24054653Sdougm if (sharepath != NULL) { 24064653Sdougm (void) printf(gettext( 24074653Sdougm "Adding multiple shares not supported\n")); 24085331Samw return (SA_SYNTAX_ERR); 24094653Sdougm } 24104653Sdougm sharepath = optarg; 24114653Sdougm break; 24124653Sdougm case 't': 24134653Sdougm persist = SA_SHARE_TRANSIENT; 24144653Sdougm break; 24154653Sdougm default: 24164653Sdougm case 'h': 24174653Sdougm case '?': 24184653Sdougm (void) printf(gettext("usage: %s\n"), 24194653Sdougm sa_get_usage(USAGE_ADD_SHARE)); 24204653Sdougm return (0); 24213034Sdougm } 24223034Sdougm } 24233034Sdougm 24243034Sdougm if (optind >= argc) { 24254653Sdougm (void) printf(gettext("usage: %s\n"), 24264653Sdougm sa_get_usage(USAGE_ADD_SHARE)); 24274653Sdougm if (dryrun || sharepath != NULL || description != NULL || 24285331Samw rsrcname != NULL || verbose || persist) { 24294653Sdougm (void) printf(gettext("\tgroup must be specified\n")); 24304653Sdougm ret = SA_NO_SUCH_GROUP; 24314653Sdougm } else { 24324653Sdougm ret = SA_OK; 24334653Sdougm } 24343034Sdougm } else { 24354653Sdougm if (sharepath == NULL) { 24364653Sdougm (void) printf(gettext("usage: %s\n"), 24374653Sdougm sa_get_usage(USAGE_ADD_SHARE)); 24384653Sdougm (void) printf(gettext( 24394653Sdougm "\t-s sharepath must be specified\n")); 24405331Samw ret = SA_BAD_PATH; 24414653Sdougm } 24425331Samw if (ret == SA_OK) { 24435331Samw if (realpath(sharepath, dir) == NULL) { 24445331Samw ret = SA_BAD_PATH; 24455331Samw (void) printf(gettext("Path " 24465331Samw "is not valid: %s\n"), 24475331Samw sharepath); 24485331Samw } else { 24495331Samw sharepath = dir; 24505331Samw } 24513034Sdougm } 24525331Samw if (ret == SA_OK && rsrcname != NULL) { 24535331Samw /* check for valid syntax */ 24545331Samw if (validresource(rsrcname)) { 24555331Samw rsrc = conv_to_utf8(rsrcname); 24565331Samw resource = sa_find_resource(handle, rsrc); 24575331Samw if (resource != NULL) { 24585331Samw /* 24595331Samw * Resource names must be 24605331Samw * unique in the system 24615331Samw */ 24625331Samw ret = SA_DUPLICATE_NAME; 24635331Samw (void) printf(gettext("usage: %s\n"), 24645331Samw sa_get_usage(USAGE_ADD_SHARE)); 24655331Samw (void) printf(gettext( 24665331Samw "\tresource names must be unique " 24675331Samw "in the system\n")); 24685331Samw } 24695331Samw } else { 24705331Samw (void) printf(gettext("usage: %s\n"), 24715331Samw sa_get_usage(USAGE_ADD_SHARE)); 24725331Samw (void) printf(gettext( 24735331Samw "\tresource names use restricted " 24745331Samw "character set\n")); 24755331Samw ret = SA_INVALID_NAME; 24765331Samw } 24773034Sdougm } 24785331Samw 24795331Samw if (ret != SA_OK) { 24805331Samw if (rsrc != NULL && rsrcname != rsrc) 24815331Samw sa_free_attr_string(rsrc); 24825331Samw return (ret); 24834653Sdougm } 24845331Samw 24854653Sdougm share = sa_find_share(handle, sharepath); 24864653Sdougm if (share != NULL) { 24875331Samw if (rsrcname == NULL) { 24885331Samw /* 24895331Samw * Can only have a duplicate share if a new 24905331Samw * resource name is being added. 24915331Samw */ 24925331Samw ret = SA_DUPLICATE_NAME; 24935331Samw (void) printf(gettext("Share path already " 24945331Samw "shared: %s\n"), sharepath); 24955331Samw } 24965331Samw } 24975331Samw if (ret != SA_OK) 24985331Samw return (ret); 24995331Samw 25005331Samw group = sa_get_group(handle, argv[optind]); 25015331Samw if (group != NULL) { 25025331Samw if (sa_require_resource(group) == B_TRUE && 25035331Samw rsrcname == NULL) { 25045331Samw (void) printf(gettext( 25055331Samw "Resource name is required " 25065331Samw "by at least one enabled protocol " 25075331Samw "in group\n")); 25085331Samw return (SA_RESOURCE_REQUIRED); 25095331Samw } 25105331Samw if (share == NULL && ret == SA_OK) { 25115331Samw if (dryrun) 25125331Samw ret = sa_check_path(group, sharepath, 25135331Samw SA_CHECK_NORMAL); 25145331Samw else 25155331Samw share = sa_add_share(group, sharepath, 25165331Samw persist, &ret); 25175331Samw } 25185331Samw /* 25195331Samw * Make sure this isn't an attempt to put a resourced 25205331Samw * share into a different group than it already is in. 25215331Samw */ 25225331Samw if (share != NULL) { 25235331Samw sa_group_t parent; 25245331Samw parent = sa_get_parent_group(share); 25255331Samw if (parent != group) { 25265331Samw ret = SA_DUPLICATE_NAME; 25274653Sdougm (void) printf(gettext( 25284653Sdougm "Share path already " 25295331Samw "shared: %s\n"), sharepath); 25304653Sdougm } 25313034Sdougm } 25323034Sdougm if (!dryrun && share == NULL) { 25334653Sdougm (void) printf(gettext( 25344653Sdougm "Could not add share: %s\n"), 25354653Sdougm sa_errorstr(ret)); 25363034Sdougm } else { 25375331Samw auth = check_authorizations(argv[optind], 25385331Samw flags); 25394653Sdougm if (!dryrun && ret == SA_OK) { 25405331Samw if (rsrcname != NULL) { 25415331Samw resource = sa_add_resource( 25425331Samw share, 25435331Samw rsrc, 25445331Samw SA_SHARE_PERMANENT, 25455331Samw &ret); 25464653Sdougm } 25474653Sdougm if (ret == SA_OK && 25484653Sdougm description != NULL) { 25495885Sdougm if (resource != NULL) 25505885Sdougm ret = 25515885Sdougm set_resource_desc( 25525885Sdougm resource, 25535885Sdougm description); 25545885Sdougm else 25555331Samw ret = 25565331Samw set_share_desc( 25575331Samw share, 25585331Samw description); 25594653Sdougm } 25604653Sdougm if (ret == SA_OK) { 25615331Samw /* now enable the share(s) */ 25625331Samw if (resource != NULL) { 25635331Samw ret = enable_share( 25645331Samw handle, 25655331Samw group, 25665331Samw resource, 25675331Samw 1); 25685331Samw } else { 25695331Samw ret = enable_share( 25705331Samw handle, 25715331Samw group, 25725331Samw share, 25735331Samw 1); 25745331Samw } 25754653Sdougm ret = sa_update_config(handle); 25764653Sdougm } 25774653Sdougm switch (ret) { 25784653Sdougm case SA_DUPLICATE_NAME: 25794653Sdougm (void) printf(gettext( 25804653Sdougm "Resource name in" 25815331Samw "use: %s\n"), 25825331Samw rsrcname); 25834653Sdougm break; 25844653Sdougm default: 25855331Samw (void) printf(gettext( 25865331Samw "Could not set " 25874653Sdougm "attribute: %s\n"), 25884653Sdougm sa_errorstr(ret)); 25894653Sdougm break; 25904653Sdougm case SA_OK: 25914653Sdougm break; 25924653Sdougm } 25935331Samw } else if (dryrun && ret == SA_OK && 25945331Samw !auth && verbose) { 25954653Sdougm (void) printf(gettext( 25964653Sdougm "Command would fail: %s\n"), 25974653Sdougm sa_errorstr(SA_NO_PERMISSION)); 25984653Sdougm ret = SA_NO_PERMISSION; 25993034Sdougm } 26003034Sdougm } 26015331Samw } else { 26025331Samw switch (ret) { 26035331Samw default: 26045331Samw (void) printf(gettext( 26055331Samw "Group \"%s\" not found\n"), argv[optind]); 26065331Samw ret = SA_NO_SUCH_GROUP; 26075331Samw break; 26085331Samw case SA_BAD_PATH: 26095331Samw case SA_DUPLICATE_NAME: 26105331Samw break; 26115331Samw } 26123034Sdougm } 26133034Sdougm } 26143034Sdougm return (ret); 26153034Sdougm } 26163034Sdougm 26173034Sdougm /* 26183034Sdougm * sa_moveshare(flags, argc, argv) 26193034Sdougm * 26203034Sdougm * implements move-share subcommand. 26213034Sdougm */ 26223034Sdougm 26233034Sdougm int 26243910Sdougm sa_moveshare(sa_handle_t handle, int flags, int argc, char *argv[]) 26253034Sdougm { 26263034Sdougm int verbose = 0; 26273034Sdougm int dryrun = 0; 26283034Sdougm int c; 26293034Sdougm int ret = SA_OK; 26303034Sdougm sa_group_t group; 26313034Sdougm sa_share_t share; 26325331Samw char *rsrcname = NULL; 26333034Sdougm char *sharepath = NULL; 26343034Sdougm int authsrc = 0, authdst = 0; 26355885Sdougm char dir[MAXPATHLEN]; 26363034Sdougm 26375331Samw while ((c = getopt(argc, argv, "?hvnr:s:")) != EOF) { 26384653Sdougm switch (c) { 26394653Sdougm case 'n': 26404653Sdougm dryrun++; 26414653Sdougm break; 26424653Sdougm case 'v': 26434653Sdougm verbose++; 26444653Sdougm break; 26455331Samw case 'r': 26465331Samw if (rsrcname != NULL) { 26475331Samw (void) printf(gettext( 26485331Samw "Moving multiple resource names not" 26495331Samw " supported\n")); 26505331Samw return (SA_SYNTAX_ERR); 26515331Samw } 26525331Samw rsrcname = optarg; 26535331Samw break; 26544653Sdougm case 's': 26554653Sdougm /* 26564653Sdougm * Remove share path from group. Currently limit 26574653Sdougm * to one share per command. 26584653Sdougm */ 26594653Sdougm if (sharepath != NULL) { 26604653Sdougm (void) printf(gettext("Moving multiple shares" 26615331Samw " not supported\n")); 26625331Samw return (SA_SYNTAX_ERR); 26634653Sdougm } 26644653Sdougm sharepath = optarg; 26654653Sdougm break; 26664653Sdougm default: 26674653Sdougm case 'h': 26684653Sdougm case '?': 26694653Sdougm (void) printf(gettext("usage: %s\n"), 26704653Sdougm sa_get_usage(USAGE_MOVE_SHARE)); 26714653Sdougm return (0); 26723034Sdougm } 26733034Sdougm } 26743034Sdougm 26753034Sdougm if (optind >= argc || sharepath == NULL) { 26765331Samw (void) printf(gettext("usage: %s\n"), 26775331Samw sa_get_usage(USAGE_MOVE_SHARE)); 26785331Samw if (dryrun || verbose || sharepath != NULL) { 26795331Samw (void) printf(gettext("\tgroup must be specified\n")); 26805331Samw ret = SA_NO_SUCH_GROUP; 26815331Samw } else { 26825331Samw if (sharepath == NULL) { 26835331Samw ret = SA_SYNTAX_ERR; 26844653Sdougm (void) printf(gettext( 26855331Samw "\tsharepath must be specified\n")); 26864653Sdougm } else { 26875331Samw ret = SA_OK; 26884653Sdougm } 26895331Samw } 26904653Sdougm } else { 26914653Sdougm sa_group_t parent; 26924653Sdougm char *zfsold; 26934653Sdougm char *zfsnew; 26944653Sdougm 26953034Sdougm if (sharepath == NULL) { 26964653Sdougm (void) printf(gettext( 26974653Sdougm "sharepath must be specified with the -s " 26984653Sdougm "option\n")); 26994653Sdougm return (SA_BAD_PATH); 27004653Sdougm } 27013910Sdougm group = sa_get_group(handle, argv[optind]); 27024653Sdougm if (group == NULL) { 27034653Sdougm (void) printf(gettext("Group \"%s\" not found\n"), 27044653Sdougm argv[optind]); 27054653Sdougm return (SA_NO_SUCH_GROUP); 27064653Sdougm } 27074653Sdougm share = sa_find_share(handle, sharepath); 27085885Sdougm /* 27095885Sdougm * If a share wasn't found, it may have been a symlink 27105885Sdougm * or has a trailing '/'. Try again after resolving 27115885Sdougm * with realpath(). 27125885Sdougm */ 27135885Sdougm if (share == NULL) { 27145885Sdougm if (realpath(sharepath, dir) == NULL) { 27155885Sdougm (void) printf(gettext("Path " 27165885Sdougm "is not valid: %s\n"), 27175885Sdougm sharepath); 27185885Sdougm return (SA_BAD_PATH); 27195885Sdougm } 27205885Sdougm sharepath = dir; 27215885Sdougm share = sa_find_share(handle, sharepath); 27225885Sdougm } 27234653Sdougm if (share == NULL) { 27243034Sdougm (void) printf(gettext("Share not found: %s\n"), 27254653Sdougm sharepath); 27264653Sdougm return (SA_NO_SUCH_PATH); 27274653Sdougm } 27285885Sdougm authdst = check_authorizations(argv[optind], flags); 27294653Sdougm 27304653Sdougm parent = sa_get_parent_group(share); 27314653Sdougm if (parent != NULL) { 27324653Sdougm char *pname; 27334653Sdougm pname = sa_get_group_attr(parent, "name"); 27344653Sdougm if (pname != NULL) { 27353034Sdougm authsrc = check_authorizations(pname, flags); 27363034Sdougm sa_free_attr_string(pname); 27374653Sdougm } 27384653Sdougm zfsold = sa_get_group_attr(parent, "zfs"); 27394653Sdougm zfsnew = sa_get_group_attr(group, "zfs"); 27404653Sdougm if ((zfsold != NULL && zfsnew == NULL) || 27414653Sdougm (zfsold == NULL && zfsnew != NULL)) { 27423034Sdougm ret = SA_NOT_ALLOWED; 27433034Sdougm } 27444653Sdougm if (zfsold != NULL) 27454653Sdougm sa_free_attr_string(zfsold); 27464653Sdougm if (zfsnew != NULL) 27474653Sdougm sa_free_attr_string(zfsnew); 27484653Sdougm } 27494653Sdougm 27504653Sdougm if (ret == SA_OK && parent != group && !dryrun) { 27514653Sdougm char *oldstate; 27524653Sdougm /* 27534653Sdougm * Note that the share may need to be 27545331Samw * "unshared" if the new group is disabled and 27555331Samw * the old was enabled or it may need to be 27565331Samw * share to update if the new group is 27575331Samw * enabled. We disable before the move and 27585331Samw * will have to enable after the move in order 27595331Samw * to cleanup entries for protocols that 27605331Samw * aren't in the new group. 27614653Sdougm */ 27624653Sdougm oldstate = sa_get_group_attr(parent, "state"); 27634653Sdougm 27644653Sdougm /* enable_share determines what to do */ 27655331Samw if (strcmp(oldstate, "enabled") == 0) 27663034Sdougm (void) sa_disable_share(share, NULL); 27675331Samw 27684653Sdougm if (oldstate != NULL) 27693034Sdougm sa_free_attr_string(oldstate); 27703034Sdougm } 27714653Sdougm 27725331Samw if (!dryrun && ret == SA_OK) 27735331Samw ret = sa_move_share(group, share); 27745331Samw 27755331Samw /* 27765331Samw * Reenable and update any config information. 27775331Samw */ 27785331Samw if (ret == SA_OK && parent != group && !dryrun) { 27795331Samw ret = sa_update_config(handle); 27805331Samw 27815331Samw (void) enable_share(handle, group, share, 1); 27825331Samw } 27835331Samw 27844653Sdougm if (ret != SA_OK) 27854653Sdougm (void) printf(gettext("Could not move share: %s\n"), 27864653Sdougm sa_errorstr(ret)); 27874653Sdougm 27884653Sdougm if (dryrun && ret == SA_OK && !(authsrc & authdst) && 27894653Sdougm verbose) { 27904653Sdougm (void) printf(gettext("Command would fail: %s\n"), 27914653Sdougm sa_errorstr(SA_NO_PERMISSION)); 27924653Sdougm } 27933034Sdougm } 27943034Sdougm return (ret); 27953034Sdougm } 27963034Sdougm 27973034Sdougm /* 27983034Sdougm * sa_removeshare(flags, argc, argv) 27993034Sdougm * 28003034Sdougm * implements remove-share subcommand. 28013034Sdougm */ 28023034Sdougm 28033034Sdougm int 28043910Sdougm sa_removeshare(sa_handle_t handle, int flags, int argc, char *argv[]) 28053034Sdougm { 28063034Sdougm int verbose = 0; 28073034Sdougm int dryrun = 0; 28083034Sdougm int force = 0; 28093034Sdougm int c; 28103034Sdougm int ret = SA_OK; 28113034Sdougm sa_group_t group; 28125331Samw sa_resource_t resource = NULL; 28135331Samw sa_share_t share = NULL; 28145331Samw char *rsrcname = NULL; 28153034Sdougm char *sharepath = NULL; 28163034Sdougm char dir[MAXPATHLEN]; 28173034Sdougm int auth; 28183034Sdougm 28195331Samw while ((c = getopt(argc, argv, "?hfnr:s:v")) != EOF) { 28204653Sdougm switch (c) { 28214653Sdougm case 'n': 28224653Sdougm dryrun++; 28234653Sdougm break; 28244653Sdougm case 'v': 28254653Sdougm verbose++; 28264653Sdougm break; 28274653Sdougm case 'f': 28284653Sdougm force++; 28294653Sdougm break; 28304653Sdougm case 's': 28314653Sdougm /* 28324653Sdougm * Remove share path from group. Currently limit 28334653Sdougm * to one share per command. 28344653Sdougm */ 28354653Sdougm if (sharepath != NULL) { 28364653Sdougm (void) printf(gettext( 28374653Sdougm "Removing multiple shares not " 28383034Sdougm "supported\n")); 28394653Sdougm return (SA_SYNTAX_ERR); 28404653Sdougm } 28414653Sdougm sharepath = optarg; 28424653Sdougm break; 28435331Samw case 'r': 28445331Samw /* 28455331Samw * Remove share from group if last resource or remove 28465331Samw * resource from share if multiple resources. 28475331Samw */ 28485331Samw if (rsrcname != NULL) { 28495331Samw (void) printf(gettext( 28505331Samw "Removing multiple resource names not " 28515331Samw "supported\n")); 28525331Samw return (SA_SYNTAX_ERR); 28535331Samw } 28545331Samw rsrcname = optarg; 28555331Samw break; 28564653Sdougm default: 28574653Sdougm case 'h': 28584653Sdougm case '?': 28594653Sdougm (void) printf(gettext("usage: %s\n"), 28604653Sdougm sa_get_usage(USAGE_REMOVE_SHARE)); 28614653Sdougm return (0); 28623034Sdougm } 28633034Sdougm } 28643034Sdougm 28655331Samw if (optind >= argc || (rsrcname == NULL && sharepath == NULL)) { 28665331Samw if (sharepath == NULL && rsrcname == NULL) { 28673034Sdougm (void) printf(gettext("usage: %s\n"), 28684653Sdougm sa_get_usage(USAGE_REMOVE_SHARE)); 28695331Samw (void) printf(gettext("\t-s sharepath or -r resource" 28705331Samw " must be specified\n")); 28714653Sdougm ret = SA_BAD_PATH; 28724653Sdougm } else { 28734653Sdougm ret = SA_OK; 28744653Sdougm } 28753034Sdougm } 28764653Sdougm if (ret != SA_OK) { 28774653Sdougm return (ret); 28784653Sdougm } 28794653Sdougm 28804653Sdougm if (optind < argc) { 28813034Sdougm if ((optind + 1) < argc) { 28824653Sdougm (void) printf(gettext("Extraneous group(s) at end of " 28834653Sdougm "command\n")); 28844653Sdougm ret = SA_SYNTAX_ERR; 28853034Sdougm } else { 28864653Sdougm group = sa_get_group(handle, argv[optind]); 28874653Sdougm if (group == NULL) { 28884653Sdougm (void) printf(gettext( 28894653Sdougm "Group \"%s\" not found\n"), argv[optind]); 28904653Sdougm ret = SA_NO_SUCH_GROUP; 28914653Sdougm } 28923034Sdougm } 28934653Sdougm } else { 28943034Sdougm group = NULL; 28954653Sdougm } 28964653Sdougm 28975331Samw if (rsrcname != NULL) { 28985331Samw resource = sa_find_resource(handle, rsrcname); 28995331Samw if (resource == NULL) { 29005331Samw ret = SA_NO_SUCH_RESOURCE; 29015331Samw (void) printf(gettext( 29025331Samw "Resource name not found for share: %s\n"), 29035331Samw rsrcname); 29045331Samw } 29055331Samw } 29065331Samw 29074653Sdougm /* 29084653Sdougm * Lookup the path in the internal configuration. Care 29094653Sdougm * must be taken to handle the case where the 29104653Sdougm * underlying path has been removed since we need to 29114653Sdougm * be able to deal with that as well. 29124653Sdougm */ 29134653Sdougm if (ret == SA_OK) { 29145331Samw if (sharepath != NULL) { 29155331Samw if (group != NULL) 29165331Samw share = sa_get_share(group, sharepath); 29175331Samw else 29185331Samw share = sa_find_share(handle, sharepath); 29195331Samw } 29205331Samw 29215331Samw if (resource != NULL) { 29225331Samw sa_share_t rsrcshare; 29235331Samw rsrcshare = sa_get_resource_parent(resource); 29245331Samw if (share == NULL) 29255331Samw share = rsrcshare; 29265331Samw else if (share != rsrcshare) { 29275331Samw ret = SA_NO_SUCH_RESOURCE; 29285331Samw (void) printf(gettext( 29295331Samw "Bad resource name for share: %s\n"), 29305331Samw rsrcname); 29315331Samw share = NULL; 29325331Samw } 29335331Samw } 29345331Samw 29353663Sdougm /* 29363663Sdougm * If we didn't find the share with the provided path, 29373663Sdougm * it may be a symlink so attempt to resolve it using 29383663Sdougm * realpath and try again. Realpath will resolve any 29393663Sdougm * symlinks and place them in "dir". Note that 29403663Sdougm * sharepath is only used for the lookup the first 29413663Sdougm * time and later for error messages. dir will be used 29423663Sdougm * on the second attempt. Once a share is found, all 29433663Sdougm * operations are based off of the share variable. 29443663Sdougm */ 29453663Sdougm if (share == NULL) { 29464653Sdougm if (realpath(sharepath, dir) == NULL) { 29474653Sdougm ret = SA_BAD_PATH; 29484653Sdougm (void) printf(gettext( 29494653Sdougm "Path is not valid: %s\n"), sharepath); 29504653Sdougm } else { 29514653Sdougm if (group != NULL) 29524653Sdougm share = sa_get_share(group, dir); 29534653Sdougm else 29544653Sdougm share = sa_find_share(handle, dir); 29554653Sdougm } 29563663Sdougm } 29574653Sdougm } 29584653Sdougm 29594653Sdougm /* 29604653Sdougm * If there hasn't been an error, there was likely a 29614653Sdougm * path found. If not, give the appropriate error 29624653Sdougm * message and set the return error. If it was found, 29634653Sdougm * then disable the share and then remove it from the 29644653Sdougm * configuration. 29654653Sdougm */ 29664653Sdougm if (ret != SA_OK) { 29674653Sdougm return (ret); 29684653Sdougm } 29694653Sdougm if (share == NULL) { 29704653Sdougm if (group != NULL) 29713034Sdougm (void) printf(gettext("Share not found in group %s:" 29724653Sdougm " %s\n"), argv[optind], sharepath); 29734653Sdougm else 29743034Sdougm (void) printf(gettext("Share not found: %s\n"), 29754653Sdougm sharepath); 29765331Samw ret = SA_NO_SUCH_PATH; 29774653Sdougm } else { 29784653Sdougm if (group == NULL) 29793034Sdougm group = sa_get_parent_group(share); 29804653Sdougm if (!dryrun) { 29813034Sdougm if (ret == SA_OK) { 29825331Samw if (resource != NULL) 29835331Samw ret = sa_disable_resource(resource, 29845331Samw NULL); 29855331Samw else 29865331Samw ret = sa_disable_share(share, NULL); 29873034Sdougm /* 29884653Sdougm * We don't care if it fails since it 29893663Sdougm * could be disabled already. Some 29903663Sdougm * unexpected errors could occur that 29913663Sdougm * prevent removal, so also check for 29923663Sdougm * force being set. 29933034Sdougm */ 29945331Samw if ((ret == SA_OK || ret == SA_NO_SUCH_PATH || 29955331Samw ret == SA_NOT_SUPPORTED || 29965331Samw ret == SA_SYSTEM_ERR || force) && 29975331Samw resource == NULL) 29985331Samw ret = sa_remove_share(share); 29995331Samw 30005331Samw if ((ret == SA_OK || ret == SA_NO_SUCH_PATH || 30014653Sdougm ret == SA_NOT_SUPPORTED || 30025331Samw ret == SA_SYSTEM_ERR || force) && 30035331Samw resource != NULL) { 30045331Samw ret = sa_remove_resource(resource); 30055331Samw if (ret == SA_OK) { 30065331Samw /* 30075331Samw * If this was the 30085331Samw * last one, remove 30095331Samw * the share as well. 30105331Samw */ 30115331Samw resource = 30125331Samw sa_get_share_resource( 30135331Samw share, NULL); 30145331Samw if (resource == NULL) 30155331Samw ret = sa_remove_share( 30165331Samw share); 30175331Samw } 30184653Sdougm } 30194653Sdougm if (ret == SA_OK) 30204653Sdougm ret = sa_update_config(handle); 30213034Sdougm } 30224653Sdougm if (ret != SA_OK) 30235331Samw (void) printf(gettext("Could not remove share:" 30245331Samw " %s\n"), sa_errorstr(ret)); 30254653Sdougm } else if (ret == SA_OK) { 30263034Sdougm char *pname; 30273034Sdougm pname = sa_get_group_attr(group, "name"); 30283034Sdougm if (pname != NULL) { 30294653Sdougm auth = check_authorizations(pname, flags); 30304653Sdougm sa_free_attr_string(pname); 30313034Sdougm } 30323034Sdougm if (!auth && verbose) { 30334653Sdougm (void) printf(gettext( 30344653Sdougm "Command would fail: %s\n"), 30354653Sdougm sa_errorstr(SA_NO_PERMISSION)); 30363034Sdougm } 30373034Sdougm } 30383034Sdougm } 30393034Sdougm return (ret); 30403034Sdougm } 30413034Sdougm 30423034Sdougm /* 30433034Sdougm * sa_set_share(flags, argc, argv) 30443034Sdougm * 30453034Sdougm * implements set-share subcommand. 30463034Sdougm */ 30473034Sdougm 30483034Sdougm int 30493910Sdougm sa_set_share(sa_handle_t handle, int flags, int argc, char *argv[]) 30503034Sdougm { 30513034Sdougm int dryrun = 0; 30523034Sdougm int c; 30533034Sdougm int ret = SA_OK; 30543034Sdougm sa_group_t group, sharegroup; 30555772Sas200622 sa_share_t share = NULL; 30565331Samw sa_resource_t resource = NULL; 30573034Sdougm char *sharepath = NULL; 30583034Sdougm char *description = NULL; 30595331Samw char *rsrcname = NULL; 30605331Samw char *rsrc = NULL; 30615331Samw char *newname = NULL; 30625331Samw char *newrsrc; 30635331Samw char *groupname = NULL; 30643034Sdougm int auth; 30653034Sdougm int verbose = 0; 30663034Sdougm 30673034Sdougm while ((c = getopt(argc, argv, "?hnd:r:s:")) != EOF) { 30684653Sdougm switch (c) { 30694653Sdougm case 'n': 30704653Sdougm dryrun++; 30714653Sdougm break; 30724653Sdougm case 'd': 30734653Sdougm description = optarg; 30744653Sdougm break; 30754653Sdougm case 'v': 30764653Sdougm verbose++; 30774653Sdougm break; 30785331Samw case 'r': 30795331Samw /* 30805331Samw * Update share by resource name 30815331Samw */ 30825331Samw if (rsrcname != NULL) { 30835331Samw (void) printf(gettext( 30845331Samw "Updating multiple resource names not " 30855331Samw "supported\n")); 30865331Samw return (SA_SYNTAX_ERR); 30875331Samw } 30885331Samw rsrcname = optarg; 30895331Samw break; 30904653Sdougm case 's': 30914653Sdougm /* 30924653Sdougm * Save share path into group. Currently limit 30934653Sdougm * to one share per command. 30944653Sdougm */ 30954653Sdougm if (sharepath != NULL) { 30964653Sdougm (void) printf(gettext( 30974653Sdougm "Updating multiple shares not " 30983034Sdougm "supported\n")); 30995331Samw return (SA_SYNTAX_ERR); 31004653Sdougm } 31014653Sdougm sharepath = optarg; 31024653Sdougm break; 31034653Sdougm default: 31044653Sdougm case 'h': 31054653Sdougm case '?': 31064653Sdougm (void) printf(gettext("usage: %s\n"), 31074653Sdougm sa_get_usage(USAGE_SET_SHARE)); 31084653Sdougm return (SA_OK); 31093034Sdougm } 31103034Sdougm } 31114653Sdougm 31125331Samw if (optind >= argc && sharepath == NULL && rsrcname == NULL) { 31134653Sdougm if (sharepath == NULL) { 31144653Sdougm (void) printf(gettext("usage: %s\n"), 31154653Sdougm sa_get_usage(USAGE_SET_SHARE)); 31164653Sdougm (void) printf(gettext("\tgroup must be specified\n")); 31174653Sdougm ret = SA_BAD_PATH; 31184653Sdougm } else { 31194653Sdougm ret = SA_OK; 31204653Sdougm } 31213034Sdougm } 31223034Sdougm if ((optind + 1) < argc) { 31234653Sdougm (void) printf(gettext("usage: %s\n"), 31244653Sdougm sa_get_usage(USAGE_SET_SHARE)); 31254653Sdougm (void) printf(gettext("\tExtraneous group(s) at end\n")); 31264653Sdougm ret = SA_SYNTAX_ERR; 31273034Sdougm } 31284653Sdougm 31295331Samw /* 31305331Samw * Must have at least one of sharepath and rsrcrname. 31315331Samw * It is a syntax error to be missing both. 31325331Samw */ 31335331Samw if (sharepath == NULL && rsrcname == NULL) { 31345331Samw (void) printf(gettext("usage: %s\n"), 31355331Samw sa_get_usage(USAGE_SET_SHARE)); 31365331Samw ret = SA_SYNTAX_ERR; 31375331Samw } 31385331Samw 31394653Sdougm if (ret != SA_OK) 31404653Sdougm return (ret); 31414653Sdougm 31424653Sdougm if (optind < argc) { 31433034Sdougm groupname = argv[optind]; 31443910Sdougm group = sa_get_group(handle, groupname); 31454653Sdougm } else { 31463034Sdougm group = NULL; 31473034Sdougm groupname = NULL; 31484653Sdougm } 31495331Samw if (rsrcname != NULL) { 31505331Samw /* 31515331Samw * If rsrcname exists, split rename syntax and then 31525331Samw * convert to utf 8 if no errors. 31535331Samw */ 31545331Samw newname = strchr(rsrcname, '='); 31555331Samw if (newname != NULL) { 31565331Samw *newname++ = '\0'; 31575331Samw } 31585331Samw if (!validresource(rsrcname)) { 31595331Samw ret = SA_INVALID_NAME; 31605331Samw (void) printf(gettext("Invalid resource name: " 31615331Samw "\"%s\"\n"), rsrcname); 31625331Samw } else { 31635331Samw rsrc = conv_to_utf8(rsrcname); 31645331Samw } 31655331Samw if (newname != NULL) { 31665331Samw if (!validresource(newname)) { 31675331Samw ret = SA_INVALID_NAME; 31685331Samw (void) printf(gettext("Invalid resource name: " 31695331Samw "%s\n"), newname); 31705331Samw } else { 31715331Samw newrsrc = conv_to_utf8(newname); 31725331Samw } 31735331Samw } 31745331Samw } 31755331Samw 31765331Samw if (ret != SA_OK) { 31775331Samw if (rsrcname != NULL && rsrcname != rsrc) 31785331Samw sa_free_attr_string(rsrc); 31795331Samw if (newname != NULL && newname != newrsrc) 31805331Samw sa_free_attr_string(newrsrc); 31815331Samw return (ret); 31825331Samw } 31835331Samw 31845331Samw if (sharepath != NULL) { 31855331Samw share = sa_find_share(handle, sharepath); 31865331Samw } else if (rsrcname != NULL) { 31875331Samw resource = sa_find_resource(handle, rsrc); 31885772Sas200622 if (resource != NULL) 31895331Samw share = sa_get_resource_parent(resource); 31905772Sas200622 else 31915772Sas200622 ret = SA_NO_SUCH_RESOURCE; 31925331Samw } 31935331Samw if (share != NULL) { 31945331Samw sharegroup = sa_get_parent_group(share); 31955331Samw if (group != NULL && group != sharegroup) { 31965331Samw (void) printf(gettext("Group \"%s\" does not contain " 31975331Samw "share %s\n"), 31985331Samw argv[optind], sharepath); 31995331Samw ret = SA_BAD_PATH; 32005331Samw } else { 32015331Samw int delgroupname = 0; 32025331Samw if (groupname == NULL) { 32035331Samw groupname = sa_get_group_attr(sharegroup, 32045331Samw "name"); 32055331Samw delgroupname = 1; 32065331Samw } 32075331Samw if (groupname != NULL) { 32085331Samw auth = check_authorizations(groupname, flags); 32095331Samw if (delgroupname) { 32105331Samw sa_free_attr_string(groupname); 32115331Samw groupname = NULL; 32125331Samw } 32135331Samw } else { 32145331Samw ret = SA_NO_MEMORY; 32155331Samw } 32165331Samw if (rsrcname != NULL) { 32175331Samw resource = sa_find_resource(handle, rsrc); 32185331Samw if (!dryrun) { 32195331Samw if (newname != NULL && 32205331Samw resource != NULL) 32215331Samw ret = sa_rename_resource( 32225331Samw resource, newrsrc); 32235331Samw else if (newname != NULL) 32245331Samw ret = SA_NO_SUCH_RESOURCE; 32255331Samw if (newname != NULL && 32265331Samw newname != newrsrc) 32275331Samw sa_free_attr_string(newrsrc); 32285331Samw } 32295331Samw if (rsrc != rsrcname) 32305331Samw sa_free_attr_string(rsrc); 32315331Samw } 32325331Samw 32335331Samw /* 32345331Samw * If the user has set a description, it will be 32355331Samw * on the resource if -r was used otherwise it 32365331Samw * must be on the share. 32375331Samw */ 32385967Scp160787 if (!dryrun && ret == SA_OK && description != NULL) { 32395967Scp160787 char *desc; 32405967Scp160787 desc = conv_to_utf8(description); 32415331Samw if (resource != NULL) 32425967Scp160787 ret = sa_set_resource_description( 32435967Scp160787 resource, desc); 32445331Samw else 32455967Scp160787 ret = sa_set_share_description(share, 32465967Scp160787 desc); 32475967Scp160787 if (desc != description) 32485967Scp160787 sa_free_share_description(desc); 32495331Samw } 32505331Samw } 32515331Samw if (!dryrun && ret == SA_OK) { 32525331Samw if (resource != NULL) 32535331Samw (void) sa_enable_resource(resource, NULL); 32545331Samw ret = sa_update_config(handle); 32555331Samw } 32565331Samw switch (ret) { 32575331Samw case SA_DUPLICATE_NAME: 32585331Samw (void) printf(gettext("Resource name in use: %s\n"), 32595331Samw rsrcname); 32605331Samw break; 32615331Samw default: 32625331Samw (void) printf(gettext("Could not set: %s\n"), 32635331Samw sa_errorstr(ret)); 32645331Samw break; 32655331Samw case SA_OK: 32665331Samw if (dryrun && !auth && verbose) { 32675331Samw (void) printf(gettext( 32685331Samw "Command would fail: %s\n"), 32695331Samw sa_errorstr(SA_NO_PERMISSION)); 32705331Samw } 32715331Samw break; 32725331Samw } 32735331Samw } else { 32745772Sas200622 switch (ret) { 32755772Sas200622 case SA_NO_SUCH_RESOURCE: 32765772Sas200622 (void) printf(gettext("Resource \"%s\" not found\n"), 32775772Sas200622 rsrcname); 32785772Sas200622 break; 32795772Sas200622 default: 32805772Sas200622 if (sharepath != NULL) { 32815772Sas200622 (void) printf( 32825772Sas200622 gettext("Share path \"%s\" not found\n"), 32835772Sas200622 sharepath); 32845772Sas200622 ret = SA_NO_SUCH_PATH; 32855772Sas200622 } else { 32865772Sas200622 (void) printf(gettext("Set failed: %s\n"), 32875772Sas200622 sa_errorstr(ret)); 32885772Sas200622 } 32895772Sas200622 } 32903034Sdougm } 32914653Sdougm 32923034Sdougm return (ret); 32933034Sdougm } 32943034Sdougm 32953034Sdougm /* 32963034Sdougm * add_security(group, sectype, optlist, proto, *err) 32973034Sdougm * 32983034Sdougm * Helper function to add a security option (named optionset) to the 32993034Sdougm * group. 33003034Sdougm */ 33013034Sdougm 33023034Sdougm static int 33033034Sdougm add_security(sa_group_t group, char *sectype, 33045331Samw struct options *optlist, char *proto, int *err) 33053034Sdougm { 33063034Sdougm sa_security_t security; 33073034Sdougm int ret = SA_OK; 33083034Sdougm int result = 0; 33093034Sdougm 33103034Sdougm sectype = sa_proto_space_alias(proto, sectype); 33113034Sdougm security = sa_get_security(group, sectype, proto); 33124653Sdougm if (security == NULL) 33134653Sdougm security = sa_create_security(group, sectype, proto); 33144653Sdougm 33153034Sdougm if (sectype != NULL) 33164653Sdougm sa_free_attr_string(sectype); 33174653Sdougm 33184653Sdougm if (security == NULL) 33194653Sdougm return (ret); 33204653Sdougm 33214653Sdougm while (optlist != NULL) { 33223034Sdougm sa_property_t prop; 33233034Sdougm prop = sa_get_property(security, optlist->optname); 33243034Sdougm if (prop == NULL) { 33253034Sdougm /* 33264653Sdougm * Add the property, but only if it is 33273034Sdougm * a non-NULL or non-zero length value 33283034Sdougm */ 33294653Sdougm if (optlist->optvalue != NULL) { 33304653Sdougm prop = sa_create_property(optlist->optname, 33314653Sdougm optlist->optvalue); 33324653Sdougm if (prop != NULL) { 33335331Samw ret = sa_valid_property(security, 33345331Samw proto, prop); 33354653Sdougm if (ret != SA_OK) { 33364653Sdougm (void) sa_remove_property(prop); 33374653Sdougm (void) printf(gettext( 33384653Sdougm "Could not add " 33394653Sdougm "property %s: %s\n"), 33404653Sdougm optlist->optname, 33414653Sdougm sa_errorstr(ret)); 33424653Sdougm } 33434653Sdougm if (ret == SA_OK) { 33444653Sdougm ret = sa_add_property(security, 33454653Sdougm prop); 33464653Sdougm if (ret != SA_OK) { 33474653Sdougm (void) printf(gettext( 33484653Sdougm "Could not add " 33495331Samw "property (%s=%s):" 33505331Samw " %s\n"), 33514653Sdougm optlist->optname, 33524653Sdougm optlist->optvalue, 33534653Sdougm sa_errorstr(ret)); 33544653Sdougm } else { 33554653Sdougm result = 1; 33564653Sdougm } 33574653Sdougm } 33583034Sdougm } 33593034Sdougm } 33603034Sdougm } else { 33614653Sdougm ret = sa_update_property(prop, optlist->optvalue); 33624653Sdougm result = 1; /* should check if really changed */ 33633034Sdougm } 33643034Sdougm optlist = optlist->next; 33654653Sdougm } 33664653Sdougm /* 33674653Sdougm * When done, properties may have all been removed but 33684653Sdougm * we need to keep the security type itself until 33694653Sdougm * explicitly removed. 33704653Sdougm */ 33714653Sdougm if (result) 33723034Sdougm ret = sa_commit_properties(security, 0); 33733034Sdougm *err = ret; 33743034Sdougm return (result); 33753034Sdougm } 33763034Sdougm 33773034Sdougm /* 33785089Sdougm * zfscheck(group, share) 33795089Sdougm * 33805089Sdougm * For the special case where a share was provided, make sure it is a 33815089Sdougm * compatible path for a ZFS property change. The only path 33825089Sdougm * acceptable is the path that defines the zfs sub-group (dataset with 33835089Sdougm * the sharenfs property set) and not one of the paths that inherited 33845089Sdougm * the NFS properties. Returns SA_OK if it is usable and 33855089Sdougm * SA_NOT_ALLOWED if it isn't. 33865089Sdougm * 33875089Sdougm * If group is not a ZFS group/subgroup, we assume OK since the check 33885089Sdougm * on return will catch errors for those cases. What we are looking 33895089Sdougm * for here is that the group is ZFS and the share is not the defining 33905089Sdougm * share. All else is SA_OK. 33915089Sdougm */ 33925089Sdougm 33935089Sdougm static int 33945089Sdougm zfscheck(sa_group_t group, sa_share_t share) 33955089Sdougm { 33965089Sdougm int ret = SA_OK; 33975089Sdougm char *attr; 33985089Sdougm 33995089Sdougm if (sa_group_is_zfs(group)) { 34005089Sdougm /* 34015089Sdougm * The group is a ZFS group. Does the share represent 34025089Sdougm * the dataset that defined the group? It is only OK 34035089Sdougm * if the attribute "subgroup" exists on the share and 34045089Sdougm * has a value of "true". 34055089Sdougm */ 34065089Sdougm 34075089Sdougm ret = SA_NOT_ALLOWED; 34085089Sdougm attr = sa_get_share_attr(share, "subgroup"); 34095089Sdougm if (attr != NULL) { 34105089Sdougm if (strcmp(attr, "true") == 0) 34115089Sdougm ret = SA_OK; 34125089Sdougm sa_free_attr_string(attr); 34135089Sdougm } 34145089Sdougm } 34155089Sdougm return (ret); 34165089Sdougm } 34175089Sdougm 34185089Sdougm /* 34195331Samw * basic_set(groupname, optlist, protocol, sharepath, rsrcname, dryrun) 34203034Sdougm * 34213034Sdougm * This function implements "set" when a name space (-S) is not 34223034Sdougm * specified. It is a basic set. Options and other CLI parsing has 34233034Sdougm * already been done. 34245331Samw * 34255331Samw * "rsrcname" is a "resource name". If it is non-NULL, it must match 34265331Samw * the sharepath if present or group if present, otherwise it is used 34275331Samw * to set options. 34285331Samw * 34295331Samw * Resource names may take options if the protocol supports it. If the 34305331Samw * protocol doesn't support resource level options, rsrcname is just 34315331Samw * an alias for the share. 34323034Sdougm */ 34333034Sdougm 34343034Sdougm static int 34353910Sdougm basic_set(sa_handle_t handle, char *groupname, struct options *optlist, 34365331Samw char *protocol, char *sharepath, char *rsrcname, int dryrun) 34373034Sdougm { 34383034Sdougm sa_group_t group; 34393034Sdougm int ret = SA_OK; 34403034Sdougm int change = 0; 34413034Sdougm struct list *worklist = NULL; 34423034Sdougm 34433910Sdougm group = sa_get_group(handle, groupname); 34443034Sdougm if (group != NULL) { 34454653Sdougm sa_share_t share = NULL; 34465331Samw sa_resource_t resource = NULL; 34475331Samw 34485331Samw /* 34495331Samw * If there is a sharepath, make sure it belongs to 34505331Samw * the group. 34515331Samw */ 34524653Sdougm if (sharepath != NULL) { 34534653Sdougm share = sa_get_share(group, sharepath); 34544653Sdougm if (share == NULL) { 34554653Sdougm (void) printf(gettext( 34564653Sdougm "Share does not exist in group %s\n"), 34574653Sdougm groupname, sharepath); 34584653Sdougm ret = SA_NO_SUCH_PATH; 34595089Sdougm } else { 34605089Sdougm /* if ZFS and OK, then only group */ 34615089Sdougm ret = zfscheck(group, share); 34625089Sdougm if (ret == SA_OK && 34635089Sdougm sa_group_is_zfs(group)) 34645089Sdougm share = NULL; 34655089Sdougm if (ret == SA_NOT_ALLOWED) 34665089Sdougm (void) printf(gettext( 34675089Sdougm "Properties on ZFS group shares " 34685089Sdougm "not supported: %s\n"), sharepath); 34694653Sdougm } 34703034Sdougm } 34715331Samw 34725331Samw /* 34735331Samw * If a resource name exists, make sure it belongs to 34745331Samw * the share if present else it belongs to the 34755331Samw * group. Also check the protocol to see if it 34765331Samw * supports resource level properties or not. If not, 34775331Samw * use share only. 34785331Samw */ 34795331Samw if (rsrcname != NULL) { 34805331Samw if (share != NULL) { 34815331Samw resource = sa_get_share_resource(share, 34825331Samw rsrcname); 34835331Samw if (resource == NULL) 34845331Samw ret = SA_NO_SUCH_RESOURCE; 34855331Samw } else { 34865331Samw resource = sa_get_resource(group, rsrcname); 34875331Samw if (resource != NULL) 34885331Samw share = sa_get_resource_parent( 34895331Samw resource); 34905331Samw else 34915331Samw ret = SA_NO_SUCH_RESOURCE; 34925331Samw } 34935331Samw if (ret == SA_OK && resource != NULL) { 34945331Samw uint64_t features; 34955331Samw /* 34965331Samw * Check to see if the resource can take 34975331Samw * properties. If so, stick the resource into 34985331Samw * "share" so it will all just work. 34995331Samw */ 35005331Samw features = sa_proto_get_featureset(protocol); 35015331Samw if (features & SA_FEATURE_RESOURCE) 35025331Samw share = (sa_share_t)resource; 35035331Samw } 35045331Samw } 35055331Samw 35064653Sdougm if (ret == SA_OK) { 35074653Sdougm /* group must exist */ 35084653Sdougm ret = valid_options(optlist, protocol, 35094653Sdougm share == NULL ? group : share, NULL); 35104653Sdougm if (ret == SA_OK && !dryrun) { 35114653Sdougm if (share != NULL) 35124653Sdougm change |= add_optionset(share, optlist, 35134653Sdougm protocol, &ret); 35144653Sdougm else 35154653Sdougm change |= add_optionset(group, optlist, 35164653Sdougm protocol, &ret); 35174653Sdougm if (ret == SA_OK && change) 35184653Sdougm worklist = add_list(worklist, group, 35195331Samw share, protocol); 35204653Sdougm } 35213034Sdougm } 35224653Sdougm free_opt(optlist); 35233034Sdougm } else { 35243034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 35253034Sdougm ret = SA_NO_SUCH_GROUP; 35263034Sdougm } 35273034Sdougm /* 35283034Sdougm * we have a group and potentially legal additions 35293034Sdougm */ 35303034Sdougm 35314653Sdougm /* 35324653Sdougm * Commit to configuration if not a dryrunp and properties 35334653Sdougm * have changed. 35344653Sdougm */ 35354653Sdougm if (!dryrun && ret == SA_OK && change && worklist != NULL) 35363034Sdougm /* properties changed, so update all shares */ 35375331Samw (void) enable_all_groups(handle, worklist, 0, 0, protocol, 35385331Samw B_TRUE); 35394653Sdougm 35403034Sdougm if (worklist != NULL) 35414653Sdougm free_list(worklist); 35423034Sdougm return (ret); 35433034Sdougm } 35443034Sdougm 35453034Sdougm /* 35463034Sdougm * space_set(groupname, optlist, protocol, sharepath, dryrun) 35473034Sdougm * 35483034Sdougm * This function implements "set" when a name space (-S) is 35493034Sdougm * specified. It is a namespace set. Options and other CLI parsing has 35503034Sdougm * already been done. 35513034Sdougm */ 35523034Sdougm 35533034Sdougm static int 35543910Sdougm space_set(sa_handle_t handle, char *groupname, struct options *optlist, 35555331Samw char *protocol, char *sharepath, int dryrun, char *sectype) 35563034Sdougm { 35573034Sdougm sa_group_t group; 35583034Sdougm int ret = SA_OK; 35593034Sdougm int change = 0; 35603034Sdougm struct list *worklist = NULL; 35613034Sdougm 35623034Sdougm /* 35633034Sdougm * make sure protcol and sectype are valid 35643034Sdougm */ 35653034Sdougm 35663034Sdougm if (sa_proto_valid_space(protocol, sectype) == 0) { 35674653Sdougm (void) printf(gettext("Option space \"%s\" not valid " 35684653Sdougm "for protocol.\n"), sectype); 35694653Sdougm return (SA_INVALID_SECURITY); 35703034Sdougm } 35713034Sdougm 35723910Sdougm group = sa_get_group(handle, groupname); 35733034Sdougm if (group != NULL) { 35744653Sdougm sa_share_t share = NULL; 35754653Sdougm if (sharepath != NULL) { 35764653Sdougm share = sa_get_share(group, sharepath); 35774653Sdougm if (share == NULL) { 35784653Sdougm (void) printf(gettext( 35794653Sdougm "Share does not exist in group %s\n"), 35804653Sdougm groupname, sharepath); 35814653Sdougm ret = SA_NO_SUCH_PATH; 35825089Sdougm } else { 35835089Sdougm /* if ZFS and OK, then only group */ 35845089Sdougm ret = zfscheck(group, share); 35855089Sdougm if (ret == SA_OK && 35865089Sdougm sa_group_is_zfs(group)) 35875089Sdougm share = NULL; 35885089Sdougm if (ret == SA_NOT_ALLOWED) 35895089Sdougm (void) printf(gettext( 35905089Sdougm "Properties on ZFS group shares " 35915089Sdougm "not supported: %s\n"), sharepath); 35924653Sdougm } 35933034Sdougm } 35944653Sdougm if (ret == SA_OK) { 35954653Sdougm /* group must exist */ 35964653Sdougm ret = valid_options(optlist, protocol, 35974653Sdougm share == NULL ? group : share, sectype); 35984653Sdougm if (ret == SA_OK && !dryrun) { 35994653Sdougm if (share != NULL) 36004653Sdougm change = add_security(share, sectype, 36014653Sdougm optlist, protocol, &ret); 36024653Sdougm else 36034653Sdougm change = add_security(group, sectype, 36044653Sdougm optlist, protocol, &ret); 36054653Sdougm if (ret != SA_OK) 36064653Sdougm (void) printf(gettext( 36074653Sdougm "Could not set property: %s\n"), 36084653Sdougm sa_errorstr(ret)); 36094653Sdougm } 36104653Sdougm if (ret == SA_OK && change) 36115331Samw worklist = add_list(worklist, group, share, 36125331Samw protocol); 36133034Sdougm } 36144653Sdougm free_opt(optlist); 36153034Sdougm } else { 36163034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 36173034Sdougm ret = SA_NO_SUCH_GROUP; 36183034Sdougm } 36195331Samw 36203034Sdougm /* 36215331Samw * We have a group and potentially legal additions. 36223034Sdougm */ 36233034Sdougm 36244653Sdougm /* Commit to configuration if not a dryrun */ 36253034Sdougm if (!dryrun && ret == 0) { 36264653Sdougm if (change && worklist != NULL) { 36274653Sdougm /* properties changed, so update all shares */ 36284653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 36295331Samw protocol, B_TRUE); 36304653Sdougm } 36314653Sdougm ret = sa_update_config(handle); 36323034Sdougm } 36333034Sdougm if (worklist != NULL) 36344653Sdougm free_list(worklist); 36353034Sdougm return (ret); 36363034Sdougm } 36373034Sdougm 36383034Sdougm /* 36393034Sdougm * sa_set(flags, argc, argv) 36403034Sdougm * 36413034Sdougm * Implements the set subcommand. It keys off of -S to determine which 36423034Sdougm * set of operations to actually do. 36433034Sdougm */ 36443034Sdougm 36453034Sdougm int 36463910Sdougm sa_set(sa_handle_t handle, int flags, int argc, char *argv[]) 36473034Sdougm { 36483034Sdougm char *groupname; 36493034Sdougm int verbose = 0; 36503034Sdougm int dryrun = 0; 36513034Sdougm int c; 36523034Sdougm char *protocol = NULL; 36533034Sdougm int ret = SA_OK; 36543034Sdougm struct options *optlist = NULL; 36555331Samw char *rsrcname = NULL; 36563034Sdougm char *sharepath = NULL; 36573034Sdougm char *optset = NULL; 36583034Sdougm int auth; 36593034Sdougm 36605331Samw while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) { 36614653Sdougm switch (c) { 36624653Sdougm case 'v': 36634653Sdougm verbose++; 36644653Sdougm break; 36654653Sdougm case 'n': 36664653Sdougm dryrun++; 36674653Sdougm break; 36684653Sdougm case 'P': 36695331Samw if (protocol != NULL) { 36705331Samw (void) printf(gettext( 36715331Samw "Specifying multiple protocols " 36725331Samw "not supported: %s\n"), protocol); 36735331Samw return (SA_SYNTAX_ERR); 36745331Samw } 36754653Sdougm protocol = optarg; 36764653Sdougm if (!sa_valid_protocol(protocol)) { 36774653Sdougm (void) printf(gettext( 36784653Sdougm "Invalid protocol specified: %s\n"), 36794653Sdougm protocol); 36804653Sdougm return (SA_INVALID_PROTOCOL); 36814653Sdougm } 36824653Sdougm break; 36834653Sdougm case 'p': 36844653Sdougm ret = add_opt(&optlist, optarg, 0); 36854653Sdougm switch (ret) { 36864653Sdougm case OPT_ADD_SYNTAX: 36874653Sdougm (void) printf(gettext("Property syntax error:" 36884653Sdougm " %s\n"), optarg); 36894653Sdougm return (SA_SYNTAX_ERR); 36904653Sdougm case OPT_ADD_MEMORY: 36914653Sdougm (void) printf(gettext("No memory to set " 36924653Sdougm "property: %s\n"), optarg); 36934653Sdougm return (SA_NO_MEMORY); 36944653Sdougm default: 36954653Sdougm break; 36964653Sdougm } 36974653Sdougm break; 36985331Samw case 'r': 36995331Samw if (rsrcname != NULL) { 37005331Samw (void) printf(gettext( 37015331Samw "Setting multiple resource names not" 37025331Samw " supported\n")); 37035331Samw return (SA_SYNTAX_ERR); 37045331Samw } 37055331Samw rsrcname = optarg; 37065331Samw break; 37074653Sdougm case 's': 37085331Samw if (sharepath != NULL) { 37095331Samw (void) printf(gettext( 37105331Samw "Setting multiple shares not supported\n")); 37115331Samw return (SA_SYNTAX_ERR); 37125331Samw } 37134653Sdougm sharepath = optarg; 37144653Sdougm break; 37154653Sdougm case 'S': 37165331Samw if (optset != NULL) { 37175331Samw (void) printf(gettext( 37185331Samw "Specifying multiple property " 37195331Samw "spaces not supported: %s\n"), optset); 37205331Samw return (SA_SYNTAX_ERR); 37215331Samw } 37224653Sdougm optset = optarg; 37234653Sdougm break; 37244653Sdougm default: 37254653Sdougm case 'h': 37264653Sdougm case '?': 37274653Sdougm (void) printf(gettext("usage: %s\n"), 37284653Sdougm sa_get_usage(USAGE_SET)); 37294653Sdougm return (SA_OK); 37303034Sdougm } 37313034Sdougm } 37323034Sdougm 37333034Sdougm if (optlist != NULL) 37344653Sdougm ret = chk_opt(optlist, optset != NULL, protocol); 37353034Sdougm 37363034Sdougm if (optind >= argc || (optlist == NULL && optset == NULL) || 37374653Sdougm protocol == NULL || ret != OPT_ADD_OK) { 37384653Sdougm char *sep = "\t"; 37394653Sdougm 37404653Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET)); 37414653Sdougm if (optind >= argc) { 37424653Sdougm (void) printf(gettext("%sgroup must be specified"), 37434653Sdougm sep); 37444653Sdougm sep = ", "; 37454653Sdougm } 37464653Sdougm if (optlist == NULL) { 37474653Sdougm (void) printf(gettext("%sat least one property must be" 37484653Sdougm " specified"), sep); 37494653Sdougm sep = ", "; 37504653Sdougm } 37514653Sdougm if (protocol == NULL) { 37524653Sdougm (void) printf(gettext("%sprotocol must be specified"), 37534653Sdougm sep); 37544653Sdougm sep = ", "; 37554653Sdougm } 37564653Sdougm (void) printf("\n"); 37574653Sdougm ret = SA_SYNTAX_ERR; 37583034Sdougm } else { 37593034Sdougm /* 37605089Sdougm * Group already exists so we can proceed after a few 37615089Sdougm * additional checks related to ZFS handling. 37623034Sdougm */ 37633034Sdougm 37644653Sdougm groupname = argv[optind]; 37655089Sdougm if (strcmp(groupname, "zfs") == 0) { 37665089Sdougm (void) printf(gettext("Changing properties for group " 37675089Sdougm "\"zfs\" not allowed\n")); 37685089Sdougm return (SA_NOT_ALLOWED); 37695089Sdougm } 37705089Sdougm 37714653Sdougm auth = check_authorizations(groupname, flags); 37724653Sdougm if (optset == NULL) 37734653Sdougm ret = basic_set(handle, groupname, optlist, protocol, 37745331Samw sharepath, rsrcname, dryrun); 37754653Sdougm else 37764653Sdougm ret = space_set(handle, groupname, optlist, protocol, 37774653Sdougm sharepath, dryrun, optset); 37784653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 37794653Sdougm (void) printf(gettext("Command would fail: %s\n"), 37804653Sdougm sa_errorstr(SA_NO_PERMISSION)); 37814653Sdougm } 37823034Sdougm } 37833034Sdougm return (ret); 37843034Sdougm } 37853034Sdougm 37863034Sdougm /* 37873034Sdougm * remove_options(group, optlist, proto, *err) 37883034Sdougm * 37894653Sdougm * Helper function to actually remove options from a group after all 37903034Sdougm * preprocessing is done. 37913034Sdougm */ 37923034Sdougm 37933034Sdougm static int 37943034Sdougm remove_options(sa_group_t group, struct options *optlist, 37955331Samw char *proto, int *err) 37963034Sdougm { 37973034Sdougm struct options *cur; 37983034Sdougm sa_optionset_t optionset; 37993034Sdougm sa_property_t prop; 38003034Sdougm int change = 0; 38013034Sdougm int ret = SA_OK; 38023034Sdougm 38033034Sdougm optionset = sa_get_optionset(group, proto); 38043034Sdougm if (optionset != NULL) { 38054653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 38064653Sdougm prop = sa_get_property(optionset, cur->optname); 38074653Sdougm if (prop != NULL) { 38084653Sdougm ret = sa_remove_property(prop); 38094653Sdougm if (ret != SA_OK) 38104653Sdougm break; 38114653Sdougm change = 1; 38124653Sdougm } 38133034Sdougm } 38143034Sdougm } 38153034Sdougm if (ret == SA_OK && change) 38164653Sdougm ret = sa_commit_properties(optionset, 0); 38173034Sdougm 38183034Sdougm if (err != NULL) 38194653Sdougm *err = ret; 38203034Sdougm return (change); 38213034Sdougm } 38223034Sdougm 38233034Sdougm /* 38243034Sdougm * valid_unset(group, optlist, proto) 38253034Sdougm * 38263034Sdougm * Sanity check the optlist to make sure they can be removed. Issue an 38273034Sdougm * error if a property doesn't exist. 38283034Sdougm */ 38293034Sdougm 38303034Sdougm static int 38313034Sdougm valid_unset(sa_group_t group, struct options *optlist, char *proto) 38323034Sdougm { 38333034Sdougm struct options *cur; 38343034Sdougm sa_optionset_t optionset; 38353034Sdougm sa_property_t prop; 38363034Sdougm int ret = SA_OK; 38373034Sdougm 38383034Sdougm optionset = sa_get_optionset(group, proto); 38393034Sdougm if (optionset != NULL) { 38404653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 38414653Sdougm prop = sa_get_property(optionset, cur->optname); 38424653Sdougm if (prop == NULL) { 38434653Sdougm (void) printf(gettext( 38444653Sdougm "Could not unset property %s: not set\n"), 38454653Sdougm cur->optname); 38464653Sdougm ret = SA_NO_SUCH_PROP; 38474653Sdougm } 38483034Sdougm } 38493034Sdougm } 38503034Sdougm return (ret); 38513034Sdougm } 38523034Sdougm 38533034Sdougm /* 38543034Sdougm * valid_unset_security(group, optlist, proto) 38553034Sdougm * 38563034Sdougm * Sanity check the optlist to make sure they can be removed. Issue an 38573034Sdougm * error if a property doesn't exist. 38583034Sdougm */ 38593034Sdougm 38603034Sdougm static int 38613034Sdougm valid_unset_security(sa_group_t group, struct options *optlist, char *proto, 38625331Samw char *sectype) 38633034Sdougm { 38643034Sdougm struct options *cur; 38653034Sdougm sa_security_t security; 38663034Sdougm sa_property_t prop; 38673034Sdougm int ret = SA_OK; 38683034Sdougm char *sec; 38693034Sdougm 38703034Sdougm sec = sa_proto_space_alias(proto, sectype); 38713034Sdougm security = sa_get_security(group, sec, proto); 38723034Sdougm if (security != NULL) { 38734653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 38744653Sdougm prop = sa_get_property(security, cur->optname); 38754653Sdougm if (prop == NULL) { 38764653Sdougm (void) printf(gettext( 38774653Sdougm "Could not unset property %s: not set\n"), 38784653Sdougm cur->optname); 38794653Sdougm ret = SA_NO_SUCH_PROP; 38804653Sdougm } 38813034Sdougm } 38823034Sdougm } else { 38834653Sdougm (void) printf(gettext( 38844653Sdougm "Could not unset %s: space not defined\n"), sectype); 38854653Sdougm ret = SA_NO_SUCH_SECURITY; 38863034Sdougm } 38873034Sdougm if (sec != NULL) 38884653Sdougm sa_free_attr_string(sec); 38893034Sdougm return (ret); 38903034Sdougm } 38913034Sdougm 38923034Sdougm /* 38933034Sdougm * remove_security(group, optlist, proto) 38943034Sdougm * 38953034Sdougm * Remove the properties since they were checked as valid. 38963034Sdougm */ 38973034Sdougm 38983034Sdougm static int 38993034Sdougm remove_security(sa_group_t group, char *sectype, 39005331Samw struct options *optlist, char *proto, int *err) 39013034Sdougm { 39023034Sdougm sa_security_t security; 39033034Sdougm int ret = SA_OK; 39043034Sdougm int change = 0; 39053034Sdougm 39063034Sdougm sectype = sa_proto_space_alias(proto, sectype); 39073034Sdougm security = sa_get_security(group, sectype, proto); 39083034Sdougm if (sectype != NULL) 39094653Sdougm sa_free_attr_string(sectype); 39103034Sdougm 39113034Sdougm if (security != NULL) { 39124653Sdougm while (optlist != NULL) { 39134653Sdougm sa_property_t prop; 39144653Sdougm prop = sa_get_property(security, optlist->optname); 39154653Sdougm if (prop != NULL) { 39164653Sdougm ret = sa_remove_property(prop); 39174653Sdougm if (ret != SA_OK) 39184653Sdougm break; 39194653Sdougm change = 1; 39204653Sdougm } 39214653Sdougm optlist = optlist->next; 39223034Sdougm } 39233034Sdougm /* 39243034Sdougm * when done, properties may have all been removed but 39253034Sdougm * we need to keep the security type itself until 39263034Sdougm * explicitly removed. 39273034Sdougm */ 39284653Sdougm if (ret == SA_OK && change) 39294653Sdougm ret = sa_commit_properties(security, 0); 39303034Sdougm } else { 39314653Sdougm ret = SA_NO_SUCH_PROP; 39323034Sdougm } 39333034Sdougm if (err != NULL) 39344653Sdougm *err = ret; 39353034Sdougm return (change); 39363034Sdougm } 39373034Sdougm 39383034Sdougm /* 39395331Samw * basic_unset(groupname, optlist, protocol, sharepath, rsrcname, dryrun) 39403034Sdougm * 39414653Sdougm * Unset non-named optionset properties. 39423034Sdougm */ 39433034Sdougm 39443034Sdougm static int 39453910Sdougm basic_unset(sa_handle_t handle, char *groupname, struct options *optlist, 39465331Samw char *protocol, char *sharepath, char *rsrcname, int dryrun) 39473034Sdougm { 39483034Sdougm sa_group_t group; 39493034Sdougm int ret = SA_OK; 39503034Sdougm int change = 0; 39513034Sdougm struct list *worklist = NULL; 39524653Sdougm sa_share_t share = NULL; 39535331Samw sa_resource_t resource = NULL; 39543034Sdougm 39553910Sdougm group = sa_get_group(handle, groupname); 39564653Sdougm if (group == NULL) 39574653Sdougm return (ret); 39584653Sdougm 39595331Samw /* 39605331Samw * If there is a sharepath, make sure it belongs to 39615331Samw * the group. 39625331Samw */ 39634653Sdougm if (sharepath != NULL) { 39643034Sdougm share = sa_get_share(group, sharepath); 39653034Sdougm if (share == NULL) { 39664653Sdougm (void) printf(gettext( 39674653Sdougm "Share does not exist in group %s\n"), 39684653Sdougm groupname, sharepath); 39694653Sdougm ret = SA_NO_SUCH_PATH; 39703034Sdougm } 39714653Sdougm } 39725331Samw /* 39735331Samw * If a resource name exists, make sure it belongs to 39745331Samw * the share if present else it belongs to the 39755331Samw * group. Also check the protocol to see if it 39765331Samw * supports resource level properties or not. If not, 39775331Samw * use share only. 39785331Samw */ 39795331Samw if (rsrcname != NULL) { 39805331Samw if (share != NULL) { 39815331Samw resource = sa_get_share_resource(share, rsrcname); 39825331Samw if (resource == NULL) 39835331Samw ret = SA_NO_SUCH_RESOURCE; 39845331Samw } else { 39855331Samw resource = sa_get_resource(group, rsrcname); 39865331Samw if (resource != NULL) { 39875331Samw share = sa_get_resource_parent(resource); 39885331Samw } else { 39895331Samw ret = SA_NO_SUCH_RESOURCE; 39905331Samw } 39915331Samw } 39925331Samw if (ret == SA_OK && resource != NULL) { 39935331Samw uint64_t features; 39945331Samw /* 39955331Samw * Check to see if the resource can take 39965331Samw * properties. If so, stick the resource into 39975331Samw * "share" so it will all just work. 39985331Samw */ 39995331Samw features = sa_proto_get_featureset(protocol); 40005331Samw if (features & SA_FEATURE_RESOURCE) 40015331Samw share = (sa_share_t)resource; 40025331Samw } 40035331Samw } 40045331Samw 40054653Sdougm if (ret == SA_OK) { 40063034Sdougm /* group must exist */ 40073034Sdougm ret = valid_unset(share != NULL ? share : group, 40084653Sdougm optlist, protocol); 40093034Sdougm if (ret == SA_OK && !dryrun) { 40104653Sdougm if (share != NULL) { 40114653Sdougm sa_optionset_t optionset; 40124653Sdougm sa_property_t prop; 40134653Sdougm change |= remove_options(share, optlist, 40144653Sdougm protocol, &ret); 40154653Sdougm /* 40164653Sdougm * If a share optionset is 40174653Sdougm * empty, remove it. 40184653Sdougm */ 40194653Sdougm optionset = sa_get_optionset((sa_share_t)share, 40204653Sdougm protocol); 40214653Sdougm if (optionset != NULL) { 40224653Sdougm prop = sa_get_property(optionset, NULL); 40234653Sdougm if (prop == NULL) 40244653Sdougm (void) sa_destroy_optionset( 40254653Sdougm optionset); 40264653Sdougm } 40274653Sdougm } else { 40284653Sdougm change |= remove_options(group, 40294653Sdougm optlist, protocol, &ret); 40303034Sdougm } 40314653Sdougm if (ret == SA_OK && change) 40325331Samw worklist = add_list(worklist, group, share, 40335331Samw protocol); 40344653Sdougm if (ret != SA_OK) 40354653Sdougm (void) printf(gettext( 40364653Sdougm "Could not remove properties: " 40374653Sdougm "%s\n"), sa_errorstr(ret)); 40383034Sdougm } 40394653Sdougm } else { 40405331Samw (void) printf(gettext("Group \"%s\" not found\n"), groupname); 40413034Sdougm ret = SA_NO_SUCH_GROUP; 40423034Sdougm } 40434653Sdougm free_opt(optlist); 40443034Sdougm 40453034Sdougm /* 40464653Sdougm * We have a group and potentially legal additions 40474653Sdougm * 40484653Sdougm * Commit to configuration if not a dryrun 40493034Sdougm */ 40503034Sdougm if (!dryrun && ret == SA_OK) { 40514653Sdougm if (change && worklist != NULL) { 40524653Sdougm /* properties changed, so update all shares */ 40534653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 40545331Samw protocol, B_TRUE); 40554653Sdougm } 40563034Sdougm } 40573034Sdougm if (worklist != NULL) 40584653Sdougm free_list(worklist); 40593034Sdougm return (ret); 40603034Sdougm } 40613034Sdougm 40623034Sdougm /* 40633034Sdougm * space_unset(groupname, optlist, protocol, sharepath, dryrun) 40643034Sdougm * 40654653Sdougm * Unset named optionset properties. 40663034Sdougm */ 40673034Sdougm static int 40683910Sdougm space_unset(sa_handle_t handle, char *groupname, struct options *optlist, 40695331Samw char *protocol, char *sharepath, int dryrun, char *sectype) 40703034Sdougm { 40713034Sdougm sa_group_t group; 40723034Sdougm int ret = SA_OK; 40733034Sdougm int change = 0; 40743034Sdougm struct list *worklist = NULL; 40754653Sdougm sa_share_t share = NULL; 40763034Sdougm 40773910Sdougm group = sa_get_group(handle, groupname); 40784653Sdougm if (group == NULL) { 40794653Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 40804653Sdougm return (SA_NO_SUCH_GROUP); 40814653Sdougm } 40824653Sdougm if (sharepath != NULL) { 40833034Sdougm share = sa_get_share(group, sharepath); 40843034Sdougm if (share == NULL) { 40854653Sdougm (void) printf(gettext( 40864653Sdougm "Share does not exist in group %s\n"), 40874653Sdougm groupname, sharepath); 40884653Sdougm return (SA_NO_SUCH_PATH); 40893034Sdougm } 40904653Sdougm } 40915331Samw ret = valid_unset_security(share != NULL ? share : group, 40925331Samw optlist, protocol, sectype); 40934653Sdougm 40944653Sdougm if (ret == SA_OK && !dryrun) { 40954653Sdougm if (optlist != NULL) { 40963034Sdougm if (share != NULL) { 40974653Sdougm sa_security_t optionset; 40984653Sdougm sa_property_t prop; 40994653Sdougm change = remove_security(share, 41004653Sdougm sectype, optlist, protocol, &ret); 41014653Sdougm 41024653Sdougm /* If a share security is empty, remove it */ 41034653Sdougm optionset = sa_get_security((sa_group_t)share, 41044653Sdougm sectype, protocol); 41054653Sdougm if (optionset != NULL) { 41064653Sdougm prop = sa_get_property(optionset, 41074653Sdougm NULL); 41084653Sdougm if (prop == NULL) 41094653Sdougm ret = sa_destroy_security( 41104653Sdougm optionset); 41114653Sdougm } 41123034Sdougm } else { 41134653Sdougm change = remove_security(group, sectype, 41144653Sdougm optlist, protocol, &ret); 41153034Sdougm } 41164653Sdougm } else { 41173034Sdougm sa_security_t security; 41183034Sdougm char *sec; 41193034Sdougm sec = sa_proto_space_alias(protocol, sectype); 41203034Sdougm security = sa_get_security(group, sec, protocol); 41213034Sdougm if (sec != NULL) 41224653Sdougm sa_free_attr_string(sec); 41233034Sdougm if (security != NULL) { 41244653Sdougm ret = sa_destroy_security(security); 41254653Sdougm if (ret == SA_OK) 41264653Sdougm change = 1; 41273034Sdougm } else { 41284653Sdougm ret = SA_NO_SUCH_PROP; 41293034Sdougm } 41304653Sdougm } 41314653Sdougm if (ret != SA_OK) 41323034Sdougm (void) printf(gettext("Could not unset property: %s\n"), 41334653Sdougm sa_errorstr(ret)); 41343034Sdougm } 41354653Sdougm 41364653Sdougm if (ret == SA_OK && change) 41375331Samw worklist = add_list(worklist, group, 0, protocol); 41384653Sdougm 41393034Sdougm free_opt(optlist); 41403034Sdougm /* 41414653Sdougm * We have a group and potentially legal additions 41423034Sdougm */ 41433034Sdougm 41444653Sdougm /* Commit to configuration if not a dryrun */ 41453034Sdougm if (!dryrun && ret == 0) { 41463034Sdougm /* properties changed, so update all shares */ 41474653Sdougm if (change && worklist != NULL) 41484653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 41495331Samw protocol, B_TRUE); 41504653Sdougm ret = sa_update_config(handle); 41513034Sdougm } 41523034Sdougm if (worklist != NULL) 41534653Sdougm free_list(worklist); 41543034Sdougm return (ret); 41553034Sdougm } 41563034Sdougm 41573034Sdougm /* 41583034Sdougm * sa_unset(flags, argc, argv) 41593034Sdougm * 41604653Sdougm * Implements the unset subcommand. Parsing done here and then basic 41613034Sdougm * or space versions of the real code are called. 41623034Sdougm */ 41633034Sdougm 41643034Sdougm int 41653910Sdougm sa_unset(sa_handle_t handle, int flags, int argc, char *argv[]) 41663034Sdougm { 41673034Sdougm char *groupname; 41683034Sdougm int verbose = 0; 41693034Sdougm int dryrun = 0; 41703034Sdougm int c; 41713034Sdougm char *protocol = NULL; 41723034Sdougm int ret = SA_OK; 41733034Sdougm struct options *optlist = NULL; 41745331Samw char *rsrcname = NULL; 41753034Sdougm char *sharepath = NULL; 41763034Sdougm char *optset = NULL; 41773034Sdougm int auth; 41783034Sdougm 41795331Samw while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) { 41804653Sdougm switch (c) { 41814653Sdougm case 'v': 41824653Sdougm verbose++; 41834653Sdougm break; 41844653Sdougm case 'n': 41854653Sdougm dryrun++; 41864653Sdougm break; 41874653Sdougm case 'P': 41885331Samw if (protocol != NULL) { 41895331Samw (void) printf(gettext( 41905331Samw "Specifying multiple protocols " 41915331Samw "not supported: %s\n"), protocol); 41925331Samw return (SA_SYNTAX_ERR); 41935331Samw } 41944653Sdougm protocol = optarg; 41954653Sdougm if (!sa_valid_protocol(protocol)) { 41964653Sdougm (void) printf(gettext( 41974653Sdougm "Invalid protocol specified: %s\n"), 41984653Sdougm protocol); 41994653Sdougm return (SA_INVALID_PROTOCOL); 42004653Sdougm } 42014653Sdougm break; 42024653Sdougm case 'p': 42034653Sdougm ret = add_opt(&optlist, optarg, 1); 42044653Sdougm switch (ret) { 42054653Sdougm case OPT_ADD_SYNTAX: 42064653Sdougm (void) printf(gettext("Property syntax error " 42074653Sdougm "for property %s\n"), optarg); 42084653Sdougm return (SA_SYNTAX_ERR); 42094653Sdougm 42104653Sdougm case OPT_ADD_PROPERTY: 42114653Sdougm (void) printf(gettext("Properties need to be " 42124653Sdougm "set with set command: %s\n"), optarg); 42134653Sdougm return (SA_SYNTAX_ERR); 42144653Sdougm 42154653Sdougm default: 42164653Sdougm break; 42174653Sdougm } 42184653Sdougm break; 42195331Samw case 'r': 42205331Samw /* 42215331Samw * Unset properties on resource if applicable or on 42225331Samw * share if resource for this protocol doesn't use 42235331Samw * resources. 42245331Samw */ 42255331Samw if (rsrcname != NULL) { 42265331Samw (void) printf(gettext( 42275331Samw "Unsetting multiple resource " 42285331Samw "names not supported\n")); 42295331Samw return (SA_SYNTAX_ERR); 42305331Samw } 42315331Samw rsrcname = optarg; 42325331Samw break; 42334653Sdougm case 's': 42345331Samw if (sharepath != NULL) { 42355331Samw (void) printf(gettext( 42365331Samw "Adding multiple shares not supported\n")); 42375331Samw return (SA_SYNTAX_ERR); 42385331Samw } 42394653Sdougm sharepath = optarg; 42404653Sdougm break; 42414653Sdougm case 'S': 42425331Samw if (optset != NULL) { 42435331Samw (void) printf(gettext( 42445331Samw "Specifying multiple property " 42455331Samw "spaces not supported: %s\n"), optset); 42465331Samw return (SA_SYNTAX_ERR); 42475331Samw } 42484653Sdougm optset = optarg; 42494653Sdougm break; 42504653Sdougm default: 42514653Sdougm case 'h': 42524653Sdougm case '?': 42534653Sdougm (void) printf(gettext("usage: %s\n"), 42544653Sdougm sa_get_usage(USAGE_UNSET)); 42554653Sdougm return (SA_OK); 42563034Sdougm } 42573034Sdougm } 42583034Sdougm 42593034Sdougm if (optlist != NULL) 42604653Sdougm ret = chk_opt(optlist, optset != NULL, protocol); 42613034Sdougm 42623034Sdougm if (optind >= argc || (optlist == NULL && optset == NULL) || 42633034Sdougm protocol == NULL) { 42644653Sdougm char *sep = "\t"; 42654653Sdougm (void) printf(gettext("usage: %s\n"), 42664653Sdougm sa_get_usage(USAGE_UNSET)); 42674653Sdougm if (optind >= argc) { 42684653Sdougm (void) printf(gettext("%sgroup must be specified"), 42694653Sdougm sep); 42704653Sdougm sep = ", "; 42714653Sdougm } 42724653Sdougm if (optlist == NULL) { 42734653Sdougm (void) printf(gettext("%sat least one property must " 42744653Sdougm "be specified"), sep); 42754653Sdougm sep = ", "; 42764653Sdougm } 42774653Sdougm if (protocol == NULL) { 42784653Sdougm (void) printf(gettext("%sprotocol must be specified"), 42794653Sdougm sep); 42804653Sdougm sep = ", "; 42814653Sdougm } 42824653Sdougm (void) printf("\n"); 42834653Sdougm ret = SA_SYNTAX_ERR; 42843034Sdougm } else { 42853034Sdougm 42863034Sdougm /* 42874653Sdougm * If a group already exists, we can only add a new 42883034Sdougm * protocol to it and not create a new one or add the 42893034Sdougm * same protocol again. 42903034Sdougm */ 42913034Sdougm 42924653Sdougm groupname = argv[optind]; 42934653Sdougm auth = check_authorizations(groupname, flags); 42944653Sdougm if (optset == NULL) 42954653Sdougm ret = basic_unset(handle, groupname, optlist, protocol, 42965331Samw sharepath, rsrcname, dryrun); 42974653Sdougm else 42984653Sdougm ret = space_unset(handle, groupname, optlist, protocol, 42994653Sdougm sharepath, dryrun, optset); 43004653Sdougm 43014653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) 43024653Sdougm (void) printf(gettext("Command would fail: %s\n"), 43034653Sdougm sa_errorstr(SA_NO_PERMISSION)); 43043034Sdougm } 43053034Sdougm return (ret); 43063034Sdougm } 43073034Sdougm 43083034Sdougm /* 43093034Sdougm * sa_enable_group(flags, argc, argv) 43103034Sdougm * 43113034Sdougm * Implements the enable subcommand 43123034Sdougm */ 43133034Sdougm 43143034Sdougm int 43153910Sdougm sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[]) 43163034Sdougm { 43173034Sdougm int verbose = 0; 43183034Sdougm int dryrun = 0; 43193034Sdougm int all = 0; 43203034Sdougm int c; 43213034Sdougm int ret = SA_OK; 43223034Sdougm char *protocol = NULL; 43233034Sdougm char *state; 43243034Sdougm struct list *worklist = NULL; 43253034Sdougm int auth = 1; 43264653Sdougm sa_group_t group; 43273034Sdougm 43283034Sdougm while ((c = getopt(argc, argv, "?havnP:")) != EOF) { 43294653Sdougm switch (c) { 43304653Sdougm case 'a': 43314653Sdougm all = 1; 43324653Sdougm break; 43334653Sdougm case 'n': 43344653Sdougm dryrun++; 43354653Sdougm break; 43364653Sdougm case 'P': 43375331Samw if (protocol != NULL) { 43385331Samw (void) printf(gettext( 43395331Samw "Specifying multiple protocols " 43405331Samw "not supported: %s\n"), protocol); 43415331Samw return (SA_SYNTAX_ERR); 43425331Samw } 43434653Sdougm protocol = optarg; 43444653Sdougm if (!sa_valid_protocol(protocol)) { 43454653Sdougm (void) printf(gettext( 43464653Sdougm "Invalid protocol specified: %s\n"), 43473034Sdougm protocol); 43484653Sdougm return (SA_INVALID_PROTOCOL); 43494653Sdougm } 43504653Sdougm break; 43514653Sdougm case 'v': 43524653Sdougm verbose++; 43534653Sdougm break; 43544653Sdougm default: 43554653Sdougm case 'h': 43564653Sdougm case '?': 43574653Sdougm (void) printf(gettext("usage: %s\n"), 43584653Sdougm sa_get_usage(USAGE_ENABLE)); 43594653Sdougm return (0); 43603034Sdougm } 43613034Sdougm } 43623034Sdougm 43633034Sdougm if (optind == argc && !all) { 43644653Sdougm (void) printf(gettext("usage: %s\n"), 43654653Sdougm sa_get_usage(USAGE_ENABLE)); 43664653Sdougm (void) printf(gettext("\tmust specify group\n")); 43674653Sdougm return (SA_NO_SUCH_PATH); 43684653Sdougm } 43694653Sdougm if (!all) { 43703034Sdougm while (optind < argc) { 43714653Sdougm group = sa_get_group(handle, argv[optind]); 43724653Sdougm if (group != NULL) { 43734653Sdougm auth &= check_authorizations(argv[optind], 43744653Sdougm flags); 43754653Sdougm state = sa_get_group_attr(group, "state"); 43764653Sdougm if (state != NULL && 43774653Sdougm strcmp(state, "enabled") == 0) { 43784653Sdougm /* already enabled */ 43794653Sdougm if (verbose) 43804653Sdougm (void) printf(gettext( 43814653Sdougm "Group \"%s\" is already " 43824653Sdougm "enabled\n"), 43834653Sdougm argv[optind]); 43844653Sdougm ret = SA_BUSY; /* already enabled */ 43854653Sdougm } else { 43864653Sdougm worklist = add_list(worklist, group, 43875331Samw 0, protocol); 43884653Sdougm if (verbose) 43894653Sdougm (void) printf(gettext( 43904653Sdougm "Enabling group \"%s\"\n"), 43914653Sdougm argv[optind]); 43924653Sdougm } 43934653Sdougm if (state != NULL) 43944653Sdougm sa_free_attr_string(state); 43953034Sdougm } else { 43964653Sdougm ret = SA_NO_SUCH_GROUP; 43973034Sdougm } 43984653Sdougm optind++; 43993034Sdougm } 44004653Sdougm } else { 44014653Sdougm for (group = sa_get_group(handle, NULL); 44024653Sdougm group != NULL; 44033034Sdougm group = sa_get_next_group(group)) { 44045331Samw worklist = add_list(worklist, group, 0, protocol); 44053034Sdougm } 44064653Sdougm } 44074653Sdougm if (!dryrun && ret == SA_OK) 44085331Samw ret = enable_all_groups(handle, worklist, 1, 0, NULL, B_FALSE); 44094653Sdougm 44104653Sdougm if (ret != SA_OK && ret != SA_BUSY) 44113034Sdougm (void) printf(gettext("Could not enable group: %s\n"), 44124653Sdougm sa_errorstr(ret)); 44134653Sdougm if (ret == SA_BUSY) 44143034Sdougm ret = SA_OK; 44154653Sdougm 44163034Sdougm if (worklist != NULL) 44174653Sdougm free_list(worklist); 44183034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 44194653Sdougm (void) printf(gettext("Command would fail: %s\n"), 44204653Sdougm sa_errorstr(SA_NO_PERMISSION)); 44213034Sdougm } 44223034Sdougm return (ret); 44233034Sdougm } 44243034Sdougm 44253034Sdougm /* 44265331Samw * disable_group(group, proto) 44273034Sdougm * 44285331Samw * Disable all the shares in the specified group.. This is a helper 44295331Samw * for disable_all_groups in order to simplify regular and subgroup 44305331Samw * (zfs) disabling. Group has already been checked for non-NULL. 44313034Sdougm */ 44323034Sdougm 44333034Sdougm static int 44345331Samw disable_group(sa_group_t group, char *proto) 44353034Sdougm { 44363034Sdougm sa_share_t share; 44373034Sdougm int ret = SA_OK; 44383034Sdougm 44395331Samw /* 44405331Samw * If the protocol isn't enabled, skip it and treat as 44415331Samw * successful. 44425331Samw */ 44435331Samw if (!has_protocol(group, proto)) 44445331Samw return (ret); 44455331Samw 44463034Sdougm for (share = sa_get_share(group, NULL); 44473034Sdougm share != NULL && ret == SA_OK; 44483034Sdougm share = sa_get_next_share(share)) { 44495331Samw ret = sa_disable_share(share, proto); 44504653Sdougm if (ret == SA_NO_SUCH_PATH) { 44514653Sdougm /* 44524653Sdougm * this is OK since the path is gone. we can't 44534653Sdougm * re-share it anyway so no error. 44544653Sdougm */ 44554653Sdougm ret = SA_OK; 44564653Sdougm } 44573034Sdougm } 44583034Sdougm return (ret); 44593034Sdougm } 44603034Sdougm 44613034Sdougm /* 44623034Sdougm * disable_all_groups(work, setstate) 44633034Sdougm * 44643034Sdougm * helper function that disables the shares in the list of groups 44653034Sdougm * provided. It optionally marks the group as disabled. Used by both 44663034Sdougm * enable and start subcommands. 44673034Sdougm */ 44683034Sdougm 44693034Sdougm static int 44703910Sdougm disable_all_groups(sa_handle_t handle, struct list *work, int setstate) 44713034Sdougm { 44723034Sdougm int ret = SA_OK; 44733034Sdougm sa_group_t subgroup, group; 44743034Sdougm 44753034Sdougm while (work != NULL && ret == SA_OK) { 44764653Sdougm group = (sa_group_t)work->item; 44774653Sdougm if (setstate) 44784653Sdougm ret = sa_set_group_attr(group, "state", "disabled"); 44794653Sdougm if (ret == SA_OK) { 44804653Sdougm char *name; 44814653Sdougm name = sa_get_group_attr(group, "name"); 44824653Sdougm if (name != NULL && strcmp(name, "zfs") == 0) { 44834653Sdougm /* need to get the sub-groups for stopping */ 44844653Sdougm for (subgroup = sa_get_sub_group(group); 44854653Sdougm subgroup != NULL; 44864653Sdougm subgroup = sa_get_next_group(subgroup)) { 44875331Samw ret = disable_group(subgroup, 44885331Samw work->proto); 44894653Sdougm } 44904653Sdougm } else { 44915331Samw ret = disable_group(group, work->proto); 44924653Sdougm } 44934653Sdougm /* 44944653Sdougm * We don't want to "disable" since it won't come 44954653Sdougm * up after a reboot. The SMF framework should do 44964653Sdougm * the right thing. On enable we do want to do 44974653Sdougm * something. 44984653Sdougm */ 44993034Sdougm } 45004653Sdougm work = work->next; 45013034Sdougm } 45023034Sdougm if (ret == SA_OK) 45034653Sdougm ret = sa_update_config(handle); 45043034Sdougm return (ret); 45053034Sdougm } 45063034Sdougm 45073034Sdougm /* 45083034Sdougm * sa_disable_group(flags, argc, argv) 45093034Sdougm * 45103034Sdougm * Implements the disable subcommand 45113034Sdougm */ 45123034Sdougm 45133034Sdougm int 45143910Sdougm sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[]) 45153034Sdougm { 45163034Sdougm int verbose = 0; 45173034Sdougm int dryrun = 0; 45183034Sdougm int all = 0; 45193034Sdougm int c; 45203034Sdougm int ret = SA_OK; 45215331Samw char *protocol = NULL; 45223034Sdougm char *state; 45233034Sdougm struct list *worklist = NULL; 45244653Sdougm sa_group_t group; 45253034Sdougm int auth = 1; 45263034Sdougm 45273034Sdougm while ((c = getopt(argc, argv, "?havn")) != EOF) { 45284653Sdougm switch (c) { 45294653Sdougm case 'a': 45304653Sdougm all = 1; 45314653Sdougm break; 45324653Sdougm case 'n': 45334653Sdougm dryrun++; 45344653Sdougm break; 45354653Sdougm case 'P': 45365331Samw if (protocol != NULL) { 45375331Samw (void) printf(gettext( 45385331Samw "Specifying multiple protocols " 45395331Samw "not supported: %s\n"), protocol); 45405331Samw return (SA_SYNTAX_ERR); 45415331Samw } 45424653Sdougm protocol = optarg; 45434653Sdougm if (!sa_valid_protocol(protocol)) { 45444653Sdougm (void) printf(gettext( 45454653Sdougm "Invalid protocol specified: %s\n"), 45464653Sdougm protocol); 45474653Sdougm return (SA_INVALID_PROTOCOL); 45484653Sdougm } 45494653Sdougm break; 45504653Sdougm case 'v': 45514653Sdougm verbose++; 45524653Sdougm break; 45534653Sdougm default: 45544653Sdougm case 'h': 45554653Sdougm case '?': 45564653Sdougm (void) printf(gettext("usage: %s\n"), 45574653Sdougm sa_get_usage(USAGE_DISABLE)); 45584653Sdougm return (0); 45593034Sdougm } 45603034Sdougm } 45613034Sdougm 45623034Sdougm if (optind == argc && !all) { 45633034Sdougm (void) printf(gettext("usage: %s\n"), 45644653Sdougm sa_get_usage(USAGE_DISABLE)); 45653034Sdougm (void) printf(gettext("\tmust specify group\n")); 45664653Sdougm return (SA_NO_SUCH_PATH); 45674653Sdougm } 45684653Sdougm if (!all) { 45694653Sdougm while (optind < argc) { 45703910Sdougm group = sa_get_group(handle, argv[optind]); 45713034Sdougm if (group != NULL) { 45724653Sdougm auth &= check_authorizations(argv[optind], 45734653Sdougm flags); 45744653Sdougm state = sa_get_group_attr(group, "state"); 45754653Sdougm if (state == NULL || 45764653Sdougm strcmp(state, "disabled") == 0) { 45774653Sdougm /* already disabled */ 45784653Sdougm if (verbose) 45794653Sdougm (void) printf(gettext( 45804653Sdougm "Group \"%s\" is " 45814653Sdougm "already disabled\n"), 45824653Sdougm argv[optind]); 45835331Samw ret = SA_BUSY; /* already disabled */ 45844653Sdougm } else { 45855331Samw worklist = add_list(worklist, group, 0, 45865331Samw protocol); 45874653Sdougm if (verbose) 45884653Sdougm (void) printf(gettext( 45894653Sdougm "Disabling group " 45904653Sdougm "\"%s\"\n"), argv[optind]); 45914653Sdougm } 45924653Sdougm if (state != NULL) 45934653Sdougm sa_free_attr_string(state); 45943034Sdougm } else { 45954653Sdougm ret = SA_NO_SUCH_GROUP; 45963034Sdougm } 45973034Sdougm optind++; 45984653Sdougm } 45994653Sdougm } else { 46004653Sdougm for (group = sa_get_group(handle, NULL); 46014653Sdougm group != NULL; 46024653Sdougm group = sa_get_next_group(group)) 46035331Samw worklist = add_list(worklist, group, 0, protocol); 46043034Sdougm } 46054653Sdougm 46064653Sdougm if (ret == SA_OK && !dryrun) 46074653Sdougm ret = disable_all_groups(handle, worklist, 1); 46084653Sdougm if (ret != SA_OK && ret != SA_BUSY) 46094653Sdougm (void) printf(gettext("Could not disable group: %s\n"), 46104653Sdougm sa_errorstr(ret)); 46114653Sdougm if (ret == SA_BUSY) 46124653Sdougm ret = SA_OK; 46133034Sdougm if (worklist != NULL) 46144653Sdougm free_list(worklist); 46154653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) 46164653Sdougm (void) printf(gettext("Command would fail: %s\n"), 46174653Sdougm sa_errorstr(SA_NO_PERMISSION)); 46183034Sdougm return (ret); 46193034Sdougm } 46203034Sdougm 46213034Sdougm /* 46223034Sdougm * sa_start_group(flags, argc, argv) 46233034Sdougm * 46243034Sdougm * Implements the start command. 46253034Sdougm * This is similar to enable except it doesn't change the state 46263034Sdougm * of the group(s) and only enables shares if the group is already 46273034Sdougm * enabled. 46283034Sdougm */ 46295331Samw 46303034Sdougm int 46313910Sdougm sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[]) 46323034Sdougm { 46333034Sdougm int verbose = 0; 46343034Sdougm int all = 0; 46353034Sdougm int c; 46363034Sdougm int ret = SMF_EXIT_OK; 46373034Sdougm char *protocol = NULL; 46383034Sdougm char *state; 46393034Sdougm struct list *worklist = NULL; 46404653Sdougm sa_group_t group; 46415331Samw #ifdef lint 46425331Samw flags = flags; 46435331Samw #endif 46443034Sdougm 46453034Sdougm while ((c = getopt(argc, argv, "?havP:")) != EOF) { 46464653Sdougm switch (c) { 46474653Sdougm case 'a': 46484653Sdougm all = 1; 46494653Sdougm break; 46504653Sdougm case 'P': 46515331Samw if (protocol != NULL) { 46525331Samw (void) printf(gettext( 46535331Samw "Specifying multiple protocols " 46545331Samw "not supported: %s\n"), protocol); 46555331Samw return (SA_SYNTAX_ERR); 46565331Samw } 46574653Sdougm protocol = optarg; 46584653Sdougm if (!sa_valid_protocol(protocol)) { 46594653Sdougm (void) printf(gettext( 46604653Sdougm "Invalid protocol specified: %s\n"), 46613034Sdougm protocol); 46624653Sdougm return (SA_INVALID_PROTOCOL); 46634653Sdougm } 46644653Sdougm break; 46654653Sdougm case 'v': 46664653Sdougm verbose++; 46674653Sdougm break; 46684653Sdougm default: 46694653Sdougm case 'h': 46704653Sdougm case '?': 46714653Sdougm (void) printf(gettext("usage: %s\n"), 46724653Sdougm sa_get_usage(USAGE_START)); 46734653Sdougm return (SA_OK); 46743034Sdougm } 46753034Sdougm } 46763034Sdougm 46773034Sdougm if (optind == argc && !all) { 46783034Sdougm (void) printf(gettext("usage: %s\n"), 46794653Sdougm sa_get_usage(USAGE_START)); 46804653Sdougm return (SMF_EXIT_ERR_FATAL); 46814653Sdougm } 46824653Sdougm 46834653Sdougm if (!all) { 46844653Sdougm while (optind < argc) { 46853910Sdougm group = sa_get_group(handle, argv[optind]); 46863034Sdougm if (group != NULL) { 46874653Sdougm state = sa_get_group_attr(group, "state"); 46884653Sdougm if (state == NULL || 46894653Sdougm strcmp(state, "enabled") == 0) { 46905331Samw worklist = add_list(worklist, group, 0, 46915331Samw protocol); 46924653Sdougm if (verbose) 46934653Sdougm (void) printf(gettext( 46944653Sdougm "Starting group \"%s\"\n"), 46954653Sdougm argv[optind]); 46964653Sdougm } else { 46974653Sdougm /* 46984653Sdougm * Determine if there are any 46995331Samw * protocols. If there aren't any, 47004653Sdougm * then there isn't anything to do in 47014653Sdougm * any case so no error. 47024653Sdougm */ 47034653Sdougm if (sa_get_optionset(group, 47044653Sdougm protocol) != NULL) { 47054653Sdougm ret = SMF_EXIT_OK; 47064653Sdougm } 47073034Sdougm } 47084653Sdougm if (state != NULL) 47094653Sdougm sa_free_attr_string(state); 47103034Sdougm } 47113034Sdougm optind++; 47124653Sdougm } 47134653Sdougm } else { 47145331Samw for (group = sa_get_group(handle, NULL); 47155331Samw group != NULL; 47164653Sdougm group = sa_get_next_group(group)) { 47173034Sdougm state = sa_get_group_attr(group, "state"); 47183034Sdougm if (state == NULL || strcmp(state, "enabled") == 0) 47195331Samw worklist = add_list(worklist, group, 0, 47205331Samw protocol); 47213034Sdougm if (state != NULL) 47224653Sdougm sa_free_attr_string(state); 47233034Sdougm } 47243034Sdougm } 47254653Sdougm 47265331Samw (void) enable_all_groups(handle, worklist, 0, 1, protocol, B_FALSE); 47274653Sdougm 47283034Sdougm if (worklist != NULL) 47294653Sdougm free_list(worklist); 47303034Sdougm return (ret); 47313034Sdougm } 47323034Sdougm 47333034Sdougm /* 47343034Sdougm * sa_stop_group(flags, argc, argv) 47353034Sdougm * 47363034Sdougm * Implements the stop command. 47373034Sdougm * This is similar to disable except it doesn't change the state 47383034Sdougm * of the group(s) and only disables shares if the group is already 47393034Sdougm * enabled. 47403034Sdougm */ 47413034Sdougm int 47423910Sdougm sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[]) 47433034Sdougm { 47443034Sdougm int verbose = 0; 47453034Sdougm int all = 0; 47463034Sdougm int c; 47473034Sdougm int ret = SMF_EXIT_OK; 47483034Sdougm char *protocol = NULL; 47493034Sdougm char *state; 47503034Sdougm struct list *worklist = NULL; 47514653Sdougm sa_group_t group; 47525331Samw #ifdef lint 47535331Samw flags = flags; 47545331Samw #endif 47553034Sdougm 47563034Sdougm while ((c = getopt(argc, argv, "?havP:")) != EOF) { 47574653Sdougm switch (c) { 47584653Sdougm case 'a': 47594653Sdougm all = 1; 47604653Sdougm break; 47614653Sdougm case 'P': 47625331Samw if (protocol != NULL) { 47635331Samw (void) printf(gettext( 47645331Samw "Specifying multiple protocols " 47655331Samw "not supported: %s\n"), protocol); 47665331Samw return (SA_SYNTAX_ERR); 47675331Samw } 47684653Sdougm protocol = optarg; 47694653Sdougm if (!sa_valid_protocol(protocol)) { 47704653Sdougm (void) printf(gettext( 47714653Sdougm "Invalid protocol specified: %s\n"), 47724653Sdougm protocol); 47734653Sdougm return (SA_INVALID_PROTOCOL); 47744653Sdougm } 47754653Sdougm break; 47764653Sdougm case 'v': 47774653Sdougm verbose++; 47784653Sdougm break; 47794653Sdougm default: 47804653Sdougm case 'h': 47814653Sdougm case '?': 47824653Sdougm (void) printf(gettext("usage: %s\n"), 47834653Sdougm sa_get_usage(USAGE_STOP)); 47844653Sdougm return (0); 47853034Sdougm } 47863034Sdougm } 47873034Sdougm 47883034Sdougm if (optind == argc && !all) { 47894653Sdougm (void) printf(gettext("usage: %s\n"), 47904653Sdougm sa_get_usage(USAGE_STOP)); 47914653Sdougm return (SMF_EXIT_ERR_FATAL); 47924653Sdougm } else if (!all) { 47934653Sdougm while (optind < argc) { 47943910Sdougm group = sa_get_group(handle, argv[optind]); 47953034Sdougm if (group != NULL) { 47964653Sdougm state = sa_get_group_attr(group, "state"); 47974653Sdougm if (state == NULL || 47984653Sdougm strcmp(state, "enabled") == 0) { 47995331Samw worklist = add_list(worklist, group, 0, 48005331Samw protocol); 48014653Sdougm if (verbose) 48024653Sdougm (void) printf(gettext( 48034653Sdougm "Stopping group \"%s\"\n"), 48044653Sdougm argv[optind]); 48054653Sdougm } else { 48064653Sdougm ret = SMF_EXIT_OK; 48074653Sdougm } 48084653Sdougm if (state != NULL) 48094653Sdougm sa_free_attr_string(state); 48103034Sdougm } 48113034Sdougm optind++; 48124653Sdougm } 48134653Sdougm } else { 48145331Samw for (group = sa_get_group(handle, NULL); 48155331Samw group != NULL; 48164653Sdougm group = sa_get_next_group(group)) { 48173034Sdougm state = sa_get_group_attr(group, "state"); 48183034Sdougm if (state == NULL || strcmp(state, "enabled") == 0) 48195331Samw worklist = add_list(worklist, group, 0, 48205331Samw protocol); 48213034Sdougm if (state != NULL) 48224653Sdougm sa_free_attr_string(state); 48233034Sdougm } 48243034Sdougm } 48254653Sdougm (void) disable_all_groups(handle, worklist, 0); 48264653Sdougm ret = sa_update_config(handle); 48274653Sdougm 48283034Sdougm if (worklist != NULL) 48294653Sdougm free_list(worklist); 48303034Sdougm return (ret); 48313034Sdougm } 48323034Sdougm 48333034Sdougm /* 48343034Sdougm * remove_all_options(share, proto) 48353034Sdougm * 48363034Sdougm * Removes all options on a share. 48373034Sdougm */ 48383034Sdougm 48393034Sdougm static void 48403034Sdougm remove_all_options(sa_share_t share, char *proto) 48413034Sdougm { 48423034Sdougm sa_optionset_t optionset; 48433034Sdougm sa_security_t security; 48443034Sdougm sa_security_t prevsec = NULL; 48453034Sdougm 48463034Sdougm optionset = sa_get_optionset(share, proto); 48473034Sdougm if (optionset != NULL) 48484653Sdougm (void) sa_destroy_optionset(optionset); 48493034Sdougm for (security = sa_get_security(share, NULL, NULL); 48503034Sdougm security != NULL; 48513034Sdougm security = sa_get_next_security(security)) { 48524653Sdougm char *type; 48533034Sdougm /* 48544653Sdougm * We walk through the list. prevsec keeps the 48553034Sdougm * previous security so we can delete it without 48563034Sdougm * destroying the list. 48573034Sdougm */ 48584653Sdougm if (prevsec != NULL) { 48594653Sdougm /* remove the previously seen security */ 48604653Sdougm (void) sa_destroy_security(prevsec); 48614653Sdougm /* set to NULL so we don't try multiple times */ 48624653Sdougm prevsec = NULL; 48634653Sdougm } 48644653Sdougm type = sa_get_security_attr(security, "type"); 48654653Sdougm if (type != NULL) { 48664653Sdougm /* 48674653Sdougm * if the security matches the specified protocol, we 48684653Sdougm * want to remove it. prevsec holds it until either 48694653Sdougm * the next pass or we fall out of the loop. 48704653Sdougm */ 48714653Sdougm if (strcmp(type, proto) == 0) 48724653Sdougm prevsec = security; 48734653Sdougm sa_free_attr_string(type); 48744653Sdougm } 48753034Sdougm } 48763034Sdougm /* in case there is one left */ 48773034Sdougm if (prevsec != NULL) 48784653Sdougm (void) sa_destroy_security(prevsec); 48793034Sdougm } 48803034Sdougm 48813034Sdougm 48823034Sdougm /* 48833034Sdougm * for legacy support, we need to handle the old syntax. This is what 48843034Sdougm * we get if sharemgr is called with the name "share" rather than 48853034Sdougm * sharemgr. 48863034Sdougm */ 48873034Sdougm 48883034Sdougm static int 48893034Sdougm format_legacy_path(char *buff, int buffsize, char *proto, char *cmd) 48903034Sdougm { 48913034Sdougm int err; 48923034Sdougm 48933034Sdougm err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd); 48943034Sdougm if (err > buffsize) 48954653Sdougm return (-1); 48963034Sdougm return (0); 48973034Sdougm } 48983034Sdougm 48993034Sdougm 49003034Sdougm /* 49013034Sdougm * check_legacy_cmd(proto, cmd) 49023034Sdougm * 49033034Sdougm * Check to see if the cmd exists in /usr/lib/fs/<proto>/<cmd> and is 49043034Sdougm * executable. 49053034Sdougm */ 49063034Sdougm 49073034Sdougm static int 49083034Sdougm check_legacy_cmd(char *path) 49093034Sdougm { 49103034Sdougm struct stat st; 49113034Sdougm int ret = 0; 49123034Sdougm 49133034Sdougm if (stat(path, &st) == 0) { 49144653Sdougm if (S_ISREG(st.st_mode) && 49154653Sdougm st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) 49164653Sdougm ret = 1; 49173034Sdougm } 49183034Sdougm return (ret); 49193034Sdougm } 49203034Sdougm 49213034Sdougm /* 49223034Sdougm * run_legacy_command(proto, cmd, argv) 49233034Sdougm * 49244653Sdougm * We know the command exists, so attempt to execute it with all the 49253034Sdougm * arguments. This implements full legacy share support for those 49263034Sdougm * protocols that don't have plugin providers. 49273034Sdougm */ 49283034Sdougm 49293034Sdougm static int 49303034Sdougm run_legacy_command(char *path, char *argv[]) 49313034Sdougm { 49323034Sdougm int ret; 49333034Sdougm 49343034Sdougm ret = execv(path, argv); 49353034Sdougm if (ret < 0) { 49364653Sdougm switch (errno) { 49374653Sdougm case EACCES: 49384653Sdougm ret = SA_NO_PERMISSION; 49394653Sdougm break; 49404653Sdougm default: 49414653Sdougm ret = SA_SYSTEM_ERR; 49424653Sdougm break; 49434653Sdougm } 49443034Sdougm } 49453034Sdougm return (ret); 49463034Sdougm } 49473034Sdougm 49483034Sdougm /* 49493348Sdougm * out_share(out, group, proto) 49503034Sdougm * 49513034Sdougm * Display the share information in the format that the "share" 49523034Sdougm * command has traditionally used. 49533034Sdougm */ 49543034Sdougm 49553034Sdougm static void 49563348Sdougm out_share(FILE *out, sa_group_t group, char *proto) 49573034Sdougm { 49583034Sdougm sa_share_t share; 49593034Sdougm char resfmt[128]; 49605331Samw char *defprop; 49615331Samw 49625331Samw /* 49635331Samw * The original share command defaulted to displaying NFS 49645331Samw * shares or allowed a protocol to be specified. We want to 49655331Samw * skip those shares that are not the specified protocol. 49665331Samw */ 49675331Samw if (proto != NULL && sa_get_optionset(group, proto) == NULL) 49685331Samw return; 49695331Samw 49705331Samw if (proto == NULL) 49715331Samw proto = "nfs"; 49725331Samw 49735331Samw /* 49745331Samw * get the default property string. NFS uses "rw" but 49755331Samw * everything else will use "". 49765331Samw */ 49775331Samw if (proto != NULL && strcmp(proto, "nfs") != 0) 49785331Samw defprop = "\"\""; 49795331Samw else 49805331Samw defprop = "rw"; 49813034Sdougm 49824653Sdougm for (share = sa_get_share(group, NULL); 49834653Sdougm share != NULL; 49844653Sdougm share = sa_get_next_share(share)) { 49854653Sdougm char *path; 49864653Sdougm char *type; 49874653Sdougm char *resource; 49884653Sdougm char *description; 49894653Sdougm char *groupname; 49904653Sdougm char *sharedstate; 49914653Sdougm int shared = 1; 49924653Sdougm char *soptions; 49935331Samw char shareopts[MAXNAMLEN]; 49944653Sdougm 49954653Sdougm sharedstate = sa_get_share_attr(share, "shared"); 49964653Sdougm path = sa_get_share_attr(share, "path"); 49974653Sdougm type = sa_get_share_attr(share, "type"); 49985331Samw resource = get_resource(share); 49994653Sdougm groupname = sa_get_group_attr(group, "name"); 50004653Sdougm 50014653Sdougm if (groupname != NULL && strcmp(groupname, "default") == 0) { 50024653Sdougm sa_free_attr_string(groupname); 50034653Sdougm groupname = NULL; 50044653Sdougm } 50054653Sdougm description = sa_get_share_description(share); 50064653Sdougm 50075331Samw /* 50085331Samw * Want the sharetab version if it exists, defaulting 50095331Samw * to NFS if no protocol specified. 50105331Samw */ 50115331Samw (void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", proto); 50125331Samw soptions = sa_get_share_attr(share, shareopts); 50134653Sdougm 50144653Sdougm if (sharedstate == NULL) 50154653Sdougm shared = 0; 50164653Sdougm 50174653Sdougm if (soptions == NULL) 50184653Sdougm soptions = sa_proto_legacy_format(proto, share, 1); 50194653Sdougm 50204653Sdougm if (shared) { 50214653Sdougm /* only active shares go here */ 50224653Sdougm (void) snprintf(resfmt, sizeof (resfmt), "%s%s%s", 50234653Sdougm resource != NULL ? resource : "-", 50244653Sdougm groupname != NULL ? "@" : "", 50254653Sdougm groupname != NULL ? groupname : ""); 50264653Sdougm (void) fprintf(out, "%-14.14s %s %s \"%s\" \n", 50274653Sdougm resfmt, path, 50284653Sdougm (soptions != NULL && strlen(soptions) > 0) ? 50295331Samw soptions : defprop, 50304653Sdougm (description != NULL) ? description : ""); 50314653Sdougm } 50324653Sdougm 50334653Sdougm if (path != NULL) 50344653Sdougm sa_free_attr_string(path); 50354653Sdougm if (type != NULL) 50364653Sdougm sa_free_attr_string(type); 50374653Sdougm if (resource != NULL) 50384653Sdougm sa_free_attr_string(resource); 50394653Sdougm if (groupname != NULL) 50404653Sdougm sa_free_attr_string(groupname); 50414653Sdougm if (description != NULL) 50424653Sdougm sa_free_share_description(description); 50434653Sdougm if (sharedstate != NULL) 50444653Sdougm sa_free_attr_string(sharedstate); 50454653Sdougm if (soptions != NULL) 50464653Sdougm sa_format_free(soptions); 50473034Sdougm } 50483034Sdougm } 50493034Sdougm 50503034Sdougm /* 50513034Sdougm * output_legacy_file(out, proto) 50523034Sdougm * 50533034Sdougm * Walk all of the groups for the specified protocol and call 50543034Sdougm * out_share() to format and write in the format displayed by the 50553034Sdougm * "share" command with no arguments. 50563034Sdougm */ 50573034Sdougm 50583034Sdougm static void 50593910Sdougm output_legacy_file(FILE *out, char *proto, sa_handle_t handle) 50603034Sdougm { 50613034Sdougm sa_group_t group; 50623034Sdougm 50635331Samw for (group = sa_get_group(handle, NULL); 50645331Samw group != NULL; 50654653Sdougm group = sa_get_next_group(group)) { 50664653Sdougm char *zfs; 50673034Sdougm 50683034Sdougm /* 50695331Samw * Go through all the groups and ZFS 50705331Samw * sub-groups. out_share() will format the shares in 50715331Samw * the group appropriately. 50723034Sdougm */ 50733034Sdougm 50744653Sdougm zfs = sa_get_group_attr(group, "zfs"); 50754653Sdougm if (zfs != NULL) { 50764653Sdougm sa_group_t zgroup; 50774653Sdougm sa_free_attr_string(zfs); 50784653Sdougm for (zgroup = sa_get_sub_group(group); 50794653Sdougm zgroup != NULL; 50804653Sdougm zgroup = sa_get_next_group(zgroup)) { 50814653Sdougm 50824653Sdougm /* got a group, so display it */ 50834653Sdougm out_share(out, zgroup, proto); 50844653Sdougm } 50854653Sdougm } else { 50864653Sdougm out_share(out, group, proto); 50873034Sdougm } 50883034Sdougm } 50893034Sdougm } 50903034Sdougm 50913034Sdougm int 50923910Sdougm sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[]) 50933034Sdougm { 50943034Sdougm char *protocol = "nfs"; 50953034Sdougm char *options = NULL; 50963034Sdougm char *description = NULL; 50973034Sdougm char *groupname = NULL; 50983034Sdougm char *sharepath = NULL; 50993034Sdougm char *resource = NULL; 51003034Sdougm char *groupstatus = NULL; 51013034Sdougm int persist = SA_SHARE_TRANSIENT; 51023034Sdougm int argsused = 0; 51033034Sdougm int c; 51043034Sdougm int ret = SA_OK; 51053034Sdougm int zfs = 0; 51063034Sdougm int true_legacy = 0; 51073034Sdougm int curtype = SA_SHARE_TRANSIENT; 51083034Sdougm char cmd[MAXPATHLEN]; 51094653Sdougm sa_group_t group = NULL; 51105331Samw sa_resource_t rsrc = NULL; 51114653Sdougm sa_share_t share; 51124653Sdougm char dir[MAXPATHLEN]; 51135331Samw uint64_t features; 51145331Samw #ifdef lint 51155331Samw flags = flags; 51165331Samw #endif 51173034Sdougm 51183034Sdougm while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) { 51194653Sdougm switch (c) { 51204653Sdougm case 'd': 51214653Sdougm description = optarg; 51224653Sdougm argsused++; 51234653Sdougm break; 51244653Sdougm case 'F': 51254653Sdougm protocol = optarg; 51264653Sdougm if (!sa_valid_protocol(protocol)) { 51274653Sdougm if (format_legacy_path(cmd, MAXPATHLEN, 51284653Sdougm protocol, "share") == 0 && 51294653Sdougm check_legacy_cmd(cmd)) { 51304653Sdougm true_legacy++; 51314653Sdougm } else { 51324653Sdougm (void) fprintf(stderr, gettext( 51334653Sdougm "Invalid protocol specified: " 51344653Sdougm "%s\n"), protocol); 51354653Sdougm return (SA_INVALID_PROTOCOL); 51364653Sdougm } 51374653Sdougm } 51384653Sdougm break; 51394653Sdougm case 'o': 51404653Sdougm options = optarg; 51414653Sdougm argsused++; 51424653Sdougm break; 51434653Sdougm case 'p': 51444653Sdougm persist = SA_SHARE_PERMANENT; 51454653Sdougm argsused++; 51464653Sdougm break; 51474653Sdougm case 'h': 51484653Sdougm case '?': 51494653Sdougm default: 51504653Sdougm (void) fprintf(stderr, gettext("usage: %s\n"), 51514653Sdougm sa_get_usage(USAGE_SHARE)); 51524653Sdougm return (SA_OK); 51533034Sdougm } 51544653Sdougm } 51554653Sdougm 51564653Sdougm /* Have the info so construct what is needed */ 51574653Sdougm if (!argsused && optind == argc) { 51584653Sdougm /* display current info in share format */ 51595331Samw (void) output_legacy_file(stdout, protocol, handle); 51604653Sdougm return (ret); 51613034Sdougm } 51623034Sdougm 51634653Sdougm /* We are modifying the configuration */ 51644653Sdougm if (optind == argc) { 51653034Sdougm (void) fprintf(stderr, gettext("usage: %s\n"), 51664653Sdougm sa_get_usage(USAGE_SHARE)); 51673034Sdougm return (SA_LEGACY_ERR); 51684653Sdougm } 51694653Sdougm if (true_legacy) { 51704653Sdougm /* If still using legacy share/unshare, exec it */ 51713034Sdougm ret = run_legacy_command(cmd, argv); 51723034Sdougm return (ret); 51734653Sdougm } 51744653Sdougm 51754653Sdougm sharepath = argv[optind++]; 51764653Sdougm if (optind < argc) { 51773034Sdougm resource = argv[optind]; 51783034Sdougm groupname = strchr(resource, '@'); 51793034Sdougm if (groupname != NULL) 51804653Sdougm *groupname++ = '\0'; 51814653Sdougm } 51824653Sdougm if (realpath(sharepath, dir) == NULL) 51833034Sdougm ret = SA_BAD_PATH; 51844653Sdougm else 51853034Sdougm sharepath = dir; 51864653Sdougm if (ret == SA_OK) 51873910Sdougm share = sa_find_share(handle, sharepath); 51884653Sdougm else 51893034Sdougm share = NULL; 51904653Sdougm 51915331Samw features = sa_proto_get_featureset(protocol); 51925331Samw 51934653Sdougm if (groupname != NULL) { 51944653Sdougm ret = SA_NOT_ALLOWED; 51954653Sdougm } else if (ret == SA_OK) { 51965331Samw char *legacygroup; 51973034Sdougm /* 51984653Sdougm * The legacy group is always present and zfs groups 51993034Sdougm * come and go. zfs shares may be in sub-groups and 52003034Sdougm * the zfs share will already be in that group so it 52015331Samw * isn't an error. If the protocol is "smb", the group 52025331Samw * "smb" is used when "default" would otherwise be 52035331Samw * used. "default" is NFS only and "smb" is SMB only. 52043034Sdougm */ 52055331Samw if (strcmp(protocol, "smb") == 0) 52065331Samw legacygroup = "smb"; 52075331Samw else 52085331Samw legacygroup = "default"; 52095331Samw 52103034Sdougm /* 52114653Sdougm * If the share exists (not NULL), then make sure it 52124653Sdougm * is one we want to handle by getting the parent 52134653Sdougm * group. 52143034Sdougm */ 52155331Samw if (share != NULL) { 52164653Sdougm group = sa_get_parent_group(share); 52175331Samw } else { 52184653Sdougm group = sa_get_group(handle, legacygroup); 52195331Samw if (group == NULL && strcmp(legacygroup, "smb") == 0) { 52205331Samw /* 52215331Samw * This group may not exist, so create 52225331Samw * as necessary. It only contains the 52235331Samw * "smb" protocol. 52245331Samw */ 52255331Samw group = sa_create_group(handle, legacygroup, 52265331Samw &ret); 52275331Samw if (group != NULL) 52285331Samw (void) sa_create_optionset(group, 52295331Samw protocol); 52305331Samw } 52315331Samw } 52325331Samw 52335331Samw if (group == NULL) { 52345331Samw ret = SA_SYSTEM_ERR; 52355331Samw goto err; 52365331Samw } 52375331Samw 52385331Samw groupstatus = group_status(group); 52395331Samw if (share == NULL) { 52405331Samw share = sa_add_share(group, sharepath, 52415331Samw persist, &ret); 52425331Samw if (share == NULL && 52435331Samw ret == SA_DUPLICATE_NAME) { 52445331Samw /* 52455331Samw * Could be a ZFS path being started 52465331Samw */ 52475331Samw if (sa_zfs_is_shared(handle, 52485331Samw sharepath)) { 52495331Samw ret = SA_OK; 52505331Samw group = sa_get_group(handle, 52515331Samw "zfs"); 52525331Samw if (group == NULL) { 52535331Samw /* 52545331Samw * This shouldn't 52555331Samw * happen. 52565331Samw */ 52575331Samw ret = SA_CONFIG_ERR; 52585331Samw } else { 52595331Samw share = sa_add_share( 52605331Samw group, sharepath, 52615331Samw persist, &ret); 52624653Sdougm } 52633034Sdougm } 52645331Samw } 52655331Samw } else { 52665331Samw char *type; 52675331Samw /* 52685331Samw * May want to change persist state, but the 52695331Samw * important thing is to change options. We 52705331Samw * need to change them regardless of the 52715331Samw * source. 52725331Samw */ 52735331Samw 52745331Samw if (sa_zfs_is_shared(handle, sharepath)) { 52755331Samw zfs = 1; 52765331Samw } 52775331Samw remove_all_options(share, protocol); 52785331Samw type = sa_get_share_attr(share, "type"); 52795331Samw if (type != NULL && 52805331Samw strcmp(type, "transient") != 0) { 52815331Samw curtype = SA_SHARE_PERMANENT; 52825331Samw } 52835331Samw if (type != NULL) 52845331Samw sa_free_attr_string(type); 52855331Samw if (curtype != persist) { 52865331Samw (void) sa_set_share_attr(share, "type", 52875331Samw persist == SA_SHARE_PERMANENT ? 52885331Samw "persist" : "transient"); 52895331Samw } 52905331Samw } 52915331Samw 52925331Samw /* 52935331Samw * If there is a resource name, we may 52945331Samw * actually care about it if this is share for 52955331Samw * a protocol that uses resource level sharing 52965331Samw * (SMB). We need to find the resource and, if 52975331Samw * it exists, make sure it belongs to the 52985331Samw * current share. If it doesn't exist, attempt 52995331Samw * to create it. 53005331Samw */ 53015331Samw 53025331Samw if (ret == SA_OK && resource != NULL) { 53035331Samw rsrc = sa_find_resource(handle, resource); 53045331Samw if (rsrc != NULL) { 53055331Samw if (share != sa_get_resource_parent(rsrc)) 53065331Samw ret = SA_DUPLICATE_NAME; 53075331Samw } else { 53085331Samw rsrc = sa_add_resource(share, resource, 53095331Samw persist, &ret); 53103034Sdougm } 53115331Samw if (features & SA_FEATURE_RESOURCE) 53125331Samw share = rsrc; 53133108Sdougm } 53145331Samw 53154653Sdougm /* Have a group to hold this share path */ 53164653Sdougm if (ret == SA_OK && options != NULL && 53174653Sdougm strlen(options) > 0) { 53184653Sdougm ret = sa_parse_legacy_options(share, 53194653Sdougm options, 53204653Sdougm protocol); 53213034Sdougm } 53224653Sdougm if (!zfs) { 53234653Sdougm /* 53245331Samw * ZFS shares never have a description 53255331Samw * and we can't store the values so 53265331Samw * don't try. 53274653Sdougm */ 53284653Sdougm if (ret == SA_OK && description != NULL) 53294653Sdougm ret = sa_set_share_description(share, 53304653Sdougm description); 53313034Sdougm } 53325331Samw if (ret == SA_OK && 53335331Samw strcmp(groupstatus, "enabled") == 0) { 53345331Samw if (rsrc != share) 53354653Sdougm ret = sa_enable_share(share, protocol); 53365331Samw else 53375331Samw ret = sa_enable_resource(rsrc, 53385331Samw protocol); 53394653Sdougm if (ret == SA_OK && 53404653Sdougm persist == SA_SHARE_PERMANENT) { 53414653Sdougm (void) sa_update_legacy(share, 53424653Sdougm protocol); 53434653Sdougm } 53444653Sdougm if (ret == SA_OK) 53454653Sdougm ret = sa_update_config(handle); 53464653Sdougm } 53473034Sdougm } 53485331Samw err: 53493034Sdougm if (ret != SA_OK) { 53504653Sdougm (void) fprintf(stderr, gettext("Could not share: %s: %s\n"), 53514653Sdougm sharepath, sa_errorstr(ret)); 53524653Sdougm ret = SA_LEGACY_ERR; 53533034Sdougm } 53543034Sdougm return (ret); 53553034Sdougm } 53563034Sdougm 53573034Sdougm /* 53583034Sdougm * sa_legacy_unshare(flags, argc, argv) 53593034Sdougm * 53603034Sdougm * Implements the original unshare command. 53613034Sdougm */ 53623034Sdougm int 53633910Sdougm sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[]) 53643034Sdougm { 53653034Sdougm char *protocol = "nfs"; /* for now */ 53663034Sdougm char *options = NULL; 53673034Sdougm char *sharepath = NULL; 53683034Sdougm int persist = SA_SHARE_TRANSIENT; 53693034Sdougm int argsused = 0; 53703034Sdougm int c; 53713034Sdougm int ret = SA_OK; 53723034Sdougm int true_legacy = 0; 53735331Samw uint64_t features = 0; 53745331Samw sa_resource_t resource = NULL; 53753034Sdougm char cmd[MAXPATHLEN]; 53765331Samw #ifdef lint 53775331Samw flags = flags; 53785331Samw options = options; 53795331Samw #endif 53803034Sdougm 53813034Sdougm while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) { 53824653Sdougm switch (c) { 53834653Sdougm case 'h': 53844653Sdougm case '?': 53854653Sdougm break; 53864653Sdougm case 'F': 53874653Sdougm protocol = optarg; 53884653Sdougm if (!sa_valid_protocol(protocol)) { 53894653Sdougm if (format_legacy_path(cmd, MAXPATHLEN, 53904653Sdougm protocol, "unshare") == 0 && 53914653Sdougm check_legacy_cmd(cmd)) { 53924653Sdougm true_legacy++; 53934653Sdougm } else { 53944653Sdougm (void) printf(gettext( 53954653Sdougm "Invalid file system name\n")); 53964653Sdougm return (SA_INVALID_PROTOCOL); 53974653Sdougm } 53984653Sdougm } 53994653Sdougm break; 54004653Sdougm case 'o': 54014653Sdougm options = optarg; 54024653Sdougm argsused++; 54034653Sdougm break; 54044653Sdougm case 'p': 54054653Sdougm persist = SA_SHARE_PERMANENT; 54064653Sdougm argsused++; 54074653Sdougm break; 54084653Sdougm default: 54094653Sdougm (void) printf(gettext("usage: %s\n"), 54104653Sdougm sa_get_usage(USAGE_UNSHARE)); 54114653Sdougm return (SA_OK); 54123034Sdougm } 54133034Sdougm } 54143034Sdougm 54154653Sdougm /* Have the info so construct what is needed */ 54164653Sdougm if (optind == argc || (optind + 1) < argc || options != NULL) { 54174653Sdougm ret = SA_SYNTAX_ERR; 54183034Sdougm } else { 54194653Sdougm sa_share_t share; 54204653Sdougm char dir[MAXPATHLEN]; 54214653Sdougm if (true_legacy) { 54224653Sdougm /* if still using legacy share/unshare, exec it */ 54234653Sdougm ret = run_legacy_command(cmd, argv); 54244653Sdougm return (ret); 54254653Sdougm } 54263663Sdougm /* 54273663Sdougm * Find the path in the internal configuration. If it 54283663Sdougm * isn't found, attempt to resolve the path via 54293663Sdougm * realpath() and try again. 54303663Sdougm */ 54314653Sdougm sharepath = argv[optind++]; 54324653Sdougm share = sa_find_share(handle, sharepath); 54334653Sdougm if (share == NULL) { 54344653Sdougm if (realpath(sharepath, dir) == NULL) { 54354653Sdougm ret = SA_NO_SUCH_PATH; 54364653Sdougm } else { 54374653Sdougm share = sa_find_share(handle, dir); 54384653Sdougm } 54393663Sdougm } 54405331Samw if (share == NULL) { 54415331Samw /* Could be a resource name so check that next */ 54425331Samw features = sa_proto_get_featureset(protocol); 54435331Samw resource = sa_find_resource(handle, sharepath); 54445331Samw if (resource != NULL) { 54455331Samw share = sa_get_resource_parent(resource); 54465331Samw if (features & SA_FEATURE_RESOURCE) 54475331Samw (void) sa_disable_resource(resource, 54485331Samw protocol); 54495331Samw if (persist == SA_SHARE_PERMANENT) { 54505331Samw ret = sa_remove_resource(resource); 54515331Samw if (ret == SA_OK) 54525331Samw ret = sa_update_config(handle); 54535331Samw } 54545331Samw /* 54555331Samw * If we still have a resource on the 54565331Samw * share, we don't disable the share 54575331Samw * itself. IF there aren't anymore, we 54585331Samw * need to remove the share. The 54595331Samw * removal will be done in the next 54605331Samw * section if appropriate. 54615331Samw */ 54625331Samw resource = sa_get_share_resource(share, NULL); 54635331Samw if (resource != NULL) 54645331Samw share = NULL; 54655331Samw } else if (ret == SA_OK) { 54665331Samw /* Didn't find path and no resource */ 54675331Samw ret = SA_BAD_PATH; 54685331Samw } 54695331Samw } 54705331Samw if (share != NULL && resource == NULL) { 54714653Sdougm ret = sa_disable_share(share, protocol); 54724653Sdougm /* 54734653Sdougm * Errors are ok and removal should still occur. The 54744653Sdougm * legacy unshare is more forgiving of errors than the 54754653Sdougm * remove-share subcommand which may need the force 54764653Sdougm * flag set for some error conditions. That is, the 54774653Sdougm * "unshare" command will always unshare if it can 54784653Sdougm * while "remove-share" might require the force option. 54794653Sdougm */ 54804653Sdougm if (persist == SA_SHARE_PERMANENT) { 54814653Sdougm ret = sa_remove_share(share); 54824653Sdougm if (ret == SA_OK) 54834653Sdougm ret = sa_update_config(handle); 54844653Sdougm } 54855331Samw } else if (ret == SA_OK && share == NULL && resource == NULL) { 54865331Samw /* 54875331Samw * If both share and resource are NULL, then 54885331Samw * share not found. If one or the other was 54895331Samw * found or there was an earlier error, we 54905331Samw * assume it was handled earlier. 54915331Samw */ 54924653Sdougm ret = SA_NOT_SHARED; 54933663Sdougm } 54943034Sdougm } 54953034Sdougm switch (ret) { 54963034Sdougm default: 54974653Sdougm (void) printf("%s: %s\n", sharepath, sa_errorstr(ret)); 54984653Sdougm ret = SA_LEGACY_ERR; 54994653Sdougm break; 55003034Sdougm case SA_SYNTAX_ERR: 55014653Sdougm (void) printf(gettext("usage: %s\n"), 55024653Sdougm sa_get_usage(USAGE_UNSHARE)); 55034653Sdougm break; 55043034Sdougm case SA_OK: 55054653Sdougm break; 55063034Sdougm } 55073034Sdougm return (ret); 55083034Sdougm } 55093034Sdougm 55103034Sdougm /* 55114653Sdougm * Common commands that implement the sub-commands used by all 55125331Samw * protocols. The entries are found via the lookup command 55133034Sdougm */ 55143034Sdougm 55153034Sdougm static sa_command_t commands[] = { 55163034Sdougm {"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET}, 55173034Sdougm {"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION}, 55183034Sdougm {"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION}, 55193034Sdougm {"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION}, 55203034Sdougm {"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION}, 55213034Sdougm {"list", 0, sa_list, USAGE_LIST}, 55223034Sdougm {"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET}, 55233034Sdougm {"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET}, 55243034Sdougm {"set", 0, sa_set, USAGE_SET, SVC_SET}, 55253034Sdougm {"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET}, 55263034Sdougm {"show", 0, sa_show, USAGE_SHOW}, 55273034Sdougm {"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION}, 55283034Sdougm {"start", CMD_NODISPLAY, sa_start_group, USAGE_START, 55295331Samw SVC_SET|SVC_ACTION}, 55303034Sdougm {"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION}, 55313034Sdougm {"unset", 0, sa_unset, USAGE_UNSET, SVC_SET}, 55323034Sdougm {"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION}, 55333034Sdougm {NULL, 0, NULL, NULL} 55343034Sdougm }; 55353034Sdougm 55363034Sdougm static char * 55373034Sdougm sa_get_usage(sa_usage_t index) 55383034Sdougm { 55393034Sdougm char *ret = NULL; 55403034Sdougm switch (index) { 55413034Sdougm case USAGE_ADD_SHARE: 55424653Sdougm ret = gettext("add-share [-nth] [-r resource-name] " 55434653Sdougm "[-d \"description text\"] -s sharepath group"); 55444653Sdougm break; 55453034Sdougm case USAGE_CREATE: 55464653Sdougm ret = gettext( 55474653Sdougm "create [-nvh] [-P proto [-p property=value]] group"); 55484653Sdougm break; 55493034Sdougm case USAGE_DELETE: 55504653Sdougm ret = gettext("delete [-nvh] [-P proto] [-f] group"); 55514653Sdougm break; 55523034Sdougm case USAGE_DISABLE: 55534653Sdougm ret = gettext("disable [-nvh] {-a | group ...}"); 55544653Sdougm break; 55553034Sdougm case USAGE_ENABLE: 55564653Sdougm ret = gettext("enable [-nvh] {-a | group ...}"); 55574653Sdougm break; 55583034Sdougm case USAGE_LIST: 55594653Sdougm ret = gettext("list [-vh] [-P proto]"); 55604653Sdougm break; 55613034Sdougm case USAGE_MOVE_SHARE: 55624653Sdougm ret = gettext( 55634653Sdougm "move-share [-nvh] -s sharepath destination-group"); 55644653Sdougm break; 55653034Sdougm case USAGE_REMOVE_SHARE: 55665331Samw ret = gettext( 55675331Samw "remove-share [-fnvh] {-s sharepath | -r resource} " 55685331Samw "group"); 55694653Sdougm break; 55703034Sdougm case USAGE_SET: 55714653Sdougm ret = gettext("set [-nvh] -P proto [-S optspace] " 55725331Samw "[-p property=value]* [-s sharepath] [-r resource]] " 55735331Samw "group"); 55744653Sdougm break; 55753034Sdougm case USAGE_SET_SECURITY: 55764653Sdougm ret = gettext("set-security [-nvh] -P proto -S security-type " 55774653Sdougm "[-p property=value]* group"); 55784653Sdougm break; 55793034Sdougm case USAGE_SET_SHARE: 55804653Sdougm ret = gettext("set-share [-nh] [-r resource] " 55814653Sdougm "[-d \"description text\"] -s sharepath group"); 55824653Sdougm break; 55833034Sdougm case USAGE_SHOW: 55844653Sdougm ret = gettext("show [-pvxh] [-P proto] [group ...]"); 55854653Sdougm break; 55863034Sdougm case USAGE_SHARE: 55874653Sdougm ret = gettext("share [-F fstype] [-p] [-o optionlist]" 55884653Sdougm "[-d description] [pathname [resourcename]]"); 55894653Sdougm break; 55903034Sdougm case USAGE_START: 55914653Sdougm ret = gettext("start [-vh] [-P proto] {-a | group ...}"); 55924653Sdougm break; 55933034Sdougm case USAGE_STOP: 55944653Sdougm ret = gettext("stop [-vh] [-P proto] {-a | group ...}"); 55954653Sdougm break; 55963034Sdougm case USAGE_UNSET: 55974653Sdougm ret = gettext("unset [-nvh] -P proto [-S optspace] " 55984653Sdougm "[-p property]* group"); 55994653Sdougm break; 56003034Sdougm case USAGE_UNSET_SECURITY: 56015331Samw ret = gettext("unset-security [-nvh] -P proto " 56025331Samw "-S security-type [-p property]* group"); 56034653Sdougm break; 56043034Sdougm case USAGE_UNSHARE: 56054653Sdougm ret = gettext( 56065331Samw "unshare [-F fstype] [-p] [-o optionlist] sharepath"); 56074653Sdougm break; 56083034Sdougm } 56093034Sdougm return (ret); 56103034Sdougm } 56113034Sdougm 56123034Sdougm /* 56133034Sdougm * sa_lookup(cmd, proto) 56143034Sdougm * 56153034Sdougm * Lookup the sub-command. proto isn't currently used, but it may 56163034Sdougm * eventually provide a way to provide protocol specific sub-commands. 56173034Sdougm */ 56183034Sdougm sa_command_t * 56193034Sdougm sa_lookup(char *cmd, char *proto) 56203034Sdougm { 56213034Sdougm int i; 56223034Sdougm size_t len; 56235331Samw #ifdef lint 56245331Samw proto = proto; 56255331Samw #endif 56263034Sdougm 56273034Sdougm len = strlen(cmd); 56283034Sdougm for (i = 0; commands[i].cmdname != NULL; i++) { 56294653Sdougm if (strncmp(cmd, commands[i].cmdname, len) == 0) 56304653Sdougm return (&commands[i]); 56313034Sdougm } 56323034Sdougm return (NULL); 56333034Sdougm } 56343034Sdougm 56353034Sdougm void 56363034Sdougm sub_command_help(char *proto) 56373034Sdougm { 56383034Sdougm int i; 56395331Samw #ifdef lint 56405331Samw proto = proto; 56415331Samw #endif 56423034Sdougm 56433034Sdougm (void) printf(gettext("\tsub-commands:\n")); 56443034Sdougm for (i = 0; commands[i].cmdname != NULL; i++) { 56454653Sdougm if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY))) 56464653Sdougm (void) printf("\t%s\n", 56474653Sdougm sa_get_usage((sa_usage_t)commands[i].cmdidx)); 56483034Sdougm } 56493034Sdougm } 5650