diff options
author | OpenGnSys Support Team <soporte-og@soleta.eu> | 2019-05-23 17:49:18 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2019-05-27 13:02:57 +0200 |
commit | 7e4e5b5e0884483e8c7d4b0adb8f10a93157ceaf (patch) | |
tree | be43c4da307ee070a3a3a558b2abbf3c1ebb66c8 | |
parent | 5797e0b199caa44b992929a0f421f6e1a455fffe (diff) |
#915 add run command to REST API in ogAdmServer
Run command on client (through POST method):
curl -X POST http://127.0.0.1:8888/shell/run -d @post_run.json
Request
POST /shell/run
{"clients": [ "192.168.2.1", "192.168.2.2" ], "run" : "ls" }
Reply:
200 OK
-rw-r--r-- | sources/ogAdmServer.cpp | 75 | ||||
-rw-r--r-- | tests/post_shell_run.json | 1 | ||||
-rwxr-xr-x | tests/run-tests.sh | 1 |
3 files changed, 77 insertions, 0 deletions
diff --git a/sources/ogAdmServer.cpp b/sources/ogAdmServer.cpp index a83a47d..0ad4d84 100644 --- a/sources/ogAdmServer.cpp +++ b/sources/ogAdmServer.cpp @@ -3630,6 +3630,7 @@ struct og_msg_params { const char *mac_array[64]; unsigned int ips_array_len; const char *wol_type; + char run_cmd[4096]; }; static int og_json_parse_clients(json_t *element, struct og_msg_params *params) @@ -3856,6 +3857,71 @@ static int og_cmd_wol(json_t *element, struct og_msg_params *params) return 0; } +static int og_json_parse_run(json_t *element, struct og_msg_params *params) +{ + if (json_typeof(element) != JSON_STRING) + return -1; + + snprintf(params->run_cmd, sizeof(params->run_cmd), "%s", + json_string_value(element)); + + return 0; +} + +static int og_cmd_run_post(json_t *element, struct og_msg_params *params) +{ + char buf[4096] = {}, iph[4096] = {}; + int err = 0, len; + const char *key; + unsigned int i; + json_t *value; + TRAMA *msg; + + 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 (!strcmp(key, "run")) + err = og_json_parse_run(value, params); + + if (err < 0) + break; + } + + for (i = 0; i < params->ips_array_len; i++) { + len = snprintf(iph + strlen(iph), sizeof(iph), "%s;", + params->ips_array[i]); + } + len = snprintf(buf, sizeof(buf), "nfn=ConsolaRemota\riph=%s\rscp=%s\r", + iph, params->run_cmd); + + msg = og_msg_alloc(buf, len); + if (!msg) + return -1; + + if (!og_send_cmd((char **)params->ips_array, params->ips_array_len, + CLIENTE_OCUPADO, msg)) + err = -1; + + og_msg_free(msg); + + if (err < 0) + return err; + + for (i = 0; i < params->ips_array_len; i++) { + char filename[4096]; + FILE *f; + + sprintf(filename, "/tmp/_Seconsola_%s", params->ips_array[i]); + f = fopen(filename, "wt"); + fclose(f); + } + + 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"; @@ -3943,6 +4009,15 @@ static int og_client_state_process_payload_rest(struct og_client *cli) return og_client_not_found(cli); } err = og_cmd_wol(root, ¶ms); + } else if (!strncmp(cmd, "shell/run", strlen("shell/run"))) { + if (method != OG_METHOD_POST) + return -1; + + if (!root) { + syslog(LOG_ERR, "command run with no payload\n"); + return og_client_not_found(cli); + } + err = og_cmd_run_post(root, ¶ms); } else { syslog(LOG_ERR, "unknown command %s\n", cmd); err = og_client_not_found(cli); diff --git a/tests/post_shell_run.json b/tests/post_shell_run.json new file mode 100644 index 0000000..1449d05 --- /dev/null +++ b/tests/post_shell_run.json @@ -0,0 +1 @@ +{ "clients" : [ "192.168.2.1", "192.168.2.2" ], "run" : "ls" } diff --git a/tests/run-tests.sh b/tests/run-tests.sh index 40c339d..f9f49ba 100755 --- a/tests/run-tests.sh +++ b/tests/run-tests.sh @@ -1,3 +1,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 |