/* * plugin_docker — SCORPIOX BOT open-source container dashboard plugin * * A single clean-room C99 binary that serves THREE modes: * 1. --meta / SX_PLUGIN_META=1 -> JSON metadata for discovery * 2. ?action=... | ?format=json -> JSON node API (list / logs / control / info) * 3. GET (default) -> responsive HTML container dashboard * * Local node only. Auto-detects `docker` or `podman` on PATH (or DOCKER_BIN). * Every container identifier is validated against [A-Za-z0-9_.-]{1,128} with a * leading-dash rejection BEFORE it can reach a shell, so neither flag injection * (`cid=--help`) nor command injection (`cid=a;id`) is reachable. * * Zero dependencies. Builds with gcc, musl-gcc -static, Apple clang and * x86_64-w64-mingw32-gcc -static. Runs as a CGI-style executable under * scorpiox-server on the node. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #if !defined(_WIN32) # define _POSIX_C_SOURCE 200809L # define _DEFAULT_SOURCE 1 #endif #include #include #include #include #include #include #if defined(_WIN32) # define WIN32_LEAN_AND_MEAN # include # include # define sx_popen _popen # define sx_pclose _pclose # define sx_access _access # define SX_X_OK 0 # define SX_PATH_SEP ';' #else # include # include # include # define sx_popen popen # define sx_pclose pclose # define sx_access access # define SX_X_OK X_OK # define SX_PATH_SEP ':' #endif #include "bot_theme_plugin.h" #define PLUGIN_SLUG "docker" #define PLUGIN_ID "plugin_docker" #define PLUGIN_VER "1.1.3" #define MAX_CONTAINERS 256 #define LOG_TAIL 100 /* ------------------------------------------------------------------ */ /* Small utilities */ /* ------------------------------------------------------------------ */ /* Percent-decode in place ('+' becomes space). */ static void url_decode(char *s) { char *o = s; if (!s) return; while (*s) { if (*s == '%' && isxdigit((unsigned char)s[1]) && isxdigit((unsigned char)s[2])) { int hi = s[1] <= '9' ? s[1] - '0' : (tolower((unsigned char)s[1]) - 'a' + 10); int lo = s[2] <= '9' ? s[2] - '0' : (tolower((unsigned char)s[2]) - 'a' + 10); *o++ = (char)((hi << 4) | lo); s += 3; } else if (*s == '+') { *o++ = ' '; s++; } else { *o++ = *s++; } } *o = '\0'; } /* Read one parameter (decoded) out of a form/query string. */ static int get_query_val(const char *qs, const char *key, char *out, size_t outsz) { size_t klen; const char *p; if (!out || outsz == 0) return 0; out[0] = '\0'; if (!qs || !key) return 0; klen = strlen(key); for (p = qs; *p; ) { if (strncmp(p, key, klen) == 0 && p[klen] == '=') { const char *v = p + klen + 1; size_t i = 0; while (*v && *v != '&' && i + 1 < outsz) out[i++] = *v++; out[i] = '\0'; url_decode(out); return 1; } p = strchr(p, '&'); if (!p) break; p++; } return 0; } static void json_str(const char *s) { putchar('"'); for (; s && *s; s++) { unsigned char c = (unsigned char)*s; switch (c) { case '"': fputs("\\\"", stdout); break; case '\\': fputs("\\\\", stdout); break; case '\n': fputs("\\n", stdout); break; case '\r': fputs("\\r", stdout); break; case '\t': fputs("\\t", stdout); break; default: if (c < 0x20) printf("\\u%04x", (unsigned)c); else putchar((int)c); } } putchar('"'); } static void utc_now(char *buf, size_t sz) { time_t t = time(NULL); struct tm tmv; #if defined(_WIN32) gmtime_s(&tmv, &t); #else gmtime_r(&t, &tmv); #endif if (strftime(buf, sz, "%Y-%m-%dT%H:%M:%SZ", &tmv) == 0) { strncpy(buf, "1970-01-01T00:00:00Z", sz - 1); buf[sz - 1] = '\0'; } } static long node_pid(void) { #if defined(_WIN32) return (long)GetCurrentProcessId(); #else return (long)getpid(); #endif } /* ------------------------------------------------------------------ */ /* Strict identifier validation */ /* ------------------------------------------------------------------ */ /* * Container identifiers (IDs, names, short IDs) must match * [A-Za-z0-9_.-]{1,128} and must not start with '-' or '.'. * Anything else is refused before it can reach a command line. */ static int valid_cid(const char *s) { size_t n, i; if (!s || !*s) return 0; n = strlen(s); if (n > 128) return 0; if (s[0] == '-' || s[0] == '.') return 0; for (i = 0; i < n; i++) { unsigned char c = (unsigned char)s[i]; if (isalnum(c) || c == '_' || c == '.' || c == '-') continue; return 0; } return 1; } /* The runtime binary path must not contain quoting hazards. */ static int valid_runtime_path(const char *s) { if (!s || !*s) return 0; if (strchr(s, '\'') || strchr(s, '\n') || strchr(s, '\r')) return 0; return 1; } static int valid_op(const char *s) { return s && (strcmp(s, "start") == 0 || strcmp(s, "stop") == 0 || strcmp(s, "restart") == 0); } /* ------------------------------------------------------------------ */ /* Command execution */ /* ------------------------------------------------------------------ */ /* Run a shell command, capture stdout+stderr. Returns malloc'd text. */ static char *run_capture(const char *cmd, int *exit_code) { FILE *fp; char *buf = NULL; size_t len = 0, cap = 0, chunk; char tmp[4096]; int status = 0; if (exit_code) *exit_code = -1; fp = sx_popen(cmd, "r"); if (!fp) return NULL; while ((chunk = fread(tmp, 1, sizeof(tmp), fp)) > 0) { if (len + chunk + 1 > cap) { size_t ncap = cap ? cap * 2 : 8192; char *nb; while (ncap < len + chunk + 1) ncap *= 2; nb = (char *)realloc(buf, ncap); if (!nb) { free(buf); sx_pclose(fp); return NULL; } buf = nb; cap = ncap; } memcpy(buf + len, tmp, chunk); len += chunk; } if (!buf) { buf = (char *)malloc(1); if (!buf) { sx_pclose(fp); return NULL; } cap = 1; } buf[len] = '\0'; status = sx_pclose(fp); #if defined(_WIN32) if (exit_code) *exit_code = status; #else if (exit_code) { if (WIFEXITED(status)) *exit_code = WEXITSTATUS(status); else *exit_code = -1; } #endif return buf; } /* ------------------------------------------------------------------ */ /* Runtime detection */ /* ------------------------------------------------------------------ */ static int path_has_exec(const char *name) { const char *env = getenv("PATH"); char candidate[1024]; if (!env) return 0; while (*env) { const char *end = strchr(env, SX_PATH_SEP); size_t dlen = end ? (size_t)(end - env) : strlen(env); if (dlen > 0 && dlen + strlen(name) + 2 < sizeof(candidate)) { memcpy(candidate, env, dlen); candidate[dlen] = '\0'; #if defined(_WIN32) snprintf(candidate + dlen, sizeof(candidate) - dlen, "\\%s.exe", name); #else snprintf(candidate + dlen, sizeof(candidate) - dlen, "/%s", name); #endif if (sx_access(candidate, SX_X_OK) == 0) return 1; } if (!end) break; env = end + 1; } return 0; } /* Resolve the container runtime. Returns 1 when something is usable. */ static int detect_runtime(char *bin, size_t binsz, char *label, size_t labelsz) { const char *ovr = getenv("DOCKER_BIN"); bin[0] = '\0'; label[0] = '\0'; if (ovr && *ovr) { if (!valid_runtime_path(ovr)) return 0; snprintf(bin, binsz, "%s", ovr); snprintf(label, labelsz, "custom"); return 1; } if (path_has_exec("docker")) { snprintf(bin, binsz, "docker"); snprintf(label, labelsz, "docker"); return 1; } if (path_has_exec("podman")) { snprintf(bin, binsz, "podman"); snprintf(label, labelsz, "podman"); return 1; } return 0; } /* ------------------------------------------------------------------ */ /* Container list */ /* ------------------------------------------------------------------ */ typedef struct { char id[80]; char name[192]; char image[192]; char state[32]; char status[192]; char ports[512]; char cpu[32]; char mem[96]; } container_t; static container_t g_containers[MAX_CONTAINERS]; static int g_container_count; /* Split on '|' in place into up to max fields. */ static int split_pipe(char *line, char **out, int max) { int n = 0; char *p = line; if (max <= 0) return 0; out[n++] = p; while (*p && n < max) { if (*p == '|') { *p = '\0'; out[n++] = p + 1; } p++; } /* consume the remainder of the last field up to a newline */ if (n > 0) { char *e = out[n - 1]; while (*e && *e != '\n' && *e != '\r') e++; *e = '\0'; } return n; } static void copy_field(char *dst, size_t dstsz, const char *src) { size_t i = 0; if (!dst || dstsz == 0) return; if (!src) { dst[0] = '\0'; return; } while (src[i] && i + 1 < dstsz) { dst[i] = src[i]; i++; } dst[i] = '\0'; } /* Normalise the runtime's state word. * * docker reports a tidy lowercase state ("running", "exited", "paused"), but * podman 3.x does not implement {{.State}} at all and substitutes the human * Status text ("Up 4 months ago"). Normalising here keeps the JSON contract * identical no matter which runtime produced the line. */ static void normalize_state(char *dst, size_t dstsz, const char *raw) { char low[64]; size_t i = 0; if (!raw) raw = ""; while (raw[i] && i + 1 < sizeof(low)) { low[i] = (char)tolower((unsigned char)raw[i]); i++; } low[i] = '\0'; if (strstr(low, "paused")) copy_field(dst, dstsz, "paused"); else if (strstr(low, "restart")) copy_field(dst, dstsz, "restarting"); else if (strstr(low, "dead")) copy_field(dst, dstsz, "dead"); else if (strstr(low, "created")) copy_field(dst, dstsz, "created"); else if (strstr(low, "exited") || strstr(low, "stopped") || strstr(low, "exit")) copy_field(dst, dstsz, "exited"); else if (strstr(low, "up") || strstr(low, "running")) copy_field(dst, dstsz, "running"); else if (low[0]) copy_field(dst, dstsz, low); else copy_field(dst, dstsz, ""); } /* Parse `ps -a` output into g_containers. Returns line count parsed. */ static int parse_ps(const char *text) { const char *p = text; int count = 0; g_container_count = 0; while (*p && g_container_count < MAX_CONTAINERS) { char line[1600]; size_t i = 0; char *fields[6]; int nf; while (*p && *p != '\n' && i + 1 < sizeof(line)) line[i++] = *p++; line[i] = '\0'; while (*p == '\n' || *p == '\r') p++; if (i == 0) continue; /* skip a header row if the runtime emitted one */ if (strncmp(line, "CONTAINER ID", 12) == 0) continue; nf = split_pipe(line, fields, 6); if (nf < 2) continue; { container_t *c = &g_containers[g_container_count]; memset(c, 0, sizeof(*c)); copy_field(c->id, sizeof(c->id), fields[0]); copy_field(c->name, sizeof(c->name), nf > 1 ? fields[1] : ""); copy_field(c->image, sizeof(c->image), nf > 2 ? fields[2] : ""); { char raw[64]; copy_field(raw, sizeof(raw), nf > 3 ? fields[3] : ""); normalize_state(c->state, sizeof(c->state), raw); } copy_field(c->status, sizeof(c->status), nf > 4 ? fields[4] : ""); copy_field(c->ports, sizeof(c->ports), nf > 5 ? fields[5] : ""); g_container_count++; count++; } } return count; } /* Merge `stats --no-stream --format {{.ID}}|{{.CPUPerc}}|{{.MemUsage}}`. */ static void merge_stats(const char *text) { const char *p = text; while (*p) { char line[600]; size_t i = 0; char *fields[3]; int nf, k; while (*p && *p != '\n' && i + 1 < sizeof(line)) line[i++] = *p++; line[i] = '\0'; while (*p == '\n' || *p == '\r') p++; if (i == 0) continue; nf = split_pipe(line, fields, 3); if (nf < 3) continue; for (k = 0; k < g_container_count; k++) { if (strncmp(g_containers[k].id, fields[0], strlen(fields[0])) == 0 || strcmp(g_containers[k].id, fields[0]) == 0) { copy_field(g_containers[k].cpu, sizeof(g_containers[k].cpu), fields[1]); copy_field(g_containers[k].mem, sizeof(g_containers[k].mem), fields[2]); break; } } } } static int container_is(const container_t *c, const char *state) { return strcmp(c->state, state) == 0; } /* ------------------------------------------------------------------ */ /* Modes */ /* ------------------------------------------------------------------ */ static int mode_meta(void) { puts("{\"name\":\"docker\",\"desc\":\"Containers\"}"); return 0; } static void json_header(void) { /* Deliberately no Content-Length — the server owns framing. */ fputs("Content-Type: application/json; charset=utf-8\r\n" "Cache-Control: no-store\r\n" "X-Content-Type-Options: nosniff\r\n\r\n", stdout); } static void json_header_status(const char *status_line) { fputs("Status: ", stdout); fputs(status_line, stdout); fputs("\r\n", stdout); json_header(); } /* Emit the runtime block (shared by info + list). */ static void emit_runtime_block(const char *bin, const char *label, int available) { fputs("\"runtime\":{", stdout); fputs("\"available\":", stdout); fputs(available ? "true" : "false", stdout); fputs(",\"bin\":", stdout); json_str(available ? bin : ""); fputs(",\"label\":", stdout); json_str(available ? label : ""); fputs("}", stdout); } static int mode_json(const char *qs, const char *body, int is_post) { char action[64]; char cid[192]; char op[32]; char format[32]; char bin[512], label[64]; int available; action[0] = cid[0] = op[0] = format[0] = '\0'; get_query_val(qs, "action", action, sizeof(action)); get_query_val(qs, "cid", cid, sizeof(cid)); get_query_val(qs, "op", op, sizeof(op)); get_query_val(qs, "format", format, sizeof(format)); /* fall back to POST body parameters when the query is empty */ if (body && *body) { if (!action[0]) get_query_val(body, "action", action, sizeof(action)); if (!cid[0]) get_query_val(body, "cid", cid, sizeof(cid)); if (!op[0]) get_query_val(body, "op", op, sizeof(op)); if (!format[0]) get_query_val(body, "format", format, sizeof(format)); } available = detect_runtime(bin, sizeof(bin), label, sizeof(label)); /* ---- control (POST only) ---- */ if (strcmp(action, "control") == 0) { int code = 0; char *out; if (!is_post) { json_header_status("405 Method Not Allowed"); fputs("{\"ok\":false,\"error\":\"control_requires_post\"," "\"hint\":\"POST ?action=control&op=start|stop|restart&cid=\"}\n", stdout); return 0; } if (!available) { json_header_status("503 Service Unavailable"); fputs("{\"ok\":false,\"error\":\"runtime_available\",\"runtime\":{\"available\":false}," "\"message\":\"Neither docker nor podman was found on this node\"}\n", stdout); return 0; } if (!valid_op(op)) { json_header_status("400 Bad Request"); fputs("{\"ok\":false,\"error\":\"invalid_op\"," "\"supported\":[\"start\",\"stop\",\"restart\"]}\n", stdout); return 0; } if (!valid_cid(cid)) { json_header_status("400 Bad Request"); fputs("{\"ok\":false,\"error\":\"invalid_cid\"," "\"message\":\"cid must match [A-Za-z0-9_.-]{1,128} and may not start with - or .\"}\n", stdout); return 0; } { char cmd[1024]; snprintf(cmd, sizeof(cmd), "'%s' %s %s 2>&1", bin, op, cid); out = run_capture(cmd, &code); } { char ts[64]; utc_now(ts, sizeof(ts)); if (code != 0) json_header_status("409 Conflict"); else json_header(); fputs("{\"ok\":", stdout); fputs(code == 0 ? "true" : "false", stdout); fputs(",\"action\":\"control\",\"op\":", stdout); json_str(op); fputs(",\"cid\":", stdout); json_str(cid); printf(",\"exit_code\":%d", code); fputs(",\"output\":", stdout); json_str(out ? out : ""); fputs(",\"utc\":", stdout); json_str(ts); fputs("}\n", stdout); } free(out); return 0; } /* ---- logs ---- */ if (strcmp(action, "logs") == 0) { if (!valid_cid(cid)) { json_header_status("400 Bad Request"); fputs("{\"ok\":false,\"error\":\"invalid_cid\",\"cid\":", stdout); json_str(cid); fputs("}\n", stdout); return 0; } if (!available) { json_header_status("503 Service Unavailable"); fputs("{\"ok\":false,\"error\":\"runtime_available\",\"runtime\":{\"available\":false}," "\"message\":\"Neither docker nor podman was found on this node\"}\n", stdout); return 0; } { char cmd[1024]; char *out; int code = 0; const char *p; snprintf(cmd, sizeof(cmd), "'%s' logs --tail %d %s 2>&1", bin, LOG_TAIL, cid); out = run_capture(cmd, &code); if (code != 0) json_header_status("409 Conflict"); else json_header(); fputs("{\"ok\":", stdout); fputs(code == 0 ? "true" : "false", stdout); fputs(",\"action\":\"logs\",\"cid\":", stdout); json_str(cid); printf(",\"tail\":%d,\"exit_code\":%d", LOG_TAIL, code); fputs(",\"lines\":[", stdout); if (out) { int first = 1; p = out; while (*p) { char line[2048]; size_t i = 0; while (*p && *p != '\n' && i + 1 < sizeof(line)) line[i++] = *p++; line[i] = '\0'; while (*p == '\n' || *p == '\r') p++; if (!first) fputs(",", stdout); first = 0; json_str(line); } } fputs("],\"text\":", stdout); json_str(out ? out : ""); fputs("}\n", stdout); free(out); } return 0; } /* ---- info / list / unknown ---- */ if (action[0] && strcmp(action, "info") != 0 && strcmp(action, "list") != 0) { json_header_status("400 Bad Request"); fputs("{\"ok\":false,\"error\":\"unknown_action\",\"action\":", stdout); json_str(action); fputs(",\"supported\":[\"list\",\"info\",\"logs\",\"control\"]}\n", stdout); return 0; } if (strcmp(action, "info") == 0 && strcmp(format, "list") == 0) { /* not meaningful; treat as list */ } if (strcmp(action, "info") == 0) { char ts[64]; utc_now(ts, sizeof(ts)); json_header(); fputs("{\"ok\":true,\"action\":\"info\",\"plugin\":\"" PLUGIN_ID "\"," "\"slug\":\"" PLUGIN_SLUG "\",\"version\":\"" PLUGIN_VER "\",", stdout); emit_runtime_block(bin, label, available); printf(",\"pid\":%ld", node_pid()); fputs(",\"utc\":", stdout); json_str(ts); fputs(",\"supported\":[\"list\",\"info\",\"logs\",\"control\"]," "\"control\":{\"method\":\"POST\",\"ops\":[\"start\",\"stop\",\"restart\"]}}\n", stdout); return 0; } /* list */ { char *psout = NULL; char *statsout = NULL; int pscode = 0, statscode = 0; char ts[64]; int total = 0, running = 0, exited = 0, paused = 0, other = 0; int i; if (available) { char cmd[1024]; snprintf(cmd, sizeof(cmd), "'%s' ps -a --format '{{.ID}}|{{.Names}}|{{.Image}}|{{.State}}|{{.Status}}|{{.Ports}}' 2>&1", bin); psout = run_capture(cmd, &pscode); if (psout && pscode == 0) parse_ps(psout); snprintf(cmd, sizeof(cmd), "'%s' stats --no-stream --format '{{.ID}}|{{.CPUPerc}}|{{.MemUsage}}' 2>&1", bin); statsout = run_capture(cmd, &statscode); if (statsout && statscode == 0 && g_container_count > 0) merge_stats(statsout); } for (i = 0; i < g_container_count; i++) { total++; if (container_is(&g_containers[i], "running")) running++; else if (container_is(&g_containers[i], "exited")) exited++; else if (container_is(&g_containers[i], "paused")) paused++; else other++; } utc_now(ts, sizeof(ts)); json_header(); fputs("{\"ok\":true,\"action\":\"list\",\"plugin\":\"" PLUGIN_ID "\"," "\"slug\":\"" PLUGIN_SLUG "\",\"version\":\"" PLUGIN_VER "\",", stdout); emit_runtime_block(bin, label, available); if (!available) { fputs(",\"message\":\"Neither docker nor podman was found on this node. " "Install a container runtime, or set DOCKER_BIN to an explicit binary path.\"", stdout); } else if (psout && pscode != 0) { fputs(",\"daemon_available\":false,\"error\":", stdout); json_str(psout); } else { fputs(",\"daemon_available\":true", stdout); } fputs(",\"utc\":", stdout); json_str(ts); printf(",\"count\":%d,\"summary\":{\"total\":%d,\"running\":%d,\"exited\":%d," "\"paused\":%d,\"other\":%d}", g_container_count, total, running, exited, paused, other); fputs(",\"containers\":[", stdout); for (i = 0; i < g_container_count; i++) { container_t *c = &g_containers[i]; if (i) fputs(",", stdout); fputs("{", stdout); fputs("\"id\":", stdout); json_str(c->id); fputs(",\"short\":", stdout); json_str(c->id[0] ? c->id : ""); fputs(",\"name\":", stdout); json_str(c->name); fputs(",\"image\":", stdout); json_str(c->image); fputs(",\"state\":", stdout); json_str(c->state); fputs(",\"status\":", stdout);json_str(c->status); fputs(",\"ports\":", stdout); json_str(c->ports); fputs(",\"cpu\":", stdout); json_str(c->cpu); fputs(",\"mem\":", stdout); json_str(c->mem); fputs("}", stdout); } fputs("]}\n", stdout); free(psout); free(statsout); } return 0; } /* ------------------------------------------------------------------ */ /* HTML UI */ /* ------------------------------------------------------------------ */ static int mode_html(int embed) { fputs("Content-Type: text/html; charset=utf-8\r\n" "Cache-Control: no-store\r\n" "X-Content-Type-Options: nosniff\r\n\r\n", stdout); fputs("\n" "" "docker", stdout); fputs(SX_BOT_THEME_PREPAINT "" SX_BOT_THEME_HANDOFF "
", stdout); if (!embed) fputs("", stdout); fputs("

docker

" "" PLUGIN_VER "" "connecting" "" "" "
", stdout); /* KPIs */ fputs("

Containers

" "
total
—
" "
running
—
" "
exited
—
" "
paused
—
" "
runtime
—
" "
" "
" "" "" "
" "
" "
" "" "
IDNameImageStateCPUMemActions
" "
No containers to show.
" "
", stdout); /* log viewer */ fputs("

Logs

" "
loading…
" "
" "" "
", stdout); /* footer */ fputs("

About this plugin

" "

Local node only. Reads " "docker or podman on this machine and makes zero " "outbound network requests. Source is MIT licensed.

" "

list, logs and " "control are the only supported actions; container identifiers are " "validated before they ever reach a command line.

", stdout); /* script */ fputs( "\n", stdout); return 0; } /* ------------------------------------------------------------------ */ /* Node auth */ /* ------------------------------------------------------------------ */ /* * Every node plugin must refuse an unauthenticated caller before it touches * the container runtime. scorpiox-server runs plugins as CGI on the node API * (:8150), which is reachable from the LAN, so without this a POST * ?action=control would let anyone stop containers without knowing the node * password. Accepted forms, matching the rest of the node API: * * X-Bot-Password: (what bot-web proxies with) * Authorization: Bearer * ?pwd= (what the plugin iframe appends) * * If BOT_PASSWORD is unset the node is running in explicitly open mode and the * check is a no-op, exactly like terminal_tmux.c and node_rdp.c. */ static void require_node_auth(void) { const char *np = getenv("BOT_PASSWORD"); const char *xp; const char *auth; char qpwd[256]; const char *qs; if (!np || !*np) return; xp = getenv("HTTP_X_BOT_PASSWORD"); if (xp && strcmp(xp, np) == 0) return; auth = getenv("HTTP_AUTHORIZATION"); if (auth) { while (*auth == ' ') auth++; if (strncmp(auth, "Bearer ", 7) == 0) { auth += 7; while (*auth == ' ') auth++; if (strcmp(auth, np) == 0) return; } } qs = getenv("QUERY_STRING"); if (qs && get_query_val(qs, "pwd", qpwd, sizeof(qpwd)) && strcmp(qpwd, np) == 0) return; fputs("Status: 401 Unauthorized\r\n" "Content-Type: application/json; charset=utf-8\r\n" "Cache-Control: no-store\r\n" "X-Content-Type-Options: nosniff\r\n\r\n" "{\"ok\":false,\"error\":\"invalid node password\"}\n", stdout); exit(0); } /* ------------------------------------------------------------------ */ /* POST body */ /* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */ /* POST body */ /* ------------------------------------------------------------------ */ static char *read_post_body(void) { const char *cl = getenv("CONTENT_LENGTH"); long want; size_t got = 0; char *buf; if (!cl || !*cl) return NULL; want = strtol(cl, NULL, 10); if (want <= 0) return NULL; if (want > 8192) want = 8192; buf = (char *)malloc((size_t)want + 1); if (!buf) return NULL; while (got < (size_t)want) { size_t n = fread(buf + got, 1, (size_t)want - got, stdin); if (n == 0) break; got += n; } buf[got] = '\0'; return buf; } /* ------------------------------------------------------------------ */ /* main */ /* ------------------------------------------------------------------ */ static int is_post_request(void) { const char *m = getenv("REQUEST_METHOD"); return m && strcmp(m, "POST") == 0; } int main(int argc, char **argv) { const char *qs; char *body = NULL; if (argc > 1) { if (strcmp(argv[1], "--meta") == 0) return mode_meta(); if (strcmp(argv[1], "--version") == 0) { puts(PLUGIN_VER); return 0; } if (strcmp(argv[1], "--help") == 0) { puts("plugin_docker " PLUGIN_VER); puts("usage: plugin_docker [--meta|--version|--help]"); puts(" QUERY_STRING='action=list|info|logs&cid=' plugin_docker"); return 0; } } if (getenv("SX_PLUGIN_META")) return mode_meta(); require_node_auth(); qs = getenv("QUERY_STRING"); if (!qs) qs = ""; if (is_post_request()) body = read_post_body(); if (strstr(qs, "action=") || strstr(qs, "format=json") || (body && strstr(body, "action="))) { int rc = mode_json(qs, body, is_post_request()); free(body); return rc; } { char embedv[16]; free(body); embedv[0] = '\0'; if (get_query_val(qs, "embed", embedv, sizeof(embedv))) return mode_html(strcmp(embedv, "1") == 0 || strcmp(embedv, "true") == 0); } return mode_html(0); }