Blame SOURCES/bz1366616-local_site_resolved_prevents_segfault.patch

24db9e
From 5b8a336bb3d7a25ba43f444bc586b27fb9f42746 Mon Sep 17 00:00:00 2001
24db9e
From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= <jpokorny@redhat.com>
24db9e
Date: Tue, 13 Sep 2016 17:47:03 +0200
24db9e
Subject: [PATCH 1/4] Low: make find_site_by_name failure set error code
24db9e
24db9e
At one instance (query_get_string_answer) it just flips the sign as it
24db9e
is customary to return negative value upon error (for uniform
24db9e
treatment).
24db9e
---
24db9e
 src/attr.c | 2 ++
24db9e
 src/main.c | 3 ++-
24db9e
 2 files changed, 4 insertions(+), 1 deletion(-)
24db9e
24db9e
diff --git a/src/attr.c b/src/attr.c
24db9e
index d9e5c91..0e407b6 100644
24db9e
--- a/src/attr.c
24db9e
+++ b/src/attr.c
24db9e
@@ -16,6 +16,7 @@
24db9e
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24db9e
  */
24db9e
 
24db9e
+#include <errno.h>
24db9e
 #include <stdio.h>
24db9e
 #include <string.h>
24db9e
 #include "attr.h"
24db9e
@@ -162,6 +163,7 @@ int do_attr_command(cmd_request_t cmd)
24db9e
 	else {
24db9e
 		if (!find_site_by_name(cl.site, &site, 1)) {
24db9e
 			log_error("Site \"%s\" not configured.", cl.site);
24db9e
+			rv = -ENOENT;
24db9e
 			goto out_close;
24db9e
 		}
24db9e
 	}
24db9e
diff --git a/src/main.c b/src/main.c
24db9e
index 206c881..b1ff1e7 100644
24db9e
--- a/src/main.c
24db9e
+++ b/src/main.c
24db9e
@@ -665,7 +665,7 @@ static int query_get_string_answer(cmd_request_t cmd)
24db9e
 		site = local;
24db9e
 	else if (!find_site_by_name(cl.site, &site, 1)) {
24db9e
 		log_error("cannot find site \"%s\"", cl.site);
24db9e
-		rv = ENOENT;
24db9e
+		rv = -ENOENT;
24db9e
 		goto out;
24db9e
 	}
24db9e
 
24db9e
@@ -741,6 +741,7 @@ static int do_command(cmd_request_t cmd)
24db9e
 	else {
24db9e
 		if (!find_site_by_name(cl.site, &site, 1)) {
24db9e
 			log_error("Site \"%s\" not configured.", cl.site);
24db9e
+			rv = -ENOENT;
24db9e
 			goto out_close;
24db9e
 		}
24db9e
 	}
24db9e
-- 
24db9e
2.4.11
24db9e
24db9e
24db9e
From bcf1117d7e1f37165c6d0da022cadc63e391a2fa Mon Sep 17 00:00:00 2001
24db9e
From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= <jpokorny@redhat.com>
24db9e
Date: Tue, 13 Sep 2016 18:20:17 +0200
24db9e
Subject: [PATCH 2/4] High: ensure local site resolved for all effective
24db9e
 actions
24db9e
24db9e
Previously, running:
24db9e
24db9e
  touch /etc/booth/booth.conf
24db9e
  booth grant a_ticket
24db9e
24db9e
would result in a segfault due to not guarding resolution of local
24db9e
site properly in some circumstances, so do it at the central place.
24db9e
Also error messaging is now centralized.
24db9e
---
24db9e
 src/main.c | 19 ++++---------------
24db9e
 1 file changed, 4 insertions(+), 15 deletions(-)
24db9e
24db9e
diff --git a/src/main.c b/src/main.c
24db9e
index b1ff1e7..c05446f 100644
24db9e
--- a/src/main.c
24db9e
+++ b/src/main.c
24db9e
@@ -377,9 +377,10 @@ static int setup_config(int type)
24db9e
 			return -EINVAL;
