Blame SOURCES/autofs-5.0.9-amd-lookup-add-parse_amd-c.patch

304803
autofs-5.0.9 - amd lookup add parse_amd.c
304803
304803
From: Ian Kent <raven@themaw.net>
304803
304803
Add a yacc parser and an autofs parser module for amd format
304803
maps.
304803
---
304803
 include/automount.h |    1 
304803
 include/mounts.h    |    6 
304803
 include/parse_amd.h |   54 ++
304803
 lib/mounts.c        |   81 +++
304803
 modules/Makefile    |   21 -
304803
 modules/amd_parse.y |  511 ++++++++++++++++++++++++
304803
 modules/amd_tok.l   |  316 +++++++++++++++
304803
 modules/parse_amd.c | 1081 ++++++++++++++++++++++++++++++++++++++++++++++++++++
304803
 8 files changed, 2069 insertions(+), 2 deletions(-)
304803
 create mode 100644 include/parse_amd.h
304803
 create mode 100644 modules/amd_parse.y
304803
 create mode 100644 modules/amd_tok.l
304803
 create mode 100644 modules/parse_amd.c
304803
304803
--- autofs-5.0.7.orig/include/automount.h
304803
+++ autofs-5.0.7/include/automount.h
304803
@@ -34,6 +34,7 @@
304803
 #include "parse_subs.h"
304803
 #include "mounts.h"
304803
 #include "dev-ioctl-lib.h"
304803
+#include "parse_amd.h"
304803
 
304803
 #ifdef WITH_DMALLOC
304803
 #include <dmalloc.h>
304803
--- autofs-5.0.7.orig/include/mounts.h
304803
+++ autofs-5.0.7/include/mounts.h
304803
@@ -85,10 +85,16 @@ unsigned int linux_version_code(void);
304803
 int check_nfs_mount_version(struct nfs_mount_vers *, struct nfs_mount_vers *);
304803
 extern unsigned int nfs_mount_uses_string_options;
304803
 
304803
+struct amd_entry;
304803
+
304803
 struct substvar *addstdenv(struct substvar *sv);
304803
 struct substvar *removestdenv(struct substvar *sv);
304803
 void add_std_amd_vars(struct substvar *sv);
304803
 void remove_std_amd_vars(void);
304803
+struct amd_entry *new_amd_entry(const struct substvar *sv);
304803
+void clear_amd_entry(struct amd_entry *entry);
304803
+void free_amd_entry(struct amd_entry *entry);
304803
+void free_amd_entry_list(struct list_head *entries);
304803
 
304803
 unsigned int query_kproto_ver(void);
304803
 unsigned int get_kver_major(void);
304803
--- /dev/null
304803
+++ autofs-5.0.7/include/parse_amd.h
304803
@@ -0,0 +1,54 @@
304803
+/* ----------------------------------------------------------------------- *
304803
+ *
304803
+ *  Copyright 2004-2006 Ian Kent <raven@themaw.net>
304803
+ *  Copyright 2013 Red Hat, Inc.
304803
+ *  All rights reserved.
304803
+ *
304803
+ *  This program is free software; you can redistribute it and/or modify
304803
+ *  it under the terms of the GNU General Public License as published by
304803
+ *  the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
304803
+ *  USA; either version 2 of the License, or (at your option) any later
304803
+ *  version; incorporated herein by reference.
304803
+ *
304803
+ * ----------------------------------------------------------------------- */
304803
+
304803
+#ifndef PARSE_AMD_H
304803
+#define PARSE_AMD_H
304803
+
304803
+#define AMD_MOUNT_TYPE_NONE	0x00000000
304803
+#define AMD_MOUNT_TYPE_AUTO	0x00000001
304803
+#define AMD_MOUNT_TYPE_NFS	0x00000002
304803
+#define AMD_MOUNT_TYPE_LINK	0x00000004
304803
+#define AMD_MOUNT_TYPE_HOST	0x00000008
304803
+#define AMD_MOUNT_TYPE_MASK	0x0000ffff
304803
+
304803
+#define AMD_ENTRY_CUT		0x00010000
304803
+#define AMD_ENTRY_MASK		0x00ff0000
304803
+
304803
+#define AMD_DEFAULTS_MERGE	0x01000000
304803
+#define AMD_DEFAULTS_RESET	0x02000000
304803
+#define AMD_DEFAULTS_MASK	0xff000000
304803
+
304803
+struct amd_entry {
304803
+	char *path;
304803
+	unsigned long flags;
304803
+	char *type;
304803
+	char *map_type;
304803
+	char *pref;
304803
+	char *fs;
304803
+	char *rhost;
304803
+	char *rfs;
304803
+	char *opts;
304803
+	char *addopts;
304803
+	char *remopts;
304803
+	char *sublink;
304803
+	struct selector *selector;
304803
+	struct list_head list;
304803
+	struct list_head entries;
304803
+	struct list_head ext_mount;
304803
+};
304803
+
304803
+int amd_parse_list(struct autofs_point *,
304803
+		   const char *, struct list_head *, struct substvar **);
304803
+
304803
+#endif
304803
--- autofs-5.0.7.orig/lib/mounts.c
304803
+++ autofs-5.0.7/lib/mounts.c
304803
@@ -442,6 +442,87 @@ void remove_std_amd_vars(void)
304803
 	return;
304803
  }
304803
 
304803
+struct amd_entry *new_amd_entry(const struct substvar *sv)
304803
+{
304803
+	struct amd_entry *new;
304803
+	const struct substvar *v;
304803
+	char *path;
304803
+
304803
+	v = macro_findvar(sv, "path", 4);
304803
+	if (!v)
304803
+		return NULL;
304803
+
304803
+	path = strdup(v->val);
304803
+	if (!path)
304803
+		return NULL;
304803
+
304803
+	new = malloc(sizeof(struct amd_entry));
304803
+	if (!new) {
304803
+		free(path);
304803
+		return NULL;
304803
+	}
304803
+
304803
+	memset(new, 0, sizeof(*new));
304803
+	new->path = path;
304803
+	INIT_LIST_HEAD(&new->list);
304803
+	INIT_LIST_HEAD(&new->entries);
304803
+	INIT_LIST_HEAD(&new->ext_mount);
304803
+
304803
+	return new;
304803
+}
304803
+
304803
+void clear_amd_entry(struct amd_entry *entry)
304803
+{
304803
+	if (!entry)
304803
+		return;
304803
+	if (entry->path)
304803
+		free(entry->path);
304803
+	if (entry->map_type)
304803
+		free(entry->map_type);
304803
+	if (entry->pref)
304803
+		free(entry->pref);
304803
+	if (entry->fs)
304803
+		free(entry->fs);
304803
+	if (entry->rhost)
304803
+		free(entry->rhost);
304803
+	if (entry->rfs)
304803
+		free(entry->rfs);
304803
+	if (entry->opts)
304803
+		free(entry->opts);
304803
+	if (entry->addopts)
304803
+		free(entry->addopts);
304803
+	if (entry->remopts)
304803
+		free(entry->remopts);
304803
+	if (entry->sublink)
304803
+		free(entry->sublink);
304803
+	if (entry->selector)
304803
+		free_selector(entry->selector);
304803
+	return;
304803
+}
304803
+
304803
+void free_amd_entry(struct amd_entry *entry)
304803
+{
304803
+	clear_amd_entry(entry);
304803
+	free(entry);
304803
+	return;
304803
+}
304803
+
304803
+void free_amd_entry_list(struct list_head *entries)
304803
+{
304803
+	if (!list_empty(entries)) {
304803
+		struct list_head *head = entries;
304803
+		struct amd_entry *this;
304803
+		struct list_head *p;
304803
+
304803
+		p = head->next;
304803
+		while (p != head) {
304803
+			this = list_entry(p, struct amd_entry, list);
304803
+			p = p->next;
304803
+			free_amd_entry(this);
304803
+		}
304803
+	}
304803
+}
304803
+
304803
 /*
304803
  * Make common autofs mount options string
304803
  */
304803
--- autofs-5.0.7.orig/modules/Makefile
304803
+++ autofs-5.0.7/modules/Makefile
304803
@@ -7,16 +7,18 @@ include ../Makefile.rules
304803
 
304803
 SRCS :=	lookup_file.c lookup_program.c  lookup_userhome.c \
304803
 	lookup_multi.c lookup_hosts.c lookup_dir.c \
304803
-	parse_sun.c    \
304803
+	parse_sun.c parse_amd.c \
304803
 	mount_generic.c  mount_nfs.c  mount_afs.c  mount_autofs.c \
304803
 	mount_changer.c  mount_bind.c
304803
 
304803
 MODS :=	lookup_file.so lookup_program.so lookup_userhome.so \
304803
 	lookup_multi.so lookup_hosts.so lookup_dir.so \
304803
-	parse_sun.so \
304803
+	parse_sun.so parse_amd.so \
304803
 	mount_generic.so mount_nfs.so mount_afs.so mount_autofs.so \
304803
 	mount_changer.so mount_bind.so
304803
 
304803
+YACCSRC = amd_tok.c amd_parse.tab.c amd_parse.tab.h \
304803
+
304803
 ifeq ($(EXT2FS), 1)
304803
   SRCS += mount_ext2.c
304803
   MODS += mount_ext2.so
304803
@@ -94,6 +96,21 @@ else ifeq ($(EXT4FS), 1)
304803
 	mv $(INSTALLROOT)$(autofslibdir)/mount_ext2.so $(INSTALLROOT)$(autofslibdir)/mount_ext4.so
304803
 endif
304803
 
304803
+amd_tok.c: amd_tok.l
304803
+	$(LEX) -o$@ -Pamd_ $?
304803
+
304803
+amd_tok.o: amd_tok.c amd_parse.tab.h
304803
+
304803
+amd_parse.tab.c amd_parse.tab.h: amd_parse.y
304803
+	$(YACC) -v -d -p amd_ -b amd_parse $?
304803
+
304803
+amd_parse.tab.o: amd_parse.tab.c amd_parse.tab.h
304803
+
304803
+parse_amd.so: parse_amd.c amd_parse.tab.o amd_tok.o
304803
+	$(CC) $(LDFLAGS) $(SOLDFLAGS) $(CFLAGS) -o parse_amd.so \
304803
+		parse_amd.c amd_parse.tab.o amd_tok.o $(AUTOFS_LIB) $(LIBS)
304803
+	$(STRIP) parse_amd.so
304803
+
304803
 #
