Blame SOURCES/autofs-5.0.7-teach-dumpmaps-to-output-simple-key-value-pairs.patch

306fa1
autofs-5.0.7 - teach dumpmaps to output simple key value pairs
306fa1
306fa1
From: Ian Kent <raven@themaw.net>
306fa1
306fa1
The dumpmaps option doesn't allow maps to be output in <key, value>
306fa1
pairs suitable for use as a file map.
306fa1
306fa1
This could be useful to save current maps as a backup for emergency
306fa1
use.
306fa1
306fa1
If the dumpmaps option is given and is followed by two parameters,
306fa1
"<map type> <map name>" then simple <key, value> pairs that would
306fa1
be read in by a map read are printed to stdout if the given map type
306fa1
and map name are found in the map configuration.
306fa1
306fa1
If the map is an LDAP map and there is more than one map of same name
306fa1
in different base dns only the first map encountered by autofs will
306fa1
be listed.
306fa1
306fa1
If the map type is an old style multi-map and any one of the map
306fa1
names in the multi-map entry matches the given map name the entries
306fa1
that would be used by autofs for the whole multi-map will be listed.
306fa1
---
306fa1
 CHANGELOG          |    1 
306fa1
 daemon/automount.c |   56 ++++++++++++---
306fa1
 include/master.h   |    1 
306fa1
 lib/master.c       |  187 +++++++++++++++++++++++++++++++++++++++++++++++++++++
306fa1
 man/automount.8    |   20 +++++
306fa1
 5 files changed, 250 insertions(+), 15 deletions(-)
306fa1
306fa1
--- autofs-5.0.7.orig/CHANGELOG
306fa1
+++ autofs-5.0.7/CHANGELOG
306fa1
@@ -60,6 +60,7 @@
306fa1
 - fix fix wildcard multi map regression.
306fa1
 - fix dumpmaps multi output.
306fa1
 - try and cleanup after dumpmaps.
306fa1
+- teach dumpmaps to output simple key value pairs.
306fa1
 
306fa1
 25/07/2012 autofs-5.0.7
306fa1
 =======================
306fa1
--- autofs-5.0.7.orig/daemon/automount.c
306fa1
+++ autofs-5.0.7/daemon/automount.c
306fa1
@@ -1725,7 +1725,8 @@ static void usage(void)
306fa1
 		"	-f --foreground do not fork into background\n"
306fa1
 		"	-r --random-multimount-selection\n"
306fa1
 		"			use ramdom replicated server selection\n"
306fa1
-		"	-m --dumpmaps	dump automounter maps and exit\n"
306fa1
+		"	-m --dumpmaps [<map type> <map name>]\n"
306fa1
+		"			dump automounter maps and exit\n"
306fa1
 		"	-n --negative-timeout n\n"
306fa1
 		"			set the timeout for failed key lookups.\n"
306fa1
 		"	-O --global-options\n"
306fa1
@@ -2125,22 +2126,33 @@ int main(int argc, char *argv[])
306fa1
 			program);
306fa1
 #endif
306fa1
 
