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

304803
autofs-5.0.9 - amd lookup update lookup yp to handle amd keys
304803
304803
From: Ian Kent <raven@themaw.net>
304803
304803
304803
---
304803
 modules/lookup_yp.c |  248 ++++++++++++++++++++++++++++++++++++++++++++-------
304803
 1 file changed, 216 insertions(+), 32 deletions(-)
304803
304803
diff --git a/modules/lookup_yp.c b/modules/lookup_yp.c
304803
index ba97ccc..146e39e 100644
304803
--- a/modules/lookup_yp.c
304803
+++ b/modules/lookup_yp.c
304803
@@ -36,9 +36,10 @@
304803
 #define MODPREFIX "lookup(yp): "
304803
 
304803
 struct lookup_context {
304803
-	const char *domainname;
304803
+	char *domainname;
304803
 	const char *mapname;
304803
 	unsigned long order;
304803
+	unsigned int check_defaults;
304803
 	struct parse_mod *parse;
304803
 };
304803
 
304803
@@ -113,7 +114,7 @@ int lookup_init(const char *mapfmt, int argc, const char *const *argv, void **co
304803
 	ctxt = malloc(sizeof(struct lookup_context));
304803
 	if (!ctxt) {
304803
 		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
-		logerr(MODPREFIX "%s", estr);
304803
+		logerr(MODPREFIX "malloc: %s", estr);
304803
 		return 1;
304803
 	}
304803
 	memset(ctxt, 0, sizeof(struct lookup_context));
304803
@@ -124,17 +125,28 @@ int lookup_init(const char *mapfmt, int argc, const char *const *argv, void **co
304803
 		return 1;
304803
 	}
304803
 	ctxt->mapname = argv[0];
304803
-
304803
-	/* This should, but doesn't, take a const char ** */
304803
-	err = yp_get_default_domain((char **) &ctxt->domainname);
304803
-	if (err) {
304803
-		size_t len = strlen(ctxt->mapname);
304803
-		char *name = alloca(len + 1);
304803
-		memcpy(name, ctxt->mapname, len);
304803
-		name[len] = '\0';
304803
-		free(ctxt);
304803
-		logerr(MODPREFIX "map %s: %s", name, yperr_string(err));
304803
-		return 1;
304803
+	ctxt->check_defaults = 1;
304803
+
304803
+	if (mapfmt && !strcmp(mapfmt, "amd"))
304803
+		ctxt->domainname = conf_amd_get_nis_domain();
304803
+
304803
+	if (!ctxt->domainname) {
304803
+		char *domainname;
304803
+		/* This should, but doesn't, take a const char ** */
304803
+		err = yp_get_default_domain(&domainname);
304803
+		if (err) {
304803
+			logerr(MODPREFIX
304803
+			      "map %s: %s", ctxt->mapname, yperr_string(err));
304803
+			free(ctxt);
304803
+			return 1;
304803
+		}
304803
+		ctxt->domainname = strdup(domainname);
304803
+		if (!ctxt->domainname) {
304803
+			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
+			logerr(MODPREFIX "strdup: %s", estr);
304803
+			free(ctxt);
304803
+			return 1;
304803
+		}
304803
 	}
304803
 
304803
 	ctxt->order = get_map_order(ctxt->domainname, ctxt->mapname);
304803
@@ -286,7 +298,12 @@ int yp_all_callback(int status, char *ypkey, int ypkeylen,
304803
 	if (*ypkey == '+')
304803
 		return 0;
304803
 
304803
-	key = sanitize_path(ypkey, ypkeylen, ap->type, ap->logopt);
304803
+	if (!(source->flags & MAP_FLAG_FORMAT_AMD))
304803
+		key = sanitize_path(ypkey, ypkeylen, ap->type, ap->logopt);
304803
+	else
304803
+		/* Don't fail on "/" in key => type == 0 */
304803
+		key = sanitize_path(ypkey, ypkeylen, 0, ap->logopt);
304803
+
304803
 	if (!key) {
304803
 		error(logopt, MODPREFIX "invalid path %s", ypkey);
304803
 		return 0;
304803
@@ -372,6 +389,9 @@ int lookup_read_map(struct autofs_point *ap, time_t age, void *context)
304803
 	}
304803
 
304803
 	source->age = age;
304803
+	pthread_mutex_lock(&ap->entry->current_mutex);
304803
+	ctxt->check_defaults = 0;
304803
+	pthread_mutex_unlock(&ap->entry->current_mutex);
304803
 
304803
 	return NSS_STATUS_SUCCESS;
304803
 }
304803
@@ -432,6 +452,66 @@ static int lookup_one(struct autofs_point *ap,
304803
 	return ret;
304803
 }
304803
 
304803
+static int match_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(ap, source, key, strlen(key), ctxt);
304803
+	if (ret < 0)
304803
+		return ret;
304803
+	if (ret == CHE_OK || ret == CHE_UPDATED)
304803
+		return ret;
304803
+
304803
+	if (!(source->flags & MAP_FLAG_FORMAT_AMD))
304803
+		return CHE_FAIL;
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(ap, source, match, len, ctxt);
304803
+		free(match);
304803
+		if (ret < 0)
304803
+			goto done;
304803
+		if (ret == CHE_OK || ret == CHE_UPDATED)
304803
+			goto done;
304803
+	}
304803
+done:
304803
+	free(lkp_key);
304803
+	return ret;
304803
+}
304803
+
304803
 static int lookup_wild(struct autofs_point *ap,
304803
 		       struct map_source *source, struct lookup_context *ctxt)
