libsharecore.c revision 24424a35444c8487648be681d47bee4f57af0ffc
/*
* 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 "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;
static void parse_dfstab(char *, xmlNodePtr);
extern char *get_token(char *);
static void dfs_free_list(xfs_sharelist_t *);
/* prototypes */
void getlegacyconfig(char *, xmlNodePtr *);
extern sa_group_t _sa_create_group(char *);
extern int _sa_remove_optionset(sa_optionset_t);
extern int set_node_share(void *, char *, char *);
extern void set_node_attr(void *, char *, char *);
/*
* 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';
}
}
}
}
}
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
{
else
} else {
} else {
}
}
}
}
/*
* open_dfstab(file)
*
* fix them.
*/
static FILE *
open_dfstab(char *file)
{
char *buff;
int 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
{
/*
* don't ignore the return since the list could have
* gone to NULL if the file only had one line in it.
*/
}
}
/*
* sa_delete_legacy(share)
*
* Delete the specified share from the legacy config file.
*/
int
{
int err;
char *path;
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)
}
}
}
} 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 formating
* 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);
/* do the dfstab format */
/*
* only update if the share is not transient -- no share type
* set or the type is not "transient".
*/
} 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_remove_property(property)
*
* remove a property only from the document.
*/
static void
{
}
/*
* 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;
/*
* if in a group, remove the inherited options and security
*/
/* find parent options to remove from child */
char *tag;
char *value1;
char *value2;
/* remove the property from the child */
(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;
char *value1;
char *value2;
/* remove duplicates from this level */
/* remove the property from the child */
(void) _sa_remove_property(prop);
}
}
}
}
(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;
}
}
}
continue;
}
else
} else {
"No share specified in dfstab: "
"line %d: %s\n"),
1);
continue;
}
defined_group = 1;
} else {
}
"Unknown group used in dfstab: "
"line %d: %s\n"),
"Unknown group specified"), 1);
continue;
}
/* this is an OK add for legacy */
&err);
(void) sa_set_share_description(share,
list->description);
(void) sa_parse_legacy_options(share,
}
} else {
"Error in dfstab: "
"line %d: %s\n"),
if (err != SA_BAD_PATH)
"Syntax"), 1);
else
"Path"), 1);
continue;
}
}
} else {
"configuration in"
"dfstab: line %d: %s\n"),
"Attempt to change configuration"),
1);
continue;
}
/* its the same group but could have changed options */
/* possibly different values */
(void) sa_destroy_optionset(opts);
(void) sa_destroy_security(secs);
}
(void) sa_parse_legacy_options(share,
}
}
}
} else {
/* shouldn't happen */
err = SA_CONFIG_ERR;
}
}
}
/*
* 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(void)
*
* 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
parse_sharetab(void)
{
int err = 0;
char *groupname;
int legacy = 0;
return (legacy);
/*
* 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 {
}
(void) sa_create_optionset(group,
}
}
}
} else {
}
}
"Problem with transient: %s\n"),
sa_errorstr(err));
(void) sa_parse_legacy_options(share,
}
}
legacy = 1;
}
} else {
/*
* 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.
*/
}
}
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;
legacy = parse_sharetab();
}
}
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
update_legacy_config(void)
{
/*
* no longer used -- this is a placeholder in case we need to
* add it back later.
*/
}
/*
* 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 {
}
/* have a valid path at this point */
}
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 inheritence 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 &&
} 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;
/* 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 */
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 struture. 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");
}
/* 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.
*/
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) {
}
}
/* dont' do anything if memory wasn't allocated */
if (newsecurity == NULL)
return (NULL);
/* found the top so working back down the stack */
/* 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.
*/
} else {
/* an entirely new property */
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
*/
/*
* fillshare(share, proto, sh)
*
* Fill the struct share with values obtained from the share object.
*/
static void
{
char *value;
char *buff;
char *zfs;
/*
* 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 {
}
else
} else
} else
}
/*
* emptyshare(sh)
*
* Free the strings in the non-NULL members of sh.
*/
static 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;
int logging = 0;
}
/* fill in share structure and write it out */
emptyshare(&shtab);
} else {
} else {
ret = SA_CONFIG_ERR;
}
}
}
return (ret);
}
/*
* sa_delete_sharetab(path, proto)
*
* remove the specified share from sharetab.
*/
int
{
int logging = 0;
#ifdef lint
#endif
}
/* should block keyboard level signals around the lock */
} else {
} else {
ret = SA_CONFIG_ERR;
}
}
}
return (ret);
}