13034Sdougm /* 23034Sdougm * CDDL HEADER START 33034Sdougm * 43034Sdougm * The contents of this file are subject to the terms of the 53034Sdougm * Common Development and Distribution License (the "License"). 63034Sdougm * You may not use this file except in compliance with the License. 73034Sdougm * 83034Sdougm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 93034Sdougm * or http://www.opensolaris.org/os/licensing. 103034Sdougm * See the License for the specific language governing permissions 113034Sdougm * and limitations under the License. 123034Sdougm * 133034Sdougm * When distributing Covered Code, include this CDDL HEADER in each 143034Sdougm * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 153034Sdougm * If applicable, add the following below this CDDL HEADER, with the 163034Sdougm * fields enclosed by brackets "[]" replaced with your own identifying 173034Sdougm * information: Portions Copyright [yyyy] [name of copyright owner] 183034Sdougm * 193034Sdougm * CDDL HEADER END 203034Sdougm */ 213034Sdougm 223034Sdougm /* 23*5772Sas200622 * 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 2195331Samw static void 2205331Samw print_rsrc_desc(char *resource) 2215331Samw { 2225331Samw char *description; 2235331Samw char *desc; 2245331Samw 2255331Samw description = sa_get_resource_description(resource); 2265331Samw if (description != NULL) { 2275331Samw desc = conv_from_utf8(description); 2285331Samw if (desc != description) { 2295331Samw sa_free_share_description(description); 2305331Samw description = desc; 2315331Samw } 2325331Samw (void) printf("\t\"%s\"", description); 2335331Samw sa_free_share_description(description); 2345331Samw } 2355331Samw } 2365331Samw 2375331Samw static int 2385331Samw set_share_desc(sa_share_t share, char *description) 2395331Samw { 2405331Samw char *desc; 2415331Samw int ret; 2425331Samw 2435331Samw desc = conv_to_utf8(description); 2445331Samw ret = sa_set_share_description(share, desc); 2455331Samw if (description != desc) 2465331Samw sa_free_share_description(desc); 2475331Samw return (ret); 2485331Samw } 2495331Samw 2505331Samw /* 2515331Samw * add_list(list, item, data, proto) 2525331Samw * Adds a new list member that points holds item in the list. 2533034Sdougm * If list is NULL, it starts a new list. The function returns 2543034Sdougm * the first member of the list. 2553034Sdougm */ 2563034Sdougm struct list * 2575331Samw add_list(struct list *listp, void *item, void *data, char *proto) 2583034Sdougm { 2593034Sdougm struct list *new, *tmp; 2603034Sdougm 2613034Sdougm new = malloc(sizeof (struct list)); 2623034Sdougm if (new != NULL) { 2634653Sdougm new->next = NULL; 2644653Sdougm new->item = item; 2654653Sdougm new->itemdata = data; 2665331Samw new->proto = proto; 2673034Sdougm } else { 2684653Sdougm return (listp); 2693034Sdougm } 2703034Sdougm 2713034Sdougm if (listp == NULL) 2724653Sdougm return (new); 2733034Sdougm 2743034Sdougm for (tmp = listp; tmp->next != NULL; tmp = tmp->next) { 2753034Sdougm /* get to end of list */ 2763034Sdougm } 2773034Sdougm tmp->next = new; 2783034Sdougm return (listp); 2793034Sdougm } 2803034Sdougm 2813034Sdougm /* 2823034Sdougm * free_list(list) 2833034Sdougm * Given a list, free all the members of the list; 2843034Sdougm */ 2853034Sdougm static void 2863034Sdougm free_list(struct list *listp) 2873034Sdougm { 2883034Sdougm struct list *tmp; 2893034Sdougm while (listp != NULL) { 2904653Sdougm tmp = listp; 2914653Sdougm listp = listp->next; 2924653Sdougm free(tmp); 2933034Sdougm } 2943034Sdougm } 2953034Sdougm 2963034Sdougm /* 2973034Sdougm * check_authorization(instname, which) 2983034Sdougm * 2993034Sdougm * Checks to see if the specific type of authorization in which is 3003034Sdougm * enabled for the user in this SMF service instance. 3013034Sdougm */ 3023034Sdougm 3033034Sdougm static int 3043034Sdougm check_authorization(char *instname, int which) 3053034Sdougm { 3063034Sdougm scf_handle_t *handle = NULL; 3073034Sdougm scf_simple_prop_t *prop = NULL; 3083034Sdougm char svcstring[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1]; 3093034Sdougm char *authstr = NULL; 3103034Sdougm ssize_t numauths; 3114653Sdougm int ret = B_TRUE; 3123034Sdougm uid_t uid; 3133034Sdougm struct passwd *pw = NULL; 3143034Sdougm 3153034Sdougm uid = getuid(); 3163034Sdougm pw = getpwuid(uid); 3174653Sdougm if (pw == NULL) { 3184653Sdougm ret = B_FALSE; 3194653Sdougm } else { 3204653Sdougm /* 3214653Sdougm * Since names are restricted to SA_MAX_NAME_LEN won't 3224653Sdougm * overflow. 3234653Sdougm */ 3244653Sdougm (void) snprintf(svcstring, sizeof (svcstring), "%s:%s", 3254653Sdougm SA_SVC_FMRI_BASE, instname); 3264653Sdougm handle = scf_handle_create(SCF_VERSION); 3274653Sdougm if (handle != NULL) { 3284653Sdougm if (scf_handle_bind(handle) == 0) { 3294653Sdougm switch (which) { 3304653Sdougm case SVC_SET: 3314653Sdougm prop = scf_simple_prop_get(handle, 3324653Sdougm svcstring, "general", 3334653Sdougm SVC_AUTH_VALUE); 3344653Sdougm break; 3354653Sdougm case SVC_ACTION: 3364653Sdougm prop = scf_simple_prop_get(handle, 3374653Sdougm svcstring, "general", 3384653Sdougm SVC_AUTH_ACTION); 3394653Sdougm break; 3404653Sdougm } 3414653Sdougm } 3423034Sdougm } 3433034Sdougm } 3443034Sdougm /* make sure we have an authorization string property */ 3453034Sdougm if (prop != NULL) { 3464653Sdougm int i; 3474653Sdougm numauths = scf_simple_prop_numvalues(prop); 3484653Sdougm for (ret = 0, i = 0; i < numauths; i++) { 3494653Sdougm authstr = scf_simple_prop_next_astring(prop); 3504653Sdougm if (authstr != NULL) { 3514653Sdougm /* check if this user has one of the strings */ 3524653Sdougm if (chkauthattr(authstr, pw->pw_name)) { 3534653Sdougm ret = 1; 3544653Sdougm break; 3554653Sdougm } 3564653Sdougm } 3573034Sdougm } 3584653Sdougm endauthattr(); 3594653Sdougm scf_simple_prop_free(prop); 3603034Sdougm } else { 3614653Sdougm /* no authorization string defined */ 3624653Sdougm ret = 0; 3633034Sdougm } 3643034Sdougm if (handle != NULL) 3654653Sdougm scf_handle_destroy(handle); 3663034Sdougm return (ret); 3673034Sdougm } 3683034Sdougm 3693034Sdougm /* 3703034Sdougm * check_authorizations(instname, flags) 3713034Sdougm * 3723034Sdougm * check all the needed authorizations for the user in this service 3733034Sdougm * instance. Return value of 1(true) or 0(false) indicates whether 3743034Sdougm * there are authorizations for the user or not. 3753034Sdougm */ 3763034Sdougm 3773034Sdougm static int 3783034Sdougm check_authorizations(char *instname, int flags) 3793034Sdougm { 3803034Sdougm int ret1 = 0; 3813034Sdougm int ret2 = 0; 3823034Sdougm int ret; 3833034Sdougm 3843034Sdougm if (flags & SVC_SET) 3854653Sdougm ret1 = check_authorization(instname, SVC_SET); 3863034Sdougm if (flags & SVC_ACTION) 3874653Sdougm ret2 = check_authorization(instname, SVC_ACTION); 3883034Sdougm switch (flags) { 3893034Sdougm case SVC_ACTION: 3904653Sdougm ret = ret2; 3914653Sdougm break; 3923034Sdougm case SVC_SET: 3934653Sdougm ret = ret1; 3944653Sdougm break; 3953034Sdougm case SVC_ACTION|SVC_SET: 3964653Sdougm ret = ret1 & ret2; 3974653Sdougm break; 3983034Sdougm default: 3994653Sdougm /* if not flags set, we assume we don't need authorizations */ 4004653Sdougm ret = 1; 4013034Sdougm } 4023034Sdougm return (ret); 4033034Sdougm } 4043034Sdougm 4053034Sdougm /* 4065331Samw * notify_or_enable_share(share, protocol) 4075331Samw * 4085331Samw * Since some protocols don't want an "enable" when properties change, 4095331Samw * this function will use the protocol specific notify function 4105331Samw * first. If that fails, it will then attempt to use the 4115331Samw * sa_enable_share(). "protocol" is the protocol that was specified 4125331Samw * on the command line. 4135331Samw */ 4145331Samw static void 4155331Samw notify_or_enable_share(sa_share_t share, char *protocol) 4165331Samw { 4175331Samw sa_group_t group; 4185331Samw sa_optionset_t opt; 4195331Samw int ret = SA_OK; 4205331Samw char *path; 4215331Samw char *groupproto; 4225331Samw sa_share_t parent = share; 4235331Samw 4245331Samw /* If really a resource, get parent share */ 4255331Samw if (!sa_is_share(share)) { 4265331Samw parent = sa_get_resource_parent((sa_resource_t)share); 4275331Samw } 4285331Samw 4295331Samw /* 4305331Samw * Now that we've got a share in "parent", make sure it has a path. 4315331Samw */ 4325331Samw path = sa_get_share_attr(parent, "path"); 4335331Samw if (path == NULL) 4345331Samw return; 4355331Samw 4365331Samw group = sa_get_parent_group(parent); 4375331Samw 4385331Samw if (group == NULL) { 4395331Samw sa_free_attr_string(path); 4405331Samw return; 4415331Samw } 4425331Samw for (opt = sa_get_optionset(group, NULL); 4435331Samw opt != NULL; 4445331Samw opt = sa_get_next_optionset(opt)) { 4455331Samw groupproto = sa_get_optionset_attr(opt, "type"); 4465331Samw if (groupproto == NULL || 4475331Samw (protocol != NULL && strcmp(groupproto, protocol) != 0)) { 4485331Samw sa_free_attr_string(groupproto); 4495331Samw continue; 4505331Samw } 4515331Samw if (sa_is_share(share)) { 4525331Samw if ((ret = sa_proto_change_notify(share, 4535331Samw groupproto)) != SA_OK) { 4545331Samw ret = sa_enable_share(share, groupproto); 4555331Samw if (ret != SA_OK) { 4565331Samw (void) printf( 4575331Samw gettext("Could not reenable" 4585331Samw " share %s: %s\n"), 4595331Samw path, sa_errorstr(ret)); 4605331Samw } 4615331Samw } 4625331Samw } else { 4635331Samw /* Must be a resource */ 4645331Samw if ((ret = sa_proto_notify_resource(share, 4655331Samw groupproto)) != SA_OK) { 4665331Samw ret = sa_enable_resource(share, groupproto); 4675331Samw if (ret != SA_OK) { 4685331Samw (void) printf( 4695331Samw gettext("Could not " 4705331Samw "reenable resource %s: " 4715331Samw "%s\n"), path, 4725331Samw sa_errorstr(ret)); 4735331Samw } 4745331Samw } 4755331Samw } 4765331Samw sa_free_attr_string(groupproto); 4775331Samw } 4785331Samw sa_free_attr_string(path); 4795331Samw } 4805331Samw 4815331Samw /* 4825331Samw * enable_group(group, updateproto, notify, proto) 4833082Sdougm * 4843082Sdougm * enable all the shares in the specified group. This is a helper for 4853082Sdougm * enable_all_groups in order to simplify regular and subgroup (zfs) 4865331Samw * enabling. Group has already been checked for non-NULL. If notify 4875331Samw * is non-zero, attempt to use the notify interface rather than 4885331Samw * enable. 4893082Sdougm */ 4903082Sdougm static void 4915331Samw enable_group(sa_group_t group, char *updateproto, int notify, char *proto) 4923082Sdougm { 4933082Sdougm sa_share_t share; 4943082Sdougm 4953082Sdougm for (share = sa_get_share(group, NULL); 4963082Sdougm share != NULL; 4973082Sdougm share = sa_get_next_share(share)) { 4984653Sdougm if (updateproto != NULL) 4994653Sdougm (void) sa_update_legacy(share, updateproto); 5005331Samw if (notify) 5015331Samw notify_or_enable_share(share, proto); 5025331Samw else 5035331Samw (void) sa_enable_share(share, proto); 5043082Sdougm } 5053082Sdougm } 5063082Sdougm 5073082Sdougm /* 5084241Sdougm * isenabled(group) 5094241Sdougm * 5104241Sdougm * Returns B_TRUE if the group is enabled or B_FALSE if it isn't. 5114241Sdougm * Moved to separate function to reduce clutter in the code. 5124241Sdougm */ 5134241Sdougm 5144241Sdougm static int 5154241Sdougm isenabled(sa_group_t group) 5164241Sdougm { 5174241Sdougm char *state; 5184241Sdougm int ret = B_FALSE; 5194241Sdougm 5204241Sdougm if (group != NULL) { 5214653Sdougm state = sa_get_group_attr(group, "state"); 5224653Sdougm if (state != NULL) { 5235331Samw 5244653Sdougm if (strcmp(state, "enabled") == 0) 5254653Sdougm ret = B_TRUE; 5264653Sdougm sa_free_attr_string(state); 5274653Sdougm } 5284241Sdougm } 5294241Sdougm return (ret); 5304241Sdougm } 5314241Sdougm 5324241Sdougm /* 5333082Sdougm * enable_all_groups(list, setstate, online, updateproto) 5345331Samw * 5355331Samw * Given a list of groups, enable each one found. If updateproto is 5365331Samw * not NULL, then update all the shares for the protocol that was 5375331Samw * passed in. If enable is non-zero, tell enable_group to try the 5385331Samw * notify interface since this is a property change. 5393034Sdougm */ 5403034Sdougm static int 5413910Sdougm enable_all_groups(sa_handle_t handle, struct list *work, int setstate, 5425331Samw int online, char *updateproto, int enable) 5433034Sdougm { 5444241Sdougm int ret; 5453034Sdougm char instance[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1]; 5463034Sdougm char *state; 5473034Sdougm char *name; 5483034Sdougm char *zfs = NULL; 5493034Sdougm sa_group_t group; 5503082Sdougm sa_group_t subgroup; 5513034Sdougm 5524241Sdougm for (ret = SA_OK; work != NULL; work = work->next) { 5534653Sdougm group = (sa_group_t)work->item; 5544241Sdougm 5554241Sdougm /* 5564241Sdougm * If setstate == TRUE, then make sure to set 5574241Sdougm * enabled. This needs to be done here in order for 5584241Sdougm * the isenabled check to succeed on a newly enabled 5594241Sdougm * group. 5604241Sdougm */ 5614653Sdougm if (setstate == B_TRUE) { 5624653Sdougm ret = sa_set_group_attr(group, "state", "enabled"); 5634653Sdougm if (ret != SA_OK) 5644653Sdougm break; 5654653Sdougm } 5664241Sdougm 5674241Sdougm /* 5684241Sdougm * Check to see if group is enabled. If it isn't, skip 5694241Sdougm * the rest. We don't want shares starting if the 5704241Sdougm * group is disabled. The properties may have been 5714241Sdougm * updated, but there won't be a change until the 5724241Sdougm * group is enabled. 5734241Sdougm */ 5744653Sdougm if (!isenabled(group)) 5754653Sdougm continue; 5764653Sdougm 5774653Sdougm /* if itemdata != NULL then a single share */ 5784653Sdougm if (work->itemdata != NULL) { 5795331Samw if (enable) { 5805331Samw if (work->itemdata != NULL) 5815331Samw notify_or_enable_share(work->itemdata, 5825331Samw updateproto); 5835331Samw else 5845331Samw ret = SA_CONFIG_ERR; 5855331Samw } else { 5865331Samw if (sa_is_share(work->itemdata)) { 5875331Samw ret = sa_enable_share( 5885331Samw (sa_share_t)work->itemdata, 5895331Samw updateproto); 5905331Samw } else { 5915331Samw ret = sa_enable_resource( 5925331Samw (sa_resource_t)work->itemdata, 5935331Samw updateproto); 5945331Samw } 5955331Samw } 5963034Sdougm } 5974653Sdougm if (ret != SA_OK) 5984653Sdougm break; 5994653Sdougm 6004653Sdougm /* if itemdata == NULL then the whole group */ 6014653Sdougm if (work->itemdata == NULL) { 6024653Sdougm zfs = sa_get_group_attr(group, "zfs"); 6034653Sdougm /* 6045331Samw * If the share is managed by ZFS, don't 6054653Sdougm * update any of the protocols since ZFS is 6065331Samw * handling this. Updateproto will contain 6074653Sdougm * the name of the protocol that we want to 6084653Sdougm * update legacy files for. 6094653Sdougm */ 6105331Samw enable_group(group, zfs == NULL ? updateproto : NULL, 6115331Samw enable, work->proto); 6124653Sdougm for (subgroup = sa_get_sub_group(group); 6134653Sdougm subgroup != NULL; 6144653Sdougm subgroup = sa_get_next_group(subgroup)) { 6154653Sdougm /* never update legacy for ZFS subgroups */ 6165331Samw enable_group(subgroup, NULL, enable, 6175331Samw work->proto); 6183034Sdougm } 6193034Sdougm } 6204653Sdougm if (online) { 6214653Sdougm zfs = sa_get_group_attr(group, "zfs"); 6224653Sdougm name = sa_get_group_attr(group, "name"); 6234653Sdougm if (name != NULL) { 6244653Sdougm if (zfs == NULL) { 6254653Sdougm (void) snprintf(instance, 6264653Sdougm sizeof (instance), "%s:%s", 6274653Sdougm SA_SVC_FMRI_BASE, name); 6284653Sdougm state = smf_get_state(instance); 6294653Sdougm if (state == NULL || 6304653Sdougm strcmp(state, "online") != 0) { 6314653Sdougm (void) smf_enable_instance( 6324653Sdougm instance, 0); 6334653Sdougm free(state); 6344653Sdougm } 6354653Sdougm } else { 6364653Sdougm sa_free_attr_string(zfs); 6374653Sdougm zfs = NULL; 6384653Sdougm } 6394653Sdougm if (name != NULL) 6404653Sdougm sa_free_attr_string(name); 6414653Sdougm } 6424653Sdougm } 6433034Sdougm } 6443034Sdougm if (ret == SA_OK) { 6454653Sdougm ret = sa_update_config(handle); 6463034Sdougm } 6473034Sdougm return (ret); 6483034Sdougm } 6493034Sdougm 6503034Sdougm /* 6513034Sdougm * chk_opt(optlistp, security, proto) 6523034Sdougm * 6533034Sdougm * Do a sanity check on the optlist provided for the protocol. This 6543034Sdougm * is a syntax check and verification that the property is either a 6553034Sdougm * general or specific to a names optionset. 6563034Sdougm */ 6573034Sdougm 6583034Sdougm static int 6593034Sdougm chk_opt(struct options *optlistp, int security, char *proto) 6603034Sdougm { 6613034Sdougm struct options *optlist; 6623034Sdougm char *sep = ""; 6633034Sdougm int notfirst = 0; 6643034Sdougm int ret; 6653034Sdougm 6663034Sdougm for (optlist = optlistp; optlist != NULL; optlist = optlist->next) { 6674653Sdougm char *optname; 6684653Sdougm 6694653Sdougm optname = optlist->optname; 6704653Sdougm ret = OPT_ADD_OK; 6714653Sdougm /* extract property/value pair */ 6724653Sdougm if (sa_is_security(optname, proto)) { 6734653Sdougm if (!security) 6744653Sdougm ret = OPT_ADD_SECURITY; 6754653Sdougm } else { 6764653Sdougm if (security) 6774653Sdougm ret = OPT_ADD_PROPERTY; 6784653Sdougm } 6794653Sdougm if (ret != OPT_ADD_OK) { 6804653Sdougm if (notfirst == 0) 6814653Sdougm (void) printf( 6824653Sdougm gettext("Property syntax error: ")); 6834653Sdougm switch (ret) { 6844653Sdougm case OPT_ADD_SYNTAX: 6854653Sdougm (void) printf(gettext("%ssyntax error: %s"), 6863034Sdougm sep, optname); 6874653Sdougm sep = ", "; 6884653Sdougm break; 6894653Sdougm case OPT_ADD_SECURITY: 6904653Sdougm (void) printf(gettext("%s%s requires -S"), 6913034Sdougm optname, sep); 6924653Sdougm sep = ", "; 6934653Sdougm break; 6944653Sdougm case OPT_ADD_PROPERTY: 6954653Sdougm (void) printf( 6964653Sdougm gettext("%s%s not supported with -S"), 6973034Sdougm optname, sep); 6984653Sdougm sep = ", "; 6994653Sdougm break; 7004653Sdougm } 7014653Sdougm notfirst++; 7023034Sdougm } 7033034Sdougm } 7043034Sdougm if (notfirst) { 7054653Sdougm (void) printf("\n"); 7064653Sdougm ret = SA_SYNTAX_ERR; 7073034Sdougm } 7083034Sdougm return (ret); 7093034Sdougm } 7103034Sdougm 7113034Sdougm /* 7123034Sdougm * free_opt(optlist) 7133034Sdougm * Free the specified option list. 7143034Sdougm */ 7153034Sdougm static void 7163034Sdougm free_opt(struct options *optlist) 7173034Sdougm { 7183034Sdougm struct options *nextopt; 7193034Sdougm while (optlist != NULL) { 7203034Sdougm nextopt = optlist->next; 7213034Sdougm free(optlist); 7223034Sdougm optlist = nextopt; 7233034Sdougm } 7243034Sdougm } 7253034Sdougm 7263034Sdougm /* 7273034Sdougm * check property list for valid properties 7283034Sdougm * A null value is a remove which is always valid. 7293034Sdougm */ 7303034Sdougm static int 7313034Sdougm valid_options(struct options *optlist, char *proto, void *object, char *sec) 7323034Sdougm { 7333034Sdougm int ret = SA_OK; 7343034Sdougm struct options *cur; 7353034Sdougm sa_property_t prop; 7363034Sdougm sa_optionset_t parent = NULL; 7373034Sdougm 7383034Sdougm if (object != NULL) { 7394653Sdougm if (sec == NULL) 7404653Sdougm parent = sa_get_optionset(object, proto); 7414653Sdougm else 7424653Sdougm parent = sa_get_security(object, sec, proto); 7433034Sdougm } 7443034Sdougm 7453034Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 7464653Sdougm if (cur->optvalue == NULL) 7474653Sdougm continue; 7483034Sdougm prop = sa_create_property(cur->optname, cur->optvalue); 7493034Sdougm if (prop == NULL) 7504653Sdougm ret = SA_NO_MEMORY; 7513034Sdougm if (ret != SA_OK || 7523034Sdougm (ret = sa_valid_property(parent, proto, prop)) != SA_OK) { 7534653Sdougm (void) printf( 7544653Sdougm gettext("Could not add property %s: %s\n"), 7554653Sdougm cur->optname, sa_errorstr(ret)); 7563034Sdougm } 7573034Sdougm (void) sa_remove_property(prop); 7583034Sdougm } 7593034Sdougm return (ret); 7603034Sdougm } 7613034Sdougm 7623034Sdougm /* 7633034Sdougm * add_optionset(group, optlist, protocol, *err) 7643034Sdougm * Add the options in optlist to an optionset and then add the optionset 7653034Sdougm * to the group. 7663034Sdougm * 7673034Sdougm * The return value indicates if there was a "change" while errors are 7683034Sdougm * returned via the *err parameters. 7693034Sdougm */ 7703034Sdougm static int 7713034Sdougm add_optionset(sa_group_t group, struct options *optlist, char *proto, int *err) 7723034Sdougm { 7733034Sdougm sa_optionset_t optionset; 7743034Sdougm int ret = SA_OK; 7755331Samw int result = B_FALSE; 7763034Sdougm 7773034Sdougm optionset = sa_get_optionset(group, proto); 7783034Sdougm if (optionset == NULL) { 7794653Sdougm optionset = sa_create_optionset(group, proto); 7805331Samw if (optionset == NULL) 7815331Samw ret = SA_NO_MEMORY; 7825331Samw result = B_TRUE; /* adding a protocol is a change */ 7833034Sdougm } 7844653Sdougm if (optionset == NULL) { 7854653Sdougm ret = SA_NO_MEMORY; 7864653Sdougm goto out; 7874653Sdougm } 7884653Sdougm while (optlist != NULL) { 7893034Sdougm sa_property_t prop; 7903034Sdougm prop = sa_get_property(optionset, optlist->optname); 7913034Sdougm if (prop == NULL) { 7923034Sdougm /* 7933034Sdougm * add the property, but only if it is 7943034Sdougm * a non-NULL or non-zero length value 7953034Sdougm */ 7964653Sdougm if (optlist->optvalue != NULL) { 7974653Sdougm prop = sa_create_property(optlist->optname, 7984653Sdougm optlist->optvalue); 7994653Sdougm if (prop != NULL) { 8004653Sdougm ret = sa_valid_property(optionset, 8014653Sdougm proto, prop); 8024653Sdougm if (ret != SA_OK) { 8034653Sdougm (void) sa_remove_property(prop); 8044653Sdougm (void) printf(gettext("Could " 8054653Sdougm "not add property " 8064653Sdougm "%s: %s\n"), 8074653Sdougm optlist->optname, 8084653Sdougm sa_errorstr(ret)); 8094653Sdougm } 8104653Sdougm } 8114653Sdougm if (ret == SA_OK) { 8124653Sdougm ret = sa_add_property(optionset, prop); 8134653Sdougm if (ret != SA_OK) { 8144653Sdougm (void) printf(gettext( 8154653Sdougm "Could not add property " 8164653Sdougm "%s: %s\n"), 8174653Sdougm optlist->optname, 8184653Sdougm sa_errorstr(ret)); 8194653Sdougm } else { 8204653Sdougm /* there was a change */ 8215331Samw result = B_TRUE; 8224653Sdougm } 8234653Sdougm } 8243034Sdougm } 8254653Sdougm } else { 8264653Sdougm ret = sa_update_property(prop, optlist->optvalue); 8274653Sdougm /* should check to see if value changed */ 8284653Sdougm if (ret != SA_OK) { 8294653Sdougm (void) printf(gettext("Could not update " 8304653Sdougm "property %s: %s\n"), optlist->optname, 8314653Sdougm sa_errorstr(ret)); 8324653Sdougm } else { 8335331Samw result = B_TRUE; 8343034Sdougm } 8353034Sdougm } 8363034Sdougm optlist = optlist->next; 8373034Sdougm } 8384653Sdougm ret = sa_commit_properties(optionset, 0); 8394653Sdougm 8404653Sdougm out: 8413034Sdougm if (err != NULL) 8424653Sdougm *err = ret; 8433034Sdougm return (result); 8443034Sdougm } 8453034Sdougm 8463034Sdougm /* 8475331Samw * resource_compliant(group) 8485331Samw * 8495331Samw * Go through all the shares in the group. Assume compliant, but if 8505331Samw * any share doesn't have at least one resource name, it isn't 8515331Samw * compliant. 8525331Samw */ 8535331Samw static int 8545331Samw resource_compliant(sa_group_t group) 8555331Samw { 8565331Samw sa_share_t share; 8575331Samw 8585331Samw for (share = sa_get_share(group, NULL); share != NULL; 8595331Samw share = sa_get_next_share(share)) { 8605331Samw if (sa_get_share_resource(share, NULL) == NULL) { 8615331Samw return (B_FALSE); 8625331Samw } 8635331Samw } 8645331Samw return (B_TRUE); 8655331Samw } 8665331Samw 8675331Samw /* 8685331Samw * fix_path(path) 8695331Samw * 8705331Samw * change all illegal characters to something else. For now, all get 8715331Samw * converted to '_' and the leading '/' is stripped off. This is used 8725331Samw * to construct an resource name (SMB share name) that is valid. 8735331Samw * Caller must pass a valid path. 8745331Samw */ 8755331Samw static void 8765331Samw fix_path(char *path) 8775331Samw { 8785331Samw char *cp; 8795331Samw size_t len; 8805331Samw 8815331Samw assert(path != NULL); 8825331Samw 8835331Samw /* make sure we are appropriate length */ 8845331Samw cp = path + 1; /* skip leading slash */ 8855331Samw while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) { 8865331Samw cp = strchr(cp, '/'); 8875331Samw if (cp != NULL) 8885331Samw cp++; 8895331Samw } 8905331Samw /* two cases - cp == NULL and cp is substring of path */ 8915331Samw if (cp == NULL) { 8925331Samw /* just take last SA_MAX_RESOURCE_NAME chars */ 8935331Samw len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME; 8945331Samw (void) memmove(path, path + len, SA_MAX_RESOURCE_NAME); 8955331Samw path[SA_MAX_RESOURCE_NAME] = '\0'; 8965331Samw } else { 8975331Samw len = strlen(cp) + 1; 8985331Samw (void) memmove(path, cp, len); 8995331Samw } 9005331Samw 9015331Samw /* 9025331Samw * Don't want any of the characters that are not allowed 9035331Samw * in and SMB share name. Replace them with '_'. 9045331Samw */ 9055331Samw while (*path) { 9065331Samw switch (*path) { 9075331Samw case '/': 9085331Samw case '"': 9095331Samw case '\\': 9105331Samw case '[': 9115331Samw case ']': 9125331Samw case ':': 9135331Samw case '|': 9145331Samw case '<': 9155331Samw case '>': 9165331Samw case '+': 9175331Samw case ';': 9185331Samw case ',': 9195331Samw case '?': 9205331Samw case '*': 9215331Samw case '=': 9225331Samw case '\t': 9235331Samw *path = '_'; 9245331Samw break; 9255331Samw } 9265331Samw path++; 9275331Samw } 9285331Samw } 9295331Samw 9305331Samw /* 9315331Samw * name_adjust(path, count) 9325331Samw * 9335331Samw * Add a ~<count> in place of last few characters. The total number of 9345331Samw * characters is dependent on count. 9355331Samw */ 9365331Samw #define MAX_MANGLE_NUMBER 10000 9375331Samw 9385331Samw static int 9395331Samw name_adjust(char *path, int count) 9405331Samw { 9415331Samw size_t len; 9425331Samw 9435331Samw len = strlen(path) - 2; 9445331Samw if (count > 10) 9455331Samw len--; 9465331Samw if (count > 100) 9475331Samw len--; 9485331Samw if (count > 1000) 9495331Samw len--; 9505331Samw if (len > 0) 9515331Samw (void) sprintf(path + len, "~%d", count); 9525331Samw else 9535331Samw return (SA_BAD_VALUE); 9545331Samw 9555331Samw return (SA_OK); 9565331Samw } 9575331Samw 9585331Samw /* 9595331Samw * make_resources(group) 9605331Samw * 9615331Samw * Go through all the shares in the group and make them have resource 9625331Samw * names. 9635331Samw */ 9645331Samw static void 9655331Samw make_resources(sa_group_t group) 9665331Samw { 9675331Samw sa_share_t share; 9685331Samw int count; 9695331Samw int err = SA_OK; 9705331Samw 9715331Samw for (share = sa_get_share(group, NULL); share != NULL; 9725331Samw share = sa_get_next_share(share)) { 9735331Samw /* Skip those with resources */ 9745331Samw if (sa_get_share_resource(share, NULL) == NULL) { 9755331Samw char *path; 9765331Samw path = sa_get_share_attr(share, "path"); 9775331Samw if (path == NULL) 9785331Samw continue; 9795331Samw fix_path(path); 9805331Samw count = 0; /* reset for next resource */ 9815331Samw while (sa_add_resource(share, path, 9825331Samw SA_SHARE_PERMANENT, &err) == NULL && 9835331Samw err == SA_DUPLICATE_NAME) { 9845331Samw int ret; 9855331Samw ret = name_adjust(path, count); 9865331Samw count++; 9875331Samw if (ret != SA_OK || 9885331Samw count >= MAX_MANGLE_NUMBER) { 9895331Samw (void) printf(gettext( 9905331Samw "Cannot create resource name for" 9915331Samw " path: %s\n"), path); 9925331Samw break; 9935331Samw } 9945331Samw } 9955331Samw sa_free_attr_string(path); 9965331Samw } 9975331Samw } 9985331Samw } 9995331Samw 10005331Samw /* 10013034Sdougm * sa_create(flags, argc, argv) 10023034Sdougm * create a new group 10033034Sdougm * this may or may not have a protocol associated with it. 10043034Sdougm * No protocol means "all" protocols in this case. 10053034Sdougm */ 10063034Sdougm static int 10073910Sdougm sa_create(sa_handle_t handle, int flags, int argc, char *argv[]) 10083034Sdougm { 10093034Sdougm char *groupname; 10103034Sdougm 10113034Sdougm sa_group_t group; 10125331Samw int force = 0; 10133034Sdougm int verbose = 0; 10143034Sdougm int dryrun = 0; 10153034Sdougm int c; 10163034Sdougm char *protocol = NULL; 10173034Sdougm int ret = SA_OK; 10183034Sdougm struct options *optlist = NULL; 10193034Sdougm int err = 0; 10203034Sdougm int auth; 10213034Sdougm 10225331Samw while ((c = getopt(argc, argv, "?fhvnP:p:")) != EOF) { 10234653Sdougm switch (c) { 10245331Samw case 'f': 10255331Samw force++; 10265331Samw break; 10274653Sdougm case 'v': 10284653Sdougm verbose++; 10294653Sdougm break; 10304653Sdougm case 'n': 10314653Sdougm dryrun++; 10324653Sdougm break; 10334653Sdougm case 'P': 10345331Samw if (protocol != NULL) { 10355331Samw (void) printf(gettext("Specifying " 10365331Samw "multiple protocols " 10375331Samw "not supported: %s\n"), protocol); 10385331Samw return (SA_SYNTAX_ERR); 10395331Samw } 10404653Sdougm protocol = optarg; 10414653Sdougm if (sa_valid_protocol(protocol)) 10424653Sdougm break; 10434653Sdougm (void) printf(gettext( 10444653Sdougm "Invalid protocol specified: %s\n"), protocol); 10454653Sdougm return (SA_INVALID_PROTOCOL); 10464653Sdougm break; 10474653Sdougm case 'p': 10484653Sdougm ret = add_opt(&optlist, optarg, 0); 10494653Sdougm switch (ret) { 10504653Sdougm case OPT_ADD_SYNTAX: 10514653Sdougm (void) printf(gettext( 10524653Sdougm "Property syntax error for property: %s\n"), 10534653Sdougm optarg); 10544653Sdougm return (SA_SYNTAX_ERR); 10554653Sdougm case OPT_ADD_SECURITY: 10564653Sdougm (void) printf(gettext( 10574653Sdougm "Security properties need " 10584653Sdougm "to be set with set-security: %s\n"), 10594653Sdougm optarg); 10604653Sdougm return (SA_SYNTAX_ERR); 10614653Sdougm default: 10624653Sdougm break; 10634653Sdougm } 10644653Sdougm break; 10654653Sdougm default: 10664653Sdougm case 'h': 10674653Sdougm case '?': 10684653Sdougm (void) printf(gettext("usage: %s\n"), 10694653Sdougm sa_get_usage(USAGE_CREATE)); 10704653Sdougm return (0); 10713034Sdougm } 10723034Sdougm } 10733034Sdougm 10743034Sdougm if (optind >= argc) { 10754653Sdougm (void) printf(gettext("usage: %s\n"), 10764653Sdougm sa_get_usage(USAGE_CREATE)); 10774653Sdougm (void) printf(gettext("\tgroup must be specified.\n")); 10784653Sdougm return (SA_BAD_PATH); 10793034Sdougm } 10803034Sdougm 10813034Sdougm if ((optind + 1) < argc) { 10824653Sdougm (void) printf(gettext("usage: %s\n"), 10834653Sdougm sa_get_usage(USAGE_CREATE)); 10844653Sdougm (void) printf(gettext("\textraneous group(s) at end\n")); 10854653Sdougm return (SA_SYNTAX_ERR); 10863034Sdougm } 10873034Sdougm 10883034Sdougm if (protocol == NULL && optlist != NULL) { 10894653Sdougm /* lookup default protocol */ 10904653Sdougm (void) printf(gettext("usage: %s\n"), 10914653Sdougm sa_get_usage(USAGE_CREATE)); 10924653Sdougm (void) printf(gettext("\tprotocol must be specified " 10934653Sdougm "with properties\n")); 10944653Sdougm return (SA_INVALID_PROTOCOL); 10953034Sdougm } 10963034Sdougm 10973034Sdougm if (optlist != NULL) 10984653Sdougm ret = chk_opt(optlist, 0, protocol); 10993034Sdougm if (ret == OPT_ADD_SECURITY) { 11004653Sdougm (void) printf(gettext("Security properties not " 11014653Sdougm "supported with create\n")); 11024653Sdougm return (SA_SYNTAX_ERR); 11033034Sdougm } 11043034Sdougm 11053034Sdougm /* 11064653Sdougm * If a group already exists, we can only add a new protocol 11073034Sdougm * to it and not create a new one or add the same protocol 11083034Sdougm * again. 11093034Sdougm */ 11103034Sdougm 11113034Sdougm groupname = argv[optind]; 11123034Sdougm 11133034Sdougm auth = check_authorizations(groupname, flags); 11143034Sdougm 11153910Sdougm group = sa_get_group(handle, groupname); 11163034Sdougm if (group != NULL) { 11174653Sdougm /* group exists so must be a protocol add */ 11184653Sdougm if (protocol != NULL) { 11194653Sdougm if (has_protocol(group, protocol)) { 11204653Sdougm (void) printf(gettext( 11214653Sdougm "Group \"%s\" already exists" 11224653Sdougm " with protocol %s\n"), groupname, 11234653Sdougm protocol); 11244653Sdougm ret = SA_DUPLICATE_NAME; 11254653Sdougm } 11264653Sdougm } else { 11274653Sdougm /* must add new protocol */ 11284653Sdougm (void) printf(gettext( 11294653Sdougm "Group already exists and no protocol " 11304653Sdougm "specified.\n")); 11314653Sdougm ret = SA_DUPLICATE_NAME; 11323034Sdougm } 11333034Sdougm } else { 11343034Sdougm /* 11353034Sdougm * is it a valid name? Must comply with SMF instance 11363034Sdougm * name restrictions. 11373034Sdougm */ 11384653Sdougm if (!sa_valid_group_name(groupname)) { 11394653Sdougm ret = SA_INVALID_NAME; 11404653Sdougm (void) printf(gettext("Invalid group name: %s\n"), 11414653Sdougm groupname); 11424653Sdougm } 11433034Sdougm } 11443034Sdougm if (ret == SA_OK) { 11454653Sdougm /* check protocol vs optlist */ 11464653Sdougm if (optlist != NULL) { 11474653Sdougm /* check options, if any, for validity */ 11484653Sdougm ret = valid_options(optlist, protocol, group, NULL); 11494653Sdougm } 11503034Sdougm } 11513034Sdougm if (ret == SA_OK && !dryrun) { 11524653Sdougm if (group == NULL) { 11534653Sdougm group = sa_create_group(handle, (char *)groupname, 11544653Sdougm &err); 11553034Sdougm } 11564653Sdougm if (group != NULL) { 11574653Sdougm sa_optionset_t optionset; 11585331Samw /* 11595331Samw * First check to see if the new protocol is one that 11605331Samw * requires resource names and make sure we are 11615331Samw * compliant before proceeding. 11625331Samw */ 11635331Samw if (protocol != NULL) { 11645331Samw uint64_t features; 11655331Samw 11665331Samw features = sa_proto_get_featureset(protocol); 11675331Samw if ((features & SA_FEATURE_RESOURCE) && 11685331Samw !resource_compliant(group)) { 11695331Samw if (force) { 11705331Samw make_resources(group); 11715331Samw } else { 11725331Samw ret = SA_RESOURCE_REQUIRED; 11735331Samw (void) printf( 11745331Samw gettext("Protocol " 11755331Samw "requires resource " 11765331Samw "names to be " 11775331Samw "set: %s\n"), 11785331Samw protocol); 11795331Samw goto err; 11805331Samw } 11815331Samw } 11825331Samw } 11834653Sdougm if (optlist != NULL) { 11844653Sdougm (void) add_optionset(group, optlist, protocol, 11854653Sdougm &ret); 11864653Sdougm } else if (protocol != NULL) { 11874653Sdougm optionset = sa_create_optionset(group, 11884653Sdougm protocol); 11894653Sdougm if (optionset == NULL) 11904653Sdougm ret = SA_NO_MEMORY; 11914653Sdougm } else if (protocol == NULL) { 11924653Sdougm char **protolist; 11934653Sdougm int numprotos, i; 11944653Sdougm numprotos = sa_get_protocols(&protolist); 11954653Sdougm for (i = 0; i < numprotos; i++) { 11964653Sdougm optionset = sa_create_optionset(group, 11974653Sdougm protolist[i]); 11984653Sdougm } 11994653Sdougm if (protolist != NULL) 12004653Sdougm free(protolist); 12014653Sdougm } 12023034Sdougm /* 12034653Sdougm * We have a group and legal additions 12043034Sdougm */ 12054653Sdougm if (ret == SA_OK) { 12064653Sdougm /* 12074653Sdougm * Commit to configuration for protocols that 12084653Sdougm * need to do block updates. For NFS, this 12094653Sdougm * doesn't do anything but it will be run for 12104653Sdougm * all protocols that implement the 12114653Sdougm * appropriate plugin. 12124653Sdougm */ 12134653Sdougm ret = sa_update_config(handle); 12144653Sdougm } else { 12154653Sdougm if (group != NULL) 12164653Sdougm (void) sa_remove_group(group); 12174653Sdougm } 12183034Sdougm } else { 12194653Sdougm ret = err; 12204653Sdougm (void) printf(gettext("Could not create group: %s\n"), 12214653Sdougm sa_errorstr(ret)); 12223034Sdougm } 12233034Sdougm } 12243034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 12254653Sdougm (void) printf(gettext("Command would fail: %s\n"), 12264653Sdougm sa_errorstr(SA_NO_PERMISSION)); 12274653Sdougm ret = SA_NO_PERMISSION; 12283034Sdougm } 12295331Samw err: 12303034Sdougm free_opt(optlist); 12313034Sdougm return (ret); 12323034Sdougm } 12333034Sdougm 12343034Sdougm /* 12353034Sdougm * group_status(group) 12363034Sdougm * 12373034Sdougm * return the current status (enabled/disabled) of the group. 12383034Sdougm */ 12393034Sdougm 12403034Sdougm static char * 12413034Sdougm group_status(sa_group_t group) 12423034Sdougm { 12433034Sdougm char *state; 12443034Sdougm int enabled = 0; 12453034Sdougm 12463034Sdougm state = sa_get_group_attr(group, "state"); 12473034Sdougm if (state != NULL) { 12484653Sdougm if (strcmp(state, "enabled") == 0) { 12494653Sdougm enabled = 1; 12504653Sdougm } 12514653Sdougm sa_free_attr_string(state); 12523034Sdougm } 12534255Sdougm return (enabled ? "enabled" : "disabled"); 12543034Sdougm } 12553034Sdougm 12563034Sdougm /* 12573034Sdougm * sa_delete(flags, argc, argv) 12583034Sdougm * 12593034Sdougm * Delete a group. 12603034Sdougm */ 12613034Sdougm 12623034Sdougm static int 12633910Sdougm sa_delete(sa_handle_t handle, int flags, int argc, char *argv[]) 12643034Sdougm { 12653034Sdougm char *groupname; 12663034Sdougm sa_group_t group; 12673034Sdougm sa_share_t share; 12683034Sdougm int verbose = 0; 12693034Sdougm int dryrun = 0; 12703034Sdougm int force = 0; 12713034Sdougm int c; 12723034Sdougm char *protocol = NULL; 12733034Sdougm char *sectype = NULL; 12743034Sdougm int ret = SA_OK; 12753034Sdougm int auth; 12763034Sdougm 12773034Sdougm while ((c = getopt(argc, argv, "?hvnP:fS:")) != EOF) { 12784653Sdougm switch (c) { 12794653Sdougm case 'v': 12804653Sdougm verbose++; 12814653Sdougm break; 12824653Sdougm case 'n': 12834653Sdougm dryrun++; 12844653Sdougm break; 12854653Sdougm case 'P': 12865331Samw if (protocol != NULL) { 12875331Samw (void) printf(gettext("Specifying " 12885331Samw "multiple protocols " 12895331Samw "not supported: %s\n"), protocol); 12905331Samw return (SA_SYNTAX_ERR); 12915331Samw } 12924653Sdougm protocol = optarg; 12934653Sdougm if (!sa_valid_protocol(protocol)) { 12944653Sdougm (void) printf(gettext("Invalid protocol " 12955331Samw "specified: %s\n"), protocol); 12964653Sdougm return (SA_INVALID_PROTOCOL); 12974653Sdougm } 12984653Sdougm break; 12994653Sdougm case 'S': 13005331Samw if (sectype != NULL) { 13015331Samw (void) printf(gettext("Specifying " 13025331Samw "multiple property " 13035331Samw "spaces not supported: %s\n"), sectype); 13045331Samw return (SA_SYNTAX_ERR); 13055331Samw } 13064653Sdougm sectype = optarg; 13074653Sdougm break; 13084653Sdougm case 'f': 13094653Sdougm force++; 13104653Sdougm break; 13114653Sdougm default: 13124653Sdougm case 'h': 13134653Sdougm case '?': 13144653Sdougm (void) printf(gettext("usage: %s\n"), 13154653Sdougm sa_get_usage(USAGE_DELETE)); 13164653Sdougm return (0); 13173034Sdougm } 13183034Sdougm } 13193034Sdougm 13203034Sdougm if (optind >= argc) { 13214653Sdougm (void) printf(gettext("usage: %s\n"), 13224653Sdougm sa_get_usage(USAGE_DELETE)); 13234653Sdougm (void) printf(gettext("\tgroup must be specified.\n")); 13244653Sdougm return (SA_SYNTAX_ERR); 13253034Sdougm } 13263034Sdougm 13273034Sdougm if ((optind + 1) < argc) { 13284653Sdougm (void) printf(gettext("usage: %s\n"), 13294653Sdougm sa_get_usage(USAGE_DELETE)); 13304653Sdougm (void) printf(gettext("\textraneous group(s) at end\n")); 13314653Sdougm return (SA_SYNTAX_ERR); 13323034Sdougm } 13333034Sdougm 13343034Sdougm if (sectype != NULL && protocol == NULL) { 13354653Sdougm (void) printf(gettext("usage: %s\n"), 13364653Sdougm sa_get_usage(USAGE_DELETE)); 13374653Sdougm (void) printf(gettext("\tsecurity requires protocol to be " 13384653Sdougm "specified.\n")); 13394653Sdougm return (SA_SYNTAX_ERR); 13403034Sdougm } 13413034Sdougm 13423034Sdougm /* 13433034Sdougm * Determine if the group already exists since it must in 13443034Sdougm * order to be removed. 13453034Sdougm * 13463034Sdougm * We can delete when: 13473034Sdougm * 13483034Sdougm * - group is empty 13493034Sdougm * - force flag is set 13503034Sdougm * - if protocol specified, only delete the protocol 13513034Sdougm */ 13523034Sdougm 13533034Sdougm groupname = argv[optind]; 13543910Sdougm group = sa_get_group(handle, groupname); 13553034Sdougm if (group == NULL) { 13563034Sdougm ret = SA_NO_SUCH_GROUP; 13574653Sdougm goto done; 13584653Sdougm } 13594653Sdougm auth = check_authorizations(groupname, flags); 13604653Sdougm if (protocol == NULL) { 13613034Sdougm share = sa_get_share(group, NULL); 13623034Sdougm if (share != NULL) 13634653Sdougm ret = SA_BUSY; 13643034Sdougm if (share == NULL || (share != NULL && force == 1)) { 13654653Sdougm ret = SA_OK; 13664653Sdougm if (!dryrun) { 13674653Sdougm while (share != NULL) { 13684653Sdougm sa_share_t next_share; 13694653Sdougm next_share = sa_get_next_share(share); 13704653Sdougm /* 13714653Sdougm * need to do the disable of 13724653Sdougm * each share, but don't 13734653Sdougm * actually do anything on a 13744653Sdougm * dryrun. 13754653Sdougm */ 13764653Sdougm ret = sa_disable_share(share, NULL); 13774653Sdougm ret = sa_remove_share(share); 13784653Sdougm share = next_share; 13794653Sdougm } 13804653Sdougm ret = sa_remove_group(group); 13813034Sdougm } 13823034Sdougm } 13834653Sdougm /* Commit to configuration if not a dryrun */ 13843034Sdougm if (!dryrun && ret == SA_OK) { 13854653Sdougm ret = sa_update_config(handle); 13863034Sdougm } 13874653Sdougm } else { 13883034Sdougm /* a protocol delete */ 13893034Sdougm sa_optionset_t optionset; 13903034Sdougm sa_security_t security; 13915331Samw if (sectype != NULL) { 13924653Sdougm /* only delete specified security */ 13934653Sdougm security = sa_get_security(group, sectype, protocol); 13944653Sdougm if (security != NULL && !dryrun) 13954653Sdougm ret = sa_destroy_security(security); 13964653Sdougm else 13974653Sdougm ret = SA_INVALID_PROTOCOL; 13983034Sdougm } else { 13994653Sdougm optionset = sa_get_optionset(group, protocol); 14004653Sdougm if (optionset != NULL && !dryrun) { 14014653Sdougm /* 14024653Sdougm * have an optionset with 14034653Sdougm * protocol to delete 14044653Sdougm */ 14054653Sdougm ret = sa_destroy_optionset(optionset); 14064653Sdougm /* 14074653Sdougm * Now find all security sets 14084653Sdougm * for the protocol and remove 14094653Sdougm * them. Don't remove other 14104653Sdougm * protocols. 14114653Sdougm */ 14124653Sdougm for (security = 14134653Sdougm sa_get_security(group, NULL, NULL); 14144653Sdougm ret == SA_OK && security != NULL; 14154653Sdougm security = sa_get_next_security(security)) { 14164653Sdougm char *secprot; 14174653Sdougm secprot = sa_get_security_attr(security, 14184653Sdougm "type"); 14194653Sdougm if (secprot != NULL && 14204653Sdougm strcmp(secprot, protocol) == 0) 14214653Sdougm ret = sa_destroy_security( 14224653Sdougm security); 14234653Sdougm if (secprot != NULL) 14244653Sdougm sa_free_attr_string(secprot); 14254653Sdougm } 14264653Sdougm } else { 14274653Sdougm if (!dryrun) 14284653Sdougm ret = SA_INVALID_PROTOCOL; 14293034Sdougm } 14303034Sdougm } 14315331Samw /* 14325331Samw * With the protocol items removed, make sure that all 14335331Samw * the shares are updated in the legacy files, if 14345331Samw * necessary. 14355331Samw */ 14365331Samw for (share = sa_get_share(group, NULL); 14375331Samw share != NULL; 14385331Samw share = sa_get_next_share(share)) { 14395331Samw (void) sa_delete_legacy(share, protocol); 14405331Samw } 14413034Sdougm } 14424653Sdougm 14434653Sdougm done: 14443034Sdougm if (ret != SA_OK) { 14454653Sdougm (void) printf(gettext("Could not delete group: %s\n"), 14464653Sdougm sa_errorstr(ret)); 14473034Sdougm } else if (dryrun && !auth && verbose) { 14484653Sdougm (void) printf(gettext("Command would fail: %s\n"), 14494653Sdougm sa_errorstr(SA_NO_PERMISSION)); 14503034Sdougm } 14513034Sdougm return (ret); 14523034Sdougm } 14533034Sdougm 14543034Sdougm /* 14553034Sdougm * strndupr(*buff, str, buffsize) 14563034Sdougm * 14573034Sdougm * used with small strings to duplicate and possibly increase the 14583034Sdougm * buffer size of a string. 14593034Sdougm */ 14603034Sdougm static char * 14613034Sdougm strndupr(char *buff, char *str, int *buffsize) 14623034Sdougm { 14633034Sdougm int limit; 14643034Sdougm char *orig_buff = buff; 14653034Sdougm 14663034Sdougm if (buff == NULL) { 14674653Sdougm buff = (char *)malloc(64); 14684653Sdougm if (buff == NULL) 14694653Sdougm return (NULL); 14704653Sdougm *buffsize = 64; 14714653Sdougm buff[0] = '\0'; 14723034Sdougm } 14733034Sdougm limit = strlen(buff) + strlen(str) + 1; 14743034Sdougm if (limit > *buffsize) { 14754653Sdougm limit = *buffsize = *buffsize + ((limit / 64) + 64); 14764653Sdougm buff = realloc(buff, limit); 14773034Sdougm } 14783034Sdougm if (buff != NULL) { 14794653Sdougm (void) strcat(buff, str); 14803034Sdougm } else { 14814653Sdougm /* if it fails, fail it hard */ 14824653Sdougm if (orig_buff != NULL) 14834653Sdougm free(orig_buff); 14843034Sdougm } 14853034Sdougm return (buff); 14863034Sdougm } 14873034Sdougm 14883034Sdougm /* 14893034Sdougm * group_proto(group) 14903034Sdougm * 14913034Sdougm * return a string of all the protocols (space separated) associated 14923034Sdougm * with this group. 14933034Sdougm */ 14943034Sdougm 14953034Sdougm static char * 14963034Sdougm group_proto(sa_group_t group) 14973034Sdougm { 14983034Sdougm sa_optionset_t optionset; 14993034Sdougm char *proto; 15003034Sdougm char *buff = NULL; 15013034Sdougm int buffsize = 0; 15023034Sdougm int addspace = 0; 15033034Sdougm /* 15043034Sdougm * get the protocol list by finding the optionsets on this 15053034Sdougm * group and extracting the type value. The initial call to 15063034Sdougm * strndupr() initailizes buff. 15073034Sdougm */ 15083034Sdougm buff = strndupr(buff, "", &buffsize); 15093034Sdougm if (buff != NULL) { 15104653Sdougm for (optionset = sa_get_optionset(group, NULL); 15114653Sdougm optionset != NULL && buff != NULL; 15124653Sdougm optionset = sa_get_next_optionset(optionset)) { 15134653Sdougm /* 15144653Sdougm * extract out the protocol type from this optionset 15154653Sdougm * and append it to the buffer "buff". strndupr() will 15164653Sdougm * reallocate space as necessay. 15174653Sdougm */ 15184653Sdougm proto = sa_get_optionset_attr(optionset, "type"); 15194653Sdougm if (proto != NULL) { 15204653Sdougm if (addspace++) 15214653Sdougm buff = strndupr(buff, " ", &buffsize); 15224653Sdougm buff = strndupr(buff, proto, &buffsize); 15234653Sdougm sa_free_attr_string(proto); 15244653Sdougm } 15253034Sdougm } 15263034Sdougm } 15273034Sdougm return (buff); 15283034Sdougm } 15293034Sdougm 15303034Sdougm /* 15313034Sdougm * sa_list(flags, argc, argv) 15323034Sdougm * 15333034Sdougm * implements the "list" subcommand to list groups and optionally 15343034Sdougm * their state and protocols. 15353034Sdougm */ 15363034Sdougm 15373034Sdougm static int 15383910Sdougm sa_list(sa_handle_t handle, int flags, int argc, char *argv[]) 15393034Sdougm { 15403034Sdougm sa_group_t group; 15413034Sdougm int verbose = 0; 15423034Sdougm int c; 15433034Sdougm char *protocol = NULL; 15445331Samw #ifdef lint 15455331Samw flags = flags; 15465331Samw #endif 15473034Sdougm 15483034Sdougm while ((c = getopt(argc, argv, "?hvP:")) != EOF) { 15494653Sdougm switch (c) { 15504653Sdougm case 'v': 15514653Sdougm verbose++; 15524653Sdougm break; 15534653Sdougm case 'P': 15545331Samw if (protocol != NULL) { 15555331Samw (void) printf(gettext( 15565331Samw "Specifying multiple protocols " 15575331Samw "not supported: %s\n"), 15585331Samw protocol); 15595331Samw return (SA_SYNTAX_ERR); 15605331Samw } 15614653Sdougm protocol = optarg; 15624653Sdougm if (!sa_valid_protocol(protocol)) { 15634653Sdougm (void) printf(gettext( 15644653Sdougm "Invalid protocol specified: %s\n"), 15654653Sdougm protocol); 15664653Sdougm return (SA_INVALID_PROTOCOL); 15674653Sdougm } 15684653Sdougm break; 15694653Sdougm default: 15704653Sdougm case 'h': 15714653Sdougm case '?': 15724653Sdougm (void) printf(gettext("usage: %s\n"), 15734653Sdougm sa_get_usage(USAGE_LIST)); 15744653Sdougm return (0); 15753034Sdougm } 15763034Sdougm } 15773034Sdougm 15784653Sdougm for (group = sa_get_group(handle, NULL); 15794653Sdougm group != NULL; 15803034Sdougm group = sa_get_next_group(group)) { 15814653Sdougm char *name; 15824653Sdougm char *proto; 15834653Sdougm if (protocol == NULL || has_protocol(group, protocol)) { 15844653Sdougm name = sa_get_group_attr(group, "name"); 15854653Sdougm if (name != NULL && (verbose > 1 || name[0] != '#')) { 15864653Sdougm (void) printf("%s", (char *)name); 15874653Sdougm if (verbose) { 15884653Sdougm /* 15894653Sdougm * Need the list of protocols 15904653Sdougm * and current status once 15914653Sdougm * available. We do want to 15924653Sdougm * translate the 15934653Sdougm * enabled/disabled text here. 15944653Sdougm */ 15954653Sdougm (void) printf("\t%s", isenabled(group) ? 15964653Sdougm gettext("enabled") : 15974653Sdougm gettext("disabled")); 15984653Sdougm proto = group_proto(group); 15994653Sdougm if (proto != NULL) { 16004653Sdougm (void) printf("\t%s", 16014653Sdougm (char *)proto); 16024653Sdougm free(proto); 16034653Sdougm } 16044653Sdougm } 16054653Sdougm (void) printf("\n"); 16063034Sdougm } 16074653Sdougm if (name != NULL) 16084653Sdougm sa_free_attr_string(name); 16093034Sdougm } 16103034Sdougm } 16113034Sdougm return (0); 16123034Sdougm } 16133034Sdougm 16143034Sdougm /* 16153034Sdougm * out_properties(optionset, proto, sec) 16163034Sdougm * 16173034Sdougm * Format the properties and encode the protocol and optional named 16183034Sdougm * optionset into the string. 16193034Sdougm * 16203034Sdougm * format is protocol[:name]=(property-list) 16213034Sdougm */ 16223034Sdougm 16233034Sdougm static void 16243034Sdougm out_properties(sa_optionset_t optionset, char *proto, char *sec) 16253034Sdougm { 16263034Sdougm char *type; 16273034Sdougm char *value; 16283034Sdougm int spacer; 16293034Sdougm sa_property_t prop; 16303034Sdougm 16314653Sdougm if (sec == NULL) 16324653Sdougm (void) printf(" %s=(", proto ? proto : gettext("all")); 16334653Sdougm else 16344653Sdougm (void) printf(" %s:%s=(", proto ? proto : gettext("all"), sec); 16353034Sdougm 16363034Sdougm for (spacer = 0, prop = sa_get_property(optionset, NULL); 16374653Sdougm prop != NULL; 16384653Sdougm prop = sa_get_next_property(prop)) { 16393034Sdougm 16403034Sdougm /* 16413034Sdougm * extract the property name/value and output with 16423034Sdougm * appropriate spacing. I.e. no prefixed space the 16433034Sdougm * first time through but a space on subsequent 16443034Sdougm * properties. 16453034Sdougm */ 16464653Sdougm type = sa_get_property_attr(prop, "type"); 16474653Sdougm value = sa_get_property_attr(prop, "value"); 16484653Sdougm if (type != NULL) { 16494653Sdougm (void) printf("%s%s=", spacer ? " " : "", type); 16504653Sdougm spacer = 1; 16514653Sdougm if (value != NULL) 16524653Sdougm (void) printf("\"%s\"", value); 16534653Sdougm else 16544653Sdougm (void) printf("\"\""); 16554653Sdougm } 16564653Sdougm if (type != NULL) 16574653Sdougm sa_free_attr_string(type); 16583034Sdougm if (value != NULL) 16594653Sdougm sa_free_attr_string(value); 16603034Sdougm } 16613034Sdougm (void) printf(")"); 16623034Sdougm } 16633034Sdougm 16643034Sdougm /* 16653034Sdougm * show_properties(group, protocol, prefix) 16663034Sdougm * 16673034Sdougm * print the properties for a group. If protocol is NULL, do all 16683034Sdougm * protocols otherwise only the specified protocol. All security 16693034Sdougm * (named groups specific to the protocol) are included. 16703034Sdougm * 16713034Sdougm * The "prefix" is always applied. The caller knows whether it wants 16723034Sdougm * some type of prefix string (white space) or not. Once the prefix 16733034Sdougm * has been output, it is reduced to the zero length string for the 16743034Sdougm * remainder of the property output. 16753034Sdougm */ 16763034Sdougm 16773034Sdougm static void 16783034Sdougm show_properties(sa_group_t group, char *protocol, char *prefix) 16793034Sdougm { 16803034Sdougm sa_optionset_t optionset; 16813034Sdougm sa_security_t security; 16823034Sdougm char *value; 16833034Sdougm char *secvalue; 16843034Sdougm 16853034Sdougm if (protocol != NULL) { 16864653Sdougm optionset = sa_get_optionset(group, protocol); 16874653Sdougm if (optionset != NULL) { 16884653Sdougm (void) printf("%s", prefix); 16894653Sdougm prefix = ""; 16904653Sdougm out_properties(optionset, protocol, NULL); 16914653Sdougm } 16924653Sdougm security = sa_get_security(group, protocol, NULL); 16934653Sdougm if (security != NULL) { 16944653Sdougm (void) printf("%s", prefix); 16954653Sdougm prefix = ""; 16964653Sdougm out_properties(security, protocol, NULL); 16974653Sdougm } 16983034Sdougm } else { 16994653Sdougm for (optionset = sa_get_optionset(group, protocol); 17004653Sdougm optionset != NULL; 17014653Sdougm optionset = sa_get_next_optionset(optionset)) { 17024653Sdougm 17034653Sdougm value = sa_get_optionset_attr(optionset, "type"); 17044653Sdougm (void) printf("%s", prefix); 17054653Sdougm prefix = ""; 17064653Sdougm out_properties(optionset, value, 0); 17074653Sdougm if (value != NULL) 17084653Sdougm sa_free_attr_string(value); 17094653Sdougm } 17104653Sdougm for (security = sa_get_security(group, NULL, protocol); 17114653Sdougm security != NULL; 17124653Sdougm security = sa_get_next_security(security)) { 17134653Sdougm 17144653Sdougm value = sa_get_security_attr(security, "type"); 17154653Sdougm secvalue = sa_get_security_attr(security, "sectype"); 17164653Sdougm (void) printf("%s", prefix); 17174653Sdougm prefix = ""; 17184653Sdougm out_properties(security, value, secvalue); 17194653Sdougm if (value != NULL) 17204653Sdougm sa_free_attr_string(value); 17214653Sdougm if (secvalue != NULL) 17224653Sdougm sa_free_attr_string(secvalue); 17234653Sdougm } 17243034Sdougm } 17253034Sdougm } 17263034Sdougm 17273034Sdougm /* 17285331Samw * get_resource(share) 17295331Samw * 17305331Samw * Get the first resource name, if any, and fix string to be in 17315331Samw * current locale and have quotes if it has embedded spaces. Return 17325331Samw * an attr string that must be freed. 17335331Samw */ 17345331Samw 17355331Samw static char * 17365331Samw get_resource(sa_share_t share) 17375331Samw { 17385331Samw sa_resource_t resource; 17395331Samw char *resstring = NULL; 17405331Samw char *retstring; 17415331Samw 17425331Samw if ((resource = sa_get_share_resource(share, NULL)) != NULL) { 17435331Samw resstring = sa_get_resource_attr(resource, "name"); 17445331Samw if (resstring != NULL) { 17455331Samw char *cp; 17465331Samw int len; 17475331Samw 17485331Samw retstring = conv_from_utf8(resstring); 17495331Samw if (retstring != resstring) { 17505331Samw sa_free_attr_string(resstring); 17515331Samw resstring = retstring; 17525331Samw } 17535331Samw if (strpbrk(resstring, " ") != NULL) { 17545331Samw /* account for quotes */ 17555331Samw len = strlen(resstring) + 3; 17565331Samw cp = calloc(len, sizeof (char)); 17575331Samw if (cp != NULL) { 17585331Samw (void) snprintf(cp, len, 17595331Samw "\"%s\"", resstring); 17605331Samw sa_free_attr_string(resstring); 17615331Samw resstring = cp; 17625331Samw } else { 17635331Samw sa_free_attr_string(resstring); 17645331Samw resstring = NULL; 17655331Samw } 17665331Samw } 17675331Samw } 17685331Samw } 17695331Samw return (resstring); 17705331Samw } 17715331Samw 17725331Samw /* 17735331Samw * has_resource_with_opt(share) 17745331Samw * 17755331Samw * Check to see if the share has any resource names with optionsets 17765331Samw * set. Also indicate if multiple resource names since the syntax 17775331Samw * would be about the same. 17785331Samw */ 17795331Samw static int 17805331Samw has_resource_with_opt(sa_share_t share) 17815331Samw { 17825331Samw sa_resource_t resource; 17835331Samw int ret = B_FALSE; 17845331Samw 17855331Samw for (resource = sa_get_share_resource(share, NULL); 17865331Samw resource != NULL; 17875331Samw resource = sa_get_next_resource(resource)) { 17885331Samw 17895331Samw if (sa_get_optionset(resource, NULL) != NULL) { 17905331Samw ret = B_TRUE; 17915331Samw break; 17925331Samw } 17935331Samw } 17945331Samw return (ret); 17955331Samw } 17965331Samw 17975331Samw /* 17985331Samw * has_multiple_resource(share) 17995331Samw * 18005331Samw * Check to see if the share has any resource names with optionsets 18015331Samw * set. Also indicate if multiple resource names since the syntax 18025331Samw * would be about the same. 18035331Samw */ 18045331Samw static int 18055331Samw has_multiple_resource(sa_share_t share) 18065331Samw { 18075331Samw sa_resource_t resource; 18085331Samw int num; 18095331Samw 18105331Samw for (num = 0, resource = sa_get_share_resource(share, NULL); 18115331Samw resource != NULL; 18125331Samw resource = sa_get_next_resource(resource)) { 18135331Samw num++; 18145331Samw if (num > 1) 18155331Samw return (B_TRUE); 18165331Samw } 18175331Samw return (B_FALSE); 18185331Samw } 18195331Samw 18205331Samw /* 18215331Samw * show_share(share, verbose, properties, proto, iszfs, sharepath) 18225331Samw * 18235331Samw * print out the share information. With the addition of resource as a 18245331Samw * full object that can have multiple instances below the share, we 18255331Samw * need to display that as well. 18265331Samw */ 18275331Samw 18285331Samw static void 18295331Samw show_share(sa_share_t share, int verbose, int properties, char *proto, 18305331Samw int iszfs, char *sharepath) 18315331Samw { 18325331Samw char *drive; 18335331Samw char *exclude; 18345331Samw sa_resource_t resource = NULL; 18355331Samw char *description; 18365331Samw char *desc; 18375331Samw char *rsrcname; 18385331Samw int rsrcwithopt; 18395331Samw int multiple; 18405331Samw char *type; 18415331Samw 18425331Samw rsrcwithopt = has_resource_with_opt(share); 18435331Samw 18445331Samw if (verbose || (properties && rsrcwithopt)) { 18455331Samw /* First, indicate if transient */ 18465331Samw type = sa_get_share_attr(share, "type"); 18475331Samw if (type != NULL && !iszfs && verbose && 18485331Samw strcmp(type, "transient") == 0) 18495331Samw (void) printf("\t* "); 18505331Samw else 18515331Samw (void) printf("\t "); 18525331Samw 18535331Samw if (type != NULL) 18545331Samw sa_free_attr_string(type); 18555331Samw 18565331Samw /* 18575331Samw * If we came in with verbose, we want to handle the case of 18585331Samw * multiple resources as though they had properties set. 18595331Samw */ 18605331Samw multiple = has_multiple_resource(share); 18615331Samw 18625331Samw /* Next, if not multiple follow old model */ 18635331Samw if (!multiple && !rsrcwithopt) { 18645331Samw rsrcname = get_resource(share); 18655331Samw if (rsrcname != NULL && strlen(rsrcname) > 0) { 18665331Samw (void) printf("%s=%s", rsrcname, sharepath); 18675331Samw } else { 18685331Samw (void) printf("%s", sharepath); 18695331Samw } 18705331Samw if (rsrcname != NULL) 18715331Samw sa_free_attr_string(rsrcname); 18725331Samw } else { 18735331Samw /* Treat as simple and then resources come later */ 18745331Samw (void) printf("%s", sharepath); 18755331Samw } 18765331Samw drive = sa_get_share_attr(share, "drive-letter"); 18775331Samw if (drive != NULL) { 18785331Samw if (strlen(drive) > 0) 18795331Samw (void) printf(gettext("\tdrive-letter=\"%s:\""), 18805331Samw drive); 18815331Samw sa_free_attr_string(drive); 18825331Samw } 18835331Samw if (properties) 18845331Samw show_properties(share, proto, "\t"); 18855331Samw exclude = sa_get_share_attr(share, "exclude"); 18865331Samw if (exclude != NULL) { 18875331Samw (void) printf(gettext("\tnot-shared-with=[%s]"), 18885331Samw exclude); 18895331Samw sa_free_attr_string(exclude); 18905331Samw } 18915331Samw description = sa_get_share_description(share); 18925331Samw if (description != NULL) { 18935331Samw if (strlen(description) > 0) { 18945331Samw desc = conv_from_utf8(description); 18955331Samw if (desc != description) { 18965331Samw sa_free_share_description(description); 18975331Samw description = desc; 18985331Samw } 18995331Samw (void) printf("\t\"%s\"", description); 19005331Samw } 19015331Samw sa_free_share_description(description); 19025331Samw } 19035331Samw 19045331Samw /* 19055331Samw * If there are resource names with options, show them 19065331Samw * here, with one line per resource. Resource specific 19075331Samw * options are at the end of the line followed by 19085331Samw * description, if any. 19095331Samw */ 19105331Samw if (rsrcwithopt || multiple) { 19115331Samw for (resource = sa_get_share_resource(share, NULL); 19125331Samw resource != NULL; 19135331Samw resource = sa_get_next_resource(resource)) { 19145331Samw int has_space; 19155331Samw char *rsrc; 19165331Samw 19175331Samw (void) printf("\n\t\t "); 19185331Samw rsrcname = sa_get_resource_attr(resource, 19195331Samw "name"); 19205331Samw if (rsrcname == NULL) 19215331Samw continue; 19225331Samw 19235331Samw rsrc = conv_from_utf8(rsrcname); 19245331Samw has_space = strpbrk(rsrc, " ") != NULL; 19255331Samw 19265331Samw if (has_space) 19275331Samw (void) printf("\"%s\"=%s", rsrc, 19285331Samw sharepath); 19295331Samw else 19305331Samw (void) printf("%s=%s", rsrc, 19315331Samw sharepath); 19325331Samw if (rsrc != rsrcname) 19335331Samw sa_free_attr_string(rsrc); 19345331Samw sa_free_attr_string(rsrcname); 19355331Samw if (properties || rsrcwithopt) 19365331Samw show_properties(resource, proto, "\t"); 19375331Samw 19385331Samw /* Get description string if any */ 19395331Samw print_rsrc_desc(resource); 19405331Samw } 19415331Samw } 19425331Samw } else { 19435331Samw (void) printf("\t %s", sharepath); 19445331Samw if (properties) 19455331Samw show_properties(share, proto, "\t"); 19465331Samw } 19475331Samw (void) printf("\n"); 19485331Samw } 19495331Samw 19505331Samw /* 19513034Sdougm * show_group(group, verbose, properties, proto, subgroup) 19523034Sdougm * 19533034Sdougm * helper function to show the contents of a group. 19543034Sdougm */ 19553034Sdougm 19563034Sdougm static void 19573034Sdougm show_group(sa_group_t group, int verbose, int properties, char *proto, 19585331Samw char *subgroup) 19593034Sdougm { 19603034Sdougm sa_share_t share; 19613034Sdougm char *groupname; 19623034Sdougm char *zfs = NULL; 19633034Sdougm int iszfs = 0; 19645331Samw char *sharepath; 19653034Sdougm 19663034Sdougm groupname = sa_get_group_attr(group, "name"); 19673034Sdougm if (groupname != NULL) { 19684653Sdougm if (proto != NULL && !has_protocol(group, proto)) { 19694653Sdougm sa_free_attr_string(groupname); 19704653Sdougm return; 19714653Sdougm } 19723034Sdougm /* 19733034Sdougm * check to see if the group is managed by ZFS. If 19743034Sdougm * there is an attribute, then it is. A non-NULL zfs 19753034Sdougm * variable will trigger the different way to display 19763034Sdougm * and will remove the transient property indicator 19773034Sdougm * from the output. 19783034Sdougm */ 19794653Sdougm zfs = sa_get_group_attr(group, "zfs"); 19804653Sdougm if (zfs != NULL) { 19814653Sdougm iszfs = 1; 19824653Sdougm sa_free_attr_string(zfs); 19833034Sdougm } 19844653Sdougm share = sa_get_share(group, NULL); 19854653Sdougm if (subgroup == NULL) 19864653Sdougm (void) printf("%s", groupname); 19874653Sdougm else 19884653Sdougm (void) printf(" %s/%s", subgroup, groupname); 19894653Sdougm if (properties) 19904653Sdougm show_properties(group, proto, ""); 19914653Sdougm (void) printf("\n"); 19924653Sdougm if (strcmp(groupname, "zfs") == 0) { 19934653Sdougm sa_group_t zgroup; 19944653Sdougm 19954653Sdougm for (zgroup = sa_get_sub_group(group); 19964653Sdougm zgroup != NULL; 19974653Sdougm zgroup = sa_get_next_group(zgroup)) { 19984653Sdougm show_group(zgroup, verbose, properties, proto, 19994653Sdougm "zfs"); 20004653Sdougm } 20014653Sdougm sa_free_attr_string(groupname); 20024653Sdougm return; 20034653Sdougm } 20043034Sdougm /* 20054653Sdougm * Have a group, so list the contents. Resource and 20063034Sdougm * description are only listed if verbose is set. 20073034Sdougm */ 20084653Sdougm for (share = sa_get_share(group, NULL); 20094653Sdougm share != NULL; 20104653Sdougm share = sa_get_next_share(share)) { 20114653Sdougm sharepath = sa_get_share_attr(share, "path"); 20124653Sdougm if (sharepath != NULL) { 20135331Samw show_share(share, verbose, properties, proto, 20145331Samw iszfs, sharepath); 20154653Sdougm sa_free_attr_string(sharepath); 20163034Sdougm } 20173034Sdougm } 20183034Sdougm } 20193034Sdougm if (groupname != NULL) { 20203034Sdougm sa_free_attr_string(groupname); 20213034Sdougm } 20223034Sdougm } 20233034Sdougm 20243034Sdougm /* 20253034Sdougm * show_group_xml_init() 20263034Sdougm * 20273034Sdougm * Create an XML document that will be used to display config info via 20283034Sdougm * XML format. 20293034Sdougm */ 20303034Sdougm 20313034Sdougm xmlDocPtr 20323034Sdougm show_group_xml_init() 20333034Sdougm { 20343034Sdougm xmlDocPtr doc; 20353034Sdougm xmlNodePtr root; 20363034Sdougm 20373034Sdougm doc = xmlNewDoc((xmlChar *)"1.0"); 20383034Sdougm if (doc != NULL) { 20394653Sdougm root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 20404653Sdougm if (root != NULL) 20414653Sdougm xmlDocSetRootElement(doc, root); 20423034Sdougm } 20433034Sdougm return (doc); 20443034Sdougm } 20453034Sdougm 20463034Sdougm /* 20473034Sdougm * show_group_xml(doc, group) 20483034Sdougm * 20493034Sdougm * Copy the group info into the XML doc. 20503034Sdougm */ 20513034Sdougm 20523034Sdougm static void 20533034Sdougm show_group_xml(xmlDocPtr doc, sa_group_t group) 20543034Sdougm { 20553034Sdougm xmlNodePtr node; 20563034Sdougm xmlNodePtr root; 20573034Sdougm 20583034Sdougm root = xmlDocGetRootElement(doc); 20593034Sdougm node = xmlCopyNode((xmlNodePtr)group, 1); 20603034Sdougm if (node != NULL && root != NULL) { 20614653Sdougm xmlAddChild(root, node); 20623034Sdougm /* 20633034Sdougm * In the future, we may have interally used tags that 20643034Sdougm * should not appear in the XML output. Remove 20653034Sdougm * anything we don't want to show here. 20663034Sdougm */ 20673034Sdougm } 20683034Sdougm } 20693034Sdougm 20703034Sdougm /* 20713034Sdougm * sa_show(flags, argc, argv) 20723034Sdougm * 20733034Sdougm * Implements the show subcommand. 20743034Sdougm */ 20753034Sdougm 20763034Sdougm int 20773910Sdougm sa_show(sa_handle_t handle, int flags, int argc, char *argv[]) 20783034Sdougm { 20793034Sdougm sa_group_t group; 20803034Sdougm int verbose = 0; 20813034Sdougm int properties = 0; 20823034Sdougm int c; 20833034Sdougm int ret = SA_OK; 20843034Sdougm char *protocol = NULL; 20853034Sdougm int xml = 0; 20863034Sdougm xmlDocPtr doc; 20875331Samw #ifdef lint 20885331Samw flags = flags; 20895331Samw #endif 20903034Sdougm 20913034Sdougm while ((c = getopt(argc, argv, "?hvP:px")) != EOF) { 20924653Sdougm switch (c) { 20934653Sdougm case 'v': 20944653Sdougm verbose++; 20954653Sdougm break; 20964653Sdougm case 'p': 20974653Sdougm properties++; 20984653Sdougm break; 20994653Sdougm case 'P': 21005331Samw if (protocol != NULL) { 21015331Samw (void) printf(gettext( 21025331Samw "Specifying multiple protocols " 21035331Samw "not supported: %s\n"), 21045331Samw protocol); 21055331Samw return (SA_SYNTAX_ERR); 21065331Samw } 21074653Sdougm protocol = optarg; 21084653Sdougm if (!sa_valid_protocol(protocol)) { 21094653Sdougm (void) printf(gettext( 21104653Sdougm "Invalid protocol specified: %s\n"), 21114653Sdougm protocol); 21124653Sdougm return (SA_INVALID_PROTOCOL); 21134653Sdougm } 21144653Sdougm break; 21154653Sdougm case 'x': 21164653Sdougm xml++; 21174653Sdougm break; 21184653Sdougm default: 21194653Sdougm case 'h': 21204653Sdougm case '?': 21214653Sdougm (void) printf(gettext("usage: %s\n"), 21224653Sdougm sa_get_usage(USAGE_SHOW)); 21234653Sdougm return (0); 21243034Sdougm } 21253034Sdougm } 21263034Sdougm 21273034Sdougm if (xml) { 21284653Sdougm doc = show_group_xml_init(); 21294653Sdougm if (doc == NULL) 21304653Sdougm ret = SA_NO_MEMORY; 21313034Sdougm } 21323034Sdougm 21333034Sdougm if (optind == argc) { 21344653Sdougm /* No group specified so go through them all */ 21354653Sdougm for (group = sa_get_group(handle, NULL); 21364653Sdougm group != NULL; 21374653Sdougm group = sa_get_next_group(group)) { 21384653Sdougm /* 21394653Sdougm * Have a group so check if one we want and then list 21404653Sdougm * contents with appropriate options. 21414653Sdougm */ 21424653Sdougm if (xml) 21434653Sdougm show_group_xml(doc, group); 21444653Sdougm else 21454653Sdougm show_group(group, verbose, properties, protocol, 21464653Sdougm NULL); 21474653Sdougm } 21483034Sdougm } else { 21494653Sdougm /* Have a specified list of groups */ 21504653Sdougm for (; optind < argc; optind++) { 21514653Sdougm group = sa_get_group(handle, argv[optind]); 21524653Sdougm if (group != NULL) { 21534653Sdougm if (xml) 21544653Sdougm show_group_xml(doc, group); 21554653Sdougm else 21564653Sdougm show_group(group, verbose, properties, 21574653Sdougm protocol, NULL); 21584653Sdougm } else { 21594653Sdougm (void) printf(gettext("%s: not found\n"), 21604653Sdougm argv[optind]); 21614653Sdougm ret = SA_NO_SUCH_GROUP; 21624653Sdougm } 21633034Sdougm } 21643034Sdougm } 21653034Sdougm if (xml && ret == SA_OK) { 21664653Sdougm xmlDocFormatDump(stdout, doc, 1); 21674653Sdougm xmlFreeDoc(doc); 21683034Sdougm } 21693034Sdougm return (ret); 21703034Sdougm 21713034Sdougm } 21723034Sdougm 21733034Sdougm /* 21743034Sdougm * enable_share(group, share, update_legacy) 21753034Sdougm * 21763034Sdougm * helper function to enable a share if the group is enabled. 21773034Sdougm */ 21783034Sdougm 21793034Sdougm static int 21803910Sdougm enable_share(sa_handle_t handle, sa_group_t group, sa_share_t share, 21815331Samw int update_legacy) 21823034Sdougm { 21833034Sdougm char *value; 21843034Sdougm int enabled; 21853034Sdougm sa_optionset_t optionset; 21865331Samw int err; 21873034Sdougm int ret = SA_OK; 21883034Sdougm char *zfs = NULL; 21893034Sdougm int iszfs = 0; 21905331Samw int isshare; 21913034Sdougm 21923034Sdougm /* 21933034Sdougm * need to enable this share if the group is enabled but not 21943034Sdougm * otherwise. The enable is also done on each protocol 21953034Sdougm * represented in the group. 21963034Sdougm */ 21973034Sdougm value = sa_get_group_attr(group, "state"); 21983034Sdougm enabled = value != NULL && strcmp(value, "enabled") == 0; 21993034Sdougm if (value != NULL) 22004653Sdougm sa_free_attr_string(value); 22013034Sdougm /* remove legacy config if necessary */ 22023034Sdougm if (update_legacy) 22035331Samw ret = sa_delete_legacy(share, NULL); 22043034Sdougm zfs = sa_get_group_attr(group, "zfs"); 22053034Sdougm if (zfs != NULL) { 22064653Sdougm iszfs++; 22074653Sdougm sa_free_attr_string(zfs); 22083034Sdougm } 22093034Sdougm 22103034Sdougm /* 22113034Sdougm * Step through each optionset at the group level and 22123034Sdougm * enable the share based on the protocol type. This 22133034Sdougm * works because protocols must be set on the group 22143034Sdougm * for the protocol to be enabled. 22153034Sdougm */ 22165331Samw isshare = sa_is_share(share); 22173034Sdougm for (optionset = sa_get_optionset(group, NULL); 22183034Sdougm optionset != NULL && ret == SA_OK; 22193034Sdougm optionset = sa_get_next_optionset(optionset)) { 22204653Sdougm value = sa_get_optionset_attr(optionset, "type"); 22214653Sdougm if (value != NULL) { 22225331Samw if (enabled) { 22235331Samw if (isshare) { 22245331Samw err = sa_enable_share(share, value); 22255331Samw } else { 22265331Samw err = sa_enable_resource(share, value); 22275331Samw if (err == SA_NOT_SUPPORTED) { 22285331Samw sa_share_t parent; 22295331Samw parent = sa_get_resource_parent( 22305331Samw share); 22315331Samw if (parent != NULL) 22325331Samw err = sa_enable_share( 22335331Samw parent, value); 22345331Samw } 22355331Samw } 22365331Samw if (err != SA_OK) { 22375331Samw ret = err; 22385331Samw (void) printf(gettext( 22395331Samw "Failed to enable share for " 22405331Samw "\"%s\": %s\n"), 22415331Samw value, sa_errorstr(ret)); 22425331Samw } 22435331Samw } 22445331Samw /* 22455331Samw * If we want to update the legacy, use a copy of 22465331Samw * share so we can avoid breaking the loop we are in 22475331Samw * since we might also need to go up the tree to the 22485331Samw * parent. 22495331Samw */ 22505331Samw if (update_legacy && !iszfs) { 22515331Samw sa_share_t update = share; 22525331Samw if (!sa_is_share(share)) { 22535331Samw update = sa_get_resource_parent(share); 22545331Samw } 22555331Samw (void) sa_update_legacy(update, value); 22565331Samw } 22574653Sdougm sa_free_attr_string(value); 22584653Sdougm } 22593034Sdougm } 22603034Sdougm if (ret == SA_OK) 22614653Sdougm (void) sa_update_config(handle); 22623034Sdougm return (ret); 22633034Sdougm } 22643034Sdougm 22653034Sdougm /* 22665331Samw * sa_require_resource(group) 22675331Samw * 22685331Samw * if any of the defined protocols on the group require resource 22695331Samw * names, then all shares must have them. 22705331Samw */ 22715331Samw 22725331Samw static int 22735331Samw sa_require_resource(sa_group_t group) 22745331Samw { 22755331Samw sa_optionset_t optionset; 22765331Samw 22775331Samw for (optionset = sa_get_optionset(group, NULL); 22785331Samw optionset != NULL; 22795331Samw optionset = sa_get_next_optionset(optionset)) { 22805331Samw char *proto; 22815331Samw 22825331Samw proto = sa_get_optionset_attr(optionset, "type"); 22835331Samw if (proto != NULL) { 22845331Samw uint64_t features; 22855331Samw 22865331Samw features = sa_proto_get_featureset(proto); 22875331Samw if (features & SA_FEATURE_RESOURCE) { 22885331Samw sa_free_attr_string(proto); 22895331Samw return (B_TRUE); 22905331Samw } 22915331Samw sa_free_attr_string(proto); 22925331Samw } 22935331Samw } 22945331Samw return (B_FALSE); 22955331Samw } 22965331Samw 22975331Samw /* 22983034Sdougm * sa_addshare(flags, argc, argv) 22993034Sdougm * 23003034Sdougm * implements add-share subcommand. 23013034Sdougm */ 23023034Sdougm 23035331Samw static int 23043910Sdougm sa_addshare(sa_handle_t handle, int flags, int argc, char *argv[]) 23053034Sdougm { 23063034Sdougm int verbose = 0; 23073034Sdougm int dryrun = 0; 23083034Sdougm int c; 23093034Sdougm int ret = SA_OK; 23103034Sdougm sa_group_t group; 23113034Sdougm sa_share_t share; 23125331Samw sa_resource_t resource = NULL; 23133034Sdougm char *sharepath = NULL; 23143034Sdougm char *description = NULL; 23155331Samw char *rsrcname = NULL; 23165331Samw char *rsrc = NULL; 23173034Sdougm int persist = SA_SHARE_PERMANENT; /* default to persist */ 23183034Sdougm int auth; 23193034Sdougm char dir[MAXPATHLEN]; 23203034Sdougm 23213034Sdougm while ((c = getopt(argc, argv, "?hvns:d:r:t")) != EOF) { 23224653Sdougm switch (c) { 23234653Sdougm case 'n': 23244653Sdougm dryrun++; 23254653Sdougm break; 23264653Sdougm case 'v': 23274653Sdougm verbose++; 23284653Sdougm break; 23294653Sdougm case 'd': 23304653Sdougm description = optarg; 23314653Sdougm break; 23324653Sdougm case 'r': 23335331Samw if (rsrcname != NULL) { 23345331Samw (void) printf(gettext("Adding multiple " 23355331Samw "resource names not" 23365331Samw " supported\n")); 23375331Samw return (SA_SYNTAX_ERR); 23385331Samw } 23395331Samw rsrcname = optarg; 23404653Sdougm break; 23414653Sdougm case 's': 23424653Sdougm /* 23434653Sdougm * Save share path into group. Currently limit 23444653Sdougm * to one share per command. 23454653Sdougm */ 23464653Sdougm if (sharepath != NULL) { 23474653Sdougm (void) printf(gettext( 23484653Sdougm "Adding multiple shares not supported\n")); 23495331Samw return (SA_SYNTAX_ERR); 23504653Sdougm } 23514653Sdougm sharepath = optarg; 23524653Sdougm break; 23534653Sdougm case 't': 23544653Sdougm persist = SA_SHARE_TRANSIENT; 23554653Sdougm break; 23564653Sdougm default: 23574653Sdougm case 'h': 23584653Sdougm case '?': 23594653Sdougm (void) printf(gettext("usage: %s\n"), 23604653Sdougm sa_get_usage(USAGE_ADD_SHARE)); 23614653Sdougm return (0); 23623034Sdougm } 23633034Sdougm } 23643034Sdougm 23653034Sdougm if (optind >= argc) { 23664653Sdougm (void) printf(gettext("usage: %s\n"), 23674653Sdougm sa_get_usage(USAGE_ADD_SHARE)); 23684653Sdougm if (dryrun || sharepath != NULL || description != NULL || 23695331Samw rsrcname != NULL || verbose || persist) { 23704653Sdougm (void) printf(gettext("\tgroup must be specified\n")); 23714653Sdougm ret = SA_NO_SUCH_GROUP; 23724653Sdougm } else { 23734653Sdougm ret = SA_OK; 23744653Sdougm } 23753034Sdougm } else { 23764653Sdougm if (sharepath == NULL) { 23774653Sdougm (void) printf(gettext("usage: %s\n"), 23784653Sdougm sa_get_usage(USAGE_ADD_SHARE)); 23794653Sdougm (void) printf(gettext( 23804653Sdougm "\t-s sharepath must be specified\n")); 23815331Samw ret = SA_BAD_PATH; 23824653Sdougm } 23835331Samw if (ret == SA_OK) { 23845331Samw if (realpath(sharepath, dir) == NULL) { 23855331Samw ret = SA_BAD_PATH; 23865331Samw (void) printf(gettext("Path " 23875331Samw "is not valid: %s\n"), 23885331Samw sharepath); 23895331Samw } else { 23905331Samw sharepath = dir; 23915331Samw } 23923034Sdougm } 23935331Samw if (ret == SA_OK && rsrcname != NULL) { 23945331Samw /* check for valid syntax */ 23955331Samw if (validresource(rsrcname)) { 23965331Samw rsrc = conv_to_utf8(rsrcname); 23975331Samw resource = sa_find_resource(handle, rsrc); 23985331Samw if (resource != NULL) { 23995331Samw /* 24005331Samw * Resource names must be 24015331Samw * unique in the system 24025331Samw */ 24035331Samw ret = SA_DUPLICATE_NAME; 24045331Samw (void) printf(gettext("usage: %s\n"), 24055331Samw sa_get_usage(USAGE_ADD_SHARE)); 24065331Samw (void) printf(gettext( 24075331Samw "\tresource names must be unique " 24085331Samw "in the system\n")); 24095331Samw } 24105331Samw } else { 24115331Samw (void) printf(gettext("usage: %s\n"), 24125331Samw sa_get_usage(USAGE_ADD_SHARE)); 24135331Samw (void) printf(gettext( 24145331Samw "\tresource names use restricted " 24155331Samw "character set\n")); 24165331Samw ret = SA_INVALID_NAME; 24175331Samw } 24183034Sdougm } 24195331Samw 24205331Samw if (ret != SA_OK) { 24215331Samw if (rsrc != NULL && rsrcname != rsrc) 24225331Samw sa_free_attr_string(rsrc); 24235331Samw return (ret); 24244653Sdougm } 24255331Samw 24264653Sdougm share = sa_find_share(handle, sharepath); 24274653Sdougm if (share != NULL) { 24285331Samw if (rsrcname == NULL) { 24295331Samw /* 24305331Samw * Can only have a duplicate share if a new 24315331Samw * resource name is being added. 24325331Samw */ 24335331Samw ret = SA_DUPLICATE_NAME; 24345331Samw (void) printf(gettext("Share path already " 24355331Samw "shared: %s\n"), sharepath); 24365331Samw } 24375331Samw } 24385331Samw if (ret != SA_OK) 24395331Samw return (ret); 24405331Samw 24415331Samw group = sa_get_group(handle, argv[optind]); 24425331Samw if (group != NULL) { 24435331Samw if (sa_require_resource(group) == B_TRUE && 24445331Samw rsrcname == NULL) { 24455331Samw (void) printf(gettext( 24465331Samw "Resource name is required " 24475331Samw "by at least one enabled protocol " 24485331Samw "in group\n")); 24495331Samw return (SA_RESOURCE_REQUIRED); 24505331Samw } 24515331Samw if (share == NULL && ret == SA_OK) { 24525331Samw if (dryrun) 24535331Samw ret = sa_check_path(group, sharepath, 24545331Samw SA_CHECK_NORMAL); 24555331Samw else 24565331Samw share = sa_add_share(group, sharepath, 24575331Samw persist, &ret); 24585331Samw } 24595331Samw /* 24605331Samw * Make sure this isn't an attempt to put a resourced 24615331Samw * share into a different group than it already is in. 24625331Samw */ 24635331Samw if (share != NULL) { 24645331Samw sa_group_t parent; 24655331Samw parent = sa_get_parent_group(share); 24665331Samw if (parent != group) { 24675331Samw ret = SA_DUPLICATE_NAME; 24684653Sdougm (void) printf(gettext( 24694653Sdougm "Share path already " 24705331Samw "shared: %s\n"), sharepath); 24714653Sdougm } 24723034Sdougm } 24733034Sdougm if (!dryrun && share == NULL) { 24744653Sdougm (void) printf(gettext( 24754653Sdougm "Could not add share: %s\n"), 24764653Sdougm sa_errorstr(ret)); 24773034Sdougm } else { 24785331Samw auth = check_authorizations(argv[optind], 24795331Samw flags); 24804653Sdougm if (!dryrun && ret == SA_OK) { 24815331Samw if (rsrcname != NULL) { 24825331Samw resource = sa_add_resource( 24835331Samw share, 24845331Samw rsrc, 24855331Samw SA_SHARE_PERMANENT, 24865331Samw &ret); 24874653Sdougm } 24884653Sdougm if (ret == SA_OK && 24894653Sdougm description != NULL) { 24905331Samw if (description != NULL) { 24915331Samw ret = 24925331Samw set_share_desc( 24935331Samw share, 24945331Samw description); 24955331Samw } 24964653Sdougm } 24974653Sdougm if (ret == SA_OK) { 24985331Samw /* now enable the share(s) */ 24995331Samw if (resource != NULL) { 25005331Samw ret = enable_share( 25015331Samw handle, 25025331Samw group, 25035331Samw resource, 25045331Samw 1); 25055331Samw } else { 25065331Samw ret = enable_share( 25075331Samw handle, 25085331Samw group, 25095331Samw share, 25105331Samw 1); 25115331Samw } 25124653Sdougm ret = sa_update_config(handle); 25134653Sdougm } 25144653Sdougm switch (ret) { 25154653Sdougm case SA_DUPLICATE_NAME: 25164653Sdougm (void) printf(gettext( 25174653Sdougm "Resource name in" 25185331Samw "use: %s\n"), 25195331Samw rsrcname); 25204653Sdougm break; 25214653Sdougm default: 25225331Samw (void) printf(gettext( 25235331Samw "Could not set " 25244653Sdougm "attribute: %s\n"), 25254653Sdougm sa_errorstr(ret)); 25264653Sdougm break; 25274653Sdougm case SA_OK: 25284653Sdougm break; 25294653Sdougm } 25305331Samw } else if (dryrun && ret == SA_OK && 25315331Samw !auth && verbose) { 25324653Sdougm (void) printf(gettext( 25334653Sdougm "Command would fail: %s\n"), 25344653Sdougm sa_errorstr(SA_NO_PERMISSION)); 25354653Sdougm ret = SA_NO_PERMISSION; 25363034Sdougm } 25373034Sdougm } 25385331Samw } else { 25395331Samw switch (ret) { 25405331Samw default: 25415331Samw (void) printf(gettext( 25425331Samw "Group \"%s\" not found\n"), argv[optind]); 25435331Samw ret = SA_NO_SUCH_GROUP; 25445331Samw break; 25455331Samw case SA_BAD_PATH: 25465331Samw case SA_DUPLICATE_NAME: 25475331Samw break; 25485331Samw } 25493034Sdougm } 25503034Sdougm } 25513034Sdougm return (ret); 25523034Sdougm } 25533034Sdougm 25543034Sdougm /* 25553034Sdougm * sa_moveshare(flags, argc, argv) 25563034Sdougm * 25573034Sdougm * implements move-share subcommand. 25583034Sdougm */ 25593034Sdougm 25603034Sdougm int 25613910Sdougm sa_moveshare(sa_handle_t handle, int flags, int argc, char *argv[]) 25623034Sdougm { 25633034Sdougm int verbose = 0; 25643034Sdougm int dryrun = 0; 25653034Sdougm int c; 25663034Sdougm int ret = SA_OK; 25673034Sdougm sa_group_t group; 25683034Sdougm sa_share_t share; 25695331Samw char *rsrcname = NULL; 25703034Sdougm char *sharepath = NULL; 25713034Sdougm int authsrc = 0, authdst = 0; 25723034Sdougm 25735331Samw while ((c = getopt(argc, argv, "?hvnr:s:")) != EOF) { 25744653Sdougm switch (c) { 25754653Sdougm case 'n': 25764653Sdougm dryrun++; 25774653Sdougm break; 25784653Sdougm case 'v': 25794653Sdougm verbose++; 25804653Sdougm break; 25815331Samw case 'r': 25825331Samw if (rsrcname != NULL) { 25835331Samw (void) printf(gettext( 25845331Samw "Moving multiple resource names not" 25855331Samw " supported\n")); 25865331Samw return (SA_SYNTAX_ERR); 25875331Samw } 25885331Samw rsrcname = optarg; 25895331Samw break; 25904653Sdougm case 's': 25914653Sdougm /* 25924653Sdougm * Remove share path from group. Currently limit 25934653Sdougm * to one share per command. 25944653Sdougm */ 25954653Sdougm if (sharepath != NULL) { 25964653Sdougm (void) printf(gettext("Moving multiple shares" 25975331Samw " not supported\n")); 25985331Samw return (SA_SYNTAX_ERR); 25994653Sdougm } 26004653Sdougm sharepath = optarg; 26014653Sdougm break; 26024653Sdougm default: 26034653Sdougm case 'h': 26044653Sdougm case '?': 26054653Sdougm (void) printf(gettext("usage: %s\n"), 26064653Sdougm sa_get_usage(USAGE_MOVE_SHARE)); 26074653Sdougm return (0); 26083034Sdougm } 26093034Sdougm } 26103034Sdougm 26113034Sdougm if (optind >= argc || sharepath == NULL) { 26125331Samw (void) printf(gettext("usage: %s\n"), 26135331Samw sa_get_usage(USAGE_MOVE_SHARE)); 26145331Samw if (dryrun || verbose || sharepath != NULL) { 26155331Samw (void) printf(gettext("\tgroup must be specified\n")); 26165331Samw ret = SA_NO_SUCH_GROUP; 26175331Samw } else { 26185331Samw if (sharepath == NULL) { 26195331Samw ret = SA_SYNTAX_ERR; 26204653Sdougm (void) printf(gettext( 26215331Samw "\tsharepath must be specified\n")); 26224653Sdougm } else { 26235331Samw ret = SA_OK; 26244653Sdougm } 26255331Samw } 26264653Sdougm } else { 26274653Sdougm sa_group_t parent; 26284653Sdougm char *zfsold; 26294653Sdougm char *zfsnew; 26304653Sdougm 26313034Sdougm if (sharepath == NULL) { 26324653Sdougm (void) printf(gettext( 26334653Sdougm "sharepath must be specified with the -s " 26344653Sdougm "option\n")); 26354653Sdougm return (SA_BAD_PATH); 26364653Sdougm } 26373910Sdougm group = sa_get_group(handle, argv[optind]); 26384653Sdougm if (group == NULL) { 26394653Sdougm (void) printf(gettext("Group \"%s\" not found\n"), 26404653Sdougm argv[optind]); 26414653Sdougm return (SA_NO_SUCH_GROUP); 26424653Sdougm } 26434653Sdougm share = sa_find_share(handle, sharepath); 26444653Sdougm authdst = check_authorizations(argv[optind], flags); 26454653Sdougm if (share == NULL) { 26463034Sdougm (void) printf(gettext("Share not found: %s\n"), 26474653Sdougm sharepath); 26484653Sdougm return (SA_NO_SUCH_PATH); 26494653Sdougm } 26504653Sdougm 26514653Sdougm parent = sa_get_parent_group(share); 26524653Sdougm if (parent != NULL) { 26534653Sdougm char *pname; 26544653Sdougm pname = sa_get_group_attr(parent, "name"); 26554653Sdougm if (pname != NULL) { 26563034Sdougm authsrc = check_authorizations(pname, flags); 26573034Sdougm sa_free_attr_string(pname); 26584653Sdougm } 26594653Sdougm zfsold = sa_get_group_attr(parent, "zfs"); 26604653Sdougm zfsnew = sa_get_group_attr(group, "zfs"); 26614653Sdougm if ((zfsold != NULL && zfsnew == NULL) || 26624653Sdougm (zfsold == NULL && zfsnew != NULL)) { 26633034Sdougm ret = SA_NOT_ALLOWED; 26643034Sdougm } 26654653Sdougm if (zfsold != NULL) 26664653Sdougm sa_free_attr_string(zfsold); 26674653Sdougm if (zfsnew != NULL) 26684653Sdougm sa_free_attr_string(zfsnew); 26694653Sdougm } 26704653Sdougm 26714653Sdougm if (ret == SA_OK && parent != group && !dryrun) { 26724653Sdougm char *oldstate; 26734653Sdougm /* 26744653Sdougm * Note that the share may need to be 26755331Samw * "unshared" if the new group is disabled and 26765331Samw * the old was enabled or it may need to be 26775331Samw * share to update if the new group is 26785331Samw * enabled. We disable before the move and 26795331Samw * will have to enable after the move in order 26805331Samw * to cleanup entries for protocols that 26815331Samw * aren't in the new group. 26824653Sdougm */ 26834653Sdougm oldstate = sa_get_group_attr(parent, "state"); 26844653Sdougm 26854653Sdougm /* enable_share determines what to do */ 26865331Samw if (strcmp(oldstate, "enabled") == 0) 26873034Sdougm (void) sa_disable_share(share, NULL); 26885331Samw 26894653Sdougm if (oldstate != NULL) 26903034Sdougm sa_free_attr_string(oldstate); 26913034Sdougm } 26924653Sdougm 26935331Samw if (!dryrun && ret == SA_OK) 26945331Samw ret = sa_move_share(group, share); 26955331Samw 26965331Samw /* 26975331Samw * Reenable and update any config information. 26985331Samw */ 26995331Samw if (ret == SA_OK && parent != group && !dryrun) { 27005331Samw ret = sa_update_config(handle); 27015331Samw 27025331Samw (void) enable_share(handle, group, share, 1); 27035331Samw } 27045331Samw 27054653Sdougm if (ret != SA_OK) 27064653Sdougm (void) printf(gettext("Could not move share: %s\n"), 27074653Sdougm sa_errorstr(ret)); 27084653Sdougm 27094653Sdougm if (dryrun && ret == SA_OK && !(authsrc & authdst) && 27104653Sdougm verbose) { 27114653Sdougm (void) printf(gettext("Command would fail: %s\n"), 27124653Sdougm sa_errorstr(SA_NO_PERMISSION)); 27134653Sdougm } 27143034Sdougm } 27153034Sdougm return (ret); 27163034Sdougm } 27173034Sdougm 27183034Sdougm /* 27193034Sdougm * sa_removeshare(flags, argc, argv) 27203034Sdougm * 27213034Sdougm * implements remove-share subcommand. 27223034Sdougm */ 27233034Sdougm 27243034Sdougm int 27253910Sdougm sa_removeshare(sa_handle_t handle, int flags, int argc, char *argv[]) 27263034Sdougm { 27273034Sdougm int verbose = 0; 27283034Sdougm int dryrun = 0; 27293034Sdougm int force = 0; 27303034Sdougm int c; 27313034Sdougm int ret = SA_OK; 27323034Sdougm sa_group_t group; 27335331Samw sa_resource_t resource = NULL; 27345331Samw sa_share_t share = NULL; 27355331Samw char *rsrcname = NULL; 27363034Sdougm char *sharepath = NULL; 27373034Sdougm char dir[MAXPATHLEN]; 27383034Sdougm int auth; 27393034Sdougm 27405331Samw while ((c = getopt(argc, argv, "?hfnr:s:v")) != EOF) { 27414653Sdougm switch (c) { 27424653Sdougm case 'n': 27434653Sdougm dryrun++; 27444653Sdougm break; 27454653Sdougm case 'v': 27464653Sdougm verbose++; 27474653Sdougm break; 27484653Sdougm case 'f': 27494653Sdougm force++; 27504653Sdougm break; 27514653Sdougm case 's': 27524653Sdougm /* 27534653Sdougm * Remove share path from group. Currently limit 27544653Sdougm * to one share per command. 27554653Sdougm */ 27564653Sdougm if (sharepath != NULL) { 27574653Sdougm (void) printf(gettext( 27584653Sdougm "Removing multiple shares not " 27593034Sdougm "supported\n")); 27604653Sdougm return (SA_SYNTAX_ERR); 27614653Sdougm } 27624653Sdougm sharepath = optarg; 27634653Sdougm break; 27645331Samw case 'r': 27655331Samw /* 27665331Samw * Remove share from group if last resource or remove 27675331Samw * resource from share if multiple resources. 27685331Samw */ 27695331Samw if (rsrcname != NULL) { 27705331Samw (void) printf(gettext( 27715331Samw "Removing multiple resource names not " 27725331Samw "supported\n")); 27735331Samw return (SA_SYNTAX_ERR); 27745331Samw } 27755331Samw rsrcname = optarg; 27765331Samw break; 27774653Sdougm default: 27784653Sdougm case 'h': 27794653Sdougm case '?': 27804653Sdougm (void) printf(gettext("usage: %s\n"), 27814653Sdougm sa_get_usage(USAGE_REMOVE_SHARE)); 27824653Sdougm return (0); 27833034Sdougm } 27843034Sdougm } 27853034Sdougm 27865331Samw if (optind >= argc || (rsrcname == NULL && sharepath == NULL)) { 27875331Samw if (sharepath == NULL && rsrcname == NULL) { 27883034Sdougm (void) printf(gettext("usage: %s\n"), 27894653Sdougm sa_get_usage(USAGE_REMOVE_SHARE)); 27905331Samw (void) printf(gettext("\t-s sharepath or -r resource" 27915331Samw " must be specified\n")); 27924653Sdougm ret = SA_BAD_PATH; 27934653Sdougm } else { 27944653Sdougm ret = SA_OK; 27954653Sdougm } 27963034Sdougm } 27974653Sdougm if (ret != SA_OK) { 27984653Sdougm return (ret); 27994653Sdougm } 28004653Sdougm 28014653Sdougm if (optind < argc) { 28023034Sdougm if ((optind + 1) < argc) { 28034653Sdougm (void) printf(gettext("Extraneous group(s) at end of " 28044653Sdougm "command\n")); 28054653Sdougm ret = SA_SYNTAX_ERR; 28063034Sdougm } else { 28074653Sdougm group = sa_get_group(handle, argv[optind]); 28084653Sdougm if (group == NULL) { 28094653Sdougm (void) printf(gettext( 28104653Sdougm "Group \"%s\" not found\n"), argv[optind]); 28114653Sdougm ret = SA_NO_SUCH_GROUP; 28124653Sdougm } 28133034Sdougm } 28144653Sdougm } else { 28153034Sdougm group = NULL; 28164653Sdougm } 28174653Sdougm 28185331Samw if (rsrcname != NULL) { 28195331Samw resource = sa_find_resource(handle, rsrcname); 28205331Samw if (resource == NULL) { 28215331Samw ret = SA_NO_SUCH_RESOURCE; 28225331Samw (void) printf(gettext( 28235331Samw "Resource name not found for share: %s\n"), 28245331Samw rsrcname); 28255331Samw } 28265331Samw } 28275331Samw 28284653Sdougm /* 28294653Sdougm * Lookup the path in the internal configuration. Care 28304653Sdougm * must be taken to handle the case where the 28314653Sdougm * underlying path has been removed since we need to 28324653Sdougm * be able to deal with that as well. 28334653Sdougm */ 28344653Sdougm if (ret == SA_OK) { 28355331Samw if (sharepath != NULL) { 28365331Samw if (group != NULL) 28375331Samw share = sa_get_share(group, sharepath); 28385331Samw else 28395331Samw share = sa_find_share(handle, sharepath); 28405331Samw } 28415331Samw 28425331Samw if (resource != NULL) { 28435331Samw sa_share_t rsrcshare; 28445331Samw rsrcshare = sa_get_resource_parent(resource); 28455331Samw if (share == NULL) 28465331Samw share = rsrcshare; 28475331Samw else if (share != rsrcshare) { 28485331Samw ret = SA_NO_SUCH_RESOURCE; 28495331Samw (void) printf(gettext( 28505331Samw "Bad resource name for share: %s\n"), 28515331Samw rsrcname); 28525331Samw share = NULL; 28535331Samw } 28545331Samw } 28555331Samw 28563663Sdougm /* 28573663Sdougm * If we didn't find the share with the provided path, 28583663Sdougm * it may be a symlink so attempt to resolve it using 28593663Sdougm * realpath and try again. Realpath will resolve any 28603663Sdougm * symlinks and place them in "dir". Note that 28613663Sdougm * sharepath is only used for the lookup the first 28623663Sdougm * time and later for error messages. dir will be used 28633663Sdougm * on the second attempt. Once a share is found, all 28643663Sdougm * operations are based off of the share variable. 28653663Sdougm */ 28663663Sdougm if (share == NULL) { 28674653Sdougm if (realpath(sharepath, dir) == NULL) { 28684653Sdougm ret = SA_BAD_PATH; 28694653Sdougm (void) printf(gettext( 28704653Sdougm "Path is not valid: %s\n"), sharepath); 28714653Sdougm } else { 28724653Sdougm if (group != NULL) 28734653Sdougm share = sa_get_share(group, dir); 28744653Sdougm else 28754653Sdougm share = sa_find_share(handle, dir); 28764653Sdougm } 28773663Sdougm } 28784653Sdougm } 28794653Sdougm 28804653Sdougm /* 28814653Sdougm * If there hasn't been an error, there was likely a 28824653Sdougm * path found. If not, give the appropriate error 28834653Sdougm * message and set the return error. If it was found, 28844653Sdougm * then disable the share and then remove it from the 28854653Sdougm * configuration. 28864653Sdougm */ 28874653Sdougm if (ret != SA_OK) { 28884653Sdougm return (ret); 28894653Sdougm } 28904653Sdougm if (share == NULL) { 28914653Sdougm if (group != NULL) 28923034Sdougm (void) printf(gettext("Share not found in group %s:" 28934653Sdougm " %s\n"), argv[optind], sharepath); 28944653Sdougm else 28953034Sdougm (void) printf(gettext("Share not found: %s\n"), 28964653Sdougm sharepath); 28975331Samw ret = SA_NO_SUCH_PATH; 28984653Sdougm } else { 28994653Sdougm if (group == NULL) 29003034Sdougm group = sa_get_parent_group(share); 29014653Sdougm if (!dryrun) { 29023034Sdougm if (ret == SA_OK) { 29035331Samw if (resource != NULL) 29045331Samw ret = sa_disable_resource(resource, 29055331Samw NULL); 29065331Samw else 29075331Samw ret = sa_disable_share(share, NULL); 29083034Sdougm /* 29094653Sdougm * We don't care if it fails since it 29103663Sdougm * could be disabled already. Some 29113663Sdougm * unexpected errors could occur that 29123663Sdougm * prevent removal, so also check for 29133663Sdougm * force being set. 29143034Sdougm */ 29155331Samw if ((ret == SA_OK || ret == SA_NO_SUCH_PATH || 29165331Samw ret == SA_NOT_SUPPORTED || 29175331Samw ret == SA_SYSTEM_ERR || force) && 29185331Samw resource == NULL) 29195331Samw ret = sa_remove_share(share); 29205331Samw 29215331Samw if ((ret == SA_OK || ret == SA_NO_SUCH_PATH || 29224653Sdougm ret == SA_NOT_SUPPORTED || 29235331Samw ret == SA_SYSTEM_ERR || force) && 29245331Samw resource != NULL) { 29255331Samw ret = sa_remove_resource(resource); 29265331Samw if (ret == SA_OK) { 29275331Samw /* 29285331Samw * If this was the 29295331Samw * last one, remove 29305331Samw * the share as well. 29315331Samw */ 29325331Samw resource = 29335331Samw sa_get_share_resource( 29345331Samw share, NULL); 29355331Samw if (resource == NULL) 29365331Samw ret = sa_remove_share( 29375331Samw share); 29385331Samw } 29394653Sdougm } 29404653Sdougm if (ret == SA_OK) 29414653Sdougm ret = sa_update_config(handle); 29423034Sdougm } 29434653Sdougm if (ret != SA_OK) 29445331Samw (void) printf(gettext("Could not remove share:" 29455331Samw " %s\n"), sa_errorstr(ret)); 29464653Sdougm } else if (ret == SA_OK) { 29473034Sdougm char *pname; 29483034Sdougm pname = sa_get_group_attr(group, "name"); 29493034Sdougm if (pname != NULL) { 29504653Sdougm auth = check_authorizations(pname, flags); 29514653Sdougm sa_free_attr_string(pname); 29523034Sdougm } 29533034Sdougm if (!auth && verbose) { 29544653Sdougm (void) printf(gettext( 29554653Sdougm "Command would fail: %s\n"), 29564653Sdougm sa_errorstr(SA_NO_PERMISSION)); 29573034Sdougm } 29583034Sdougm } 29593034Sdougm } 29603034Sdougm return (ret); 29613034Sdougm } 29623034Sdougm 29633034Sdougm /* 29643034Sdougm * sa_set_share(flags, argc, argv) 29653034Sdougm * 29663034Sdougm * implements set-share subcommand. 29673034Sdougm */ 29683034Sdougm 29693034Sdougm int 29703910Sdougm sa_set_share(sa_handle_t handle, int flags, int argc, char *argv[]) 29713034Sdougm { 29723034Sdougm int dryrun = 0; 29733034Sdougm int c; 29743034Sdougm int ret = SA_OK; 29753034Sdougm sa_group_t group, sharegroup; 2976*5772Sas200622 sa_share_t share = NULL; 29775331Samw sa_resource_t resource = NULL; 29783034Sdougm char *sharepath = NULL; 29793034Sdougm char *description = NULL; 29805331Samw char *desc; 29815331Samw char *rsrcname = NULL; 29825331Samw char *rsrc = NULL; 29835331Samw char *newname = NULL; 29845331Samw char *newrsrc; 29855331Samw char *groupname = NULL; 29863034Sdougm int auth; 29873034Sdougm int verbose = 0; 29883034Sdougm 29893034Sdougm while ((c = getopt(argc, argv, "?hnd:r:s:")) != EOF) { 29904653Sdougm switch (c) { 29914653Sdougm case 'n': 29924653Sdougm dryrun++; 29934653Sdougm break; 29944653Sdougm case 'd': 29954653Sdougm description = optarg; 29964653Sdougm break; 29974653Sdougm case 'v': 29984653Sdougm verbose++; 29994653Sdougm break; 30005331Samw case 'r': 30015331Samw /* 30025331Samw * Update share by resource name 30035331Samw */ 30045331Samw if (rsrcname != NULL) { 30055331Samw (void) printf(gettext( 30065331Samw "Updating multiple resource names not " 30075331Samw "supported\n")); 30085331Samw return (SA_SYNTAX_ERR); 30095331Samw } 30105331Samw rsrcname = optarg; 30115331Samw break; 30124653Sdougm case 's': 30134653Sdougm /* 30144653Sdougm * Save share path into group. Currently limit 30154653Sdougm * to one share per command. 30164653Sdougm */ 30174653Sdougm if (sharepath != NULL) { 30184653Sdougm (void) printf(gettext( 30194653Sdougm "Updating multiple shares not " 30203034Sdougm "supported\n")); 30215331Samw return (SA_SYNTAX_ERR); 30224653Sdougm } 30234653Sdougm sharepath = optarg; 30244653Sdougm break; 30254653Sdougm default: 30264653Sdougm case 'h': 30274653Sdougm case '?': 30284653Sdougm (void) printf(gettext("usage: %s\n"), 30294653Sdougm sa_get_usage(USAGE_SET_SHARE)); 30304653Sdougm return (SA_OK); 30313034Sdougm } 30323034Sdougm } 30334653Sdougm 30345331Samw if (optind >= argc && sharepath == NULL && rsrcname == NULL) { 30354653Sdougm if (sharepath == NULL) { 30364653Sdougm (void) printf(gettext("usage: %s\n"), 30374653Sdougm sa_get_usage(USAGE_SET_SHARE)); 30384653Sdougm (void) printf(gettext("\tgroup must be specified\n")); 30394653Sdougm ret = SA_BAD_PATH; 30404653Sdougm } else { 30414653Sdougm ret = SA_OK; 30424653Sdougm } 30433034Sdougm } 30443034Sdougm if ((optind + 1) < argc) { 30454653Sdougm (void) printf(gettext("usage: %s\n"), 30464653Sdougm sa_get_usage(USAGE_SET_SHARE)); 30474653Sdougm (void) printf(gettext("\tExtraneous group(s) at end\n")); 30484653Sdougm ret = SA_SYNTAX_ERR; 30493034Sdougm } 30504653Sdougm 30515331Samw /* 30525331Samw * Must have at least one of sharepath and rsrcrname. 30535331Samw * It is a syntax error to be missing both. 30545331Samw */ 30555331Samw if (sharepath == NULL && rsrcname == NULL) { 30565331Samw (void) printf(gettext("usage: %s\n"), 30575331Samw sa_get_usage(USAGE_SET_SHARE)); 30585331Samw ret = SA_SYNTAX_ERR; 30595331Samw } 30605331Samw 30614653Sdougm if (ret != SA_OK) 30624653Sdougm return (ret); 30634653Sdougm 30644653Sdougm if (optind < argc) { 30653034Sdougm groupname = argv[optind]; 30663910Sdougm group = sa_get_group(handle, groupname); 30674653Sdougm } else { 30683034Sdougm group = NULL; 30693034Sdougm groupname = NULL; 30704653Sdougm } 30715331Samw if (rsrcname != NULL) { 30725331Samw /* 30735331Samw * If rsrcname exists, split rename syntax and then 30745331Samw * convert to utf 8 if no errors. 30755331Samw */ 30765331Samw newname = strchr(rsrcname, '='); 30775331Samw if (newname != NULL) { 30785331Samw *newname++ = '\0'; 30795331Samw } 30805331Samw if (!validresource(rsrcname)) { 30815331Samw ret = SA_INVALID_NAME; 30825331Samw (void) printf(gettext("Invalid resource name: " 30835331Samw "\"%s\"\n"), rsrcname); 30845331Samw } else { 30855331Samw rsrc = conv_to_utf8(rsrcname); 30865331Samw } 30875331Samw if (newname != NULL) { 30885331Samw if (!validresource(newname)) { 30895331Samw ret = SA_INVALID_NAME; 30905331Samw (void) printf(gettext("Invalid resource name: " 30915331Samw "%s\n"), newname); 30925331Samw } else { 30935331Samw newrsrc = conv_to_utf8(newname); 30945331Samw } 30955331Samw } 30965331Samw } 30975331Samw 30985331Samw if (ret != SA_OK) { 30995331Samw if (rsrcname != NULL && rsrcname != rsrc) 31005331Samw sa_free_attr_string(rsrc); 31015331Samw if (newname != NULL && newname != newrsrc) 31025331Samw sa_free_attr_string(newrsrc); 31035331Samw return (ret); 31045331Samw } 31055331Samw 31065331Samw if (sharepath != NULL) { 31075331Samw share = sa_find_share(handle, sharepath); 31085331Samw } else if (rsrcname != NULL) { 31095331Samw resource = sa_find_resource(handle, rsrc); 3110*5772Sas200622 if (resource != NULL) 31115331Samw share = sa_get_resource_parent(resource); 3112*5772Sas200622 else 3113*5772Sas200622 ret = SA_NO_SUCH_RESOURCE; 31145331Samw } 31155331Samw if (share != NULL) { 31165331Samw sharegroup = sa_get_parent_group(share); 31175331Samw if (group != NULL && group != sharegroup) { 31185331Samw (void) printf(gettext("Group \"%s\" does not contain " 31195331Samw "share %s\n"), 31205331Samw argv[optind], sharepath); 31215331Samw ret = SA_BAD_PATH; 31225331Samw } else { 31235331Samw int delgroupname = 0; 31245331Samw if (groupname == NULL) { 31255331Samw groupname = sa_get_group_attr(sharegroup, 31265331Samw "name"); 31275331Samw delgroupname = 1; 31285331Samw } 31295331Samw if (groupname != NULL) { 31305331Samw auth = check_authorizations(groupname, flags); 31315331Samw if (delgroupname) { 31325331Samw sa_free_attr_string(groupname); 31335331Samw groupname = NULL; 31345331Samw } 31355331Samw } else { 31365331Samw ret = SA_NO_MEMORY; 31375331Samw } 31385331Samw if (rsrcname != NULL) { 31395331Samw resource = sa_find_resource(handle, rsrc); 31405331Samw if (!dryrun) { 31415331Samw if (newname != NULL && 31425331Samw resource != NULL) 31435331Samw ret = sa_rename_resource( 31445331Samw resource, newrsrc); 31455331Samw else if (newname != NULL) 31465331Samw ret = SA_NO_SUCH_RESOURCE; 31475331Samw if (newname != NULL && 31485331Samw newname != newrsrc) 31495331Samw sa_free_attr_string(newrsrc); 31505331Samw } 31515331Samw if (rsrc != rsrcname) 31525331Samw sa_free_attr_string(rsrc); 31535331Samw } 31545331Samw 31555331Samw /* 31565331Samw * If the user has set a description, it will be 31575331Samw * on the resource if -r was used otherwise it 31585331Samw * must be on the share. 31595331Samw */ 31605331Samw if (ret == SA_OK && description != NULL) { 31615331Samw desc = conv_to_utf8(description); 31625331Samw if (resource != NULL) 31635331Samw ret = sa_set_resource_description( 31645331Samw resource, desc); 31655331Samw else 31665331Samw ret = sa_set_share_description(share, 31675331Samw desc); 31685331Samw if (desc != description) 31695331Samw sa_free_share_description(desc); 31705331Samw } 31715331Samw } 31725331Samw if (!dryrun && ret == SA_OK) { 31735331Samw if (resource != NULL) 31745331Samw (void) sa_enable_resource(resource, NULL); 31755331Samw ret = sa_update_config(handle); 31765331Samw } 31775331Samw switch (ret) { 31785331Samw case SA_DUPLICATE_NAME: 31795331Samw (void) printf(gettext("Resource name in use: %s\n"), 31805331Samw rsrcname); 31815331Samw break; 31825331Samw default: 31835331Samw (void) printf(gettext("Could not set: %s\n"), 31845331Samw sa_errorstr(ret)); 31855331Samw break; 31865331Samw case SA_OK: 31875331Samw if (dryrun && !auth && verbose) { 31885331Samw (void) printf(gettext( 31895331Samw "Command would fail: %s\n"), 31905331Samw sa_errorstr(SA_NO_PERMISSION)); 31915331Samw } 31925331Samw break; 31935331Samw } 31945331Samw } else { 3195*5772Sas200622 switch (ret) { 3196*5772Sas200622 case SA_NO_SUCH_RESOURCE: 3197*5772Sas200622 (void) printf(gettext("Resource \"%s\" not found\n"), 3198*5772Sas200622 rsrcname); 3199*5772Sas200622 break; 3200*5772Sas200622 default: 3201*5772Sas200622 if (sharepath != NULL) { 3202*5772Sas200622 (void) printf( 3203*5772Sas200622 gettext("Share path \"%s\" not found\n"), 3204*5772Sas200622 sharepath); 3205*5772Sas200622 ret = SA_NO_SUCH_PATH; 3206*5772Sas200622 } else { 3207*5772Sas200622 (void) printf(gettext("Set failed: %s\n"), 3208*5772Sas200622 sa_errorstr(ret)); 3209*5772Sas200622 } 3210*5772Sas200622 } 32113034Sdougm } 32124653Sdougm 32133034Sdougm return (ret); 32143034Sdougm } 32153034Sdougm 32163034Sdougm /* 32173034Sdougm * add_security(group, sectype, optlist, proto, *err) 32183034Sdougm * 32193034Sdougm * Helper function to add a security option (named optionset) to the 32203034Sdougm * group. 32213034Sdougm */ 32223034Sdougm 32233034Sdougm static int 32243034Sdougm add_security(sa_group_t group, char *sectype, 32255331Samw struct options *optlist, char *proto, int *err) 32263034Sdougm { 32273034Sdougm sa_security_t security; 32283034Sdougm int ret = SA_OK; 32293034Sdougm int result = 0; 32303034Sdougm 32313034Sdougm sectype = sa_proto_space_alias(proto, sectype); 32323034Sdougm security = sa_get_security(group, sectype, proto); 32334653Sdougm if (security == NULL) 32344653Sdougm security = sa_create_security(group, sectype, proto); 32354653Sdougm 32363034Sdougm if (sectype != NULL) 32374653Sdougm sa_free_attr_string(sectype); 32384653Sdougm 32394653Sdougm if (security == NULL) 32404653Sdougm return (ret); 32414653Sdougm 32424653Sdougm while (optlist != NULL) { 32433034Sdougm sa_property_t prop; 32443034Sdougm prop = sa_get_property(security, optlist->optname); 32453034Sdougm if (prop == NULL) { 32463034Sdougm /* 32474653Sdougm * Add the property, but only if it is 32483034Sdougm * a non-NULL or non-zero length value 32493034Sdougm */ 32504653Sdougm if (optlist->optvalue != NULL) { 32514653Sdougm prop = sa_create_property(optlist->optname, 32524653Sdougm optlist->optvalue); 32534653Sdougm if (prop != NULL) { 32545331Samw ret = sa_valid_property(security, 32555331Samw proto, prop); 32564653Sdougm if (ret != SA_OK) { 32574653Sdougm (void) sa_remove_property(prop); 32584653Sdougm (void) printf(gettext( 32594653Sdougm "Could not add " 32604653Sdougm "property %s: %s\n"), 32614653Sdougm optlist->optname, 32624653Sdougm sa_errorstr(ret)); 32634653Sdougm } 32644653Sdougm if (ret == SA_OK) { 32654653Sdougm ret = sa_add_property(security, 32664653Sdougm prop); 32674653Sdougm if (ret != SA_OK) { 32684653Sdougm (void) printf(gettext( 32694653Sdougm "Could not add " 32705331Samw "property (%s=%s):" 32715331Samw " %s\n"), 32724653Sdougm optlist->optname, 32734653Sdougm optlist->optvalue, 32744653Sdougm sa_errorstr(ret)); 32754653Sdougm } else { 32764653Sdougm result = 1; 32774653Sdougm } 32784653Sdougm } 32793034Sdougm } 32803034Sdougm } 32813034Sdougm } else { 32824653Sdougm ret = sa_update_property(prop, optlist->optvalue); 32834653Sdougm result = 1; /* should check if really changed */ 32843034Sdougm } 32853034Sdougm optlist = optlist->next; 32864653Sdougm } 32874653Sdougm /* 32884653Sdougm * When done, properties may have all been removed but 32894653Sdougm * we need to keep the security type itself until 32904653Sdougm * explicitly removed. 32914653Sdougm */ 32924653Sdougm if (result) 32933034Sdougm ret = sa_commit_properties(security, 0); 32943034Sdougm *err = ret; 32953034Sdougm return (result); 32963034Sdougm } 32973034Sdougm 32983034Sdougm /* 32995089Sdougm * zfscheck(group, share) 33005089Sdougm * 33015089Sdougm * For the special case where a share was provided, make sure it is a 33025089Sdougm * compatible path for a ZFS property change. The only path 33035089Sdougm * acceptable is the path that defines the zfs sub-group (dataset with 33045089Sdougm * the sharenfs property set) and not one of the paths that inherited 33055089Sdougm * the NFS properties. Returns SA_OK if it is usable and 33065089Sdougm * SA_NOT_ALLOWED if it isn't. 33075089Sdougm * 33085089Sdougm * If group is not a ZFS group/subgroup, we assume OK since the check 33095089Sdougm * on return will catch errors for those cases. What we are looking 33105089Sdougm * for here is that the group is ZFS and the share is not the defining 33115089Sdougm * share. All else is SA_OK. 33125089Sdougm */ 33135089Sdougm 33145089Sdougm static int 33155089Sdougm zfscheck(sa_group_t group, sa_share_t share) 33165089Sdougm { 33175089Sdougm int ret = SA_OK; 33185089Sdougm char *attr; 33195089Sdougm 33205089Sdougm if (sa_group_is_zfs(group)) { 33215089Sdougm /* 33225089Sdougm * The group is a ZFS group. Does the share represent 33235089Sdougm * the dataset that defined the group? It is only OK 33245089Sdougm * if the attribute "subgroup" exists on the share and 33255089Sdougm * has a value of "true". 33265089Sdougm */ 33275089Sdougm 33285089Sdougm ret = SA_NOT_ALLOWED; 33295089Sdougm attr = sa_get_share_attr(share, "subgroup"); 33305089Sdougm if (attr != NULL) { 33315089Sdougm if (strcmp(attr, "true") == 0) 33325089Sdougm ret = SA_OK; 33335089Sdougm sa_free_attr_string(attr); 33345089Sdougm } 33355089Sdougm } 33365089Sdougm return (ret); 33375089Sdougm } 33385089Sdougm 33395089Sdougm /* 33405331Samw * basic_set(groupname, optlist, protocol, sharepath, rsrcname, dryrun) 33413034Sdougm * 33423034Sdougm * This function implements "set" when a name space (-S) is not 33433034Sdougm * specified. It is a basic set. Options and other CLI parsing has 33443034Sdougm * already been done. 33455331Samw * 33465331Samw * "rsrcname" is a "resource name". If it is non-NULL, it must match 33475331Samw * the sharepath if present or group if present, otherwise it is used 33485331Samw * to set options. 33495331Samw * 33505331Samw * Resource names may take options if the protocol supports it. If the 33515331Samw * protocol doesn't support resource level options, rsrcname is just 33525331Samw * an alias for the share. 33533034Sdougm */ 33543034Sdougm 33553034Sdougm static int 33563910Sdougm basic_set(sa_handle_t handle, char *groupname, struct options *optlist, 33575331Samw char *protocol, char *sharepath, char *rsrcname, int dryrun) 33583034Sdougm { 33593034Sdougm sa_group_t group; 33603034Sdougm int ret = SA_OK; 33613034Sdougm int change = 0; 33623034Sdougm struct list *worklist = NULL; 33633034Sdougm 33643910Sdougm group = sa_get_group(handle, groupname); 33653034Sdougm if (group != NULL) { 33664653Sdougm sa_share_t share = NULL; 33675331Samw sa_resource_t resource = NULL; 33685331Samw 33695331Samw /* 33705331Samw * If there is a sharepath, make sure it belongs to 33715331Samw * the group. 33725331Samw */ 33734653Sdougm if (sharepath != NULL) { 33744653Sdougm share = sa_get_share(group, sharepath); 33754653Sdougm if (share == NULL) { 33764653Sdougm (void) printf(gettext( 33774653Sdougm "Share does not exist in group %s\n"), 33784653Sdougm groupname, sharepath); 33794653Sdougm ret = SA_NO_SUCH_PATH; 33805089Sdougm } else { 33815089Sdougm /* if ZFS and OK, then only group */ 33825089Sdougm ret = zfscheck(group, share); 33835089Sdougm if (ret == SA_OK && 33845089Sdougm sa_group_is_zfs(group)) 33855089Sdougm share = NULL; 33865089Sdougm if (ret == SA_NOT_ALLOWED) 33875089Sdougm (void) printf(gettext( 33885089Sdougm "Properties on ZFS group shares " 33895089Sdougm "not supported: %s\n"), sharepath); 33904653Sdougm } 33913034Sdougm } 33925331Samw 33935331Samw /* 33945331Samw * If a resource name exists, make sure it belongs to 33955331Samw * the share if present else it belongs to the 33965331Samw * group. Also check the protocol to see if it 33975331Samw * supports resource level properties or not. If not, 33985331Samw * use share only. 33995331Samw */ 34005331Samw if (rsrcname != NULL) { 34015331Samw if (share != NULL) { 34025331Samw resource = sa_get_share_resource(share, 34035331Samw rsrcname); 34045331Samw if (resource == NULL) 34055331Samw ret = SA_NO_SUCH_RESOURCE; 34065331Samw } else { 34075331Samw resource = sa_get_resource(group, rsrcname); 34085331Samw if (resource != NULL) 34095331Samw share = sa_get_resource_parent( 34105331Samw resource); 34115331Samw else 34125331Samw ret = SA_NO_SUCH_RESOURCE; 34135331Samw } 34145331Samw if (ret == SA_OK && resource != NULL) { 34155331Samw uint64_t features; 34165331Samw /* 34175331Samw * Check to see if the resource can take 34185331Samw * properties. If so, stick the resource into 34195331Samw * "share" so it will all just work. 34205331Samw */ 34215331Samw features = sa_proto_get_featureset(protocol); 34225331Samw if (features & SA_FEATURE_RESOURCE) 34235331Samw share = (sa_share_t)resource; 34245331Samw } 34255331Samw } 34265331Samw 34274653Sdougm if (ret == SA_OK) { 34284653Sdougm /* group must exist */ 34294653Sdougm ret = valid_options(optlist, protocol, 34304653Sdougm share == NULL ? group : share, NULL); 34314653Sdougm if (ret == SA_OK && !dryrun) { 34324653Sdougm if (share != NULL) 34334653Sdougm change |= add_optionset(share, optlist, 34344653Sdougm protocol, &ret); 34354653Sdougm else 34364653Sdougm change |= add_optionset(group, optlist, 34374653Sdougm protocol, &ret); 34384653Sdougm if (ret == SA_OK && change) 34394653Sdougm worklist = add_list(worklist, group, 34405331Samw share, protocol); 34414653Sdougm } 34423034Sdougm } 34434653Sdougm free_opt(optlist); 34443034Sdougm } else { 34453034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 34463034Sdougm ret = SA_NO_SUCH_GROUP; 34473034Sdougm } 34483034Sdougm /* 34493034Sdougm * we have a group and potentially legal additions 34503034Sdougm */ 34513034Sdougm 34524653Sdougm /* 34534653Sdougm * Commit to configuration if not a dryrunp and properties 34544653Sdougm * have changed. 34554653Sdougm */ 34564653Sdougm if (!dryrun && ret == SA_OK && change && worklist != NULL) 34573034Sdougm /* properties changed, so update all shares */ 34585331Samw (void) enable_all_groups(handle, worklist, 0, 0, protocol, 34595331Samw B_TRUE); 34604653Sdougm 34613034Sdougm if (worklist != NULL) 34624653Sdougm free_list(worklist); 34633034Sdougm return (ret); 34643034Sdougm } 34653034Sdougm 34663034Sdougm /* 34673034Sdougm * space_set(groupname, optlist, protocol, sharepath, dryrun) 34683034Sdougm * 34693034Sdougm * This function implements "set" when a name space (-S) is 34703034Sdougm * specified. It is a namespace set. Options and other CLI parsing has 34713034Sdougm * already been done. 34723034Sdougm */ 34733034Sdougm 34743034Sdougm static int 34753910Sdougm space_set(sa_handle_t handle, char *groupname, struct options *optlist, 34765331Samw char *protocol, char *sharepath, int dryrun, char *sectype) 34773034Sdougm { 34783034Sdougm sa_group_t group; 34793034Sdougm int ret = SA_OK; 34803034Sdougm int change = 0; 34813034Sdougm struct list *worklist = NULL; 34823034Sdougm 34833034Sdougm /* 34843034Sdougm * make sure protcol and sectype are valid 34853034Sdougm */ 34863034Sdougm 34873034Sdougm if (sa_proto_valid_space(protocol, sectype) == 0) { 34884653Sdougm (void) printf(gettext("Option space \"%s\" not valid " 34894653Sdougm "for protocol.\n"), sectype); 34904653Sdougm return (SA_INVALID_SECURITY); 34913034Sdougm } 34923034Sdougm 34933910Sdougm group = sa_get_group(handle, groupname); 34943034Sdougm if (group != NULL) { 34954653Sdougm sa_share_t share = NULL; 34964653Sdougm if (sharepath != NULL) { 34974653Sdougm share = sa_get_share(group, sharepath); 34984653Sdougm if (share == NULL) { 34994653Sdougm (void) printf(gettext( 35004653Sdougm "Share does not exist in group %s\n"), 35014653Sdougm groupname, sharepath); 35024653Sdougm ret = SA_NO_SUCH_PATH; 35035089Sdougm } else { 35045089Sdougm /* if ZFS and OK, then only group */ 35055089Sdougm ret = zfscheck(group, share); 35065089Sdougm if (ret == SA_OK && 35075089Sdougm sa_group_is_zfs(group)) 35085089Sdougm share = NULL; 35095089Sdougm if (ret == SA_NOT_ALLOWED) 35105089Sdougm (void) printf(gettext( 35115089Sdougm "Properties on ZFS group shares " 35125089Sdougm "not supported: %s\n"), sharepath); 35134653Sdougm } 35143034Sdougm } 35154653Sdougm if (ret == SA_OK) { 35164653Sdougm /* group must exist */ 35174653Sdougm ret = valid_options(optlist, protocol, 35184653Sdougm share == NULL ? group : share, sectype); 35194653Sdougm if (ret == SA_OK && !dryrun) { 35204653Sdougm if (share != NULL) 35214653Sdougm change = add_security(share, sectype, 35224653Sdougm optlist, protocol, &ret); 35234653Sdougm else 35244653Sdougm change = add_security(group, sectype, 35254653Sdougm optlist, protocol, &ret); 35264653Sdougm if (ret != SA_OK) 35274653Sdougm (void) printf(gettext( 35284653Sdougm "Could not set property: %s\n"), 35294653Sdougm sa_errorstr(ret)); 35304653Sdougm } 35314653Sdougm if (ret == SA_OK && change) 35325331Samw worklist = add_list(worklist, group, share, 35335331Samw protocol); 35343034Sdougm } 35354653Sdougm free_opt(optlist); 35363034Sdougm } else { 35373034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 35383034Sdougm ret = SA_NO_SUCH_GROUP; 35393034Sdougm } 35405331Samw 35413034Sdougm /* 35425331Samw * We have a group and potentially legal additions. 35433034Sdougm */ 35443034Sdougm 35454653Sdougm /* Commit to configuration if not a dryrun */ 35463034Sdougm if (!dryrun && ret == 0) { 35474653Sdougm if (change && worklist != NULL) { 35484653Sdougm /* properties changed, so update all shares */ 35494653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 35505331Samw protocol, B_TRUE); 35514653Sdougm } 35524653Sdougm ret = sa_update_config(handle); 35533034Sdougm } 35543034Sdougm if (worklist != NULL) 35554653Sdougm free_list(worklist); 35563034Sdougm return (ret); 35573034Sdougm } 35583034Sdougm 35593034Sdougm /* 35603034Sdougm * sa_set(flags, argc, argv) 35613034Sdougm * 35623034Sdougm * Implements the set subcommand. It keys off of -S to determine which 35633034Sdougm * set of operations to actually do. 35643034Sdougm */ 35653034Sdougm 35663034Sdougm int 35673910Sdougm sa_set(sa_handle_t handle, int flags, int argc, char *argv[]) 35683034Sdougm { 35693034Sdougm char *groupname; 35703034Sdougm int verbose = 0; 35713034Sdougm int dryrun = 0; 35723034Sdougm int c; 35733034Sdougm char *protocol = NULL; 35743034Sdougm int ret = SA_OK; 35753034Sdougm struct options *optlist = NULL; 35765331Samw char *rsrcname = NULL; 35773034Sdougm char *sharepath = NULL; 35783034Sdougm char *optset = NULL; 35793034Sdougm int auth; 35803034Sdougm 35815331Samw while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) { 35824653Sdougm switch (c) { 35834653Sdougm case 'v': 35844653Sdougm verbose++; 35854653Sdougm break; 35864653Sdougm case 'n': 35874653Sdougm dryrun++; 35884653Sdougm break; 35894653Sdougm case 'P': 35905331Samw if (protocol != NULL) { 35915331Samw (void) printf(gettext( 35925331Samw "Specifying multiple protocols " 35935331Samw "not supported: %s\n"), protocol); 35945331Samw return (SA_SYNTAX_ERR); 35955331Samw } 35964653Sdougm protocol = optarg; 35974653Sdougm if (!sa_valid_protocol(protocol)) { 35984653Sdougm (void) printf(gettext( 35994653Sdougm "Invalid protocol specified: %s\n"), 36004653Sdougm protocol); 36014653Sdougm return (SA_INVALID_PROTOCOL); 36024653Sdougm } 36034653Sdougm break; 36044653Sdougm case 'p': 36054653Sdougm ret = add_opt(&optlist, optarg, 0); 36064653Sdougm switch (ret) { 36074653Sdougm case OPT_ADD_SYNTAX: 36084653Sdougm (void) printf(gettext("Property syntax error:" 36094653Sdougm " %s\n"), optarg); 36104653Sdougm return (SA_SYNTAX_ERR); 36114653Sdougm case OPT_ADD_MEMORY: 36124653Sdougm (void) printf(gettext("No memory to set " 36134653Sdougm "property: %s\n"), optarg); 36144653Sdougm return (SA_NO_MEMORY); 36154653Sdougm default: 36164653Sdougm break; 36174653Sdougm } 36184653Sdougm break; 36195331Samw case 'r': 36205331Samw if (rsrcname != NULL) { 36215331Samw (void) printf(gettext( 36225331Samw "Setting multiple resource names not" 36235331Samw " supported\n")); 36245331Samw return (SA_SYNTAX_ERR); 36255331Samw } 36265331Samw rsrcname = optarg; 36275331Samw break; 36284653Sdougm case 's': 36295331Samw if (sharepath != NULL) { 36305331Samw (void) printf(gettext( 36315331Samw "Setting multiple shares not supported\n")); 36325331Samw return (SA_SYNTAX_ERR); 36335331Samw } 36344653Sdougm sharepath = optarg; 36354653Sdougm break; 36364653Sdougm case 'S': 36375331Samw if (optset != NULL) { 36385331Samw (void) printf(gettext( 36395331Samw "Specifying multiple property " 36405331Samw "spaces not supported: %s\n"), optset); 36415331Samw return (SA_SYNTAX_ERR); 36425331Samw } 36434653Sdougm optset = optarg; 36444653Sdougm break; 36454653Sdougm default: 36464653Sdougm case 'h': 36474653Sdougm case '?': 36484653Sdougm (void) printf(gettext("usage: %s\n"), 36494653Sdougm sa_get_usage(USAGE_SET)); 36504653Sdougm return (SA_OK); 36513034Sdougm } 36523034Sdougm } 36533034Sdougm 36543034Sdougm if (optlist != NULL) 36554653Sdougm ret = chk_opt(optlist, optset != NULL, protocol); 36563034Sdougm 36573034Sdougm if (optind >= argc || (optlist == NULL && optset == NULL) || 36584653Sdougm protocol == NULL || ret != OPT_ADD_OK) { 36594653Sdougm char *sep = "\t"; 36604653Sdougm 36614653Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET)); 36624653Sdougm if (optind >= argc) { 36634653Sdougm (void) printf(gettext("%sgroup must be specified"), 36644653Sdougm sep); 36654653Sdougm sep = ", "; 36664653Sdougm } 36674653Sdougm if (optlist == NULL) { 36684653Sdougm (void) printf(gettext("%sat least one property must be" 36694653Sdougm " specified"), sep); 36704653Sdougm sep = ", "; 36714653Sdougm } 36724653Sdougm if (protocol == NULL) { 36734653Sdougm (void) printf(gettext("%sprotocol must be specified"), 36744653Sdougm sep); 36754653Sdougm sep = ", "; 36764653Sdougm } 36774653Sdougm (void) printf("\n"); 36784653Sdougm ret = SA_SYNTAX_ERR; 36793034Sdougm } else { 36803034Sdougm /* 36815089Sdougm * Group already exists so we can proceed after a few 36825089Sdougm * additional checks related to ZFS handling. 36833034Sdougm */ 36843034Sdougm 36854653Sdougm groupname = argv[optind]; 36865089Sdougm if (strcmp(groupname, "zfs") == 0) { 36875089Sdougm (void) printf(gettext("Changing properties for group " 36885089Sdougm "\"zfs\" not allowed\n")); 36895089Sdougm return (SA_NOT_ALLOWED); 36905089Sdougm } 36915089Sdougm 36924653Sdougm auth = check_authorizations(groupname, flags); 36934653Sdougm if (optset == NULL) 36944653Sdougm ret = basic_set(handle, groupname, optlist, protocol, 36955331Samw sharepath, rsrcname, dryrun); 36964653Sdougm else 36974653Sdougm ret = space_set(handle, groupname, optlist, protocol, 36984653Sdougm sharepath, dryrun, optset); 36994653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 37004653Sdougm (void) printf(gettext("Command would fail: %s\n"), 37014653Sdougm sa_errorstr(SA_NO_PERMISSION)); 37024653Sdougm } 37033034Sdougm } 37043034Sdougm return (ret); 37053034Sdougm } 37063034Sdougm 37073034Sdougm /* 37083034Sdougm * remove_options(group, optlist, proto, *err) 37093034Sdougm * 37104653Sdougm * Helper function to actually remove options from a group after all 37113034Sdougm * preprocessing is done. 37123034Sdougm */ 37133034Sdougm 37143034Sdougm static int 37153034Sdougm remove_options(sa_group_t group, struct options *optlist, 37165331Samw char *proto, int *err) 37173034Sdougm { 37183034Sdougm struct options *cur; 37193034Sdougm sa_optionset_t optionset; 37203034Sdougm sa_property_t prop; 37213034Sdougm int change = 0; 37223034Sdougm int ret = SA_OK; 37233034Sdougm 37243034Sdougm optionset = sa_get_optionset(group, proto); 37253034Sdougm if (optionset != NULL) { 37264653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 37274653Sdougm prop = sa_get_property(optionset, cur->optname); 37284653Sdougm if (prop != NULL) { 37294653Sdougm ret = sa_remove_property(prop); 37304653Sdougm if (ret != SA_OK) 37314653Sdougm break; 37324653Sdougm change = 1; 37334653Sdougm } 37343034Sdougm } 37353034Sdougm } 37363034Sdougm if (ret == SA_OK && change) 37374653Sdougm ret = sa_commit_properties(optionset, 0); 37383034Sdougm 37393034Sdougm if (err != NULL) 37404653Sdougm *err = ret; 37413034Sdougm return (change); 37423034Sdougm } 37433034Sdougm 37443034Sdougm /* 37453034Sdougm * valid_unset(group, optlist, proto) 37463034Sdougm * 37473034Sdougm * Sanity check the optlist to make sure they can be removed. Issue an 37483034Sdougm * error if a property doesn't exist. 37493034Sdougm */ 37503034Sdougm 37513034Sdougm static int 37523034Sdougm valid_unset(sa_group_t group, struct options *optlist, char *proto) 37533034Sdougm { 37543034Sdougm struct options *cur; 37553034Sdougm sa_optionset_t optionset; 37563034Sdougm sa_property_t prop; 37573034Sdougm int ret = SA_OK; 37583034Sdougm 37593034Sdougm optionset = sa_get_optionset(group, proto); 37603034Sdougm if (optionset != NULL) { 37614653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 37624653Sdougm prop = sa_get_property(optionset, cur->optname); 37634653Sdougm if (prop == NULL) { 37644653Sdougm (void) printf(gettext( 37654653Sdougm "Could not unset property %s: not set\n"), 37664653Sdougm cur->optname); 37674653Sdougm ret = SA_NO_SUCH_PROP; 37684653Sdougm } 37693034Sdougm } 37703034Sdougm } 37713034Sdougm return (ret); 37723034Sdougm } 37733034Sdougm 37743034Sdougm /* 37753034Sdougm * valid_unset_security(group, optlist, proto) 37763034Sdougm * 37773034Sdougm * Sanity check the optlist to make sure they can be removed. Issue an 37783034Sdougm * error if a property doesn't exist. 37793034Sdougm */ 37803034Sdougm 37813034Sdougm static int 37823034Sdougm valid_unset_security(sa_group_t group, struct options *optlist, char *proto, 37835331Samw char *sectype) 37843034Sdougm { 37853034Sdougm struct options *cur; 37863034Sdougm sa_security_t security; 37873034Sdougm sa_property_t prop; 37883034Sdougm int ret = SA_OK; 37893034Sdougm char *sec; 37903034Sdougm 37913034Sdougm sec = sa_proto_space_alias(proto, sectype); 37923034Sdougm security = sa_get_security(group, sec, proto); 37933034Sdougm if (security != NULL) { 37944653Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 37954653Sdougm prop = sa_get_property(security, cur->optname); 37964653Sdougm if (prop == NULL) { 37974653Sdougm (void) printf(gettext( 37984653Sdougm "Could not unset property %s: not set\n"), 37994653Sdougm cur->optname); 38004653Sdougm ret = SA_NO_SUCH_PROP; 38014653Sdougm } 38023034Sdougm } 38033034Sdougm } else { 38044653Sdougm (void) printf(gettext( 38054653Sdougm "Could not unset %s: space not defined\n"), sectype); 38064653Sdougm ret = SA_NO_SUCH_SECURITY; 38073034Sdougm } 38083034Sdougm if (sec != NULL) 38094653Sdougm sa_free_attr_string(sec); 38103034Sdougm return (ret); 38113034Sdougm } 38123034Sdougm 38133034Sdougm /* 38143034Sdougm * remove_security(group, optlist, proto) 38153034Sdougm * 38163034Sdougm * Remove the properties since they were checked as valid. 38173034Sdougm */ 38183034Sdougm 38193034Sdougm static int 38203034Sdougm remove_security(sa_group_t group, char *sectype, 38215331Samw struct options *optlist, char *proto, int *err) 38223034Sdougm { 38233034Sdougm sa_security_t security; 38243034Sdougm int ret = SA_OK; 38253034Sdougm int change = 0; 38263034Sdougm 38273034Sdougm sectype = sa_proto_space_alias(proto, sectype); 38283034Sdougm security = sa_get_security(group, sectype, proto); 38293034Sdougm if (sectype != NULL) 38304653Sdougm sa_free_attr_string(sectype); 38313034Sdougm 38323034Sdougm if (security != NULL) { 38334653Sdougm while (optlist != NULL) { 38344653Sdougm sa_property_t prop; 38354653Sdougm prop = sa_get_property(security, optlist->optname); 38364653Sdougm if (prop != NULL) { 38374653Sdougm ret = sa_remove_property(prop); 38384653Sdougm if (ret != SA_OK) 38394653Sdougm break; 38404653Sdougm change = 1; 38414653Sdougm } 38424653Sdougm optlist = optlist->next; 38433034Sdougm } 38443034Sdougm /* 38453034Sdougm * when done, properties may have all been removed but 38463034Sdougm * we need to keep the security type itself until 38473034Sdougm * explicitly removed. 38483034Sdougm */ 38494653Sdougm if (ret == SA_OK && change) 38504653Sdougm ret = sa_commit_properties(security, 0); 38513034Sdougm } else { 38524653Sdougm ret = SA_NO_SUCH_PROP; 38533034Sdougm } 38543034Sdougm if (err != NULL) 38554653Sdougm *err = ret; 38563034Sdougm return (change); 38573034Sdougm } 38583034Sdougm 38593034Sdougm /* 38605331Samw * basic_unset(groupname, optlist, protocol, sharepath, rsrcname, dryrun) 38613034Sdougm * 38624653Sdougm * Unset non-named optionset properties. 38633034Sdougm */ 38643034Sdougm 38653034Sdougm static int 38663910Sdougm basic_unset(sa_handle_t handle, char *groupname, struct options *optlist, 38675331Samw char *protocol, char *sharepath, char *rsrcname, int dryrun) 38683034Sdougm { 38693034Sdougm sa_group_t group; 38703034Sdougm int ret = SA_OK; 38713034Sdougm int change = 0; 38723034Sdougm struct list *worklist = NULL; 38734653Sdougm sa_share_t share = NULL; 38745331Samw sa_resource_t resource = NULL; 38753034Sdougm 38763910Sdougm group = sa_get_group(handle, groupname); 38774653Sdougm if (group == NULL) 38784653Sdougm return (ret); 38794653Sdougm 38805331Samw /* 38815331Samw * If there is a sharepath, make sure it belongs to 38825331Samw * the group. 38835331Samw */ 38844653Sdougm if (sharepath != NULL) { 38853034Sdougm share = sa_get_share(group, sharepath); 38863034Sdougm if (share == NULL) { 38874653Sdougm (void) printf(gettext( 38884653Sdougm "Share does not exist in group %s\n"), 38894653Sdougm groupname, sharepath); 38904653Sdougm ret = SA_NO_SUCH_PATH; 38913034Sdougm } 38924653Sdougm } 38935331Samw /* 38945331Samw * If a resource name exists, make sure it belongs to 38955331Samw * the share if present else it belongs to the 38965331Samw * group. Also check the protocol to see if it 38975331Samw * supports resource level properties or not. If not, 38985331Samw * use share only. 38995331Samw */ 39005331Samw if (rsrcname != NULL) { 39015331Samw if (share != NULL) { 39025331Samw resource = sa_get_share_resource(share, rsrcname); 39035331Samw if (resource == NULL) 39045331Samw ret = SA_NO_SUCH_RESOURCE; 39055331Samw } else { 39065331Samw resource = sa_get_resource(group, rsrcname); 39075331Samw if (resource != NULL) { 39085331Samw share = sa_get_resource_parent(resource); 39095331Samw } else { 39105331Samw ret = SA_NO_SUCH_RESOURCE; 39115331Samw } 39125331Samw } 39135331Samw if (ret == SA_OK && resource != NULL) { 39145331Samw uint64_t features; 39155331Samw /* 39165331Samw * Check to see if the resource can take 39175331Samw * properties. If so, stick the resource into 39185331Samw * "share" so it will all just work. 39195331Samw */ 39205331Samw features = sa_proto_get_featureset(protocol); 39215331Samw if (features & SA_FEATURE_RESOURCE) 39225331Samw share = (sa_share_t)resource; 39235331Samw } 39245331Samw } 39255331Samw 39264653Sdougm if (ret == SA_OK) { 39273034Sdougm /* group must exist */ 39283034Sdougm ret = valid_unset(share != NULL ? share : group, 39294653Sdougm optlist, protocol); 39303034Sdougm if (ret == SA_OK && !dryrun) { 39314653Sdougm if (share != NULL) { 39324653Sdougm sa_optionset_t optionset; 39334653Sdougm sa_property_t prop; 39344653Sdougm change |= remove_options(share, optlist, 39354653Sdougm protocol, &ret); 39364653Sdougm /* 39374653Sdougm * If a share optionset is 39384653Sdougm * empty, remove it. 39394653Sdougm */ 39404653Sdougm optionset = sa_get_optionset((sa_share_t)share, 39414653Sdougm protocol); 39424653Sdougm if (optionset != NULL) { 39434653Sdougm prop = sa_get_property(optionset, NULL); 39444653Sdougm if (prop == NULL) 39454653Sdougm (void) sa_destroy_optionset( 39464653Sdougm optionset); 39474653Sdougm } 39484653Sdougm } else { 39494653Sdougm change |= remove_options(group, 39504653Sdougm optlist, protocol, &ret); 39513034Sdougm } 39524653Sdougm if (ret == SA_OK && change) 39535331Samw worklist = add_list(worklist, group, share, 39545331Samw protocol); 39554653Sdougm if (ret != SA_OK) 39564653Sdougm (void) printf(gettext( 39574653Sdougm "Could not remove properties: " 39584653Sdougm "%s\n"), sa_errorstr(ret)); 39593034Sdougm } 39604653Sdougm } else { 39615331Samw (void) printf(gettext("Group \"%s\" not found\n"), groupname); 39623034Sdougm ret = SA_NO_SUCH_GROUP; 39633034Sdougm } 39644653Sdougm free_opt(optlist); 39653034Sdougm 39663034Sdougm /* 39674653Sdougm * We have a group and potentially legal additions 39684653Sdougm * 39694653Sdougm * Commit to configuration if not a dryrun 39703034Sdougm */ 39713034Sdougm if (!dryrun && ret == SA_OK) { 39724653Sdougm if (change && worklist != NULL) { 39734653Sdougm /* properties changed, so update all shares */ 39744653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 39755331Samw protocol, B_TRUE); 39764653Sdougm } 39773034Sdougm } 39783034Sdougm if (worklist != NULL) 39794653Sdougm free_list(worklist); 39803034Sdougm return (ret); 39813034Sdougm } 39823034Sdougm 39833034Sdougm /* 39843034Sdougm * space_unset(groupname, optlist, protocol, sharepath, dryrun) 39853034Sdougm * 39864653Sdougm * Unset named optionset properties. 39873034Sdougm */ 39883034Sdougm static int 39893910Sdougm space_unset(sa_handle_t handle, char *groupname, struct options *optlist, 39905331Samw char *protocol, char *sharepath, int dryrun, char *sectype) 39913034Sdougm { 39923034Sdougm sa_group_t group; 39933034Sdougm int ret = SA_OK; 39943034Sdougm int change = 0; 39953034Sdougm struct list *worklist = NULL; 39964653Sdougm sa_share_t share = NULL; 39973034Sdougm 39983910Sdougm group = sa_get_group(handle, groupname); 39994653Sdougm if (group == NULL) { 40004653Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 40014653Sdougm return (SA_NO_SUCH_GROUP); 40024653Sdougm } 40034653Sdougm if (sharepath != NULL) { 40043034Sdougm share = sa_get_share(group, sharepath); 40053034Sdougm if (share == NULL) { 40064653Sdougm (void) printf(gettext( 40074653Sdougm "Share does not exist in group %s\n"), 40084653Sdougm groupname, sharepath); 40094653Sdougm return (SA_NO_SUCH_PATH); 40103034Sdougm } 40114653Sdougm } 40125331Samw ret = valid_unset_security(share != NULL ? share : group, 40135331Samw optlist, protocol, sectype); 40144653Sdougm 40154653Sdougm if (ret == SA_OK && !dryrun) { 40164653Sdougm if (optlist != NULL) { 40173034Sdougm if (share != NULL) { 40184653Sdougm sa_security_t optionset; 40194653Sdougm sa_property_t prop; 40204653Sdougm change = remove_security(share, 40214653Sdougm sectype, optlist, protocol, &ret); 40224653Sdougm 40234653Sdougm /* If a share security is empty, remove it */ 40244653Sdougm optionset = sa_get_security((sa_group_t)share, 40254653Sdougm sectype, protocol); 40264653Sdougm if (optionset != NULL) { 40274653Sdougm prop = sa_get_property(optionset, 40284653Sdougm NULL); 40294653Sdougm if (prop == NULL) 40304653Sdougm ret = sa_destroy_security( 40314653Sdougm optionset); 40324653Sdougm } 40333034Sdougm } else { 40344653Sdougm change = remove_security(group, sectype, 40354653Sdougm optlist, protocol, &ret); 40363034Sdougm } 40374653Sdougm } else { 40383034Sdougm sa_security_t security; 40393034Sdougm char *sec; 40403034Sdougm sec = sa_proto_space_alias(protocol, sectype); 40413034Sdougm security = sa_get_security(group, sec, protocol); 40423034Sdougm if (sec != NULL) 40434653Sdougm sa_free_attr_string(sec); 40443034Sdougm if (security != NULL) { 40454653Sdougm ret = sa_destroy_security(security); 40464653Sdougm if (ret == SA_OK) 40474653Sdougm change = 1; 40483034Sdougm } else { 40494653Sdougm ret = SA_NO_SUCH_PROP; 40503034Sdougm } 40514653Sdougm } 40524653Sdougm if (ret != SA_OK) 40533034Sdougm (void) printf(gettext("Could not unset property: %s\n"), 40544653Sdougm sa_errorstr(ret)); 40553034Sdougm } 40564653Sdougm 40574653Sdougm if (ret == SA_OK && change) 40585331Samw worklist = add_list(worklist, group, 0, protocol); 40594653Sdougm 40603034Sdougm free_opt(optlist); 40613034Sdougm /* 40624653Sdougm * We have a group and potentially legal additions 40633034Sdougm */ 40643034Sdougm 40654653Sdougm /* Commit to configuration if not a dryrun */ 40663034Sdougm if (!dryrun && ret == 0) { 40673034Sdougm /* properties changed, so update all shares */ 40684653Sdougm if (change && worklist != NULL) 40694653Sdougm (void) enable_all_groups(handle, worklist, 0, 0, 40705331Samw protocol, B_TRUE); 40714653Sdougm ret = sa_update_config(handle); 40723034Sdougm } 40733034Sdougm if (worklist != NULL) 40744653Sdougm free_list(worklist); 40753034Sdougm return (ret); 40763034Sdougm } 40773034Sdougm 40783034Sdougm /* 40793034Sdougm * sa_unset(flags, argc, argv) 40803034Sdougm * 40814653Sdougm * Implements the unset subcommand. Parsing done here and then basic 40823034Sdougm * or space versions of the real code are called. 40833034Sdougm */ 40843034Sdougm 40853034Sdougm int 40863910Sdougm sa_unset(sa_handle_t handle, int flags, int argc, char *argv[]) 40873034Sdougm { 40883034Sdougm char *groupname; 40893034Sdougm int verbose = 0; 40903034Sdougm int dryrun = 0; 40913034Sdougm int c; 40923034Sdougm char *protocol = NULL; 40933034Sdougm int ret = SA_OK; 40943034Sdougm struct options *optlist = NULL; 40955331Samw char *rsrcname = NULL; 40963034Sdougm char *sharepath = NULL; 40973034Sdougm char *optset = NULL; 40983034Sdougm int auth; 40993034Sdougm 41005331Samw while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) { 41014653Sdougm switch (c) { 41024653Sdougm case 'v': 41034653Sdougm verbose++; 41044653Sdougm break; 41054653Sdougm case 'n': 41064653Sdougm dryrun++; 41074653Sdougm break; 41084653Sdougm case 'P': 41095331Samw if (protocol != NULL) { 41105331Samw (void) printf(gettext( 41115331Samw "Specifying multiple protocols " 41125331Samw "not supported: %s\n"), protocol); 41135331Samw return (SA_SYNTAX_ERR); 41145331Samw } 41154653Sdougm protocol = optarg; 41164653Sdougm if (!sa_valid_protocol(protocol)) { 41174653Sdougm (void) printf(gettext( 41184653Sdougm "Invalid protocol specified: %s\n"), 41194653Sdougm protocol); 41204653Sdougm return (SA_INVALID_PROTOCOL); 41214653Sdougm } 41224653Sdougm break; 41234653Sdougm case 'p': 41244653Sdougm ret = add_opt(&optlist, optarg, 1); 41254653Sdougm switch (ret) { 41264653Sdougm case OPT_ADD_SYNTAX: 41274653Sdougm (void) printf(gettext("Property syntax error " 41284653Sdougm "for property %s\n"), optarg); 41294653Sdougm return (SA_SYNTAX_ERR); 41304653Sdougm 41314653Sdougm case OPT_ADD_PROPERTY: 41324653Sdougm (void) printf(gettext("Properties need to be " 41334653Sdougm "set with set command: %s\n"), optarg); 41344653Sdougm return (SA_SYNTAX_ERR); 41354653Sdougm 41364653Sdougm default: 41374653Sdougm break; 41384653Sdougm } 41394653Sdougm break; 41405331Samw case 'r': 41415331Samw /* 41425331Samw * Unset properties on resource if applicable or on 41435331Samw * share if resource for this protocol doesn't use 41445331Samw * resources. 41455331Samw */ 41465331Samw if (rsrcname != NULL) { 41475331Samw (void) printf(gettext( 41485331Samw "Unsetting multiple resource " 41495331Samw "names not supported\n")); 41505331Samw return (SA_SYNTAX_ERR); 41515331Samw } 41525331Samw rsrcname = optarg; 41535331Samw break; 41544653Sdougm case 's': 41555331Samw if (sharepath != NULL) { 41565331Samw (void) printf(gettext( 41575331Samw "Adding multiple shares not supported\n")); 41585331Samw return (SA_SYNTAX_ERR); 41595331Samw } 41604653Sdougm sharepath = optarg; 41614653Sdougm break; 41624653Sdougm case 'S': 41635331Samw if (optset != NULL) { 41645331Samw (void) printf(gettext( 41655331Samw "Specifying multiple property " 41665331Samw "spaces not supported: %s\n"), optset); 41675331Samw return (SA_SYNTAX_ERR); 41685331Samw } 41694653Sdougm optset = optarg; 41704653Sdougm break; 41714653Sdougm default: 41724653Sdougm case 'h': 41734653Sdougm case '?': 41744653Sdougm (void) printf(gettext("usage: %s\n"), 41754653Sdougm sa_get_usage(USAGE_UNSET)); 41764653Sdougm return (SA_OK); 41773034Sdougm } 41783034Sdougm } 41793034Sdougm 41803034Sdougm if (optlist != NULL) 41814653Sdougm ret = chk_opt(optlist, optset != NULL, protocol); 41823034Sdougm 41833034Sdougm if (optind >= argc || (optlist == NULL && optset == NULL) || 41843034Sdougm protocol == NULL) { 41854653Sdougm char *sep = "\t"; 41864653Sdougm (void) printf(gettext("usage: %s\n"), 41874653Sdougm sa_get_usage(USAGE_UNSET)); 41884653Sdougm if (optind >= argc) { 41894653Sdougm (void) printf(gettext("%sgroup must be specified"), 41904653Sdougm sep); 41914653Sdougm sep = ", "; 41924653Sdougm } 41934653Sdougm if (optlist == NULL) { 41944653Sdougm (void) printf(gettext("%sat least one property must " 41954653Sdougm "be specified"), sep); 41964653Sdougm sep = ", "; 41974653Sdougm } 41984653Sdougm if (protocol == NULL) { 41994653Sdougm (void) printf(gettext("%sprotocol must be specified"), 42004653Sdougm sep); 42014653Sdougm sep = ", "; 42024653Sdougm } 42034653Sdougm (void) printf("\n"); 42044653Sdougm ret = SA_SYNTAX_ERR; 42053034Sdougm } else { 42063034Sdougm 42073034Sdougm /* 42084653Sdougm * If a group already exists, we can only add a new 42093034Sdougm * protocol to it and not create a new one or add the 42103034Sdougm * same protocol again. 42113034Sdougm */ 42123034Sdougm 42134653Sdougm groupname = argv[optind]; 42144653Sdougm auth = check_authorizations(groupname, flags); 42154653Sdougm if (optset == NULL) 42164653Sdougm ret = basic_unset(handle, groupname, optlist, protocol, 42175331Samw sharepath, rsrcname, dryrun); 42184653Sdougm else 42194653Sdougm ret = space_unset(handle, groupname, optlist, protocol, 42204653Sdougm sharepath, dryrun, optset); 42214653Sdougm 42224653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) 42234653Sdougm (void) printf(gettext("Command would fail: %s\n"), 42244653Sdougm sa_errorstr(SA_NO_PERMISSION)); 42253034Sdougm } 42263034Sdougm return (ret); 42273034Sdougm } 42283034Sdougm 42293034Sdougm /* 42303034Sdougm * sa_enable_group(flags, argc, argv) 42313034Sdougm * 42323034Sdougm * Implements the enable subcommand 42333034Sdougm */ 42343034Sdougm 42353034Sdougm int 42363910Sdougm sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[]) 42373034Sdougm { 42383034Sdougm int verbose = 0; 42393034Sdougm int dryrun = 0; 42403034Sdougm int all = 0; 42413034Sdougm int c; 42423034Sdougm int ret = SA_OK; 42433034Sdougm char *protocol = NULL; 42443034Sdougm char *state; 42453034Sdougm struct list *worklist = NULL; 42463034Sdougm int auth = 1; 42474653Sdougm sa_group_t group; 42483034Sdougm 42493034Sdougm while ((c = getopt(argc, argv, "?havnP:")) != EOF) { 42504653Sdougm switch (c) { 42514653Sdougm case 'a': 42524653Sdougm all = 1; 42534653Sdougm break; 42544653Sdougm case 'n': 42554653Sdougm dryrun++; 42564653Sdougm break; 42574653Sdougm case 'P': 42585331Samw if (protocol != NULL) { 42595331Samw (void) printf(gettext( 42605331Samw "Specifying multiple protocols " 42615331Samw "not supported: %s\n"), protocol); 42625331Samw return (SA_SYNTAX_ERR); 42635331Samw } 42644653Sdougm protocol = optarg; 42654653Sdougm if (!sa_valid_protocol(protocol)) { 42664653Sdougm (void) printf(gettext( 42674653Sdougm "Invalid protocol specified: %s\n"), 42683034Sdougm protocol); 42694653Sdougm return (SA_INVALID_PROTOCOL); 42704653Sdougm } 42714653Sdougm break; 42724653Sdougm case 'v': 42734653Sdougm verbose++; 42744653Sdougm break; 42754653Sdougm default: 42764653Sdougm case 'h': 42774653Sdougm case '?': 42784653Sdougm (void) printf(gettext("usage: %s\n"), 42794653Sdougm sa_get_usage(USAGE_ENABLE)); 42804653Sdougm return (0); 42813034Sdougm } 42823034Sdougm } 42833034Sdougm 42843034Sdougm if (optind == argc && !all) { 42854653Sdougm (void) printf(gettext("usage: %s\n"), 42864653Sdougm sa_get_usage(USAGE_ENABLE)); 42874653Sdougm (void) printf(gettext("\tmust specify group\n")); 42884653Sdougm return (SA_NO_SUCH_PATH); 42894653Sdougm } 42904653Sdougm if (!all) { 42913034Sdougm while (optind < argc) { 42924653Sdougm group = sa_get_group(handle, argv[optind]); 42934653Sdougm if (group != NULL) { 42944653Sdougm auth &= check_authorizations(argv[optind], 42954653Sdougm flags); 42964653Sdougm state = sa_get_group_attr(group, "state"); 42974653Sdougm if (state != NULL && 42984653Sdougm strcmp(state, "enabled") == 0) { 42994653Sdougm /* already enabled */ 43004653Sdougm if (verbose) 43014653Sdougm (void) printf(gettext( 43024653Sdougm "Group \"%s\" is already " 43034653Sdougm "enabled\n"), 43044653Sdougm argv[optind]); 43054653Sdougm ret = SA_BUSY; /* already enabled */ 43064653Sdougm } else { 43074653Sdougm worklist = add_list(worklist, group, 43085331Samw 0, protocol); 43094653Sdougm if (verbose) 43104653Sdougm (void) printf(gettext( 43114653Sdougm "Enabling group \"%s\"\n"), 43124653Sdougm argv[optind]); 43134653Sdougm } 43144653Sdougm if (state != NULL) 43154653Sdougm sa_free_attr_string(state); 43163034Sdougm } else { 43174653Sdougm ret = SA_NO_SUCH_GROUP; 43183034Sdougm } 43194653Sdougm optind++; 43203034Sdougm } 43214653Sdougm } else { 43224653Sdougm for (group = sa_get_group(handle, NULL); 43234653Sdougm group != NULL; 43243034Sdougm group = sa_get_next_group(group)) { 43255331Samw worklist = add_list(worklist, group, 0, protocol); 43263034Sdougm } 43274653Sdougm } 43284653Sdougm if (!dryrun && ret == SA_OK) 43295331Samw ret = enable_all_groups(handle, worklist, 1, 0, NULL, B_FALSE); 43304653Sdougm 43314653Sdougm if (ret != SA_OK && ret != SA_BUSY) 43323034Sdougm (void) printf(gettext("Could not enable group: %s\n"), 43334653Sdougm sa_errorstr(ret)); 43344653Sdougm if (ret == SA_BUSY) 43353034Sdougm ret = SA_OK; 43364653Sdougm 43373034Sdougm if (worklist != NULL) 43384653Sdougm free_list(worklist); 43393034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 43404653Sdougm (void) printf(gettext("Command would fail: %s\n"), 43414653Sdougm sa_errorstr(SA_NO_PERMISSION)); 43423034Sdougm } 43433034Sdougm return (ret); 43443034Sdougm } 43453034Sdougm 43463034Sdougm /* 43475331Samw * disable_group(group, proto) 43483034Sdougm * 43495331Samw * Disable all the shares in the specified group.. This is a helper 43505331Samw * for disable_all_groups in order to simplify regular and subgroup 43515331Samw * (zfs) disabling. Group has already been checked for non-NULL. 43523034Sdougm */ 43533034Sdougm 43543034Sdougm static int 43555331Samw disable_group(sa_group_t group, char *proto) 43563034Sdougm { 43573034Sdougm sa_share_t share; 43583034Sdougm int ret = SA_OK; 43593034Sdougm 43605331Samw /* 43615331Samw * If the protocol isn't enabled, skip it and treat as 43625331Samw * successful. 43635331Samw */ 43645331Samw if (!has_protocol(group, proto)) 43655331Samw return (ret); 43665331Samw 43673034Sdougm for (share = sa_get_share(group, NULL); 43683034Sdougm share != NULL && ret == SA_OK; 43693034Sdougm share = sa_get_next_share(share)) { 43705331Samw ret = sa_disable_share(share, proto); 43714653Sdougm if (ret == SA_NO_SUCH_PATH) { 43724653Sdougm /* 43734653Sdougm * this is OK since the path is gone. we can't 43744653Sdougm * re-share it anyway so no error. 43754653Sdougm */ 43764653Sdougm ret = SA_OK; 43774653Sdougm } 43783034Sdougm } 43793034Sdougm return (ret); 43803034Sdougm } 43813034Sdougm 43823034Sdougm /* 43833034Sdougm * disable_all_groups(work, setstate) 43843034Sdougm * 43853034Sdougm * helper function that disables the shares in the list of groups 43863034Sdougm * provided. It optionally marks the group as disabled. Used by both 43873034Sdougm * enable and start subcommands. 43883034Sdougm */ 43893034Sdougm 43903034Sdougm static int 43913910Sdougm disable_all_groups(sa_handle_t handle, struct list *work, int setstate) 43923034Sdougm { 43933034Sdougm int ret = SA_OK; 43943034Sdougm sa_group_t subgroup, group; 43953034Sdougm 43963034Sdougm while (work != NULL && ret == SA_OK) { 43974653Sdougm group = (sa_group_t)work->item; 43984653Sdougm if (setstate) 43994653Sdougm ret = sa_set_group_attr(group, "state", "disabled"); 44004653Sdougm if (ret == SA_OK) { 44014653Sdougm char *name; 44024653Sdougm name = sa_get_group_attr(group, "name"); 44034653Sdougm if (name != NULL && strcmp(name, "zfs") == 0) { 44044653Sdougm /* need to get the sub-groups for stopping */ 44054653Sdougm for (subgroup = sa_get_sub_group(group); 44064653Sdougm subgroup != NULL; 44074653Sdougm subgroup = sa_get_next_group(subgroup)) { 44085331Samw ret = disable_group(subgroup, 44095331Samw work->proto); 44104653Sdougm } 44114653Sdougm } else { 44125331Samw ret = disable_group(group, work->proto); 44134653Sdougm } 44144653Sdougm /* 44154653Sdougm * We don't want to "disable" since it won't come 44164653Sdougm * up after a reboot. The SMF framework should do 44174653Sdougm * the right thing. On enable we do want to do 44184653Sdougm * something. 44194653Sdougm */ 44203034Sdougm } 44214653Sdougm work = work->next; 44223034Sdougm } 44233034Sdougm if (ret == SA_OK) 44244653Sdougm ret = sa_update_config(handle); 44253034Sdougm return (ret); 44263034Sdougm } 44273034Sdougm 44283034Sdougm /* 44293034Sdougm * sa_disable_group(flags, argc, argv) 44303034Sdougm * 44313034Sdougm * Implements the disable subcommand 44323034Sdougm */ 44333034Sdougm 44343034Sdougm int 44353910Sdougm sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[]) 44363034Sdougm { 44373034Sdougm int verbose = 0; 44383034Sdougm int dryrun = 0; 44393034Sdougm int all = 0; 44403034Sdougm int c; 44413034Sdougm int ret = SA_OK; 44425331Samw char *protocol = NULL; 44433034Sdougm char *state; 44443034Sdougm struct list *worklist = NULL; 44454653Sdougm sa_group_t group; 44463034Sdougm int auth = 1; 44473034Sdougm 44483034Sdougm while ((c = getopt(argc, argv, "?havn")) != EOF) { 44494653Sdougm switch (c) { 44504653Sdougm case 'a': 44514653Sdougm all = 1; 44524653Sdougm break; 44534653Sdougm case 'n': 44544653Sdougm dryrun++; 44554653Sdougm break; 44564653Sdougm case 'P': 44575331Samw if (protocol != NULL) { 44585331Samw (void) printf(gettext( 44595331Samw "Specifying multiple protocols " 44605331Samw "not supported: %s\n"), protocol); 44615331Samw return (SA_SYNTAX_ERR); 44625331Samw } 44634653Sdougm protocol = optarg; 44644653Sdougm if (!sa_valid_protocol(protocol)) { 44654653Sdougm (void) printf(gettext( 44664653Sdougm "Invalid protocol specified: %s\n"), 44674653Sdougm protocol); 44684653Sdougm return (SA_INVALID_PROTOCOL); 44694653Sdougm } 44704653Sdougm break; 44714653Sdougm case 'v': 44724653Sdougm verbose++; 44734653Sdougm break; 44744653Sdougm default: 44754653Sdougm case 'h': 44764653Sdougm case '?': 44774653Sdougm (void) printf(gettext("usage: %s\n"), 44784653Sdougm sa_get_usage(USAGE_DISABLE)); 44794653Sdougm return (0); 44803034Sdougm } 44813034Sdougm } 44823034Sdougm 44833034Sdougm if (optind == argc && !all) { 44843034Sdougm (void) printf(gettext("usage: %s\n"), 44854653Sdougm sa_get_usage(USAGE_DISABLE)); 44863034Sdougm (void) printf(gettext("\tmust specify group\n")); 44874653Sdougm return (SA_NO_SUCH_PATH); 44884653Sdougm } 44894653Sdougm if (!all) { 44904653Sdougm while (optind < argc) { 44913910Sdougm group = sa_get_group(handle, argv[optind]); 44923034Sdougm if (group != NULL) { 44934653Sdougm auth &= check_authorizations(argv[optind], 44944653Sdougm flags); 44954653Sdougm state = sa_get_group_attr(group, "state"); 44964653Sdougm if (state == NULL || 44974653Sdougm strcmp(state, "disabled") == 0) { 44984653Sdougm /* already disabled */ 44994653Sdougm if (verbose) 45004653Sdougm (void) printf(gettext( 45014653Sdougm "Group \"%s\" is " 45024653Sdougm "already disabled\n"), 45034653Sdougm argv[optind]); 45045331Samw ret = SA_BUSY; /* already disabled */ 45054653Sdougm } else { 45065331Samw worklist = add_list(worklist, group, 0, 45075331Samw protocol); 45084653Sdougm if (verbose) 45094653Sdougm (void) printf(gettext( 45104653Sdougm "Disabling group " 45114653Sdougm "\"%s\"\n"), argv[optind]); 45124653Sdougm } 45134653Sdougm if (state != NULL) 45144653Sdougm sa_free_attr_string(state); 45153034Sdougm } else { 45164653Sdougm ret = SA_NO_SUCH_GROUP; 45173034Sdougm } 45183034Sdougm optind++; 45194653Sdougm } 45204653Sdougm } else { 45214653Sdougm for (group = sa_get_group(handle, NULL); 45224653Sdougm group != NULL; 45234653Sdougm group = sa_get_next_group(group)) 45245331Samw worklist = add_list(worklist, group, 0, protocol); 45253034Sdougm } 45264653Sdougm 45274653Sdougm if (ret == SA_OK && !dryrun) 45284653Sdougm ret = disable_all_groups(handle, worklist, 1); 45294653Sdougm if (ret != SA_OK && ret != SA_BUSY) 45304653Sdougm (void) printf(gettext("Could not disable group: %s\n"), 45314653Sdougm sa_errorstr(ret)); 45324653Sdougm if (ret == SA_BUSY) 45334653Sdougm ret = SA_OK; 45343034Sdougm if (worklist != NULL) 45354653Sdougm free_list(worklist); 45364653Sdougm if (dryrun && ret == SA_OK && !auth && verbose) 45374653Sdougm (void) printf(gettext("Command would fail: %s\n"), 45384653Sdougm sa_errorstr(SA_NO_PERMISSION)); 45393034Sdougm return (ret); 45403034Sdougm } 45413034Sdougm 45423034Sdougm /* 45433034Sdougm * sa_start_group(flags, argc, argv) 45443034Sdougm * 45453034Sdougm * Implements the start command. 45463034Sdougm * This is similar to enable except it doesn't change the state 45473034Sdougm * of the group(s) and only enables shares if the group is already 45483034Sdougm * enabled. 45493034Sdougm */ 45505331Samw 45513034Sdougm int 45523910Sdougm sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[]) 45533034Sdougm { 45543034Sdougm int verbose = 0; 45553034Sdougm int all = 0; 45563034Sdougm int c; 45573034Sdougm int ret = SMF_EXIT_OK; 45583034Sdougm char *protocol = NULL; 45593034Sdougm char *state; 45603034Sdougm struct list *worklist = NULL; 45614653Sdougm sa_group_t group; 45625331Samw #ifdef lint 45635331Samw flags = flags; 45645331Samw #endif 45653034Sdougm 45663034Sdougm while ((c = getopt(argc, argv, "?havP:")) != EOF) { 45674653Sdougm switch (c) { 45684653Sdougm case 'a': 45694653Sdougm all = 1; 45704653Sdougm break; 45714653Sdougm case 'P': 45725331Samw if (protocol != NULL) { 45735331Samw (void) printf(gettext( 45745331Samw "Specifying multiple protocols " 45755331Samw "not supported: %s\n"), protocol); 45765331Samw return (SA_SYNTAX_ERR); 45775331Samw } 45784653Sdougm protocol = optarg; 45794653Sdougm if (!sa_valid_protocol(protocol)) { 45804653Sdougm (void) printf(gettext( 45814653Sdougm "Invalid protocol specified: %s\n"), 45823034Sdougm protocol); 45834653Sdougm return (SA_INVALID_PROTOCOL); 45844653Sdougm } 45854653Sdougm break; 45864653Sdougm case 'v': 45874653Sdougm verbose++; 45884653Sdougm break; 45894653Sdougm default: 45904653Sdougm case 'h': 45914653Sdougm case '?': 45924653Sdougm (void) printf(gettext("usage: %s\n"), 45934653Sdougm sa_get_usage(USAGE_START)); 45944653Sdougm return (SA_OK); 45953034Sdougm } 45963034Sdougm } 45973034Sdougm 45983034Sdougm if (optind == argc && !all) { 45993034Sdougm (void) printf(gettext("usage: %s\n"), 46004653Sdougm sa_get_usage(USAGE_START)); 46014653Sdougm return (SMF_EXIT_ERR_FATAL); 46024653Sdougm } 46034653Sdougm 46044653Sdougm if (!all) { 46054653Sdougm while (optind < argc) { 46063910Sdougm group = sa_get_group(handle, argv[optind]); 46073034Sdougm if (group != NULL) { 46084653Sdougm state = sa_get_group_attr(group, "state"); 46094653Sdougm if (state == NULL || 46104653Sdougm strcmp(state, "enabled") == 0) { 46115331Samw worklist = add_list(worklist, group, 0, 46125331Samw protocol); 46134653Sdougm if (verbose) 46144653Sdougm (void) printf(gettext( 46154653Sdougm "Starting group \"%s\"\n"), 46164653Sdougm argv[optind]); 46174653Sdougm } else { 46184653Sdougm /* 46194653Sdougm * Determine if there are any 46205331Samw * protocols. If there aren't any, 46214653Sdougm * then there isn't anything to do in 46224653Sdougm * any case so no error. 46234653Sdougm */ 46244653Sdougm if (sa_get_optionset(group, 46254653Sdougm protocol) != NULL) { 46264653Sdougm ret = SMF_EXIT_OK; 46274653Sdougm } 46283034Sdougm } 46294653Sdougm if (state != NULL) 46304653Sdougm sa_free_attr_string(state); 46313034Sdougm } 46323034Sdougm optind++; 46334653Sdougm } 46344653Sdougm } else { 46355331Samw for (group = sa_get_group(handle, NULL); 46365331Samw group != NULL; 46374653Sdougm group = sa_get_next_group(group)) { 46383034Sdougm state = sa_get_group_attr(group, "state"); 46393034Sdougm if (state == NULL || strcmp(state, "enabled") == 0) 46405331Samw worklist = add_list(worklist, group, 0, 46415331Samw protocol); 46423034Sdougm if (state != NULL) 46434653Sdougm sa_free_attr_string(state); 46443034Sdougm } 46453034Sdougm } 46464653Sdougm 46475331Samw (void) enable_all_groups(handle, worklist, 0, 1, protocol, B_FALSE); 46484653Sdougm 46493034Sdougm if (worklist != NULL) 46504653Sdougm free_list(worklist); 46513034Sdougm return (ret); 46523034Sdougm } 46533034Sdougm 46543034Sdougm /* 46553034Sdougm * sa_stop_group(flags, argc, argv) 46563034Sdougm * 46573034Sdougm * Implements the stop command. 46583034Sdougm * This is similar to disable except it doesn't change the state 46593034Sdougm * of the group(s) and only disables shares if the group is already 46603034Sdougm * enabled. 46613034Sdougm */ 46623034Sdougm int 46633910Sdougm sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[]) 46643034Sdougm { 46653034Sdougm int verbose = 0; 46663034Sdougm int all = 0; 46673034Sdougm int c; 46683034Sdougm int ret = SMF_EXIT_OK; 46693034Sdougm char *protocol = NULL; 46703034Sdougm char *state; 46713034Sdougm struct list *worklist = NULL; 46724653Sdougm sa_group_t group; 46735331Samw #ifdef lint 46745331Samw flags = flags; 46755331Samw #endif 46763034Sdougm 46773034Sdougm while ((c = getopt(argc, argv, "?havP:")) != EOF) { 46784653Sdougm switch (c) { 46794653Sdougm case 'a': 46804653Sdougm all = 1; 46814653Sdougm break; 46824653Sdougm case 'P': 46835331Samw if (protocol != NULL) { 46845331Samw (void) printf(gettext( 46855331Samw "Specifying multiple protocols " 46865331Samw "not supported: %s\n"), protocol); 46875331Samw return (SA_SYNTAX_ERR); 46885331Samw } 46894653Sdougm protocol = optarg; 46904653Sdougm if (!sa_valid_protocol(protocol)) { 46914653Sdougm (void) printf(gettext( 46924653Sdougm "Invalid protocol specified: %s\n"), 46934653Sdougm protocol); 46944653Sdougm return (SA_INVALID_PROTOCOL); 46954653Sdougm } 46964653Sdougm break; 46974653Sdougm case 'v': 46984653Sdougm verbose++; 46994653Sdougm break; 47004653Sdougm default: 47014653Sdougm case 'h': 47024653Sdougm case '?': 47034653Sdougm (void) printf(gettext("usage: %s\n"), 47044653Sdougm sa_get_usage(USAGE_STOP)); 47054653Sdougm return (0); 47063034Sdougm } 47073034Sdougm } 47083034Sdougm 47093034Sdougm if (optind == argc && !all) { 47104653Sdougm (void) printf(gettext("usage: %s\n"), 47114653Sdougm sa_get_usage(USAGE_STOP)); 47124653Sdougm return (SMF_EXIT_ERR_FATAL); 47134653Sdougm } else if (!all) { 47144653Sdougm while (optind < argc) { 47153910Sdougm group = sa_get_group(handle, argv[optind]); 47163034Sdougm if (group != NULL) { 47174653Sdougm state = sa_get_group_attr(group, "state"); 47184653Sdougm if (state == NULL || 47194653Sdougm strcmp(state, "enabled") == 0) { 47205331Samw worklist = add_list(worklist, group, 0, 47215331Samw protocol); 47224653Sdougm if (verbose) 47234653Sdougm (void) printf(gettext( 47244653Sdougm "Stopping group \"%s\"\n"), 47254653Sdougm argv[optind]); 47264653Sdougm } else { 47274653Sdougm ret = SMF_EXIT_OK; 47284653Sdougm } 47294653Sdougm if (state != NULL) 47304653Sdougm sa_free_attr_string(state); 47313034Sdougm } 47323034Sdougm optind++; 47334653Sdougm } 47344653Sdougm } else { 47355331Samw for (group = sa_get_group(handle, NULL); 47365331Samw group != NULL; 47374653Sdougm group = sa_get_next_group(group)) { 47383034Sdougm state = sa_get_group_attr(group, "state"); 47393034Sdougm if (state == NULL || strcmp(state, "enabled") == 0) 47405331Samw worklist = add_list(worklist, group, 0, 47415331Samw protocol); 47423034Sdougm if (state != NULL) 47434653Sdougm sa_free_attr_string(state); 47443034Sdougm } 47453034Sdougm } 47464653Sdougm (void) disable_all_groups(handle, worklist, 0); 47474653Sdougm ret = sa_update_config(handle); 47484653Sdougm 47493034Sdougm if (worklist != NULL) 47504653Sdougm free_list(worklist); 47513034Sdougm return (ret); 47523034Sdougm } 47533034Sdougm 47543034Sdougm /* 47553034Sdougm * remove_all_options(share, proto) 47563034Sdougm * 47573034Sdougm * Removes all options on a share. 47583034Sdougm */ 47593034Sdougm 47603034Sdougm static void 47613034Sdougm remove_all_options(sa_share_t share, char *proto) 47623034Sdougm { 47633034Sdougm sa_optionset_t optionset; 47643034Sdougm sa_security_t security; 47653034Sdougm sa_security_t prevsec = NULL; 47663034Sdougm 47673034Sdougm optionset = sa_get_optionset(share, proto); 47683034Sdougm if (optionset != NULL) 47694653Sdougm (void) sa_destroy_optionset(optionset); 47703034Sdougm for (security = sa_get_security(share, NULL, NULL); 47713034Sdougm security != NULL; 47723034Sdougm security = sa_get_next_security(security)) { 47734653Sdougm char *type; 47743034Sdougm /* 47754653Sdougm * We walk through the list. prevsec keeps the 47763034Sdougm * previous security so we can delete it without 47773034Sdougm * destroying the list. 47783034Sdougm */ 47794653Sdougm if (prevsec != NULL) { 47804653Sdougm /* remove the previously seen security */ 47814653Sdougm (void) sa_destroy_security(prevsec); 47824653Sdougm /* set to NULL so we don't try multiple times */ 47834653Sdougm prevsec = NULL; 47844653Sdougm } 47854653Sdougm type = sa_get_security_attr(security, "type"); 47864653Sdougm if (type != NULL) { 47874653Sdougm /* 47884653Sdougm * if the security matches the specified protocol, we 47894653Sdougm * want to remove it. prevsec holds it until either 47904653Sdougm * the next pass or we fall out of the loop. 47914653Sdougm */ 47924653Sdougm if (strcmp(type, proto) == 0) 47934653Sdougm prevsec = security; 47944653Sdougm sa_free_attr_string(type); 47954653Sdougm } 47963034Sdougm } 47973034Sdougm /* in case there is one left */ 47983034Sdougm if (prevsec != NULL) 47994653Sdougm (void) sa_destroy_security(prevsec); 48003034Sdougm } 48013034Sdougm 48023034Sdougm 48033034Sdougm /* 48043034Sdougm * for legacy support, we need to handle the old syntax. This is what 48053034Sdougm * we get if sharemgr is called with the name "share" rather than 48063034Sdougm * sharemgr. 48073034Sdougm */ 48083034Sdougm 48093034Sdougm static int 48103034Sdougm format_legacy_path(char *buff, int buffsize, char *proto, char *cmd) 48113034Sdougm { 48123034Sdougm int err; 48133034Sdougm 48143034Sdougm err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd); 48153034Sdougm if (err > buffsize) 48164653Sdougm return (-1); 48173034Sdougm return (0); 48183034Sdougm } 48193034Sdougm 48203034Sdougm 48213034Sdougm /* 48223034Sdougm * check_legacy_cmd(proto, cmd) 48233034Sdougm * 48243034Sdougm * Check to see if the cmd exists in /usr/lib/fs/<proto>/<cmd> and is 48253034Sdougm * executable. 48263034Sdougm */ 48273034Sdougm 48283034Sdougm static int 48293034Sdougm check_legacy_cmd(char *path) 48303034Sdougm { 48313034Sdougm struct stat st; 48323034Sdougm int ret = 0; 48333034Sdougm 48343034Sdougm if (stat(path, &st) == 0) { 48354653Sdougm if (S_ISREG(st.st_mode) && 48364653Sdougm st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) 48374653Sdougm ret = 1; 48383034Sdougm } 48393034Sdougm return (ret); 48403034Sdougm } 48413034Sdougm 48423034Sdougm /* 48433034Sdougm * run_legacy_command(proto, cmd, argv) 48443034Sdougm * 48454653Sdougm * We know the command exists, so attempt to execute it with all the 48463034Sdougm * arguments. This implements full legacy share support for those 48473034Sdougm * protocols that don't have plugin providers. 48483034Sdougm */ 48493034Sdougm 48503034Sdougm static int 48513034Sdougm run_legacy_command(char *path, char *argv[]) 48523034Sdougm { 48533034Sdougm int ret; 48543034Sdougm 48553034Sdougm ret = execv(path, argv); 48563034Sdougm if (ret < 0) { 48574653Sdougm switch (errno) { 48584653Sdougm case EACCES: 48594653Sdougm ret = SA_NO_PERMISSION; 48604653Sdougm break; 48614653Sdougm default: 48624653Sdougm ret = SA_SYSTEM_ERR; 48634653Sdougm break; 48644653Sdougm } 48653034Sdougm } 48663034Sdougm return (ret); 48673034Sdougm } 48683034Sdougm 48693034Sdougm /* 48703348Sdougm * out_share(out, group, proto) 48713034Sdougm * 48723034Sdougm * Display the share information in the format that the "share" 48733034Sdougm * command has traditionally used. 48743034Sdougm */ 48753034Sdougm 48763034Sdougm static void 48773348Sdougm out_share(FILE *out, sa_group_t group, char *proto) 48783034Sdougm { 48793034Sdougm sa_share_t share; 48803034Sdougm char resfmt[128]; 48815331Samw char *defprop; 48825331Samw 48835331Samw /* 48845331Samw * The original share command defaulted to displaying NFS 48855331Samw * shares or allowed a protocol to be specified. We want to 48865331Samw * skip those shares that are not the specified protocol. 48875331Samw */ 48885331Samw if (proto != NULL && sa_get_optionset(group, proto) == NULL) 48895331Samw return; 48905331Samw 48915331Samw if (proto == NULL) 48925331Samw proto = "nfs"; 48935331Samw 48945331Samw /* 48955331Samw * get the default property string. NFS uses "rw" but 48965331Samw * everything else will use "". 48975331Samw */ 48985331Samw if (proto != NULL && strcmp(proto, "nfs") != 0) 48995331Samw defprop = "\"\""; 49005331Samw else 49015331Samw defprop = "rw"; 49023034Sdougm 49034653Sdougm for (share = sa_get_share(group, NULL); 49044653Sdougm share != NULL; 49054653Sdougm share = sa_get_next_share(share)) { 49064653Sdougm char *path; 49074653Sdougm char *type; 49084653Sdougm char *resource; 49094653Sdougm char *description; 49104653Sdougm char *groupname; 49114653Sdougm char *sharedstate; 49124653Sdougm int shared = 1; 49134653Sdougm char *soptions; 49145331Samw char shareopts[MAXNAMLEN]; 49154653Sdougm 49164653Sdougm sharedstate = sa_get_share_attr(share, "shared"); 49174653Sdougm path = sa_get_share_attr(share, "path"); 49184653Sdougm type = sa_get_share_attr(share, "type"); 49195331Samw resource = get_resource(share); 49204653Sdougm groupname = sa_get_group_attr(group, "name"); 49214653Sdougm 49224653Sdougm if (groupname != NULL && strcmp(groupname, "default") == 0) { 49234653Sdougm sa_free_attr_string(groupname); 49244653Sdougm groupname = NULL; 49254653Sdougm } 49264653Sdougm description = sa_get_share_description(share); 49274653Sdougm 49285331Samw /* 49295331Samw * Want the sharetab version if it exists, defaulting 49305331Samw * to NFS if no protocol specified. 49315331Samw */ 49325331Samw (void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", proto); 49335331Samw soptions = sa_get_share_attr(share, shareopts); 49344653Sdougm 49354653Sdougm if (sharedstate == NULL) 49364653Sdougm shared = 0; 49374653Sdougm 49384653Sdougm if (soptions == NULL) 49394653Sdougm soptions = sa_proto_legacy_format(proto, share, 1); 49404653Sdougm 49414653Sdougm if (shared) { 49424653Sdougm /* only active shares go here */ 49434653Sdougm (void) snprintf(resfmt, sizeof (resfmt), "%s%s%s", 49444653Sdougm resource != NULL ? resource : "-", 49454653Sdougm groupname != NULL ? "@" : "", 49464653Sdougm groupname != NULL ? groupname : ""); 49474653Sdougm (void) fprintf(out, "%-14.14s %s %s \"%s\" \n", 49484653Sdougm resfmt, path, 49494653Sdougm (soptions != NULL && strlen(soptions) > 0) ? 49505331Samw soptions : defprop, 49514653Sdougm (description != NULL) ? description : ""); 49524653Sdougm } 49534653Sdougm 49544653Sdougm if (path != NULL) 49554653Sdougm sa_free_attr_string(path); 49564653Sdougm if (type != NULL) 49574653Sdougm sa_free_attr_string(type); 49584653Sdougm if (resource != NULL) 49594653Sdougm sa_free_attr_string(resource); 49604653Sdougm if (groupname != NULL) 49614653Sdougm sa_free_attr_string(groupname); 49624653Sdougm if (description != NULL) 49634653Sdougm sa_free_share_description(description); 49644653Sdougm if (sharedstate != NULL) 49654653Sdougm sa_free_attr_string(sharedstate); 49664653Sdougm if (soptions != NULL) 49674653Sdougm sa_format_free(soptions); 49683034Sdougm } 49693034Sdougm } 49703034Sdougm 49713034Sdougm /* 49723034Sdougm * output_legacy_file(out, proto) 49733034Sdougm * 49743034Sdougm * Walk all of the groups for the specified protocol and call 49753034Sdougm * out_share() to format and write in the format displayed by the 49763034Sdougm * "share" command with no arguments. 49773034Sdougm */ 49783034Sdougm 49793034Sdougm static void 49803910Sdougm output_legacy_file(FILE *out, char *proto, sa_handle_t handle) 49813034Sdougm { 49823034Sdougm sa_group_t group; 49833034Sdougm 49845331Samw for (group = sa_get_group(handle, NULL); 49855331Samw group != NULL; 49864653Sdougm group = sa_get_next_group(group)) { 49874653Sdougm char *zfs; 49883034Sdougm 49893034Sdougm /* 49905331Samw * Go through all the groups and ZFS 49915331Samw * sub-groups. out_share() will format the shares in 49925331Samw * the group appropriately. 49933034Sdougm */ 49943034Sdougm 49954653Sdougm zfs = sa_get_group_attr(group, "zfs"); 49964653Sdougm if (zfs != NULL) { 49974653Sdougm sa_group_t zgroup; 49984653Sdougm sa_free_attr_string(zfs); 49994653Sdougm for (zgroup = sa_get_sub_group(group); 50004653Sdougm zgroup != NULL; 50014653Sdougm zgroup = sa_get_next_group(zgroup)) { 50024653Sdougm 50034653Sdougm /* got a group, so display it */ 50044653Sdougm out_share(out, zgroup, proto); 50054653Sdougm } 50064653Sdougm } else { 50074653Sdougm out_share(out, group, proto); 50083034Sdougm } 50093034Sdougm } 50103034Sdougm } 50113034Sdougm 50123034Sdougm int 50133910Sdougm sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[]) 50143034Sdougm { 50153034Sdougm char *protocol = "nfs"; 50163034Sdougm char *options = NULL; 50173034Sdougm char *description = NULL; 50183034Sdougm char *groupname = NULL; 50193034Sdougm char *sharepath = NULL; 50203034Sdougm char *resource = NULL; 50213034Sdougm char *groupstatus = NULL; 50223034Sdougm int persist = SA_SHARE_TRANSIENT; 50233034Sdougm int argsused = 0; 50243034Sdougm int c; 50253034Sdougm int ret = SA_OK; 50263034Sdougm int zfs = 0; 50273034Sdougm int true_legacy = 0; 50283034Sdougm int curtype = SA_SHARE_TRANSIENT; 50293034Sdougm char cmd[MAXPATHLEN]; 50304653Sdougm sa_group_t group = NULL; 50315331Samw sa_resource_t rsrc = NULL; 50324653Sdougm sa_share_t share; 50334653Sdougm char dir[MAXPATHLEN]; 50345331Samw uint64_t features; 50355331Samw #ifdef lint 50365331Samw flags = flags; 50375331Samw #endif 50383034Sdougm 50393034Sdougm while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) { 50404653Sdougm switch (c) { 50414653Sdougm case 'd': 50424653Sdougm description = optarg; 50434653Sdougm argsused++; 50444653Sdougm break; 50454653Sdougm case 'F': 50464653Sdougm protocol = optarg; 50474653Sdougm if (!sa_valid_protocol(protocol)) { 50484653Sdougm if (format_legacy_path(cmd, MAXPATHLEN, 50494653Sdougm protocol, "share") == 0 && 50504653Sdougm check_legacy_cmd(cmd)) { 50514653Sdougm true_legacy++; 50524653Sdougm } else { 50534653Sdougm (void) fprintf(stderr, gettext( 50544653Sdougm "Invalid protocol specified: " 50554653Sdougm "%s\n"), protocol); 50564653Sdougm return (SA_INVALID_PROTOCOL); 50574653Sdougm } 50584653Sdougm } 50594653Sdougm break; 50604653Sdougm case 'o': 50614653Sdougm options = optarg; 50624653Sdougm argsused++; 50634653Sdougm break; 50644653Sdougm case 'p': 50654653Sdougm persist = SA_SHARE_PERMANENT; 50664653Sdougm argsused++; 50674653Sdougm break; 50684653Sdougm case 'h': 50694653Sdougm case '?': 50704653Sdougm default: 50714653Sdougm (void) fprintf(stderr, gettext("usage: %s\n"), 50724653Sdougm sa_get_usage(USAGE_SHARE)); 50734653Sdougm return (SA_OK); 50743034Sdougm } 50754653Sdougm } 50764653Sdougm 50774653Sdougm /* Have the info so construct what is needed */ 50784653Sdougm if (!argsused && optind == argc) { 50794653Sdougm /* display current info in share format */ 50805331Samw (void) output_legacy_file(stdout, protocol, handle); 50814653Sdougm return (ret); 50823034Sdougm } 50833034Sdougm 50844653Sdougm /* We are modifying the configuration */ 50854653Sdougm if (optind == argc) { 50863034Sdougm (void) fprintf(stderr, gettext("usage: %s\n"), 50874653Sdougm sa_get_usage(USAGE_SHARE)); 50883034Sdougm return (SA_LEGACY_ERR); 50894653Sdougm } 50904653Sdougm if (true_legacy) { 50914653Sdougm /* If still using legacy share/unshare, exec it */ 50923034Sdougm ret = run_legacy_command(cmd, argv); 50933034Sdougm return (ret); 50944653Sdougm } 50954653Sdougm 50964653Sdougm sharepath = argv[optind++]; 50974653Sdougm if (optind < argc) { 50983034Sdougm resource = argv[optind]; 50993034Sdougm groupname = strchr(resource, '@'); 51003034Sdougm if (groupname != NULL) 51014653Sdougm *groupname++ = '\0'; 51024653Sdougm } 51034653Sdougm if (realpath(sharepath, dir) == NULL) 51043034Sdougm ret = SA_BAD_PATH; 51054653Sdougm else 51063034Sdougm sharepath = dir; 51074653Sdougm if (ret == SA_OK) 51083910Sdougm share = sa_find_share(handle, sharepath); 51094653Sdougm else 51103034Sdougm share = NULL; 51114653Sdougm 51125331Samw features = sa_proto_get_featureset(protocol); 51135331Samw 51144653Sdougm if (groupname != NULL) { 51154653Sdougm ret = SA_NOT_ALLOWED; 51164653Sdougm } else if (ret == SA_OK) { 51175331Samw char *legacygroup; 51183034Sdougm /* 51194653Sdougm * The legacy group is always present and zfs groups 51203034Sdougm * come and go. zfs shares may be in sub-groups and 51213034Sdougm * the zfs share will already be in that group so it 51225331Samw * isn't an error. If the protocol is "smb", the group 51235331Samw * "smb" is used when "default" would otherwise be 51245331Samw * used. "default" is NFS only and "smb" is SMB only. 51253034Sdougm */ 51265331Samw if (strcmp(protocol, "smb") == 0) 51275331Samw legacygroup = "smb"; 51285331Samw else 51295331Samw legacygroup = "default"; 51305331Samw 51313034Sdougm /* 51324653Sdougm * If the share exists (not NULL), then make sure it 51334653Sdougm * is one we want to handle by getting the parent 51344653Sdougm * group. 51353034Sdougm */ 51365331Samw if (share != NULL) { 51374653Sdougm group = sa_get_parent_group(share); 51385331Samw } else { 51394653Sdougm group = sa_get_group(handle, legacygroup); 51405331Samw if (group == NULL && strcmp(legacygroup, "smb") == 0) { 51415331Samw /* 51425331Samw * This group may not exist, so create 51435331Samw * as necessary. It only contains the 51445331Samw * "smb" protocol. 51455331Samw */ 51465331Samw group = sa_create_group(handle, legacygroup, 51475331Samw &ret); 51485331Samw if (group != NULL) 51495331Samw (void) sa_create_optionset(group, 51505331Samw protocol); 51515331Samw } 51525331Samw } 51535331Samw 51545331Samw if (group == NULL) { 51555331Samw ret = SA_SYSTEM_ERR; 51565331Samw goto err; 51575331Samw } 51585331Samw 51595331Samw groupstatus = group_status(group); 51605331Samw if (share == NULL) { 51615331Samw share = sa_add_share(group, sharepath, 51625331Samw persist, &ret); 51635331Samw if (share == NULL && 51645331Samw ret == SA_DUPLICATE_NAME) { 51655331Samw /* 51665331Samw * Could be a ZFS path being started 51675331Samw */ 51685331Samw if (sa_zfs_is_shared(handle, 51695331Samw sharepath)) { 51705331Samw ret = SA_OK; 51715331Samw group = sa_get_group(handle, 51725331Samw "zfs"); 51735331Samw if (group == NULL) { 51745331Samw /* 51755331Samw * This shouldn't 51765331Samw * happen. 51775331Samw */ 51785331Samw ret = SA_CONFIG_ERR; 51795331Samw } else { 51805331Samw share = sa_add_share( 51815331Samw group, sharepath, 51825331Samw persist, &ret); 51834653Sdougm } 51843034Sdougm } 51855331Samw } 51865331Samw } else { 51875331Samw char *type; 51885331Samw /* 51895331Samw * May want to change persist state, but the 51905331Samw * important thing is to change options. We 51915331Samw * need to change them regardless of the 51925331Samw * source. 51935331Samw */ 51945331Samw 51955331Samw if (sa_zfs_is_shared(handle, sharepath)) { 51965331Samw zfs = 1; 51975331Samw } 51985331Samw remove_all_options(share, protocol); 51995331Samw type = sa_get_share_attr(share, "type"); 52005331Samw if (type != NULL && 52015331Samw strcmp(type, "transient") != 0) { 52025331Samw curtype = SA_SHARE_PERMANENT; 52035331Samw } 52045331Samw if (type != NULL) 52055331Samw sa_free_attr_string(type); 52065331Samw if (curtype != persist) { 52075331Samw (void) sa_set_share_attr(share, "type", 52085331Samw persist == SA_SHARE_PERMANENT ? 52095331Samw "persist" : "transient"); 52105331Samw } 52115331Samw } 52125331Samw 52135331Samw /* 52145331Samw * If there is a resource name, we may 52155331Samw * actually care about it if this is share for 52165331Samw * a protocol that uses resource level sharing 52175331Samw * (SMB). We need to find the resource and, if 52185331Samw * it exists, make sure it belongs to the 52195331Samw * current share. If it doesn't exist, attempt 52205331Samw * to create it. 52215331Samw */ 52225331Samw 52235331Samw if (ret == SA_OK && resource != NULL) { 52245331Samw rsrc = sa_find_resource(handle, resource); 52255331Samw if (rsrc != NULL) { 52265331Samw if (share != sa_get_resource_parent(rsrc)) 52275331Samw ret = SA_DUPLICATE_NAME; 52285331Samw } else { 52295331Samw rsrc = sa_add_resource(share, resource, 52305331Samw persist, &ret); 52313034Sdougm } 52325331Samw if (features & SA_FEATURE_RESOURCE) 52335331Samw share = rsrc; 52343108Sdougm } 52355331Samw 52364653Sdougm /* Have a group to hold this share path */ 52374653Sdougm if (ret == SA_OK && options != NULL && 52384653Sdougm strlen(options) > 0) { 52394653Sdougm ret = sa_parse_legacy_options(share, 52404653Sdougm options, 52414653Sdougm protocol); 52423034Sdougm } 52434653Sdougm if (!zfs) { 52444653Sdougm /* 52455331Samw * ZFS shares never have a description 52465331Samw * and we can't store the values so 52475331Samw * don't try. 52484653Sdougm */ 52494653Sdougm if (ret == SA_OK && description != NULL) 52504653Sdougm ret = sa_set_share_description(share, 52514653Sdougm description); 52523034Sdougm } 52535331Samw if (ret == SA_OK && 52545331Samw strcmp(groupstatus, "enabled") == 0) { 52555331Samw if (rsrc != share) 52564653Sdougm ret = sa_enable_share(share, protocol); 52575331Samw else 52585331Samw ret = sa_enable_resource(rsrc, 52595331Samw protocol); 52604653Sdougm if (ret == SA_OK && 52614653Sdougm persist == SA_SHARE_PERMANENT) { 52624653Sdougm (void) sa_update_legacy(share, 52634653Sdougm protocol); 52644653Sdougm } 52654653Sdougm if (ret == SA_OK) 52664653Sdougm ret = sa_update_config(handle); 52674653Sdougm } 52683034Sdougm } 52695331Samw err: 52703034Sdougm if (ret != SA_OK) { 52714653Sdougm (void) fprintf(stderr, gettext("Could not share: %s: %s\n"), 52724653Sdougm sharepath, sa_errorstr(ret)); 52734653Sdougm ret = SA_LEGACY_ERR; 52743034Sdougm } 52753034Sdougm return (ret); 52763034Sdougm } 52773034Sdougm 52783034Sdougm /* 52793034Sdougm * sa_legacy_unshare(flags, argc, argv) 52803034Sdougm * 52813034Sdougm * Implements the original unshare command. 52823034Sdougm */ 52833034Sdougm int 52843910Sdougm sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[]) 52853034Sdougm { 52863034Sdougm char *protocol = "nfs"; /* for now */ 52873034Sdougm char *options = NULL; 52883034Sdougm char *sharepath = NULL; 52893034Sdougm int persist = SA_SHARE_TRANSIENT; 52903034Sdougm int argsused = 0; 52913034Sdougm int c; 52923034Sdougm int ret = SA_OK; 52933034Sdougm int true_legacy = 0; 52945331Samw uint64_t features = 0; 52955331Samw sa_resource_t resource = NULL; 52963034Sdougm char cmd[MAXPATHLEN]; 52975331Samw #ifdef lint 52985331Samw flags = flags; 52995331Samw options = options; 53005331Samw #endif 53013034Sdougm 53023034Sdougm while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) { 53034653Sdougm switch (c) { 53044653Sdougm case 'h': 53054653Sdougm case '?': 53064653Sdougm break; 53074653Sdougm case 'F': 53084653Sdougm protocol = optarg; 53094653Sdougm if (!sa_valid_protocol(protocol)) { 53104653Sdougm if (format_legacy_path(cmd, MAXPATHLEN, 53114653Sdougm protocol, "unshare") == 0 && 53124653Sdougm check_legacy_cmd(cmd)) { 53134653Sdougm true_legacy++; 53144653Sdougm } else { 53154653Sdougm (void) printf(gettext( 53164653Sdougm "Invalid file system name\n")); 53174653Sdougm return (SA_INVALID_PROTOCOL); 53184653Sdougm } 53194653Sdougm } 53204653Sdougm break; 53214653Sdougm case 'o': 53224653Sdougm options = optarg; 53234653Sdougm argsused++; 53244653Sdougm break; 53254653Sdougm case 'p': 53264653Sdougm persist = SA_SHARE_PERMANENT; 53274653Sdougm argsused++; 53284653Sdougm break; 53294653Sdougm default: 53304653Sdougm (void) printf(gettext("usage: %s\n"), 53314653Sdougm sa_get_usage(USAGE_UNSHARE)); 53324653Sdougm return (SA_OK); 53333034Sdougm } 53343034Sdougm } 53353034Sdougm 53364653Sdougm /* Have the info so construct what is needed */ 53374653Sdougm if (optind == argc || (optind + 1) < argc || options != NULL) { 53384653Sdougm ret = SA_SYNTAX_ERR; 53393034Sdougm } else { 53404653Sdougm sa_share_t share; 53414653Sdougm char dir[MAXPATHLEN]; 53424653Sdougm if (true_legacy) { 53434653Sdougm /* if still using legacy share/unshare, exec it */ 53444653Sdougm ret = run_legacy_command(cmd, argv); 53454653Sdougm return (ret); 53464653Sdougm } 53473663Sdougm /* 53483663Sdougm * Find the path in the internal configuration. If it 53493663Sdougm * isn't found, attempt to resolve the path via 53503663Sdougm * realpath() and try again. 53513663Sdougm */ 53524653Sdougm sharepath = argv[optind++]; 53534653Sdougm share = sa_find_share(handle, sharepath); 53544653Sdougm if (share == NULL) { 53554653Sdougm if (realpath(sharepath, dir) == NULL) { 53564653Sdougm ret = SA_NO_SUCH_PATH; 53574653Sdougm } else { 53584653Sdougm share = sa_find_share(handle, dir); 53594653Sdougm } 53603663Sdougm } 53615331Samw if (share == NULL) { 53625331Samw /* Could be a resource name so check that next */ 53635331Samw features = sa_proto_get_featureset(protocol); 53645331Samw resource = sa_find_resource(handle, sharepath); 53655331Samw if (resource != NULL) { 53665331Samw share = sa_get_resource_parent(resource); 53675331Samw if (features & SA_FEATURE_RESOURCE) 53685331Samw (void) sa_disable_resource(resource, 53695331Samw protocol); 53705331Samw if (persist == SA_SHARE_PERMANENT) { 53715331Samw ret = sa_remove_resource(resource); 53725331Samw if (ret == SA_OK) 53735331Samw ret = sa_update_config(handle); 53745331Samw } 53755331Samw /* 53765331Samw * If we still have a resource on the 53775331Samw * share, we don't disable the share 53785331Samw * itself. IF there aren't anymore, we 53795331Samw * need to remove the share. The 53805331Samw * removal will be done in the next 53815331Samw * section if appropriate. 53825331Samw */ 53835331Samw resource = sa_get_share_resource(share, NULL); 53845331Samw if (resource != NULL) 53855331Samw share = NULL; 53865331Samw } else if (ret == SA_OK) { 53875331Samw /* Didn't find path and no resource */ 53885331Samw ret = SA_BAD_PATH; 53895331Samw } 53905331Samw } 53915331Samw if (share != NULL && resource == NULL) { 53924653Sdougm ret = sa_disable_share(share, protocol); 53934653Sdougm /* 53944653Sdougm * Errors are ok and removal should still occur. The 53954653Sdougm * legacy unshare is more forgiving of errors than the 53964653Sdougm * remove-share subcommand which may need the force 53974653Sdougm * flag set for some error conditions. That is, the 53984653Sdougm * "unshare" command will always unshare if it can 53994653Sdougm * while "remove-share" might require the force option. 54004653Sdougm */ 54014653Sdougm if (persist == SA_SHARE_PERMANENT) { 54024653Sdougm ret = sa_remove_share(share); 54034653Sdougm if (ret == SA_OK) 54044653Sdougm ret = sa_update_config(handle); 54054653Sdougm } 54065331Samw } else if (ret == SA_OK && share == NULL && resource == NULL) { 54075331Samw /* 54085331Samw * If both share and resource are NULL, then 54095331Samw * share not found. If one or the other was 54105331Samw * found or there was an earlier error, we 54115331Samw * assume it was handled earlier. 54125331Samw */ 54134653Sdougm ret = SA_NOT_SHARED; 54143663Sdougm } 54153034Sdougm } 54163034Sdougm switch (ret) { 54173034Sdougm default: 54184653Sdougm (void) printf("%s: %s\n", sharepath, sa_errorstr(ret)); 54194653Sdougm ret = SA_LEGACY_ERR; 54204653Sdougm break; 54213034Sdougm case SA_SYNTAX_ERR: 54224653Sdougm (void) printf(gettext("usage: %s\n"), 54234653Sdougm sa_get_usage(USAGE_UNSHARE)); 54244653Sdougm break; 54253034Sdougm case SA_OK: 54264653Sdougm break; 54273034Sdougm } 54283034Sdougm return (ret); 54293034Sdougm } 54303034Sdougm 54313034Sdougm /* 54324653Sdougm * Common commands that implement the sub-commands used by all 54335331Samw * protocols. The entries are found via the lookup command 54343034Sdougm */ 54353034Sdougm 54363034Sdougm static sa_command_t commands[] = { 54373034Sdougm {"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET}, 54383034Sdougm {"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION}, 54393034Sdougm {"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION}, 54403034Sdougm {"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION}, 54413034Sdougm {"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION}, 54423034Sdougm {"list", 0, sa_list, USAGE_LIST}, 54433034Sdougm {"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET}, 54443034Sdougm {"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET}, 54453034Sdougm {"set", 0, sa_set, USAGE_SET, SVC_SET}, 54463034Sdougm {"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET}, 54473034Sdougm {"show", 0, sa_show, USAGE_SHOW}, 54483034Sdougm {"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION}, 54493034Sdougm {"start", CMD_NODISPLAY, sa_start_group, USAGE_START, 54505331Samw SVC_SET|SVC_ACTION}, 54513034Sdougm {"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION}, 54523034Sdougm {"unset", 0, sa_unset, USAGE_UNSET, SVC_SET}, 54533034Sdougm {"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION}, 54543034Sdougm {NULL, 0, NULL, NULL} 54553034Sdougm }; 54563034Sdougm 54573034Sdougm static char * 54583034Sdougm sa_get_usage(sa_usage_t index) 54593034Sdougm { 54603034Sdougm char *ret = NULL; 54613034Sdougm switch (index) { 54623034Sdougm case USAGE_ADD_SHARE: 54634653Sdougm ret = gettext("add-share [-nth] [-r resource-name] " 54644653Sdougm "[-d \"description text\"] -s sharepath group"); 54654653Sdougm break; 54663034Sdougm case USAGE_CREATE: 54674653Sdougm ret = gettext( 54684653Sdougm "create [-nvh] [-P proto [-p property=value]] group"); 54694653Sdougm break; 54703034Sdougm case USAGE_DELETE: 54714653Sdougm ret = gettext("delete [-nvh] [-P proto] [-f] group"); 54724653Sdougm break; 54733034Sdougm case USAGE_DISABLE: 54744653Sdougm ret = gettext("disable [-nvh] {-a | group ...}"); 54754653Sdougm break; 54763034Sdougm case USAGE_ENABLE: 54774653Sdougm ret = gettext("enable [-nvh] {-a | group ...}"); 54784653Sdougm break; 54793034Sdougm case USAGE_LIST: 54804653Sdougm ret = gettext("list [-vh] [-P proto]"); 54814653Sdougm break; 54823034Sdougm case USAGE_MOVE_SHARE: 54834653Sdougm ret = gettext( 54844653Sdougm "move-share [-nvh] -s sharepath destination-group"); 54854653Sdougm break; 54863034Sdougm case USAGE_REMOVE_SHARE: 54875331Samw ret = gettext( 54885331Samw "remove-share [-fnvh] {-s sharepath | -r resource} " 54895331Samw "group"); 54904653Sdougm break; 54913034Sdougm case USAGE_SET: 54924653Sdougm ret = gettext("set [-nvh] -P proto [-S optspace] " 54935331Samw "[-p property=value]* [-s sharepath] [-r resource]] " 54945331Samw "group"); 54954653Sdougm break; 54963034Sdougm case USAGE_SET_SECURITY: 54974653Sdougm ret = gettext("set-security [-nvh] -P proto -S security-type " 54984653Sdougm "[-p property=value]* group"); 54994653Sdougm break; 55003034Sdougm case USAGE_SET_SHARE: 55014653Sdougm ret = gettext("set-share [-nh] [-r resource] " 55024653Sdougm "[-d \"description text\"] -s sharepath group"); 55034653Sdougm break; 55043034Sdougm case USAGE_SHOW: 55054653Sdougm ret = gettext("show [-pvxh] [-P proto] [group ...]"); 55064653Sdougm break; 55073034Sdougm case USAGE_SHARE: 55084653Sdougm ret = gettext("share [-F fstype] [-p] [-o optionlist]" 55094653Sdougm "[-d description] [pathname [resourcename]]"); 55104653Sdougm break; 55113034Sdougm case USAGE_START: 55124653Sdougm ret = gettext("start [-vh] [-P proto] {-a | group ...}"); 55134653Sdougm break; 55143034Sdougm case USAGE_STOP: 55154653Sdougm ret = gettext("stop [-vh] [-P proto] {-a | group ...}"); 55164653Sdougm break; 55173034Sdougm case USAGE_UNSET: 55184653Sdougm ret = gettext("unset [-nvh] -P proto [-S optspace] " 55194653Sdougm "[-p property]* group"); 55204653Sdougm break; 55213034Sdougm case USAGE_UNSET_SECURITY: 55225331Samw ret = gettext("unset-security [-nvh] -P proto " 55235331Samw "-S security-type [-p property]* group"); 55244653Sdougm break; 55253034Sdougm case USAGE_UNSHARE: 55264653Sdougm ret = gettext( 55275331Samw "unshare [-F fstype] [-p] [-o optionlist] sharepath"); 55284653Sdougm break; 55293034Sdougm } 55303034Sdougm return (ret); 55313034Sdougm } 55323034Sdougm 55333034Sdougm /* 55343034Sdougm * sa_lookup(cmd, proto) 55353034Sdougm * 55363034Sdougm * Lookup the sub-command. proto isn't currently used, but it may 55373034Sdougm * eventually provide a way to provide protocol specific sub-commands. 55383034Sdougm */ 55393034Sdougm sa_command_t * 55403034Sdougm sa_lookup(char *cmd, char *proto) 55413034Sdougm { 55423034Sdougm int i; 55433034Sdougm size_t len; 55445331Samw #ifdef lint 55455331Samw proto = proto; 55465331Samw #endif 55473034Sdougm 55483034Sdougm len = strlen(cmd); 55493034Sdougm for (i = 0; commands[i].cmdname != NULL; i++) { 55504653Sdougm if (strncmp(cmd, commands[i].cmdname, len) == 0) 55514653Sdougm return (&commands[i]); 55523034Sdougm } 55533034Sdougm return (NULL); 55543034Sdougm } 55553034Sdougm 55563034Sdougm void 55573034Sdougm sub_command_help(char *proto) 55583034Sdougm { 55593034Sdougm int i; 55605331Samw #ifdef lint 55615331Samw proto = proto; 55625331Samw #endif 55633034Sdougm 55643034Sdougm (void) printf(gettext("\tsub-commands:\n")); 55653034Sdougm for (i = 0; commands[i].cmdname != NULL; i++) { 55664653Sdougm if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY))) 55674653Sdougm (void) printf("\t%s\n", 55684653Sdougm sa_get_usage((sa_usage_t)commands[i].cmdidx)); 55693034Sdougm } 55703034Sdougm } 5571