From c8dc4925d1e0f5d5086a4c15415ee9fb0b7e6155 Mon Sep 17 00:00:00 2001
From: Drew DeVault <sir@cmpwn.com>
Date: Fri, 2 Dec 2016 17:34:26 -0500
Subject: Add IPC security policy command handlers

---
 sway/commands/ipc.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 140 insertions(+)
 create mode 100644 sway/commands/ipc.c

(limited to 'sway/commands')

diff --git a/sway/commands/ipc.c b/sway/commands/ipc.c
new file mode 100644
index 00000000..e6ae27a4
--- /dev/null
+++ b/sway/commands/ipc.c
@@ -0,0 +1,140 @@
+#include <stdio.h>
+#include <string.h>
+#include "sway/commands.h"
+#include "sway/config.h"
+#include "ipc.h"
+#include "log.h"
+#include "util.h"
+
+struct cmd_results *cmd_ipc(int argc, char **argv) {
+	struct cmd_results *error = NULL;
+	if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) {
+		return error;
+	}
+
+	if (config->reading && strcmp("{", argv[0]) != 0) {
+		return cmd_results_new(CMD_INVALID, "ipc",
+				"Expected '{' at start of IPC config definition.");
+	}
+
+	if (!config->reading) {
+		return cmd_results_new(CMD_FAILURE, "ipc", "Can only be used in config file.");
+	}
+
+	return cmd_results_new(CMD_BLOCK_IPC, NULL, NULL);
+}
+
+struct cmd_results *cmd_ipc_events(int argc, char **argv) {
+	struct cmd_results *error = NULL;
+	if ((error = checkarg(argc, "events", EXPECTED_EQUAL_TO, 1))) {
+		return error;
+	}
+
+	if (config->reading && strcmp("{", argv[0]) != 0) {
+		return cmd_results_new(CMD_INVALID, "events",
+				"Expected '{' at start of IPC event config definition.");
+	}
+
+	if (!config->reading) {
+		return cmd_results_new(CMD_FAILURE, "events", "Can only be used in config file.");
+	}
+
+	return cmd_results_new(CMD_BLOCK_IPC_EVENTS, NULL, NULL);
+}
+
+struct cmd_results *cmd_ipc_cmd(int argc, char **argv) {
+	struct cmd_results *error = NULL;
+	if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) {
+		return error;
+	}
+
+	bool enabled;
+	if (strcmp(argv[0], "enabled") == 0) {
+		enabled = true;
+	} else if (strcmp(argv[0], "disabled") == 0) {
+		enabled = false;
+	} else {
+		return cmd_results_new(CMD_INVALID, argv[-1],
+				"Argument must be one of 'enabled' or 'disabled'");
+	}
+
+	struct {
+		char *name;
+		enum ipc_command_type type;
+	} types[] = {
+		{ "command", IPC_COMMAND },
+		{ "workspaces", IPC_GET_WORKSPACES },
+		{ "outputs", IPC_GET_OUTPUTS },
+		{ "tree", IPC_GET_TREE },
+		{ "marks", IPC_GET_MARKS },
+		{ "bar-config", IPC_GET_BAR_CONFIG },
+		{ "inputs", IPC_GET_INPUTS },
+	};
+
+	uint32_t type = 0;
+
+	for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) {
+		if (strcmp(types[i].name, argv[-1]) == 0) {
+			type = types[i].type;
+			break;
+		}
+	}
+
+	if (enabled) {
+		config->ipc_policy |= type;
+		sway_log(L_DEBUG, "Enabled IPC %s feature", argv[-1]);
+	} else {
+		config->ipc_policy &= ~type;
+		sway_log(L_DEBUG, "Disabled IPC %s feature", argv[-1]);
+	}
+
+	return cmd_results_new(CMD_SUCCESS, NULL, NULL);
+}
+
+struct cmd_results *cmd_ipc_event_cmd(int argc, char **argv) {
+	struct cmd_results *error = NULL;
+	if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) {
+		return error;
+	}
+
+	bool enabled;
+	if (strcmp(argv[0], "enabled") == 0) {
+		enabled = true;
+	} else if (strcmp(argv[0], "disabled") == 0) {
+		enabled = false;
+	} else {
+		return cmd_results_new(CMD_INVALID, argv[-1],
+				"Argument must be one of 'enabled' or 'disabled'");
+	}
+
+	struct {
+		char *name;
+		enum ipc_command_type type;
+	} types[] = {
+		{ "workspace", event_mask(IPC_EVENT_WORKSPACE) },
+		{ "output", event_mask(IPC_EVENT_OUTPUT) },
+		{ "mode", event_mask(IPC_EVENT_MODE) },
+		{ "window", event_mask(IPC_EVENT_WINDOW) },
+		{ "binding", event_mask(IPC_EVENT_BINDING) },
+		{ "input", event_mask(IPC_EVENT_INPUT) },
+	};
+
+	uint32_t type = 0;
+
+	for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) {
+		if (strcmp(types[i].name, argv[-1]) == 0) {
+			type = types[i].type;
+			break;
+		}
+	}
+
+	if (enabled) {
+		config->ipc_policy |= type;
+		sway_log(L_DEBUG, "Enabled IPC %s event", argv[-1]);
+	} else {
+		config->ipc_policy &= ~type;
+		sway_log(L_DEBUG, "Disabled IPC %s event", argv[-1]);
+	}
+
+	return cmd_results_new(CMD_SUCCESS, NULL, NULL);
+}
-- 
cgit v1.2.3