summaryrefslogtreecommitdiffstats
path: root/src/rest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rest.c')
-rw-r--r--src/rest.c141
1 files changed, 137 insertions, 4 deletions
diff --git a/src/rest.c b/src/rest.c
index b58b468..5cd6791 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -6388,6 +6388,135 @@ static int og_client_ok(struct og_client *cli, char *buf_reply)
return 0;
}
+static const char *og_method_str[] = {
+ [OG_METHOD_GET] = "GET",
+ [OG_METHOD_POST] = "POST",
+ [OG_METHOD_DELETE] = "DELETE",
+ [OG_METHOD_NO_HTTP] = "NOHTTP",
+};
+
+static const char *og_method(enum og_rest_method method)
+{
+ if (method > OG_METHOD_NO_HTTP)
+ return "UNKNOWN";
+
+ return og_method_str[method];
+}
+
+struct {
+ const char *uri;
+} og_uri_handler[] = {
+ [OG_URI_UNKNOWN] = { "unknown", },
+ [OG_URI_CLIENTS] = { "clients", },
+ [OG_URI_CLIENT_REPO] = { "client/repo", },
+ [OG_URI_CLIENT_SETUP] = { "client/setup", },
+ [OG_URI_CLIENT_SERVER] = { "client/server", },
+ [OG_URI_CLIENT_INFO] = { "client/info", },
+ [OG_URI_CLIENT_ADD] = { "client/add", },
+ [OG_URI_CLIENT_UPDATE] = { "client/update", },
+ [OG_URI_CLIENT_DELETE] = { "client/delete", },
+ [OG_URI_WOL] = { "wol", },
+ [OG_URI_SHELL_RUN] = { "shell/run", },
+ [OG_URI_SHELL_OUTPUT] = { "shell/output", },
+ [OG_URI_SESSION] = { "session", },
+ [OG_URI_SCOPES] = { "scopes", },
+ [OG_URI_POWEROFF] = { "poweroff", },
+ [OG_URI_REBOOT] = { "reboot", },
+ [OG_URI_BOOT_MODE] = { "mode", },
+ [OG_URI_STOP] = { "stop", },
+ [OG_URI_REFRESH] = { "refresh", },
+ [OG_URI_HARDWARE] = { "hardware", },
+ [OG_URI_SOFTWARE] = { "software", },
+ [OG_URI_REPO] = { "repositories", },
+ [OG_URI_REPO_ADD] = { "repository/add", },
+ [OG_URI_REPO_DELETE] = { "repository/delete", },
+ [OG_URI_IMAGES] = { "images", },
+ [OG_URI_IMAGE_CREATE] = { "image/create" },
+ [OG_URI_IMAGE_RESTORE] = { "image/restore", },
+ [OG_URI_IMAGE_DELETE] = { "image/delete", },
+ [OG_URI_PART_SETUP] = { "setup", },
+ [OG_URI_RUN_SCHEDULE] = { "run/schedule", },
+ [OG_URI_TASK_RUN] = { "task/run", },
+ [OG_URI_SCHEDULE_CREATE] = { "schedule/create", },
+ [OG_URI_SCHEDULE_DELETE] = { "schedule/delete", },
+ [OG_URI_SCHEDULE_UPDATE] = { "schedule/update", },
+ [OG_URI_SCHEDULE_GET] = { "schedule/get", },
+ [OG_URI_OGLIVE_LIST] = { "oglive/list", },
+ [OG_URI_OGLIVE_SET] = { "oglive/set", },
+ [OG_URI_CENTER_ADD] = { "center/add", },
+ [OG_URI_CENTER_DELETE] = { "center/delete", },
+ [OG_URI_ROOM_ADD] = { "room/add", },
+ [OG_URI_ROOM_DELETE] = { "room/delete", },
+ [OG_URI_PROC_ADD] = { "procedure/add", },
+ [OG_URI_PROC_UPDATE] = { "procedure/update", },
+ [OG_URI_PROC_RUN] = { "procedure/run", },
+ [OG_URI_SCHEDULE_RUN] = { "schedule/command", },
+ [OG_URI_PROC_DEL] = { "procedure/delete", },
+ [OG_URI_TASK_ADD] = { "task/add", },
+ [OG_URI_SERVER] = { "server", },
+ [OG_URI_STATS] = { "stats", },
+};
+
+static const char *og_uri(enum og_rest_uri uri)
+{
+ if (uri >= OG_URI_MAX)
+ return "unknown";
+
+ return og_uri_handler[uri].uri;
+}
+
+static enum og_rest_uri og_str_to_uri(const char *uri)
+{
+ int i;
+
+ for (i = 0; i < OG_URI_MAX; i++) {
+ if (!strncmp(og_uri_handler[i].uri, uri,
+ strlen(og_uri_handler[i].uri)))
+ return i;
+ }
+
+ return OG_URI_UNKNOWN;
+}
+
+static void og_rest_log(const struct og_client *cli, enum og_rest_method method,
+ enum og_rest_uri uri, const struct og_msg_params *params)
+{
+ char log_buf[(16 * OG_CLIENTS_MAX) + 4096] = {};
+ int i, ret;
+
+ switch (uri) {
+ case OG_URI_SCOPES:
+ case OG_URI_CLIENTS:
+ /* very spammy, do not log these. */
+ return;
+ default:
+ break;
+ }
+
+ ret = snprintf(log_buf, sizeof(log_buf), "%s:%hu %s /%s ",
+ inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port),
+ og_method(method), og_uri(uri));
+
+ if (params->ips_array_len > 0) {
+ ret += snprintf(&log_buf[ret], sizeof(log_buf) - ret, "clients=");
+ if (ret > sizeof(log_buf))
+ return;
+
+ for (i = 0; i < params->ips_array_len - 1; i++) {
+ ret += snprintf(&log_buf[ret], sizeof(log_buf) - ret,
+ "%s,", params->ips_array[i]);
+ if (ret > sizeof(log_buf))
+ return;
+ }
+
+ ret += snprintf(&log_buf[ret], sizeof(log_buf) - ret, "%s", params->ips_array[i]);
+ if (ret > sizeof(log_buf))
+ return;
+ }
+
+ syslog(LOG_INFO, "%s", log_buf);
+}
+
int og_client_state_process_payload_rest(struct og_client *cli)
{
char buf_reply[OG_MSG_RESPONSE_MAXLEN] = {};
@@ -6395,13 +6524,10 @@ int og_client_state_process_payload_rest(struct og_client *cli)
enum og_rest_method method;
const char *cmd, *body;
json_error_t json_err;
+ enum og_rest_uri uri;
json_t *root = NULL;
int err = 0;
- syslog(LOG_DEBUG, "%s:%hu %.32s ...\n",
- inet_ntoa(cli->addr.sin_addr),
- ntohs(cli->addr.sin_port), cli->buf);
-
if (!strncmp(cli->buf, "GET", strlen("GET"))) {
method = OG_METHOD_GET;
cmd = cli->buf + strlen("GET") + 2;
@@ -6430,6 +6556,8 @@ int og_client_state_process_payload_rest(struct og_client *cli)
}
}
+ uri = og_str_to_uri(cmd);
+
if (!strncmp(cmd, "clients", strlen("clients"))) {
if (method != OG_METHOD_POST &&
method != OG_METHOD_GET) {
@@ -7065,6 +7193,8 @@ int og_client_state_process_payload_rest(struct og_client *cli)
err = og_client_not_found(cli);
}
+ og_rest_log(cli, method, uri, &params);
+
json_decref(root);
if (err < 0)
@@ -7073,6 +7203,9 @@ int og_client_state_process_payload_rest(struct og_client *cli)
return og_client_ok(cli, buf_reply);
err_process_rest_payload:
+ syslog(LOG_ERR, "%s:%hu %.32s ...\n",
+ inet_ntoa(cli->addr.sin_addr),
+ ntohs(cli->addr.sin_port), cli->buf);
json_decref(root);
return err;