Blame SOURCES/bz1366616-local_site_resolved_prevents_segfault.patch

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