#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "libshare.h"
#include "libshare_impl.h"
#include <fcntl.h>
#include <thread.h>
#include <grp.h>
#include <limits.h>
#include <sys/param.h>
#include <signal.h>
#include <libintl.h>
#include <dirent.h>
#include <sharefs/share.h>
#include "sharetab.h"
#define DFSTAB_NOTICE_LINES 5
static char *notice[DFSTAB_NOTICE_LINES] = {
"# Do not modify this file directly.\n",
"# Use the sharemgr(8) command for all share management\n",
"# This file is reconstructed and only maintained for backward\n",
"# compatibility. Configuration lines could be lost.\n",
"#\n"
};
#define STRNCAT(x, y, z) (xmlChar *)strncat((char *)x, (char *)y, z)
#define MAXARGSFORSHARE 256
static mutex_t sharetab_lock = DEFAULTMUTEX;
extern mutex_t sa_dfstab_lock;
typedef
struct sharelist {
struct sharelist *next;
int persist;
char *path;
char *resource;
char *fstype;
char *options;
char *description;
char *group;
char *origline;
int lineno;
} xfs_sharelist_t;
static void parse_dfstab(sa_handle_t, char *, xmlNodePtr);
extern char *_sa_get_token(char *);
static void dfs_free_list(xfs_sharelist_t *);
void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t);
extern sa_group_t _sa_create_group(sa_handle_impl_t, char *);
static void outdfstab(FILE *, xfs_sharelist_t *);
extern int _sa_remove_optionset(sa_optionset_t);
extern int set_node_share(void *, char *, char *);
extern void set_node_attr(void *, char *, char *);
void
sablocksigs(sigset_t *sigs)
{
sigset_t new;
if (sigs != NULL) {
(void) sigprocmask(SIG_BLOCK, NULL, &new);
(void) sigaddset(&new, SIGHUP);
(void) sigaddset(&new, SIGINT);
(void) sigaddset(&new, SIGQUIT);
(void) sigaddset(&new, SIGTSTP);
(void) sigprocmask(SIG_SETMASK, &new, sigs);
}
}
void
saunblocksigs(sigset_t *sigs)
{
if (sigs != NULL)
(void) sigprocmask(SIG_SETMASK, sigs, NULL);
}
static xfs_sharelist_t *
alloc_sharelist()
{
xfs_sharelist_t *item;
item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t));
if (item != NULL)
(void) memset(item, '\0', sizeof (xfs_sharelist_t));
return (item);
}
static xfs_sharelist_t *
fix_notice(xfs_sharelist_t *list)
{
xfs_sharelist_t *item, *prev;
int i;
if (list == NULL) {
list = alloc_sharelist();
if (list == NULL)
return (NULL);
list->description = strdup("#\n");
}
if (list->path == NULL && list->description != NULL &&
strcmp(list->description, notice[0]) != 0) {
for (prev = NULL, i = 0; i < DFSTAB_NOTICE_LINES; i++) {
item = alloc_sharelist();
if (item != NULL) {
item->description = strdup(notice[i]);
if (prev == NULL) {
item->next = list;
prev = item;
list = item;
} else {
item->next = prev->next;
prev->next = item;
prev = item;
}
}
}
}
return (list);
}
static xfs_sharelist_t *
getdfstab(FILE *dfs)
{
char buff[_POSIX_ARG_MAX];
char *bp;
char *token;
char *args[MAXARGSFORSHARE];
int argc;
int c;
static int line = 0;
xfs_sharelist_t *item = NULL, *first = NULL, *last;
if (dfs != NULL) {
first = NULL;
line = 0;
while (fgets(buff, sizeof (buff), dfs) != NULL) {
line++;
bp = buff;
if (buff[0] == '#') {
item = alloc_sharelist();
if (item != NULL) {
item->lineno = line;
item->description = strdup(buff);
if (first == NULL) {
first = item;
last = item;
} else {
last->next = item;
last = item;
}
} else {
break;
}
continue;
} else if (buff[0] == '\n') {
continue;
}
optind = 1;
item = alloc_sharelist();
if (item == NULL) {
break;
} else if (first == NULL) {
first = item;
last = item;
} else {
last->next = item;
last = item;
}
item->lineno = line;
item->origline = strdup(buff);
(void) _sa_get_token(NULL);
argc = 0;
while ((token = _sa_get_token(bp)) != NULL) {
if (argc < MAXARGSFORSHARE)
args[argc++] = token;
}
while ((c = getopt(argc, args, "F:o:d:pg:")) != -1) {
switch (c) {
case 'p':
item->persist = 1;
break;
case 'F':
item->fstype = strdup(optarg);
break;
case 'o':
item->options = strdup(optarg);
break;
case 'd':
item->description = strdup(optarg);
break;
case 'g':
item->group = strdup(optarg);
break;
default:
break;
}
}
if (optind < argc) {
item->path = strdup(args[optind]);
optind++;
if (optind < argc) {
char *resource;
char *optgroup;
resource = args[optind];
optgroup = strchr(resource, '@');
if (optgroup != NULL)
*optgroup++ = '\0';
if (optgroup != NULL)
item->group = strdup(optgroup);
if (resource != NULL &&
strlen(resource) > 0)
item->resource =
strdup(resource);
}
}
if (item != NULL && item->fstype == NULL)
item->fstype = strdup("nfs");
}
}
first = fix_notice(first);
return (first);
}
static xfs_sharelist_t *
finddfsentry(xfs_sharelist_t *list, char *path)
{
xfs_sharelist_t *item;
for (item = list; item != NULL; item = item->next) {
if (item->path != NULL && strcmp(item->path, path) == 0)
return (item);
}
return (NULL);
}
static xfs_sharelist_t *
remdfsentry(xfs_sharelist_t *list, char *path, char *proto)
{
xfs_sharelist_t *item, *prev = NULL;
for (item = prev = list; item != NULL; item = item->next) {
if (item->path == NULL) {
prev = item;
continue;
}
if (proto == NULL || (strcmp(item->path, path) == 0 &&
(item->fstype != NULL && strcmp(item->fstype, proto) == 0)))
break;
if (item->fstype == NULL &&
(proto == NULL || strcmp(proto, "nfs") == 0))
break;
prev = item;
}
if (item != NULL) {
if (item == prev)
list = item->next;
else
prev->next = item->next;
item->next = NULL;
dfs_free_list(item);
}
return (list);
}
static xfs_sharelist_t *
remdfsline(xfs_sharelist_t *list, char *line)
{
xfs_sharelist_t *item, *prev = NULL;
for (item = prev = list; item != NULL; item = item->next) {
if (item->path == NULL) {
prev = item;
continue;
}
if (strcmp(item->origline, line) == 0)
break;
prev = item;
}
if (item != NULL) {
if (item == prev)
list = item->next;
else
prev->next = item->next;
item->next = NULL;
dfs_free_list(item);
}
return (list);
}
static xfs_sharelist_t *
adddfsentry(xfs_sharelist_t *list, sa_share_t share, char *proto)
{
xfs_sharelist_t *item, *tmp;
sa_group_t parent;
char *groupname;
item = alloc_sharelist();
if (item != NULL) {
parent = sa_get_parent_group(share);
groupname = sa_get_group_attr(parent, "name");
if (groupname != NULL && strcmp(groupname, "default") == 0) {
sa_free_attr_string(groupname);
groupname = NULL;
}
item->path = sa_get_share_attr(share, "path");
item->resource = sa_get_share_attr(share, "resource");
item->group = groupname;
item->fstype = strdup(proto);
item->options = sa_proto_legacy_format(proto, share, 1);
if (item->options != NULL && strlen(item->options) == 0) {
free(item->options);
item->options = NULL;
}
item->description = sa_get_share_description(share);
if (item->description != NULL &&
strlen(item->description) == 0) {
sa_free_share_description(item->description);
item->description = NULL;
}
if (list == NULL) {
list = item;
} else {
for (tmp = list; tmp->next != NULL; tmp = tmp->next)
;
tmp->next = item;
}
}
return (list);
}
static void
outdfstab(FILE *dfstab, xfs_sharelist_t *list)
{
xfs_sharelist_t *item;
(void) ftruncate(fileno(dfstab), 0);
for (item = list; item != NULL; item = item->next) {
if (item->path != NULL) {
if (*item->path == '/') {
(void) fprintf(dfstab,
"share %s%s%s%s%s%s%s %s%s%s%s%s\n",
(item->fstype != NULL) ? "-F " : "",
(item->fstype != NULL) ? item->fstype : "",
(item->options != NULL) ? " -o " : "",
(item->options != NULL) ?
item->options : "",
(item->description != NULL) ?
" -d \"" : "",
(item->description != NULL) ?
item->description : "",
(item->description != NULL) ? "\"" : "",
item->path,
((item->resource != NULL) ||
(item->group != NULL)) ? " " : "",
(item->resource != NULL) ?
item->resource : "",
item->group != NULL ? "@" : "",
item->group != NULL ? item->group : "");
} else {
(void) fprintf(dfstab, "%s", item->origline);
}
} else {
if (item->description != NULL)
(void) fprintf(dfstab, "%s", item->description);
else
(void) fprintf(dfstab, "%s", item->origline);
}
}
}
static FILE *
open_dfstab(char *file)
{
struct group *grp;
struct group group;
char *buff;
int grsize;
FILE *dfstab;
dfstab = fopen(file, "r+");
if (dfstab == NULL) {
dfstab = fopen(file, "w+");
}
if (dfstab != NULL) {
grsize = sysconf(_SC_GETGR_R_SIZE_MAX);
buff = malloc(grsize);
if (buff != NULL)
grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff,
grsize);
else
grp = getgrnam(SA_DEFAULT_FILE_GRP);
(void) fchmod(fileno(dfstab), 0644);
(void) fchown(fileno(dfstab), 0,
grp != NULL ? grp->gr_gid : 3);
if (buff != NULL)
free(buff);
rewind(dfstab);
}
return (dfstab);
}
static void
sa_comment_line(char *line, char *err)
{
FILE *dfstab;
xfs_sharelist_t *list;
sigset_t old;
dfstab = open_dfstab(SA_LEGACY_DFSTAB);
if (dfstab != NULL) {
(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
sablocksigs(&old);
(void) lockf(fileno(dfstab), F_LOCK, 0);
(void) mutex_lock(&sa_dfstab_lock);
list = getdfstab(dfstab);
rewind(dfstab);
list = remdfsline(list, line);
outdfstab(dfstab, list);
(void) fprintf(dfstab, "# Error: %s: %s", err, line);
(void) fsync(fileno(dfstab));
(void) mutex_unlock(&sa_dfstab_lock);
(void) lockf(fileno(dfstab), F_ULOCK, 0);
(void) fclose(dfstab);
saunblocksigs(&old);
if (list != NULL)
dfs_free_list(list);
}
}
int
sa_delete_legacy(sa_share_t share, char *protocol)
{
FILE *dfstab;
int err;
int ret = SA_OK;
xfs_sharelist_t *list;
char *path;
sa_optionset_t optionset;
sa_group_t parent;
sigset_t old;
path = sa_get_share_attr(share, "path");
if (path == NULL)
return (ret);
dfstab = open_dfstab(SA_LEGACY_DFSTAB);
if (dfstab != NULL) {
(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
sablocksigs(&old);
parent = sa_get_parent_group(share);
if (parent != NULL) {
(void) lockf(fileno(dfstab), F_LOCK, 0);
(void) mutex_lock(&sa_dfstab_lock);
list = getdfstab(dfstab);
rewind(dfstab);
if (protocol != NULL) {
if (list != NULL)
list = remdfsentry(list, path,
protocol);
} else {
for (optionset = sa_get_optionset(parent, NULL);
optionset != NULL;
optionset =
sa_get_next_optionset(optionset)) {
char *proto = sa_get_optionset_attr(
optionset, "type");
if (list != NULL && proto != NULL)
list = remdfsentry(list, path,
proto);
if (proto == NULL)
ret = SA_NO_MEMORY;
if (ret == SA_OK) {
err = sa_proto_delete_legacy(
proto, share);
if (err != SA_NOT_IMPLEMENTED)
ret = err;
}
if (proto != NULL)
sa_free_attr_string(proto);
}
}
outdfstab(dfstab, list);
if (list != NULL)
dfs_free_list(list);
(void) fflush(dfstab);
(void) mutex_unlock(&sa_dfstab_lock);
(void) lockf(fileno(dfstab), F_ULOCK, 0);
}
(void) fsync(fileno(dfstab));
saunblocksigs(&old);
(void) fclose(dfstab);
} else {
if (errno == EACCES || errno == EPERM)
ret = SA_NO_PERMISSION;
else
ret = SA_CONFIG_ERR;
}
if (path != NULL)
sa_free_attr_string(path);
return (ret);
}
int
sa_update_legacy(sa_share_t share, char *proto)
{
FILE *dfstab;
int ret = SA_OK;
xfs_sharelist_t *list;
char *path;
sigset_t old;
char *persist;
uint64_t features;
ret = sa_proto_update_legacy(proto, share);
if (ret != SA_NOT_IMPLEMENTED)
return (ret);
features = sa_proto_get_featureset(proto);
if (!(features & SA_FEATURE_DFSTAB))
return (ret);
persist = sa_get_share_attr(share, "type");
if (persist == NULL || strcmp(persist, "transient") != 0) {
path = sa_get_share_attr(share, "path");
if (path == NULL) {
ret = SA_NO_MEMORY;
goto out;
}
dfstab = open_dfstab(SA_LEGACY_DFSTAB);
if (dfstab != NULL) {
(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
sablocksigs(&old);
(void) lockf(fileno(dfstab), F_LOCK, 0);
(void) mutex_lock(&sa_dfstab_lock);
list = getdfstab(dfstab);
rewind(dfstab);
if (list != NULL)
list = remdfsentry(list, path, proto);
list = adddfsentry(list, share, proto);
outdfstab(dfstab, list);
(void) fflush(dfstab);
(void) mutex_unlock(&sa_dfstab_lock);
(void) lockf(fileno(dfstab), F_ULOCK, 0);
(void) fsync(fileno(dfstab));
saunblocksigs(&old);
(void) fclose(dfstab);
if (list != NULL)
dfs_free_list(list);
} else {
if (errno == EACCES || errno == EPERM)
ret = SA_NO_PERMISSION;
else
ret = SA_CONFIG_ERR;
}
sa_free_attr_string(path);
}
out:
if (persist != NULL)
sa_free_attr_string(persist);
return (ret);
}
int
sa_is_security(char *optname, char *proto)
{
int ret = 0;
if (proto != NULL)
ret = sa_proto_security_prop(proto, optname);
return (ret);
}
static void
add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab)
{
xmlNodePtr node;
node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line);
if (node != NULL)
(void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err);
if (todfstab)
sa_comment_line(line, err);
}
int
sa_is_share(void *object)
{
if (object != NULL) {
if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0)
return (1);
}
return (0);
}
int
sa_is_resource(void *object)
{
if (object != NULL) {
if (strcmp((char *)((xmlNodePtr)object)->name, "resource") == 0)
return (1);
}
return (0);
}
static void
_sa_remove_property(sa_property_t property)
{
xmlUnlinkNode((xmlNodePtr)property);
xmlFreeNode((xmlNodePtr)property);
}
static sa_group_t
_sa_create_dummy_share()
{
xmlNodePtr parent_node = NULL;
xmlNodePtr child_node = NULL;
parent_node = xmlNewNode(NULL, (xmlChar *)"group");
if (parent_node != NULL) {
child_node = xmlNewChild(parent_node, NULL, (xmlChar *)"share",
NULL);
if (child_node != NULL) {
set_node_attr(parent_node, "type", "transient");
set_node_attr(parent_node, "zfs", "true");
set_node_attr(child_node, "type", "transient");
set_node_attr(child_node, "zfs", "true");
} else {
xmlFreeNode(parent_node);
}
}
return (child_node);
}
static int
_sa_free_dummy_share(sa_share_t share)
{
xmlNodePtr node = (xmlNodePtr)share;
xmlNodePtr parent;
int ret = SA_OK;
char *name;
if (node != NULL) {
parent = node->parent;
name = (char *)xmlGetProp(node, (xmlChar *)"path");
if (name != NULL) {
ret = SA_NOT_ALLOWED;
sa_free_attr_string(name);
} else {
if (parent != NULL) {
node = parent;
}
xmlUnlinkNode(node);
xmlFreeNode(node);
}
}
return (ret);
}
int
sa_parse_legacy_options(sa_group_t group, char *options, char *proto)
{
int ret = SA_INVALID_PROTOCOL;
sa_group_t parent;
int using_dummy = B_FALSE;
char *pvalue;
sa_optionset_t optionset;
sa_property_t popt, prop;
sa_optionset_t localoptions;
if (group == NULL) {
group = (sa_group_t)_sa_create_dummy_share();
using_dummy = B_TRUE;
}
parent = sa_get_parent_group(group);
if (proto != NULL)
ret = sa_proto_legacy_opts(proto, group, options);
if (using_dummy) {
(void) _sa_free_dummy_share(parent);
return (ret);
}
if (ret != SA_OK)
return (ret);
if (parent == NULL)
return (ret);
optionset = sa_get_derived_optionset(parent, proto, 1);
localoptions = sa_get_optionset(group, proto);
if (optionset != NULL) {
for (popt = sa_get_property(optionset, NULL);
popt != NULL;
popt = sa_get_next_property(popt)) {
char *tag;
char *value;
tag = sa_get_property_attr(popt, "type");
if (tag == NULL)
continue;
prop = sa_get_property(localoptions, tag);
if (prop != NULL) {
value = sa_get_property_attr(popt,
"value");
pvalue = sa_get_property_attr(prop,
"value");
if (value != NULL && pvalue != NULL &&
strcmp(value, pvalue) == 0) {
(void) _sa_remove_property(
prop);
}
if (value != NULL)
sa_free_attr_string(value);
if (pvalue != NULL)
sa_free_attr_string(pvalue);
}
sa_free_attr_string(tag);
}
prop = sa_get_property(localoptions, NULL);
if (prop == NULL && sa_is_share(group)) {
(void) _sa_remove_optionset(localoptions);
}
sa_free_derived_optionset(optionset);
}
localoptions = sa_get_all_security_types(group, proto, 0);
if (localoptions != NULL) {
for (prop = sa_get_property(localoptions, NULL);
prop != NULL;
prop = sa_get_next_property(prop)) {
char *tag;
sa_security_t security;
tag = sa_get_property_attr(prop, "type");
if (tag != NULL) {
sa_property_t nextpopt = NULL;
security = sa_get_security(group, tag, proto);
sa_free_attr_string(tag);
pvalue = sa_get_property_attr(prop, "value");
for (popt = sa_get_property(security, NULL);
popt != NULL;
popt = nextpopt) {
char *value;
nextpopt = sa_get_next_property(popt);
value = sa_get_property_attr(popt,
"value");
if (value != NULL && pvalue != NULL &&
strcmp(value, pvalue) == 0) {
(void) _sa_remove_property
(popt);
}
if (value != NULL)
sa_free_attr_string(value);
}
if (pvalue != NULL)
sa_free_attr_string(pvalue);
}
}
(void) sa_destroy_optionset(localoptions);
}
return (ret);
}
static void
dfs_free_list(xfs_sharelist_t *list)
{
xfs_sharelist_t *entry;
for (entry = list; entry != NULL; entry = list) {
if (entry->path != NULL)
free(entry->path);
if (entry->resource != NULL)
free(entry->resource);
if (entry->fstype != NULL)
free(entry->fstype);
if (entry->options != NULL)
free(entry->options);
if (entry->description != NULL)
free(entry->description);
if (entry->origline != NULL)
free(entry->origline);
if (entry->group != NULL)
free(entry->group);
list = list->next;
free(entry);
}
}
static void
parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root)
{
sa_share_t share;
sa_group_t group;
sa_group_t sgroup = NULL;
sa_group_t defgroup;
xfs_sharelist_t *head, *list;
int err;
int defined_group;
FILE *dfs;
char *oldprops;
dfs = fopen(dfstab, "r");
if (dfs == NULL)
return;
defgroup = sa_get_group(handle, "default");
for (head = list = getdfstab(dfs);
list != NULL;
list = list->next) {
share = NULL;
group = NULL;
defined_group = 0;
err = 0;
if (list->origline == NULL) {
if (list->description != NULL &&
strncmp(list->description, "# Error: ", 9) == 0) {
char *line;
char *error;
char *cmd;
line = strdup(list->description);
if (line != NULL) {
error = line + 9;
cmd = strchr(error, ':');
if (cmd != NULL) {
int len;
*cmd = '\0';
cmd += 2;
len = strlen(cmd);
cmd[len - 1] = '\0';
add_syntax_comment(root, cmd,
error, 0);
}
free(line);
}
}
continue;
}
if (list->path != NULL && strlen(list->path) > 0 &&
*list->path == '/') {
share = sa_find_share(handle, list->path);
if (share != NULL)
sgroup = sa_get_parent_group(share);
else
sgroup = NULL;
} else {
(void) printf(dgettext(TEXT_DOMAIN,
"No share specified in dfstab: "
"line %d: %s\n"),
list->lineno, list->origline);
add_syntax_comment(root, list->origline,
dgettext(TEXT_DOMAIN, "No share specified"), 1);
continue;
}
if (list->group != NULL && strlen(list->group) > 0) {
group = sa_get_group(handle, list->group);
defined_group = 1;
} else {
group = defgroup;
}
if (defined_group && group == NULL) {
(void) printf(dgettext(TEXT_DOMAIN,
"Unknown group used in dfstab: line %d: %s\n"),
list->lineno, list->origline);
add_syntax_comment(root, list->origline,
dgettext(TEXT_DOMAIN, "Unknown group specified"),
1);
continue;
}
if (group == NULL) {
err = SA_CONFIG_ERR;
continue;
}
if (share == NULL) {
if (defined_group || group != defgroup)
continue;
share = sa_add_share(defgroup, list->path,
SA_SHARE_PERMANENT | SA_SHARE_PARSER, &err);
if (share != NULL) {
if (list->description != NULL &&
strlen(list->description) > 0)
(void) sa_set_share_description(share,
list->description);
if (list->options != NULL &&
strlen(list->options) > 0) {
(void) sa_parse_legacy_options(share,
list->options, list->fstype);
}
if (list->resource != NULL)
(void) sa_set_share_attr(share,
"resource", list->resource);
} else {
(void) printf(dgettext(TEXT_DOMAIN,
"Error in dfstab: line %d: %s\n"),
list->lineno, list->origline);
if (err != SA_BAD_PATH)
add_syntax_comment(root, list->origline,
dgettext(TEXT_DOMAIN, "Syntax"), 1);
else
add_syntax_comment(root, list->origline,
dgettext(TEXT_DOMAIN,
"Path"), 1);
continue;
}
} else {
if (group != sgroup) {
(void) printf(dgettext(TEXT_DOMAIN,
"Attempt to change configuration in "
"dfstab: line %d: %s\n"),
list->lineno, list->origline);
add_syntax_comment(root, list->origline,
dgettext(TEXT_DOMAIN,
"Attempt to change configuration"), 1);
continue;
}
oldprops = sa_proto_legacy_format(list->fstype, share,
B_TRUE);
if (oldprops == NULL)
continue;
if (list->options != NULL &&
strcmp(oldprops, list->options) != 0) {
sa_optionset_t opts;
sa_security_t secs;
opts = sa_get_optionset((sa_group_t)
share, list->fstype);
(void) sa_destroy_optionset(opts);
for (secs = sa_get_security(
(sa_group_t)share, NULL, list->fstype);
secs != NULL;
secs = sa_get_security((sa_group_t)share,
NULL, list->fstype)) {
(void) sa_destroy_security(
secs);
}
(void) sa_parse_legacy_options(share,
list->options, list->fstype);
}
sa_format_free(oldprops);
}
}
dfs_free_list(head);
}
static void
legacy_removes(sa_group_t group, char *file)
{
sa_share_t share;
char *path;
xfs_sharelist_t *list, *item;
FILE *dfstab;
dfstab = fopen(file, "r");
if (dfstab != NULL) {
list = getdfstab(dfstab);
(void) fclose(dfstab);
retry:
for (share = sa_get_share(group, NULL);
share != NULL;
share = sa_get_next_share(share)) {
path = sa_get_share_attr(share, "path");
if (path != NULL) {
item = finddfsentry(list, path);
sa_free_attr_string(path);
if (item == NULL) {
(void) sa_remove_share(share);
goto retry;
}
}
}
if (list != NULL)
dfs_free_list(list);
}
}
void
getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root)
{
sa_group_t defgroup;
if (root != NULL) {
if (*root == NULL)
*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
if (*root != NULL) {
if (strcmp(path, SA_LEGACY_DFSTAB) == 0) {
defgroup = sa_get_group(handle, "default");
if (defgroup != NULL)
legacy_removes(defgroup, path);
parse_dfstab(handle, path, *root);
}
}
}
}
static xfs_sharelist_t *
get_share_list(int *errp)
{
xfs_sharelist_t *newp;
xfs_sharelist_t *headp;
xfs_sharelist_t *tailp;
FILE *fp;
headp = NULL;
tailp = NULL;
if ((fp = fopen(SHARETAB, "r")) != NULL) {
struct share *sharetab_entry;
(void) lockf(fileno(fp), F_LOCK, 0);
(void) mutex_lock(&sharetab_lock);
while (getshare(fp, &sharetab_entry) > 0) {
newp = alloc_sharelist();
if (newp == NULL) {
(void) mutex_unlock(&sharetab_lock);
(void) lockf(fileno(fp), F_ULOCK, 0);
goto err;
}
if (headp == NULL) {
headp = newp;
tailp = newp;
} else {
tailp->next = newp;
tailp = newp;
}
newp->path = strdup(sharetab_entry->sh_path);
newp->resource = strdup(sharetab_entry->sh_res);
newp->fstype = strdup(sharetab_entry->sh_fstype);
newp->options = strdup(sharetab_entry->sh_opts);
newp->description = strdup(sharetab_entry->sh_descr);
if (newp->path == NULL || newp->resource == NULL ||
newp->fstype == NULL || newp->options == NULL ||
newp->description == NULL) {
(void) mutex_unlock(&sharetab_lock);
(void) lockf(fileno(fp), F_ULOCK, 0);
goto err;
}
}
(void) mutex_unlock(&sharetab_lock);
(void) lockf(fileno(fp), F_ULOCK, 0);
(void) fclose(fp);
} else {
*errp = errno;
}
return (headp);
err:
dfs_free_list(headp);
(void) fclose(fp);
return (NULL);
}
static int
parse_sharetab_impl(sa_handle_t handle, xfs_sharelist_t *tmplist,
sa_group_t lgroup)
{
int err = 0;
sa_share_t share;
sa_group_t group;
char *groupname;
int legacy = 0;
char shareopts[MAXNAMLEN];
group = NULL;
share = sa_find_share(handle, tmplist->path);
if (share != NULL) {
set_node_attr(share, "shared", "true");
(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s",
tmplist->fstype);
set_node_attr(share, shareopts, tmplist->options);
return (1);
}
if (tmplist->resource != NULL &&
(groupname = strchr(tmplist->resource, '@')) != NULL) {
*groupname++ = '\0';
group = sa_get_group(handle, groupname);
if (group != NULL) {
share = _sa_add_share(group, tmplist->path,
SA_SHARE_TRANSIENT, &err,
(uint64_t)SA_FEATURE_NONE);
} else {
share = _sa_add_share(lgroup,
tmplist->path, SA_SHARE_TRANSIENT,
&err, (uint64_t)SA_FEATURE_NONE);
}
} else {
if (sa_zfs_is_shared(handle, tmplist->path)) {
group = sa_get_group(handle, "zfs");
if (group == NULL) {
group = sa_create_group(handle,
"zfs", &err);
if (group == NULL &&
err == SA_NO_PERMISSION) {
group = _sa_create_group(
(sa_handle_impl_t)
handle,
"zfs");
}
if (group != NULL) {
(void) sa_create_optionset(
group, tmplist->fstype);
(void) sa_set_group_attr(group,
"zfs", "true");
}
}
if (group != NULL) {
share = _sa_add_share(group,
tmplist->path, SA_SHARE_TRANSIENT,
&err, (uint64_t)SA_FEATURE_NONE);
}
} else {
share = _sa_add_share(lgroup, tmplist->path,
SA_SHARE_TRANSIENT, &err,
(uint64_t)SA_FEATURE_NONE);
}
}
if (share == NULL)
(void) printf(dgettext(TEXT_DOMAIN,
"Problem with transient: %s\n"), sa_errorstr(err));
if (share != NULL)
set_node_attr(share, "shared", "true");
if (err == SA_OK) {
if (tmplist->options != NULL &&
strlen(tmplist->options) > 0) {
(void) sa_parse_legacy_options(share,
tmplist->options, tmplist->fstype);
}
if (tmplist->resource != NULL &&
strcmp(tmplist->resource, "-") != 0)
set_node_attr(share, "resource",
tmplist->resource);
if (tmplist->description != NULL) {
xmlNodePtr node;
node = xmlNewChild((xmlNodePtr)share, NULL,
(xmlChar *)"description", NULL);
(void) xmlNodeSetContent(node,
(xmlChar *)tmplist->description);
}
legacy = 1;
}
return (legacy);
}
int
parse_sharetab_for_paths(sa_handle_t handle, char **paths, size_t paths_len)
{
int err = 0;
int legacy = 0;
sa_group_t lgroup;
xfs_sharelist_t *list = get_share_list(&err);
if (list == NULL)
return (legacy);
lgroup = sa_get_group(handle, "default");
for (int i = 0; i < paths_len; ++i) {
xfs_sharelist_t *tmplist;
for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) {
if (strcmp(tmplist->path, paths[i]) == 0) {
break;
}
}
if (tmplist == NULL) {
continue;
}
legacy = parse_sharetab_impl(handle, tmplist, lgroup);
}
dfs_free_list(list);
return (legacy);
}
int
parse_sharetab(sa_handle_t handle)
{
int err = 0;
int legacy = 0;
sa_group_t lgroup;
xfs_sharelist_t *list = get_share_list(&err);
if (list == NULL)
return (legacy);
lgroup = sa_get_group(handle, "default");
for (xfs_sharelist_t *tmplist = list; tmplist != NULL;
tmplist = tmplist->next) {
legacy |= parse_sharetab_impl(handle, tmplist, lgroup);
}
dfs_free_list(list);
return (legacy);
}
int
get_one_transient(sa_handle_impl_t ihandle, xmlNodePtr *root, char **paths,
size_t paths_len)
{
int legacy = 0;
char **protocols = NULL;
if (root != NULL) {
if (*root == NULL)
*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
if (*root != NULL) {
legacy = parse_sharetab_for_paths(ihandle, paths,
paths_len);
int numproto = sa_get_protocols(&protocols);
for (int i = 0; i < numproto; i++)
legacy |= sa_proto_get_transients(
(sa_handle_t)ihandle, protocols[i]);
if (protocols != NULL)
free(protocols);
}
}
return (legacy);
}
int
gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root)
{
int legacy = 0;
int numproto;
char **protocols = NULL;
int i;
if (root != NULL) {
if (*root == NULL)
*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
if (*root != NULL) {
legacy = parse_sharetab(ihandle);
numproto = sa_get_protocols(&protocols);
for (i = 0; i < numproto; i++)
legacy |= sa_proto_get_transients(
(sa_handle_t)ihandle, protocols[i]);
if (protocols != NULL)
free(protocols);
}
}
return (legacy);
}
int
sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
{
char *name;
sa_property_t otherprop;
int result = 0;
if (optionset != NULL) {
name = sa_get_property_attr(prop, "type");
if (name != NULL) {
otherprop = sa_get_property(optionset, name);
if (otherprop != NULL)
result = 1;
sa_free_attr_string(name);
}
}
return (result);
}
void
update_legacy_config(sa_handle_t handle)
{
#ifdef lint
handle = handle;
#endif
}
int
sa_valid_property(sa_handle_t handle, void *object, char *proto,
sa_property_t property)
{
int ret = SA_OK;
if (proto != NULL && property != NULL) {
ret = sa_proto_valid_prop(handle, proto, property, object);
}
return (ret);
}
char *
sa_fstype(char *path)
{
int err;
struct stat st;
err = stat(path, &st);
if (err < 0)
err = SA_NO_SUCH_PATH;
else
err = SA_OK;
if (err == SA_OK)
return (strdup(st.st_fstype));
return (NULL);
}
void
sa_free_fstype(char *type)
{
free(type);
}
sa_optionset_t
sa_get_derived_optionset(void *object, char *proto, int hier)
{
sa_optionset_t newoptionset;
sa_optionset_t optionset;
sa_group_t group;
if (hier &&
(group = sa_get_parent_group((sa_share_t)object)) != NULL) {
newoptionset = sa_get_derived_optionset((void *)group, proto,
hier);
} else {
newoptionset = (sa_optionset_t)xmlNewNode(NULL,
(xmlChar *)"optionset");
if (newoptionset != NULL) {
sa_set_optionset_attr(newoptionset, "type", proto);
}
}
if (newoptionset == NULL)
return (NULL);
optionset = sa_get_optionset((sa_optionset_t)object, proto);
if (optionset != NULL) {
sa_property_t prop;
for (prop = sa_get_property(optionset, NULL);
prop != NULL;
prop = sa_get_next_property(prop)) {
sa_property_t newprop;
char *name;
char *value;
name = sa_get_property_attr(prop, "type");
value = sa_get_property_attr(prop, "value");
if (name == NULL)
continue;
newprop = sa_get_property(newoptionset, name);
if (newprop != NULL) {
if (value != NULL)
set_node_attr(newprop, "value", value);
} else {
if (value != NULL) {
newprop = sa_create_property(name,
value);
if (newprop != NULL) {
newprop = (sa_property_t)
xmlAddChild(
(xmlNodePtr)newoptionset,
(xmlNodePtr)newprop);
}
}
}
sa_free_attr_string(name);
if (value != NULL)
sa_free_attr_string(value);
}
}
return (newoptionset);
}
void
sa_free_derived_optionset(sa_optionset_t optionset)
{
if (optionset != NULL) {
xmlUnlinkNode((xmlNodePtr) optionset);
xmlFreeNode((xmlNodePtr) optionset);
}
}
sa_optionset_t
sa_get_all_security_types(void *object, char *proto, int hier)
{
sa_optionset_t options;
sa_security_t security;
sa_group_t group;
sa_property_t prop;
options = NULL;
if (hier &&
(group = sa_get_parent_group((sa_share_t)object)) != NULL)
options = sa_get_all_security_types((void *)group, proto, hier);
else
options = (sa_optionset_t)xmlNewNode(NULL,
(xmlChar *)"optionset");
if (options == NULL)
return (options);
for (security = sa_get_security((sa_group_t)object, NULL, NULL);
security != NULL;
security = sa_get_next_security(security)) {
char *type;
char *sectype;
type = sa_get_security_attr(security, "type");
if (type != NULL) {
if (strcmp(type, proto) != 0) {
sa_free_attr_string(type);
continue;
}
sectype = sa_get_security_attr(security, "sectype");
if (sectype != NULL) {
if (sa_get_property(options, sectype) == NULL) {
prop = sa_create_property(sectype,
"true");
if (prop != NULL)
prop = (sa_property_t)
xmlAddChild(
(xmlNodePtr)options,
(xmlNodePtr)prop);
}
sa_free_attr_string(sectype);
}
sa_free_attr_string(type);
}
}
return (options);
}
sa_security_t
sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
{
sa_security_t newsecurity;
sa_security_t security;
sa_group_t group;
sa_property_t prop;
if (hier &&
(group = sa_get_parent_group((sa_share_t)object)) != NULL) {
newsecurity = sa_get_derived_security((void *)group,
sectype, proto, hier);
} else {
newsecurity = (sa_security_t)xmlNewNode(NULL,
(xmlChar *)"security");
if (newsecurity != NULL) {
sa_set_security_attr(newsecurity, "type", proto);
sa_set_security_attr(newsecurity, "sectype", sectype);
}
}
if (newsecurity == NULL)
return (newsecurity);
security = sa_get_security((sa_security_t)object, sectype, proto);
if (security == NULL)
return (newsecurity);
for (prop = sa_get_property(security, NULL);
prop != NULL; prop = sa_get_next_property(prop)) {
sa_property_t newprop;
char *name;
char *value;
name = sa_get_property_attr(prop, "type");
value = sa_get_property_attr(prop, "value");
if (name != NULL) {
newprop = sa_get_property(newsecurity, name);
if (newprop != NULL) {
if (value != NULL)
set_node_attr(newprop, "value", value);
} else {
if (value != NULL) {
newprop = sa_create_property(name,
value);
newprop = (sa_property_t)
xmlAddChild((xmlNodePtr)newsecurity,
(xmlNodePtr)newprop);
}
}
sa_free_attr_string(name);
}
if (value != NULL)
sa_free_attr_string(value);
}
return (newsecurity);
}
void
sa_free_derived_security(sa_security_t security)
{
if (security != NULL) {
xmlUnlinkNode((xmlNodePtr)security);
xmlFreeNode((xmlNodePtr)security);
}
}
void
sa_fillshare(sa_share_t share, char *proto, struct share *sh)
{
char *groupname = NULL;
char *value;
sa_group_t group;
char *buff;
char *zfs;
sa_resource_t resource;
char *rsrcname = NULL;
char *defprop;
if (sa_is_resource(share)) {
resource = (sa_resource_t)share;
share = sa_get_resource_parent(resource);
rsrcname = sa_get_resource_attr(resource, "name");
}
group = sa_get_parent_group(share);
if (group != NULL) {
zfs = sa_get_group_attr(group, "zfs");
groupname = sa_get_group_attr(group, "name");
if (groupname != NULL &&
(strcmp(groupname, "default") == 0 || zfs != NULL)) {
sa_free_attr_string(groupname);
groupname = NULL;
}
if (zfs != NULL)
sa_free_attr_string(zfs);
}
value = sa_get_share_attr(share, "path");
if (value != NULL) {
sh->sh_path = strdup(value);
sa_free_attr_string(value);
}
if (rsrcname != NULL || groupname != NULL) {
int len = 0;
if (rsrcname != NULL)
len += strlen(rsrcname);
if (groupname != NULL)
len += strlen(groupname);
len += 3;
buff = malloc(len);
(void) snprintf(buff, len, "%s%s%s",
(rsrcname != NULL &&
strlen(rsrcname) > 0) ? rsrcname : "-",
groupname != NULL ? "@" : "",
groupname != NULL ? groupname : "");
sh->sh_res = buff;
if (rsrcname != NULL)
sa_free_attr_string(rsrcname);
if (groupname != NULL)
sa_free_attr_string(groupname);
} else {
sh->sh_res = strdup("-");
}
if (strcmp(proto, "nfs") != 0)
defprop = "\"\"";
else
defprop = "rw";
sh->sh_fstype = strdup(proto);
value = sa_proto_legacy_format(proto, share, 1);
if (value != NULL) {
if (strlen(value) > 0)
sh->sh_opts = strdup(value);
else
sh->sh_opts = strdup(defprop);
free(value);
} else {
sh->sh_opts = strdup(defprop);
}
value = sa_get_share_description(share);
if (value != NULL) {
sh->sh_descr = strdup(value);
sa_free_share_description(value);
} else {
sh->sh_descr = strdup("");
}
}
void
sa_emptyshare(struct share *sh)
{
if (sh->sh_path != NULL)
free(sh->sh_path);
sh->sh_path = NULL;
if (sh->sh_res != NULL)
free(sh->sh_res);
sh->sh_res = NULL;
if (sh->sh_fstype != NULL)
free(sh->sh_fstype);
sh->sh_fstype = NULL;
if (sh->sh_opts != NULL)
free(sh->sh_opts);
sh->sh_opts = NULL;
if (sh->sh_descr != NULL)
free(sh->sh_descr);
sh->sh_descr = NULL;
}
void
sa_update_sharetab_ts(sa_handle_t handle)
{
struct stat st;
sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
if (implhandle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
implhandle->tssharetab = TSTAMP(st.st_mtim);
}
int
sa_update_sharetab(sa_share_t share, char *proto)
{
int ret = SA_OK;
share_t sh;
char *path;
sa_handle_t handle;
path = sa_get_share_attr(share, "path");
if (path != NULL) {
(void) memset(&sh, '\0', sizeof (sh));
handle = sa_find_group_handle((sa_group_t)share);
if (handle != NULL) {
(void) sa_fillshare(share, proto, &sh);
(void) _sharefs(SHAREFS_ADD, &sh);
sa_update_sharetab_ts(handle);
sa_emptyshare(&sh);
} else {
ret = SA_CONFIG_ERR;
}
sa_free_attr_string(path);
}
return (ret);
}
int
sa_delete_sharetab(sa_handle_t handle, char *path, char *proto)
{
int ret = SA_OK;
struct stat st;
share_t sh;
if (path != NULL && proto != NULL) {
(void) memset(&sh, '\0', sizeof (sh));
sh.sh_path = path;
sh.sh_fstype = proto;
ret = _sharefs(SHAREFS_REMOVE, &sh);
if (handle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
sa_update_sharetab_ts(handle);
}
return (ret);
}
boolean_t
sa_needs_refresh(sa_handle_t handle)
{
sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
struct stat st;
char *str;
uint64_t tstamp;
scf_simple_prop_t *prop;
if (handle == NULL || implhandle->sa_service &
(SA_INIT_ONE_SHARE_FROM_NAME | SA_INIT_ONE_SHARE_FROM_HANDLE))
return (B_TRUE);
if (stat(SA_LEGACY_SHARETAB, &st) == 0 &&
TSTAMP(st.st_mtim) != implhandle->tssharetab)
return (B_TRUE);
prop = scf_simple_prop_get(implhandle->scfhandle->handle,
(const char *)SA_SVC_FMRI_BASE ":default", "state",
"lastupdate");
if (prop != NULL) {
str = scf_simple_prop_next_astring(prop);
if (str != NULL)
tstamp = strtoull(str, NULL, 0);
else
tstamp = 0;
scf_simple_prop_free(prop);
if (tstamp != implhandle->tstrans)
return (B_TRUE);
}
return (B_FALSE);
}
void
sa_fix_resource_name(char *path)
{
char *invalid = "\"/\\[]:|<>+;,?*=";
char *p = path;
char *q;
size_t len;
assert(path != NULL);
p += strspn(p, "/");
q = strchr(p, '\0');
if (q != NULL && q != path) {
while ((--q, *q == '/'))
*q = '\0';
}
if (*p == '\0') {
(void) strcpy(path, "_");
return;
}
q = p;
while ((q != NULL) && (strlen(q) > SA_MAX_RESOURCE_NAME)) {
if ((q = strchr(q, '/')) != NULL) {
++q;
p = q;
}
}
if ((len = strlen(p)) > SA_MAX_RESOURCE_NAME) {
len = SA_MAX_RESOURCE_NAME;
p = strchr(p, '\0') - (SA_MAX_RESOURCE_NAME - 1);
}
(void) memmove(path, p, len);
path[len] = '\0';
for (p = path; *p != '\0'; ++p) {
if ((iscntrl(*p)) || strchr(invalid, *p))
*p = '_';
}
}