304803
 {
304803
@@ -480,6 +560,52 @@ static int lookup_wild(struct autofs_point *ap,
304803
 	return ret;
304803
 }
304803
 
304803
+static int lookup_amd_defaults(struct autofs_point *ap,
304803
+			       struct map_source *source,
304803
+			       struct lookup_context *ctxt)
304803
+{
304803
+	struct mapent_cache *mc = source->mc;
304803
+	char *mapname;
304803
+	char *mapent;
304803
+	int mapent_len;
304803
+	int ret;
304803
+
304803
+	mapname = malloc(strlen(ctxt->mapname) + 1);
304803
+	if (!mapname)
304803
+		return 0;
304803
+
304803
+	strcpy(mapname, ctxt->mapname);
304803
+
304803
+	ret = yp_match((char *) ctxt->domainname, mapname,
304803
+		       (char *) "/defaults", 9, &mapent, &mapent_len);
304803
+
304803
+	if (ret != YPERR_SUCCESS) {
304803
+		if (ret == YPERR_MAP) {
304803
+			char *usc;
304803
+
304803
+			while ((usc = strchr(mapname, '_')))
304803
+				*usc = '.';
304803
+
304803
+			ret = yp_match((char *) ctxt->domainname, mapname,
304803
+				       "/defaults", 9, &mapent, &mapent_len);
304803
+		}
304803
+	}
304803
+	free(mapname);
304803
+
304803
+	/* No /defaults entry */
304803
+	if (ret == YPERR_KEY)
304803
+		return CHE_OK;
304803
+
304803
+	if (ret != YPERR_SUCCESS)
304803
+		return CHE_FAIL;
304803
+
304803
+	cache_writelock(mc);
304803
+	ret = cache_update(mc, source, "/defaults", mapent, time(NULL));
304803
+	cache_unlock(mc);
304803
+
304803
+	return ret;
304803
+}
304803
+
304803
 static int check_map_indirect(struct autofs_point *ap,
304803
 			      struct map_source *source,
304803
 			      char *key, int key_len,
304803
@@ -492,8 +618,28 @@ static int check_map_indirect(struct autofs_point *ap,
304803
 
304803
 	mc = source->mc;
304803
 
304803
+	/* Only read map if it has been modified */
304803
+	pthread_mutex_lock(&ap->entry->current_mutex);
304803
+	map_order = get_map_order(ctxt->domainname, ctxt->mapname);
304803
+	if (map_order > ctxt->order) {
304803
+		ctxt->order = map_order;
304803
+		source->stale = 1;
304803
+		ctxt->check_defaults = 1;
304803
+	}
304803
+
304803
+	if (source->flags & MAP_FLAG_FORMAT_AMD && ctxt->check_defaults) {
304803
+		/* Check for a /defaults entry to update the map source */
304803
+		if (lookup_amd_defaults(ap, source, ctxt) == CHE_FAIL) {
304803
+			warn(ap->logopt, MODPREFIX
304803
+			     "error getting /defaults from map %s",
304803
+			     ctxt->mapname);
304803
+		} else
304803
+			ctxt->check_defaults = 0;
304803
+	}
304803
+	pthread_mutex_unlock(&ap->entry->current_mutex);
304803
+
304803
 	/* check map and if change is detected re-read map */
304803
-	ret = lookup_one(ap, source, key, key_len, ctxt);
304803
+	ret = match_key(ap, source, key, key_len, ctxt);
304803
 	if (ret == CHE_FAIL)
304803
 		return NSS_STATUS_NOTFOUND;
304803
 
304803
@@ -503,7 +649,10 @@ static int check_map_indirect(struct autofs_point *ap,
304803
 		 * and belongs to this map return success and use the entry.
304803
 		 */
304803
 		cache_readlock(mc);
304803
-		exists = cache_lookup(mc, key);
304803
+		if (source->flags & MAP_FLAG_FORMAT_AMD)
304803
+			exists = match_cached_key(ap, MODPREFIX, source, key);
304803
+		else
304803
+			exists = cache_lookup(mc, key);
304803
 		if (exists && exists->source == source) {
304803
 			cache_unlock(mc);
304803
 			return NSS_STATUS_SUCCESS;
304803
@@ -517,15 +666,11 @@ static int check_map_indirect(struct autofs_point *ap,
304803
 		return NSS_STATUS_UNAVAIL;
304803
 	}
304803
 
304803
-	/* Only read map if it has been modified */
304803
-	map_order = get_map_order(ctxt->domainname, ctxt->mapname);
304803
-	if (map_order > ctxt->order) {
304803
-		ctxt->order = map_order;
304803
-		source->stale = 1;
304803
-	}
304803
-
304803
 	cache_writelock(mc);
304803
-	exists = cache_lookup_distinct(mc, key);
304803
+	if (source->flags & MAP_FLAG_FORMAT_AMD)
304803
+		exists = match_cached_key(ap, MODPREFIX, source, key);
304803
+	else
304803
+		exists = cache_lookup_distinct(mc, key);
304803
 	/* Not found in the map but found in the cache */
304803
 	if (exists && exists->source == source && ret & CHE_MISSING) {
304803
 		if (exists->mapent) {
304803
@@ -578,9 +723,11 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
304803
 	struct mapent_cache *mc;
304803
 	char key[KEY_MAX_LEN + 1];
304803
 	int key_len;
304803
+	char *lkp_key;
304803
 	char *mapent = NULL;
304803
 	int mapent_len;
304803
 	struct mapent *me;
304803
+	char buf[MAX_ERR_BUF];
304803
 	int status = 0;
304803
 	int ret = 1;
304803
 
304803
@@ -592,9 +739,18 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
304803
 
304803
 	debug(ap->logopt, MODPREFIX "looking up %s", name);
304803
 
304803
-	key_len = snprintf(key, KEY_MAX_LEN + 1, "%s", name);
304803
-	if (key_len > KEY_MAX_LEN)
304803
-		return NSS_STATUS_NOTFOUND;
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
+		memset(key, 0, KEY_MAX_LEN + 1);
304803
+		expandamdent(name, key, NULL);
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, key, LKP_DISTINCT);
304803
@@ -628,18 +784,26 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
304803
 	  * we never know about it.
304803
 	  */
304803
         if (ap->type == LKP_INDIRECT && *key != '/') {
304803
-		char *lkp_key;
304803
-
304803
 		cache_readlock(mc);
304803
 		me = cache_lookup_distinct(mc, key);
304803
 		if (me && me->multi)
304803
 			lkp_key = strdup(me->multi->key);
304803
-		else
304803
+		else if (!ap->pref)
304803
 			lkp_key = strdup(key);
304803
+		else {
304803
+			lkp_key = malloc(strlen(ap->pref) + strlen(key) + 1);
304803
+			if (lkp_key) {
304803
+				strcpy(lkp_key, ap->pref);
304803
+				strcat(lkp_key, key);
304803
+			}
304803
+		}
304803
 		cache_unlock(mc);
304803
 
304803
-		if (!lkp_key)
304803
+		if (!lkp_key) {
304803
+			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
+			error(ap->logopt, MODPREFIX "malloc: %s", estr);
304803
 			return NSS_STATUS_UNKNOWN;
304803
+		}
304803
 
304803
 		status = check_map_indirect(ap, source,
304803
 					    lkp_key, strlen(lkp_key), ctxt);
304803
@@ -660,7 +824,25 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
304803
 		cache_readlock(mc);
304803
 	else
304803
 		cache_writelock(mc);
304803
-	me = cache_lookup(mc, key);
304803
+
304803
+	if (!ap->pref)
304803
+		lkp_key = strdup(key);
304803
+	else {
304803
+		lkp_key = malloc(strlen(ap->pref) + strlen(key) + 1);
304803
+		if (lkp_key) {
304803
+			strcpy(lkp_key, ap->pref);
304803
+			strcat(lkp_key, key);
304803
+		}
304803
+	}
304803
+
304803
+	if (!lkp_key) {
304803
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
+		error(ap->logopt, MODPREFIX "malloc: %s", estr);
304803
+		cache_unlock(mc);
304803
+		return NSS_STATUS_UNKNOWN;
304803
+	}
304803
+
304803
+	me = match_cached_key(ap, MODPREFIX, source, lkp_key);
304803
 	/* Stale mapent => check for entry in alternate source or wildcard */
304803
 	if (me && !me->mapent) {
304803
 		while ((me = cache_lookup_key_next(me)))
304803
@@ -687,6 +869,7 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
304803
 		}
304803
 	}
304803
 	cache_unlock(mc);
304803
+	free(lkp_key);
304803
 
304803
 	if (mapent) {
304803
 		master_source_current_wait(ap->entry);
304803
@@ -716,6 +899,7 @@ int lookup_done(void *context)
304803
 {
304803
 	struct lookup_context *ctxt = (struct lookup_context *) context;
304803
 	int rv = close_parse(ctxt->parse);
304803
+	free(ctxt->domainname);
304803
 	free(ctxt);
304803
 	return rv;
304803
 }