Blame SOURCES/autofs-5.0.9-amd-lookup-update-lookup-file-to-handle-amd-keys.patch

6bbd11
autofs-5.0.9 - amd lookup update lookup file to handle amd keys
6bbd11
6bbd11
From: Ian Kent <raven@themaw.net>
6bbd11
6bbd11
Update file map lookup to handle map key matching for amd format
6bbd11
maps.
6bbd11
---
6bbd11
 modules/lookup_file.c |  287 +++++++++++++++++++++++++++++++++++--------------
6bbd11
 1 file changed, 206 insertions(+), 81 deletions(-)
6bbd11
6bbd11
diff --git a/modules/lookup_file.c b/modules/lookup_file.c
6bbd11
index 512e3ef..7c982c6 100644
6bbd11
--- a/modules/lookup_file.c
6bbd11
+++ b/modules/lookup_file.c
6bbd11
@@ -702,9 +702,22 @@ int lookup_read_map(struct autofs_point *ap, time_t age, void *context)
6bbd11
 		} else {
6bbd11
 			char *s_key; 
6bbd11
 
6bbd11
-			s_key = sanitize_path(key, k_len, ap->type, ap->logopt);
6bbd11
-			if (!s_key)
6bbd11
-				continue;
6bbd11
+			if (source->flags & MAP_FLAG_FORMAT_AMD) {
6bbd11
+				if (!strcmp(key, "/defaults")) {
6bbd11
+					cache_writelock(mc);
6bbd11
+					cache_update(mc, source, key, mapent, age);
6bbd11
+					cache_unlock(mc);
6bbd11
+					continue;
6bbd11
+				}
6bbd11
+				/* Don't fail on "/" in key => type == 0 */
6bbd11
+				s_key = sanitize_path(key, k_len, 0, ap->logopt);
6bbd11
+				if (!s_key)
6bbd11
+					continue;
6bbd11
+			} else {
6bbd11
+				s_key = sanitize_path(key, k_len, ap->type, ap->logopt);
6bbd11
+				if (!s_key)
6bbd11
+					continue;
6bbd11
+			}
6bbd11
 
6bbd11
 			cache_writelock(mc);
6bbd11
 			cache_update(mc, source, s_key, mapent, age);
6bbd11
@@ -724,12 +737,75 @@ int lookup_read_map(struct autofs_point *ap, time_t age, void *context)
6bbd11
 	return NSS_STATUS_SUCCESS;
6bbd11
 }
6bbd11
 
6bbd11
+static int match_key(struct autofs_point *ap,
6bbd11
+		     struct map_source *source, char *map_key,
6bbd11
+		     const char *key, size_t key_len, const char *mapent)
6bbd11
+{
6bbd11
+	char buf[MAX_ERR_BUF];
6bbd11
+	struct mapent_cache *mc;
6bbd11
+	time_t age = time(NULL);
6bbd11
+	char *lkp_key;
6bbd11
+	char *prefix;
6bbd11
+	size_t map_key_len;
6bbd11
+	int ret, eq;
6bbd11
+
6bbd11
+	mc = source->mc;
6bbd11
+
6bbd11
+	/* exact match is a match for both autofs and amd */
6bbd11
+	eq = strcmp(map_key, key);
6bbd11
+	if (eq == 0) {
6bbd11
+		cache_writelock(mc);
6bbd11
+		ret = cache_update(mc, source, key, mapent, age);
6bbd11
+		cache_unlock(mc);
6bbd11
+		return ret;
6bbd11
+	}
6bbd11
+
6bbd11
+	if (!(source->flags & MAP_FLAG_FORMAT_AMD))
6bbd11
+		return CHE_FAIL;
6bbd11
+
6bbd11
+	map_key_len = strlen(map_key);
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_FAIL;
6bbd11
+
6bbd11
+	if (map_key_len > (strlen(lkp_key) + 2))
6bbd11
+		goto done;
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. If we get a match
6bbd11
+	 * then update the cache with the read key and its mapent.
6bbd11
+	*/
6bbd11
+	while ((prefix = strrchr(lkp_key, '/'))) {
6bbd11
+		size_t len;
6bbd11
+		*prefix = '\0';
6bbd11
+		len = strlen(lkp_key);
6bbd11
+		eq = strncmp(map_key, lkp_key, len);
6bbd11
+		if (!eq && map_key[len + 1] == '*') {
6bbd11
+			cache_writelock(mc);
6bbd11
+			ret = cache_update(mc, source, map_key, mapent, age);
6bbd11
+			cache_unlock(mc);
6bbd11
+			goto done;
6bbd11
+		}
6bbd11
+	}
6bbd11
+done:
6bbd11
+	free(lkp_key);
6bbd11
+	return ret;
6bbd11
+}
6bbd11
+
6bbd11
 static int lookup_one(struct autofs_point *ap,
6bbd11
 		      struct map_source *source,
6bbd11
 		      const char *key, int key_len,
6bbd11
 		      struct lookup_context *ctxt)
