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 219*5885Sdougm /* 220*5885Sdougm * print_rsrc_desc(resource, sharedesc) 221*5885Sdougm * 222*5885Sdougm * Print the resource description string after converting from UTF8 to 223*5885Sdougm * the current locale. If sharedesc is not NULL and there is no 224*5885Sdougm * description on the resource, use sharedesc. sharedesc will already 225*5885Sdougm * be converted to UTF8. 226*5885Sdougm */ 227*5885Sdougm 2285331Samw static void 229*5885Sdougm print_rsrc_desc(sa_resource_t resource, char *sharedesc) 2305331Samw { 2315331Samw char *description; 2325331Samw char *desc; 2335331Samw 234*5885Sdougm if (resource == NULL) 235*5885Sdougm return; 236*5885Sdougm 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 } 244*5885Sdougm } else if (sharedesc != NULL) { 245*5885Sdougm description = strdup(sharedesc); 246*5885Sdougm } 247*5885Sdougm if (description != NULL) { 2485331Samw (void) printf("\t\"%s\"", description); 2495331Samw sa_free_share_description(description); 2505331Samw } 2515331Samw } 2525331Samw 253*5885Sdougm /* 254*5885Sdougm * set_resource_desc(share, description) 255*5885Sdougm * 256*5885Sdougm * Set the share description value after converting the description 257*5885Sdougm * string to UTF8 from the current locale. 258*5885Sdougm */ 259*5885Sdougm 260*5885Sdougm static int 261*5885Sdougm set_resource_desc(sa_share_t share, char *description) 262*5885Sdougm { 263*5885Sdougm char *desc; 264*5885Sdougm int ret; 265*5885Sdougm 266*5885Sdougm desc = conv_to_utf8(description); 267*5885Sdougm ret = sa_set_resource_description(share, desc); 268*5885Sdougm if (description != desc) 269*5885Sdougm sa_free_share_description(desc); 270*5885Sdougm return (ret); 271*5885Sdougm } 272*5885Sdougm 273*5885Sdougm /* 274*5885Sdougm * set_share_desc(share, description) 275*5885Sdougm * 276*5885Sdougm * Set the resource description value after converting the description 277*5885Sdougm * string to UTF8 from the current locale. 278*5885Sdougm */ 279*5885Sdougm 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 1621*5885Sdougm if (optind != argc) { 1622*5885Sdougm (void) printf(gettext("usage: %s\n"), 1623*5885Sdougm sa_get_usage(USAGE_LIST)); 1624*5885Sdougm return (SA_SYNTAX_ERR); 1625*5885Sdougm } 1626*5885Sdougm 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 * 1849*5885Sdougm * Check to see if the share has multiple resource names since 1850*5885Sdougm * the syntax would be about the same. 18515331Samw */ 1852*5885Sdougm 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; 1886*5885Sdougm 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 1909*5885Sdougm /* 1910*5885Sdougm * if there is a description on the share and there 1911*5885Sdougm * are resources, treat as multiple resources in order 1912*5885Sdougm * to get all descriptions displayed. 1913*5885Sdougm */ 1914*5885Sdougm description = sa_get_share_description(share); 1915*5885Sdougm resource = sa_get_share_resource(share, NULL); 1916*5885Sdougm 1917*5885Sdougm if (description != NULL && resource != NULL) 1918*5885Sdougm multiple = B_TRUE; 1919*5885Sdougm 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); 1930*5885Sdougm /* Print the description string if there is one. */ 1931*5885Sdougm 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 } 1951*5885Sdougm 19525331Samw if (description != NULL) { 1953*5885Sdougm 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 */ 1990*5885Sdougm print_rsrc_desc(resource, description); 19915331Samw } 19925331Samw } 1993*5885Sdougm if (description != NULL) 1994*5885Sdougm 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) { 2543*5885Sdougm if (resource != NULL) 2544*5885Sdougm ret = 2545*5885Sdougm set_resource_desc( 2546*5885Sdougm resource, 2547*5885Sdougm description); 2548*5885Sdougm 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; 2629*5885Sdougm 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); 2702*5885Sdougm /* 2703*5885Sdougm * If a share wasn't found, it may have been a symlink 2704*5885Sdougm * or has a trailing '/'. Try again after resolving 2705*5885Sdougm * with realpath(). 2706*5885Sdougm */ 2707*5885Sdougm if (share == NULL) { 2708*5885Sdougm if (realpath(sharepath, dir) == NULL) { 2709*5885Sdougm (void) printf(gettext("Path " 2710*5885Sdougm "is not valid: %s\n"), 2711*5885Sdougm sharepath); 2712*5885Sdougm return (SA_BAD_PATH); 2713*5885Sdougm } 2714*5885Sdougm sharepath = dir; 2715*5885Sdougm share = sa_find_share(handle, sharepath); 2716*5885Sdougm } 27174653Sdougm if (share == NULL) { 27183034Sdougm (void) printf(gettext("Share not found: %s\n"), 27194653Sdougm sharepath); 27204653Sdougm return (SA_NO_SUCH_PATH); 27214653Sdougm } 2722*5885Sdougm 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 */ 32325331Samw if (ret == SA_OK && description != NULL) { 32335331Samw if (resource != NULL) 3234*5885Sdougm ret = set_resource_desc(resource, 3235*5885Sdougm description); 32365331Samw else 3237*5885Sdougm ret = set_share_desc(share, 3238*5885Sdougm description); 32395331Samw } 32405331Samw } 32415331Samw if (!dryrun && ret == SA_OK) { 32425331Samw if (resource != NULL) 32435331Samw (void) sa_enable_resource(resource, NULL); 32445331Samw ret = sa_update_config(handle); 32455331Samw } 32465331Samw switch (ret) { 32475331Samw case SA_DUPLICATE_NAME: 32485331Samw (void) printf(gettext("Resource name in use: %s\n"), 32495331Samw rsrcname); 32505331Samw break; 32515331Samw default: 32525331Samw (void) printf(gettext("Could not set: %s\n"), 32535331Samw sa_errorstr(ret)); 32545331Samw break; 32555331Samw case SA_OK: 32565331Samw if (dryrun && !auth && verbose) { 32575331Samw (void) printf(gettext( 32585331Samw "Command would fail: %s\n"), 32595331Samw sa_errorstr(SA_NO_PERMISSION)); 32605331Samw } 32615331Samw break; 32625331Samw } 32635331Samw } else { 32645772Sas200622 switch (ret) { 32655772Sas200622 case SA_NO_SUCH_RESOURCE: 32665772Sas200622 (void) printf(gettext("Resource \"%s\" not found\n"), 32675772Sas200622 rsrcname); 32685772Sas200622 break; 32695772Sas200622 default: 32705772Sas200622 if (sharepath != NULL) { 32715772Sas200622 (void) printf( 32725772Sas200622 gettext("Share path \"%s\" not found\n"), 32735772Sas200622 sharepath); 32745772Sas200622 ret = SA_NO_SUCH_PATH; 32755772Sas200622 } else { 32765772Sas200622 (void) printf(gettext("Set failed: %s\n"), 32775772Sas200622 sa_errorstr(ret)); 32785772Sas200622 } 32795772Sas200622 } 32803034Sdougm } 32814653Sdougm 32823034Sdougm return (ret); 32833034Sdougm } 32843034Sdougm 32853034Sdougm /* 32863034Sdougm * add_security(group, sectype, optlist, proto, *err) 32873034Sdougm * 32883034Sdougm * Helper function to add a security option (named optionset) to the 32893034Sdougm * group. 32903034Sdougm */ 32913034Sdougm 32923034Sdougm static int 32933034Sdougm add_security(sa_group_t group, char *sectype, 32945331Samw struct options *optlist, char *proto, int *err) 32953034Sdougm { 32963034Sdougm sa_security_t security; 32973034Sdougm int ret = SA_OK; 32983034Sdougm int result = 0; 32993034Sdougm 33003034Sdougm sectype = sa_proto_space_alias(proto, sectype); 33013034Sdougm security = sa_get_security(group, sectype, proto); 33024653Sdougm if (security == NULL) 33034653Sdougm security = sa_create_security(group, sectype, proto); 33044653Sdougm 33053034Sdougm if (sectype != NULL) 33064653Sdougm sa_free_attr_string(sectype); 33074653Sdougm 33084653Sdougm if (security == NULL) 33094653Sdougm return (ret); 33104653Sdougm 33114653Sdougm while (optlist != NULL) { 33123034Sdougm sa_property_t prop; 33133034Sdougm prop = sa_get_property(security, optlist->optname); 33143034Sdougm if (prop == NULL) { 33153034Sdougm /* 33164653Sdougm * Add the property, but only if it is 33173034Sdougm * a non-NULL or non-zero length value 33183034Sdougm */ 33194653Sdougm if (optlist->optvalue != NULL) { 33204653Sdougm prop = sa_create_property(optlist->optname, 33214653Sdougm optlist->optvalue); 33224653Sdougm if (prop != NULL) { 33235331Samw ret = sa_valid_property(security, 33245331Samw proto, prop); 33254653Sdougm if (ret != SA_OK) { 33264653Sdougm (void) sa_remove_property(prop); 33274653Sdougm (void) printf(gettext( 33284653Sdougm "Could not add " 33294653Sdougm "property %s: %s\n"), 33304653Sdougm optlist->optname, 33314653Sdougm sa_errorstr(ret)); 33324653Sdougm } 33334653Sdougm if (ret == SA_OK) { 33344653Sdougm ret = sa_add_property(security, 33354653Sdougm prop); 33364653Sdougm if (ret != SA_OK) { 33374653Sdougm (void) printf(gettext( 33384653Sdougm "Could not add " 33395331Samw "property (%s=%s):" 33405331Samw " %s\n"), 33414653Sdougm optlist->optname, 33424653Sdougm optlist->optvalue, 33434653Sdougm sa_errorstr(ret)); 33444653Sdougm } else { 33454653Sdougm result = 1; 33464653Sdougm } 33474653Sdougm } 33483034Sdougm } 33493034Sdougm } 33503034Sdougm } else { 33514653Sdougm ret = sa_update_property(prop, optlist->optvalue); 33524653Sdougm result = 1; /* should check if really changed */ 33533034Sdougm } 33543034Sdougm optlist = optlist->next; 33554653Sdougm } 33564653Sdougm /* 33574653Sdougm * When done, properties may have all been removed but 33584653Sdougm * we need to keep the security type itself until 33594653Sdougm * explicitly removed. 33604653Sdougm */ 33614653Sdougm if (result) 33623034Sdougm ret = sa_commit_properties(security, 0); 33633034Sdougm *err = ret; 33643034Sdougm return (result); 33653034Sdougm } 33663034Sdougm 33673034Sdougm /* 33685089Sdougm * zfscheck(group, share) 33695089Sdougm * 33705089Sdougm * For the special case where a share was provided, make sure it is a 33715089Sdougm * compatible path for a ZFS property change. The only path 33725089Sdougm * acceptable is the path that defines the zfs sub-group (dataset with 33735089Sdougm * the sharenfs property set) and not one of the paths that inherited 33745089Sdougm * the NFS properties. Returns SA_OK if it is usable and 33755089Sdougm * SA_NOT_ALLOWED if it isn't. 33765089Sdougm * 33775089Sdougm * If group is not a ZFS group/subgroup, we assume OK since the check 33785089Sdougm * on return will catch errors for those cases. What we are looking 33795089Sdougm * for here is that the group is ZFS and the share is not the defining 33805089Sdougm * share. All else is SA_OK. 33815089Sdougm */ 33825089Sdougm 33835089Sdougm static int 33845089Sdougm zfscheck(sa_group_t group, sa_share_t share) 33855089Sdougm { 33865089Sdougm int ret = SA_OK; 33875089Sdougm char *attr; 33885089Sdougm 33895089Sdougm if (sa_group_is_zfs(group)) { 33905089Sdougm /* 33915089Sdougm * The group is a ZFS group. Does the share represent 33925089Sdougm * the dataset that defined the group? It is only OK 33935089Sdougm * if the attribute "subgroup" exists on the share and 33945089Sdougm * has a value of "true". 33955089Sdougm */ 33965089Sdougm 33975089Sdougm ret = SA_NOT_ALLOWED; 33985089Sdougm attr = sa_get_share_attr(share, "subgroup"); 33995089Sdougm if (attr != NULL) { 34005089Sdougm if (strcmp(attr, "true") == 0) 34015089Sdougm ret = SA_OK; 34025089Sdougm sa_free_attr_string(attr); 34035089Sdougm } 34045089Sdougm } 34055089Sdougm return (ret); 34065089Sdougm } 34075089Sdougm 34085089Sdougm /* 34095331Samw * basic_set(groupname, optlist, protocol, sharepath, rsrcname, dryrun) 34103034Sdougm * 34113034Sdougm * This function implements "set" when a name space (-S) is not 34123034Sdougm * specified. It is a basic set. Options and other CLI parsing has 34133034Sdougm * already been done. 34145331Samw * 34155331Samw * "rsrcname" is a "resource name". If it is non-NULL, it must match 34165331Samw * the sharepath if present or group if present, otherwise it is used 34175331Samw * to set options. 34185331Samw * 34195331Samw * Resource names may take options if the protocol supports it. If the 34205331Samw * protocol doesn't support resource level options, rsrcname is just 34215331Samw * an alias for the share. 34223034Sdougm */ 34233034Sdougm 34243034Sdougm static int 34253910Sdougm basic_set(sa_handle_t handle, char *groupname, struct options *optlist, 34265331Samw char *protocol, char *sharepath, char *rsrcname, int dryrun) 34273034Sdougm { 34283034Sdougm sa_group_t group; 34293034Sdougm int ret = SA_OK; 34303034Sdougm int change = 0; 34313034Sdougm struct list *worklist = NULL; 34323034Sdougm 34333910Sdougm group = sa_get_group(handle, groupname); 34343034Sdougm if (group != NULL) { 34354653Sdougm sa_share_t share = NULL; 34365331Samw sa_resource_t resource = NULL; 34375331Samw 34385331Samw /* 34395331Samw * If there is a sharepath, make sure it belongs to 34405331Samw * the group. 34415331Samw */ 34424653Sdougm if (sharepath != NULL) { 34434653Sdougm share = sa_get_share(group, sharepath); 34444653Sdougm if (share == NULL) { 34454653Sdougm (void) printf(gettext( 34464653Sdougm "Share does not exist in group %s\n"), 34474653Sdougm groupname, sharepath); 34484653Sdougm ret = SA_NO_SUCH_PATH; 34495089Sdougm } else { 34505089Sdougm /* if ZFS and OK, then only group */ 34515089Sdougm ret = zfscheck(group, share); 34525089Sdougm if (ret == SA_OK && 34535089Sdougm sa_group_is_zfs(group)) 34545089Sdougm share = NULL; 34555089Sdougm if (ret == SA_NOT_ALLOWED) 34565089Sdougm (void) printf(gettext( 34575089Sdougm "Properties on ZFS group shares " 34585089Sdougm "not supported: %s\n"), sharepath); 34594653Sdougm } 34603034Sdougm } 34615331Samw 34625331Samw /* 34635331Samw * If a resource name exists, make sure it belongs to 34645331Samw * the share if present else it belongs to the 34655331Samw * group. Also check the protocol to see if it 34665331Samw * supports resource level properties or not. If not, 34675331Samw * use share only. 34685331Samw */ 34695331Samw if (rsrcname != NULL) { 34705331Samw if (share != NULL) { 34715331Samw resource = sa_get_share_resource(share, 34725331Samw rsrcname); 34735331Samw if (resource == NULL) 34745331Samw ret = SA_NO_SUCH_RESOURCE; 34755331Samw } else { 34765331Samw resource = sa_get_resource(group, rsrcname); 34775331Samw if (resource != NULL) 34785331Samw share = sa_get_resource_parent( 34795331Samw resource); 34805331Samw else 34815331Samw ret = SA_NO_SUCH_RESOURCE; 34825331Samw } 34835331Samw if (ret == SA_OK && resource != NULL) { 34845331Samw uint64_t features; 34855331Samw /* 34865331Samw * Check to see if the resource can take 34875331Samw * properties. If so, stick the resource into 34885331Samw * "share" so it will all just work. 34895331Samw */ 34905331Samw features = sa_proto_get_featureset(protocol); 34915331Samw if (features & SA_FEATURE_RESOURCE) 34925331Samw share = (sa_share_t)resource; 34935331Samw } 34945331Samw } 34955331Samw 34964653Sdougm if (ret == SA_OK) { 34974653Sdougm /* group must exist */ 34984653Sdougm ret = valid_options(optlist, protocol, 34994653Sdougm share == NULL ? group : share, NULL); 35004653Sdougm if (ret == SA_OK && !dryrun) { 35014653Sdougm if (share != NULL) 35024653Sdougm change |= add_optionset(share, optlist, 35034653Sdougm protocol, &ret); 35044653Sdougm else 35054653Sdougm change |= add_optionset(group, optlist, 35064653Sdougm protocol, &ret); 35074653Sdougm if (ret == SA_OK && change) 35084653Sdougm worklist = add_list(worklist, group, 35095331Samw share, protocol); 35104653Sdougm } 35113034Sdougm } 35124653Sdougm free_opt(optlist); 35133034Sdougm } else { 35143034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 35153034Sdougm ret = SA_NO_SUCH_GROUP; 35163034Sdougm } 35173034Sdougm /* 35183034Sdougm * we have a group and potentially legal additions 35193034Sdougm */ 35203034Sdougm 35214653Sdougm /* 35224653Sdougm * Commit to configuration if not a dryrunp and properties 35234653Sdougm * have changed. 35244653Sdougm */ 35254653Sdougm if (!dryrun && ret == SA_OK && change && worklist != NULL) 35263034Sdougm /* properties changed, so update all shares */ 35275331Samw (void) enable_all_groups(handle, worklist, 0, 0, protocol, 35285331Samw B_TRUE); 35294653Sdougm 35303034Sdougm if (worklist != NULL) 35314653Sdougm free_list(worklist); 35323034Sdougm return (ret); 35333034Sdougm } 35343034Sdougm 35353034Sdougm /* 35363034Sdougm * space_set(groupname, optlist, protocol, sharepath, dryrun) 35373034Sdougm * 35383034Sdougm * This function implements "set" when a name space (-S) is 35393034Sdougm * specified. It is a namespace set. Options and other CLI parsing has 35403034Sdougm * already been done. 35413034Sdougm */ 35423034Sdougm 35433034Sdougm static int 35443910Sdougm space_set(sa_handle_t handle, char *groupname, struct options *optlist, 35455331Samw char *protocol, char *sharepath, int dryrun, char *sectype) 35463034Sdougm { 35473034Sdougm sa_group_t group; 35483034Sdougm int ret = SA_OK; 35493034Sdougm int change = 0; 35503034Sdougm struct list *worklist = NULL; 35513034Sdougm 35523034Sdougm /* 35533034Sdougm * make sure protcol and sectype are valid 35543034Sdougm */ 35553034Sdougm 35563034Sdougm if (sa_proto_valid_space(protocol, sectype) == 0) { 35574653Sdougm (void) printf(gettext("Option space \"%s\" not valid " 35584653Sdougm "for protocol.\n"), sectype); 35594653Sdougm return (SA_INVALID_SECURITY); 35603034Sdougm } 35613034Sdougm 35623910Sdougm group = sa_get_group(handle, groupname); 35633034Sdougm if (group != NULL) { 35644653Sdougm sa_share_t share = NULL; 35654653Sdougm if (sharepath != NULL) { 35664653Sdougm share = sa_get_share(group, sharepath); 35674653Sdougm if (share == NULL) { 35684653Sdougm (void) printf(gettext( 35694653Sdougm "Share does not exist in group %s\n"), 35704653Sdougm groupname, sharepath); 35714653Sdougm ret = SA_NO_SUCH_PATH; 35725089Sdougm } else { 35735089Sdougm /* if ZFS and OK, then only group */ 35745089Sdougm ret = zfscheck(group, share); 35755089Sdougm if (ret == SA_OK && 35765089Sdougm sa_group_is_zfs(group)) 35775089Sdougm share = NULL; 35785089Sdougm if (ret == SA_NOT_ALLOWED) 35795089Sdougm (void) printf(gettext( 35805089Sdougm "Properties on ZFS group shares " 35815089Sdougm "not supported: %s\n"), sharepath); 35824653Sdougm } 35833034Sdougm } 35844653Sdougm if (ret == SA_OK) { 35854653Sdougm /* group must exist */ 35864653Sdougm ret = valid_options(optlist, protocol, 35874653Sdougm share == NULL ? group : share, sectype); 35884653Sdougm if (ret == SA_OK && !dryrun) { 35894653Sdougm if (share != NULL) 35904653Sdougm change = add_security(share, sectype, 35914653Sdougm optlist, protocol, &ret); 35924653Sdougm else 35934653Sdougm change = add_security(group, sectype, 35944653Sdougm optlist, protocol, &ret); 35954653Sdougm if (ret != SA_OK) 35964653Sdougm (void) printf(gettext( 35974653Sdougm "Could not set property: %s\n"), 35984653Sdougm sa_errorstr(ret)); 35994653Sdougm } 36004653Sdougm if (ret == SA_OK && change) 36015331Samw worklist = add_list(worklist, group, share, 36025331Samw protocol); 36033034Sdougm } 36044653Sdougm free_opt(optlist); 36053034Sdougm } else { 36063034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 36073034Sdougm ret = SA_NO_SUCH_GROUP; 36083034Sdougm } 36095331Samw 36103034Sdougm /* 36115331Samw * We have a group and potentially legal additions. 36123034Sdougm */ 36133034Sdougm 36144653Sdougm /* Commit to configuration if not a dryrun */ 36153034Sdougm if (!dryrun && ret == 0) { 36164653Sdougm if (change && worklist != NULL) { 36174653Sdougm /* properties changed, so update all shares */ 36184653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 36195331Samw protocol, B_TRUE); 36204653Sdougm } 36214653Sdougm ret = sa_update_config(handle); 36223034Sdougm } 36233034Sdougm if (worklist != NULL) 36244653Sdougm free_list(worklist); 36253034Sdougm return (ret); 36263034Sdougm } 36273034Sdougm 36283034Sdougm /* 36293034Sdougm * sa_set(flags, argc, argv) 36303034Sdougm * 36313034Sdougm * Implements the set subcommand. It keys off of -S to determine which 36323034Sdougm * set of operations to actually do. 36333034Sdougm */ 36343034Sdougm 36353034Sdougm int 36363910Sdougm sa_set(sa_handle_t handle, int flags, int argc, char *argv[]) 36373034Sdougm { 36383034Sdougm char *groupname; 36393034Sdougm int verbose = 0; 36403034Sdougm int dryrun = 0; 36413034Sdougm int c; 36423034Sdougm char *protocol = NULL; 36433034Sdougm int ret = SA_OK; 36443034Sdougm struct options *optlist = NULL; 36455331Samw char *rsrcname = NULL; 36463034Sdougm char *sharepath = NULL; 36473034Sdougm char *optset = NULL; 36483034Sdougm int auth; 36493034Sdougm 36505331Samw while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) { 36514653Sdougm switch (c) { 36524653Sdougm case 'v': 36534653Sdougm verbose++; 36544653Sdougm break; 36554653Sdougm case 'n': 36564653Sdougm dryrun++; 36574653Sdougm break; 36584653Sdougm case 'P': 36595331Samw if (protocol != NULL) { 36605331Samw (void) printf(gettext( 36615331Samw "Specifying multiple protocols " 36625331Samw "not supported: %s\n"), protocol); 36635331Samw return (SA_SYNTAX_ERR); 36645331Samw } 36654653Sdougm protocol = optarg; 36664653Sdougm if (!sa_valid_protocol(protocol)) { 36674653Sdougm (void) printf(gettext( 36684653Sdougm "Invalid protocol specified: %s\n"), 36694653Sdougm protocol); 36704653Sdougm return (SA_INVALID_PROTOCOL); 36714653Sdougm } 36724653Sdougm break; 36734653Sdougm case 'p': 36744653Sdougm ret = add_opt(&optlist, optarg, 0); 36754653Sdougm switch (ret) { 36764653Sdougm case OPT_ADD_SYNTAX: 36774653Sdougm (void) printf(gettext("Property syntax error:" 36784653Sdougm " %s\n"), optarg); 36794653Sdougm return (SA_SYNTAX_ERR); 36804653Sdougm case OPT_ADD_MEMORY: 36814653Sdougm (void) printf(gettext("No memory to set " 36824653Sdougm "property: %s\n"), optarg); 36834653Sdougm return (SA_NO_MEMORY); 36844653Sdougm default: 36854653Sdougm break; 36864653Sdougm } 36874653Sdougm break; 36885331Samw case 'r': 36895331Samw if (rsrcname != NULL) { 36905331Samw (void) printf(gettext( 36915331Samw "Setting multiple resource names not" 36925331Samw " supported\n")); 36935331Samw return (SA_SYNTAX_ERR); 36945331Samw } 36955331Samw rsrcname = optarg; 36965331Samw break; 36974653Sdougm case 's': 36985331Samw if (sharepath != NULL) { 36995331Samw (void) printf(gettext( 37005331Samw "Setting multiple shares not supported\n")); 37015331Samw return (SA_SYNTAX_ERR); 37025331Samw } 37034653Sdougm sharepath = optarg; 37044653Sdougm break; 37054653Sdougm case 'S': 37065331Samw if (optset != NULL) { 37075331Samw (void) printf(gettext( 37085331Samw "Specifying multiple property " 37095331Samw "spaces not supported: %s\n"), optset); 37105331Samw return (SA_SYNTAX_ERR); 37115331Samw } 37124653Sdougm optset = optarg; 37134653Sdougm break; 37144653Sdougm default: 37154653Sdougm case 'h': 37164653Sdougm case '?': 37174653Sdougm (void) printf(gettext("usage: %s\n"), 37184653Sdougm sa_get_usage(USAGE_SET)); 37194653Sdougm return (SA_OK); 37203034Sdougm } 37213034Sdougm } 37223034Sdougm 37233034Sdougm if (optlist != NULL) 37244653Sdougm ret = chk_opt(optlist, optset != NULL, protocol); 37253034Sdougm 37263034Sdougm if (optind >= argc || (optlist == NULL && optset == NULL) || 37274653Sdougm protocol == NULL || ret != OPT_ADD_OK) { 37284653Sdougm char *sep = "\t"; 37294653Sdougm 37304653Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET)); 37314653Sdougm if (optind >= argc) { 37324653Sdougm (void) printf(gettext("%sgroup must be specified"), 37334653Sdougm sep); 37344653Sdougm sep = ", "; 37354653Sdougm } 37364653Sdougm if (optlist == NULL) { 37374653Sdougm (void) printf(gettext("%sat least one property must be" 37384653Sdougm " specified"), sep); 37394653Sdougm sep = ", "; 37404653Sdougm } 37414653Sdougm if (protocol == NULL) { 37424653Sdougm (void) printf(gettext("%sprotocol must be specified"), 37434653Sdougm sep); 37444653Sdougm sep = ", "; 37454653Sdougm } 37464653Sdougm (void) printf("\n"); 37474653Sdougm ret = SA_SYNTAX_ERR; 37483034Sdougm } else { 37493034Sdougm /* 37505089Sdougm * Group already exists so we can proceed after a few 37515089Sdougm * additional checks related to ZFS handling. 37523034Sdougm */ 37533034Sdougm 37544653Sdougm groupname = argv[optind]; 37555089Sdougm if (strcmp(groupname, "zfs") == 0) { 37565089Sdougm (void) printf(gettext("Changing properties for group " 37575089Sdougm "\"zfs\" not allowed\n")); 37585089Sdougm return (SA_NOT_ALLOWED); 37595089Sdougm } 37605089Sdougm 37614653Sdougm auth = check_authorizations(groupname, flags); 37624653Sdougm if (optset == NULL) 37634653Sdougm ret = basic_set(handle, groupname, optlist, protocol, 37645331Samw sharepath, rsrcname, dryrun); 37654653Sdougm else 37664653Sdougm ret = space_set(handle, groupname, optlist, protocol, 37674653Sdougm sharepath, dryrun, optset); 37684653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 37694653Sdougm (void) printf(gettext("Command would fail: %s\n"), 37704653Sdougm sa_errorstr(SA_NO_PERMISSION)); 37714653Sdougm } 37723034Sdougm } 37733034Sdougm return (ret); 37743034Sdougm } 37753034Sdougm 37763034Sdougm /* 37773034Sdougm * remove_options(group, optlist, proto, *err) 37783034Sdougm * 37794653Sdougm * Helper function to actually remove options from a group after all 37803034Sdougm * preprocessing is done. 37813034Sdougm */ 37823034Sdougm 37833034Sdougm static int 37843034Sdougm remove_options(sa_group_t group, struct options *optlist, 37855331Samw char *proto, int *err) 37863034Sdougm { 37873034Sdougm struct options *cur; 37883034Sdougm sa_optionset_t optionset; 37893034Sdougm sa_property_t prop; 37903034Sdougm int change = 0; 37913034Sdougm int ret = SA_OK; 37923034Sdougm 37933034Sdougm optionset = sa_get_optionset(group, proto); 37943034Sdougm if (optionset != NULL) { 37954653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 37964653Sdougm prop = sa_get_property(optionset, cur->optname); 37974653Sdougm if (prop != NULL) { 37984653Sdougm ret = sa_remove_property(prop); 37994653Sdougm if (ret != SA_OK) 38004653Sdougm break; 38014653Sdougm change = 1; 38024653Sdougm } 38033034Sdougm } 38043034Sdougm } 38053034Sdougm if (ret == SA_OK && change) 38064653Sdougm ret = sa_commit_properties(optionset, 0); 38073034Sdougm 38083034Sdougm if (err != NULL) 38094653Sdougm *err = ret; 38103034Sdougm return (change); 38113034Sdougm } 38123034Sdougm 38133034Sdougm /* 38143034Sdougm * valid_unset(group, optlist, proto) 38153034Sdougm * 38163034Sdougm * Sanity check the optlist to make sure they can be removed. Issue an 38173034Sdougm * error if a property doesn't exist. 38183034Sdougm */ 38193034Sdougm 38203034Sdougm static int 38213034Sdougm valid_unset(sa_group_t group, struct options *optlist, char *proto) 38223034Sdougm { 38233034Sdougm struct options *cur; 38243034Sdougm sa_optionset_t optionset; 38253034Sdougm sa_property_t prop; 38263034Sdougm int ret = SA_OK; 38273034Sdougm 38283034Sdougm optionset = sa_get_optionset(group, proto); 38293034Sdougm if (optionset != NULL) { 38304653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 38314653Sdougm prop = sa_get_property(optionset, cur->optname); 38324653Sdougm if (prop == NULL) { 38334653Sdougm (void) printf(gettext( 38344653Sdougm "Could not unset property %s: not set\n"), 38354653Sdougm cur->optname); 38364653Sdougm ret = SA_NO_SUCH_PROP; 38374653Sdougm } 38383034Sdougm } 38393034Sdougm } 38403034Sdougm return (ret); 38413034Sdougm } 38423034Sdougm 38433034Sdougm /* 38443034Sdougm * valid_unset_security(group, optlist, proto) 38453034Sdougm * 38463034Sdougm * Sanity check the optlist to make sure they can be removed. Issue an 38473034Sdougm * error if a property doesn't exist. 38483034Sdougm */ 38493034Sdougm 38503034Sdougm static int 38513034Sdougm valid_unset_security(sa_group_t group, struct options *optlist, char *proto, 38525331Samw char *sectype) 38533034Sdougm { 38543034Sdougm struct options *cur; 38553034Sdougm sa_security_t security; 38563034Sdougm sa_property_t prop; 38573034Sdougm int ret = SA_OK; 38583034Sdougm char *sec; 38593034Sdougm 38603034Sdougm sec = sa_proto_space_alias(proto, sectype); 38613034Sdougm security = sa_get_security(group, sec, proto); 38623034Sdougm if (security != NULL) { 38634653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 38644653Sdougm prop = sa_get_property(security, cur->optname); 38654653Sdougm if (prop == NULL) { 38664653Sdougm (void) printf(gettext( 38674653Sdougm "Could not unset property %s: not set\n"), 38684653Sdougm cur->optname); 38694653Sdougm ret = SA_NO_SUCH_PROP; 38704653Sdougm } 38713034Sdougm } 38723034Sdougm } else { 38734653Sdougm (void) printf(gettext( 38744653Sdougm "Could not unset %s: space not defined\n"), sectype); 38754653Sdougm ret = SA_NO_SUCH_SECURITY; 38763034Sdougm } 38773034Sdougm if (sec != NULL) 38784653Sdougm sa_free_attr_string(sec); 38793034Sdougm return (ret); 38803034Sdougm } 38813034Sdougm 38823034Sdougm /* 38833034Sdougm * remove_security(group, optlist, proto) 38843034Sdougm * 38853034Sdougm * Remove the properties since they were checked as valid. 38863034Sdougm */ 38873034Sdougm 38883034Sdougm static int 38893034Sdougm remove_security(sa_group_t group, char *sectype, 38905331Samw struct options *optlist, char *proto, int *err) 38913034Sdougm { 38923034Sdougm sa_security_t security; 38933034Sdougm int ret = SA_OK; 38943034Sdougm int change = 0; 38953034Sdougm 38963034Sdougm sectype = sa_proto_space_alias(proto, sectype); 38973034Sdougm security = sa_get_security(group, sectype, proto); 38983034Sdougm if (sectype != NULL) 38994653Sdougm sa_free_attr_string(sectype); 39003034Sdougm 39013034Sdougm if (security != NULL) { 39024653Sdougm while (optlist != NULL) { 39034653Sdougm sa_property_t prop; 39044653Sdougm prop = sa_get_property(security, optlist->optname); 39054653Sdougm if (prop != NULL) { 39064653Sdougm ret = sa_remove_property(prop); 39074653Sdougm if (ret != SA_OK) 39084653Sdougm break; 39094653Sdougm change = 1; 39104653Sdougm } 39114653Sdougm optlist = optlist->next; 39123034Sdougm } 39133034Sdougm /* 39143034Sdougm * when done, properties may have all been removed but 39153034Sdougm * we need to keep the security type itself until 39163034Sdougm * explicitly removed. 39173034Sdougm */ 39184653Sdougm if (ret == SA_OK && change) 39194653Sdougm ret = sa_commit_properties(security, 0); 39203034Sdougm } else { 39214653Sdougm ret = SA_NO_SUCH_PROP; 39223034Sdougm } 39233034Sdougm if (err != NULL) 39244653Sdougm *err = ret; 39253034Sdougm return (change); 39263034Sdougm } 39273034Sdougm 39283034Sdougm /* 39295331Samw * basic_unset(groupname, optlist, protocol, sharepath, rsrcname, dryrun) 39303034Sdougm * 39314653Sdougm * Unset non-named optionset properties. 39323034Sdougm */ 39333034Sdougm 39343034Sdougm static int 39353910Sdougm basic_unset(sa_handle_t handle, char *groupname, struct options *optlist, 39365331Samw char *protocol, char *sharepath, char *rsrcname, int dryrun) 39373034Sdougm { 39383034Sdougm sa_group_t group; 39393034Sdougm int ret = SA_OK; 39403034Sdougm int change = 0; 39413034Sdougm struct list *worklist = NULL; 39424653Sdougm sa_share_t share = NULL; 39435331Samw sa_resource_t resource = NULL; 39443034Sdougm 39453910Sdougm group = sa_get_group(handle, groupname); 39464653Sdougm if (group == NULL) 39474653Sdougm return (ret); 39484653Sdougm 39495331Samw /* 39505331Samw * If there is a sharepath, make sure it belongs to 39515331Samw * the group. 39525331Samw */ 39534653Sdougm if (sharepath != NULL) { 39543034Sdougm share = sa_get_share(group, sharepath); 39553034Sdougm if (share == NULL) { 39564653Sdougm (void) printf(gettext( 39574653Sdougm "Share does not exist in group %s\n"), 39584653Sdougm groupname, sharepath); 39594653Sdougm ret = SA_NO_SUCH_PATH; 39603034Sdougm } 39614653Sdougm } 39625331Samw /* 39635331Samw * If a resource name exists, make sure it belongs to 39645331Samw * the share if present else it belongs to the 39655331Samw * group. Also check the protocol to see if it 39665331Samw * supports resource level properties or not. If not, 39675331Samw * use share only. 39685331Samw */ 39695331Samw if (rsrcname != NULL) { 39705331Samw if (share != NULL) { 39715331Samw resource = sa_get_share_resource(share, rsrcname); 39725331Samw if (resource == NULL) 39735331Samw ret = SA_NO_SUCH_RESOURCE; 39745331Samw } else { 39755331Samw resource = sa_get_resource(group, rsrcname); 39765331Samw if (resource != NULL) { 39775331Samw share = sa_get_resource_parent(resource); 39785331Samw } else { 39795331Samw ret = SA_NO_SUCH_RESOURCE; 39805331Samw } 39815331Samw } 39825331Samw if (ret == SA_OK && resource != NULL) { 39835331Samw uint64_t features; 39845331Samw /* 39855331Samw * Check to see if the resource can take 39865331Samw * properties. If so, stick the resource into 39875331Samw * "share" so it will all just work. 39885331Samw */ 39895331Samw features = sa_proto_get_featureset(protocol); 39905331Samw if (features & SA_FEATURE_RESOURCE) 39915331Samw share = (sa_share_t)resource; 39925331Samw } 39935331Samw } 39945331Samw 39954653Sdougm if (ret == SA_OK) { 39963034Sdougm /* group must exist */ 39973034Sdougm ret = valid_unset(share != NULL ? share : group, 39984653Sdougm optlist, protocol); 39993034Sdougm if (ret == SA_OK && !dryrun) { 40004653Sdougm if (share != NULL) { 40014653Sdougm sa_optionset_t optionset; 40024653Sdougm sa_property_t prop; 40034653Sdougm change |= remove_options(share, optlist, 40044653Sdougm protocol, &ret); 40054653Sdougm /* 40064653Sdougm * If a share optionset is 40074653Sdougm * empty, remove it. 40084653Sdougm */ 40094653Sdougm optionset = sa_get_optionset((sa_share_t)share, 40104653Sdougm protocol); 40114653Sdougm if (optionset != NULL) { 40124653Sdougm prop = sa_get_property(optionset, NULL); 40134653Sdougm if (prop == NULL) 40144653Sdougm (void) sa_destroy_optionset( 40154653Sdougm optionset); 40164653Sdougm } 40174653Sdougm } else { 40184653Sdougm change |= remove_options(group, 40194653Sdougm optlist, protocol, &ret); 40203034Sdougm } 40214653Sdougm if (ret == SA_OK && change) 40225331Samw worklist = add_list(worklist, group, share, 40235331Samw protocol); 40244653Sdougm if (ret != SA_OK) 40254653Sdougm (void) printf(gettext( 40264653Sdougm "Could not remove properties: " 40274653Sdougm "%s\n"), sa_errorstr(ret)); 40283034Sdougm } 40294653Sdougm } else { 40305331Samw (void) printf(gettext("Group \"%s\" not found\n"), groupname); 40313034Sdougm ret = SA_NO_SUCH_GROUP; 40323034Sdougm } 40334653Sdougm free_opt(optlist); 40343034Sdougm 40353034Sdougm /* 40364653Sdougm * We have a group and potentially legal additions 40374653Sdougm * 40384653Sdougm * Commit to configuration if not a dryrun 40393034Sdougm */ 40403034Sdougm if (!dryrun && ret == SA_OK) { 40414653Sdougm if (change && worklist != NULL) { 40424653Sdougm /* properties changed, so update all shares */ 40434653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 40445331Samw protocol, B_TRUE); 40454653Sdougm } 40463034Sdougm } 40473034Sdougm if (worklist != NULL) 40484653Sdougm free_list(worklist); 40493034Sdougm return (ret); 40503034Sdougm } 40513034Sdougm 40523034Sdougm /* 40533034Sdougm * space_unset(groupname, optlist, protocol, sharepath, dryrun) 40543034Sdougm * 40554653Sdougm * Unset named optionset properties. 40563034Sdougm */ 40573034Sdougm static int 40583910Sdougm space_unset(sa_handle_t handle, char *groupname, struct options *optlist, 40595331Samw char *protocol, char *sharepath, int dryrun, char *sectype) 40603034Sdougm { 40613034Sdougm sa_group_t group; 40623034Sdougm int ret = SA_OK; 40633034Sdougm int change = 0; 40643034Sdougm struct list *worklist = NULL; 40654653Sdougm sa_share_t share = NULL; 40663034Sdougm 40673910Sdougm group = sa_get_group(handle, groupname); 40684653Sdougm if (group == NULL) { 40694653Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 40704653Sdougm return (SA_NO_SUCH_GROUP); 40714653Sdougm } 40724653Sdougm if (sharepath != NULL) { 40733034Sdougm share = sa_get_share(group, sharepath); 40743034Sdougm if (share == NULL) { 40754653Sdougm (void) printf(gettext( 40764653Sdougm "Share does not exist in group %s\n"), 40774653Sdougm groupname, sharepath); 40784653Sdougm return (SA_NO_SUCH_PATH); 40793034Sdougm } 40804653Sdougm } 40815331Samw ret = valid_unset_security(share != NULL ? share : group, 40825331Samw optlist, protocol, sectype); 40834653Sdougm 40844653Sdougm if (ret == SA_OK && !dryrun) { 40854653Sdougm if (optlist != NULL) { 40863034Sdougm if (share != NULL) { 40874653Sdougm sa_security_t optionset; 40884653Sdougm sa_property_t prop; 40894653Sdougm change = remove_security(share, 40904653Sdougm sectype, optlist, protocol, &ret); 40914653Sdougm 40924653Sdougm /* If a share security is empty, remove it */ 40934653Sdougm optionset = sa_get_security((sa_group_t)share, 40944653Sdougm sectype, protocol); 40954653Sdougm if (optionset != NULL) { 40964653Sdougm prop = sa_get_property(optionset, 40974653Sdougm NULL); 40984653Sdougm if (prop == NULL) 40994653Sdougm ret = sa_destroy_security( 41004653Sdougm optionset); 41014653Sdougm } 41023034Sdougm } else { 41034653Sdougm change = remove_security(group, sectype, 41044653Sdougm optlist, protocol, &ret); 41053034Sdougm } 41064653Sdougm } else { 41073034Sdougm sa_security_t security; 41083034Sdougm char *sec; 41093034Sdougm sec = sa_proto_space_alias(protocol, sectype); 41103034Sdougm security = sa_get_security(group, sec, protocol); 41113034Sdougm if (sec != NULL) 41124653Sdougm sa_free_attr_string(sec); 41133034Sdougm if (security != NULL) { 41144653Sdougm ret = sa_destroy_security(security); 41154653Sdougm if (ret == SA_OK) 41164653Sdougm change = 1; 41173034Sdougm } else { 41184653Sdougm ret = SA_NO_SUCH_PROP; 41193034Sdougm } 41204653Sdougm } 41214653Sdougm if (ret != SA_OK) 41223034Sdougm (void) printf(gettext("Could not unset property: %s\n"), 41234653Sdougm sa_errorstr(ret)); 41243034Sdougm } 41254653Sdougm 41264653Sdougm if (ret == SA_OK && change) 41275331Samw worklist = add_list(worklist, group, 0, protocol); 41284653Sdougm 41293034Sdougm free_opt(optlist); 41303034Sdougm /* 41314653Sdougm * We have a group and potentially legal additions 41323034Sdougm */ 41333034Sdougm 41344653Sdougm /* Commit to configuration if not a dryrun */ 41353034Sdougm if (!dryrun && ret == 0) { 41363034Sdougm /* properties changed, so update all shares */ 41374653Sdougm if (change && worklist != NULL) 41384653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 41395331Samw protocol, B_TRUE); 41404653Sdougm ret = sa_update_config(handle); 41413034Sdougm } 41423034Sdougm if (worklist != NULL) 41434653Sdougm free_list(worklist); 41443034Sdougm return (ret); 41453034Sdougm } 41463034Sdougm 41473034Sdougm /* 41483034Sdougm * sa_unset(flags, argc, argv) 41493034Sdougm * 41504653Sdougm * Implements the unset subcommand. Parsing done here and then basic 41513034Sdougm * or space versions of the real code are called. 41523034Sdougm */ 41533034Sdougm 41543034Sdougm int 41553910Sdougm sa_unset(sa_handle_t handle, int flags, int argc, char *argv[]) 41563034Sdougm { 41573034Sdougm char *groupname; 41583034Sdougm int verbose = 0; 41593034Sdougm int dryrun = 0; 41603034Sdougm int c; 41613034Sdougm char *protocol = NULL; 41623034Sdougm int ret = SA_OK; 41633034Sdougm struct options *optlist = NULL; 41645331Samw char *rsrcname = NULL; 41653034Sdougm char *sharepath = NULL; 41663034Sdougm char *optset = NULL; 41673034Sdougm int auth; 41683034Sdougm 41695331Samw while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) { 41704653Sdougm switch (c) { 41714653Sdougm case 'v': 41724653Sdougm verbose++; 41734653Sdougm break; 41744653Sdougm case 'n': 41754653Sdougm dryrun++; 41764653Sdougm break; 41774653Sdougm case 'P': 41785331Samw if (protocol != NULL) { 41795331Samw (void) printf(gettext( 41805331Samw "Specifying multiple protocols " 41815331Samw "not supported: %s\n"), protocol); 41825331Samw return (SA_SYNTAX_ERR); 41835331Samw } 41844653Sdougm protocol = optarg; 41854653Sdougm if (!sa_valid_protocol(protocol)) { 41864653Sdougm (void) printf(gettext( 41874653Sdougm "Invalid protocol specified: %s\n"), 41884653Sdougm protocol); 41894653Sdougm return (SA_INVALID_PROTOCOL); 41904653Sdougm } 41914653Sdougm break; 41924653Sdougm case 'p': 41934653Sdougm ret = add_opt(&optlist, optarg, 1); 41944653Sdougm switch (ret) { 41954653Sdougm case OPT_ADD_SYNTAX: 41964653Sdougm (void) printf(gettext("Property syntax error " 41974653Sdougm "for property %s\n"), optarg); 41984653Sdougm return (SA_SYNTAX_ERR); 41994653Sdougm 42004653Sdougm case OPT_ADD_PROPERTY: 42014653Sdougm (void) printf(gettext("Properties need to be " 42024653Sdougm "set with set command: %s\n"), optarg); 42034653Sdougm return (SA_SYNTAX_ERR); 42044653Sdougm 42054653Sdougm default: 42064653Sdougm break; 42074653Sdougm } 42084653Sdougm break; 42095331Samw case 'r': 42105331Samw /* 42115331Samw * Unset properties on resource if applicable or on 42125331Samw * share if resource for this protocol doesn't use 42135331Samw * resources. 42145331Samw */ 42155331Samw if (rsrcname != NULL) { 42165331Samw (void) printf(gettext( 42175331Samw "Unsetting multiple resource " 42185331Samw "names not supported\n")); 42195331Samw return (SA_SYNTAX_ERR); 42205331Samw } 42215331Samw rsrcname = optarg; 42225331Samw break; 42234653Sdougm case 's': 42245331Samw if (sharepath != NULL) { 42255331Samw (void) printf(gettext( 42265331Samw "Adding multiple shares not supported\n")); 42275331Samw return (SA_SYNTAX_ERR); 42285331Samw } 42294653Sdougm sharepath = optarg; 42304653Sdougm break; 42314653Sdougm case 'S': 42325331Samw if (optset != NULL) { 42335331Samw (void) printf(gettext( 42345331Samw "Specifying multiple property " 42355331Samw "spaces not supported: %s\n"), optset); 42365331Samw return (SA_SYNTAX_ERR); 42375331Samw } 42384653Sdougm optset = optarg; 42394653Sdougm break; 42404653Sdougm default: 42414653Sdougm case 'h': 42424653Sdougm case '?': 42434653Sdougm (void) printf(gettext("usage: %s\n"), 42444653Sdougm sa_get_usage(USAGE_UNSET)); 42454653Sdougm return (SA_OK); 42463034Sdougm } 42473034Sdougm } 42483034Sdougm 42493034Sdougm if (optlist != NULL) 42504653Sdougm ret = chk_opt(optlist, optset != NULL, protocol); 42513034Sdougm 42523034Sdougm if (optind >= argc || (optlist == NULL && optset == NULL) || 42533034Sdougm protocol == NULL) { 42544653Sdougm char *sep = "\t"; 42554653Sdougm (void) printf(gettext("usage: %s\n"), 42564653Sdougm sa_get_usage(USAGE_UNSET)); 42574653Sdougm if (optind >= argc) { 42584653Sdougm (void) printf(gettext("%sgroup must be specified"), 42594653Sdougm sep); 42604653Sdougm sep = ", "; 42614653Sdougm } 42624653Sdougm if (optlist == NULL) { 42634653Sdougm (void) printf(gettext("%sat least one property must " 42644653Sdougm "be specified"), sep); 42654653Sdougm sep = ", "; 42664653Sdougm } 42674653Sdougm if (protocol == NULL) { 42684653Sdougm (void) printf(gettext("%sprotocol must be specified"), 42694653Sdougm sep); 42704653Sdougm sep = ", "; 42714653Sdougm } 42724653Sdougm (void) printf("\n"); 42734653Sdougm ret = SA_SYNTAX_ERR; 42743034Sdougm } else { 42753034Sdougm 42763034Sdougm /* 42774653Sdougm * If a group already exists, we can only add a new 42783034Sdougm * protocol to it and not create a new one or add the 42793034Sdougm * same protocol again. 42803034Sdougm */ 42813034Sdougm 42824653Sdougm groupname = argv[optind]; 42834653Sdougm auth = check_authorizations(groupname, flags); 42844653Sdougm if (optset == NULL) 42854653Sdougm ret = basic_unset(handle, groupname, optlist, protocol, 42865331Samw sharepath, rsrcname, dryrun); 42874653Sdougm else 42884653Sdougm ret = space_unset(handle, groupname, optlist, protocol, 42894653Sdougm sharepath, dryrun, optset); 42904653Sdougm 42914653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) 42924653Sdougm (void) printf(gettext("Command would fail: %s\n"), 42934653Sdougm sa_errorstr(SA_NO_PERMISSION)); 42943034Sdougm } 42953034Sdougm return (ret); 42963034Sdougm } 42973034Sdougm 42983034Sdougm /* 42993034Sdougm * sa_enable_group(flags, argc, argv) 43003034Sdougm * 43013034Sdougm * Implements the enable subcommand 43023034Sdougm */ 43033034Sdougm 43043034Sdougm int 43053910Sdougm sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[]) 43063034Sdougm { 43073034Sdougm int verbose = 0; 43083034Sdougm int dryrun = 0; 43093034Sdougm int all = 0; 43103034Sdougm int c; 43113034Sdougm int ret = SA_OK; 43123034Sdougm char *protocol = NULL; 43133034Sdougm char *state; 43143034Sdougm struct list *worklist = NULL; 43153034Sdougm int auth = 1; 43164653Sdougm sa_group_t group; 43173034Sdougm 43183034Sdougm while ((c = getopt(argc, argv, "?havnP:")) != EOF) { 43194653Sdougm switch (c) { 43204653Sdougm case 'a': 43214653Sdougm all = 1; 43224653Sdougm break; 43234653Sdougm case 'n': 43244653Sdougm dryrun++; 43254653Sdougm break; 43264653Sdougm case 'P': 43275331Samw if (protocol != NULL) { 43285331Samw (void) printf(gettext( 43295331Samw "Specifying multiple protocols " 43305331Samw "not supported: %s\n"), protocol); 43315331Samw return (SA_SYNTAX_ERR); 43325331Samw } 43334653Sdougm protocol = optarg; 43344653Sdougm if (!sa_valid_protocol(protocol)) { 43354653Sdougm (void) printf(gettext( 43364653Sdougm "Invalid protocol specified: %s\n"), 43373034Sdougm protocol); 43384653Sdougm return (SA_INVALID_PROTOCOL); 43394653Sdougm } 43404653Sdougm break; 43414653Sdougm case 'v': 43424653Sdougm verbose++; 43434653Sdougm break; 43444653Sdougm default: 43454653Sdougm case 'h': 43464653Sdougm case '?': 43474653Sdougm (void) printf(gettext("usage: %s\n"), 43484653Sdougm sa_get_usage(USAGE_ENABLE)); 43494653Sdougm return (0); 43503034Sdougm } 43513034Sdougm } 43523034Sdougm 43533034Sdougm if (optind == argc && !all) { 43544653Sdougm (void) printf(gettext("usage: %s\n"), 43554653Sdougm sa_get_usage(USAGE_ENABLE)); 43564653Sdougm (void) printf(gettext("\tmust specify group\n")); 43574653Sdougm return (SA_NO_SUCH_PATH); 43584653Sdougm } 43594653Sdougm if (!all) { 43603034Sdougm while (optind < argc) { 43614653Sdougm group = sa_get_group(handle, argv[optind]); 43624653Sdougm if (group != NULL) { 43634653Sdougm auth &= check_authorizations(argv[optind], 43644653Sdougm flags); 43654653Sdougm state = sa_get_group_attr(group, "state"); 43664653Sdougm if (state != NULL && 43674653Sdougm strcmp(state, "enabled") == 0) { 43684653Sdougm /* already enabled */ 43694653Sdougm if (verbose) 43704653Sdougm (void) printf(gettext( 43714653Sdougm "Group \"%s\" is already " 43724653Sdougm "enabled\n"), 43734653Sdougm argv[optind]); 43744653Sdougm ret = SA_BUSY; /* already enabled */ 43754653Sdougm } else { 43764653Sdougm worklist = add_list(worklist, group, 43775331Samw 0, protocol); 43784653Sdougm if (verbose) 43794653Sdougm (void) printf(gettext( 43804653Sdougm "Enabling group \"%s\"\n"), 43814653Sdougm argv[optind]); 43824653Sdougm } 43834653Sdougm if (state != NULL) 43844653Sdougm sa_free_attr_string(state); 43853034Sdougm } else { 43864653Sdougm ret = SA_NO_SUCH_GROUP; 43873034Sdougm } 43884653Sdougm optind++; 43893034Sdougm } 43904653Sdougm } else { 43914653Sdougm for (group = sa_get_group(handle, NULL); 43924653Sdougm group != NULL; 43933034Sdougm group = sa_get_next_group(group)) { 43945331Samw worklist = add_list(worklist, group, 0, protocol); 43953034Sdougm } 43964653Sdougm } 43974653Sdougm if (!dryrun && ret == SA_OK) 43985331Samw ret = enable_all_groups(handle, worklist, 1, 0, NULL, B_FALSE); 43994653Sdougm 44004653Sdougm if (ret != SA_OK && ret != SA_BUSY) 44013034Sdougm (void) printf(gettext("Could not enable group: %s\n"), 44024653Sdougm sa_errorstr(ret)); 44034653Sdougm if (ret == SA_BUSY) 44043034Sdougm ret = SA_OK; 44054653Sdougm 44063034Sdougm if (worklist != NULL) 44074653Sdougm free_list(worklist); 44083034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 44094653Sdougm (void) printf(gettext("Command would fail: %s\n"), 44104653Sdougm sa_errorstr(SA_NO_PERMISSION)); 44113034Sdougm } 44123034Sdougm return (ret); 44133034Sdougm } 44143034Sdougm 44153034Sdougm /* 44165331Samw * disable_group(group, proto) 44173034Sdougm * 44185331Samw * Disable all the shares in the specified group.. This is a helper 44195331Samw * for disable_all_groups in order to simplify regular and subgroup 44205331Samw * (zfs) disabling. Group has already been checked for non-NULL. 44213034Sdougm */ 44223034Sdougm 44233034Sdougm static int 44245331Samw disable_group(sa_group_t group, char *proto) 44253034Sdougm { 44263034Sdougm sa_share_t share; 44273034Sdougm int ret = SA_OK; 44283034Sdougm 44295331Samw /* 44305331Samw * If the protocol isn't enabled, skip it and treat as 44315331Samw * successful. 44325331Samw */ 44335331Samw if (!has_protocol(group, proto)) 44345331Samw return (ret); 44355331Samw 44363034Sdougm for (share = sa_get_share(group, NULL); 44373034Sdougm share != NULL && ret == SA_OK; 44383034Sdougm share = sa_get_next_share(share)) { 44395331Samw ret = sa_disable_share(share, proto); 44404653Sdougm if (ret == SA_NO_SUCH_PATH) { 44414653Sdougm /* 44424653Sdougm * this is OK since the path is gone. we can't 44434653Sdougm * re-share it anyway so no error. 44444653Sdougm */ 44454653Sdougm ret = SA_OK; 44464653Sdougm } 44473034Sdougm } 44483034Sdougm return (ret); 44493034Sdougm } 44503034Sdougm 44513034Sdougm /* 44523034Sdougm * disable_all_groups(work, setstate) 44533034Sdougm * 44543034Sdougm * helper function that disables the shares in the list of groups 44553034Sdougm * provided. It optionally marks the group as disabled. Used by both 44563034Sdougm * enable and start subcommands. 44573034Sdougm */ 44583034Sdougm 44593034Sdougm static int 44603910Sdougm disable_all_groups(sa_handle_t handle, struct list *work, int setstate) 44613034Sdougm { 44623034Sdougm int ret = SA_OK; 44633034Sdougm sa_group_t subgroup, group; 44643034Sdougm 44653034Sdougm while (work != NULL && ret == SA_OK) { 44664653Sdougm group = (sa_group_t)work->item; 44674653Sdougm if (setstate) 44684653Sdougm ret = sa_set_group_attr(group, "state", "disabled"); 44694653Sdougm if (ret == SA_OK) { 44704653Sdougm char *name; 44714653Sdougm name = sa_get_group_attr(group, "name"); 44724653Sdougm if (name != NULL && strcmp(name, "zfs") == 0) { 44734653Sdougm /* need to get the sub-groups for stopping */ 44744653Sdougm for (subgroup = sa_get_sub_group(group); 44754653Sdougm subgroup != NULL; 44764653Sdougm subgroup = sa_get_next_group(subgroup)) { 44775331Samw ret = disable_group(subgroup, 44785331Samw work->proto); 44794653Sdougm } 44804653Sdougm } else { 44815331Samw ret = disable_group(group, work->proto); 44824653Sdougm } 44834653Sdougm /* 44844653Sdougm * We don't want to "disable" since it won't come 44854653Sdougm * up after a reboot. The SMF framework should do 44864653Sdougm * the right thing. On enable we do want to do 44874653Sdougm * something. 44884653Sdougm */ 44893034Sdougm } 44904653Sdougm work = work->next; 44913034Sdougm } 44923034Sdougm if (ret == SA_OK) 44934653Sdougm ret = sa_update_config(handle); 44943034Sdougm return (ret); 44953034Sdougm } 44963034Sdougm 44973034Sdougm /* 44983034Sdougm * sa_disable_group(flags, argc, argv) 44993034Sdougm * 45003034Sdougm * Implements the disable subcommand 45013034Sdougm */ 45023034Sdougm 45033034Sdougm int 45043910Sdougm sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[]) 45053034Sdougm { 45063034Sdougm int verbose = 0; 45073034Sdougm int dryrun = 0; 45083034Sdougm int all = 0; 45093034Sdougm int c; 45103034Sdougm int ret = SA_OK; 45115331Samw char *protocol = NULL; 45123034Sdougm char *state; 45133034Sdougm struct list *worklist = NULL; 45144653Sdougm sa_group_t group; 45153034Sdougm int auth = 1; 45163034Sdougm 45173034Sdougm while ((c = getopt(argc, argv, "?havn")) != EOF) { 45184653Sdougm switch (c) { 45194653Sdougm case 'a': 45204653Sdougm all = 1; 45214653Sdougm break; 45224653Sdougm case 'n': 45234653Sdougm dryrun++; 45244653Sdougm break; 45254653Sdougm case 'P': 45265331Samw if (protocol != NULL) { 45275331Samw (void) printf(gettext( 45285331Samw "Specifying multiple protocols " 45295331Samw "not supported: %s\n"), protocol); 45305331Samw return (SA_SYNTAX_ERR); 45315331Samw } 45324653Sdougm protocol = optarg; 45334653Sdougm if (!sa_valid_protocol(protocol)) { 45344653Sdougm (void) printf(gettext( 45354653Sdougm "Invalid protocol specified: %s\n"), 45364653Sdougm protocol); 45374653Sdougm return (SA_INVALID_PROTOCOL); 45384653Sdougm } 45394653Sdougm break; 45404653Sdougm case 'v': 45414653Sdougm verbose++; 45424653Sdougm break; 45434653Sdougm default: 45444653Sdougm case 'h': 45454653Sdougm case '?': 45464653Sdougm (void) printf(gettext("usage: %s\n"), 45474653Sdougm sa_get_usage(USAGE_DISABLE)); 45484653Sdougm return (0); 45493034Sdougm } 45503034Sdougm } 45513034Sdougm 45523034Sdougm if (optind == argc && !all) { 45533034Sdougm (void) printf(gettext("usage: %s\n"), 45544653Sdougm sa_get_usage(USAGE_DISABLE)); 45553034Sdougm (void) printf(gettext("\tmust specify group\n")); 45564653Sdougm return (SA_NO_SUCH_PATH); 45574653Sdougm } 45584653Sdougm if (!all) { 45594653Sdougm while (optind < argc) { 45603910Sdougm group = sa_get_group(handle, argv[optind]); 45613034Sdougm if (group != NULL) { 45624653Sdougm auth &= check_authorizations(argv[optind], 45634653Sdougm flags); 45644653Sdougm state = sa_get_group_attr(group, "state"); 45654653Sdougm if (state == NULL || 45664653Sdougm strcmp(state, "disabled") == 0) { 45674653Sdougm /* already disabled */ 45684653Sdougm if (verbose) 45694653Sdougm (void) printf(gettext( 45704653Sdougm "Group \"%s\" is " 45714653Sdougm "already disabled\n"), 45724653Sdougm argv[optind]); 45735331Samw ret = SA_BUSY; /* already disabled */ 45744653Sdougm } else { 45755331Samw worklist = add_list(worklist, group, 0, 45765331Samw protocol); 45774653Sdougm if (verbose) 45784653Sdougm (void) printf(gettext( 45794653Sdougm "Disabling group " 45804653Sdougm "\"%s\"\n"), argv[optind]); 45814653Sdougm } 45824653Sdougm if (state != NULL) 45834653Sdougm sa_free_attr_string(state); 45843034Sdougm } else { 45854653Sdougm ret = SA_NO_SUCH_GROUP; 45863034Sdougm } 45873034Sdougm optind++; 45884653Sdougm } 45894653Sdougm } else { 45904653Sdougm for (group = sa_get_group(handle, NULL); 45914653Sdougm group != NULL; 45924653Sdougm group = sa_get_next_group(group)) 45935331Samw worklist = add_list(worklist, group, 0, protocol); 45943034Sdougm } 45954653Sdougm 45964653Sdougm if (ret == SA_OK && !dryrun) 45974653Sdougm ret = disable_all_groups(handle, worklist, 1); 45984653Sdougm if (ret != SA_OK && ret != SA_BUSY) 45994653Sdougm (void) printf(gettext("Could not disable group: %s\n"), 46004653Sdougm sa_errorstr(ret)); 46014653Sdougm if (ret == SA_BUSY) 46024653Sdougm ret = SA_OK; 46033034Sdougm if (worklist != NULL) 46044653Sdougm free_list(worklist); 46054653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) 46064653Sdougm (void) printf(gettext("Command would fail: %s\n"), 46074653Sdougm sa_errorstr(SA_NO_PERMISSION)); 46083034Sdougm return (ret); 46093034Sdougm } 46103034Sdougm 46113034Sdougm /* 46123034Sdougm * sa_start_group(flags, argc, argv) 46133034Sdougm * 46143034Sdougm * Implements the start command. 46153034Sdougm * This is similar to enable except it doesn't change the state 46163034Sdougm * of the group(s) and only enables shares if the group is already 46173034Sdougm * enabled. 46183034Sdougm */ 46195331Samw 46203034Sdougm int 46213910Sdougm sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[]) 46223034Sdougm { 46233034Sdougm int verbose = 0; 46243034Sdougm int all = 0; 46253034Sdougm int c; 46263034Sdougm int ret = SMF_EXIT_OK; 46273034Sdougm char *protocol = NULL; 46283034Sdougm char *state; 46293034Sdougm struct list *worklist = NULL; 46304653Sdougm sa_group_t group; 46315331Samw #ifdef lint 46325331Samw flags = flags; 46335331Samw #endif 46343034Sdougm 46353034Sdougm while ((c = getopt(argc, argv, "?havP:")) != EOF) { 46364653Sdougm switch (c) { 46374653Sdougm case 'a': 46384653Sdougm all = 1; 46394653Sdougm break; 46404653Sdougm case 'P': 46415331Samw if (protocol != NULL) { 46425331Samw (void) printf(gettext( 46435331Samw "Specifying multiple protocols " 46445331Samw "not supported: %s\n"), protocol); 46455331Samw return (SA_SYNTAX_ERR); 46465331Samw } 46474653Sdougm protocol = optarg; 46484653Sdougm if (!sa_valid_protocol(protocol)) { 46494653Sdougm (void) printf(gettext( 46504653Sdougm "Invalid protocol specified: %s\n"), 46513034Sdougm protocol); 46524653Sdougm return (SA_INVALID_PROTOCOL); 46534653Sdougm } 46544653Sdougm break; 46554653Sdougm case 'v': 46564653Sdougm verbose++; 46574653Sdougm break; 46584653Sdougm default: 46594653Sdougm case 'h': 46604653Sdougm case '?': 46614653Sdougm (void) printf(gettext("usage: %s\n"), 46624653Sdougm sa_get_usage(USAGE_START)); 46634653Sdougm return (SA_OK); 46643034Sdougm } 46653034Sdougm } 46663034Sdougm 46673034Sdougm if (optind == argc && !all) { 46683034Sdougm (void) printf(gettext("usage: %s\n"), 46694653Sdougm sa_get_usage(USAGE_START)); 46704653Sdougm return (SMF_EXIT_ERR_FATAL); 46714653Sdougm } 46724653Sdougm 46734653Sdougm if (!all) { 46744653Sdougm while (optind < argc) { 46753910Sdougm group = sa_get_group(handle, argv[optind]); 46763034Sdougm if (group != NULL) { 46774653Sdougm state = sa_get_group_attr(group, "state"); 46784653Sdougm if (state == NULL || 46794653Sdougm strcmp(state, "enabled") == 0) { 46805331Samw worklist = add_list(worklist, group, 0, 46815331Samw protocol); 46824653Sdougm if (verbose) 46834653Sdougm (void) printf(gettext( 46844653Sdougm "Starting group \"%s\"\n"), 46854653Sdougm argv[optind]); 46864653Sdougm } else { 46874653Sdougm /* 46884653Sdougm * Determine if there are any 46895331Samw * protocols. If there aren't any, 46904653Sdougm * then there isn't anything to do in 46914653Sdougm * any case so no error. 46924653Sdougm */ 46934653Sdougm if (sa_get_optionset(group, 46944653Sdougm protocol) != NULL) { 46954653Sdougm ret = SMF_EXIT_OK; 46964653Sdougm } 46973034Sdougm } 46984653Sdougm if (state != NULL) 46994653Sdougm sa_free_attr_string(state); 47003034Sdougm } 47013034Sdougm optind++; 47024653Sdougm } 47034653Sdougm } else { 47045331Samw for (group = sa_get_group(handle, NULL); 47055331Samw group != NULL; 47064653Sdougm group = sa_get_next_group(group)) { 47073034Sdougm state = sa_get_group_attr(group, "state"); 47083034Sdougm if (state == NULL || strcmp(state, "enabled") == 0) 47095331Samw worklist = add_list(worklist, group, 0, 47105331Samw protocol); 47113034Sdougm if (state != NULL) 47124653Sdougm sa_free_attr_string(state); 47133034Sdougm } 47143034Sdougm } 47154653Sdougm 47165331Samw (void) enable_all_groups(handle, worklist, 0, 1, protocol, B_FALSE); 47174653Sdougm 47183034Sdougm if (worklist != NULL) 47194653Sdougm free_list(worklist); 47203034Sdougm return (ret); 47213034Sdougm } 47223034Sdougm 47233034Sdougm /* 47243034Sdougm * sa_stop_group(flags, argc, argv) 47253034Sdougm * 47263034Sdougm * Implements the stop command. 47273034Sdougm * This is similar to disable except it doesn't change the state 47283034Sdougm * of the group(s) and only disables shares if the group is already 47293034Sdougm * enabled. 47303034Sdougm */ 47313034Sdougm int 47323910Sdougm sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[]) 47333034Sdougm { 47343034Sdougm int verbose = 0; 47353034Sdougm int all = 0; 47363034Sdougm int c; 47373034Sdougm int ret = SMF_EXIT_OK; 47383034Sdougm char *protocol = NULL; 47393034Sdougm char *state; 47403034Sdougm struct list *worklist = NULL; 47414653Sdougm sa_group_t group; 47425331Samw #ifdef lint 47435331Samw flags = flags; 47445331Samw #endif 47453034Sdougm 47463034Sdougm while ((c = getopt(argc, argv, "?havP:")) != EOF) { 47474653Sdougm switch (c) { 47484653Sdougm case 'a': 47494653Sdougm all = 1; 47504653Sdougm break; 47514653Sdougm case 'P': 47525331Samw if (protocol != NULL) { 47535331Samw (void) printf(gettext( 47545331Samw "Specifying multiple protocols " 47555331Samw "not supported: %s\n"), protocol); 47565331Samw return (SA_SYNTAX_ERR); 47575331Samw } 47584653Sdougm protocol = optarg; 47594653Sdougm if (!sa_valid_protocol(protocol)) { 47604653Sdougm (void) printf(gettext( 47614653Sdougm "Invalid protocol specified: %s\n"), 47624653Sdougm protocol); 47634653Sdougm return (SA_INVALID_PROTOCOL); 47644653Sdougm } 47654653Sdougm break; 47664653Sdougm case 'v': 47674653Sdougm verbose++; 47684653Sdougm break; 47694653Sdougm default: 47704653Sdougm case 'h': 47714653Sdougm case '?': 47724653Sdougm (void) printf(gettext("usage: %s\n"), 47734653Sdougm sa_get_usage(USAGE_STOP)); 47744653Sdougm return (0); 47753034Sdougm } 47763034Sdougm } 47773034Sdougm 47783034Sdougm if (optind == argc && !all) { 47794653Sdougm (void) printf(gettext("usage: %s\n"), 47804653Sdougm sa_get_usage(USAGE_STOP)); 47814653Sdougm return (SMF_EXIT_ERR_FATAL); 47824653Sdougm } else if (!all) { 47834653Sdougm while (optind < argc) { 47843910Sdougm group = sa_get_group(handle, argv[optind]); 47853034Sdougm if (group != NULL) { 47864653Sdougm state = sa_get_group_attr(group, "state"); 47874653Sdougm if (state == NULL || 47884653Sdougm strcmp(state, "enabled") == 0) { 47895331Samw worklist = add_list(worklist, group, 0, 47905331Samw protocol); 47914653Sdougm if (verbose) 47924653Sdougm (void) printf(gettext( 47934653Sdougm "Stopping group \"%s\"\n"), 47944653Sdougm argv[optind]); 47954653Sdougm } else { 47964653Sdougm ret = SMF_EXIT_OK; 47974653Sdougm } 47984653Sdougm if (state != NULL) 47994653Sdougm sa_free_attr_string(state); 48003034Sdougm } 48013034Sdougm optind++; 48024653Sdougm } 48034653Sdougm } else { 48045331Samw for (group = sa_get_group(handle, NULL); 48055331Samw group != NULL; 48064653Sdougm group = sa_get_next_group(group)) { 48073034Sdougm state = sa_get_group_attr(group, "state"); 48083034Sdougm if (state == NULL || strcmp(state, "enabled") == 0) 48095331Samw worklist = add_list(worklist, group, 0, 48105331Samw protocol); 48113034Sdougm if (state != NULL) 48124653Sdougm sa_free_attr_string(state); 48133034Sdougm } 48143034Sdougm } 48154653Sdougm (void) disable_all_groups(handle, worklist, 0); 48164653Sdougm ret = sa_update_config(handle); 48174653Sdougm 48183034Sdougm if (worklist != NULL) 48194653Sdougm free_list(worklist); 48203034Sdougm return (ret); 48213034Sdougm } 48223034Sdougm 48233034Sdougm /* 48243034Sdougm * remove_all_options(share, proto) 48253034Sdougm * 48263034Sdougm * Removes all options on a share. 48273034Sdougm */ 48283034Sdougm 48293034Sdougm static void 48303034Sdougm remove_all_options(sa_share_t share, char *proto) 48313034Sdougm { 48323034Sdougm sa_optionset_t optionset; 48333034Sdougm sa_security_t security; 48343034Sdougm sa_security_t prevsec = NULL; 48353034Sdougm 48363034Sdougm optionset = sa_get_optionset(share, proto); 48373034Sdougm if (optionset != NULL) 48384653Sdougm (void) sa_destroy_optionset(optionset); 48393034Sdougm for (security = sa_get_security(share, NULL, NULL); 48403034Sdougm security != NULL; 48413034Sdougm security = sa_get_next_security(security)) { 48424653Sdougm char *type; 48433034Sdougm /* 48444653Sdougm * We walk through the list. prevsec keeps the 48453034Sdougm * previous security so we can delete it without 48463034Sdougm * destroying the list. 48473034Sdougm */ 48484653Sdougm if (prevsec != NULL) { 48494653Sdougm /* remove the previously seen security */ 48504653Sdougm (void) sa_destroy_security(prevsec); 48514653Sdougm /* set to NULL so we don't try multiple times */ 48524653Sdougm prevsec = NULL; 48534653Sdougm } 48544653Sdougm type = sa_get_security_attr(security, "type"); 48554653Sdougm if (type != NULL) { 48564653Sdougm /* 48574653Sdougm * if the security matches the specified protocol, we 48584653Sdougm * want to remove it. prevsec holds it until either 48594653Sdougm * the next pass or we fall out of the loop. 48604653Sdougm */ 48614653Sdougm if (strcmp(type, proto) == 0) 48624653Sdougm prevsec = security; 48634653Sdougm sa_free_attr_string(type); 48644653Sdougm } 48653034Sdougm } 48663034Sdougm /* in case there is one left */ 48673034Sdougm if (prevsec != NULL) 48684653Sdougm (void) sa_destroy_security(prevsec); 48693034Sdougm } 48703034Sdougm 48713034Sdougm 48723034Sdougm /* 48733034Sdougm * for legacy support, we need to handle the old syntax. This is what 48743034Sdougm * we get if sharemgr is called with the name "share" rather than 48753034Sdougm * sharemgr. 48763034Sdougm */ 48773034Sdougm 48783034Sdougm static int 48793034Sdougm format_legacy_path(char *buff, int buffsize, char *proto, char *cmd) 48803034Sdougm { 48813034Sdougm int err; 48823034Sdougm 48833034Sdougm err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd); 48843034Sdougm if (err > buffsize) 48854653Sdougm return (-1); 48863034Sdougm return (0); 48873034Sdougm } 48883034Sdougm 48893034Sdougm 48903034Sdougm /* 48913034Sdougm * check_legacy_cmd(proto, cmd) 48923034Sdougm * 48933034Sdougm * Check to see if the cmd exists in /usr/lib/fs/<proto>/<cmd> and is 48943034Sdougm * executable. 48953034Sdougm */ 48963034Sdougm 48973034Sdougm static int 48983034Sdougm check_legacy_cmd(char *path) 48993034Sdougm { 49003034Sdougm struct stat st; 49013034Sdougm int ret = 0; 49023034Sdougm 49033034Sdougm if (stat(path, &st) == 0) { 49044653Sdougm if (S_ISREG(st.st_mode) && 49054653Sdougm st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) 49064653Sdougm ret = 1; 49073034Sdougm } 49083034Sdougm return (ret); 49093034Sdougm } 49103034Sdougm 49113034Sdougm /* 49123034Sdougm * run_legacy_command(proto, cmd, argv) 49133034Sdougm * 49144653Sdougm * We know the command exists, so attempt to execute it with all the 49153034Sdougm * arguments. This implements full legacy share support for those 49163034Sdougm * protocols that don't have plugin providers. 49173034Sdougm */ 49183034Sdougm 49193034Sdougm static int 49203034Sdougm run_legacy_command(char *path, char *argv[]) 49213034Sdougm { 49223034Sdougm int ret; 49233034Sdougm 49243034Sdougm ret = execv(path, argv); 49253034Sdougm if (ret < 0) { 49264653Sdougm switch (errno) { 49274653Sdougm case EACCES: 49284653Sdougm ret = SA_NO_PERMISSION; 49294653Sdougm break; 49304653Sdougm default: 49314653Sdougm ret = SA_SYSTEM_ERR; 49324653Sdougm break; 49334653Sdougm } 49343034Sdougm } 49353034Sdougm return (ret); 49363034Sdougm } 49373034Sdougm 49383034Sdougm /* 49393348Sdougm * out_share(out, group, proto) 49403034Sdougm * 49413034Sdougm * Display the share information in the format that the "share" 49423034Sdougm * command has traditionally used. 49433034Sdougm */ 49443034Sdougm 49453034Sdougm static void 49463348Sdougm out_share(FILE *out, sa_group_t group, char *proto) 49473034Sdougm { 49483034Sdougm sa_share_t share; 49493034Sdougm char resfmt[128]; 49505331Samw char *defprop; 49515331Samw 49525331Samw /* 49535331Samw * The original share command defaulted to displaying NFS 49545331Samw * shares or allowed a protocol to be specified. We want to 49555331Samw * skip those shares that are not the specified protocol. 49565331Samw */ 49575331Samw if (proto != NULL && sa_get_optionset(group, proto) == NULL) 49585331Samw return; 49595331Samw 49605331Samw if (proto == NULL) 49615331Samw proto = "nfs"; 49625331Samw 49635331Samw /* 49645331Samw * get the default property string. NFS uses "rw" but 49655331Samw * everything else will use "". 49665331Samw */ 49675331Samw if (proto != NULL && strcmp(proto, "nfs") != 0) 49685331Samw defprop = "\"\""; 49695331Samw else 49705331Samw defprop = "rw"; 49713034Sdougm 49724653Sdougm for (share = sa_get_share(group, NULL); 49734653Sdougm share != NULL; 49744653Sdougm share = sa_get_next_share(share)) { 49754653Sdougm char *path; 49764653Sdougm char *type; 49774653Sdougm char *resource; 49784653Sdougm char *description; 49794653Sdougm char *groupname; 49804653Sdougm char *sharedstate; 49814653Sdougm int shared = 1; 49824653Sdougm char *soptions; 49835331Samw char shareopts[MAXNAMLEN]; 49844653Sdougm 49854653Sdougm sharedstate = sa_get_share_attr(share, "shared"); 49864653Sdougm path = sa_get_share_attr(share, "path"); 49874653Sdougm type = sa_get_share_attr(share, "type"); 49885331Samw resource = get_resource(share); 49894653Sdougm groupname = sa_get_group_attr(group, "name"); 49904653Sdougm 49914653Sdougm if (groupname != NULL && strcmp(groupname, "default") == 0) { 49924653Sdougm sa_free_attr_string(groupname); 49934653Sdougm groupname = NULL; 49944653Sdougm } 49954653Sdougm description = sa_get_share_description(share); 49964653Sdougm 49975331Samw /* 49985331Samw * Want the sharetab version if it exists, defaulting 49995331Samw * to NFS if no protocol specified. 50005331Samw */ 50015331Samw (void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", proto); 50025331Samw soptions = sa_get_share_attr(share, shareopts); 50034653Sdougm 50044653Sdougm if (sharedstate == NULL) 50054653Sdougm shared = 0; 50064653Sdougm 50074653Sdougm if (soptions == NULL) 50084653Sdougm soptions = sa_proto_legacy_format(proto, share, 1); 50094653Sdougm 50104653Sdougm if (shared) { 50114653Sdougm /* only active shares go here */ 50124653Sdougm (void) snprintf(resfmt, sizeof (resfmt), "%s%s%s", 50134653Sdougm resource != NULL ? resource : "-", 50144653Sdougm groupname != NULL ? "@" : "", 50154653Sdougm groupname != NULL ? groupname : ""); 50164653Sdougm (void) fprintf(out, "%-14.14s %s %s \"%s\" \n", 50174653Sdougm resfmt, path, 50184653Sdougm (soptions != NULL && strlen(soptions) > 0) ? 50195331Samw soptions : defprop, 50204653Sdougm (description != NULL) ? description : ""); 50214653Sdougm } 50224653Sdougm 50234653Sdougm if (path != NULL) 50244653Sdougm sa_free_attr_string(path); 50254653Sdougm if (type != NULL) 50264653Sdougm sa_free_attr_string(type); 50274653Sdougm if (resource != NULL) 50284653Sdougm sa_free_attr_string(resource); 50294653Sdougm if (groupname != NULL) 50304653Sdougm sa_free_attr_string(groupname); 50314653Sdougm if (description != NULL) 50324653Sdougm sa_free_share_description(description); 50334653Sdougm if (sharedstate != NULL) 50344653Sdougm sa_free_attr_string(sharedstate); 50354653Sdougm if (soptions != NULL) 50364653Sdougm sa_format_free(soptions); 50373034Sdougm } 50383034Sdougm } 50393034Sdougm 50403034Sdougm /* 50413034Sdougm * output_legacy_file(out, proto) 50423034Sdougm * 50433034Sdougm * Walk all of the groups for the specified protocol and call 50443034Sdougm * out_share() to format and write in the format displayed by the 50453034Sdougm * "share" command with no arguments. 50463034Sdougm */ 50473034Sdougm 50483034Sdougm static void 50493910Sdougm output_legacy_file(FILE *out, char *proto, sa_handle_t handle) 50503034Sdougm { 50513034Sdougm sa_group_t group; 50523034Sdougm 50535331Samw for (group = sa_get_group(handle, NULL); 50545331Samw group != NULL; 50554653Sdougm group = sa_get_next_group(group)) { 50564653Sdougm char *zfs; 50573034Sdougm 50583034Sdougm /* 50595331Samw * Go through all the groups and ZFS 50605331Samw * sub-groups. out_share() will format the shares in 50615331Samw * the group appropriately. 50623034Sdougm */ 50633034Sdougm 50644653Sdougm zfs = sa_get_group_attr(group, "zfs"); 50654653Sdougm if (zfs != NULL) { 50664653Sdougm sa_group_t zgroup; 50674653Sdougm sa_free_attr_string(zfs); 50684653Sdougm for (zgroup = sa_get_sub_group(group); 50694653Sdougm zgroup != NULL; 50704653Sdougm zgroup = sa_get_next_group(zgroup)) { 50714653Sdougm 50724653Sdougm /* got a group, so display it */ 50734653Sdougm out_share(out, zgroup, proto); 50744653Sdougm } 50754653Sdougm } else { 50764653Sdougm out_share(out, group, proto); 50773034Sdougm } 50783034Sdougm } 50793034Sdougm } 50803034Sdougm 50813034Sdougm int 50823910Sdougm sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[]) 50833034Sdougm { 50843034Sdougm char *protocol = "nfs"; 50853034Sdougm char *options = NULL; 50863034Sdougm char *description = NULL; 50873034Sdougm char *groupname = NULL; 50883034Sdougm char *sharepath = NULL; 50893034Sdougm char *resource = NULL; 50903034Sdougm char *groupstatus = NULL; 50913034Sdougm int persist = SA_SHARE_TRANSIENT; 50923034Sdougm int argsused = 0; 50933034Sdougm int c; 50943034Sdougm int ret = SA_OK; 50953034Sdougm int zfs = 0; 50963034Sdougm int true_legacy = 0; 50973034Sdougm int curtype = SA_SHARE_TRANSIENT; 50983034Sdougm char cmd[MAXPATHLEN]; 50994653Sdougm sa_group_t group = NULL; 51005331Samw sa_resource_t rsrc = NULL; 51014653Sdougm sa_share_t share; 51024653Sdougm char dir[MAXPATHLEN]; 51035331Samw uint64_t features; 51045331Samw #ifdef lint 51055331Samw flags = flags; 51065331Samw #endif 51073034Sdougm 51083034Sdougm while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) { 51094653Sdougm switch (c) { 51104653Sdougm case 'd': 51114653Sdougm description = optarg; 51124653Sdougm argsused++; 51134653Sdougm break; 51144653Sdougm case 'F': 51154653Sdougm protocol = optarg; 51164653Sdougm if (!sa_valid_protocol(protocol)) { 51174653Sdougm if (format_legacy_path(cmd, MAXPATHLEN, 51184653Sdougm protocol, "share") == 0 && 51194653Sdougm check_legacy_cmd(cmd)) { 51204653Sdougm true_legacy++; 51214653Sdougm } else { 51224653Sdougm (void) fprintf(stderr, gettext( 51234653Sdougm "Invalid protocol specified: " 51244653Sdougm "%s\n"), protocol); 51254653Sdougm return (SA_INVALID_PROTOCOL); 51264653Sdougm } 51274653Sdougm } 51284653Sdougm break; 51294653Sdougm case 'o': 51304653Sdougm options = optarg; 51314653Sdougm argsused++; 51324653Sdougm break; 51334653Sdougm case 'p': 51344653Sdougm persist = SA_SHARE_PERMANENT; 51354653Sdougm argsused++; 51364653Sdougm break; 51374653Sdougm case 'h': 51384653Sdougm case '?': 51394653Sdougm default: 51404653Sdougm (void) fprintf(stderr, gettext("usage: %s\n"), 51414653Sdougm sa_get_usage(USAGE_SHARE)); 51424653Sdougm return (SA_OK); 51433034Sdougm } 51444653Sdougm } 51454653Sdougm 51464653Sdougm /* Have the info so construct what is needed */ 51474653Sdougm if (!argsused && optind == argc) { 51484653Sdougm /* display current info in share format */ 51495331Samw (void) output_legacy_file(stdout, protocol, handle); 51504653Sdougm return (ret); 51513034Sdougm } 51523034Sdougm 51534653Sdougm /* We are modifying the configuration */ 51544653Sdougm if (optind == argc) { 51553034Sdougm (void) fprintf(stderr, gettext("usage: %s\n"), 51564653Sdougm sa_get_usage(USAGE_SHARE)); 51573034Sdougm return (SA_LEGACY_ERR); 51584653Sdougm } 51594653Sdougm if (true_legacy) { 51604653Sdougm /* If still using legacy share/unshare, exec it */ 51613034Sdougm ret = run_legacy_command(cmd, argv); 51623034Sdougm return (ret); 51634653Sdougm } 51644653Sdougm 51654653Sdougm sharepath = argv[optind++]; 51664653Sdougm if (optind < argc) { 51673034Sdougm resource = argv[optind]; 51683034Sdougm groupname = strchr(resource, '@'); 51693034Sdougm if (groupname != NULL) 51704653Sdougm *groupname++ = '\0'; 51714653Sdougm } 51724653Sdougm if (realpath(sharepath, dir) == NULL) 51733034Sdougm ret = SA_BAD_PATH; 51744653Sdougm else 51753034Sdougm sharepath = dir; 51764653Sdougm if (ret == SA_OK) 51773910Sdougm share = sa_find_share(handle, sharepath); 51784653Sdougm else 51793034Sdougm share = NULL; 51804653Sdougm 51815331Samw features = sa_proto_get_featureset(protocol); 51825331Samw 51834653Sdougm if (groupname != NULL) { 51844653Sdougm ret = SA_NOT_ALLOWED; 51854653Sdougm } else if (ret == SA_OK) { 51865331Samw char *legacygroup; 51873034Sdougm /* 51884653Sdougm * The legacy group is always present and zfs groups 51893034Sdougm * come and go. zfs shares may be in sub-groups and 51903034Sdougm * the zfs share will already be in that group so it 51915331Samw * isn't an error. If the protocol is "smb", the group 51925331Samw * "smb" is used when "default" would otherwise be 51935331Samw * used. "default" is NFS only and "smb" is SMB only. 51943034Sdougm */ 51955331Samw if (strcmp(protocol, "smb") == 0) 51965331Samw legacygroup = "smb"; 51975331Samw else 51985331Samw legacygroup = "default"; 51995331Samw 52003034Sdougm /* 52014653Sdougm * If the share exists (not NULL), then make sure it 52024653Sdougm * is one we want to handle by getting the parent 52034653Sdougm * group. 52043034Sdougm */ 52055331Samw if (share != NULL) { 52064653Sdougm group = sa_get_parent_group(share); 52075331Samw } else { 52084653Sdougm group = sa_get_group(handle, legacygroup); 52095331Samw if (group == NULL && strcmp(legacygroup, "smb") == 0) { 52105331Samw /* 52115331Samw * This group may not exist, so create 52125331Samw * as necessary. It only contains the 52135331Samw * "smb" protocol. 52145331Samw */ 52155331Samw group = sa_create_group(handle, legacygroup, 52165331Samw &ret); 52175331Samw if (group != NULL) 52185331Samw (void) sa_create_optionset(group, 52195331Samw protocol); 52205331Samw } 52215331Samw } 52225331Samw 52235331Samw if (group == NULL) { 52245331Samw ret = SA_SYSTEM_ERR; 52255331Samw goto err; 52265331Samw } 52275331Samw 52285331Samw groupstatus = group_status(group); 52295331Samw if (share == NULL) { 52305331Samw share = sa_add_share(group, sharepath, 52315331Samw persist, &ret); 52325331Samw if (share == NULL && 52335331Samw ret == SA_DUPLICATE_NAME) { 52345331Samw /* 52355331Samw * Could be a ZFS path being started 52365331Samw */ 52375331Samw if (sa_zfs_is_shared(handle, 52385331Samw sharepath)) { 52395331Samw ret = SA_OK; 52405331Samw group = sa_get_group(handle, 52415331Samw "zfs"); 52425331Samw if (group == NULL) { 52435331Samw /* 52445331Samw * This shouldn't 52455331Samw * happen. 52465331Samw */ 52475331Samw ret = SA_CONFIG_ERR; 52485331Samw } else { 52495331Samw share = sa_add_share( 52505331Samw group, sharepath, 52515331Samw persist, &ret); 52524653Sdougm } 52533034Sdougm } 52545331Samw } 52555331Samw } else { 52565331Samw char *type; 52575331Samw /* 52585331Samw * May want to change persist state, but the 52595331Samw * important thing is to change options. We 52605331Samw * need to change them regardless of the 52615331Samw * source. 52625331Samw */ 52635331Samw 52645331Samw if (sa_zfs_is_shared(handle, sharepath)) { 52655331Samw zfs = 1; 52665331Samw } 52675331Samw remove_all_options(share, protocol); 52685331Samw type = sa_get_share_attr(share, "type"); 52695331Samw if (type != NULL && 52705331Samw strcmp(type, "transient") != 0) { 52715331Samw curtype = SA_SHARE_PERMANENT; 52725331Samw } 52735331Samw if (type != NULL) 52745331Samw sa_free_attr_string(type); 52755331Samw if (curtype != persist) { 52765331Samw (void) sa_set_share_attr(share, "type", 52775331Samw persist == SA_SHARE_PERMANENT ? 52785331Samw "persist" : "transient"); 52795331Samw } 52805331Samw } 52815331Samw 52825331Samw /* 52835331Samw * If there is a resource name, we may 52845331Samw * actually care about it if this is share for 52855331Samw * a protocol that uses resource level sharing 52865331Samw * (SMB). We need to find the resource and, if 52875331Samw * it exists, make sure it belongs to the 52885331Samw * current share. If it doesn't exist, attempt 52895331Samw * to create it. 52905331Samw */ 52915331Samw 52925331Samw if (ret == SA_OK && resource != NULL) { 52935331Samw rsrc = sa_find_resource(handle, resource); 52945331Samw if (rsrc != NULL) { 52955331Samw if (share != sa_get_resource_parent(rsrc)) 52965331Samw ret = SA_DUPLICATE_NAME; 52975331Samw } else { 52985331Samw rsrc = sa_add_resource(share, resource, 52995331Samw persist, &ret); 53003034Sdougm } 53015331Samw if (features & SA_FEATURE_RESOURCE) 53025331Samw share = rsrc; 53033108Sdougm } 53045331Samw 53054653Sdougm /* Have a group to hold this share path */ 53064653Sdougm if (ret == SA_OK && options != NULL && 53074653Sdougm strlen(options) > 0) { 53084653Sdougm ret = sa_parse_legacy_options(share, 53094653Sdougm options, 53104653Sdougm protocol); 53113034Sdougm } 53124653Sdougm if (!zfs) { 53134653Sdougm /* 53145331Samw * ZFS shares never have a description 53155331Samw * and we can't store the values so 53165331Samw * don't try. 53174653Sdougm */ 53184653Sdougm if (ret == SA_OK && description != NULL) 53194653Sdougm ret = sa_set_share_description(share, 53204653Sdougm description); 53213034Sdougm } 53225331Samw if (ret == SA_OK && 53235331Samw strcmp(groupstatus, "enabled") == 0) { 53245331Samw if (rsrc != share) 53254653Sdougm ret = sa_enable_share(share, protocol); 53265331Samw else 53275331Samw ret = sa_enable_resource(rsrc, 53285331Samw protocol); 53294653Sdougm if (ret == SA_OK && 53304653Sdougm persist == SA_SHARE_PERMANENT) { 53314653Sdougm (void) sa_update_legacy(share, 53324653Sdougm protocol); 53334653Sdougm } 53344653Sdougm if (ret == SA_OK) 53354653Sdougm ret = sa_update_config(handle); 53364653Sdougm } 53373034Sdougm } 53385331Samw err: 53393034Sdougm if (ret != SA_OK) { 53404653Sdougm (void) fprintf(stderr, gettext("Could not share: %s: %s\n"), 53414653Sdougm sharepath, sa_errorstr(ret)); 53424653Sdougm ret = SA_LEGACY_ERR; 53433034Sdougm } 53443034Sdougm return (ret); 53453034Sdougm } 53463034Sdougm 53473034Sdougm /* 53483034Sdougm * sa_legacy_unshare(flags, argc, argv) 53493034Sdougm * 53503034Sdougm * Implements the original unshare command. 53513034Sdougm */ 53523034Sdougm int 53533910Sdougm sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[]) 53543034Sdougm { 53553034Sdougm char *protocol = "nfs"; /* for now */ 53563034Sdougm char *options = NULL; 53573034Sdougm char *sharepath = NULL; 53583034Sdougm int persist = SA_SHARE_TRANSIENT; 53593034Sdougm int argsused = 0; 53603034Sdougm int c; 53613034Sdougm int ret = SA_OK; 53623034Sdougm int true_legacy = 0; 53635331Samw uint64_t features = 0; 53645331Samw sa_resource_t resource = NULL; 53653034Sdougm char cmd[MAXPATHLEN]; 53665331Samw #ifdef lint 53675331Samw flags = flags; 53685331Samw options = options; 53695331Samw #endif 53703034Sdougm 53713034Sdougm while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) { 53724653Sdougm switch (c) { 53734653Sdougm case 'h': 53744653Sdougm case '?': 53754653Sdougm break; 53764653Sdougm case 'F': 53774653Sdougm protocol = optarg; 53784653Sdougm if (!sa_valid_protocol(protocol)) { 53794653Sdougm if (format_legacy_path(cmd, MAXPATHLEN, 53804653Sdougm protocol, "unshare") == 0 && 53814653Sdougm check_legacy_cmd(cmd)) { 53824653Sdougm true_legacy++; 53834653Sdougm } else { 53844653Sdougm (void) printf(gettext( 53854653Sdougm "Invalid file system name\n")); 53864653Sdougm return (SA_INVALID_PROTOCOL); 53874653Sdougm } 53884653Sdougm } 53894653Sdougm break; 53904653Sdougm case 'o': 53914653Sdougm options = optarg; 53924653Sdougm argsused++; 53934653Sdougm break; 53944653Sdougm case 'p': 53954653Sdougm persist = SA_SHARE_PERMANENT; 53964653Sdougm argsused++; 53974653Sdougm break; 53984653Sdougm default: 53994653Sdougm (void) printf(gettext("usage: %s\n"), 54004653Sdougm sa_get_usage(USAGE_UNSHARE)); 54014653Sdougm return (SA_OK); 54023034Sdougm } 54033034Sdougm } 54043034Sdougm 54054653Sdougm /* Have the info so construct what is needed */ 54064653Sdougm if (optind == argc || (optind + 1) < argc || options != NULL) { 54074653Sdougm ret = SA_SYNTAX_ERR; 54083034Sdougm } else { 54094653Sdougm sa_share_t share; 54104653Sdougm char dir[MAXPATHLEN]; 54114653Sdougm if (true_legacy) { 54124653Sdougm /* if still using legacy share/unshare, exec it */ 54134653Sdougm ret = run_legacy_command(cmd, argv); 54144653Sdougm return (ret); 54154653Sdougm } 54163663Sdougm /* 54173663Sdougm * Find the path in the internal configuration. If it 54183663Sdougm * isn't found, attempt to resolve the path via 54193663Sdougm * realpath() and try again. 54203663Sdougm */ 54214653Sdougm sharepath = argv[optind++]; 54224653Sdougm share = sa_find_share(handle, sharepath); 54234653Sdougm if (share == NULL) { 54244653Sdougm if (realpath(sharepath, dir) == NULL) { 54254653Sdougm ret = SA_NO_SUCH_PATH; 54264653Sdougm } else { 54274653Sdougm share = sa_find_share(handle, dir); 54284653Sdougm } 54293663Sdougm } 54305331Samw if (share == NULL) { 54315331Samw /* Could be a resource name so check that next */ 54325331Samw features = sa_proto_get_featureset(protocol); 54335331Samw resource = sa_find_resource(handle, sharepath); 54345331Samw if (resource != NULL) { 54355331Samw share = sa_get_resource_parent(resource); 54365331Samw if (features & SA_FEATURE_RESOURCE) 54375331Samw (void) sa_disable_resource(resource, 54385331Samw protocol); 54395331Samw if (persist == SA_SHARE_PERMANENT) { 54405331Samw ret = sa_remove_resource(resource); 54415331Samw if (ret == SA_OK) 54425331Samw ret = sa_update_config(handle); 54435331Samw } 54445331Samw /* 54455331Samw * If we still have a resource on the 54465331Samw * share, we don't disable the share 54475331Samw * itself. IF there aren't anymore, we 54485331Samw * need to remove the share. The 54495331Samw * removal will be done in the next 54505331Samw * section if appropriate. 54515331Samw */ 54525331Samw resource = sa_get_share_resource(share, NULL); 54535331Samw if (resource != NULL) 54545331Samw share = NULL; 54555331Samw } else if (ret == SA_OK) { 54565331Samw /* Didn't find path and no resource */ 54575331Samw ret = SA_BAD_PATH; 54585331Samw } 54595331Samw } 54605331Samw if (share != NULL && resource == NULL) { 54614653Sdougm ret = sa_disable_share(share, protocol); 54624653Sdougm /* 54634653Sdougm * Errors are ok and removal should still occur. The 54644653Sdougm * legacy unshare is more forgiving of errors than the 54654653Sdougm * remove-share subcommand which may need the force 54664653Sdougm * flag set for some error conditions. That is, the 54674653Sdougm * "unshare" command will always unshare if it can 54684653Sdougm * while "remove-share" might require the force option. 54694653Sdougm */ 54704653Sdougm if (persist == SA_SHARE_PERMANENT) { 54714653Sdougm ret = sa_remove_share(share); 54724653Sdougm if (ret == SA_OK) 54734653Sdougm ret = sa_update_config(handle); 54744653Sdougm } 54755331Samw } else if (ret == SA_OK && share == NULL && resource == NULL) { 54765331Samw /* 54775331Samw * If both share and resource are NULL, then 54785331Samw * share not found. If one or the other was 54795331Samw * found or there was an earlier error, we 54805331Samw * assume it was handled earlier. 54815331Samw */ 54824653Sdougm ret = SA_NOT_SHARED; 54833663Sdougm } 54843034Sdougm } 54853034Sdougm switch (ret) { 54863034Sdougm default: 54874653Sdougm (void) printf("%s: %s\n", sharepath, sa_errorstr(ret)); 54884653Sdougm ret = SA_LEGACY_ERR; 54894653Sdougm break; 54903034Sdougm case SA_SYNTAX_ERR: 54914653Sdougm (void) printf(gettext("usage: %s\n"), 54924653Sdougm sa_get_usage(USAGE_UNSHARE)); 54934653Sdougm break; 54943034Sdougm case SA_OK: 54954653Sdougm break; 54963034Sdougm } 54973034Sdougm return (ret); 54983034Sdougm } 54993034Sdougm 55003034Sdougm /* 55014653Sdougm * Common commands that implement the sub-commands used by all 55025331Samw * protocols. The entries are found via the lookup command 55033034Sdougm */ 55043034Sdougm 55053034Sdougm static sa_command_t commands[] = { 55063034Sdougm {"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET}, 55073034Sdougm {"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION}, 55083034Sdougm {"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION}, 55093034Sdougm {"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION}, 55103034Sdougm {"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION}, 55113034Sdougm {"list", 0, sa_list, USAGE_LIST}, 55123034Sdougm {"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET}, 55133034Sdougm {"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET}, 55143034Sdougm {"set", 0, sa_set, USAGE_SET, SVC_SET}, 55153034Sdougm {"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET}, 55163034Sdougm {"show", 0, sa_show, USAGE_SHOW}, 55173034Sdougm {"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION}, 55183034Sdougm {"start", CMD_NODISPLAY, sa_start_group, USAGE_START, 55195331Samw SVC_SET|SVC_ACTION}, 55203034Sdougm {"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION}, 55213034Sdougm {"unset", 0, sa_unset, USAGE_UNSET, SVC_SET}, 55223034Sdougm {"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION}, 55233034Sdougm {NULL, 0, NULL, NULL} 55243034Sdougm }; 55253034Sdougm 55263034Sdougm static char * 55273034Sdougm sa_get_usage(sa_usage_t index) 55283034Sdougm { 55293034Sdougm char *ret = NULL; 55303034Sdougm switch (index) { 55313034Sdougm case USAGE_ADD_SHARE: 55324653Sdougm ret = gettext("add-share [-nth] [-r resource-name] " 55334653Sdougm "[-d \"description text\"] -s sharepath group"); 55344653Sdougm break; 55353034Sdougm case USAGE_CREATE: 55364653Sdougm ret = gettext( 55374653Sdougm "create [-nvh] [-P proto [-p property=value]] group"); 55384653Sdougm break; 55393034Sdougm case USAGE_DELETE: 55404653Sdougm ret = gettext("delete [-nvh] [-P proto] [-f] group"); 55414653Sdougm break; 55423034Sdougm case USAGE_DISABLE: 55434653Sdougm ret = gettext("disable [-nvh] {-a | group ...}"); 55444653Sdougm break; 55453034Sdougm case USAGE_ENABLE: 55464653Sdougm ret = gettext("enable [-nvh] {-a | group ...}"); 55474653Sdougm break; 55483034Sdougm case USAGE_LIST: 55494653Sdougm ret = gettext("list [-vh] [-P proto]"); 55504653Sdougm break; 55513034Sdougm case USAGE_MOVE_SHARE: 55524653Sdougm ret = gettext( 55534653Sdougm "move-share [-nvh] -s sharepath destination-group"); 55544653Sdougm break; 55553034Sdougm case USAGE_REMOVE_SHARE: 55565331Samw ret = gettext( 55575331Samw "remove-share [-fnvh] {-s sharepath | -r resource} " 55585331Samw "group"); 55594653Sdougm break; 55603034Sdougm case USAGE_SET: 55614653Sdougm ret = gettext("set [-nvh] -P proto [-S optspace] " 55625331Samw "[-p property=value]* [-s sharepath] [-r resource]] " 55635331Samw "group"); 55644653Sdougm break; 55653034Sdougm case USAGE_SET_SECURITY: 55664653Sdougm ret = gettext("set-security [-nvh] -P proto -S security-type " 55674653Sdougm "[-p property=value]* group"); 55684653Sdougm break; 55693034Sdougm case USAGE_SET_SHARE: 55704653Sdougm ret = gettext("set-share [-nh] [-r resource] " 55714653Sdougm "[-d \"description text\"] -s sharepath group"); 55724653Sdougm break; 55733034Sdougm case USAGE_SHOW: 55744653Sdougm ret = gettext("show [-pvxh] [-P proto] [group ...]"); 55754653Sdougm break; 55763034Sdougm case USAGE_SHARE: 55774653Sdougm ret = gettext("share [-F fstype] [-p] [-o optionlist]" 55784653Sdougm "[-d description] [pathname [resourcename]]"); 55794653Sdougm break; 55803034Sdougm case USAGE_START: 55814653Sdougm ret = gettext("start [-vh] [-P proto] {-a | group ...}"); 55824653Sdougm break; 55833034Sdougm case USAGE_STOP: 55844653Sdougm ret = gettext("stop [-vh] [-P proto] {-a | group ...}"); 55854653Sdougm break; 55863034Sdougm case USAGE_UNSET: 55874653Sdougm ret = gettext("unset [-nvh] -P proto [-S optspace] " 55884653Sdougm "[-p property]* group"); 55894653Sdougm break; 55903034Sdougm case USAGE_UNSET_SECURITY: 55915331Samw ret = gettext("unset-security [-nvh] -P proto " 55925331Samw "-S security-type [-p property]* group"); 55934653Sdougm break; 55943034Sdougm case USAGE_UNSHARE: 55954653Sdougm ret = gettext( 55965331Samw "unshare [-F fstype] [-p] [-o optionlist] sharepath"); 55974653Sdougm break; 55983034Sdougm } 55993034Sdougm return (ret); 56003034Sdougm } 56013034Sdougm 56023034Sdougm /* 56033034Sdougm * sa_lookup(cmd, proto) 56043034Sdougm * 56053034Sdougm * Lookup the sub-command. proto isn't currently used, but it may 56063034Sdougm * eventually provide a way to provide protocol specific sub-commands. 56073034Sdougm */ 56083034Sdougm sa_command_t * 56093034Sdougm sa_lookup(char *cmd, char *proto) 56103034Sdougm { 56113034Sdougm int i; 56123034Sdougm size_t len; 56135331Samw #ifdef lint 56145331Samw proto = proto; 56155331Samw #endif 56163034Sdougm 56173034Sdougm len = strlen(cmd); 56183034Sdougm for (i = 0; commands[i].cmdname != NULL; i++) { 56194653Sdougm if (strncmp(cmd, commands[i].cmdname, len) == 0) 56204653Sdougm return (&commands[i]); 56213034Sdougm } 56223034Sdougm return (NULL); 56233034Sdougm } 56243034Sdougm 56253034Sdougm void 56263034Sdougm sub_command_help(char *proto) 56273034Sdougm { 56283034Sdougm int i; 56295331Samw #ifdef lint 56305331Samw proto = proto; 56315331Samw #endif 56323034Sdougm 56333034Sdougm (void) printf(gettext("\tsub-commands:\n")); 56343034Sdougm for (i = 0; commands[i].cmdname != NULL; i++) { 56354653Sdougm if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY))) 56364653Sdougm (void) printf("\t%s\n", 56374653Sdougm sa_get_usage((sa_usage_t)commands[i].cmdidx)); 56383034Sdougm } 56393034Sdougm } 5640