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

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