6bbd11
 {
6bbd11
-	struct mapent_cache *mc;
6bbd11
+	struct mapent_cache *mc = source->mc;
6bbd11
 	char mkey[KEY_MAX_LEN + 1];
6bbd11
 	char mapent[MAPENT_MAX_LEN + 1];
6bbd11
 	time_t age = time(NULL);
6bbd11
@@ -737,8 +813,6 @@ static int lookup_one(struct autofs_point *ap,
6bbd11
 	unsigned int k_len, m_len;
6bbd11
 	int entry, ret;
6bbd11
 
6bbd11
-	mc = source->mc;
6bbd11
-
6bbd11
 	f = open_fopen_r(ctxt->mapname);
6bbd11
 	if (!f) {
6bbd11
 		error(ap->logopt,
6bbd11
@@ -781,29 +855,38 @@ static int lookup_one(struct autofs_point *ap,
6bbd11
 				}
6bbd11
 			} else {
6bbd11
 				char *s_key; 
6bbd11
-				int eq;
6bbd11
 
6bbd11
-				s_key = sanitize_path(mkey, k_len, ap->type, ap->logopt);
6bbd11
-				if (!s_key)
6bbd11
-					continue;
6bbd11
+				if (source->flags & MAP_FLAG_FORMAT_AMD) {
6bbd11
+					if (!strcmp(mkey, "/defaults")) {
6bbd11
+						cache_writelock(mc);
6bbd11
+						cache_update(mc, source, mkey, mapent, age);
6bbd11
+						cache_unlock(mc);
6bbd11
+						continue;
6bbd11
+					}
6bbd11
+					/* Don't fail on "/" in key => type == 0 */
6bbd11
+					s_key = sanitize_path(mkey, k_len, 0, ap->logopt);
6bbd11
+					if (!s_key)
6bbd11
+						continue;
6bbd11
+				} else {
6bbd11
+					s_key = sanitize_path(mkey, k_len, ap->type, ap->logopt);
6bbd11
+					if (!s_key)
6bbd11
+						continue;
6bbd11
 
6bbd11
-				if (key_len != strlen(s_key)) {
6bbd11
-					free(s_key);
6bbd11
-					continue;
6bbd11
+					if (key_len != strlen(s_key)) {
6bbd11
+						free(s_key);
6bbd11
+						continue;
6bbd11
+					}
6bbd11
 				}
6bbd11
 
6bbd11
-				eq = strncmp(s_key, key, key_len);
6bbd11
-				if (eq != 0) {
6bbd11
+				ret = match_key(ap, source,
6bbd11
+						s_key, key, key_len, mapent);
6bbd11
+				if (ret == CHE_FAIL) {
6bbd11
 					free(s_key);
6bbd11
 					continue;
6bbd11
 				}
6bbd11
 
6bbd11
 				free(s_key);
6bbd11
 
6bbd11
-				cache_writelock(mc);
6bbd11
-				ret = cache_update(mc, source, key, mapent, age);
6bbd11
-				cache_unlock(mc);
6bbd11
-
6bbd11
 				fclose(f);
6bbd11
 
6bbd11
 				return ret;
6bbd11
@@ -897,7 +980,10 @@ static int check_map_indirect(struct autofs_point *ap,
6bbd11
 		return NSS_STATUS_NOTFOUND;
6bbd11
 
6bbd11
 	cache_writelock(mc);
6bbd11
-	exists = cache_lookup_distinct(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_distinct(mc, key);
6bbd11
 	/* Not found in the map but found in the cache */
6bbd11
 	if (exists && exists->source == source && ret & CHE_MISSING) {
6bbd11
 		if (exists->mapent) {
6bbd11
@@ -936,6 +1022,53 @@ static int check_map_indirect(struct autofs_point *ap,
6bbd11
 	return NSS_STATUS_SUCCESS;
6bbd11
 }
6bbd11
 
6bbd11
+static int map_update_needed(struct autofs_point *ap,
6bbd11
+			     struct map_source *source,
6bbd11
+			     struct lookup_context * ctxt)
6bbd11
+{
6bbd11
+	struct mapent_cache *mc;
6bbd11
+	struct mapent *me;
6bbd11
+	struct stat st;
6bbd11
+	int ret = 1;
6bbd11
+
6bbd11
+	mc = source->mc;
6bbd11
+
6bbd11
+	/*
6bbd11
+	 * We can skip the map lookup and cache update altogether
6bbd11
+	 * if we know the map hasn't been modified since it was
6bbd11
+	 * last read. If it has then we can mark the map stale
6bbd11
+	 * so a re-read is triggered following the lookup.
6bbd11
+	 */
6bbd11
+	if (stat(ctxt->mapname, &st)) {
6bbd11
+		error(ap->logopt, MODPREFIX
6bbd11
+		      "file map %s, could not stat", ctxt->mapname);
6bbd11
+		return -1;
6bbd11
+	}
6bbd11
+
6bbd11
+	cache_readlock(mc);
6bbd11
+	me = cache_lookup_first(mc);
6bbd11
+	if (me && st.st_mtime <= me->age) {
6bbd11
+		/*
6bbd11
+		 * If any map instances are present for this source
6bbd11
+		 * then either we have plus included entries or we
6bbd11
+		 * are looking through the list of nsswitch sources.
6bbd11
+		 * In either case, or if it's a "multi" source, we
6bbd11
+		 * cannot avoid reading through the map because we
6bbd11
+		 * must preserve the key order over multiple sources
6bbd11
+		 * or maps. But also, we can't know, at this point,
6bbd11
+		 * if a source instance has been changed since the
6bbd11
+		 * last time we checked it.
6bbd11
+		 */
6bbd11
+		if (!source->instance &&
6bbd11
+		    source->type && strcmp(source->type, "multi"))
6bbd11
+			ret = 0;
6bbd11
+	} else
6bbd11
+		source->stale = 1;
6bbd11
+	cache_unlock(mc);
6bbd11
+
6bbd11
+	return ret;
6bbd11
+}
6bbd11
+
6bbd11
 int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *context)
6bbd11
 {
6bbd11
 	struct lookup_context *ctxt = (struct lookup_context *) context;
6bbd11
@@ -944,8 +1077,10 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
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
@@ -967,9 +1102,18 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
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
+		memset(key, 0, KEY_MAX_LEN + 1);
6bbd11
+		expandamdent(name, key, NULL);
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
@@ -1003,50 +1147,35 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
6bbd11
 	 * we never know about it.
6bbd11
 	 */
6bbd11
 	if (ap->type == LKP_INDIRECT && *key != '/') {
6bbd11
-		struct stat st;
6bbd11
-		char *lkp_key;
6bbd11
+		int ret;
6bbd11
 
6bbd11
-		/*
6bbd11
-		 * We can skip the map lookup and cache update altogether
6bbd11
-		 * if we know the map hasn't been modified since it was
6bbd11
-		 * last read. If it has then we can mark the map stale
6bbd11
-		 * so a re-read is triggered following the lookup.
6bbd11
-		 */
6bbd11
-		if (stat(ctxt->mapname, &st)) {
6bbd11
-			error(ap->logopt, MODPREFIX
6bbd11
-			      "file map %s, could not stat", ctxt->mapname);
6bbd11
-			return NSS_STATUS_UNAVAIL;
6bbd11
-		}
6bbd11
+		ret = map_update_needed(ap, source, ctxt);
6bbd11
+		if (!ret)
6bbd11
+			goto do_cache_lookup;
6bbd11
+		/* Map isn't accessable, just try the cache */
6bbd11
+		if (ret < 0)
6bbd11
+			goto do_cache_lookup;
6bbd11
 
6bbd11
 		cache_readlock(mc);
6bbd11
-		me = cache_lookup_first(mc);
6bbd11
-		if (me && st.st_mtime <= me->age) {
6bbd11
-			/*
6bbd11
-			 * If any map instances are present for this source
6bbd11
-			 * then either we have plus included entries or we
6bbd11
-			 * are looking through the list of nsswitch sources.
6bbd11
-			 * In either case, or if it's a "multi" source, we
6bbd11
-			 * cannot avoid reading through the map because we
6bbd11
-			 * must preserve the key order over multiple sources
6bbd11
-			 * or maps. But also, we can't know, at this point,
6bbd11
-			 * if a source instance has been changed since the
6bbd11
-			 * last time we checked it.
6bbd11
-			 */
6bbd11
-			if (!source->instance &&
6bbd11
-			    source->type && strcmp(source->type, "multi"))
6bbd11
-				goto do_cache_lookup;
6bbd11
-		} else
6bbd11
-			source->stale = 1;
6bbd11
-
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
@@ -1066,42 +1195,38 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
6bbd11
 	 * when we're starting up so just take the readlock in that
6bbd11
 	 * case.
6bbd11
 	 */
6bbd11
+do_cache_lookup:
6bbd11
 	if (ap->flags & MOUNT_FLAG_REMOUNT)
6bbd11
 		cache_readlock(mc);
6bbd11
 	else
6bbd11
 		cache_writelock(mc);
6bbd11
-do_cache_lookup:
6bbd11
-	me = cache_lookup(mc, key);
6bbd11
-	/*
6bbd11
-	 * Stale mapent => check for entry in alternate source or wildcard.
6bbd11
-	 * Note, plus included direct mount map entries are included as an
6bbd11
-	 * instance (same map entry cache), not in a distinct source.
6bbd11
-	 */
6bbd11
-	if (me && (!me->mapent || 
6bbd11
-	   (me->source != source && *me->key != '/'))) {
6bbd11
-		while ((me = cache_lookup_key_next(me)))
6bbd11
-			if (me->source == source)
6bbd11
-				break;
6bbd11
-		if (!me)
6bbd11
-			me = cache_lookup_distinct(mc, "*");
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
 	if (me && me->mapent) {
6bbd11
-		/*
6bbd11
-		 * If this is a lookup add wildcard match for later validation
6bbd11
-		 * checks and negative cache lookups.
6bbd11
-		 */
6bbd11
-		if (!(ap->flags & MOUNT_FLAG_REMOUNT) &&
6bbd11
-		    ap->type == LKP_INDIRECT && *me->key == '*') {
6bbd11
-			ret = cache_update(mc, source, key, me->mapent, me->age);
6bbd11
-			if (!(ret & (CHE_OK | CHE_UPDATED)))
6bbd11
-				me = NULL;
6bbd11
-		}
6bbd11
 		if (me && (me->source == source || *me->key == '/')) {
6bbd11
 			strcpy(mapent_buf, me->mapent);
6bbd11
 			mapent = mapent_buf;
6bbd11
 		}
6bbd11
 	}
6bbd11
 	cache_unlock(mc);
6bbd11
+	free(lkp_key);
6bbd11
 
6bbd11
 	if (!me)
6bbd11
 		return NSS_STATUS_NOTFOUND;