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

306fa1
autofs-5.0.9 - amd lookup update lookup nisplus to handle amd keys
306fa1
306fa1
From: Ian Kent <raven@themaw.net>
306fa1
306fa1
306fa1
---
306fa1
 modules/lookup_nisplus.c |  208 +++++++++++++++++++++++++++++++++++++++++++---
306fa1
 1 file changed, 196 insertions(+), 12 deletions(-)
306fa1
306fa1
diff --git a/modules/lookup_nisplus.c b/modules/lookup_nisplus.c
306fa1
index 42c9ccc..e9444c9 100644
306fa1
--- a/modules/lookup_nisplus.c
306fa1
+++ b/modules/lookup_nisplus.c
306fa1
@@ -243,7 +243,19 @@ int lookup_read_map(struct autofs_point *ap, time_t age, void *context)
306fa1
 		if (*key == '+')
306fa1
 			continue;
306fa1
 
306fa1
-		s_key = sanitize_path(key, len, ap->type, ap->logopt);
306fa1
+		if (!(source->flags & MAP_FLAG_FORMAT_AMD))
306fa1
+			s_key = sanitize_path(key, len, ap->type, ap->logopt);
306fa1
+		else {
306fa1
+			if (!strcmp(key, "/defaults")) {
306fa1
+				mapent = ENTRY_VAL(this, 1);
306fa1
+				cache_writelock(mc);
306fa1
+				cache_update(mc, source, key, mapent, age);
306fa1
+				cache_unlock(mc);
306fa1
+				continue;
306fa1
+			}
306fa1
+			/* Don't fail on "/" in key => type == 0 */
306fa1
+			s_key = sanitize_path(key, len, 0, ap->logopt);
306fa1
+		}
306fa1
 		if (!s_key)
306fa1
 			continue;
306fa1
 
306fa1
@@ -322,6 +334,66 @@ static int lookup_one(struct autofs_point *ap,
306fa1
 	return ret;
306fa1
 }
306fa1
 
306fa1
+static int match_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(ap, source, key, key_len, ctxt);
306fa1
+	if (ret < 0)
306fa1
+		return ret;
306fa1
+	if (ret == CHE_OK || ret == CHE_UPDATED)
306fa1
+		return ret;
306fa1
+
306fa1
+	if (!(source->flags & MAP_FLAG_FORMAT_AMD))
306fa1
+		return CHE_FAIL;
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(ap, source, match, len, ctxt);
306fa1
+		free(match);
306fa1
+		if (ret < 0)
306fa1
+			goto done;
306fa1
+		if (ret == CHE_OK || ret == CHE_UPDATED)
306fa1
+			goto done;
306fa1
+	}
306fa1
+done:
306fa1
+	free(lkp_key);
306fa1
+	return ret;
306fa1
+}
306fa1
+
306fa1
 static int lookup_wild(struct autofs_point *ap,
306fa1
 		       struct map_source *source, struct lookup_context *ctxt)
