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

6bbd11
autofs-5.0.8 - amd lookup update lookup hesiod to handle amd keys
6bbd11
6bbd11
From: Ian Kent <raven@themaw.net>
6bbd11
6bbd11
Warning, this is completely untested.
6bbd11
6bbd11
I don't have a hesiod test environment so I can't test this at all.
6bbd11
If we do in fact have hesiod users then I'll need to work with them
6bbd11
to fix any reported problems.
6bbd11
---
6bbd11
 modules/lookup_hesiod.c |  330 +++++++++++++++++++++++++++++++++++++++---------
6bbd11
 1 file changed, 270 insertions(+), 60 deletions(-)
6bbd11
6bbd11
--- autofs-5.0.7.orig/modules/lookup_hesiod.c
6bbd11
+++ autofs-5.0.7/modules/lookup_hesiod.c
6bbd11
@@ -20,12 +20,15 @@
6bbd11
 #include "automount.h"
6bbd11
 #include "nsswitch.h"
6bbd11
 
6bbd11
-#define MAPFMT_DEFAULT "hesiod"
6bbd11
+#define MAPFMT_DEFAULT	   "hesiod"
6bbd11
+#define AMD_MAP_PREFIX	   "hesiod."
6bbd11
+#define AMD_MAP_PREFIX_LEN 7
6bbd11
 
6bbd11
 #define MODPREFIX "lookup(hesiod): "
6bbd11
 #define HESIOD_LEN 512
6bbd11
 
6bbd11
 struct lookup_context {
6bbd11
+	const char *mapname;
6bbd11
 	struct parse_mod *parser;
6bbd11
 	void *hesiod_context;
6bbd11
 };
6bbd11
@@ -50,6 +53,7 @@ int lookup_init(const char *mapfmt, int
6bbd11
 		logerr(MODPREFIX "malloc: %s", estr);
6bbd11
 		return 1;
6bbd11
 	}
6bbd11
+	memset(ctxt, 0, sizeof(struct lookup_context));
6bbd11
 
6bbd11
 	/* Initialize the resolver. */
6bbd11
 	res_init();
