#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
#include <utmpx.h>
#include <pwd.h>
#include <auth_attr.h>
#include <secdb.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <libshare.h>
#include "sharemgr.h"
#include <libscf.h>
#include <libxml/tree.h>
#include <libintl.h>
#include <assert.h>
#include <iconv.h>
#include <langinfo.h>
#include <dirent.h>
static char *sa_get_usage(sa_usage_t);
static int
has_protocol(sa_group_t group, char *protocol)
{
sa_optionset_t optionset;
int result = 0;
optionset = sa_get_optionset(group, protocol);
if (optionset != NULL) {
result++;
}
return (result);
}
static int
validresource(const char *name)
{
const char *cp;
size_t len;
if (name == NULL)
return (B_FALSE);
len = strlen(name);
if (len == 0 || len > SA_MAX_RESOURCE_NAME)
return (B_FALSE);
if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
return (B_FALSE);
}
for (cp = name; *cp != '\0'; cp++)
if (iscntrl(*cp))
return (B_FALSE);
return (B_TRUE);
}
static char *
conv_to_utf8(char *input)
{
iconv_t cd;
char *inval = input;
char *output = input;
char *outleft;
char *curlocale;
size_t bytesleft;
size_t size;
size_t osize;
static int warned = 0;
curlocale = nl_langinfo(CODESET);
if (curlocale == NULL)
curlocale = "C";
cd = iconv_open("UTF-8", curlocale);
if (cd != NULL && cd != (iconv_t)-1) {
size = strlen(input);
bytesleft = size * 4;
output = calloc(bytesleft, 1);
if (output != NULL) {
outleft = output;
osize = iconv(cd, (const char **)&inval, &size,
&outleft, &bytesleft);
if (osize == (size_t)-1 || size != 0) {
free(output);
output = input;
}
} else {
output = input;
}
(void) iconv_close(cd);
} else {
if (!warned)
(void) fprintf(stderr,
gettext("Cannot convert to UTF-8 from %s\n"),
curlocale ? curlocale : gettext("unknown"));
warned = 1;
}
return (output);
}
static char *
conv_from_utf8(char *input)
{
iconv_t cd;
char *output = input;
char *inval = input;
char *outleft;
char *curlocale;
size_t bytesleft;
size_t size;
size_t osize;
static int warned = 0;
curlocale = nl_langinfo(CODESET);
if (curlocale == NULL)
curlocale = "C";
cd = iconv_open(curlocale, "UTF-8");
if (cd != NULL && cd != (iconv_t)-1) {
size = strlen(input);
bytesleft = size * 4;
output = calloc(bytesleft, 1);
if (output != NULL) {
outleft = output;
osize = iconv(cd, (const char **)&inval, &size,
&outleft, &bytesleft);
if (osize == (size_t)-1 || size != 0)
output = input;
} else {
output = input;
}
(void) iconv_close(cd);
} else {
if (!warned)
(void) fprintf(stderr,
gettext("Cannot convert to %s from UTF-8\n"),
curlocale ? curlocale : gettext("unknown"));
warned = 1;
}
return (output);
}
static void
print_rsrc_desc(sa_resource_t resource, char *sharedesc)
{
char *description;
char *desc;
if (resource == NULL)
return;
description = sa_get_resource_description(resource);
if (description != NULL) {
desc = conv_from_utf8(description);
if (desc != description) {
sa_free_share_description(description);
description = desc;
}
} else if (sharedesc != NULL) {
description = strdup(sharedesc);
}
if (description != NULL) {
(void) printf("\t\"%s\"", description);
sa_free_share_description(description);
}
}
static int
set_resource_desc(sa_share_t share, char *description)
{
char *desc;
int ret;
desc = conv_to_utf8(description);
ret = sa_set_resource_description(share, desc);
if (description != desc)
sa_free_share_description(desc);
return (ret);
}
static int
set_share_desc(sa_share_t share, char *description)
{
char *desc;
int ret;
desc = conv_to_utf8(description);
ret = sa_set_share_description(share, desc);
if (description != desc)
sa_free_share_description(desc);
return (ret);
}
struct list *
add_list(struct list *listp, void *item, void *data, char *proto)
{
struct list *new, *tmp;
new = malloc(sizeof (struct list));
if (new != NULL) {
new->next = NULL;
new->item = item;
new->itemdata = data;
new->proto = proto;
} else {
return (listp);
}
if (listp == NULL)
return (new);
for (tmp = listp; tmp->next != NULL; tmp = tmp->next) {
}
tmp->next = new;
return (listp);
}
static void
free_list(struct list *listp)
{
struct list *tmp;
while (listp != NULL) {
tmp = listp;
listp = listp->next;
free(tmp);
}
}
static int
check_authorization(char *instname, int which)
{
scf_handle_t *handle = NULL;
scf_simple_prop_t *prop = NULL;
char svcstring[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
char *authstr = NULL;
ssize_t numauths;
int ret = B_TRUE;
uid_t uid;
struct passwd *pw = NULL;
uid = getuid();
pw = getpwuid(uid);
if (pw == NULL) {
ret = B_FALSE;
} else {
(void) snprintf(svcstring, sizeof (svcstring), "%s:%s",
SA_SVC_FMRI_BASE, instname);
handle = scf_handle_create(SCF_VERSION);
if (handle != NULL) {
if (scf_handle_bind(handle) == 0) {
switch (which) {
case SVC_SET:
prop = scf_simple_prop_get(handle,
svcstring, "general",
SVC_AUTH_VALUE);
break;
case SVC_ACTION:
prop = scf_simple_prop_get(handle,
svcstring, "general",
SVC_AUTH_ACTION);
break;
}
}
}
}
if (prop != NULL) {
int i;
numauths = scf_simple_prop_numvalues(prop);
for (ret = 0, i = 0; i < numauths; i++) {
authstr = scf_simple_prop_next_astring(prop);
if (authstr != NULL) {
if (chkauthattr(authstr, pw->pw_name)) {
ret = 1;
break;
}
}
}
endauthattr();
scf_simple_prop_free(prop);
} else {
ret = 0;
}
if (handle != NULL)
scf_handle_destroy(handle);
return (ret);
}
static int
check_authorizations(char *instname, int flags)
{
int ret1 = 0;
int ret2 = 0;
int ret;
if (flags & SVC_SET)
ret1 = check_authorization(instname, SVC_SET);
if (flags & SVC_ACTION)
ret2 = check_authorization(instname, SVC_ACTION);
switch (flags) {
case SVC_ACTION:
ret = ret2;
break;
case SVC_SET:
ret = ret1;
break;
case SVC_ACTION|SVC_SET:
ret = ret1 & ret2;
break;
default:
ret = 1;
}
return (ret);
}
static void
notify_or_enable_share(sa_share_t share, char *protocol)
{
sa_group_t group;
sa_optionset_t opt;
int ret = SA_OK;
char *path;
char *groupproto;
sa_share_t parent = share;
if (!sa_is_share(share)) {
parent = sa_get_resource_parent((sa_resource_t)share);
}
path = sa_get_share_attr(parent, "path");
if (path == NULL)
return;
group = sa_get_parent_group(parent);
if (group == NULL) {
sa_free_attr_string(path);
return;
}
for (opt = sa_get_optionset(group, NULL);
opt != NULL;
opt = sa_get_next_optionset(opt)) {
groupproto = sa_get_optionset_attr(opt, "type");
if (groupproto == NULL ||
(protocol != NULL && strcmp(groupproto, protocol) != 0)) {
if (groupproto != NULL)
sa_free_attr_string(groupproto);
continue;
}
if (sa_is_share(share)) {
if ((ret = sa_proto_change_notify(share,
groupproto)) != SA_OK) {
ret = sa_enable_share(share, groupproto);
if (ret != SA_OK) {
(void) printf(
gettext("Could not reenable"
" share %s: %s\n"),
path, sa_errorstr(ret));
}
}
} else {
if ((ret = sa_proto_notify_resource(share,
groupproto)) != SA_OK) {
ret = sa_enable_resource(share, groupproto);
if (ret != SA_OK) {
(void) printf(
gettext("Could not "
"reenable resource %s: "
"%s\n"), path,
sa_errorstr(ret));
}
}
}
sa_free_attr_string(groupproto);
}
sa_free_attr_string(path);
}
static void
enable_group(sa_group_t group, char *updateproto, int notify, char *proto)
{
sa_share_t share;
if (!has_protocol(group, proto))
return;
for (share = sa_get_share(group, NULL);
share != NULL;
share = sa_get_next_share(share)) {
if (updateproto != NULL)
(void) sa_update_legacy(share, updateproto);
if (notify)
notify_or_enable_share(share, proto);
else
(void) sa_enable_share(share, proto);
}
}
static int
isenabled(sa_group_t group)
{
char *state;
int ret = B_FALSE;
if (group != NULL) {
state = sa_get_group_attr(group, "state");
if (state != NULL) {
if (strcmp(state, "enabled") == 0)
ret = B_TRUE;
sa_free_attr_string(state);
}
}
return (ret);
}
static int
enable_all_groups(sa_handle_t handle, struct list *work, int setstate,
int online, char *updateproto, int enable)
{
int ret;
char instance[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
char *state;
char *name;
char *zfs = NULL;
sa_group_t group;
sa_group_t subgroup;
for (ret = SA_OK; work != NULL; work = work->next) {
group = (sa_group_t)work->item;
if (setstate == B_TRUE) {
ret = sa_set_group_attr(group, "state", "enabled");
if (ret != SA_OK)
break;
}
if (!isenabled(group))
continue;
if (work->itemdata != NULL) {
if (enable) {
if (work->itemdata != NULL)
notify_or_enable_share(work->itemdata,
updateproto);
else
ret = SA_CONFIG_ERR;
} else {
if (sa_is_share(work->itemdata)) {
ret = sa_enable_share(
(sa_share_t)work->itemdata,
updateproto);
} else {
ret = sa_enable_resource(
(sa_resource_t)work->itemdata,
updateproto);
}
}
}
if (ret != SA_OK)
break;
if (work->itemdata == NULL) {
zfs = sa_get_group_attr(group, "zfs");
enable_group(group, zfs == NULL ? updateproto : NULL,
enable, work->proto);
if (zfs != NULL)
sa_free_attr_string(zfs);
for (subgroup = sa_get_sub_group(group);
subgroup != NULL;
subgroup = sa_get_next_group(subgroup)) {
enable_group(subgroup, NULL, enable,
work->proto);
}
}
if (online) {
zfs = sa_get_group_attr(group, "zfs");
name = sa_get_group_attr(group, "name");
if (name != NULL) {
if (zfs == NULL) {
(void) snprintf(instance,
sizeof (instance), "%s:%s",
SA_SVC_FMRI_BASE, name);
state = smf_get_state(instance);
if (state == NULL ||
strcmp(state, "online") != 0) {
(void) smf_enable_instance(
instance, 0);
free(state);
}
} else {
sa_free_attr_string(zfs);
zfs = NULL;
}
if (name != NULL)
sa_free_attr_string(name);
}
}
}
if (ret == SA_OK) {
ret = sa_update_config(handle);
}
return (ret);
}
static int
chk_opt(struct options *optlistp, int security, char *proto)
{
struct options *optlist;
char *sep = "";
int notfirst = 0;
int ret = OPT_ADD_OK;
for (optlist = optlistp; optlist != NULL; optlist = optlist->next) {
char *optname;
optname = optlist->optname;
ret = OPT_ADD_OK;
if (sa_is_security(optname, proto)) {
if (!security)
ret = OPT_ADD_SECURITY;
} else {
if (security)
ret = OPT_ADD_PROPERTY;
}
if (ret != OPT_ADD_OK) {
if (notfirst == 0)
(void) printf(
gettext("Property syntax error: "));
switch (ret) {
case OPT_ADD_SYNTAX:
(void) printf(gettext("%ssyntax error: %s"),
sep, optname);
sep = ", ";
break;
case OPT_ADD_SECURITY:
(void) printf(gettext("%s%s requires -S"),
optname, sep);
sep = ", ";
break;
case OPT_ADD_PROPERTY:
(void) printf(
gettext("%s%s not supported with -S"),
optname, sep);
sep = ", ";
break;
}
notfirst++;
}
}
if (notfirst) {
(void) printf("\n");
ret = SA_SYNTAX_ERR;
}
return (ret);
}
static void
free_opt(struct options *optlist)
{
struct options *nextopt;
while (optlist != NULL) {
nextopt = optlist->next;
free(optlist);
optlist = nextopt;
}
}
static int
valid_options(sa_handle_t handle, struct options *optlist, char *proto,
void *object, char *sec)
{
int ret = SA_OK;
struct options *cur;
sa_property_t prop;
sa_optionset_t parent = NULL;
if (object != NULL) {
if (sec == NULL)
parent = sa_get_optionset(object, proto);
else
parent = sa_get_security(object, sec, proto);
}
for (cur = optlist; cur != NULL; cur = cur->next) {
if (cur->optvalue == NULL)
continue;
prop = sa_create_property(cur->optname, cur->optvalue);
if (prop == NULL)
ret = SA_NO_MEMORY;
if (ret != SA_OK ||
(ret = sa_valid_property(handle, parent, proto, prop)) !=
SA_OK) {
(void) printf(
gettext("Could not add property %s: %s\n"),
cur->optname, sa_errorstr(ret));
}
(void) sa_remove_property(prop);
}
return (ret);
}
static int
add_optionset(sa_group_t group, struct options *optlist, char *proto, int *err)
{
sa_optionset_t optionset;
int ret = SA_OK;
int result = B_FALSE;
sa_handle_t handle;
optionset = sa_get_optionset(group, proto);
if (optionset == NULL) {
optionset = sa_create_optionset(group, proto);
if (optionset == NULL)
ret = SA_NO_MEMORY;
result = B_TRUE;
}
if (optionset == NULL) {
ret = SA_NO_MEMORY;
goto out;
}
handle = sa_find_group_handle(group);
if (handle == NULL) {
ret = SA_CONFIG_ERR;
goto out;
}
while (optlist != NULL) {
sa_property_t prop;
prop = sa_get_property(optionset, optlist->optname);
if (prop == NULL) {
if (optlist->optvalue != NULL) {
prop = sa_create_property(optlist->optname,
optlist->optvalue);
if (prop != NULL) {
ret = sa_valid_property(handle,
optionset, proto, prop);
if (ret != SA_OK) {
(void) sa_remove_property(prop);
(void) printf(gettext("Could "
"not add property "
"%s: %s\n"),
optlist->optname,
sa_errorstr(ret));
}
}
if (ret == SA_OK) {
ret = sa_add_property(optionset, prop);
if (ret != SA_OK) {
(void) printf(gettext(
"Could not add property "
"%s: %s\n"),
optlist->optname,
sa_errorstr(ret));
} else {
result = B_TRUE;
}
}
}
} else {
ret = sa_update_property(prop, optlist->optvalue);
if (ret != SA_OK) {
(void) printf(gettext("Could not update "
"property %s: %s\n"), optlist->optname,
sa_errorstr(ret));
} else {
result = B_TRUE;
}
}
optlist = optlist->next;
}
ret = sa_commit_properties(optionset, 0);
out:
if (err != NULL)
*err = ret;
return (result);
}
static int
resource_compliant(sa_group_t group)
{
sa_share_t share;
for (share = sa_get_share(group, NULL); share != NULL;
share = sa_get_next_share(share)) {
if (sa_get_share_resource(share, NULL) == NULL) {
return (B_FALSE);
}
}
return (B_TRUE);
}
static void
fix_path(char *path)
{
char *cp;
size_t len;
assert(path != NULL);
cp = path + 1;
while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) {
cp = strchr(cp, '/');
if (cp != NULL)
cp++;
}
if (cp == NULL) {
len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME;
(void) memmove(path, path + len, SA_MAX_RESOURCE_NAME);
path[SA_MAX_RESOURCE_NAME] = '\0';
} else {
len = strlen(cp) + 1;
(void) memmove(path, cp, len);
}
while (*path) {
switch (*path) {
case '/':
case '"':
case '\\':
case '[':
case ']':
case ':':
case '|':
case '<':
case '>':
case '+':
case ';':
case ',':
case '?':
case '*':
case '=':
case '\t':
*path = '_';
break;
}
path++;
}
}
#define MAX_MANGLE_NUMBER 10000
static int
name_adjust(char *path, int count)
{
size_t len;
len = strlen(path) - 2;
if (count > 10)
len--;
if (count > 100)
len--;
if (count > 1000)
len--;
if (len > 0)
(void) sprintf(path + len, "~%d", count);
else
return (SA_BAD_VALUE);
return (SA_OK);
}
static void
make_resources(sa_group_t group)
{
sa_share_t share;
int count;
int err = SA_OK;
for (share = sa_get_share(group, NULL); share != NULL;
share = sa_get_next_share(share)) {
if (sa_get_share_resource(share, NULL) == NULL) {
char *path;
path = sa_get_share_attr(share, "path");
if (path == NULL)
continue;
fix_path(path);
count = 0;
while (sa_add_resource(share, path,
SA_SHARE_PERMANENT, &err) == NULL &&
err == SA_DUPLICATE_NAME) {
int ret;
ret = name_adjust(path, count);
count++;
if (ret != SA_OK ||
count >= MAX_MANGLE_NUMBER) {
(void) printf(gettext(
"Cannot create resource name for"
" path: %s\n"), path);
break;
}
}
sa_free_attr_string(path);
}
}
}
static int
check_valid_group(sa_group_t group, char *groupname, char *protocol)
{
if (protocol != NULL) {
if (has_protocol(group, protocol)) {
(void) printf(gettext(
"Group \"%s\" already exists"
" with protocol %s\n"), groupname,
protocol);
return (SA_DUPLICATE_NAME);
} else if (strcmp(groupname, "default") == 0 &&
strcmp(protocol, "nfs") != 0) {
(void) printf(gettext(
"Group \"%s\" only allows protocol "
"\"%s\"\n"), groupname, "nfs");
return (SA_INVALID_PROTOCOL);
}
} else {
(void) printf(gettext(
"Group already exists and no protocol "
"specified.\n"));
return (SA_DUPLICATE_NAME);
}
return (SA_OK);
}
static int
enforce_featureset(sa_group_t group, char *protocol, boolean_t dryrun,
boolean_t force)
{
uint64_t features;
if (protocol == NULL)
return (SA_OK);
features = sa_proto_get_featureset(protocol);
if (!(features & SA_FEATURE_SERVER)) {
(void) printf(
gettext("Protocol \"%s\" not supported.\n"), protocol);
return (SA_INVALID_PROTOCOL);
}
if ((features & SA_FEATURE_RESOURCE) &&
!resource_compliant(group)) {
if (force && !dryrun) {
make_resources(group);
} else {
(void) printf(
gettext("Protocol requires resource names to be "
"set: %s\n"), protocol);
return (SA_RESOURCE_REQUIRED);
}
}
return (SA_OK);
}
static int
set_all_protocols(sa_group_t group)
{
char **protolist;
int numprotos, i;
uint64_t features;
sa_optionset_t optionset;
int ret = SA_OK;
numprotos = sa_get_protocols(&protolist);
for (i = 0; i < numprotos; i++) {
features = sa_proto_get_featureset(protolist[i]);
if (features & SA_FEATURE_SERVER) {
optionset = sa_create_optionset(group, protolist[i]);
if (optionset == NULL) {
ret = SA_NO_MEMORY;
break;
}
}
}
if (protolist != NULL)
free(protolist);
return (ret);
}
static int
sa_create(sa_handle_t handle, int flags, int argc, char *argv[])
{
char *groupname;
sa_group_t group;
boolean_t force = B_FALSE;
boolean_t verbose = B_FALSE;
boolean_t dryrun = B_FALSE;
int c;
char *protocol = NULL;
int ret = SA_OK;
struct options *optlist = NULL;
int err = SA_OK;
int auth;
boolean_t created = B_FALSE;
while ((c = getopt(argc, argv, "?fhvnP:p:")) != EOF) {
switch (c) {
case 'f':
force = B_TRUE;
break;
case 'v':
verbose = B_TRUE;
break;
case 'n':
dryrun = B_TRUE;
break;
case 'P':
if (protocol != NULL) {
(void) printf(gettext("Specifying "
"multiple protocols "
"not supported: %s\n"), protocol);
return (SA_SYNTAX_ERR);
}
protocol = optarg;
if (sa_valid_protocol(protocol))
break;
(void) printf(gettext(
"Invalid protocol specified: %s\n"), protocol);
return (SA_INVALID_PROTOCOL);
case 'p':
ret = add_opt(&optlist, optarg, 0);
switch (ret) {
case OPT_ADD_SYNTAX:
(void) printf(gettext(
"Property syntax error for property: %s\n"),
optarg);
return (SA_SYNTAX_ERR);
case OPT_ADD_SECURITY:
(void) printf(gettext(
"Security properties need "
"to be set with set-security: %s\n"),
optarg);
return (SA_SYNTAX_ERR);
default:
break;
}
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
err = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_CREATE));
return (err);
}
}
if (optind >= argc) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_CREATE));
(void) printf(gettext("\tgroup must be specified.\n"));
return (SA_BAD_PATH);
}
if ((optind + 1) < argc) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_CREATE));
(void) printf(gettext("\textraneous group(s) at end\n"));
return (SA_SYNTAX_ERR);
}
if (protocol == NULL && optlist != NULL) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_CREATE));
(void) printf(gettext("\tprotocol must be specified "
"with properties\n"));
return (SA_INVALID_PROTOCOL);
}
if (optlist != NULL)
ret = chk_opt(optlist, 0, protocol);
if (ret == OPT_ADD_SECURITY) {
(void) printf(gettext("Security properties not "
"supported with create\n"));
return (SA_SYNTAX_ERR);
}
groupname = argv[optind];
auth = check_authorizations(groupname, flags);
group = sa_get_group(handle, groupname);
if (group != NULL) {
ret = check_valid_group(group, groupname, protocol);
} else {
if (!sa_valid_group_name(groupname)) {
ret = SA_INVALID_NAME;
(void) printf(gettext("Invalid group name: %s\n"),
groupname);
}
}
if (ret == SA_OK) {
if (optlist != NULL) {
ret = valid_options(handle, optlist, protocol,
group, NULL);
}
}
if (ret == SA_OK && !dryrun) {
if (group == NULL) {
group = sa_create_group(handle, (char *)groupname,
&err);
created = B_TRUE;
}
if (group != NULL) {
sa_optionset_t optionset;
ret = enforce_featureset(group, protocol,
dryrun, force);
if (ret != SA_OK)
goto err;
if (optlist != NULL) {
(void) add_optionset(group, optlist, protocol,
&ret);
} else if (protocol != NULL) {
optionset = sa_create_optionset(group,
protocol);
if (optionset == NULL)
ret = SA_NO_MEMORY;
} else if (protocol == NULL) {
ret = set_all_protocols(group);
}
if (ret == SA_OK) {
ret = sa_update_config(handle);
} else {
if (group != NULL)
(void) sa_remove_group(group);
}
} else {
ret = err;
(void) printf(gettext("Could not create group: %s\n"),
sa_errorstr(ret));
}
}
if (dryrun && ret == SA_OK && !auth && verbose) {
(void) printf(gettext("Command would fail: %s\n"),
sa_errorstr(SA_NO_PERMISSION));
ret = SA_NO_PERMISSION;
}
err:
if (ret != SA_OK && created)
ret = sa_remove_group(group);
free_opt(optlist);
return (ret);
}
static char *
group_status(sa_group_t group)
{
char *state;
int enabled = 0;
state = sa_get_group_attr(group, "state");
if (state != NULL) {
if (strcmp(state, "enabled") == 0) {
enabled = 1;
}
sa_free_attr_string(state);
}
return (enabled ? "enabled" : "disabled");
}
static int
sa_delete(sa_handle_t handle, int flags, int argc, char *argv[])
{
char *groupname;
sa_group_t group;
sa_share_t share;
int verbose = 0;
int dryrun = 0;
int force = 0;
int c;
char *protocol = NULL;
char *sectype = NULL;
int ret = SA_OK;
int auth;
while ((c = getopt(argc, argv, "?hvnP:fS:")) != EOF) {
switch (c) {
case 'v':
verbose++;
break;
case 'n':
dryrun++;
break;
case 'P':
if (protocol != NULL) {
(void) printf(gettext("Specifying "
"multiple protocols "
"not supported: %s\n"), protocol);
return (SA_SYNTAX_ERR);
}
protocol = optarg;
if (!sa_valid_protocol(protocol)) {
(void) printf(gettext("Invalid protocol "
"specified: %s\n"), protocol);
return (SA_INVALID_PROTOCOL);
}
break;
case 'S':
if (sectype != NULL) {
(void) printf(gettext("Specifying "
"multiple property "
"spaces not supported: %s\n"), sectype);
return (SA_SYNTAX_ERR);
}
sectype = optarg;
break;
case 'f':
force++;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_DELETE));
return (ret);
}
}
if (optind >= argc) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_DELETE));
(void) printf(gettext("\tgroup must be specified.\n"));
return (SA_SYNTAX_ERR);
}
if ((optind + 1) < argc) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_DELETE));
(void) printf(gettext("\textraneous group(s) at end\n"));
return (SA_SYNTAX_ERR);
}
if (sectype != NULL && protocol == NULL) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_DELETE));
(void) printf(gettext("\tsecurity requires protocol to be "
"specified.\n"));
return (SA_SYNTAX_ERR);
}
groupname = argv[optind];
group = sa_get_group(handle, groupname);
if (group == NULL) {
ret = SA_NO_SUCH_GROUP;
goto done;
}
auth = check_authorizations(groupname, flags);
if (protocol == NULL) {
share = sa_get_share(group, NULL);
if (share != NULL)
ret = SA_BUSY;
if (share == NULL || (share != NULL && force == 1)) {
ret = SA_OK;
if (!dryrun) {
while (share != NULL) {
sa_share_t next_share;
next_share = sa_get_next_share(share);
ret = sa_disable_share(share, NULL);
ret = sa_remove_share(share);
share = next_share;
}
ret = sa_remove_group(group);
}
}
if (!dryrun && ret == SA_OK) {
ret = sa_update_config(handle);
}
} else {
sa_optionset_t optionset;
sa_security_t security;
if (sectype != NULL) {
security = sa_get_security(group, sectype, protocol);
if (security != NULL && !dryrun)
ret = sa_destroy_security(security);
else
ret = SA_INVALID_PROTOCOL;
} else {
optionset = sa_get_optionset(group, protocol);
if (optionset != NULL && !dryrun) {
ret = sa_destroy_optionset(optionset);
for (security =
sa_get_security(group, NULL, NULL);
ret == SA_OK && security != NULL;
security = sa_get_next_security(security)) {
char *secprot;
secprot = sa_get_security_attr(security,
"type");
if (secprot != NULL &&
strcmp(secprot, protocol) == 0)
ret = sa_destroy_security(
security);
if (secprot != NULL)
sa_free_attr_string(secprot);
}
} else {
if (!dryrun)
ret = SA_INVALID_PROTOCOL;
}
}
for (share = sa_get_share(group, NULL);
share != NULL;
share = sa_get_next_share(share)) {
(void) sa_delete_legacy(share, protocol);
}
}
done:
if (ret != SA_OK) {
(void) printf(gettext("Could not delete group: %s\n"),
sa_errorstr(ret));
} else if (dryrun && !auth && verbose) {
(void) printf(gettext("Command would fail: %s\n"),
sa_errorstr(SA_NO_PERMISSION));
}
return (ret);
}
static char *
strndupr(char *buff, char *str, int *buffsize)
{
int limit;
char *orig_buff = buff;
if (buff == NULL) {
buff = (char *)malloc(64);
if (buff == NULL)
return (NULL);
*buffsize = 64;
buff[0] = '\0';
}
limit = strlen(buff) + strlen(str) + 1;
if (limit > *buffsize) {
limit = *buffsize = *buffsize + ((limit / 64) + 64);
buff = realloc(buff, limit);
}
if (buff != NULL) {
(void) strcat(buff, str);
} else {
if (orig_buff != NULL)
free(orig_buff);
}
return (buff);
}
static char *
group_proto(sa_group_t group)
{
sa_optionset_t optionset;
char *proto;
char *buff = NULL;
int buffsize = 0;
int addspace = 0;
buff = strndupr(buff, "", &buffsize);
if (buff != NULL) {
for (optionset = sa_get_optionset(group, NULL);
optionset != NULL && buff != NULL;
optionset = sa_get_next_optionset(optionset)) {
proto = sa_get_optionset_attr(optionset, "type");
if (proto != NULL) {
if (addspace++)
buff = strndupr(buff, " ", &buffsize);
buff = strndupr(buff, proto, &buffsize);
sa_free_attr_string(proto);
}
}
}
return (buff);
}
static int
sa_list(sa_handle_t handle, int flags, int argc, char *argv[])
{
sa_group_t group;
int verbose = 0;
int c;
char *protocol = NULL;
int ret = SA_OK;
#ifdef lint
flags = flags;
#endif
while ((c = getopt(argc, argv, "?hvP:")) != EOF) {
switch (c) {
case 'v':
verbose++;
break;
case 'P':
if (protocol != NULL) {
(void) printf(gettext(
"Specifying multiple protocols "
"not supported: %s\n"),
protocol);
return (SA_SYNTAX_ERR);
}
protocol = optarg;
if (!sa_valid_protocol(protocol)) {
(void) printf(gettext(
"Invalid protocol specified: %s\n"),
protocol);
return (SA_INVALID_PROTOCOL);
}
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_LIST));
return (ret);
}
}
if (optind != argc) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_LIST));
return (SA_SYNTAX_ERR);
}
for (group = sa_get_group(handle, NULL);
group != NULL;
group = sa_get_next_group(group)) {
char *name;
char *proto;
if (protocol == NULL || has_protocol(group, protocol)) {
name = sa_get_group_attr(group, "name");
if (name != NULL && (verbose > 1 || name[0] != '#')) {
(void) printf("%s", (char *)name);
if (verbose) {
(void) printf("\t%s", isenabled(group) ?
gettext("enabled") :
gettext("disabled"));
proto = group_proto(group);
if (proto != NULL) {
(void) printf("\t%s",
(char *)proto);
free(proto);
}
}
(void) printf("\n");
}
if (name != NULL)
sa_free_attr_string(name);
}
}
return (0);
}
static void
out_properties(sa_optionset_t optionset, char *proto, char *sec)
{
char *type;
char *value;
int spacer;
sa_property_t prop;
if (sec == NULL)
(void) printf(" %s=(", proto ? proto : gettext("all"));
else
(void) printf(" %s:%s=(", proto ? proto : gettext("all"), sec);
for (spacer = 0, prop = sa_get_property(optionset, NULL);
prop != NULL;
prop = sa_get_next_property(prop)) {
type = sa_get_property_attr(prop, "type");
value = sa_get_property_attr(prop, "value");
if (type != NULL) {
(void) printf("%s%s=", spacer ? " " : "", type);
spacer = 1;
if (value != NULL)
(void) printf("\"%s\"", value);
else
(void) printf("\"\"");
}
if (type != NULL)
sa_free_attr_string(type);
if (value != NULL)
sa_free_attr_string(value);
}
(void) printf(")");
}
static void
show_properties(sa_group_t group, char *protocol, char *prefix)
{
sa_optionset_t optionset;
sa_security_t security;
char *value;
char *secvalue;
if (protocol != NULL) {
optionset = sa_get_optionset(group, protocol);
if (optionset != NULL) {
(void) printf("%s", prefix);
prefix = "";
out_properties(optionset, protocol, NULL);
}
security = sa_get_security(group, protocol, NULL);
if (security != NULL) {
(void) printf("%s", prefix);
prefix = "";
out_properties(security, protocol, NULL);
}
} else {
for (optionset = sa_get_optionset(group, protocol);
optionset != NULL;
optionset = sa_get_next_optionset(optionset)) {
value = sa_get_optionset_attr(optionset, "type");
(void) printf("%s", prefix);
prefix = "";
out_properties(optionset, value, 0);
if (value != NULL)
sa_free_attr_string(value);
}
for (security = sa_get_security(group, NULL, protocol);
security != NULL;
security = sa_get_next_security(security)) {
value = sa_get_security_attr(security, "type");
secvalue = sa_get_security_attr(security, "sectype");
(void) printf("%s", prefix);
prefix = "";
out_properties(security, value, secvalue);
if (value != NULL)
sa_free_attr_string(value);
if (secvalue != NULL)
sa_free_attr_string(secvalue);
}
}
}
static char *
get_resource(sa_share_t share)
{
sa_resource_t resource;
char *resstring = NULL;
char *retstring;
if ((resource = sa_get_share_resource(share, NULL)) != NULL) {
resstring = sa_get_resource_attr(resource, "name");
if (resstring != NULL) {
char *cp;
int len;
retstring = conv_from_utf8(resstring);
if (retstring != resstring) {
sa_free_attr_string(resstring);
resstring = retstring;
}
if (strpbrk(resstring, " ") != NULL) {
len = strlen(resstring) + 3;
cp = calloc(len, sizeof (char));
if (cp != NULL) {
(void) snprintf(cp, len,
"\"%s\"", resstring);
sa_free_attr_string(resstring);
resstring = cp;
} else {
sa_free_attr_string(resstring);
resstring = NULL;
}
}
}
}
return (resstring);
}
static int
has_resource_with_opt(sa_share_t share)
{
sa_resource_t resource;
int ret = B_FALSE;
for (resource = sa_get_share_resource(share, NULL);
resource != NULL;
resource = sa_get_next_resource(resource)) {
if (sa_get_optionset(resource, NULL) != NULL) {
ret = B_TRUE;
break;
}
}
return (ret);
}
static boolean_t
has_multiple_resource(sa_share_t share)
{
sa_resource_t resource;
int num;
for (num = 0, resource = sa_get_share_resource(share, NULL);
resource != NULL;
resource = sa_get_next_resource(resource)) {
num++;
if (num > 1)
return (B_TRUE);
}
return (B_FALSE);
}
static void
show_share(sa_share_t share, int verbose, int properties, char *proto,
int iszfs, char *sharepath)
{
char *drive;
char *exclude;
sa_resource_t resource = NULL;
char *description;
char *rsrcname;
int rsrcwithopt;
boolean_t multiple;
char *type;
rsrcwithopt = has_resource_with_opt(share);
if (verbose || (properties && rsrcwithopt)) {
type = sa_get_share_attr(share, "type");
if (type != NULL && !iszfs && verbose &&
strcmp(type, "transient") == 0)
(void) printf("\t* ");
else
(void) printf("\t ");
if (type != NULL)
sa_free_attr_string(type);
multiple = has_multiple_resource(share);
description = sa_get_share_description(share);
resource = sa_get_share_resource(share, NULL);
if (description != NULL && resource != NULL)
multiple = B_TRUE;
if (!multiple && !rsrcwithopt) {
rsrcname = get_resource(share);
if (rsrcname != NULL && strlen(rsrcname) > 0) {
(void) printf("%s=%s", rsrcname, sharepath);
} else {
(void) printf("%s", sharepath);
}
if (rsrcname != NULL)
sa_free_attr_string(rsrcname);
print_rsrc_desc(resource, description);
} else {
(void) printf("%s", sharepath);
}
drive = sa_get_share_attr(share, "drive-letter");
if (drive != NULL) {
if (strlen(drive) > 0)
(void) printf(gettext("\tdrive-letter=\"%s:\""),
drive);
sa_free_attr_string(drive);
}
if (properties)
show_properties(share, proto, "\t");
exclude = sa_get_share_attr(share, "exclude");
if (exclude != NULL) {
(void) printf(gettext("\tnot-shared-with=[%s]"),
exclude);
sa_free_attr_string(exclude);
}
if (description != NULL) {
print_rsrc_desc((sa_resource_t)share, description);
}
if (rsrcwithopt || multiple) {
for (resource = sa_get_share_resource(share, NULL);
resource != NULL;
resource = sa_get_next_resource(resource)) {
int has_space;
char *rsrc;
(void) printf("\n\t\t ");
rsrcname = sa_get_resource_attr(resource,
"name");
if (rsrcname == NULL)
continue;
rsrc = conv_from_utf8(rsrcname);
has_space = strpbrk(rsrc, " ") != NULL;
if (has_space)
(void) printf("\"%s\"=%s", rsrc,
sharepath);
else
(void) printf("%s=%s", rsrc,
sharepath);
if (rsrc != rsrcname)
sa_free_attr_string(rsrc);
sa_free_attr_string(rsrcname);
if (properties || rsrcwithopt)
show_properties(resource, proto, "\t");
print_rsrc_desc(resource, description);
}
}
if (description != NULL)
sa_free_share_description(description);
} else {
(void) printf("\t %s", sharepath);
if (properties)
show_properties(share, proto, "\t");
}
(void) printf("\n");
}
static void
show_group(sa_group_t group, int verbose, int properties, char *proto,
char *subgroup)
{
char *groupname;
char *zfs = NULL;
int iszfs = 0;
char *sharepath;
groupname = sa_get_group_attr(group, "name");
if (groupname != NULL) {
sa_share_t share;
if (proto != NULL && !has_protocol(group, proto)) {
sa_free_attr_string(groupname);
return;
}
zfs = sa_get_group_attr(group, "zfs");
if (zfs != NULL) {
iszfs = 1;
sa_free_attr_string(zfs);
}
if (subgroup == NULL)
(void) printf("%s", groupname);
else
(void) printf(" %s/%s", subgroup, groupname);
if (properties)
show_properties(group, proto, "");
(void) printf("\n");
if (strcmp(groupname, "zfs") == 0) {
sa_group_t zgroup;
for (zgroup = sa_get_sub_group(group);
zgroup != NULL;
zgroup = sa_get_next_group(zgroup)) {
show_group(zgroup, verbose, properties, proto,
"zfs");
}
sa_free_attr_string(groupname);
return;
}
for (share = sa_get_share(group, NULL);
share != NULL;
share = sa_get_next_share(share)) {
sharepath = sa_get_share_attr(share, "path");
if (sharepath != NULL) {
show_share(share, verbose, properties, proto,
iszfs, sharepath);
sa_free_attr_string(sharepath);
}
}
}
if (groupname != NULL) {
sa_free_attr_string(groupname);
}
}
xmlDocPtr
show_group_xml_init()
{
xmlDocPtr doc;
xmlNodePtr root;
doc = xmlNewDoc((xmlChar *)"1.0");
if (doc != NULL) {
root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
if (root != NULL)
(void) xmlDocSetRootElement(doc, root);
}
return (doc);
}
static void
show_group_xml(xmlDocPtr doc, sa_group_t group)
{
xmlNodePtr node;
xmlNodePtr root;
root = xmlDocGetRootElement(doc);
node = xmlCopyNode((xmlNodePtr)group, 1);
if (node != NULL && root != NULL) {
(void) xmlAddChild(root, node);
}
}
int
sa_show(sa_handle_t handle, int flags, int argc, char *argv[])
{
sa_group_t group;
int verbose = 0;
int properties = 0;
int c;
int ret = SA_OK;
char *protocol = NULL;
int xml = 0;
xmlDocPtr doc;
#ifdef lint
flags = flags;
#endif
while ((c = getopt(argc, argv, "?hvP:px")) != EOF) {
switch (c) {
case 'v':
verbose++;
break;
case 'p':
properties++;
break;
case 'P':
if (protocol != NULL) {
(void) printf(gettext(
"Specifying multiple protocols "
"not supported: %s\n"),
protocol);
return (SA_SYNTAX_ERR);
}
protocol = optarg;
if (!sa_valid_protocol(protocol)) {
(void) printf(gettext(
"Invalid protocol specified: %s\n"),
protocol);
return (SA_INVALID_PROTOCOL);
}
break;
case 'x':
xml++;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_SHOW));
return (ret);
}
}
if (xml) {
doc = show_group_xml_init();
if (doc == NULL)
ret = SA_NO_MEMORY;
}
if (optind == argc) {
for (group = sa_get_group(handle, NULL);
group != NULL;
group = sa_get_next_group(group)) {
if (xml)
show_group_xml(doc, group);
else
show_group(group, verbose, properties, protocol,
NULL);
}
} else {
for (; optind < argc; optind++) {
group = sa_get_group(handle, argv[optind]);
if (group != NULL) {
if (xml)
show_group_xml(doc, group);
else
show_group(group, verbose, properties,
protocol, NULL);
} else {
(void) printf(gettext("%s: not found\n"),
argv[optind]);
ret = SA_NO_SUCH_GROUP;
}
}
}
if (xml && ret == SA_OK) {
(void) xmlDocFormatDump(stdout, doc, 1);
xmlFreeDoc(doc);
}
return (ret);
}
static int
enable_share(sa_handle_t handle, sa_group_t group, sa_share_t share,
int update_legacy)
{
char *value;
int enabled;
sa_optionset_t optionset;
int err;
int ret = SA_OK;
char *zfs = NULL;
int iszfs = 0;
int isshare;
value = sa_get_group_attr(group, "state");
enabled = value != NULL && strcmp(value, "enabled") == 0;
if (value != NULL)
sa_free_attr_string(value);
if (update_legacy)
ret = sa_delete_legacy(share, NULL);
zfs = sa_get_group_attr(group, "zfs");
if (zfs != NULL) {
iszfs++;
sa_free_attr_string(zfs);
}
isshare = sa_is_share(share);
for (optionset = sa_get_optionset(group, NULL);
optionset != NULL && ret == SA_OK;
optionset = sa_get_next_optionset(optionset)) {
value = sa_get_optionset_attr(optionset, "type");
if (value != NULL) {
if (enabled) {
if (isshare) {
err = sa_enable_share(share, value);
} else {
err = sa_enable_resource(share, value);
if (err == SA_NOT_SUPPORTED) {
sa_share_t parent;
parent = sa_get_resource_parent(
share);
if (parent != NULL)
err = sa_enable_share(
parent, value);
}
}
if (err != SA_OK) {
ret = err;
(void) printf(gettext(
"Failed to enable share for "
"\"%s\": %s\n"),
value, sa_errorstr(ret));
}
}
if (update_legacy && !iszfs) {
sa_share_t update = share;
if (!sa_is_share(share)) {
update = sa_get_resource_parent(share);
}
(void) sa_update_legacy(update, value);
}
sa_free_attr_string(value);
}
}
if (ret == SA_OK)
(void) sa_update_config(handle);
return (ret);
}
static int
sa_require_resource(sa_group_t group)
{
sa_optionset_t optionset;
for (optionset = sa_get_optionset(group, NULL);
optionset != NULL;
optionset = sa_get_next_optionset(optionset)) {
char *proto;
proto = sa_get_optionset_attr(optionset, "type");
if (proto != NULL) {
uint64_t features;
features = sa_proto_get_featureset(proto);
if (features & SA_FEATURE_RESOURCE) {
sa_free_attr_string(proto);
return (B_TRUE);
}
sa_free_attr_string(proto);
}
}
return (B_FALSE);
}
static int
sa_addshare(sa_handle_t handle, int flags, int argc, char *argv[])
{
int verbose = 0;
int dryrun = 0;
int c;
int ret = SA_OK;
sa_group_t group;
sa_share_t share;
sa_resource_t resource = NULL;
char *sharepath = NULL;
char *description = NULL;
char *rsrcname = NULL;
char *rsrc = NULL;
int persist = SA_SHARE_PERMANENT;
int auth;
char dir[MAXPATHLEN];
while ((c = getopt(argc, argv, "?hvns:d:r:t")) != EOF) {
switch (c) {
case 'n':
dryrun++;
break;
case 'v':
verbose++;
break;
case 'd':
description = optarg;
break;
case 'r':
if (rsrcname != NULL) {
(void) printf(gettext("Adding multiple "
"resource names not"
" supported\n"));
return (SA_SYNTAX_ERR);
}
rsrcname = optarg;
break;
case 's':
if (sharepath != NULL) {
(void) printf(gettext(
"Adding multiple shares not supported\n"));
return (SA_SYNTAX_ERR);
}
sharepath = optarg;
break;
case 't':
persist = SA_SHARE_TRANSIENT;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_ADD_SHARE));
return (ret);
}
}
if (optind >= argc) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_ADD_SHARE));
if (dryrun || sharepath != NULL || description != NULL ||
rsrcname != NULL || verbose || persist) {
(void) printf(gettext("\tgroup must be specified\n"));
ret = SA_NO_SUCH_GROUP;
} else {
ret = SA_OK;
}
} else {
if (sharepath == NULL) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_ADD_SHARE));
(void) printf(gettext(
"\t-s sharepath must be specified\n"));
ret = SA_BAD_PATH;
}
if (ret == SA_OK) {
if (realpath(sharepath, dir) == NULL) {
ret = SA_BAD_PATH;
(void) printf(gettext("Path "
"is not valid: %s\n"),
sharepath);
} else {
sharepath = dir;
}
}
if (ret == SA_OK && rsrcname != NULL) {
if (validresource(rsrcname)) {
rsrc = conv_to_utf8(rsrcname);
resource = sa_find_resource(handle, rsrc);
if (resource != NULL) {
ret = SA_DUPLICATE_NAME;
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_ADD_SHARE));
(void) printf(gettext(
"\tresource names must be unique "
"in the system\n"));
}
} else {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_ADD_SHARE));
(void) printf(gettext(
"\tresource names use restricted "
"character set\n"));
ret = SA_INVALID_NAME;
}
}
if (ret != SA_OK) {
if (rsrc != NULL && rsrcname != rsrc)
sa_free_attr_string(rsrc);
return (ret);
}
share = sa_find_share(handle, sharepath);
if (share != NULL) {
if (rsrcname == NULL) {
ret = SA_DUPLICATE_NAME;
(void) printf(gettext("Share path already "
"shared: %s\n"), sharepath);
}
}
if (ret != SA_OK)
return (ret);
group = sa_get_group(handle, argv[optind]);
if (group != NULL) {
if (sa_require_resource(group) == B_TRUE &&
rsrcname == NULL) {
(void) printf(gettext(
"Resource name is required "
"by at least one enabled protocol "
"in group\n"));
return (SA_RESOURCE_REQUIRED);
}
if (share == NULL && ret == SA_OK) {
if (dryrun)
ret = sa_check_path(group, sharepath,
SA_CHECK_NORMAL);
else
share = sa_add_share(group, sharepath,
persist, &ret);
}
if (share != NULL) {
sa_group_t parent;
parent = sa_get_parent_group(share);
if (parent != group) {
ret = SA_DUPLICATE_NAME;
(void) printf(gettext(
"Share path already "
"shared: %s\n"), sharepath);
}
}
if (!dryrun && share == NULL) {
(void) printf(gettext(
"Could not add share: %s\n"),
sa_errorstr(ret));
} else {
auth = check_authorizations(argv[optind],
flags);
if (!dryrun && ret == SA_OK) {
if (rsrcname != NULL) {
resource = sa_add_resource(
share,
rsrc,
SA_SHARE_PERMANENT,
&ret);
}
if (ret == SA_OK &&
description != NULL) {
if (resource != NULL)
ret =
set_resource_desc(
resource,
description);
else
ret =
set_share_desc(
share,
description);
}
if (ret == SA_OK) {
if (resource != NULL) {
ret = enable_share(
handle,
group,
resource,
1);
} else {
ret = enable_share(
handle,
group,
share,
1);
}
ret = sa_update_config(handle);
}
switch (ret) {
case SA_DUPLICATE_NAME:
(void) printf(gettext(
"Resource name in"
"use: %s\n"),
rsrcname);
break;
default:
(void) printf(gettext(
"Could not set "
"attribute: %s\n"),
sa_errorstr(ret));
break;
case SA_OK:
break;
}
} else if (dryrun && ret == SA_OK &&
!auth && verbose) {
(void) printf(gettext(
"Command would fail: %s\n"),
sa_errorstr(SA_NO_PERMISSION));
ret = SA_NO_PERMISSION;
}
}
} else {
switch (ret) {
default:
(void) printf(gettext(
"Group \"%s\" not found\n"), argv[optind]);
ret = SA_NO_SUCH_GROUP;
break;
case SA_BAD_PATH:
case SA_DUPLICATE_NAME:
break;
}
}
}
return (ret);
}
int
sa_moveshare(sa_handle_t handle, int flags, int argc, char *argv[])
{
int verbose = 0;
int dryrun = 0;
int c;
int ret = SA_OK;
sa_group_t group;
sa_share_t share;
char *rsrcname = NULL;
char *sharepath = NULL;
int authsrc = 0, authdst = 0;
char dir[MAXPATHLEN];
while ((c = getopt(argc, argv, "?hvnr:s:")) != EOF) {
switch (c) {
case 'n':
dryrun++;
break;
case 'v':
verbose++;
break;
case 'r':
if (rsrcname != NULL) {
(void) printf(gettext(
"Moving multiple resource names not"
" supported\n"));
return (SA_SYNTAX_ERR);
}
rsrcname = optarg;
break;
case 's':
if (sharepath != NULL) {
(void) printf(gettext("Moving multiple shares"
" not supported\n"));
return (SA_SYNTAX_ERR);
}
sharepath = optarg;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_MOVE_SHARE));
return (ret);
}
}
if (optind >= argc || sharepath == NULL) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_MOVE_SHARE));
if (dryrun || verbose || sharepath != NULL) {
(void) printf(gettext("\tgroup must be specified\n"));
ret = SA_NO_SUCH_GROUP;
} else {
if (sharepath == NULL) {
ret = SA_SYNTAX_ERR;
(void) printf(gettext(
"\tsharepath must be specified\n"));
} else {
ret = SA_OK;
}
}
} else {
sa_group_t parent;
char *zfsold;
char *zfsnew;
if (sharepath == NULL) {
(void) printf(gettext(
"sharepath must be specified with the -s "
"option\n"));
return (SA_BAD_PATH);
}
group = sa_get_group(handle, argv[optind]);
if (group == NULL) {
(void) printf(gettext("Group \"%s\" not found\n"),
argv[optind]);
return (SA_NO_SUCH_GROUP);
}
share = sa_find_share(handle, sharepath);
if (share == NULL) {
if (realpath(sharepath, dir) == NULL) {
(void) printf(gettext("Path "
"is not valid: %s\n"),
sharepath);
return (SA_BAD_PATH);
}
sharepath = dir;
share = sa_find_share(handle, sharepath);
}
if (share == NULL) {
(void) printf(gettext("Share not found: %s\n"),
sharepath);
return (SA_NO_SUCH_PATH);
}
authdst = check_authorizations(argv[optind], flags);
parent = sa_get_parent_group(share);
if (parent != NULL) {
char *pname;
pname = sa_get_group_attr(parent, "name");
if (pname != NULL) {
authsrc = check_authorizations(pname, flags);
sa_free_attr_string(pname);
}
zfsold = sa_get_group_attr(parent, "zfs");
zfsnew = sa_get_group_attr(group, "zfs");
if ((zfsold != NULL && zfsnew == NULL) ||
(zfsold == NULL && zfsnew != NULL)) {
ret = SA_NOT_ALLOWED;
}
if (zfsold != NULL)
sa_free_attr_string(zfsold);
if (zfsnew != NULL)
sa_free_attr_string(zfsnew);
}
if (ret == SA_OK && parent != group && !dryrun) {
char *oldstate;
oldstate = sa_get_group_attr(parent, "state");
if (oldstate != NULL) {
if (strcmp(oldstate, "enabled") == 0)
(void) sa_disable_share(share, NULL);
sa_free_attr_string(oldstate);
}
}
if (!dryrun && ret == SA_OK)
ret = sa_move_share(group, share);
if (ret == SA_OK && parent != group && !dryrun) {
ret = sa_update_config(handle);
(void) enable_share(handle, group, share, 1);
}
if (ret != SA_OK)
(void) printf(gettext("Could not move share: %s\n"),
sa_errorstr(ret));
if (dryrun && ret == SA_OK && !(authsrc & authdst) &&
verbose) {
(void) printf(gettext("Command would fail: %s\n"),
sa_errorstr(SA_NO_PERMISSION));
}
}
return (ret);
}
int
sa_removeshare(sa_handle_t handle, int flags, int argc, char *argv[])
{
int verbose = 0;
int dryrun = 0;
int force = 0;
int c;
int ret = SA_OK;
sa_group_t group;
sa_resource_t resource = NULL;
sa_share_t share = NULL;
char *rsrcname = NULL;
char *sharepath = NULL;
char dir[MAXPATHLEN];
while ((c = getopt(argc, argv, "?hfnr:s:v")) != EOF) {
switch (c) {
case 'n':
dryrun++;
break;
case 'v':
verbose++;
break;
case 'f':
force++;
break;
case 's':
if (sharepath != NULL) {
(void) printf(gettext(
"Removing multiple shares not "
"supported\n"));
return (SA_SYNTAX_ERR);
}
sharepath = optarg;
break;
case 'r':
if (rsrcname != NULL) {
(void) printf(gettext(
"Removing multiple resource names not "
"supported\n"));
return (SA_SYNTAX_ERR);
}
rsrcname = optarg;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_REMOVE_SHARE));
return (ret);
}
}
if (optind >= argc || (rsrcname == NULL && sharepath == NULL)) {
if (sharepath == NULL && rsrcname == NULL) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_REMOVE_SHARE));
(void) printf(gettext("\t-s sharepath or -r resource"
" must be specified\n"));
ret = SA_BAD_PATH;
} else {
ret = SA_OK;
}
}
if (ret != SA_OK) {
return (ret);
}
if (optind < argc) {
if ((optind + 1) < argc) {
(void) printf(gettext("Extraneous group(s) at end of "
"command\n"));
ret = SA_SYNTAX_ERR;
} else {
group = sa_get_group(handle, argv[optind]);
if (group == NULL) {
(void) printf(gettext(
"Group \"%s\" not found\n"), argv[optind]);
ret = SA_NO_SUCH_GROUP;
}
}
} else {
group = NULL;
}
if (rsrcname != NULL) {
resource = sa_find_resource(handle, rsrcname);
if (resource == NULL) {
ret = SA_NO_SUCH_RESOURCE;
(void) printf(gettext(
"Resource name not found for share: %s\n"),
rsrcname);
}
}
if (ret == SA_OK) {
if (sharepath != NULL) {
if (group != NULL)
share = sa_get_share(group, sharepath);
else
share = sa_find_share(handle, sharepath);
}
if (resource != NULL) {
sa_share_t rsrcshare;
rsrcshare = sa_get_resource_parent(resource);
if (share == NULL)
share = rsrcshare;
else if (share != rsrcshare) {
ret = SA_NO_SUCH_RESOURCE;
(void) printf(gettext(
"Bad resource name for share: %s\n"),
rsrcname);
share = NULL;
}
}
if (share == NULL) {
if (realpath(sharepath, dir) == NULL) {
ret = SA_BAD_PATH;
(void) printf(gettext(
"Path is not valid: %s\n"), sharepath);
} else {
if (group != NULL)
share = sa_get_share(group, dir);
else
share = sa_find_share(handle, dir);
}
}
}
if (ret != SA_OK) {
return (ret);
}
if (share == NULL) {
if (group != NULL)
(void) printf(gettext("Share not found in group %s:"
" %s\n"), argv[optind], sharepath);
else
(void) printf(gettext("Share not found: %s\n"),
sharepath);
ret = SA_NO_SUCH_PATH;
} else {
if (group == NULL)
group = sa_get_parent_group(share);
if (!dryrun) {
if (ret == SA_OK) {
if (resource != NULL)
ret = sa_disable_resource(resource,
NULL);
else
ret = sa_disable_share(share, NULL);
if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
ret == SA_NOT_SUPPORTED ||
ret == SA_SYSTEM_ERR || force) &&
resource == NULL)
ret = sa_remove_share(share);
if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
ret == SA_NOT_SUPPORTED ||
ret == SA_SYSTEM_ERR || force) &&
resource != NULL) {
ret = sa_remove_resource(resource);
if (ret == SA_OK) {
resource =
sa_get_share_resource(
share, NULL);
if (resource == NULL)
ret = sa_remove_share(
share);
}
}
if (ret == SA_OK)
ret = sa_update_config(handle);
}
if (ret != SA_OK)
(void) printf(gettext("Could not remove share:"
" %s\n"), sa_errorstr(ret));
} else if (ret == SA_OK) {
char *pname;
pname = sa_get_group_attr(group, "name");
if (pname != NULL) {
int auth;
auth = check_authorizations(pname, flags);
if (!auth && verbose) {
(void) printf(gettext(
"Command would fail: %s\n"),
sa_errorstr(SA_NO_PERMISSION));
}
sa_free_attr_string(pname);
}
}
}
return (ret);
}
int
sa_set_share(sa_handle_t handle, int flags, int argc, char *argv[])
{
int dryrun = 0;
int c;
int ret = SA_OK;
sa_group_t group, sharegroup;
sa_share_t share = NULL;
sa_resource_t resource = NULL;
char *sharepath = NULL;
char *description = NULL;
char *rsrcname = NULL;
char *rsrc = NULL;
char *newname = NULL;
char *newrsrc = NULL;
char *groupname = NULL;
int auth = 1;
int verbose = 0;
while ((c = getopt(argc, argv, "?hnd:r:s:")) != EOF) {
switch (c) {
case 'n':
dryrun++;
break;
case 'd':
description = optarg;
break;
case 'v':
verbose++;
break;
case 'r':
if (rsrcname != NULL) {
(void) printf(gettext(
"Updating multiple resource names not "
"supported\n"));
return (SA_SYNTAX_ERR);
}
rsrcname = optarg;
break;
case 's':
if (sharepath != NULL) {
(void) printf(gettext(
"Updating multiple shares not "
"supported\n"));
return (SA_SYNTAX_ERR);
}
sharepath = optarg;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_SET_SHARE));
return (ret);
}
}
if (optind >= argc && sharepath == NULL && rsrcname == NULL) {
if (sharepath == NULL) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_SET_SHARE));
(void) printf(gettext("\tgroup must be specified\n"));
ret = SA_BAD_PATH;
} else {
ret = SA_OK;
}
}
if ((optind + 1) < argc) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_SET_SHARE));
(void) printf(gettext("\tExtraneous group(s) at end\n"));
ret = SA_SYNTAX_ERR;
}
if (sharepath == NULL && rsrcname == NULL) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_SET_SHARE));
ret = SA_SYNTAX_ERR;
}
if (ret != SA_OK)
return (ret);
if (optind < argc) {
groupname = argv[optind];
group = sa_get_group(handle, groupname);
} else {
group = NULL;
groupname = NULL;
}
if (rsrcname != NULL) {
newname = strchr(rsrcname, '=');
if (newname != NULL) {
*newname++ = '\0';
}
if (!validresource(rsrcname)) {
ret = SA_INVALID_NAME;
(void) printf(gettext("Invalid resource name: "
"\"%s\"\n"), rsrcname);
} else {
rsrc = conv_to_utf8(rsrcname);
}
if (newname != NULL) {
if (!validresource(newname)) {
ret = SA_INVALID_NAME;
(void) printf(gettext("Invalid resource name: "
"%s\n"), newname);
newname = NULL;
} else {
newrsrc = conv_to_utf8(newname);
}
}
}
if (ret != SA_OK) {
if (rsrcname != NULL && rsrcname != rsrc)
sa_free_attr_string(rsrc);
if (newname != NULL && newname != newrsrc)
sa_free_attr_string(newrsrc);
return (ret);
}
if (sharepath != NULL) {
share = sa_find_share(handle, sharepath);
} else if (rsrcname != NULL) {
resource = sa_find_resource(handle, rsrc);
if (resource != NULL)
share = sa_get_resource_parent(resource);
else
ret = SA_NO_SUCH_RESOURCE;
}
if (share != NULL) {
sharegroup = sa_get_parent_group(share);
if (group != NULL && group != sharegroup) {
(void) printf(gettext("Group \"%s\" does not contain "
"share %s\n"),
argv[optind], sharepath);
ret = SA_BAD_PATH;
} else {
int delgroupname = 0;
if (groupname == NULL) {
groupname = sa_get_group_attr(sharegroup,
"name");
delgroupname = 1;
}
if (groupname != NULL) {
auth = check_authorizations(groupname, flags);
if (delgroupname) {
sa_free_attr_string(groupname);
groupname = NULL;
}
} else {
ret = SA_NO_MEMORY;
}
if (rsrcname != NULL) {
resource = sa_find_resource(handle, rsrc);
if (!dryrun) {
if (newname != NULL &&
resource != NULL)
ret = sa_rename_resource(
resource, newrsrc);
else if (newname != NULL)
ret = SA_NO_SUCH_RESOURCE;
if (newname != NULL &&
newname != newrsrc)
sa_free_attr_string(newrsrc);
}
if (rsrc != rsrcname)
sa_free_attr_string(rsrc);
}
if (!dryrun && ret == SA_OK && description != NULL) {
char *desc;
desc = conv_to_utf8(description);
if (resource != NULL)
ret = sa_set_resource_description(
resource, desc);
else
ret = sa_set_share_description(share,
desc);
if (desc != description)
sa_free_share_description(desc);
}
}
if (!dryrun && ret == SA_OK) {
if (resource != NULL)
(void) sa_enable_resource(resource, NULL);
ret = sa_update_config(handle);
}
switch (ret) {
case SA_DUPLICATE_NAME:
(void) printf(gettext("Resource name in use: %s\n"),
rsrcname);
break;
default:
(void) printf(gettext("Could not set: %s\n"),
sa_errorstr(ret));
break;
case SA_OK:
if (dryrun && !auth && verbose) {
(void) printf(gettext(
"Command would fail: %s\n"),
sa_errorstr(SA_NO_PERMISSION));
}
break;
}
} else {
switch (ret) {
case SA_NO_SUCH_RESOURCE:
(void) printf(gettext("Resource \"%s\" not found\n"),
rsrcname);
break;
default:
if (sharepath != NULL) {
(void) printf(
gettext("Share path \"%s\" not found\n"),
sharepath);
ret = SA_NO_SUCH_PATH;
} else {
(void) printf(gettext("Set failed: %s\n"),
sa_errorstr(ret));
}
}
}
return (ret);
}
static int
add_security(sa_group_t group, char *sectype,
struct options *optlist, char *proto, int *err)
{
sa_security_t security;
int ret = SA_OK;
int result = 0;
sa_handle_t handle;
sectype = sa_proto_space_alias(proto, sectype);
security = sa_get_security(group, sectype, proto);
if (security == NULL)
security = sa_create_security(group, sectype, proto);
if (sectype != NULL)
sa_free_attr_string(sectype);
if (security == NULL)
goto done;
handle = sa_find_group_handle(group);
if (handle == NULL) {
ret = SA_CONFIG_ERR;
goto done;
}
while (optlist != NULL) {
sa_property_t prop;
prop = sa_get_property(security, optlist->optname);
if (prop == NULL) {
if (optlist->optvalue != NULL) {
prop = sa_create_property(optlist->optname,
optlist->optvalue);
if (prop != NULL) {
ret = sa_valid_property(handle,
security, proto, prop);
if (ret != SA_OK) {
(void) sa_remove_property(prop);
(void) printf(gettext(
"Could not add "
"property %s: %s\n"),
optlist->optname,
sa_errorstr(ret));
}
if (ret == SA_OK) {
ret = sa_add_property(security,
prop);
if (ret != SA_OK) {
(void) printf(gettext(
"Could not add "
"property (%s=%s):"
" %s\n"),
optlist->optname,
optlist->optvalue,
sa_errorstr(ret));
} else {
result = 1;
}
}
}
}
} else {
ret = sa_update_property(prop, optlist->optvalue);
result = 1;
}
optlist = optlist->next;
}
if (result)
ret = sa_commit_properties(security, 0);
done:
*err = ret;
return (result);
}
static int
zfscheck(sa_group_t group, sa_share_t share)
{
int ret = SA_OK;
char *attr;
if (sa_group_is_zfs(group)) {
ret = SA_NOT_ALLOWED;
attr = sa_get_share_attr(share, "subgroup");
if (attr != NULL) {
if (strcmp(attr, "true") == 0)
ret = SA_OK;
sa_free_attr_string(attr);
}
}
return (ret);
}
static int
basic_set(sa_handle_t handle, char *groupname, struct options *optlist,
char *protocol, char *sharepath, char *rsrcname, int dryrun)
{
sa_group_t group;
int ret = SA_OK;
int change = 0;
struct list *worklist = NULL;
group = sa_get_group(handle, groupname);
if (group != NULL) {
sa_share_t share = NULL;
sa_resource_t resource = NULL;
if (sharepath != NULL) {
share = sa_get_share(group, sharepath);
if (share == NULL) {
(void) printf(gettext(
"Share does not exist in group %s\n"),
groupname, sharepath);
ret = SA_NO_SUCH_PATH;
} else {
ret = zfscheck(group, share);
if (ret == SA_OK &&
sa_group_is_zfs(group))
share = NULL;
if (ret == SA_NOT_ALLOWED)
(void) printf(gettext(
"Properties on ZFS group shares "
"not supported: %s\n"), sharepath);
}
}
if (rsrcname != NULL) {
if (share != NULL) {
resource = sa_get_share_resource(share,
rsrcname);
if (resource == NULL)
ret = SA_NO_SUCH_RESOURCE;
} else {
resource = sa_get_resource(group, rsrcname);
if (resource != NULL)
share = sa_get_resource_parent(
resource);
else
ret = SA_NO_SUCH_RESOURCE;
}
if (ret == SA_OK && resource != NULL) {
uint64_t features;
features = sa_proto_get_featureset(protocol);
if (features & SA_FEATURE_RESOURCE)
share = (sa_share_t)resource;
}
}
if (ret == SA_OK) {
ret = valid_options(handle, optlist, protocol,
share == NULL ? group : share, NULL);
if (ret == SA_OK && !dryrun) {
if (share != NULL)
change |= add_optionset(share, optlist,
protocol, &ret);
else
change |= add_optionset(group, optlist,
protocol, &ret);
if (ret == SA_OK && change)
worklist = add_list(worklist, group,
share, protocol);
}
}
free_opt(optlist);
} else {
(void) printf(gettext("Group \"%s\" not found\n"), groupname);
ret = SA_NO_SUCH_GROUP;
}
if (!dryrun && ret == SA_OK && change && worklist != NULL)
(void) enable_all_groups(handle, worklist, 0, 0, protocol,
B_TRUE);
if (worklist != NULL)
free_list(worklist);
return (ret);
}
static int
space_set(sa_handle_t handle, char *groupname, struct options *optlist,
char *protocol, char *sharepath, int dryrun, char *sectype)
{
sa_group_t group;
int ret = SA_OK;
int change = 0;
struct list *worklist = NULL;
if (sa_proto_valid_space(protocol, sectype) == 0) {
(void) printf(gettext("Option space \"%s\" not valid "
"for protocol.\n"), sectype);
return (SA_INVALID_SECURITY);
}
group = sa_get_group(handle, groupname);
if (group != NULL) {
sa_share_t share = NULL;
if (sharepath != NULL) {
share = sa_get_share(group, sharepath);
if (share == NULL) {
(void) printf(gettext(
"Share does not exist in group %s\n"),
groupname, sharepath);
ret = SA_NO_SUCH_PATH;
} else {
ret = zfscheck(group, share);
if (ret == SA_OK &&
sa_group_is_zfs(group))
share = NULL;
if (ret == SA_NOT_ALLOWED)
(void) printf(gettext(
"Properties on ZFS group shares "
"not supported: %s\n"), sharepath);
}
}
if (ret == SA_OK) {
ret = valid_options(handle, optlist, protocol,
share == NULL ? group : share, sectype);
if (ret == SA_OK && !dryrun) {
if (share != NULL)
change = add_security(share, sectype,
optlist, protocol, &ret);
else
change = add_security(group, sectype,
optlist, protocol, &ret);
if (ret != SA_OK)
(void) printf(gettext(
"Could not set property: %s\n"),
sa_errorstr(ret));
}
if (ret == SA_OK && change)
worklist = add_list(worklist, group, share,
protocol);
}
free_opt(optlist);
} else {
(void) printf(gettext("Group \"%s\" not found\n"), groupname);
ret = SA_NO_SUCH_GROUP;
}
if (!dryrun && ret == 0) {
if (change && worklist != NULL) {
(void) enable_all_groups(handle, worklist, 0, 0,
protocol, B_TRUE);
}
ret = sa_update_config(handle);
}
if (worklist != NULL)
free_list(worklist);
return (ret);
}
int
sa_set(sa_handle_t handle, int flags, int argc, char *argv[])
{
char *groupname;
int verbose = 0;
int dryrun = 0;
int c;
char *protocol = NULL;
int ret = SA_OK;
struct options *optlist = NULL;
char *rsrcname = NULL;
char *sharepath = NULL;
char *optset = NULL;
int auth;
while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
switch (c) {
case 'v':
verbose++;
break;
case 'n':
dryrun++;
break;
case 'P':
if (protocol != NULL) {
(void) printf(gettext(
"Specifying multiple protocols "
"not supported: %s\n"), protocol);
return (SA_SYNTAX_ERR);
}
protocol = optarg;
if (!sa_valid_protocol(protocol)) {
(void) printf(gettext(
"Invalid protocol specified: %s\n"),
protocol);
return (SA_INVALID_PROTOCOL);
}
break;
case 'p':
ret = add_opt(&optlist, optarg, 0);
switch (ret) {
case OPT_ADD_SYNTAX:
(void) printf(gettext("Property syntax error:"
" %s\n"), optarg);
return (SA_SYNTAX_ERR);
case OPT_ADD_MEMORY:
(void) printf(gettext("No memory to set "
"property: %s\n"), optarg);
return (SA_NO_MEMORY);
default:
break;
}
break;
case 'r':
if (rsrcname != NULL) {
(void) printf(gettext(
"Setting multiple resource names not"
" supported\n"));
return (SA_SYNTAX_ERR);
}
rsrcname = optarg;
break;
case 's':
if (sharepath != NULL) {
(void) printf(gettext(
"Setting multiple shares not supported\n"));
return (SA_SYNTAX_ERR);
}
sharepath = optarg;
break;
case 'S':
if (optset != NULL) {
(void) printf(gettext(
"Specifying multiple property "
"spaces not supported: %s\n"), optset);
return (SA_SYNTAX_ERR);
}
optset = optarg;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_SET));
return (ret);
}
}
if (optlist != NULL)
ret = chk_opt(optlist, optset != NULL, protocol);
if (optind >= argc || (optlist == NULL && optset == NULL) ||
protocol == NULL || ret != OPT_ADD_OK) {
char *sep = "\t";
(void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET));
if (optind >= argc) {
(void) printf(gettext("%sgroup must be specified"),
sep);
sep = ", ";
}
if (optlist == NULL) {
(void) printf(gettext("%sat least one property must be"
" specified"), sep);
sep = ", ";
}
if (protocol == NULL) {
(void) printf(gettext("%sprotocol must be specified"),
sep);
sep = ", ";
}
(void) printf("\n");
ret = SA_SYNTAX_ERR;
} else {
groupname = argv[optind];
if (strcmp(groupname, "zfs") == 0) {
(void) printf(gettext("Changing properties for group "
"\"zfs\" not allowed\n"));
return (SA_NOT_ALLOWED);
}
auth = check_authorizations(groupname, flags);
if (optset == NULL)
ret = basic_set(handle, groupname, optlist, protocol,
sharepath, rsrcname, dryrun);
else
ret = space_set(handle, groupname, optlist, protocol,
sharepath, dryrun, optset);
if (dryrun && ret == SA_OK && !auth && verbose) {
(void) printf(gettext("Command would fail: %s\n"),
sa_errorstr(SA_NO_PERMISSION));
}
}
return (ret);
}
static int
remove_options(sa_group_t group, struct options *optlist,
char *proto, int *err)
{
struct options *cur;
sa_optionset_t optionset;
sa_property_t prop;
int change = 0;
int ret = SA_OK;
optionset = sa_get_optionset(group, proto);
if (optionset != NULL) {
for (cur = optlist; cur != NULL; cur = cur->next) {
prop = sa_get_property(optionset, cur->optname);
if (prop != NULL) {
ret = sa_remove_property(prop);
if (ret != SA_OK)
break;
change = 1;
}
}
}
if (ret == SA_OK && change)
ret = sa_commit_properties(optionset, 0);
if (err != NULL)
*err = ret;
return (change);
}
static int
valid_unset(sa_group_t group, struct options *optlist, char *proto)
{
struct options *cur;
sa_optionset_t optionset;
sa_property_t prop;
int ret = SA_OK;
optionset = sa_get_optionset(group, proto);
if (optionset != NULL) {
for (cur = optlist; cur != NULL; cur = cur->next) {
prop = sa_get_property(optionset, cur->optname);
if (prop == NULL) {
(void) printf(gettext(
"Could not unset property %s: not set\n"),
cur->optname);
ret = SA_NO_SUCH_PROP;
}
}
}
return (ret);
}
static int
valid_unset_security(sa_group_t group, struct options *optlist, char *proto,
char *sectype)
{
struct options *cur;
sa_security_t security;
sa_property_t prop;
int ret = SA_OK;
char *sec;
sec = sa_proto_space_alias(proto, sectype);
security = sa_get_security(group, sec, proto);
if (security != NULL) {
for (cur = optlist; cur != NULL; cur = cur->next) {
prop = sa_get_property(security, cur->optname);
if (prop == NULL) {
(void) printf(gettext(
"Could not unset property %s: not set\n"),
cur->optname);
ret = SA_NO_SUCH_PROP;
}
}
} else {
(void) printf(gettext(
"Could not unset %s: space not defined\n"), sectype);
ret = SA_NO_SUCH_SECURITY;
}
if (sec != NULL)
sa_free_attr_string(sec);
return (ret);
}
static int
remove_security(sa_group_t group, char *sectype,
struct options *optlist, char *proto, int *err)
{
sa_security_t security;
int ret = SA_OK;
int change = 0;
sectype = sa_proto_space_alias(proto, sectype);
security = sa_get_security(group, sectype, proto);
if (sectype != NULL)
sa_free_attr_string(sectype);
if (security != NULL) {
while (optlist != NULL) {
sa_property_t prop;
prop = sa_get_property(security, optlist->optname);
if (prop != NULL) {
ret = sa_remove_property(prop);
if (ret != SA_OK)
break;
change = 1;
}
optlist = optlist->next;
}
if (ret == SA_OK && change)
ret = sa_commit_properties(security, 0);
} else {
ret = SA_NO_SUCH_PROP;
}
if (err != NULL)
*err = ret;
return (change);
}
static int
basic_unset(sa_handle_t handle, char *groupname, struct options *optlist,
char *protocol, char *sharepath, char *rsrcname, int dryrun)
{
sa_group_t group;
int ret = SA_OK;
int change = 0;
struct list *worklist = NULL;
sa_share_t share = NULL;
sa_resource_t resource = NULL;
group = sa_get_group(handle, groupname);
if (group == NULL)
return (ret);
if (sharepath != NULL) {
share = sa_get_share(group, sharepath);
if (share == NULL) {
(void) printf(gettext(
"Share does not exist in group %s\n"),
groupname, sharepath);
ret = SA_NO_SUCH_PATH;
}
}
if (rsrcname != NULL) {
if (share != NULL) {
resource = sa_get_share_resource(share, rsrcname);
if (resource == NULL)
ret = SA_NO_SUCH_RESOURCE;
} else {
resource = sa_get_resource(group, rsrcname);
if (resource != NULL) {
share = sa_get_resource_parent(resource);
} else {
ret = SA_NO_SUCH_RESOURCE;
}
}
if (ret == SA_OK && resource != NULL) {
uint64_t features;
features = sa_proto_get_featureset(protocol);
if (features & SA_FEATURE_RESOURCE)
share = (sa_share_t)resource;
}
}
if (ret == SA_OK) {
ret = valid_unset(share != NULL ? share : group,
optlist, protocol);
if (ret == SA_OK && !dryrun) {
if (share != NULL) {
sa_optionset_t optionset;
sa_property_t prop;
change |= remove_options(share, optlist,
protocol, &ret);
optionset = sa_get_optionset((sa_share_t)share,
protocol);
if (optionset != NULL) {
prop = sa_get_property(optionset, NULL);
if (prop == NULL)
(void) sa_destroy_optionset(
optionset);
}
} else {
change |= remove_options(group,
optlist, protocol, &ret);
}
if (ret == SA_OK && change)
worklist = add_list(worklist, group, share,
protocol);
if (ret != SA_OK)
(void) printf(gettext(
"Could not remove properties: "
"%s\n"), sa_errorstr(ret));
}
} else {
(void) printf(gettext("Group \"%s\" not found\n"), groupname);
ret = SA_NO_SUCH_GROUP;
}
free_opt(optlist);
if (!dryrun && ret == SA_OK) {
if (change && worklist != NULL) {
(void) enable_all_groups(handle, worklist, 0, 0,
protocol, B_TRUE);
}
}
if (worklist != NULL)
free_list(worklist);
return (ret);
}
static int
space_unset(sa_handle_t handle, char *groupname, struct options *optlist,
char *protocol, char *sharepath, int dryrun, char *sectype)
{
sa_group_t group;
int ret = SA_OK;
int change = 0;
struct list *worklist = NULL;
sa_share_t share = NULL;
group = sa_get_group(handle, groupname);
if (group == NULL) {
(void) printf(gettext("Group \"%s\" not found\n"), groupname);
return (SA_NO_SUCH_GROUP);
}
if (sharepath != NULL) {
share = sa_get_share(group, sharepath);
if (share == NULL) {
(void) printf(gettext(
"Share does not exist in group %s\n"),
groupname, sharepath);
return (SA_NO_SUCH_PATH);
}
}
ret = valid_unset_security(share != NULL ? share : group,
optlist, protocol, sectype);
if (ret == SA_OK && !dryrun) {
if (optlist != NULL) {
if (share != NULL) {
sa_security_t optionset;
sa_property_t prop;
change = remove_security(share,
sectype, optlist, protocol, &ret);
optionset = sa_get_security((sa_group_t)share,
sectype, protocol);
if (optionset != NULL) {
prop = sa_get_property(optionset,
NULL);
if (prop == NULL)
ret = sa_destroy_security(
optionset);
}
} else {
change = remove_security(group, sectype,
optlist, protocol, &ret);
}
} else {
sa_security_t security;
char *sec;
sec = sa_proto_space_alias(protocol, sectype);
security = sa_get_security(group, sec, protocol);
if (sec != NULL)
sa_free_attr_string(sec);
if (security != NULL) {
ret = sa_destroy_security(security);
if (ret == SA_OK)
change = 1;
} else {
ret = SA_NO_SUCH_PROP;
}
}
if (ret != SA_OK)
(void) printf(gettext("Could not unset property: %s\n"),
sa_errorstr(ret));
}
if (ret == SA_OK && change)
worklist = add_list(worklist, group, 0, protocol);
free_opt(optlist);
if (!dryrun && ret == 0) {
if (change && worklist != NULL)
(void) enable_all_groups(handle, worklist, 0, 0,
protocol, B_TRUE);
ret = sa_update_config(handle);
}
if (worklist != NULL)
free_list(worklist);
return (ret);
}
int
sa_unset(sa_handle_t handle, int flags, int argc, char *argv[])
{
char *groupname;
int verbose = 0;
int dryrun = 0;
int c;
char *protocol = NULL;
int ret = SA_OK;
struct options *optlist = NULL;
char *rsrcname = NULL;
char *sharepath = NULL;
char *optset = NULL;
int auth;
while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
switch (c) {
case 'v':
verbose++;
break;
case 'n':
dryrun++;
break;
case 'P':
if (protocol != NULL) {
(void) printf(gettext(
"Specifying multiple protocols "
"not supported: %s\n"), protocol);
return (SA_SYNTAX_ERR);
}
protocol = optarg;
if (!sa_valid_protocol(protocol)) {
(void) printf(gettext(
"Invalid protocol specified: %s\n"),
protocol);
return (SA_INVALID_PROTOCOL);
}
break;
case 'p':
ret = add_opt(&optlist, optarg, 1);
switch (ret) {
case OPT_ADD_SYNTAX:
(void) printf(gettext("Property syntax error "
"for property %s\n"), optarg);
return (SA_SYNTAX_ERR);
case OPT_ADD_PROPERTY:
(void) printf(gettext("Properties need to be "
"set with set command: %s\n"), optarg);
return (SA_SYNTAX_ERR);
default:
break;
}
break;
case 'r':
if (rsrcname != NULL) {
(void) printf(gettext(
"Unsetting multiple resource "
"names not supported\n"));
return (SA_SYNTAX_ERR);
}
rsrcname = optarg;
break;
case 's':
if (sharepath != NULL) {
(void) printf(gettext(
"Adding multiple shares not supported\n"));
return (SA_SYNTAX_ERR);
}
sharepath = optarg;
break;
case 'S':
if (optset != NULL) {
(void) printf(gettext(
"Specifying multiple property "
"spaces not supported: %s\n"), optset);
return (SA_SYNTAX_ERR);
}
optset = optarg;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_UNSET));
return (ret);
}
}
if (optlist != NULL)
ret = chk_opt(optlist, optset != NULL, protocol);
if (optind >= argc || (optlist == NULL && optset == NULL) ||
protocol == NULL) {
char *sep = "\t";
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_UNSET));
if (optind >= argc) {
(void) printf(gettext("%sgroup must be specified"),
sep);
sep = ", ";
}
if (optlist == NULL) {
(void) printf(gettext("%sat least one property must "
"be specified"), sep);
sep = ", ";
}
if (protocol == NULL) {
(void) printf(gettext("%sprotocol must be specified"),
sep);
sep = ", ";
}
(void) printf("\n");
ret = SA_SYNTAX_ERR;
} else {
groupname = argv[optind];
auth = check_authorizations(groupname, flags);
if (optset == NULL)
ret = basic_unset(handle, groupname, optlist, protocol,
sharepath, rsrcname, dryrun);
else
ret = space_unset(handle, groupname, optlist, protocol,
sharepath, dryrun, optset);
if (dryrun && ret == SA_OK && !auth && verbose)
(void) printf(gettext("Command would fail: %s\n"),
sa_errorstr(SA_NO_PERMISSION));
}
return (ret);
}
int
sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[])
{
int verbose = 0;
int dryrun = 0;
int all = 0;
int c;
int ret = SA_OK;
char *protocol = NULL;
char *state;
struct list *worklist = NULL;
int auth = 1;
sa_group_t group;
while ((c = getopt(argc, argv, "?havnP:")) != EOF) {
switch (c) {
case 'a':
all = 1;
break;
case 'n':
dryrun++;
break;
case 'P':
if (protocol != NULL) {
(void) printf(gettext(
"Specifying multiple protocols "
"not supported: %s\n"), protocol);
return (SA_SYNTAX_ERR);
}
protocol = optarg;
if (!sa_valid_protocol(protocol)) {
(void) printf(gettext(
"Invalid protocol specified: %s\n"),
protocol);
return (SA_INVALID_PROTOCOL);
}
break;
case 'v':
verbose++;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_ENABLE));
return (ret);
}
}
}
if (optind == argc && !all) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_ENABLE));
(void) printf(gettext("\tmust specify group\n"));
return (SA_NO_SUCH_PATH);
}
if (!all) {
while (optind < argc) {
group = sa_get_group(handle, argv[optind]);
if (group != NULL) {
auth &= check_authorizations(argv[optind],
flags);
state = sa_get_group_attr(group, "state");
if (state != NULL &&
strcmp(state, "enabled") == 0) {
if (verbose)
(void) printf(gettext(
"Group \"%s\" is already "
"enabled\n"),
argv[optind]);
ret = SA_BUSY;
} else {
worklist = add_list(worklist, group,
0, protocol);
if (verbose)
(void) printf(gettext(
"Enabling group \"%s\"\n"),
argv[optind]);
}
if (state != NULL)
sa_free_attr_string(state);
} else {
ret = SA_NO_SUCH_GROUP;
}
optind++;
}
} else {
for (group = sa_get_group(handle, NULL);
group != NULL;
group = sa_get_next_group(group)) {
worklist = add_list(worklist, group, 0, protocol);
}
}
if (!dryrun && ret == SA_OK)
ret = enable_all_groups(handle, worklist, 1, 0, NULL, B_FALSE);
if (ret != SA_OK && ret != SA_BUSY)
(void) printf(gettext("Could not enable group: %s\n"),
sa_errorstr(ret));
if (ret == SA_BUSY)
ret = SA_OK;
if (worklist != NULL)
free_list(worklist);
if (dryrun && ret == SA_OK && !auth && verbose) {
(void) printf(gettext("Command would fail: %s\n"),
sa_errorstr(SA_NO_PERMISSION));
}
return (ret);
}
static int
disable_group(sa_group_t group, char *proto)
{
sa_share_t share;
int ret = SA_OK;
if (!has_protocol(group, proto))
return (ret);
for (share = sa_get_share(group, NULL);
share != NULL && ret == SA_OK;
share = sa_get_next_share(share)) {
ret = sa_disable_share(share, proto);
if (ret == SA_NO_SUCH_PATH) {
ret = SA_OK;
}
}
return (ret);
}
static int
disable_all_groups(sa_handle_t handle, struct list *work, int setstate)
{
int ret = SA_OK;
sa_group_t subgroup, group;
while (work != NULL && ret == SA_OK) {
group = (sa_group_t)work->item;
if (setstate)
ret = sa_set_group_attr(group, "state", "disabled");
if (ret == SA_OK) {
char *name;
name = sa_get_group_attr(group, "name");
if (name != NULL && strcmp(name, "zfs") == 0) {
for (subgroup = sa_get_sub_group(group);
subgroup != NULL;
subgroup = sa_get_next_group(subgroup)) {
ret = disable_group(subgroup,
work->proto);
}
} else {
ret = disable_group(group, work->proto);
}
if (name != NULL)
sa_free_attr_string(name);
}
work = work->next;
}
if (ret == SA_OK)
ret = sa_update_config(handle);
return (ret);
}
int
sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[])
{
int verbose = 0;
int dryrun = 0;
int all = 0;
int c;
int ret = SA_OK;
char *protocol = NULL;
char *state;
struct list *worklist = NULL;
sa_group_t group;
int auth = 1;
while ((c = getopt(argc, argv, "?havn")) != EOF) {
switch (c) {
case 'a':
all = 1;
break;
case 'n':
dryrun++;
break;
case 'P':
if (protocol != NULL) {
(void) printf(gettext(
"Specifying multiple protocols "
"not supported: %s\n"), protocol);
return (SA_SYNTAX_ERR);
}
protocol = optarg;
if (!sa_valid_protocol(protocol)) {
(void) printf(gettext(
"Invalid protocol specified: %s\n"),
protocol);
return (SA_INVALID_PROTOCOL);
}
break;
case 'v':
verbose++;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_DISABLE));
return (ret);
}
}
if (optind == argc && !all) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_DISABLE));
(void) printf(gettext("\tmust specify group\n"));
return (SA_NO_SUCH_PATH);
}
if (!all) {
while (optind < argc) {
group = sa_get_group(handle, argv[optind]);
if (group != NULL) {
auth &= check_authorizations(argv[optind],
flags);
state = sa_get_group_attr(group, "state");
if (state == NULL ||
strcmp(state, "disabled") == 0) {
if (verbose)
(void) printf(gettext(
"Group \"%s\" is "
"already disabled\n"),
argv[optind]);
ret = SA_BUSY;
} else {
worklist = add_list(worklist, group, 0,
protocol);
if (verbose)
(void) printf(gettext(
"Disabling group "
"\"%s\"\n"), argv[optind]);
}
if (state != NULL)
sa_free_attr_string(state);
} else {
ret = SA_NO_SUCH_GROUP;
}
optind++;
}
} else {
for (group = sa_get_group(handle, NULL);
group != NULL;
group = sa_get_next_group(group))
worklist = add_list(worklist, group, 0, protocol);
}
if (ret == SA_OK && !dryrun)
ret = disable_all_groups(handle, worklist, 1);
if (ret != SA_OK && ret != SA_BUSY)
(void) printf(gettext("Could not disable group: %s\n"),
sa_errorstr(ret));
if (ret == SA_BUSY)
ret = SA_OK;
if (worklist != NULL)
free_list(worklist);
if (dryrun && ret == SA_OK && !auth && verbose)
(void) printf(gettext("Command would fail: %s\n"),
sa_errorstr(SA_NO_PERMISSION));
return (ret);
}
int
sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[])
{
int verbose = 0;
int all = 0;
int c;
int ret = SMF_EXIT_OK;
char *protocol = NULL;
char *state;
struct list *worklist = NULL;
sa_group_t group;
#ifdef lint
flags = flags;
#endif
while ((c = getopt(argc, argv, "?havP:")) != EOF) {
switch (c) {
case 'a':
all = 1;
break;
case 'P':
if (protocol != NULL) {
(void) printf(gettext(
"Specifying multiple protocols "
"not supported: %s\n"), protocol);
return (SA_SYNTAX_ERR);
}
protocol = optarg;
if (!sa_valid_protocol(protocol)) {
(void) printf(gettext(
"Invalid protocol specified: %s\n"),
protocol);
return (SA_INVALID_PROTOCOL);
}
break;
case 'v':
verbose++;
break;
case 'h':
optopt = c;
case '?':
default:
ret = SA_OK;
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_START));
return (ret);
}
}
if (optind == argc && !all) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_START));
return (SMF_EXIT_ERR_FATAL);
}
if (!all) {
while (optind < argc) {
group = sa_get_group(handle, argv[optind]);
if (group != NULL) {
state = sa_get_group_attr(group, "state");
if (state == NULL ||
strcmp(state, "enabled") == 0) {
worklist = add_list(worklist, group, 0,
protocol);
if (verbose)
(void) printf(gettext(
"Starting group \"%s\"\n"),
argv[optind]);
} else {
if (sa_get_optionset(group,
protocol) != NULL) {
ret = SMF_EXIT_OK;
}
}
if (state != NULL)
sa_free_attr_string(state);
}
optind++;
}
} else {
for (group = sa_get_group(handle, NULL);
group != NULL;
group = sa_get_next_group(group)) {
state = sa_get_group_attr(group, "state");
if (state == NULL || strcmp(state, "enabled") == 0)
worklist = add_list(worklist, group, 0,
protocol);
if (state != NULL)
sa_free_attr_string(state);
}
}
(void) enable_all_groups(handle, worklist, 0, 1, protocol, B_FALSE);
if (worklist != NULL)
free_list(worklist);
return (ret);
}
int
sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[])
{
int verbose = 0;
int all = 0;
int c;
int ret = SMF_EXIT_OK;
char *protocol = NULL;
char *state;
struct list *worklist = NULL;
sa_group_t group;
#ifdef lint
flags = flags;
#endif
while ((c = getopt(argc, argv, "?havP:")) != EOF) {
switch (c) {
case 'a':
all = 1;
break;
case 'P':
if (protocol != NULL) {
(void) printf(gettext(
"Specifying multiple protocols "
"not supported: %s\n"), protocol);
return (SA_SYNTAX_ERR);
}
protocol = optarg;
if (!sa_valid_protocol(protocol)) {
(void) printf(gettext(
"Invalid protocol specified: %s\n"),
protocol);
return (SA_INVALID_PROTOCOL);
}
break;
case 'v':
verbose++;
break;
case 'h':
optopt = c;
case '?':
default:
ret = SA_OK;
switch (optopt) {
default:
ret = SA_SYNTAX_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_STOP));
return (ret);
}
}
if (optind == argc && !all) {
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_STOP));
return (SMF_EXIT_ERR_FATAL);
} else if (!all) {
while (optind < argc) {
group = sa_get_group(handle, argv[optind]);
if (group != NULL) {
state = sa_get_group_attr(group, "state");
if (state == NULL ||
strcmp(state, "enabled") == 0) {
worklist = add_list(worklist, group, 0,
protocol);
if (verbose)
(void) printf(gettext(
"Stopping group \"%s\"\n"),
argv[optind]);
} else {
ret = SMF_EXIT_OK;
}
if (state != NULL)
sa_free_attr_string(state);
}
optind++;
}
} else {
for (group = sa_get_group(handle, NULL);
group != NULL;
group = sa_get_next_group(group)) {
state = sa_get_group_attr(group, "state");
if (state == NULL || strcmp(state, "enabled") == 0)
worklist = add_list(worklist, group, 0,
protocol);
if (state != NULL)
sa_free_attr_string(state);
}
}
(void) disable_all_groups(handle, worklist, 0);
ret = sa_update_config(handle);
if (worklist != NULL)
free_list(worklist);
return (ret);
}
static void
remove_all_options(sa_share_t share, char *proto)
{
sa_optionset_t optionset;
sa_security_t security;
sa_security_t prevsec = NULL;
optionset = sa_get_optionset(share, proto);
if (optionset != NULL)
(void) sa_destroy_optionset(optionset);
for (security = sa_get_security(share, NULL, NULL);
security != NULL;
security = sa_get_next_security(security)) {
char *type;
if (prevsec != NULL) {
(void) sa_destroy_security(prevsec);
prevsec = NULL;
}
type = sa_get_security_attr(security, "type");
if (type != NULL) {
if (strcmp(type, proto) == 0)
prevsec = security;
sa_free_attr_string(type);
}
}
if (prevsec != NULL)
(void) sa_destroy_security(prevsec);
}
static int
format_legacy_path(char *buff, int buffsize, char *proto, char *cmd)
{
int err;
err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd);
if (err > buffsize)
return (-1);
return (0);
}
static int
check_legacy_cmd(char *path)
{
struct stat st;
int ret = 0;
if (stat(path, &st) == 0) {
if (S_ISREG(st.st_mode) &&
st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
ret = 1;
}
return (ret);
}
static int
run_legacy_command(char *path, char *argv[])
{
int ret;
ret = execv(path, argv);
if (ret < 0) {
switch (errno) {
case EACCES:
ret = SA_NO_PERMISSION;
break;
default:
ret = SA_SYSTEM_ERR;
break;
}
}
return (ret);
}
static void
out_share(FILE *out, sa_group_t group, char *proto)
{
sa_share_t share;
char resfmt[128];
char *defprop;
if (proto != NULL && sa_get_optionset(group, proto) == NULL)
return;
if (proto == NULL)
proto = "nfs";
if (proto != NULL && strcmp(proto, "nfs") != 0)
defprop = "\"\"";
else
defprop = "rw";
for (share = sa_get_share(group, NULL);
share != NULL;
share = sa_get_next_share(share)) {
char *path;
char *type;
char *resource;
char *description;
char *groupname;
char *sharedstate;
int shared = 1;
char *soptions;
char shareopts[MAXNAMLEN];
sharedstate = sa_get_share_attr(share, "shared");
path = sa_get_share_attr(share, "path");
type = sa_get_share_attr(share, "type");
resource = get_resource(share);
groupname = sa_get_group_attr(group, "name");
if (groupname != NULL && strcmp(groupname, "default") == 0) {
sa_free_attr_string(groupname);
groupname = NULL;
}
description = sa_get_share_description(share);
(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", proto);
soptions = sa_get_share_attr(share, shareopts);
if (sharedstate == NULL)
shared = 0;
if (soptions == NULL)
soptions = sa_proto_legacy_format(proto, share, 1);
if (shared) {
(void) snprintf(resfmt, sizeof (resfmt), "%s%s%s",
resource != NULL ? resource : "-",
groupname != NULL ? "@" : "",
groupname != NULL ? groupname : "");
(void) fprintf(out, "%-14.14s %s %s \"%s\" \n",
resfmt, (path != NULL) ? path : "",
(soptions != NULL && strlen(soptions) > 0) ?
soptions : defprop,
(description != NULL) ? description : "");
}
if (path != NULL)
sa_free_attr_string(path);
if (type != NULL)
sa_free_attr_string(type);
if (resource != NULL)
sa_free_attr_string(resource);
if (groupname != NULL)
sa_free_attr_string(groupname);
if (description != NULL)
sa_free_share_description(description);
if (sharedstate != NULL)
sa_free_attr_string(sharedstate);
if (soptions != NULL)
sa_format_free(soptions);
}
}
static void
output_legacy_file(FILE *out, char *proto, sa_handle_t handle)
{
sa_group_t group;
for (group = sa_get_group(handle, NULL);
group != NULL;
group = sa_get_next_group(group)) {
char *zfs;
zfs = sa_get_group_attr(group, "zfs");
if (zfs != NULL) {
sa_group_t zgroup;
sa_free_attr_string(zfs);
for (zgroup = sa_get_sub_group(group);
zgroup != NULL;
zgroup = sa_get_next_group(zgroup)) {
out_share(out, zgroup, proto);
}
} else {
out_share(out, group, proto);
}
}
}
int
sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[])
{
char *protocol = "nfs";
char *options = NULL;
char *description = NULL;
char *groupname = NULL;
char *sharepath = NULL;
char *resource = NULL;
char *groupstatus = NULL;
int persist = SA_SHARE_TRANSIENT;
int argsused = 0;
int c;
int ret = SA_OK;
int zfs = 0;
int true_legacy = 0;
int curtype = SA_SHARE_TRANSIENT;
char cmd[MAXPATHLEN];
sa_group_t group = NULL;
sa_resource_t rsrc = NULL;
sa_share_t share;
char dir[MAXPATHLEN];
uint64_t features;
#ifdef lint
flags = flags;
#endif
while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) {
switch (c) {
case 'd':
description = optarg;
argsused++;
break;
case 'F':
protocol = optarg;
if (!sa_valid_protocol(protocol)) {
if (format_legacy_path(cmd, MAXPATHLEN,
protocol, "share") == 0 &&
check_legacy_cmd(cmd)) {
true_legacy++;
} else {
(void) fprintf(stderr, gettext(
"Invalid protocol specified: "
"%s\n"), protocol);
return (SA_INVALID_PROTOCOL);
}
}
break;
case 'o':
options = optarg;
argsused++;
break;
case 'p':
persist = SA_SHARE_PERMANENT;
argsused++;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_LEGACY_ERR;
break;
case 'h':
case '?':
break;
}
(void) fprintf(stderr, gettext("usage: %s\n"),
sa_get_usage(USAGE_SHARE));
return (ret);
}
}
if (!argsused && optind == argc) {
(void) output_legacy_file(stdout, protocol, handle);
return (ret);
}
if (optind == argc) {
(void) fprintf(stderr, gettext("usage: %s\n"),
sa_get_usage(USAGE_SHARE));
return (SA_LEGACY_ERR);
}
if (true_legacy) {
ret = run_legacy_command(cmd, argv);
return (ret);
}
sharepath = argv[optind++];
if (optind < argc) {
resource = argv[optind];
groupname = strchr(resource, '@');
if (groupname != NULL)
*groupname++ = '\0';
}
if (realpath(sharepath, dir) == NULL)
ret = SA_BAD_PATH;
else
sharepath = dir;
if (ret == SA_OK)
share = sa_find_share(handle, sharepath);
else
share = NULL;
features = sa_proto_get_featureset(protocol);
if (groupname != NULL) {
ret = SA_NOT_ALLOWED;
} else if (ret == SA_OK) {
char *legacygroup;
if (strcmp(protocol, "smb") == 0)
legacygroup = "smb";
else
legacygroup = "default";
if (share != NULL) {
group = sa_get_parent_group(share);
} else {
group = sa_get_group(handle, legacygroup);
if (group == NULL && strcmp(legacygroup, "smb") == 0) {
group = sa_create_group(handle, legacygroup,
&ret);
if (group != NULL)
(void) sa_create_optionset(group,
protocol);
}
}
if (group == NULL) {
ret = SA_SYSTEM_ERR;
goto err;
}
groupstatus = group_status(group);
if (share == NULL) {
share = sa_add_share(group, sharepath,
persist, &ret);
if (share == NULL &&
ret == SA_DUPLICATE_NAME) {
if (sa_zfs_is_shared(handle,
sharepath)) {
ret = SA_OK;
group = sa_get_group(handle,
"zfs");
if (group == NULL) {
ret = SA_CONFIG_ERR;
} else {
share = sa_add_share(
group, sharepath,
persist, &ret);
}
}
}
} else {
char *type;
if (sa_zfs_is_shared(handle, sharepath)) {
zfs = 1;
}
remove_all_options(share, protocol);
type = sa_get_share_attr(share, "type");
if (type != NULL &&
strcmp(type, "transient") != 0) {
curtype = SA_SHARE_PERMANENT;
}
if (type != NULL)
sa_free_attr_string(type);
if (curtype != persist) {
(void) sa_set_share_attr(share, "type",
persist == SA_SHARE_PERMANENT ?
"persist" : "transient");
}
}
if (ret == SA_OK && resource != NULL) {
rsrc = sa_find_resource(handle, resource);
if (rsrc != NULL) {
if (share != sa_get_resource_parent(rsrc))
ret = SA_DUPLICATE_NAME;
} else {
rsrc = sa_add_resource(share, resource,
persist, &ret);
}
if (features & SA_FEATURE_RESOURCE)
share = rsrc;
}
if (ret == SA_OK && options != NULL &&
strlen(options) > 0) {
ret = sa_parse_legacy_options(share,
options,
protocol);
}
if (!zfs) {
if (ret == SA_OK && description != NULL)
ret = sa_set_share_description(share,
description);
}
if (ret == SA_OK &&
strcmp(groupstatus, "enabled") == 0) {
if (rsrc != share)
ret = sa_enable_share(share, protocol);
else
ret = sa_enable_resource(rsrc,
protocol);
if (ret == SA_OK &&
persist == SA_SHARE_PERMANENT) {
(void) sa_update_legacy(share,
protocol);
}
if (ret == SA_OK)
ret = sa_update_config(handle);
}
}
err:
if (ret != SA_OK) {
(void) fprintf(stderr, gettext("Could not share: %s: %s\n"),
sharepath, sa_errorstr(ret));
ret = SA_LEGACY_ERR;
}
return (ret);
}
int
sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[])
{
char *protocol = "nfs";
char *options = NULL;
char *sharepath = NULL;
int persist = SA_SHARE_TRANSIENT;
int argsused = 0;
int c;
int ret = SA_OK;
int true_legacy = 0;
uint64_t features = 0;
sa_resource_t resource = NULL;
char cmd[MAXPATHLEN];
#ifdef lint
flags = flags;
options = options;
#endif
while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) {
switch (c) {
case 'F':
protocol = optarg;
if (!sa_valid_protocol(protocol)) {
if (format_legacy_path(cmd, MAXPATHLEN,
protocol, "unshare") == 0 &&
check_legacy_cmd(cmd)) {
true_legacy++;
} else {
(void) printf(gettext(
"Invalid file system name\n"));
return (SA_INVALID_PROTOCOL);
}
}
break;
case 'o':
options = optarg;
argsused++;
break;
case 'p':
persist = SA_SHARE_PERMANENT;
argsused++;
break;
case 'h':
optopt = c;
case '?':
default:
switch (optopt) {
default:
ret = SA_LEGACY_ERR;
break;
case 'h':
case '?':
break;
}
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_UNSHARE));
return (ret);
}
}
if (optind == argc || (optind + 1) < argc || options != NULL) {
ret = SA_SYNTAX_ERR;
} else {
sa_share_t share;
char dir[MAXPATHLEN];
if (true_legacy) {
ret = run_legacy_command(cmd, argv);
return (ret);
}
sharepath = argv[optind++];
share = sa_find_share(handle, sharepath);
if (share == NULL) {
if (realpath(sharepath, dir) == NULL) {
ret = SA_NO_SUCH_PATH;
} else {
share = sa_find_share(handle, dir);
}
}
if (share == NULL) {
features = sa_proto_get_featureset(protocol);
resource = sa_find_resource(handle, sharepath);
if (resource != NULL) {
share = sa_get_resource_parent(resource);
if (features & SA_FEATURE_RESOURCE)
(void) sa_disable_resource(resource,
protocol);
if (persist == SA_SHARE_PERMANENT) {
ret = sa_remove_resource(resource);
if (ret == SA_OK)
ret = sa_update_config(handle);
}
resource = sa_get_share_resource(share, NULL);
if (resource != NULL)
share = NULL;
} else if (ret == SA_OK) {
ret = SA_BAD_PATH;
}
}
if (share != NULL && resource == NULL) {
ret = sa_disable_share(share, protocol);
if (persist == SA_SHARE_PERMANENT) {
ret = sa_remove_share(share);
if (ret == SA_OK)
ret = sa_update_config(handle);
}
} else if (ret == SA_OK && share == NULL && resource == NULL) {
ret = SA_NOT_SHARED;
}
}
switch (ret) {
default:
(void) printf("%s: %s\n", sharepath, sa_errorstr(ret));
ret = SA_LEGACY_ERR;
break;
case SA_SYNTAX_ERR:
(void) printf(gettext("usage: %s\n"),
sa_get_usage(USAGE_UNSHARE));
break;
case SA_OK:
break;
}
return (ret);
}
static sa_command_t commands[] = {
{"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET},
{"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION},
{"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION},
{"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION},
{"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION},
{"list", 0, sa_list, USAGE_LIST},
{"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET},
{"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET},
{"set", 0, sa_set, USAGE_SET, SVC_SET},
{"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET},
{"show", 0, sa_show, USAGE_SHOW},
{"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION},
{"start", CMD_NODISPLAY, sa_start_group, USAGE_START,
SVC_SET|SVC_ACTION},
{"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION},
{"unset", 0, sa_unset, USAGE_UNSET, SVC_SET},
{"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION},
{NULL, 0, NULL, 0}
};
static char *
sa_get_usage(sa_usage_t index)
{
char *ret = NULL;
switch (index) {
case USAGE_ADD_SHARE:
ret = gettext("add-share [-nth] [-r resource-name] "
"[-d \"description text\"] -s sharepath group");
break;
case USAGE_CREATE:
ret = gettext(
"create [-nvh] [-P proto [-p property=value]] group");
break;
case USAGE_DELETE:
ret = gettext("delete [-nvh] [-P proto] [-f] group");
break;
case USAGE_DISABLE:
ret = gettext("disable [-nvh] {-a | group ...}");
break;
case USAGE_ENABLE:
ret = gettext("enable [-nvh] {-a | group ...}");
break;
case USAGE_LIST:
ret = gettext("list [-vh] [-P proto]");
break;
case USAGE_MOVE_SHARE:
ret = gettext(
"move-share [-nvh] -s sharepath destination-group");
break;
case USAGE_REMOVE_SHARE:
ret = gettext(
"remove-share [-fnvh] {-s sharepath | -r resource} "
"group");
break;
case USAGE_SET:
ret = gettext("set [-nvh] -P proto [-S optspace] "
"[-p property=value]* [-s sharepath] [-r resource]] "
"group");
break;
case USAGE_SET_SECURITY:
ret = gettext("set-security [-nvh] -P proto -S security-type "
"[-p property=value]* group");
break;
case USAGE_SET_SHARE:
ret = gettext("set-share [-nh] [-r resource] "
"[-d \"description text\"] -s sharepath group");
break;
case USAGE_SHOW:
ret = gettext("show [-pvxh] [-P proto] [group ...]");
break;
case USAGE_SHARE:
ret = gettext("share [-F fstype] [-p] [-o optionlist]"
"[-d description] [pathname [resourcename]]");
break;
case USAGE_START:
ret = gettext("start [-vh] [-P proto] {-a | group ...}");
break;
case USAGE_STOP:
ret = gettext("stop [-vh] [-P proto] {-a | group ...}");
break;
case USAGE_UNSET:
ret = gettext("unset [-nvh] -P proto [-S optspace] "
"[-p property]* group");
break;
case USAGE_UNSET_SECURITY:
ret = gettext("unset-security [-nvh] -P proto "
"-S security-type [-p property]* group");
break;
case USAGE_UNSHARE:
ret = gettext(
"unshare [-F fstype] [-p] [-o optionlist] sharepath");
break;
}
return (ret);
}
sa_command_t *
sa_lookup(char *cmd, char *proto)
{
int i;
size_t len;
#ifdef lint
proto = proto;
#endif
len = strlen(cmd);
for (i = 0; commands[i].cmdname != NULL; i++) {
if (strncmp(cmd, commands[i].cmdname, len) == 0)
return (&commands[i]);
}
return (NULL);
}
void
sub_command_help(char *proto)
{
int i;
#ifdef lint
proto = proto;
#endif
(void) printf(gettext("\tsub-commands:\n"));
for (i = 0; commands[i].cmdname != NULL; i++) {
if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY)))
(void) printf("\t%s\n",
sa_get_usage((sa_usage_t)commands[i].cmdidx));
}
}