304803
 # Ad hoc compilation rules for modules which need auxilliary libraries
304803
 #
304803
--- /dev/null
304803
+++ autofs-5.0.7/modules/amd_parse.y
304803
@@ -0,0 +1,511 @@
304803
+%{
304803
+/* ----------------------------------------------------------------------- *
304803
+ *
304803
+ *  Copyright 2013 Ian Kent <raven@themaw.net>
304803
+ *  Copyright 2013 Red Hat, Inc.
304803
+ *  All rights reserved.
304803
+ *
304803
+ *  This program is free software; you can redistribute it and/or modify
304803
+ *  it under the terms of the GNU General Public License as published by
304803
+ *  the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
304803
+ *  USA; either version 2 of the License, or (at your option) any later
304803
+ *  version.
304803
+ *
304803
+ *  This program is distributed in the hope that it will be useful,
304803
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
304803
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
304803
+ *  GNU General Public License for more details.
304803
+ *
304803
+ * ----------------------------------------------------------------------- */
304803
+
304803
+#include <stdio.h>
304803
+#include <string.h>
304803
+#include <stdlib.h>
304803
+#include <stdarg.h>
304803
+#include <ctype.h>
304803
+#include <sys/ioctl.h>
304803
+#include <syslog.h>
304803
+
304803
+#include "automount.h"
304803
+#include "parse_amd.h"
304803
+
304803
+#define MAX_OPTS_LEN	1024
304803
+#define MAX_ERR_LEN	512
304803
+
304803
+static pthread_mutex_t parse_mutex = PTHREAD_MUTEX_INITIALIZER;
304803
+
304803
+extern FILE *amd_in;
304803
+extern char *amd_text;
304803
+extern int amd_lex(void);
304803
+extern void amd_set_scan_buffer(const char *);
304803
+
304803
+static char *amd_strdup(char *);
304803
+static void local_init_vars(void);
304803
+static void local_free_vars(void);
304803
+
304803
+static int amd_error(const char *s);
304803
+static int amd_notify(const char *s);
304803
+static int amd_msg(const char *s);
304803
+
304803
+static int add_location(void);
304803
+static int make_selector(char *name,
304803
+			 char *value1, char *value2,
304803
+			 unsigned int compare);
304803
+static void add_selector(struct selector *selector);
304803
+
304803
+static struct amd_entry entry;
304803
+static struct list_head *entries;
304803
+static struct autofs_point *pap;
304803
+struct substvar *psv;
304803
+static char opts[MAX_OPTS_LEN];
304803
+static void prepend_opt(char *, char *);
304803
+
304803
+#define YYDEBUG 0
304803
+
304803
+#ifndef YYENABLE_NLS
304803
+#define YYENABLE_NLS 0
304803
+#endif
304803
+#ifndef YYLTYPE_IS_TRIVIAL
304803
+#define YYLTYPE_IS_TRIVIAL 0
304803
+#endif
304803
+
304803
+#if YYDEBUG
304803
+static int amd_fprintf(FILE *, char *, ...);
304803
+#undef YYFPRINTF
304803
+#define YYFPRINTF amd_fprintf
304803
+#endif
304803
+
304803
+%}
304803
+
304803
+%union {
304803
+	char strtype[2048];
304803
+	int inttype;
304803
+	long longtype;
304803
+}
304803
+
304803
+%token COMMENT
304803
+%token SEPERATOR
304803
+%token SPACE
304803
+%token HYPHEN
304803
+%token IS_EQUAL
304803
+%token CUT
304803
+%token NOT_EQUAL
304803
+%token COMMA
304803
+%token OPTION_ASSIGN
304803
+%token NILL
304803
+
304803
+%token <strtype> MAP_OPTION
304803
+%token <strtype> MAP_TYPE
304803
+%token <strtype> FS_TYPE
304803
+%token <strtype> FS_OPTION
304803
+%token <strtype> FS_OPT_VALUE
304803
+%token <strtype> MNT_OPTION
304803
+%token <strtype> SELECTOR
304803
+%token <strtype> SELECTOR_VALUE
304803
+%token <strtype> OPTION
304803
+%token <strtype> MACRO
304803
+%token <strtype> OTHER
304803
+
304803
+%type <strtype> options
304803
+
304803
+%start file
304803
+
304803
+%%
304803
+
304803
+file: {
304803
+#if YYDEBUG != 0
304803
+		amd_debug = YYDEBUG;
304803
+#endif
304803
+		memset(opts, 0, sizeof(opts));
304803
+	} line
304803
+	;
304803
+
304803
+line:
304803
+	| location_selection_list
304803
+	;
304803
+
304803
+location_selection_list: location
304803
+	{
304803
+		if (!add_location()) {
304803
+			amd_msg("failed to allocate new location");
304803
+			YYABORT;
304803
+		}
304803
+	}
304803
+	| location_selection_list SPACE location
304803
+	{
304803
+		if (!add_location()) {
304803
+			amd_msg("failed to allocate new location");
304803
+			YYABORT;
304803
+		}
304803
+	}
304803
+	| location_selection_list SPACE CUT SPACE location
304803
+	{
304803
+		entry.flags |= AMD_ENTRY_CUT;
304803
+		if (!add_location()) {
304803
+			amd_msg("failed to allocate new location");
304803
+			YYABORT;
304803
+		}
304803
+	}
304803
+	;
304803
+
304803
+location: location_entry
304803
+	{
304803
+	}
304803
+	| HYPHEN location_entry
304803
+	{
304803
+		entry.flags |= AMD_DEFAULTS_MERGE;
304803
+	}
304803
+	| HYPHEN
304803
+	{
304803
+		entry.flags |= AMD_DEFAULTS_RESET;
304803
+	}
304803
+	;
304803
+
304803
+location_entry: selector_or_option
304803
+	{
304803
+	}
304803
+	| location_entry SEPERATOR selector_or_option
304803
+	{
304803
+	}
304803
+	| location_entry SEPERATOR
304803
+	{
304803
+	}
304803
+	;
304803
+
304803
+selector_or_option: selection
304803
+	{
304803
+	}
304803
+	| option_assignment
304803
+	{
304803
+	}
304803
+	| OTHER
304803
+	{
304803
+		amd_notify($1);
304803
+		YYABORT;
304803
+	}
304803
+	;
304803
+
304803
+selection: SELECTOR IS_EQUAL SELECTOR_VALUE
304803
+	{
304803
+		if (!make_selector($1, $3, NULL, SEL_TYPE_EQUAL)) {
304803
+			amd_notify($1);
304803
+			YYABORT;
304803
+		}
304803
+	}
304803
+	| SELECTOR NOT_EQUAL SELECTOR_VALUE
304803
+	{
304803
+		if (!make_selector($1, $3, NULL, SEL_TYPE_NOTEQUAL)) {
304803
+			amd_notify($1);
304803
+			YYABORT;
304803
+		}
304803
+	}
304803
+	;
304803
+
304803
+option_assignment: MAP_OPTION OPTION_ASSIGN FS_TYPE
304803
+	{
304803
+		if (!strcmp($3, "auto")) {
304803
+			entry.flags |= AMD_MOUNT_TYPE_AUTO;
304803
+			entry.type = amd_strdup($3);
304803
+		} else if (!strcmp($3, "nfs") ||
304803
+			   !strcmp($3, "nfs4")) {
304803
+			entry.flags |= AMD_MOUNT_TYPE_NFS;
304803
+			entry.type = amd_strdup($3);
304803
+		} else if (!strcmp($3, "link")) {
304803
+			entry.flags |= AMD_MOUNT_TYPE_LINK;
304803
+			entry.type = amd_strdup($3);
304803
+		} else if (!strcmp($3, "host")) {
304803
+			entry.flags |= AMD_MOUNT_TYPE_HOST;
304803
+			entry.type = amd_strdup($3);
304803
+		} else {
304803
+			amd_notify($1);
304803
+			YYABORT;
304803
+		}
304803
+	}
304803
+	| MAP_OPTION OPTION_ASSIGN MAP_TYPE
304803
+	{
304803
+		if (!strcmp($3, "file") ||
304803
+		    !strcmp($3, "nis") ||
304803
+		    !strcmp($3, "nisplus") ||
304803
+		    !strcmp($3, "ldap") ||
304803
+		    !strcmp($3, "hesiod"))
304803
+			entry.map_type = amd_strdup($3);
304803
+		else if (!strcmp($3, "exec"))
304803
+			/* autofs uses "program" for "exec" map type */
304803
+			entry.map_type = amd_strdup("program");
304803
+		else {
304803
+			amd_notify($1);
304803
+			YYABORT;
304803
+		}
304803
+	}
304803
+	| MAP_OPTION OPTION_ASSIGN FS_OPT_VALUE
304803
+	{
304803
+		if (!strcmp($1, "fs"))
304803
+			entry.fs = amd_strdup($3);
304803
+		else if (!strcmp($1, "sublink"))
304803
+			entry.sublink = amd_strdup($3);
304803
+		else if (!strcmp($1, "pref")) {
304803
+			if (!strcmp($3, "null"))
304803
+				entry.pref = amd_strdup("");
304803
+			else
304803
+				entry.pref = amd_strdup($3);
304803
+		} else {
304803
+			amd_notify($1);
304803
+			YYABORT;
304803
+		}
304803
+	}
304803
+	| FS_OPTION OPTION_ASSIGN FS_OPT_VALUE
304803
+	{
304803
+		if (!strcmp($1, "rhost"))
304803
+			entry.rhost = amd_strdup($3);
304803
+		else if (!strcmp($1, "rfs"))
304803
+			entry.rfs = amd_strdup($3);
304803
+		else {
304803
+			amd_notify($1);
304803
+			YYABORT;
304803
+		}
304803
+	}
304803
+	| MNT_OPTION OPTION_ASSIGN options
304803
+	{
304803
+		if (!strcmp($1, "opts")) {
304803
+			entry.opts = amd_strdup(opts);
304803
+			memset(opts, 0, sizeof(opts));
304803
+		} else if (!strcmp($1, "addopts")) {
304803
+			entry.addopts = amd_strdup(opts);
304803
+			memset(opts, 0, sizeof(opts));
304803
+		} else if (!strcmp($1, "remopts")) {
304803
+			entry.remopts = amd_strdup(opts);
304803
+			memset(opts, 0, sizeof(opts));
304803
+		} else {
304803
+			memset(opts, 0, sizeof(opts));
304803
+			amd_notify($1);
304803
+			YYABORT;
304803
+		}
304803
+	}
304803
+	;
304803
+
304803
+options: OPTION
304803
+	{
304803
+		prepend_opt(opts, $1);
304803
+	}
304803
+	| OPTION COMMA options
304803
+	{
304803
+		prepend_opt(opts, $1);
304803
+	}
304803
+	;
304803
+
304803
+%%
304803
+
304803
+static void prepend_opt(char *dest, char *opt)
304803
+{
304803
+	char new[MAX_OPTS_LEN];
304803
+	strcpy(new, opt);
304803
+	if (*dest != '\0') {
304803
+		strcat(new, ",");
304803
+		strcat(new, dest);
304803
+	}
304803
+	memmove(dest, new, strlen(new));
304803
+}
304803
+
304803
+#if YYDEBUG
304803
+static int amd_fprintf(FILE *f, char *msg, ...)
304803
+{
304803
+	va_list ap;
304803
+	va_start(ap, msg);
304803
+	vsyslog(LOG_DEBUG, msg, ap);
304803
+	va_end(ap);
304803
+	return 1;
304803
+}
304803
+#endif
304803
+
304803
+static char *amd_strdup(char *str)
304803
+{
304803
+	char *tmp;
304803
+
304803
+	tmp = strdup(str);
304803
+	if (!tmp)
304803
+		amd_error("memory allocation error");
304803
+	return tmp;
304803
+}
304803
+
304803
+static int amd_error(const char *s)
304803
+{
304803
+	if (strcmp(s, "syntax"))
304803
+		logmsg("syntax error in location near [ %s ]\n", amd_text);
304803
+	else
304803
+		logmsg("%s while parsing location.\n", s);
304803
+	return 0;
304803
+}
304803
+
304803
+static int amd_notify(const char *s)
304803
+{
304803
+	logmsg("syntax error in location near [ %s ]\n", s);
304803
+	return(0);
304803
+}
304803
+
304803
+static int amd_msg(const char *s)
304803
+{
304803
+	logmsg("%s\n", s);
304803
+	return 0;
304803
+}
304803
+
304803
+static void local_init_vars(void)
304803
+{
304803
+	memset(&entry, 0, sizeof(entry));
304803
+	memset(opts, 0, sizeof(opts));
304803
+}
304803
+
304803
+static void local_free_vars(void)
304803
+{
304803
+	clear_amd_entry(&entry);
304803
+	return;
304803
+}
304803
+
304803
+static void add_selector(struct selector *selector)
304803
+{
304803
+	struct selector *s = entry.selector;
304803
+
304803
+	if (!s) {
304803
+		entry.selector = selector;
304803
+		return;
304803
+	}
304803
+
304803
+	while (s->next)
304803
+		s = s->next;
304803
+
304803
+	selector->next = s;
304803
+	entry.selector = selector;
304803
+
304803
+	return;
304803
+}
304803
+
304803
+static int make_selector(char *name,
304803
+			 char *value1, char *value2,
304803
+			 unsigned int compare)
304803
+{
304803
+	struct selector *s;
304803
+	char *tmp;
304803
+
304803
+	if (!sel_lookup(name))
304803
+		return 0;
304803
+
304803
+	if (!value1)
304803
+		return 0;
304803
+
304803
+	s = get_selector(name);
304803
+	if (!s)
304803
+		return 0;
304803
+
304803
+	if (s->sel->flags & SEL_FLAG_MACRO) {
304803
+		tmp = amd_strdup(value1);
304803
+		if (!tmp)
304803
+			goto error;
304803
+		s->comp.value = tmp;
304803
+	} else if (s->sel->flags & SEL_FLAG_FUNC1) {
304803
+		char *tmp = amd_strdup(value1);
304803
+		if (!tmp)
304803
+			goto error;
304803
+		s->func.arg1 = tmp;
304803
+	} else if (s->sel->flags & SEL_FLAG_FUNC2) {
304803
+		char *tmp = amd_strdup(value1);
304803
+		if (!tmp)
304803
+			goto error;
304803
+		s->func.arg1 = tmp;
304803
+		if (value2) {
304803
+			tmp = amd_strdup(value2);
304803
+			if (tmp)
304803
+				s->func.arg2 = tmp;
304803
+		}
304803
+	}
304803
+	s->compare = compare;
304803
+
304803
+	add_selector(s);
304803
+
304803
+	return 1;
304803
+error:
304803
+	free_selector(s);
304803
+	return 0;
304803
+}
304803
+
304803
+void amd_init_scan(void)
304803
+{
304803
+}
304803
+
304803
+static void parse_mutex_lock(void)
304803
+{
304803
+	int status = pthread_mutex_lock(&parse_mutex);
304803
+	if (status)
304803
+		fatal(status);
304803
+	return;
304803
+}
304803
+
304803
+static void parse_mutex_unlock(void *arg)
304803
+{
304803
+	int status = pthread_mutex_unlock(&parse_mutex);
304803
+	if (status)
304803
+		fatal(status);
304803
+	return;
304803
+}
304803
+
304803
+static int add_location(void)
304803
+{
304803
+	struct amd_entry *new;
304803
+
304803
+	new = new_amd_entry(psv);
304803
+	if (!new)
304803
+		return 0;
304803
+
304803
+	if (entry.path) {
304803
+		free(new->path);
304803
+		new->path = entry.path;
304803
+	}
304803
+	new->flags = entry.flags;
304803
+	new->type = entry.type;
304803
+	new->map_type = entry.map_type;
304803
+	new->pref = entry.pref;
304803
+	new->fs = entry.fs;
304803
+	new->rhost = entry.rhost;
304803
+	new->rfs = entry.rfs;
304803
+	new->dev = entry.dev;
304803
+	new->opts = entry.opts;
304803
+	new->addopts = entry.addopts;
304803
+	new->remopts = entry.remopts;
304803
+	new->sublink = entry.sublink;
304803
+	new->selector = entry.selector;
304803
+	list_add_tail(&new->list, entries);
304803
+	memset(&entry, 0, sizeof(struct amd_entry));
304803
+
304803
+	return 1;
304803
+}
304803
+
304803
+int amd_parse_list(struct autofs_point *ap,
304803
+		   const char *buffer, struct list_head *list,
304803
+		   struct substvar **sv)
304803
+{
304803
+	char *buf;
304803
+	size_t len;
304803
+	int ret;
304803
+
304803
+	len = strlen(buffer) + 2;
304803
+	buf = malloc(len);
304803
+	if (!buf)
304803
+		return 0;
304803
+	strcpy(buf, buffer);
304803
+
304803
+	parse_mutex_lock();
304803
+	pthread_cleanup_push(parse_mutex_unlock, NULL);
304803
+
304803
+	pap = ap;
304803
+	psv = *sv;
304803
+	entries = list;
304803
+	amd_set_scan_buffer(buf);
304803
+
304803
+	local_init_vars();
304803
+	ret = amd_parse();
304803
+	local_free_vars();
304803
+	*sv = psv;
304803
+
304803
+	pthread_cleanup_pop(1);
304803
+	free(buf);
304803
+
304803
+	return ret;
304803
+}
304803
--- /dev/null
304803
+++ autofs-5.0.7/modules/amd_tok.l
304803
@@ -0,0 +1,316 @@
304803
+%{
304803
+/* ----------------------------------------------------------------------- *
304803
+ *
304803
+ *  Copyright 2013 Ian Kent <raven@themaw.net>
304803
+ *  Copyright 2013 Red Hat, Inc.
304803
+ *  All rights reserved.
304803
+ *
304803
+ *  This program is free software; you can redistribute it and/or modify
304803
+ *  it under the terms of the GNU General Public License as published by
304803
+ *  the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
304803
+ *  USA; either version 2 of the License, or (at your option) any later
304803
+ *  version.
304803
+ *
304803
+ *  This program is distributed in the hope that it will be useful,
304803
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
304803
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
304803
+ *  GNU General Public License for more details.
304803
+ *
304803
+ * ----------------------------------------------------------------------- */
304803
+
304803
+#ifdef ECHO
304803
+# undef ECHO
304803
+#endif
304803
+static void amd_echo(void);	/* forward definition */
304803
+#define ECHO amd_echo()
304803
+int amd_wrap(void);
304803
+
304803
+#include <stdio.h>
304803
+#include <stdlib.h>
304803
+#include <string.h>
304803
+#include <ctype.h>
304803
+#include "amd_parse.tab.h"
304803
+
304803
+int amd_lex(void);
304803
+int mad_wrap(void);
304803
+
304803
+#define YY_SKIP_YYWRAP
304803
+
304803
+#ifndef YY_STACK_USED
304803
+#define YY_STACK_USED 0
304803
+#endif
304803
+#ifndef YY_ALWAYS_INTERACTIVE
304803
+#define YY_ALWAYS_INTERACTIVE 0
304803
+#endif
304803
+#ifndef YY_NEVER_INTERACTIVE
304803
+#define YY_NEVER_INTERACTIVE 0
304803
+#endif
304803
+#ifndef YY_MAIN
304803
+#define YY_MAIN 0
304803
+#endif
304803
+
304803
+void amd_set_scan_buffer(const char *);
304803
+static const char *line = NULL;
304803
+
304803
+#ifdef FLEX_SCANNER
304803
+static const char *line_pos = NULL;
304803
+static const char *line_lim = NULL;
304803
+int amd_yyinput(char *, int);
304803
+
304803
+#undef YY_INPUT
304803
+#define YY_INPUT(b, r, ms) (r = amd_yyinput(b, ms))
304803
+#else
304803
+#undef input
304803
+#undef unput
304803
+#define input()  (*(char *) line++)
304803
+#define unput(c) (*(char *) --line = c)
304803
+#endif
304803
+
304803
+%}
304803
+
304803
+%option nounput
304803
+
304803
+%x MAPOPTVAL FSOPTVAL MNTOPTVAL SELOPTVAL
304803
+
304803
+NL		\r?\n
304803
+OPTWS		[[:blank:]]*
304803
+OTHR		[^!;:=/|\- \t\r\n#]*
304803
+
304803
+MACRO		(\$\{([[:alpha:]_/]([[:alnum:]_\-])([[:alnum:]_\-/])*)\})
304803
+QSTR		(\"([^"\\]|\\.)*\")
304803
+OSTR		([[:alpha:]]([[:alnum:]_\-])+)
304803
+FSTR		([[:alnum:]_/\.]([[:alnum:]_\-/\.]|(\\.))*)
304803
+VSTR		(([[:alnum:]_\-\:/\.])+)
304803
+SSTR		([[:alpha:]]([[:alnum:]\-\.])+)
304803
+
304803
+FOPT		(({QSTR}|{FSTR}|{MACRO})+)
304803
+OPTS		({OSTR}(=({VSTR}|{QSTR}|{MACRO})+)?)
304803
+SOPT		(({SSTR}|{QSTR}|{MACRO})+)
304803
+
304803
+MAPOPT		(fs|type|maptype|pref|sublink|delay)
304803
+MNTOPT		(opts|addopts|remopts)
304803
+FSOPTS		(rhost|rfs|dev|cachedir)
304803
+MAPTYPE		(file|nis|nisplus|ldap|hesiod|exec|ndbm|passwd|union)
304803
+FSTYPE		(auto|nfs|link|host|nfsx|ufs|xfs|efs)
304803
+
304803
+OSSEL		(arch|karch|os|osver|full_os|vendor)
304803
+HSTSEL		(host|hostd|xhost|domain|byte|cluster)
304803
+NETSEL		(netnumber|network|wire|netgrp|netgrpd|in_network)
304803
+USRSEL		(uid|gid)
304803
+MAPSEL		(key|map|path)
304803
+OTRSEL		(exists|autodir|dollar)
304803
+BOLSEL		(true|false)
304803
+SELOPT		({OSSEL}|{HSTSEL}|{NETSEL}|{BOLSEL}|{USRSEL}|{MAPSEL}|{OTRSEL})
304803
+
304803
+CUTSEP		(\|\||\/)
304803
+
304803
+%%
304803
+
304803
+<INITIAL>{
304803
+	{NL} |
304803
+	\x00 { }
304803
+
304803
+	{MAPOPT} {
304803
+		BEGIN(MAPOPTVAL);
304803
+		strcpy(amd_lval.strtype, amd_text);
304803
+		return MAP_OPTION;
304803
+
304803
+	}
304803
+
304803
+	{FSOPTS} {
304803
+		BEGIN(FSOPTVAL);
304803
+		strcpy(amd_lval.strtype, amd_text);
304803
+		return FS_OPTION;
304803
+	}
304803
+
304803
+	{MNTOPT} {
304803
+		BEGIN(MNTOPTVAL);
304803
+		strcpy(amd_lval.strtype, amd_text);
304803
+		return MNT_OPTION;
304803
+	}
304803
+
304803
+	{SELOPT} {
304803
+		BEGIN(SELOPTVAL);
304803
+		strcpy(amd_lval.strtype, amd_text);
304803
+		return SELECTOR;
304803
+	}
304803
+
304803
+	{CUTSEP} { return CUT; }
304803
+
304803
+	"-" { return HYPHEN; }
304803
+
304803
+	{OPTWS} { return SPACE; }
304803
+
304803
+	#.*  { return COMMENT; }
304803
+
304803
+	{OTHR} {
304803
+		strcpy(amd_lval.strtype, amd_text);
304803
+		return OTHER;
304803
+	}
304803
+}
304803
+
304803
+<MAPOPTVAL>{
304803
+	{NL} |
304803
+	\x00 {
304803
+		BEGIN(INITIAL);
304803
+		yyless(1);
304803
+	}
304803
+
304803
+	";" {
304803
+		BEGIN(INITIAL);
304803
+		return SEPERATOR;
304803
+	}
304803
+
304803
+	{OPTWS} {
304803
+		BEGIN(INITIAL);
304803
+		return SPACE;
304803
+	}
304803
+
304803
+	":=" { return OPTION_ASSIGN; }
304803
+
304803
+	{FSTYPE} {
304803
+		strcpy(amd_lval.strtype, amd_text);
304803
+		return FS_TYPE;
304803
+	}
304803
+
304803
+	{MAPTYPE} {
304803
+		strcpy(amd_lval.strtype, amd_text);
304803
+		return MAP_TYPE;
304803
+	}
304803
+
304803
+	{FOPT} {
304803
+		strcpy(amd_lval.strtype, amd_text);
304803
+		return FS_OPT_VALUE;
304803
+	}
304803
+}
304803
+
304803
+<FSOPTVAL>{
304803
+	{NL} |
304803
+	\x00 {
304803
+		BEGIN(INITIAL);
304803
+		yyless(1);
304803
+	}
304803
+
304803
+	";" {
304803
+		BEGIN(INITIAL);
304803
+		return SEPERATOR;
304803
+	}
304803
+
304803
+	{OPTWS} {
304803
+		BEGIN(INITIAL);
304803
+		return SPACE;
304803
+	}
304803
+
304803
+	":=" { return OPTION_ASSIGN; }
304803
+
304803
+	{FOPT} {
304803
+		strcpy(amd_lval.strtype, amd_text);
304803
+		return FS_OPT_VALUE;
304803
+	}
304803
+}
304803
+
304803
+<MNTOPTVAL>{
304803
+	{NL} |
304803
+	\x00 {
304803
+		BEGIN(INITIAL);
304803
+		yyless(1);
304803
+	}
304803
+
304803
+	";" {
304803
+		BEGIN(INITIAL);
304803
+		return SEPERATOR;
304803
+	}
304803
+
304803
+	{OPTWS} {
304803
+		BEGIN(INITIAL);
304803
+		return SPACE;
304803
+	}
304803
+
304803
+	":=" { return OPTION_ASSIGN; }
304803
+
304803
+	"," { return COMMA; }
304803
+
304803
+	{OPTS} {
304803
+		strcpy(amd_lval.strtype, amd_text);
304803
+		return OPTION;
304803
+	}
304803
+}
304803
+
304803
+<SELOPTVAL>{
304803
+	{NL} |
304803
+	\x00 {
304803
+		BEGIN(INITIAL);
304803
+		yyless(1);
304803
+	}
304803
+
304803
+	";" {
304803
+		BEGIN(INITIAL);
304803
+		return SEPERATOR;
304803
+	}
304803
+
304803
+	{OPTWS} {
304803
+		BEGIN(INITIAL);
304803
+		return SPACE;
304803
+	}
304803
+
304803
+	"==" { return IS_EQUAL; }
304803
+
304803
+	"!=" { return NOT_EQUAL; }
304803
+
304803
+	{SOPT} {
304803
+		strcpy(amd_lval.strtype, amd_text);
304803
+		return SELECTOR_VALUE;
304803
+	}
304803
+}
304803
+
304803
+%%
304803
+
304803
+#include "automount.h"
304803
+
304803
+int amd_wrap(void)
304803
+{
304803
+	return 1;
304803
+}
304803
+
304803
+static void amd_echo(void)
304803
+{
304803
+	logmsg("%s\n", amd_text);
304803
+	return;
304803
+}
304803
+
304803
+#ifdef FLEX_SCANNER
304803
+
304803
+void amd_set_scan_buffer(const char *buffer)
304803
+{
304803
+	line = buffer;
304803
+	line_pos = &line[0];
304803
+	/*
304803
+	 * Ensure buffer is 1 greater than string and is zeroed before
304803
+	 * the parse so we can fit the extra NULL which allows us to
304803
+	 * explicitly match an end of line within the buffer (ie. the
304803
+	 * need for 2 NULLS when parsing in memeory buffers).
304803
+	 */
304803
+	line_lim = line + strlen(buffer) + 1;
304803
+}
304803
+
304803
+#define amd_min(a,b) (((a) < (b)) ? (a) : (b))
304803
+
304803
+int amd_yyinput(char *buffer, int max_size)
304803
+{
304803
+	int n = amd_min(max_size, line_lim - line_pos);
304803
+
304803
+	if (n > 0) {
304803
+		memcpy(buffer, line_pos, n);
304803
+		line_pos += n;
304803
+	}
304803
+	return n;
304803
+}
304803
+
304803
+#else
304803
+
304803
+void amd_set_scan_buffer(const char *buffer)
304803
+{
304803
+	line = buffer;
304803
+}
304803
+
304803
+#endif
304803
--- /dev/null
304803
+++ autofs-5.0.7/modules/parse_amd.c
304803
@@ -0,0 +1,1081 @@
304803
+/* ----------------------------------------------------------------------- *
304803
+ *
304803
+ *  Copyright 2013 Ian Kent <raven@themaw.net>
304803
+ *  Copyright 2013 Red Hat, Inc.
304803
+ *  All rights reserved.
304803
+ *
304803
+ *  This program is free software; you can redistribute it and/or modify
304803
+ *  it under the terms of the GNU General Public License as published by
304803
+ *  the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
304803
+ *  USA; either version 2 of the License, or (at your option) any later
304803
+ *  version; incorporated herein by reference.
304803
+ *
304803
+ * ----------------------------------------------------------------------- */
304803
+
304803
+#include <stdio.h>
304803
+#include <malloc.h>
304803
+#include <netdb.h>
304803
+#include <stdlib.h>
304803
+#include <string.h>
304803
+#include <ctype.h>
304803
+#include <limits.h>
304803
+#include <sys/param.h>
304803
+#include <sys/socket.h>
304803
+#include <sys/types.h>
304803
+#include <sys/stat.h>
304803
+#include <sys/vfs.h>
304803
+#include <sys/utsname.h>
304803
+#include <netinet/in.h>
304803
+#include <sys/mount.h>
304803
+#include <linux/fs.h>
304803
+
304803
+#define MODULE_PARSE
304803
+#include "automount.h"
304803
+
304803
+#define MODPREFIX "parse(amd): "
304803
+
304803
+int parse_version = AUTOFS_PARSE_VERSION;	/* Required by protocol */
304803
+
304803
+static struct mount_mod *mount_nfs = NULL;
304803
+static int init_ctr = 0;
304803
+static pthread_mutex_t instance_mutex = PTHREAD_MUTEX_INITIALIZER;
304803
+
304803
+static void instance_mutex_lock(void)
304803
+{
304803
+	int status = pthread_mutex_lock(&instance_mutex);
304803
+	if (status)
304803
+		fatal(status);
304803
+}
304803
+
304803
+static void instance_mutex_unlock(void)
304803
+{
304803
+	int status = pthread_mutex_unlock(&instance_mutex);
304803
+	if (status)
304803
+		fatal(status);
304803
+}
304803
+
304803
+extern const char *global_options;
304803
+
304803
+struct parse_context {
304803
+	char *optstr;		/* Mount options */
304803
+	char *macros;		/* Map wide macro defines */
304803
+	struct substvar *subst;	/* $-substitutions */
304803
+};
304803
+
304803
+struct multi_mnt {
304803
+	char *path;
304803
+	char *options;
304803
+	char *location;
304803
+	struct multi_mnt *next;
304803
+};
304803
+
304803
+/* Default context */
304803
+
304803
+static struct parse_context default_context = {
304803
+	NULL,			/* No mount options */
304803
+	NULL,			/* No map wide macros */
304803
+	NULL			/* The substvar local vars table */
304803
+};
304803
+
304803
+/* Free all storage associated with this context */
304803
+static void kill_context(struct parse_context *ctxt)
304803
+{
304803
+	macro_lock();
304803
+	macro_free_table(ctxt->subst);
304803
+	macro_unlock();
304803
+	if (ctxt->optstr)
304803
+		free(ctxt->optstr);
304803
+	if (ctxt->macros)
304803
+		free(ctxt->macros);
304803
+	free(ctxt);
304803
+}
304803
+
304803
+int parse_init(int argc, const char *const *argv, void **context)
304803
+{
304803
+	struct parse_context *ctxt;
304803
+	char buf[MAX_ERR_BUF];
304803
+
304803
+	sel_hash_init();
304803
+
304803
+	/* Set up context and escape chain */
304803
+
304803
+	if (!(ctxt = (struct parse_context *) malloc(sizeof(struct parse_context)))) {
304803
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
+		logerr(MODPREFIX "malloc: %s", estr);
304803
+		*context = NULL;
304803
+		return 1;
304803
+	}
304803
+	*context = (void *) ctxt;
304803
+
304803
+	*ctxt = default_context;
304803
+
304803
+	/* We only need this once.  NFS mounts are so common that we cache
304803
+	   this module. */
304803
+	instance_mutex_lock();
304803
+	if (mount_nfs)
304803
+		init_ctr++;
304803
+	else {
304803
+		if ((mount_nfs = open_mount("nfs", MODPREFIX))) {
304803
+			init_ctr++;
304803
+		} else {
304803
+			kill_context(ctxt);
304803
+			*context = NULL;
304803
+			instance_mutex_unlock();
304803
+			return 1;
304803
+		}
304803
+	}
304803
+	instance_mutex_unlock();
304803
+
304803
+	return 0;
304803
+}
304803
+
304803
+static void update_with_defaults(struct amd_entry *defaults,
304803
+				 struct amd_entry *entry,
304803
+				 struct substvar *sv)
304803
+{
304803
+	const struct substvar *v;
304803
+	unsigned long fstype = entry->flags & AMD_MOUNT_TYPE_MASK;
304803
+	char *tmp;
304803
+
304803
+	if (fstype == AMD_MOUNT_TYPE_NONE) {
304803
+		unsigned long deftype = defaults->flags & AMD_MOUNT_TYPE_MASK;
304803
+		if (deftype != AMD_MOUNT_TYPE_NONE)
304803
+			entry->flags |= (defaults->flags & AMD_MOUNT_TYPE_MASK);
304803
+		else {
304803
+			entry->flags = AMD_MOUNT_TYPE_NFS;
304803
+			tmp = strdup("nfs");
304803
+			if (tmp)
304803
+				entry->type = tmp;
304803
+		}
304803
+	}
304803
+
304803
+	if (!entry->type && defaults->type) {
304803
+		tmp = strdup(defaults->type);
304803
+		if (tmp)
304803
+			entry->type = tmp;
304803
+	}
304803
+
304803
+	if (!entry->map_type && defaults->map_type) {
304803
+		tmp = strdup(defaults->map_type);
304803
+		if (tmp)
304803
+			entry->map_type = tmp;
304803
+	}
304803
+
304803
+	if (!entry->pref && defaults->pref) {
304803
+		tmp = strdup(defaults->pref);
304803
+		if (tmp)
304803
+			entry->pref = tmp;
304803
+	}
304803
+
304803
+	if (!entry->fs && defaults->fs) {
304803
+		tmp = strdup(defaults->fs);
304803
+		if (tmp)
304803
+			entry->fs = tmp;
304803
+	}
304803
+
304803
+	if (!entry->rfs && defaults->rfs) {
304803
+		tmp = strdup(defaults->rfs);
304803
+		if (tmp)
304803
+			entry->rfs = tmp;
304803
+	}
304803
+
304803
+	if (!entry->rhost && defaults->rhost) {
304803
+		tmp = strdup(defaults->rhost);
304803
+		if (tmp)
304803
+			entry->rhost = tmp;
304803
+	}
304803
+
304803
+	if (!entry->opts && defaults->opts) {
304803
+		tmp = merge_options(defaults->opts, entry->opts);
304803
+		if (tmp)
304803
+			entry->opts = tmp;
304803
+	}
304803
+
304803
+	if (!entry->addopts && defaults->addopts) {
304803
+		tmp = merge_options(defaults->addopts, entry->addopts);
304803
+		if (tmp)
304803
+			entry->addopts = tmp;
304803
+	}
304803
+
304803
+	if (!entry->remopts && defaults->remopts) {
304803
+		tmp = merge_options(defaults->remopts, entry->remopts);
304803
+		if (tmp)
304803
+			entry->remopts = tmp;
304803
+	}
304803
+
304803
+	return;
304803
+}
304803
+
304803
+static char *normalize_hostname(unsigned int logopt, const char *host,
304803
+				unsigned int flags, struct substvar *sv)
304803
+{
304803
+	struct addrinfo hints, *ni;
304803
+	char *name;
304803
+	int ret;
304803
+
304803
+	if (!(flags & CONF_NORMALIZE_HOSTNAMES))
304803
+		name = strdup(host);
304803
+	else {
304803
+		memset(&hints, 0, sizeof(hints));
304803
+		hints.ai_flags = AI_CANONNAME;
304803
+		hints.ai_family = AF_UNSPEC;
304803
+		hints.ai_socktype = SOCK_DGRAM;
304803
+
304803
+		ret = getaddrinfo(host, NULL, &hints, &ni);
304803
+		if (ret) {
304803
+			error(logopt, "hostname lookup failed: %s", gai_strerror(ret));
304803
+			return NULL;
304803
+		}
304803
+		name = strdup(ni->ai_canonname);
304803
+		freeaddrinfo(ni);
304803
+	}
304803
+
304803
+	if (!name)
304803
+		return NULL;
304803
+
304803
+	if (flags & CONF_DOMAIN_STRIP) {
304803
+		const struct substvar *v = macro_findvar(sv, "hostd", 5);
304803
+		if (v) {
304803
+			char *d1 = strchr(name, '.');
304803
+			if (d1) {
304803
+				char *d2 = strchr(v->val, '.');
304803
+				if (d2 && !strcmp(d1, d2))
304803
+					*d1 = '\0';
304803
+			}
304803
+		}
304803
+	}
304803
+
304803
+	return name;
304803
+}
304803
+
304803
+static struct substvar *expand_entry(struct autofs_point *ap,
304803
+				     struct amd_entry *entry,
304803
+				     unsigned int flags,
304803
+				     struct substvar *sv)
304803
+{
304803
+	unsigned int logopt = ap->logopt;
304803
+	char *expand;
304803
+
304803
+	if (entry->rhost) {
304803
+		char *host = strdup(entry->rhost);
304803
+		char *nn;
304803
+		if (!host) {
304803
+			error(ap->logopt, MODPREFIX
304803
+			      "failed to allocate storage for rhost");
304803
+			goto next;
304803
+		}
304803
+		if (expand_selectors(ap, host, &expand, sv)) {
304803
+			free(host);
304803
+			host = expand;
304803
+		}
304803
+		nn = normalize_hostname(ap->logopt, host, flags, sv);
304803
+		if (!nn)
304803
+			sv = macro_addvar(sv, "rhost", 5, host);
304803
+		else {
304803
+			sv = macro_addvar(sv, "rhost", 5, nn);
304803
+			free(host);
304803
+			host = nn;
304803
+		}
304803
+		debug(logopt, MODPREFIX
304803
+		      "rhost expand(\"%s\") -> %s", entry->rhost, host);
304803
+		free(entry->rhost);
304803
+		entry->rhost = host;
304803
+	}
304803
+next:
304803
+	if (entry->sublink) {
304803
+		if (expand_selectors(ap, entry->sublink, &expand, sv)) {
304803
+			debug(logopt, MODPREFIX
304803
+			      "sublink expand(\"%s\") -> %s",
304803
+			      entry->sublink, expand);
304803
+			free(entry->sublink);
304803
+			entry->sublink = expand;
304803
+		}
304803
+		sv = macro_addvar(sv, "sublink", 7, entry->sublink);
304803
+	}
304803
+
304803
+	if (entry->rfs) {
304803
+		if (expand_selectors(ap, entry->rfs, &expand, sv)) {
304803
+			debug(logopt, MODPREFIX
304803
+			      "rfs expand(\"%s\") -> %s", entry->rfs, expand);
304803
+			free(entry->rfs);
304803
+			entry->rfs = expand;
304803
+		}
304803
+		sv = macro_addvar(sv, "rfs", 3, entry->rfs);
304803
+	}
304803
+
304803
+	if (entry->fs) {
304803
+		if (expand_selectors(ap, entry->fs, &expand, sv)) {
304803
+			debug(logopt, MODPREFIX
304803
+			      "fs expand(\"%s\") -> %s", entry->fs, expand);
304803
+			free(entry->fs);
304803
+			entry->fs = expand;
304803
+		}
304803
+		sv = macro_addvar(sv, "fs", 2, entry->fs);
304803
+	}
304803
+
304803
+	if (entry->opts) {
304803
+		if (expand_selectors(ap, entry->opts, &expand, sv)) {
304803
+			debug(logopt, MODPREFIX
304803
+			      "ops expand(\"%s\") -> %s", entry->opts, expand);
304803
+			free(entry->opts);
304803
+			entry->opts = expand;
304803
+		}
304803
+		sv = macro_addvar(sv, "opts", 4, entry->opts);
304803
+	}
304803
+
304803
+	if (entry->addopts) {
304803
+		if (expand_selectors(ap, entry->addopts, &expand, sv)) {
304803
+			debug(logopt, MODPREFIX
304803
+			      "addopts expand(\"%s\") -> %s",
304803
+			      entry->addopts, expand);
304803
+			free(entry->addopts);
304803
+			entry->addopts = expand;
304803
+		}
304803
+		sv = macro_addvar(sv, "addopts", 7, entry->addopts);
304803
+	}
304803
+
304803
+	if (entry->remopts) {
304803
+		if (expand_selectors(ap, entry->remopts, &expand, sv)) {
304803
+			debug(logopt, MODPREFIX
304803
+			      "remopts expand(\"%s\") -> %s",
304803
+			      entry->remopts, expand);
304803
+			free(entry->remopts);
304803
+			entry->remopts = expand;
304803
+		}
304803
+		sv = macro_addvar(sv, "remopts", 7, entry->remopts);
304803
+	}
304803
+
304803
+	return sv;
304803
+}
304803
+
304803
+static void expand_merge_options(struct autofs_point *ap,
304803
+				 struct amd_entry *entry,
304803
+				 struct substvar *sv)
304803
+{
304803
+	char *tmp;
304803
+
304803
+	if (entry->opts) {
304803
+		if (!expand_selectors(ap, entry->opts, &tmp, sv))
304803
+			error(ap->logopt, MODPREFIX "failed to expand opts");
304803
+		else {
304803
+			free(entry->opts);
304803
+			entry->opts = tmp;
304803
+		}
304803
+	}
304803
+
304803
+	if (entry->addopts) {
304803
+		if (!expand_selectors(ap, entry->addopts, &tmp, sv))
304803
+			error(ap->logopt, MODPREFIX "failed to expand addopts");
304803
+		else {
304803
+			free(entry->addopts);
304803
+			entry->addopts = tmp;
304803
+		}
304803
+	}
304803
+
304803
+	if (entry->remopts) {
304803
+		if (!expand_selectors(ap, entry->remopts, &tmp, sv))
304803
+			error(ap->logopt, MODPREFIX "failed to expand remopts");
304803
+		else {
304803
+			free(entry->remopts);
304803
+			entry->remopts = tmp;
304803
+		}
304803
+	}
304803
+
304803
+	return;
304803
+}
304803
+
304803
+static struct substvar *merge_entry_options(struct autofs_point *ap,
304803
+					    struct amd_entry *entry,
304803
+				            struct substvar *sv)
304803
+{
304803
+	char *tmp;
304803
+
304803
+	if (!entry->addopts)
304803
+		return sv;
304803
+
304803
+	if (entry->opts && entry->remopts &&
304803
+	    !strcmp(entry->opts, entry->remopts)) {
304803
+		expand_merge_options(ap, entry, sv);
304803
+		tmp = merge_options(entry->opts, entry->addopts);
304803
+		if (tmp) {
304803
+			info(ap->logopt, MODPREFIX
304803
+			     "merge remopts \"%s\" addopts \"%s\" => \"%s\"",
304803
+			      entry->opts, entry->addopts, tmp);
304803
+			free(entry->opts);
304803
+			entry->opts = tmp;
304803
+			sv = macro_addvar(sv, "opts", 4, entry->opts);
304803
+		}
304803
+		tmp = strdup(entry->opts);
304803
+		if (tmp) {
304803
+			free(entry->remopts);
304803
+			entry->remopts = tmp;
304803
+			sv = macro_addvar(sv, "remopts", 7, entry->remopts);
304803
+		}
304803
+		return sv;
304803
+	}
304803
+
304803
+	expand_merge_options(ap, entry, sv);
304803
+
304803
+	if (entry->opts && entry->addopts) {
304803
+		tmp = merge_options(entry->opts, entry->addopts);
304803
+		if (tmp) {
304803
+			info(ap->logopt, MODPREFIX
304803
+			     "merge opts \"%s\" addopts \"%s\" => \"%s\"",
304803
+			      entry->opts, entry->addopts, tmp);
304803
+			free(entry->opts);
304803
+			entry->opts = tmp;
304803
+			sv = macro_addvar(sv, "opts", 4, entry->opts);
304803
+		}
304803
+	} else if (entry->addopts) {
304803
+		tmp = strdup(entry->addopts);
304803
+		if (tmp) {
304803
+			info(ap->logopt, MODPREFIX
304803
+			     "opts add addopts \"%s\" => \"%s\"", entry->addopts, tmp);
304803
+			entry->opts = tmp;
304803
+			sv = macro_addvar(sv, "opts", 4, entry->opts);
304803
+		}
304803
+	}
304803
+
304803
+	expand_merge_options(ap, entry, sv);
304803
+
304803
+	if (entry->remopts && entry->addopts) {
304803
+		tmp = merge_options(entry->remopts, entry->addopts);
304803
+		if (tmp) {
304803
+			info(ap->logopt, MODPREFIX
304803
+			     "merge remopts \"%s\" addopts \"%s\" => \"%s\"",
304803
+			      entry->remopts, entry->addopts, tmp);
304803
+			free(entry->remopts);
304803
+			entry->remopts = tmp;
304803
+			sv = macro_addvar(sv, "remopts", 7, entry->remopts);
304803
+		}
304803
+	} else if (entry->addopts) {
304803
+		tmp = strdup(entry->addopts);
304803
+		if (tmp) {
304803
+			info(ap->logopt, MODPREFIX
304803
+			     "remopts add addopts \"%s\" => \"%s\"",
304803
+			     entry->addopts, tmp);
304803
+			entry->remopts = tmp;
304803
+			sv = macro_addvar(sv, "remopts", 7, entry->remopts);
304803
+		}
304803
+	}
304803
+
304803
+	return sv;
304803
+}
304803
+
304803
+static int do_auto_mount(struct autofs_point *ap, const char *name,
304803
+			 struct amd_entry *entry, unsigned int flags)
304803
+{
304803
+	char target[PATH_MAX + 1];
304803
+	int ret;
304803
+
304803
+	if (!entry->map_type)
304803
+		strcpy(target, entry->fs);
304803
+	else {
304803
+		strcpy(target, entry->map_type);
304803
+		strcat(target, ",amd:");
304803
+		strcat(target, entry->fs);
304803
+	}
304803
+
304803
+	ret = do_mount(ap, ap->path,
304803
+		       name, strlen(name), target, "autofs", NULL);
304803
+	if (!ret) {
304803
+		struct autofs_point *sm;
304803
+		sm = master_find_submount(ap, entry->path);
304803
+		if (sm) {
304803
+			sm->pref = entry->pref;
304803
+			entry->pref = NULL;
304803
+		}
304803
+	}
304803
+
304803
+	return ret;
304803
+}
304803
+
304803
+static int do_link_mount(struct autofs_point *ap, const char *name,
304803
+			 struct amd_entry *entry, unsigned int flags)
304803
+{
304803
+	char target[PATH_MAX + 1];
304803
+	int ret;
304803
+
304803
+	if (entry->sublink)
304803
+		strcpy(target, entry->sublink);
304803
+	else
304803
+		strcpy(target, entry->fs);
304803
+
304803
+	if (!(flags & CONF_AUTOFS_USE_LOFS))
304803
+		goto symlink;
304803
+
304803
+	/* For a sublink this might cause an external mount */
304803
+	ret = do_mount(ap, ap->path,
304803
+		       name, strlen(name), target, "bind", entry->opts);
304803
+	if (!ret)
304803
+		goto out;
304803
+
304803
+	debug(ap->logopt, MODPREFIX "bind mount failed, symlinking");
304803
+
304803
+symlink:
304803
+	ret = do_mount(ap, ap->path,
304803
+		       name, strlen(name), target, "bind", "symlink");
304803
+	if (!ret)
304803
+		goto out;
304803
+
304803
+	error(ap->logopt, MODPREFIX
304803
+	      "failed to symlink %s to %s", entry->path, target);
304803
+
304803
+	if (entry->sublink) {
304803
+		/* failed to complete sublink mount */
304803
+		if (ext_mount_remove(&entry->ext_mount, entry->fs))
304803
+			umount_ent(ap, entry->fs);
304803
+	}
304803
+out:
304803
+	return ret;
304803
+}
304803
+
304803
+static int do_nfs_mount(struct autofs_point *ap, const char *name,
304803
+			struct amd_entry *entry, unsigned int flags)
304803
+{
304803
+	char target[PATH_MAX + 1];
304803
+	int ret = 0;
304803
+
304803
+	strcpy(target, entry->rhost);
304803
+	strcat(target, ":");
304803
+	strcat(target, entry->rfs);
304803
+
304803
+	if (!entry->sublink) {
304803
+		ret = mount_nfs->mount_mount(ap, ap->path, name, strlen(name),
304803
+					     target, entry->type, entry->opts,
304803
+					     mount_nfs->context);
304803
+	} else {
304803
+		if (!is_mounted(_PATH_MOUNTED, entry->fs, MNTS_REAL)) {
304803
+			ret = mount_nfs->mount_mount(ap, entry->fs, "/", 1,
304803
+						target, entry->type, entry->opts,
304803
+						mount_nfs->context);
304803
+			if (ret)
304803
+				goto out;
304803
+		}
304803
+		/* We might be using an external mount */
304803
+		ext_mount_add(&entry->ext_mount, entry->fs);
304803
+		ret = do_link_mount(ap, name, entry, flags);
304803
+	}
304803
+out:
304803
+	return ret;
304803
+}
304803
+
304803
+static int do_host_mount(struct autofs_point *ap, const char *name,
304803
+			 struct amd_entry *entry, struct map_source *source,
304803
+			 unsigned int flags)
304803
+{
304803
+	struct lookup_mod *lookup;
304803
+	struct mapent *me;
304803
+	const char *argv[2];
304803
+	int ret = 1;
304803
+
304803
+	argv[0] = entry->opts;
304803
+	argv[1] = NULL;
304803
+
304803
+	lookup = open_lookup("hosts", MODPREFIX, NULL, 1, argv);
304803
+	if (!lookup) {
304803
+		debug(ap->logopt, "open lookup module hosts failed");
304803
+		goto out;
304803
+	}
304803
+
304803
+	me = cache_lookup_distinct(source->mc, name);
304803
+	if (me)
304803
+		cache_push_mapent(me, NULL);
304803
+
304803
+	master_source_current_wait(ap->entry);
304803
+	ap->entry->current = source;
304803
+
304803
+	ret = lookup->lookup_mount(ap, name, strlen(name), lookup->context);
304803
+
304803
+	close_lookup(lookup);
304803
+out:
304803
+	return ret;
304803
+}
304803
+
304803
+static int amd_mount(struct autofs_point *ap, const char *name,
304803
+		     struct amd_entry *entry, struct map_source *source,
304803
+		     struct substvar *sv, unsigned int flags,
304803
+		     struct parse_context *ctxt)
304803
+{
304803
+	unsigned long fstype = entry->flags & AMD_MOUNT_TYPE_MASK;
304803
+	int ret = 1;
304803
+
304803
+	switch (fstype) {
304803
+	case AMD_MOUNT_TYPE_AUTO:
304803
+		ret = do_auto_mount(ap, name, entry, flags);
304803
+		break;
304803
+
304803
+	case AMD_MOUNT_TYPE_NFS:
304803
+		ret = do_nfs_mount(ap, name, entry, flags);
304803
+		break;
304803
+
304803
+	case AMD_MOUNT_TYPE_LINK:
304803
+		ret = do_link_mount(ap, name, entry, flags);
304803
+		break;
304803
+
304803
+	case AMD_MOUNT_TYPE_HOST:
304803
+		ret = do_host_mount(ap, name, entry, source, flags);
304803
+		break;
304803
+
304803
+	default:
304803
+		info(ap->logopt,
304803
+		     MODPREFIX "unkown file system type %x", fstype);
304803
+		break;
304803
+	}
304803
+
304803
+	return ret;
304803
+}
304803
+
304803
+void dequote_entry(struct autofs_point *ap, struct amd_entry *entry)
304803
+{
304803
+	char *res;
304803
+
304803
+	if (entry->pref) {
304803
+		res = dequote(entry->pref, strlen(entry->pref), ap->logopt);
304803
+		if (res) {
304803
+			debug(ap->logopt,
304803
+			      MODPREFIX "pref dequote(\"%.*s\") -> %s",
304803
+			      strlen(entry->pref), entry->pref, res);
304803
+			free(entry->pref);
304803
+			entry->pref = res;
304803
+		}
304803
+	}
304803
+
304803
+	if (entry->sublink) {
304803
+		res = dequote(entry->sublink, strlen(entry->sublink), ap->logopt);
304803
+		if (res) {
304803
+			debug(ap->logopt,
304803
+			      MODPREFIX "sublink dequote(\"%.*s\") -> %s",
304803
+			      strlen(entry->sublink), entry->sublink, res);
304803
+			free(entry->sublink);
304803
+			entry->sublink = res;
304803
+		}
304803
+	}
304803
+
304803
+	if (entry->fs) {
304803
+		res = dequote(entry->fs, strlen(entry->fs), ap->logopt);
304803
+		if (res) {
304803
+			debug(ap->logopt,
304803
+			      MODPREFIX "fs dequote(\"%.*s\") -> %s",
304803
+			      strlen(entry->fs), entry->fs, res);
304803
+			free(entry->fs);
304803
+			entry->fs = res;
304803
+		}
304803
+	}
304803
+
304803
+	if (entry->rfs) {
304803
+		res = dequote(entry->rfs, strlen(entry->rfs), ap->logopt);
304803
+		if (res) {
304803
+			debug(ap->logopt,
304803
+			      MODPREFIX "rfs dequote(\"%.*s\") -> %s",
304803
+			      strlen(entry->rfs), entry->rfs, res);
304803
+			free(entry->rfs);
304803
+			entry->rfs = res;
304803
+		}
304803
+	}
304803
+
304803
+	if (entry->opts) {
304803
+		res = dequote(entry->opts, strlen(entry->opts), ap->logopt);
304803
+		if (res) {
304803
+			debug(ap->logopt,
304803
+			      MODPREFIX "ops dequote(\"%.*s\") -> %s",
304803
+			      strlen(entry->opts), entry->opts, res);
304803
+			free(entry->opts);
304803
+			entry->opts = res;
304803
+		}
304803
+	}
304803
+
304803
+	if (entry->remopts) {
304803
+		res = dequote(entry->remopts, strlen(entry->remopts), ap->logopt);
304803
+		if (res) {
304803
+			debug(ap->logopt,
304803
+			      MODPREFIX "remopts dequote(\"%.*s\") -> %s",
304803
+			      strlen(entry->remopts), entry->remopts, res);
304803
+			free(entry->remopts);
304803
+			entry->remopts = res;
304803
+		}
304803
+	}
304803
+
304803
+	if (entry->addopts) {
304803
+		res = dequote(entry->addopts, strlen(entry->addopts), ap->logopt);
304803
+		if (res) {
304803
+			debug(ap->logopt,
304803
+			      MODPREFIX "addopts dequote(\"%.*s\") -> %s",
304803
+			      strlen(entry->addopts), entry->addopts, res);
304803
+			free(entry->addopts);
304803
+			entry->addopts = res;
304803
+		}
304803
+	}
304803
+
304803
+	return;
304803
+}
304803
+
304803
+static void normalize_sublink(unsigned int logopt,
304803
+			      struct amd_entry *entry, struct substvar *sv)
304803
+{
304803
+	char *new;
304803
+	size_t len;
304803
+
304803
+	if (entry->sublink && *entry->sublink != '/') {
304803
+		len = strlen(entry->fs) + strlen(entry->sublink) + 2;
304803
+		new = malloc(len);
304803
+		if (!new) {
304803
+			error(logopt, MODPREFIX
304803
+			      "error: couldn't allocate storage for sublink");
304803
+			return;
304803
+		}
304803
+		strcpy(new, entry->fs);
304803
+		strcat(new, "/");
304803
+		strcat(new, entry->sublink);
304803
+		debug(logopt, MODPREFIX
304803
+		      "rfs dequote(\"%.*s\") -> %s",
304803
+		      strlen(entry->sublink), entry->sublink, new);
304803
+		free(entry->sublink);
304803
+		entry->sublink = new;
304803
+	}
304803
+	return;
304803
+}
304803
+
304803
+static struct amd_entry *dup_defaults_entry(struct amd_entry *defaults)
304803
+{
304803
+	struct amd_entry *entry;
304803
+	char *tmp;
304803
+
304803
+	entry = malloc(sizeof(struct amd_entry));
304803
+	if (!entry)
304803
+		return NULL;
304803
+	memset(entry, 0, sizeof(struct amd_entry));
304803
+
304803
+	entry->flags = defaults->flags;
304803
+
304803
+	if (defaults->type) {
304803
+		tmp = strdup(defaults->type);
304803
+		if (tmp)
304803
+			entry->type = tmp;
304803
+	}
304803
+
304803
+	if (defaults->map_type) {
304803
+		tmp = strdup(defaults->map_type);
304803
+		if (tmp)
304803
+			entry->map_type = tmp;
304803
+	}
304803
+
304803
+	if (defaults->pref) {
304803
+		tmp = strdup(defaults->pref);
304803
+		if (tmp)
304803
+			entry->pref = tmp;
304803
+	}
304803
+
304803
+	if (defaults->fs) {
304803
+		tmp = strdup(defaults->fs);
304803
+		if (tmp)
304803
+			entry->fs = tmp;
304803
+	}
304803
+
304803
+	if (defaults->rfs) {
304803
+		tmp = strdup(defaults->rfs);
304803
+		if (tmp)
304803
+			entry->rfs = tmp;
304803
+	}
304803
+
304803
+	if (defaults->rhost) {
304803
+		tmp = strdup(defaults->rhost);
304803
+		if (tmp)
304803
+			entry->rhost = tmp;
304803
+	}
304803
+
304803
+	if (defaults->opts) {
304803
+		tmp = strdup(defaults->opts);
304803
+		if (tmp)
304803
+			entry->opts = tmp;
304803
+	}
304803
+
304803
+	if (defaults->addopts) {
304803
+		tmp = strdup(defaults->addopts);
304803
+		if (tmp)
304803
+			entry->addopts = tmp;
304803
+	}
304803
+
304803
+	if (defaults->remopts) {
304803
+		tmp = strdup(defaults->remopts);
304803
+		if (tmp)
304803
+			entry->remopts = tmp;
304803
+	}
304803
+
304803
+	INIT_LIST_HEAD(&entry->list);
304803
+
304803
+	return entry;
304803
+}
304803
+
304803
+struct amd_entry *make_default_entry(struct autofs_point *ap,
304803
+				     struct substvar *sv)
304803
+{
304803
+	char *defaults = "opts:=rw,defaults";
304803
+	struct amd_entry *defaults_entry;
304803
+	struct list_head dflts;
304803
+
304803
+	INIT_LIST_HEAD(&dflts);
304803
+	if (amd_parse_list(ap, defaults, &dflts, &sv))
304803
+		return NULL;
304803
+	defaults_entry = list_entry(dflts.next, struct amd_entry, list);
304803
+	list_del_init(&defaults_entry->list);
304803
+	/* The list should now be empty .... */
304803
+	free_amd_entry_list(&dflts);
304803
+	return defaults_entry;
304803
+}
304803
+
304803
+static struct amd_entry *select_default_entry(struct autofs_point *ap,
304803
+					      struct list_head *entries,
304803
+					      struct substvar *sv)
304803
+{
304803
+	unsigned long flags = conf_amd_get_flags(ap->path);
304803
+	struct amd_entry *defaults_entry = NULL;
304803
+	struct amd_entry *entry_default = NULL;
304803
+	struct list_head *p, *head;
304803
+
304803
+	if (!(flags & CONF_SELECTORS_IN_DEFAULTS))
304803
+		goto no_sel;
304803
+
304803
+	head = entries;
304803
+	p = head->next;
304803
+	while (p != head) {
304803
+		struct amd_entry *this = list_entry(p, struct amd_entry, list);
304803
+
304803
+		p = p->next;
304803
+
304803
+		if (this->flags & AMD_DEFAULTS_MERGE) {
304803
+			if (entry_default)
304803
+				free_amd_entry(entry_default);
304803
+			list_del_init(&this->list);
304803
+			entry_default = this;
304803
+			continue;
304803
+		} else if (this->flags & AMD_DEFAULTS_RESET) {
304803
+			struct amd_entry *new;
304803
+			new = dup_defaults_entry(defaults_entry);
304803
+			if (new) {
304803
+				free_amd_entry(entry_default);
304803
+				entry_default = new;
304803
+			}
304803
+			list_del_init(&this->list);
304803
+			free_amd_entry(this);
304803
+			continue;
304803
+		}
304803
+	}
304803
+
304803
+	/* Not strickly amd semantics but ... */
304803
+	if (!defaults_entry && entry_default) {
304803
+		defaults_entry = entry_default;
304803
+		goto done;
304803
+	}
304803
+
304803
+	if (!defaults_entry) {
304803
+		debug(ap->logopt, MODPREFIX
304803
+		      "no matching selector(s) found in defaults, "
304803
+		      "using internal defaults");
304803
+		goto ret_default;
304803
+	}
304803
+
304803
+	goto done;
304803
+
304803
+no_sel:
304803
+	if (list_empty(entries))
304803
+		goto ret_default;
304803
+
304803
+	defaults_entry = list_entry(entries->next, struct amd_entry, list);
304803
+	list_del_init(&defaults_entry->list);
304803
+	if (!list_empty(entries)) {
304803
+		free_amd_entry(defaults_entry);
304803
+		goto ret_default;
304803
+	}
304803
+done:
304803
+	/*merge_entry_options(ap, defaults_entry, sv);*/
304803
+	/*normalize_sublink(ap->logopt, defaults_entry, sv);*/
304803
+	return defaults_entry;
304803
+
304803
+ret_default:
304803
+	return make_default_entry(ap, sv);
304803
+}
304803
+
304803
+static struct amd_entry *get_defaults_entry(struct autofs_point *ap,
304803
+					    const char *defaults,
304803
+					    struct substvar *sv)
304803
+{
304803
+	struct amd_entry *entry;
304803
+	struct list_head dflts;
304803
+
304803
+	INIT_LIST_HEAD(&dflts);
304803
+
304803
+	entry = NULL;
304803
+	if (!defaults)
304803
+		goto out;
304803
+	else {
304803
+		char *expand;
304803
+		if (!expand_selectors(ap, defaults, &expand, sv))
304803
+			goto out;
304803
+		if (amd_parse_list(ap, expand, &dflts, &sv))
304803
+			goto out;
304803
+		entry = select_default_entry(ap, &dflts, sv);
304803
+		free(expand);
304803
+	}
304803
+
304803
+	return entry;
304803
+out:
304803
+	return make_default_entry(ap, sv);
304803
+}
304803
+
304803
+int parse_mount(struct autofs_point *ap, const char *name,
304803
+		int name_len, const char *mapent, void *context)
304803
+{
304803
+	struct parse_context *ctxt = (struct parse_context *) context;
304803
+	unsigned int flags = conf_amd_get_flags(ap->path);
304803
+	struct substvar *sv = NULL;
304803
+	struct map_source *source;
304803
+	struct mapent_cache *mc;
304803
+	struct mapent *me;
304803
+	struct list_head entries, *p, *head;
304803
+	struct amd_entry *defaults_entry;
304803
+	struct amd_entry *cur_defaults;
304803
+	char *defaults;
304803
+	char *pmapent;
304803
+	int len, rv = 1;
304803
+	int cur_state;
304803
+	int ret;
304803
+
304803
+	source = ap->entry->current;
304803
+	ap->entry->current = NULL;
304803
+	master_source_current_signal(ap->entry);
304803
+
304803
+	mc = source->mc;
304803
+
304803
+	if (!mapent) {
304803
+		warn(ap->logopt, MODPREFIX "error: empty map entry");
304803
+		return 1;
304803
+	}
304803
+
304803
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
304803
+
304803
+	len = expand_selectors(ap, mapent, &pmapent, sv);
304803
+	if (!len) {
304803
+		macro_free_table(sv);
304803
+		pthread_setcancelstate(cur_state, NULL);
304803
+		return 1;
304803
+	}
304803
+
304803
+	pthread_setcancelstate(cur_state, NULL);
304803
+
304803
+	debug(ap->logopt, MODPREFIX "expanded mapent: %s", pmapent);
304803
+
304803
+	defaults = conf_amd_get_map_defaults(ap->path);
304803
+	if (defaults) {
304803
+		debug(ap->logopt, MODPREFIX
304803
+		      "using map_defaults %s for %s", defaults, ap->path);
304803
+	} else if ((me = cache_lookup_distinct(mc, "/defaults"))) {
304803
+		defaults = strdup(me->mapent);
304803
+		if (defaults)
304803
+			debug(ap->logopt, MODPREFIX
304803
+			      "using /defaults %s from map", defaults);
304803
+		else {
304803
+			char buf[MAX_ERR_BUF];
304803
+			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
+			error(ap->logopt, MODPREFIX "malloc: %s", estr);
304803
+		}
304803
+	}
304803
+
304803
+	defaults_entry = get_defaults_entry(ap, defaults, sv);
304803
+	if (!defaults_entry) {
304803
+		error(ap->logopt, MODPREFIX "failed to get a defaults entry");
304803
+		if (defaults)
304803
+			free(defaults);
304803
+		free(pmapent);
304803
+		macro_free_table(sv);
304803
+		return 1;
304803
+	}
304803
+	if (defaults)
304803
+		free(defaults);
304803
+
304803
+	INIT_LIST_HEAD(&entries);
304803
+
304803
+	ret = amd_parse_list(ap, pmapent, &entries, &sv;;
304803
+	if (ret) {
304803
+		error(ap->logopt,
304803
+		      MODPREFIX "failed to parse entry: %s", pmapent);
304803
+		free(pmapent);
304803
+		goto done;
304803
+	}
304803
+
304803
+	free(pmapent);
304803
+
304803
+	if (list_empty(&entries)) {
304803
+		error(ap->logopt, MODPREFIX "no location found after parse");
304803
+		goto done;
304803
+	}
304803
+
304803
+	cur_defaults = dup_defaults_entry(defaults_entry);
304803
+	if (!cur_defaults) {
304803
+		error(ap->logopt, MODPREFIX
304803
+		      "failed to duplicate defaults entry");
304803
+		goto done;
304803
+	}
304803
+
304803
+	head = &entries;
304803
+	p = head->next;
304803
+	while (p != head) {
304803
+		struct amd_entry *this = list_entry(p, struct amd_entry, list);
304803
+		p = p->next;
304803
+
304803
+		if (this->flags & AMD_DEFAULTS_MERGE) {
304803
+			free_amd_entry(cur_defaults);
304803
+			list_del_init(&this->list);
304803
+			cur_defaults = this;
304803
+			continue;
304803
+		} else if (this->flags & AMD_DEFAULTS_RESET) {
304803
+			struct amd_entry *new;
304803
+			new = dup_defaults_entry(defaults_entry);
304803
+			if (new) {
304803
+				free_amd_entry(cur_defaults);
304803
+				cur_defaults = new;
304803
+			}
304803
+			list_del_init(&this->list);
304803
+			free_amd_entry(this);
304803
+			continue;
304803
+		}
304803
+
304803
+		update_with_defaults(cur_defaults, this, sv);
304803
+		sv = expand_entry(ap, this, flags, sv);
304803
+		sv = merge_entry_options(ap, this, sv);
304803
+		normalize_sublink(ap->logopt, this, sv);
304803
+
304803
+		dequote_entry(ap, this);
304803
+
304803
+		rv = amd_mount(ap, name, this, source, sv, flags, ctxt);
304803
+		if (!rv)
304803
+			break;
304803
+	}
304803
+	free_amd_entry(cur_defaults);
304803
+
304803
+	if (rv)
304803
+		debug(ap->logopt, "no more locations to try, returning fail");
304803
+done:
304803
+	free_amd_entry_list(&entries);
304803
+	free_amd_entry(defaults_entry);
304803
+	macro_free_table(sv);
304803
+
304803
+	return rv;
304803
+}
304803
+
304803
+int parse_done(void *context)
304803
+{
304803
+	int rv = 0;
304803
+	struct parse_context *ctxt = (struct parse_context *) context;
304803
+
304803
+	instance_mutex_lock();
304803
+	if (--init_ctr == 0) {
304803
+		rv = close_mount(mount_nfs);
304803
+		mount_nfs = NULL;
304803
+	}
304803
+	instance_mutex_unlock();
304803
+	if (ctxt)
304803
+		kill_context(ctxt);
304803
+
304803
+	return rv;
304803
+}