Blame SOURCES/autofs-5.1.1-implement-reinit-in-multi-lookup-module.patch

019928
autofs-5.1.1 - implement reinit in multi lookup module
019928
019928
From: Ian Kent <raven@themaw.net>
019928
019928
Update the multi lookup module to add an implementation for the newly
019928
added reinit entry point.
019928
019928
Signed-off-by: Ian Kent <raven@themaw.net>
019928
---
019928
 modules/lookup_multi.c |  228 ++++++++++++++++++++++++++++++++++++++++++++++++
019928
 1 file changed, 227 insertions(+), 1 deletion(-)
019928
019928
diff --git a/modules/lookup_multi.c b/modules/lookup_multi.c
019928
index f8ebf94..fadd2ea 100644
019928
--- a/modules/lookup_multi.c
019928
+++ b/modules/lookup_multi.c
019928
@@ -146,6 +146,31 @@ static int free_multi_context(struct lookup_context *ctxt)
019928
 	return rv;
019928
 }
019928
 
019928
+static struct lookup_context *update_multi_context(struct lookup_context *ctxt,
019928
+						   struct lookup_context *new)
019928
+{
019928
+	int i;
019928
+
019928
+	for (i = 0; i < new->n && i < ctxt->n; i++) {
019928
+		if (new->m[i].mod)
019928
+			continue;
019928
+
019928
+		if (!ctxt->m[i].mod)
019928
+			continue;
019928
+
019928
+		/* reinit or open failed, use old one, questionable but
019928
+		 * we need to do something.
019928
+		 */
019928
+		new->m[i].mod = ctxt->m[i].mod;
019928
+		ctxt->m[i].mod = NULL;
019928
+		new->m[i].argc = ctxt->m[i].argc;
019928
+		new->m[i].argv = ctxt->m[i].argv;
019928
+		ctxt->m[i].argv = NULL;
019928
+	}
019928
+
019928
+	return new;
019928
+}
019928
+
019928
 static struct lookup_mod *nss_open_lookup(const char *format, int argc, const char **argv)
