From c6020f2ad186954d6c083d000f282f58b44017f1 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Fri, 24 May 2019 10:48:46 +0200 Subject: #915 add POST shell/output command to REST API in ogAdmServer Fetching result from run command on client (through GET method): curl -X POST http://127.0.0.1:8888/shell/output -d @post_shell_output.json Request POST /shell/output {"clients": [ "192.168.2.1" ] } Reply: 200 OK {"clients": [ { "addr" : "192.168.2.1", "output" : "..." } ] } --- sources/ogAdmServer.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++ tests/post_shell_output.json | 1 + tests/run-tests.sh | 1 + 3 files changed, 95 insertions(+) create mode 100644 tests/post_shell_output.json diff --git a/sources/ogAdmServer.cpp b/sources/ogAdmServer.cpp index 0ad4d84..8763ceb 100644 --- a/sources/ogAdmServer.cpp +++ b/sources/ogAdmServer.cpp @@ -12,6 +12,9 @@ #include #include #include +#include +#include +#include #include static char usuario[LONPRM]; // Usuario de acceso a la base de datos @@ -3922,6 +3925,86 @@ static int og_cmd_run_post(json_t *element, struct og_msg_params *params) return 0; } +static int og_cmd_run_get(json_t *element, struct og_msg_params *params, + char *buffer_reply) +{ + struct og_buffer og_buffer = { + .data = buffer_reply, + }; + json_t *root, *value, *array; + const char *key; + unsigned int i; + int err = 0; + + if (json_typeof(element) != JSON_OBJECT) + return -1; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "clients")) + err = og_json_parse_clients(value, params); + + if (err < 0) + return err; + } + + array = json_array(); + if (!array) + return -1; + + for (i = 0; i < params->ips_array_len; i++) { + json_t *object, *output, *addr; + char data[4096] = {}; + char filename[4096]; + int fd, numbytes; + + sprintf(filename, "/tmp/_Seconsola_%s", params->ips_array[i]); + + fd = open(filename, O_RDONLY); + if (!fd) + return -1; + + numbytes = read(fd, data, sizeof(data)); + if (numbytes < 0) { + close(fd); + return -1; + } + data[sizeof(data) - 1] = '\0'; + close(fd); + + object = json_object(); + if (!object) { + json_decref(array); + return -1; + } + addr = json_string(params->ips_array[i]); + if (!addr) { + json_decref(object); + json_decref(array); + return -1; + } + json_object_set_new(object, "addr", addr); + + output = json_string(data); + if (!output) { + json_decref(object); + json_decref(array); + return -1; + } + json_object_set_new(object, "output", output); + + json_array_append_new(array, object); + } + + root = json_pack("{s:o}", "clients", array); + if (!root) + return -1; + + json_dump_callback(root, og_json_dump_clients, &og_buffer, 4096); + json_decref(root); + + return 0; +} + static int og_client_not_found(struct og_client *cli) { char buf[] = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; @@ -4018,6 +4101,16 @@ static int og_client_state_process_payload_rest(struct og_client *cli) return og_client_not_found(cli); } err = og_cmd_run_post(root, ¶ms); + } else if (!strncmp(cmd, "shell/output", strlen("shell/output"))) { + if (method != OG_METHOD_POST) + return -1; + + if (!root) { + syslog(LOG_ERR, "command output with no payload\n"); + return og_client_not_found(cli); + } + + err = og_cmd_run_get(root, ¶ms, buf_reply); } else { syslog(LOG_ERR, "unknown command %s\n", cmd); err = og_client_not_found(cli); diff --git a/tests/post_shell_output.json b/tests/post_shell_output.json new file mode 100644 index 0000000..1badfed --- /dev/null +++ b/tests/post_shell_output.json @@ -0,0 +1 @@ +{ "clients" : [ "192.168.2.1", "192.168.2.2" ]} diff --git a/tests/run-tests.sh b/tests/run-tests.sh index f9f49ba..b5239d5 100755 --- a/tests/run-tests.sh +++ b/tests/run-tests.sh @@ -2,3 +2,4 @@ curl -X POST http://127.0.0.1:8888/clients -d @post_clients.json curl -X GET http://127.0.0.1:8888/clients curl -X POST http://127.0.0.1:8888/wol -d @wol.json curl -X POST http://127.0.0.1:8888/shell/run -d @post_shell_run.json +curl -X POST http://127.0.0.1:8888/shell/output -d @post_shell_output.json -- cgit v1.2.3-18-g5258