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

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