| 1 | /* |
| 2 | * ws_config.c - where plugin_websearch keeps its state, and how a |
| 3 | * configuration value is resolved. |
| 4 | * |
| 5 | * Resolution order, lowest priority first: |
| 6 | * |
| 7 | * 1. compiled defaults (api.search.brave.com, ...) |
| 8 | * 2. <connector dir>/config.json mode 0600, written by the setup page |
| 9 | * 3. the environment (BRAVE_API_KEY, WEBSEARCH_* ...) |
| 10 | * |
| 11 | * The environment wins because that is the only layer an operator can set |
| 12 | * without a browser, and because the offline suite needs to point every |
| 13 | * provider at a loopback mock without touching a real config file. |
| 14 | * |
| 15 | * The connector directory deliberately lives OUTSIDE the plugin binary's own |
| 16 | * directory, so a plugin update (which replaces the binary) can never take the |
| 17 | * user's keys with it: |
| 18 | * |
| 19 | * $SWEBSEARCH_CONFIG_DIR (tests only) |
| 20 | * /workspace/.scorpiox/connectors/plugin_websearch (BotCloud, survives a |
| 21 | * container rebuild) |
| 22 | * ~/.scorpiox/connectors/plugin_websearch (POSIX) |
| 23 | * %LOCALAPPDATA%\scorpiox\connectors\plugin_websearch (Windows) |
| 24 | * |
| 25 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 26 | */ |
| 27 | |
| 28 | #if defined(_WIN32) && !defined(_WIN32_WINNT) |
| 29 | # define _WIN32_WINNT 0x0601 |
| 30 | #endif |
| 31 | #if !defined(_WIN32) |
| 32 | # if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L |
| 33 | # undef _POSIX_C_SOURCE |
| 34 | # define _POSIX_C_SOURCE 200809L |
| 35 | # endif |
| 36 | # ifndef _DEFAULT_SOURCE |
| 37 | # define _DEFAULT_SOURCE 1 |
| 38 | # endif |
| 39 | #endif |
| 40 | |
| 41 | #include "ws.h" |
| 42 | |
| 43 | #include <errno.h> |
| 44 | #include <stdio.h> |
| 45 | #include <stdlib.h> |
| 46 | #include <string.h> |
| 47 | |
| 48 | #if defined(_WIN32) |
| 49 | # define WIN32_LEAN_AND_MEAN |
| 50 | # include <windows.h> |
| 51 | # include <direct.h> |
| 52 | # include <io.h> |
| 53 | #else |
| 54 | # include <sys/stat.h> |
| 55 | # include <sys/types.h> |
| 56 | # include <unistd.h> |
| 57 | #endif |
| 58 | |
| 59 | #define WS_DEFAULT_DDG_BASE "https://html.duckduckgo.com" |
| 60 | #define WS_DEFAULT_BRAVE_BASE "https://api.search.brave.com" |
| 61 | #define WS_DEFAULT_TAVILY_BASE "https://api.tavily.com" |
| 62 | #define WS_DEFAULT_EXA_BASE "https://api.exa.ai" |
| 63 | #define WS_DEFAULT_PPLX_BASE "https://api.perplexity.ai" |
| 64 | |
| 65 | static int ws_is_dir(const char *path) |
| 66 | { |
| 67 | #if defined(_WIN32) |
| 68 | DWORD a = GetFileAttributesA(path); |
| 69 | return (a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY)) ? 1 : 0; |
| 70 | #else |
| 71 | struct stat st; |
| 72 | return (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) ? 1 : 0; |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | /* mkdir -p. Returns 0 when the directory exists afterwards. */ |
| 77 | static int ws_mkdir_p(const char *path) |
| 78 | { |
| 79 | char tmp[1024]; |
| 80 | size_t len, i; |
| 81 | |
| 82 | ws_copy(tmp, sizeof tmp, path); |
| 83 | len = strlen(tmp); |
| 84 | if (len == 0) return -1; |
| 85 | for (i = 1; i <= len; i++) { |
| 86 | if (tmp[i] == '/' || tmp[i] == '\\' || tmp[i] == '\0') { |
| 87 | char save = tmp[i]; |
| 88 | tmp[i] = '\0'; |
| 89 | #if defined(_WIN32) |
| 90 | _mkdir(tmp); |
| 91 | #else |
| 92 | if (mkdir(tmp, 0700) != 0 && errno != EEXIST) { /* keep going: the |
| 93 | final stat below is the real answer */ } |
| 94 | #endif |
| 95 | tmp[i] = save; |
| 96 | if (save == '\0') break; |
| 97 | } |
| 98 | } |
| 99 | return ws_is_dir(path) ? 0 : -1; |
| 100 | } |
| 101 | |
| 102 | int ws_config_dir(char *out, size_t capsz) |
| 103 | { |
| 104 | const char *env = getenv("SWEBSEARCH_CONFIG_DIR"); |
| 105 | char base[1024]; |
| 106 | |
| 107 | if (!out || capsz == 0) return -1; |
| 108 | out[0] = '\0'; |
| 109 | |
| 110 | if (env && *env) { |
| 111 | ws_copy(out, capsz, env); |
| 112 | return ws_mkdir_p(out); |
| 113 | } |
| 114 | if (ws_is_dir("/workspace")) { |
| 115 | snprintf(base, sizeof base, "/workspace/.scorpiox/connectors/%s", WS_ID); |
| 116 | ws_copy(out, capsz, base); |
| 117 | if (ws_mkdir_p(out) == 0) return 0; |
| 118 | } |
| 119 | #if defined(_WIN32) |
| 120 | { |
| 121 | const char *lad = getenv("LOCALAPPDATA"); |
| 122 | if (lad && *lad) { |
| 123 | snprintf(base, sizeof base, "%s\\scorpiox\\connectors\\%s", lad, WS_ID); |
| 124 | } else { |
| 125 | const char *up = getenv("USERPROFILE"); |
| 126 | snprintf(base, sizeof base, "%s\\.scorpiox\\connectors\\%s", |
| 127 | (up && *up) ? up : ".", WS_ID); |
| 128 | } |
| 129 | } |
| 130 | #else |
| 131 | { |
| 132 | const char *home = getenv("HOME"); |
| 133 | snprintf(base, sizeof base, "%s/.scorpiox/connectors/%s", |
| 134 | (home && *home) ? home : ".", WS_ID); |
| 135 | } |
| 136 | #endif |
| 137 | ws_copy(out, capsz, base); |
| 138 | return ws_mkdir_p(out); |
| 139 | } |
| 140 | |
| 141 | int ws_config_path(char *out, size_t capsz) |
| 142 | { |
| 143 | char dir[1024]; |
| 144 | |
| 145 | if (ws_config_dir(dir, sizeof dir) != 0) return -1; |
| 146 | snprintf(out, capsz, "%s/config.json", dir); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static void set_defaults(ws_config *c) |
| 151 | { |
| 152 | memset(c, 0, sizeof *c); |
| 153 | ws_copy(c->active, sizeof c->active, "duckduckgo"); |
| 154 | c->count = WS_DEFAULT_COUNT; |
| 155 | ws_copy(c->region, sizeof c->region, ""); |
| 156 | ws_copy(c->safe, sizeof c->safe, "moderate"); |
| 157 | ws_copy(c->language, sizeof c->language, ""); |
| 158 | ws_copy(c->searxng_categories, sizeof c->searxng_categories, "general"); |
| 159 | c->tavily_advanced = 0; |
| 160 | c->tavily_answer = 0; |
| 161 | |
| 162 | ws_copy(c->ddg_base, sizeof c->ddg_base, WS_DEFAULT_DDG_BASE); |
| 163 | ws_copy(c->brave_base, sizeof c->brave_base, WS_DEFAULT_BRAVE_BASE); |
| 164 | ws_copy(c->tavily_base, sizeof c->tavily_base, WS_DEFAULT_TAVILY_BASE); |
| 165 | ws_copy(c->exa_base, sizeof c->exa_base, WS_DEFAULT_EXA_BASE); |
| 166 | ws_copy(c->pplx_base, sizeof c->pplx_base, WS_DEFAULT_PPLX_BASE); |
| 167 | c->searxng_base[0] = '\0'; |
| 168 | } |
| 169 | |
| 170 | /* A JSON string field, or NULL when absent/not a string. */ |
| 171 | static const char *jstr(yyjson_val *o, const char *key) |
| 172 | { |
| 173 | yyjson_val *v; |
| 174 | if (!o || !yyjson_is_obj(o)) return NULL; |
| 175 | v = yyjson_obj_get(o, key); |
| 176 | return (v && yyjson_is_str(v)) ? yyjson_get_str(v) : NULL; |
| 177 | } |
| 178 | |
| 179 | /* A JSON integer or boolean-as-int field, or `deflt`. A page sends |
| 180 | * {"tavily_answer": true} from a checkbox, so a bool has to count. */ |
| 181 | static int jint(yyjson_val *o, const char *key, int deflt) |
| 182 | { |
| 183 | yyjson_val *v; |
| 184 | if (!o || !yyjson_is_obj(o)) return deflt; |
| 185 | v = yyjson_obj_get(o, key); |
| 186 | if (!v) return deflt; |
| 187 | if (yyjson_is_bool(v)) return yyjson_get_bool(v) ? 1 : 0; |
| 188 | if (yyjson_is_int(v)) return (int)yyjson_get_int(v); |
| 189 | if (yyjson_is_str(v)) return atoi(yyjson_get_str(v)); |
| 190 | return deflt; |
| 191 | } |
| 192 | |
| 193 | /* Empty string means "not configured" — writing "" from a cleared field must |
| 194 | * switch a provider off rather than send an empty header. */ |
| 195 | static void put_str(char *dst, size_t capsz, const char *val, int keep_empty) |
| 196 | { |
| 197 | if (!val) return; |
| 198 | if (!*val && !keep_empty) { dst[0] = '\0'; return; } |
| 199 | ws_copy(dst, capsz, val); |
| 200 | } |
| 201 | |
| 202 | static void load_file(ws_config *c, const char *path) |
| 203 | { |
| 204 | FILE *f; |
| 205 | long sz; |
| 206 | char *buf; |
| 207 | size_t got; |
| 208 | yyjson_doc *doc; |
| 209 | yyjson_val *root; |
| 210 | |
| 211 | f = fopen(path, "rb"); |
| 212 | if (!f) return; |
| 213 | if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return; } |
| 214 | sz = ftell(f); |
| 215 | if (sz <= 0 || sz > WS_BODY_MAX) { fclose(f); return; } |
| 216 | rewind(f); |
| 217 | buf = (char *)malloc((size_t)sz + 1); |
| 218 | if (!buf) { fclose(f); return; } |
| 219 | got = fread(buf, 1, (size_t)sz, f); |
| 220 | fclose(f); |
| 221 | buf[got] = '\0'; |
| 222 | |
| 223 | doc = yyjson_read(buf, got, 0); |
| 224 | free(buf); |
| 225 | if (!doc) return; |
| 226 | root = yyjson_doc_get_root(doc); |
| 227 | if (root && yyjson_is_obj(root)) { |
| 228 | put_str(c->active, sizeof c->active, jstr(root, "provider"), 1); |
| 229 | if (jstr(root, "provider") && !c->active[0]) |
| 230 | ws_copy(c->active, sizeof c->active, "duckduckgo"); |
| 231 | |
| 232 | { |
| 233 | int n = jint(root, "count", c->count); |
| 234 | if (n < 1) n = 1; |
| 235 | if (n > WS_MAX_COUNT) n = WS_MAX_COUNT; |
| 236 | c->count = n; |
| 237 | } |
| 238 | put_str(c->region, sizeof c->region, jstr(root, "region"), 1); |
| 239 | put_str(c->safe, sizeof c->safe, jstr(root, "safe"), 0); |
| 240 | put_str(c->language, sizeof c->language, jstr(root, "language"), 0); |
| 241 | put_str(c->searxng_categories, sizeof c->searxng_categories, |
| 242 | jstr(root, "searxng_categories"), 1); |
| 243 | c->tavily_advanced = jint(root, "tavily_advanced", c->tavily_advanced); |
| 244 | c->tavily_answer = jint(root, "tavily_answer", c->tavily_answer); |
| 245 | |
| 246 | put_str(c->brave_key, sizeof c->brave_key, jstr(root, "brave_key"), 0); |
| 247 | put_str(c->tavily_key, sizeof c->tavily_key, jstr(root, "tavily_key"), 0); |
| 248 | put_str(c->exa_key, sizeof c->exa_key, jstr(root, "exa_key"), 0); |
| 249 | put_str(c->pplx_key, sizeof c->pplx_key, jstr(root, "pplx_key"), 0); |
| 250 | |
| 251 | /* A base URL saved as "" means "use the compiled default", except for |
| 252 | * SearXNG where an empty base is a real, meaningful "not configured". */ |
| 253 | if (jstr(root, "ddg_base") && *jstr(root, "ddg_base")) |
| 254 | put_str(c->ddg_base, sizeof c->ddg_base, jstr(root, "ddg_base"), 1); |
| 255 | if (jstr(root, "brave_base") && *jstr(root, "brave_base")) |
| 256 | put_str(c->brave_base, sizeof c->brave_base, jstr(root, "brave_base"), 1); |
| 257 | if (jstr(root, "tavily_base") && *jstr(root, "tavily_base")) |
| 258 | put_str(c->tavily_base, sizeof c->tavily_base, jstr(root, "tavily_base"), 1); |
| 259 | if (jstr(root, "exa_base") && *jstr(root, "exa_base")) |
| 260 | put_str(c->exa_base, sizeof c->exa_base, jstr(root, "exa_base"), 1); |
| 261 | if (jstr(root, "pplx_base") && *jstr(root, "pplx_base")) |
| 262 | put_str(c->pplx_base, sizeof c->pplx_base, jstr(root, "pplx_base"), 1); |
| 263 | put_str(c->searxng_base, sizeof c->searxng_base, jstr(root, "searxng_base"), 1); |
| 264 | } |
| 265 | yyjson_doc_free(doc); |
| 266 | } |
| 267 | |
| 268 | static void env_str(const char *name, char *dst, size_t capsz, int keep_empty) |
| 269 | { |
| 270 | const char *v = getenv(name); |
| 271 | if (v) put_str(dst, capsz, v, keep_empty); |
| 272 | } |
| 273 | |
| 274 | void ws_config_load(ws_config *c) |
| 275 | { |
| 276 | char path[1200]; |
| 277 | const char *mock; |
| 278 | |
| 279 | set_defaults(c); |
| 280 | if (ws_config_path(path, sizeof path) == 0) load_file(c, path); |
| 281 | |
| 282 | /* environment overrides */ |
| 283 | env_str("WEBSEARCH_PROVIDER", c->active, sizeof c->active, 0); |
| 284 | { |
| 285 | const char *n = getenv("WEBSEARCH_COUNT"); |
| 286 | if (n && *n) { |
| 287 | int v = atoi(n); |
| 288 | if (v < 1) v = 1; |
| 289 | if (v > WS_MAX_COUNT) v = WS_MAX_COUNT; |
| 290 | c->count = v; |
| 291 | } |
| 292 | } |
| 293 | env_str("WEBSEARCH_REGION", c->region, sizeof c->region, 1); |
| 294 | env_str("WEBSEARCH_SAFE", c->safe, sizeof c->safe, 0); |
| 295 | env_str("WEBSEARCH_LANGUAGE", c->language, sizeof c->language, 0); |
| 296 | env_str("WEBSEARCH_SEARXNG_CATEGORIES", c->searxng_categories, |
| 297 | sizeof c->searxng_categories, 1); |
| 298 | { |
| 299 | const char *a = getenv("WEBSEARCH_TAVILY_ADVANCED"); |
| 300 | if (a && *a) c->tavily_advanced = atoi(a) ? 1 : 0; |
| 301 | a = getenv("WEBSEARCH_TAVILY_ANSWER"); |
| 302 | if (a && *a) c->tavily_answer = atoi(a) ? 1 : 0; |
| 303 | } |
| 304 | |
| 305 | env_str("BRAVE_API_KEY", c->brave_key, sizeof c->brave_key, 0); |
| 306 | env_str("TAVILY_API_KEY", c->tavily_key, sizeof c->tavily_key, 0); |
| 307 | env_str("EXA_API_KEY", c->exa_key, sizeof c->exa_key, 0); |
| 308 | env_str("PERPLEXITY_API_KEY", c->pplx_key, sizeof c->pplx_key, 0); |
| 309 | env_str("WEBSEARCH_BRAVE_KEY", c->brave_key, sizeof c->brave_key, 0); |
| 310 | env_str("WEBSEARCH_TAVILY_KEY", c->tavily_key, sizeof c->tavily_key, 0); |
| 311 | env_str("WEBSEARCH_EXA_KEY", c->exa_key, sizeof c->exa_key, 0); |
| 312 | env_str("WEBSEARCH_PPLX_KEY", c->pplx_key, sizeof c->pplx_key, 0); |
| 313 | |
| 314 | env_str("WEBSEARCH_DDG_BASE", c->ddg_base, sizeof c->ddg_base, 0); |
| 315 | env_str("WEBSEARCH_SEARXNG_BASE", c->searxng_base, sizeof c->searxng_base, 1); |
| 316 | env_str("SEARXNG_URL", c->searxng_base, sizeof c->searxng_base, 1); |
| 317 | env_str("WEBSEARCH_BRAVE_BASE", c->brave_base, sizeof c->brave_base, 0); |
| 318 | env_str("WEBSEARCH_TAVILY_BASE", c->tavily_base, sizeof c->tavily_base, 0); |
| 319 | env_str("WEBSEARCH_EXA_BASE", c->exa_base, sizeof c->exa_base, 0); |
| 320 | env_str("WEBSEARCH_PPLX_BASE", c->pplx_base, sizeof c->pplx_base, 0); |
| 321 | |
| 322 | /* One env var points every provider at a loopback mock, each on its own |
| 323 | * path, so the offline suite exercises the real request builders instead |
| 324 | * of a special "test" branch. Never a production default. */ |
| 325 | mock = getenv("WEBSEARCH_MOCK_BASE"); |
| 326 | if (mock && *mock) { |
| 327 | snprintf(c->ddg_base, sizeof c->ddg_base, "%s/duckduckgo", mock); |
| 328 | snprintf(c->brave_base, sizeof c->brave_base, "%s/brave", mock); |
| 329 | snprintf(c->tavily_base, sizeof c->tavily_base, "%s/tavily", mock); |
| 330 | snprintf(c->exa_base, sizeof c->exa_base, "%s/exa", mock); |
| 331 | snprintf(c->pplx_base, sizeof c->pplx_base, "%s/perplexity", mock); |
| 332 | snprintf(c->searxng_base, sizeof c->searxng_base, "%s/searxng", mock); |
| 333 | } |
| 334 | |
| 335 | if (!ws_provider_known(c->active)) |
| 336 | ws_copy(c->active, sizeof c->active, "duckduckgo"); |
| 337 | } |
| 338 | |
| 339 | int ws_config_save(const ws_config *c) |
| 340 | { |
| 341 | char path[1200], tmp[1300]; |
| 342 | yyjson_mut_doc *md; |
| 343 | yyjson_mut_val *root; |
| 344 | FILE *f; |
| 345 | char *js; |
| 346 | size_t len; |
| 347 | int ok; |
| 348 | |
| 349 | if (ws_config_path(path, sizeof path) != 0) return -1; |
| 350 | snprintf(tmp, sizeof tmp, "%s.tmp", path); |
| 351 | |
| 352 | md = yyjson_mut_doc_new(NULL); |
| 353 | if (!md) return -1; |
| 354 | root = yyjson_mut_obj(md); |
| 355 | yyjson_mut_doc_set_root(md, root); |
| 356 | |
| 357 | yyjson_mut_obj_add_strcpy(md, root, "provider", c->active); |
| 358 | yyjson_mut_obj_add_int(md, root, "count", c->count); |
| 359 | yyjson_mut_obj_add_strcpy(md, root, "region", c->region); |
| 360 | yyjson_mut_obj_add_strcpy(md, root, "safe", c->safe); |
| 361 | yyjson_mut_obj_add_strcpy(md, root, "language", c->language); |
| 362 | yyjson_mut_obj_add_strcpy(md, root, "searxng_categories", c->searxng_categories); |
| 363 | yyjson_mut_obj_add_int(md, root, "tavily_advanced", c->tavily_advanced ? 1 : 0); |
| 364 | yyjson_mut_obj_add_int(md, root, "tavily_answer", c->tavily_answer ? 1 : 0); |
| 365 | |
| 366 | yyjson_mut_obj_add_strcpy(md, root, "brave_key", c->brave_key); |
| 367 | yyjson_mut_obj_add_strcpy(md, root, "tavily_key", c->tavily_key); |
| 368 | yyjson_mut_obj_add_strcpy(md, root, "exa_key", c->exa_key); |
| 369 | yyjson_mut_obj_add_strcpy(md, root, "pplx_key", c->pplx_key); |
| 370 | |
| 371 | yyjson_mut_obj_add_strcpy(md, root, "ddg_base", c->ddg_base); |
| 372 | yyjson_mut_obj_add_strcpy(md, root, "searxng_base", c->searxng_base); |
| 373 | yyjson_mut_obj_add_strcpy(md, root, "brave_base", c->brave_base); |
| 374 | yyjson_mut_obj_add_strcpy(md, root, "tavily_base", c->tavily_base); |
| 375 | yyjson_mut_obj_add_strcpy(md, root, "exa_base", c->exa_base); |
| 376 | yyjson_mut_obj_add_strcpy(md, root, "pplx_base", c->pplx_base); |
| 377 | |
| 378 | js = yyjson_mut_write(md, YYJSON_WRITE_PRETTY, &len); |
| 379 | yyjson_mut_doc_free(md); |
| 380 | if (!js) return -1; |
| 381 | |
| 382 | f = fopen(tmp, "wb"); |
| 383 | if (!f) { free(js); return -1; } |
| 384 | #if !defined(_WIN32) |
| 385 | /* The keys are in this file: never let another user read it, and set the |
| 386 | * mode before the first byte is written rather than after. */ |
| 387 | { |
| 388 | int fd = fileno(f); |
| 389 | if (fd >= 0) { |
| 390 | if (fchmod(fd, 0600) != 0) { /* umask may already be tight */ } |
| 391 | } |
| 392 | } |
| 393 | #endif |
| 394 | ok = (fwrite(js, 1, len, f) == len); |
| 395 | if (fflush(f) != 0) ok = 0; |
| 396 | fclose(f); |
| 397 | free(js); |
| 398 | if (!ok) { remove(tmp); return -1; } |
| 399 | |
| 400 | remove(path); |
| 401 | if (rename(tmp, path) != 0) { remove(tmp); return -1; } |
| 402 | #if !defined(_WIN32) |
| 403 | chmod(path, 0600); |
| 404 | #endif |
| 405 | return 0; |
| 406 | } |