6bbd11
@@ -66,6 +70,20 @@ int lookup_init(const char *mapfmt, int
6bbd11
 	if (!mapfmt)
6bbd11
 		mapfmt = MAPFMT_DEFAULT;
6bbd11
 
6bbd11
+	if (!strcmp(mapfmt, "amd")) {
6bbd11
+		/* amd formated hesiod maps have a map name */
6bbd11
+		const char *mapname = argv[0];
6bbd11
+		if (strncmp(mapname, AMD_MAP_PREFIX, AMD_MAP_PREFIX_LEN)) {
6bbd11
+			logerr(MODPREFIX
6bbd11
+			      "incorrect prefix for hesiod map %s", mapname);
6bbd11
+			free(ctxt);
6bbd11
+			return 1;
6bbd11
+		}
6bbd11
+		ctxt->mapname = mapname;
6bbd11
+		argc--;
6bbd11
+		argv++;
6bbd11
+	}
6bbd11
+
6bbd11
 	/* Open the parser, if we can. */
6bbd11
 	ctxt->parser = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
6bbd11
 	if (!ctxt->parser) {
6bbd11
@@ -97,16 +115,203 @@ int lookup_read_map(struct autofs_point
6bbd11
  * it's an ERR filesystem, it's an error message we should log.  Otherwise,
6bbd11
  * assume it's something we know how to deal with already (generic).
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
+	char **hes_result;
6bbd11
+	char **record, *best_record = NULL, *p;
6bbd11
+	int priority, lowest_priority = INT_MAX;
6bbd11
+	int ret, status;
6bbd11
+
6bbd11
+	mc = source->mc;
6bbd11
+
6bbd11
+	status = pthread_mutex_lock(&hesiod_mutex);
6bbd11
+	if (status)
6bbd11
+		fatal(status);
6bbd11
+
6bbd11
+	hes_result = hesiod_resolve(ctxt->hesiod_context, key, "filsys");
6bbd11
+	if (!hes_result || !hes_result[0]) {
6bbd11
+		int err = errno;
6bbd11
+		error(ap->logopt,
6bbd11
+		      MODPREFIX "key \"%s\" not found in map", key);
6bbd11
+		status = pthread_mutex_unlock(&hesiod_mutex);
6bbd11
+		if (status)
6bbd11
+			fatal(status);
6bbd11
+		if (err == HES_ER_NOTFOUND)
6bbd11
+			return CHE_MISSING;
6bbd11
+		else
6bbd11
+			return CHE_FAIL;
6bbd11
+	}
6bbd11
+
6bbd11
+	/* autofs doesn't support falling back to alternate records, so just
6bbd11
+	   find the record with the lowest priority and hope it works.
6bbd11
+	   -- Aaron Ucko <amu@alum.mit.edu> 2002-03-11 */
6bbd11
+	for (record = hes_result; *record; ++record) {
6bbd11
+	    p = strrchr(*record, ' ');
6bbd11
+	    if ( p && isdigit(p[1]) ) {
6bbd11
+		priority = atoi(p+1);
6bbd11
+	    } else {
6bbd11
+		priority = INT_MAX - 1;
6bbd11
+	    }
6bbd11
+	    if (priority < lowest_priority) {
6bbd11
+		lowest_priority = priority;
6bbd11
+		best_record = *record;
6bbd11
+	    }
6bbd11
+	}
6bbd11
+
6bbd11
+	cache_writelock(mc);
6bbd11
+	ret = cache_update(mc, source, key, best_record, time(NULL));
6bbd11
+	cache_unlock(mc);
6bbd11
+	if (ret == CHE_FAIL) {
6bbd11
+		hesiod_free_list(ctxt->hesiod_context, hes_result);
6bbd11
+		status = pthread_mutex_unlock(&hesiod_mutex);
6bbd11
+		if (status)
6bbd11
+			fatal(status);
6bbd11
+		return ret;
6bbd11
+	}
6bbd11
+
6bbd11
+	debug(ap->logopt,
6bbd11
+	      MODPREFIX "lookup for \"%s\" gave \"%s\"",
6bbd11
+	      key, best_record);
6bbd11
+
6bbd11
+	hesiod_free_list(ctxt->hesiod_context, hes_result);
6bbd11
+
6bbd11
+	status = pthread_mutex_unlock(&hesiod_mutex);
6bbd11
+	if (status)
6bbd11
+		fatal(status);
6bbd11
+
6bbd11
+	return ret;
6bbd11
+}
6bbd11
+
6bbd11
+static int lookup_one_amd(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
+	char *hesiod_base;
6bbd11
+	char **hes_result;
6bbd11
+	char *lkp_key;
6bbd11
+	int status, ret;
6bbd11
+
6bbd11
+	mc = source->mc;
6bbd11
+
6bbd11
+	hesiod_base = conf_amd_get_hesiod_base();
6bbd11
+	if (!hesiod_base)
6bbd11
+		return CHE_FAIL;
6bbd11
+
6bbd11
+	lkp_key = malloc(key_len + strlen(ctxt->mapname) - 7 + 2);
6bbd11
+	if (!lkp_key) {
6bbd11
+		free(hesiod_base);
6bbd11
+		return CHE_FAIL;
6bbd11
+	}
6bbd11
+
6bbd11
+	strcpy(lkp_key, key);
6bbd11
+	strcat(lkp_key, ".");
6bbd11
+	strcat(lkp_key, ctxt->mapname + AMD_MAP_PREFIX_LEN);
6bbd11
+
6bbd11
+	status = pthread_mutex_lock(&hesiod_mutex);
6bbd11
+	if (status)
6bbd11
+		fatal(status);
6bbd11
+
6bbd11
+	hes_result = hesiod_resolve(ctxt->hesiod_context, lkp_key, hesiod_base);
6bbd11
+	if (!hes_result || !hes_result[0]) {
6bbd11
+		int err = errno;
6bbd11
+		if (err == HES_ER_NOTFOUND)
6bbd11
+			ret = CHE_MISSING;
6bbd11
+		else
6bbd11
+			ret = CHE_FAIL;
6bbd11
+		goto done;
6bbd11
+	}
6bbd11
+
6bbd11
+	cache_writelock(mc);
6bbd11
+	ret = cache_update(mc, source, lkp_key, *hes_result, time(NULL));
6bbd11
+	cache_unlock(mc);
6bbd11
+
6bbd11
+	if (hes_result)
6bbd11
+		hesiod_free_list(ctxt->hesiod_context, hes_result);
6bbd11
+done:
6bbd11
+	free(lkp_key);
6bbd11
+
6bbd11
+	status = pthread_mutex_unlock(&hesiod_mutex);
6bbd11
+	if (status)
6bbd11
+		fatal(status);
6bbd11
+
6bbd11
+	return ret;
6bbd11
+}
6bbd11
+
6bbd11
+static int match_amd_key(struct autofs_point *ap,
6bbd11
+			 struct map_source *source,
6bbd11
+			 const char *key, int key_len,
6bbd11
+			 struct lookup_context *ctxt)
6bbd11
+{
6bbd11
+	char buf[MAX_ERR_BUF];
6bbd11
+	char *lkp_key;
6bbd11
+	char *prefix;
6bbd11
+	int ret;
6bbd11
+
6bbd11
+	ret = lookup_one_amd(ap, source, key, key_len, ctxt);
6bbd11
+	if (ret == CHE_OK || ret == CHE_UPDATED)
6bbd11
+		return ret;
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
+
6bbd11
+	/* Lastly try the wildcard */
6bbd11
+	ret = lookup_one_amd(ap, source, "*", 1, ctxt);
6bbd11
+done:
6bbd11
+	free(lkp_key);
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
-	struct map_source *source;
6bbd11
 	struct mapent_cache *mc;
6bbd11
+	char buf[MAX_ERR_BUF];
6bbd11
+	struct map_source *source;
6bbd11
 	struct mapent *me;
6bbd11
-	char **hes_result;
6bbd11
-	int status, rv;
6bbd11
-	char **record, *best_record = NULL, *p;
6bbd11
-	int priority, lowest_priority = INT_MAX;	
6bbd11
+	char key[KEY_MAX_LEN + 1];
6bbd11
+	size_t key_len;
6bbd11
+	char *lkp_key;
6bbd11
+	size_t len;
6bbd11
+	char *mapent;
6bbd11
+	int rv;
6bbd11
 
6bbd11
 	source = ap->entry->current;
6bbd11
 	ap->entry->current = NULL;
6bbd11
@@ -118,6 +323,19 @@ int lookup_mount(struct autofs_point *ap
6bbd11
 	      MODPREFIX "looking up root=\"%s\", name=\"%s\"",
6bbd11
 	      ap->path, name);
6bbd11
 
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, name, LKP_DISTINCT);
6bbd11
 	if (me) {
6bbd11
@@ -144,69 +362,61 @@ int lookup_mount(struct autofs_point *ap
6bbd11
 		}
6bbd11
 	}
6bbd11
 
6bbd11
-	chdir("/");		/* If this is not here the filesystem stays
6bbd11
-				   busy, for some reason... */
6bbd11
-
6bbd11
-	status = pthread_mutex_lock(&hesiod_mutex);
6bbd11
-	if (status)
6bbd11
-		fatal(status);
6bbd11
-
6bbd11
-	hes_result = hesiod_resolve(ctxt->hesiod_context, name, "filsys");
6bbd11
-	if (!hes_result || !hes_result[0]) {
6bbd11
-		/* Note: it is not clear to me how to distinguish between
6bbd11
-		 * the "no search results" case and other failures.  --JM */
6bbd11
-		error(ap->logopt,
6bbd11
-		      MODPREFIX "key \"%s\" not found in map", name);
6bbd11
-		status = pthread_mutex_unlock(&hesiod_mutex);
6bbd11
-		if (status)
6bbd11
-			fatal(status);
6bbd11
-		return NSS_STATUS_NOTFOUND;
6bbd11
+	/* If this is not here the filesystem stays busy, for some reason... */
6bbd11
+	if (chdir("/"))
6bbd11
+		warn(ap->logopt,
6bbd11
+		     MODPREFIX "failed to set working directory to \"/\"");
6bbd11
+
6bbd11
+	len = key_len;
6bbd11
+	if (!(source->flags & MAP_FLAG_FORMAT_AMD))
6bbd11
+		lkp_key = strdup(key);
6bbd11
+	else {
6bbd11
+		rv = lookup_one_amd(ap, source, "/defaults", 9, ctxt);
6bbd11
+		if (rv == CHE_FAIL)
6bbd11
+			warn(ap->logopt,
6bbd11
+			     MODPREFIX "failed to lookup \"/defaults\" entry");
6bbd11
+
6bbd11
+		if (!ap->pref)
6bbd11
+			lkp_key = strdup(key);
6bbd11
+		else {
6bbd11
+			len += strlen(ap->pref);
6bbd11
+			lkp_key = malloc(len + 1);
6bbd11
+			if (lkp_key) {
6bbd11
+				strcpy(lkp_key, ap->pref);
6bbd11
+				strcat(lkp_key, name);
6bbd11
+			}
6bbd11
+		}
6bbd11
 	}
6bbd11
 
6bbd11
-	/* autofs doesn't support falling back to alternate records, so just
6bbd11
-	   find the record with the lowest priority and hope it works.
6bbd11
-	   -- Aaron Ucko <amu@alum.mit.edu> 2002-03-11 */
6bbd11
-	for (record = hes_result; *record; ++record) {
6bbd11
-	    p = strrchr(*record, ' ');
6bbd11
-	    if ( p && isdigit(p[1]) ) {
6bbd11
-		priority = atoi(p+1);
6bbd11
-	    } else {
6bbd11
-		priority = INT_MAX - 1;
6bbd11
-	    }
6bbd11
-	    if (priority < lowest_priority) {
6bbd11
-		lowest_priority = priority;
6bbd11
-		best_record = *record;
6bbd11
-	    }
6bbd11
+	if (!lkp_key) {
6bbd11
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
6bbd11
+		error(ap->logopt, "malloc: %s", estr);
6bbd11
+		return NSS_STATUS_UNKNOWN;
6bbd11
 	}
6bbd11
 
6bbd11
-	cache_writelock(mc);
6bbd11
-	rv = cache_update(mc, source, name, best_record, time(NULL));
6bbd11
-	cache_unlock(mc);
6bbd11
-	if (rv == CHE_FAIL)
6bbd11
-		return NSS_STATUS_UNAVAIL;
6bbd11
+	if (source->flags & MAP_FLAG_FORMAT_AMD)
6bbd11
+		rv = match_amd_key(ap, source, lkp_key, len, ctxt);
6bbd11
+	else
6bbd11
+		rv = lookup_one(ap, source, lkp_key, len, ctxt);
6bbd11
 
6bbd11
-	debug(ap->logopt,
6bbd11
-	      MODPREFIX "lookup for \"%s\" gave \"%s\"",
6bbd11
-	      name, best_record);
6bbd11
+	if (rv == CHE_FAIL) {
6bbd11
+		free(lkp_key);
6bbd11
+		return NSS_STATUS_UNAVAIL;
6bbd11
+	}
6bbd11
 
6bbd11
-	rv = ctxt->parser->parse_mount(ap, name, name_len, best_record,
6bbd11
-				       ctxt->parser->context);
6bbd11
+	me = match_cached_key(ap, MODPREFIX, source, lkp_key);
6bbd11
+	free(lkp_key);
6bbd11
+	if (!me)
6bbd11
+		return NSS_STATUS_NOTFOUND;
6bbd11
 
6bbd11
-	hesiod_free_list(ctxt->hesiod_context, hes_result);
6bbd11
+	if (!me->mapent)
6bbd11
+		return NSS_STATUS_UNAVAIL;
6bbd11
 
6bbd11
-	status = pthread_mutex_unlock(&hesiod_mutex);
6bbd11
-	if (status)
6bbd11
-		fatal(status);
6bbd11
+	mapent = strdup(me->mapent);
6bbd11
 
6bbd11
-	if (rv) {
6bbd11
-		/* Don't update negative cache when re-connecting */
6bbd11
-		if (ap->flags & MOUNT_FLAG_REMOUNT)
6bbd11
-			return NSS_STATUS_TRYAGAIN;
6bbd11
-		cache_writelock(mc);
6bbd11
-		cache_update_negative(mc, source, name, ap->negative_timeout);
6bbd11
-		cache_unlock(mc);
6bbd11
-		return NSS_STATUS_TRYAGAIN;
6bbd11
-	}
6bbd11
+	rv = ctxt->parser->parse_mount(ap, key, key_len,
6bbd11
+				       mapent, ctxt->parser->context);
6bbd11
+	free(mapent);
6bbd11
 
6bbd11
 	/*
6bbd11
 	 * Unavailable due to error such as module load fail