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

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