diff options
author | OpenGnSys Support Team <soporte-og@soleta.eu> | 2019-05-24 10:48:46 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2019-05-27 13:03:00 +0200 |
commit | c6020f2ad186954d6c083d000f282f58b44017f1 (patch) | |
tree | 41ba3359b635d4a6c2ede5270eda5720cf58c0ac /sources | |
parent | 7e4e5b5e0884483e8c7d4b0adb8f10a93157ceaf (diff) |
#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" : "..." } ] }
Diffstat (limited to 'sources')
-rw-r--r-- | sources/ogAdmServer.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
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 <syslog.h> #include <sys/ioctl.h> #include <ifaddrs.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> #include <jansson.h> 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); |