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

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