libsharecore.c revision 97df5ac96dbf15a7624a8e07b7dd686404d988c0
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* core library for common functions across all config store types
* parsing. Need to eliminate XML where possible.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include "libshare.h"
#include "libshare_impl.h"
#include <fcntl.h>
#include <grp.h>
#include <limits.h>
#include <signal.h>
#include <libintl.h>
#include <dirent.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(1m) command for all share management\n",
"# This file is reconstructed and only maintained for backward\n",
"# compatibility. Configuration lines could be lost.\n",
"#\n"
};
/* will be much smaller, but this handles bad syntax in the file */
#define MAXARGSFORSHARE 256
/* used internally only */
typedef
struct sharelist {
int persist;
char *path;
char *resource;
char *fstype;
char *options;
char *description;
char *group;
char *origline;
int lineno;
extern char *get_token(char *);
static void dfs_free_list(xfs_sharelist_t *);
/* prototypes */
extern int _sa_remove_optionset(sa_optionset_t);
extern int set_node_share(void *, char *, char *);
extern void set_node_attr(void *, char *, char *);
/*
* sablocksigs(*sigs)
*
* block important signals for a critical region. Arg is a pointer to
* a sigset_t that is used later for the unblock.
*/
void
{
}
}
/*
* saunblocksigs(*sigs)
*
* unblock previously blocked signals from the sigs arg.
*/
void
{
}
/*
* alloc_sharelist()
*
* allocator function to return an zfs_sharelist_t
*/
static xfs_sharelist_t *
{
return (item);
}
/*
* fix_notice(list)
*
* the do not modify notice if it doesn't exist.
*/
static xfs_sharelist_t *
{
int i;
/* zero length dfstab */
list = alloc_sharelist();
return (NULL);
}
item = alloc_sharelist();
} else {
}
}
}
}
return (list);
}
/*
* getdfstab(dfs)
*
* Returns an zfs_sharelist_t list of lines from the dfstab file
* pointed to by the FILE pointer dfs. Each entry is parsed and the
* original line is also preserved. Used in parsing and updating the
* dfstab file.
*/
static xfs_sharelist_t *
{
char *bp;
char *token;
char *args[MAXARGSFORSHARE];
int argc;
int c;
static int line = 0;
line = 0;
line++;
if (buff[0] == '#') {
item = alloc_sharelist();
/* if no path, then comment */
} else {
}
} else {
break;
}
continue;
} else if (buff[0] == '\n') {
continue;
}
optind = 1;
item = alloc_sharelist();
break;
} else {
}
argc = 0;
if (argc < MAXARGSFORSHARE)
}
switch (c) {
case 'p':
break;
case 'F':
break;
case 'o':
break;
case 'd':
break;
case 'g':
break;
default:
break;
}
}
optind++;
char *resource;
char *optgroup;
*optgroup++ = '\0';
}
}
/* NFS is the default if none defined */
}
}
return (first);
}
/*
* finddfsentry(list, path)
*
* Look for path in the zfs_sharelist_t list and return the entry if it
* exists.
*/
static xfs_sharelist_t *
{
return (item);
}
return (NULL);
}
/*
* remdfsentry(list, path, proto)
*
* Remove the specified path (with protocol) from the list. This will
* remove it from dfstab when the file is rewritten.
*/
static xfs_sharelist_t *
{
/* skip comment entry but don't lose it */
continue;
}
/* if proto is NULL, remove all protocols */
break;
break;
}
else
}
return (list);
}
/*
* remdfsline(list, line)
*
* Remove the line specified from the list.
*/
static xfs_sharelist_t *
{
/* skip comment entry but don't lose it */
continue;
}
break;
}
else
}
return (list);
}
/*
* adddfsentry(list, share, proto)
*
* Add an entry to the dfstab list for share (relative to proto). This
* is used to update dfstab for legacy purposes.
*/
static xfs_sharelist_t *
{
char *groupname;
item = alloc_sharelist();
}
}
}
} else {
/* do nothing */;
}
}
return (list);
}
/*
* outdfstab(dfstab, list)
*
* Output the list to dfstab making sure the file is truncated.
* Comments and errors are preserved.
*/
static void
{
"share %s%s%s%s%s%s%s %s%s%s%s%s\n",
" -d \"" : "",
} else {
}
} else {
else
}
}
}
/*
* open_dfstab(file)
*
* fix them.
*/
static FILE *
open_dfstab(char *file)
{
char *buff;
int grsize;
}
grsize);
else
}
return (dfstab);
}
/*
* sa_comment_line(line, err)
*
* Add a comment to the dfstab file with err as a prefix to the
* original line.
*/
static void
{
sablocksigs(&old);
/*
* don't ignore the return since the list could have
* gone to NULL if the file only had one line in it.
*/
saunblocksigs(&old);
}
}
/*
* sa_delete_legacy(share, protocol)
*
* Delete the specified share from the legacy config file.
*/
int
{
int err;
char *path;
/*
* Protect against shares that don't have paths. This is not
* really an error at this point.
*/
return (ret);
sablocksigs(&old);
protocol);
} else {
char *proto = sa_get_optionset_attr(
optionset, "type");
proto);
ret = SA_NO_MEMORY;
/*
* may want to only do the dfstab if
* this call returns NOT IMPLEMENTED
* but it shouldn't hurt.
*/
if (err != SA_NOT_IMPLEMENTED)
}
}
}
}
saunblocksigs(&old);
} else {
else
ret = SA_CONFIG_ERR;
}
return (ret);
}
/*
* sa_update_legacy(share, proto)
*
* There is an assumption that dfstab will be the most common form of
* legacy configuration file for shares, but not the only one. Because
* of that, dfstab handling is done in the main code with calls to
* this function and protocol specific calls to deal with formatting
* will be dfstab, there is a provision for calling a protocol
* specific plugin interface that allows the protocol plugin to do its
* own legacy files and skip the dfstab update.
*/
int
{
char *path;
char *persist;
if (ret != SA_NOT_IMPLEMENTED)
return (ret);
if (!(features & SA_FEATURE_DFSTAB))
return (ret);
/* do the dfstab format */
/*
* only update if the share is not transient -- no share type
* set or the type is not "transient".
*/
sablocksigs(&old);
saunblocksigs(&old);
} else {
else
ret = SA_CONFIG_ERR;
}
}
return (ret);
}
/*
* sa_is_security(optname, proto)
*
* Check to see if optname is a security (named optionset) specific
* property for the specified protocol.
*/
int
{
int ret = 0;
return (ret);
}
/*
* add_syntax_comment(root, line, err, todfstab)
*
* Add a comment to the document indicating a syntax error. If
* todfstab is set, write it back to the dfstab file as well.
*/
static void
{
if (todfstab)
}
/*
* sa_is_share(object)
*
* returns true of the object is of type "share".
*/
int
sa_is_share(void *object)
{
return (1);
}
return (0);
}
/*
* sa_is_resource(object)
*
* returns true of the object is of type "share".
*/
int
sa_is_resource(void *object)
{
return (1);
}
return (0);
}
/*
* _sa_remove_property(property)
*
* remove a property only from the document.
*/
static void
{
}
/*
* _sa_create_dummy_share()
*
* Create a share entry suitable for parsing but not tied to any real
* config tree. Need to have a parent as well as the node to parse
* on. Free using _sa_free_dummy_share(share);
*/
static sa_group_t
{
if (parent_node != NULL) {
NULL);
if (child_node != NULL) {
/*
* Use a "zfs" tag since that will make sure nothing
* really attempts to put values into the
* repository. Also ZFS is currently the only user of
* this interface.
*/
} else {
}
}
return (child_node);
}
/*
* _sa_free_dummy_share(share)
*
* Free the dummy share and its parent. It is an error to try and
* free something that isn't a dummy.
*/
static int
{
char *name;
/* Real shares always have a path but a dummy doesn't */
} else {
/*
* If there is a parent, do the free on that since
* xmlFreeNode is a recursive function and free's an
* child nodes.
*/
}
}
}
return (ret);
}
/*
* sa_parse_legacy_options(group, options, proto)
*
* In order to support legacy configurations, we allow the protocol
* specific plugin to parse legacy syntax options (like those in
* share).
*
* Once the optionset has been created, we then get the derived
* optionset of the parent (options from the optionset of the parent
* and any parent it might have) and remove those from the created
* optionset. This avoids duplication of options.
*/
int
{
int ret = SA_INVALID_PROTOCOL;
int using_dummy = B_FALSE;
char *pvalue;
/*
* If "group" is NULL, this is just a parse without saving
* anything in either SMF or ZFS. Create a dummy group to
* handle this case.
*/
}
if (using_dummy) {
/* Since this is a dummy parse, cleanup and quit here */
(void) _sa_free_dummy_share(parent);
return (ret);
}
return (ret);
/*
* If in a group, remove the inherited options and security
*/
return (ret);
/* Find parent options to remove from child */
char *tag;
char *value;
continue;
"value");
"value");
/*
* Remove the property
* from the
* child. While we
* removed it, we
* don't need to reset
* as we do below
* since we always
* search from the
* beginning.
*/
(void) _sa_remove_property(
prop);
}
}
}
/*
* All properties removed so remove the
* optionset if it is on a share
*/
(void) _sa_remove_optionset(localoptions);
}
}
/*
* Need to remove security here. If there are no
* bother since those are the only ones that would be
* affected.
*/
if (localoptions != NULL) {
char *tag;
/*
* prop's value only changes outside this loop
*/
char *value;
/*
* Need to get the next prop
* now since we could break
* the list during removal.
*/
/* remove Duplicates from this level */
"value");
/*
* remove the property
* from the child
*/
(void) _sa_remove_property
(popt);
}
}
}
}
(void) sa_destroy_optionset(localoptions);
}
return (ret);
}
/*
* dfs_free_list(list)
*
* Free the data in each list entry of the list as well as freeing the
* entries themselves. We need to avoid memory leaks and don't want to
* dereference any NULL members.
*/
static void
{
}
}
/*
* parse_dfstab(dfstab, root)
*
* Open and read the existing dfstab, parsing each line and adding it
* to the internal configuration. Make sure syntax errors, etc are
* preserved as comments.
*/
static void
{
int err;
int defined_group;
char *oldprops;
/* read the dfstab format file and fill in the doc tree */
return;
defined_group = 0;
err = 0;
/*
* Comment line that we will likely skip.
* If the line has the syntax:
* # error: string: string
* It should be preserved until manually deleted.
*/
char *line;
char *error;
char *cmd;
int len;
*cmd = '\0';
cmd += 2;
error, 0);
}
}
}
continue;
}
else
} else {
"No share specified in dfstab: "
"line %d: %s\n"),
continue;
}
defined_group = 1;
} else {
}
"Unknown group used in dfstab: line %d: %s\n"),
1);
continue;
}
/* Shouldn't happen unless an SMF error */
err = SA_CONFIG_ERR;
continue;
}
continue;
/* This is an OK add for legacy */
(void) sa_set_share_description(share,
list->description);
(void) sa_parse_legacy_options(share,
}
(void) sa_set_share_attr(share,
} else {
"Error in dfstab: line %d: %s\n"),
if (err != SA_BAD_PATH)
else
"Path"), 1);
continue;
}
} else {
"Attempt to change configuration in "
"dfstab: line %d: %s\n"),
"Attempt to change configuration"), 1);
continue;
}
/*
* It is the same group but could have changed
* options. Make sure we include the group's
* properties so we don't end up moving them to
* the share inadvertantly. The last arg being
* true says to get the inherited properties as well
* as the local properties.
*/
B_TRUE);
continue;
/* possibly different values */
(void) sa_destroy_optionset(opts);
for (secs = sa_get_security(
(void) sa_destroy_security(
secs);
}
(void) sa_parse_legacy_options(share,
}
}
}
}
/*
* legacy_removes(group, file)
*
* Find any shares that are "missing" from the legacy file. These
* should be removed from the configuration since they are likely from
* a legacy app or the admin modified the dfstab file directly. We
* have to support this even if it is not the recommended way to do
* things.
*/
static void
{
char *path;
/* now see if the share is in the dfstab file */
/* The share was removed this way */
(void) sa_remove_share(share);
/*
* Start over since the list was broken
*/
goto retry;
}
}
}
}
}
/*
* getlegacyconfig(path, root)
*
* Parse dfstab and build the legacy configuration. This only gets
* called when a change was detected.
*/
void
{
/*
* Walk the default shares and find anything
* missing. we do this first to make sure it
* is cleaned up since there may be legacy
* cleanup SMF.
*/
/* Parse the dfstab and add anything new */
}
}
}
}
/*
* get_share_list(&err)
*
* Get a linked list of all the shares on the system from
* can't use due to package dependencies.
*/
static xfs_sharelist_t *
get_share_list(int *errp)
{
struct share *sharetab_entry;
newp = alloc_sharelist();
goto err;
/*
* Link into the list here so we don't leak
* memory on a failure from strdup().
*/
} else {
}
goto err;
goto err;
goto err;
goto err;
goto err;
}
} else {
}
/*
* Caller must free the mount list
*/
return (headp);
err:
/*
* Out of memory so cleanup and leave.
*/
return (NULL);
}
/*
* parse_sharetab(handle)
*
* in the repository. These shares are marked transient. We also need
* to see if they are ZFS shares since ZFS bypasses the SMF
* repository.
*/
int
{
int err = 0;
char *groupname;
int legacy = 0;
return (legacy);
/*
* If this is a legacy share, mark as shared so we
* only update sharetab appropriately. We also keep
* the sharetab options in order to display for legacy
* share with no arguments.
*/
continue;
}
/*
* This share is transient so needs to be
* added. Initially, this will be under
* default(legacy) unless it is a ZFS
* share. If zfs, we need a zfs group.
*/
/* There is a defined group */
*groupname++ = '\0';
} else {
/*
* While this case shouldn't
* occur very often, it does
* occur out of a "zfs set
* sharenfs=off" when the
* dataset is also set to
* canmount=off. A warning
* will then cause the zfs
* command to abort. Since we
* add it to the default list,
* everything works properly
* anyway and the library
* doesn't need to give a
* warning.
*/
}
} else {
"zfs", &err);
err == SA_NO_PERMISSION) {
"zfs");
}
(void) sa_create_optionset(
(void) sa_set_group_attr(group,
"zfs", "true");
}
}
}
} else {
}
}
(void) sa_parse_legacy_options(share,
}
}
legacy = 1;
}
}
return (legacy);
}
/*
* Get the transient shares from the sharetab (or other) file. since
* these are transient, they only appear in the working file and not
* in a repository.
*/
int
{
int legacy = 0;
int numproto;
int i;
for (i = 0; i < numproto; i++)
}
}
return (legacy);
}
/*
* sa_has_prop(optionset, prop)
*
* Is the specified property a member of the optionset?
*/
int
{
char *name;
int result = 0;
result = 1;
}
}
return (result);
}
/*
* Update legacy files
*
* in dfstab and sharetab
*/
void
{
/*
* no longer used -- this is a placeholder in case we need to
* add it back later.
*/
#ifdef lint
#endif
}
/*
* sa_valid_property(object, proto, property)
*
* check to see if the specified property is valid relative to the
* specified protocol. The protocol plugin is called to do the work.
*/
int
{
}
return (ret);
}
/*
* sa_fstype(path)
*
* Given path, return the string representing the path's file system
* type. This is used to discover ZFS shares.
*/
char *
{
int err;
if (err < 0)
else
/*
* If we have a valid path at this point ret, return the fstype.
*/
return (NULL);
}
void
sa_free_fstype(char *type)
{
}
/*
* sa_get_derived_optionset(object, proto, hier)
*
* Work backward to the top of the share object tree and start
* copying protocol specific optionsets into a newly created
* optionset that doesn't have a parent (it will be freed
* later). This provides for the property inheritance model. That
* is, properties closer to the share take precedence over group
* level. This also provides for groups of groups in the future.
*/
{
if (hier &&
hier);
} else {
(xmlChar *)"optionset");
if (newoptionset != NULL) {
}
}
/* Dont' do anything if memory wasn't allocated */
if (newoptionset == NULL)
return (NULL);
/* Found the top so working back down the stack */
/* add optionset to the newoptionset */
char *name;
char *value;
continue;
/* Replace the value with the new value */
/*
* Only set if value is non NULL, old value ok
* if it is NULL.
*/
} else {
/* an entirely new property */
value);
newprop = (sa_property_t)
}
}
}
}
}
return (newoptionset);
}
void
{
/* While it shouldn't be linked, it doesn't hurt */
}
}
/*
* sa_get_all_security_types(object, proto, hier)
*
* Find all the security types set for this object. This is
* preliminary to getting a derived security set. The return value is an
* optionset containg properties which are the sectype values found by
* walking up the XML document structure. The returned optionset
* is a derived optionset.
*
* If hier is 0, only look at object. If non-zero, walk up the tree.
*/
{
if (hier &&
else
(xmlChar *)"optionset");
return (options);
/* Hit the top so collect the security types working back. */
char *type;
char *sectype;
continue;
}
/*
* Have a security type, check to see if
* already present in optionset and add if it
* isn't.
*/
"true");
prop = (sa_property_t)
(xmlNodePtr)prop);
}
}
}
}
return (options);
}
/*
* sa_get_derived_security(object, sectype, proto, hier)
*
* Get the derived security(named optionset) for the object given the
* sectype and proto. If hier is non-zero, walk up the tree to get all
* properties defined for this object, otherwise just those on the
* object.
*/
{
if (hier &&
} else {
(xmlChar *)"security");
if (newsecurity != NULL) {
}
}
/* Don't do anything if memory wasn't allocated */
if (newsecurity == NULL)
return (newsecurity);
/* Found the top so working back down the stack. */
return (newsecurity);
/* add security to the newsecurity */
char *name;
char *value;
/* Replace the value with the new value */
/*
* Only set if value is non NULL, old
* value ok if it is NULL. The value
* must be associated with the "value"
* tag within XML.
*/
} else {
/* An entirely new property */
value);
newprop = (sa_property_t)
}
}
}
}
return (newsecurity);
}
void
{
/* while it shouldn't be linked, it doesn't hurt */
}
}
/*
* sharetab utility functions
*
* Makes use of the original sharetab.c from fs.d/nfs/lib
*/
/*
* sa_fillshare(share, proto, sh)
*
* Fill the struct share with values obtained from the share object.
*/
void
{
char *value;
char *buff;
char *zfs;
char *defprop;
/*
* We only want to deal with the path level shares for the
* sharetab file. If a resource, get the parent.
*/
if (sa_is_resource(share)) {
}
/*
* since the groupname is either "default" or the
* group is a ZFS group, we don't want to keep
* groupname. We do want it if it is any other type of
* group.
*/
}
}
}
int len = 0;
} else {
}
/*
* Get correct default prop string. NFS uses "rw", others use
* "".
*/
defprop = "\"\"";
else
defprop = "rw";
else
} else {
}
} else {
}
}
/*
* sa_emptyshare(sh)
*
* Free the strings in the non-NULL members of sh.
*/
void
{
}
/*
* sa_update_sharetab(share, proto)
*
* Update the sharetab file with info from the specified share.
* This could be an update or add.
*/
int
{
char *path;
/*
* Fill in share structure and send it to the kernel.
*/
sa_emptyshare(&sh);
}
return (ret);
}
/*
* sa_delete_sharetab(path, proto)
*
* remove the specified share from sharetab.
*/
int
{
/*
* Both the path and the proto are
* keys into the sharetab.
*/
}
return (ret);
}
/*
* sa_fix_resource_name(path)
*
* change all illegal characters to something else. For now, all get
* converted to '_' and the leading '/' is stripped off. This is used
* to construct an resource name (SMB share name) that is valid.
* Caller must pass a valid path.
*/
void
sa_fix_resource_name(char *path)
{
char *cp;
/* make sure we are appropriate length */
if (*cp == '/')
cp++; /* skip leading slash */
cp++;
}
/* two cases - cp == NULL and cp is substring of path */
/* just take last SA_MAX_RESOURCE_NAME chars */
} else {
}
/*
* Don't want any of the characters that are not allowed
* in an SMB share name. Replace them with '_'.
*/
while (*path) {
switch (*path) {
case '/':
case '"':
case '\\':
case '[':
case ']':
case ':':
case '|':
case '<':
case '>':
case '+':
case ';':
case ',':
case '?':
case '*':
case '=':
case '\t':
*path = '_';
break;
}
path++;
}
}