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 /* 233348Sdougm * Copyright 2007 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> 513034Sdougm 523034Sdougm static char *sa_get_usage(sa_usage_t); 533034Sdougm 543034Sdougm /* 553034Sdougm * Implementation of the common sub-commands supported by sharemgr. 563034Sdougm * A number of helper functions are also included. 573034Sdougm */ 583034Sdougm 593034Sdougm /* 603034Sdougm * has_protocol(group, proto) 613034Sdougm * If the group has an optionset with the specified protocol, 623034Sdougm * return true (1) otherwise false (0). 633034Sdougm */ 643034Sdougm static int 653034Sdougm has_protocol(sa_group_t group, char *protocol) 663034Sdougm { 673034Sdougm sa_optionset_t optionset; 683034Sdougm int result = 0; 693034Sdougm 703034Sdougm optionset = sa_get_optionset(group, protocol); 713034Sdougm if (optionset != NULL) { 723034Sdougm result++; 733034Sdougm } 743034Sdougm return (result); 753034Sdougm } 763034Sdougm 773034Sdougm /* 783034Sdougm * add_list(list, item) 793034Sdougm * Adds a new list member that points to item to the list. 803034Sdougm * If list is NULL, it starts a new list. The function returns 813034Sdougm * the first member of the list. 823034Sdougm */ 833034Sdougm struct list * 843034Sdougm add_list(struct list *listp, void *item, void *data) 853034Sdougm { 863034Sdougm struct list *new, *tmp; 873034Sdougm 883034Sdougm new = malloc(sizeof (struct list)); 893034Sdougm if (new != NULL) { 903034Sdougm new->next = NULL; 913034Sdougm new->item = item; 923034Sdougm new->itemdata = data; 933034Sdougm } else { 943034Sdougm return (listp); 953034Sdougm } 963034Sdougm 973034Sdougm if (listp == NULL) 983034Sdougm return (new); 993034Sdougm 1003034Sdougm for (tmp = listp; tmp->next != NULL; tmp = tmp->next) { 1013034Sdougm /* get to end of list */ 1023034Sdougm } 1033034Sdougm tmp->next = new; 1043034Sdougm return (listp); 1053034Sdougm } 1063034Sdougm 1073034Sdougm /* 1083034Sdougm * free_list(list) 1093034Sdougm * Given a list, free all the members of the list; 1103034Sdougm */ 1113034Sdougm static void 1123034Sdougm free_list(struct list *listp) 1133034Sdougm { 1143034Sdougm struct list *tmp; 1153034Sdougm while (listp != NULL) { 1163034Sdougm tmp = listp; 1173034Sdougm listp = listp->next; 1183034Sdougm free(tmp); 1193034Sdougm } 1203034Sdougm } 1213034Sdougm 1223034Sdougm /* 1233034Sdougm * check_authorization(instname, which) 1243034Sdougm * 1253034Sdougm * Checks to see if the specific type of authorization in which is 1263034Sdougm * enabled for the user in this SMF service instance. 1273034Sdougm */ 1283034Sdougm 1293034Sdougm static int 1303034Sdougm check_authorization(char *instname, int which) 1313034Sdougm { 1323034Sdougm scf_handle_t *handle = NULL; 1333034Sdougm scf_simple_prop_t *prop = NULL; 1343034Sdougm char svcstring[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1]; 1353034Sdougm char *authstr = NULL; 1363034Sdougm ssize_t numauths; 1373034Sdougm int ret = 1; 1383034Sdougm uid_t uid; 1393034Sdougm struct passwd *pw = NULL; 1403034Sdougm 1413034Sdougm uid = getuid(); 1423034Sdougm pw = getpwuid(uid); 1433034Sdougm if (pw == NULL) 1443034Sdougm ret = 0; 1453034Sdougm 1463034Sdougm if (ret == 1) { 1473034Sdougm /* since names are restricted to SA_MAX_NAME_LEN won't overflow */ 1483034Sdougm (void) snprintf(svcstring, sizeof (svcstring), 1493034Sdougm "%s:%s", SA_SVC_FMRI_BASE, instname); 1503034Sdougm handle = scf_handle_create(SCF_VERSION); 1513034Sdougm if (handle != NULL) { 1523034Sdougm if (scf_handle_bind(handle) == 0) { 1533034Sdougm switch (which) { 1543034Sdougm case SVC_SET: 1553034Sdougm prop = scf_simple_prop_get(handle, svcstring, 1563034Sdougm "general", 1573034Sdougm SVC_AUTH_VALUE); 1583034Sdougm break; 1593034Sdougm case SVC_ACTION: 1603034Sdougm prop = scf_simple_prop_get(handle, svcstring, 1613034Sdougm "general", 1623034Sdougm SVC_AUTH_ACTION); 1633034Sdougm break; 1643034Sdougm } 1653034Sdougm } 1663034Sdougm } 1673034Sdougm } 1683034Sdougm /* make sure we have an authorization string property */ 1693034Sdougm if (prop != NULL) { 1703034Sdougm int i; 1713034Sdougm numauths = scf_simple_prop_numvalues(prop); 1723034Sdougm for (ret = 0, i = 0; i < numauths; i++) { 1733034Sdougm authstr = scf_simple_prop_next_astring(prop); 1743034Sdougm if (authstr != NULL) { 1753034Sdougm /* check if this user has one of the strings */ 1763034Sdougm if (chkauthattr(authstr, pw->pw_name)) { 1773034Sdougm ret = 1; 1783034Sdougm break; 1793034Sdougm } 1803034Sdougm } 1813034Sdougm } 1823034Sdougm endauthattr(); 1833034Sdougm scf_simple_prop_free(prop); 1843034Sdougm } else { 1853034Sdougm /* no authorization string defined */ 1863034Sdougm ret = 0; 1873034Sdougm } 1883034Sdougm if (handle != NULL) 1893034Sdougm scf_handle_destroy(handle); 1903034Sdougm return (ret); 1913034Sdougm } 1923034Sdougm 1933034Sdougm /* 1943034Sdougm * check_authorizations(instname, flags) 1953034Sdougm * 1963034Sdougm * check all the needed authorizations for the user in this service 1973034Sdougm * instance. Return value of 1(true) or 0(false) indicates whether 1983034Sdougm * there are authorizations for the user or not. 1993034Sdougm */ 2003034Sdougm 2013034Sdougm static int 2023034Sdougm check_authorizations(char *instname, int flags) 2033034Sdougm { 2043034Sdougm int ret1 = 0; 2053034Sdougm int ret2 = 0; 2063034Sdougm int ret; 2073034Sdougm 2083034Sdougm if (flags & SVC_SET) 2093034Sdougm ret1 = check_authorization(instname, SVC_SET); 2103034Sdougm if (flags & SVC_ACTION) 2113034Sdougm ret2 = check_authorization(instname, SVC_ACTION); 2123034Sdougm switch (flags) { 2133034Sdougm case SVC_ACTION: 2143034Sdougm ret = ret2; 2153034Sdougm break; 2163034Sdougm case SVC_SET: 2173034Sdougm ret = ret1; 2183034Sdougm break; 2193034Sdougm case SVC_ACTION|SVC_SET: 2203034Sdougm ret = ret1 & ret2; 2213034Sdougm break; 2223034Sdougm default: 2233034Sdougm /* if not flags set, we assume we don't need authorizations */ 2243034Sdougm ret = 1; 2253034Sdougm } 2263034Sdougm return (ret); 2273034Sdougm } 2283034Sdougm 2293034Sdougm /* 2303082Sdougm * enable_group(group, updateproto) 2313082Sdougm * 2323082Sdougm * enable all the shares in the specified group. This is a helper for 2333082Sdougm * enable_all_groups in order to simplify regular and subgroup (zfs) 2343082Sdougm * disabling. Group has already been checked for non-NULL. 2353082Sdougm */ 2363082Sdougm 2373082Sdougm static void 2383082Sdougm enable_group(sa_group_t group, char *updateproto) 2393082Sdougm { 2403082Sdougm sa_share_t share; 2413082Sdougm 2423082Sdougm for (share = sa_get_share(group, NULL); 2433082Sdougm share != NULL; 2443082Sdougm share = sa_get_next_share(share)) { 2453082Sdougm if (updateproto != NULL) 2463082Sdougm (void) sa_update_legacy(share, updateproto); 2473082Sdougm (void) sa_enable_share(share, NULL); 2483082Sdougm } 2493082Sdougm } 2503082Sdougm 2513082Sdougm /* 2524241Sdougm * isenabled(group) 2534241Sdougm * 2544241Sdougm * Returns B_TRUE if the group is enabled or B_FALSE if it isn't. 2554241Sdougm * Moved to separate function to reduce clutter in the code. 2564241Sdougm */ 2574241Sdougm 2584241Sdougm static int 2594241Sdougm isenabled(sa_group_t group) 2604241Sdougm { 2614241Sdougm char *state; 2624241Sdougm int ret = B_FALSE; 2634241Sdougm 2644241Sdougm if (group != NULL) { 2654241Sdougm state = sa_get_group_attr(group, "state"); 2664241Sdougm if (state != NULL) { 2674241Sdougm if (strcmp(state, "enabled") == 0) 2684241Sdougm ret = B_TRUE; 2694241Sdougm sa_free_attr_string(state); 2704241Sdougm } 2714241Sdougm } 2724241Sdougm return (ret); 2734241Sdougm } 2744241Sdougm 2754241Sdougm /* 2763082Sdougm * enable_all_groups(list, setstate, online, updateproto) 2773082Sdougm * Given a list of groups, enable each one found. If updateproto 2783082Sdougm * is not NULL, then update all the shares for the protocol that 2793082Sdougm * was passed in. 2803034Sdougm */ 2813034Sdougm static int 2823910Sdougm enable_all_groups(sa_handle_t handle, struct list *work, int setstate, 2833910Sdougm int online, char *updateproto) 2843034Sdougm { 2854241Sdougm int ret; 2863034Sdougm char instance[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1]; 2873034Sdougm char *state; 2883034Sdougm char *name; 2893034Sdougm char *zfs = NULL; 2903034Sdougm sa_group_t group; 2913082Sdougm sa_group_t subgroup; 2923034Sdougm 2934241Sdougm for (ret = SA_OK; work != NULL; work = work->next) { 2943034Sdougm group = (sa_group_t)work->item; 2954241Sdougm 2964241Sdougm /* 2974241Sdougm * If setstate == TRUE, then make sure to set 2984241Sdougm * enabled. This needs to be done here in order for 2994241Sdougm * the isenabled check to succeed on a newly enabled 3004241Sdougm * group. 3014241Sdougm */ 3024241Sdougm if (setstate == B_TRUE) { 3034241Sdougm ret = sa_set_group_attr(group, "state", "enabled"); 3044241Sdougm if (ret != SA_OK) 3054241Sdougm break; 3064241Sdougm } 3074241Sdougm 3084241Sdougm /* 3094241Sdougm * Check to see if group is enabled. If it isn't, skip 3104241Sdougm * the rest. We don't want shares starting if the 3114241Sdougm * group is disabled. The properties may have been 3124241Sdougm * updated, but there won't be a change until the 3134241Sdougm * group is enabled. 3144241Sdougm */ 3154241Sdougm if (!isenabled(group)) 3164241Sdougm continue; 3174241Sdougm 3183034Sdougm /* if itemdata != NULL then a single share */ 3193034Sdougm if (work->itemdata != NULL) { 3203034Sdougm ret = sa_enable_share((sa_share_t)work->itemdata, NULL); 3213034Sdougm } 3224241Sdougm if (ret != SA_OK) 3234241Sdougm break; 3244241Sdougm 3254241Sdougm /* if itemdata == NULL then the whole group */ 3264241Sdougm if (work->itemdata == NULL) { 3274241Sdougm zfs = sa_get_group_attr(group, "zfs"); 3284241Sdougm /* 3294241Sdougm * if the share is managed by ZFS, don't 3304241Sdougm * update any of the protocols since ZFS is 3314241Sdougm * handling this. updateproto will contain 3324241Sdougm * the name of the protocol that we want to 3334241Sdougm * update legacy files for. 3344241Sdougm */ 3354241Sdougm enable_group(group, zfs == NULL ? updateproto : NULL); 3364241Sdougm for (subgroup = sa_get_sub_group(group); subgroup != NULL; 3373082Sdougm subgroup = sa_get_next_group(subgroup)) { 3383082Sdougm /* never update legacy for ZFS subgroups */ 3393082Sdougm enable_group(subgroup, NULL); 3403034Sdougm } 3414241Sdougm } 3424241Sdougm if (online) { 3434241Sdougm zfs = sa_get_group_attr(group, "zfs"); 3444241Sdougm name = sa_get_group_attr(group, "name"); 3454241Sdougm if (name != NULL) { 3463034Sdougm if (zfs == NULL) { 3473034Sdougm (void) snprintf(instance, sizeof (instance), 3483034Sdougm "%s:%s", 3493034Sdougm SA_SVC_FMRI_BASE, name); 3503034Sdougm state = smf_get_state(instance); 3513034Sdougm if (state == NULL || 3523034Sdougm strcmp(state, "online") != 0) { 3533034Sdougm (void) smf_enable_instance(instance, 0); 3543034Sdougm free(state); 3553034Sdougm } 3563034Sdougm } else { 3573034Sdougm sa_free_attr_string(zfs); 3583034Sdougm zfs = NULL; 3593034Sdougm } 3603034Sdougm if (name != NULL) 3613034Sdougm sa_free_attr_string(name); 3623034Sdougm } 3633034Sdougm } 3643034Sdougm } 3653034Sdougm if (ret == SA_OK) { 3663910Sdougm ret = sa_update_config(handle); 3673034Sdougm } 3683034Sdougm return (ret); 3693034Sdougm } 3703034Sdougm 3713034Sdougm /* 3723034Sdougm * chk_opt(optlistp, security, proto) 3733034Sdougm * 3743034Sdougm * Do a sanity check on the optlist provided for the protocol. This 3753034Sdougm * is a syntax check and verification that the property is either a 3763034Sdougm * general or specific to a names optionset. 3773034Sdougm */ 3783034Sdougm 3793034Sdougm static int 3803034Sdougm chk_opt(struct options *optlistp, int security, char *proto) 3813034Sdougm { 3823034Sdougm struct options *optlist; 3833034Sdougm char *sep = ""; 3843034Sdougm int notfirst = 0; 3853034Sdougm int ret; 3863034Sdougm 3873034Sdougm for (optlist = optlistp; optlist != NULL; optlist = optlist->next) { 3883034Sdougm char *optname; 3893034Sdougm 3903034Sdougm optname = optlist->optname; 3913034Sdougm ret = OPT_ADD_OK; 3923034Sdougm /* extract property/value pair */ 3933034Sdougm if (sa_is_security(optname, proto)) { 3943034Sdougm if (!security) 3953034Sdougm ret = OPT_ADD_SECURITY; 3963034Sdougm } else { 3973034Sdougm if (security) 3983034Sdougm ret = OPT_ADD_PROPERTY; 3993034Sdougm } 4003034Sdougm if (ret != OPT_ADD_OK) { 4013034Sdougm if (notfirst == 0) 4023034Sdougm (void) printf(gettext("Property syntax error: ")); 4033034Sdougm switch (ret) { 4043034Sdougm case OPT_ADD_SYNTAX: 4053034Sdougm (void) printf(gettext("%ssyntax error: %s"), 4063034Sdougm sep, optname); 4073034Sdougm sep = ", "; 4083034Sdougm break; 4093034Sdougm case OPT_ADD_SECURITY: 4103034Sdougm (void) printf(gettext("%s%s requires -S"), 4113034Sdougm optname, sep); 4123034Sdougm sep = ", "; 4133034Sdougm break; 4143034Sdougm case OPT_ADD_PROPERTY: 4153034Sdougm (void) printf(gettext("%s%s not supported with -S"), 4163034Sdougm optname, sep); 4173034Sdougm sep = ", "; 4183034Sdougm break; 4193034Sdougm } 4203034Sdougm notfirst++; 4213034Sdougm } 4223034Sdougm } 4233034Sdougm if (notfirst) { 4243034Sdougm (void) printf("\n"); 4253034Sdougm ret = SA_SYNTAX_ERR; 4263034Sdougm } 4273034Sdougm return (ret); 4283034Sdougm } 4293034Sdougm 4303034Sdougm /* 4313034Sdougm * free_opt(optlist) 4323034Sdougm * Free the specified option list. 4333034Sdougm */ 4343034Sdougm static void 4353034Sdougm free_opt(struct options *optlist) 4363034Sdougm { 4373034Sdougm struct options *nextopt; 4383034Sdougm while (optlist != NULL) { 4393034Sdougm nextopt = optlist->next; 4403034Sdougm free(optlist); 4413034Sdougm optlist = nextopt; 4423034Sdougm } 4433034Sdougm } 4443034Sdougm 4453034Sdougm /* 4463034Sdougm * check property list for valid properties 4473034Sdougm * A null value is a remove which is always valid. 4483034Sdougm */ 4493034Sdougm static int 4503034Sdougm valid_options(struct options *optlist, char *proto, void *object, char *sec) 4513034Sdougm { 4523034Sdougm int ret = SA_OK; 4533034Sdougm struct options *cur; 4543034Sdougm sa_property_t prop; 4553034Sdougm sa_optionset_t parent = NULL; 4563034Sdougm 4573034Sdougm if (object != NULL) { 4583034Sdougm if (sec == NULL) 4593034Sdougm parent = sa_get_optionset(object, proto); 4603034Sdougm else 4613034Sdougm parent = sa_get_security(object, sec, proto); 4623034Sdougm } 4633034Sdougm 4643034Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 4653034Sdougm if (cur->optvalue != NULL) { 4663034Sdougm prop = sa_create_property(cur->optname, cur->optvalue); 4673034Sdougm if (prop == NULL) 4683034Sdougm ret = SA_NO_MEMORY; 4693034Sdougm if (ret != SA_OK || 4703034Sdougm (ret = sa_valid_property(parent, proto, prop)) != SA_OK) { 4713034Sdougm (void) printf(gettext("Could not add property %s: %s\n"), 4723034Sdougm cur->optname, 4733034Sdougm sa_errorstr(ret)); 4743034Sdougm } 4753034Sdougm (void) sa_remove_property(prop); 4763034Sdougm } 4773034Sdougm } 4783034Sdougm return (ret); 4793034Sdougm } 4803034Sdougm 4813034Sdougm /* 4823034Sdougm * add_optionset(group, optlist, protocol, *err) 4833034Sdougm * Add the options in optlist to an optionset and then add the optionset 4843034Sdougm * to the group. 4853034Sdougm * 4863034Sdougm * The return value indicates if there was a "change" while errors are 4873034Sdougm * returned via the *err parameters. 4883034Sdougm */ 4893034Sdougm static int 4903034Sdougm add_optionset(sa_group_t group, struct options *optlist, char *proto, int *err) 4913034Sdougm { 4923034Sdougm sa_optionset_t optionset; 4933034Sdougm int ret = SA_OK; 4943034Sdougm int result = 0; 4953034Sdougm 4963034Sdougm optionset = sa_get_optionset(group, proto); 4973034Sdougm if (optionset == NULL) { 4983034Sdougm optionset = sa_create_optionset(group, proto); 4993034Sdougm result = 1; /* adding a protocol is a change */ 5003034Sdougm } 5013034Sdougm if (optionset != NULL) { 5023034Sdougm while (optlist != NULL) { 5033034Sdougm sa_property_t prop; 5043034Sdougm prop = sa_get_property(optionset, optlist->optname); 5053034Sdougm if (prop == NULL) { 5063034Sdougm /* 5073034Sdougm * add the property, but only if it is 5083034Sdougm * a non-NULL or non-zero length value 5093034Sdougm */ 5103034Sdougm if (optlist->optvalue != NULL) { 5113034Sdougm prop = sa_create_property(optlist->optname, 5123034Sdougm optlist->optvalue); 5133034Sdougm if (prop != NULL) { 5143034Sdougm ret = sa_valid_property(optionset, proto, prop); 5153034Sdougm if (ret != SA_OK) { 5163034Sdougm (void) sa_remove_property(prop); 5173034Sdougm (void) printf(gettext("Could not add property " 5183034Sdougm "%s: %s\n"), 5193034Sdougm optlist->optname, 5203034Sdougm sa_errorstr(ret)); 5213034Sdougm } 5223034Sdougm } 5233034Sdougm if (ret == SA_OK) { 5243034Sdougm ret = sa_add_property(optionset, prop); 5253034Sdougm if (ret != SA_OK) { 5263034Sdougm (void) printf(gettext("Could not add property" 5273034Sdougm " %s: %s\n"), 5283034Sdougm optlist->optname, 5293034Sdougm sa_errorstr(ret)); 5303034Sdougm } else { 5313034Sdougm /* there was a change */ 5323034Sdougm result = 1; 5333034Sdougm } 5343034Sdougm } 5353034Sdougm } 5363034Sdougm } else { 5373034Sdougm ret = sa_update_property(prop, optlist->optvalue); 5383034Sdougm /* should check to see if value changed */ 5393034Sdougm if (ret != SA_OK) { 5403034Sdougm (void) printf(gettext("Could not update " 5413034Sdougm "property %s: %s\n"), 5423034Sdougm optlist->optname, 5433034Sdougm sa_errorstr(ret)); 5443034Sdougm } else { 5453034Sdougm result = 1; 5463034Sdougm } 5473034Sdougm } 5483034Sdougm optlist = optlist->next; 5493034Sdougm } 5503034Sdougm ret = sa_commit_properties(optionset, 0); 5513034Sdougm } 5523034Sdougm if (err != NULL) 5533034Sdougm *err = ret; 5543034Sdougm return (result); 5553034Sdougm } 5563034Sdougm 5573034Sdougm /* 5583034Sdougm * sa_create(flags, argc, argv) 5593034Sdougm * create a new group 5603034Sdougm * this may or may not have a protocol associated with it. 5613034Sdougm * No protocol means "all" protocols in this case. 5623034Sdougm */ 5633034Sdougm static int 5643910Sdougm sa_create(sa_handle_t handle, int flags, int argc, char *argv[]) 5653034Sdougm { 5663034Sdougm char *groupname; 5673034Sdougm 5683034Sdougm sa_group_t group; 5693034Sdougm int verbose = 0; 5703034Sdougm int dryrun = 0; 5713034Sdougm int c; 5723034Sdougm char *protocol = NULL; 5733034Sdougm int ret = SA_OK; 5743034Sdougm struct options *optlist = NULL; 5753034Sdougm int err = 0; 5763034Sdougm int auth; 5773034Sdougm 5783034Sdougm while ((c = getopt(argc, argv, "?hvnP:p:")) != EOF) { 5793034Sdougm switch (c) { 5803034Sdougm case 'v': 5813034Sdougm verbose++; 5823034Sdougm break; 5833034Sdougm case 'n': 5843034Sdougm dryrun++; 5853034Sdougm break; 5863034Sdougm case 'P': 5873034Sdougm protocol = optarg; 5883034Sdougm if (!sa_valid_protocol(protocol)) { 5893034Sdougm (void) printf(gettext("Invalid protocol specified: %s\n"), 5903034Sdougm protocol); 5913034Sdougm return (SA_INVALID_PROTOCOL); 5923034Sdougm } 5933034Sdougm break; 5943034Sdougm case 'p': 5953034Sdougm ret = add_opt(&optlist, optarg, 0); 5963034Sdougm switch (ret) { 5973034Sdougm case OPT_ADD_SYNTAX: 5983034Sdougm (void) printf(gettext("Property syntax error for " 5993034Sdougm "property: %s\n"), 6003034Sdougm optarg); 6013034Sdougm return (SA_SYNTAX_ERR); 6023034Sdougm case OPT_ADD_SECURITY: 6033034Sdougm (void) printf(gettext("Security properties need " 6043034Sdougm "to be set with set-security: %s\n"), 6053034Sdougm optarg); 6063034Sdougm return (SA_SYNTAX_ERR); 6073034Sdougm default: 6083034Sdougm break; 6093034Sdougm } 6103034Sdougm 6113034Sdougm break; 6123034Sdougm default: 6133034Sdougm case 'h': 6143034Sdougm case '?': 6153034Sdougm (void) printf(gettext("usage: %s\n"), 6163034Sdougm sa_get_usage(USAGE_CREATE)); 6173034Sdougm return (0); 6183034Sdougm } 6193034Sdougm } 6203034Sdougm 6213034Sdougm if (optind >= argc) { 6223034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_CREATE)); 6233034Sdougm (void) printf(gettext("\tgroup must be specified.\n")); 6243034Sdougm return (SA_BAD_PATH); 6253034Sdougm } 6263034Sdougm 6273034Sdougm if ((optind + 1) < argc) { 6283034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_CREATE)); 6293034Sdougm (void) printf(gettext("\textraneous group(s) at end\n")); 6303034Sdougm return (SA_SYNTAX_ERR); 6313034Sdougm } 6323034Sdougm 6333034Sdougm if (protocol == NULL && optlist != NULL) { 6343034Sdougm /* lookup default protocol */ 6353034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_CREATE)); 6363034Sdougm (void) printf(gettext("\tprotocol must be specified " 6373034Sdougm "with properties\n")); 6383034Sdougm return (SA_INVALID_PROTOCOL); 6393034Sdougm } 6403034Sdougm 6413034Sdougm if (optlist != NULL) 6423034Sdougm ret = chk_opt(optlist, 0, protocol); 6433034Sdougm if (ret == OPT_ADD_SECURITY) { 6443034Sdougm (void) printf(gettext("Security properties not " 6453034Sdougm "supported with create\n")); 6463034Sdougm return (SA_SYNTAX_ERR); 6473034Sdougm } 6483034Sdougm 6493034Sdougm /* 6503034Sdougm * if a group already exists, we can only add a new protocol 6513034Sdougm * to it and not create a new one or add the same protocol 6523034Sdougm * again. 6533034Sdougm */ 6543034Sdougm 6553034Sdougm groupname = argv[optind]; 6563034Sdougm 6573034Sdougm auth = check_authorizations(groupname, flags); 6583034Sdougm 6593910Sdougm group = sa_get_group(handle, groupname); 6603034Sdougm if (group != NULL) { 6613034Sdougm /* group exists so must be a protocol add */ 6623034Sdougm if (protocol != NULL) { 6633034Sdougm if (has_protocol(group, protocol)) { 6643034Sdougm (void) printf(gettext("Group \"%s\" already exists" 6653034Sdougm " with protocol %s\n"), 6663034Sdougm groupname, protocol); 6673034Sdougm ret = SA_DUPLICATE_NAME; 6683034Sdougm } 6693034Sdougm } else { 6703034Sdougm /* must add new protocol */ 6713034Sdougm (void) printf(gettext("Group already exists and no protocol" 6723034Sdougm " specified.\n")); 6733034Sdougm ret = SA_DUPLICATE_NAME; 6743034Sdougm } 6753034Sdougm } else { 6763034Sdougm /* 6773034Sdougm * is it a valid name? Must comply with SMF instance 6783034Sdougm * name restrictions. 6793034Sdougm */ 6803034Sdougm if (!sa_valid_group_name(groupname)) { 6813034Sdougm ret = SA_INVALID_NAME; 6823034Sdougm (void) printf(gettext("Invalid group name: %s\n"), groupname); 6833034Sdougm } 6843034Sdougm } 6853034Sdougm if (ret == SA_OK) { 6863034Sdougm /* check protocol vs optlist */ 6873034Sdougm if (optlist != NULL) { 6883034Sdougm /* check options, if any, for validity */ 6893034Sdougm ret = valid_options(optlist, protocol, group, NULL); 6903034Sdougm } 6913034Sdougm } 6923034Sdougm if (ret == SA_OK && !dryrun) { 6933034Sdougm if (group == NULL) { 6943910Sdougm group = sa_create_group(handle, (char *)groupname, &err); 6953034Sdougm } 6963034Sdougm if (group != NULL) { 6973034Sdougm sa_optionset_t optionset; 6983034Sdougm if (optlist != NULL) { 6993034Sdougm (void) add_optionset(group, optlist, protocol, &ret); 7003034Sdougm } else if (protocol != NULL) { 7013034Sdougm optionset = sa_create_optionset(group, protocol); 7023034Sdougm if (optionset == NULL) 7033034Sdougm ret = SA_NO_MEMORY; 7043034Sdougm } else if (protocol == NULL) { 7053034Sdougm char **protolist; 7063034Sdougm int numprotos, i; 7073034Sdougm numprotos = sa_get_protocols(&protolist); 7083034Sdougm for (i = 0; i < numprotos; i++) { 7093034Sdougm optionset = sa_create_optionset(group, protolist[i]); 7103034Sdougm } 7113034Sdougm if (protolist != NULL) 7123034Sdougm free(protolist); 7133034Sdougm } 7143034Sdougm /* 7153034Sdougm * we have a group and legal additions 7163034Sdougm */ 7173034Sdougm if (ret == SA_OK) { 7183034Sdougm /* 7193034Sdougm * commit to configuration for protocols that 7203034Sdougm * need to do block updates. For NFS, this 7213034Sdougm * doesn't do anything but it will be run for 7223034Sdougm * all protocols that implement the 7233034Sdougm * appropriate plugin. 7243034Sdougm */ 7253910Sdougm ret = sa_update_config(handle); 7263034Sdougm } else { 7273034Sdougm if (group != NULL) 7283034Sdougm (void) sa_remove_group(group); 7293034Sdougm } 7303034Sdougm } else { 7313034Sdougm ret = err; 7323034Sdougm (void) printf(gettext("Could not create group: %s\n"), 7333034Sdougm sa_errorstr(ret)); 7343034Sdougm } 7353034Sdougm } 7363034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 7373034Sdougm (void) printf(gettext("Command would fail: %s\n"), 7383034Sdougm sa_errorstr(SA_NO_PERMISSION)); 7393034Sdougm ret = SA_NO_PERMISSION; 7403034Sdougm } 7413034Sdougm free_opt(optlist); 7423034Sdougm return (ret); 7433034Sdougm } 7443034Sdougm 7453034Sdougm /* 7463034Sdougm * group_status(group) 7473034Sdougm * 7483034Sdougm * return the current status (enabled/disabled) of the group. 7493034Sdougm */ 7503034Sdougm 7513034Sdougm static char * 7523034Sdougm group_status(sa_group_t group) 7533034Sdougm { 7543034Sdougm char *state; 7553034Sdougm int enabled = 0; 7563034Sdougm 7573034Sdougm state = sa_get_group_attr(group, "state"); 7583034Sdougm if (state != NULL) { 7593034Sdougm if (strcmp(state, "enabled") == 0) { 7603034Sdougm enabled = 1; 7613034Sdougm } 7623034Sdougm sa_free_attr_string(state); 7633034Sdougm } 764*4255Sdougm return (enabled ? "enabled" : "disabled"); 7653034Sdougm } 7663034Sdougm 7673034Sdougm /* 7683034Sdougm * sa_delete(flags, argc, argv) 7693034Sdougm * 7703034Sdougm * Delete a group. 7713034Sdougm */ 7723034Sdougm 7733034Sdougm static int 7743910Sdougm sa_delete(sa_handle_t handle, int flags, int argc, char *argv[]) 7753034Sdougm { 7763034Sdougm char *groupname; 7773034Sdougm sa_group_t group; 7783034Sdougm sa_share_t share; 7793034Sdougm int verbose = 0; 7803034Sdougm int dryrun = 0; 7813034Sdougm int force = 0; 7823034Sdougm int c; 7833034Sdougm char *protocol = NULL; 7843034Sdougm char *sectype = NULL; 7853034Sdougm int ret = SA_OK; 7863034Sdougm int auth; 7873034Sdougm 7883034Sdougm while ((c = getopt(argc, argv, "?hvnP:fS:")) != EOF) { 7893034Sdougm switch (c) { 7903034Sdougm case 'v': 7913034Sdougm verbose++; 7923034Sdougm break; 7933034Sdougm case 'n': 7943034Sdougm dryrun++; 7953034Sdougm break; 7963034Sdougm case 'P': 7973034Sdougm protocol = optarg; 7983034Sdougm if (!sa_valid_protocol(protocol)) { 7993034Sdougm (void) printf(gettext("Invalid protocol specified: %s\n"), 8003034Sdougm protocol); 8013034Sdougm return (SA_INVALID_PROTOCOL); 8023034Sdougm } 8033034Sdougm break; 8043034Sdougm case 'S': 8053034Sdougm sectype = optarg; 8063034Sdougm break; 8073034Sdougm case 'f': 8083034Sdougm force++; 8093034Sdougm break; 8103034Sdougm default: 8113034Sdougm case 'h': 8123034Sdougm case '?': 8133034Sdougm (void) printf(gettext("usage: %s\n"), 8143034Sdougm sa_get_usage(USAGE_DELETE)); 8153034Sdougm return (0); 8163034Sdougm } 8173034Sdougm } 8183034Sdougm 8193034Sdougm if (optind >= argc) { 8203034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_DELETE)); 8213034Sdougm (void) printf(gettext("\tgroup must be specified.\n")); 8223034Sdougm return (SA_SYNTAX_ERR); 8233034Sdougm } 8243034Sdougm 8253034Sdougm if ((optind + 1) < argc) { 8263034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_DELETE)); 8273034Sdougm (void) printf(gettext("\textraneous group(s) at end\n")); 8283034Sdougm return (SA_SYNTAX_ERR); 8293034Sdougm } 8303034Sdougm 8313034Sdougm if (sectype != NULL && protocol == NULL) { 8323034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_DELETE)); 8333034Sdougm (void) printf(gettext("\tsecurity requires protocol to be " 8343034Sdougm "specified.\n")); 8353034Sdougm return (SA_SYNTAX_ERR); 8363034Sdougm } 8373034Sdougm 8383034Sdougm /* 8393034Sdougm * Determine if the group already exists since it must in 8403034Sdougm * order to be removed. 8413034Sdougm * 8423034Sdougm * We can delete when: 8433034Sdougm * 8443034Sdougm * - group is empty 8453034Sdougm * - force flag is set 8463034Sdougm * - if protocol specified, only delete the protocol 8473034Sdougm */ 8483034Sdougm 8493034Sdougm groupname = argv[optind]; 8503910Sdougm group = sa_get_group(handle, groupname); 8513034Sdougm if (group == NULL) { 8523034Sdougm ret = SA_NO_SUCH_GROUP; 8533034Sdougm } else { 8543034Sdougm auth = check_authorizations(groupname, flags); 8553034Sdougm if (protocol == NULL) { 8563034Sdougm share = sa_get_share(group, NULL); 8573034Sdougm if (share != NULL) 8583034Sdougm ret = SA_BUSY; 8593034Sdougm if (share == NULL || (share != NULL && force == 1)) { 8603034Sdougm ret = SA_OK; 8613034Sdougm if (!dryrun) { 8623034Sdougm while (share != NULL) { 8633034Sdougm sa_share_t next_share; 8643034Sdougm next_share = sa_get_next_share(share); 8653034Sdougm /* 8663034Sdougm * need to do the disable of each 8673034Sdougm * share, but don't actually do 8683034Sdougm * anything on a dryrun. 8693034Sdougm */ 8703034Sdougm ret = sa_disable_share(share, NULL); 8713034Sdougm ret = sa_remove_share(share); 8723034Sdougm share = next_share; 8733034Sdougm } 8743034Sdougm ret = sa_remove_group(group); 8753034Sdougm } 8763034Sdougm } 8773034Sdougm /* commit to configuration if not a dryrun */ 8783034Sdougm if (!dryrun && ret == SA_OK) { 8793910Sdougm ret = sa_update_config(handle); 8803034Sdougm } 8813034Sdougm } else { 8823034Sdougm /* a protocol delete */ 8833034Sdougm sa_optionset_t optionset; 8843034Sdougm sa_security_t security; 8853034Sdougm if (sectype != NULL) { 8863034Sdougm /* only delete specified security */ 8873034Sdougm security = sa_get_security(group, sectype, protocol); 8883034Sdougm if (security != NULL && !dryrun) { 8893034Sdougm ret = sa_destroy_security(security); 8903034Sdougm } else { 8913034Sdougm ret = SA_INVALID_PROTOCOL; 8923034Sdougm } 8933034Sdougm } else { 8943034Sdougm optionset = sa_get_optionset(group, protocol); 8953034Sdougm if (optionset != NULL && !dryrun) { 8963034Sdougm /* have an optionset with protocol to delete */ 8973034Sdougm ret = sa_destroy_optionset(optionset); 8983034Sdougm /* 8993034Sdougm * now find all security sets for the protocol 9003034Sdougm * and remove them. Don't remove other 9013034Sdougm * protocols. 9023034Sdougm */ 9033034Sdougm for (security = sa_get_security(group, NULL, NULL); 9043034Sdougm ret == SA_OK && security != NULL; 9053034Sdougm security = sa_get_next_security(security)) { 9063034Sdougm char *secprot; 9073034Sdougm 9083034Sdougm secprot = sa_get_security_attr(security, "type"); 9093034Sdougm if (secprot != NULL && 9103034Sdougm strcmp(secprot, protocol) == 0) 9113034Sdougm ret = sa_destroy_security(security); 9123034Sdougm if (secprot != NULL) 9133034Sdougm sa_free_attr_string(secprot); 9143034Sdougm } 9153034Sdougm } else { 9163034Sdougm if (!dryrun) 9173034Sdougm ret = SA_INVALID_PROTOCOL; 9183034Sdougm } 9193034Sdougm } 9203034Sdougm } 9213034Sdougm } 9223034Sdougm if (ret != SA_OK) { 9233034Sdougm (void) printf(gettext("Could not delete group: %s\n"), 9243034Sdougm sa_errorstr(ret)); 9253034Sdougm } else if (dryrun && !auth && verbose) { 9263034Sdougm (void) printf(gettext("Command would fail: %s\n"), 9273034Sdougm sa_errorstr(SA_NO_PERMISSION)); 9283034Sdougm } 9293034Sdougm return (ret); 9303034Sdougm } 9313034Sdougm 9323034Sdougm /* 9333034Sdougm * strndupr(*buff, str, buffsize) 9343034Sdougm * 9353034Sdougm * used with small strings to duplicate and possibly increase the 9363034Sdougm * buffer size of a string. 9373034Sdougm */ 9383034Sdougm static char * 9393034Sdougm strndupr(char *buff, char *str, int *buffsize) 9403034Sdougm { 9413034Sdougm int limit; 9423034Sdougm char *orig_buff = buff; 9433034Sdougm 9443034Sdougm if (buff == NULL) { 9453034Sdougm buff = (char *)malloc(64); 9463034Sdougm if (buff == NULL) 9473034Sdougm return (NULL); 9483034Sdougm *buffsize = 64; 9493034Sdougm buff[0] = '\0'; 9503034Sdougm } 9513034Sdougm limit = strlen(buff) + strlen(str) + 1; 9523034Sdougm if (limit > *buffsize) { 9533034Sdougm limit = *buffsize = *buffsize + ((limit / 64) + 64); 9543034Sdougm buff = realloc(buff, limit); 9553034Sdougm } 9563034Sdougm if (buff != NULL) { 9573034Sdougm (void) strcat(buff, str); 9583034Sdougm } else { 9593034Sdougm /* if it fails, fail it hard */ 9603034Sdougm if (orig_buff != NULL) 9613034Sdougm free(orig_buff); 9623034Sdougm } 9633034Sdougm return (buff); 9643034Sdougm } 9653034Sdougm 9663034Sdougm /* 9673034Sdougm * group_proto(group) 9683034Sdougm * 9693034Sdougm * return a string of all the protocols (space separated) associated 9703034Sdougm * with this group. 9713034Sdougm */ 9723034Sdougm 9733034Sdougm static char * 9743034Sdougm group_proto(sa_group_t group) 9753034Sdougm { 9763034Sdougm sa_optionset_t optionset; 9773034Sdougm char *proto; 9783034Sdougm char *buff = NULL; 9793034Sdougm int buffsize = 0; 9803034Sdougm int addspace = 0; 9813034Sdougm /* 9823034Sdougm * get the protocol list by finding the optionsets on this 9833034Sdougm * group and extracting the type value. The initial call to 9843034Sdougm * strndupr() initailizes buff. 9853034Sdougm */ 9863034Sdougm buff = strndupr(buff, "", &buffsize); 9873034Sdougm if (buff != NULL) { 9883034Sdougm for (optionset = sa_get_optionset(group, NULL); 9893034Sdougm optionset != NULL && buff != NULL; 9903034Sdougm optionset = sa_get_next_optionset(optionset)) { 9913034Sdougm /* 9923034Sdougm * extract out the protocol type from this optionset 9933034Sdougm * and append it to the buffer "buff". strndupr() will 9943034Sdougm * reallocate space as necessay. 9953034Sdougm */ 9963034Sdougm proto = sa_get_optionset_attr(optionset, "type"); 9973034Sdougm if (proto != NULL) { 9983034Sdougm if (addspace++) 9993034Sdougm buff = strndupr(buff, " ", &buffsize); 10003034Sdougm buff = strndupr(buff, proto, &buffsize); 10013034Sdougm sa_free_attr_string(proto); 10023034Sdougm } 10033034Sdougm } 10043034Sdougm } 10053034Sdougm return (buff); 10063034Sdougm } 10073034Sdougm 10083034Sdougm /* 10093034Sdougm * sa_list(flags, argc, argv) 10103034Sdougm * 10113034Sdougm * implements the "list" subcommand to list groups and optionally 10123034Sdougm * their state and protocols. 10133034Sdougm */ 10143034Sdougm 10153034Sdougm static int 10163910Sdougm sa_list(sa_handle_t handle, int flags, int argc, char *argv[]) 10173034Sdougm { 10183034Sdougm sa_group_t group; 10193034Sdougm int verbose = 0; 10203034Sdougm int c; 10213034Sdougm char *protocol = NULL; 10223034Sdougm #ifdef lint 10233034Sdougm flags = flags; 10243034Sdougm #endif 10253034Sdougm 10263034Sdougm while ((c = getopt(argc, argv, "?hvP:")) != EOF) { 10273034Sdougm switch (c) { 10283034Sdougm case 'v': 10293034Sdougm verbose++; 10303034Sdougm break; 10313034Sdougm case 'P': 10323034Sdougm protocol = optarg; 10333034Sdougm if (!sa_valid_protocol(protocol)) { 10343034Sdougm (void) printf(gettext("Invalid protocol specified:" 10353034Sdougm "%s\n"), 10363034Sdougm protocol); 10373034Sdougm return (SA_INVALID_PROTOCOL); 10383034Sdougm } 10393034Sdougm break; 10403034Sdougm default: 10413034Sdougm case 'h': 10423034Sdougm case '?': 10433034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_LIST)); 10443034Sdougm return (0); 10453034Sdougm } 10463034Sdougm } 10473034Sdougm 10483910Sdougm for (group = sa_get_group(handle, NULL); group != NULL; 10493034Sdougm group = sa_get_next_group(group)) { 10503034Sdougm char *name; 10513034Sdougm char *proto; 10523034Sdougm if (protocol == NULL || has_protocol(group, protocol)) { 10533034Sdougm name = sa_get_group_attr(group, "name"); 10543034Sdougm if (name != NULL && (verbose > 1 || name[0] != '#')) { 10553034Sdougm (void) printf("%s", (char *)name); 10563034Sdougm if (verbose) { 10573034Sdougm /* 1058*4255Sdougm * need the list of protocols and current 1059*4255Sdougm * status once available. We do want to 1060*4255Sdougm * translate the enabled/disabled text here. 10613034Sdougm */ 1062*4255Sdougm (void) printf("\t%s", isenabled(group) ? 1063*4255Sdougm gettext("enabled") : 1064*4255Sdougm gettext("disabled")); 10653034Sdougm proto = group_proto(group); 10663034Sdougm if (proto != NULL) { 10673034Sdougm (void) printf("\t%s", (char *)proto); 10683034Sdougm free(proto); 10693034Sdougm } 10703034Sdougm } 10713034Sdougm (void) printf("\n"); 10723034Sdougm } 10733034Sdougm if (name != NULL) 10743034Sdougm sa_free_attr_string(name); 10753034Sdougm } 10763034Sdougm } 10773034Sdougm return (0); 10783034Sdougm } 10793034Sdougm 10803034Sdougm /* 10813034Sdougm * out_properties(optionset, proto, sec) 10823034Sdougm * 10833034Sdougm * Format the properties and encode the protocol and optional named 10843034Sdougm * optionset into the string. 10853034Sdougm * 10863034Sdougm * format is protocol[:name]=(property-list) 10873034Sdougm */ 10883034Sdougm 10893034Sdougm static void 10903034Sdougm out_properties(sa_optionset_t optionset, char *proto, char *sec) 10913034Sdougm { 10923034Sdougm char *type; 10933034Sdougm char *value; 10943034Sdougm int spacer; 10953034Sdougm sa_property_t prop; 10963034Sdougm 10973034Sdougm if (sec == NULL) { 10983034Sdougm (void) printf(" %s=(", proto ? proto : gettext("all")); 10993034Sdougm } else { 11003034Sdougm (void) printf(" %s:%s=(", proto ? proto : gettext("all"), sec); 11013034Sdougm } 11023034Sdougm 11033034Sdougm for (spacer = 0, prop = sa_get_property(optionset, NULL); 11043034Sdougm prop != NULL; prop = sa_get_next_property(prop)) { 11053034Sdougm 11063034Sdougm /* 11073034Sdougm * extract the property name/value and output with 11083034Sdougm * appropriate spacing. I.e. no prefixed space the 11093034Sdougm * first time through but a space on subsequent 11103034Sdougm * properties. 11113034Sdougm */ 11123034Sdougm type = sa_get_property_attr(prop, "type"); 11133034Sdougm value = sa_get_property_attr(prop, "value"); 11143034Sdougm if (type != NULL) { 11153034Sdougm (void) printf("%s%s=", spacer ? " " : "", type); 11163034Sdougm spacer = 1; 11173034Sdougm if (value != NULL) 11183034Sdougm (void) printf("\"%s\"", value); 11193034Sdougm else 11203034Sdougm (void) printf("\"\""); 11213034Sdougm } 11223034Sdougm if (type != NULL) 11233034Sdougm sa_free_attr_string(type); 11243034Sdougm if (value != NULL) 11253034Sdougm sa_free_attr_string(value); 11263034Sdougm } 11273034Sdougm (void) printf(")"); 11283034Sdougm } 11293034Sdougm 11303034Sdougm /* 11313034Sdougm * show_properties(group, protocol, prefix) 11323034Sdougm * 11333034Sdougm * print the properties for a group. If protocol is NULL, do all 11343034Sdougm * protocols otherwise only the specified protocol. All security 11353034Sdougm * (named groups specific to the protocol) are included. 11363034Sdougm * 11373034Sdougm * The "prefix" is always applied. The caller knows whether it wants 11383034Sdougm * some type of prefix string (white space) or not. Once the prefix 11393034Sdougm * has been output, it is reduced to the zero length string for the 11403034Sdougm * remainder of the property output. 11413034Sdougm */ 11423034Sdougm 11433034Sdougm static void 11443034Sdougm show_properties(sa_group_t group, char *protocol, char *prefix) 11453034Sdougm { 11463034Sdougm sa_optionset_t optionset; 11473034Sdougm sa_security_t security; 11483034Sdougm char *value; 11493034Sdougm char *secvalue; 11503034Sdougm 11513034Sdougm if (protocol != NULL) { 11523034Sdougm optionset = sa_get_optionset(group, protocol); 11533034Sdougm if (optionset != NULL) { 11543034Sdougm (void) printf("%s", prefix); 11553034Sdougm prefix = ""; 11563034Sdougm out_properties(optionset, protocol, NULL); 11573034Sdougm } 11583034Sdougm security = sa_get_security(group, protocol, NULL); 11593034Sdougm if (security != NULL) { 11603034Sdougm (void) printf("%s", prefix); 11613034Sdougm prefix = ""; 11623034Sdougm out_properties(security, protocol, NULL); 11633034Sdougm } 11643034Sdougm } else { 11653034Sdougm for (optionset = sa_get_optionset(group, protocol); 11663034Sdougm optionset != NULL; 11673034Sdougm optionset = sa_get_next_optionset(optionset)) { 11683034Sdougm 11693034Sdougm value = sa_get_optionset_attr(optionset, "type"); 11703034Sdougm (void) printf("%s", prefix); 11713034Sdougm prefix = ""; 11723034Sdougm out_properties(optionset, value, 0); 11733034Sdougm if (value != NULL) 11743034Sdougm sa_free_attr_string(value); 11753034Sdougm } 11763034Sdougm for (security = sa_get_security(group, NULL, protocol); 11773034Sdougm security != NULL; 11783034Sdougm security = sa_get_next_security(security)) { 11793034Sdougm 11803034Sdougm value = sa_get_security_attr(security, "type"); 11813034Sdougm secvalue = sa_get_security_attr(security, "sectype"); 11823034Sdougm (void) printf("%s", prefix); 11833034Sdougm prefix = ""; 11843034Sdougm out_properties(security, value, secvalue); 11853034Sdougm if (value != NULL) 11863034Sdougm sa_free_attr_string(value); 11873034Sdougm if (secvalue != NULL) 11883034Sdougm sa_free_attr_string(secvalue); 11893034Sdougm } 11903034Sdougm } 11913034Sdougm } 11923034Sdougm 11933034Sdougm /* 11943034Sdougm * show_group(group, verbose, properties, proto, subgroup) 11953034Sdougm * 11963034Sdougm * helper function to show the contents of a group. 11973034Sdougm */ 11983034Sdougm 11993034Sdougm static void 12003034Sdougm show_group(sa_group_t group, int verbose, int properties, char *proto, 12013034Sdougm char *subgroup) 12023034Sdougm { 12033034Sdougm sa_share_t share; 12043034Sdougm char *groupname; 12053034Sdougm char *sharepath; 12063034Sdougm char *resource; 12073034Sdougm char *description; 12083034Sdougm char *type; 12093034Sdougm char *zfs = NULL; 12103034Sdougm int iszfs = 0; 12113034Sdougm 12123034Sdougm groupname = sa_get_group_attr(group, "name"); 12133034Sdougm if (groupname != NULL) { 12143034Sdougm if (proto != NULL && !has_protocol(group, proto)) { 12153034Sdougm sa_free_attr_string(groupname); 12163034Sdougm return; 12173034Sdougm } 12183034Sdougm /* 12193034Sdougm * check to see if the group is managed by ZFS. If 12203034Sdougm * there is an attribute, then it is. A non-NULL zfs 12213034Sdougm * variable will trigger the different way to display 12223034Sdougm * and will remove the transient property indicator 12233034Sdougm * from the output. 12243034Sdougm */ 12253034Sdougm zfs = sa_get_group_attr(group, "zfs"); 12263034Sdougm if (zfs != NULL) { 12273034Sdougm iszfs = 1; 12283034Sdougm sa_free_attr_string(zfs); 12293034Sdougm } 12303034Sdougm share = sa_get_share(group, NULL); 12313034Sdougm if (subgroup == NULL) 12323034Sdougm (void) printf("%s", groupname); 12333034Sdougm else 12343034Sdougm (void) printf(" %s/%s", subgroup, groupname); 12353034Sdougm if (properties) { 12363034Sdougm show_properties(group, proto, ""); 12373034Sdougm } 12383034Sdougm (void) printf("\n"); 12393034Sdougm if (strcmp(groupname, "zfs") == 0) { 12403034Sdougm sa_group_t zgroup; 12413034Sdougm 12423034Sdougm for (zgroup = sa_get_sub_group(group); zgroup != NULL; 12433034Sdougm zgroup = sa_get_next_group(zgroup)) { 12443034Sdougm show_group(zgroup, verbose, properties, proto, "zfs"); 12453034Sdougm } 12463034Sdougm sa_free_attr_string(groupname); 12473034Sdougm return; 12483034Sdougm } 12493034Sdougm /* 12503034Sdougm * have a group, so list the contents. Resource and 12513034Sdougm * description are only listed if verbose is set. 12523034Sdougm */ 12533034Sdougm for (share = sa_get_share(group, NULL); share != NULL; 12543034Sdougm share = sa_get_next_share(share)) { 12553034Sdougm sharepath = sa_get_share_attr(share, "path"); 12563034Sdougm if (sharepath != NULL) { 12573034Sdougm if (verbose) { 12583034Sdougm resource = sa_get_share_attr(share, "resource"); 12593034Sdougm description = sa_get_share_description(share); 12603034Sdougm type = sa_get_share_attr(share, "type"); 12613034Sdougm if (type != NULL && !iszfs && 12623034Sdougm strcmp(type, "transient") == 0) 12633034Sdougm (void) printf("\t* "); 12643034Sdougm else 12653034Sdougm (void) printf("\t "); 12663034Sdougm if (resource != NULL && strlen(resource) > 0) { 12673034Sdougm (void) printf("%s=%s", resource, sharepath); 12683034Sdougm } else { 12693034Sdougm (void) printf("%s", sharepath); 12703034Sdougm } 12713034Sdougm if (resource != NULL) 12723034Sdougm sa_free_attr_string(resource); 12733034Sdougm if (properties) 12743034Sdougm show_properties(share, NULL, "\t"); 12753034Sdougm if (description != NULL) { 12763034Sdougm if (strlen(description) > 0) { 12773034Sdougm (void) printf("\t\"%s\"", description); 12783034Sdougm } 12793034Sdougm sa_free_share_description(description); 12803034Sdougm } 12813034Sdougm if (type != NULL) 12823034Sdougm sa_free_attr_string(type); 12833034Sdougm } else { 12843034Sdougm (void) printf("\t%s", sharepath); 12853034Sdougm if (properties) 12863034Sdougm show_properties(share, NULL, "\t"); 12873034Sdougm } 12883034Sdougm (void) printf("\n"); 12893034Sdougm sa_free_attr_string(sharepath); 12903034Sdougm } 12913034Sdougm } 12923034Sdougm } 12933034Sdougm if (groupname != NULL) { 12943034Sdougm sa_free_attr_string(groupname); 12953034Sdougm } 12963034Sdougm } 12973034Sdougm 12983034Sdougm /* 12993034Sdougm * show_group_xml_init() 13003034Sdougm * 13013034Sdougm * Create an XML document that will be used to display config info via 13023034Sdougm * XML format. 13033034Sdougm */ 13043034Sdougm 13053034Sdougm xmlDocPtr 13063034Sdougm show_group_xml_init() 13073034Sdougm { 13083034Sdougm xmlDocPtr doc; 13093034Sdougm xmlNodePtr root; 13103034Sdougm 13113034Sdougm doc = xmlNewDoc((xmlChar *)"1.0"); 13123034Sdougm if (doc != NULL) { 13133034Sdougm root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 13143034Sdougm if (root != NULL) 13153034Sdougm xmlDocSetRootElement(doc, root); 13163034Sdougm } 13173034Sdougm return (doc); 13183034Sdougm } 13193034Sdougm 13203034Sdougm /* 13213034Sdougm * show_group_xml(doc, group) 13223034Sdougm * 13233034Sdougm * Copy the group info into the XML doc. 13243034Sdougm */ 13253034Sdougm 13263034Sdougm static void 13273034Sdougm show_group_xml(xmlDocPtr doc, sa_group_t group) 13283034Sdougm { 13293034Sdougm xmlNodePtr node; 13303034Sdougm xmlNodePtr root; 13313034Sdougm 13323034Sdougm root = xmlDocGetRootElement(doc); 13333034Sdougm node = xmlCopyNode((xmlNodePtr)group, 1); 13343034Sdougm if (node != NULL && root != NULL) { 13353034Sdougm xmlAddChild(root, node); 13363034Sdougm /* 13373034Sdougm * In the future, we may have interally used tags that 13383034Sdougm * should not appear in the XML output. Remove 13393034Sdougm * anything we don't want to show here. 13403034Sdougm */ 13413034Sdougm } 13423034Sdougm } 13433034Sdougm 13443034Sdougm /* 13453034Sdougm * sa_show(flags, argc, argv) 13463034Sdougm * 13473034Sdougm * Implements the show subcommand. 13483034Sdougm */ 13493034Sdougm 13503034Sdougm int 13513910Sdougm sa_show(sa_handle_t handle, int flags, int argc, char *argv[]) 13523034Sdougm { 13533034Sdougm sa_group_t group; 13543034Sdougm int verbose = 0; 13553034Sdougm int properties = 0; 13563034Sdougm int c; 13573034Sdougm int ret = SA_OK; 13583034Sdougm char *protocol = NULL; 13593034Sdougm int xml = 0; 13603034Sdougm xmlDocPtr doc; 13613034Sdougm #ifdef lint 13623034Sdougm flags = flags; 13633034Sdougm #endif 13643034Sdougm 13653034Sdougm while ((c = getopt(argc, argv, "?hvP:px")) != EOF) { 13663034Sdougm switch (c) { 13673034Sdougm case 'v': 13683034Sdougm verbose++; 13693034Sdougm break; 13703034Sdougm case 'p': 13713034Sdougm properties++; 13723034Sdougm break; 13733034Sdougm case 'P': 13743034Sdougm protocol = optarg; 13753034Sdougm if (!sa_valid_protocol(protocol)) { 13763034Sdougm (void) printf(gettext("Invalid protocol specified: %s\n"), 13773034Sdougm protocol); 13783034Sdougm return (SA_INVALID_PROTOCOL); 13793034Sdougm } 13803034Sdougm break; 13813034Sdougm case 'x': 13823034Sdougm xml++; 13833034Sdougm break; 13843034Sdougm default: 13853034Sdougm case 'h': 13863034Sdougm case '?': 13873034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SHOW)); 13883034Sdougm return (0); 13893034Sdougm } 13903034Sdougm } 13913034Sdougm 13923034Sdougm if (xml) { 13933034Sdougm doc = show_group_xml_init(); 13943034Sdougm if (doc == NULL) 13953034Sdougm ret = SA_NO_MEMORY; 13963034Sdougm } 13973034Sdougm 13983034Sdougm if (optind == argc) { 13993034Sdougm /* no group specified so go through them all */ 14003910Sdougm for (group = sa_get_group(handle, NULL); group != NULL; 14013034Sdougm group = sa_get_next_group(group)) { 14023034Sdougm /* 14033034Sdougm * have a group so check if one we want and then list 14043034Sdougm * contents with appropriate options. 14053034Sdougm */ 14063034Sdougm if (xml) 14073034Sdougm show_group_xml(doc, group); 14083034Sdougm else 14093034Sdougm show_group(group, verbose, properties, protocol, NULL); 14103034Sdougm } 14113034Sdougm } else { 14123034Sdougm /* have a specified list of groups */ 14133034Sdougm for (; optind < argc; optind++) { 14143910Sdougm group = sa_get_group(handle, argv[optind]); 14153034Sdougm if (group != NULL) { 14163034Sdougm if (xml) 14173034Sdougm show_group_xml(doc, group); 14183034Sdougm else 14193034Sdougm show_group(group, verbose, properties, protocol, NULL); 14203034Sdougm } else { 14213034Sdougm (void) printf(gettext("%s: not found\n"), argv[optind]); 14223034Sdougm ret = SA_NO_SUCH_GROUP; 14233034Sdougm } 14243034Sdougm } 14253034Sdougm } 14263034Sdougm if (xml && ret == SA_OK) { 14273034Sdougm xmlDocFormatDump(stdout, doc, 1); 14283034Sdougm xmlFreeDoc(doc); 14293034Sdougm } 14303034Sdougm return (ret); 14313034Sdougm 14323034Sdougm } 14333034Sdougm 14343034Sdougm /* 14353034Sdougm * enable_share(group, share, update_legacy) 14363034Sdougm * 14373034Sdougm * helper function to enable a share if the group is enabled. 14383034Sdougm */ 14393034Sdougm 14403034Sdougm static int 14413910Sdougm enable_share(sa_handle_t handle, sa_group_t group, sa_share_t share, 14423910Sdougm int update_legacy) 14433034Sdougm { 14443034Sdougm char *value; 14453034Sdougm int enabled; 14463034Sdougm sa_optionset_t optionset; 14473034Sdougm int ret = SA_OK; 14483034Sdougm char *zfs = NULL; 14493034Sdougm int iszfs = 0; 14503034Sdougm 14513034Sdougm /* 14523034Sdougm * need to enable this share if the group is enabled but not 14533034Sdougm * otherwise. The enable is also done on each protocol 14543034Sdougm * represented in the group. 14553034Sdougm */ 14563034Sdougm value = sa_get_group_attr(group, "state"); 14573034Sdougm enabled = value != NULL && strcmp(value, "enabled") == 0; 14583034Sdougm if (value != NULL) 14593034Sdougm sa_free_attr_string(value); 14603034Sdougm /* remove legacy config if necessary */ 14613034Sdougm if (update_legacy) 14623034Sdougm ret = sa_delete_legacy(share); 14633034Sdougm zfs = sa_get_group_attr(group, "zfs"); 14643034Sdougm if (zfs != NULL) { 14653034Sdougm iszfs++; 14663034Sdougm sa_free_attr_string(zfs); 14673034Sdougm } 14683034Sdougm 14693034Sdougm /* 14703034Sdougm * Step through each optionset at the group level and 14713034Sdougm * enable the share based on the protocol type. This 14723034Sdougm * works because protocols must be set on the group 14733034Sdougm * for the protocol to be enabled. 14743034Sdougm */ 14753034Sdougm for (optionset = sa_get_optionset(group, NULL); 14763034Sdougm optionset != NULL && ret == SA_OK; 14773034Sdougm optionset = sa_get_next_optionset(optionset)) { 14783034Sdougm value = sa_get_optionset_attr(optionset, "type"); 14793034Sdougm if (value != NULL) { 14803034Sdougm if (enabled) 14813034Sdougm ret = sa_enable_share(share, value); 14823034Sdougm if (update_legacy && !iszfs) 14833034Sdougm (void) sa_update_legacy(share, value); 14843034Sdougm sa_free_attr_string(value); 14853034Sdougm } 14863034Sdougm } 14873034Sdougm if (ret == SA_OK) 14883910Sdougm (void) sa_update_config(handle); 14893034Sdougm return (ret); 14903034Sdougm } 14913034Sdougm 14923034Sdougm /* 14933034Sdougm * sa_addshare(flags, argc, argv) 14943034Sdougm * 14953034Sdougm * implements add-share subcommand. 14963034Sdougm */ 14973034Sdougm 14983034Sdougm int 14993910Sdougm sa_addshare(sa_handle_t handle, int flags, int argc, char *argv[]) 15003034Sdougm { 15013034Sdougm int verbose = 0; 15023034Sdougm int dryrun = 0; 15033034Sdougm int c; 15043034Sdougm int ret = SA_OK; 15053034Sdougm sa_group_t group; 15063034Sdougm sa_share_t share; 15073034Sdougm char *sharepath = NULL; 15083034Sdougm char *description = NULL; 15093034Sdougm char *resource = NULL; 15103034Sdougm int persist = SA_SHARE_PERMANENT; /* default to persist */ 15113034Sdougm int auth; 15123034Sdougm char dir[MAXPATHLEN]; 15133034Sdougm 15143034Sdougm while ((c = getopt(argc, argv, "?hvns:d:r:t")) != EOF) { 15153034Sdougm switch (c) { 15163034Sdougm case 'n': 15173034Sdougm dryrun++; 15183034Sdougm break; 15193034Sdougm case 'v': 15203034Sdougm verbose++; 15213034Sdougm break; 15223034Sdougm case 'd': 15233034Sdougm description = optarg; 15243034Sdougm break; 15253034Sdougm case 'r': 15263034Sdougm resource = optarg; 15273034Sdougm break; 15283034Sdougm case 's': 15293034Sdougm /* 15303034Sdougm * save share path into group. Currently limit 15313034Sdougm * to one share per command. 15323034Sdougm */ 15333034Sdougm if (sharepath != NULL) { 15343034Sdougm (void) printf(gettext("Adding multiple shares not" 15353034Sdougm "supported\n")); 15363034Sdougm return (1); 15373034Sdougm } 15383034Sdougm sharepath = optarg; 15393034Sdougm break; 15403034Sdougm case 't': 15413034Sdougm persist = SA_SHARE_TRANSIENT; 15423034Sdougm break; 15433034Sdougm default: 15443034Sdougm case 'h': 15453034Sdougm case '?': 15463034Sdougm (void) printf(gettext("usage: %s\n"), 15473034Sdougm sa_get_usage(USAGE_ADD_SHARE)); 15483034Sdougm return (0); 15493034Sdougm } 15503034Sdougm } 15513034Sdougm 15523034Sdougm if (optind >= argc) { 15533034Sdougm (void) printf(gettext("usage: %s\n"), 15543034Sdougm sa_get_usage(USAGE_ADD_SHARE)); 15553034Sdougm if (dryrun || sharepath != NULL || description != NULL || 15563034Sdougm resource != NULL || verbose || persist) { 15573034Sdougm (void) printf(gettext("\tgroup must be specified\n")); 15583034Sdougm ret = SA_NO_SUCH_GROUP; 15593034Sdougm } else { 15603034Sdougm ret = SA_OK; 15613034Sdougm } 15623034Sdougm } else { 15633034Sdougm if (sharepath == NULL) { 15643034Sdougm (void) printf(gettext("usage: %s\n"), 15653034Sdougm sa_get_usage(USAGE_ADD_SHARE)); 15663034Sdougm (void) printf(gettext("\t-s sharepath must be specified\n")); 15673034Sdougm ret = SA_BAD_PATH; 15683034Sdougm } 15693034Sdougm if (ret == SA_OK) { 15703034Sdougm if (realpath(sharepath, dir) == NULL) { 15713034Sdougm ret = SA_BAD_PATH; 15723034Sdougm (void) printf(gettext("Path is not valid: %s\n"), 15733034Sdougm sharepath); 15743034Sdougm } else { 15753034Sdougm sharepath = dir; 15763034Sdougm } 15773034Sdougm } 15783034Sdougm if (ret == SA_OK && resource != NULL) { 15793034Sdougm /* check for valid syntax */ 15803034Sdougm if (strpbrk(resource, " \t/") != NULL) { 15813034Sdougm (void) printf(gettext("usage: %s\n"), 15823034Sdougm sa_get_usage(USAGE_ADD_SHARE)); 15833034Sdougm (void) printf(gettext("\tresource must not contain white" 15843034Sdougm "space or '/' characters\n")); 15853034Sdougm ret = SA_BAD_PATH; 15863034Sdougm } 15873034Sdougm } 15883034Sdougm if (ret == SA_OK) { 15893910Sdougm group = sa_get_group(handle, argv[optind]); 15903034Sdougm if (group != NULL) { 15913034Sdougm auth = check_authorizations(argv[optind], flags); 15923910Sdougm share = sa_find_share(handle, sharepath); 15933034Sdougm if (share != NULL) { 15943034Sdougm group = sa_get_parent_group(share); 15953034Sdougm if (group != NULL) { 15963034Sdougm char *groupname; 15973034Sdougm groupname = sa_get_group_attr(group, "name"); 15983034Sdougm if (groupname != NULL) { 15993034Sdougm (void) printf(gettext("Share path already " 16003034Sdougm "shared in group " 16013034Sdougm "\"%s\": %s\n"), 16023034Sdougm groupname, sharepath); 16033034Sdougm sa_free_attr_string(groupname); 16043034Sdougm } else { 16053034Sdougm (void) printf(gettext("Share path already" 16063034Sdougm "shared: %s\n"), 16073034Sdougm groupname, sharepath); 16083034Sdougm } 16093034Sdougm } else { 16103034Sdougm (void) printf(gettext("Share path %s already " 16113034Sdougm "shared\n"), 16123034Sdougm sharepath); 16133034Sdougm } 16143034Sdougm ret = SA_DUPLICATE_NAME; 16153034Sdougm } else { 16163034Sdougm /* 16173034Sdougm * need to check that resource name is unique 16183348Sdougm * at some point. Path checking should use the 16193348Sdougm * "normal" rules which don't check the repository. 16203034Sdougm */ 16213034Sdougm if (dryrun) 16223348Sdougm ret = sa_check_path(group, sharepath, 16233348Sdougm SA_CHECK_NORMAL); 16243034Sdougm else 16253034Sdougm share = sa_add_share(group, sharepath, 16263034Sdougm persist, &ret); 16273034Sdougm if (!dryrun && share == NULL) { 16283034Sdougm (void) printf(gettext("Could not add share: " 16293034Sdougm "%s\n"), 16303034Sdougm sa_errorstr(ret)); 16313034Sdougm } else { 16323034Sdougm if (!dryrun && ret == SA_OK) { 16333034Sdougm if (resource != NULL) { 16343034Sdougm if (strpbrk(resource, " \t/") == NULL) { 16353034Sdougm ret = sa_set_share_attr(share, 16363034Sdougm "resource", 16373034Sdougm resource); 16383034Sdougm } 16393034Sdougm } 16403034Sdougm if (ret == SA_OK && description != NULL) { 16413034Sdougm ret = sa_set_share_description(share, 16423034Sdougm description); 16433034Sdougm } 16443034Sdougm if (ret == SA_OK) { 16453034Sdougm /* now enable the share(s) */ 16463910Sdougm ret = enable_share(handle, group, share, 1); 16473910Sdougm ret = sa_update_config(handle); 16483034Sdougm } 16493034Sdougm switch (ret) { 16503034Sdougm case SA_DUPLICATE_NAME: 16513034Sdougm (void) printf(gettext("Resource name in" 16523034Sdougm "use: %s\n"), 16533034Sdougm resource); 16543034Sdougm break; 16553034Sdougm default: 16563034Sdougm (void) printf(gettext("Could not set " 16573034Sdougm "attribute: %s\n"), 16583034Sdougm sa_errorstr(ret)); 16593034Sdougm break; 16603034Sdougm case SA_OK: 16613034Sdougm break; 16623034Sdougm } 16633034Sdougm } else if (dryrun && ret == SA_OK && 16643034Sdougm !auth && verbose) { 16653034Sdougm (void) printf(gettext("Command would fail: " 16663034Sdougm "%s\n"), 16673034Sdougm sa_errorstr(SA_NO_PERMISSION)); 16683034Sdougm ret = SA_NO_PERMISSION; 16693034Sdougm } 16703034Sdougm } 16713034Sdougm } 16723034Sdougm } else { 16733034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), 16743034Sdougm argv[optind]); 16753034Sdougm ret = SA_NO_SUCH_GROUP; 16763034Sdougm } 16773034Sdougm } 16783034Sdougm } 16793034Sdougm return (ret); 16803034Sdougm } 16813034Sdougm 16823034Sdougm /* 16833034Sdougm * sa_moveshare(flags, argc, argv) 16843034Sdougm * 16853034Sdougm * implements move-share subcommand. 16863034Sdougm */ 16873034Sdougm 16883034Sdougm int 16893910Sdougm sa_moveshare(sa_handle_t handle, int flags, int argc, char *argv[]) 16903034Sdougm { 16913034Sdougm int verbose = 0; 16923034Sdougm int dryrun = 0; 16933034Sdougm int c; 16943034Sdougm int ret = SA_OK; 16953034Sdougm sa_group_t group; 16963034Sdougm sa_share_t share; 16973034Sdougm char *sharepath = NULL; 16983034Sdougm int authsrc = 0, authdst = 0; 16993034Sdougm 17003034Sdougm while ((c = getopt(argc, argv, "?hvns:")) != EOF) { 17013034Sdougm switch (c) { 17023034Sdougm case 'n': 17033034Sdougm dryrun++; 17043034Sdougm break; 17053034Sdougm case 'v': 17063034Sdougm verbose++; 17073034Sdougm break; 17083034Sdougm case 's': 17093034Sdougm /* 17103034Sdougm * remove share path from group. Currently limit 17113034Sdougm * to one share per command. 17123034Sdougm */ 17133034Sdougm if (sharepath != NULL) { 17143034Sdougm (void) printf(gettext("Moving multiple shares not" 17153034Sdougm "supported\n")); 17163034Sdougm return (SA_BAD_PATH); 17173034Sdougm } 17183034Sdougm sharepath = optarg; 17193034Sdougm break; 17203034Sdougm default: 17213034Sdougm case 'h': 17223034Sdougm case '?': 17233034Sdougm (void) printf(gettext("usage: %s\n"), 17243034Sdougm sa_get_usage(USAGE_MOVE_SHARE)); 17253034Sdougm return (0); 17263034Sdougm } 17273034Sdougm } 17283034Sdougm 17293034Sdougm if (optind >= argc || sharepath == NULL) { 17303034Sdougm (void) printf(gettext("usage: %s\n"), 17313034Sdougm sa_get_usage(USAGE_MOVE_SHARE)); 17323034Sdougm if (dryrun || verbose || sharepath != NULL) { 17333034Sdougm (void) printf(gettext("\tgroup must be specified\n")); 17343034Sdougm ret = SA_NO_SUCH_GROUP; 17353034Sdougm } else { 17363034Sdougm if (sharepath == NULL) { 17373034Sdougm ret = SA_SYNTAX_ERR; 17383034Sdougm (void) printf(gettext("\tsharepath must be specified\n")); 17393034Sdougm } else 17403034Sdougm ret = SA_OK; 17413034Sdougm } 17423034Sdougm } else { 17433034Sdougm if (sharepath == NULL) { 17443034Sdougm (void) printf(gettext("sharepath must be specified with " 17453034Sdougm "the -s option\n")); 17463034Sdougm ret = SA_BAD_PATH; 17473034Sdougm } else { 17483910Sdougm group = sa_get_group(handle, argv[optind]); 17493034Sdougm if (group != NULL) { 17503910Sdougm share = sa_find_share(handle, sharepath); 17513034Sdougm authdst = check_authorizations(argv[optind], flags); 17523034Sdougm if (share == NULL) { 17533034Sdougm (void) printf(gettext("Share not found: %s\n"), 17543034Sdougm sharepath); 17553034Sdougm ret = SA_NO_SUCH_PATH; 17563034Sdougm } else { 17573034Sdougm sa_group_t parent; 17583034Sdougm char *zfsold; 17593034Sdougm char *zfsnew; 17603034Sdougm 17613034Sdougm parent = sa_get_parent_group(share); 17623034Sdougm if (parent != NULL) { 17633034Sdougm char *pname; 17643034Sdougm pname = sa_get_group_attr(parent, "name"); 17653034Sdougm if (pname != NULL) { 17663034Sdougm authsrc = check_authorizations(pname, flags); 17673034Sdougm sa_free_attr_string(pname); 17683034Sdougm } 17693034Sdougm zfsold = sa_get_group_attr(parent, "zfs"); 17703034Sdougm zfsnew = sa_get_group_attr(group, "zfs"); 17713034Sdougm if ((zfsold != NULL && zfsnew == NULL) || 17723034Sdougm (zfsold == NULL && zfsnew != NULL)) { 17733034Sdougm ret = SA_NOT_ALLOWED; 17743034Sdougm } 17753034Sdougm if (zfsold != NULL) 17763034Sdougm sa_free_attr_string(zfsold); 17773034Sdougm if (zfsnew != NULL) 17783034Sdougm sa_free_attr_string(zfsnew); 17793034Sdougm } 17803034Sdougm if (!dryrun && ret == SA_OK) { 17813034Sdougm ret = sa_move_share(group, share); 17823034Sdougm } 17833034Sdougm if (ret == SA_OK && parent != group && !dryrun) { 17843034Sdougm char *oldstate; 17853910Sdougm ret = sa_update_config(handle); 17863034Sdougm /* 17873034Sdougm * note that the share may need to be 17883034Sdougm * "unshared" if the new group is 17893034Sdougm * disabled and the old was enabled or 17903034Sdougm * it may need to be share to update 17913034Sdougm * if the new group is enabled. 17923034Sdougm */ 17933034Sdougm oldstate = sa_get_group_attr(parent, "state"); 17943034Sdougm /* enable_share determines what to do */ 17953034Sdougm if (strcmp(oldstate, "enabled") == 0) { 17963034Sdougm (void) sa_disable_share(share, NULL); 17973034Sdougm } 17983910Sdougm (void) enable_share(handle, group, share, 1); 17993034Sdougm if (oldstate != NULL) 18003034Sdougm sa_free_attr_string(oldstate); 18013034Sdougm } 18023034Sdougm if (ret != SA_OK) { 18033034Sdougm (void) printf(gettext("Could not move share: %s\n"), 18043034Sdougm sa_errorstr(ret)); 18053034Sdougm } 18063034Sdougm if (dryrun && ret == SA_OK && !(authsrc & authdst) && 18073034Sdougm verbose) { 18083034Sdougm (void) printf(gettext("Command would fail: %s\n"), 18093034Sdougm sa_errorstr(SA_NO_PERMISSION)); 18103034Sdougm } 18113034Sdougm } 18123034Sdougm } else { 18133034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), 18143034Sdougm argv[optind]); 18153034Sdougm ret = SA_NO_SUCH_GROUP; 18163034Sdougm } 18173034Sdougm } 18183034Sdougm } 18193034Sdougm return (ret); 18203034Sdougm } 18213034Sdougm 18223034Sdougm /* 18233034Sdougm * sa_removeshare(flags, argc, argv) 18243034Sdougm * 18253034Sdougm * implements remove-share subcommand. 18263034Sdougm */ 18273034Sdougm 18283034Sdougm int 18293910Sdougm sa_removeshare(sa_handle_t handle, int flags, int argc, char *argv[]) 18303034Sdougm { 18313034Sdougm int verbose = 0; 18323034Sdougm int dryrun = 0; 18333034Sdougm int force = 0; 18343034Sdougm int c; 18353034Sdougm int ret = SA_OK; 18363034Sdougm sa_group_t group; 18373034Sdougm sa_share_t share; 18383034Sdougm char *sharepath = NULL; 18393034Sdougm char dir[MAXPATHLEN]; 18403034Sdougm int auth; 18413034Sdougm 18423034Sdougm while ((c = getopt(argc, argv, "?hfns:v")) != EOF) { 18433034Sdougm switch (c) { 18443034Sdougm case 'n': 18453034Sdougm dryrun++; 18463034Sdougm break; 18473034Sdougm case 'v': 18483034Sdougm verbose++; 18493034Sdougm break; 18503034Sdougm case 'f': 18513034Sdougm force++; 18523034Sdougm break; 18533034Sdougm case 's': 18543034Sdougm /* 18553034Sdougm * remove share path from group. Currently limit 18563034Sdougm * to one share per command. 18573034Sdougm */ 18583034Sdougm if (sharepath != NULL) { 18593034Sdougm (void) printf(gettext("Removing multiple shares not" 18603034Sdougm "supported\n")); 18613034Sdougm return (SA_SYNTAX_ERR); 18623034Sdougm } 18633034Sdougm sharepath = optarg; 18643034Sdougm break; 18653034Sdougm default: 18663034Sdougm case 'h': 18673034Sdougm case '?': 18683034Sdougm (void) printf(gettext("usage: %s\n"), 18693034Sdougm sa_get_usage(USAGE_REMOVE_SHARE)); 18703034Sdougm return (0); 18713034Sdougm } 18723034Sdougm } 18733034Sdougm 18743034Sdougm if (optind >= argc || sharepath == NULL) { 18753034Sdougm if (sharepath == NULL) { 18763034Sdougm (void) printf(gettext("usage: %s\n"), 18773034Sdougm sa_get_usage(USAGE_REMOVE_SHARE)); 18783034Sdougm (void) printf(gettext("\t-s sharepath must be specified\n")); 18793034Sdougm ret = SA_BAD_PATH; 18803034Sdougm } else { 18813034Sdougm ret = SA_OK; 18823034Sdougm } 18833034Sdougm } 18843034Sdougm if (ret == SA_OK) { 18853034Sdougm if (optind < argc) { 18863034Sdougm if ((optind + 1) < argc) { 18873034Sdougm (void) printf(gettext("Extraneous group(s) at end of " 18883034Sdougm "command\n")); 18893034Sdougm ret = SA_SYNTAX_ERR; 18903034Sdougm } else { 18913910Sdougm group = sa_get_group(handle, argv[optind]); 18923034Sdougm if (group == NULL) { 18933034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), 18943034Sdougm argv[optind]); 18953034Sdougm ret = SA_NO_SUCH_GROUP; 18963034Sdougm } 18973034Sdougm } 18983034Sdougm } else { 18993034Sdougm group = NULL; 19003034Sdougm } 19013663Sdougm 19023663Sdougm /* 19033663Sdougm * Lookup the path in the internal configuration. Care 19043663Sdougm * must be taken to handle the case where the 19053663Sdougm * underlying path has been removed since we need to 19063663Sdougm * be able to deal with that as well. 19073663Sdougm */ 19083034Sdougm if (ret == SA_OK) { 19093034Sdougm if (group != NULL) 19103034Sdougm share = sa_get_share(group, sharepath); 19113034Sdougm else 19123910Sdougm share = sa_find_share(handle, sharepath); 19133663Sdougm /* 19143663Sdougm * If we didn't find the share with the provided path, 19153663Sdougm * it may be a symlink so attempt to resolve it using 19163663Sdougm * realpath and try again. Realpath will resolve any 19173663Sdougm * symlinks and place them in "dir". Note that 19183663Sdougm * sharepath is only used for the lookup the first 19193663Sdougm * time and later for error messages. dir will be used 19203663Sdougm * on the second attempt. Once a share is found, all 19213663Sdougm * operations are based off of the share variable. 19223663Sdougm */ 19233663Sdougm if (share == NULL) { 19243663Sdougm if (realpath(sharepath, dir) == NULL) { 19253663Sdougm ret = SA_BAD_PATH; 19263663Sdougm (void) printf(gettext("Path is not valid: %s\n"), 19273663Sdougm sharepath); 19283663Sdougm } else { 19293663Sdougm if (group != NULL) 19303663Sdougm share = sa_get_share(group, dir); 19313663Sdougm else 19323910Sdougm share = sa_find_share(handle, dir); 19333663Sdougm } 19343663Sdougm } 19353663Sdougm } 19363663Sdougm 19373663Sdougm /* 19383663Sdougm * If there hasn't been an error, there was likely a 19393663Sdougm * path found. If not, give the appropriate error 19403663Sdougm * message and set the return error. If it was found, 19413663Sdougm * then disable the share and then remove it from the 19423663Sdougm * configuration. 19433663Sdougm */ 19443663Sdougm if (ret == SA_OK) { 19453034Sdougm if (share == NULL) { 19463034Sdougm if (group != NULL) 19473034Sdougm (void) printf(gettext("Share not found in group %s:" 19483663Sdougm " %s\n"), 19493034Sdougm argv[optind], sharepath); 19503034Sdougm else 19513034Sdougm (void) printf(gettext("Share not found: %s\n"), 19523034Sdougm sharepath); 19533034Sdougm ret = SA_NO_SUCH_PATH; 19543034Sdougm } else { 19553034Sdougm if (group == NULL) 19563034Sdougm group = sa_get_parent_group(share); 19573034Sdougm if (!dryrun) { 19583034Sdougm if (ret == SA_OK) { 19593034Sdougm ret = sa_disable_share(share, NULL); 19603034Sdougm /* 19613034Sdougm * we don't care if it fails since it 19623663Sdougm * could be disabled already. Some 19633663Sdougm * unexpected errors could occur that 19643663Sdougm * prevent removal, so also check for 19653663Sdougm * force being set. 19663034Sdougm */ 19673034Sdougm if (ret == SA_OK || ret == SA_NO_SUCH_PATH || 19683663Sdougm ret == SA_NOT_SUPPORTED || 19693663Sdougm ret == SA_SYSTEM_ERR || force) { 19703034Sdougm ret = sa_remove_share(share); 19713034Sdougm } 19723034Sdougm if (ret == SA_OK) 19733910Sdougm ret = sa_update_config(handle); 19743034Sdougm } 19753034Sdougm if (ret != SA_OK) { 19763034Sdougm (void) printf(gettext("Could not remove share:" 19773034Sdougm " %s\n"), 19783034Sdougm sa_errorstr(ret)); 19793034Sdougm } 19803034Sdougm } else if (ret == SA_OK) { 19813034Sdougm char *pname; 19823034Sdougm pname = sa_get_group_attr(group, "name"); 19833034Sdougm if (pname != NULL) { 19843034Sdougm auth = check_authorizations(pname, flags); 19853034Sdougm sa_free_attr_string(pname); 19863034Sdougm } 19873034Sdougm if (!auth && verbose) { 19883034Sdougm (void) printf(gettext("Command would fail: %s\n"), 19893034Sdougm sa_errorstr(SA_NO_PERMISSION)); 19903034Sdougm } 19913034Sdougm } 19923034Sdougm } 19933034Sdougm } 19943034Sdougm } 19953034Sdougm return (ret); 19963034Sdougm } 19973034Sdougm 19983034Sdougm /* 19993034Sdougm * sa_set_share(flags, argc, argv) 20003034Sdougm * 20013034Sdougm * implements set-share subcommand. 20023034Sdougm */ 20033034Sdougm 20043034Sdougm int 20053910Sdougm sa_set_share(sa_handle_t handle, int flags, int argc, char *argv[]) 20063034Sdougm { 20073034Sdougm int dryrun = 0; 20083034Sdougm int c; 20093034Sdougm int ret = SA_OK; 20103034Sdougm sa_group_t group, sharegroup; 20113034Sdougm sa_share_t share; 20123034Sdougm char *sharepath = NULL; 20133034Sdougm char *description = NULL; 20143034Sdougm char *resource = NULL; 20153034Sdougm int auth; 20163034Sdougm int verbose = 0; 20173034Sdougm 20183034Sdougm while ((c = getopt(argc, argv, "?hnd:r:s:")) != EOF) { 20193034Sdougm switch (c) { 20203034Sdougm case 'n': 20213034Sdougm dryrun++; 20223034Sdougm break; 20233034Sdougm case 'd': 20243034Sdougm description = optarg; 20253034Sdougm break; 20263034Sdougm case 'r': 20273034Sdougm resource = optarg; 20283034Sdougm break; 20293034Sdougm case 'v': 20303034Sdougm verbose++; 20313034Sdougm break; 20323034Sdougm case 's': 20333034Sdougm /* 20343034Sdougm * save share path into group. Currently limit 20353034Sdougm * to one share per command. 20363034Sdougm */ 20373034Sdougm if (sharepath != NULL) { 20383034Sdougm (void) printf(gettext("Updating multiple shares not" 20393034Sdougm "supported\n")); 20403034Sdougm return (SA_BAD_PATH); 20413034Sdougm } 20423034Sdougm sharepath = optarg; 20433034Sdougm break; 20443034Sdougm default: 20453034Sdougm case 'h': 20463034Sdougm case '?': 20473034Sdougm (void) printf(gettext("usage: %s\n"), 20483034Sdougm sa_get_usage(USAGE_SET_SHARE)); 20493034Sdougm return (SA_OK); 20503034Sdougm } 20513034Sdougm } 20523034Sdougm if (optind >= argc || sharepath == NULL) { 20533034Sdougm if (sharepath == NULL) { 20543034Sdougm (void) printf(gettext("usage: %s\n"), 20553034Sdougm sa_get_usage(USAGE_SET_SHARE)); 20563034Sdougm (void) printf(gettext("\tgroup must be specified\n")); 20573034Sdougm ret = SA_BAD_PATH; 20583034Sdougm } else { 20593034Sdougm ret = SA_OK; 20603034Sdougm } 20613034Sdougm } 20623034Sdougm if ((optind + 1) < argc) { 20633034Sdougm (void) printf(gettext("usage: %s\n"), 20643034Sdougm sa_get_usage(USAGE_SET_SHARE)); 20653034Sdougm (void) printf(gettext("\tExtraneous group(s) at end\n")); 20663034Sdougm ret = SA_SYNTAX_ERR; 20673034Sdougm } 20683034Sdougm if (ret == SA_OK) { 20693034Sdougm char *groupname; 20703034Sdougm if (optind < argc) { 20713034Sdougm groupname = argv[optind]; 20723910Sdougm group = sa_get_group(handle, groupname); 20733034Sdougm } else { 20743034Sdougm group = NULL; 20753034Sdougm groupname = NULL; 20763034Sdougm } 20773910Sdougm share = sa_find_share(handle, sharepath); 20783034Sdougm if (share != NULL) { 20793034Sdougm sharegroup = sa_get_parent_group(share); 20803034Sdougm if (group != NULL && group != sharegroup) { 20813034Sdougm (void) printf(gettext("Group \"%s\" does not contain " 20823034Sdougm "share %s\n"), 20833034Sdougm argv[optind], sharepath); 20843034Sdougm ret = SA_BAD_PATH; 20853034Sdougm } else { 20863034Sdougm int delgroupname = 0; 20873034Sdougm if (groupname == NULL) { 20883034Sdougm groupname = sa_get_group_attr(sharegroup, "name"); 20893034Sdougm delgroupname = 1; 20903034Sdougm } 20913034Sdougm if (groupname != NULL) { 20923034Sdougm auth = check_authorizations(groupname, flags); 20933034Sdougm if (delgroupname) { 20943034Sdougm sa_free_attr_string(groupname); 20953034Sdougm groupname = NULL; 20963034Sdougm } 20973034Sdougm } else { 20983034Sdougm ret = SA_NO_MEMORY; 20993034Sdougm } 21003034Sdougm if (resource != NULL) { 21013034Sdougm if (strpbrk(resource, " \t/") == NULL) { 21023034Sdougm if (!dryrun) { 21033034Sdougm ret = sa_set_share_attr(share, "resource", 21043034Sdougm resource); 21053034Sdougm } else { 21063034Sdougm sa_share_t resshare; 21073034Sdougm resshare = sa_get_resource(sharegroup, 21083034Sdougm resource); 21093034Sdougm if (resshare != NULL && resshare != share) 21103034Sdougm ret = SA_DUPLICATE_NAME; 21113034Sdougm } 21123034Sdougm } else { 21133034Sdougm ret = SA_BAD_PATH; 21143034Sdougm (void) printf(gettext("Resource must not contain " 21153034Sdougm "white space or '/'\n")); 21163034Sdougm } 21173034Sdougm } 21183034Sdougm if (ret == SA_OK && description != NULL) { 21193034Sdougm ret = sa_set_share_description(share, description); 21203034Sdougm } 21213034Sdougm } 21223034Sdougm if (!dryrun && ret == SA_OK) { 21233910Sdougm ret = sa_update_config(handle); 21243034Sdougm } 21253034Sdougm switch (ret) { 21263034Sdougm case SA_DUPLICATE_NAME: 21273034Sdougm (void) printf(gettext("Resource name in use: %s\n"), 21283034Sdougm resource); 21293034Sdougm break; 21303034Sdougm default: 21313034Sdougm (void) printf(gettext("Could not set attribute: %s\n"), 21323034Sdougm sa_errorstr(ret)); 21333034Sdougm break; 21343034Sdougm case SA_OK: 21353034Sdougm if (dryrun && !auth && verbose) { 21363034Sdougm (void) printf(gettext("Command would fail: %s\n"), 21373034Sdougm sa_errorstr(SA_NO_PERMISSION)); 21383034Sdougm } 21393034Sdougm break; 21403034Sdougm } 21413034Sdougm } else { 21423034Sdougm (void) printf(gettext("Share path \"%s\" not found\n"), 21433034Sdougm sharepath); 21443034Sdougm ret = SA_NO_SUCH_PATH; 21453034Sdougm } 21463034Sdougm } 21473034Sdougm return (ret); 21483034Sdougm } 21493034Sdougm 21503034Sdougm /* 21513034Sdougm * add_security(group, sectype, optlist, proto, *err) 21523034Sdougm * 21533034Sdougm * Helper function to add a security option (named optionset) to the 21543034Sdougm * group. 21553034Sdougm */ 21563034Sdougm 21573034Sdougm static int 21583034Sdougm add_security(sa_group_t group, char *sectype, 21593034Sdougm struct options *optlist, char *proto, int *err) 21603034Sdougm { 21613034Sdougm sa_security_t security; 21623034Sdougm int ret = SA_OK; 21633034Sdougm int result = 0; 21643034Sdougm 21653034Sdougm sectype = sa_proto_space_alias(proto, sectype); 21663034Sdougm security = sa_get_security(group, sectype, proto); 21673034Sdougm if (security == NULL) { 21683034Sdougm security = sa_create_security(group, sectype, proto); 21693034Sdougm } 21703034Sdougm if (sectype != NULL) 21713034Sdougm sa_free_attr_string(sectype); 21723034Sdougm if (security != NULL) { 21733034Sdougm while (optlist != NULL) { 21743034Sdougm sa_property_t prop; 21753034Sdougm prop = sa_get_property(security, optlist->optname); 21763034Sdougm if (prop == NULL) { 21773034Sdougm /* 21783034Sdougm * add the property, but only if it is 21793034Sdougm * a non-NULL or non-zero length value 21803034Sdougm */ 21813034Sdougm if (optlist->optvalue != NULL) { 21823034Sdougm prop = sa_create_property(optlist->optname, 21833034Sdougm optlist->optvalue); 21843034Sdougm if (prop != NULL) { 21853034Sdougm ret = sa_valid_property(security, proto, prop); 21863034Sdougm if (ret != SA_OK) { 21873034Sdougm (void) sa_remove_property(prop); 21883034Sdougm (void) printf(gettext("Could not add " 21893034Sdougm "property %s: %s\n"), 21903034Sdougm optlist->optname, 21913034Sdougm sa_errorstr(ret)); 21923034Sdougm } 21933034Sdougm if (ret == SA_OK) { 21943034Sdougm ret = sa_add_property(security, prop); 21953034Sdougm if (ret != SA_OK) { 21963034Sdougm (void) printf(gettext("Could not add " 21973034Sdougm "property (%s=%s): %s\n"), 21983034Sdougm optlist->optname, 21993034Sdougm optlist->optvalue, 22003034Sdougm sa_errorstr(ret)); 22013034Sdougm } else { 22023034Sdougm result = 1; 22033034Sdougm } 22043034Sdougm } 22053034Sdougm } 22063034Sdougm } 22073034Sdougm } else { 22083034Sdougm ret = sa_update_property(prop, optlist->optvalue); 22093034Sdougm result = 1; /* should check if really changed */ 22103034Sdougm } 22113034Sdougm optlist = optlist->next; 22123034Sdougm } 22133034Sdougm /* 22143034Sdougm * when done, properties may have all been removed but 22153034Sdougm * we need to keep the security type itself until 22163034Sdougm * explicitly removed. 22173034Sdougm */ 22183034Sdougm if (result) 22193034Sdougm ret = sa_commit_properties(security, 0); 22203034Sdougm } 22213034Sdougm *err = ret; 22223034Sdougm return (result); 22233034Sdougm } 22243034Sdougm 22253034Sdougm /* 22263034Sdougm * basic_set(groupname, optlist, protocol, sharepath, dryrun) 22273034Sdougm * 22283034Sdougm * This function implements "set" when a name space (-S) is not 22293034Sdougm * specified. It is a basic set. Options and other CLI parsing has 22303034Sdougm * already been done. 22313034Sdougm */ 22323034Sdougm 22333034Sdougm static int 22343910Sdougm basic_set(sa_handle_t handle, char *groupname, struct options *optlist, 22353910Sdougm char *protocol, char *sharepath, int dryrun) 22363034Sdougm { 22373034Sdougm sa_group_t group; 22383034Sdougm int ret = SA_OK; 22393034Sdougm int change = 0; 22403034Sdougm struct list *worklist = NULL; 22413034Sdougm 22423910Sdougm group = sa_get_group(handle, groupname); 22433034Sdougm if (group != NULL) { 22443034Sdougm sa_share_t share = NULL; 22453034Sdougm if (sharepath != NULL) { 22463034Sdougm share = sa_get_share(group, sharepath); 22473034Sdougm if (share == NULL) { 22483034Sdougm (void) printf(gettext("Share does not exist in group %s\n"), 22493034Sdougm groupname, sharepath); 22503034Sdougm ret = SA_NO_SUCH_PATH; 22513034Sdougm } 22523034Sdougm } 22533034Sdougm if (ret == SA_OK) { 22543034Sdougm /* group must exist */ 22553034Sdougm ret = valid_options(optlist, protocol, 22563034Sdougm share == NULL ? group : share, NULL); 22573034Sdougm if (ret == SA_OK && !dryrun) { 22583034Sdougm if (share != NULL) 22593034Sdougm change |= add_optionset(share, optlist, protocol, 22603034Sdougm &ret); 22613034Sdougm else 22623034Sdougm change |= add_optionset(group, optlist, protocol, 22633034Sdougm &ret); 22643034Sdougm if (ret == SA_OK && change) { 22653034Sdougm worklist = add_list(worklist, group, share); 22663034Sdougm } 22673034Sdougm } 22683034Sdougm } 22693034Sdougm free_opt(optlist); 22703034Sdougm } else { 22713034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 22723034Sdougm ret = SA_NO_SUCH_GROUP; 22733034Sdougm } 22743034Sdougm /* 22753034Sdougm * we have a group and potentially legal additions 22763034Sdougm */ 22773034Sdougm 22783034Sdougm /* commit to configuration if not a dryrun */ 22793034Sdougm if (!dryrun && ret == SA_OK) { 22803034Sdougm if (change && worklist != NULL) { 22813034Sdougm /* properties changed, so update all shares */ 22823910Sdougm (void) enable_all_groups(handle, worklist, 0, 0, protocol); 22833034Sdougm } 22843034Sdougm } 22853034Sdougm if (worklist != NULL) 22863034Sdougm free_list(worklist); 22873034Sdougm return (ret); 22883034Sdougm } 22893034Sdougm 22903034Sdougm /* 22913034Sdougm * space_set(groupname, optlist, protocol, sharepath, dryrun) 22923034Sdougm * 22933034Sdougm * This function implements "set" when a name space (-S) is 22943034Sdougm * specified. It is a namespace set. Options and other CLI parsing has 22953034Sdougm * already been done. 22963034Sdougm */ 22973034Sdougm 22983034Sdougm static int 22993910Sdougm space_set(sa_handle_t handle, char *groupname, struct options *optlist, 23003910Sdougm char *protocol, char *sharepath, int dryrun, char *sectype) 23013034Sdougm { 23023034Sdougm sa_group_t group; 23033034Sdougm int ret = SA_OK; 23043034Sdougm int change = 0; 23053034Sdougm struct list *worklist = NULL; 23063034Sdougm 23073034Sdougm /* 23083034Sdougm * make sure protcol and sectype are valid 23093034Sdougm */ 23103034Sdougm 23113034Sdougm if (sa_proto_valid_space(protocol, sectype) == 0) { 23123034Sdougm (void) printf(gettext("Option space \"%s\" not valid " 23133034Sdougm "for protocol.\n"), 23143034Sdougm sectype); 23153034Sdougm return (SA_INVALID_SECURITY); 23163034Sdougm } 23173034Sdougm 23183910Sdougm group = sa_get_group(handle, groupname); 23193034Sdougm if (group != NULL) { 23203034Sdougm sa_share_t share = NULL; 23213034Sdougm if (sharepath != NULL) { 23223034Sdougm share = sa_get_share(group, sharepath); 23233034Sdougm if (share == NULL) { 23243034Sdougm (void) printf(gettext("Share does not exist in group %s\n"), 23253034Sdougm groupname, sharepath); 23263034Sdougm ret = SA_NO_SUCH_PATH; 23273034Sdougm } 23283034Sdougm } 23293034Sdougm if (ret == SA_OK) { 23303034Sdougm /* group must exist */ 23313034Sdougm ret = valid_options(optlist, protocol, 23323034Sdougm share == NULL ? group : share, sectype); 23333034Sdougm if (ret == SA_OK && !dryrun) { 23343034Sdougm if (share != NULL) 23353034Sdougm change = add_security(share, sectype, optlist, 23363034Sdougm protocol, 23373034Sdougm &ret); 23383034Sdougm else 23393034Sdougm change = add_security(group, sectype, optlist, 23403034Sdougm protocol, 23413034Sdougm &ret); 23423034Sdougm if (ret != SA_OK) 23433034Sdougm (void) printf(gettext("Could not set property: %s\n"), 23443034Sdougm sa_errorstr(ret)); 23453034Sdougm } 23463034Sdougm if (ret == SA_OK && change) 23473034Sdougm worklist = add_list(worklist, group, share); 23483034Sdougm } 23493034Sdougm free_opt(optlist); 23503034Sdougm } else { 23513034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 23523034Sdougm ret = SA_NO_SUCH_GROUP; 23533034Sdougm } 23543034Sdougm /* 23553034Sdougm * we have a group and potentially legal additions 23563034Sdougm */ 23573034Sdougm 23583034Sdougm /* commit to configuration if not a dryrun */ 23593034Sdougm if (!dryrun && ret == 0) { 23603034Sdougm if (change && worklist != NULL) { 23613034Sdougm /* properties changed, so update all shares */ 23623910Sdougm (void) enable_all_groups(handle, worklist, 0, 0, protocol); 23633034Sdougm } 23643910Sdougm ret = sa_update_config(handle); 23653034Sdougm } 23663034Sdougm if (worklist != NULL) 23673034Sdougm free_list(worklist); 23683034Sdougm return (ret); 23693034Sdougm } 23703034Sdougm 23713034Sdougm /* 23723034Sdougm * sa_set(flags, argc, argv) 23733034Sdougm * 23743034Sdougm * Implements the set subcommand. It keys off of -S to determine which 23753034Sdougm * set of operations to actually do. 23763034Sdougm */ 23773034Sdougm 23783034Sdougm int 23793910Sdougm sa_set(sa_handle_t handle, int flags, int argc, char *argv[]) 23803034Sdougm { 23813034Sdougm char *groupname; 23823034Sdougm int verbose = 0; 23833034Sdougm int dryrun = 0; 23843034Sdougm int c; 23853034Sdougm char *protocol = NULL; 23863034Sdougm int ret = SA_OK; 23873034Sdougm struct options *optlist = NULL; 23883034Sdougm char *sharepath = NULL; 23893034Sdougm char *optset = NULL; 23903034Sdougm int auth; 23913034Sdougm 23923034Sdougm while ((c = getopt(argc, argv, "?hvnP:p:s:S:")) != EOF) { 23933034Sdougm switch (c) { 23943034Sdougm case 'v': 23953034Sdougm verbose++; 23963034Sdougm break; 23973034Sdougm case 'n': 23983034Sdougm dryrun++; 23993034Sdougm break; 24003034Sdougm case 'P': 24013034Sdougm protocol = optarg; 24023034Sdougm if (!sa_valid_protocol(protocol)) { 24033034Sdougm (void) printf(gettext("Invalid protocol specified:" 24043034Sdougm "%s\n"), 24053034Sdougm protocol); 24063034Sdougm return (SA_INVALID_PROTOCOL); 24073034Sdougm } 24083034Sdougm break; 24093034Sdougm case 'p': 24103034Sdougm ret = add_opt(&optlist, optarg, 0); 24113034Sdougm switch (ret) { 24123034Sdougm case OPT_ADD_SYNTAX: 24133034Sdougm (void) printf(gettext("Property syntax error: %s\n"), 24143034Sdougm optarg); 24153034Sdougm return (SA_SYNTAX_ERR); 24163034Sdougm case OPT_ADD_MEMORY: 24173034Sdougm (void) printf(gettext("No memory to set property: %s\n"), 24183034Sdougm optarg); 24193034Sdougm return (SA_NO_MEMORY); 24203034Sdougm default: 24213034Sdougm break; 24223034Sdougm } 24233034Sdougm break; 24243034Sdougm case 's': 24253034Sdougm sharepath = optarg; 24263034Sdougm break; 24273034Sdougm case 'S': 24283034Sdougm optset = optarg; 24293034Sdougm break; 24303034Sdougm default: 24313034Sdougm case 'h': 24323034Sdougm case '?': 24333034Sdougm (void) printf(gettext("usage: %s\n"), 24343034Sdougm sa_get_usage(USAGE_SET)); 24353034Sdougm return (SA_OK); 24363034Sdougm } 24373034Sdougm } 24383034Sdougm 24393034Sdougm if (optlist != NULL) 24403034Sdougm ret = chk_opt(optlist, optset != NULL, protocol); 24413034Sdougm 24423034Sdougm if (optind >= argc || (optlist == NULL && optset == NULL) || 24433034Sdougm protocol == NULL || 24443034Sdougm ret != OPT_ADD_OK) { 24453034Sdougm char *sep = "\t"; 24463034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET)); 24473034Sdougm if (optind >= argc) { 24483034Sdougm (void) printf(gettext("%sgroup must be specified"), sep); 24493034Sdougm sep = ", "; 24503034Sdougm } 24513034Sdougm if (optlist == NULL) { 24523034Sdougm (void) printf(gettext("%sat least one property must be" 24533034Sdougm " specified"), sep); 24543034Sdougm sep = ", "; 24553034Sdougm } 24563034Sdougm if (protocol == NULL) { 24573034Sdougm (void) printf(gettext("%sprotocol must be specified"), sep); 24583034Sdougm sep = ", "; 24593034Sdougm } 24603034Sdougm (void) printf("\n"); 24613034Sdougm ret = SA_SYNTAX_ERR; 24623034Sdougm } else { 24633034Sdougm /* 24643034Sdougm * if a group already exists, we can only add a new 24653034Sdougm * protocol to it and not create a new one or add the 24663034Sdougm * same protocol again. 24673034Sdougm */ 24683034Sdougm 24693034Sdougm groupname = argv[optind]; 24703034Sdougm auth = check_authorizations(groupname, flags); 24713034Sdougm if (optset == NULL) 24723910Sdougm ret = basic_set(handle, groupname, optlist, protocol, 24733034Sdougm sharepath, dryrun); 24743034Sdougm else 24753910Sdougm ret = space_set(handle, groupname, optlist, protocol, 24763034Sdougm sharepath, dryrun, optset); 24773034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 24783034Sdougm (void) printf(gettext("Command would fail: %s\n"), 24793034Sdougm sa_errorstr(SA_NO_PERMISSION)); 24803034Sdougm } 24813034Sdougm } 24823034Sdougm return (ret); 24833034Sdougm } 24843034Sdougm 24853034Sdougm /* 24863034Sdougm * remove_options(group, optlist, proto, *err) 24873034Sdougm * 24883034Sdougm * helper function to actually remove options from a group after all 24893034Sdougm * preprocessing is done. 24903034Sdougm */ 24913034Sdougm 24923034Sdougm static int 24933034Sdougm remove_options(sa_group_t group, struct options *optlist, 24943034Sdougm char *proto, int *err) 24953034Sdougm { 24963034Sdougm struct options *cur; 24973034Sdougm sa_optionset_t optionset; 24983034Sdougm sa_property_t prop; 24993034Sdougm int change = 0; 25003034Sdougm int ret = SA_OK; 25013034Sdougm 25023034Sdougm optionset = sa_get_optionset(group, proto); 25033034Sdougm if (optionset != NULL) { 25043034Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 25053034Sdougm prop = sa_get_property(optionset, cur->optname); 25063034Sdougm if (prop != NULL) { 25073034Sdougm ret = sa_remove_property(prop); 25083034Sdougm if (ret != SA_OK) 25093034Sdougm break; 25103034Sdougm change = 1; 25113034Sdougm } 25123034Sdougm } 25133034Sdougm } 25143034Sdougm if (ret == SA_OK && change) 25153034Sdougm ret = sa_commit_properties(optionset, 0); 25163034Sdougm 25173034Sdougm if (err != NULL) 25183034Sdougm *err = ret; 25193034Sdougm return (change); 25203034Sdougm } 25213034Sdougm 25223034Sdougm /* 25233034Sdougm * valid_unset(group, optlist, proto) 25243034Sdougm * 25253034Sdougm * Sanity check the optlist to make sure they can be removed. Issue an 25263034Sdougm * error if a property doesn't exist. 25273034Sdougm */ 25283034Sdougm 25293034Sdougm static int 25303034Sdougm valid_unset(sa_group_t group, struct options *optlist, char *proto) 25313034Sdougm { 25323034Sdougm struct options *cur; 25333034Sdougm sa_optionset_t optionset; 25343034Sdougm sa_property_t prop; 25353034Sdougm int ret = SA_OK; 25363034Sdougm 25373034Sdougm optionset = sa_get_optionset(group, proto); 25383034Sdougm if (optionset != NULL) { 25393034Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 25403034Sdougm prop = sa_get_property(optionset, cur->optname); 25413034Sdougm if (prop == NULL) { 25423034Sdougm (void) printf(gettext("Could not unset property %s:" 25433034Sdougm " not set\n"), 25443034Sdougm cur->optname); 25453034Sdougm ret = SA_NO_SUCH_PROP; 25463034Sdougm } 25473034Sdougm } 25483034Sdougm } 25493034Sdougm return (ret); 25503034Sdougm } 25513034Sdougm 25523034Sdougm /* 25533034Sdougm * valid_unset_security(group, optlist, proto) 25543034Sdougm * 25553034Sdougm * Sanity check the optlist to make sure they can be removed. Issue an 25563034Sdougm * error if a property doesn't exist. 25573034Sdougm */ 25583034Sdougm 25593034Sdougm static int 25603034Sdougm valid_unset_security(sa_group_t group, struct options *optlist, char *proto, 25613034Sdougm char *sectype) 25623034Sdougm { 25633034Sdougm struct options *cur; 25643034Sdougm sa_security_t security; 25653034Sdougm sa_property_t prop; 25663034Sdougm int ret = SA_OK; 25673034Sdougm char *sec; 25683034Sdougm 25693034Sdougm sec = sa_proto_space_alias(proto, sectype); 25703034Sdougm security = sa_get_security(group, sec, proto); 25713034Sdougm if (security != NULL) { 25723034Sdougm for (cur = optlist; cur != NULL; cur = cur->next) { 25733034Sdougm prop = sa_get_property(security, cur->optname); 25743034Sdougm if (prop == NULL) { 25753034Sdougm (void) printf(gettext("Could not unset property %s:" 25763034Sdougm " not set\n"), 25773034Sdougm cur->optname); 25783034Sdougm ret = SA_NO_SUCH_PROP; 25793034Sdougm } 25803034Sdougm } 25813034Sdougm } else { 25823034Sdougm (void) printf(gettext("Could not unset %s: space not defined\n"), 25833034Sdougm sectype); 25843034Sdougm ret = SA_NO_SUCH_SECURITY; 25853034Sdougm } 25863034Sdougm if (sec != NULL) 25873034Sdougm sa_free_attr_string(sec); 25883034Sdougm return (ret); 25893034Sdougm } 25903034Sdougm 25913034Sdougm /* 25923034Sdougm * remove_security(group, optlist, proto) 25933034Sdougm * 25943034Sdougm * Remove the properties since they were checked as valid. 25953034Sdougm */ 25963034Sdougm 25973034Sdougm static int 25983034Sdougm remove_security(sa_group_t group, char *sectype, 25993034Sdougm struct options *optlist, char *proto, int *err) 26003034Sdougm { 26013034Sdougm sa_security_t security; 26023034Sdougm int ret = SA_OK; 26033034Sdougm int change = 0; 26043034Sdougm 26053034Sdougm sectype = sa_proto_space_alias(proto, sectype); 26063034Sdougm security = sa_get_security(group, sectype, proto); 26073034Sdougm if (sectype != NULL) 26083034Sdougm sa_free_attr_string(sectype); 26093034Sdougm 26103034Sdougm if (security != NULL) { 26113034Sdougm while (optlist != NULL) { 26123034Sdougm sa_property_t prop; 26133034Sdougm prop = sa_get_property(security, optlist->optname); 26143034Sdougm if (prop != NULL) { 26153034Sdougm ret = sa_remove_property(prop); 26163034Sdougm if (ret != SA_OK) 26173034Sdougm break; 26183034Sdougm change = 1; 26193034Sdougm } 26203034Sdougm optlist = optlist->next; 26213034Sdougm } 26223034Sdougm /* 26233034Sdougm * when done, properties may have all been removed but 26243034Sdougm * we need to keep the security type itself until 26253034Sdougm * explicitly removed. 26263034Sdougm */ 26273034Sdougm if (ret == SA_OK && change) 26283034Sdougm ret = sa_commit_properties(security, 0); 26293034Sdougm } else { 26303034Sdougm ret = SA_NO_SUCH_PROP; 26313034Sdougm } 26323034Sdougm if (err != NULL) 26333034Sdougm *err = ret; 26343034Sdougm return (change); 26353034Sdougm } 26363034Sdougm 26373034Sdougm /* 26383034Sdougm * basic_unset(groupname, optlist, protocol, sharepath, dryrun) 26393034Sdougm * 26403034Sdougm * unset non-named optionset properties. 26413034Sdougm */ 26423034Sdougm 26433034Sdougm static int 26443910Sdougm basic_unset(sa_handle_t handle, char *groupname, struct options *optlist, 26453910Sdougm char *protocol, char *sharepath, int dryrun) 26463034Sdougm { 26473034Sdougm sa_group_t group; 26483034Sdougm int ret = SA_OK; 26493034Sdougm int change = 0; 26503034Sdougm struct list *worklist = NULL; 26513034Sdougm 26523910Sdougm group = sa_get_group(handle, groupname); 26533034Sdougm if (group != NULL) { 26543034Sdougm sa_share_t share = NULL; 26553034Sdougm if (sharepath != NULL) { 26563034Sdougm share = sa_get_share(group, sharepath); 26573034Sdougm if (share == NULL) { 26583034Sdougm (void) printf(gettext("Share does not exist in group %s\n"), 26593034Sdougm groupname, sharepath); 26603034Sdougm ret = SA_NO_SUCH_PATH; 26613034Sdougm } 26623034Sdougm } 26633034Sdougm if (ret == SA_OK) { 26643034Sdougm /* group must exist */ 26653034Sdougm ret = valid_unset(share != NULL ? share : group, 26663034Sdougm optlist, protocol); 26673034Sdougm if (ret == SA_OK && !dryrun) { 26683034Sdougm if (share != NULL) { 26693034Sdougm sa_optionset_t optionset; 26703034Sdougm sa_property_t prop; 26713034Sdougm change |= remove_options(share, optlist, protocol, 26723034Sdougm &ret); 26733034Sdougm /* if a share optionset is empty, remove it */ 26743034Sdougm optionset = sa_get_optionset((sa_share_t)share, 26753034Sdougm protocol); 26763034Sdougm if (optionset != NULL) { 26773034Sdougm prop = sa_get_property(optionset, NULL); 26783034Sdougm if (prop == NULL) 26793034Sdougm (void) sa_destroy_optionset(optionset); 26803034Sdougm } 26813034Sdougm } else { 26823034Sdougm change |= remove_options(group, optlist, protocol, 26833034Sdougm &ret); 26843034Sdougm } 26853034Sdougm if (ret == SA_OK && change) 26863034Sdougm worklist = add_list(worklist, group, share); 26873034Sdougm if (ret != SA_OK) 26883034Sdougm (void) printf(gettext("Could not remove properties:" 26893034Sdougm "%s\n"), 26903034Sdougm sa_errorstr(ret)); 26913034Sdougm } 26923034Sdougm } else { 26933034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 26943034Sdougm ret = SA_NO_SUCH_GROUP; 26953034Sdougm } 26963034Sdougm free_opt(optlist); 26973034Sdougm } 26983034Sdougm 26993034Sdougm /* 27003034Sdougm * we have a group and potentially legal additions 27013034Sdougm */ 27023034Sdougm /* commit to configuration if not a dryrun */ 27033034Sdougm if (!dryrun && ret == SA_OK) { 27043034Sdougm if (change && worklist != NULL) { 27053034Sdougm /* properties changed, so update all shares */ 27063910Sdougm (void) enable_all_groups(handle, worklist, 0, 0, protocol); 27073034Sdougm } 27083034Sdougm } 27093034Sdougm if (worklist != NULL) 27103034Sdougm free_list(worklist); 27113034Sdougm return (ret); 27123034Sdougm } 27133034Sdougm 27143034Sdougm /* 27153034Sdougm * space_unset(groupname, optlist, protocol, sharepath, dryrun) 27163034Sdougm * 27173034Sdougm * unset named optionset properties. 27183034Sdougm */ 27193034Sdougm static int 27203910Sdougm space_unset(sa_handle_t handle, char *groupname, struct options *optlist, 27213910Sdougm char *protocol, char *sharepath, int dryrun, char *sectype) 27223034Sdougm { 27233034Sdougm sa_group_t group; 27243034Sdougm int ret = SA_OK; 27253034Sdougm int change = 0; 27263034Sdougm struct list *worklist = NULL; 27273034Sdougm 27283910Sdougm group = sa_get_group(handle, groupname); 27293034Sdougm if (group != NULL) { 27303034Sdougm sa_share_t share = NULL; 27313034Sdougm if (sharepath != NULL) { 27323034Sdougm share = sa_get_share(group, sharepath); 27333034Sdougm if (share == NULL) { 27343034Sdougm (void) printf(gettext("Share does not exist in group %s\n"), 27353034Sdougm groupname, sharepath); 27363034Sdougm ret = SA_NO_SUCH_PATH; 27373034Sdougm } 27383034Sdougm } 27393034Sdougm if (ret == SA_OK) { 27403034Sdougm ret = valid_unset_security(share != NULL ? share : group, 27413034Sdougm optlist, protocol, sectype); 27423034Sdougm if (ret == SA_OK && !dryrun) { 27433034Sdougm if (optlist != NULL) { 27443034Sdougm if (share != NULL) { 27453034Sdougm sa_security_t optionset; 27463034Sdougm sa_property_t prop; 27473034Sdougm change = remove_security(share, sectype, 27483034Sdougm optlist, protocol, 27493034Sdougm &ret); 27503034Sdougm /* if a share security is empty, remove it */ 27513034Sdougm optionset = sa_get_security((sa_group_t)share, 27523034Sdougm sectype, 27533034Sdougm protocol); 27543034Sdougm if (optionset != NULL) { 27553034Sdougm prop = sa_get_property(optionset, NULL); 27563034Sdougm if (prop == NULL) 27573034Sdougm ret = sa_destroy_security(optionset); 27583034Sdougm } 27593034Sdougm } else { 27603034Sdougm change = remove_security(group, sectype, 27613034Sdougm optlist, protocol, 27623034Sdougm &ret); 27633034Sdougm } 27643034Sdougm } else { 27653034Sdougm sa_security_t security; 27663034Sdougm char *sec; 27673034Sdougm sec = sa_proto_space_alias(protocol, sectype); 27683034Sdougm security = sa_get_security(group, sec, protocol); 27693034Sdougm if (sec != NULL) 27703034Sdougm sa_free_attr_string(sec); 27713034Sdougm if (security != NULL) { 27723034Sdougm ret = sa_destroy_security(security); 27733034Sdougm if (ret == SA_OK) 27743034Sdougm change = 1; 27753034Sdougm } else { 27763034Sdougm ret = SA_NO_SUCH_PROP; 27773034Sdougm } 27783034Sdougm } 27793034Sdougm if (ret != SA_OK) 27803034Sdougm (void) printf(gettext("Could not unset property: %s\n"), 27813034Sdougm sa_errorstr(ret)); 27823034Sdougm } 27833034Sdougm 27843034Sdougm if (ret == SA_OK && change) 27853034Sdougm worklist = add_list(worklist, group, 0); 27863034Sdougm } 27873034Sdougm } else { 27883034Sdougm (void) printf(gettext("Group \"%s\" not found\n"), groupname); 27893034Sdougm ret = SA_NO_SUCH_GROUP; 27903034Sdougm } 27913034Sdougm free_opt(optlist); 27923034Sdougm /* 27933034Sdougm * we have a group and potentially legal additions 27943034Sdougm */ 27953034Sdougm 27963034Sdougm /* commit to configuration if not a dryrun */ 27973034Sdougm if (!dryrun && ret == 0) { 27983034Sdougm if (change && worklist != NULL) { 27993034Sdougm /* properties changed, so update all shares */ 28003910Sdougm (void) enable_all_groups(handle, worklist, 0, 0, protocol); 28013034Sdougm } 28023910Sdougm ret = sa_update_config(handle); 28033034Sdougm } 28043034Sdougm if (worklist != NULL) 28053034Sdougm free_list(worklist); 28063034Sdougm return (ret); 28073034Sdougm } 28083034Sdougm 28093034Sdougm /* 28103034Sdougm * sa_unset(flags, argc, argv) 28113034Sdougm * 28123034Sdougm * implements the unset subcommand. Parsing done here and then basic 28133034Sdougm * or space versions of the real code are called. 28143034Sdougm */ 28153034Sdougm 28163034Sdougm int 28173910Sdougm sa_unset(sa_handle_t handle, int flags, int argc, char *argv[]) 28183034Sdougm { 28193034Sdougm char *groupname; 28203034Sdougm int verbose = 0; 28213034Sdougm int dryrun = 0; 28223034Sdougm int c; 28233034Sdougm char *protocol = NULL; 28243034Sdougm int ret = SA_OK; 28253034Sdougm struct options *optlist = NULL; 28263034Sdougm char *sharepath = NULL; 28273034Sdougm char *optset = NULL; 28283034Sdougm int auth; 28293034Sdougm 28303034Sdougm while ((c = getopt(argc, argv, "?hvnP:p:s:S:")) != EOF) { 28313034Sdougm switch (c) { 28323034Sdougm case 'v': 28333034Sdougm verbose++; 28343034Sdougm break; 28353034Sdougm case 'n': 28363034Sdougm dryrun++; 28373034Sdougm break; 28383034Sdougm case 'P': 28393034Sdougm protocol = optarg; 28403034Sdougm if (!sa_valid_protocol(protocol)) { 28413034Sdougm (void) printf(gettext("Invalid protocol specified: %s\n"), 28423034Sdougm protocol); 28433034Sdougm return (SA_INVALID_PROTOCOL); 28443034Sdougm } 28453034Sdougm break; 28463034Sdougm case 'p': 28473034Sdougm ret = add_opt(&optlist, optarg, 1); 28483034Sdougm switch (ret) { 28493034Sdougm case OPT_ADD_SYNTAX: 28503034Sdougm (void) printf(gettext("Property syntax error for " 28513034Sdougm "property %s\n"), 28523034Sdougm optarg); 28533034Sdougm return (SA_SYNTAX_ERR); 28543034Sdougm case OPT_ADD_PROPERTY: 28553034Sdougm (void) printf(gettext("Properties need to be set" 28563034Sdougm " with set command: %s\n"), 28573034Sdougm optarg); 28583034Sdougm return (SA_SYNTAX_ERR); 28593034Sdougm default: 28603034Sdougm break; 28613034Sdougm } 28623034Sdougm break; 28633034Sdougm case 's': 28643034Sdougm sharepath = optarg; 28653034Sdougm break; 28663034Sdougm case 'S': 28673034Sdougm optset = optarg; 28683034Sdougm break; 28693034Sdougm default: 28703034Sdougm case 'h': 28713034Sdougm case '?': 28723034Sdougm (void) printf(gettext("usage: %s\n"), 28733034Sdougm sa_get_usage(USAGE_UNSET)); 28743034Sdougm return (SA_OK); 28753034Sdougm } 28763034Sdougm } 28773034Sdougm 28783034Sdougm if (optlist != NULL) 28793034Sdougm ret = chk_opt(optlist, optset != NULL, protocol); 28803034Sdougm 28813034Sdougm if (optind >= argc || (optlist == NULL && optset == NULL) || 28823034Sdougm protocol == NULL) { 28833034Sdougm char *sep = "\t"; 28843034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_UNSET)); 28853034Sdougm if (optind >= argc) { 28863034Sdougm (void) printf(gettext("%sgroup must be specified"), sep); 28873034Sdougm sep = ", "; 28883034Sdougm } 28893034Sdougm if (optlist == NULL) { 28903034Sdougm (void) printf(gettext("%sat least one property must be " 28913034Sdougm "specified"), 28923034Sdougm sep); 28933034Sdougm sep = ", "; 28943034Sdougm } 28953034Sdougm if (protocol == NULL) { 28963034Sdougm (void) printf(gettext("%sprotocol must be specified"), sep); 28973034Sdougm sep = ", "; 28983034Sdougm } 28993034Sdougm (void) printf("\n"); 29003034Sdougm ret = SA_SYNTAX_ERR; 29013034Sdougm } else { 29023034Sdougm 29033034Sdougm /* 29043034Sdougm * if a group already exists, we can only add a new 29053034Sdougm * protocol to it and not create a new one or add the 29063034Sdougm * same protocol again. 29073034Sdougm */ 29083034Sdougm 29093034Sdougm groupname = argv[optind]; 29103034Sdougm auth = check_authorizations(groupname, flags); 29113034Sdougm if (optset == NULL) 29123910Sdougm ret = basic_unset(handle, groupname, optlist, protocol, 29133034Sdougm sharepath, dryrun); 29143034Sdougm else 29153910Sdougm ret = space_unset(handle, groupname, optlist, protocol, 29163034Sdougm sharepath, dryrun, optset); 29173034Sdougm 29183034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 29193034Sdougm (void) printf(gettext("Command would fail: %s\n"), 29203034Sdougm sa_errorstr(SA_NO_PERMISSION)); 29213034Sdougm } 29223034Sdougm } 29233034Sdougm return (ret); 29243034Sdougm } 29253034Sdougm 29263034Sdougm /* 29273034Sdougm * sa_enable_group(flags, argc, argv) 29283034Sdougm * 29293034Sdougm * Implements the enable subcommand 29303034Sdougm */ 29313034Sdougm 29323034Sdougm int 29333910Sdougm sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[]) 29343034Sdougm { 29353034Sdougm int verbose = 0; 29363034Sdougm int dryrun = 0; 29373034Sdougm int all = 0; 29383034Sdougm int c; 29393034Sdougm int ret = SA_OK; 29403034Sdougm char *protocol = NULL; 29413034Sdougm char *state; 29423034Sdougm struct list *worklist = NULL; 29433034Sdougm int auth = 1; 29443034Sdougm 29453034Sdougm while ((c = getopt(argc, argv, "?havnP:")) != EOF) { 29463034Sdougm switch (c) { 29473034Sdougm case 'a': 29483034Sdougm all = 1; 29493034Sdougm break; 29503034Sdougm case 'n': 29513034Sdougm dryrun++; 29523034Sdougm break; 29533034Sdougm case 'P': 29543034Sdougm protocol = optarg; 29553034Sdougm if (!sa_valid_protocol(protocol)) { 29563034Sdougm (void) printf(gettext("Invalid protocol specified: %s\n"), 29573034Sdougm protocol); 29583034Sdougm return (SA_INVALID_PROTOCOL); 29593034Sdougm } 29603034Sdougm break; 29613034Sdougm case 'v': 29623034Sdougm verbose++; 29633034Sdougm break; 29643034Sdougm default: 29653034Sdougm case 'h': 29663034Sdougm case '?': 29673034Sdougm (void) printf(gettext("usage: %s\n"), 29683034Sdougm sa_get_usage(USAGE_ENABLE)); 29693034Sdougm return (0); 29703034Sdougm } 29713034Sdougm } 29723034Sdougm 29733034Sdougm if (optind == argc && !all) { 29743034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_ENABLE)); 29753034Sdougm (void) printf(gettext("\tmust specify group\n")); 29763034Sdougm ret = SA_NO_SUCH_PATH; 29773034Sdougm } else { 29783034Sdougm sa_group_t group; 29793034Sdougm if (!all) { 29803034Sdougm while (optind < argc) { 29813910Sdougm group = sa_get_group(handle, argv[optind]); 29823034Sdougm if (group != NULL) { 29833034Sdougm auth &= check_authorizations(argv[optind], flags); 29843034Sdougm state = sa_get_group_attr(group, "state"); 29853034Sdougm if (state != NULL && 29863034Sdougm strcmp(state, "enabled") == 0) { 29873034Sdougm /* already enabled */ 29883034Sdougm if (verbose) 29893034Sdougm (void) printf(gettext("Group \"%s\" is already " 29903034Sdougm "enabled\n"), 29913034Sdougm argv[optind]); 29923034Sdougm ret = SA_BUSY; /* already enabled */ 29933034Sdougm } else { 29943034Sdougm worklist = add_list(worklist, group, 0); 29953034Sdougm if (verbose) 29963034Sdougm (void) printf(gettext("Enabling group " 29973034Sdougm "\"%s\"\n"), 29983034Sdougm argv[optind]); 29993034Sdougm } 30003034Sdougm if (state != NULL) 30013034Sdougm sa_free_attr_string(state); 30023034Sdougm } else { 30033034Sdougm ret = SA_NO_SUCH_GROUP; 30043034Sdougm } 30053034Sdougm optind++; 30063034Sdougm } 30073034Sdougm } else { 30083910Sdougm for (group = sa_get_group(handle, NULL); group != NULL; 30093034Sdougm group = sa_get_next_group(group)) { 30103034Sdougm worklist = add_list(worklist, group, 0); 30113034Sdougm } 30123034Sdougm } 30133034Sdougm if (!dryrun && ret == SA_OK) { 30143910Sdougm ret = enable_all_groups(handle, worklist, 1, 0, NULL); 30153034Sdougm } 30163034Sdougm if (ret != SA_OK && ret != SA_BUSY) 30173034Sdougm (void) printf(gettext("Could not enable group: %s\n"), 30183034Sdougm sa_errorstr(ret)); 30193034Sdougm if (ret == SA_BUSY) 30203034Sdougm ret = SA_OK; 30213034Sdougm } 30223034Sdougm if (worklist != NULL) 30233034Sdougm free_list(worklist); 30243034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 30253034Sdougm (void) printf(gettext("Command would fail: %s\n"), 30263034Sdougm sa_errorstr(SA_NO_PERMISSION)); 30273034Sdougm } 30283034Sdougm return (ret); 30293034Sdougm } 30303034Sdougm 30313034Sdougm /* 30323034Sdougm * disable_group(group, setstate) 30333034Sdougm * 30343034Sdougm * disable all the shares in the specified group honoring the setstate 30353034Sdougm * argument. This is a helper for disable_all_groups in order to 30363034Sdougm * simplify regular and subgroup (zfs) disabling. Group has already 30373034Sdougm * been checked for non-NULL. 30383034Sdougm */ 30393034Sdougm 30403034Sdougm static int 30413034Sdougm disable_group(sa_group_t group) 30423034Sdougm { 30433034Sdougm sa_share_t share; 30443034Sdougm int ret = SA_OK; 30453034Sdougm 30463034Sdougm for (share = sa_get_share(group, NULL); 30473034Sdougm share != NULL && ret == SA_OK; 30483034Sdougm share = sa_get_next_share(share)) { 30493034Sdougm ret = sa_disable_share(share, NULL); 30503034Sdougm if (ret == SA_NO_SUCH_PATH) { 30513034Sdougm /* 30523034Sdougm * this is OK since the path is gone. we can't 30533034Sdougm * re-share it anyway so no error. 30543034Sdougm */ 30553034Sdougm ret = SA_OK; 30563034Sdougm } 30573034Sdougm } 30583034Sdougm return (ret); 30593034Sdougm } 30603034Sdougm 30613034Sdougm 30623034Sdougm /* 30633034Sdougm * disable_all_groups(work, setstate) 30643034Sdougm * 30653034Sdougm * helper function that disables the shares in the list of groups 30663034Sdougm * provided. It optionally marks the group as disabled. Used by both 30673034Sdougm * enable and start subcommands. 30683034Sdougm */ 30693034Sdougm 30703034Sdougm static int 30713910Sdougm disable_all_groups(sa_handle_t handle, struct list *work, int setstate) 30723034Sdougm { 30733034Sdougm int ret = SA_OK; 30743034Sdougm sa_group_t subgroup, group; 30753034Sdougm 30763034Sdougm while (work != NULL && ret == SA_OK) { 30773034Sdougm group = (sa_group_t)work->item; 30783034Sdougm if (setstate) 30793034Sdougm ret = sa_set_group_attr(group, "state", "disabled"); 30803034Sdougm if (ret == SA_OK) { 30813034Sdougm char *name; 30823034Sdougm name = sa_get_group_attr(group, "name"); 30833034Sdougm if (name != NULL && strcmp(name, "zfs") == 0) { 30843034Sdougm /* need to get the sub-groups for stopping */ 30853034Sdougm for (subgroup = sa_get_sub_group(group); subgroup != NULL; 30863034Sdougm subgroup = sa_get_next_group(subgroup)) { 30873034Sdougm ret = disable_group(subgroup); 30883034Sdougm } 30893034Sdougm } else { 30903034Sdougm ret = disable_group(group); 30913034Sdougm } 30923034Sdougm /* 30933034Sdougm * we don't want to "disable" since it won't come 30943034Sdougm * up after a reboot. The SMF framework should do 30953034Sdougm * the right thing. On enable we do want to do 30963034Sdougm * something. 30973034Sdougm */ 30983034Sdougm } 30993034Sdougm work = work->next; 31003034Sdougm } 31013034Sdougm if (ret == SA_OK) 31023910Sdougm ret = sa_update_config(handle); 31033034Sdougm return (ret); 31043034Sdougm } 31053034Sdougm 31063034Sdougm /* 31073034Sdougm * sa_disable_group(flags, argc, argv) 31083034Sdougm * 31093034Sdougm * Implements the disable subcommand 31103034Sdougm */ 31113034Sdougm 31123034Sdougm int 31133910Sdougm sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[]) 31143034Sdougm { 31153034Sdougm int verbose = 0; 31163034Sdougm int dryrun = 0; 31173034Sdougm int all = 0; 31183034Sdougm int c; 31193034Sdougm int ret = SA_OK; 31203034Sdougm char *protocol; 31213034Sdougm char *state; 31223034Sdougm struct list *worklist = NULL; 31233034Sdougm int auth = 1; 31243034Sdougm 31253034Sdougm while ((c = getopt(argc, argv, "?havn")) != EOF) { 31263034Sdougm switch (c) { 31273034Sdougm case 'a': 31283034Sdougm all = 1; 31293034Sdougm break; 31303034Sdougm case 'n': 31313034Sdougm dryrun++; 31323034Sdougm break; 31333034Sdougm case 'P': 31343034Sdougm protocol = optarg; 31353034Sdougm if (!sa_valid_protocol(protocol)) { 31363034Sdougm (void) printf(gettext("Invalid protocol specified: %s\n"), 31373034Sdougm protocol); 31383034Sdougm return (SA_INVALID_PROTOCOL); 31393034Sdougm } 31403034Sdougm break; 31413034Sdougm case 'v': 31423034Sdougm verbose++; 31433034Sdougm break; 31443034Sdougm default: 31453034Sdougm case 'h': 31463034Sdougm case '?': 31473034Sdougm (void) printf(gettext("usage: %s\n"), 31483034Sdougm sa_get_usage(USAGE_DISABLE)); 31493034Sdougm return (0); 31503034Sdougm } 31513034Sdougm } 31523034Sdougm 31533034Sdougm if (optind == argc && !all) { 31543034Sdougm (void) printf(gettext("usage: %s\n"), 31553034Sdougm sa_get_usage(USAGE_DISABLE)); 31563034Sdougm (void) printf(gettext("\tmust specify group\n")); 31573034Sdougm ret = SA_NO_SUCH_PATH; 31583034Sdougm } else { 31593034Sdougm sa_group_t group; 31603034Sdougm if (!all) { 31613034Sdougm while (optind < argc) { 31623910Sdougm group = sa_get_group(handle, argv[optind]); 31633034Sdougm if (group != NULL) { 31643034Sdougm auth &= check_authorizations(argv[optind], flags); 31653034Sdougm state = sa_get_group_attr(group, "state"); 31663034Sdougm if (state == NULL || 31673034Sdougm strcmp(state, "disabled") == 0) { 31683034Sdougm /* already disabled */ 31693034Sdougm if (verbose) 31703034Sdougm (void) printf(gettext("Group \"%s\" is " 31713034Sdougm "already disabled\n"), 31723034Sdougm argv[optind]); 31733034Sdougm ret = SA_BUSY; /* already disable */ 31743034Sdougm } else { 31753034Sdougm worklist = add_list(worklist, group, 0); 31763034Sdougm if (verbose) 31773034Sdougm (void) printf(gettext("Disabling group " 31783034Sdougm "\"%s\"\n"), 31793034Sdougm argv[optind]); 31803034Sdougm } 31813034Sdougm if (state != NULL) 31823034Sdougm sa_free_attr_string(state); 31833034Sdougm } else { 31843034Sdougm ret = SA_NO_SUCH_GROUP; 31853034Sdougm } 31863034Sdougm optind++; 31873034Sdougm } 31883034Sdougm } else { 31893910Sdougm for (group = sa_get_group(handle, NULL); group != NULL; 31903034Sdougm group = sa_get_next_group(group)) { 31913034Sdougm worklist = add_list(worklist, group, 0); 31923034Sdougm } 31933034Sdougm } 31943034Sdougm if (ret == SA_OK && !dryrun) { 31953910Sdougm ret = disable_all_groups(handle, worklist, 1); 31963034Sdougm } 31973034Sdougm if (ret != SA_OK && ret != SA_BUSY) 31983034Sdougm (void) printf(gettext("Could not disable group: %s\n"), 31993034Sdougm sa_errorstr(ret)); 32003034Sdougm if (ret == SA_BUSY) 32013034Sdougm ret = SA_OK; 32023034Sdougm } 32033034Sdougm if (worklist != NULL) 32043034Sdougm free_list(worklist); 32053034Sdougm if (dryrun && ret == SA_OK && !auth && verbose) { 32063034Sdougm (void) printf(gettext("Command would fail: %s\n"), 32073034Sdougm sa_errorstr(SA_NO_PERMISSION)); 32083034Sdougm } 32093034Sdougm return (ret); 32103034Sdougm } 32113034Sdougm 32123034Sdougm /* 32133034Sdougm * sa_start_group(flags, argc, argv) 32143034Sdougm * 32153034Sdougm * Implements the start command. 32163034Sdougm * This is similar to enable except it doesn't change the state 32173034Sdougm * of the group(s) and only enables shares if the group is already 32183034Sdougm * enabled. 32193034Sdougm */ 32203034Sdougm 32213034Sdougm int 32223910Sdougm sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[]) 32233034Sdougm { 32243034Sdougm int verbose = 0; 32253034Sdougm int all = 0; 32263034Sdougm int c; 32273034Sdougm int ret = SMF_EXIT_OK; 32283034Sdougm char *protocol = NULL; 32293034Sdougm char *state; 32303034Sdougm struct list *worklist = NULL; 32313034Sdougm #ifdef lint 32323034Sdougm flags = flags; 32333034Sdougm #endif 32343034Sdougm 32353034Sdougm while ((c = getopt(argc, argv, "?havP:")) != EOF) { 32363034Sdougm switch (c) { 32373034Sdougm case 'a': 32383034Sdougm all = 1; 32393034Sdougm break; 32403034Sdougm case 'P': 32413034Sdougm protocol = optarg; 32423034Sdougm if (!sa_valid_protocol(protocol)) { 32433034Sdougm (void) printf(gettext("Invalid protocol specified: %s\n"), 32443034Sdougm protocol); 32453034Sdougm return (SA_INVALID_PROTOCOL); 32463034Sdougm } 32473034Sdougm break; 32483034Sdougm case 'v': 32493034Sdougm verbose++; 32503034Sdougm break; 32513034Sdougm default: 32523034Sdougm case 'h': 32533034Sdougm case '?': 32543034Sdougm (void) printf(gettext("usage: %s\n"), 32553034Sdougm sa_get_usage(USAGE_START)); 32563034Sdougm return (SA_OK); 32573034Sdougm } 32583034Sdougm } 32593034Sdougm 32603034Sdougm if (optind == argc && !all) { 32613034Sdougm (void) printf(gettext("usage: %s\n"), 32623034Sdougm sa_get_usage(USAGE_START)); 32633034Sdougm ret = SMF_EXIT_ERR_FATAL; 32643034Sdougm } else { 32653034Sdougm sa_group_t group; 32663034Sdougm 32673034Sdougm if (!all) { 32683034Sdougm while (optind < argc) { 32693910Sdougm group = sa_get_group(handle, argv[optind]); 32703034Sdougm if (group != NULL) { 32713034Sdougm state = sa_get_group_attr(group, "state"); 32723034Sdougm if (state == NULL || 32733034Sdougm strcmp(state, "enabled") == 0) { 32743034Sdougm worklist = add_list(worklist, group, 0); 32753034Sdougm if (verbose) 32763034Sdougm (void) printf(gettext("Starting group " 32773034Sdougm "\"%s\"\n"), 32783034Sdougm argv[optind]); 32793034Sdougm } else { 32803034Sdougm /* 32813034Sdougm * determine if there are any 32823034Sdougm * protocols. if there aren't any, 32833034Sdougm * then there isn't anything to do in 32843034Sdougm * any case so no error. 32853034Sdougm */ 32863034Sdougm if (sa_get_optionset(group, protocol) != NULL) { 32873034Sdougm ret = SMF_EXIT_OK; 32883034Sdougm } 32893034Sdougm } 32903034Sdougm if (state != NULL) 32913034Sdougm sa_free_attr_string(state); 32923034Sdougm } 32933034Sdougm optind++; 32943034Sdougm } 32953034Sdougm } else { 32963910Sdougm for (group = sa_get_group(handle, NULL); group != NULL; 32973034Sdougm group = sa_get_next_group(group)) { 32983034Sdougm state = sa_get_group_attr(group, "state"); 32993034Sdougm if (state == NULL || strcmp(state, "enabled") == 0) 33003034Sdougm worklist = add_list(worklist, group, 0); 33013034Sdougm if (state != NULL) 33023034Sdougm sa_free_attr_string(state); 33033034Sdougm } 33043034Sdougm } 33053910Sdougm (void) enable_all_groups(handle, worklist, 0, 1, NULL); 33063034Sdougm } 33073034Sdougm if (worklist != NULL) 33083034Sdougm free_list(worklist); 33093034Sdougm return (ret); 33103034Sdougm } 33113034Sdougm 33123034Sdougm /* 33133034Sdougm * sa_stop_group(flags, argc, argv) 33143034Sdougm * 33153034Sdougm * Implements the stop command. 33163034Sdougm * This is similar to disable except it doesn't change the state 33173034Sdougm * of the group(s) and only disables shares if the group is already 33183034Sdougm * enabled. 33193034Sdougm */ 33203034Sdougm 33213034Sdougm int 33223910Sdougm sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[]) 33233034Sdougm { 33243034Sdougm int verbose = 0; 33253034Sdougm int all = 0; 33263034Sdougm int c; 33273034Sdougm int ret = SMF_EXIT_OK; 33283034Sdougm char *protocol = NULL; 33293034Sdougm char *state; 33303034Sdougm struct list *worklist = NULL; 33313034Sdougm #ifdef lint 33323034Sdougm flags = flags; 33333034Sdougm #endif 33343034Sdougm 33353034Sdougm while ((c = getopt(argc, argv, "?havP:")) != EOF) { 33363034Sdougm switch (c) { 33373034Sdougm case 'a': 33383034Sdougm all = 1; 33393034Sdougm break; 33403034Sdougm case 'P': 33413034Sdougm protocol = optarg; 33423034Sdougm if (!sa_valid_protocol(protocol)) { 33433034Sdougm (void) printf(gettext("Invalid protocol specified: %s\n"), 33443034Sdougm protocol); 33453034Sdougm return (SA_INVALID_PROTOCOL); 33463034Sdougm } 33473034Sdougm break; 33483034Sdougm case 'v': 33493034Sdougm verbose++; 33503034Sdougm break; 33513034Sdougm default: 33523034Sdougm case 'h': 33533034Sdougm case '?': 33543034Sdougm (void) printf(gettext("usage: %s\n"), 33553034Sdougm sa_get_usage(USAGE_STOP)); 33563034Sdougm return (0); 33573034Sdougm } 33583034Sdougm } 33593034Sdougm 33603034Sdougm if (optind == argc && !all) { 33613034Sdougm (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_STOP)); 33623034Sdougm ret = SMF_EXIT_ERR_FATAL; 33633034Sdougm } else { 33643034Sdougm sa_group_t group; 33653034Sdougm if (!all) { 33663034Sdougm while (optind < argc) { 33673910Sdougm group = sa_get_group(handle, argv[optind]); 33683034Sdougm if (group != NULL) { 33693034Sdougm state = sa_get_group_attr(group, "state"); 33703034Sdougm if (state == NULL || 33713034Sdougm strcmp(state, "enabled") == 0) { 33723034Sdougm worklist = add_list(worklist, group, 0); 33733034Sdougm if (verbose) 33743034Sdougm (void) printf(gettext("Stopping group " 33753034Sdougm "\"%s\"\n"), 33763034Sdougm argv[optind]); 33773034Sdougm } else { 33783034Sdougm ret = SMF_EXIT_OK; 33793034Sdougm } 33803034Sdougm if (state != NULL) 33813034Sdougm sa_free_attr_string(state); 33823034Sdougm } 33833034Sdougm optind++; 33843034Sdougm } 33853034Sdougm } else { 33863910Sdougm for (group = sa_get_group(handle, NULL); group != NULL; 33873034Sdougm group = sa_get_next_group(group)) { 33883034Sdougm state = sa_get_group_attr(group, "state"); 33893034Sdougm if (state == NULL || strcmp(state, "enabled") == 0) 33903034Sdougm worklist = add_list(worklist, group, 0); 33913034Sdougm if (state != NULL) 33923034Sdougm sa_free_attr_string(state); 33933034Sdougm } 33943034Sdougm } 33953910Sdougm (void) disable_all_groups(handle, worklist, 0); 33963910Sdougm ret = sa_update_config(handle); 33973034Sdougm } 33983034Sdougm if (worklist != NULL) 33993034Sdougm free_list(worklist); 34003034Sdougm return (ret); 34013034Sdougm } 34023034Sdougm 34033034Sdougm /* 34043034Sdougm * remove_all_options(share, proto) 34053034Sdougm * 34063034Sdougm * Removes all options on a share. 34073034Sdougm */ 34083034Sdougm 34093034Sdougm static void 34103034Sdougm remove_all_options(sa_share_t share, char *proto) 34113034Sdougm { 34123034Sdougm sa_optionset_t optionset; 34133034Sdougm sa_security_t security; 34143034Sdougm sa_security_t prevsec = NULL; 34153034Sdougm 34163034Sdougm optionset = sa_get_optionset(share, proto); 34173034Sdougm if (optionset != NULL) 34183034Sdougm (void) sa_destroy_optionset(optionset); 34193034Sdougm for (security = sa_get_security(share, NULL, NULL); 34203034Sdougm security != NULL; 34213034Sdougm security = sa_get_next_security(security)) { 34223034Sdougm char *type; 34233034Sdougm /* 34243034Sdougm * we walk through the list. prevsec keeps the 34253034Sdougm * previous security so we can delete it without 34263034Sdougm * destroying the list. 34273034Sdougm */ 34283034Sdougm if (prevsec != NULL) { 34293034Sdougm /* remove the previously seen security */ 34303034Sdougm (void) sa_destroy_security(prevsec); 34313034Sdougm /* set to NULL so we don't try multiple times */ 34323034Sdougm prevsec = NULL; 34333034Sdougm } 34343034Sdougm type = sa_get_security_attr(security, "type"); 34353034Sdougm if (type != NULL) { 34363034Sdougm /* 34373034Sdougm * if the security matches the specified protocol, we 34383034Sdougm * want to remove it. prevsec holds it until either 34393034Sdougm * the next pass or we fall out of the loop. 34403034Sdougm */ 34413034Sdougm if (strcmp(type, proto) == 0) 34423034Sdougm prevsec = security; 34433034Sdougm sa_free_attr_string(type); 34443034Sdougm } 34453034Sdougm } 34463034Sdougm /* in case there is one left */ 34473034Sdougm if (prevsec != NULL) 34483034Sdougm (void) sa_destroy_security(prevsec); 34493034Sdougm } 34503034Sdougm 34513034Sdougm 34523034Sdougm /* 34533034Sdougm * for legacy support, we need to handle the old syntax. This is what 34543034Sdougm * we get if sharemgr is called with the name "share" rather than 34553034Sdougm * sharemgr. 34563034Sdougm */ 34573034Sdougm 34583034Sdougm static int 34593034Sdougm format_legacy_path(char *buff, int buffsize, char *proto, char *cmd) 34603034Sdougm { 34613034Sdougm int err; 34623034Sdougm 34633034Sdougm err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd); 34643034Sdougm if (err > buffsize) 34653034Sdougm return (-1); 34663034Sdougm return (0); 34673034Sdougm } 34683034Sdougm 34693034Sdougm 34703034Sdougm /* 34713034Sdougm * check_legacy_cmd(proto, cmd) 34723034Sdougm * 34733034Sdougm * Check to see if the cmd exists in /usr/lib/fs/<proto>/<cmd> and is 34743034Sdougm * executable. 34753034Sdougm */ 34763034Sdougm 34773034Sdougm static int 34783034Sdougm check_legacy_cmd(char *path) 34793034Sdougm { 34803034Sdougm struct stat st; 34813034Sdougm int ret = 0; 34823034Sdougm 34833034Sdougm if (stat(path, &st) == 0) { 34843034Sdougm if (S_ISREG(st.st_mode) && st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) 34853034Sdougm ret = 1; 34863034Sdougm } 34873034Sdougm return (ret); 34883034Sdougm } 34893034Sdougm 34903034Sdougm /* 34913034Sdougm * run_legacy_command(proto, cmd, argv) 34923034Sdougm * 34933034Sdougm * we know the command exists, so attempt to execute it with all the 34943034Sdougm * arguments. This implements full legacy share support for those 34953034Sdougm * protocols that don't have plugin providers. 34963034Sdougm */ 34973034Sdougm 34983034Sdougm static int 34993034Sdougm run_legacy_command(char *path, char *argv[]) 35003034Sdougm { 35013034Sdougm int ret; 35023034Sdougm 35033034Sdougm ret = execv(path, argv); 35043034Sdougm if (ret < 0) { 35053034Sdougm switch (errno) { 35063034Sdougm case EACCES: 35073034Sdougm ret = SA_NO_PERMISSION; 35083034Sdougm break; 35093034Sdougm default: 35103034Sdougm ret = SA_SYSTEM_ERR; 35113034Sdougm break; 35123034Sdougm } 35133034Sdougm } 35143034Sdougm return (ret); 35153034Sdougm } 35163034Sdougm 35173034Sdougm /* 35183348Sdougm * out_share(out, group, proto) 35193034Sdougm * 35203034Sdougm * Display the share information in the format that the "share" 35213034Sdougm * command has traditionally used. 35223034Sdougm */ 35233034Sdougm 35243034Sdougm static void 35253348Sdougm out_share(FILE *out, sa_group_t group, char *proto) 35263034Sdougm { 35273034Sdougm sa_share_t share; 35283034Sdougm char resfmt[128]; 35293034Sdougm 35303034Sdougm for (share = sa_get_share(group, NULL); share != NULL; 35313034Sdougm share = sa_get_next_share(share)) { 35323034Sdougm char *path; 35333034Sdougm char *type; 35343034Sdougm char *resource; 35353034Sdougm char *description; 35363034Sdougm char *groupname; 35373034Sdougm char *sharedstate; 35383034Sdougm int shared = 1; 35393034Sdougm char *soptions; 35403034Sdougm 35413034Sdougm sharedstate = sa_get_share_attr(share, "shared"); 35423034Sdougm path = sa_get_share_attr(share, "path"); 35433034Sdougm type = sa_get_share_attr(share, "type"); 35443034Sdougm resource = sa_get_share_attr(share, "resource"); 35453034Sdougm groupname = sa_get_group_attr(group, "name"); 35463034Sdougm 35473034Sdougm if (groupname != NULL && strcmp(groupname, "default") == 0) { 35483034Sdougm sa_free_attr_string(groupname); 35493034Sdougm groupname = NULL; 35503034Sdougm } 35513034Sdougm description = sa_get_share_description(share); 35523348Sdougm 35533348Sdougm /* want the sharetab version if it exists */ 35543348Sdougm soptions = sa_get_share_attr(share, "shareopts"); 35553034Sdougm 35563034Sdougm if (sharedstate == NULL) 35573034Sdougm shared = 0; 35583034Sdougm 35593348Sdougm if (soptions == NULL) 35603348Sdougm soptions = sa_proto_legacy_format(proto, share, 1); 35613034Sdougm 35623034Sdougm if (shared) { 35633348Sdougm /* only active shares go here */ 35643034Sdougm (void) snprintf(resfmt, sizeof (resfmt), "%s%s%s", 35653034Sdougm resource != NULL ? resource : "-", 35663034Sdougm groupname != NULL ? "@" : "", 35673034Sdougm groupname != NULL ? groupname : ""); 35683034Sdougm (void) fprintf(out, "%-14.14s %s %s \"%s\" \n", 35693034Sdougm resfmt, 35703034Sdougm path, 35713034Sdougm (soptions != NULL && strlen(soptions) > 0) ? 35723034Sdougm soptions : "rw", 35733034Sdougm (description != NULL) ? description : ""); 35743034Sdougm } 35753034Sdougm 35763034Sdougm if (path != NULL) 35773034Sdougm sa_free_attr_string(path); 35783034Sdougm if (type != NULL) 35793034Sdougm sa_free_attr_string(type); 35803034Sdougm if (resource != NULL) 35813034Sdougm sa_free_attr_string(resource); 35823034Sdougm if (groupname != NULL) 35833034Sdougm sa_free_attr_string(groupname); 35843034Sdougm if (description != NULL) 35853034Sdougm sa_free_share_description(description); 35863034Sdougm if (sharedstate != NULL) 35873034Sdougm sa_free_attr_string(sharedstate); 35883348Sdougm if (soptions != NULL) 35893034Sdougm sa_format_free(soptions); 35903034Sdougm } 35913034Sdougm } 35923034Sdougm 35933034Sdougm /* 35943034Sdougm * output_legacy_file(out, proto) 35953034Sdougm * 35963034Sdougm * Walk all of the groups for the specified protocol and call 35973034Sdougm * out_share() to format and write in the format displayed by the 35983034Sdougm * "share" command with no arguments. 35993034Sdougm */ 36003034Sdougm 36013034Sdougm static void 36023910Sdougm output_legacy_file(FILE *out, char *proto, sa_handle_t handle) 36033034Sdougm { 36043034Sdougm sa_group_t group; 36053034Sdougm 36063910Sdougm for (group = sa_get_group(handle, NULL); group != NULL; 36073034Sdougm group = sa_get_next_group(group)) { 36083034Sdougm char *options; 36093034Sdougm char *zfs; 36103034Sdougm 36113034Sdougm /* 36123034Sdougm * get default options preformated, being careful to 36133034Sdougm * handle legacy shares differently from new style 36143034Sdougm * shares. Legacy share have options on the share. 36153034Sdougm */ 36163034Sdougm 36173034Sdougm zfs = sa_get_group_attr(group, "zfs"); 36183034Sdougm if (zfs != NULL) { 36193034Sdougm sa_group_t zgroup; 36203034Sdougm sa_free_attr_string(zfs); 36213034Sdougm options = sa_proto_legacy_format(proto, group, 1); 36223034Sdougm for (zgroup = sa_get_sub_group(group); zgroup != NULL; 36233034Sdougm zgroup = sa_get_next_group(zgroup)) { 36243034Sdougm 36253034Sdougm /* got a group, so display it */ 36263348Sdougm out_share(out, zgroup, proto); 36273034Sdougm } 36283034Sdougm } else { 36293034Sdougm options = sa_proto_legacy_format(proto, group, 1); 36303348Sdougm out_share(out, group, proto); 36313034Sdougm } 36323034Sdougm if (options != NULL) 36333034Sdougm free(options); 36343034Sdougm } 36353034Sdougm } 36363034Sdougm 36373034Sdougm int 36383910Sdougm sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[]) 36393034Sdougm { 36403034Sdougm char *protocol = "nfs"; 36413034Sdougm char *options = NULL; 36423034Sdougm char *description = NULL; 36433034Sdougm char *groupname = NULL; 36443034Sdougm char *sharepath = NULL; 36453034Sdougm char *resource = NULL; 36463034Sdougm char *groupstatus = NULL; 36473034Sdougm int persist = SA_SHARE_TRANSIENT; 36483034Sdougm int argsused = 0; 36493034Sdougm int c; 36503034Sdougm int ret = SA_OK; 36513034Sdougm int zfs = 0; 36523034Sdougm int true_legacy = 0; 36533034Sdougm int curtype = SA_SHARE_TRANSIENT; 36543034Sdougm char cmd[MAXPATHLEN]; 36553034Sdougm #ifdef lint 36563034Sdougm flags = flags; 36573034Sdougm #endif 36583034Sdougm 36593034Sdougm while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) { 36603034Sdougm switch (c) { 36613034Sdougm case 'd': 36623034Sdougm description = optarg; 36633034Sdougm argsused++; 36643034Sdougm break; 36653034Sdougm case 'F': 36663034Sdougm protocol = optarg; 36673034Sdougm if (!sa_valid_protocol(protocol)) { 36683034Sdougm if (format_legacy_path(cmd, MAXPATHLEN, 36693034Sdougm protocol, "share") == 0 && check_legacy_cmd(cmd)) { 36703034Sdougm true_legacy++; 36713034Sdougm } else { 36723034Sdougm (void) fprintf(stderr, 36733034Sdougm gettext("Invalid protocol specified:" 36743034Sdougm "%s\n"), 36753034Sdougm protocol); 36763034Sdougm return (SA_INVALID_PROTOCOL); 36773034Sdougm } 36783034Sdougm } 36793034Sdougm break; 36803034Sdougm case 'o': 36813034Sdougm options = optarg; 36823034Sdougm argsused++; 36833034Sdougm break; 36843034Sdougm case 'p': 36853034Sdougm persist = SA_SHARE_PERMANENT; 36863034Sdougm argsused++; 36873034Sdougm break; 36883034Sdougm case 'h': 36893034Sdougm case '?': 36903034Sdougm default: 36913034Sdougm (void) fprintf(stderr, gettext("usage: %s\n"), 36923034Sdougm sa_get_usage(USAGE_SHARE)); 36933034Sdougm return (SA_OK); 36943034Sdougm } 36953034Sdougm } 36963034Sdougm 36973034Sdougm /* have the info so construct what is needed */ 36983034Sdougm if (!argsused && optind == argc) { 36993034Sdougm /* display current info in share format */ 37003910Sdougm (void) output_legacy_file(stdout, "nfs", handle); 37013034Sdougm } else { 37023034Sdougm sa_group_t group = NULL; 37033034Sdougm sa_share_t share; 37043034Sdougm char dir[MAXPATHLEN]; 37053034Sdougm 37063034Sdougm /* we are modifying the configuration */ 37073034Sdougm if (optind == argc) { 37083034Sdougm (void) fprintf(stderr, gettext("usage: %s\n"), 37093034Sdougm sa_get_usage(USAGE_SHARE)); 37103034Sdougm return (SA_LEGACY_ERR); 37113034Sdougm } 37123034Sdougm 37133034Sdougm if (true_legacy) { 37143034Sdougm /* if still using legacy share/unshare, exec it */ 37153034Sdougm ret = run_legacy_command(cmd, argv); 37163034Sdougm return (ret); 37173034Sdougm } 37183034Sdougm 37193034Sdougm sharepath = argv[optind++]; 37203034Sdougm if (optind < argc) { 37213034Sdougm resource = argv[optind]; 37223034Sdougm groupname = strchr(resource, '@'); 37233034Sdougm if (groupname != NULL) 37243034Sdougm *groupname++ = '\0'; 37253034Sdougm } 37263034Sdougm if (realpath(sharepath, dir) == NULL) 37273034Sdougm ret = SA_BAD_PATH; 37283034Sdougm else 37293034Sdougm sharepath = dir; 37303034Sdougm if (ret == SA_OK) { 37313910Sdougm share = sa_find_share(handle, sharepath); 37323034Sdougm } else { 37333034Sdougm share = NULL; 37343034Sdougm } 37353034Sdougm if (groupname != NULL) { 37363034Sdougm ret = SA_NOT_ALLOWED; 37373034Sdougm } else if (ret == SA_OK) { 37383034Sdougm char *legacygroup = "default"; 37393034Sdougm /* 37403034Sdougm * the legacy group is always present and zfs groups 37413034Sdougm * come and go. zfs shares may be in sub-groups and 37423034Sdougm * the zfs share will already be in that group so it 37433034Sdougm * isn't an error. 37443034Sdougm */ 37453034Sdougm if (share != NULL) { 37463034Sdougm /* 37473034Sdougm * if the share exists, then make sure it is one we 37483034Sdougm * want to handle. 37493034Sdougm */ 37503034Sdougm group = sa_get_parent_group(share); 37513034Sdougm } else { 37523910Sdougm group = sa_get_group(handle, legacygroup); 37533034Sdougm } 37543034Sdougm if (group != NULL) { 37553034Sdougm groupstatus = group_status(group); 37563034Sdougm if (share == NULL) { 37573034Sdougm share = sa_add_share(group, sharepath, persist, &ret); 37583034Sdougm if (share == NULL && ret == SA_DUPLICATE_NAME) { 37593034Sdougm /* could be a ZFS path being started */ 37603910Sdougm if (sa_zfs_is_shared(handle, sharepath)) { 37613034Sdougm ret = SA_OK; 37623910Sdougm group = sa_get_group(handle, "zfs"); 37633034Sdougm if (group == NULL) { 37643034Sdougm /* this shouldn't happen */ 37653034Sdougm ret = SA_CONFIG_ERR; 37663034Sdougm } 37673034Sdougm if (group != NULL) { 37683034Sdougm share = sa_add_share(group, sharepath, 37693034Sdougm persist, &ret); 37703034Sdougm } 37713034Sdougm } 37723034Sdougm } 37733034Sdougm } else { 37743108Sdougm char *type; 37753034Sdougm /* 37763034Sdougm * may want to change persist state, but the 37773108Sdougm * important thing is to change options. We 37783108Sdougm * need to change them regardless of the 37793108Sdougm * source. 37803034Sdougm */ 37813910Sdougm if (sa_zfs_is_shared(handle, sharepath)) { 37823108Sdougm zfs = 1; 37833108Sdougm } 37843108Sdougm remove_all_options(share, protocol); 37853108Sdougm type = sa_get_share_attr(share, "type"); 37863108Sdougm if (type != NULL && 37873108Sdougm strcmp(type, "transient") != 0) { 37883108Sdougm curtype = SA_SHARE_PERMANENT; 37893108Sdougm } 37903108Sdougm if (type != NULL) 37913108Sdougm sa_free_attr_string(type); 37923108Sdougm if (curtype != persist) { 37933108Sdougm (void) sa_set_share_attr(share, "type", 37943034Sdougm persist == SA_SHARE_PERMANENT ? 37953034Sdougm "persist" : "transient"); 37963034Sdougm } 37973034Sdougm } 37983108Sdougm /* have a group to hold this share path */ 37993108Sdougm if (ret == SA_OK && options != NULL && 38003108Sdougm strlen(options) > 0) { 38013108Sdougm ret = sa_parse_legacy_options(share, 38023108Sdougm options, 38033108Sdougm protocol); 38043108Sdougm } 38053034Sdougm if (!zfs) { 38063108Sdougm /* 38073108Sdougm * zfs shares never have resource or 38083108Sdougm * description and we can't store the values 38093108Sdougm * so don't try. 38103108Sdougm */ 38113034Sdougm if (ret == SA_OK && description != NULL) 38123034Sdougm ret = sa_set_share_description(share, description); 38133034Sdougm if (ret == SA_OK && resource != NULL) 38143034Sdougm ret = sa_set_share_attr(share, "resource", 38153034Sdougm resource); 38163034Sdougm } 38173034Sdougm if (ret == SA_OK) { 38183034Sdougm if (strcmp(groupstatus, "enabled") == 0) 38193034Sdougm ret = sa_enable_share(share, protocol); 38203034Sdougm if (ret == SA_OK && persist == SA_SHARE_PERMANENT) { 38213034Sdougm (void) sa_update_legacy(share, protocol); 38223034Sdougm } 38233034Sdougm if (ret == SA_OK) 38243910Sdougm ret = sa_update_config(handle); 38253034Sdougm } 38263034Sdougm } else { 38273034Sdougm ret = SA_SYSTEM_ERR; 38283034Sdougm } 38293034Sdougm } 38303034Sdougm } 38313034Sdougm if (ret != SA_OK) { 38323034Sdougm (void) fprintf(stderr, gettext("Could not share: %s: %s\n"), 38333034Sdougm sharepath, sa_errorstr(ret)); 38343034Sdougm ret = SA_LEGACY_ERR; 38353034Sdougm 38363034Sdougm } 38373034Sdougm return (ret); 38383034Sdougm } 38393034Sdougm 38403034Sdougm /* 38413034Sdougm * sa_legacy_unshare(flags, argc, argv) 38423034Sdougm * 38433034Sdougm * Implements the original unshare command. 38443034Sdougm */ 38453034Sdougm 38463034Sdougm int 38473910Sdougm sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[]) 38483034Sdougm { 38493034Sdougm char *protocol = "nfs"; /* for now */ 38503034Sdougm char *options = NULL; 38513034Sdougm char *sharepath = NULL; 38523034Sdougm int persist = SA_SHARE_TRANSIENT; 38533034Sdougm int argsused = 0; 38543034Sdougm int c; 38553034Sdougm int ret = SA_OK; 38563034Sdougm int true_legacy = 0; 38573034Sdougm char cmd[MAXPATHLEN]; 38583034Sdougm #ifdef lint 38593034Sdougm flags = flags; 38603034Sdougm options = options; 38613034Sdougm #endif 38623034Sdougm 38633034Sdougm while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) { 38643034Sdougm switch (c) { 38653034Sdougm case 'h': 38663034Sdougm case '?': 38673034Sdougm break; 38683034Sdougm case 'F': 38693034Sdougm protocol = optarg; 38703034Sdougm if (!sa_valid_protocol(protocol)) { 38713034Sdougm if (format_legacy_path(cmd, MAXPATHLEN, 38723034Sdougm protocol, "unshare") == 0 && 38733034Sdougm check_legacy_cmd(cmd)) { 38743034Sdougm true_legacy++; 38753034Sdougm } else { 38763034Sdougm (void) printf(gettext("Invalid file system name\n")); 38773034Sdougm return (SA_INVALID_PROTOCOL); 38783034Sdougm } 38793034Sdougm } 38803034Sdougm break; 38813034Sdougm case 'o': 38823034Sdougm options = optarg; 38833034Sdougm argsused++; 38843034Sdougm break; 38853034Sdougm case 'p': 38863034Sdougm persist = SA_SHARE_PERMANENT; 38873034Sdougm argsused++; 38883034Sdougm break; 38893034Sdougm default: 38903034Sdougm (void) printf(gettext("usage: %s\n"), 38913034Sdougm sa_get_usage(USAGE_UNSHARE)); 38923034Sdougm return (SA_OK); 38933034Sdougm } 38943034Sdougm } 38953034Sdougm 38963034Sdougm /* have the info so construct what is needed */ 38973034Sdougm if (optind == argc || (optind + 1) < argc) { 38983034Sdougm ret = SA_SYNTAX_ERR; 38993034Sdougm } else { 39003034Sdougm sa_share_t share; 39013034Sdougm char dir[MAXPATHLEN]; 39023034Sdougm if (true_legacy) { 39033034Sdougm /* if still using legacy share/unshare, exec it */ 39043034Sdougm ret = run_legacy_command(cmd, argv); 39053034Sdougm return (ret); 39063034Sdougm } 39073663Sdougm /* 39083663Sdougm * Find the path in the internal configuration. If it 39093663Sdougm * isn't found, attempt to resolve the path via 39103663Sdougm * realpath() and try again. 39113663Sdougm */ 39123034Sdougm sharepath = argv[optind++]; 39133910Sdougm share = sa_find_share(handle, sharepath); 39143663Sdougm if (share == NULL) { 39153663Sdougm if (realpath(sharepath, dir) == NULL) { 39163663Sdougm ret = SA_NO_SUCH_PATH; 39173663Sdougm } else { 39183910Sdougm share = sa_find_share(handle, dir); 39193663Sdougm } 39203663Sdougm } 39213663Sdougm if (share != NULL) { 39223663Sdougm ret = sa_disable_share(share, protocol); 39233663Sdougm /* 39243663Sdougm * Errors are ok and removal should still occur. The 39253663Sdougm * legacy unshare is more forgiving of errors than the 39263663Sdougm * remove-share subcommand which may need the force 39273663Sdougm * flag set for some error conditions. That is, the 39283663Sdougm * "unshare" command will always unshare if it can 39293663Sdougm * while "remove-share" might require the force option. 39303663Sdougm */ 39313663Sdougm if (persist == SA_SHARE_PERMANENT) { 39323663Sdougm ret = sa_remove_share(share); 39333663Sdougm if (ret == SA_OK) 39343910Sdougm ret = sa_update_config(handle); 39353663Sdougm } 39363034Sdougm } else { 39373663Sdougm ret = SA_NOT_SHARED; 39383034Sdougm } 39393034Sdougm } 39403034Sdougm switch (ret) { 39413034Sdougm default: 39423034Sdougm (void) printf("%s: %s\n", sharepath, sa_errorstr(ret)); 39433034Sdougm ret = SA_LEGACY_ERR; 39443034Sdougm break; 39453034Sdougm case SA_SYNTAX_ERR: 39463034Sdougm (void) printf(gettext("usage: %s\n"), 39473034Sdougm sa_get_usage(USAGE_UNSHARE)); 39483034Sdougm break; 39493034Sdougm case SA_OK: 39503034Sdougm break; 39513034Sdougm } 39523034Sdougm return (ret); 39533034Sdougm } 39543034Sdougm 39553034Sdougm /* 39563034Sdougm * common commands that implement the sub-commands used by all 39573034Sdougm * protcols. The entries are found via the lookup command 39583034Sdougm */ 39593034Sdougm 39603034Sdougm static sa_command_t commands[] = { 39613034Sdougm {"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET}, 39623034Sdougm {"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION}, 39633034Sdougm {"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION}, 39643034Sdougm {"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION}, 39653034Sdougm {"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION}, 39663034Sdougm {"list", 0, sa_list, USAGE_LIST}, 39673034Sdougm {"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET}, 39683034Sdougm {"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET}, 39693034Sdougm {"set", 0, sa_set, USAGE_SET, SVC_SET}, 39703034Sdougm {"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET}, 39713034Sdougm {"show", 0, sa_show, USAGE_SHOW}, 39723034Sdougm {"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION}, 39733034Sdougm {"start", CMD_NODISPLAY, sa_start_group, USAGE_START, 39743034Sdougm SVC_SET|SVC_ACTION}, 39753034Sdougm {"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION}, 39763034Sdougm {"unset", 0, sa_unset, USAGE_UNSET, SVC_SET}, 39773034Sdougm {"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION}, 39783034Sdougm {NULL, 0, NULL, NULL} 39793034Sdougm }; 39803034Sdougm 39813034Sdougm static char * 39823034Sdougm sa_get_usage(sa_usage_t index) 39833034Sdougm { 39843034Sdougm char *ret = NULL; 39853034Sdougm switch (index) { 39863034Sdougm case USAGE_ADD_SHARE: 39873034Sdougm ret = gettext("add-share [-nth] [-r resource-name] " 39883034Sdougm "[-d \"description text\"] -s sharepath group"); 39893034Sdougm break; 39903034Sdougm case USAGE_CREATE: 39913034Sdougm ret = gettext("create [-nvh] [-P proto [-p property=value]] group"); 39923034Sdougm break; 39933034Sdougm case USAGE_DELETE: 39943034Sdougm ret = gettext("delete [-nvh] [-P proto] [-f] group"); 39953034Sdougm break; 39963034Sdougm case USAGE_DISABLE: 39973034Sdougm ret = gettext("disable [-nvh] {-a | group ...}"); 39983034Sdougm break; 39993034Sdougm case USAGE_ENABLE: 40003034Sdougm ret = gettext("enable [-nvh] {-a | group ...}"); 40013034Sdougm break; 40023034Sdougm case USAGE_LIST: 40033034Sdougm ret = gettext("list [-vh] [-P proto]"); 40043034Sdougm break; 40053034Sdougm case USAGE_MOVE_SHARE: 40063034Sdougm ret = gettext("move-share [-nvh] -s sharepath destination-group"); 40073034Sdougm break; 40083034Sdougm case USAGE_REMOVE_SHARE: 40093034Sdougm ret = gettext("remove-share [-fnvh] -s sharepath group"); 40103034Sdougm break; 40113034Sdougm case USAGE_SET: 40123034Sdougm ret = gettext("set [-nvh] -P proto [-S optspace] " 40133034Sdougm "[-p property=value]* [-s sharepath] group"); 40143034Sdougm break; 40153034Sdougm case USAGE_SET_SECURITY: 40163034Sdougm ret = gettext("set-security [-nvh] -P proto -S security-type " 40173034Sdougm "[-p property=value]* group"); 40183034Sdougm break; 40193034Sdougm case USAGE_SET_SHARE: 40203034Sdougm ret = gettext("set-share [-nh] [-r resource] " 40213034Sdougm "[-d \"description text\"] -s sharepath group"); 40223034Sdougm break; 40233034Sdougm case USAGE_SHOW: 40243034Sdougm ret = gettext("show [-pvxh] [-P proto] [group ...]"); 40253034Sdougm break; 40263034Sdougm case USAGE_SHARE: 40273034Sdougm ret = gettext("share [-F fstype] [-p] [-o optionlist]" 40283034Sdougm "[-d description] [pathname [resourcename]]"); 40293034Sdougm break; 40303034Sdougm case USAGE_START: 40313034Sdougm ret = gettext("start [-vh] [-P proto] {-a | group ...}"); 40323034Sdougm break; 40333034Sdougm case USAGE_STOP: 40343034Sdougm ret = gettext("stop [-vh] [-P proto] {-a | group ...}"); 40353034Sdougm break; 40363034Sdougm case USAGE_UNSET: 40373034Sdougm ret = gettext("unset [-nvh] -P proto [-S optspace] " 40383034Sdougm "[-p property]* group"); 40393034Sdougm break; 40403034Sdougm case USAGE_UNSET_SECURITY: 40413034Sdougm ret = gettext("unset-security [-nvh] -P proto -S security-type " 40423034Sdougm "[-p property]* group"); 40433034Sdougm break; 40443034Sdougm case USAGE_UNSHARE: 40453034Sdougm ret = gettext("unshare [-F fstype] [-p] [-o optionlist] sharepath"); 40463034Sdougm break; 40473034Sdougm } 40483034Sdougm return (ret); 40493034Sdougm } 40503034Sdougm 40513034Sdougm /* 40523034Sdougm * sa_lookup(cmd, proto) 40533034Sdougm * 40543034Sdougm * Lookup the sub-command. proto isn't currently used, but it may 40553034Sdougm * eventually provide a way to provide protocol specific sub-commands. 40563034Sdougm */ 40573034Sdougm 40583034Sdougm sa_command_t * 40593034Sdougm sa_lookup(char *cmd, char *proto) 40603034Sdougm { 40613034Sdougm int i; 40623034Sdougm size_t len; 40633034Sdougm #ifdef lint 40643034Sdougm proto = proto; 40653034Sdougm #endif 40663034Sdougm 40673034Sdougm len = strlen(cmd); 40683034Sdougm for (i = 0; commands[i].cmdname != NULL; i++) { 40693034Sdougm if (strncmp(cmd, commands[i].cmdname, len) == 0) 40703034Sdougm return (&commands[i]); 40713034Sdougm } 40723034Sdougm return (NULL); 40733034Sdougm } 40743034Sdougm 40753034Sdougm void 40763034Sdougm sub_command_help(char *proto) 40773034Sdougm { 40783034Sdougm int i; 40793034Sdougm #ifdef lint 40803034Sdougm proto = proto; 40813034Sdougm #endif 40823034Sdougm 40833034Sdougm (void) printf(gettext("\tsub-commands:\n")); 40843034Sdougm for (i = 0; commands[i].cmdname != NULL; i++) { 40853034Sdougm if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY))) 40863034Sdougm (void) printf("\t%s\n", 40873034Sdougm sa_get_usage((sa_usage_t)commands[i].cmdidx)); 40883034Sdougm } 40893034Sdougm } 4090