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