/* * ws_config.c - where plugin_websearch keeps its state, and how a * configuration value is resolved. * * Resolution order, lowest priority first: * * 1. compiled defaults (api.search.brave.com, ...) * 2. /config.json mode 0600, written by the setup page * 3. the environment (BRAVE_API_KEY, WEBSEARCH_* ...) * * The environment wins because that is the only layer an operator can set * without a browser, and because the offline suite needs to point every * provider at a loopback mock without touching a real config file. * * The connector directory deliberately lives OUTSIDE the plugin binary's own * directory, so a plugin update (which replaces the binary) can never take the * user's keys with it: * * $SWEBSEARCH_CONFIG_DIR (tests only) * /workspace/.scorpiox/connectors/plugin_websearch (BotCloud, survives a * container rebuild) * ~/.scorpiox/connectors/plugin_websearch (POSIX) * %LOCALAPPDATA%\scorpiox\connectors\plugin_websearch (Windows) * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #if defined(_WIN32) && !defined(_WIN32_WINNT) # define _WIN32_WINNT 0x0601 #endif #if !defined(_WIN32) # if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L # undef _POSIX_C_SOURCE # define _POSIX_C_SOURCE 200809L # endif # ifndef _DEFAULT_SOURCE # define _DEFAULT_SOURCE 1 # endif #endif #include "ws.h" #include #include #include #include #if defined(_WIN32) # define WIN32_LEAN_AND_MEAN # include # include # include #else # include # include # include #endif #define WS_DEFAULT_DDG_BASE "https://html.duckduckgo.com" #define WS_DEFAULT_BRAVE_BASE "https://api.search.brave.com" #define WS_DEFAULT_TAVILY_BASE "https://api.tavily.com" #define WS_DEFAULT_EXA_BASE "https://api.exa.ai" #define WS_DEFAULT_PPLX_BASE "https://api.perplexity.ai" static int ws_is_dir(const char *path) { #if defined(_WIN32) DWORD a = GetFileAttributesA(path); return (a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY)) ? 1 : 0; #else struct stat st; return (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) ? 1 : 0; #endif } /* mkdir -p. Returns 0 when the directory exists afterwards. */ static int ws_mkdir_p(const char *path) { char tmp[1024]; size_t len, i; ws_copy(tmp, sizeof tmp, path); len = strlen(tmp); if (len == 0) return -1; for (i = 1; i <= len; i++) { if (tmp[i] == '/' || tmp[i] == '\\' || tmp[i] == '\0') { char save = tmp[i]; tmp[i] = '\0'; #if defined(_WIN32) _mkdir(tmp); #else if (mkdir(tmp, 0700) != 0 && errno != EEXIST) { /* keep going: the final stat below is the real answer */ } #endif tmp[i] = save; if (save == '\0') break; } } return ws_is_dir(path) ? 0 : -1; } int ws_config_dir(char *out, size_t capsz) { const char *env = getenv("SWEBSEARCH_CONFIG_DIR"); char base[1024]; if (!out || capsz == 0) return -1; out[0] = '\0'; if (env && *env) { ws_copy(out, capsz, env); return ws_mkdir_p(out); } if (ws_is_dir("/workspace")) { snprintf(base, sizeof base, "/workspace/.scorpiox/connectors/%s", WS_ID); ws_copy(out, capsz, base); if (ws_mkdir_p(out) == 0) return 0; } #if defined(_WIN32) { const char *lad = getenv("LOCALAPPDATA"); if (lad && *lad) { snprintf(base, sizeof base, "%s\\scorpiox\\connectors\\%s", lad, WS_ID); } else { const char *up = getenv("USERPROFILE"); snprintf(base, sizeof base, "%s\\.scorpiox\\connectors\\%s", (up && *up) ? up : ".", WS_ID); } } #else { const char *home = getenv("HOME"); snprintf(base, sizeof base, "%s/.scorpiox/connectors/%s", (home && *home) ? home : ".", WS_ID); } #endif ws_copy(out, capsz, base); return ws_mkdir_p(out); } int ws_config_path(char *out, size_t capsz) { char dir[1024]; if (ws_config_dir(dir, sizeof dir) != 0) return -1; snprintf(out, capsz, "%s/config.json", dir); return 0; } static void set_defaults(ws_config *c) { memset(c, 0, sizeof *c); ws_copy(c->active, sizeof c->active, "duckduckgo"); c->count = WS_DEFAULT_COUNT; ws_copy(c->region, sizeof c->region, ""); ws_copy(c->safe, sizeof c->safe, "moderate"); ws_copy(c->language, sizeof c->language, ""); ws_copy(c->searxng_categories, sizeof c->searxng_categories, "general"); c->tavily_advanced = 0; c->tavily_answer = 0; ws_copy(c->ddg_base, sizeof c->ddg_base, WS_DEFAULT_DDG_BASE); ws_copy(c->brave_base, sizeof c->brave_base, WS_DEFAULT_BRAVE_BASE); ws_copy(c->tavily_base, sizeof c->tavily_base, WS_DEFAULT_TAVILY_BASE); ws_copy(c->exa_base, sizeof c->exa_base, WS_DEFAULT_EXA_BASE); ws_copy(c->pplx_base, sizeof c->pplx_base, WS_DEFAULT_PPLX_BASE); c->searxng_base[0] = '\0'; } /* A JSON string field, or NULL when absent/not a string. */ static const char *jstr(yyjson_val *o, const char *key) { yyjson_val *v; if (!o || !yyjson_is_obj(o)) return NULL; v = yyjson_obj_get(o, key); return (v && yyjson_is_str(v)) ? yyjson_get_str(v) : NULL; } /* A JSON integer or boolean-as-int field, or `deflt`. A page sends * {"tavily_answer": true} from a checkbox, so a bool has to count. */ static int jint(yyjson_val *o, const char *key, int deflt) { yyjson_val *v; if (!o || !yyjson_is_obj(o)) return deflt; v = yyjson_obj_get(o, key); if (!v) return deflt; if (yyjson_is_bool(v)) return yyjson_get_bool(v) ? 1 : 0; if (yyjson_is_int(v)) return (int)yyjson_get_int(v); if (yyjson_is_str(v)) return atoi(yyjson_get_str(v)); return deflt; } /* Empty string means "not configured" — writing "" from a cleared field must * switch a provider off rather than send an empty header. */ static void put_str(char *dst, size_t capsz, const char *val, int keep_empty) { if (!val) return; if (!*val && !keep_empty) { dst[0] = '\0'; return; } ws_copy(dst, capsz, val); } static void load_file(ws_config *c, const char *path) { FILE *f; long sz; char *buf; size_t got; yyjson_doc *doc; yyjson_val *root; f = fopen(path, "rb"); if (!f) return; if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return; } sz = ftell(f); if (sz <= 0 || sz > WS_BODY_MAX) { fclose(f); return; } rewind(f); buf = (char *)malloc((size_t)sz + 1); if (!buf) { fclose(f); return; } got = fread(buf, 1, (size_t)sz, f); fclose(f); buf[got] = '\0'; doc = yyjson_read(buf, got, 0); free(buf); if (!doc) return; root = yyjson_doc_get_root(doc); if (root && yyjson_is_obj(root)) { put_str(c->active, sizeof c->active, jstr(root, "provider"), 1); if (jstr(root, "provider") && !c->active[0]) ws_copy(c->active, sizeof c->active, "duckduckgo"); { int n = jint(root, "count", c->count); if (n < 1) n = 1; if (n > WS_MAX_COUNT) n = WS_MAX_COUNT; c->count = n; } put_str(c->region, sizeof c->region, jstr(root, "region"), 1); put_str(c->safe, sizeof c->safe, jstr(root, "safe"), 0); put_str(c->language, sizeof c->language, jstr(root, "language"), 0); put_str(c->searxng_categories, sizeof c->searxng_categories, jstr(root, "searxng_categories"), 1); c->tavily_advanced = jint(root, "tavily_advanced", c->tavily_advanced); c->tavily_answer = jint(root, "tavily_answer", c->tavily_answer); put_str(c->brave_key, sizeof c->brave_key, jstr(root, "brave_key"), 0); put_str(c->tavily_key, sizeof c->tavily_key, jstr(root, "tavily_key"), 0); put_str(c->exa_key, sizeof c->exa_key, jstr(root, "exa_key"), 0); put_str(c->pplx_key, sizeof c->pplx_key, jstr(root, "pplx_key"), 0); /* A base URL saved as "" means "use the compiled default", except for * SearXNG where an empty base is a real, meaningful "not configured". */ if (jstr(root, "ddg_base") && *jstr(root, "ddg_base")) put_str(c->ddg_base, sizeof c->ddg_base, jstr(root, "ddg_base"), 1); if (jstr(root, "brave_base") && *jstr(root, "brave_base")) put_str(c->brave_base, sizeof c->brave_base, jstr(root, "brave_base"), 1); if (jstr(root, "tavily_base") && *jstr(root, "tavily_base")) put_str(c->tavily_base, sizeof c->tavily_base, jstr(root, "tavily_base"), 1); if (jstr(root, "exa_base") && *jstr(root, "exa_base")) put_str(c->exa_base, sizeof c->exa_base, jstr(root, "exa_base"), 1); if (jstr(root, "pplx_base") && *jstr(root, "pplx_base")) put_str(c->pplx_base, sizeof c->pplx_base, jstr(root, "pplx_base"), 1); put_str(c->searxng_base, sizeof c->searxng_base, jstr(root, "searxng_base"), 1); } yyjson_doc_free(doc); } static void env_str(const char *name, char *dst, size_t capsz, int keep_empty) { const char *v = getenv(name); if (v) put_str(dst, capsz, v, keep_empty); } void ws_config_load(ws_config *c) { char path[1200]; const char *mock; set_defaults(c); if (ws_config_path(path, sizeof path) == 0) load_file(c, path); /* environment overrides */ env_str("WEBSEARCH_PROVIDER", c->active, sizeof c->active, 0); { const char *n = getenv("WEBSEARCH_COUNT"); if (n && *n) { int v = atoi(n); if (v < 1) v = 1; if (v > WS_MAX_COUNT) v = WS_MAX_COUNT; c->count = v; } } env_str("WEBSEARCH_REGION", c->region, sizeof c->region, 1); env_str("WEBSEARCH_SAFE", c->safe, sizeof c->safe, 0); env_str("WEBSEARCH_LANGUAGE", c->language, sizeof c->language, 0); env_str("WEBSEARCH_SEARXNG_CATEGORIES", c->searxng_categories, sizeof c->searxng_categories, 1); { const char *a = getenv("WEBSEARCH_TAVILY_ADVANCED"); if (a && *a) c->tavily_advanced = atoi(a) ? 1 : 0; a = getenv("WEBSEARCH_TAVILY_ANSWER"); if (a && *a) c->tavily_answer = atoi(a) ? 1 : 0; } env_str("BRAVE_API_KEY", c->brave_key, sizeof c->brave_key, 0); env_str("TAVILY_API_KEY", c->tavily_key, sizeof c->tavily_key, 0); env_str("EXA_API_KEY", c->exa_key, sizeof c->exa_key, 0); env_str("PERPLEXITY_API_KEY", c->pplx_key, sizeof c->pplx_key, 0); env_str("WEBSEARCH_BRAVE_KEY", c->brave_key, sizeof c->brave_key, 0); env_str("WEBSEARCH_TAVILY_KEY", c->tavily_key, sizeof c->tavily_key, 0); env_str("WEBSEARCH_EXA_KEY", c->exa_key, sizeof c->exa_key, 0); env_str("WEBSEARCH_PPLX_KEY", c->pplx_key, sizeof c->pplx_key, 0); env_str("WEBSEARCH_DDG_BASE", c->ddg_base, sizeof c->ddg_base, 0); env_str("WEBSEARCH_SEARXNG_BASE", c->searxng_base, sizeof c->searxng_base, 1); env_str("SEARXNG_URL", c->searxng_base, sizeof c->searxng_base, 1); env_str("WEBSEARCH_BRAVE_BASE", c->brave_base, sizeof c->brave_base, 0); env_str("WEBSEARCH_TAVILY_BASE", c->tavily_base, sizeof c->tavily_base, 0); env_str("WEBSEARCH_EXA_BASE", c->exa_base, sizeof c->exa_base, 0); env_str("WEBSEARCH_PPLX_BASE", c->pplx_base, sizeof c->pplx_base, 0); /* One env var points every provider at a loopback mock, each on its own * path, so the offline suite exercises the real request builders instead * of a special "test" branch. Never a production default. */ mock = getenv("WEBSEARCH_MOCK_BASE"); if (mock && *mock) { snprintf(c->ddg_base, sizeof c->ddg_base, "%s/duckduckgo", mock); snprintf(c->brave_base, sizeof c->brave_base, "%s/brave", mock); snprintf(c->tavily_base, sizeof c->tavily_base, "%s/tavily", mock); snprintf(c->exa_base, sizeof c->exa_base, "%s/exa", mock); snprintf(c->pplx_base, sizeof c->pplx_base, "%s/perplexity", mock); snprintf(c->searxng_base, sizeof c->searxng_base, "%s/searxng", mock); } if (!ws_provider_known(c->active)) ws_copy(c->active, sizeof c->active, "duckduckgo"); } int ws_config_save(const ws_config *c) { char path[1200], tmp[1300]; yyjson_mut_doc *md; yyjson_mut_val *root; FILE *f; char *js; size_t len; int ok; if (ws_config_path(path, sizeof path) != 0) return -1; snprintf(tmp, sizeof tmp, "%s.tmp", path); md = yyjson_mut_doc_new(NULL); if (!md) return -1; root = yyjson_mut_obj(md); yyjson_mut_doc_set_root(md, root); yyjson_mut_obj_add_strcpy(md, root, "provider", c->active); yyjson_mut_obj_add_int(md, root, "count", c->count); yyjson_mut_obj_add_strcpy(md, root, "region", c->region); yyjson_mut_obj_add_strcpy(md, root, "safe", c->safe); yyjson_mut_obj_add_strcpy(md, root, "language", c->language); yyjson_mut_obj_add_strcpy(md, root, "searxng_categories", c->searxng_categories); yyjson_mut_obj_add_int(md, root, "tavily_advanced", c->tavily_advanced ? 1 : 0); yyjson_mut_obj_add_int(md, root, "tavily_answer", c->tavily_answer ? 1 : 0); yyjson_mut_obj_add_strcpy(md, root, "brave_key", c->brave_key); yyjson_mut_obj_add_strcpy(md, root, "tavily_key", c->tavily_key); yyjson_mut_obj_add_strcpy(md, root, "exa_key", c->exa_key); yyjson_mut_obj_add_strcpy(md, root, "pplx_key", c->pplx_key); yyjson_mut_obj_add_strcpy(md, root, "ddg_base", c->ddg_base); yyjson_mut_obj_add_strcpy(md, root, "searxng_base", c->searxng_base); yyjson_mut_obj_add_strcpy(md, root, "brave_base", c->brave_base); yyjson_mut_obj_add_strcpy(md, root, "tavily_base", c->tavily_base); yyjson_mut_obj_add_strcpy(md, root, "exa_base", c->exa_base); yyjson_mut_obj_add_strcpy(md, root, "pplx_base", c->pplx_base); js = yyjson_mut_write(md, YYJSON_WRITE_PRETTY, &len); yyjson_mut_doc_free(md); if (!js) return -1; f = fopen(tmp, "wb"); if (!f) { free(js); return -1; } #if !defined(_WIN32) /* The keys are in this file: never let another user read it, and set the * mode before the first byte is written rather than after. */ { int fd = fileno(f); if (fd >= 0) { if (fchmod(fd, 0600) != 0) { /* umask may already be tight */ } } } #endif ok = (fwrite(js, 1, len, f) == len); if (fflush(f) != 0) ok = 0; fclose(f); free(js); if (!ok) { remove(tmp); return -1; } remove(path); if (rename(tmp, path) != 0) { remove(tmp); return -1; } #if !defined(_WIN32) chmod(path, 0600); #endif return 0; }