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

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