306fa1
 {
306fa1
@@ -374,6 +446,59 @@ static int lookup_wild(struct autofs_point *ap,
306fa1
 	return ret;
306fa1
 }
306fa1
 
306fa1
+static int lookup_amd_defaults(struct autofs_point *ap,
306fa1
+			       struct map_source *source,
306fa1
+			       struct lookup_context *ctxt)
306fa1
+{
306fa1
+	struct mapent_cache *mc = source->mc;
306fa1
+	char *tablename;
306fa1
+	nis_result *result;
306fa1
+	nis_object *this;
306fa1
+	char *mapent;
306fa1
+	char buf[MAX_ERR_BUF];
306fa1
+	int cur_state;
306fa1
+	int ret;
306fa1
+
306fa1
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
306fa1
+	tablename = malloc(9 + strlen(ctxt->mapname) +
306fa1
+			   strlen(ctxt->domainname) + 20);
306fa1
+	if (!tablename) {
306fa1
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
306fa1
+		logerr(MODPREFIX "malloc: %s", estr);
306fa1
+		pthread_setcancelstate(cur_state, NULL);
306fa1
+		return CHE_FAIL;
306fa1
+	}
306fa1
+	sprintf(tablename, "[key=/defaults],%s.org_dir.%s",
306fa1
+		ctxt->mapname, ctxt->domainname);
306fa1
+
306fa1
+	result = nis_list(tablename, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
306fa1
+	if (result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) {
306fa1
+		nis_error rs = result->status;
306fa1
+		nis_freeresult(result);
306fa1
+		free(tablename);
306fa1
+		pthread_setcancelstate(cur_state, NULL);
306fa1
+		if (rs == NIS_NOTFOUND ||
306fa1
+		    rs == NIS_S_NOTFOUND ||
306fa1
+		    rs == NIS_PARTIAL)
306fa1
+			return CHE_MISSING;
306fa1
+
306fa1
+		return -rs;
306fa1
+	}
306fa1
+
306fa1
+	this = NIS_RES_OBJECT(result);
306fa1
+	mapent = ENTRY_VAL(this, 1);
306fa1
+
306fa1
+	cache_writelock(mc);
306fa1
+	ret = cache_update(mc, source, "/defaults", mapent, time(NULL));
306fa1
+	cache_unlock(mc);
306fa1
+
306fa1
+	nis_freeresult(result);
306fa1
+	free(tablename);
306fa1
+	pthread_setcancelstate(cur_state, NULL);
306fa1
+
306fa1
+	return ret;
306fa1
+}
306fa1
+
306fa1
 static int check_map_indirect(struct autofs_point *ap,
306fa1
 			      struct map_source *source,
306fa1
 			      char *key, int key_len,
306fa1
@@ -387,8 +512,17 @@ static int check_map_indirect(struct autofs_point *ap,
306fa1
 
306fa1
 	mc = source->mc;
306fa1
 
306fa1
+	if (source->flags & MAP_FLAG_FORMAT_AMD) {
306fa1
+		/* Check for a /defaults entry to update the map source */
306fa1
+		if (lookup_amd_defaults(ap, source, ctxt) == CHE_FAIL) {
306fa1
+			warn(ap->logopt, MODPREFIX
306fa1
+			     "error getting /defaults from map %s",
306fa1
+			     ctxt->mapname);
306fa1
+		}
306fa1
+	}
306fa1
+
306fa1
 	/* check map and if change is detected re-read map */
306fa1
-	ret = lookup_one(ap, source, key, key_len, ctxt);
306fa1
+	ret = match_key(ap, source, key, key_len, ctxt);
306fa1
 	if (ret == CHE_FAIL)
306fa1
 		return NSS_STATUS_NOTFOUND;
306fa1
 
306fa1
@@ -397,9 +531,16 @@ static int check_map_indirect(struct autofs_point *ap,
306fa1
 		 * If the server is down and the entry exists in the cache
306fa1
 		 * and belongs to this map return success and use the entry.
306fa1
 		 */
306fa1
-		exists = cache_lookup(mc, key);
306fa1
-		if (exists && exists->source == source)
306fa1
+		cache_readlock(mc);
306fa1
+		if (source->flags & MAP_FLAG_FORMAT_AMD)
306fa1
+			exists = match_cached_key(ap, MODPREFIX, source, key);
306fa1
+		else
306fa1
+			exists = cache_lookup(mc, key);
306fa1
+		if (exists && exists->source == source) {
306fa1
+			cache_unlock(mc);
306fa1
 			return NSS_STATUS_SUCCESS;
306fa1
+		}
306fa1
+		cache_unlock(mc);
306fa1
 
306fa1
 		warn(ap->logopt,
306fa1
 		     MODPREFIX "lookup for %s failed: %s",
306fa1
@@ -418,6 +559,10 @@ static int check_map_indirect(struct autofs_point *ap,
306fa1
 		}
306fa1
 		me = cache_lookup_next(mc, me);
306fa1
 	}
306fa1
+	if (source->flags & MAP_FLAG_FORMAT_AMD)
306fa1
+		exists = match_cached_key(ap, MODPREFIX, source, key);
306fa1
+	else
306fa1
+		exists = cache_lookup_distinct(mc, key);
306fa1
 	exists = cache_lookup_distinct(mc, key);
306fa1
 	/* Not found in the map but found in the cache */
306fa1
 	if (exists && exists->source == source && ret & CHE_MISSING) {
306fa1
@@ -474,9 +619,11 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
306fa1
 	struct mapent_cache *mc;
306fa1
 	char key[KEY_MAX_LEN + 1];
306fa1
 	int key_len;
306fa1
+	char *lkp_key;
306fa1
 	char *mapent = NULL;
306fa1
 	int mapent_len;
306fa1
 	struct mapent *me;
306fa1
+	char buf[MAX_ERR_BUF];
306fa1
 	int status;
306fa1
 	int ret = 1;
306fa1
 
306fa1
@@ -488,9 +635,18 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
306fa1
 
306fa1
 	debug(ap->logopt, MODPREFIX "looking up %s", name);
306fa1
 
306fa1
-	key_len = snprintf(key, KEY_MAX_LEN + 1, "%s", name);
306fa1
-	if (key_len > KEY_MAX_LEN)
306fa1
-		return NSS_STATUS_NOTFOUND;
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
+		memset(key, 0, KEY_MAX_LEN + 1);
306fa1
+		expandamdent(name, key, NULL);
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, key, LKP_DISTINCT);
306fa1
@@ -524,21 +680,30 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
306fa1
 	 * we never know about it.
306fa1
 	 */
306fa1
 	if (ap->type == LKP_INDIRECT && *key != '/') {
306fa1
-		char *lkp_key;
306fa1
-
306fa1
 		cache_readlock(mc);
306fa1
 		me = cache_lookup_distinct(mc, key);
306fa1
 		if (me && me->multi)
306fa1
 			lkp_key = strdup(me->multi->key);
306fa1
-		else
306fa1
+		else if (!ap->pref)
306fa1
 			lkp_key = strdup(key);
306fa1
+		else {
306fa1
+			lkp_key = malloc(strlen(ap->pref) + strlen(key) + 1);
306fa1
+			if (lkp_key) {
306fa1
+				strcpy(lkp_key, ap->pref);
306fa1
+				strcat(lkp_key, key);
306fa1
+			}
306fa1
+		}
306fa1
 		cache_unlock(mc);
306fa1
 
306fa1
-		if (!lkp_key)
306fa1
+		if (!lkp_key) {
306fa1
+			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
306fa1
+			error(ap->logopt, MODPREFIX "malloc: %s", estr);
306fa1
 			return NSS_STATUS_UNKNOWN;
306fa1
+		}
306fa1
 
306fa1
 		status = check_map_indirect(ap, source,
306fa1
 					    lkp_key, strlen(lkp_key), ctxt);
306fa1
+		free(lkp_key);
306fa1
 		if (status)
306fa1
 			return status;
306fa1
 	}
306fa1
@@ -555,7 +720,25 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
306fa1
 		cache_readlock(mc);
306fa1
 	else
306fa1
 		cache_writelock(mc);
306fa1
-	me = cache_lookup(mc, key);
306fa1
+
306fa1
+	if (!ap->pref)
306fa1
+		lkp_key = strdup(key);
306fa1
+	else {
306fa1
+		lkp_key = malloc(strlen(ap->pref) + strlen(key) + 1);
306fa1
+		if (lkp_key) {
306fa1
+			strcpy(lkp_key, ap->pref);
306fa1
+			strcat(lkp_key, key);
306fa1
+		}
306fa1
+	}
306fa1
+
306fa1
+	if (!lkp_key) {
306fa1
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
306fa1
+		error(ap->logopt, MODPREFIX "malloc: %s", estr);
306fa1
+		cache_unlock(mc);
306fa1
+		return NSS_STATUS_UNKNOWN;
306fa1
+	}
306fa1
+
306fa1
+	me = match_cached_key(ap, MODPREFIX, source, lkp_key);
306fa1
 	/* Stale mapent => check for entry in alternate source or wildcard */
306fa1
 	if (me && !me->mapent) {
306fa1
 		while ((me = cache_lookup_key_next(me)))
306fa1
@@ -583,6 +766,7 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
306fa1
 		}
306fa1
 	}
306fa1
 	cache_unlock(mc);
306fa1
+	free(lkp_key);
306fa1
 
306fa1
 	if (!mapent)
306fa1
 		return NSS_STATUS_TRYAGAIN;