24db9e
 		}
24db9e
 		local->local = 1;
24db9e
-	} else
24db9e
-		find_myself(NULL, type == CLIENT || type == GEOSTORE);
24db9e
-
24db9e
+	} else if (!find_myself(NULL, type == CLIENT || type == GEOSTORE)) {
24db9e
+		log_error("Cannot find myself in the configuration.");
24db9e
+		return -EINVAL;
24db9e
+	}
24db9e
 
24db9e
 	rv = check_config(type);
24db9e
 	if (rv < 0)
24db9e
@@ -1302,13 +1303,6 @@ static int do_status(int type)
24db9e
 		goto quit;
24db9e
 	}
24db9e
 
24db9e
-
24db9e
-	if (!local) {
24db9e
-		reason = "No Service IP active here.";
24db9e
-		goto quit;
24db9e
-	}
24db9e
-
24db9e
-
24db9e
 	rv = _lockfile(O_RDWR, &status_lock_fd, &pid;;
24db9e
 	if (status_lock_fd == -1) {
24db9e
 		reason = "No PID file.";
24db9e
@@ -1422,11 +1416,6 @@ static int do_server(int type)
24db9e
 	if (rv < 0)
24db9e
 		return rv;
24db9e
 
24db9e
-	if (!local) {
24db9e
-		log_error("Cannot find myself in the configuration.");
24db9e
-		exit(EXIT_FAILURE);
24db9e
-	}
24db9e
-
24db9e
 	if (daemonize) {
24db9e
 		if (daemon(0, 0) < 0) {
24db9e
 			perror("daemon error");
24db9e
-- 
24db9e
2.4.11
24db9e
24db9e
24db9e
From 1185487afbd2a063664863f7bd98d1480ca0a2dd Mon Sep 17 00:00:00 2001
24db9e
From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= <jpokorny@redhat.com>
24db9e
Date: Tue, 13 Sep 2016 20:18:33 +0200
24db9e
Subject: [PATCH 3/4] Low: make daemon with "-s site" (debug mode) claim
24db9e
 "myself"
24db9e
24db9e
---
24db9e
 src/main.c | 4 ++--
24db9e
 1 file changed, 2 insertions(+), 2 deletions(-)
24db9e
24db9e
diff --git a/src/main.c b/src/main.c
24db9e
index c05446f..76e62c7 100644
24db9e
--- a/src/main.c
24db9e
+++ b/src/main.c
24db9e
@@ -372,8 +372,8 @@ static int setup_config(int type)
24db9e
 	/* Set "local" pointer, ignoring errors. */
24db9e
 	if (cl.type == DAEMON && cl.site[0]) {
24db9e
 		if (!find_site_by_name(cl.site, &local, 1)) {
24db9e
-			log_error("Cannot find \"%s\" in the configuration.",
24db9e
-					cl.site);
24db9e
+			log_error("Cannot find \"%s\" (myself) in the configuration.",
24db9e
+				  cl.site);
24db9e
 			return -EINVAL;
24db9e
 		}
24db9e
 		local->local = 1;
24db9e
-- 
24db9e
2.4.11
24db9e
24db9e
24db9e
From 736f58db41acd32b2ea2af1b4c0ba02683d58cf5 Mon Sep 17 00:00:00 2001
24db9e
From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= <jpokorny@redhat.com>
24db9e
Date: Tue, 13 Sep 2016 20:10:13 +0200
24db9e
Subject: [PATCH 4/4] Refactor: call find_site_by_name just once, up the stream
24db9e
24db9e
Respective logic was duplicated for all "booth list/peers/grant/revoke"
24db9e
and "geostore list/get/set/del" separately, so utilize a natural control
24db9e
flow to carry this once-resolved target site from here, sharing it with
24db9e
the special case of "daemon" role invoked with "-s site" (debug mode).
24db9e
Side effect: simpler, terser code.
24db9e
---
24db9e
 src/attr.c | 13 +----------
24db9e
 src/attr.h |  2 +-
24db9e
 src/main.c | 75 ++++++++++++++++++++++++++++----------------------------------
24db9e
 3 files changed, 36 insertions(+), 54 deletions(-)
24db9e
24db9e
diff --git a/src/attr.c b/src/attr.c
24db9e
index 0e407b6..805ccb3 100644
24db9e
--- a/src/attr.c
24db9e
+++ b/src/attr.c
24db9e
@@ -150,24 +150,13 @@ static int read_server_reply(
24db9e
 	return rv;
24db9e
 }
24db9e
 
24db9e
-int do_attr_command(cmd_request_t cmd)
24db9e
+int do_attr_command(cmd_request_t cmd, struct booth_site *site)
24db9e
 {
24db9e
-	struct booth_site *site = NULL;
24db9e
 	struct boothc_header *header;
24db9e
 	struct booth_transport const *tpt;
24db9e
 	int len, rv = -1;
24db9e
 	char *msg = NULL;
24db9e
 
24db9e
-	if (!*cl.site)
24db9e
-		site = local;
24db9e
-	else {
24db9e
-		if (!find_site_by_name(cl.site, &site, 1)) {
24db9e
-			log_error("Site \"%s\" not configured.", cl.site);
24db9e
-			rv = -ENOENT;
24db9e
-			goto out_close;
24db9e
-		}
24db9e
-	}
24db9e
-
24db9e
 	if (site->type == ARBITRATOR) {
24db9e
 		if (site == local) {
24db9e
 			log_error("We're just an arbitrator, no attributes here.");
24db9e
diff --git a/src/attr.h b/src/attr.h
24db9e
index 1c680bd..a94ac16 100644
24db9e
--- a/src/attr.h
24db9e
+++ b/src/attr.h
24db9e
@@ -31,7 +31,7 @@
24db9e
 
24db9e
 void print_geostore_usage(void);
24db9e
 int test_attr_reply(cmd_result_t reply_code, cmd_request_t cmd);
24db9e
-int do_attr_command(cmd_request_t cmd);
24db9e
+int do_attr_command(cmd_request_t cmd, struct booth_site *site);
24db9e
 int process_attr_request(struct client *req_client, void *buf);
24db9e
 int attr_recv(void *buf, struct booth_site *source);
24db9e
 int store_geo_attr(struct ticket_config *tk, const char *name, const char *val, int notime);
24db9e
diff --git a/src/main.c b/src/main.c
24db9e
index 76e62c7..e09536a 100644
24db9e
--- a/src/main.c
24db9e
+++ b/src/main.c
24db9e
@@ -346,7 +346,7 @@ int update_authkey()
24db9e
 	return 0;
24db9e
 }
24db9e
 
24db9e
-static int setup_config(int type)
24db9e
+static int setup_config(int type, struct booth_site **site)
24db9e
 {
24db9e
 	int rv;
24db9e
 
24db9e
@@ -369,18 +369,31 @@ static int setup_config(int type)
24db9e
 #endif
24db9e
 	}
24db9e
 
24db9e
-	/* Set "local" pointer, ignoring errors. */
24db9e
-	if (cl.type == DAEMON && cl.site[0]) {
24db9e
-		if (!find_site_by_name(cl.site, &local, 1)) {
24db9e
-			log_error("Cannot find \"%s\" (myself) in the configuration.",
24db9e
-				  cl.site);
24db9e
-			return -EINVAL;
24db9e
+	/* Determine the target based on the provided address, ignore
24db9e
+	   errors with DAEMON (special debug/testing arrangement). */
24db9e
+	if (*cl.site && (cl.type == DAEMON || (site && strcmp(cl.site, OTHER_SITE)))) {
24db9e
+		if (!find_site_by_name(cl.site, cl.type == DAEMON ? &local : site, 1)) {
24db9e
+			log_error("Cannot find \"%s\"%s in the configuration.",
24db9e
+				  cl.site, cl.type == DAEMON ? " (myself)" : "");
24db9e
+			if (cl.type != DAEMON)
24db9e
+				return -EINVAL;
24db9e
 		}
24db9e
-		local->local = 1;
24db9e
-	} else if (!find_myself(NULL, type == CLIENT || type == GEOSTORE)) {
24db9e
+		if (cl.type == DAEMON)
24db9e
+			local->local = 1;
24db9e
+		else
24db9e
+			site = NULL;  /* prevent from overwriting */
24db9e
+	}
24db9e
+	/* Self-determine us. */
24db9e
+	if (!find_myself(site, type == CLIENT || type == GEOSTORE)) {
24db9e
 		log_error("Cannot find myself in the configuration.");
24db9e
 		return -EINVAL;
24db9e
 	}
24db9e
+	/* We can resolve "other" only after we've determined us. */
24db9e
+	if (*cl.site && site && !strcmp(cl.site, OTHER_SITE)
24db9e
+			&& !find_site_by_name(cl.site, site, 1)) {
24db9e
+		log_error("Cannot find %s node in the configuration.", cl.site);
24db9e
+		return -EINVAL;
24db9e
+	}
24db9e
 
24db9e
 	rv = check_config(type);
24db9e
 	if (rv < 0)
24db9e
@@ -635,9 +648,8 @@ static int test_reply(cmd_result_t reply_code, cmd_request_t cmd)
24db9e
 	return rv;
24db9e
 }
24db9e
 
24db9e
-static int query_get_string_answer(cmd_request_t cmd)
24db9e
+static int query_get_string_answer(cmd_request_t cmd, struct booth_site *site)
24db9e
 {
24db9e
-	struct booth_site *site;
24db9e
 	struct boothc_hdr_msg reply;
24db9e
 	struct boothc_header *header;
24db9e
 	char *data;
24db9e
@@ -662,14 +674,6 @@ static int query_get_string_answer(cmd_request_t cmd)
24db9e
 
24db9e
 	init_header(header, cmd, 0, cl.options, 0, 0, msg_size);
24db9e
 
24db9e
-	if (!*cl.site)
24db9e
-		site = local;
24db9e
-	else if (!find_site_by_name(cl.site, &site, 1)) {
24db9e
-		log_error("cannot find site \"%s\"", cl.site);
24db9e
-		rv = -ENOENT;
24db9e
-		goto out;
24db9e
-	}
24db9e
-
24db9e
 	tpt = booth_transport + TCP;
24db9e
 	rv = tpt->open(site);
24db9e
 	if (rv < 0)
24db9e
@@ -709,16 +713,14 @@ out_test_reply:
24db9e
 	rv = test_reply_f(ntohl(reply.header.result), cmd);
24db9e
 out_close:
24db9e
 	tpt->close(site);
24db9e
-out:
24db9e
 	if (data)
24db9e
 		free(data);
24db9e
 	return rv;
24db9e
 }
24db9e
 
24db9e
 
24db9e
-static int do_command(cmd_request_t cmd)
24db9e
+static int do_command(cmd_request_t cmd, struct booth_site *site)
24db9e
 {
24db9e
-	struct booth_site *site;
24db9e
 	struct boothc_ticket_msg reply;
24db9e
 	struct booth_transport const *tpt;
24db9e
 	uint32_t leader_id;
24db9e
@@ -732,21 +734,10 @@ static int do_command(cmd_request_t cmd)
24db9e
 		op_str = "revoke";
24db9e
 
24db9e
 	rv = 0;
24db9e
-	site = NULL;
24db9e
 
24db9e
 	/* Always use TCP for client - at least for now. */
24db9e
 	tpt = booth_transport + TCP;
24db9e
 
24db9e
-	if (!*cl.site)
24db9e
-		site = local;
24db9e
-	else {
24db9e
-		if (!find_site_by_name(cl.site, &site, 1)) {
24db9e
-			log_error("Site \"%s\" not configured.", cl.site);
24db9e
-			rv = -ENOENT;
24db9e
-			goto out_close;
24db9e
-		}
24db9e
-	}
24db9e
-
24db9e
 	if (site->type == ARBITRATOR) {
24db9e
 		if (site == local) {
24db9e
 			log_error("We're just an arbitrator, cannot grant/revoke tickets here.");
24db9e
@@ -1296,7 +1287,7 @@ static int do_status(int type)
24db9e
 
24db9e
 	ret = PCMK_OCF_NOT_RUNNING;
24db9e
 
24db9e
-	rv = setup_config(type);
24db9e
+	rv = setup_config(type, NULL);
24db9e
 	if (rv) {
24db9e
 		reason = "Error reading configuration.";
24db9e
 		ret = PCMK_OCF_UNKNOWN_ERROR;
24db9e
@@ -1412,7 +1403,7 @@ static int do_server(int type)
24db9e
 	int rv = -1;
24db9e
 	static char log_ent[128] = DAEMON_NAME "-";
24db9e
 
24db9e
-	rv = setup_config(type);
24db9e
+	rv = setup_config(type, NULL);
24db9e
 	if (rv < 0)
24db9e
 		return rv;
24db9e
 
24db9e
@@ -1478,8 +1469,9 @@ static int do_server(int type)
24db9e
 static int do_client(void)
24db9e
 {
24db9e
 	int rv;
24db9e
+	struct booth_site *site;
24db9e
 
24db9e
-	rv = setup_config(CLIENT);
24db9e
+	rv = setup_config(CLIENT, &site);
24db9e
 	if (rv < 0) {
24db9e
 		log_error("cannot read config");
24db9e
 		goto out;
24db9e
@@ -1488,12 +1480,12 @@ static int do_client(void)
24db9e
 	switch (cl.op) {
24db9e
 	case CMD_LIST:
24db9e
 	case CMD_PEERS:
24db9e
-		rv = query_get_string_answer(cl.op);
24db9e
+		rv = query_get_string_answer(cl.op, site);
24db9e
 		break;
24db9e
 
24db9e
 	case CMD_GRANT:
24db9e
 	case CMD_REVOKE:
24db9e
-		rv = do_command(cl.op);
24db9e
+		rv = do_command(cl.op, site);
24db9e
 		break;
24db9e
 	}
24db9e
 
24db9e
@@ -1504,8 +1496,9 @@ out:
24db9e
 static int do_attr(void)
24db9e
 {
24db9e
 	int rv = -1;
24db9e
+	struct booth_site *site;
24db9e
 
24db9e
-	rv = setup_config(GEOSTORE);
24db9e
+	rv = setup_config(GEOSTORE, &site);
24db9e
 	if (rv < 0) {
24db9e
 		log_error("cannot read config");
24db9e
 		goto out;
24db9e
@@ -1529,12 +1522,12 @@ static int do_attr(void)
24db9e
 	switch (cl.op) {
24db9e
 	case ATTR_LIST:
24db9e
 	case ATTR_GET:
24db9e
-		rv = query_get_string_answer(cl.op);
24db9e
+		rv = query_get_string_answer(cl.op, site);
24db9e
 		break;
24db9e
 
24db9e
 	case ATTR_SET:
24db9e
 	case ATTR_DEL:
24db9e
-		rv = do_attr_command(cl.op);
24db9e
+		rv = do_attr_command(cl.op, site);
24db9e
 		break;
24db9e
 	}
24db9e
 
24db9e
-- 
24db9e
2.4.11
24db9e