/*
* Copyright (c) 1997-2014 Erez Zadok
* Copyright (c) 1989 Jan-Simon Pendry
* Copyright (c) 1989 Imperial College of Science, Technology & Medicine
* Copyright (c) 1989 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry at Imperial College, London.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*
* File: am-utils/amd/am_ops.c
*
*/
/*
* The order of these entries matters, since lookups in this table are done
* on a first-match basis. The entries below are a mixture of native
* filesystems supported by the OS (HAVE_FS_FOO), and some meta-filesystems
* supported by amd (HAVE_AMU_FS_FOO). The order is set here in expected
* match-hit such that more popular filesystems are listed first (nfs is the
* most popular, followed by a symlink F/S)
*/
static am_ops *vops[] =
{
#ifdef HAVE_FS_NFS
&nfs_ops, /* network F/S (version 2) */
#endif /* HAVE_FS_NFS */
#ifdef HAVE_AMU_FS_LINK
&amfs_link_ops, /* symlink F/S */
#endif /* HAVE_AMU_FS_LINK */
/*
* Other amd-supported meta-filesystems.
*/
#ifdef HAVE_AMU_FS_NFSX
&amfs_nfsx_ops, /* multiple-nfs F/S */
#endif /* HAVE_AMU_FS_NFSX */
#ifdef HAVE_AMU_FS_NFSL
&amfs_nfsl_ops, /* NFS with local link existence check */
#endif /* HAVE_AMU_FS_NFSL */
#ifdef HAVE_AMU_FS_HOST
&amfs_host_ops, /* multiple exported nfs F/S */
#endif /* HAVE_AMU_FS_HOST */
#ifdef HAVE_AMU_FS_LINKX
&amfs_linkx_ops, /* symlink F/S with link target verify */
#endif /* HAVE_AMU_FS_LINKX */
#ifdef HAVE_AMU_FS_PROGRAM
&amfs_program_ops, /* program F/S */
#endif /* HAVE_AMU_FS_PROGRAM */
#ifdef HAVE_AMU_FS_UNION
&amfs_union_ops, /* union F/S */
#endif /* HAVE_AMU_FS_UNION */
/* terminate with a period, newline, and NULL */
if (buf[strlen(buf)-1] == '\n')
buf[strlen(buf) - 4] = '\0';
else
buf[strlen(buf) - 2] = '\0';
xstrlcat(buf, ".\n", l);
}
/*
* return string option which is the reverse of opt.
* nosuid -> suid
* quota -> noquota
* ro -> rw
* etc.
* may return pointer to static buffer or subpointer within opt.
*/
static char *
reverse_option(const char *opt)
{
static char buf[80];
/* sanity check */
if (!opt)
return NULL;
/* check special cases */
/* XXX: if this gets too long, rewrite the code more flexibly */
if (STREQ(opt, "ro")) return "rw";
if (STREQ(opt, "rw")) return "ro";
if (STREQ(opt, "bg")) return "fg";
if (STREQ(opt, "fg")) return "bg";
if (STREQ(opt, "soft")) return "hard";
if (STREQ(opt, "hard")) return "soft";
/* check if string starts with 'no' and chop it */
if (NSTREQ(opt, "no", 2)) {
xstrlcpy(buf, &opt[2], sizeof(buf));
} else {
/* finally return a string prepended with 'no' */
xstrlcpy(buf, "no", sizeof(buf));
xstrlcat(buf, opt, sizeof(buf));
}
return buf;
}
/*
* start with an empty string. for each opts1 option that is not
* in opts2, add it to the string (make sure the reverse of it
* isn't in either). finally add opts2. return new string.
* Both opts1 and opts2 must not be null!
* Caller must eventually free the string being returned.
*/
static char *
merge_opts(const char *opts1, const char *opts2)
{
mntent_t mnt2; /* place holder for opts2 */
char *newstr; /* new string to return (malloc'ed) */
char *tmpstr; /* temp */
char *eq; /* pointer to whatever follows '=' within temp */
char oneopt[80]; /* one option w/o value if any */
char *revoneopt; /* reverse of oneopt */
size_t len = strlen(opts1) + strlen(opts2) + 2; /* space for "," and NULL */
char *s1 = xstrdup(opts1); /* copy of opts1 to munge */
for (tmpstr = strtok(s1, ",");
tmpstr;
tmpstr = strtok(NULL, ",")) {
/* copy option to temp buffer */
xstrlcpy(oneopt, tmpstr, sizeof(oneopt));
/* if option has a value such as rsize=1024, chop the value part */
if ((eq = strchr(oneopt, '=')))
*eq = '\0';
/* find reverse option of oneopt */
revoneopt = reverse_option(oneopt);
/* if option or its reverse exist in opts2, ignore it */
if (amu_hasmntopt(&mnt2, oneopt) || amu_hasmntopt(&mnt2, revoneopt))
continue;
/* add option to returned string */
if (newstr[0]) {
xstrlcat(newstr, ",", len);
xstrlcat(newstr, tmpstr, len);
} else {
xstrlcpy(newstr, tmpstr, len);
}
}
/*
* First crack the global opts and the local opts
*/
if (!eval_fs_opts(fo, key, g_key, path, keym, map)) {
rop = &amfs_error_ops;
} else if (fo->opt_type == 0) {
plog(XLOG_USER, "No fs type specified (key = \"%s\", map = \"%s\")", keym, map);
rop = &amfs_error_ops;
} else {
/*
* Next find the correct filesystem type
*/
rop = ops_search(fo->opt_type);
if (!rop) {
plog(XLOG_USER, "fs type \"%s\" not recognized", fo->opt_type);
rop = &amfs_error_ops;
}
}
/*
* Make sure we have a default mount option.
* Otherwise skip past any leading '-'.
*/
if (fo->opt_opts == 0)
fo->opt_opts = xstrdup("rw,defaults");
else if (*fo->opt_opts == '-') {
/*
* We cannot simply do fo->opt_opts++ here since the opts
* module will try to free the pointer fo->opt_opts later.
* So just reallocate the thing -- stolcke 11/11/94
*/
char *old = fo->opt_opts;
fo->opt_opts = xstrdup(old + 1);
XFREE(old);
}
/*
* If addopts option was used, then append it to the
* current options and remote mount options.
*/
if (fo->opt_addopts) {
if (STREQ(fo->opt_opts, fo->opt_remopts)) {
/* optimize things for the common case where opts==remopts */
char *mergedstr;
mergedstr = merge_opts(fo->opt_opts, fo->opt_addopts);
plog(XLOG_INFO, "merge rem/opts \"%s\" add \"%s\" => \"%s\"",
fo->opt_opts, fo->opt_addopts, mergedstr);
XFREE(fo->opt_opts);
XFREE(fo->opt_remopts);
fo->opt_opts = mergedstr;
fo->opt_remopts = xstrdup(mergedstr);
} else {
char *mergedstr, *remmergedstr;
mergedstr = merge_opts(fo->opt_opts, fo->opt_addopts);
plog(XLOG_INFO, "merge opts \"%s\" add \"%s\" => \"%s\"",
fo->opt_opts, fo->opt_addopts, mergedstr);
XFREE(fo->opt_opts);
fo->opt_opts = mergedstr;
remmergedstr = merge_opts(fo->opt_remopts, fo->opt_addopts);
plog(XLOG_INFO, "merge remopts \"%s\" add \"%s\" => \"%s\"",
fo->opt_remopts, fo->opt_addopts, remmergedstr);
XFREE(fo->opt_remopts);
fo->opt_remopts = remmergedstr;
}
}
/*
* Initialize opt_mount_type to "nfs", if it's not initialized already
*/
if (!fo->opt_mount_type)
fo->opt_mount_type = "nfs";
/* Normalize the sublink and make it absolute */
link_dir = fo->opt_sublink;
if (link_dir && link_dir[0] && link_dir[0] != '/') {
link_dir = str3cat((char *) NULL, fo->opt_fs, "/", link_dir);
normalize_slash(link_dir);
XFREE(fo->opt_sublink);
fo->opt_sublink = link_dir;
}
/*
* Check the filesystem is happy
*/
XFREE(fo->fs_mtab);
fo->fs_mtab = rop->fs_match(fo);
if (fo->fs_mtab)
return rop;