| 1 | /* |
| 2 | * plugin_files - SCORPIOX BOT open-source read-only file browser for your node |
| 3 | * |
| 4 | * A single clean-room C99 binary that serves THREE modes: |
| 5 | * 1. --meta / SX_PLUGIN_META=1 -> JSON discovery metadata |
| 6 | * 2. ?action=... / ?format=json -> JSON node API |
| 7 | * 3. GET (default) -> browse + preview page |
| 8 | * |
| 9 | * Repo: https://plugingit.scorpiox.net/scorpiox-bot-plugin-files.git |
| 10 | * |
| 11 | * READ-ONLY BY CONSTRUCTION. There is no create, no write, no delete, no |
| 12 | * rename, no chmod anywhere in this file: the plugin exists to look at files, |
| 13 | * never to change them. |
| 14 | * |
| 15 | * Every path that arrives in a request is canonicalised on the real |
| 16 | * filesystem (realpath() on POSIX, GetFullPathNameA + GetFinalPathNameByHandleA |
| 17 | * on Windows) and then prefix-checked against the allowlisted roots AFTER |
| 18 | * symlink resolution. `..`, embedded NUL bytes, backslash tricks and symlinks |
| 19 | * that point outside the roots are all rejected with 403. /proc, /sys and /dev |
| 20 | * are refused even when they sit under an allowed root (/proc lives under /). |
| 21 | * |
| 22 | * Zero dependencies. Builds with gcc, musl-gcc -static, Apple clang and |
| 23 | * x86_64-w64-mingw32-gcc -static. Runs as a CGI-style executable under |
| 24 | * scorpiox-server on the node. |
| 25 | * |
| 26 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 27 | */ |
| 28 | |
| 29 | /* Must come before every include on Windows: MinGW defaults to 0x502 |
| 30 | * (Server 2003), which hides GetFinalPathNameByHandleA and friends. */ |
| 31 | #if defined(_WIN32) && !defined(_WIN32_WINNT) |
| 32 | # define _WIN32_WINNT 0x0601 |
| 33 | #endif |
| 34 | |
| 35 | #if !defined(_WIN32) |
| 36 | # define _POSIX_C_SOURCE 200809L |
| 37 | # define _DEFAULT_SOURCE 1 |
| 38 | # if defined(__APPLE__) |
| 39 | # define _DARWIN_C_SOURCE 1 |
| 40 | # endif |
| 41 | #endif |
| 42 | |
| 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
| 46 | #include <stdarg.h> |
| 47 | #include <ctype.h> |
| 48 | #include <errno.h> |
| 49 | #include <time.h> |
| 50 | |
| 51 | #if defined(_WIN32) |
| 52 | # define WIN32_LEAN_AND_MEAN |
| 53 | # include <windows.h> |
| 54 | # include <io.h> |
| 55 | # define SEP '\\' |
| 56 | #else |
| 57 | # include <unistd.h> |
| 58 | # include <sys/stat.h> |
| 59 | # include <sys/types.h> |
| 60 | # include <dirent.h> |
| 61 | # define SEP '/' |
| 62 | #endif |
| 63 | |
| 64 | #include "bot_theme_plugin.h" |
| 65 | #include "ui_html.h" |
| 66 | |
| 67 | #define PLUGIN_SLUG "files" |
| 68 | #define PLUGIN_ID "plugin_files" |
| 69 | #define PLUGIN_VER "1.0.0" |
| 70 | #define PLUGIN_NAME "files" |
| 71 | #define PLUGIN_DESC "File browser" |
| 72 | |
| 73 | #define PATH_SZ 4096 |
| 74 | #define NAME_SZ 512 |
| 75 | #define MAX_ROOTS 8 |
| 76 | #define MAX_ENTRIES 5000 |
| 77 | #define TEXT_CAP (1024 * 1024) /* 1 MiB text preview */ |
| 78 | #define RAW_CAP ((long long)64 * 1024 * 1024) /* 64 MiB raw stream */ |
| 79 | #define BIN_PROBE 8192 |
| 80 | #define IO_CHUNK 65536 |
| 81 | |
| 82 | typedef struct { |
| 83 | char name[NAME_SZ]; |
| 84 | int is_dir; |
| 85 | int is_link; |
| 86 | long long size; |
| 87 | long long mtime; |
| 88 | unsigned mode; |
| 89 | } ent_t; |
| 90 | |
| 91 | static char g_roots[MAX_ROOTS][PATH_SZ]; |
| 92 | static int g_root_count; |
| 93 | |
| 94 | /* ------------------------------------------------------------------ */ |
| 95 | /* Growable byte buffer */ |
| 96 | /* ------------------------------------------------------------------ */ |
| 97 | |
| 98 | typedef struct { |
| 99 | char *p; |
| 100 | size_t n; |
| 101 | size_t cap; |
| 102 | } buf_t; |
| 103 | |
| 104 | static void buf_init(buf_t *b) |
| 105 | { |
| 106 | b->p = NULL; |
| 107 | b->n = 0; |
| 108 | b->cap = 0; |
| 109 | } |
| 110 | |
| 111 | static int buf_need(buf_t *b, size_t extra) |
| 112 | { |
| 113 | size_t want = b->n + extra + 1; |
| 114 | char *np; |
| 115 | if (want <= b->cap) return 1; |
| 116 | if (b->cap == 0) b->cap = 1024; |
| 117 | while (b->cap < want) { |
| 118 | if (b->cap > (size_t)128 * 1024 * 1024) return 0; |
| 119 | b->cap *= 2; |
| 120 | } |
| 121 | np = (char *)realloc(b->p, b->cap); |
| 122 | if (!np) return 0; |
| 123 | b->p = np; |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | static void buf_add(buf_t *b, const char *s, size_t n) |
| 128 | { |
| 129 | if (!buf_need(b, n)) return; |
| 130 | memcpy(b->p + b->n, s, n); |
| 131 | b->n += n; |
| 132 | b->p[b->n] = '\0'; |
| 133 | } |
| 134 | |
| 135 | static void buf_puts(buf_t *b, const char *s) |
| 136 | { |
| 137 | if (s) buf_add(b, s, strlen(s)); |
| 138 | } |
| 139 | |
| 140 | static void buf_putc(buf_t *b, char c) |
| 141 | { |
| 142 | if (!buf_need(b, 1)) return; |
| 143 | b->p[b->n++] = c; |
| 144 | b->p[b->n] = '\0'; |
| 145 | } |
| 146 | |
| 147 | static const char *buf_str(buf_t *b) |
| 148 | { |
| 149 | return b->p ? b->p : ""; |
| 150 | } |
| 151 | |
| 152 | static void buf_printf(buf_t *b, const char *fmt, ...) |
| 153 | { |
| 154 | char tmp[1024]; |
| 155 | va_list ap; |
| 156 | int n; |
| 157 | va_start(ap, fmt); |
| 158 | n = vsnprintf(tmp, sizeof tmp, fmt, ap); |
| 159 | va_end(ap); |
| 160 | if (n < 0) return; |
| 161 | if ((size_t)n < sizeof tmp) { |
| 162 | buf_add(b, tmp, (size_t)n); |
| 163 | return; |
| 164 | } |
| 165 | { |
| 166 | char *big = (char *)malloc((size_t)n + 1); |
| 167 | if (!big) return; |
| 168 | va_start(ap, fmt); |
| 169 | vsnprintf(big, (size_t)n + 1, fmt, ap); |
| 170 | va_end(ap); |
| 171 | buf_add(b, big, (size_t)n); |
| 172 | free(big); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | static void buf_free(buf_t *b) |
| 177 | { |
| 178 | free(b->p); |
| 179 | b->p = NULL; |
| 180 | b->n = 0; |
| 181 | b->cap = 0; |
| 182 | } |
| 183 | |
| 184 | /* ------------------------------------------------------------------ */ |
| 185 | /* Small utilities */ |
| 186 | /* ------------------------------------------------------------------ */ |
| 187 | |
| 188 | static void str_copy(char *dst, size_t sz, const char *src) |
| 189 | { |
| 190 | size_t n; |
| 191 | if (!dst || sz == 0) return; |
| 192 | if (!src) { dst[0] = '\0'; return; } |
| 193 | n = strlen(src); |
| 194 | if (n >= sz) n = sz - 1; |
| 195 | memcpy(dst, src, n); |
| 196 | dst[n] = '\0'; |
| 197 | } |
| 198 | |
| 199 | static void path_join(char *out, size_t sz, const char *a, const char *b) |
| 200 | { |
| 201 | size_t n; |
| 202 | if (!out || sz == 0) return; |
| 203 | out[0] = '\0'; |
| 204 | if (!a) a = ""; |
| 205 | if (!b) b = ""; |
| 206 | str_copy(out, sz, a); |
| 207 | n = strlen(out); |
| 208 | if (n && out[n - 1] != '/' && out[n - 1] != '\\' && n + 1 < sz) { |
| 209 | out[n++] = SEP; |
| 210 | out[n] = '\0'; |
| 211 | } |
| 212 | if (n + strlen(b) < sz) str_copy(out + n, sz - n, b); |
| 213 | } |
| 214 | |
| 215 | static void url_decode(char *s) |
| 216 | { |
| 217 | char *o = s; |
| 218 | if (!s) return; |
| 219 | while (*s) { |
| 220 | if (*s == '%' && isxdigit((unsigned char)s[1]) && isxdigit((unsigned char)s[2])) { |
| 221 | int hi = s[1] <= '9' ? s[1] - '0' : (tolower((unsigned char)s[1]) - 'a' + 10); |
| 222 | int lo = s[2] <= '9' ? s[2] - '0' : (tolower((unsigned char)s[2]) - 'a' + 10); |
| 223 | *o++ = (char)((hi << 4) | lo); |
| 224 | s += 3; |
| 225 | } else if (*s == '+') { |
| 226 | *o++ = ' '; |
| 227 | s++; |
| 228 | } else { |
| 229 | *o++ = *s++; |
| 230 | } |
| 231 | } |
| 232 | *o = '\0'; |
| 233 | } |
| 234 | |
| 235 | static int get_query_val(const char *qs, const char *key, char *out, size_t outsz) |
| 236 | { |
| 237 | size_t klen; |
| 238 | const char *p; |
| 239 | |
| 240 | if (!out || outsz == 0) return 0; |
| 241 | out[0] = '\0'; |
| 242 | if (!qs || !key) return 0; |
| 243 | klen = strlen(key); |
| 244 | |
| 245 | for (p = qs; *p; ) { |
| 246 | if (strncmp(p, key, klen) == 0 && p[klen] == '=') { |
| 247 | const char *v = p + klen + 1; |
| 248 | size_t i = 0; |
| 249 | while (*v && *v != '&' && i + 1 < outsz) |
| 250 | out[i++] = *v++; |
| 251 | out[i] = '\0'; |
| 252 | url_decode(out); |
| 253 | return 1; |
| 254 | } |
| 255 | p = strchr(p, '&'); |
| 256 | if (!p) break; |
| 257 | p++; |
| 258 | } |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static void json_str(buf_t *b, const char *s) |
| 263 | { |
| 264 | buf_putc(b, '"'); |
| 265 | for (; s && *s; s++) { |
| 266 | unsigned char c = (unsigned char)*s; |
| 267 | switch (c) { |
| 268 | case '"': buf_puts(b, "\\\""); break; |
| 269 | case '\\': buf_puts(b, "\\\\"); break; |
| 270 | case '\n': buf_puts(b, "\\n"); break; |
| 271 | case '\r': buf_puts(b, "\\r"); break; |
| 272 | case '\t': buf_puts(b, "\\t"); break; |
| 273 | default: |
| 274 | if (c < 0x20) { |
| 275 | char tmp[8]; |
| 276 | sprintf(tmp, "\\u%04x", c); |
| 277 | buf_puts(b, tmp); |
| 278 | } else { |
| 279 | buf_putc(b, (char)c); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | buf_putc(b, '"'); |
| 284 | } |
| 285 | |
| 286 | static void fmt_iso(long long t, char *out, size_t sz) |
| 287 | { |
| 288 | time_t tt = (time_t)t; |
| 289 | struct tm tmv; |
| 290 | if (t <= 0) { str_copy(out, sz, ""); return; } |
| 291 | #if defined(_WIN32) |
| 292 | gmtime_s(&tmv, &tt); |
| 293 | #else |
| 294 | gmtime_r(&tt, &tmv); |
| 295 | #endif |
| 296 | strftime(out, sz, "%Y-%m-%dT%H:%M:%SZ", &tmv); |
| 297 | } |
| 298 | |
| 299 | static int ci_cmp(const char *a, const char *b) |
| 300 | { |
| 301 | for (;;) { |
| 302 | unsigned char ca = (unsigned char)*a++; |
| 303 | unsigned char cb = (unsigned char)*b++; |
| 304 | ca = (unsigned char)tolower(ca); |
| 305 | cb = (unsigned char)tolower(cb); |
| 306 | if (ca != cb) return ca < cb ? -1 : 1; |
| 307 | if (!ca) return 0; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* ------------------------------------------------------------------ */ |
| 312 | /* Path canonicalisation + allowlist */ |
| 313 | /* ------------------------------------------------------------------ */ |
| 314 | |
| 315 | /* POSIX: realpath() resolves every symlink, so the prefix check happens on |
| 316 | * the real target. Windows: GetFullPathNameA normalises `..`, then |
| 317 | * GetFinalPathNameByHandleA resolves reparse points when the file exists. */ |
| 318 | |
| 319 | #if defined(_WIN32) |
| 320 | |
| 321 | static void win_norm(char *s) |
| 322 | { |
| 323 | for (; *s; s++) { |
| 324 | if (*s == '/') *s = '\\'; |
| 325 | *s = (char)tolower((unsigned char)*s); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | static int path_under_win(const char *p, const char *root) |
| 330 | { |
| 331 | char a[PATH_SZ], b[PATH_SZ]; |
| 332 | size_t n; |
| 333 | str_copy(a, sizeof a, p); |
| 334 | str_copy(b, sizeof b, root); |
| 335 | win_norm(a); |
| 336 | win_norm(b); |
| 337 | n = strlen(b); |
| 338 | while (n > 3 && b[n - 1] == '\\') b[--n] = '\0'; |
| 339 | if (strcmp(a, b) == 0) return 1; |
| 340 | if (strncmp(a, b, n) == 0 && a[n] == '\\') return 1; |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int canon_path(const char *raw, char *out, size_t outsz, int *err) |
| 345 | { |
| 346 | char full[PATH_SZ]; |
| 347 | DWORD n; |
| 348 | HANDLE h; |
| 349 | char final[PATH_SZ]; |
| 350 | |
| 351 | *err = 0; |
| 352 | if (!raw || !*raw) { *err = 400; return 0; } |
| 353 | if (strchr(raw, '\0') == NULL) return 0; /* unreachable; keeps compilers quiet */ |
| 354 | |
| 355 | n = GetFullPathNameA(raw, (DWORD)sizeof full, full, NULL); |
| 356 | if (n == 0 || n >= (DWORD)sizeof full) { *err = 400; return 0; } |
| 357 | |
| 358 | if (GetFileAttributesA(full) == INVALID_FILE_ATTRIBUTES) { *err = 404; return 0; } |
| 359 | |
| 360 | h = CreateFileA(full, 0, |
| 361 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 362 | NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 363 | if (h != INVALID_HANDLE_VALUE) { |
| 364 | DWORD fn = GetFinalPathNameByHandleA(h, final, (DWORD)sizeof final, 0); |
| 365 | CloseHandle(h); |
| 366 | if (fn > 0 && fn < (DWORD)sizeof final) { |
| 367 | char *src = final; |
| 368 | if (strncmp(src, "\\\\?\\", 4) == 0) src += 4; |
| 369 | str_copy(out, outsz, src); |
| 370 | return 1; |
| 371 | } |
| 372 | } |
| 373 | str_copy(out, outsz, full); |
| 374 | return 1; |
| 375 | } |
| 376 | |
| 377 | #else /* POSIX */ |
| 378 | |
| 379 | static int path_under(const char *p, const char *root) |
| 380 | { |
| 381 | size_t n = strlen(root); |
| 382 | if (strcmp(p, root) == 0) return 1; |
| 383 | if (n && root[n - 1] == '/') return strncmp(p, root, n) == 0; |
| 384 | if (n && strncmp(p, root, n) == 0 && p[n] == '/') return 1; |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int canon_path(const char *raw, char *out, size_t outsz, int *err) |
| 389 | { |
| 390 | char *r; |
| 391 | *err = 0; |
| 392 | if (!raw || !*raw) { *err = 400; return 0; } |
| 393 | if (raw[0] != '/') { *err = 400; return 0; } |
| 394 | if (strstr(raw, "/../") || (strlen(raw) > 3 && strcmp(raw + strlen(raw) - 3, "/..") == 0)) { |
| 395 | /* realpath would handle it, but a `..` in a browse path is always a |
| 396 | * probe, never a legit UI request. */ |
| 397 | *err = 400; |
| 398 | return 0; |
| 399 | } |
| 400 | r = realpath(raw, NULL); |
| 401 | if (!r) { *err = (errno == ENOENT || errno == ENOTDIR) ? 404 : 403; return 0; } |
| 402 | str_copy(out, outsz, r); |
| 403 | free(r); |
| 404 | return 1; |
| 405 | } |
| 406 | |
| 407 | #endif |
| 408 | |
| 409 | /* Paths that must never be browsed even when they sit under an allowed root. */ |
| 410 | static int denied_path(const char *p) |
| 411 | { |
| 412 | #if defined(_WIN32) |
| 413 | (void)p; |
| 414 | return 0; |
| 415 | #else |
| 416 | static const char *deny[] = { "/proc", "/sys", "/dev" }; |
| 417 | size_t i; |
| 418 | for (i = 0; i < sizeof deny / sizeof deny[0]; i++) |
| 419 | if (path_under(p, deny[i])) return 1; |
| 420 | return 0; |
| 421 | #endif |
| 422 | } |
| 423 | |
| 424 | static int is_allowed(const char *canon) |
| 425 | { |
| 426 | int i; |
| 427 | if (denied_path(canon)) return 0; |
| 428 | for (i = 0; i < g_root_count; i++) { |
| 429 | #if defined(_WIN32) |
| 430 | if (path_under_win(canon, g_roots[i])) return 1; |
| 431 | #else |
| 432 | if (path_under(canon, g_roots[i])) return 1; |
| 433 | #endif |
| 434 | } |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | /* Resolve + authorise in one step. Returns 1 on success. */ |
| 439 | static int resolve_allowed(const char *raw, char *out, size_t outsz, int *err) |
| 440 | { |
| 441 | if (!canon_path(raw, out, outsz, err)) return 0; |
| 442 | if (!is_allowed(out)) { *err = 403; return 0; } |
| 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | /* Which allowlisted root contains this canonical path (-1 = none). */ |
| 447 | static int root_of(const char *canon) |
| 448 | { |
| 449 | int i; |
| 450 | for (i = 0; i < g_root_count; i++) { |
| 451 | #if defined(_WIN32) |
| 452 | if (path_under_win(canon, g_roots[i])) return i; |
| 453 | #else |
| 454 | if (path_under(canon, g_roots[i])) return i; |
| 455 | #endif |
| 456 | } |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | static void parent_of(const char *canon, char *out, size_t outsz) |
| 461 | { |
| 462 | size_t n; |
| 463 | str_copy(out, outsz, canon); |
| 464 | n = strlen(out); |
| 465 | while (n > 1 && (out[n - 1] == '/' || out[n - 1] == '\\')) out[--n] = '\0'; |
| 466 | #if defined(_WIN32) |
| 467 | while (n > 3 && out[n - 1] != '\\') out[--n] = '\0'; |
| 468 | if (n > 3) out[--n] = '\0'; |
| 469 | else str_copy(out, outsz, canon); |
| 470 | #else |
| 471 | while (n > 1 && out[n - 1] != '/') out[--n] = '\0'; |
| 472 | if (n > 1) out[--n] = '\0'; |
| 473 | if (outsz && out[0] == '\0') str_copy(out, outsz, "/"); |
| 474 | #endif |
| 475 | } |
| 476 | |
| 477 | /* ------------------------------------------------------------------ */ |
| 478 | /* Allowlisted roots */ |
| 479 | /* ------------------------------------------------------------------ */ |
| 480 | |
| 481 | static void add_root(const char *cand) |
| 482 | { |
| 483 | char canon[PATH_SZ]; |
| 484 | int err = 0; |
| 485 | int i; |
| 486 | |
| 487 | if (g_root_count >= MAX_ROOTS) return; |
| 488 | if (!cand || !*cand) return; |
| 489 | if (!canon_path(cand, canon, sizeof canon, &err)) return; |
| 490 | if (denied_path(canon)) return; |
| 491 | for (i = 0; i < g_root_count; i++) { |
| 492 | #if defined(_WIN32) |
| 493 | if (path_under_win(canon, g_roots[i]) && path_under_win(g_roots[i], canon)) return; |
| 494 | #else |
| 495 | if (strcmp(canon, g_roots[i]) == 0) return; |
| 496 | #endif |
| 497 | } |
| 498 | str_copy(g_roots[g_root_count], sizeof g_roots[g_root_count], canon); |
| 499 | g_root_count++; |
| 500 | } |
| 501 | |
| 502 | static void init_roots(void) |
| 503 | { |
| 504 | const char *extra; |
| 505 | const char *home; |
| 506 | |
| 507 | if (g_root_count) return; |
| 508 | |
| 509 | #if defined(_WIN32) |
| 510 | home = getenv("USERPROFILE"); |
| 511 | add_root("D:\\codebases"); |
| 512 | if (home && *home) add_root(home); |
| 513 | #else |
| 514 | add_root("/workspace"); |
| 515 | add_root("/codebases"); |
| 516 | home = getenv("HOME"); |
| 517 | if (home && *home) add_root(home); |
| 518 | #endif |
| 519 | |
| 520 | /* $FILES_ALLOWED_ROOT: one extra root, or several separated by ':' on |
| 521 | * POSIX / ';' on Windows. Tests use it to point at a fixture tree. */ |
| 522 | extra = getenv("FILES_ALLOWED_ROOT"); |
| 523 | if (extra && *extra) { |
| 524 | #if defined(_WIN32) |
| 525 | const char *sep = ";"; |
| 526 | #else |
| 527 | const char *sep = ":"; |
| 528 | #endif |
| 529 | char tmp[PATH_SZ]; |
| 530 | const char *p = extra; |
| 531 | while (*p) { |
| 532 | const char *e = strstr(p, sep); |
| 533 | size_t n = e ? (size_t)(e - p) : strlen(p); |
| 534 | if (n < sizeof tmp) { |
| 535 | memcpy(tmp, p, n); |
| 536 | tmp[n] = '\0'; |
| 537 | add_root(tmp); |
| 538 | } |
| 539 | if (!e) break; |
| 540 | p = e + strlen(sep); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* ------------------------------------------------------------------ */ |
| 546 | /* Directory listing */ |
| 547 | /* ------------------------------------------------------------------ */ |
| 548 | |
| 549 | static int ent_cmp(const void *pa, const void *pb) |
| 550 | { |
| 551 | const ent_t *a = (const ent_t *)pa; |
| 552 | const ent_t *b = (const ent_t *)pb; |
| 553 | if (a->is_dir != b->is_dir) return a->is_dir ? -1 : 1; |
| 554 | return ci_cmp(a->name, b->name); |
| 555 | } |
| 556 | |
| 557 | static void fill_entry_meta(ent_t *e, const char *full) |
| 558 | { |
| 559 | #if defined(_WIN32) |
| 560 | WIN32_FILE_ATTRIBUTE_DATA fd; |
| 561 | if (GetFileAttributesExA(full, GetFileExInfoStandard, &fd)) { |
| 562 | ULARGE_INTEGER sz, tm; |
| 563 | e->size = 0; |
| 564 | sz.LowPart = fd.nFileSizeLow; |
| 565 | sz.HighPart = fd.nFileSizeHigh; |
| 566 | e->size = (long long)sz.QuadPart; |
| 567 | tm.LowPart = fd.ftLastWriteTime.dwLowDateTime; |
| 568 | tm.HighPart = fd.ftLastWriteTime.dwHighDateTime; |
| 569 | /* FILETIME -> unix seconds */ |
| 570 | e->mtime = (long long)((tm.QuadPart / 10000000ULL) - 11644473600ULL); |
| 571 | e->mode = (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? 0755u : 0644u; |
| 572 | } |
| 573 | #else |
| 574 | struct stat st; |
| 575 | struct stat lst; |
| 576 | if (lstat(full, &lst) == 0) { |
| 577 | e->is_link = S_ISLNK(lst.st_mode) ? 1 : 0; |
| 578 | e->size = (long long)lst.st_size; |
| 579 | e->mtime = (long long)lst.st_mtime; |
| 580 | e->mode = (unsigned)(lst.st_mode & 07777); |
| 581 | if (stat(full, &st) == 0) { |
| 582 | if (S_ISDIR(st.st_mode)) e->is_dir = 1; |
| 583 | e->size = (long long)st.st_size; |
| 584 | e->mode = (unsigned)(st.st_mode & 07777); |
| 585 | } |
| 586 | } |
| 587 | #endif |
| 588 | } |
| 589 | |
| 590 | static int list_dir(const char *path, int show_hidden, ent_t **out, int *count, int *truncated) |
| 591 | { |
| 592 | ent_t *ents; |
| 593 | int n = 0; |
| 594 | int trunc = 0; |
| 595 | |
| 596 | ents = (ent_t *)calloc(MAX_ENTRIES, sizeof(ent_t)); |
| 597 | if (!ents) return 0; |
| 598 | |
| 599 | #if defined(_WIN32) |
| 600 | { |
| 601 | char pattern[PATH_SZ]; |
| 602 | WIN32_FIND_DATAA fd; |
| 603 | HANDLE h; |
| 604 | path_join(pattern, sizeof pattern, path, "*"); |
| 605 | h = FindFirstFileA(pattern, &fd); |
| 606 | if (h == INVALID_HANDLE_VALUE) { free(ents); return 0; } |
| 607 | do { |
| 608 | char full[PATH_SZ]; |
| 609 | ent_t *e; |
| 610 | if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, "..")) continue; |
| 611 | if (!show_hidden && fd.cFileName[0] == '.') continue; |
| 612 | if (n >= MAX_ENTRIES) { trunc = 1; break; } |
| 613 | e = &ents[n]; |
| 614 | memset(e, 0, sizeof *e); |
| 615 | str_copy(e->name, sizeof e->name, fd.cFileName); |
| 616 | e->is_dir = (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 0; |
| 617 | e->is_link = (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ? 1 : 0; |
| 618 | path_join(full, sizeof full, path, fd.cFileName); |
| 619 | fill_entry_meta(e, full); |
| 620 | n++; |
| 621 | } while (FindNextFileA(h, &fd)); |
| 622 | FindClose(h); |
| 623 | } |
| 624 | #else |
| 625 | { |
| 626 | DIR *d = opendir(path); |
| 627 | struct dirent *de; |
| 628 | if (!d) { free(ents); return 0; } |
| 629 | while ((de = readdir(d)) != NULL) { |
| 630 | char full[PATH_SZ]; |
| 631 | ent_t *e; |
| 632 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue; |
| 633 | if (!show_hidden && de->d_name[0] == '.') continue; |
| 634 | if (n >= MAX_ENTRIES) { trunc = 1; break; } |
| 635 | e = &ents[n]; |
| 636 | memset(e, 0, sizeof *e); |
| 637 | str_copy(e->name, sizeof e->name, de->d_name); |
| 638 | path_join(full, sizeof full, path, de->d_name); |
| 639 | fill_entry_meta(e, full); |
| 640 | n++; |
| 641 | } |
| 642 | closedir(d); |
| 643 | } |
| 644 | #endif |
| 645 | |
| 646 | if (n > 1) qsort(ents, (size_t)n, sizeof(ent_t), ent_cmp); |
| 647 | *out = ents; |
| 648 | *count = n; |
| 649 | *truncated = trunc; |
| 650 | return 1; |
| 651 | } |
| 652 | |
| 653 | /* ------------------------------------------------------------------ */ |
| 654 | /* Node authentication */ |
| 655 | /* ------------------------------------------------------------------ */ |
| 656 | |
| 657 | static void require_node_auth(void) |
| 658 | { |
| 659 | const char *np = getenv("BOT_PASSWORD"); |
| 660 | const char *xp; |
| 661 | const char *auth; |
| 662 | const char *qs; |
| 663 | char qpwd[256]; |
| 664 | |
| 665 | if (!np || !*np) return; |
| 666 | |
| 667 | xp = getenv("HTTP_X_BOT_PASSWORD"); |
| 668 | if (xp && strcmp(xp, np) == 0) return; |
| 669 | |
| 670 | auth = getenv("HTTP_AUTHORIZATION"); |
| 671 | if (auth) { |
| 672 | while (*auth == ' ') auth++; |
| 673 | if (strncmp(auth, "Bearer ", 7) == 0) { |
| 674 | auth += 7; |
| 675 | while (*auth == ' ') auth++; |
| 676 | if (strcmp(auth, np) == 0) return; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | qs = getenv("QUERY_STRING"); |
| 681 | if (qs && get_query_val(qs, "pwd", qpwd, sizeof qpwd) && strcmp(qpwd, np) == 0) |
| 682 | return; |
| 683 | |
| 684 | fputs("Status: 401 Unauthorized\r\n" |
| 685 | "Content-Type: application/json; charset=utf-8\r\n" |
| 686 | "Cache-Control: no-store\r\n" |
| 687 | "X-Content-Type-Options: nosniff\r\n\r\n" |
| 688 | "{\"ok\":false,\"error\":\"invalid node password\"}\n", stdout); |
| 689 | exit(0); |
| 690 | } |
| 691 | |
| 692 | /* ------------------------------------------------------------------ */ |
| 693 | /* JSON responses */ |
| 694 | /* ------------------------------------------------------------------ */ |
| 695 | |
| 696 | static void json_header(void) |
| 697 | { |
| 698 | fputs("Content-Type: application/json; charset=utf-8\r\n" |
| 699 | "Cache-Control: no-store\r\n" |
| 700 | "X-Content-Type-Options: nosniff\r\n\r\n", stdout); |
| 701 | } |
| 702 | |
| 703 | static void json_err(int code, const char *msg) |
| 704 | { |
| 705 | if (code == 400) fputs("Status: 400 Bad Request\r\n", stdout); |
| 706 | else if (code == 403) fputs("Status: 403 Forbidden\r\n", stdout); |
| 707 | else if (code == 404) fputs("Status: 404 Not Found\r\n", stdout); |
| 708 | else if (code == 405) fputs("Status: 405 Method Not Allowed\r\n", stdout); |
| 709 | else if (code == 413) fputs("Status: 413 Payload Too Large\r\n", stdout); |
| 710 | else fputs("Status: 500 Internal Server Error\r\n", stdout); |
| 711 | json_header(); |
| 712 | printf("{\"ok\":false,\"error\":"); |
| 713 | { |
| 714 | buf_t b; |
| 715 | buf_init(&b); |
| 716 | json_str(&b, msg ? msg : "error"); |
| 717 | fputs(buf_str(&b), stdout); |
| 718 | buf_free(&b); |
| 719 | } |
| 720 | fputs("}\n", stdout); |
| 721 | } |
| 722 | |
| 723 | /* ------------------------------------------------------------------ */ |
| 724 | /* ?action=roots */ |
| 725 | /* ------------------------------------------------------------------ */ |
| 726 | |
| 727 | static void do_roots(void) |
| 728 | { |
| 729 | int i; |
| 730 | buf_t b; |
| 731 | buf_init(&b); |
| 732 | buf_printf(&b, "{\"ok\":true,\"count\":%d,\"roots\":[", g_root_count); |
| 733 | for (i = 0; i < g_root_count; i++) { |
| 734 | const char *base = strrchr(g_roots[i], SEP); |
| 735 | char label[NAME_SZ]; |
| 736 | if (i) buf_putc(&b, ','); |
| 737 | buf_puts(&b, "\n {\"path\":"); |
| 738 | json_str(&b, g_roots[i]); |
| 739 | buf_puts(&b, ",\"name\":"); |
| 740 | if (base && base[1]) str_copy(label, sizeof label, base + 1); |
| 741 | else str_copy(label, sizeof label, g_roots[i]); |
| 742 | json_str(&b, label); |
| 743 | buf_putc(&b, '}'); |
| 744 | } |
| 745 | buf_puts(&b, "\n]}\n"); |
| 746 | json_header(); |
| 747 | fputs(buf_str(&b), stdout); |
| 748 | buf_free(&b); |
| 749 | } |
| 750 | |
| 751 | /* ------------------------------------------------------------------ */ |
| 752 | /* ?action=ls&path=<abs> */ |
| 753 | /* ------------------------------------------------------------------ */ |
| 754 | |
| 755 | static void do_ls(const char *raw_path, const char *qs) |
| 756 | { |
| 757 | char canon[PATH_SZ]; |
| 758 | char parent[PATH_SZ]; |
| 759 | char hiddenbuf[8]; |
| 760 | int show_hidden = 0; |
| 761 | int err = 0; |
| 762 | int count = 0, trunc = 0; |
| 763 | int ri; |
| 764 | ent_t *ents = NULL; |
| 765 | buf_t b; |
| 766 | int i; |
| 767 | |
| 768 | if (!raw_path || !*raw_path) { |
| 769 | if (g_root_count) str_copy(canon, sizeof canon, g_roots[0]); |
| 770 | else { json_err(500, "no allowlisted root available"); return; } |
| 771 | } |
| 772 | |
| 773 | if (raw_path && *raw_path) { |
| 774 | if (!resolve_allowed(raw_path, canon, sizeof canon, &err)) { |
| 775 | json_err(err, err == 403 ? "path not allowed" : (err == 404 ? "path not found" : "bad path")); |
| 776 | return; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | if (get_query_val(qs, "hidden", hiddenbuf, sizeof hiddenbuf)) { |
| 781 | show_hidden = (strcmp(hiddenbuf, "1") == 0 || strcmp(hiddenbuf, "true") == 0); |
| 782 | } |
| 783 | |
| 784 | if (!list_dir(canon, show_hidden, &ents, &count, &trunc)) { |
| 785 | json_err(403, "cannot list directory"); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | ri = root_of(canon); |
| 790 | parent[0] = '\0'; |
| 791 | if (ri >= 0 && strcmp(canon, g_roots[ri]) != 0) |
| 792 | parent_of(canon, parent, sizeof parent); |
| 793 | |
| 794 | buf_init(&b); |
| 795 | buf_puts(&b, "{\"ok\":true,\"path\":"); |
| 796 | json_str(&b, canon); |
| 797 | buf_puts(&b, ",\"parent\":"); |
| 798 | if (parent[0]) json_str(&b, parent); else buf_puts(&b, "null"); |
| 799 | buf_puts(&b, ",\"root\":"); |
| 800 | if (ri >= 0) json_str(&b, g_roots[ri]); else buf_puts(&b, "null"); |
| 801 | buf_puts(&b, ",\"hidden\":"); |
| 802 | buf_putc(&b, show_hidden ? '1' : '0'); |
| 803 | buf_printf(&b, ",\"count\":%d,\"truncated\":%s,\"entries\":[", |
| 804 | count, trunc ? "true" : "false"); |
| 805 | |
| 806 | for (i = 0; i < count; i++) { |
| 807 | char iso[32]; |
| 808 | char mode[8]; |
| 809 | fmt_iso(ents[i].mtime, iso, sizeof iso); |
| 810 | sprintf(mode, "%04o", ents[i].mode & 07777); |
| 811 | buf_puts(&b, i ? ",\n {\"name\":" : "\n {\"name\":"); |
| 812 | json_str(&b, ents[i].name); |
| 813 | buf_printf(&b, ",\"type\":\"%s\",\"link\":%s,\"size\":%lld," |
| 814 | "\"mtime\":\"%s\",\"mode\":\"%s\"}", |
| 815 | ents[i].is_dir ? "dir" : "file", |
| 816 | ents[i].is_link ? "true" : "false", |
| 817 | ents[i].size, iso, mode); |
| 818 | } |
| 819 | buf_puts(&b, "\n]}\n"); |
| 820 | |
| 821 | json_header(); |
| 822 | fputs(buf_str(&b), stdout); |
| 823 | free(ents); |
| 824 | buf_free(&b); |
| 825 | } |
| 826 | |
| 827 | /* ------------------------------------------------------------------ */ |
| 828 | /* ?action=read&path=<abs> */ |
| 829 | /* ------------------------------------------------------------------ */ |
| 830 | |
| 831 | static int read_range(const char *path, char *out, size_t cap, size_t *got, int *binary) |
| 832 | { |
| 833 | FILE *f; |
| 834 | size_t n; |
| 835 | |
| 836 | *got = 0; |
| 837 | *binary = 0; |
| 838 | f = fopen(path, "rb"); |
| 839 | if (!f) return 0; |
| 840 | n = fread(out, 1, cap, f); |
| 841 | if (ferror(f)) { fclose(f); return 0; } |
| 842 | *got = n; |
| 843 | { |
| 844 | size_t lim = n < BIN_PROBE ? n : BIN_PROBE; |
| 845 | size_t i; |
| 846 | for (i = 0; i < lim; i++) { |
| 847 | if (out[i] == '\0') { *binary = 1; break; } |
| 848 | } |
| 849 | } |
| 850 | fclose(f); |
| 851 | return 1; |
| 852 | } |
| 853 | |
| 854 | static void do_read(const char *raw_path) |
| 855 | { |
| 856 | char canon[PATH_SZ]; |
| 857 | int err = 0; |
| 858 | struct { long long size; long long mtime; unsigned mode; int is_dir; } info; |
| 859 | char iso[32], mode[8]; |
| 860 | static char text[TEXT_CAP + 1]; |
| 861 | size_t got = 0; |
| 862 | int binary = 0; |
| 863 | int trunc = 0; |
| 864 | buf_t b; |
| 865 | |
| 866 | #if defined(_WIN32) |
| 867 | { |
| 868 | WIN32_FILE_ATTRIBUTE_DATA fd; |
| 869 | memset(&info, 0, sizeof info); |
| 870 | if (!resolve_allowed(raw_path, canon, sizeof canon, &err)) { |
| 871 | json_err(err, err == 403 ? "path not allowed" : (err == 404 ? "path not found" : "bad path")); |
| 872 | return; |
| 873 | } |
| 874 | if (!GetFileAttributesExA(canon, GetFileExInfoStandard, &fd)) { |
| 875 | json_err(404, "path not found"); |
| 876 | return; |
| 877 | } |
| 878 | info.is_dir = (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 0; |
| 879 | { |
| 880 | ULARGE_INTEGER sz, tm; |
| 881 | sz.LowPart = fd.nFileSizeLow; sz.HighPart = fd.nFileSizeHigh; |
| 882 | info.size = (long long)sz.QuadPart; |
| 883 | tm.LowPart = fd.ftLastWriteTime.dwLowDateTime; tm.HighPart = fd.ftLastWriteTime.dwHighDateTime; |
| 884 | info.mtime = (long long)((tm.QuadPart / 10000000ULL) - 11644473600ULL); |
| 885 | } |
| 886 | info.mode = info.is_dir ? 0755u : 0644u; |
| 887 | } |
| 888 | #else |
| 889 | { |
| 890 | struct stat st; |
| 891 | memset(&info, 0, sizeof info); |
| 892 | if (!resolve_allowed(raw_path, canon, sizeof canon, &err)) { |
| 893 | json_err(err, err == 403 ? "path not allowed" : (err == 404 ? "path not found" : "bad path")); |
| 894 | return; |
| 895 | } |
| 896 | if (stat(canon, &st) != 0) { json_err(404, "path not found"); return; } |
| 897 | info.is_dir = S_ISDIR(st.st_mode) ? 1 : 0; |
| 898 | info.size = (long long)st.st_size; |
| 899 | info.mtime = (long long)st.st_mtime; |
| 900 | info.mode = (unsigned)(st.st_mode & 07777); |
| 901 | } |
| 902 | #endif |
| 903 | |
| 904 | if (info.is_dir) { json_err(400, "not a regular file"); return; } |
| 905 | |
| 906 | if (info.size > TEXT_CAP) trunc = 1; |
| 907 | if (!read_range(canon, text, TEXT_CAP, &got, &binary)) { |
| 908 | json_err(403, "cannot read file"); |
| 909 | return; |
| 910 | } |
| 911 | text[got] = '\0'; |
| 912 | if (binary) trunc = 0; |
| 913 | |
| 914 | fmt_iso(info.mtime, iso, sizeof iso); |
| 915 | sprintf(mode, "%04o", info.mode & 07777); |
| 916 | buf_init(&b); |
| 917 | buf_puts(&b, "{\"ok\":true,\"path\":"); |
| 918 | json_str(&b, canon); |
| 919 | buf_puts(&b, ",\"name\":"); |
| 920 | { |
| 921 | const char *base = strrchr(canon, SEP); |
| 922 | json_str(&b, (base && base[1]) ? base + 1 : canon); |
| 923 | } |
| 924 | buf_printf(&b, ",\"size\":%lld,\"mtime\":\"%s\",\"mode\":\"%s\"", info.size, iso, mode); |
| 925 | buf_printf(&b, ",\"binary\":%s,\"truncated\":%s,\"bytes_read\":%lu", |
| 926 | binary ? "true" : "false", trunc ? "true" : "false", (unsigned long)got); |
| 927 | if (!binary) { |
| 928 | buf_puts(&b, ",\"text\":"); |
| 929 | json_str(&b, text); |
| 930 | } |
| 931 | buf_puts(&b, "}\n"); |
| 932 | |
| 933 | json_header(); |
| 934 | fputs(buf_str(&b), stdout); |
| 935 | buf_free(&b); |
| 936 | } |
| 937 | |
| 938 | typedef struct { const char *ext; const char *type; } mime_t; |
| 939 | |
| 940 | static const mime_t g_mime[] = { |
| 941 | { "png", "image/png" }, |
| 942 | { "jpg", "image/jpeg" }, |
| 943 | { "jpeg", "image/jpeg" }, |
| 944 | { "gif", "image/gif" }, |
| 945 | { "webp", "image/webp" }, |
| 946 | { "bmp", "image/bmp" }, |
| 947 | { "ico", "image/x-icon" }, |
| 948 | { "svg", "text/plain; charset=utf-8" }, |
| 949 | { "pdf", "application/pdf" }, |
| 950 | { "txt", "text/plain; charset=utf-8" }, |
| 951 | { "md", "text/plain; charset=utf-8" }, |
| 952 | { "json", "text/plain; charset=utf-8" }, |
| 953 | { "c", "text/plain; charset=utf-8" }, |
| 954 | { "h", "text/plain; charset=utf-8" }, |
| 955 | { "log", "text/plain; charset=utf-8" } |
| 956 | }; |
| 957 | |
| 958 | /* Markup is served as text/plain on purpose: this route shares an origin with |
| 959 | * the node API, so rendering a local .html file as text/html would hand any |
| 960 | * page on disk the ability to call the plugin API with the user's password. |
| 961 | * Images keep their real type because <img> only decodes them. */ |
| 962 | static const char *guess_type(const char *name) |
| 963 | { |
| 964 | const char *dot = strrchr(name, '.'); |
| 965 | size_t i; |
| 966 | if (!dot || !dot[1]) return "application/octet-stream"; |
| 967 | for (i = 0; i < sizeof g_mime / sizeof g_mime[0]; i++) { |
| 968 | size_t k = strlen(g_mime[i].ext); |
| 969 | size_t n = strlen(dot + 1); |
| 970 | if (n == k) { |
| 971 | size_t j; int same = 1; |
| 972 | for (j = 0; j < k; j++) |
| 973 | if (tolower((unsigned char)dot[1 + j]) != g_mime[i].ext[j]) { same = 0; break; } |
| 974 | if (same) return g_mime[i].type; |
| 975 | } |
| 976 | } |
| 977 | if (strstr(dot + 1, "htm")) return "text/plain; charset=utf-8"; |
| 978 | return "application/octet-stream"; |
| 979 | } |
| 980 | |
| 981 | static int is_image_type(const char *t) |
| 982 | { |
| 983 | return strncmp(t, "image/", 6) == 0; |
| 984 | } |
| 985 | |
| 986 | /* Header-safe file name: quotes, CR and LF out. */ |
| 987 | static void header_name(const char *name, char *out, size_t sz) |
| 988 | { |
| 989 | size_t i = 0; |
| 990 | for (; *name && i + 1 < sz; name++) { |
| 991 | char c = *name; |
| 992 | if (c == '"' || c == '\\' || c == '\r' || c == '\n') c = '_'; |
| 993 | out[i++] = c; |
| 994 | } |
| 995 | out[i] = '\0'; |
| 996 | } |
| 997 | |
| 998 | static void do_raw(const char *raw_path, const char *qs) |
| 999 | { |
| 1000 | char canon[PATH_SZ]; |
| 1001 | char name[NAME_SZ]; |
| 1002 | char hn[NAME_SZ]; |
| 1003 | char dl[8]; |
| 1004 | const char *base; |
| 1005 | const char *type; |
| 1006 | int err = 0; |
| 1007 | long long size = -1; |
| 1008 | int want_dl = 0; |
| 1009 | FILE *f; |
| 1010 | static char chunk[IO_CHUNK]; |
| 1011 | |
| 1012 | #if defined(_WIN32) |
| 1013 | { |
| 1014 | WIN32_FILE_ATTRIBUTE_DATA fd; |
| 1015 | if (!resolve_allowed(raw_path, canon, sizeof canon, &err)) { |
| 1016 | json_err(err, err == 403 ? "path not allowed" : (err == 404 ? "path not found" : "bad path")); |
| 1017 | return; |
| 1018 | } |
| 1019 | if (!GetFileAttributesExA(canon, GetFileExInfoStandard, &fd)) { json_err(404, "path not found"); return; } |
| 1020 | if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { json_err(400, "not a regular file"); return; } |
| 1021 | { |
| 1022 | ULARGE_INTEGER sz; |
| 1023 | sz.LowPart = fd.nFileSizeLow; sz.HighPart = fd.nFileSizeHigh; |
| 1024 | size = (long long)sz.QuadPart; |
| 1025 | } |
| 1026 | } |
| 1027 | #else |
| 1028 | { |
| 1029 | struct stat st; |
| 1030 | if (!resolve_allowed(raw_path, canon, sizeof canon, &err)) { |
| 1031 | json_err(err, err == 403 ? "path not allowed" : (err == 404 ? "path not found" : "bad path")); |
| 1032 | return; |
| 1033 | } |
| 1034 | if (stat(canon, &st) != 0) { json_err(404, "path not found"); return; } |
| 1035 | if (S_ISDIR(st.st_mode)) { json_err(400, "not a regular file"); return; } |
| 1036 | size = (long long)st.st_size; |
| 1037 | } |
| 1038 | #endif |
| 1039 | |
| 1040 | if (size > RAW_CAP) { json_err(413, "file larger than 64 MiB"); return; } |
| 1041 | |
| 1042 | base = strrchr(canon, SEP); |
| 1043 | str_copy(name, sizeof name, (base && base[1]) ? base + 1 : canon); |
| 1044 | type = guess_type(name); |
| 1045 | header_name(name, hn, sizeof hn); |
| 1046 | |
| 1047 | if (get_query_val(qs, "download", dl, sizeof dl)) |
| 1048 | want_dl = (strcmp(dl, "1") == 0 || strcmp(dl, "true") == 0); |
| 1049 | |
| 1050 | f = fopen(canon, "rb"); |
| 1051 | if (!f) { json_err(403, "cannot open file"); return; } |
| 1052 | |
| 1053 | printf("Content-Type: %s\r\n", type); |
| 1054 | fputs("Cache-Control: no-store\r\n", stdout); |
| 1055 | fputs("X-Content-Type-Options: nosniff\r\n", stdout); |
| 1056 | fputs("Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; " |
| 1057 | "img-src data:\r\n", stdout); |
| 1058 | fputs("X-Frame-Options: SAMEORIGIN\r\n", stdout); |
| 1059 | printf("Content-Disposition: %s; filename=\"%s\"\r\n", |
| 1060 | (want_dl || !is_image_type(type)) ? "attachment" : "inline", hn); |
| 1061 | /* Deliberately no Content-Length - the server owns framing. */ |
| 1062 | fputs("\r\n", stdout); |
| 1063 | |
| 1064 | for (;;) { |
| 1065 | size_t n = fread(chunk, 1, sizeof chunk, f); |
| 1066 | if (n > 0) fwrite(chunk, 1, n, stdout); |
| 1067 | if (n < sizeof chunk) break; |
| 1068 | } |
| 1069 | fclose(f); |
| 1070 | fflush(stdout); |
| 1071 | } |
| 1072 | |
| 1073 | /* ------------------------------------------------------------------ */ |
| 1074 | /* HTML mode */ |
| 1075 | /* ------------------------------------------------------------------ */ |
| 1076 | |
| 1077 | /* Expand the two control-byte placeholders from src/ui.html into the bot |
| 1078 | * theme macros, so the page and the bot palette can never drift. */ |
| 1079 | static void emit_ui(int embed) |
| 1080 | { |
| 1081 | const char *p = UI_HTML; |
| 1082 | |
| 1083 | while (*p) { |
| 1084 | if (*p == '\001') { |
| 1085 | fputs(SX_BOT_THEME_PREPAINT "<style>" SX_BOT_THEME_TOKENS |
| 1086 | ":root{--tx:var(--text);--mu:var(--text-2);--ln:var(--line);--ac:var(--blue)}" |
| 1087 | "</style>", stdout); |
| 1088 | p++; |
| 1089 | } else if (*p == '\002') { |
| 1090 | fputs(SX_BOT_THEME_HANDOFF, stdout); |
| 1091 | p++; |
| 1092 | } else { |
| 1093 | putchar(*p++); |
| 1094 | } |
| 1095 | } |
| 1096 | (void)embed; |
| 1097 | } |
| 1098 | |
| 1099 | static void mode_html(int embed) |
| 1100 | { |
| 1101 | fputs("Content-Type: text/html; charset=utf-8\r\n" |
| 1102 | "Cache-Control: no-store\r\n" |
| 1103 | "X-Content-Type-Options: nosniff\r\n" |
| 1104 | "X-Frame-Options: SAMEORIGIN\r\n" |
| 1105 | "Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'; " |
| 1106 | "style-src 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; " |
| 1107 | "form-action 'none'; base-uri 'none'\r\n\r\n", stdout); |
| 1108 | emit_ui(embed); |
| 1109 | } |
| 1110 | |
| 1111 | /* ------------------------------------------------------------------ */ |
| 1112 | /* Entry */ |
| 1113 | /* ------------------------------------------------------------------ */ |
| 1114 | |
| 1115 | static const char *method(void) |
| 1116 | { |
| 1117 | const char *m = getenv("REQUEST_METHOD"); |
| 1118 | return (m && *m) ? m : "GET"; |
| 1119 | } |
| 1120 | |
| 1121 | int main(int argc, char **argv) |
| 1122 | { |
| 1123 | const char *qs; |
| 1124 | char action[64]; |
| 1125 | char path[PATH_SZ]; |
| 1126 | |
| 1127 | /* 1. Discovery metadata (unauthenticated so /plugins can probe it) */ |
| 1128 | if ((argc > 1 && strcmp(argv[1], "--meta") == 0) || getenv("SX_PLUGIN_META")) { |
| 1129 | printf("{\"name\":\"%s\",\"desc\":\"%s\"}\n", PLUGIN_NAME, PLUGIN_DESC); |
| 1130 | return 0; |
| 1131 | } |
| 1132 | if (argc > 1 && strcmp(argv[1], "--version") == 0) { |
| 1133 | printf("%s %s\n", PLUGIN_ID, PLUGIN_VER); |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | require_node_auth(); |
| 1138 | init_roots(); |
| 1139 | |
| 1140 | qs = getenv("QUERY_STRING"); |
| 1141 | if (!qs) qs = ""; |
| 1142 | |
| 1143 | action[0] = '\0'; |
| 1144 | get_query_val(qs, "action", action, sizeof action); |
| 1145 | |
| 1146 | /* 2. Anything that names an action or asks for JSON is API. */ |
| 1147 | if (action[0] || strstr(qs, "format=json")) { |
| 1148 | char fmt[32]; |
| 1149 | if (!action[0]) { |
| 1150 | if (get_query_val(qs, "format", fmt, sizeof fmt) && strcmp(fmt, "json") == 0) |
| 1151 | str_copy(action, sizeof action, "roots"); |
| 1152 | } |
| 1153 | |
| 1154 | path[0] = '\0'; |
| 1155 | get_query_val(qs, "path", path, sizeof path); |
| 1156 | |
| 1157 | if (strcmp(action, "roots") == 0) { |
| 1158 | do_roots(); |
| 1159 | } else if (strcmp(action, "ls") == 0) { |
| 1160 | do_ls(path, qs); |
| 1161 | } else if (strcmp(action, "read") == 0) { |
| 1162 | do_read(path); |
| 1163 | } else if (strcmp(action, "raw") == 0) { |
| 1164 | do_raw(path, qs); |
| 1165 | } else { |
| 1166 | json_err(400, "unknown action"); |
| 1167 | } |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | /* 3. Everything else is the page. */ |
| 1172 | { |
| 1173 | int embed = strstr(qs, "embed=1") != NULL || strstr(qs, "embed=true") != NULL; |
| 1174 | mode_html(embed); |
| 1175 | } |
| 1176 | (void)method; |
| 1177 | return 0; |
| 1178 | } |