019928
 {
019928
 	struct list_head nsslist;
019928
@@ -268,6 +293,8 @@ int lookup_init(const char *my_mapfmt,
019928
 	struct lookup_context *ctxt;
019928
 	int i;
019928
 
019928
+	*context = NULL;
019928
+
019928
 	ctxt = alloc_context(my_mapfmt, argc, argv);
019928
 	if (!ctxt)
019928
 		return 1;
019928
@@ -291,7 +318,206 @@ int lookup_init(const char *my_mapfmt,
019928
 int lookup_reinit(const char *my_mapfmt,
019928
 		  int argc, const char *const *argv, void **context)
019928
 {
019928
-	return 0;
019928
+	struct lookup_context *ctxt = (struct lookup_context *) *context;
019928
+	struct list_head nsslist;
019928
+	struct list_head *head, *p;
019928
+	struct lookup_context *new;
019928
+	char buf[MAX_ERR_BUF], *estr;
019928
+	int i, ret = 0;
019928
+	int status;
019928
+
019928
+	new = alloc_context(my_mapfmt, argc, argv);
019928
+	if (!new)
019928
+		return 1;
019928
+
019928
+	for (i = 0; i < new->n; i++) {
019928
+		if (i >= ctxt->n) {
019928
+			new->m[i].mod = nss_open_lookup(my_mapfmt,
019928
+							new->m[i].argc,
019928
+							new->m[i].argv);
019928
+			if (!new->m[i].mod) {
019928
+				logerr(MODPREFIX "error opening module");
019928
+				/* TODO: check */
019928
+				ret = 1;
019928
+				goto out;
019928
+			}
019928
+			continue;
019928
+		}
019928
+
019928
+		if (*new->m[i].argv[0] == '/') {
019928
+			if (strcmp(new->m[i].argv[0], ctxt->m[i].argv[0]))
019928
+				open_lookup("file", MODPREFIX,
019928
+					     my_mapfmt,
019928
+					     new->m[i].argc,
019928
+					     new->m[i].argv,
019928
+					     &new->m[i].mod);
019928
+			else {
019928
+				new->m[i].mod = ctxt->m[i].mod;
019928
+				if (reinit_lookup(new->m[i].mod, "file",
019928
+					      MODPREFIX, my_mapfmt,
019928
+					      new->m[i].argc, new->m[i].argv))
019928
+					new->m[i].mod = NULL;
019928
+				else
019928
+					ctxt->m[i].mod = NULL;
019928
+			}
019928
+			continue;
019928
+		}
019928
+
019928
+		if (!strncmp(new->m[i].argv[0], "file", 4) ||
019928
+		    !strncmp(new->m[i].argv[0], "yp", 2) ||
019928
+		    !strncmp(new->m[i].argv[0], "nisplus", 7) ||
019928
+		    !strncmp(new->m[i].argv[0], "nis", 3) ||
019928
+		    !strncmp(new->m[i].argv[0], "ldaps", 5) ||
019928
+		    !strncmp(new->m[i].argv[0], "ldap", 4) ||
019928
+		    !strncmp(new->m[i].argv[0], "sss", 3)) {
019928
+			char type[MAX_MAP_TYPE_STRING];
019928
+			char *fmt;
019928
+
019928
+			strcpy(type, new->m[i].argv[0]);
019928
+			fmt = strchr(type, ',');
019928
+			if (!fmt)
019928
+				fmt = (char *) my_mapfmt;
019928
+			else {
019928
+				*fmt = '\0';
019928
+				fmt++;
019928
+			}
019928
+
019928
+			if (!strcmp(new->m[i].argv[0], ctxt->m[i].argv[0]) &&
019928
+			    !strcmp(new->m[i].argv[1], ctxt->m[i].argv[1])) {
019928
+				new->m[i].mod = ctxt->m[i].mod;
019928
+				if (reinit_lookup(new->m[i].mod, new->m[i].argv[0],
019928
+					      MODPREFIX, fmt,
019928
+					      new->m[i].argc - 1, new->m[i].argv + 1))
019928
+					new->m[i].mod = NULL;
019928
+				else
019928
+					ctxt->m[i].mod = NULL;
019928
+			} else {
019928
+				open_lookup(type, MODPREFIX, fmt,
019928
+					    new->m[i].argc - 1,
019928
+					    new->m[i].argv + 1,
019928
+					    &new->m[i].mod);
019928
+			}
019928
+			continue;
019928
+		}
019928
+
019928
+		INIT_LIST_HEAD(&nsslist);
019928
+
019928
+		if (nsswitch_parse(&nsslist)) {
019928
+			if (!list_empty(&nsslist))
019928
+				free_sources(&nsslist);
019928
+			logerr("can't to read name service switch config.");
019928
+			/* TODO: check */
019928
+			ret = 1;
019928
+			goto out;
019928
+		}
019928
+
019928
+		head = &nsslist;
019928
+		list_for_each(p, head) {
019928
+			struct nss_source *this;
019928
+
019928
+			this = list_entry(p, struct nss_source, list);
019928
+
019928
+			if (!strcmp(this->source, ctxt->m[i].mod->type)) {
019928
+				new->m[i].mod = ctxt->m[i].mod;
019928
+				if (reinit_lookup(new->m[i].mod, this->source,
019928
+					      MODPREFIX, my_mapfmt,
019928
+					      new->m[i].argc, new->m[i].argv))
019928
+					new->m[i].mod = NULL;
019928
+				else
019928
+					ctxt->m[i].mod = NULL;
019928
+				continue;
019928
+			}
019928
+
019928
+			if (!strcmp(this->source, "files")) {
019928
+				char src_file[] = "file";
019928
+				char src_prog[] = "program";
019928
+				struct stat st;
019928
+				char *type, *path, *save_argv0;
019928
+
019928
+				path = malloc(strlen(AUTOFS_MAP_DIR) +
019928
+					      strlen(new->m[i].argv[0]) + 2);
019928
+				if (!path) {
019928
+					estr = strerror_r(errno, buf, MAX_ERR_BUF);
019928
+					logerr(MODPREFIX "error: %s", estr);
019928
+					free_sources(&nsslist);
019928
+					ret = 1;
019928
+					goto out;
019928
+				}
019928
+				strcpy(path, AUTOFS_MAP_DIR);
019928
+				strcat(path, "/");
019928
+				strcat(path, new->m[i].argv[0]);
019928
+
019928
+				if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) {
019928
+					free(path);
019928
+					continue;
019928
+				}
019928
+
019928
+				if (st.st_mode & __S_IEXEC)
019928
+					type = src_prog;
019928
+				else
019928
+					type = src_file;
019928
+
019928
+				save_argv0 = (char *) new->m[i].argv[0];
019928
+				new->m[i].argv[0] = path;
019928
+
019928
+				if (strcmp(type, ctxt->m[i].mod->type)) {
019928
+					status = open_lookup(type,
019928
+							     MODPREFIX,
019928
+							     my_mapfmt,
019928
+							     new->m[i].argc,
019928
+							     new->m[i].argv,
019928
+							     &new->m[i].mod);
019928
+					if (status == NSS_STATUS_SUCCESS) {
019928
+						free(save_argv0);
019928
+						break;
019928
+					}
019928
+				} else {
019928
+					new->m[i].mod = ctxt->m[i].mod;
019928
+					if (reinit_lookup(new->m[i].mod, type,
019928
+						      MODPREFIX, my_mapfmt,
019928
+						      new->m[i].argc, new->m[i].argv))
019928
+						new->m[i].mod = NULL;
019928
+					else {
019928
+						ctxt->m[i].mod = NULL;
019928
+						free(save_argv0);
019928
+						break;
019928
+					}
019928
+				}
019928
+
019928
+				new->m[i].argv[0] = save_argv0;
019928
+				free(path);
019928
+				continue;
019928
+			}
019928
+
019928
+			if (strcmp(this->source, ctxt->m[i].mod->type)) {
019928
+				status = open_lookup(this->source, MODPREFIX,
019928
+						     my_mapfmt,
019928
+						     new->m[i].argc,
019928
+						     new->m[i].argv,
019928
+						     &new->m[i].mod);
019928
+				if (status == NSS_STATUS_SUCCESS)
019928
+					break;
019928
+			} else {
019928
+				new->m[i].mod = ctxt->m[i].mod;
019928
+				if (reinit_lookup(new->m[i].mod, this->source,
019928
+					      MODPREFIX, my_mapfmt,
019928
+					      new->m[i].argc, new->m[i].argv))
019928
+					new->m[i].mod = NULL;
019928
+				else {
019928
+					ctxt->m[i].mod = NULL;
019928
+					break;
019928
+				}
019928
+			}
019928
+		}
019928
+		free_sources(&nsslist);
019928
+	}
019928
+out:
019928
+	/* Update new context with any needed old context */
019928
+	*context = update_multi_context(ctxt, new);
019928
+	free_multi_context(ctxt);
019928
+	free(ctxt);
019928
+
019928
+	return ret;
019928
 }
019928
 
019928
 int lookup_read_master(struct master *master, time_t age, void *context)