Blame SOURCES/bz1366616-local_site_resolved_prevents_segfault.patch

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