306fa1
-	if (argc == 0)
306fa1
-		master_list = master_new(NULL, timeout, ghost);
306fa1
-	else
306fa1
-		master_list = master_new(argv[0], timeout, ghost);
306fa1
-
306fa1
-	if (!master_list) {
306fa1
-		printf("%s: can't create master map %s", program, argv[0]);
306fa1
-		exit(1);
306fa1
-	}
306fa1
-
306fa1
 	if (dumpmaps) {
306fa1
 		struct master_mapent *entry;
306fa1
 		struct list_head *head, *p;
306fa1
 		struct mapent_cache *nc;
306fa1
+		const char *type = NULL;
306fa1
+		const char *name = NULL;
306fa1
+		const char *master = NULL;
306fa1
+
306fa1
+		if (argc > 0) {
306fa1
+			if (argc >= 2) {
306fa1
+				type = argv[0];
306fa1
+				name = argv[1];
306fa1
+			}
306fa1
+			if (argc == 3)
306fa1
+				master = argv[2];
306fa1
+		}
306fa1
 
306fa1
-		open_log();
306fa1
+		if (master)
306fa1
+			master_list = master_new(NULL, timeout, ghost);
306fa1
+		else
306fa1
+			master_list = master_new(master, timeout, ghost);
306fa1
+		if (!master_list) {
306fa1
+			printf("%s: can't create master map", program);
306fa1
+			exit(1);
306fa1
+		}
306fa1
+
306fa1
+		log_to_stderr();
306fa1
 
306fa1
 		master_init_scan();
306fa1
 
306fa1
@@ -2153,7 +2165,15 @@ int main(int argc, char *argv[])
306fa1
 		master_list->nc = nc;
306fa1
 
306fa1
 		lookup_nss_read_master(master_list, 0);
306fa1
-		master_show_mounts(master_list);
306fa1
+		if (type) {
306fa1
+			const char *map = basename(name);
306fa1
+			if (!map)
306fa1
+				printf("%s: invalid map name %s\n",
306fa1
+					program, name);
306fa1
+			else
306fa1
+				dump_map(master_list, type, map);
306fa1
+		} else
306fa1
+			master_show_mounts(master_list);
306fa1
 
306fa1
 		head = &master_list->mounts;
306fa1
 		p = head->next;
306fa1
@@ -2168,6 +2188,16 @@ int main(int argc, char *argv[])
306fa1
 		exit(0);
306fa1
 	}
306fa1
 
306fa1
+	if (argc == 0)
306fa1
+		master_list = master_new(NULL, timeout, ghost);
306fa1
+	else
306fa1
+		master_list = master_new(argv[0], timeout, ghost);
306fa1
+
306fa1
+	if (!master_list) {
306fa1
+		printf("%s: can't create master map %s", program, argv[0]);
306fa1
+		exit(1);
306fa1
+	}
306fa1
+
306fa1
 	become_daemon(foreground, daemon_check);
306fa1
 
