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; 11684653Sdougm } 11694653Sdougm } else { 11704653Sdougm /* must add new protocol */ 11714653Sdougm (void) printf(gettext( 11724653Sdougm "Group already exists and no protocol " 11734653Sdougm "specified.\n")); 11744653Sdougm ret = SA_DUPLICATE_NAME; 11753034Sdougm } 11763034Sdougm } else { 11773034Sdougm /* 11783034Sdougm * is it a valid name? Must comply with SMF instance 11793034Sdougm * name restrictions. 11803034Sdougm */ 11814653Sdougm if (!sa_valid_group_name(groupname)) { 11824653Sdougm ret = SA_INVALID_NAME; 11834653Sdougm (void) printf(gettext("Invalid group name: %s\n"), 11844653Sdougm groupname); 11854653Sdougm } 11863034Sdougm } 11873034Sdougm if (ret == SA_OK) { 11884653Sdougm /* check protocol vs optlist */ 11894653Sdougm if (optlist != NULL) { 11904653Sdougm /* check options, if any, for validity */ 11914653Sdougm ret = valid_options(optlist, protocol, group, NULL); 11924653Sdougm } 11933034Sdougm } 11943034Sdougm if (ret == SA_OK && !dryrun) { 11954653Sdougm if (group == NULL) { 11964653Sdougm group = sa_create_group(handle, (char *)groupname, 11974653Sdougm &err); 11983034Sdougm } 11994653Sdougm if (group != NULL) { 12004653Sdougm sa_optionset_t optionset; 12015331Samw /* 12025331Samw * First check to see if the new protocol is one that 12035331Samw * requires resource names and make sure we are 12045331Samw * compliant before proceeding. 12055331Samw */ 12065331Samw if (protocol != NULL) { 12075331Samw uint64_t features; 12085331Samw 12095331Samw features = sa_proto_get_featureset(protocol); 12105331Samw if ((features & SA_FEATURE_RESOURCE) && 12115331Samw !resource_compliant(group)) { 12125331Samw if (force) { 12135331Samw make_resources(group); 12145331Samw } else { 12155331Samw ret = SA_RESOURCE_REQUIRED; 12165331Samw (void) printf( 12175331Samw gettext("Protocol " 12185331Samw "requires resource " 12195331Samw "names to be " 12205331Samw "set: %s\n"), 12215331Samw protocol); 12225331Samw goto err; 12235331Samw } 12245331Samw } 12255331Samw } 12264653Sdougm if (optlist != NULL) { 12274653Sdougm (void) add_optionset(group, optlist, protocol, 12284653Sdougm &ret); 12294653Sdougm } else if (protocol != NULL) { 12304653Sdougm optionset = sa_create_optionset(group, 12314653Sdougm protocol); 12324653Sdougm if (optionset == NULL) 12334653Sdougm ret = SA_NO_MEMORY; 12344653Sdougm } else if (protocol == NULL) { 12354653Sdougm char **protolist; 12364653Sdougm int numprotos, i; 12374653Sdougm numprotos = sa_get_protocols(&protolist); 12384653Sdougm for (i = 0; i < numprotos; i++) { 12394653Sdougm optionset = sa_create_optionset(group, 12404653Sdougm protolist[i]); 12414653Sdougm } 12424653Sdougm if (protolist != NULL) 12434653Sdougm free(protolist); 12444653Sdougm } 12453034Sdougm /* 12464653Sdougm * We have a group and legal additions 12473034Sdougm */ 12484653Sdougm if (ret == SA_OK) { 12494653Sdougm /* 12504653Sdougm * Commit to configuration for protocols that 12514653Sdougm * need to do block updates. For NFS, this 12524653Sdougm * doesn't do anything but it will be run for 12534653Sdougm * all protocols that implement the 12544653Sdougm * appropriate plugin. 12554653Sdougm */ 12564653Sdougm ret = sa_update_config(handle); 12574653Sdougm } else { 12584653Sdougm if (group != NULL) 12594653Sdougm (void) sa_remove_group(group); 12604653Sdougm } 12613034Sdougm } else { 12624653Sdougm ret = err; 12634653Sdougm (void) printf(gettext("Could not create group: %s\n"), 12644653Sdougm sa_errorstr(ret)); 12653034Sdougm } 12663034Sdougm } 12673034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 12684653Sdougm (void) printf(gettext("Command would fail: %s\n"), 12694653Sdougm sa_errorstr(SA_NO_PERMISSION)); 12704653Sdougm ret = SA_NO_PERMISSION; 12713034Sdougm } 12725331Samw err: 12733034Sdougm free_opt(optlist); 12743034Sdougm return (ret); 12753034Sdougm } 12763034Sdougm 12773034Sdougm /* 12783034Sdougm * group_status(group) 12793034Sdougm * 12803034Sdougm * return the current status (enabled/disabled) of the group. 12813034Sdougm */ 12823034Sdougm 12833034Sdougm static char * 12843034Sdougm group_status(sa_group_t group) 12853034Sdougm { 12863034Sdougm char *state; 12873034Sdougm int enabled = 0; 12883034Sdougm 12893034Sdougm state = sa_get_group_attr(group, "state"); 12903034Sdougm if (state != NULL) { 12914653Sdougm if (strcmp(state, "enabled") == 0) { 12924653Sdougm enabled = 1; 12934653Sdougm } 12944653Sdougm sa_free_attr_string(state); 12953034Sdougm } 12964255Sdougm return (enabled ? "enabled" : "disabled"); 12973034Sdougm } 12983034Sdougm 12993034Sdougm /* 13003034Sdougm * sa_delete(flags, argc, argv) 13013034Sdougm * 13023034Sdougm * Delete a group. 13033034Sdougm */ 13043034Sdougm 13053034Sdougm static int 13063910Sdougm sa_delete(sa_handle_t handle, int flags, int argc, char *argv[]) 13073034Sdougm { 13083034Sdougm char *groupname; 13093034Sdougm sa_group_t group; 13103034Sdougm sa_share_t share; 13113034Sdougm int verbose = 0; 13123034Sdougm int dryrun = 0; 13133034Sdougm int force = 0; 13143034Sdougm int c; 13153034Sdougm char *protocol = NULL; 13163034Sdougm char *sectype = NULL; 13173034Sdougm int ret = SA_OK; 13183034Sdougm int auth; 13193034Sdougm 13203034Sdougm while ((c = getopt(argc, argv, "?hvnP:fS:")) != EOF) { 13214653Sdougm switch (c) { 13224653Sdougm case 'v': 13234653Sdougm verbose++; 13244653Sdougm break; 13254653Sdougm case 'n': 13264653Sdougm dryrun++; 13274653Sdougm break; 13284653Sdougm case 'P': 13295331Samw if (protocol != NULL) { 13305331Samw (void) printf(gettext("Specifying " 13315331Samw "multiple protocols " 13325331Samw "not supported: %s\n"), protocol); 13335331Samw return (SA_SYNTAX_ERR); 13345331Samw } 13354653Sdougm protocol = optarg; 13364653Sdougm if (!sa_valid_protocol(protocol)) { 13374653Sdougm (void) printf(gettext("Invalid protocol " 13385331Samw "specified: %s\n"), protocol); 13394653Sdougm return (SA_INVALID_PROTOCOL); 13404653Sdougm } 13414653Sdougm break; 13424653Sdougm case 'S': 13435331Samw if (sectype != NULL) { 13445331Samw (void) printf(gettext("Specifying " 13455331Samw "multiple property " 13465331Samw "spaces not supported: %s\n"), sectype); 13475331Samw return (SA_SYNTAX_ERR); 13485331Samw } 13494653Sdougm sectype = optarg; 13504653Sdougm break; 13514653Sdougm case 'f': 13524653Sdougm force++; 13534653Sdougm break; 13544653Sdougm default: 13554653Sdougm case 'h': 13564653Sdougm case '?': 13574653Sdougm (void) printf(gettext("usage: %s\n"), 13584653Sdougm sa_get_usage(USAGE_DELETE)); 13594653Sdougm return (0); 13603034Sdougm } 13613034Sdougm } 13623034Sdougm 13633034Sdougm if (optind >= argc) { 13644653Sdougm (void) printf(gettext("usage: %s\n"), 13654653Sdougm sa_get_usage(USAGE_DELETE)); 13664653Sdougm (void) printf(gettext("\tgroup must be specified.\n")); 13674653Sdougm return (SA_SYNTAX_ERR); 13683034Sdougm } 13693034Sdougm 13703034Sdougm if ((optind + 1) < argc) { 13714653Sdougm (void) printf(gettext("usage: %s\n"), 13724653Sdougm sa_get_usage(USAGE_DELETE)); 13734653Sdougm (void) printf(gettext("\textraneous group(s) at end\n")); 13744653Sdougm return (SA_SYNTAX_ERR); 13753034Sdougm } 13763034Sdougm 13773034Sdougm if (sectype != NULL && protocol == NULL) { 13784653Sdougm (void) printf(gettext("usage: %s\n"), 13794653Sdougm sa_get_usage(USAGE_DELETE)); 13804653Sdougm (void) printf(gettext("\tsecurity requires protocol to be " 13814653Sdougm "specified.\n")); 13824653Sdougm return (SA_SYNTAX_ERR); 13833034Sdougm } 13843034Sdougm 13853034Sdougm /* 13863034Sdougm * Determine if the group already exists since it must in 13873034Sdougm * order to be removed. 13883034Sdougm * 13893034Sdougm * We can delete when: 13903034Sdougm * 13913034Sdougm * - group is empty 13923034Sdougm * - force flag is set 13933034Sdougm * - if protocol specified, only delete the protocol 13943034Sdougm */ 13953034Sdougm 13963034Sdougm groupname = argv[optind]; 13973910Sdougm group = sa_get_group(handle, groupname); 13983034Sdougm if (group == NULL) { 13993034Sdougm ret = SA_NO_SUCH_GROUP; 14004653Sdougm goto done; 14014653Sdougm } 14024653Sdougm auth = check_authorizations(groupname, flags); 14034653Sdougm if (protocol == NULL) { 14043034Sdougm share = sa_get_share(group, NULL); 14053034Sdougm if (share != NULL) 14064653Sdougm ret = SA_BUSY; 14073034Sdougm if (share == NULL || (share != NULL && force == 1)) { 14084653Sdougm ret = SA_OK; 14094653Sdougm if (!dryrun) { 14104653Sdougm while (share != NULL) { 14114653Sdougm sa_share_t next_share; 14124653Sdougm next_share = sa_get_next_share(share); 14134653Sdougm /* 14144653Sdougm * need to do the disable of 14154653Sdougm * each share, but don't 14164653Sdougm * actually do anything on a 14174653Sdougm * dryrun. 14184653Sdougm */ 14194653Sdougm ret = sa_disable_share(share, NULL); 14204653Sdougm ret = sa_remove_share(share); 14214653Sdougm share = next_share; 14224653Sdougm } 14234653Sdougm ret = sa_remove_group(group); 14243034Sdougm } 14253034Sdougm } 14264653Sdougm /* Commit to configuration if not a dryrun */ 14273034Sdougm if (!dryrun && ret == SA_OK) { 14284653Sdougm ret = sa_update_config(handle); 14293034Sdougm } 14304653Sdougm } else { 14313034Sdougm /* a protocol delete */ 14323034Sdougm sa_optionset_t optionset; 14333034Sdougm sa_security_t security; 14345331Samw if (sectype != NULL) { 14354653Sdougm /* only delete specified security */ 14364653Sdougm security = sa_get_security(group, sectype, protocol); 14374653Sdougm if (security != NULL && !dryrun) 14384653Sdougm ret = sa_destroy_security(security); 14394653Sdougm else 14404653Sdougm ret = SA_INVALID_PROTOCOL; 14413034Sdougm } else { 14424653Sdougm optionset = sa_get_optionset(group, protocol); 14434653Sdougm if (optionset != NULL && !dryrun) { 14444653Sdougm /* 14454653Sdougm * have an optionset with 14464653Sdougm * protocol to delete 14474653Sdougm */ 14484653Sdougm ret = sa_destroy_optionset(optionset); 14494653Sdougm /* 14504653Sdougm * Now find all security sets 14514653Sdougm * for the protocol and remove 14524653Sdougm * them. Don't remove other 14534653Sdougm * protocols. 14544653Sdougm */ 14554653Sdougm for (security = 14564653Sdougm sa_get_security(group, NULL, NULL); 14574653Sdougm ret == SA_OK && security != NULL; 14584653Sdougm security = sa_get_next_security(security)) { 14594653Sdougm char *secprot; 14604653Sdougm secprot = sa_get_security_attr(security, 14614653Sdougm "type"); 14624653Sdougm if (secprot != NULL && 14634653Sdougm strcmp(secprot, protocol) == 0) 14644653Sdougm ret = sa_destroy_security( 14654653Sdougm security); 14664653Sdougm if (secprot != NULL) 14674653Sdougm sa_free_attr_string(secprot); 14684653Sdougm } 14694653Sdougm } else { 14704653Sdougm if (!dryrun) 14714653Sdougm ret = SA_INVALID_PROTOCOL; 14723034Sdougm } 14733034Sdougm } 14745331Samw /* 14755331Samw * With the protocol items removed, make sure that all 14765331Samw * the shares are updated in the legacy files, if 14775331Samw * necessary. 14785331Samw */ 14795331Samw for (share = sa_get_share(group, NULL); 14805331Samw share != NULL; 14815331Samw share = sa_get_next_share(share)) { 14825331Samw (void) sa_delete_legacy(share, protocol); 14835331Samw } 14843034Sdougm } 14854653Sdougm 14864653Sdougm done: 14873034Sdougm if (ret != SA_OK) { 14884653Sdougm (void) printf(gettext("Could not delete group: %s\n"), 14894653Sdougm sa_errorstr(ret)); 14903034Sdougm } else if (dryrun && !auth && verbose) { 14914653Sdougm (void) printf(gettext("Command would fail: %s\n"), 14924653Sdougm sa_errorstr(SA_NO_PERMISSION)); 14933034Sdougm } 14943034Sdougm return (ret); 14953034Sdougm } 14963034Sdougm 14973034Sdougm /* 14983034Sdougm * strndupr(*buff, str, buffsize) 14993034Sdougm * 15003034Sdougm * used with small strings to duplicate and possibly increase the 15013034Sdougm * buffer size of a string. 15023034Sdougm */ 15033034Sdougm static char * 15043034Sdougm strndupr(char *buff, char *str, int *buffsize) 15053034Sdougm { 15063034Sdougm int limit; 15073034Sdougm char *orig_buff = buff; 15083034Sdougm 15093034Sdougm if (buff == NULL) { 15104653Sdougm buff = (char *)malloc(64); 15114653Sdougm if (buff == NULL) 15124653Sdougm return (NULL); 15134653Sdougm *buffsize = 64; 15144653Sdougm buff[0] = '\0'; 15153034Sdougm } 15163034Sdougm limit = strlen(buff) + strlen(str) + 1; 15173034Sdougm if (limit > *buffsize) { 15184653Sdougm limit = *buffsize = *buffsize + ((limit / 64) + 64); 15194653Sdougm buff = realloc(buff, limit); 15203034Sdougm } 15213034Sdougm if (buff != NULL) { 15224653Sdougm (void) strcat(buff, str); 15233034Sdougm } else { 15244653Sdougm /* if it fails, fail it hard */ 15254653Sdougm if (orig_buff != NULL) 15264653Sdougm free(orig_buff); 15273034Sdougm } 15283034Sdougm return (buff); 15293034Sdougm } 15303034Sdougm 15313034Sdougm /* 15323034Sdougm * group_proto(group) 15333034Sdougm * 15343034Sdougm * return a string of all the protocols (space separated) associated 15353034Sdougm * with this group. 15363034Sdougm */ 15373034Sdougm 15383034Sdougm static char * 15393034Sdougm group_proto(sa_group_t group) 15403034Sdougm { 15413034Sdougm sa_optionset_t optionset; 15423034Sdougm char *proto; 15433034Sdougm char *buff = NULL; 15443034Sdougm int buffsize = 0; 15453034Sdougm int addspace = 0; 15463034Sdougm /* 15473034Sdougm * get the protocol list by finding the optionsets on this 15483034Sdougm * group and extracting the type value. The initial call to 15493034Sdougm * strndupr() initailizes buff. 15503034Sdougm */ 15513034Sdougm buff = strndupr(buff, "", &buffsize); 15523034Sdougm if (buff != NULL) { 15534653Sdougm for (optionset = sa_get_optionset(group, NULL); 15544653Sdougm optionset != NULL && buff != NULL; 15554653Sdougm optionset = sa_get_next_optionset(optionset)) { 15564653Sdougm /* 15574653Sdougm * extract out the protocol type from this optionset 15584653Sdougm * and append it to the buffer "buff". strndupr() will 15594653Sdougm * reallocate space as necessay. 15604653Sdougm */ 15614653Sdougm proto = sa_get_optionset_attr(optionset, "type"); 15624653Sdougm if (proto != NULL) { 15634653Sdougm if (addspace++) 15644653Sdougm buff = strndupr(buff, " ", &buffsize); 15654653Sdougm buff = strndupr(buff, proto, &buffsize); 15664653Sdougm sa_free_attr_string(proto); 15674653Sdougm } 15683034Sdougm } 15693034Sdougm } 15703034Sdougm return (buff); 15713034Sdougm } 15723034Sdougm 15733034Sdougm /* 15743034Sdougm * sa_list(flags, argc, argv) 15753034Sdougm * 15763034Sdougm * implements the "list" subcommand to list groups and optionally 15773034Sdougm * their state and protocols. 15783034Sdougm */ 15793034Sdougm 15803034Sdougm static int 15813910Sdougm sa_list(sa_handle_t handle, int flags, int argc, char *argv[]) 15823034Sdougm { 15833034Sdougm sa_group_t group; 15843034Sdougm int verbose = 0; 15853034Sdougm int c; 15863034Sdougm char *protocol = NULL; 15875331Samw #ifdef lint 15885331Samw flags = flags; 15895331Samw #endif 15903034Sdougm 15913034Sdougm while ((c = getopt(argc, argv, "?hvP:")) != EOF) { 15924653Sdougm switch (c) { 15934653Sdougm case 'v': 15944653Sdougm verbose++; 15954653Sdougm break; 15964653Sdougm case 'P': 15975331Samw if (protocol != NULL) { 15985331Samw (void) printf(gettext( 15995331Samw "Specifying multiple protocols " 16005331Samw "not supported: %s\n"), 16015331Samw protocol); 16025331Samw return (SA_SYNTAX_ERR); 16035331Samw } 16044653Sdougm protocol = optarg; 16054653Sdougm if (!sa_valid_protocol(protocol)) { 16064653Sdougm (void) printf(gettext( 16074653Sdougm "Invalid protocol specified: %s\n"), 16084653Sdougm protocol); 16094653Sdougm return (SA_INVALID_PROTOCOL); 16104653Sdougm } 16114653Sdougm break; 16124653Sdougm default: 16134653Sdougm case 'h': 16144653Sdougm case '?': 16154653Sdougm (void) printf(gettext("usage: %s\n"), 16164653Sdougm sa_get_usage(USAGE_LIST)); 16174653Sdougm return (0); 16183034Sdougm } 16193034Sdougm } 16203034Sdougm 16215885Sdougm if (optind != argc) { 16225885Sdougm (void) printf(gettext("usage: %s\n"), 16235885Sdougm sa_get_usage(USAGE_LIST)); 16245885Sdougm return (SA_SYNTAX_ERR); 16255885Sdougm } 16265885Sdougm 16274653Sdougm for (group = sa_get_group(handle, NULL); 16284653Sdougm group != NULL; 16293034Sdougm group = sa_get_next_group(group)) { 16304653Sdougm char *name; 16314653Sdougm char *proto; 16324653Sdougm if (protocol == NULL || has_protocol(group, protocol)) { 16334653Sdougm name = sa_get_group_attr(group, "name"); 16344653Sdougm if (name != NULL && (verbose > 1 || name[0] != '#')) { 16354653Sdougm (void) printf("%s", (char *)name); 16364653Sdougm if (verbose) { 16374653Sdougm /* 16384653Sdougm * Need the list of protocols 16394653Sdougm * and current status once 16404653Sdougm * available. We do want to 16414653Sdougm * translate the 16424653Sdougm * enabled/disabled text here. 16434653Sdougm */ 16444653Sdougm (void) printf("\t%s", isenabled(group) ? 16454653Sdougm gettext("enabled") : 16464653Sdougm gettext("disabled")); 16474653Sdougm proto = group_proto(group); 16484653Sdougm if (proto != NULL) { 16494653Sdougm (void) printf("\t%s", 16504653Sdougm (char *)proto); 16514653Sdougm free(proto); 16524653Sdougm } 16534653Sdougm } 16544653Sdougm (void) printf("\n"); 16553034Sdougm } 16564653Sdougm if (name != NULL) 16574653Sdougm sa_free_attr_string(name); 16583034Sdougm } 16593034Sdougm } 16603034Sdougm return (0); 16613034Sdougm } 16623034Sdougm 16633034Sdougm /* 16643034Sdougm * out_properties(optionset, proto, sec) 16653034Sdougm * 16663034Sdougm * Format the properties and encode the protocol and optional named 16673034Sdougm * optionset into the string. 16683034Sdougm * 16693034Sdougm * format is protocol[:name]=(property-list) 16703034Sdougm */ 16713034Sdougm 16723034Sdougm static void 16733034Sdougm out_properties(sa_optionset_t optionset, char *proto, char *sec) 16743034Sdougm { 16753034Sdougm char *type; 16763034Sdougm char *value; 16773034Sdougm int spacer; 16783034Sdougm sa_property_t prop; 16793034Sdougm 16804653Sdougm if (sec == NULL) 16814653Sdougm (void) printf(" %s=(", proto ? proto : gettext("all")); 16824653Sdougm else 16834653Sdougm (void) printf(" %s:%s=(", proto ? proto : gettext("all"), sec); 16843034Sdougm 16853034Sdougm for (spacer = 0, prop = sa_get_property(optionset, NULL); 16864653Sdougm prop != NULL; 16874653Sdougm prop = sa_get_next_property(prop)) { 16883034Sdougm 16893034Sdougm /* 16903034Sdougm * extract the property name/value and output with 16913034Sdougm * appropriate spacing. I.e. no prefixed space the 16923034Sdougm * first time through but a space on subsequent 16933034Sdougm * properties. 16943034Sdougm */ 16954653Sdougm type = sa_get_property_attr(prop, "type"); 16964653Sdougm value = sa_get_property_attr(prop, "value"); 16974653Sdougm if (type != NULL) { 16984653Sdougm (void) printf("%s%s=", spacer ? " " : "", type); 16994653Sdougm spacer = 1; 17004653Sdougm if (value != NULL) 17014653Sdougm (void) printf("\"%s\"", value); 17024653Sdougm else 17034653Sdougm (void) printf("\"\""); 17044653Sdougm } 17054653Sdougm if (type != NULL) 17064653Sdougm sa_free_attr_string(type); 17073034Sdougm if (value != NULL) 17084653Sdougm sa_free_attr_string(value); 17093034Sdougm } 17103034Sdougm (void) printf(")"); 17113034Sdougm } 17123034Sdougm 17133034Sdougm /* 17143034Sdougm * show_properties(group, protocol, prefix) 17153034Sdougm * 17163034Sdougm * print the properties for a group. If protocol is NULL, do all 17173034Sdougm * protocols otherwise only the specified protocol. All security 17183034Sdougm * (named groups specific to the protocol) are included. 17193034Sdougm * 17203034Sdougm * The "prefix" is always applied. The caller knows whether it wants 17213034Sdougm * some type of prefix string (white space) or not. Once the prefix 17223034Sdougm * has been output, it is reduced to the zero length string for the 17233034Sdougm * remainder of the property output. 17243034Sdougm */ 17253034Sdougm 17263034Sdougm static void 17273034Sdougm show_properties(sa_group_t group, char *protocol, char *prefix) 17283034Sdougm { 17293034Sdougm sa_optionset_t optionset; 17303034Sdougm sa_security_t security; 17313034Sdougm char *value; 17323034Sdougm char *secvalue; 17333034Sdougm 17343034Sdougm if (protocol != NULL) { 17354653Sdougm optionset = sa_get_optionset(group, protocol); 17364653Sdougm if (optionset != NULL) { 17374653Sdougm (void) printf("%s", prefix); 17384653Sdougm prefix = ""; 17394653Sdougm out_properties(optionset, protocol, NULL); 17404653Sdougm } 17414653Sdougm security = sa_get_security(group, protocol, NULL); 17424653Sdougm if (security != NULL) { 17434653Sdougm (void) printf("%s", prefix); 17444653Sdougm prefix = ""; 17454653Sdougm out_properties(security, protocol, NULL); 17464653Sdougm } 17473034Sdougm } else { 17484653Sdougm for (optionset = sa_get_optionset(group, protocol); 17494653Sdougm optionset != NULL; 17504653Sdougm optionset = sa_get_next_optionset(optionset)) { 17514653Sdougm 17524653Sdougm value = sa_get_optionset_attr(optionset, "type"); 17534653Sdougm (void) printf("%s", prefix); 17544653Sdougm prefix = ""; 17554653Sdougm out_properties(optionset, value, 0); 17564653Sdougm if (value != NULL) 17574653Sdougm sa_free_attr_string(value); 17584653Sdougm } 17594653Sdougm for (security = sa_get_security(group, NULL, protocol); 17604653Sdougm security != NULL; 17614653Sdougm security = sa_get_next_security(security)) { 17624653Sdougm 17634653Sdougm value = sa_get_security_attr(security, "type"); 17644653Sdougm secvalue = sa_get_security_attr(security, "sectype"); 17654653Sdougm (void) printf("%s", prefix); 17664653Sdougm prefix = ""; 17674653Sdougm out_properties(security, value, secvalue); 17684653Sdougm if (value != NULL) 17694653Sdougm sa_free_attr_string(value); 17704653Sdougm if (secvalue != NULL) 17714653Sdougm sa_free_attr_string(secvalue); 17724653Sdougm } 17733034Sdougm } 17743034Sdougm } 17753034Sdougm 17763034Sdougm /* 17775331Samw * get_resource(share) 17785331Samw * 17795331Samw * Get the first resource name, if any, and fix string to be in 17805331Samw * current locale and have quotes if it has embedded spaces. Return 17815331Samw * an attr string that must be freed. 17825331Samw */ 17835331Samw 17845331Samw static char * 17855331Samw get_resource(sa_share_t share) 17865331Samw { 17875331Samw sa_resource_t resource; 17885331Samw char *resstring = NULL; 17895331Samw char *retstring; 17905331Samw 17915331Samw if ((resource = sa_get_share_resource(share, NULL)) != NULL) { 17925331Samw resstring = sa_get_resource_attr(resource, "name"); 17935331Samw if (resstring != NULL) { 17945331Samw char *cp; 17955331Samw int len; 17965331Samw 17975331Samw retstring = conv_from_utf8(resstring); 17985331Samw if (retstring != resstring) { 17995331Samw sa_free_attr_string(resstring); 18005331Samw resstring = retstring; 18015331Samw } 18025331Samw if (strpbrk(resstring, " ") != NULL) { 18035331Samw /* account for quotes */ 18045331Samw len = strlen(resstring) + 3; 18055331Samw cp = calloc(len, sizeof (char)); 18065331Samw if (cp != NULL) { 18075331Samw (void) snprintf(cp, len, 18085331Samw "\"%s\"", resstring); 18095331Samw sa_free_attr_string(resstring); 18105331Samw resstring = cp; 18115331Samw } else { 18125331Samw sa_free_attr_string(resstring); 18135331Samw resstring = NULL; 18145331Samw } 18155331Samw } 18165331Samw } 18175331Samw } 18185331Samw return (resstring); 18195331Samw } 18205331Samw 18215331Samw /* 18225331Samw * has_resource_with_opt(share) 18235331Samw * 18245331Samw * Check to see if the share has any resource names with optionsets 18255331Samw * set. Also indicate if multiple resource names since the syntax 18265331Samw * would be about the same. 18275331Samw */ 18285331Samw static int 18295331Samw has_resource_with_opt(sa_share_t share) 18305331Samw { 18315331Samw sa_resource_t resource; 18325331Samw int ret = B_FALSE; 18335331Samw 18345331Samw for (resource = sa_get_share_resource(share, NULL); 18355331Samw resource != NULL; 18365331Samw resource = sa_get_next_resource(resource)) { 18375331Samw 18385331Samw if (sa_get_optionset(resource, NULL) != NULL) { 18395331Samw ret = B_TRUE; 18405331Samw break; 18415331Samw } 18425331Samw } 18435331Samw return (ret); 18445331Samw } 18455331Samw 18465331Samw /* 18475331Samw * has_multiple_resource(share) 18485331Samw * 18495885Sdougm * Check to see if the share has multiple resource names since 18505885Sdougm * the syntax would be about the same. 18515331Samw */ 18525885Sdougm static boolean_t 18535331Samw has_multiple_resource(sa_share_t share) 18545331Samw { 18555331Samw sa_resource_t resource; 18565331Samw int num; 18575331Samw 18585331Samw for (num = 0, resource = sa_get_share_resource(share, NULL); 18595331Samw resource != NULL; 18605331Samw resource = sa_get_next_resource(resource)) { 18615331Samw num++; 18625331Samw if (num > 1) 18635331Samw return (B_TRUE); 18645331Samw } 18655331Samw return (B_FALSE); 18665331Samw } 18675331Samw 18685331Samw /* 18695331Samw * show_share(share, verbose, properties, proto, iszfs, sharepath) 18705331Samw * 18715331Samw * print out the share information. With the addition of resource as a 18725331Samw * full object that can have multiple instances below the share, we 18735331Samw * need to display that as well. 18745331Samw */ 18755331Samw 18765331Samw static void 18775331Samw show_share(sa_share_t share, int verbose, int properties, char *proto, 18785331Samw int iszfs, char *sharepath) 18795331Samw { 18805331Samw char *drive; 18815331Samw char *exclude; 18825331Samw sa_resource_t resource = NULL; 18835331Samw char *description; 18845331Samw char *rsrcname; 18855331Samw int rsrcwithopt; 18865885Sdougm boolean_t multiple; 18875331Samw char *type; 18885331Samw 18895331Samw rsrcwithopt = has_resource_with_opt(share); 18905331Samw 18915331Samw if (verbose || (properties && rsrcwithopt)) { 18925331Samw /* First, indicate if transient */ 18935331Samw type = sa_get_share_attr(share, "type"); 18945331Samw if (type != NULL && !iszfs && verbose && 18955331Samw strcmp(type, "transient") == 0) 18965331Samw (void) printf("\t* "); 18975331Samw else 18985331Samw (void) printf("\t "); 18995331Samw 19005331Samw if (type != NULL) 19015331Samw sa_free_attr_string(type); 19025331Samw 19035331Samw /* 19045331Samw * If we came in with verbose, we want to handle the case of 19055331Samw * multiple resources as though they had properties set. 19065331Samw */ 19075331Samw multiple = has_multiple_resource(share); 19085331Samw 19095885Sdougm /* 19105885Sdougm * if there is a description on the share and there 19115885Sdougm * are resources, treat as multiple resources in order 19125885Sdougm * to get all descriptions displayed. 19135885Sdougm */ 19145885Sdougm description = sa_get_share_description(share); 19155885Sdougm resource = sa_get_share_resource(share, NULL); 19165885Sdougm 19175885Sdougm if (description != NULL && resource != NULL) 19185885Sdougm multiple = B_TRUE; 19195885Sdougm 19205331Samw /* Next, if not multiple follow old model */ 19215331Samw if (!multiple && !rsrcwithopt) { 19225331Samw rsrcname = get_resource(share); 19235331Samw if (rsrcname != NULL && strlen(rsrcname) > 0) { 19245331Samw (void) printf("%s=%s", rsrcname, sharepath); 19255331Samw } else { 19265331Samw (void) printf("%s", sharepath); 19275331Samw } 19285331Samw if (rsrcname != NULL) 19295331Samw sa_free_attr_string(rsrcname); 19305885Sdougm /* Print the description string if there is one. */ 19315885Sdougm print_rsrc_desc(resource, description); 19325331Samw } else { 19335331Samw /* Treat as simple and then resources come later */ 19345331Samw (void) printf("%s", sharepath); 19355331Samw } 19365331Samw drive = sa_get_share_attr(share, "drive-letter"); 19375331Samw if (drive != NULL) { 19385331Samw if (strlen(drive) > 0) 19395331Samw (void) printf(gettext("\tdrive-letter=\"%s:\""), 19405331Samw drive); 19415331Samw sa_free_attr_string(drive); 19425331Samw } 19435331Samw if (properties) 19445331Samw show_properties(share, proto, "\t"); 19455331Samw exclude = sa_get_share_attr(share, "exclude"); 19465331Samw if (exclude != NULL) { 19475331Samw (void) printf(gettext("\tnot-shared-with=[%s]"), 19485331Samw exclude); 19495331Samw sa_free_attr_string(exclude); 19505331Samw } 19515885Sdougm 19525331Samw if (description != NULL) { 19535885Sdougm print_rsrc_desc((sa_resource_t)share, description); 19545331Samw } 19555331Samw /* 19565331Samw * If there are resource names with options, show them 19575331Samw * here, with one line per resource. Resource specific 19585331Samw * options are at the end of the line followed by 19595331Samw * description, if any. 19605331Samw */ 19615331Samw if (rsrcwithopt || multiple) { 19625331Samw for (resource = sa_get_share_resource(share, NULL); 19635331Samw resource != NULL; 19645331Samw resource = sa_get_next_resource(resource)) { 19655331Samw int has_space; 19665331Samw char *rsrc; 19675331Samw 19685331Samw (void) printf("\n\t\t "); 19695331Samw rsrcname = sa_get_resource_attr(resource, 19705331Samw "name"); 19715331Samw if (rsrcname == NULL) 19725331Samw continue; 19735331Samw 19745331Samw rsrc = conv_from_utf8(rsrcname); 19755331Samw has_space = strpbrk(rsrc, " ") != NULL; 19765331Samw 19775331Samw if (has_space) 19785331Samw (void) printf("\"%s\"=%s", rsrc, 19795331Samw sharepath); 19805331Samw else 19815331Samw (void) printf("%s=%s", rsrc, 19825331Samw sharepath); 19835331Samw if (rsrc != rsrcname) 19845331Samw sa_free_attr_string(rsrc); 19855331Samw sa_free_attr_string(rsrcname); 19865331Samw if (properties || rsrcwithopt) 19875331Samw show_properties(resource, proto, "\t"); 19885331Samw 19895331Samw /* Get description string if any */ 19905885Sdougm print_rsrc_desc(resource, description); 19915331Samw } 19925331Samw } 19935885Sdougm if (description != NULL) 19945885Sdougm sa_free_share_description(description); 19955331Samw } else { 19965331Samw (void) printf("\t %s", sharepath); 19975331Samw if (properties) 19985331Samw show_properties(share, proto, "\t"); 19995331Samw } 20005331Samw (void) printf("\n"); 20015331Samw } 20025331Samw 20035331Samw /* 20043034Sdougm * show_group(group, verbose, properties, proto, subgroup) 20053034Sdougm * 20063034Sdougm * helper function to show the contents of a group. 20073034Sdougm */ 20083034Sdougm 20093034Sdougm static void 20103034Sdougm show_group(sa_group_t group, int verbose, int properties, char *proto, 20115331Samw char *subgroup) 20123034Sdougm { 20133034Sdougm sa_share_t share; 20143034Sdougm char *groupname; 20153034Sdougm char *zfs = NULL; 20163034Sdougm int iszfs = 0; 20175331Samw char *sharepath; 20183034Sdougm 20193034Sdougm groupname = sa_get_group_attr(group, "name"); 20203034Sdougm if (groupname != NULL) { 20214653Sdougm if (proto != NULL && !has_protocol(group, proto)) { 20224653Sdougm sa_free_attr_string(groupname); 20234653Sdougm return; 20244653Sdougm } 20253034Sdougm /* 20263034Sdougm * check to see if the group is managed by ZFS. If 20273034Sdougm * there is an attribute, then it is. A non-NULL zfs 20283034Sdougm * variable will trigger the different way to display 20293034Sdougm * and will remove the transient property indicator 20303034Sdougm * from the output. 20313034Sdougm */ 20324653Sdougm zfs = sa_get_group_attr(group, "zfs"); 20334653Sdougm if (zfs != NULL) { 20344653Sdougm iszfs = 1; 20354653Sdougm sa_free_attr_string(zfs); 20363034Sdougm } 20374653Sdougm share = sa_get_share(group, NULL); 20384653Sdougm if (subgroup == NULL) 20394653Sdougm (void) printf("%s", groupname); 20404653Sdougm else 20414653Sdougm (void) printf(" %s/%s", subgroup, groupname); 20424653Sdougm if (properties) 20434653Sdougm show_properties(group, proto, ""); 20444653Sdougm (void) printf("\n"); 20454653Sdougm if (strcmp(groupname, "zfs") == 0) { 20464653Sdougm sa_group_t zgroup; 20474653Sdougm 20484653Sdougm for (zgroup = sa_get_sub_group(group); 20494653Sdougm zgroup != NULL; 20504653Sdougm zgroup = sa_get_next_group(zgroup)) { 20514653Sdougm show_group(zgroup, verbose, properties, proto, 20524653Sdougm "zfs"); 20534653Sdougm } 20544653Sdougm sa_free_attr_string(groupname); 20554653Sdougm return; 20564653Sdougm } 20573034Sdougm /* 20584653Sdougm * Have a group, so list the contents. Resource and 20593034Sdougm * description are only listed if verbose is set. 20603034Sdougm */ 20614653Sdougm for (share = sa_get_share(group, NULL); 20624653Sdougm share != NULL; 20634653Sdougm share = sa_get_next_share(share)) { 20644653Sdougm sharepath = sa_get_share_attr(share, "path"); 20654653Sdougm if (sharepath != NULL) { 20665331Samw show_share(share, verbose, properties, proto, 20675331Samw iszfs, sharepath); 20684653Sdougm sa_free_attr_string(sharepath); 20693034Sdougm } 20703034Sdougm } 20713034Sdougm } 20723034Sdougm if (groupname != NULL) { 20733034Sdougm sa_free_attr_string(groupname); 20743034Sdougm } 20753034Sdougm } 20763034Sdougm 20773034Sdougm /* 20783034Sdougm * show_group_xml_init() 20793034Sdougm * 20803034Sdougm * Create an XML document that will be used to display config info via 20813034Sdougm * XML format. 20823034Sdougm */ 20833034Sdougm 20843034Sdougm xmlDocPtr 20853034Sdougm show_group_xml_init() 20863034Sdougm { 20873034Sdougm xmlDocPtr doc; 20883034Sdougm xmlNodePtr root; 20893034Sdougm 20903034Sdougm doc = xmlNewDoc((xmlChar *)"1.0"); 20913034Sdougm if (doc != NULL) { 20924653Sdougm root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 20934653Sdougm if (root != NULL) 20944653Sdougm xmlDocSetRootElement(doc, root); 20953034Sdougm } 20963034Sdougm return (doc); 20973034Sdougm } 20983034Sdougm 20993034Sdougm /* 21003034Sdougm * show_group_xml(doc, group) 21013034Sdougm * 21023034Sdougm * Copy the group info into the XML doc. 21033034Sdougm */ 21043034Sdougm 21053034Sdougm static void 21063034Sdougm show_group_xml(xmlDocPtr doc, sa_group_t group) 21073034Sdougm { 21083034Sdougm xmlNodePtr node; 21093034Sdougm xmlNodePtr root; 21103034Sdougm 21113034Sdougm root = xmlDocGetRootElement(doc); 21123034Sdougm node = xmlCopyNode((xmlNodePtr)group, 1); 21133034Sdougm if (node != NULL && root != NULL) { 21144653Sdougm xmlAddChild(root, node); 21153034Sdougm /* 21163034Sdougm * In the future, we may have interally used tags that 21173034Sdougm * should not appear in the XML output. Remove 21183034Sdougm * anything we don't want to show here. 21193034Sdougm */ 21203034Sdougm } 21213034Sdougm } 21223034Sdougm 21233034Sdougm /* 21243034Sdougm * sa_show(flags, argc, argv) 21253034Sdougm * 21263034Sdougm * Implements the show subcommand. 21273034Sdougm */ 21283034Sdougm 21293034Sdougm int 21303910Sdougm sa_show(sa_handle_t handle, int flags, int argc, char *argv[]) 21313034Sdougm { 21323034Sdougm sa_group_t group; 21333034Sdougm int verbose = 0; 21343034Sdougm int properties = 0; 21353034Sdougm int c; 21363034Sdougm int ret = SA_OK; 21373034Sdougm char *protocol = NULL; 21383034Sdougm int xml = 0; 21393034Sdougm xmlDocPtr doc; 21405331Samw #ifdef lint 21415331Samw flags = flags; 21425331Samw #endif 21433034Sdougm 21443034Sdougm while ((c = getopt(argc, argv, "?hvP:px")) != EOF) { 21454653Sdougm switch (c) { 21464653Sdougm case 'v': 21474653Sdougm verbose++; 21484653Sdougm break; 21494653Sdougm case 'p': 21504653Sdougm properties++; 21514653Sdougm break; 21524653Sdougm case 'P': 21535331Samw if (protocol != NULL) { 21545331Samw (void) printf(gettext( 21555331Samw "Specifying multiple protocols " 21565331Samw "not supported: %s\n"), 21575331Samw protocol); 21585331Samw return (SA_SYNTAX_ERR); 21595331Samw } 21604653Sdougm protocol = optarg; 21614653Sdougm if (!sa_valid_protocol(protocol)) { 21624653Sdougm (void) printf(gettext( 21634653Sdougm "Invalid protocol specified: %s\n"), 21644653Sdougm protocol); 21654653Sdougm return (SA_INVALID_PROTOCOL); 21664653Sdougm } 21674653Sdougm break; 21684653Sdougm case 'x': 21694653Sdougm xml++; 21704653Sdougm break; 21714653Sdougm default: 21724653Sdougm case 'h': 21734653Sdougm case '?': 21744653Sdougm (void) printf(gettext("usage: %s\n"), 21754653Sdougm sa_get_usage(USAGE_SHOW)); 21764653Sdougm return (0); 21773034Sdougm } 21783034Sdougm } 21793034Sdougm 21803034Sdougm if (xml) { 21814653Sdougm doc = show_group_xml_init(); 21824653Sdougm if (doc == NULL) 21834653Sdougm ret = SA_NO_MEMORY; 21843034Sdougm } 21853034Sdougm 21863034Sdougm if (optind == argc) { 21874653Sdougm /* No group specified so go through them all */ 21884653Sdougm for (group = sa_get_group(handle, NULL); 21894653Sdougm group != NULL; 21904653Sdougm group = sa_get_next_group(group)) { 21914653Sdougm /* 21924653Sdougm * Have a group so check if one we want and then list 21934653Sdougm * contents with appropriate options. 21944653Sdougm */ 21954653Sdougm if (xml) 21964653Sdougm show_group_xml(doc, group); 21974653Sdougm else 21984653Sdougm show_group(group, verbose, properties, protocol, 21994653Sdougm NULL); 22004653Sdougm } 22013034Sdougm } else { 22024653Sdougm /* Have a specified list of groups */ 22034653Sdougm for (; optind < argc; optind++) { 22044653Sdougm group = sa_get_group(handle, argv[optind]); 22054653Sdougm if (group != NULL) { 22064653Sdougm if (xml) 22074653Sdougm show_group_xml(doc, group); 22084653Sdougm else 22094653Sdougm show_group(group, verbose, properties, 22104653Sdougm protocol, NULL); 22114653Sdougm } else { 22124653Sdougm (void) printf(gettext("%s: not found\n"), 22134653Sdougm argv[optind]); 22144653Sdougm ret = SA_NO_SUCH_GROUP; 22154653Sdougm } 22163034Sdougm } 22173034Sdougm } 22183034Sdougm if (xml && ret == SA_OK) { 22194653Sdougm xmlDocFormatDump(stdout, doc, 1); 22204653Sdougm xmlFreeDoc(doc); 22213034Sdougm } 22223034Sdougm return (ret); 22233034Sdougm 22243034Sdougm } 22253034Sdougm 22263034Sdougm /* 22273034Sdougm * enable_share(group, share, update_legacy) 22283034Sdougm * 22293034Sdougm * helper function to enable a share if the group is enabled. 22303034Sdougm */ 22313034Sdougm 22323034Sdougm static int 22333910Sdougm enable_share(sa_handle_t handle, sa_group_t group, sa_share_t share, 22345331Samw int update_legacy) 22353034Sdougm { 22363034Sdougm char *value; 22373034Sdougm int enabled; 22383034Sdougm sa_optionset_t optionset; 22395331Samw int err; 22403034Sdougm int ret = SA_OK; 22413034Sdougm char *zfs = NULL; 22423034Sdougm int iszfs = 0; 22435331Samw int isshare; 22443034Sdougm 22453034Sdougm /* 22463034Sdougm * need to enable this share if the group is enabled but not 22473034Sdougm * otherwise. The enable is also done on each protocol 22483034Sdougm * represented in the group. 22493034Sdougm */ 22503034Sdougm value = sa_get_group_attr(group, "state"); 22513034Sdougm enabled = value != NULL && strcmp(value, "enabled") == 0; 22523034Sdougm if (value != NULL) 22534653Sdougm sa_free_attr_string(value); 22543034Sdougm /* remove legacy config if necessary */ 22553034Sdougm if (update_legacy) 22565331Samw ret = sa_delete_legacy(share, NULL); 22573034Sdougm zfs = sa_get_group_attr(group, "zfs"); 22583034Sdougm if (zfs != NULL) { 22594653Sdougm iszfs++; 22604653Sdougm sa_free_attr_string(zfs); 22613034Sdougm } 22623034Sdougm 22633034Sdougm /* 22643034Sdougm * Step through each optionset at the group level and 22653034Sdougm * enable the share based on the protocol type. This 22663034Sdougm * works because protocols must be set on the group 22673034Sdougm * for the protocol to be enabled. 22683034Sdougm */ 22695331Samw isshare = sa_is_share(share); 22703034Sdougm for (optionset = sa_get_optionset(group, NULL); 22713034Sdougm optionset != NULL && ret == SA_OK; 22723034Sdougm optionset = sa_get_next_optionset(optionset)) { 22734653Sdougm value = sa_get_optionset_attr(optionset, "type"); 22744653Sdougm if (value != NULL) { 22755331Samw if (enabled) { 22765331Samw if (isshare) { 22775331Samw err = sa_enable_share(share, value); 22785331Samw } else { 22795331Samw err = sa_enable_resource(share, value); 22805331Samw if (err == SA_NOT_SUPPORTED) { 22815331Samw sa_share_t parent; 22825331Samw parent = sa_get_resource_parent( 22835331Samw share); 22845331Samw if (parent != NULL) 22855331Samw err = sa_enable_share( 22865331Samw parent, value); 22875331Samw } 22885331Samw } 22895331Samw if (err != SA_OK) { 22905331Samw ret = err; 22915331Samw (void) printf(gettext( 22925331Samw "Failed to enable share for " 22935331Samw "\"%s\": %s\n"), 22945331Samw value, sa_errorstr(ret)); 22955331Samw } 22965331Samw } 22975331Samw /* 22985331Samw * If we want to update the legacy, use a copy of 22995331Samw * share so we can avoid breaking the loop we are in 23005331Samw * since we might also need to go up the tree to the 23015331Samw * parent. 23025331Samw */ 23035331Samw if (update_legacy && !iszfs) { 23045331Samw sa_share_t update = share; 23055331Samw if (!sa_is_share(share)) { 23065331Samw update = sa_get_resource_parent(share); 23075331Samw } 23085331Samw (void) sa_update_legacy(update, value); 23095331Samw } 23104653Sdougm sa_free_attr_string(value); 23114653Sdougm } 23123034Sdougm } 23133034Sdougm if (ret == SA_OK) 23144653Sdougm (void) sa_update_config(handle); 23153034Sdougm return (ret); 23163034Sdougm } 23173034Sdougm 23183034Sdougm /* 23195331Samw * sa_require_resource(group) 23205331Samw * 23215331Samw * if any of the defined protocols on the group require resource 23225331Samw * names, then all shares must have them. 23235331Samw */ 23245331Samw 23255331Samw static int 23265331Samw sa_require_resource(sa_group_t group) 23275331Samw { 23285331Samw sa_optionset_t optionset; 23295331Samw 23305331Samw for (optionset = sa_get_optionset(group, NULL); 23315331Samw optionset != NULL; 23325331Samw optionset = sa_get_next_optionset(optionset)) { 23335331Samw char *proto; 23345331Samw 23355331Samw proto = sa_get_optionset_attr(optionset, "type"); 23365331Samw if (proto != NULL) { 23375331Samw uint64_t features; 23385331Samw 23395331Samw features = sa_proto_get_featureset(proto); 23405331Samw if (features & SA_FEATURE_RESOURCE) { 23415331Samw sa_free_attr_string(proto); 23425331Samw return (B_TRUE); 23435331Samw } 23445331Samw sa_free_attr_string(proto); 23455331Samw } 23465331Samw } 23475331Samw return (B_FALSE); 23485331Samw } 23495331Samw 23505331Samw /* 23513034Sdougm * sa_addshare(flags, argc, argv) 23523034Sdougm * 23533034Sdougm * implements add-share subcommand. 23543034Sdougm */ 23553034Sdougm 23565331Samw static int 23573910Sdougm sa_addshare(sa_handle_t handle, int flags, int argc, char *argv[]) 23583034Sdougm { 23593034Sdougm int verbose = 0; 23603034Sdougm int dryrun = 0; 23613034Sdougm int c; 23623034Sdougm int ret = SA_OK; 23633034Sdougm sa_group_t group; 23643034Sdougm sa_share_t share; 23655331Samw sa_resource_t resource = NULL; 23663034Sdougm char *sharepath = NULL; 23673034Sdougm char *description = NULL; 23685331Samw char *rsrcname = NULL; 23695331Samw char *rsrc = NULL; 23703034Sdougm int persist = SA_SHARE_PERMANENT; /* default to persist */ 23713034Sdougm int auth; 23723034Sdougm char dir[MAXPATHLEN]; 23733034Sdougm 23743034Sdougm while ((c = getopt(argc, argv, "?hvns:d:r:t")) != EOF) { 23754653Sdougm switch (c) { 23764653Sdougm case 'n': 23774653Sdougm dryrun++; 23784653Sdougm break; 23794653Sdougm case 'v': 23804653Sdougm verbose++; 23814653Sdougm break; 23824653Sdougm case 'd': 23834653Sdougm description = optarg; 23844653Sdougm break; 23854653Sdougm case 'r': 23865331Samw if (rsrcname != NULL) { 23875331Samw (void) printf(gettext("Adding multiple " 23885331Samw "resource names not" 23895331Samw " supported\n")); 23905331Samw return (SA_SYNTAX_ERR); 23915331Samw } 23925331Samw rsrcname = optarg; 23934653Sdougm break; 23944653Sdougm case 's': 23954653Sdougm /* 23964653Sdougm * Save share path into group. Currently limit 23974653Sdougm * to one share per command. 23984653Sdougm */ 23994653Sdougm if (sharepath != NULL) { 24004653Sdougm (void) printf(gettext( 24014653Sdougm "Adding multiple shares not supported\n")); 24025331Samw return (SA_SYNTAX_ERR); 24034653Sdougm } 24044653Sdougm sharepath = optarg; 24054653Sdougm break; 24064653Sdougm case 't': 24074653Sdougm persist = SA_SHARE_TRANSIENT; 24084653Sdougm break; 24094653Sdougm default: 24104653Sdougm case 'h': 24114653Sdougm case '?': 24124653Sdougm (void) printf(gettext("usage: %s\n"), 24134653Sdougm sa_get_usage(USAGE_ADD_SHARE)); 24144653Sdougm return (0); 24153034Sdougm } 24163034Sdougm } 24173034Sdougm 24183034Sdougm if (optind >= argc) { 24194653Sdougm (void) printf(gettext("usage: %s\n"), 24204653Sdougm sa_get_usage(USAGE_ADD_SHARE)); 24214653Sdougm if (dryrun || sharepath != NULL || description != NULL || 24225331Samw rsrcname != NULL || verbose || persist) { 24234653Sdougm (void) printf(gettext("\tgroup must be specified\n")); 24244653Sdougm ret = SA_NO_SUCH_GROUP; 24254653Sdougm } else { 24264653Sdougm ret = SA_OK; 24274653Sdougm } 24283034Sdougm } else { 24294653Sdougm if (sharepath == NULL) { 24304653Sdougm (void) printf(gettext("usage: %s\n"), 24314653Sdougm sa_get_usage(USAGE_ADD_SHARE)); 24324653Sdougm (void) printf(gettext( 24334653Sdougm "\t-s sharepath must be specified\n")); 24345331Samw ret = SA_BAD_PATH; 24354653Sdougm } 24365331Samw if (ret == SA_OK) { 24375331Samw if (realpath(sharepath, dir) == NULL) { 24385331Samw ret = SA_BAD_PATH; 24395331Samw (void) printf(gettext("Path " 24405331Samw "is not valid: %s\n"), 24415331Samw sharepath); 24425331Samw } else { 24435331Samw sharepath = dir; 24445331Samw } 24453034Sdougm } 24465331Samw if (ret == SA_OK && rsrcname != NULL) { 24475331Samw /* check for valid syntax */ 24485331Samw if (validresource(rsrcname)) { 24495331Samw rsrc = conv_to_utf8(rsrcname); 24505331Samw resource = sa_find_resource(handle, rsrc); 24515331Samw if (resource != NULL) { 24525331Samw /* 24535331Samw * Resource names must be 24545331Samw * unique in the system 24555331Samw */ 24565331Samw ret = SA_DUPLICATE_NAME; 24575331Samw (void) printf(gettext("usage: %s\n"), 24585331Samw sa_get_usage(USAGE_ADD_SHARE)); 24595331Samw (void) printf(gettext( 24605331Samw "\tresource names must be unique " 24615331Samw "in the system\n")); 24625331Samw } 24635331Samw } else { 24645331Samw (void) printf(gettext("usage: %s\n"), 24655331Samw sa_get_usage(USAGE_ADD_SHARE)); 24665331Samw (void) printf(gettext( 24675331Samw "\tresource names use restricted " 24685331Samw "character set\n")); 24695331Samw ret = SA_INVALID_NAME; 24705331Samw } 24713034Sdougm } 24725331Samw 24735331Samw if (ret != SA_OK) { 24745331Samw if (rsrc != NULL && rsrcname != rsrc) 24755331Samw sa_free_attr_string(rsrc); 24765331Samw return (ret); 24774653Sdougm } 24785331Samw 24794653Sdougm share = sa_find_share(handle, sharepath); 24804653Sdougm if (share != NULL) { 24815331Samw if (rsrcname == NULL) { 24825331Samw /* 24835331Samw * Can only have a duplicate share if a new 24845331Samw * resource name is being added. 24855331Samw */ 24865331Samw ret = SA_DUPLICATE_NAME; 24875331Samw (void) printf(gettext("Share path already " 24885331Samw "shared: %s\n"), sharepath); 24895331Samw } 24905331Samw } 24915331Samw if (ret != SA_OK) 24925331Samw return (ret); 24935331Samw 24945331Samw group = sa_get_group(handle, argv[optind]); 24955331Samw if (group != NULL) { 24965331Samw if (sa_require_resource(group) == B_TRUE && 24975331Samw rsrcname == NULL) { 24985331Samw (void) printf(gettext( 24995331Samw "Resource name is required " 25005331Samw "by at least one enabled protocol " 25015331Samw "in group\n")); 25025331Samw return (SA_RESOURCE_REQUIRED); 25035331Samw } 25045331Samw if (share == NULL && ret == SA_OK) { 25055331Samw if (dryrun) 25065331Samw ret = sa_check_path(group, sharepath, 25075331Samw SA_CHECK_NORMAL); 25085331Samw else 25095331Samw share = sa_add_share(group, sharepath, 25105331Samw persist, &ret); 25115331Samw } 25125331Samw /* 25135331Samw * Make sure this isn't an attempt to put a resourced 25145331Samw * share into a different group than it already is in. 25155331Samw */ 25165331Samw if (share != NULL) { 25175331Samw sa_group_t parent; 25185331Samw parent = sa_get_parent_group(share); 25195331Samw if (parent != group) { 25205331Samw ret = SA_DUPLICATE_NAME; 25214653Sdougm (void) printf(gettext( 25224653Sdougm "Share path already " 25235331Samw "shared: %s\n"), sharepath); 25244653Sdougm } 25253034Sdougm } 25263034Sdougm if (!dryrun && share == NULL) { 25274653Sdougm (void) printf(gettext( 25284653Sdougm "Could not add share: %s\n"), 25294653Sdougm sa_errorstr(ret)); 25303034Sdougm } else { 25315331Samw auth = check_authorizations(argv[optind], 25325331Samw flags); 25334653Sdougm if (!dryrun && ret == SA_OK) { 25345331Samw if (rsrcname != NULL) { 25355331Samw resource = sa_add_resource( 25365331Samw share, 25375331Samw rsrc, 25385331Samw SA_SHARE_PERMANENT, 25395331Samw &ret); 25404653Sdougm } 25414653Sdougm if (ret == SA_OK && 25424653Sdougm description != NULL) { 25435885Sdougm if (resource != NULL) 25445885Sdougm ret = 25455885Sdougm set_resource_desc( 25465885Sdougm resource, 25475885Sdougm description); 25485885Sdougm else 25495331Samw ret = 25505331Samw set_share_desc( 25515331Samw share, 25525331Samw description); 25534653Sdougm } 25544653Sdougm if (ret == SA_OK) { 25555331Samw /* now enable the share(s) */ 25565331Samw if (resource != NULL) { 25575331Samw ret = enable_share( 25585331Samw handle, 25595331Samw group, 25605331Samw resource, 25615331Samw 1); 25625331Samw } else { 25635331Samw ret = enable_share( 25645331Samw handle, 25655331Samw group, 25665331Samw share, 25675331Samw 1); 25685331Samw } 25694653Sdougm ret = sa_update_config(handle); 25704653Sdougm } 25714653Sdougm switch (ret) { 25724653Sdougm case SA_DUPLICATE_NAME: 25734653Sdougm (void) printf(gettext( 25744653Sdougm "Resource name in" 25755331Samw "use: %s\n"), 25765331Samw rsrcname); 25774653Sdougm break; 25784653Sdougm default: 25795331Samw (void) printf(gettext( 25805331Samw "Could not set " 25814653Sdougm "attribute: %s\n"), 25824653Sdougm sa_errorstr(ret)); 25834653Sdougm break; 25844653Sdougm case SA_OK: 25854653Sdougm break; 25864653Sdougm } 25875331Samw } else if (dryrun && ret == SA_OK && 25885331Samw !auth && verbose) { 25894653Sdougm (void) printf(gettext( 25904653Sdougm "Command would fail: %s\n"), 25914653Sdougm sa_errorstr(SA_NO_PERMISSION)); 25924653Sdougm ret = SA_NO_PERMISSION; 25933034Sdougm } 25943034Sdougm } 25955331Samw } else { 25965331Samw switch (ret) { 25975331Samw default: 25985331Samw (void) printf(gettext( 25995331Samw "Group \"%s\" not found\n"), argv[optind]); 26005331Samw ret = SA_NO_SUCH_GROUP; 26015331Samw break; 26025331Samw case SA_BAD_PATH: 26035331Samw case SA_DUPLICATE_NAME: 26045331Samw break; 26055331Samw } 26063034Sdougm } 26073034Sdougm } 26083034Sdougm return (ret); 26093034Sdougm } 26103034Sdougm 26113034Sdougm /* 26123034Sdougm * sa_moveshare(flags, argc, argv) 26133034Sdougm * 26143034Sdougm * implements move-share subcommand. 26153034Sdougm */ 26163034Sdougm 26173034Sdougm int 26183910Sdougm sa_moveshare(sa_handle_t handle, int flags, int argc, char *argv[]) 26193034Sdougm { 26203034Sdougm int verbose = 0; 26213034Sdougm int dryrun = 0; 26223034Sdougm int c; 26233034Sdougm int ret = SA_OK; 26243034Sdougm sa_group_t group; 26253034Sdougm sa_share_t share; 26265331Samw char *rsrcname = NULL; 26273034Sdougm char *sharepath = NULL; 26283034Sdougm int authsrc = 0, authdst = 0; 26295885Sdougm char dir[MAXPATHLEN]; 26303034Sdougm 26315331Samw while ((c = getopt(argc, argv, "?hvnr:s:")) != EOF) { 26324653Sdougm switch (c) { 26334653Sdougm case 'n': 26344653Sdougm dryrun++; 26354653Sdougm break; 26364653Sdougm case 'v': 26374653Sdougm verbose++; 26384653Sdougm break; 26395331Samw case 'r': 26405331Samw if (rsrcname != NULL) { 26415331Samw (void) printf(gettext( 26425331Samw "Moving multiple resource names not" 26435331Samw " supported\n")); 26445331Samw return (SA_SYNTAX_ERR); 26455331Samw } 26465331Samw rsrcname = optarg; 26475331Samw break; 26484653Sdougm case 's': 26494653Sdougm /* 26504653Sdougm * Remove share path from group. Currently limit 26514653Sdougm * to one share per command. 26524653Sdougm */ 26534653Sdougm if (sharepath != NULL) { 26544653Sdougm (void) printf(gettext("Moving multiple shares" 26555331Samw " not supported\n")); 26565331Samw return (SA_SYNTAX_ERR); 26574653Sdougm } 26584653Sdougm sharepath = optarg; 26594653Sdougm break; 26604653Sdougm default: 26614653Sdougm case 'h': 26624653Sdougm case '?': 26634653Sdougm (void) printf(gettext("usage: %s\n"), 26644653Sdougm sa_get_usage(USAGE_MOVE_SHARE)); 26654653Sdougm return (0); 26663034Sdougm } 26673034Sdougm } 26683034Sdougm 26693034Sdougm if (optind >= argc || sharepath == NULL) { 26705331Samw (void) printf(gettext("usage: %s\n"), 26715331Samw sa_get_usage(USAGE_MOVE_SHARE)); 26725331Samw if (dryrun || verbose || sharepath != NULL) { 26735331Samw (void) printf(gettext("\tgroup must be specified\n")); 26745331Samw ret = SA_NO_SUCH_GROUP; 26755331Samw } else { 26765331Samw if (sharepath == NULL) { 26775331Samw ret = SA_SYNTAX_ERR; 26784653Sdougm (void) printf(gettext( 26795331Samw "\tsharepath must be specified\n")); 26804653Sdougm } else { 26815331Samw ret = SA_OK; 26824653Sdougm } 26835331Samw } 26844653Sdougm } else { 26854653Sdougm sa_group_t parent; 26864653Sdougm char *zfsold; 26874653Sdougm char *zfsnew; 26884653Sdougm 26893034Sdougm if (sharepath == NULL) { 26904653Sdougm (void) printf(gettext( 26914653Sdougm "sharepath must be specified with the -s " 26924653Sdougm "option\n")); 26934653Sdougm return (SA_BAD_PATH); 26944653Sdougm } 26953910Sdougm group = sa_get_group(handle, argv[optind]); 26964653Sdougm if (group == NULL) { 26974653Sdougm (void) printf(gettext("Group \"%s\" not found\n"), 26984653Sdougm argv[optind]); 26994653Sdougm return (SA_NO_SUCH_GROUP); 27004653Sdougm } 27014653Sdougm share = sa_find_share(handle, sharepath); 27025885Sdougm /* 27035885Sdougm * If a share wasn't found, it may have been a symlink 27045885Sdougm * or has a trailing '/'. Try again after resolving 27055885Sdougm * with realpath(). 27065885Sdougm */ 27075885Sdougm if (share == NULL) { 27085885Sdougm if (realpath(sharepath, dir) == NULL) { 27095885Sdougm (void) printf(gettext("Path " 27105885Sdougm "is not valid: %s\n"), 27115885Sdougm sharepath); 27125885Sdougm return (SA_BAD_PATH); 27135885Sdougm } 27145885Sdougm sharepath = dir; 27155885Sdougm share = sa_find_share(handle, sharepath); 27165885Sdougm } 27174653Sdougm if (share == NULL) { 27183034Sdougm (void) printf(gettext("Share not found: %s\n"), 27194653Sdougm sharepath); 27204653Sdougm return (SA_NO_SUCH_PATH); 27214653Sdougm } 27225885Sdougm authdst = check_authorizations(argv[optind], flags); 27234653Sdougm 27244653Sdougm parent = sa_get_parent_group(share); 27254653Sdougm if (parent != NULL) { 27264653Sdougm char *pname; 27274653Sdougm pname = sa_get_group_attr(parent, "name"); 27284653Sdougm if (pname != NULL) { 27293034Sdougm authsrc = check_authorizations(pname, flags); 27303034Sdougm sa_free_attr_string(pname); 27314653Sdougm } 27324653Sdougm zfsold = sa_get_group_attr(parent, "zfs"); 27334653Sdougm zfsnew = sa_get_group_attr(group, "zfs"); 27344653Sdougm if ((zfsold != NULL && zfsnew == NULL) || 27354653Sdougm (zfsold == NULL && zfsnew != NULL)) { 27363034Sdougm ret = SA_NOT_ALLOWED; 27373034Sdougm } 27384653Sdougm if (zfsold != NULL) 27394653Sdougm sa_free_attr_string(zfsold); 27404653Sdougm if (zfsnew != NULL) 27414653Sdougm sa_free_attr_string(zfsnew); 27424653Sdougm } 27434653Sdougm 27444653Sdougm if (ret == SA_OK && parent != group && !dryrun) { 27454653Sdougm char *oldstate; 27464653Sdougm /* 27474653Sdougm * Note that the share may need to be 27485331Samw * "unshared" if the new group is disabled and 27495331Samw * the old was enabled or it may need to be 27505331Samw * share to update if the new group is 27515331Samw * enabled. We disable before the move and 27525331Samw * will have to enable after the move in order 27535331Samw * to cleanup entries for protocols that 27545331Samw * aren't in the new group. 27554653Sdougm */ 27564653Sdougm oldstate = sa_get_group_attr(parent, "state"); 27574653Sdougm 27584653Sdougm /* enable_share determines what to do */ 27595331Samw if (strcmp(oldstate, "enabled") == 0) 27603034Sdougm (void) sa_disable_share(share, NULL); 27615331Samw 27624653Sdougm if (oldstate != NULL) 27633034Sdougm sa_free_attr_string(oldstate); 27643034Sdougm } 27654653Sdougm 27665331Samw if (!dryrun && ret == SA_OK) 27675331Samw ret = sa_move_share(group, share); 27685331Samw 27695331Samw /* 27705331Samw * Reenable and update any config information. 27715331Samw */ 27725331Samw if (ret == SA_OK && parent != group && !dryrun) { 27735331Samw ret = sa_update_config(handle); 27745331Samw 27755331Samw (void) enable_share(handle, group, share, 1); 27765331Samw } 27775331Samw 27784653Sdougm if (ret != SA_OK) 27794653Sdougm (void) printf(gettext("Could not move share: %s\n"), 27804653Sdougm sa_errorstr(ret)); 27814653Sdougm 27824653Sdougm if (dryrun && ret == SA_OK && !(authsrc & authdst) && 27834653Sdougm verbose) { 27844653Sdougm (void) printf(gettext("Command would fail: %s\n"), 27854653Sdougm sa_errorstr(SA_NO_PERMISSION)); 27864653Sdougm } 27873034Sdougm } 27883034Sdougm return (ret); 27893034Sdougm } 27903034Sdougm 27913034Sdougm /* 27923034Sdougm * sa_removeshare(flags, argc, argv) 27933034Sdougm * 27943034Sdougm * implements remove-share subcommand. 27953034Sdougm */ 27963034Sdougm 27973034Sdougm int 27983910Sdougm sa_removeshare(sa_handle_t handle, int flags, int argc, char *argv[]) 27993034Sdougm { 28003034Sdougm int verbose = 0; 28013034Sdougm int dryrun = 0; 28023034Sdougm int force = 0; 28033034Sdougm int c; 28043034Sdougm int ret = SA_OK; 28053034Sdougm sa_group_t group; 28065331Samw sa_resource_t resource = NULL; 28075331Samw sa_share_t share = NULL; 28085331Samw char *rsrcname = NULL; 28093034Sdougm char *sharepath = NULL; 28103034Sdougm char dir[MAXPATHLEN]; 28113034Sdougm int auth; 28123034Sdougm 28135331Samw while ((c = getopt(argc, argv, "?hfnr:s:v")) != EOF) { 28144653Sdougm switch (c) { 28154653Sdougm case 'n': 28164653Sdougm dryrun++; 28174653Sdougm break; 28184653Sdougm case 'v': 28194653Sdougm verbose++; 28204653Sdougm break; 28214653Sdougm case 'f': 28224653Sdougm force++; 28234653Sdougm break; 28244653Sdougm case 's': 28254653Sdougm /* 28264653Sdougm * Remove share path from group. Currently limit 28274653Sdougm * to one share per command. 28284653Sdougm */ 28294653Sdougm if (sharepath != NULL) { 28304653Sdougm (void) printf(gettext( 28314653Sdougm "Removing multiple shares not " 28323034Sdougm "supported\n")); 28334653Sdougm return (SA_SYNTAX_ERR); 28344653Sdougm } 28354653Sdougm sharepath = optarg; 28364653Sdougm break; 28375331Samw case 'r': 28385331Samw /* 28395331Samw * Remove share from group if last resource or remove 28405331Samw * resource from share if multiple resources. 28415331Samw */ 28425331Samw if (rsrcname != NULL) { 28435331Samw (void) printf(gettext( 28445331Samw "Removing multiple resource names not " 28455331Samw "supported\n")); 28465331Samw return (SA_SYNTAX_ERR); 28475331Samw } 28485331Samw rsrcname = optarg; 28495331Samw break; 28504653Sdougm default: 28514653Sdougm case 'h': 28524653Sdougm case '?': 28534653Sdougm (void) printf(gettext("usage: %s\n"), 28544653Sdougm sa_get_usage(USAGE_REMOVE_SHARE)); 28554653Sdougm return (0); 28563034Sdougm } 28573034Sdougm } 28583034Sdougm 28595331Samw if (optind >= argc || (rsrcname == NULL && sharepath == NULL)) { 28605331Samw if (sharepath == NULL && rsrcname == NULL) { 28613034Sdougm (void) printf(gettext("usage: %s\n"), 28624653Sdougm sa_get_usage(USAGE_REMOVE_SHARE)); 28635331Samw (void) printf(gettext("\t-s sharepath or -r resource" 28645331Samw " must be specified\n")); 28654653Sdougm ret = SA_BAD_PATH; 28664653Sdougm } else { 28674653Sdougm ret = SA_OK; 28684653Sdougm } 28693034Sdougm } 28704653Sdougm if (ret != SA_OK) { 28714653Sdougm return (ret); 28724653Sdougm } 28734653Sdougm 28744653Sdougm if (optind < argc) { 28753034Sdougm if ((optind + 1) < argc) { 28764653Sdougm (void) printf(gettext("Extraneous group(s) at end of " 28774653Sdougm "command\n")); 28784653Sdougm ret = SA_SYNTAX_ERR; 28793034Sdougm } else { 28804653Sdougm group = sa_get_group(handle, argv[optind]); 28814653Sdougm if (group == NULL) { 28824653Sdougm (void) printf(gettext( 28834653Sdougm "Group \"%s\" not found\n"), argv[optind]); 28844653Sdougm ret = SA_NO_SUCH_GROUP; 28854653Sdougm } 28863034Sdougm } 28874653Sdougm } else { 28883034Sdougm group = NULL; 28894653Sdougm } 28904653Sdougm 28915331Samw if (rsrcname != NULL) { 28925331Samw resource = sa_find_resource(handle, rsrcname); 28935331Samw if (resource == NULL) { 28945331Samw ret = SA_NO_SUCH_RESOURCE; 28955331Samw (void) printf(gettext( 28965331Samw "Resource name not found for share: %s\n"), 28975331Samw rsrcname); 28985331Samw } 28995331Samw } 29005331Samw 29014653Sdougm /* 29024653Sdougm * Lookup the path in the internal configuration. Care 29034653Sdougm * must be taken to handle the case where the 29044653Sdougm * underlying path has been removed since we need to 29054653Sdougm * be able to deal with that as well. 29064653Sdougm */ 29074653Sdougm if (ret == SA_OK) { 29085331Samw if (sharepath != NULL) { 29095331Samw if (group != NULL) 29105331Samw share = sa_get_share(group, sharepath); 29115331Samw else 29125331Samw share = sa_find_share(handle, sharepath); 29135331Samw } 29145331Samw 29155331Samw if (resource != NULL) { 29165331Samw sa_share_t rsrcshare; 29175331Samw rsrcshare = sa_get_resource_parent(resource); 29185331Samw if (share == NULL) 29195331Samw share = rsrcshare; 29205331Samw else if (share != rsrcshare) { 29215331Samw ret = SA_NO_SUCH_RESOURCE; 29225331Samw (void) printf(gettext( 29235331Samw "Bad resource name for share: %s\n"), 29245331Samw rsrcname); 29255331Samw share = NULL; 29265331Samw } 29275331Samw } 29285331Samw 29293663Sdougm /* 29303663Sdougm * If we didn't find the share with the provided path, 29313663Sdougm * it may be a symlink so attempt to resolve it using 29323663Sdougm * realpath and try again. Realpath will resolve any 29333663Sdougm * symlinks and place them in "dir". Note that 29343663Sdougm * sharepath is only used for the lookup the first 29353663Sdougm * time and later for error messages. dir will be used 29363663Sdougm * on the second attempt. Once a share is found, all 29373663Sdougm * operations are based off of the share variable. 29383663Sdougm */ 29393663Sdougm if (share == NULL) { 29404653Sdougm if (realpath(sharepath, dir) == NULL) { 29414653Sdougm ret = SA_BAD_PATH; 29424653Sdougm (void) printf(gettext( 29434653Sdougm "Path is not valid: %s\n"), sharepath); 29444653Sdougm } else { 29454653Sdougm if (group != NULL) 29464653Sdougm share = sa_get_share(group, dir); 29474653Sdougm else 29484653Sdougm share = sa_find_share(handle, dir); 29494653Sdougm } 29503663Sdougm } 29514653Sdougm } 29524653Sdougm 29534653Sdougm /* 29544653Sdougm * If there hasn't been an error, there was likely a 29554653Sdougm * path found. If not, give the appropriate error 29564653Sdougm * message and set the return error. If it was found, 29574653Sdougm * then disable the share and then remove it from the 29584653Sdougm * configuration. 29594653Sdougm */ 29604653Sdougm if (ret != SA_OK) { 29614653Sdougm return (ret); 29624653Sdougm } 29634653Sdougm if (share == NULL) { 29644653Sdougm if (group != NULL) 29653034Sdougm (void) printf(gettext("Share not found in group %s:" 29664653Sdougm " %s\n"), argv[optind], sharepath); 29674653Sdougm else 29683034Sdougm (void) printf(gettext("Share not found: %s\n"), 29694653Sdougm sharepath); 29705331Samw ret = SA_NO_SUCH_PATH; 29714653Sdougm } else { 29724653Sdougm if (group == NULL) 29733034Sdougm group = sa_get_parent_group(share); 29744653Sdougm if (!dryrun) { 29753034Sdougm if (ret == SA_OK) { 29765331Samw if (resource != NULL) 29775331Samw ret = sa_disable_resource(resource, 29785331Samw NULL); 29795331Samw else 29805331Samw ret = sa_disable_share(share, NULL); 29813034Sdougm /* 29824653Sdougm * We don't care if it fails since it 29833663Sdougm * could be disabled already. Some 29843663Sdougm * unexpected errors could occur that 29853663Sdougm * prevent removal, so also check for 29863663Sdougm * force being set. 29873034Sdougm */ 29885331Samw if ((ret == SA_OK || ret == SA_NO_SUCH_PATH || 29895331Samw ret == SA_NOT_SUPPORTED || 29905331Samw ret == SA_SYSTEM_ERR || force) && 29915331Samw resource == NULL) 29925331Samw ret = sa_remove_share(share); 29935331Samw 29945331Samw if ((ret == SA_OK || ret == SA_NO_SUCH_PATH || 29954653Sdougm ret == SA_NOT_SUPPORTED || 29965331Samw ret == SA_SYSTEM_ERR || force) && 29975331Samw resource != NULL) { 29985331Samw ret = sa_remove_resource(resource); 29995331Samw if (ret == SA_OK) { 30005331Samw /* 30015331Samw * If this was the 30025331Samw * last one, remove 30035331Samw * the share as well. 30045331Samw */ 30055331Samw resource = 30065331Samw sa_get_share_resource( 30075331Samw share, NULL); 30085331Samw if (resource == NULL) 30095331Samw ret = sa_remove_share( 30105331Samw share); 30115331Samw } 30124653Sdougm } 30134653Sdougm if (ret == SA_OK) 30144653Sdougm ret = sa_update_config(handle); 30153034Sdougm } 30164653Sdougm if (ret != SA_OK) 30175331Samw (void) printf(gettext("Could not remove share:" 30185331Samw " %s\n"), sa_errorstr(ret)); 30194653Sdougm } else if (ret == SA_OK) { 30203034Sdougm char *pname; 30213034Sdougm pname = sa_get_group_attr(group, "name"); 30223034Sdougm if (pname != NULL) { 30234653Sdougm auth = check_authorizations(pname, flags); 30244653Sdougm sa_free_attr_string(pname); 30253034Sdougm } 30263034Sdougm if (!auth && verbose) { 30274653Sdougm (void) printf(gettext( 30284653Sdougm "Command would fail: %s\n"), 30294653Sdougm sa_errorstr(SA_NO_PERMISSION)); 30303034Sdougm } 30313034Sdougm } 30323034Sdougm } 30333034Sdougm return (ret); 30343034Sdougm } 30353034Sdougm 30363034Sdougm /* 30373034Sdougm * sa_set_share(flags, argc, argv) 30383034Sdougm * 30393034Sdougm * implements set-share subcommand. 30403034Sdougm */ 30413034Sdougm 30423034Sdougm int 30433910Sdougm sa_set_share(sa_handle_t handle, int flags, int argc, char *argv[]) 30443034Sdougm { 30453034Sdougm int dryrun = 0; 30463034Sdougm int c; 30473034Sdougm int ret = SA_OK; 30483034Sdougm sa_group_t group, sharegroup; 30495772Sas200622 sa_share_t share = NULL; 30505331Samw sa_resource_t resource = NULL; 30513034Sdougm char *sharepath = NULL; 30523034Sdougm char *description = NULL; 30535331Samw char *rsrcname = NULL; 30545331Samw char *rsrc = NULL; 30555331Samw char *newname = NULL; 30565331Samw char *newrsrc; 30575331Samw char *groupname = NULL; 30583034Sdougm int auth; 30593034Sdougm int verbose = 0; 30603034Sdougm 30613034Sdougm while ((c = getopt(argc, argv, "?hnd:r:s:")) != EOF) { 30624653Sdougm switch (c) { 30634653Sdougm case 'n': 30644653Sdougm dryrun++; 30654653Sdougm break; 30664653Sdougm case 'd': 30674653Sdougm description = optarg; 30684653Sdougm break; 30694653Sdougm case 'v': 30704653Sdougm verbose++; 30714653Sdougm break; 30725331Samw case 'r': 30735331Samw /* 30745331Samw * Update share by resource name 30755331Samw */ 30765331Samw if (rsrcname != NULL) { 30775331Samw (void) printf(gettext( 30785331Samw "Updating multiple resource names not " 30795331Samw "supported\n")); 30805331Samw return (SA_SYNTAX_ERR); 30815331Samw } 30825331Samw rsrcname = optarg; 30835331Samw break; 30844653Sdougm case 's': 30854653Sdougm /* 30864653Sdougm * Save share path into group. Currently limit 30874653Sdougm * to one share per command. 30884653Sdougm */ 30894653Sdougm if (sharepath != NULL) { 30904653Sdougm (void) printf(gettext( 30914653Sdougm "Updating multiple shares not " 30923034Sdougm "supported\n")); 30935331Samw return (SA_SYNTAX_ERR); 30944653Sdougm } 30954653Sdougm sharepath = optarg; 30964653Sdougm break; 30974653Sdougm default: 30984653Sdougm case 'h': 30994653Sdougm case '?': 31004653Sdougm (void) printf(gettext("usage: %s\n"), 31014653Sdougm sa_get_usage(USAGE_SET_SHARE)); 31024653Sdougm return (SA_OK); 31033034Sdougm } 31043034Sdougm } 31054653Sdougm 31065331Samw if (optind >= argc && sharepath == NULL && rsrcname == NULL) { 31074653Sdougm if (sharepath == NULL) { 31084653Sdougm (void) printf(gettext("usage: %s\n"), 31094653Sdougm sa_get_usage(USAGE_SET_SHARE)); 31104653Sdougm (void) printf(gettext("\tgroup must be specified\n")); 31114653Sdougm ret = SA_BAD_PATH; 31124653Sdougm } else { 31134653Sdougm ret = SA_OK; 31144653Sdougm } 31153034Sdougm } 31163034Sdougm if ((optind + 1) < argc) { 31174653Sdougm (void) printf(gettext("usage: %s\n"), 31184653Sdougm sa_get_usage(USAGE_SET_SHARE)); 31194653Sdougm (void) printf(gettext("\tExtraneous group(s) at end\n")); 31204653Sdougm ret = SA_SYNTAX_ERR; 31213034Sdougm } 31224653Sdougm 31235331Samw /* 31245331Samw * Must have at least one of sharepath and rsrcrname. 31255331Samw * It is a syntax error to be missing both. 31265331Samw */ 31275331Samw if (sharepath == NULL && rsrcname == NULL) { 31285331Samw (void) printf(gettext("usage: %s\n"), 31295331Samw sa_get_usage(USAGE_SET_SHARE)); 31305331Samw ret = SA_SYNTAX_ERR; 31315331Samw } 31325331Samw 31334653Sdougm if (ret != SA_OK) 31344653Sdougm return (ret); 31354653Sdougm 31364653Sdougm if (optind < argc) { 31373034Sdougm groupname = argv[optind]; 31383910Sdougm group = sa_get_group(handle, groupname); 31394653Sdougm } else { 31403034Sdougm group = NULL; 31413034Sdougm groupname = NULL; 31424653Sdougm } 31435331Samw if (rsrcname != NULL) { 31445331Samw /* 31455331Samw * If rsrcname exists, split rename syntax and then 31465331Samw * convert to utf 8 if no errors. 31475331Samw */ 31485331Samw newname = strchr(rsrcname, '='); 31495331Samw if (newname != NULL) { 31505331Samw *newname++ = '\0'; 31515331Samw } 31525331Samw if (!validresource(rsrcname)) { 31535331Samw ret = SA_INVALID_NAME; 31545331Samw (void) printf(gettext("Invalid resource name: " 31555331Samw "\"%s\"\n"), rsrcname); 31565331Samw } else { 31575331Samw rsrc = conv_to_utf8(rsrcname); 31585331Samw } 31595331Samw if (newname != NULL) { 31605331Samw if (!validresource(newname)) { 31615331Samw ret = SA_INVALID_NAME; 31625331Samw (void) printf(gettext("Invalid resource name: " 31635331Samw "%s\n"), newname); 31645331Samw } else { 31655331Samw newrsrc = conv_to_utf8(newname); 31665331Samw } 31675331Samw } 31685331Samw } 31695331Samw 31705331Samw if (ret != SA_OK) { 31715331Samw if (rsrcname != NULL && rsrcname != rsrc) 31725331Samw sa_free_attr_string(rsrc); 31735331Samw if (newname != NULL && newname != newrsrc) 31745331Samw sa_free_attr_string(newrsrc); 31755331Samw return (ret); 31765331Samw } 31775331Samw 31785331Samw if (sharepath != NULL) { 31795331Samw share = sa_find_share(handle, sharepath); 31805331Samw } else if (rsrcname != NULL) { 31815331Samw resource = sa_find_resource(handle, rsrc); 31825772Sas200622 if (resource != NULL) 31835331Samw share = sa_get_resource_parent(resource); 31845772Sas200622 else 31855772Sas200622 ret = SA_NO_SUCH_RESOURCE; 31865331Samw } 31875331Samw if (share != NULL) { 31885331Samw sharegroup = sa_get_parent_group(share); 31895331Samw if (group != NULL && group != sharegroup) { 31905331Samw (void) printf(gettext("Group \"%s\" does not contain " 31915331Samw "share %s\n"), 31925331Samw argv[optind], sharepath); 31935331Samw ret = SA_BAD_PATH; 31945331Samw } else { 31955331Samw int delgroupname = 0; 31965331Samw if (groupname == NULL) { 31975331Samw groupname = sa_get_group_attr(sharegroup, 31985331Samw "name"); 31995331Samw delgroupname = 1; 32005331Samw } 32015331Samw if (groupname != NULL) { 32025331Samw auth = check_authorizations(groupname, flags); 32035331Samw if (delgroupname) { 32045331Samw sa_free_attr_string(groupname); 32055331Samw groupname = NULL; 32065331Samw } 32075331Samw } else { 32085331Samw ret = SA_NO_MEMORY; 32095331Samw } 32105331Samw if (rsrcname != NULL) { 32115331Samw resource = sa_find_resource(handle, rsrc); 32125331Samw if (!dryrun) { 32135331Samw if (newname != NULL && 32145331Samw resource != NULL) 32155331Samw ret = sa_rename_resource( 32165331Samw resource, newrsrc); 32175331Samw else if (newname != NULL) 32185331Samw ret = SA_NO_SUCH_RESOURCE; 32195331Samw if (newname != NULL && 32205331Samw newname != newrsrc) 32215331Samw sa_free_attr_string(newrsrc); 32225331Samw } 32235331Samw if (rsrc != rsrcname) 32245331Samw sa_free_attr_string(rsrc); 32255331Samw } 32265331Samw 32275331Samw /* 32285331Samw * If the user has set a description, it will be 32295331Samw * on the resource if -r was used otherwise it 32305331Samw * must be on the share. 32315331Samw */ 3232*5967Scp160787 if (!dryrun && ret == SA_OK && description != NULL) { 3233*5967Scp160787 char *desc; 3234*5967Scp160787 desc = conv_to_utf8(description); 32355331Samw if (resource != NULL) 3236*5967Scp160787 ret = sa_set_resource_description( 3237*5967Scp160787 resource, desc); 32385331Samw else 3239*5967Scp160787 ret = sa_set_share_description(share, 3240*5967Scp160787 desc); 3241*5967Scp160787 if (desc != description) 3242*5967Scp160787 sa_free_share_description(desc); 32435331Samw } 32445331Samw } 32455331Samw if (!dryrun && ret == SA_OK) { 32465331Samw if (resource != NULL) 32475331Samw (void) sa_enable_resource(resource, NULL); 32485331Samw ret = sa_update_config(handle); 32495331Samw } 32505331Samw switch (ret) { 32515331Samw case SA_DUPLICATE_NAME: 32525331Samw (void) printf(gettext("Resource name in use: %s\n"), 32535331Samw rsrcname); 32545331Samw break; 32555331Samw default: 32565331Samw (void) printf(gettext("Could not set: %s\n"), 32575331Samw sa_errorstr(ret)); 32585331Samw break; 32595331Samw case SA_OK: 32605331Samw if (dryrun && !auth && verbose) { 32615331Samw (void) printf(gettext( 32625331Samw "Command would fail: %s\n"), 32635331Samw sa_errorstr(SA_NO_PERMISSION)); 32645331Samw } 32655331Samw break; 32665331Samw } 32675331Samw } else { 32685772Sas200622 switch (ret) { 32695772Sas200622 case SA_NO_SUCH_RESOURCE: 32705772Sas200622 (void) printf(gettext("Resource \"%s\" not found\n"), 32715772Sas200622 rsrcname); 32725772Sas200622 break; 32735772Sas200622 default: 32745772Sas200622 if (sharepath != NULL) { 32755772Sas200622 (void) printf( 32765772Sas200622 gettext("Share path \"%s\" not found\n"), 32775772Sas200622 sharepath); 32785772Sas200622 ret = SA_NO_SUCH_PATH; 32795772Sas200622 } else { 32805772Sas200622 (void) printf(gettext("Set failed: %s\n"), 32815772Sas200622 sa_errorstr(ret)); 32825772Sas200622 } 32835772Sas200622 } 32843034Sdougm } 32854653Sdougm 32863034Sdougm return (ret); 32873034Sdougm } 32883034Sdougm 32893034Sdougm /* 32903034Sdougm * add_security(group, sectype, optlist, proto, *err) 32913034Sdougm * 32923034Sdougm * Helper function to add a security option (named optionset) to the 32933034Sdougm * group. 32943034Sdougm */ 32953034Sdougm 32963034Sdougm static int 32973034Sdougm add_security(sa_group_t group, char *sectype, 32985331Samw struct options *optlist, char *proto, int *err) 32993034Sdougm { 33003034Sdougm sa_security_t security; 33013034Sdougm int ret = SA_OK; 33023034Sdougm int result = 0; 33033034Sdougm 33043034Sdougm sectype = sa_proto_space_alias(proto, sectype); 33053034Sdougm security = sa_get_security(group, sectype, proto); 33064653Sdougm if (security == NULL) 33074653Sdougm security = sa_create_security(group, sectype, proto); 33084653Sdougm 33093034Sdougm if (sectype != NULL) 33104653Sdougm sa_free_attr_string(sectype); 33114653Sdougm 33124653Sdougm if (security == NULL) 33134653Sdougm return (ret); 33144653Sdougm 33154653Sdougm while (optlist != NULL) { 33163034Sdougm sa_property_t prop; 33173034Sdougm prop = sa_get_property(security, optlist->optname); 33183034Sdougm if (prop == NULL) { 33193034Sdougm /* 33204653Sdougm * Add the property, but only if it is 33213034Sdougm * a non-NULL or non-zero length value 33223034Sdougm */ 33234653Sdougm if (optlist->optvalue != NULL) { 33244653Sdougm prop = sa_create_property(optlist->optname, 33254653Sdougm optlist->optvalue); 33264653Sdougm if (prop != NULL) { 33275331Samw ret = sa_valid_property(security, 33285331Samw proto, prop); 33294653Sdougm if (ret != SA_OK) { 33304653Sdougm (void) sa_remove_property(prop); 33314653Sdougm (void) printf(gettext( 33324653Sdougm "Could not add " 33334653Sdougm "property %s: %s\n"), 33344653Sdougm optlist->optname, 33354653Sdougm sa_errorstr(ret)); 33364653Sdougm } 33374653Sdougm if (ret == SA_OK) { 33384653Sdougm ret = sa_add_property(security, 33394653Sdougm prop); 33404653Sdougm if (ret != SA_OK) { 33414653Sdougm (void) printf(gettext( 33424653Sdougm "Could not add " 33435331Samw "property (%s=%s):" 33445331Samw " %s\n"), 33454653Sdougm optlist->optname, 33464653Sdougm optlist->optvalue, 33474653Sdougm sa_errorstr(ret)); 33484653Sdougm } else { 33494653Sdougm result = 1; 33504653Sdougm } 33514653Sdougm } 33523034Sdougm } 33533034Sdougm } 33543034Sdougm } else { 33554653Sdougm ret = sa_update_property(prop, optlist->optvalue); 33564653Sdougm result = 1; /* should check if really changed */ 33573034Sdougm } 33583034Sdougm optlist = optlist->next; 33594653Sdougm } 33604653Sdougm /* 33614653Sdougm * When done, properties may have all been removed but 33624653Sdougm * we need to keep the security type itself until 33634653Sdougm * explicitly removed. 33644653Sdougm */ 33654653Sdougm if (result) 33663034Sdougm ret = sa_commit_properties(security, 0); 33673034Sdougm *err = ret; 33683034Sdougm return (result); 33693034Sdougm } 33703034Sdougm 33713034Sdougm /* 33725089Sdougm * zfscheck(group, share) 33735089Sdougm * 33745089Sdougm * For the special case where a share was provided, make sure it is a 33755089Sdougm * compatible path for a ZFS property change. The only path 33765089Sdougm * acceptable is the path that defines the zfs sub-group (dataset with 33775089Sdougm * the sharenfs property set) and not one of the paths that inherited 33785089Sdougm * the NFS properties. Returns SA_OK if it is usable and 33795089Sdougm * SA_NOT_ALLOWED if it isn't. 33805089Sdougm * 33815089Sdougm * If group is not a ZFS group/subgroup, we assume OK since the check 33825089Sdougm * on return will catch errors for those cases. What we are looking 33835089Sdougm * for here is that the group is ZFS and the share is not the defining 33845089Sdougm * share. All else is SA_OK. 33855089Sdougm */ 33865089Sdougm 33875089Sdougm static int 33885089Sdougm zfscheck(sa_group_t group, sa_share_t share) 33895089Sdougm { 33905089Sdougm int ret = SA_OK; 33915089Sdougm char *attr; 33925089Sdougm 33935089Sdougm if (sa_group_is_zfs(group)) { 33945089Sdougm /* 33955089Sdougm * The group is a ZFS group. Does the share represent 33965089Sdougm * the dataset that defined the group? It is only OK 33975089Sdougm * if the attribute "subgroup" exists on the share and 33985089Sdougm * has a value of "true". 33995089Sdougm */ 34005089Sdougm 34015089Sdougm ret = SA_NOT_ALLOWED; 34025089Sdougm attr = sa_get_share_attr(share, "subgroup"); 34035089Sdougm if (attr != NULL) { 34045089Sdougm if (strcmp(attr, "true") == 0) 34055089Sdougm ret = SA_OK; 34065089Sdougm sa_free_attr_string(attr); 34075089Sdougm } 34085089Sdougm } 34095089Sdougm return (ret); 34105089Sdougm } 34115089Sdougm 34125089Sdougm /* 34135331Samw * basic_set(groupname, optlist, protocol, sharepath, rsrcname, dryrun) 34143034Sdougm * 34153034Sdougm * This function implements "set" when a name space (-S) is not 34163034Sdougm * specified. It is a basic set. Options and other CLI parsing has 34173034Sdougm * already been done. 34185331Samw * 34195331Samw * "rsrcname" is a "resource name". If it is non-NULL, it must match 34205331Samw * the sharepath if present or group if present, otherwise it is used 34215331Samw * to set options. 34225331Samw * 34235331Samw * Resource names may take options if the protocol supports it. If the 34245331Samw * protocol doesn't support resource level options, rsrcname is just 34255331Samw * an alias for the share. 34263034Sdougm */ 34273034Sdougm 34283034Sdougm static int 34293910Sdougm basic_set(sa_handle_t handle, char *groupname, struct options *optlist, 34305331Samw char *protocol, char *sharepath, char *rsrcname, int dryrun) 34313034Sdougm { 34323034Sdougm sa_group_t group; 34333034Sdougm int ret = SA_OK; 34343034Sdougm int change = 0; 34353034Sdougm struct list *worklist = NULL; 34363034Sdougm 34373910Sdougm group = sa_get_group(handle, groupname); 34383034Sdougm if (group != NULL) { 34394653Sdougm sa_share_t share = NULL; 34405331Samw sa_resource_t resource = NULL; 34415331Samw 34425331Samw /* 34435331Samw * If there is a sharepath, make sure it belongs to 34445331Samw * the group. 34455331Samw */ 34464653Sdougm if (sharepath != NULL) { 34474653Sdougm share = sa_get_share(group, sharepath); 34484653Sdougm if (share == NULL) { 34494653Sdougm (void) printf(gettext( 34504653Sdougm "Share does not exist in group %s\n"), 34514653Sdougm groupname, sharepath); 34524653Sdougm ret = SA_NO_SUCH_PATH; 34535089Sdougm } else { 34545089Sdougm /* if ZFS and OK, then only group */ 34555089Sdougm ret = zfscheck(group, share); 34565089Sdougm if (ret == SA_OK && 34575089Sdougm sa_group_is_zfs(group)) 34585089Sdougm share = NULL; 34595089Sdougm if (ret == SA_NOT_ALLOWED) 34605089Sdougm (void) printf(gettext( 34615089Sdougm "Properties on ZFS group shares " 34625089Sdougm "not supported: %s\n"), sharepath); 34634653Sdougm } 34643034Sdougm } 34655331Samw 34665331Samw /* 34675331Samw * If a resource name exists, make sure it belongs to 34685331Samw * the share if present else it belongs to the 34695331Samw * group. Also check the protocol to see if it 34705331Samw * supports resource level properties or not. If not, 34715331Samw * use share only. 34725331Samw */ 34735331Samw if (rsrcname != NULL) { 34745331Samw if (share != NULL) { 34755331Samw resource = sa_get_share_resource(share, 34765331Samw rsrcname); 34775331Samw if (resource == NULL) 34785331Samw ret = SA_NO_SUCH_RESOURCE; 34795331Samw } else { 34805331Samw resource = sa_get_resource(group, rsrcname); 34815331Samw if (resource != NULL) 34825331Samw share = sa_get_resource_parent( 34835331Samw resource); 34845331Samw else 34855331Samw ret = SA_NO_SUCH_RESOURCE; 34865331Samw } 34875331Samw if (ret == SA_OK && resource != NULL) { 34885331Samw uint64_t features; 34895331Samw /* 34905331Samw * Check to see if the resource can take 34915331Samw * properties. If so, stick the resource into 34925331Samw * "share" so it will all just work. 34935331Samw */ 34945331Samw features = sa_proto_get_featureset(protocol); 34955331Samw if (features & SA_FEATURE_RESOURCE) 34965331Samw share = (sa_share_t)resource; 34975331Samw } 34985331Samw } 34995331Samw 35004653Sdougm if (ret == SA_OK) { 35014653Sdougm /* group must exist */ 35024653Sdougm ret = valid_options(optlist, protocol, 35034653Sdougm share == NULL ? group : share, NULL); 35044653Sdougm if (ret == SA_OK && !dryrun) { 35054653Sdougm if (share != NULL) 35064653Sdougm change |= add_optionset(share, optlist, 35074653Sdougm protocol, &ret); 35084653Sdougm else 35094653Sdougm change |= add_optionset(group, optlist, 35104653Sdougm protocol, &ret); 35114653Sdougm if (ret == SA_OK && change) 35124653Sdougm worklist = add_list(worklist, group, 35135331Samw share, protocol); 35144653Sdougm } 35153034Sdougm } 35164653Sdougm free_opt(optlist); 35173034Sdougm } else { 35183034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 35193034Sdougm ret = SA_NO_SUCH_GROUP; 35203034Sdougm } 35213034Sdougm /* 35223034Sdougm * we have a group and potentially legal additions 35233034Sdougm */ 35243034Sdougm 35254653Sdougm /* 35264653Sdougm * Commit to configuration if not a dryrunp and properties 35274653Sdougm * have changed. 35284653Sdougm */ 35294653Sdougm if (!dryrun && ret == SA_OK && change && worklist != NULL) 35303034Sdougm /* properties changed, so update all shares */ 35315331Samw (void) enable_all_groups(handle, worklist, 0, 0, protocol, 35325331Samw B_TRUE); 35334653Sdougm 35343034Sdougm if (worklist != NULL) 35354653Sdougm free_list(worklist); 35363034Sdougm return (ret); 35373034Sdougm } 35383034Sdougm 35393034Sdougm /* 35403034Sdougm * space_set(groupname, optlist, protocol, sharepath, dryrun) 35413034Sdougm * 35423034Sdougm * This function implements "set" when a name space (-S) is 35433034Sdougm * specified. It is a namespace set. Options and other CLI parsing has 35443034Sdougm * already been done. 35453034Sdougm */ 35463034Sdougm 35473034Sdougm static int 35483910Sdougm space_set(sa_handle_t handle, char *groupname, struct options *optlist, 35495331Samw char *protocol, char *sharepath, int dryrun, char *sectype) 35503034Sdougm { 35513034Sdougm sa_group_t group; 35523034Sdougm int ret = SA_OK; 35533034Sdougm int change = 0; 35543034Sdougm struct list *worklist = NULL; 35553034Sdougm 35563034Sdougm /* 35573034Sdougm * make sure protcol and sectype are valid 35583034Sdougm */ 35593034Sdougm 35603034Sdougm if (sa_proto_valid_space(protocol, sectype) == 0) { 35614653Sdougm (void) printf(gettext("Option space \"%s\" not valid " 35624653Sdougm "for protocol.\n"), sectype); 35634653Sdougm return (SA_INVALID_SECURITY); 35643034Sdougm } 35653034Sdougm 35663910Sdougm group = sa_get_group(handle, groupname); 35673034Sdougm if (group != NULL) { 35684653Sdougm sa_share_t share = NULL; 35694653Sdougm if (sharepath != NULL) { 35704653Sdougm share = sa_get_share(group, sharepath); 35714653Sdougm if (share == NULL) { 35724653Sdougm (void) printf(gettext( 35734653Sdougm "Share does not exist in group %s\n"), 35744653Sdougm groupname, sharepath); 35754653Sdougm ret = SA_NO_SUCH_PATH; 35765089Sdougm } else { 35775089Sdougm /* if ZFS and OK, then only group */ 35785089Sdougm ret = zfscheck(group, share); 35795089Sdougm if (ret == SA_OK && 35805089Sdougm sa_group_is_zfs(group)) 35815089Sdougm share = NULL; 35825089Sdougm if (ret == SA_NOT_ALLOWED) 35835089Sdougm (void) printf(gettext( 35845089Sdougm "Properties on ZFS group shares " 35855089Sdougm "not supported: %s\n"), sharepath); 35864653Sdougm } 35873034Sdougm } 35884653Sdougm if (ret == SA_OK) { 35894653Sdougm /* group must exist */ 35904653Sdougm ret = valid_options(optlist, protocol, 35914653Sdougm share == NULL ? group : share, sectype); 35924653Sdougm if (ret == SA_OK && !dryrun) { 35934653Sdougm if (share != NULL) 35944653Sdougm change = add_security(share, sectype, 35954653Sdougm optlist, protocol, &ret); 35964653Sdougm else 35974653Sdougm change = add_security(group, sectype, 35984653Sdougm optlist, protocol, &ret); 35994653Sdougm if (ret != SA_OK) 36004653Sdougm (void) printf(gettext( 36014653Sdougm "Could not set property: %s\n"), 36024653Sdougm sa_errorstr(ret)); 36034653Sdougm } 36044653Sdougm if (ret == SA_OK && change) 36055331Samw worklist = add_list(worklist, group, share, 36065331Samw protocol); 36073034Sdougm } 36084653Sdougm free_opt(optlist); 36093034Sdougm } else { 36103034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 36113034Sdougm ret = SA_NO_SUCH_GROUP; 36123034Sdougm } 36135331Samw 36143034Sdougm /* 36155331Samw * We have a group and potentially legal additions. 36163034Sdougm */ 36173034Sdougm 36184653Sdougm /* Commit to configuration if not a dryrun */ 36193034Sdougm if (!dryrun && ret == 0) { 36204653Sdougm if (change && worklist != NULL) { 36214653Sdougm /* properties changed, so update all shares */ 36224653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 36235331Samw protocol, B_TRUE); 36244653Sdougm } 36254653Sdougm ret = sa_update_config(handle); 36263034Sdougm } 36273034Sdougm if (worklist != NULL) 36284653Sdougm free_list(worklist); 36293034Sdougm return (ret); 36303034Sdougm } 36313034Sdougm 36323034Sdougm /* 36333034Sdougm * sa_set(flags, argc, argv) 36343034Sdougm * 36353034Sdougm * Implements the set subcommand. It keys off of -S to determine which 36363034Sdougm * set of operations to actually do. 36373034Sdougm */ 36383034Sdougm 36393034Sdougm int 36403910Sdougm sa_set(sa_handle_t handle, int flags, int argc, char *argv[]) 36413034Sdougm { 36423034Sdougm char *groupname; 36433034Sdougm int verbose = 0; 36443034Sdougm int dryrun = 0; 36453034Sdougm int c; 36463034Sdougm char *protocol = NULL; 36473034Sdougm int ret = SA_OK; 36483034Sdougm struct options *optlist = NULL; 36495331Samw char *rsrcname = NULL; 36503034Sdougm char *sharepath = NULL; 36513034Sdougm char *optset = NULL; 36523034Sdougm int auth; 36533034Sdougm 36545331Samw while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) { 36554653Sdougm switch (c) { 36564653Sdougm case 'v': 36574653Sdougm verbose++; 36584653Sdougm break; 36594653Sdougm case 'n': 36604653Sdougm dryrun++; 36614653Sdougm break; 36624653Sdougm case 'P': 36635331Samw if (protocol != NULL) { 36645331Samw (void) printf(gettext( 36655331Samw "Specifying multiple protocols " 36665331Samw "not supported: %s\n"), protocol); 36675331Samw return (SA_SYNTAX_ERR); 36685331Samw } 36694653Sdougm protocol = optarg; 36704653Sdougm if (!sa_valid_protocol(protocol)) { 36714653Sdougm (void) printf(gettext( 36724653Sdougm "Invalid protocol specified: %s\n"), 36734653Sdougm protocol); 36744653Sdougm return (SA_INVALID_PROTOCOL); 36754653Sdougm } 36764653Sdougm break; 36774653Sdougm case 'p': 36784653Sdougm ret = add_opt(&optlist, optarg, 0); 36794653Sdougm switch (ret) { 36804653Sdougm case OPT_ADD_SYNTAX: 36814653Sdougm (void) printf(gettext("Property syntax error:" 36824653Sdougm " %s\n"), optarg); 36834653Sdougm return (SA_SYNTAX_ERR); 36844653Sdougm case OPT_ADD_MEMORY: 36854653Sdougm (void) printf(gettext("No memory to set " 36864653Sdougm "property: %s\n"), optarg); 36874653Sdougm return (SA_NO_MEMORY); 36884653Sdougm default: 36894653Sdougm break; 36904653Sdougm } 36914653Sdougm break; 36925331Samw case 'r': 36935331Samw if (rsrcname != NULL) { 36945331Samw (void) printf(gettext( 36955331Samw "Setting multiple resource names not" 36965331Samw " supported\n")); 36975331Samw return (SA_SYNTAX_ERR); 36985331Samw } 36995331Samw rsrcname = optarg; 37005331Samw break; 37014653Sdougm case 's': 37025331Samw if (sharepath != NULL) { 37035331Samw (void) printf(gettext( 37045331Samw "Setting multiple shares not supported\n")); 37055331Samw return (SA_SYNTAX_ERR); 37065331Samw } 37074653Sdougm sharepath = optarg; 37084653Sdougm break; 37094653Sdougm case 'S': 37105331Samw if (optset != NULL) { 37115331Samw (void) printf(gettext( 37125331Samw "Specifying multiple property " 37135331Samw "spaces not supported: %s\n"), optset); 37145331Samw return (SA_SYNTAX_ERR); 37155331Samw } 37164653Sdougm optset = optarg; 37174653Sdougm break; 37184653Sdougm default: 37194653Sdougm case 'h': 37204653Sdougm case '?': 37214653Sdougm (void) printf(gettext("usage: %s\n"), 37224653Sdougm sa_get_usage(USAGE_SET)); 37234653Sdougm return (SA_OK); 37243034Sdougm } 37253034Sdougm } 37263034Sdougm 37273034Sdougm if (optlist != NULL) 37284653Sdougm ret = chk_opt(optlist, optset != NULL, protocol); 37293034Sdougm 37303034Sdougm if (optind >= argc || (optlist == NULL && optset == NULL) || 37314653Sdougm protocol == NULL || ret != OPT_ADD_OK) { 37324653Sdougm char *sep = "\t"; 37334653Sdougm 37344653Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET)); 37354653Sdougm if (optind >= argc) { 37364653Sdougm (void) printf(gettext("%sgroup must be specified"), 37374653Sdougm sep); 37384653Sdougm sep = ", "; 37394653Sdougm } 37404653Sdougm if (optlist == NULL) { 37414653Sdougm (void) printf(gettext("%sat least one property must be" 37424653Sdougm " specified"), sep); 37434653Sdougm sep = ", "; 37444653Sdougm } 37454653Sdougm if (protocol == NULL) { 37464653Sdougm (void) printf(gettext("%sprotocol must be specified"), 37474653Sdougm sep); 37484653Sdougm sep = ", "; 37494653Sdougm } 37504653Sdougm (void) printf("\n"); 37514653Sdougm ret = SA_SYNTAX_ERR; 37523034Sdougm } else { 37533034Sdougm /* 37545089Sdougm * Group already exists so we can proceed after a few 37555089Sdougm * additional checks related to ZFS handling. 37563034Sdougm */ 37573034Sdougm 37584653Sdougm groupname = argv[optind]; 37595089Sdougm if (strcmp(groupname, "zfs") == 0) { 37605089Sdougm (void) printf(gettext("Changing properties for group " 37615089Sdougm "\"zfs\" not allowed\n")); 37625089Sdougm return (SA_NOT_ALLOWED); 37635089Sdougm } 37645089Sdougm 37654653Sdougm auth = check_authorizations(groupname, flags); 37664653Sdougm if (optset == NULL) 37674653Sdougm ret = basic_set(handle, groupname, optlist, protocol, 37685331Samw sharepath, rsrcname, dryrun); 37694653Sdougm else 37704653Sdougm ret = space_set(handle, groupname, optlist, protocol, 37714653Sdougm sharepath, dryrun, optset); 37724653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 37734653Sdougm (void) printf(gettext("Command would fail: %s\n"), 37744653Sdougm sa_errorstr(SA_NO_PERMISSION)); 37754653Sdougm } 37763034Sdougm } 37773034Sdougm return (ret); 37783034Sdougm } 37793034Sdougm 37803034Sdougm /* 37813034Sdougm * remove_options(group, optlist, proto, *err) 37823034Sdougm * 37834653Sdougm * Helper function to actually remove options from a group after all 37843034Sdougm * preprocessing is done. 37853034Sdougm */ 37863034Sdougm 37873034Sdougm static int 37883034Sdougm remove_options(sa_group_t group, struct options *optlist, 37895331Samw char *proto, int *err) 37903034Sdougm { 37913034Sdougm struct options *cur; 37923034Sdougm sa_optionset_t optionset; 37933034Sdougm sa_property_t prop; 37943034Sdougm int change = 0; 37953034Sdougm int ret = SA_OK; 37963034Sdougm 37973034Sdougm optionset = sa_get_optionset(group, proto); 37983034Sdougm if (optionset != NULL) { 37994653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 38004653Sdougm prop = sa_get_property(optionset, cur->optname); 38014653Sdougm if (prop != NULL) { 38024653Sdougm ret = sa_remove_property(prop); 38034653Sdougm if (ret != SA_OK) 38044653Sdougm break; 38054653Sdougm change = 1; 38064653Sdougm } 38073034Sdougm } 38083034Sdougm } 38093034Sdougm if (ret == SA_OK && change) 38104653Sdougm ret = sa_commit_properties(optionset, 0); 38113034Sdougm 38123034Sdougm if (err != NULL) 38134653Sdougm *err = ret; 38143034Sdougm return (change); 38153034Sdougm } 38163034Sdougm 38173034Sdougm /* 38183034Sdougm * valid_unset(group, optlist, proto) 38193034Sdougm * 38203034Sdougm * Sanity check the optlist to make sure they can be removed. Issue an 38213034Sdougm * error if a property doesn't exist. 38223034Sdougm */ 38233034Sdougm 38243034Sdougm static int 38253034Sdougm valid_unset(sa_group_t group, struct options *optlist, char *proto) 38263034Sdougm { 38273034Sdougm struct options *cur; 38283034Sdougm sa_optionset_t optionset; 38293034Sdougm sa_property_t prop; 38303034Sdougm int ret = SA_OK; 38313034Sdougm 38323034Sdougm optionset = sa_get_optionset(group, proto); 38333034Sdougm if (optionset != NULL) { 38344653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 38354653Sdougm prop = sa_get_property(optionset, cur->optname); 38364653Sdougm if (prop == NULL) { 38374653Sdougm (void) printf(gettext( 38384653Sdougm "Could not unset property %s: not set\n"), 38394653Sdougm cur->optname); 38404653Sdougm ret = SA_NO_SUCH_PROP; 38414653Sdougm } 38423034Sdougm } 38433034Sdougm } 38443034Sdougm return (ret); 38453034Sdougm } 38463034Sdougm 38473034Sdougm /* 38483034Sdougm * valid_unset_security(group, optlist, proto) 38493034Sdougm * 38503034Sdougm * Sanity check the optlist to make sure they can be removed. Issue an 38513034Sdougm * error if a property doesn't exist. 38523034Sdougm */ 38533034Sdougm 38543034Sdougm static int 38553034Sdougm valid_unset_security(sa_group_t group, struct options *optlist, char *proto, 38565331Samw char *sectype) 38573034Sdougm { 38583034Sdougm struct options *cur; 38593034Sdougm sa_security_t security; 38603034Sdougm sa_property_t prop; 38613034Sdougm int ret = SA_OK; 38623034Sdougm char *sec; 38633034Sdougm 38643034Sdougm sec = sa_proto_space_alias(proto, sectype); 38653034Sdougm security = sa_get_security(group, sec, proto); 38663034Sdougm if (security != NULL) { 38674653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 38684653Sdougm prop = sa_get_property(security, cur->optname); 38694653Sdougm if (prop == NULL) { 38704653Sdougm (void) printf(gettext( 38714653Sdougm "Could not unset property %s: not set\n"), 38724653Sdougm cur->optname); 38734653Sdougm ret = SA_NO_SUCH_PROP; 38744653Sdougm } 38753034Sdougm } 38763034Sdougm } else { 38774653Sdougm (void) printf(gettext( 38784653Sdougm "Could not unset %s: space not defined\n"), sectype); 38794653Sdougm ret = SA_NO_SUCH_SECURITY; 38803034Sdougm } 38813034Sdougm if (sec != NULL) 38824653Sdougm sa_free_attr_string(sec); 38833034Sdougm return (ret); 38843034Sdougm } 38853034Sdougm 38863034Sdougm /* 38873034Sdougm * remove_security(group, optlist, proto) 38883034Sdougm * 38893034Sdougm * Remove the properties since they were checked as valid. 38903034Sdougm */ 38913034Sdougm 38923034Sdougm static int 38933034Sdougm remove_security(sa_group_t group, char *sectype, 38945331Samw struct options *optlist, char *proto, int *err) 38953034Sdougm { 38963034Sdougm sa_security_t security; 38973034Sdougm int ret = SA_OK; 38983034Sdougm int change = 0; 38993034Sdougm 39003034Sdougm sectype = sa_proto_space_alias(proto, sectype); 39013034Sdougm security = sa_get_security(group, sectype, proto); 39023034Sdougm if (sectype != NULL) 39034653Sdougm sa_free_attr_string(sectype); 39043034Sdougm 39053034Sdougm if (security != NULL) { 39064653Sdougm while (optlist != NULL) { 39074653Sdougm sa_property_t prop; 39084653Sdougm prop = sa_get_property(security, optlist->optname); 39094653Sdougm if (prop != NULL) { 39104653Sdougm ret = sa_remove_property(prop); 39114653Sdougm if (ret != SA_OK) 39124653Sdougm break; 39134653Sdougm change = 1; 39144653Sdougm } 39154653Sdougm optlist = optlist->next; 39163034Sdougm } 39173034Sdougm /* 39183034Sdougm * when done, properties may have all been removed but 39193034Sdougm * we need to keep the security type itself until 39203034Sdougm * explicitly removed. 39213034Sdougm */ 39224653Sdougm if (ret == SA_OK && change) 39234653Sdougm ret = sa_commit_properties(security, 0); 39243034Sdougm } else { 39254653Sdougm ret = SA_NO_SUCH_PROP; 39263034Sdougm } 39273034Sdougm if (err != NULL) 39284653Sdougm *err = ret; 39293034Sdougm return (change); 39303034Sdougm } 39313034Sdougm 39323034Sdougm /* 39335331Samw * basic_unset(groupname, optlist, protocol, sharepath, rsrcname, dryrun) 39343034Sdougm * 39354653Sdougm * Unset non-named optionset properties. 39363034Sdougm */ 39373034Sdougm 39383034Sdougm static int 39393910Sdougm basic_unset(sa_handle_t handle, char *groupname, struct options *optlist, 39405331Samw char *protocol, char *sharepath, char *rsrcname, int dryrun) 39413034Sdougm { 39423034Sdougm sa_group_t group; 39433034Sdougm int ret = SA_OK; 39443034Sdougm int change = 0; 39453034Sdougm struct list *worklist = NULL; 39464653Sdougm sa_share_t share = NULL; 39475331Samw sa_resource_t resource = NULL; 39483034Sdougm 39493910Sdougm group = sa_get_group(handle, groupname); 39504653Sdougm if (group == NULL) 39514653Sdougm return (ret); 39524653Sdougm 39535331Samw /* 39545331Samw * If there is a sharepath, make sure it belongs to 39555331Samw * the group. 39565331Samw */ 39574653Sdougm if (sharepath != NULL) { 39583034Sdougm share = sa_get_share(group, sharepath); 39593034Sdougm if (share == NULL) { 39604653Sdougm (void) printf(gettext( 39614653Sdougm "Share does not exist in group %s\n"), 39624653Sdougm groupname, sharepath); 39634653Sdougm ret = SA_NO_SUCH_PATH; 39643034Sdougm } 39654653Sdougm } 39665331Samw /* 39675331Samw * If a resource name exists, make sure it belongs to 39685331Samw * the share if present else it belongs to the 39695331Samw * group. Also check the protocol to see if it 39705331Samw * supports resource level properties or not. If not, 39715331Samw * use share only. 39725331Samw */ 39735331Samw if (rsrcname != NULL) { 39745331Samw if (share != NULL) { 39755331Samw resource = sa_get_share_resource(share, rsrcname); 39765331Samw if (resource == NULL) 39775331Samw ret = SA_NO_SUCH_RESOURCE; 39785331Samw } else { 39795331Samw resource = sa_get_resource(group, rsrcname); 39805331Samw if (resource != NULL) { 39815331Samw share = sa_get_resource_parent(resource); 39825331Samw } else { 39835331Samw ret = SA_NO_SUCH_RESOURCE; 39845331Samw } 39855331Samw } 39865331Samw if (ret == SA_OK && resource != NULL) { 39875331Samw uint64_t features; 39885331Samw /* 39895331Samw * Check to see if the resource can take 39905331Samw * properties. If so, stick the resource into 39915331Samw * "share" so it will all just work. 39925331Samw */ 39935331Samw features = sa_proto_get_featureset(protocol); 39945331Samw if (features & SA_FEATURE_RESOURCE) 39955331Samw share = (sa_share_t)resource; 39965331Samw } 39975331Samw } 39985331Samw 39994653Sdougm if (ret == SA_OK) { 40003034Sdougm /* group must exist */ 40013034Sdougm ret = valid_unset(share != NULL ? share : group, 40024653Sdougm optlist, protocol); 40033034Sdougm if (ret == SA_OK && !dryrun) { 40044653Sdougm if (share != NULL) { 40054653Sdougm sa_optionset_t optionset; 40064653Sdougm sa_property_t prop; 40074653Sdougm change |= remove_options(share, optlist, 40084653Sdougm protocol, &ret); 40094653Sdougm /* 40104653Sdougm * If a share optionset is 40114653Sdougm * empty, remove it. 40124653Sdougm */ 40134653Sdougm optionset = sa_get_optionset((sa_share_t)share, 40144653Sdougm protocol); 40154653Sdougm if (optionset != NULL) { 40164653Sdougm prop = sa_get_property(optionset, NULL); 40174653Sdougm if (prop == NULL) 40184653Sdougm (void) sa_destroy_optionset( 40194653Sdougm optionset); 40204653Sdougm } 40214653Sdougm } else { 40224653Sdougm change |= remove_options(group, 40234653Sdougm optlist, protocol, &ret); 40243034Sdougm } 40254653Sdougm if (ret == SA_OK && change) 40265331Samw worklist = add_list(worklist, group, share, 40275331Samw protocol); 40284653Sdougm if (ret != SA_OK) 40294653Sdougm (void) printf(gettext( 40304653Sdougm "Could not remove properties: " 40314653Sdougm "%s\n"), sa_errorstr(ret)); 40323034Sdougm } 40334653Sdougm } else { 40345331Samw (void) printf(gettext("Group \"%s\" not found\n"), groupname); 40353034Sdougm ret = SA_NO_SUCH_GROUP; 40363034Sdougm } 40374653Sdougm free_opt(optlist); 40383034Sdougm 40393034Sdougm /* 40404653Sdougm * We have a group and potentially legal additions 40414653Sdougm * 40424653Sdougm * Commit to configuration if not a dryrun 40433034Sdougm */ 40443034Sdougm if (!dryrun && ret == SA_OK) { 40454653Sdougm if (change && worklist != NULL) { 40464653Sdougm /* properties changed, so update all shares */ 40474653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 40485331Samw protocol, B_TRUE); 40494653Sdougm } 40503034Sdougm } 40513034Sdougm if (worklist != NULL) 40524653Sdougm free_list(worklist); 40533034Sdougm return (ret); 40543034Sdougm } 40553034Sdougm 40563034Sdougm /* 40573034Sdougm * space_unset(groupname, optlist, protocol, sharepath, dryrun) 40583034Sdougm * 40594653Sdougm * Unset named optionset properties. 40603034Sdougm */ 40613034Sdougm static int 40623910Sdougm space_unset(sa_handle_t handle, char *groupname, struct options *optlist, 40635331Samw char *protocol, char *sharepath, int dryrun, char *sectype) 40643034Sdougm { 40653034Sdougm sa_group_t group; 40663034Sdougm int ret = SA_OK; 40673034Sdougm int change = 0; 40683034Sdougm struct list *worklist = NULL; 40694653Sdougm sa_share_t share = NULL; 40703034Sdougm 40713910Sdougm group = sa_get_group(handle, groupname); 40724653Sdougm if (group == NULL) { 40734653Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 40744653Sdougm return (SA_NO_SUCH_GROUP); 40754653Sdougm } 40764653Sdougm if (sharepath != NULL) { 40773034Sdougm share = sa_get_share(group, sharepath); 40783034Sdougm if (share == NULL) { 40794653Sdougm (void) printf(gettext( 40804653Sdougm "Share does not exist in group %s\n"), 40814653Sdougm groupname, sharepath); 40824653Sdougm return (SA_NO_SUCH_PATH); 40833034Sdougm } 40844653Sdougm } 40855331Samw ret = valid_unset_security(share != NULL ? share : group, 40865331Samw optlist, protocol, sectype); 40874653Sdougm 40884653Sdougm if (ret == SA_OK && !dryrun) { 40894653Sdougm if (optlist != NULL) { 40903034Sdougm if (share != NULL) { 40914653Sdougm sa_security_t optionset; 40924653Sdougm sa_property_t prop; 40934653Sdougm change = remove_security(share, 40944653Sdougm sectype, optlist, protocol, &ret); 40954653Sdougm 40964653Sdougm /* If a share security is empty, remove it */ 40974653Sdougm optionset = sa_get_security((sa_group_t)share, 40984653Sdougm sectype, protocol); 40994653Sdougm if (optionset != NULL) { 41004653Sdougm prop = sa_get_property(optionset, 41014653Sdougm NULL); 41024653Sdougm if (prop == NULL) 41034653Sdougm ret = sa_destroy_security( 41044653Sdougm optionset); 41054653Sdougm } 41063034Sdougm } else { 41074653Sdougm change = remove_security(group, sectype, 41084653Sdougm optlist, protocol, &ret); 41093034Sdougm } 41104653Sdougm } else { 41113034Sdougm sa_security_t security; 41123034Sdougm char *sec; 41133034Sdougm sec = sa_proto_space_alias(protocol, sectype); 41143034Sdougm security = sa_get_security(group, sec, protocol); 41153034Sdougm if (sec != NULL) 41164653Sdougm sa_free_attr_string(sec); 41173034Sdougm if (security != NULL) { 41184653Sdougm ret = sa_destroy_security(security); 41194653Sdougm if (ret == SA_OK) 41204653Sdougm change = 1; 41213034Sdougm } else { 41224653Sdougm ret = SA_NO_SUCH_PROP; 41233034Sdougm } 41244653Sdougm } 41254653Sdougm if (ret != SA_OK) 41263034Sdougm (void) printf(gettext("Could not unset property: %s\n"), 41274653Sdougm sa_errorstr(ret)); 41283034Sdougm } 41294653Sdougm 41304653Sdougm if (ret == SA_OK && change) 41315331Samw worklist = add_list(worklist, group, 0, protocol); 41324653Sdougm 41333034Sdougm free_opt(optlist); 41343034Sdougm /* 41354653Sdougm * We have a group and potentially legal additions 41363034Sdougm */ 41373034Sdougm 41384653Sdougm /* Commit to configuration if not a dryrun */ 41393034Sdougm if (!dryrun && ret == 0) { 41403034Sdougm /* properties changed, so update all shares */ 41414653Sdougm if (change && worklist != NULL) 41424653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 41435331Samw protocol, B_TRUE); 41444653Sdougm ret = sa_update_config(handle); 41453034Sdougm } 41463034Sdougm if (worklist != NULL) 41474653Sdougm free_list(worklist); 41483034Sdougm return (ret); 41493034Sdougm } 41503034Sdougm 41513034Sdougm /* 41523034Sdougm * sa_unset(flags, argc, argv) 41533034Sdougm * 41544653Sdougm * Implements the unset subcommand. Parsing done here and then basic 41553034Sdougm * or space versions of the real code are called. 41563034Sdougm */ 41573034Sdougm 41583034Sdougm int 41593910Sdougm sa_unset(sa_handle_t handle, int flags, int argc, char *argv[]) 41603034Sdougm { 41613034Sdougm char *groupname; 41623034Sdougm int verbose = 0; 41633034Sdougm int dryrun = 0; 41643034Sdougm int c; 41653034Sdougm char *protocol = NULL; 41663034Sdougm int ret = SA_OK; 41673034Sdougm struct options *optlist = NULL; 41685331Samw char *rsrcname = NULL; 41693034Sdougm char *sharepath = NULL; 41703034Sdougm char *optset = NULL; 41713034Sdougm int auth; 41723034Sdougm 41735331Samw while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) { 41744653Sdougm switch (c) { 41754653Sdougm case 'v': 41764653Sdougm verbose++; 41774653Sdougm break; 41784653Sdougm case 'n': 41794653Sdougm dryrun++; 41804653Sdougm break; 41814653Sdougm case 'P': 41825331Samw if (protocol != NULL) { 41835331Samw (void) printf(gettext( 41845331Samw "Specifying multiple protocols " 41855331Samw "not supported: %s\n"), protocol); 41865331Samw return (SA_SYNTAX_ERR); 41875331Samw } 41884653Sdougm protocol = optarg; 41894653Sdougm if (!sa_valid_protocol(protocol)) { 41904653Sdougm (void) printf(gettext( 41914653Sdougm "Invalid protocol specified: %s\n"), 41924653Sdougm protocol); 41934653Sdougm return (SA_INVALID_PROTOCOL); 41944653Sdougm } 41954653Sdougm break; 41964653Sdougm case 'p': 41974653Sdougm ret = add_opt(&optlist, optarg, 1); 41984653Sdougm switch (ret) { 41994653Sdougm case OPT_ADD_SYNTAX: 42004653Sdougm (void) printf(gettext("Property syntax error " 42014653Sdougm "for property %s\n"), optarg); 42024653Sdougm return (SA_SYNTAX_ERR); 42034653Sdougm 42044653Sdougm case OPT_ADD_PROPERTY: 42054653Sdougm (void) printf(gettext("Properties need to be " 42064653Sdougm "set with set command: %s\n"), optarg); 42074653Sdougm return (SA_SYNTAX_ERR); 42084653Sdougm 42094653Sdougm default: 42104653Sdougm break; 42114653Sdougm } 42124653Sdougm break; 42135331Samw case 'r': 42145331Samw /* 42155331Samw * Unset properties on resource if applicable or on 42165331Samw * share if resource for this protocol doesn't use 42175331Samw * resources. 42185331Samw */ 42195331Samw if (rsrcname != NULL) { 42205331Samw (void) printf(gettext( 42215331Samw "Unsetting multiple resource " 42225331Samw "names not supported\n")); 42235331Samw return (SA_SYNTAX_ERR); 42245331Samw } 42255331Samw rsrcname = optarg; 42265331Samw break; 42274653Sdougm case 's': 42285331Samw if (sharepath != NULL) { 42295331Samw (void) printf(gettext( 42305331Samw "Adding multiple shares not supported\n")); 42315331Samw return (SA_SYNTAX_ERR); 42325331Samw } 42334653Sdougm sharepath = optarg; 42344653Sdougm break; 42354653Sdougm case 'S': 42365331Samw if (optset != NULL) { 42375331Samw (void) printf(gettext( 42385331Samw "Specifying multiple property " 42395331Samw "spaces not supported: %s\n"), optset); 42405331Samw return (SA_SYNTAX_ERR); 42415331Samw } 42424653Sdougm optset = optarg; 42434653Sdougm break; 42444653Sdougm default: 42454653Sdougm case 'h': 42464653Sdougm case '?': 42474653Sdougm (void) printf(gettext("usage: %s\n"), 42484653Sdougm sa_get_usage(USAGE_UNSET)); 42494653Sdougm return (SA_OK); 42503034Sdougm } 42513034Sdougm } 42523034Sdougm 42533034Sdougm if (optlist != NULL) 42544653Sdougm ret = chk_opt(optlist, optset != NULL, protocol); 42553034Sdougm 42563034Sdougm if (optind >= argc || (optlist == NULL && optset == NULL) || 42573034Sdougm protocol == NULL) { 42584653Sdougm char *sep = "\t"; 42594653Sdougm (void) printf(gettext("usage: %s\n"), 42604653Sdougm sa_get_usage(USAGE_UNSET)); 42614653Sdougm if (optind >= argc) { 42624653Sdougm (void) printf(gettext("%sgroup must be specified"), 42634653Sdougm sep); 42644653Sdougm sep = ", "; 42654653Sdougm } 42664653Sdougm if (optlist == NULL) { 42674653Sdougm (void) printf(gettext("%sat least one property must " 42684653Sdougm "be specified"), sep); 42694653Sdougm sep = ", "; 42704653Sdougm } 42714653Sdougm if (protocol == NULL) { 42724653Sdougm (void) printf(gettext("%sprotocol must be specified"), 42734653Sdougm sep); 42744653Sdougm sep = ", "; 42754653Sdougm } 42764653Sdougm (void) printf("\n"); 42774653Sdougm ret = SA_SYNTAX_ERR; 42783034Sdougm } else { 42793034Sdougm 42803034Sdougm /* 42814653Sdougm * If a group already exists, we can only add a new 42823034Sdougm * protocol to it and not create a new one or add the 42833034Sdougm * same protocol again. 42843034Sdougm */ 42853034Sdougm 42864653Sdougm groupname = argv[optind]; 42874653Sdougm auth = check_authorizations(groupname, flags); 42884653Sdougm if (optset == NULL) 42894653Sdougm ret = basic_unset(handle, groupname, optlist, protocol, 42905331Samw sharepath, rsrcname, dryrun); 42914653Sdougm else 42924653Sdougm ret = space_unset(handle, groupname, optlist, protocol, 42934653Sdougm sharepath, dryrun, optset); 42944653Sdougm 42954653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) 42964653Sdougm (void) printf(gettext("Command would fail: %s\n"), 42974653Sdougm sa_errorstr(SA_NO_PERMISSION)); 42983034Sdougm } 42993034Sdougm return (ret); 43003034Sdougm } 43013034Sdougm 43023034Sdougm /* 43033034Sdougm * sa_enable_group(flags, argc, argv) 43043034Sdougm * 43053034Sdougm * Implements the enable subcommand 43063034Sdougm */ 43073034Sdougm 43083034Sdougm int 43093910Sdougm sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[]) 43103034Sdougm { 43113034Sdougm int verbose = 0; 43123034Sdougm int dryrun = 0; 43133034Sdougm int all = 0; 43143034Sdougm int c; 43153034Sdougm int ret = SA_OK; 43163034Sdougm char *protocol = NULL; 43173034Sdougm char *state; 43183034Sdougm struct list *worklist = NULL; 43193034Sdougm int auth = 1; 43204653Sdougm sa_group_t group; 43213034Sdougm 43223034Sdougm while ((c = getopt(argc, argv, "?havnP:")) != EOF) { 43234653Sdougm switch (c) { 43244653Sdougm case 'a': 43254653Sdougm all = 1; 43264653Sdougm break; 43274653Sdougm case 'n': 43284653Sdougm dryrun++; 43294653Sdougm break; 43304653Sdougm case 'P': 43315331Samw if (protocol != NULL) { 43325331Samw (void) printf(gettext( 43335331Samw "Specifying multiple protocols " 43345331Samw "not supported: %s\n"), protocol); 43355331Samw return (SA_SYNTAX_ERR); 43365331Samw } 43374653Sdougm protocol = optarg; 43384653Sdougm if (!sa_valid_protocol(protocol)) { 43394653Sdougm (void) printf(gettext( 43404653Sdougm "Invalid protocol specified: %s\n"), 43413034Sdougm protocol); 43424653Sdougm return (SA_INVALID_PROTOCOL); 43434653Sdougm } 43444653Sdougm break; 43454653Sdougm case 'v': 43464653Sdougm verbose++; 43474653Sdougm break; 43484653Sdougm default: 43494653Sdougm case 'h': 43504653Sdougm case '?': 43514653Sdougm (void) printf(gettext("usage: %s\n"), 43524653Sdougm sa_get_usage(USAGE_ENABLE)); 43534653Sdougm return (0); 43543034Sdougm } 43553034Sdougm } 43563034Sdougm 43573034Sdougm if (optind == argc && !all) { 43584653Sdougm (void) printf(gettext("usage: %s\n"), 43594653Sdougm sa_get_usage(USAGE_ENABLE)); 43604653Sdougm (void) printf(gettext("\tmust specify group\n")); 43614653Sdougm return (SA_NO_SUCH_PATH); 43624653Sdougm } 43634653Sdougm if (!all) { 43643034Sdougm while (optind < argc) { 43654653Sdougm group = sa_get_group(handle, argv[optind]); 43664653Sdougm if (group != NULL) { 43674653Sdougm auth &= check_authorizations(argv[optind], 43684653Sdougm flags); 43694653Sdougm state = sa_get_group_attr(group, "state"); 43704653Sdougm if (state != NULL && 43714653Sdougm strcmp(state, "enabled") == 0) { 43724653Sdougm /* already enabled */ 43734653Sdougm if (verbose) 43744653Sdougm (void) printf(gettext( 43754653Sdougm "Group \"%s\" is already " 43764653Sdougm "enabled\n"), 43774653Sdougm argv[optind]); 43784653Sdougm ret = SA_BUSY; /* already enabled */ 43794653Sdougm } else { 43804653Sdougm worklist = add_list(worklist, group, 43815331Samw 0, protocol); 43824653Sdougm if (verbose) 43834653Sdougm (void) printf(gettext( 43844653Sdougm "Enabling group \"%s\"\n"), 43854653Sdougm argv[optind]); 43864653Sdougm } 43874653Sdougm if (state != NULL) 43884653Sdougm sa_free_attr_string(state); 43893034Sdougm } else { 43904653Sdougm ret = SA_NO_SUCH_GROUP; 43913034Sdougm } 43924653Sdougm optind++; 43933034Sdougm } 43944653Sdougm } else { 43954653Sdougm for (group = sa_get_group(handle, NULL); 43964653Sdougm group != NULL; 43973034Sdougm group = sa_get_next_group(group)) { 43985331Samw worklist = add_list(worklist, group, 0, protocol); 43993034Sdougm } 44004653Sdougm } 44014653Sdougm if (!dryrun && ret == SA_OK) 44025331Samw ret = enable_all_groups(handle, worklist, 1, 0, NULL, B_FALSE); 44034653Sdougm 44044653Sdougm if (ret != SA_OK && ret != SA_BUSY) 44053034Sdougm (void) printf(gettext("Could not enable group: %s\n"), 44064653Sdougm sa_errorstr(ret)); 44074653Sdougm if (ret == SA_BUSY) 44083034Sdougm ret = SA_OK; 44094653Sdougm 44103034Sdougm if (worklist != NULL) 44114653Sdougm free_list(worklist); 44123034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 44134653Sdougm (void) printf(gettext("Command would fail: %s\n"), 44144653Sdougm sa_errorstr(SA_NO_PERMISSION)); 44153034Sdougm } 44163034Sdougm return (ret); 44173034Sdougm } 44183034Sdougm 44193034Sdougm /* 44205331Samw * disable_group(group, proto) 44213034Sdougm * 44225331Samw * Disable all the shares in the specified group.. This is a helper 44235331Samw * for disable_all_groups in order to simplify regular and subgroup 44245331Samw * (zfs) disabling. Group has already been checked for non-NULL. 44253034Sdougm */ 44263034Sdougm 44273034Sdougm static int 44285331Samw disable_group(sa_group_t group, char *proto) 44293034Sdougm { 44303034Sdougm sa_share_t share; 44313034Sdougm int ret = SA_OK; 44323034Sdougm 44335331Samw /* 44345331Samw * If the protocol isn't enabled, skip it and treat as 44355331Samw * successful. 44365331Samw */ 44375331Samw if (!has_protocol(group, proto)) 44385331Samw return (ret); 44395331Samw 44403034Sdougm for (share = sa_get_share(group, NULL); 44413034Sdougm share != NULL && ret == SA_OK; 44423034Sdougm share = sa_get_next_share(share)) { 44435331Samw ret = sa_disable_share(share, proto); 44444653Sdougm if (ret == SA_NO_SUCH_PATH) { 44454653Sdougm /* 44464653Sdougm * this is OK since the path is gone. we can't 44474653Sdougm * re-share it anyway so no error. 44484653Sdougm */ 44494653Sdougm ret = SA_OK; 44504653Sdougm } 44513034Sdougm } 44523034Sdougm return (ret); 44533034Sdougm } 44543034Sdougm 44553034Sdougm /* 44563034Sdougm * disable_all_groups(work, setstate) 44573034Sdougm * 44583034Sdougm * helper function that disables the shares in the list of groups 44593034Sdougm * provided. It optionally marks the group as disabled. Used by both 44603034Sdougm * enable and start subcommands. 44613034Sdougm */ 44623034Sdougm 44633034Sdougm static int 44643910Sdougm disable_all_groups(sa_handle_t handle, struct list *work, int setstate) 44653034Sdougm { 44663034Sdougm int ret = SA_OK; 44673034Sdougm sa_group_t subgroup, group; 44683034Sdougm 44693034Sdougm while (work != NULL && ret == SA_OK) { 44704653Sdougm group = (sa_group_t)work->item; 44714653Sdougm if (setstate) 44724653Sdougm ret = sa_set_group_attr(group, "state", "disabled"); 44734653Sdougm if (ret == SA_OK) { 44744653Sdougm char *name; 44754653Sdougm name = sa_get_group_attr(group, "name"); 44764653Sdougm if (name != NULL && strcmp(name, "zfs") == 0) { 44774653Sdougm /* need to get the sub-groups for stopping */ 44784653Sdougm for (subgroup = sa_get_sub_group(group); 44794653Sdougm subgroup != NULL; 44804653Sdougm subgroup = sa_get_next_group(subgroup)) { 44815331Samw ret = disable_group(subgroup, 44825331Samw work->proto); 44834653Sdougm } 44844653Sdougm } else { 44855331Samw ret = disable_group(group, work->proto); 44864653Sdougm } 44874653Sdougm /* 44884653Sdougm * We don't want to "disable" since it won't come 44894653Sdougm * up after a reboot. The SMF framework should do 44904653Sdougm * the right thing. On enable we do want to do 44914653Sdougm * something. 44924653Sdougm */ 44933034Sdougm } 44944653Sdougm work = work->next; 44953034Sdougm } 44963034Sdougm if (ret == SA_OK) 44974653Sdougm ret = sa_update_config(handle); 44983034Sdougm return (ret); 44993034Sdougm } 45003034Sdougm 45013034Sdougm /* 45023034Sdougm * sa_disable_group(flags, argc, argv) 45033034Sdougm * 45043034Sdougm * Implements the disable subcommand 45053034Sdougm */ 45063034Sdougm 45073034Sdougm int 45083910Sdougm sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[]) 45093034Sdougm { 45103034Sdougm int verbose = 0; 45113034Sdougm int dryrun = 0; 45123034Sdougm int all = 0; 45133034Sdougm int c; 45143034Sdougm int ret = SA_OK; 45155331Samw char *protocol = NULL; 45163034Sdougm char *state; 45173034Sdougm struct list *worklist = NULL; 45184653Sdougm sa_group_t group; 45193034Sdougm int auth = 1; 45203034Sdougm 45213034Sdougm while ((c = getopt(argc, argv, "?havn")) != EOF) { 45224653Sdougm switch (c) { 45234653Sdougm case 'a': 45244653Sdougm all = 1; 45254653Sdougm break; 45264653Sdougm case 'n': 45274653Sdougm dryrun++; 45284653Sdougm break; 45294653Sdougm case 'P': 45305331Samw if (protocol != NULL) { 45315331Samw (void) printf(gettext( 45325331Samw "Specifying multiple protocols " 45335331Samw "not supported: %s\n"), protocol); 45345331Samw return (SA_SYNTAX_ERR); 45355331Samw } 45364653Sdougm protocol = optarg; 45374653Sdougm if (!sa_valid_protocol(protocol)) { 45384653Sdougm (void) printf(gettext( 45394653Sdougm "Invalid protocol specified: %s\n"), 45404653Sdougm protocol); 45414653Sdougm return (SA_INVALID_PROTOCOL); 45424653Sdougm } 45434653Sdougm break; 45444653Sdougm case 'v': 45454653Sdougm verbose++; 45464653Sdougm break; 45474653Sdougm default: 45484653Sdougm case 'h': 45494653Sdougm case '?': 45504653Sdougm (void) printf(gettext("usage: %s\n"), 45514653Sdougm sa_get_usage(USAGE_DISABLE)); 45524653Sdougm return (0); 45533034Sdougm } 45543034Sdougm } 45553034Sdougm 45563034Sdougm if (optind == argc && !all) { 45573034Sdougm (void) printf(gettext("usage: %s\n"), 45584653Sdougm sa_get_usage(USAGE_DISABLE)); 45593034Sdougm (void) printf(gettext("\tmust specify group\n")); 45604653Sdougm return (SA_NO_SUCH_PATH); 45614653Sdougm } 45624653Sdougm if (!all) { 45634653Sdougm while (optind < argc) { 45643910Sdougm group = sa_get_group(handle, argv[optind]); 45653034Sdougm if (group != NULL) { 45664653Sdougm auth &= check_authorizations(argv[optind], 45674653Sdougm flags); 45684653Sdougm state = sa_get_group_attr(group, "state"); 45694653Sdougm if (state == NULL || 45704653Sdougm strcmp(state, "disabled") == 0) { 45714653Sdougm /* already disabled */ 45724653Sdougm if (verbose) 45734653Sdougm (void) printf(gettext( 45744653Sdougm "Group \"%s\" is " 45754653Sdougm "already disabled\n"), 45764653Sdougm argv[optind]); 45775331Samw ret = SA_BUSY; /* already disabled */ 45784653Sdougm } else { 45795331Samw worklist = add_list(worklist, group, 0, 45805331Samw protocol); 45814653Sdougm if (verbose) 45824653Sdougm (void) printf(gettext( 45834653Sdougm "Disabling group " 45844653Sdougm "\"%s\"\n"), argv[optind]); 45854653Sdougm } 45864653Sdougm if (state != NULL) 45874653Sdougm sa_free_attr_string(state); 45883034Sdougm } else { 45894653Sdougm ret = SA_NO_SUCH_GROUP; 45903034Sdougm } 45913034Sdougm optind++; 45924653Sdougm } 45934653Sdougm } else { 45944653Sdougm for (group = sa_get_group(handle, NULL); 45954653Sdougm group != NULL; 45964653Sdougm group = sa_get_next_group(group)) 45975331Samw worklist = add_list(worklist, group, 0, protocol); 45983034Sdougm } 45994653Sdougm 46004653Sdougm if (ret == SA_OK && !dryrun) 46014653Sdougm ret = disable_all_groups(handle, worklist, 1); 46024653Sdougm if (ret != SA_OK && ret != SA_BUSY) 46034653Sdougm (void) printf(gettext("Could not disable group: %s\n"), 46044653Sdougm sa_errorstr(ret)); 46054653Sdougm if (ret == SA_BUSY) 46064653Sdougm ret = SA_OK; 46073034Sdougm if (worklist != NULL) 46084653Sdougm free_list(worklist); 46094653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) 46104653Sdougm (void) printf(gettext("Command would fail: %s\n"), 46114653Sdougm sa_errorstr(SA_NO_PERMISSION)); 46123034Sdougm return (ret); 46133034Sdougm } 46143034Sdougm 46153034Sdougm /* 46163034Sdougm * sa_start_group(flags, argc, argv) 46173034Sdougm * 46183034Sdougm * Implements the start command. 46193034Sdougm * This is similar to enable except it doesn't change the state 46203034Sdougm * of the group(s) and only enables shares if the group is already 46213034Sdougm * enabled. 46223034Sdougm */ 46235331Samw 46243034Sdougm int 46253910Sdougm sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[]) 46263034Sdougm { 46273034Sdougm int verbose = 0; 46283034Sdougm int all = 0; 46293034Sdougm int c; 46303034Sdougm int ret = SMF_EXIT_OK; 46313034Sdougm char *protocol = NULL; 46323034Sdougm char *state; 46333034Sdougm struct list *worklist = NULL; 46344653Sdougm sa_group_t group; 46355331Samw #ifdef lint 46365331Samw flags = flags; 46375331Samw #endif 46383034Sdougm 46393034Sdougm while ((c = getopt(argc, argv, "?havP:")) != EOF) { 46404653Sdougm switch (c) { 46414653Sdougm case 'a': 46424653Sdougm all = 1; 46434653Sdougm break; 46444653Sdougm case 'P': 46455331Samw if (protocol != NULL) { 46465331Samw (void) printf(gettext( 46475331Samw "Specifying multiple protocols " 46485331Samw "not supported: %s\n"), protocol); 46495331Samw return (SA_SYNTAX_ERR); 46505331Samw } 46514653Sdougm protocol = optarg; 46524653Sdougm if (!sa_valid_protocol(protocol)) { 46534653Sdougm (void) printf(gettext( 46544653Sdougm "Invalid protocol specified: %s\n"), 46553034Sdougm protocol); 46564653Sdougm return (SA_INVALID_PROTOCOL); 46574653Sdougm } 46584653Sdougm break; 46594653Sdougm case 'v': 46604653Sdougm verbose++; 46614653Sdougm break; 46624653Sdougm default: 46634653Sdougm case 'h': 46644653Sdougm case '?': 46654653Sdougm (void) printf(gettext("usage: %s\n"), 46664653Sdougm sa_get_usage(USAGE_START)); 46674653Sdougm return (SA_OK); 46683034Sdougm } 46693034Sdougm } 46703034Sdougm 46713034Sdougm if (optind == argc && !all) { 46723034Sdougm (void) printf(gettext("usage: %s\n"), 46734653Sdougm sa_get_usage(USAGE_START)); 46744653Sdougm return (SMF_EXIT_ERR_FATAL); 46754653Sdougm } 46764653Sdougm 46774653Sdougm if (!all) { 46784653Sdougm while (optind < argc) { 46793910Sdougm group = sa_get_group(handle, argv[optind]); 46803034Sdougm if (group != NULL) { 46814653Sdougm state = sa_get_group_attr(group, "state"); 46824653Sdougm if (state == NULL || 46834653Sdougm strcmp(state, "enabled") == 0) { 46845331Samw worklist = add_list(worklist, group, 0, 46855331Samw protocol); 46864653Sdougm if (verbose) 46874653Sdougm (void) printf(gettext( 46884653Sdougm "Starting group \"%s\"\n"), 46894653Sdougm argv[optind]); 46904653Sdougm } else { 46914653Sdougm /* 46924653Sdougm * Determine if there are any 46935331Samw * protocols. If there aren't any, 46944653Sdougm * then there isn't anything to do in 46954653Sdougm * any case so no error. 46964653Sdougm */ 46974653Sdougm if (sa_get_optionset(group, 46984653Sdougm protocol) != NULL) { 46994653Sdougm ret = SMF_EXIT_OK; 47004653Sdougm } 47013034Sdougm } 47024653Sdougm if (state != NULL) 47034653Sdougm sa_free_attr_string(state); 47043034Sdougm } 47053034Sdougm optind++; 47064653Sdougm } 47074653Sdougm } else { 47085331Samw for (group = sa_get_group(handle, NULL); 47095331Samw group != NULL; 47104653Sdougm group = sa_get_next_group(group)) { 47113034Sdougm state = sa_get_group_attr(group, "state"); 47123034Sdougm if (state == NULL || strcmp(state, "enabled") == 0) 47135331Samw worklist = add_list(worklist, group, 0, 47145331Samw protocol); 47153034Sdougm if (state != NULL) 47164653Sdougm sa_free_attr_string(state); 47173034Sdougm } 47183034Sdougm } 47194653Sdougm 47205331Samw (void) enable_all_groups(handle, worklist, 0, 1, protocol, B_FALSE); 47214653Sdougm 47223034Sdougm if (worklist != NULL) 47234653Sdougm free_list(worklist); 47243034Sdougm return (ret); 47253034Sdougm } 47263034Sdougm 47273034Sdougm /* 47283034Sdougm * sa_stop_group(flags, argc, argv) 47293034Sdougm * 47303034Sdougm * Implements the stop command. 47313034Sdougm * This is similar to disable except it doesn't change the state 47323034Sdougm * of the group(s) and only disables shares if the group is already 47333034Sdougm * enabled. 47343034Sdougm */ 47353034Sdougm int 47363910Sdougm sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[]) 47373034Sdougm { 47383034Sdougm int verbose = 0; 47393034Sdougm int all = 0; 47403034Sdougm int c; 47413034Sdougm int ret = SMF_EXIT_OK; 47423034Sdougm char *protocol = NULL; 47433034Sdougm char *state; 47443034Sdougm struct list *worklist = NULL; 47454653Sdougm sa_group_t group; 47465331Samw #ifdef lint 47475331Samw flags = flags; 47485331Samw #endif 47493034Sdougm 47503034Sdougm while ((c = getopt(argc, argv, "?havP:")) != EOF) { 47514653Sdougm switch (c) { 47524653Sdougm case 'a': 47534653Sdougm all = 1; 47544653Sdougm break; 47554653Sdougm case 'P': 47565331Samw if (protocol != NULL) { 47575331Samw (void) printf(gettext( 47585331Samw "Specifying multiple protocols " 47595331Samw "not supported: %s\n"), protocol); 47605331Samw return (SA_SYNTAX_ERR); 47615331Samw } 47624653Sdougm protocol = optarg; 47634653Sdougm if (!sa_valid_protocol(protocol)) { 47644653Sdougm (void) printf(gettext( 47654653Sdougm "Invalid protocol specified: %s\n"), 47664653Sdougm protocol); 47674653Sdougm return (SA_INVALID_PROTOCOL); 47684653Sdougm } 47694653Sdougm break; 47704653Sdougm case 'v': 47714653Sdougm verbose++; 47724653Sdougm break; 47734653Sdougm default: 47744653Sdougm case 'h': 47754653Sdougm case '?': 47764653Sdougm (void) printf(gettext("usage: %s\n"), 47774653Sdougm sa_get_usage(USAGE_STOP)); 47784653Sdougm return (0); 47793034Sdougm } 47803034Sdougm } 47813034Sdougm 47823034Sdougm if (optind == argc && !all) { 47834653Sdougm (void) printf(gettext("usage: %s\n"), 47844653Sdougm sa_get_usage(USAGE_STOP)); 47854653Sdougm return (SMF_EXIT_ERR_FATAL); 47864653Sdougm } else if (!all) { 47874653Sdougm while (optind < argc) { 47883910Sdougm group = sa_get_group(handle, argv[optind]); 47893034Sdougm if (group != NULL) { 47904653Sdougm state = sa_get_group_attr(group, "state"); 47914653Sdougm if (state == NULL || 47924653Sdougm strcmp(state, "enabled") == 0) { 47935331Samw worklist = add_list(worklist, group, 0, 47945331Samw protocol); 47954653Sdougm if (verbose) 47964653Sdougm (void) printf(gettext( 47974653Sdougm "Stopping group \"%s\"\n"), 47984653Sdougm argv[optind]); 47994653Sdougm } else { 48004653Sdougm ret = SMF_EXIT_OK; 48014653Sdougm } 48024653Sdougm if (state != NULL) 48034653Sdougm sa_free_attr_string(state); 48043034Sdougm } 48053034Sdougm optind++; 48064653Sdougm } 48074653Sdougm } else { 48085331Samw for (group = sa_get_group(handle, NULL); 48095331Samw group != NULL; 48104653Sdougm group = sa_get_next_group(group)) { 48113034Sdougm state = sa_get_group_attr(group, "state"); 48123034Sdougm if (state == NULL || strcmp(state, "enabled") == 0) 48135331Samw worklist = add_list(worklist, group, 0, 48145331Samw protocol); 48153034Sdougm if (state != NULL) 48164653Sdougm sa_free_attr_string(state); 48173034Sdougm } 48183034Sdougm } 48194653Sdougm (void) disable_all_groups(handle, worklist, 0); 48204653Sdougm ret = sa_update_config(handle); 48214653Sdougm 48223034Sdougm if (worklist != NULL) 48234653Sdougm free_list(worklist); 48243034Sdougm return (ret); 48253034Sdougm } 48263034Sdougm 48273034Sdougm /* 48283034Sdougm * remove_all_options(share, proto) 48293034Sdougm * 48303034Sdougm * Removes all options on a share. 48313034Sdougm */ 48323034Sdougm 48333034Sdougm static void 48343034Sdougm remove_all_options(sa_share_t share, char *proto) 48353034Sdougm { 48363034Sdougm sa_optionset_t optionset; 48373034Sdougm sa_security_t security; 48383034Sdougm sa_security_t prevsec = NULL; 48393034Sdougm 48403034Sdougm optionset = sa_get_optionset(share, proto); 48413034Sdougm if (optionset != NULL) 48424653Sdougm (void) sa_destroy_optionset(optionset); 48433034Sdougm for (security = sa_get_security(share, NULL, NULL); 48443034Sdougm security != NULL; 48453034Sdougm security = sa_get_next_security(security)) { 48464653Sdougm char *type; 48473034Sdougm /* 48484653Sdougm * We walk through the list. prevsec keeps the 48493034Sdougm * previous security so we can delete it without 48503034Sdougm * destroying the list. 48513034Sdougm */ 48524653Sdougm if (prevsec != NULL) { 48534653Sdougm /* remove the previously seen security */ 48544653Sdougm (void) sa_destroy_security(prevsec); 48554653Sdougm /* set to NULL so we don't try multiple times */ 48564653Sdougm prevsec = NULL; 48574653Sdougm } 48584653Sdougm type = sa_get_security_attr(security, "type"); 48594653Sdougm if (type != NULL) { 48604653Sdougm /* 48614653Sdougm * if the security matches the specified protocol, we 48624653Sdougm * want to remove it. prevsec holds it until either 48634653Sdougm * the next pass or we fall out of the loop. 48644653Sdougm */ 48654653Sdougm if (strcmp(type, proto) == 0) 48664653Sdougm prevsec = security; 48674653Sdougm sa_free_attr_string(type); 48684653Sdougm } 48693034Sdougm } 48703034Sdougm /* in case there is one left */ 48713034Sdougm if (prevsec != NULL) 48724653Sdougm (void) sa_destroy_security(prevsec); 48733034Sdougm } 48743034Sdougm 48753034Sdougm 48763034Sdougm /* 48773034Sdougm * for legacy support, we need to handle the old syntax. This is what 48783034Sdougm * we get if sharemgr is called with the name "share" rather than 48793034Sdougm * sharemgr. 48803034Sdougm */ 48813034Sdougm 48823034Sdougm static int 48833034Sdougm format_legacy_path(char *buff, int buffsize, char *proto, char *cmd) 48843034Sdougm { 48853034Sdougm int err; 48863034Sdougm 48873034Sdougm err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd); 48883034Sdougm if (err > buffsize) 48894653Sdougm return (-1); 48903034Sdougm return (0); 48913034Sdougm } 48923034Sdougm 48933034Sdougm 48943034Sdougm /* 48953034Sdougm * check_legacy_cmd(proto, cmd) 48963034Sdougm * 48973034Sdougm * Check to see if the cmd exists in /usr/lib/fs/<proto>/<cmd> and is 48983034Sdougm * executable. 48993034Sdougm */ 49003034Sdougm 49013034Sdougm static int 49023034Sdougm check_legacy_cmd(char *path) 49033034Sdougm { 49043034Sdougm struct stat st; 49053034Sdougm int ret = 0; 49063034Sdougm 49073034Sdougm if (stat(path, &st) == 0) { 49084653Sdougm if (S_ISREG(st.st_mode) && 49094653Sdougm st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) 49104653Sdougm ret = 1; 49113034Sdougm } 49123034Sdougm return (ret); 49133034Sdougm } 49143034Sdougm 49153034Sdougm /* 49163034Sdougm * run_legacy_command(proto, cmd, argv) 49173034Sdougm * 49184653Sdougm * We know the command exists, so attempt to execute it with all the 49193034Sdougm * arguments. This implements full legacy share support for those 49203034Sdougm * protocols that don't have plugin providers. 49213034Sdougm */ 49223034Sdougm 49233034Sdougm static int 49243034Sdougm run_legacy_command(char *path, char *argv[]) 49253034Sdougm { 49263034Sdougm int ret; 49273034Sdougm 49283034Sdougm ret = execv(path, argv); 49293034Sdougm if (ret < 0) { 49304653Sdougm switch (errno) { 49314653Sdougm case EACCES: 49324653Sdougm ret = SA_NO_PERMISSION; 49334653Sdougm break; 49344653Sdougm default: 49354653Sdougm ret = SA_SYSTEM_ERR; 49364653Sdougm break; 49374653Sdougm } 49383034Sdougm } 49393034Sdougm return (ret); 49403034Sdougm } 49413034Sdougm 49423034Sdougm /* 49433348Sdougm * out_share(out, group, proto) 49443034Sdougm * 49453034Sdougm * Display the share information in the format that the "share" 49463034Sdougm * command has traditionally used. 49473034Sdougm */ 49483034Sdougm 49493034Sdougm static void 49503348Sdougm out_share(FILE *out, sa_group_t group, char *proto) 49513034Sdougm { 49523034Sdougm sa_share_t share; 49533034Sdougm char resfmt[128]; 49545331Samw char *defprop; 49555331Samw 49565331Samw /* 49575331Samw * The original share command defaulted to displaying NFS 49585331Samw * shares or allowed a protocol to be specified. We want to 49595331Samw * skip those shares that are not the specified protocol. 49605331Samw */ 49615331Samw if (proto != NULL && sa_get_optionset(group, proto) == NULL) 49625331Samw return; 49635331Samw 49645331Samw if (proto == NULL) 49655331Samw proto = "nfs"; 49665331Samw 49675331Samw /* 49685331Samw * get the default property string. NFS uses "rw" but 49695331Samw * everything else will use "". 49705331Samw */ 49715331Samw if (proto != NULL && strcmp(proto, "nfs") != 0) 49725331Samw defprop = "\"\""; 49735331Samw else 49745331Samw defprop = "rw"; 49753034Sdougm 49764653Sdougm for (share = sa_get_share(group, NULL); 49774653Sdougm share != NULL; 49784653Sdougm share = sa_get_next_share(share)) { 49794653Sdougm char *path; 49804653Sdougm char *type; 49814653Sdougm char *resource; 49824653Sdougm char *description; 49834653Sdougm char *groupname; 49844653Sdougm char *sharedstate; 49854653Sdougm int shared = 1; 49864653Sdougm char *soptions; 49875331Samw char shareopts[MAXNAMLEN]; 49884653Sdougm 49894653Sdougm sharedstate = sa_get_share_attr(share, "shared"); 49904653Sdougm path = sa_get_share_attr(share, "path"); 49914653Sdougm type = sa_get_share_attr(share, "type"); 49925331Samw resource = get_resource(share); 49934653Sdougm groupname = sa_get_group_attr(group, "name"); 49944653Sdougm 49954653Sdougm if (groupname != NULL && strcmp(groupname, "default") == 0) { 49964653Sdougm sa_free_attr_string(groupname); 49974653Sdougm groupname = NULL; 49984653Sdougm } 49994653Sdougm description = sa_get_share_description(share); 50004653Sdougm 50015331Samw /* 50025331Samw * Want the sharetab version if it exists, defaulting 50035331Samw * to NFS if no protocol specified. 50045331Samw */ 50055331Samw (void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", proto); 50065331Samw soptions = sa_get_share_attr(share, shareopts); 50074653Sdougm 50084653Sdougm if (sharedstate == NULL) 50094653Sdougm shared = 0; 50104653Sdougm 50114653Sdougm if (soptions == NULL) 50124653Sdougm soptions = sa_proto_legacy_format(proto, share, 1); 50134653Sdougm 50144653Sdougm if (shared) { 50154653Sdougm /* only active shares go here */ 50164653Sdougm (void) snprintf(resfmt, sizeof (resfmt), "%s%s%s", 50174653Sdougm resource != NULL ? resource : "-", 50184653Sdougm groupname != NULL ? "@" : "", 50194653Sdougm groupname != NULL ? groupname : ""); 50204653Sdougm (void) fprintf(out, "%-14.14s %s %s \"%s\" \n", 50214653Sdougm resfmt, path, 50224653Sdougm (soptions != NULL && strlen(soptions) > 0) ? 50235331Samw soptions : defprop, 50244653Sdougm (description != NULL) ? description : ""); 50254653Sdougm } 50264653Sdougm 50274653Sdougm if (path != NULL) 50284653Sdougm sa_free_attr_string(path); 50294653Sdougm if (type != NULL) 50304653Sdougm sa_free_attr_string(type); 50314653Sdougm if (resource != NULL) 50324653Sdougm sa_free_attr_string(resource); 50334653Sdougm if (groupname != NULL) 50344653Sdougm sa_free_attr_string(groupname); 50354653Sdougm if (description != NULL) 50364653Sdougm sa_free_share_description(description); 50374653Sdougm if (sharedstate != NULL) 50384653Sdougm sa_free_attr_string(sharedstate); 50394653Sdougm if (soptions != NULL) 50404653Sdougm sa_format_free(soptions); 50413034Sdougm } 50423034Sdougm } 50433034Sdougm 50443034Sdougm /* 50453034Sdougm * output_legacy_file(out, proto) 50463034Sdougm * 50473034Sdougm * Walk all of the groups for the specified protocol and call 50483034Sdougm * out_share() to format and write in the format displayed by the 50493034Sdougm * "share" command with no arguments. 50503034Sdougm */ 50513034Sdougm 50523034Sdougm static void 50533910Sdougm output_legacy_file(FILE *out, char *proto, sa_handle_t handle) 50543034Sdougm { 50553034Sdougm sa_group_t group; 50563034Sdougm 50575331Samw for (group = sa_get_group(handle, NULL); 50585331Samw group != NULL; 50594653Sdougm group = sa_get_next_group(group)) { 50604653Sdougm char *zfs; 50613034Sdougm 50623034Sdougm /* 50635331Samw * Go through all the groups and ZFS 50645331Samw * sub-groups. out_share() will format the shares in 50655331Samw * the group appropriately. 50663034Sdougm */ 50673034Sdougm 50684653Sdougm zfs = sa_get_group_attr(group, "zfs"); 50694653Sdougm if (zfs != NULL) { 50704653Sdougm sa_group_t zgroup; 50714653Sdougm sa_free_attr_string(zfs); 50724653Sdougm for (zgroup = sa_get_sub_group(group); 50734653Sdougm zgroup != NULL; 50744653Sdougm zgroup = sa_get_next_group(zgroup)) { 50754653Sdougm 50764653Sdougm /* got a group, so display it */ 50774653Sdougm out_share(out, zgroup, proto); 50784653Sdougm } 50794653Sdougm } else { 50804653Sdougm out_share(out, group, proto); 50813034Sdougm } 50823034Sdougm } 50833034Sdougm } 50843034Sdougm 50853034Sdougm int 50863910Sdougm sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[]) 50873034Sdougm { 50883034Sdougm char *protocol = "nfs"; 50893034Sdougm char *options = NULL; 50903034Sdougm char *description = NULL; 50913034Sdougm char *groupname = NULL; 50923034Sdougm char *sharepath = NULL; 50933034Sdougm char *resource = NULL; 50943034Sdougm char *groupstatus = NULL; 50953034Sdougm int persist = SA_SHARE_TRANSIENT; 50963034Sdougm int argsused = 0; 50973034Sdougm int c; 50983034Sdougm int ret = SA_OK; 50993034Sdougm int zfs = 0; 51003034Sdougm int true_legacy = 0; 51013034Sdougm int curtype = SA_SHARE_TRANSIENT; 51023034Sdougm char cmd[MAXPATHLEN]; 51034653Sdougm sa_group_t group = NULL; 51045331Samw sa_resource_t rsrc = NULL; 51054653Sdougm sa_share_t share; 51064653Sdougm char dir[MAXPATHLEN]; 51075331Samw uint64_t features; 51085331Samw #ifdef lint 51095331Samw flags = flags; 51105331Samw #endif 51113034Sdougm 51123034Sdougm while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) { 51134653Sdougm switch (c) { 51144653Sdougm case 'd': 51154653Sdougm description = optarg; 51164653Sdougm argsused++; 51174653Sdougm break; 51184653Sdougm case 'F': 51194653Sdougm protocol = optarg; 51204653Sdougm if (!sa_valid_protocol(protocol)) { 51214653Sdougm if (format_legacy_path(cmd, MAXPATHLEN, 51224653Sdougm protocol, "share") == 0 && 51234653Sdougm check_legacy_cmd(cmd)) { 51244653Sdougm true_legacy++; 51254653Sdougm } else { 51264653Sdougm (void) fprintf(stderr, gettext( 51274653Sdougm "Invalid protocol specified: " 51284653Sdougm "%s\n"), protocol); 51294653Sdougm return (SA_INVALID_PROTOCOL); 51304653Sdougm } 51314653Sdougm } 51324653Sdougm break; 51334653Sdougm case 'o': 51344653Sdougm options = optarg; 51354653Sdougm argsused++; 51364653Sdougm break; 51374653Sdougm case 'p': 51384653Sdougm persist = SA_SHARE_PERMANENT; 51394653Sdougm argsused++; 51404653Sdougm break; 51414653Sdougm case 'h': 51424653Sdougm case '?': 51434653Sdougm default: 51444653Sdougm (void) fprintf(stderr, gettext("usage: %s\n"), 51454653Sdougm sa_get_usage(USAGE_SHARE)); 51464653Sdougm return (SA_OK); 51473034Sdougm } 51484653Sdougm } 51494653Sdougm 51504653Sdougm /* Have the info so construct what is needed */ 51514653Sdougm if (!argsused && optind == argc) { 51524653Sdougm /* display current info in share format */ 51535331Samw (void) output_legacy_file(stdout, protocol, handle); 51544653Sdougm return (ret); 51553034Sdougm } 51563034Sdougm 51574653Sdougm /* We are modifying the configuration */ 51584653Sdougm if (optind == argc) { 51593034Sdougm (void) fprintf(stderr, gettext("usage: %s\n"), 51604653Sdougm sa_get_usage(USAGE_SHARE)); 51613034Sdougm return (SA_LEGACY_ERR); 51624653Sdougm } 51634653Sdougm if (true_legacy) { 51644653Sdougm /* If still using legacy share/unshare, exec it */ 51653034Sdougm ret = run_legacy_command(cmd, argv); 51663034Sdougm return (ret); 51674653Sdougm } 51684653Sdougm 51694653Sdougm sharepath = argv[optind++]; 51704653Sdougm if (optind < argc) { 51713034Sdougm resource = argv[optind]; 51723034Sdougm groupname = strchr(resource, '@'); 51733034Sdougm if (groupname != NULL) 51744653Sdougm *groupname++ = '\0'; 51754653Sdougm } 51764653Sdougm if (realpath(sharepath, dir) == NULL) 51773034Sdougm ret = SA_BAD_PATH; 51784653Sdougm else 51793034Sdougm sharepath = dir; 51804653Sdougm if (ret == SA_OK) 51813910Sdougm share = sa_find_share(handle, sharepath); 51824653Sdougm else 51833034Sdougm share = NULL; 51844653Sdougm 51855331Samw features = sa_proto_get_featureset(protocol); 51865331Samw 51874653Sdougm if (groupname != NULL) { 51884653Sdougm ret = SA_NOT_ALLOWED; 51894653Sdougm } else if (ret == SA_OK) { 51905331Samw char *legacygroup; 51913034Sdougm /* 51924653Sdougm * The legacy group is always present and zfs groups 51933034Sdougm * come and go. zfs shares may be in sub-groups and 51943034Sdougm * the zfs share will already be in that group so it 51955331Samw * isn't an error. If the protocol is "smb", the group 51965331Samw * "smb" is used when "default" would otherwise be 51975331Samw * used. "default" is NFS only and "smb" is SMB only. 51983034Sdougm */ 51995331Samw if (strcmp(protocol, "smb") == 0) 52005331Samw legacygroup = "smb"; 52015331Samw else 52025331Samw legacygroup = "default"; 52035331Samw 52043034Sdougm /* 52054653Sdougm * If the share exists (not NULL), then make sure it 52064653Sdougm * is one we want to handle by getting the parent 52074653Sdougm * group. 52083034Sdougm */ 52095331Samw if (share != NULL) { 52104653Sdougm group = sa_get_parent_group(share); 52115331Samw } else { 52124653Sdougm group = sa_get_group(handle, legacygroup); 52135331Samw if (group == NULL && strcmp(legacygroup, "smb") == 0) { 52145331Samw /* 52155331Samw * This group may not exist, so create 52165331Samw * as necessary. It only contains the 52175331Samw * "smb" protocol. 52185331Samw */ 52195331Samw group = sa_create_group(handle, legacygroup, 52205331Samw &ret); 52215331Samw if (group != NULL) 52225331Samw (void) sa_create_optionset(group, 52235331Samw protocol); 52245331Samw } 52255331Samw } 52265331Samw 52275331Samw if (group == NULL) { 52285331Samw ret = SA_SYSTEM_ERR; 52295331Samw goto err; 52305331Samw } 52315331Samw 52325331Samw groupstatus = group_status(group); 52335331Samw if (share == NULL) { 52345331Samw share = sa_add_share(group, sharepath, 52355331Samw persist, &ret); 52365331Samw if (share == NULL && 52375331Samw ret == SA_DUPLICATE_NAME) { 52385331Samw /* 52395331Samw * Could be a ZFS path being started 52405331Samw */ 52415331Samw if (sa_zfs_is_shared(handle, 52425331Samw sharepath)) { 52435331Samw ret = SA_OK; 52445331Samw group = sa_get_group(handle, 52455331Samw "zfs"); 52465331Samw if (group == NULL) { 52475331Samw /* 52485331Samw * This shouldn't 52495331Samw * happen. 52505331Samw */ 52515331Samw ret = SA_CONFIG_ERR; 52525331Samw } else { 52535331Samw share = sa_add_share( 52545331Samw group, sharepath, 52555331Samw persist, &ret); 52564653Sdougm } 52573034Sdougm } 52585331Samw } 52595331Samw } else { 52605331Samw char *type; 52615331Samw /* 52625331Samw * May want to change persist state, but the 52635331Samw * important thing is to change options. We 52645331Samw * need to change them regardless of the 52655331Samw * source. 52665331Samw */ 52675331Samw 52685331Samw if (sa_zfs_is_shared(handle, sharepath)) { 52695331Samw zfs = 1; 52705331Samw } 52715331Samw remove_all_options(share, protocol); 52725331Samw type = sa_get_share_attr(share, "type"); 52735331Samw if (type != NULL && 52745331Samw strcmp(type, "transient") != 0) { 52755331Samw curtype = SA_SHARE_PERMANENT; 52765331Samw } 52775331Samw if (type != NULL) 52785331Samw sa_free_attr_string(type); 52795331Samw if (curtype != persist) { 52805331Samw (void) sa_set_share_attr(share, "type", 52815331Samw persist == SA_SHARE_PERMANENT ? 52825331Samw "persist" : "transient"); 52835331Samw } 52845331Samw } 52855331Samw 52865331Samw /* 52875331Samw * If there is a resource name, we may 52885331Samw * actually care about it if this is share for 52895331Samw * a protocol that uses resource level sharing 52905331Samw * (SMB). We need to find the resource and, if 52915331Samw * it exists, make sure it belongs to the 52925331Samw * current share. If it doesn't exist, attempt 52935331Samw * to create it. 52945331Samw */ 52955331Samw 52965331Samw if (ret == SA_OK && resource != NULL) { 52975331Samw rsrc = sa_find_resource(handle, resource); 52985331Samw if (rsrc != NULL) { 52995331Samw if (share != sa_get_resource_parent(rsrc)) 53005331Samw ret = SA_DUPLICATE_NAME; 53015331Samw } else { 53025331Samw rsrc = sa_add_resource(share, resource, 53035331Samw persist, &ret); 53043034Sdougm } 53055331Samw if (features & SA_FEATURE_RESOURCE) 53065331Samw share = rsrc; 53073108Sdougm } 53085331Samw 53094653Sdougm /* Have a group to hold this share path */ 53104653Sdougm if (ret == SA_OK && options != NULL && 53114653Sdougm strlen(options) > 0) { 53124653Sdougm ret = sa_parse_legacy_options(share, 53134653Sdougm options, 53144653Sdougm protocol); 53153034Sdougm } 53164653Sdougm if (!zfs) { 53174653Sdougm /* 53185331Samw * ZFS shares never have a description 53195331Samw * and we can't store the values so 53205331Samw * don't try. 53214653Sdougm */ 53224653Sdougm if (ret == SA_OK && description != NULL) 53234653Sdougm ret = sa_set_share_description(share, 53244653Sdougm description); 53253034Sdougm } 53265331Samw if (ret == SA_OK && 53275331Samw strcmp(groupstatus, "enabled") == 0) { 53285331Samw if (rsrc != share) 53294653Sdougm ret = sa_enable_share(share, protocol); 53305331Samw else 53315331Samw ret = sa_enable_resource(rsrc, 53325331Samw protocol); 53334653Sdougm if (ret == SA_OK && 53344653Sdougm persist == SA_SHARE_PERMANENT) { 53354653Sdougm (void) sa_update_legacy(share, 53364653Sdougm protocol); 53374653Sdougm } 53384653Sdougm if (ret == SA_OK) 53394653Sdougm ret = sa_update_config(handle); 53404653Sdougm } 53413034Sdougm } 53425331Samw err: 53433034Sdougm if (ret != SA_OK) { 53444653Sdougm (void) fprintf(stderr, gettext("Could not share: %s: %s\n"), 53454653Sdougm sharepath, sa_errorstr(ret)); 53464653Sdougm ret = SA_LEGACY_ERR; 53473034Sdougm } 53483034Sdougm return (ret); 53493034Sdougm } 53503034Sdougm 53513034Sdougm /* 53523034Sdougm * sa_legacy_unshare(flags, argc, argv) 53533034Sdougm * 53543034Sdougm * Implements the original unshare command. 53553034Sdougm */ 53563034Sdougm int 53573910Sdougm sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[]) 53583034Sdougm { 53593034Sdougm char *protocol = "nfs"; /* for now */ 53603034Sdougm char *options = NULL; 53613034Sdougm char *sharepath = NULL; 53623034Sdougm int persist = SA_SHARE_TRANSIENT; 53633034Sdougm int argsused = 0; 53643034Sdougm int c; 53653034Sdougm int ret = SA_OK; 53663034Sdougm int true_legacy = 0; 53675331Samw uint64_t features = 0; 53685331Samw sa_resource_t resource = NULL; 53693034Sdougm char cmd[MAXPATHLEN]; 53705331Samw #ifdef lint 53715331Samw flags = flags; 53725331Samw options = options; 53735331Samw #endif 53743034Sdougm 53753034Sdougm while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) { 53764653Sdougm switch (c) { 53774653Sdougm case 'h': 53784653Sdougm case '?': 53794653Sdougm break; 53804653Sdougm case 'F': 53814653Sdougm protocol = optarg; 53824653Sdougm if (!sa_valid_protocol(protocol)) { 53834653Sdougm if (format_legacy_path(cmd, MAXPATHLEN, 53844653Sdougm protocol, "unshare") == 0 && 53854653Sdougm check_legacy_cmd(cmd)) { 53864653Sdougm true_legacy++; 53874653Sdougm } else { 53884653Sdougm (void) printf(gettext( 53894653Sdougm "Invalid file system name\n")); 53904653Sdougm return (SA_INVALID_PROTOCOL); 53914653Sdougm } 53924653Sdougm } 53934653Sdougm break; 53944653Sdougm case 'o': 53954653Sdougm options = optarg; 53964653Sdougm argsused++; 53974653Sdougm break; 53984653Sdougm case 'p': 53994653Sdougm persist = SA_SHARE_PERMANENT; 54004653Sdougm argsused++; 54014653Sdougm break; 54024653Sdougm default: 54034653Sdougm (void) printf(gettext("usage: %s\n"), 54044653Sdougm sa_get_usage(USAGE_UNSHARE)); 54054653Sdougm return (SA_OK); 54063034Sdougm } 54073034Sdougm } 54083034Sdougm 54094653Sdougm /* Have the info so construct what is needed */ 54104653Sdougm if (optind == argc || (optind + 1) < argc || options != NULL) { 54114653Sdougm ret = SA_SYNTAX_ERR; 54123034Sdougm } else { 54134653Sdougm sa_share_t share; 54144653Sdougm char dir[MAXPATHLEN]; 54154653Sdougm if (true_legacy) { 54164653Sdougm /* if still using legacy share/unshare, exec it */ 54174653Sdougm ret = run_legacy_command(cmd, argv); 54184653Sdougm return (ret); 54194653Sdougm } 54203663Sdougm /* 54213663Sdougm * Find the path in the internal configuration. If it 54223663Sdougm * isn't found, attempt to resolve the path via 54233663Sdougm * realpath() and try again. 54243663Sdougm */ 54254653Sdougm sharepath = argv[optind++]; 54264653Sdougm share = sa_find_share(handle, sharepath); 54274653Sdougm if (share == NULL) { 54284653Sdougm if (realpath(sharepath, dir) == NULL) { 54294653Sdougm ret = SA_NO_SUCH_PATH; 54304653Sdougm } else { 54314653Sdougm share = sa_find_share(handle, dir); 54324653Sdougm } 54333663Sdougm } 54345331Samw if (share == NULL) { 54355331Samw /* Could be a resource name so check that next */ 54365331Samw features = sa_proto_get_featureset(protocol); 54375331Samw resource = sa_find_resource(handle, sharepath); 54385331Samw if (resource != NULL) { 54395331Samw share = sa_get_resource_parent(resource); 54405331Samw if (features & SA_FEATURE_RESOURCE) 54415331Samw (void) sa_disable_resource(resource, 54425331Samw protocol); 54435331Samw if (persist == SA_SHARE_PERMANENT) { 54445331Samw ret = sa_remove_resource(resource); 54455331Samw if (ret == SA_OK) 54465331Samw ret = sa_update_config(handle); 54475331Samw } 54485331Samw /* 54495331Samw * If we still have a resource on the 54505331Samw * share, we don't disable the share 54515331Samw * itself. IF there aren't anymore, we 54525331Samw * need to remove the share. The 54535331Samw * removal will be done in the next 54545331Samw * section if appropriate. 54555331Samw */ 54565331Samw resource = sa_get_share_resource(share, NULL); 54575331Samw if (resource != NULL) 54585331Samw share = NULL; 54595331Samw } else if (ret == SA_OK) { 54605331Samw /* Didn't find path and no resource */ 54615331Samw ret = SA_BAD_PATH; 54625331Samw } 54635331Samw } 54645331Samw if (share != NULL && resource == NULL) { 54654653Sdougm ret = sa_disable_share(share, protocol); 54664653Sdougm /* 54674653Sdougm * Errors are ok and removal should still occur. The 54684653Sdougm * legacy unshare is more forgiving of errors than the 54694653Sdougm * remove-share subcommand which may need the force 54704653Sdougm * flag set for some error conditions. That is, the 54714653Sdougm * "unshare" command will always unshare if it can 54724653Sdougm * while "remove-share" might require the force option. 54734653Sdougm */ 54744653Sdougm if (persist == SA_SHARE_PERMANENT) { 54754653Sdougm ret = sa_remove_share(share); 54764653Sdougm if (ret == SA_OK) 54774653Sdougm ret = sa_update_config(handle); 54784653Sdougm } 54795331Samw } else if (ret == SA_OK && share == NULL && resource == NULL) { 54805331Samw /* 54815331Samw * If both share and resource are NULL, then 54825331Samw * share not found. If one or the other was 54835331Samw * found or there was an earlier error, we 54845331Samw * assume it was handled earlier. 54855331Samw */ 54864653Sdougm ret = SA_NOT_SHARED; 54873663Sdougm } 54883034Sdougm } 54893034Sdougm switch (ret) { 54903034Sdougm default: 54914653Sdougm (void) printf("%s: %s\n", sharepath, sa_errorstr(ret)); 54924653Sdougm ret = SA_LEGACY_ERR; 54934653Sdougm break; 54943034Sdougm case SA_SYNTAX_ERR: 54954653Sdougm (void) printf(gettext("usage: %s\n"), 54964653Sdougm sa_get_usage(USAGE_UNSHARE)); 54974653Sdougm break; 54983034Sdougm case SA_OK: 54994653Sdougm break; 55003034Sdougm } 55013034Sdougm return (ret); 55023034Sdougm } 55033034Sdougm 55043034Sdougm /* 55054653Sdougm * Common commands that implement the sub-commands used by all 55065331Samw * protocols. The entries are found via the lookup command 55073034Sdougm */ 55083034Sdougm 55093034Sdougm static sa_command_t commands[] = { 55103034Sdougm {"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET}, 55113034Sdougm {"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION}, 55123034Sdougm {"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION}, 55133034Sdougm {"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION}, 55143034Sdougm {"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION}, 55153034Sdougm {"list", 0, sa_list, USAGE_LIST}, 55163034Sdougm {"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET}, 55173034Sdougm {"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET}, 55183034Sdougm {"set", 0, sa_set, USAGE_SET, SVC_SET}, 55193034Sdougm {"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET}, 55203034Sdougm {"show", 0, sa_show, USAGE_SHOW}, 55213034Sdougm {"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION}, 55223034Sdougm {"start", CMD_NODISPLAY, sa_start_group, USAGE_START, 55235331Samw SVC_SET|SVC_ACTION}, 55243034Sdougm {"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION}, 55253034Sdougm {"unset", 0, sa_unset, USAGE_UNSET, SVC_SET}, 55263034Sdougm {"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION}, 55273034Sdougm {NULL, 0, NULL, NULL} 55283034Sdougm }; 55293034Sdougm 55303034Sdougm static char * 55313034Sdougm sa_get_usage(sa_usage_t index) 55323034Sdougm { 55333034Sdougm char *ret = NULL; 55343034Sdougm switch (index) { 55353034Sdougm case USAGE_ADD_SHARE: 55364653Sdougm ret = gettext("add-share [-nth] [-r resource-name] " 55374653Sdougm "[-d \"description text\"] -s sharepath group"); 55384653Sdougm break; 55393034Sdougm case USAGE_CREATE: 55404653Sdougm ret = gettext( 55414653Sdougm "create [-nvh] [-P proto [-p property=value]] group"); 55424653Sdougm break; 55433034Sdougm case USAGE_DELETE: 55444653Sdougm ret = gettext("delete [-nvh] [-P proto] [-f] group"); 55454653Sdougm break; 55463034Sdougm case USAGE_DISABLE: 55474653Sdougm ret = gettext("disable [-nvh] {-a | group ...}"); 55484653Sdougm break; 55493034Sdougm case USAGE_ENABLE: 55504653Sdougm ret = gettext("enable [-nvh] {-a | group ...}"); 55514653Sdougm break; 55523034Sdougm case USAGE_LIST: 55534653Sdougm ret = gettext("list [-vh] [-P proto]"); 55544653Sdougm break; 55553034Sdougm case USAGE_MOVE_SHARE: 55564653Sdougm ret = gettext( 55574653Sdougm "move-share [-nvh] -s sharepath destination-group"); 55584653Sdougm break; 55593034Sdougm case USAGE_REMOVE_SHARE: 55605331Samw ret = gettext( 55615331Samw "remove-share [-fnvh] {-s sharepath | -r resource} " 55625331Samw "group"); 55634653Sdougm break; 55643034Sdougm case USAGE_SET: 55654653Sdougm ret = gettext("set [-nvh] -P proto [-S optspace] " 55665331Samw "[-p property=value]* [-s sharepath] [-r resource]] " 55675331Samw "group"); 55684653Sdougm break; 55693034Sdougm case USAGE_SET_SECURITY: 55704653Sdougm ret = gettext("set-security [-nvh] -P proto -S security-type " 55714653Sdougm "[-p property=value]* group"); 55724653Sdougm break; 55733034Sdougm case USAGE_SET_SHARE: 55744653Sdougm ret = gettext("set-share [-nh] [-r resource] " 55754653Sdougm "[-d \"description text\"] -s sharepath group"); 55764653Sdougm break; 55773034Sdougm case USAGE_SHOW: 55784653Sdougm ret = gettext("show [-pvxh] [-P proto] [group ...]"); 55794653Sdougm break; 55803034Sdougm case USAGE_SHARE: 55814653Sdougm ret = gettext("share [-F fstype] [-p] [-o optionlist]" 55824653Sdougm "[-d description] [pathname [resourcename]]"); 55834653Sdougm break; 55843034Sdougm case USAGE_START: 55854653Sdougm ret = gettext("start [-vh] [-P proto] {-a | group ...}"); 55864653Sdougm break; 55873034Sdougm case USAGE_STOP: 55884653Sdougm ret = gettext("stop [-vh] [-P proto] {-a | group ...}"); 55894653Sdougm break; 55903034Sdougm case USAGE_UNSET: 55914653Sdougm ret = gettext("unset [-nvh] -P proto [-S optspace] " 55924653Sdougm "[-p property]* group"); 55934653Sdougm break; 55943034Sdougm case USAGE_UNSET_SECURITY: 55955331Samw ret = gettext("unset-security [-nvh] -P proto " 55965331Samw "-S security-type [-p property]* group"); 55974653Sdougm break; 55983034Sdougm case USAGE_UNSHARE: 55994653Sdougm ret = gettext( 56005331Samw "unshare [-F fstype] [-p] [-o optionlist] sharepath"); 56014653Sdougm break; 56023034Sdougm } 56033034Sdougm return (ret); 56043034Sdougm } 56053034Sdougm 56063034Sdougm /* 56073034Sdougm * sa_lookup(cmd, proto) 56083034Sdougm * 56093034Sdougm * Lookup the sub-command. proto isn't currently used, but it may 56103034Sdougm * eventually provide a way to provide protocol specific sub-commands. 56113034Sdougm */ 56123034Sdougm sa_command_t * 56133034Sdougm sa_lookup(char *cmd, char *proto) 56143034Sdougm { 56153034Sdougm int i; 56163034Sdougm size_t len; 56175331Samw #ifdef lint 56185331Samw proto = proto; 56195331Samw #endif 56203034Sdougm 56213034Sdougm len = strlen(cmd); 56223034Sdougm for (i = 0; commands[i].cmdname != NULL; i++) { 56234653Sdougm if (strncmp(cmd, commands[i].cmdname, len) == 0) 56244653Sdougm return (&commands[i]); 56253034Sdougm } 56263034Sdougm return (NULL); 56273034Sdougm } 56283034Sdougm 56293034Sdougm void 56303034Sdougm sub_command_help(char *proto) 56313034Sdougm { 56323034Sdougm int i; 56335331Samw #ifdef lint 56345331Samw proto = proto; 56355331Samw #endif 56363034Sdougm 56373034Sdougm (void) printf(gettext("\tsub-commands:\n")); 56383034Sdougm for (i = 0; commands[i].cmdname != NULL; i++) { 56394653Sdougm if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY))) 56404653Sdougm (void) printf("\t%s\n", 56414653Sdougm sa_get_usage((sa_usage_t)commands[i].cmdidx)); 56423034Sdougm } 56433034Sdougm } 5644