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

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