306fa1
 	if (pthread_attr_init(&th_attr)) {
306fa1
--- autofs-5.0.7.orig/include/master.h
306fa1
+++ autofs-5.0.7/include/master.h
306fa1
@@ -112,6 +112,7 @@ int master_submount_list_empty(struct au
306fa1
 int master_notify_submount(struct autofs_point *, const char *path, enum states);
306fa1
 void master_notify_state_change(struct master *, int);
306fa1
 int master_mount_mounts(struct master *, time_t, int);
306fa1
+int dump_map(struct master *, const char *, const char *);
306fa1
 int master_show_mounts(struct master *);
306fa1
 extern inline unsigned int master_get_logopt(void);
306fa1
 int master_list_empty(struct master *);
306fa1
--- autofs-5.0.7.orig/lib/master.c
306fa1
+++ autofs-5.0.7/lib/master.c
306fa1
@@ -1329,6 +1329,193 @@ static void print_map_info(struct map_so
306fa1
 	return;
306fa1
 }
306fa1
 
306fa1
+static int match_type(const char *source, const char *type)
306fa1
+{
306fa1
+	if (!strcmp(source, type))
306fa1
+		return 1;
306fa1
+	/* Sources file and files are synonymous */
306fa1
+	if (!strncmp(source, type, 4) && (strlen(source) <= 5))
306fa1
+		return 1;
306fa1
+	return 0;
306fa1
+}
306fa1
+
306fa1
+static char *get_map_name(const char *string)
306fa1
+{
306fa1
+	char *name, *tmp;
306fa1
+	char *start, *end, *base;
306fa1
+
306fa1
+	tmp = strdup(string);
306fa1
+	if (!tmp) {
306fa1
+		printf("error: allocation failure: %s\n", strerror(errno));
306fa1
+		return NULL;
306fa1
+	}
306fa1
+
306fa1
+	base = basename(tmp);
306fa1
+	end = strchr(base, ',');
306fa1
+	if (end)
306fa1
+		*end = '\0';
306fa1
+	start = strchr(tmp, '=');
306fa1
+	if (start)
306fa1
+		start++;
306fa1
+	else {
306fa1
+		char *colon = strrchr(base, ':');
306fa1
+		if (colon)
306fa1
+			start = ++colon;
306fa1
+		else
306fa1
+			start = base;
306fa1
+	}
306fa1
+
306fa1
+	name = strdup(start);
306fa1
+	if (!name)
306fa1
+		printf("error: allocation failure: %s\n", strerror(errno));
306fa1
+	free(tmp);
306fa1
+
306fa1
+	return name;
306fa1
+}
306fa1
+
306fa1
+static int match_name(struct map_source *source, const char *name)
306fa1
+{
306fa1
+	int argc = source->argc;
306fa1
+	int ret = 0;
306fa1
+	int i;
306fa1
+
306fa1
+	/*
306fa1
+	 * This can't work for old style "multi" type sources since
306fa1
+	 * there's no way to know from which map the cache entry came
306fa1
+	 * from and duplicate entries are ignored at map read time.
306fa1
+	 * All we can really do is list all the entries for the given
306fa1
+	 * multi map if one of its map names matches.
306fa1
+	 */
306fa1
+	for (i = 0; i < argc; i++) {
306fa1
+		if (i == 0 || !strcmp(source->argv[i], "--")) {
306fa1
+			if (i != 0) {
306fa1
+				i++;
306fa1
+				if (i >= argc)
306fa1
+					break;
306fa1
+			}
306fa1
+
306fa1
+			if (source->argv[i] && *source->argv[i] != '-') {
306fa1
+				char *map = get_map_name(source->argv[i]);
306fa1
+				if (!map)
306fa1
+					break;
306fa1
+				if (!strcmp(map, name)) {
306fa1
+					ret = 1;
306fa1
+					free(map);
306fa1
+					break;
306fa1
+				}
306fa1
+				free(map);
306fa1
+			}
306fa1
+		}
306fa1
+	}
306fa1
+
306fa1
+	return ret;
306fa1
+}
306fa1
+
306fa1
+int dump_map(struct master *master, const char *type, const char *name)
306fa1
+{
306fa1
+	struct list_head *p, *head;
306fa1
+
306fa1
+	if (list_empty(&master->mounts)) {
306fa1
+		printf("no master map entries found\n");
306fa1
+		return 1;
306fa1
+	}
306fa1
+
306fa1
+	head = &master->mounts;
306fa1
+	p = head->next;
306fa1
+	while (p != head) {
306fa1
+		struct map_source *source;
306fa1
+		struct master_mapent *this;
306fa1
+		struct autofs_point *ap;
306fa1
+		time_t now = time(NULL);
306fa1
+
306fa1
+		this = list_entry(p, struct master_mapent, list);
306fa1
+		p = p->next;
306fa1
+
306fa1
+		ap = this->ap;
306fa1
+
306fa1
+		/*
306fa1
+		 * Ensure we actually read indirect map entries so we can
306fa1
+		 * list them. The map reads won't read any indirect map
306fa1
+		 * entries (other than those in a file map) unless the
306fa1
+		 * browse option is set.
306fa1
+		 */
306fa1
+		if (ap->type == LKP_INDIRECT)
306fa1
+			ap->flags |= MOUNT_FLAG_GHOST;
306fa1
+
306fa1
+		/* Read the map content into the cache */
306fa1
+		if (lookup_nss_read_map(ap, NULL, now))
306fa1
+			lookup_prune_cache(ap, now);
306fa1
+		else {
306fa1
+			printf("failed to read map\n");
306fa1
+			lookup_close_lookup(ap);
306fa1
+			continue;
306fa1
+		}
306fa1
+
306fa1
+		if (!this->maps) {
306fa1
+			printf("no map sources found for %s\n", ap->path);
306fa1
+			lookup_close_lookup(ap);
306fa1
+			continue;
306fa1
+		}
306fa1
+
306fa1
+		source = this->maps;
306fa1
+		while (source) {
306fa1
+			struct map_source *instance;
306fa1
+			struct mapent *me;
306fa1
+
306fa1
+			instance = NULL;
306fa1
+			if (source->type) {
306fa1
+				if (!match_type(source->type, type)) {
306fa1
+					source = source->next;
306fa1
+					continue;
306fa1
+				}
306fa1
+				if (!match_name(source, name)) {
306fa1
+					source = source->next;
306fa1
+					continue;
306fa1
+				}
306fa1
+				instance = source;
306fa1
+			} else {
306fa1
+				struct map_source *map;
306fa1
+
306fa1
+				map = source->instance;
306fa1
+				while (map) {
306fa1
+					if (!match_type(map->type, type)) {
306fa1
+						map = map->next;
306fa1
+						continue;
306fa1
+					}
306fa1
+					if (!match_name(map, name)) {
306fa1
+						map = map->next;
306fa1
+						continue;
306fa1
+					}
306fa1
+					instance = map;
306fa1
+					break;
306fa1
+				}
306fa1
+			}
306fa1
+
306fa1
+			if (!instance) {
306fa1
+				source = source->next;
306fa1
+				lookup_close_lookup(ap);
306fa1
+				continue;
306fa1
+			}
306fa1
+
306fa1
+			me = cache_lookup_first(source->mc);
306fa1
+			if (!me)
306fa1
+				printf("no keys found in map\n");
306fa1
+			else {
306fa1
+				do {
306fa1
+					if (me->source == instance)
306fa1
+						printf("%s\t%s\n", me->key, me->mapent);
306fa1
+				} while ((me = cache_lookup_next(source->mc, me)));
306fa1
+			}
306fa1
+
306fa1
+			lookup_close_lookup(ap);
306fa1
+			return 1;
306fa1
+		}
306fa1
+		lookup_close_lookup(ap);
306fa1
+	}
306fa1
+
306fa1
+	return 0;
306fa1
+}
306fa1
+
306fa1
 int master_show_mounts(struct master *master)
306fa1
 {
306fa1
 	struct list_head *p, *head;
306fa1
--- autofs-5.0.7.orig/man/automount.8
306fa1
+++ autofs-5.0.7/man/automount.8
306fa1
@@ -57,8 +57,24 @@ Run the daemon in the foreground and log
306fa1
 Enables the use of ramdom selection when choosing a host from a
306fa1
 list of replicated servers.
306fa1
 .TP
306fa1
-.I "\-m, \-\-dumpmaps"
306fa1
-Dump configured automounter maps, then exit.
306fa1
+.I "\-m, \-\-dumpmaps [<map type> <map name>]"
306fa1
+With no parameters, list information about the configured automounter
306fa1
+maps, then exit.
306fa1
+
306fa1
+If the dumpmaps option is given and is followed by two parameters,
306fa1
+"<map type> <map name>" then simple "<key, value>" pairs that would
306fa1
+be read in by a map read are printed to stdout if the given map type
306fa1
+and map name are found in the map configuration.
306fa1
+
306fa1
+If the map is an LDAP map and there is more than one map of same name
306fa1
+in different base dns only the first map encountered by autofs will
306fa1
+be listed. Similarly, if the map is a file map and there is more than
306fa1
+one map of the same name in different directories, only the first map
306fa1
+encountered will be listed.
306fa1
+
306fa1
+If the map type is an old style multi-map and any one of the map
306fa1
+names in the multi-map entry matches the given map name the entries
306fa1
+that would be used by autofs for the whole multi-map will be listed.
306fa1
 .TP
306fa1
 .I "\-O, \-\-global-options"
306fa1
 Allows the specification of global mount options used for all master