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

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