Blame SOURCES/autofs-5.0.8-amd-lookup-update-lookup-ldap-to-handle-amd-keys.patch

304803
autofs-5.0.8 - amd lookup update lookup ldap to handle amd keys
304803
304803
From: Ian Kent <raven@themaw.net>
304803
304803
304803
---
304803
 include/lookup_ldap.h |    3 
304803
 modules/lookup_ldap.c |  707 +++++++++++++++++++++++++++++++++++++++++++++-----
304803
 2 files changed, 651 insertions(+), 59 deletions(-)
304803
304803
--- autofs-5.0.7.orig/include/lookup_ldap.h
304803
+++ autofs-5.0.7/include/lookup_ldap.h
304803
@@ -36,6 +36,7 @@ struct ldap_searchdn {
304803
 
304803
 struct lookup_context {
304803
 	char *mapname;
304803
+	unsigned int format;
304803
 
304803
 	char *server;
304803
 	int port;
304803
@@ -43,6 +44,8 @@ struct lookup_context {
304803
 	char *qdn;
304803
 	unsigned int timeout;
304803
 	unsigned int network_timeout;
304803
+	unsigned long timestamp;
304803
+	unsigned int check_defaults;
304803
 
304803
 	/* LDAP version 2 or 3 */
304803
 	int version;
304803
--- autofs-5.0.7.orig/modules/lookup_ldap.c
304803
+++ autofs-5.0.7/modules/lookup_ldap.c
304803
@@ -29,6 +29,7 @@
304803
 #include <resolv.h>
304803
 #include <lber.h>
304803
 #include <libxml/tree.h>
304803
+#include <stdlib.h>
304803
 
304803
 #define MODULE_LOOKUP
304803
 #include "automount.h"
304803
@@ -52,6 +53,14 @@ static struct ldap_schema common_schema[
304803
 };
304803
 static unsigned int common_schema_count = sizeof(common_schema)/sizeof(struct ldap_schema);
304803
 
304803
+static struct ldap_schema amd_timestamp = {
304803
+	"madmap", "amdmapName", "amdmapTimestamp", NULL, "amdmapTimestamp"
304803
+};
304803
+
304803
+static struct ldap_schema amd_schema = {
304803
+	"amdmap", "amdmapName", "amdmap", "amdmapKey", "amdmapValue"
304803
+};
304803
+
304803
 /*
304803
  * Initialization and de-initialization of LDAP and OpenSSL must be
304803
  * always serialized to avoid corruption of context structures inside
304803
@@ -62,6 +71,7 @@ pthread_mutex_t ldapinit_mutex = PTHREAD
304803
 struct ldap_search_params {
304803
 	struct autofs_point *ap;
304803
 	LDAP *ldap;
304803
+	char *base;
304803
 	char *query, **attrs;
304803
 	struct berval *cookie;
304803
 	ber_int_t pageSize;
304803
@@ -531,6 +541,16 @@ static int find_query_dn(unsigned logopt
304803
 	if (ctxt->schema)
304803
 		return 0;
304803
 
304803
+	if (ctxt->format & MAP_FLAG_FORMAT_AMD) {
304803
+		schema = alloc_common_schema(&amd_schema);
304803
+		if (!schema) {
304803
+			error(logopt, MODPREFIX "failed to allocate schema");
304803
+			return 0;
304803
+		}
304803
+		ctxt->schema = schema;
304803
+		return 1;
304803
+	}
304803
+
304803
 	for (i = 0; i < common_schema_count; i++) {
304803
 		const char *class = common_schema[i].map_class;
304803
 		const char *key = common_schema[i].map_attr;
304803
@@ -587,8 +607,10 @@ static int do_bind(unsigned logopt, LDAP
304803
 
304803
 	if (!ctxt->cur_host) {
304803
 		ctxt->cur_host = nhost;
304803
-		/* Check if schema defined in conf first time only */
304803
-		ctxt->schema = defaults_get_schema();
304803
+		if (!(ctxt->format & MAP_FLAG_FORMAT_AMD)) {
304803
+			/* Check if schema defined in conf first time only */
304803
+			ctxt->schema = defaults_get_schema();
304803
+		}
304803
 	} else {
304803
 		/* If connection host has changed update */
304803
 		if (strcmp(ctxt->cur_host, nhost)) {
304803
@@ -614,7 +636,7 @@ static int do_bind(unsigned logopt, LDAP
304803
 			      MODPREFIX "failed to find valid query dn");
304803
 			return 0;
304803
 		}
304803
-	} else {
304803
+	} else if (!(ctxt->format & MAP_FLAG_FORMAT_AMD)) {
304803
 		const char *class = ctxt->schema->map_class;
304803
 		const char *key = ctxt->schema->map_attr;
304803
 		if (!get_query_dn(logopt, ldap, ctxt, class, key)) {
304803
@@ -648,6 +670,126 @@ static LDAP *do_connect(unsigned logopt,
304803
 	return ldap;
304803
 }
304803
 
304803
+static unsigned long get_amd_timestamp(struct lookup_context *ctxt)
304803
+{
304803
+	LDAP *ldap;
304803
+	LDAPMessage *result = NULL, *e;
304803
+	char *query;
304803
+	int scope = LDAP_SCOPE_SUBTREE;
304803
+	char *map, *class, *value;
304803
+	char *attrs[2];
304803
+	struct berval **bvValues;
304803
+	unsigned long timestamp = 0;
304803
+	int rv, l, ql;
304803
+
304803
+	ldap = do_connect(LOGOPT_ANY, ctxt->server, ctxt);
304803
+	if (!ldap)
304803
+		return 0;
304803
+
304803
+	map = amd_timestamp.map_attr;
304803
+	class = amd_timestamp.entry_class;
304803
+	value = amd_timestamp.value_attr;
304803
+
304803
+	attrs[0] = value;
304803
+	attrs[1] = NULL;
304803
+
304803
+	/* Build a query string. */
304803
+	l = strlen(class) +
304803
+	    strlen(map) + strlen(ctxt->mapname) + 21;
304803
+
304803
+	query = malloc(l);
304803
+	if (query == NULL) {
304803
+		char buf[MAX_ERR_BUF];
304803
+		char *estr = strerror_r(errno, buf, sizeof(buf));
304803
+		crit(LOGOPT_ANY, MODPREFIX "malloc: %s", estr);
304803
+		return 0;
304803
+	}
304803
+
304803
+	/*
304803
+	 * Look for an entry in class under ctxt-base
304803
+	 * whose entry is equal to qKey.
304803
+	 */
304803
+	ql = sprintf(query, "(&(objectclass=%s)(%s=%s))",
304803
+		     class, map, ctxt->mapname);
304803
+	if (ql >= l) {
304803
+		error(LOGOPT_ANY,
304803
+		      MODPREFIX "error forming query string");
304803
+		free(query);
304803
+		return 0;
304803
+	}
304803
+
304803
+	rv = ldap_search_s(ldap, ctxt->base, scope, query, attrs, 0, &result);
304803
+	if ((rv != LDAP_SUCCESS) || !result) {
304803
+		crit(LOGOPT_ANY, MODPREFIX "timestamp query failed %s", query);
304803
+		unbind_ldap_connection(LOGOPT_ANY, ldap, ctxt);
304803
+		if (result)
304803
+			ldap_msgfree(result);
304803
+		free(query);
304803
+		return 0;
304803
+	}
304803
+
304803
+	e = ldap_first_entry(ldap, result);
304803
+	if (!e) {
304803
+		debug(LOGOPT_ANY,
304803
+		     MODPREFIX "got answer, but no entry for timestamp");
304803
+		ldap_msgfree(result);
304803
+		unbind_ldap_connection(LOGOPT_ANY, ldap, ctxt);
304803
+		free(query);
304803
+		return CHE_MISSING;
304803
+	}
304803
+
304803
+	while (e) {
304803
+		char *v_val;
304803
+		char *endptr;
304803
+
304803
+		bvValues = ldap_get_values_len(ldap, e, value);
304803
+		if (!bvValues || !*bvValues) {
304803
+			debug(LOGOPT_ANY,
304803
+			      MODPREFIX "no value found in timestamp");
304803
+			goto next;
304803
+		}
304803
+
304803
+		/* There should be one value for a timestamp */
304803
+		v_val = bvValues[0]->bv_val;
304803
+
304803
+		timestamp = strtol(v_val, &endptr, 0);
304803
+		if ((errno == ERANGE &&
304803
+		    (timestamp == LONG_MAX || timestamp == LONG_MIN)) ||
304803
+		    (errno != 0 && timestamp == 0)) {
304803
+			debug(LOGOPT_ANY,
304803
+			      MODPREFIX "invalid value in timestamp");
304803
+			free(query);
304803
+			return 0;
304803
+		}
304803
+
304803
+		if (endptr == v_val) {
304803
+			debug(LOGOPT_ANY,
304803
+			      MODPREFIX "no digits found in timestamp");
304803
+			free(query);
304803
+			return 0;
304803
+		}
304803
+
304803
+		if (*endptr != '\0') {
304803
+			warn(LOGOPT_ANY, MODPREFIX
304803
+			     "characters found after number: %s", endptr);
304803
+			warn(LOGOPT_ANY,
304803
+			     MODPREFIX "timestamp may be invalid");
304803
+		}
304803
+
304803
+		ldap_value_free_len(bvValues);
304803
+		break;
304803
+next:
304803
+		ldap_value_free_len(bvValues);
304803
+		e = ldap_next_entry(ldap, e);
304803
+	}
304803
+
304803
+	ldap_msgfree(result);
304803
+	unbind_ldap_connection(LOGOPT_ANY, ldap, ctxt);
304803
+	free(query);
304803
+
304803
+	return timestamp;
304803
+}
304803
+
304803
 static LDAP *connect_to_server(unsigned logopt, const char *uri, struct lookup_context *ctxt)
304803
 {
304803
 	LDAP *ldap;
304803
@@ -1215,7 +1357,7 @@ static int parse_server_string(unsigned
304803
 		const char *q = NULL;
304803
 
304803
 		/* Isolate the server(s). */
304803
-		if ((q = strchr(s, '/'))) {
304803
+		if ((q = strchr(s, '/')) || (q = strchr(s, '\0'))) {
304803
 			l = q - s;
304803
 			if (*proto) {
304803
 				al_len = l + strlen(proto) + 2;
304803
@@ -1318,8 +1460,7 @@ static int parse_server_string(unsigned
304803
 		ptr += l + 1;
304803
 	}
304803
 
304803
-	/* TODO: why did I do this - how can the map name "and" base dn be missing? */
304803
-	if (!ptr)
304803
+	if (!ptr || ctxt->format & MAP_FLAG_FORMAT_AMD)
304803
 		goto done;
304803
 
304803
 	/*
304803
@@ -1505,36 +1646,83 @@ int lookup_init(const char *mapfmt, int
304803
 	/* If a map type isn't explicitly given, parse it like sun entries. */
304803
 	if (mapfmt == NULL)
304803
 		mapfmt = MAPFMT_DEFAULT;
304803
-
304803
-	/*
304803
-	 * Parse out the server name and base dn, and fill them
304803
-	 * into the proper places in the lookup context structure.
304803
-	 */
304803
-	if (!parse_server_string(LOGOPT_NONE, argv[0], ctxt)) {
304803
-		error(LOGOPT_ANY, MODPREFIX "cannot parse server string");
304803
-		free_context(ctxt);
304803
-		return 1;
304803
+	if (!strcmp(mapfmt, "amd")) {
304803
+		ctxt->format = MAP_FLAG_FORMAT_AMD;
304803
+		ctxt->check_defaults = 1;
304803
 	}
304803
 
304803
-	if (!ctxt->base)
304803
-		ctxt->sdns = defaults_get_searchdns();
304803
-
304803
 	ctxt->timeout = defaults_get_ldap_timeout();
304803
 	ctxt->network_timeout = defaults_get_ldap_network_timeout();
304803
 
304803
-	if (!ctxt->server) {
304803
-		struct list_head *uris = defaults_get_uris();
304803
-		if (uris) {
304803
-			validate_uris(uris);
304803
-			if (!list_empty(uris))
304803
-				ctxt->uris = uris;
304803
-			else {
304803
-				error(LOGOPT_ANY,
304803
-				      "no valid uris found in config list"
304803
-				      ", using default system config");
304803
-				free(uris);
304803
+	if (!(ctxt->format & MAP_FLAG_FORMAT_AMD)) {
304803
+		/*
304803
+		 * Parse out the server name and base dn, and fill them
304803
+		 * into the proper places in the lookup context structure.
304803
+		 */
304803
+		if (!parse_server_string(LOGOPT_NONE, argv[0], ctxt)) {
304803
+			error(LOGOPT_ANY, MODPREFIX "cannot parse server string");
304803
+			free_context(ctxt);
304803
+			return 1;
304803
+		}
304803
+
304803
+		if (!ctxt->base)
304803
+			ctxt->sdns = defaults_get_searchdns();
304803
+
304803
+		if (!ctxt->server) {
304803
+			struct list_head *uris = defaults_get_uris();
304803
+			if (uris) {
304803
+				validate_uris(uris);
304803
+				if (!list_empty(uris))
304803
+					ctxt->uris = uris;
304803
+				else {
304803
+					error(LOGOPT_ANY, MODPREFIX
304803
+					    "no valid uris found in config list"
304803
+					    ", using default system config");
304803
+					free(uris);
304803
+				}
304803
 			}
304803
 		}
304803
+	} else {
304803
+		char *tmp = conf_amd_get_ldap_base();
304803
+		if (!tmp) {
304803
+			error(LOGOPT_ANY, MODPREFIX "failed to get base dn");
304803
+			free_context(ctxt);
304803
+			return 1;
304803
+		}
304803
+		ctxt->base = tmp;
304803
+
304803
+		tmp = conf_amd_get_ldap_hostports();
304803
+		if (!tmp) {
304803
+			error(LOGOPT_ANY,
304803
+			      MODPREFIX "failed to get ldap_hostports");
304803
+			free_context(ctxt);
304803
+			return 1;
304803
+		}
304803
+
304803
+		/*
304803
+		 * Parse out the server name and port, and save them in
304803
+		 * the proper places in the lookup context structure.
304803
+		 */
304803
+		if (!parse_server_string(LOGOPT_NONE, tmp, ctxt)) {
304803
+			error(LOGOPT_ANY, MODPREFIX "cannot parse server string");
304803
+			free_context(ctxt);
304803
+			return 1;
304803
+		}
304803
+		free(tmp);
304803
+
304803
+		if (!ctxt->server) {
304803
+			error(LOGOPT_ANY, MODPREFIX "ldap_hostports not valid");
304803
+			free_context(ctxt);
304803
+			return 1;
304803
+		}
304803
+
304803
+		tmp = strdup(argv[0]);
304803
+		if (!tmp) {
304803
+			error(LOGOPT_ANY, MODPREFIX "failed to set mapname");
304803
+			free_context(ctxt);
304803
+			return 1;
304803
+		}
304803
+		ctxt->mapname = tmp;
304803
 	}
304803
 
304803
 	/*
304803
@@ -1558,6 +1746,8 @@ int lookup_init(const char *mapfmt, int
304803
 	}
304803
 #endif
304803
 
304803
+	ctxt->timestamp = get_amd_timestamp(ctxt);
304803
+
304803
 	/* Open the parser, if we can. */
304803
 	ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
304803
 	if (!ctxt->parse) {
304803
@@ -2029,7 +2219,7 @@ static int do_paged_query(struct ldap_se
304803
 	if (sp->morePages == TRUE)
304803
 		goto do_paged;
304803
 
304803
-	rv = ldap_search_s(sp->ldap, ctxt->qdn, scope, sp->query, sp->attrs, 0, &sp->result);
304803
+	rv = ldap_search_s(sp->ldap, sp->base, scope, sp->query, sp->attrs, 0, &sp->result);
304803
 	if ((rv != LDAP_SUCCESS) || !sp->result) {
304803
 		/*
304803
  		 * Check for Size Limit exceeded and force run through loop
304803
@@ -2063,7 +2253,7 @@ do_paged:
304803
 
304803
 	/* Search for entries in the directory using the parmeters. */
304803
 	rv = ldap_search_ext_s(sp->ldap,
304803
-			       ctxt->qdn, scope, sp->query, sp->attrs,
304803
+			       sp->base, scope, sp->query, sp->attrs,
304803
 			       0, controls, NULL, NULL, 0, &sp->result);
304803
 	if ((rv != LDAP_SUCCESS) && (rv != LDAP_PARTIAL_RESULTS)) {
304803
 		ldap_control_free(pageControl);
304803
@@ -2364,6 +2554,115 @@ next:
304803
 	return LDAP_SUCCESS;
304803
 }
304803
 
304803
+static int do_get_amd_entries(struct ldap_search_params *sp,
304803
+			      struct map_source *source,
304803
+			      struct lookup_context *ctxt)
304803
+{
304803
+	struct autofs_point *ap = sp->ap;
304803
+	struct mapent_cache *mc = source->mc;
304803
+	struct berval **bvKey;
304803
+	struct berval **bvValues;
304803
+	LDAPMessage *e;
304803
+	char *entry, *value;
304803
+	int rv, ret, count;
304803
+
304803
+	entry = ctxt->schema->entry_attr;
304803
+	value = ctxt->schema->value_attr;
304803
+
304803
+	e = ldap_first_entry(sp->ldap, sp->result);
304803
+	if (!e) {
304803
+		debug(ap->logopt,
304803
+		      MODPREFIX "query succeeded, no matches for %s",
304803
+		      sp->query);
304803
+		ret = ldap_parse_result(sp->ldap, sp->result,
304803
+					&rv, NULL, NULL, NULL, NULL, 0);
304803
+		if (ret == LDAP_SUCCESS)
304803
+			return rv;
304803
+		else
304803
+			return LDAP_OPERATIONS_ERROR;
304803
+	} else
304803
+		debug(ap->logopt, MODPREFIX "examining entries");
304803
+
304803
+	while (e) {
304803
+		char *k_val, *v_val;
304803
+		ber_len_t k_len;
304803
+		char *s_key;
304803
+
304803
+		bvKey = ldap_get_values_len(sp->ldap, e, entry);
304803
+		if (!bvKey || !*bvKey) {
304803
+			e = ldap_next_entry(sp->ldap, e);
304803
+			if (!e) {
304803
+				debug(ap->logopt, MODPREFIX
304803
+				      "failed to get next entry for query %s",
304803
+				      sp->query);
304803
+				ret = ldap_parse_result(sp->ldap,
304803
+							sp->result, &rv,
304803
+							NULL, NULL, NULL, NULL, 0);
304803
+				if (ret == LDAP_SUCCESS)
304803
+					return rv;
304803
+				else
304803
+					return LDAP_OPERATIONS_ERROR;
304803
+			}
304803
+			continue;
304803
+		}
304803
+
304803
+		/* By definition keys should be unique within each map entry */
304803
+		k_val = NULL;
304803
+		k_len = 0;
304803
+
304803
+		count = ldap_count_values_len(bvKey);
304803
+		if (count > 1)
304803
+			warn(ap->logopt, MODPREFIX
304803
+			     "more than one %s, using first", entry);
304803
+
304803
+		k_val = bvKey[0]->bv_val;
304803
+		k_len = bvKey[0]->bv_len;
304803
+
304803
+		bvValues = ldap_get_values_len(sp->ldap, e, value);
304803
+		if (!bvValues || !*bvValues) {
304803
+			debug(ap->logopt,
304803
+			      MODPREFIX "no %s defined for %s",
304803
+			      value, sp->query);
304803
+			goto next;
304803
+		}
304803
+
304803
+		count = ldap_count_values_len(bvValues);
304803
+		if (count > 1)
304803
+			warn(ap->logopt, MODPREFIX
304803
+			     "more than one %s, using first", value);
304803
+
304803
+		v_val = bvValues[0]->bv_val;
304803
+
304803
+		/* Don't fail on "/" in key => type == 0 */
304803
+		s_key = sanitize_path(k_val, k_len, 0, ap->logopt);
304803
+		if (!s_key)
304803
+			goto next;
304803
+
304803
+		cache_writelock(mc);
304803
+		cache_update(mc, source, s_key, v_val, sp->age);
304803
+		cache_unlock(mc);
304803
+
304803
+		free(s_key);
304803
+next:
304803
+		ldap_value_free_len(bvValues);
304803
+		ldap_value_free_len(bvKey);
304803
+		e = ldap_next_entry(sp->ldap, e);
304803
+		if (!e) {
304803
+			debug(ap->logopt, MODPREFIX
304803
+			      "failed to get next entry for query %s",
304803
+			      sp->query);
304803
+			ret = ldap_parse_result(sp->ldap,
304803
+						sp->result, &rv,
304803
+						NULL, NULL, NULL, NULL, 0);
304803
+			if (ret == LDAP_SUCCESS)
304803
+				return rv;
304803
+			else
304803
+				return LDAP_OPERATIONS_ERROR;
304803
+		}
304803
+	}
304803
+
304803
+	return LDAP_SUCCESS;
304803
+}
304803
 
304803
 static int read_one_map(struct autofs_point *ap,
304803
 			struct map_source *source,
304803
@@ -2419,9 +2718,14 @@ static int read_one_map(struct autofs_po
304803
 		return NSS_STATUS_UNAVAIL;
304803
 	}
304803
 
304803
+	if (ctxt->format & MAP_FLAG_FORMAT_AMD)
304803
+		sp.base = ctxt->base;
304803
+	else
304803
+		sp.base = ctxt->qdn;
304803
+
304803
 	/* Look around. */
304803
 	debug(ap->logopt,
304803
-	      MODPREFIX "searching for \"%s\" under \"%s\"", sp.query, ctxt->qdn);
304803
+	      MODPREFIX "searching for \"%s\" under \"%s\"", sp.query, sp.base);
304803
 
304803
 	sp.cookie = NULL;
304803
 	sp.pageSize = 2000;
304803
@@ -2465,7 +2769,10 @@ static int read_one_map(struct autofs_po
304803
 			return NSS_STATUS_UNAVAIL;
304803
 		}
304803
 
304803
-		rv = do_get_entries(&sp, source, ctxt);
304803
+		if (source->flags & MAP_FLAG_FORMAT_AMD)
304803
+			rv = do_get_amd_entries(&sp, source, ctxt);
304803
+		else
304803
+			rv = do_get_entries(&sp, source, ctxt);
304803
 		if (rv != LDAP_SUCCESS) {
304803
 			ldap_msgfree(sp.result);
304803
 			unbind_ldap_connection(ap->logopt, sp.ldap, ctxt);
304803
@@ -2874,6 +3181,219 @@ next:
304803
 	return ret;
304803
 }
304803
 
304803
+static int lookup_one_amd(struct autofs_point *ap,
304803
+			  struct map_source *source,
304803
+			  char *qKey, int qKey_len,
304803
+			  struct lookup_context *ctxt)
304803
+{
304803
+	struct mapent_cache *mc = source->mc;
304803
+	LDAP *ldap;
304803
+	LDAPMessage *result = NULL, *e;
304803
+	char *query;
304803
+	int scope = LDAP_SCOPE_SUBTREE;
304803
+	char *map, *class, *entry, *value;
304803
+	char *attrs[3];
304803
+	struct berval **bvKey;
304803
+	struct berval **bvValues;
304803
+	char buf[MAX_ERR_BUF];
304803
+	time_t age = time(NULL);
304803
+	int rv, l, ql, count;
304803
+	int ret = CHE_MISSING;
304803
+
304803
+	if (ctxt == NULL) {
304803
+		crit(ap->logopt, MODPREFIX "context was NULL");
304803
+		return CHE_FAIL;
304803
+	}
304803
+
304803
+	/* Initialize the LDAP context. */
304803
+	ldap = do_reconnect(ap->logopt, ctxt);
304803
+	if (!ldap)
304803
+		return CHE_UNAVAIL;
304803
+
304803
+	map = ctxt->schema->map_attr;
304803
+	class = ctxt->schema->entry_class;
304803
+	entry = ctxt->schema->entry_attr;
304803
+	value = ctxt->schema->value_attr;
304803
+
304803
+	attrs[0] = entry;
304803
+	attrs[1] = value;
304803
+	attrs[2] = NULL;
304803
+
304803
+	/* Build a query string. */
304803
+	l = strlen(class) +
304803
+	    strlen(map) + strlen(ctxt->mapname) +
304803
+	    strlen(entry) + strlen(qKey) + 24;
304803
+
304803
+	query = malloc(l);
304803
+	if (query == NULL) {
304803
+		char *estr = strerror_r(errno, buf, sizeof(buf));
304803
+		crit(ap->logopt, MODPREFIX "malloc: %s", estr);
304803
+		return CHE_FAIL;
304803
+	}
304803
+
304803
+	/*
304803
+	 * Look for an entry in class under ctxt-base
304803
+	 * whose entry is equal to qKey.
304803
+	 */
304803
+	ql = sprintf(query, "(&(objectclass=%s)(%s=%s)(%s=%s))",
304803
+		     class, map, ctxt->mapname, entry, qKey);
304803
+	if (ql >= l) {
304803
+		error(ap->logopt,
304803
+		      MODPREFIX "error forming query string");
304803
+		free(query);
304803
+		return CHE_FAIL;
304803
+	}
304803
+
304803
+	debug(ap->logopt,
304803
+	      MODPREFIX "searching for \"%s\" under \"%s\"", query, ctxt->base);
304803
+
304803
+	rv = ldap_search_s(ldap, ctxt->base, scope, query, attrs, 0, &result);
304803
+	if ((rv != LDAP_SUCCESS) || !result) {
304803
+		crit(ap->logopt, MODPREFIX "query failed for %s", query);
304803
+		unbind_ldap_connection(ap->logopt, ldap, ctxt);
304803
+		if (result)
304803
+			ldap_msgfree(result);
304803
+		free(query);
304803
+		return CHE_FAIL;
304803
+	}
304803
+
304803
+	debug(ap->logopt,
304803
+	      MODPREFIX "getting first entry for %s=\"%s\"", entry, qKey);
304803
+
304803
+	e = ldap_first_entry(ldap, result);
304803
+	if (!e) {
304803
+		debug(ap->logopt,
304803
+		     MODPREFIX "got answer, but no entry for %s", query);
304803
+		ldap_msgfree(result);
304803
+		unbind_ldap_connection(ap->logopt, ldap, ctxt);
304803
+		free(query);
304803
+		return CHE_MISSING;
304803
+	}
304803
+
304803
+	while (e) {
304803
+		char *k_val, *v_val;
304803
+		ber_len_t k_len;
304803
+		char *s_key;
304803
+
304803
+		bvKey = ldap_get_values_len(ldap, e, entry);
304803
+		if (!bvKey || !*bvKey) {
304803
+			e = ldap_next_entry(ldap, e);
304803
+			continue;
304803
+		}
304803
+
304803
+		/* By definition keys should be unique within each map entry */
304803
+		k_val = NULL;
304803
+		k_len = 0;
304803
+
304803
+		count = ldap_count_values_len(bvKey);
304803
+		if (count > 1)
304803
+			warn(ap->logopt, MODPREFIX
304803
+			     "more than one %s, using first", entry);
304803
+
304803
+		k_val = bvKey[0]->bv_val;
304803
+		k_len = bvKey[0]->bv_len;
304803
+
304803
+		debug(ap->logopt, MODPREFIX "examining first entry");
304803
+
304803
+		bvValues = ldap_get_values_len(ldap, e, value);
304803
+		if (!bvValues || !*bvValues) {
304803
+			debug(ap->logopt,
304803
+			      MODPREFIX "no %s defined for %s", value, query);
304803
+			goto next;
304803
+		}
304803
+
304803
+		count = ldap_count_values_len(bvValues);
304803
+		if (count > 1)
304803
+			warn(ap->logopt, MODPREFIX
304803
+			     "more than one %s, using first", value);
304803
+
304803
+		/* There should be one value for a key, use first value */
304803
+		v_val = bvValues[0]->bv_val;
304803
+
304803
+		/* Don't fail on "/" in key => type == 0 */
304803
+		s_key = sanitize_path(k_val, k_len, 0, ap->logopt);
304803
+		if (!s_key)
304803
+			goto next;
304803
+
304803
+		cache_writelock(mc);
304803
+		ret = cache_update(mc, source, s_key, v_val, age);
304803
+		cache_unlock(mc);
304803
+
304803
+		free(s_key);
304803
+next:
304803
+		ldap_value_free_len(bvValues);
304803
+		ldap_value_free_len(bvKey);
304803
+		e = ldap_next_entry(ldap, e);
304803
+	}
304803
+
304803
+	ldap_msgfree(result);
304803
+	unbind_ldap_connection(ap->logopt, ldap, ctxt);
304803
+	free(query);
304803
+
304803
+	return ret;
304803
+}
304803
+
304803
+static int match_key(struct autofs_point *ap,
304803
+		     struct map_source *source,
304803
+		     char *key, int key_len,
304803
+		     struct lookup_context *ctxt)
304803
+{
304803
+	unsigned int is_amd_format = source->flags & MAP_FLAG_FORMAT_AMD;
304803
+	char buf[MAX_ERR_BUF];
304803
+	char *lkp_key;
304803
+	char *prefix;
304803
+	int ret;
304803
+
304803
+	if (is_amd_format)
304803
+		ret = lookup_one_amd(ap, source, key, key_len, ctxt);
304803
+	else
304803
+		ret = lookup_one(ap, source, key, key_len, ctxt);
304803
+
304803
+	if (ret == CHE_OK || ret == CHE_UPDATED)
304803
+		return ret;
304803
+
304803
+	if (!is_amd_format)
304803
+		return CHE_FAIL;
304803
+
304803
+	lkp_key = strdup(key);
304803
+	if (!lkp_key) {
304803
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
+		error(ap->logopt, MODPREFIX "strdup: %s", estr);
304803
+		return CHE_FAIL;
304803
+	}
304803
+
304803
+	ret = CHE_MISSING;
304803
+
304803
+	/*
304803
+	 * Now strip successive directory components and try a
304803
+	 * match against map entries ending with a wildcard and
304803
+	 * finally try the wilcard entry itself.
304803
+	 */
304803
+	while ((prefix = strrchr(lkp_key, '/'))) {
304803
+		char *match;
304803
+		size_t len;
304803
+		*prefix = '\0';
304803
+		len = strlen(lkp_key + 3);
304803
+		match = malloc(len);
304803
+		if (!match) {
304803
+			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
+			error(ap->logopt, MODPREFIX "malloc: %s", estr);
304803
+			ret = CHE_FAIL;
304803
+			goto done;
304803
+		}
304803
+		len--;
304803
+		strcpy(match, lkp_key);
304803
+		strcat(match, "/*");
304803
+		ret = lookup_one_amd(ap, source, match, len, ctxt);
304803
+		free(match);
304803
+		if (ret == CHE_OK || ret == CHE_UPDATED)
304803
+			goto done;
304803
+	}
304803
+done:
304803
+	free(lkp_key);
304803
+	return ret;
304803
+}
304803
+
304803
 static int check_map_indirect(struct autofs_point *ap,
304803
 			      struct map_source *source,
304803
 			      char *key, int key_len,
304803
@@ -2888,16 +3408,43 @@ static int check_map_indirect(struct aut
304803
 	mc = source->mc;
304803
 
304803
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
304803
-	ret = lookup_one(ap, source, key, key_len, ctxt);
304803
+
304803
+	pthread_mutex_lock(&ap->entry->current_mutex);
304803
+	if (source->flags & MAP_FLAG_FORMAT_AMD) {
304803
+		unsigned long timestamp = get_amd_timestamp(ctxt);
304803
+		if (timestamp > ctxt->timestamp) {
304803
+			ctxt->timestamp = timestamp;
304803
+			source->stale = 1;
304803
+			ctxt->check_defaults = 1;
304803
+		}
304803
+
304803
+		if (ctxt->check_defaults) {
304803
+			/* Check for a /defaults entry */
304803
+			ret = lookup_one_amd(ap, source, "/defaults", 9, ctxt);
304803
+			if (ret == CHE_FAIL) {
304803
+				warn(ap->logopt, MODPREFIX
304803
+				     "error getting /defaults from map %s",
304803
+				     ctxt->mapname);
304803
+			} else
304803
+				ctxt->check_defaults = 0;
304803
+		}
304803
+	}
304803
+	pthread_mutex_unlock(&ap->entry->current_mutex);
304803
+
304803
+	ret = match_key(ap, source, key, key_len, ctxt);
304803
 	if (ret == CHE_FAIL) {
304803
 		pthread_setcancelstate(cur_state, NULL);
304803
 		return NSS_STATUS_NOTFOUND;
304803
 	} else if (ret == CHE_UNAVAIL) {
304803
+		struct mapent *exists;
304803
 		/*
304803
 		 * If the server is down and the entry exists in the cache
304803
 		 * and belongs to this map return success and use the entry.
304803
 		 */
304803
-		struct mapent *exists = cache_lookup(mc, key);
304803
+		if (source->flags & MAP_FLAG_FORMAT_AMD)
304803
+			exists = match_cached_key(ap, MODPREFIX, source, key);
304803
+		else
304803
+			exists = cache_lookup(mc, key);
304803
 		if (exists && exists->source == source) {
304803
 			pthread_setcancelstate(cur_state, NULL);
304803
 			return NSS_STATUS_SUCCESS;
304803
@@ -2910,24 +3457,28 @@ static int check_map_indirect(struct aut
304803
 	}
304803
 	pthread_setcancelstate(cur_state, NULL);
304803
 
304803
-	/*
304803
-	 * Check for map change and update as needed for
304803
-	 * following cache lookup.
304803
-	 */
304803
-	cache_readlock(mc);
304803
-	t_last_read = ap->exp_runfreq + 1;
304803
-	me = cache_lookup_first(mc);
304803
-	while (me) {
304803
-		if (me->source == source) {
304803
-			t_last_read = now - me->age;
304803
-			break;
304803
+	if (!(source->flags & MAP_FLAG_FORMAT_AMD)) {
304803
+		/*
304803
+		 * Check for map change and update as needed for
304803
+		 * following cache lookup.
304803
+		 */
304803
+		cache_readlock(mc);
304803
+		t_last_read = ap->exp_runfreq + 1;
304803
+		me = cache_lookup_first(mc);
304803
+		while (me) {
304803
+			if (me->source == source) {
304803
+				t_last_read = now - me->age;
304803
+				break;
304803
+			}
304803
+			me = cache_lookup_next(mc, me);
304803
 		}
304803
-		me = cache_lookup_next(mc, me);
304803
-	}
304803
-	cache_unlock(mc);
304803
+		cache_unlock(mc);
304803
 
304803
-	if (t_last_read > ap->exp_runfreq && ret & CHE_UPDATED)
304803
-		source->stale = 1;
304803
+		pthread_mutex_lock(&ap->entry->current_mutex);
304803
+		if (t_last_read > ap->exp_runfreq && ret & CHE_UPDATED)
304803
+			source->stale = 1;
304803
+		pthread_mutex_unlock(&ap->entry->current_mutex);
304803
+	}
304803
 
304803
 	cache_readlock(mc);
304803
 	me = cache_lookup_distinct(mc, "*");
304803
@@ -2948,8 +3499,10 @@ int lookup_mount(struct autofs_point *ap
304803
 	struct mapent *me;
304803
 	char key[KEY_MAX_LEN + 1];
304803
 	int key_len;
304803
+	char *lkp_key;
304803
 	char *mapent = NULL;
304803
 	char mapent_buf[MAPENT_MAX_LEN + 1];
304803
+	char buf[MAX_ERR_BUF];
304803
 	int status = 0;
304803
 	int ret = 1;
304803
 
304803
@@ -2961,9 +3514,18 @@ int lookup_mount(struct autofs_point *ap
304803
 
304803
 	debug(ap->logopt, MODPREFIX "looking up %s", name);
304803
 
304803
-	key_len = snprintf(key, KEY_MAX_LEN + 1, "%s", name);
304803
-	if (key_len > KEY_MAX_LEN)
304803
-		return NSS_STATUS_NOTFOUND;
304803
+	if (!(source->flags & MAP_FLAG_FORMAT_AMD)) {
304803
+		key_len = snprintf(key, KEY_MAX_LEN + 1, "%s", name);
304803
+		if (key_len > KEY_MAX_LEN)
304803
+			return NSS_STATUS_NOTFOUND;
304803
+	} else {
304803
+		key_len = expandamdent(name, NULL, NULL);
304803
+		if (key_len > KEY_MAX_LEN)
304803
+			return NSS_STATUS_NOTFOUND;
304803
+		expandamdent(name, key, NULL);
304803
+		key[key_len] = '\0';
304803
+		debug(ap->logopt, MODPREFIX "expanded key: \"%s\"", key);
304803
+	}
304803
 
304803
 	/* Check if we recorded a mount fail for this key anywhere */
304803
 	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
304803
@@ -2997,18 +3559,26 @@ int lookup_mount(struct autofs_point *ap
304803
 	 * we never know about it.
304803
 	 */
304803
 	if (ap->type == LKP_INDIRECT && *key != '/') {
304803
-		char *lkp_key;
304803
-
304803
 		cache_readlock(mc);
304803
 		me = cache_lookup_distinct(mc, key);
304803
 		if (me && me->multi)
304803
 			lkp_key = strdup(me->multi->key);
304803
-		else
304803
+		else if (!ap->pref)
304803
 			lkp_key = strdup(key);
304803
+		else {
304803
+			lkp_key = malloc(strlen(ap->pref) + strlen(key) + 1);
304803
+			if (lkp_key) {
304803
+				strcpy(lkp_key, ap->pref);
304803
+				strcat(lkp_key, key);
304803
+			}
304803
+		}
304803
 		cache_unlock(mc);
304803
 
304803
-		if (!lkp_key)
304803
+		if (!lkp_key) {
304803
+			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
+			error(ap->logopt, MODPREFIX "malloc: %s", estr);
304803
 			return NSS_STATUS_UNKNOWN;
304803
+		}
304803
 
304803
 		status = check_map_indirect(ap, source,
304803
 					    lkp_key, strlen(lkp_key), ctxt);
304803
@@ -3029,7 +3599,25 @@ int lookup_mount(struct autofs_point *ap
304803
 		cache_readlock(mc);
304803
 	else
304803
 		cache_writelock(mc);
304803
-	me = cache_lookup(mc, key);
304803
+
304803
+	if (!ap->pref)
304803
+		lkp_key = strdup(key);
304803
+	else {
304803
+		lkp_key = malloc(strlen(ap->pref) + strlen(key) + 1);
304803
+		if (lkp_key) {
304803
+			strcpy(lkp_key, ap->pref);
304803
+			strcat(lkp_key, key);
304803
+		}
304803
+	}
304803
+
304803
+	if (!lkp_key) {
304803
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
+		error(ap->logopt, MODPREFIX "malloc: %s", estr);
304803
+		cache_unlock(mc);
304803
+		return NSS_STATUS_UNKNOWN;
304803
+	}
304803
+
304803
+	me = match_cached_key(ap, MODPREFIX, source, lkp_key);
304803
 	/* Stale mapent => check for entry in alternate source or wildcard */
304803
 	if (me && !me->mapent) {
304803
 		while ((me = cache_lookup_key_next(me)))
304803
@@ -3055,6 +3643,7 @@ int lookup_mount(struct autofs_point *ap
304803
 		}
304803
 	}
304803
 	cache_unlock(mc);
304803
+	free(lkp_key);
304803
 
304803
 	if (!mapent)
304803
 		return NSS_STATUS_TRYAGAIN;