#pragma ident "@(#)autod_parse.c 1.31 05/06/08 SMI"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <errno.h>
#include <pwd.h>
#include <mntopts.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fstab.h>
#include <locale.h>
#include <stdlib.h>
#include <unistd.h>
#include <rpc/rpc.h>
#include <rpcsvc/mount.h>
#include <fcntl.h>
#include <limits.h>
#include "automount.h"
#include "auto_mntopts.h"
struct _hiernode
{
char dirname[MAXFILENAMELEN+1];
struct _hiernode *subdir;
struct _hiernode *leveldir;
struct mapent *mapent;
};
typedef struct _hiernode hiernode;
void free_mapent(struct mapent *);
static int mapline_to_mapent(struct mapent **, struct mapline *, char *, char *,
char *, char *, uint_t);
static int hierarchical_sort(struct mapent *, hiernode **, char *, char *);
static int push_options(hiernode *, char *, char *, int);
static int set_mapent_opts(struct mapent *, char *, char *, char *);
static int get_opts(char *, char *, char *, size_t, bool_t *);
static int fstype_opts(struct mapent *, char *, char *, char *);
static int modify_mapents(struct mapent **, char *, char *, char *, hiernode *,
char *, uint_t, bool_t);
static int set_and_fake_mapent_mntlevel(hiernode *, char *, char *, char *,
struct mapent **, uint_t, char *, bool_t);
static int mark_level1_root(hiernode *, char *);
static int mark_and_fake_level1_noroot(hiernode *, char *, char *, char *,
struct mapent **, uint_t i, char *);
static int convert_mapent_to_automount(struct mapent *, char *, char *);
static int automount_opts(char **, char *);
static int parse_fsinfo(char *, struct mapent *);
static int parse_nfs(char *, struct mapent *, char *, char *, char **, char **);
static int parse_special(struct mapent *, char *, char *, char **, char **,
int);
static int get_dir_from_path(char *, char **, int);
static int alloc_hiernode(hiernode **, char *);
static void free_hiernode(hiernode *);
static void trace_mapents(char *, struct mapent *);
static void trace_hierarchy(hiernode *, int);
static struct mapent *do_mapent_hosts(char *, char *, uint_t, int *);
static void freeex_ent(struct exportnode *);
static void freeex(struct exportnode *);
static struct mapent *do_mapent_fstab(char *, char *, uint_t, int *);
static struct mapent *do_mapent_static(char *, uint_t, int *);
static void dump_mapent_err(struct mapent *, char *, char *);
#define PARSE_OK 0
#define PARSE_ERROR -1
#define MAX_FSLEN 32
#define MAPENT_NOERR 0
#define MAPENT_UATFS 1
static int
strldup(char **dstp, const char *str, size_t maxlen)
{
size_t len;
char *dst;
len = strlen(str) + 1;
if (len > maxlen)
return (ENAMETOOLONG);
dst = malloc(len);
if (dst == NULL)
return (ENOMEM);
memcpy(dst, str, len);
*dstp = dst;
return (0);
}
static char *
strprefix_slash(const char *str)
{
size_t outstrsize;
char *outstr;
outstrsize = strlen(str) + 2;
outstr = malloc(outstrsize);
if (outstr == NULL)
return (NULL);
*outstr = '/';
memcpy(outstr + 1, str, outstrsize - 1);
return (outstr);
}
struct mapent *
parse_entry(char *key, char *mapname, char *mapopts, char *subdir,
uint_t isdirect, bool_t *iswildcard,
bool_t isrestricted, bool_t mount_access, int *err)
{
char *stack[STACKSIZ];
char **stkptr = stack;
struct mapline ml;
char *p;
char defaultopts[AUTOFS_MAXOPTSLEN];
struct mapent *mapents = NULL;
hiernode *rootnode = NULL;
if (strcmp(mapname, "-hosts") == 0) {
mapents = do_mapent_hosts(mapopts, key, isdirect, err);
if (mapents == NULL)
return (mapents);
if (trace > 3)
trace_mapents("do_mapent_hosts:(return)", mapents);
*err = hierarchical_sort(mapents, &rootnode, key, mapname);
if (*err != 0)
goto parse_error_quiet;
} else if (strcmp(mapname, "-fstab") == 0) {
mapents = do_mapent_fstab(mapopts, key, isdirect, err);
if (mapents == NULL)
return (mapents);
if (trace > 3)
trace_mapents("do_mapent_fstab:(return)", mapents);
*err = hierarchical_sort(mapents, &rootnode, key, mapname);
if (*err != 0)
goto parse_error_quiet;
} else if (strcmp(mapname, "-static") == 0) {
mapents = do_mapent_static(key, isdirect, err);
if (mapents == NULL)
return (mapents);
if (trace > 3)
trace_mapents("do_mapent_static:(return)", mapents);
*err = hierarchical_sort(mapents, &rootnode, key, mapname);
if (*err != 0)
goto parse_error_quiet;
} else {
stack_op(INIT, NULL, stack, &stkptr);
switch (getmapent(key, mapname, &ml, stack, &stkptr, iswildcard,
isrestricted)) {
case __NSW_SUCCESS:
break;
case __NSW_NOTFOUND:
*err = ENOENT;
return ((struct mapent *)NULL);
case __NSW_UNAVAIL:
syslog(LOG_ERR, "parse_entry: getmapent for map %s, key %s failed",
mapname, key);
*err = EIO;
return ((struct mapent *)NULL);
}
if (trace > 1)
trace_prt(1, " mapline: %s\n", ml.linebuf);
for (p = key; *p != '\0'; p++) {
if (isspace(*p)) {
syslog(LOG_ERR,
"parse_entry: bad key in map %s: %s", mapname, key);
*err = EIO;
return ((struct mapent *)NULL);
}
}
*err = mapline_to_mapent(&mapents, &ml, key, mapname,
mapopts, defaultopts, isdirect);
if (*err != 0)
goto parse_error;
if (mapents == NULL) {
*err = 0;
return (mapents);
}
*err = hierarchical_sort(mapents, &rootnode, key, mapname);
if (*err != 0)
goto parse_error_quiet;
*err = push_options(rootnode, defaultopts, mapopts,
MAPENT_NOERR);
if (*err != 0)
goto parse_error;
if (trace > 3) {
trace_prt(1, "\n\tpush_options (return)\n");
trace_prt(0, "\tdefault options=%s\n", defaultopts);
trace_hierarchy(rootnode, 0);
};
*err = parse_fsinfo(mapname, mapents);
if (*err != 0)
goto parse_error;
}
*err = modify_mapents(&mapents, mapname, mapopts, subdir,
rootnode, key, isdirect, mount_access);
if (*err != 0)
goto parse_error;
if (rootnode != NULL)
free_hiernode(rootnode);
return (mapents);
parse_error:
syslog(LOG_ERR, "parse_entry: mapentry parse error: map=%s key=%s",
mapname, key);
parse_error_quiet:
free_mapent(mapents);
if (rootnode != NULL)
free_hiernode(rootnode);
return ((struct mapent *)NULL);
}
static int
mapline_to_mapent(struct mapent **mapents, struct mapline *ml, char *key,
char *mapname, char *mapopts, char *defaultopts,
uint_t isdirect)
{
struct mapent *me = NULL;
struct mapent *mp;
char w[MAXPATHLEN];
char wq[MAXPATHLEN];
int implied;
int err;
char *lp = ml->linebuf;
char *lq = ml->lineqbuf;
switch (macro_expand(key, lp, lq, LINESZ)) {
case MEXPAND_OK:
break;
case MEXPAND_LINE_TOO_LONG:
syslog(LOG_ERR,
"mapline_to_mapent: map %s: line too long (max %d chars)",
mapname, LINESZ - 1);
return (EIO);
case MEXPAND_VARNAME_TOO_LONG:
syslog(LOG_ERR,
"mapline_to_mapent: map %s: variable name too long",
mapname);
return (EIO);
}
if (trace > 3 && (strcmp(ml->linebuf, lp) != 0))
trace_prt(1,
" mapline_to_mapent: (expanded) mapline (%s,%s)\n",
ml->linebuf, ml->lineqbuf);
*mapents = NULL;
if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
return (EIO);
if (*w == '-') {
if (strlcpy(defaultopts, w, MAXOPTSLEN) >= MAXOPTSLEN) {
syslog(LOG_ERR, "default options are too long");
return (EIO);
}
if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
return (EIO);
} else
CHECK_STRCPY(defaultopts, mapopts, MAXOPTSLEN);
implied = *w != '/';
while (*w == '/' || implied) {
mp = me;
if ((me = (struct mapent *)malloc(sizeof (*me))) == NULL)
goto alloc_failed;
(void) memset((char *)me, 0, sizeof (*me));
if (*mapents == NULL)
*mapents = me;
else
mp->map_next = me;
if (isdirect)
me->map_root = strdup("");
else
me->map_root = strprefix_slash(key);
if (me->map_root == NULL)
goto alloc_failed;
if (strcmp(w, "/") == 0 || implied)
me->map_mntpnt = strdup("");
else
me->map_mntpnt = strdup(w);
if (me->map_mntpnt == NULL)
goto alloc_failed;
if (implied)
implied = 0;
else {
if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
return (EIO);
if (w[0] == '-') {
err = strldup(&me->map_mntopts, w, MAXOPTSLEN);
if (err == ENAMETOOLONG) {
syslog(LOG_ERR,
"mount options for are too long");
return (EIO);
}
if (err == ENOMEM)
goto alloc_failed;
if (getword(w, wq, &lp, &lq, ' ',
sizeof (w)) == -1)
return (EIO);
}
}
if (w[0] == '\0' || w[0] == '-') {
syslog(LOG_ERR,
"mapline_to_mapent: bad location=%s map=%s key=%s",
w, mapname, key);
return (EIO);
}
if (((me->map_fsw = strdup(w)) == NULL) ||
((me->map_fswq = strdup(wq)) == NULL))
goto alloc_failed;
if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
return (EIO);
while (*w && *w != '/') {
char *fsw, *fswq;
if (asprintf(&fsw, "%s %s", me->map_fsw, w) == -1)
goto alloc_failed;
free(me->map_fsw);
me->map_fsw = fsw;
if (asprintf(&fswq, "%s %s", me->map_fswq, wq) == -1)
goto alloc_failed;
free(me->map_fswq);
me->map_fswq = fswq;
if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
return (EIO);
}
me->map_mntlevel = -1;
me->map_modified = FALSE;
me->map_faked = FALSE;
me->map_err = MAPENT_NOERR;
me->map_next = NULL;
}
if (*mapents == NULL || w[0] != '\0') {
if (verbose) {
if (*mapents == NULL)
syslog(LOG_ERR,
"mapline_to_mapent: parsed with null mapents");
else
syslog(LOG_ERR,
"mapline_to_mapent: parsed nononempty w=%s", w);
}
return (EIO);
}
if (trace > 3)
trace_mapents("mapline_to_mapent:", *mapents);
return (0);
alloc_failed:
syslog(LOG_ERR, "mapline_to_mapent: Memory allocation failed");
return (ENOMEM);
}
static int
hierarchical_sort(struct mapent *mapents, hiernode **rootnode, char *key,
char *mapname)
{
hiernode *prevnode, *currnode, *newnode;
char *path;
char dirname[MAXFILENAMELEN];
int rc = 0;
struct mapent *me = mapents;
*rootnode = NULL;
if ((rc = alloc_hiernode(rootnode, "")) != 0)
return (rc);
while (me != NULL) {
path = me->map_mntpnt;
if ((rc = get_dir_from_path(dirname, &path,
sizeof (dirname))) != 0)
return (rc);
prevnode = *rootnode;
currnode = (*rootnode)->subdir;
while (dirname[0] != '\0') {
if (currnode != NULL) {
if (strcmp(currnode->dirname, dirname) == 0) {
prevnode = currnode;
currnode = currnode->subdir;
} else {
prevnode = currnode;
currnode = currnode->leveldir;
if (currnode == NULL) {
if ((rc = alloc_hiernode
(&newnode, dirname))
!= 0)
return (rc);
prevnode->leveldir = newnode;
prevnode = newnode;
currnode = newnode->subdir;
} else {
continue;
}
}
} else {
if ((rc = alloc_hiernode(&newnode,
dirname)) != 0)
return (rc);
prevnode->subdir = newnode;
prevnode = newnode;
currnode = newnode->subdir;
}
if ((rc = get_dir_from_path(dirname, &path,
sizeof (dirname))) != 0)
return (rc);
}
if (prevnode->mapent != NULL) {
char *root;
if (strcmp(mapname, "-fstab") == 0) {
syslog(LOG_ERR,
"Duplicate mounts for %s:%s in fstab",
key, me->map_mntpnt);
} else {
root = me->map_root;
while (*root == '/')
root++;
syslog(LOG_ERR,
"Duplicate submounts for %s%s in map %s, key %s",
root, me->map_mntpnt, mapname, key);
}
return (EIO);
}
prevnode->mapent = me;
me = me->map_next;
}
if (trace > 3) {
trace_prt(1, "\n\thierarchical_sort:\n");
trace_hierarchy(*rootnode, 0);
}
return (rc);
}
static int
push_options(hiernode *node, char *defaultopts, char *mapopts, int err)
{
int rc = 0;
struct mapent *me = NULL;
while (node != NULL) {
me = node->mapent;
if (me != NULL) {
me->map_err = err;
if ((rc = set_mapent_opts(me, me->map_mntopts,
defaultopts, mapopts)) != 0)
return (rc);
}
if (node->subdir != NULL) {
if (node->mapent && strcmp(node->mapent->map_fstype,
MNTTYPE_AUTOFS) == 0)
err = MAPENT_UATFS;
if ((rc = push_options(node->subdir, defaultopts,
mapopts, err)) != 0)
return (rc);
}
node = node->leveldir;
}
return (rc);
}
#define BACKFSTYPE "backfstype"
#define BACKFSTYPE_EQ "backfstype="
#define FSTYPE "fstype"
#define FSTYPE_EQ "fstype="
#define NO_OPTS ""
static int
set_mapent_opts(struct mapent *me, char *opts, char *defaultopts,
char *mapopts)
{
char entryopts[AUTOFS_MAXOPTSLEN];
char fstype[MAX_FSLEN], mounter[MAX_FSLEN];
int rc = 0;
bool_t fstype_opt = FALSE;
CHECK_STRCPY(fstype, MNTTYPE_NFS, sizeof (fstype));
if (opts == NULL) {
opts = defaultopts;
if (defaultopts == NULL) {
strcpy(mounter, fstype);
goto done;
}
}
if (*opts == '-')
opts++;
if (!get_opts(opts, entryopts, fstype, sizeof (fstype), &fstype_opt)) {
syslog(LOG_ERR, "file system type is too long");
return (EIO);
}
if (me->map_mntopts != NULL)
free(me->map_mntopts);
if ((me->map_mntopts = strdup(entryopts)) == NULL)
return (ENOMEM);
strcpy(mounter, fstype);
#ifdef MNTTYPE_CACHEFS
if (strcmp(fstype, MNTTYPE_CACHEFS) == 0) {
struct mnttab m;
char *p;
m.mnt_mntopts = entryopts;
if ((p = hasmntopt(&m, BACKFSTYPE)) != NULL) {
int len = strlen(MNTTYPE_NFS);
p += strlen(BACKFSTYPE_EQ);
if (strncmp(p, MNTTYPE_NFS, len) == 0 &&
(p[len] == '\0' || p[len] == ',')) {
CHECK_STRCPY(fstype, MNTTYPE_NFS, sizeof (fstype));
CHECK_STRCPY(mounter, MNTTYPE_CACHEFS, sizeof (mounter));
}
}
}
#endif
if (fstype_opt == TRUE &&
(strcmp(me->map_mntopts, NO_OPTS) == 0)) {
free(me->map_mntopts);
if ((rc = fstype_opts(me, opts, defaultopts,
mapopts)) != 0)
return (rc);
}
done:
if (((me->map_fstype = strdup(fstype)) == NULL) ||
((me->map_mounter = strdup(mounter)) == NULL)) {
if (me->map_fstype != NULL)
free(me->map_fstype);
syslog(LOG_ERR, "set_mapent_opts: No memory");
return (ENOMEM);
}
return (rc);
}
static int
get_opts(input, opts, fstype, fstype_size, fstype_opt)
char *input;
char *opts;
char *fstype;
size_t fstype_size;
bool_t *fstype_opt;
{
char *p, *pb;
char buf[MAXOPTSLEN];
char *placeholder;
*opts = '\0';
CHECK_STRCPY(buf, input, sizeof (buf));
pb = buf;
while ((p = strtok_r(pb, ",", &placeholder)) != NULL) {
pb = NULL;
if (strncmp(p, FSTYPE_EQ, 7) == 0) {
if (fstype_opt != NULL)
*fstype_opt = TRUE;
if (strlcpy(fstype, p + 7, fstype_size) >= fstype_size)
return (0);
} else {
if (*opts)
CHECK_STRCAT(opts, ",", MAXOPTSLEN);
CHECK_STRCAT(opts, p, MAXOPTSLEN);
}
}
return (1);
}
static int
fstype_opts(struct mapent *me, char *opts, char *defaultopts,
char *mapopts)
{
char *optstopush;
char pushopts[AUTOFS_MAXOPTSLEN];
char pushentryopts[AUTOFS_MAXOPTSLEN];
char pushfstype[MAX_FSLEN];
int err;
if (defaultopts && *defaultopts == '-')
defaultopts++;
if (strcmp(defaultopts, opts) == 0) {
if (*mapopts == '-')
mapopts++;
optstopush = mapopts;
} else
optstopush = defaultopts;
if (!get_opts(optstopush, pushentryopts, pushfstype,
sizeof (pushfstype), NULL)) {
syslog(LOG_ERR, "file system type is too long");
return (EIO);
}
CHECK_STRCPY(pushopts, optstopush, sizeof (pushopts));
#ifdef MNTTYPE_CACHEFS
if (strcmp(pushfstype, MNTTYPE_CACHEFS) == 0)
err = strldup(&me->map_mntopts, pushopts, MAXOPTSLEN);
else
#endif
err = strldup(&me->map_mntopts, pushentryopts, MAXOPTSLEN);
if (err == ENAMETOOLONG) {
syslog(LOG_ERR, "mount options are too long");
return (EIO);
}
if (err == ENOMEM) {
syslog(LOG_ERR, "fstype_opts: No memory");
return (ENOMEM);
}
return (0);
}
static int
modify_mapents(struct mapent **mapents, char *mapname,
char *mapopts, char *subdir, hiernode *rootnode,
char *key, uint_t isdirect, bool_t mount_access)
{
struct mapent *mp = NULL;
char w[MAXPATHLEN];
struct mapent *me;
int rc = 0;
struct mapent *faked_mapents = NULL;
if ((rc = set_and_fake_mapent_mntlevel(rootnode, subdir, key, mapname,
&faked_mapents, isdirect, mapopts, mount_access)) != 0)
return (rc);
me = *mapents;
while (me->map_next != NULL)
me = me->map_next;
me->map_next = faked_mapents;
me = *mapents;
while (me != NULL) {
if ((me->map_mntlevel == -1) || (me->map_err) ||
(mount_access == FALSE && me->map_mntlevel == 0)) {
if (me->map_err)
dump_mapent_err(me, key, mapname);
if (me == (*mapents)) {
*mapents = me->map_next;
if ((*mapents) == NULL) {
if (verbose)
syslog(LOG_ERR,
"modify_mapents: level error");
return (EIO);
}
me->map_next = NULL;
free_mapent(me);
me = *mapents;
} else {
mp->map_next = me->map_next;
me->map_next = NULL;
free_mapent(me);
me = mp->map_next;
}
continue;
}
if (me->map_mntlevel == 1 &&
(strcmp(me->map_fstype, MNTTYPE_AUTOFS) != 0) &&
(me->map_faked != TRUE)) {
if ((rc = convert_mapent_to_automount(me, mapname,
mapopts)) != 0)
return (rc);
}
CHECK_STRCPY(w, (me->map_mntpnt+strlen(subdir)), sizeof (w));
strcpy(me->map_mntpnt, w);
mp = me;
me = me->map_next;
}
if (trace > 3)
trace_mapents("modify_mapents:", *mapents);
return (0);
}
static int
set_and_fake_mapent_mntlevel(hiernode *rootnode, char *subdir, char *key,
char *mapname, struct mapent **faked_mapents,
uint_t isdirect, char *mapopts, bool_t mount_access)
{
char dirname[MAXFILENAMELEN];
char traversed_path[MAXPATHLEN];
char *subdir_child = subdir;
hiernode *prevnode = rootnode;
hiernode *currnode = rootnode->subdir;
int rc = 0;
traversed_path[0] = '\0';
if ((rc = get_dir_from_path(dirname, &subdir_child, sizeof (dirname)))
!= 0)
return (rc);
if (dirname[0] != '\0') {
if (strlcat(traversed_path, "/", sizeof (traversed_path)) >=
sizeof (traversed_path) ||
strlcat(traversed_path, dirname, sizeof (traversed_path)) >=
sizeof (traversed_path)) {
syslog(LOG_ERR, "set_and_fake_mapent_mntlevel: maximum path name length exceeded");
return (EIO);
}
}
prevnode = rootnode;
currnode = rootnode->subdir;
while (dirname[0] != '\0' && currnode != NULL) {
if (strcmp(currnode->dirname, dirname) == 0) {
prevnode = currnode;
currnode = currnode->subdir;
if ((rc = get_dir_from_path(dirname, &subdir_child,
sizeof (dirname))) != 0)
return (rc);
if (dirname[0] != '\0') {
if (strlcat(traversed_path, "/", sizeof (traversed_path)) >=
sizeof (traversed_path) ||
strlcat(traversed_path, dirname, sizeof (traversed_path)) >=
sizeof (traversed_path)) {
syslog(LOG_ERR,
"set_and_fake_mapent_mntlevel: maximum path name length exceeded");
return (EIO);
}
}
} else {
prevnode = currnode;
currnode = currnode->leveldir;
}
}
if (dirname[0] != '\0') {
if (verbose)
syslog(LOG_ERR,
"set_and_fake_mapent_mntlevel: subdir=%s error: map=%s",
subdir, mapname);
return (EIO);
}
if (prevnode->mapent != NULL && mount_access == TRUE) {
if (trace > 3)
trace_prt(1, " node mountpoint %s\t travpath=%s\n",
prevnode->mapent->map_mntpnt, traversed_path);
if (strlen(prevnode->mapent->map_mntpnt) <
strlen(traversed_path)) {
if (verbose)
syslog(LOG_ERR,
"set_and_fake_mapent_mntlevel: path=%s error",
traversed_path);
return (EIO);
}
if (strcmp(prevnode->mapent->map_mntpnt, traversed_path) != 0)
strcpy(prevnode->mapent->map_mntpnt, traversed_path);
prevnode->mapent->map_mntlevel = 0;
if (currnode != NULL) {
if ((rc = mark_level1_root(currnode,
traversed_path)) != 0)
return (rc);
}
} else if (currnode != NULL) {
if (trace > 3)
trace_prt(1, " No rootnode, travpath=%s\n",
traversed_path);
if ((rc = mark_and_fake_level1_noroot(currnode,
traversed_path, key, mapname, faked_mapents, isdirect,
mapopts)) != 0)
return (rc);
}
if (trace > 3) {
trace_prt(1, "\n\tset_and_fake_mapent_mntlevel\n");
trace_hierarchy(rootnode, 0);
}
return (rc);
}
static int
mark_level1_root(hiernode *node, char *traversed_path)
{
while (node) {
if (node->mapent == NULL) {
char w[MAXPATHLEN];
if (node->subdir != NULL) {
if (snprintf(w, sizeof (w), "%s/%s",
traversed_path, node->dirname) >=
(int)sizeof (w)) {
syslog(LOG_ERR, "mark_level1_root: maximum path name length exceeded");
return (EIO);
}
if (mark_level1_root(node->subdir, w) == EIO)
return (EIO);
} else {
if (verbose) {
syslog(LOG_ERR,
"mark_level1_root: hierarchy error");
}
return (EIO);
}
} else {
char w[MAXPATHLEN];
if (snprintf(w, sizeof (w), "%s/%s", traversed_path,
node->dirname) >= (int)sizeof (w)) {
syslog(LOG_ERR, "mark_level1_root: maximum path name length exceeded");
return (EIO);
}
if (trace > 3)
trace_prt(1, " node mntpnt %s\t travpath %s\n",
node->mapent->map_mntpnt, w);
if (strlen(node->mapent->map_mntpnt) < strlen(w)) {
if (verbose) {
syslog(LOG_ERR,
"mark_level1_root: path=%s error",
traversed_path);
}
return (EIO);
}
if (strcmp(node->mapent->map_mntpnt, w) != 0)
strcpy(node->mapent->map_mntpnt, w);
node->mapent->map_mntlevel = 1;
}
node = node->leveldir;
}
return (0);
}
static int
mark_and_fake_level1_noroot(hiernode *node, char *traversed_path,
char *key, char *mapname, struct mapent **faked_mapents,
uint_t isdirect, char *mapopts)
{
struct mapent *me;
int rc = 0;
char w[MAXPATHLEN];
while (node != NULL) {
me = NULL;
if (node->mapent != NULL) {
if (snprintf(w, sizeof (w), "%s/%s", traversed_path,
node->dirname) >= (int)sizeof (w)) {
syslog(LOG_ERR,
"mark_fake_level1_noroot: maximum path name length exceeded");
return (EIO);
}
if (trace > 3)
trace_prt(1, " node mntpnt=%s\t travpath=%s\n",
node->mapent->map_mntpnt, w);
if (strlen(node->mapent->map_mntpnt) < strlen(w)) {
if (verbose) {
syslog(LOG_ERR,
"mark_fake_level1_noroot:path=%s error",
traversed_path);
}
return (EIO);
}
if (strcmp(node->mapent->map_mntpnt, w) != 0)
strcpy(node->mapent->map_mntpnt, w);
node->mapent->map_mntlevel = 1;
} else {
if ((me = (struct mapent *)malloc(sizeof (*me)))
== NULL)
goto alloc_failed;
(void) memset((char *)me, 0, sizeof (*me));
if ((me->map_fs = (struct mapfs *)
malloc(sizeof (struct mapfs))) == NULL)
goto alloc_failed;
(void) memset(me->map_fs, 0, sizeof (struct mapfs));
if (isdirect)
me->map_root = strdup("");
else
me->map_root = strprefix_slash(key);
if (me->map_root == NULL)
goto alloc_failed;
if (asprintf(&me->map_mntpnt, "%s/%s", traversed_path,
node->dirname) == -1)
goto alloc_failed;
me->map_fstype = strdup(MNTTYPE_AUTOFS);
me->map_mounter = strdup(MNTTYPE_AUTOFS);
if ((rc = automount_opts(&me->map_mntopts, mapopts))
!= 0)
return (rc);
me->map_fs->mfs_dir = strdup(mapname);
me->map_mntlevel = 1;
me->map_modified = FALSE;
me->map_faked = TRUE;
if (me->map_root == NULL ||
me->map_mntpnt == NULL ||
me->map_fstype == NULL ||
me->map_mounter == NULL ||
me->map_mntopts == NULL ||
me->map_fs->mfs_dir == NULL)
goto alloc_failed;
if (*faked_mapents == NULL)
*faked_mapents = me;
else {
me->map_next = *faked_mapents;
*faked_mapents = me;
}
node->mapent = me;
}
node = node->leveldir;
}
return (rc);
alloc_failed:
syslog(LOG_ERR, "mark_and_fake_level1_noroot: out of memory");
if (me != NULL) {
if (me->map_mounter != NULL)
free(me->map_mounter);
if (me->map_fstype != NULL)
free(me->map_fstype);
if (me->map_mntpnt != NULL)
free(me->map_mntpnt);
if (me->map_root != NULL)
free(me->map_root);
if (me->map_fs != NULL) {
if (me->map_fs->mfs_dir != NULL)
free(me->map_fs->mfs_dir);
free(me->map_fs);
}
free(me);
}
free_mapent(*faked_mapents);
return (ENOMEM);
}
static int
convert_mapent_to_automount(struct mapent *me, char *mapname,
char *mapopts)
{
struct mapfs *mfs = me->map_fs;
int rc = 0;
if (mfs->mfs_host) {
free(mfs->mfs_host);
mfs->mfs_host = NULL;
}
while (me->map_fs->mfs_next != NULL) {
mfs = me->map_fs->mfs_next;
if (mfs->mfs_host)
free(mfs->mfs_host);
if (mfs->mfs_dir)
free(mfs->mfs_dir);
me->map_fs->mfs_next = mfs->mfs_next;
free((void*)mfs);
}
if (me->map_fstype)
free(me->map_fstype);
if ((me->map_fstype = strdup(MNTTYPE_AUTOFS)) == NULL)
goto alloc_failed;
if (me->map_mounter)
free(me->map_mounter);
if ((me->map_mounter = strdup(me->map_fstype)) == NULL)
goto alloc_failed;
if (me->map_fs->mfs_dir)
free(me->map_fs->mfs_dir);
if ((me->map_fs->mfs_dir = strdup(mapname)) == NULL)
goto alloc_failed;
if (me->map_mntopts)
free(me->map_mntopts);
if ((rc = automount_opts(&me->map_mntopts, mapopts)) != 0)
return (rc);
me->map_modified = TRUE;
return (rc);
alloc_failed:
syslog(LOG_ERR,
"convert_mapent_to_automount: Memory allocation failed");
return (ENOMEM);
}
static int
automount_opts(char **map_mntopts, char *mapopts)
{
char *opts;
char *opt;
size_t len;
char *placeholder;
char buf[AUTOFS_MAXOPTSLEN];
char *addopt = "direct";
len = strlen(mapopts)+ strlen(addopt)+2;
if (len > AUTOFS_MAXOPTSLEN) {
syslog(LOG_ERR,
"option string %s too long (max=%d)", mapopts,
AUTOFS_MAXOPTSLEN-8);
return (EIO);
}
if (((*map_mntopts) = ((char *)malloc(len))) == NULL) {
syslog(LOG_ERR, "automount_opts: Memory allocation failed");
return (ENOMEM);
}
memset(*map_mntopts, 0, len);
strcpy(buf, mapopts);
opts = buf;
while ((opt = strtok_r(opts, ",", &placeholder)) != NULL) {
opts = NULL;
while (isspace(*opt))
opt++;
len = strlen(opt)-1;
while (isspace(opt[len]))
opt[len--] = '\0';
if ((strcmp(opt, "indirect") == 0) ||
(strcmp(opt, "direct") == 0))
continue;
if (*map_mntopts[0] != '\0')
strcat(*map_mntopts, ",");
strcat(*map_mntopts, opt);
}
if (*map_mntopts[0] != '\0')
strcat(*map_mntopts, ",");
strcat(*map_mntopts, addopt);
return (0);
}
static int
parse_fsinfo(char *mapname, struct mapent *mapents)
{
struct mapent *me = mapents;
char *bufp;
char *bufq;
int err = 0;
while (me != NULL) {
bufp = "";
bufq = "";
if (strcmp(me->map_fstype, "nfs") == 0) {
err = parse_nfs(mapname, me, me->map_fsw,
me->map_fswq, &bufp, &bufq);
} else {
err = parse_special(me, me->map_fsw, me->map_fswq,
&bufp, &bufq, MAXPATHLEN);
}
if (err != 0 || *me->map_fsw != '\0' ||
*me->map_fswq != '\0') {
if (verbose)
syslog(LOG_ERR,
"parse_fsinfo: mount location error %s",
me->map_fsw);
return (EIO);
}
me = me->map_next;
}
if (trace > 3) {
trace_mapents("parse_fsinfo:", mapents);
}
return (0);
}
static int
parse_nfs(mapname, me, fsw, fswq, lp, lq)
struct mapent *me;
char *mapname, *fsw, *fswq, **lp, **lq;
{
struct mapfs *mfs, **mfsp;
char *wlp, *wlq;
char *hl, hostlist[1024], *hlq, hostlistq[1024];
char hostname_and_penalty[MXHOSTNAMELEN+5];
char *hn, *hnq, hostname[MXHOSTNAMELEN+1];
char dirname[MAXPATHLEN+1], subdir[MAXPATHLEN+1];
char qbuff[MAXPATHLEN+1], qbuff1[MAXPATHLEN+1];
char pbuff[10], pbuffq[10];
int penalty;
char w[MAXPATHLEN];
char wq[MAXPATHLEN];
int host_cnt;
mfsp = &me->map_fs;
*mfsp = NULL;
*lp = fsw;
*lq = fswq;
if (getword(w, wq, lp, lq, ' ', MAXPATHLEN) == -1)
return (EIO);
while (*w && *w != '/') {
bool_t maybe_url;
maybe_url = TRUE;
wlp = w; wlq = wq;
if (getword(hostlist, hostlistq, &wlp, &wlq, ':',
sizeof (hostlist)) == -1)
return (EIO);
if (!*hostlist) {
syslog(LOG_ERR,
"parse_nfs: host list empty in map %s \"%s\"",
mapname, w);
goto bad_entry;
}
if (strcmp(hostlist, "nfs") != 0)
maybe_url = FALSE;
if (getword(dirname, qbuff, &wlp, &wlq, ':',
sizeof (dirname)) == -1)
return (EIO);
if (*dirname == '\0') {
syslog(LOG_ERR,
"parse_nfs: directory name empty in map %s \"%s\"",
mapname, w);
goto bad_entry;
}
if (maybe_url == TRUE && strncmp(dirname, "//", 2) != 0)
maybe_url = FALSE;
if (maybe_url == FALSE)
*subdir = '/';
else
*subdir = ':';
*qbuff = ' ';
if (getword(subdir+1, qbuff+1, &wlp, &wlq, ':',
sizeof (subdir)) == -1)
return (EIO);
if (*(subdir+1)) {
if (strlcat(dirname, subdir, sizeof (dirname)) >=
sizeof (dirname)) {
syslog(LOG_ERR, "directory+subdirectory is longer than MAXPATHLEN");
return (EIO);
}
}
hl = hostlist; hlq = hostlistq;
host_cnt = 0;
for (;;) {
if (getword(hostname_and_penalty, qbuff, &hl, &hlq, ',',
sizeof (hostname_and_penalty)) == -1)
return (EIO);
if (!*hostname_and_penalty)
break;
host_cnt++;
if (host_cnt > 1)
maybe_url = FALSE;
hn = hostname_and_penalty;
hnq = qbuff;
if (getword(hostname, qbuff1, &hn, &hnq, '(',
sizeof (hostname)) == -1)
return (EIO);
if (hostname[0] == '\0') {
syslog(LOG_ERR,
"parse_nfs: host name empty in host list in map %s \"%s\"",
mapname, w);
goto bad_entry;
}
if (strcmp(hostname, hostname_and_penalty) == 0) {
penalty = 0;
} else {
maybe_url = FALSE;
hn++; hnq++;
if (getword(pbuff, pbuffq, &hn, &hnq, ')',
sizeof (pbuff)) == -1)
return (EIO);
if (!*pbuff)
penalty = 0;
else
penalty = atoi(pbuff);
}
mfs = (struct mapfs *)malloc(sizeof (*mfs));
if (mfs == NULL) {
syslog(LOG_ERR,
"parse_nfs: Memory allocation failed");
return (EIO);
}
(void) memset(mfs, 0, sizeof (*mfs));
*mfsp = mfs;
mfsp = &mfs->mfs_next;
if (maybe_url == TRUE) {
char *host;
char *path;
char *sport;
host = dirname+2;
path = strchr(host, '/');
if (path == NULL) {
syslog(LOG_ERR,
"parse_nfs: illegal nfs url syntax: %s",
host);
return (EIO);
}
*path = '\0';
sport = strchr(host, ':');
if (sport != NULL && sport < path) {
*sport = '\0';
mfs->mfs_port = atoi(sport+1);
if (mfs->mfs_port > USHRT_MAX) {
syslog(LOG_ERR,
"parse_nfs: invalid "
"port number (%d) in "
"NFS URL",
mfs->mfs_port);
return (EIO);
}
}
path++;
if (*path == '\0')
path = ".";
mfs->mfs_flags |= MFS_URL;
mfs->mfs_host = strdup(host);
mfs->mfs_dir = strdup(path);
} else {
mfs->mfs_host = strdup(hostname);
mfs->mfs_dir = strdup(dirname);
}
mfs->mfs_penalty = penalty;
if (mfs->mfs_host == NULL || mfs->mfs_dir == NULL) {
syslog(LOG_ERR,
"parse_nfs: Memory allocation failed");
return (EIO);
}
}
if (host_cnt == 0) {
syslog(LOG_ERR,
"parse_nfs: invalid host specified - bad entry "
"in map %s \"%s\"",
mapname, w);
return (EIO);
}
if (getword(w, wq, lp, lq, ' ', MAXPATHLEN) == -1)
return (EIO);
}
strcpy(fsw, w);
strcpy(fswq, wq);
return (0);
bad_entry:
return (EIO);
}
static int
parse_special(me, w, wq, lp, lq, wsize)
struct mapent *me;
char *w, *wq, **lp, **lq;
int wsize;
{
char mountname[MAXPATHLEN + 1], qbuf[MAXPATHLEN + 1];
char *wlp, *wlq;
struct mapfs *mfs;
wlp = w;
wlq = wq;
if (getword(mountname, qbuf, &wlp, &wlq, ' ', sizeof (mountname)) == -1)
return (EIO);
if (mountname[0] == '\0')
return (EIO);
mfs = (struct mapfs *)malloc(sizeof (struct mapfs));
if (mfs == NULL) {
syslog(LOG_ERR, "parse_special: Memory allocation failed");
return (EIO);
}
(void) memset(mfs, 0, sizeof (*mfs));
mfs->mfs_dir = strdup(&mountname[mountname[0] == ':']);
if (mfs->mfs_dir == NULL) {
syslog(LOG_ERR, "parse_special: Memory allocation failed");
return (EIO);
}
me->map_fs = mfs;
if (getword(w, wq, lp, lq, ' ', wsize) == -1)
return (EIO);
return (0);
}
static int
get_dir_from_path(char *dir, char **path, int dirsz)
{
char *tmp = dir;
int count = dirsz;
if (dirsz <= 0) {
if (verbose)
syslog(LOG_ERR,
"get_dir_from_path: invalid directory size %d", dirsz);
return (EIO);
}
while (**path == '/')
(*path)++;
while ((**path) && ((**path) != '/')) {
if (--count <= 0) {
*tmp = '\0';
syslog(LOG_ERR,
"get_dir_from_path: max pathlength exceeded %d", dirsz);
return (EIO);
}
*dir++ = *(*path)++;
}
*dir = '\0';
while (**path == '/')
(*path)++;
return (0);
}
static int
alloc_hiernode(hiernode **newnode, char *dirname)
{
if ((*newnode = (hiernode *)malloc(sizeof (hiernode))) == NULL) {
syslog(LOG_ERR, "alloc_hiernode: Memory allocation failed");
return (ENOMEM);
}
memset(((char *)*newnode), 0, sizeof (hiernode));
CHECK_STRCPY((*newnode)->dirname, dirname, sizeof ((*newnode)->dirname));
return (0);
}
static void
free_hiernode(hiernode *node)
{
hiernode *currnode = node;
hiernode *prevnode = NULL;
while (currnode != NULL) {
if (currnode->subdir != NULL)
free_hiernode(currnode->subdir);
prevnode = currnode;
currnode = currnode->leveldir;
free((void*)prevnode);
}
}
void
free_mapent(me)
struct mapent *me;
{
struct mapfs *mfs;
struct mapent *m;
while (me) {
while (me->map_fs) {
mfs = me->map_fs;
if (mfs->mfs_host)
free(mfs->mfs_host);
if (mfs->mfs_dir)
free(mfs->mfs_dir);
if (mfs->mfs_args)
free(mfs->mfs_args);
me->map_fs = mfs->mfs_next;
free((char *)mfs);
}
if (me->map_root)
free(me->map_root);
if (me->map_mntpnt)
free(me->map_mntpnt);
if (me->map_mntopts)
free(me->map_mntopts);
if (me->map_fstype)
free(me->map_fstype);
if (me->map_mounter)
free(me->map_mounter);
if (me->map_fsw)
free(me->map_fsw);
if (me->map_fswq)
free(me->map_fswq);
m = me;
me = me->map_next;
free((char *)m);
}
}
static void
trace_mapents(char *s, struct mapent *mapents)
{
struct mapfs *mfs;
struct mapent *me;
trace_prt(1, "\n\t%s\n", s);
for (me = mapents; me; me = me->map_next) {
trace_prt(1, " (%s,%s)\t %s%s -%s\n",
me->map_fstype ? me->map_fstype : "",
me->map_mounter ? me->map_mounter : "",
me->map_root ? me->map_root : "",
me->map_mntpnt ? me->map_mntpnt : "",
me->map_mntopts ? me->map_mntopts : "");
for (mfs = me->map_fs; mfs; mfs = mfs->mfs_next)
trace_prt(0, "\t\t%s:%s\n",
mfs->mfs_host ? mfs->mfs_host: "",
mfs->mfs_dir ? mfs->mfs_dir : "");
trace_prt(1, "\tme->map_fsw=%s\n",
me->map_fsw ? me->map_fsw:"");
trace_prt(1, "\tme->map_fswq=%s\n",
me->map_fswq ? me->map_fswq:"");
trace_prt(1, "\t mntlevel=%d\t%s\t%s err=%d\n",
me->map_mntlevel,
me->map_modified ? "modify=TRUE":"modify=FALSE",
me->map_faked ? "faked=TRUE":"faked=FALSE",
me->map_err);
}
}
static void
trace_hierarchy(hiernode *node, int nodelevel)
{
hiernode *currnode = node;
int i;
while (currnode != NULL) {
if (currnode->subdir != NULL) {
for (i = 0; i < nodelevel; i++)
trace_prt(0, "\t");
trace_prt(0, "\t(%s, ",
currnode->dirname ? currnode->dirname :"");
if (currnode->mapent) {
trace_prt(0, "%d, %s)\n",
currnode->mapent->map_mntlevel,
currnode->mapent->map_mntopts ?
currnode->mapent->map_mntopts:"");
}
else
trace_prt(0, " ,)\n");
nodelevel++;
trace_hierarchy(currnode->subdir, nodelevel);
} else {
for (i = 0; i < nodelevel; i++)
trace_prt(0, "\t");
trace_prt(0, "\t(%s, ",
currnode->dirname ? currnode->dirname :"");
if (currnode->mapent) {
trace_prt(0, "%d, %s)\n",
currnode->mapent->map_mntlevel,
currnode->mapent->map_mntopts ?
currnode->mapent->map_mntopts:"");
}
else
trace_prt(0, ", )\n");
}
currnode = currnode->leveldir;
}
}
static const struct mntopt mopts_vers[] = {
MOPT_VERS,
{ NULL, 0, 0, 0 }
};
static int
clnt_stat_to_errno(enum clnt_stat clnt_stat)
{
switch (clnt_stat) {
case RPC_UNKNOWNHOST:
return (ENOENT);
case RPC_TIMEDOUT:
return (ETIMEDOUT);
default:
return (EIO);
}
}
struct mapent *
do_mapent_hosts(mapopts, host, isdirect, err)
char *mapopts, *host;
uint_t isdirect;
int *err;
{
CLIENT *cl;
struct mapent *me, *ms = NULL, *mp;
struct mapfs *mfs;
struct exportnode *ex = NULL;
struct exportnode *exlist = NULL, *texlist, **texp, *exnext;
struct timeval timeout;
enum clnt_stat clnt_stat;
char entryopts[MAXOPTSLEN];
char fstype[MAX_FSLEN], mounter[MAX_FSLEN];
size_t exlen;
int duplicate;
mntoptparse_t mop;
int flags;
int altflags;
long optval;
rpcvers_t nfsvers;
rpcvers_t vers, versmin;
int retries, delay;
if (trace > 1)
trace_prt(1, " do_mapent_hosts: host %s\n", host);
if (host[0] == '.') {
*err = ENOENT;
return ((struct mapent *)NULL);
}
#ifdef HAVE_LOFS
if (self_check(host)) {
ms = (struct mapent *)malloc(sizeof (*ms));
if (ms == NULL)
goto alloc_failed;
(void) memset((char *)ms, 0, sizeof (*ms));
CHECK_STRCPY(fstype, MNTTYPE_NFS, sizeof (fstype));
if (!get_opts(mapopts, entryopts, fstype, sizeof (fstype), NULL))
goto fstype_too_long;
ms->map_mntopts = strdup(entryopts);
if (ms->map_mntopts == NULL)
goto alloc_failed;
ms->map_mounter = strdup(fstype);
if (ms->map_mounter == NULL)
goto alloc_failed;
ms->map_fstype = strdup(MNTTYPE_NFS);
if (ms->map_fstype == NULL)
goto alloc_failed;
if (isdirect)
ms->map_root = strdup("");
else
ms->map_root = strprefix_slash(host);
if (ms->map_root == NULL)
goto alloc_failed;
ms->map_mntpnt = strdup("");
if (ms->map_mntpnt == NULL)
goto alloc_failed;
mfs = (struct mapfs *)malloc(sizeof (*mfs));
if (mfs == NULL)
goto alloc_failed;
(void) memset((char *)mfs, 0, sizeof (*mfs));
ms->map_fs = mfs;
mfs->mfs_host = strdup(host);
if (mfs->mfs_host == NULL)
goto alloc_failed;
mfs->mfs_dir = strdup("/");
if (mfs->mfs_dir == NULL)
goto alloc_failed;
ms->map_mntlevel = -1;
ms->map_modified = FALSE;
ms->map_faked = FALSE;
if (trace > 1)
trace_prt(1,
" do_mapent_hosts: self-host %s OK\n", host);
*err = 0;
return (ms);
}
#endif
flags = altflags = 0;
getmnt_silent = 1;
mop = getmntopts(mapopts, mopts_vers, &flags, &altflags);
if (mop == NULL) {
syslog(LOG_ERR, "Couldn't parse mount options \"%s\" for %s: %m",
mapopts, host);
*err = EIO;
return ((struct mapent *)NULL);
}
if (altflags & NFS_MNT_VERS) {
optval = getmntoptnum(mop, "vers");
if (optval == -1 || optval > (long)UINT_MAX) {
syslog(LOG_ERR, "Invalid NFS version number for %s", host);
freemntopts(mop);
*err = EIO;
return ((struct mapent *)NULL);
}
nfsvers = (rpcvers_t)optval;
} else
nfsvers = 0;
freemntopts(mop);
if (set_versrange(nfsvers, &vers, &versmin) != 0) {
syslog(LOG_ERR, "Incorrect NFS version specified for %s", host);
*err = EIO;
return ((struct mapent *)NULL);
}
clnt_stat = pingnfs(host, &vers, versmin, 0, NULL, NULL);
if (clnt_stat != RPC_SUCCESS) {
*err = clnt_stat_to_errno(clnt_stat);
return ((struct mapent *)NULL);
}
retries = get_retry(mapopts);
delay = INITDELAY;
retry:
cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "tcp");
if (cl == NULL) {
cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "udp");
if (cl == NULL) {
syslog(LOG_ERR,
"do_mapent_hosts: %s %s", host, clnt_spcreateerror(""));
*err = clnt_stat_to_errno(rpc_createerr.cf_stat);
return ((struct mapent *)NULL);
}
}
#ifdef MALLOC_DEBUG
add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
__FILE__, __LINE__);
#endif
timeout.tv_usec = 0;
timeout.tv_sec = 25;
if ((clnt_stat = clnt_call(cl, MOUNTPROC_EXPORT, (xdrproc_t)xdr_void, 0,
(xdrproc_t)xdr_exports, (caddr_t)&ex, timeout)) != RPC_SUCCESS) {
if (retries-- > 0) {
clnt_destroy(cl);
DO_DELAY(delay);
goto retry;
}
syslog(LOG_ERR,
"do_mapent_hosts: %s: export list: %s",
host, clnt_sperrno(clnt_stat));
#ifdef MALLOC_DEBUG
drop_alloc("CLNT_HANDLE", cl, __FILE__, __LINE__);
drop_alloc("AUTH_HANDLE", cl->cl_auth,
__FILE__, __LINE__);
#endif
clnt_destroy(cl);
*err = clnt_stat_to_errno(clnt_stat);
return ((struct mapent *)NULL);
}
#ifdef MALLOC_DEBUG
drop_alloc("CLNT_HANDLE", cl, __FILE__, __LINE__);
drop_alloc("AUTH_HANDLE", cl->cl_auth,
__FILE__, __LINE__);
#endif
clnt_destroy(cl);
if (ex == NULL) {
if (trace > 1)
trace_prt(1, " getmapent_hosts: null export list\n");
*err = 0;
return ((struct mapent *)NULL);
}
exlist = ex;
texlist = NULL;
#ifdef lint
exnext = NULL;
#endif
for (; ex; ex = exnext) {
exnext = ex->ex_next;
exlen = strlen(ex->ex_dir);
duplicate = 0;
for (texp = &texlist; *texp; texp = &((*texp)->ex_next)) {
if (exlen < strlen((*texp)->ex_dir))
break;
duplicate = (strcmp(ex->ex_dir, (*texp)->ex_dir) == 0);
if (duplicate) {
freeex_ent(ex);
break;
}
}
if (!duplicate) {
ex->ex_next = *texp;
*texp = ex;
}
}
exlist = texlist;
CHECK_STRCPY(fstype, MNTTYPE_NFS, sizeof (fstype));
if (!get_opts(mapopts, entryopts, fstype, sizeof (fstype), NULL))
goto fstype_too_long;
(void) strcpy(mounter, fstype);
#ifdef MNTTYPE_CACHEFS
if (strcmp(fstype, MNTTYPE_CACHEFS) == 0) {
struct mnttab m;
char *p;
m.mnt_mntopts = entryopts;
if ((p = hasmntopt(&m, "backfstype")) != NULL) {
int len = strlen(MNTTYPE_NFS);
p += 11;
if (strncmp(p, MNTTYPE_NFS, len) == 0 &&
(p[len] == '\0' || p[len] == ',')) {
CHECK_STRCPY(fstype, MNTTYPE_NFS, sizeof (fstype));
CHECK_STRCPY(mounter, MNTTYPE_CACHEFS, sizeof (mounter));
}
}
}
#endif
ms = NULL;
me = NULL;
for (ex = exlist; ex; ex = ex->ex_next) {
mp = me;
me = (struct mapent *)malloc(sizeof (*me));
if (me == NULL)
goto alloc_failed;
(void) memset((char *)me, 0, sizeof (*me));
if (ms == NULL)
ms = me;
else
mp->map_next = me;
if (isdirect)
me->map_root = strdup("");
else
me->map_root = strprefix_slash(host);
if (me->map_root == NULL)
goto alloc_failed;
if (strcmp(ex->ex_dir, "/") != 0) {
if (*(ex->ex_dir) != '/')
me->map_mntpnt = strprefix_slash(ex->ex_dir);
else
me->map_mntpnt = strdup(ex->ex_dir);
} else
me->map_mntpnt = strdup("");
if (me->map_mntpnt == NULL)
goto alloc_failed;
me->map_fstype = strdup(fstype);
if (me->map_fstype == NULL)
goto alloc_failed;
me->map_mounter = strdup(mounter);
if (me->map_mounter == NULL)
goto alloc_failed;
me->map_mntopts = strdup(entryopts);
if (me->map_mntopts == NULL)
goto alloc_failed;
mfs = (struct mapfs *)malloc(sizeof (*mfs));
if (mfs == NULL)
goto alloc_failed;
(void) memset((char *)mfs, 0, sizeof (*mfs));
me->map_fs = mfs;
mfs->mfs_host = strdup(host);
if (mfs->mfs_host == NULL)
goto alloc_failed;
mfs->mfs_dir = strdup(ex->ex_dir);
if (mfs->mfs_dir == NULL)
goto alloc_failed;
me->map_mntlevel = -1;
me->map_modified = FALSE;
me->map_faked = FALSE;
}
freeex(exlist);
if (trace > 1)
trace_prt(1, " do_mapent_hosts: host %s OK\n", host);
*err = 0;
return (ms);
alloc_failed:
syslog(LOG_ERR, "do_mapent_hosts: Memory allocation failed");
free_mapent(ms);
freeex(exlist);
*err = ENOMEM;
return ((struct mapent *)NULL);
fstype_too_long:
syslog(LOG_ERR, "do_mapent_hosts: File system type is too long");
free_mapent(ms);
freeex(exlist);
*err = EIO;
return ((struct mapent *)NULL);
}
static void
freeex_ent(ex)
struct exportnode *ex;
{
struct groupnode *group, *tmpgroups;
free(ex->ex_dir);
group = ex->ex_groups;
while (group) {
free(group->gr_name);
tmpgroups = group->gr_next;
free((char *)group);
group = tmpgroups;
}
free((char *)ex);
}
static void
freeex(ex)
struct exportnode *ex;
{
struct exportnode *tmpex;
while (ex) {
tmpex = ex->ex_next;
freeex_ent(ex);
ex = tmpex;
}
}
struct create_mapent_args {
uint_t isdirect;
char *host;
struct mapent *ms;
struct mapent *me;
};
static int
create_mapent(struct fstabnode *fst, void *arg)
{
struct create_mapent_args *args = arg;
struct mapent *me, *mp;
struct mapfs *mfs;
mp = args->me;
me = (struct mapent *)malloc(sizeof (*me));
if (me == NULL)
goto alloc_failed;
(void) memset((char *)me, 0, sizeof (*me));
args->me = me;
if (args->isdirect)
me->map_root = strdup("");
else
me->map_root = strprefix_slash(args->host);
if (me->map_root == NULL)
goto alloc_failed;
if (strcmp(fst->fst_dir, "/") == 0)
me->map_mntpnt = strdup("");
else {
if (*(fst->fst_dir) != '/')
me->map_mntpnt = strprefix_slash(fst->fst_dir);
else
me->map_mntpnt = strdup(fst->fst_dir);
}
if (me->map_mntpnt == NULL)
goto alloc_failed;
me->map_fstype = strdup(fst->fst_vfstype);
if (me->map_fstype == NULL)
goto alloc_failed;
me->map_mounter = strdup(fst->fst_vfstype);
if (me->map_mounter == NULL)
goto alloc_failed;
me->map_mntopts = strdup(fst->fst_mntops);
if (me->map_mntopts == NULL)
goto alloc_failed;
mfs = (struct mapfs *)malloc(sizeof (*mfs));
if (mfs == NULL)
goto alloc_failed;
(void) memset((char *)mfs, 0, sizeof (*mfs));
me->map_fs = mfs;
mfs->mfs_host = strdup(args->host);
if (mfs->mfs_host == NULL)
goto alloc_failed;
if (strcmp(fst->fst_vfstype, "url") == 0) {
mfs->mfs_dir = strdup(fst->fst_url);
} else
mfs->mfs_dir = strdup(fst->fst_dir);
if (mfs->mfs_dir == NULL)
goto alloc_failed;
me->map_mntlevel = -1;
me->map_modified = FALSE;
me->map_faked = FALSE;
if (args->ms == NULL)
args->ms = me;
else
mp->map_next = me;
return (0);
alloc_failed:
free_mapent(me);
return (ENOMEM);
}
struct mapent *
do_mapent_fstab(mapopts, host, isdirect, err)
char *mapopts, *host;
uint_t isdirect;
int *err;
{
struct mapent *ms;
struct mapfs *mfs;
char entryopts[MAXOPTSLEN];
char fstype[MAX_FSLEN];
struct create_mapent_args args;
if (trace > 1)
trace_prt(1, " do_mapent_fstab: host %s\n", host);
if (host_is_us(host, strlen(host))) {
ms = (struct mapent *)malloc(sizeof (*ms));
if (ms == NULL)
goto alloc_failed;
(void) memset((char *)ms, 0, sizeof (*ms));
CHECK_STRCPY(fstype, MNTTYPE_NFS, sizeof (fstype));
if (!get_opts(mapopts, entryopts, fstype, sizeof (fstype), NULL))
goto fstype_too_long;
ms->map_mntopts = strdup(entryopts);
if (ms->map_mntopts == NULL)
goto alloc_failed;
ms->map_mounter = strdup(fstype);
if (ms->map_mounter == NULL)
goto alloc_failed;
ms->map_fstype = strdup(MNTTYPE_NFS);
if (ms->map_fstype == NULL)
goto alloc_failed;
if (isdirect)
ms->map_root = strdup("");
else
ms->map_root = strprefix_slash(host);
if (ms->map_root == NULL)
goto alloc_failed;
ms->map_mntpnt = strdup("");
if (ms->map_mntpnt == NULL)
goto alloc_failed;
mfs = (struct mapfs *)malloc(sizeof (*mfs));
if (mfs == NULL)
goto alloc_failed;
(void) memset((char *)mfs, 0, sizeof (*mfs));
ms->map_fs = mfs;
mfs->mfs_host = strdup(host);
if (mfs->mfs_host == NULL)
goto alloc_failed;
mfs->mfs_dir = strdup("/");
if (mfs->mfs_dir == NULL)
goto alloc_failed;
ms->map_mntlevel = -1;
ms->map_modified = FALSE;
ms->map_faked = FALSE;
if (trace > 1)
trace_prt(1,
" do_mapent_fstab: self-host %s OK\n", host);
*err = 0;
return (ms);
}
args.isdirect = isdirect;
args.host = host;
args.ms = NULL;
args.me = NULL;
*err = fstab_process_host(host, create_mapent, &args);
if (*err != 0) {
if (*err == -1)
*err = ENOENT;
else
syslog(LOG_ERR, "do_mapent_fstab: Memory allocation failed");
return ((struct mapent *)NULL);
}
if (trace > 1)
trace_prt(1, " do_mapent_fstab: host %s OK\n", host);
return (args.ms);
alloc_failed:
syslog(LOG_ERR, "do_mapent_fstab: Memory allocation failed");
free_mapent(ms);
*err = ENOMEM;
return ((struct mapent *)NULL);
fstype_too_long:
syslog(LOG_ERR, "do_mapent_hosts: File system type is too long");
free_mapent(ms);
*err = EIO;
return ((struct mapent *)NULL);
}
struct mapent *
do_mapent_static(key, isdirect, err)
char *key;
uint_t isdirect;
int *err;
{
struct staticmap *static_ent;
struct mapent *me;
struct mapfs *mfs;
static_ent = get_staticmap_entry(key);
if (static_ent == NULL) {
*err = isdirect ? 0 : ENOENT;
return (NULL);
}
if ((me = (struct mapent *)malloc(sizeof (*me))) == NULL)
goto alloc_failed;
memset((char *)me, 0, sizeof (*me));
if (isdirect)
me->map_root = strdup("");
else
me->map_root = strprefix_slash(key);
if (me->map_root == NULL)
goto alloc_failed;
me->map_mntpnt = strdup("");
if (me->map_mntpnt == NULL)
goto alloc_failed;
me->map_fstype = strdup(static_ent->vfstype);
if (me->map_fstype == NULL)
goto alloc_failed;
me->map_mounter = strdup(static_ent->vfstype);
if (me->map_mounter == NULL)
goto alloc_failed;
me->map_mntopts = strdup(static_ent->mntops);
if (me->map_mntopts == NULL)
goto alloc_failed;
me->map_fsw = strdup("");
if (me->map_fsw == NULL)
goto alloc_failed;
me->map_fswq = strdup("");
if (me->map_fswq == NULL)
goto alloc_failed;
me->map_mntlevel = -1;
me->map_modified = FALSE;
me->map_faked = FALSE;
me->map_err = MAPENT_NOERR;
mfs = (struct mapfs *)malloc(sizeof (*mfs));
if (mfs == NULL)
goto alloc_failed;
(void) memset((char *)mfs, 0, sizeof (*mfs));
me->map_fs = mfs;
mfs->mfs_host = strdup(static_ent->host);
if (mfs->mfs_host == NULL)
goto alloc_failed;
mfs->mfs_dir = strdup(static_ent->spec);
if (mfs->mfs_dir == NULL)
goto alloc_failed;
me->map_next = NULL;
release_staticmap_entry(static_ent);
*err = 0;
return (me);
alloc_failed:
release_staticmap_entry(static_ent);
syslog(LOG_ERR, "do_mapent_static: Memory allocation failed");
free_mapent(me);
*err = ENOMEM;
return (NULL);
}
static const char uatfs_err[] = "submount under fstype=autofs not supported";
static void dump_mapent_err(struct mapent *me, char *key, char *mapname)
{
switch (me->map_err) {
case MAPENT_NOERR:
if (verbose)
syslog(LOG_ERR,
"map=%s key=%s mntpnt=%s: no error");
break;
case MAPENT_UATFS:
syslog(LOG_ERR,
"mountpoint %s in map %s key %s not mounted: %s",
me->map_mntpnt, mapname, key, uatfs_err);
break;
default:
if (verbose)
syslog(LOG_ERR,
"map=%s key=%s mntpnt=%s: unknown mapentry error");
}
}