aboutsummaryrefslogtreecommitdiff
path: root/sway/commands
diff options
context:
space:
mode:
Diffstat (limited to 'sway/commands')
-rw-r--r--sway/commands/commands.c23
-rw-r--r--sway/commands/ipc.c140
-rw-r--r--sway/commands/permit.c94
3 files changed, 257 insertions, 0 deletions
diff --git a/sway/commands/commands.c b/sway/commands/commands.c
new file mode 100644
index 00000000..5d248e30
--- /dev/null
+++ b/sway/commands/commands.c
@@ -0,0 +1,23 @@
+#include <stdbool.h>
+#include <string.h>
+#include "sway/commands.h"
+#include "sway/config.h"
+#include "list.h"
+#include "log.h"
+
+struct cmd_results *cmd_commands(int argc, char **argv) {
+ struct cmd_results *error = NULL;
+ if ((error = checkarg(argc, "commands", EXPECTED_EQUAL_TO, 1))) {
+ return error;
+ }
+
+ if (strcmp(argv[0], "{") != 0) {
+ return cmd_results_new(CMD_FAILURE, "commands", "Expected block declaration");
+ }
+
+ if (!config->reading) {
+ return cmd_results_new(CMD_FAILURE, "commands", "Can only be used in config file.");
+ }
+
+ return cmd_results_new(CMD_BLOCK_COMMANDS, NULL, NULL);
+}
diff --git a/sway/commands/ipc.c b/sway/commands/ipc.c
new file mode 100644
index 00000000..222be0dd
--- /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_feature type;
+ } types[] = {
+ { "command", IPC_FEATURE_COMMAND },
+ { "workspaces", IPC_FEATURE_GET_WORKSPACES },
+ { "outputs", IPC_FEATURE_GET_OUTPUTS },
+ { "tree", IPC_FEATURE_GET_TREE },
+ { "marks", IPC_FEATURE_GET_MARKS },
+ { "bar-config", IPC_FEATURE_GET_BAR_CONFIG },
+ { "inputs", IPC_FEATURE_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_feature type;
+ } types[] = {
+ { "workspace", IPC_FEATURE_EVENT_WORKSPACE },
+ { "output", IPC_FEATURE_EVENT_OUTPUT },
+ { "mode", IPC_FEATURE_EVENT_MODE },
+ { "window", IPC_FEATURE_EVENT_WINDOW },
+ { "binding", IPC_FEATURE_EVENT_BINDING },
+ { "input", IPC_FEATURE_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);
+}
diff --git a/sway/commands/permit.c b/sway/commands/permit.c
new file mode 100644
index 00000000..7a25e4ce
--- /dev/null
+++ b/sway/commands/permit.c
@@ -0,0 +1,94 @@
+#include <string.h>
+#include "sway/commands.h"
+#include "sway/config.h"
+#include "sway/security.h"
+#include "log.h"
+
+static enum secure_feature get_features(int argc, char **argv,
+ struct cmd_results **error) {
+ enum secure_feature features = 0;
+
+ struct {
+ char *name;
+ enum secure_feature feature;
+ } feature_names[] = {
+ { "lock", FEATURE_LOCK },
+ { "panel", FEATURE_PANEL },
+ { "background", FEATURE_BACKGROUND },
+ { "screenshot", FEATURE_SCREENSHOT },
+ { "fullscreen", FEATURE_FULLSCREEN },
+ { "keyboard", FEATURE_KEYBOARD },
+ { "mouse", FEATURE_MOUSE },
+ { "ipc", FEATURE_IPC },
+ };
+
+ for (int i = 1; i < argc; ++i) {
+ size_t j;
+ for (j = 0; j < sizeof(feature_names) / sizeof(feature_names[0]); ++j) {
+ if (strcmp(feature_names[j].name, argv[i]) == 0) {
+ break;
+ }
+ }
+ if (j == sizeof(feature_names) / sizeof(feature_names[0])) {
+ *error = cmd_results_new(CMD_INVALID,
+ "permit", "Invalid feature grant %s", argv[i]);
+ return 0;
+ }
+ features |= feature_names[j].feature;
+ }
+ return features;
+}
+
+static struct feature_policy *get_policy(const char *name) {
+ struct feature_policy *policy = NULL;
+ for (int i = 0; i < config->feature_policies->length; ++i) {
+ struct feature_policy *p = config->feature_policies->items[i];
+ if (strcmp(p->program, name) == 0) {
+ policy = p;
+ break;
+ }
+ }
+ if (!policy) {
+ policy = alloc_feature_policy(name);
+ list_add(config->feature_policies, policy);
+ }
+ return policy;
+}
+
+struct cmd_results *cmd_permit(int argc, char **argv) {
+ struct cmd_results *error = NULL;
+ if ((error = checkarg(argc, "permit", EXPECTED_MORE_THAN, 1))) {
+ return error;
+ }
+
+ struct feature_policy *policy = get_policy(argv[0]);
+ policy->features |= get_features(argc, argv, &error);
+
+ if (error) {
+ return error;
+ }
+
+ sway_log(L_DEBUG, "Permissions granted to %s for features %d",
+ policy->program, policy->features);
+
+ return cmd_results_new(CMD_SUCCESS, NULL, NULL);
+}
+
+struct cmd_results *cmd_reject(int argc, char **argv) {
+ struct cmd_results *error = NULL;
+ if ((error = checkarg(argc, "reject", EXPECTED_MORE_THAN, 1))) {
+ return error;
+ }
+
+ struct feature_policy *policy = get_policy(argv[0]);
+ policy->features &= ~get_features(argc, argv, &error);
+
+ if (error) {
+ return error;
+ }
+
+ sway_log(L_DEBUG, "Permissions granted to %s for features %d",
+ policy->program, policy->features);
+
+ return cmd_results_new(CMD_SUCCESS, NULL, NULL);
+}