LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.
C cdebiandesktop-environmentebpffirewalllandlocklinuxlinux-distributionopen-sourceprivacyquickjsraspberry-pi-5reactsecuritywaylandwayland-compositor
Blocklists: curated catalog, subscriptions and lord-fetch, the sandboxed HTTPS downloader
Federico Pereirac77ea49bd265
diff --git a/assets/fw/blocklists.json b/assets/fw/blocklists.jsonnew file mode 100644index 0000000..af2cdaa--- /dev/null+++ b/assets/fw/blocklists.json@@ -0,0 +1,43 @@+{+ "note": "The curated blocklist catalog (docs/FIREWALL.md, \"Blocklists\"). Each list's licence was checked on the date given, from the list file's own header and its project's licence file, before it was added. LordOS ships no list data: each user's machine downloads the lists it enables. Lists over FWL_LIST_DOMAINS entries (fw/fwlists.h) are not offered.",+ "lists": [+ { "id": "ads", "topic": "ads", "kind": "domains", "format": "domains", "sub": 0,+ "url": "https://blocklistproject.github.io/Lists/alt-version/ads-nl.txt",+ "source": "The Block List Project", "homepage": "https://github.com/blocklistproject/Lists",+ "license": "MIT (file header); the project's repository: Unlicense", "checked": "2026-10-19", "approx": 234000 },+ { "id": "tracking", "topic": "trackers", "kind": "domains", "format": "domains", "sub": 0,+ "url": "https://blocklistproject.github.io/Lists/alt-version/tracking-nl.txt",+ "source": "The Block List Project", "homepage": "https://github.com/blocklistproject/Lists",+ "license": "MIT (file header); the project's repository: Unlicense", "checked": "2026-10-19", "approx": 144000 },+ { "id": "unified-hosts", "topic": "ads", "kind": "domains", "format": "hosts", "sub": 0,+ "url": "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts",+ "source": "Unified hosts (Steven Black)", "homepage": "https://github.com/StevenBlack/hosts",+ "license": "MIT", "checked": "2026-10-19", "approx": 71000 },+ { "id": "phishing", "topic": "malware", "kind": "domains", "format": "domains", "sub": 0,+ "url": "https://blocklistproject.github.io/Lists/alt-version/phishing-nl.txt",+ "source": "The Block List Project", "homepage": "https://github.com/blocklistproject/Lists",+ "license": "MIT (file header); the project's repository: Unlicense", "checked": "2026-10-19", "approx": 190000 },+ { "id": "scam", "topic": "malware", "kind": "domains", "format": "domains", "sub": 0,+ "url": "https://blocklistproject.github.io/Lists/alt-version/scam-nl.txt",+ "source": "The Block List Project", "homepage": "https://github.com/blocklistproject/Lists",+ "license": "MIT (file header); the project's repository: Unlicense", "checked": "2026-10-19", "approx": 8500 },+ { "id": "ransomware", "topic": "malware", "kind": "domains", "format": "domains", "sub": 0,+ "url": "https://blocklistproject.github.io/Lists/alt-version/ransomware-nl.txt",+ "source": "The Block List Project", "homepage": "https://github.com/blocklistproject/Lists",+ "license": "MIT (file header); the project's repository: Unlicense", "checked": "2026-10-19", "approx": 1900 },+ { "id": "cryptomining", "topic": "malware", "kind": "domains", "format": "domains", "sub": 0,+ "url": "https://blocklistproject.github.io/Lists/alt-version/crypto-nl.txt",+ "source": "The Block List Project", "homepage": "https://github.com/blocklistproject/Lists",+ "license": "MIT (file header); the project's repository: Unlicense", "checked": "2026-10-19", "approx": 1300 },+ { "id": "attackers", "topic": "malware", "kind": "ips", "format": "ips", "sub": 0,+ "url": "https://raw.githubusercontent.com/stamparm/ipsum/master/levels/3.txt",+ "source": "IPsum, level 3 (addresses on 3 or more public lists)", "homepage": "https://github.com/stamparm/ipsum",+ "license": "Unlicense", "checked": "2026-10-19", "approx": 18000 }+ ],+ "rejected": [+ { "list": "a malware URL feed with fair-use terms only (no stated licence)", "why": "licence not clear" },+ { "list": "a botnet controller address list (CC0)", "why": "empty when checked (2026-10-19)" },+ { "list": "The Block List Project's malware list", "why": "2.6 million entries, over the size limit" },+ { "list": "lists under non-commercial licences", "why": "LordOS may be used commercially" }+ ]+}diff --git a/server/Makefile b/server/Makefileindex e24756d..3e369ec 100644--- a/server/Makefile+++ b/server/Makefile@@ -69,7 +69,7 @@ DOC_SRC := src/doc/doc.c src/doc/wire.c src/ui/js_doc.c src/print/ipp.c src/prinsrc/print/escl.c src/print/print.c src/ui/js_print.cSRC += $(DOC_SRC)# Per-app firewall (src/fw, docs/FIREWALL.md): rules, decision and names learned from DNS-FW_SRC := src/fw/rules.c src/fw/dnsmap.c src/fw/fwmap.c src/fw/fwlists.c src/ui/js_fw.c src/sec/netconn.c+FW_SRC := src/fw/rules.c src/fw/dnsmap.c src/fw/fwmap.c src/fw/fwlists.c src/fw/fwsub.c src/ui/js_fw.c src/sec/netconn.cSRC += $(FW_SRC)OBJ := $(SRC:src/%.c=build/obj/%.o)HDR := $(wildcard src/*.h src/gpu/*.h src/ui/*.h src/term/*.h src/wl/*.h src/wasm/*.h src/sound/*.h src/pkg/*.h src/net/*.h src/install/*.h src/remote/*.h src/users/*.h src/sec/*.h src/monitor/*.h src/media/*.h src/app/*.h src/doc/*.h src/print/*.h) $(WL_GEN) $(APP_CLIENT_HDR)diff --git a/server/mk/fetch.mk b/server/mk/fetch.mknew file mode 100644index 0000000..fd6afd3--- /dev/null+++ b/server/mk/fetch.mk@@ -0,0 +1,22 @@+# lord-fetch (0.8.4, docs/FIREWALL.md "Blocklists"): the sandboxed helper that downloads the+# subscribed blocklists over HTTPS and parses them. With lord-sessiond and lord-maild, the only+# binaries that link OpenSSL; built when its headers are there (MAILNET, mk/mail.mk).+FETCH_SRC := src/fetch/fetch.c src/fw/fwlists.c src/fw/rules.c src/fw/fwmap.c src/mail/http.c src/mail/net.c \+ src/doc/wire.c src/sec/json.c src/sec/sandbox.c src/sec/cgroup.c+MAILNET ?= $(shell pkg-config --exists openssl 2>/dev/null && echo 1 || echo 0)+ifeq ($(MAILNET),1)+all: build/lord-fetch+endif+build/lord-fetch: $(FETCH_SRC) src/fetch/fetchproto.h src/fw/fwlists.h src/mail/http.h src/mail/net.h src/sec/sandbox.h+ @mkdir -p build+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ $(FETCH_SRC) $(shell pkg-config --libs openssl) -lpthread -lm+# subscriptions (fw/fwsub.c) without network, and lord-fetch's refusals (tests/fwsub_test.c)+FWSUB_SRC := src/fw/fwsub.c src/fw/fwlists.c src/fw/rules.c src/fw/fwmap.c src/doc/wire.c src/sec/json.c src/sec/netconn.c src/sec/cgroup.c+ifeq ($(MAILNET),1)+test: build/fwsub-test+build/fwsub-test: build/lord-fetch+endif+build/fwsub-test: tests/fwsub_test.c $(FWSUB_SRC) src/fw/fwsub.h src/fetch/fetchproto.h+ @mkdir -p build+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/fwsub_test.c $(FWSUB_SRC) -lpthread+ ./$@ build/lord-fetch || { rm -f $@; exit 1; }diff --git a/server/src/fetch/fetch.c b/server/src/fetch/fetch.cnew file mode 100644index 0000000..8ccd00d--- /dev/null+++ b/server/src/fetch/fetch.c@@ -0,0 +1,151 @@+/* fetch.c — lord-fetch: downloads the subscribed blocklists over HTTPS for lord-server, sandboxed+ * (see fetchproto.h). With lord-sessiond and lord-maild, the only binaries that link OpenSSL. */+#include "fetchproto.h"+#include "../doc/wire.h"+#include "../fw/fwlists.h"+#include "../mail/http.h"+#include "../mail/net.h"+#include "../sec/json.h"+#include "../sec/sandbox.h"++#include <signal.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/prctl.h>+#include <sys/resource.h>+#include <sys/stat.h>+#include <unistd.h>++#define SOCK 3++static void reply(uint32_t op, uint32_t id, JBuf *b, int fd) {+ doc_send(SOCK, op, id, b->p ? b->p : "{}", b->p ? b->n : 2, fd >= 0 ? &fd : NULL, fd >= 0 ? 1 : 0);+}++static void fail(uint32_t id, const char *code, const char *detail) {+ JBuf b;+ jb_init(&b);+ jb_raw(&b, "{\"error\":");+ jb_str(&b, code);+ jb_raw(&b, ",\"detail\":");+ jb_str(&b, detail ? detail : "");+ jb_raw(&b, "}");+ reply(FR_ERR, id, &b, -1);+ jb_free(&b);+}++/* host: a DNS name or an address, printable ASCII without spaces; path: starts with /, printable */+static int good_host(const char *h) {+ size_t n = h ? strlen(h) : 0;+ if (!n || n > 253) return 0;+ for (const char *p = h; *p; p++) if (!((*p >= 'a' && *p <= 'z') || (*p >= '0' && *p <= '9') || *p == '.' || *p == '-' || *p == ':')) return 0;+ return 1;+}++static int good_path(const char *p) {+ size_t n = p ? strlen(p) : 0;+ if (!n || n > 1000 || p[0] != '/') return 0;+ for (; *p; p++) if ((unsigned char)*p <= 0x20 || (unsigned char)*p >= 0x7f) return 0;+ return 1;+}++static void get(uint32_t id, const char *json, size_t len, int fd) {+ JDoc d;+ if (json_parse(json, len, &d, NULL, 0)) { fail(id, "bad-request", ""); return; }+ const char *host = json_str(d.root, "host"), *path = json_str(d.root, "path"), *fmt = json_str(d.root, "format");+ int sub = json_num(d.root, "sub", 0) == 1;+ FwlFormat f = fwl_format_from_name(fmt);+ if (!good_host(host) || !good_path(path) || !f) { json_free(&d); fail(id, "bad-request", ""); return; }+ Conn *c = conn_new(fd, host, 30000);+ if (!c) { close(fd); json_free(&d); fail(id, "oom", ""); return; }+ TlsInfo info;+ memset(&info, 0, sizeof info);+ if (conn_tls(c, NULL, &info)) {+ fail(id, info.code ? "cert" : "tls", info.code ? info.reason : conn_error(c));+ conn_close(c);+ json_free(&d);+ return;+ }+ HttpResp r;+ memset(&r, 0, sizeof r);+ int rc = http_request(c, "GET", host, path, "Accept-Encoding: identity\r\nUser-Agent: LordOS-blocklists/1\r\n", NULL, 0,+ FWL_RAW_MAX, 180000, &r);+ conn_close(c);+ json_free(&d);+ if (rc == -2) { fail(id, "big", ""); return; }+ if (rc) { fail(id, "http", ""); http_resp_free(&r); return; }+ if (r.status == 301 || r.status == 302 || r.status == 303 || r.status == 307 || r.status == 308) {+ JBuf b;+ jb_init(&b);+ jb_raw(&b, "{\"location\":");+ jb_str(&b, r.location);+ jb_raw(&b, "}");+ reply(FR_REDIRECT, id, &b, -1);+ jb_free(&b);+ http_resp_free(&r);+ return;+ }+ if (r.status != 200) {+ char s[16];+ snprintf(s, sizeof s, "%d", r.status);+ fail(id, "status", s);+ http_resp_free(&r);+ return;+ }+ char *out = NULL, err[32] = "";+ size_t olen = 0;+ FwlStats st;+ if (fwl_parse_raw(r.body ? r.body : "", r.blen, f, sub, &out, &olen, &st, err, sizeof err)) {+ fail(id, err, "");+ http_resp_free(&r);+ return;+ }+ size_t bytes = r.blen;+ http_resp_free(&r);+ int mfd = doc_memfd("lord-fetch-list", out, olen);+ free(out);+ if (mfd < 0) { fail(id, "oom", ""); return; }+ JBuf b;+ jb_init(&b);+ jb_printf(&b, "{\"domains\":%zu,\"nets\":%zu,\"skipped\":%zu,\"bad\":%zu,\"dups\":%zu,\"truncated\":%d,\"bytes\":%zu,\"lines\":%zu}",+ st.domains, st.nets, st.skipped, st.bad, st.dups, st.truncated, bytes, st.lines);+ reply(FR_DONE, id, &b, mfd);+ jb_free(&b);+ close(mfd);+}++int main(void) {+ prctl(PR_SET_PDEATHSIG, SIGKILL);+ prctl(PR_SET_DUMPABLE, 0);+ struct rlimit nocore = { 0, 0 };+ setrlimit(RLIMIT_CORE, &nocore);+ signal(SIGPIPE, SIG_IGN);+ struct stat st;+ if (fstat(SOCK, &st) || !S_ISSOCK(st.st_mode)) { fprintf(stderr, "lord-fetch: started by lord-server only\n"); return 2; }+ if (net_init()) { fprintf(stderr, "lord-fetch: TLS init failed\n"); return 3; }+ SandboxHelperCfg cfg = {+ .name = "lordos-fetch", /* cgroup scope lordos-fetch.scope (lord-sessiond creates it) */+ .ro_paths = NULL,+ .extra_syscalls = (int[]){ -1 },+ .allow_ioctl_classes = 0,+ .mem_max_mb = 256,+ .ro_dir_fds = NULL,+ .rw_dir_fds = NULL,+ };+ setpriority(PRIO_PROCESS, 0, 10);+ sandbox_apply_helper(&cfg);+ for (;;) {+ DocMsg m;+ int rc = doc_recv(SOCK, &m);+ if (rc) return rc == 1 ? 0 : 1;+ if (m.h.op == FQ_END) { doc_msg_free(&m); return 0; }+ if (m.h.op == FQ_GET && m.nfds == 1 && m.json) {+ int fd = m.fds[0];+ m.fds[0] = -1;+ m.nfds = 0;+ get(m.h.id, m.json, m.len, fd); /* conn_close closes fd */+ } else fail(m.h.id, "bad-request", "");+ doc_msg_free(&m);+ }+}diff --git a/server/src/fetch/fetchproto.h b/server/src/fetch/fetchproto.hnew file mode 100644index 0000000..fefa0bb--- /dev/null+++ b/server/src/fetch/fetchproto.h@@ -0,0 +1,26 @@+/* fetchproto.h — lord-server ↔ lord-fetch, the blocklist downloader (0.8.4, docs/FIREWALL.md,+ * "Blocklists"). The channel and the framing are lord-doc's (doc/wire.h: header + JSON + fds over a+ * SOCK_SEQPACKET pair on fd 3).+ *+ * FQ_GET {"host":"example.org","path":"/list.txt","format":"hosts|domains|ips","sub":0|1}+ * + 1 fd: a TCP socket lord-server already connected (the blocklists' connector, in+ * lordos-net-lists.scope) to host:443+ * FQ_END {}: the helper exits+ *+ * FR_DONE {"domains":N,"nets":N,"skipped":N,"bad":N,"dups":N,"truncated":0|1,"bytes":N,+ * "lastModified":"…"} + 1 sealed memfd with the normalized lines (fw/fwlists.h)+ * FR_REDIRECT {"location":"https://…"} (lord-server checks it and connects again; at most 3)+ * FR_ERR {"error":"tls|cert|http|status|big|nul|line|format|empty|oom|bad-request","detail":"…"}+ *+ * lord-fetch never opens a socket or a file: it loads the CA bundle, locks itself in with+ * sandbox_apply_helper (no socket/connect/exec, no files), speaks TLS 1.2+ (certificate checked+ * against the system CA store and the host name, never relaxed) and HTTP/1.1 GET with a size+ * limit, and parses what came strictly (fwl_parse_raw). lord-server validates every line again+ * before it compiles the lists. */+#ifndef LORD_FETCH_PROTO_H+#define LORD_FETCH_PROTO_H++enum { FQ_GET = 1, FQ_END = 2 };+enum { FR_DONE = 101, FR_REDIRECT = 102, FR_ERR = 103 };++#endifdiff --git a/server/src/fsops.c b/server/src/fsops.cindex e641128..730ebc7 100644--- a/server/src/fsops.c+++ b/server/src/fsops.c@@ -65,6 +65,9 @@ int fsops_protected(const char *real) {"/.local/share/lordos/apps", "/.config/lordos/settings.json","/.config/lordos/permisos.json","/.config/lordos/firewall.json", /* network permissions: an app does not write its own rules */+ "/.config/lordos/fwlists.json", /* (0.8.4) subscribed blocklists: only lord.fw */+ "/.config/lordos/fwlists.txt", /* their compiled entries, read by lord-netperm */+ "/.cache/lordos/fwlists", /* their downloads: only lord-server writes them */"/.config/lordos/backup.json", /* Backups: decides where the data goes */"/.config/lordos/mail", /* Mail accounts and trusted certificates: only lord.mail.saveConfig (docs/MAIL.md) */"/.local/share/lordos/mail", /* Mail cache: only lord-maild writes it */diff --git a/server/src/fw/fwsub.c b/server/src/fw/fwsub.cnew file mode 100644index 0000000..b191bd0--- /dev/null+++ b/server/src/fw/fwsub.c@@ -0,0 +1,617 @@+/* fwsub.c — the user's blocklist subscriptions, lord-server's side (see fwsub.h). */+#include "fwsub.h"+#include "fwlists.h"+#include "../doc/wire.h"+#include "../fetch/fetchproto.h"+#include "../sec/json.h"+#include "../sec/netconn.h"++#include <ctype.h>+#include <errno.h>+#include <fcntl.h>+#include <limits.h>+#include <netdb.h>+#include <poll.h>+#include <pthread.h>+#include <signal.h>+#include <spawn.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/mman.h>+#include <sys/socket.h>+#include <sys/stat.h>+#include <sys/wait.h>+#include <time.h>+#include <unistd.h>++typedef struct {+ char id[32], url[FWS_URL_MAX + 1], format[8];+ int sub, custom, on;+ long long updated, tried; /* Unix seconds of the last good download / the last attempt */+ long domains, nets;+ int truncated;+ char error[64]; /* the last attempt's error code ("" if it went well) */+} List;++static List L[FWS_MAX_LISTS];+static int nl;+static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;+static int busy, want_fetch, want_compile, job_done;+static FwLists *cur, *next_set;+static char cur_ids[FWS_MAX_LISTS][32], next_ids[FWS_MAX_LISTS][32];+static char cfg_path[PATH_MAX], txt_path[PATH_MAX], cache_dir[PATH_MAX - 64], fetch_bin[PATH_MAX], catalog_path[PATH_MAX];+static int inited;++static long long now_s(void) { return (long long)time(NULL); }++/* ---- files ---- */++static char *read_file(const char *p, size_t max, size_t *len) {+ int fd = open(p, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);+ if (fd < 0) return NULL;+ struct stat st;+ if (fstat(fd, &st) || !S_ISREG(st.st_mode) || st.st_size > (off_t)max) { close(fd); return NULL; }+ char *b = malloc((size_t)st.st_size + 1);+ size_t got = 0;+ while (b && got < (size_t)st.st_size) {+ ssize_t n = read(fd, b + got, (size_t)st.st_size - got);+ if (n <= 0) break;+ got += (size_t)n;+ }+ close(fd);+ if (!b || got != (size_t)st.st_size) { free(b); return NULL; }+ b[got] = 0;+ *len = got;+ return b;+}++static int write_file(const char *p, const char *data, size_t len) {+ char tmp[PATH_MAX + 8];+ snprintf(tmp, sizeof tmp, "%s.tmp", p);+ int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 0600);+ if (fd < 0) return -1;+ size_t off = 0;+ while (off < len) {+ ssize_t n = write(fd, data + off, len - off);+ if (n <= 0) { close(fd); unlink(tmp); return -1; }+ off += (size_t)n;+ }+ if (fsync(fd) || close(fd)) { unlink(tmp); return -1; }+ return rename(tmp, p);+}++static int good_id(const char *s) {+ size_t n = s ? strlen(s) : 0;+ if (!n || n > 31) return 0;+ for (; *s; s++) if (!((*s >= 'a' && *s <= 'z') || (*s >= '0' && *s <= '9') || *s == '-')) return 0;+ return 1;+}++int fws_split_url(const char *url, char *host, size_t hcap, int *port, char *path, size_t pcap) {+ if (!url || strncmp(url, "https://", 8) || strlen(url) > FWS_URL_MAX) return -1;+ const char *h = url + 8, *e = h;+ while (*e && *e != '/' && *e != ':' && *e != '?' && *e != '#') e++;+ size_t hl = (size_t)(e - h);+ if (!hl || hl >= hcap || hl > 253) return -1;+ for (size_t i = 0; i < hl; i++) {+ char c = (char)tolower((unsigned char)h[i]);+ if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '.' || c == '-')) return -1;+ host[i] = c;+ }+ host[hl] = 0;+ if (host[0] == '.' || host[0] == '-' || host[hl - 1] == '.' || strstr(host, "..") || !strchr(host, '.')) return -1;+ *port = 443;+ if (*e == ':') {+ char *end;+ long p = strtol(e + 1, &end, 10);+ if (end == e + 1 || p < 1 || p > 65535 || (*end && *end != '/')) return -1;+ *port = (int)p;+ e = end;+ }+ const char *pa = *e == '/' ? e : "/";+ if (*e && *e != '/') return -1; /* ?query or #fragment right after the host */+ size_t pl = strlen(pa);+ if (pl >= pcap) return -1;+ for (size_t i = 0; i < pl; i++) if ((unsigned char)pa[i] <= 0x20 || (unsigned char)pa[i] >= 0x7f || pa[i] == '#') return -1;+ memcpy(path, pa, pl + 1);+ return 0;+}++/* ---- the catalog, the config and the cache ---- */++static List *find(const char *id) {+ for (int i = 0; i < nl; i++) if (!strcmp(L[i].id, id)) return &L[i];+ return NULL;+}++static int add_list(const char *id, const char *url, const char *fmt, int sub, int custom) {+ char h[256], p[FWS_URL_MAX + 1];+ int port;+ if (nl >= FWS_MAX_LISTS || !good_id(id) || find(id) || fws_split_url(url, h, sizeof h, &port, p, sizeof p) || !fwl_format_from_name(fmt)) return -1;+ List *l = &L[nl++];+ memset(l, 0, sizeof *l);+ snprintf(l->id, sizeof l->id, "%s", id);+ snprintf(l->url, sizeof l->url, "%s", url);+ snprintf(l->format, sizeof l->format, "%s", fmt);+ l->sub = sub;+ l->custom = custom;+ return 0;+}++static void load_catalog(void) {+ size_t len;+ char *t = read_file(catalog_path, 1 << 20, &len);+ JDoc d;+ if (!t || json_parse(t, len, &d, NULL, 0)) { free(t); fprintf(stderr, "lord.fw: no blocklist catalog (%s)\n", catalog_path); return; }+ const JVal *lists = json_get(d.root, "lists");+ size_t i = 0;+ for (const JVal *o = lists && lists->type == J_ARR ? lists->kid : NULL; o; o = o->next, i++) {+ if (add_list(json_str(o, "id") ? json_str(o, "id") : "", json_str(o, "url") ? json_str(o, "url") : "",+ json_str(o, "format") ? json_str(o, "format") : "", json_num(o, "sub", 0) == 1, 0))+ fprintf(stderr, "lord.fw: blocklist catalog entry %zu is invalid\n", i);+ }+ json_free(&d);+ free(t);+}++static void load_config(void) {+ size_t len;+ char *t = read_file(cfg_path, 1 << 20, &len);+ JDoc d;+ if (!t || json_parse(t, len, &d, NULL, 0)) { free(t); return; }+ const JVal *cu = json_get(d.root, "custom");+ size_t i = 0;+ for (const JVal *o = cu && cu->type == J_ARR ? cu->kid : NULL; o && i < FWS_MAX_CUSTOM; o = o->next, i++) {+ const char *id = json_str(o, "id"), *url = json_str(o, "url"), *fmt = json_str(o, "format");+ if (id && url && fmt && !strncmp(id, "custom-", 7)) add_list(id, url, fmt, 0, 1);+ }+ const JVal *en = json_get(d.root, "enabled");+ for (const JVal *v = en && en->type == J_ARR ? en->kid : NULL; v; v = v->next) {+ if (v->type != J_STR) continue;+ List *l = find(v->str);+ if (l) l->on = 1;+ }+ json_free(&d);+ free(t);+}++static void save_config(void) {+ JBuf b;+ jb_init(&b);+ jb_raw(&b, "{\"v\":1,\"enabled\":[");+ int k = 0;+ for (int i = 0; i < nl; i++) if (L[i].on) { if (k++) jb_raw(&b, ","); jb_str(&b, L[i].id); }+ jb_raw(&b, "],\"custom\":[");+ k = 0;+ for (int i = 0; i < nl; i++) {+ if (!L[i].custom) continue;+ if (k++) jb_raw(&b, ",");+ jb_raw(&b, "{\"id\":");+ jb_str(&b, L[i].id);+ jb_raw(&b, ",\"url\":");+ jb_str(&b, L[i].url);+ jb_raw(&b, ",\"format\":");+ jb_str(&b, L[i].format);+ jb_raw(&b, "}");+ }+ jb_raw(&b, "]}\n");+ if (!b.oom && write_file(cfg_path, b.p, b.n)) fprintf(stderr, "lord.fw: could not save %s\n", cfg_path);+ jb_free(&b);+}++static void cache_path(const char *id, const char *ext, char *out, size_t n) {+ if ((size_t)snprintf(out, n, "%s/%.31s.%.4s", cache_dir, id, ext) >= n) out[0] = 0;+}++static void load_state(List *l) {+ char p[PATH_MAX];+ cache_path(l->id, "json", p, sizeof p);+ size_t len;+ char *t = read_file(p, 4096, &len);+ JDoc d;+ if (!t || json_parse(t, len, &d, NULL, 0)) { free(t); return; }+ l->updated = (long long)json_num(d.root, "updated", 0);+ l->tried = (long long)json_num(d.root, "tried", 0);+ l->domains = (long)json_num(d.root, "domains", 0);+ l->nets = (long)json_num(d.root, "nets", 0);+ l->truncated = json_num(d.root, "truncated", 0) == 1;+ const char *e = json_str(d.root, "error");+ snprintf(l->error, sizeof l->error, "%s", e ? e : "");+ json_free(&d);+ free(t);+}++static void save_state(const List *l) {+ char p[PATH_MAX], j[400], e[140];+ cache_path(l->id, "json", p, sizeof p);+ json_quote(e, sizeof e, l->error);+ int n = snprintf(j, sizeof j, "{\"updated\":%lld,\"tried\":%lld,\"domains\":%ld,\"nets\":%ld,\"truncated\":%d,\"error\":%s}\n",+ l->updated, l->tried, l->domains, l->nets, l->truncated, e);+ write_file(p, j, (size_t)n);+}++static int has_copy(const List *l) {+ char p[PATH_MAX];+ cache_path(l->id, "txt", p, sizeof p);+ struct stat st;+ return !stat(p, &st) && S_ISREG(st.st_mode);+}++/* ---- the worker: downloading (lord-fetch) and compiling ---- */++typedef struct { int fd; pid_t pid; } Helper;++static int helper_start(Helper *h) {+ struct stat st, me;+ if (stat(fetch_bin, &st) || !S_ISREG(st.st_mode) || stat("/proc/self/exe", &me) ||+ (st.st_uid != 0 && st.st_uid != me.st_uid) || (st.st_mode & 022)) return -1; /* root's or ours, not writable by others */+ int sv[2];+ if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv)) return -1;+ posix_spawn_file_actions_t fa;+ posix_spawn_file_actions_init(&fa);+ posix_spawn_file_actions_adddup2(&fa, sv[1], 3);+ posix_spawnattr_t at;+ posix_spawnattr_init(&at);+ sigset_t none, all;+ sigemptyset(&none);+ sigfillset(&all);+ posix_spawnattr_setsigmask(&at, &none);+ posix_spawnattr_setsigdefault(&at, &all);+ posix_spawnattr_setflags(&at, POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF);+ char *argv[] = { "lord-fetch", NULL };+ char *envp[] = { "LANG=C.UTF-8", getenv("LORDOS_SESSIOND") ? "LORDOS_SESSIOND=1" : NULL, NULL };+ int e = posix_spawn(&h->pid, fetch_bin, &fa, &at, argv, envp);+ posix_spawn_file_actions_destroy(&fa);+ posix_spawnattr_destroy(&at);+ close(sv[1]);+ if (e) { close(sv[0]); return -1; }+ h->fd = sv[0];+ return 0;+}++static void helper_stop(Helper *h) {+ if (h->fd < 0) return;+ doc_send(h->fd, FQ_END, 0, "{}", 2, NULL, 0);+ close(h->fd);+ h->fd = -1;+ for (int i = 0; i < 50; i++) {+ if (waitpid(h->pid, NULL, WNOHANG) == h->pid) return;+ struct timespec ts = { 0, 20 * 1000 * 1000 };+ nanosleep(&ts, NULL);+ }+ kill(h->pid, SIGKILL);+ waitpid(h->pid, NULL, 0);+}++static int connect_to(const char *host, int port) {+ char ps[8];+ snprintf(ps, sizeof ps, "%d", port);+ struct addrinfo hints = { 0 }, *res = NULL;+ hints.ai_family = AF_UNSPEC;+ hints.ai_socktype = SOCK_STREAM;+ hints.ai_flags = AI_ADDRCONFIG;+ if (getaddrinfo(host, ps, &hints, &res)) return -2;+ int fd = -1;+ for (struct addrinfo *a = res; a && fd < 0; a = a->ai_next) {+ fd = netconn_socket(NC_LISTS, a->ai_family, SOCK_STREAM, a->ai_addr, a->ai_addrlen);+ if (fd < 0) continue;+ struct pollfd p = { fd, POLLOUT, 0 };+ int err = 0;+ socklen_t el = sizeof err;+ if (poll(&p, 1, 15000) != 1 || getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &el) || err) { close(fd); fd = -1; }+ }+ freeaddrinfo(res);+ if (fd >= 0) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);+ return fd;+}++/* Downloads one list into its cache; l's state says how it went. */+static void fetch_one(Helper *h, List *l) {+ l->tried = now_s();+ char url[FWS_URL_MAX + 1];+ snprintf(url, sizeof url, "%s", l->url);+ for (int hop = 0; hop < 4; hop++) {+ char host[256], path[FWS_URL_MAX + 1];+ int port;+ if (fws_split_url(url, host, sizeof host, &port, path, sizeof path)) { snprintf(l->error, sizeof l->error, "badUrl"); return; }+ if (h->fd < 0 && helper_start(h)) { snprintf(l->error, sizeof l->error, "helper"); return; }+ int fd = connect_to(host, port);+ if (fd < 0) { snprintf(l->error, sizeof l->error, fd == -2 ? "dns" : "connect"); return; }+ JBuf b;+ jb_init(&b);+ jb_raw(&b, "{\"host\":");+ jb_str(&b, host);+ jb_raw(&b, ",\"path\":");+ jb_str(&b, path);+ jb_raw(&b, ",\"format\":");+ jb_str(&b, l->format);+ jb_printf(&b, ",\"sub\":%d}", l->sub);+ int sent = !b.oom && doc_send(h->fd, FQ_GET, 1, b.p, b.n, &fd, 1) == 0;+ jb_free(&b);+ close(fd);+ struct pollfd p = { h->fd, POLLIN, 0 };+ DocMsg m;+ if (!sent || poll(&p, 1, 240000) != 1 || doc_recv(h->fd, &m)) { snprintf(l->error, sizeof l->error, "helper"); helper_stop(h); return; }+ JDoc d;+ int have = m.json && !json_parse(m.json, m.len, &d, NULL, 0);+ if (m.h.op == FR_REDIRECT && have) {+ const char *loc = json_str(d.root, "location");+ if (!loc || strncmp(loc, "https://", 8) || strlen(loc) > FWS_URL_MAX) {+ snprintf(l->error, sizeof l->error, "redirect");+ json_free(&d);+ doc_msg_free(&m);+ return;+ }+ snprintf(url, sizeof url, "%s", loc); /* only https, checked again at the top */+ json_free(&d);+ doc_msg_free(&m);+ continue;+ }+ if (m.h.op == FR_ERR && have) {+ const char *e = json_str(d.root, "error"), *det = json_str(d.root, "detail");+ if (e && !strcmp(e, "status") && det) snprintf(l->error, sizeof l->error, "status %.6s", det);+ else snprintf(l->error, sizeof l->error, "%.40s", e ? e : "helper");+ json_free(&d);+ doc_msg_free(&m);+ return;+ }+ if (m.h.op != FR_DONE || !have || m.nfds != 1) {+ snprintf(l->error, sizeof l->error, "helper");+ if (have) json_free(&d);+ doc_msg_free(&m);+ return;+ }+ /* the normalized text: a sealed memfd, read and validated line by line again */+ long sz = doc_check_sealed(m.fds[0], 0);+ char *txt = sz >= 0 && sz <= (long)FWL_FILE_MAX ? malloc((size_t)sz + 1) : NULL;+ if (txt && sz > 0) {+ void *mp = mmap(NULL, (size_t)sz, PROT_READ, MAP_PRIVATE, m.fds[0], 0);+ if (mp == MAP_FAILED) { free(txt); txt = NULL; }+ else { memcpy(txt, mp, (size_t)sz); munmap(mp, (size_t)sz); }+ }+ FwLists *chk = txt ? fwl_new() : NULL;+ long added = chk ? fwl_add_text(chk, txt, (size_t)sz, 1) : -1;+ char p2[PATH_MAX];+ cache_path(l->id, "txt", p2, sizeof p2);+ if (added <= 0 || write_file(p2, txt, (size_t)sz)) snprintf(l->error, sizeof l->error, added <= 0 ? "invalid" : "write");+ else {+ l->error[0] = 0;+ l->updated = now_s();+ l->domains = (long)fwl_domains(chk);+ l->nets = (long)fwl_nets(chk);+ l->truncated = json_num(d.root, "truncated", 0) == 1;+ }+ fwl_free(chk);+ free(txt);+ json_free(&d);+ doc_msg_free(&m);+ return;+ }+ snprintf(l->error, sizeof l->error, "redirect");+}++static void *worker(void *arg) {+ (void)arg;+ for (;;) {+ pthread_mutex_lock(&mu);+ int fetch = want_fetch;+ want_fetch = want_compile = 0;+ List copy[FWS_MAX_LISTS];+ int n = nl;+ memcpy(copy, L, sizeof(List) * (size_t)n);+ pthread_mutex_unlock(&mu);++ Helper h = { -1, 0 };+ long long t = now_s();+ for (int i = 0; i < n && fetch; i++) {+ List *l = ©[i];+ int stale = !has_copy(l) || t - l->updated > FWS_STALE_S;+ /* a list that failed is tried again after an hour, not in a loop */+ if (!l->on || (fetch == 1 && !stale) || (fetch == 1 && l->error[0] && t - l->tried < 3600 && has_copy(l))) continue;+ if (fetch == 3 && has_copy(l)) continue; /* only what has no copy yet */+ fetch_one(&h, l);+ save_state(l);+ pthread_mutex_lock(&mu);+ List *o = find(l->id);+ if (o) {+ o->updated = l->updated; o->tried = l->tried; o->domains = l->domains; o->nets = l->nets;+ o->truncated = l->truncated;+ memcpy(o->error, l->error, sizeof o->error);+ }+ pthread_mutex_unlock(&mu);+ }+ helper_stop(&h);++ /* compile every enabled list that has a copy */+ FwLists *s = fwl_new();+ char ids[FWS_MAX_LISTS][32];+ memset(ids, 0, sizeof ids);+ for (int i = 0; s && i < n; i++) {+ if (!copy[i].on) continue;+ char p[PATH_MAX];+ cache_path(copy[i].id, "txt", p, sizeof p);+ size_t len;+ char *txt = read_file(p, FWL_FILE_MAX, &len);+ if (!txt) continue;+ memcpy(ids[i], copy[i].id, sizeof ids[i]);+ if (fwl_add_text(s, txt, len, (uint16_t)(i + 1)) < 0) fprintf(stderr, "lord.fw: the copy of blocklist %s is invalid\n", copy[i].id);+ free(txt);+ }+ size_t tl = 0;+ char *text = s ? fwl_to_text(s, &tl) : NULL;+ if (!text || write_file(txt_path, text, tl)) fprintf(stderr, "lord.fw: could not write %s\n", txt_path);+ free(text);++ pthread_mutex_lock(&mu);+ fwl_free(next_set);+ next_set = s;+ memcpy(next_ids, ids, sizeof ids);+ job_done = 1;+ int again = want_fetch || want_compile;+ if (!again) busy = 0;+ pthread_mutex_unlock(&mu);+ if (!again) return NULL;+ }+}++/* Called with mu held. */+static int rank(int f) { return f == 2 ? 3 : f == 1 ? 2 : f == 3 ? 1 : 0; } /* force > stale > missing > none */++static void kick(int fetch) {+ if (rank(fetch) > rank(want_fetch)) want_fetch = fetch;+ want_compile = 1;+ if (busy) return;+ busy = 1;+ pthread_t th;+ pthread_attr_t a;+ pthread_attr_init(&a);+ pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);+ if (pthread_create(&th, &a, worker, NULL)) busy = 0;+ pthread_attr_destroy(&a);+}++/* ---- the API ---- */++int fws_init(const char *home, const char *assets, const char *bindir) {+ if (inited) return 0;+ if (!home || !assets || !bindir) return -1;+ snprintf(cfg_path, sizeof cfg_path, "%s/.config/lordos/fwlists.json", home);+ snprintf(txt_path, sizeof txt_path, "%s/.config/lordos/fwlists.txt", home);+ snprintf(catalog_path, sizeof catalog_path, "%s/fw/blocklists.json", assets);+ snprintf(fetch_bin, sizeof fetch_bin, "%s/lord-fetch", bindir);+ char p[PATH_MAX];+ snprintf(p, sizeof p, "%s/.cache", home);+ mkdir(p, 0700);+ snprintf(p, sizeof p, "%s/.cache/lordos", home);+ mkdir(p, 0700);+ snprintf(cache_dir, sizeof cache_dir, "%s/.cache/lordos/fwlists", home);+ mkdir(cache_dir, 0700);+ snprintf(p, sizeof p, "%s/.config", home);+ mkdir(p, 0700);+ snprintf(p, sizeof p, "%s/.config/lordos", home);+ mkdir(p, 0700);+ inited = 1;+ load_catalog();+ load_config();+ int any = 0;+ for (int i = 0; i < nl; i++) { load_state(&L[i]); any |= L[i].on; }+ /* the set for the Monitor's "on a list" (the kernel's copy is lord-netperm's) */+ if (any) { pthread_mutex_lock(&mu); kick(0); pthread_mutex_unlock(&mu); }+ return 0;+}++int fws_poll(void) {+ if (!inited) return 0;+ pthread_mutex_lock(&mu);+ int r = job_done;+ if (r) {+ fwl_free(cur);+ cur = next_set;+ next_set = NULL;+ memcpy(cur_ids, next_ids, sizeof cur_ids);+ job_done = 0;+ }+ pthread_mutex_unlock(&mu);+ return r;+}++int fws_set(const char *id, int on) {+ pthread_mutex_lock(&mu);+ List *l = id ? find(id) : NULL;+ if (!l) { pthread_mutex_unlock(&mu); return -1; }+ l->on = !!on;+ save_config();+ kick(on ? 3 : 0); /* enabling one downloads it if there's no copy yet */+ pthread_mutex_unlock(&mu);+ return 0;+}++int fws_add_custom(const char *url, const char *format, char *id_out, char *err, size_t errlen) {+ char h[256], p[FWS_URL_MAX + 1];+ int port;+ if (fws_split_url(url, h, sizeof h, &port, p, sizeof p)) { snprintf(err, errlen, "fw.list.badUrl"); return -1; }+ if (!fwl_format_from_name(format)) { snprintf(err, errlen, "fw.list.badFormat"); return -1; }+ pthread_mutex_lock(&mu);+ int ncu = 0;+ for (int i = 0; i < nl; i++) {+ ncu += L[i].custom;+ if (!strcmp(L[i].url, url)) { pthread_mutex_unlock(&mu); snprintf(err, errlen, "fw.list.exists"); return -1; }+ }+ if (ncu >= FWS_MAX_CUSTOM || nl >= FWS_MAX_LISTS) { pthread_mutex_unlock(&mu); snprintf(err, errlen, "fw.list.tooMany"); return -1; }+ char id[32];+ for (int k = 1; k < 1000; k++) { snprintf(id, sizeof id, "custom-%d", k); if (!find(id)) break; }+ if (add_list(id, url, format, 0, 1)) { pthread_mutex_unlock(&mu); snprintf(err, errlen, "fw.list.badUrl"); return -1; }+ L[nl - 1].on = 1;+ save_config();+ kick(3);+ pthread_mutex_unlock(&mu);+ snprintf(id_out, 32, "%s", id);+ return 0;+}++int fws_remove_custom(const char *id) {+ pthread_mutex_lock(&mu);+ List *l = id ? find(id) : NULL;+ if (!l || !l->custom) { pthread_mutex_unlock(&mu); return -1; }+ char p[PATH_MAX];+ cache_path(l->id, "txt", p, sizeof p);+ unlink(p);+ cache_path(l->id, "json", p, sizeof p);+ unlink(p);+ int k = (int)(l - L);+ memmove(&L[k], &L[k + 1], sizeof(List) * (size_t)(nl - k - 1));+ nl--;+ save_config();+ kick(0);+ pthread_mutex_unlock(&mu);+ return 0;+}++int fws_update(int force) {+ pthread_mutex_lock(&mu);+ int any = 0;+ long long t = now_s();+ for (int i = 0; i < nl; i++) {+ if (!L[i].on) continue;+ int stale = t - L[i].updated > FWS_STALE_S && !(L[i].error[0] && t - L[i].tried < 3600);+ if (force || stale || !L[i].updated) any = 1;+ }+ int r = !any ? 1 : busy ? -1 : 0;+ if (any) kick(force ? 2 : 1);+ pthread_mutex_unlock(&mu);+ return r;+}++const char *fws_match(const char *const *names, int n, int fam, const uint8_t *addr) {+ if (!cur) return NULL;+ uint16_t t = 0;+ for (int i = 0; i < n && !t; i++) t = fwl_match_name(cur, names[i]);+ if (!t && (fam == 4 || fam == 6) && addr) t = fwl_match_addr(cur, fam, addr);+ return t && t <= FWS_MAX_LISTS && cur_ids[t - 1][0] ? cur_ids[t - 1] : NULL;+}++char *fws_json(void) {+ JBuf b;+ jb_init(&b);+ pthread_mutex_lock(&mu);+ jb_raw(&b, "{\"lists\":[");+ for (int i = 0; i < nl; i++) {+ const List *l = &L[i];+ jb_raw(&b, i ? ",{\"id\":" : "{\"id\":");+ jb_str(&b, l->id);+ jb_raw(&b, ",\"url\":");+ jb_str(&b, l->url);+ jb_raw(&b, ",\"format\":");+ jb_str(&b, l->format);+ jb_raw(&b, ",\"error\":");+ jb_str(&b, l->error);+ jb_printf(&b, ",\"custom\":%s,\"on\":%s,\"updated\":%lld,\"tried\":%lld,\"domains\":%ld,\"nets\":%ld,\"truncated\":%s}",+ l->custom ? "true" : "false", l->on ? "true" : "false", l->updated, l->tried, l->domains, l->nets,+ l->truncated ? "true" : "false");+ }+ jb_printf(&b, "],\"busy\":%s,\"domains\":%zu,\"nets\":%zu}", busy ? "true" : "false", fwl_domains(cur), fwl_nets(cur));+ pthread_mutex_unlock(&mu);+ if (b.oom) { jb_free(&b); return NULL; }+ return b.p;+}diff --git a/server/src/fw/fwsub.h b/server/src/fw/fwsub.hnew file mode 100644index 0000000..6a012d1--- /dev/null+++ b/server/src/fw/fwsub.h@@ -0,0 +1,49 @@+/* fwsub.h — the user's blocklist subscriptions, lord-server's side (0.8.4, docs/FIREWALL.md,+ * "Blocklists").+ *+ * assets/fw/blocklists.json the curated catalog (our own asset: id, topic, url, format,+ * licence, source); every entry's licence was checked before it+ * went in+ * ~/.config/lordos/fwlists.json which ones are on, and the custom lists (https URLs)+ * ~/.cache/lordos/fwlists/<id>.txt the normalized download of each list, and <id>.json its state+ * ~/.config/lordos/fwlists.txt every enabled list compiled (fw/fwlists.h); lord-netperm reads it+ *+ * All three user files are written only here (fsops_protected). The work (downloading through+ * lord-fetch and compiling) runs on its own thread: the screen never waits. When it ends,+ * fws_poll() swaps the set in and says so, and lord.fw sends lord-netperm its RELOAD. Lists are+ * downloaded when enabled, and again when the last download is older than a day (fws_update(0)+ * every hour from the SDK). */+#ifndef LORD_FW_FWSUB_H+#define LORD_FW_FWSUB_H++#include <stddef.h>+#include <stdint.h>++#define FWS_MAX_LISTS 48 /* catalog + custom */+#define FWS_MAX_CUSTOM 16+#define FWS_URL_MAX 400+#define FWS_STALE_S (20 * 3600) /* "daily": older than this is downloaded again */++/* home: the user's real home folder; assets: LordOS's assets folder; bindir: where lord-fetch is. */+int fws_init(const char *home, const char *assets, const char *bindir);+/* {"catalog":[…],"custom":[…],"enabled":[…],"state":{id:{…}},"busy":bool,"domains":N,"nets":N} (malloc'd) */+char *fws_json(void);+/* 0 ok; -1 unknown id. Starts a job (compile, and download what has no copy yet). */+int fws_set(const char *id, int on);+/* Adds a custom list; 0 with its id in id_out (32 bytes), or -1 with a code in err+ * ("fw.list.badUrl", "fw.list.badFormat", "fw.list.tooMany", "fw.list.exists"). */+int fws_add_custom(const char *url, const char *format, char *id_out, char *err, size_t errlen);+int fws_remove_custom(const char *id);+/* Downloads the enabled lists (all of them with force, else only the stale ones). 0 started, 1 nothing+ * to do, -1 a job is already running (it runs again when that one ends). */+int fws_update(int force);+/* Main thread: 1 if a job just ended and the compiled set changed (send RELOAD). */+int fws_poll(void);+/* Which enabled list covers these names or this address (fam 4 or 6, or 0): its id, or NULL. */+const char *fws_match(const char *const *names, int n, int fam, const uint8_t *addr);++/* ---- exposed for tests ---- */+/* https://host[:port]/path → parts; 0 ok. host: lowercase name or IPv4, port 1..65535. */+int fws_split_url(const char *url, char *host, size_t hcap, int *port, char *path, size_t pcap);++#endifdiff --git a/server/src/ui/js_fw.c b/server/src/ui/js_fw.cindex c77178e..4576a57 100644--- a/server/src/ui/js_fw.c+++ b/server/src/ui/js_fw.c@@ -12,7 +12,7 @@* state() { mode, trusted: [app], restricted: { app: [restriction] } }* appRule(app) 'allow' | 'deny' | null (the {app, out, any} rule)* setAppRule(app, v) v: 'allow' | 'deny' | null (removes it); the Monitor's switch- * setTrusted(app, on) / setRestricted(app, [restriction]) / setMode('silent'|'ask'|'off')+ * setTrusted(app, on) / setRestricted(app, [restriction]) / setMode('silent'|'ask'|'off'|'deny')* rules(app) [{ id, dir, dst, port, proto, action, when, lastMatch }]* when: Unix seconds of the decision; lastMatch: Unix seconds of the* last connection lord-netperm reported as decided by that rule@@ -27,6 +27,15 @@* blocks(app, restriction) true if the app is restricted in it (sdk/notify.js asks before showing* a built-in app's notification)*+ * (0.8.4) Subscribed blocklists (fw/fwsub.h; docs/FIREWALL.md, "Blocklists"):+ * lists() { lists: [{ id, url, format, custom, on, updated, tried, domains, nets,+ * truncated, error }], busy, domains, nets }+ * setList(id, on) a catalog or custom list on or off+ * addList(url, format) → id a custom list (https only; 'hosts' | 'domains' | 'ips'); throws fw.list.*+ * removeList(id) a custom list+ * updateLists(force) 0 started, 1 nothing to do, -1 already running+ * listMatch([names], addr) the id of the enabled list that covers that destination, or null+ ** The non-network restrictions are enforced in C where the resource is handed out (sec/restrict.h:* the broker and js_media.c read this same store); adding one to a running app takes the camera* or the microphone away at once (restrict_changed).@@ -35,10 +44,13 @@#include "js_fw.h"#include "../fw/rules.h"#include "../fw/fwmap.h"+#include "../fw/fwsub.h"+#include "js_sys.h"#include "../sec/restrict.h"#include "js_err.h"#include "../netperm/npproto.h"+#include <arpa/inet.h>#include <errno.h>#include <fcntl.h>#include <limits.h>@@ -198,9 +210,13 @@ static void keep(char **slot, const unsigned char *p, size_t n) {*slot = s;}-/* Reads whatever has arrived, without waiting. */+static int lists_on;++/* Reads whatever has arrived, without waiting; a blocklist job that ended makes lord-netperm read+ * the new compiled lists. */static void pump(void) {ensure();+ if (lists_on && fws_poll()) send_reload();if (sock < 0) return;if (!inbuf && !(inbuf = malloc(NP_OUT_MAX + 8))) return;for (;;) {@@ -606,7 +622,118 @@ const char *js_fw_counts(uint64_t *seq) {return counts_json;}+/* ---- blocklists (0.8.4) ---- */++static void lists_init(void) {+ if (lists_on) return;+ const char *h = getenv("HOME");+ char real[PATH_MAX], self[PATH_MAX];+ if (!h || !realpath(h, real)) return;+ ssize_t n = readlink("/proc/self/exe", self, sizeof self - 1);+ if (n <= 0) return;+ self[n] = 0;+ char *sl = strrchr(self, '/');+ if (!sl) return;+ *sl = 0;+ if (!fws_init(real, js_sys_assets(), self)) lists_on = 1;+}++static JSValue f_lists(JSContext *c, JSValueConst t, int argc, JSValueConst *argv) {+ (void)t; (void)argc; (void)argv;+ lists_init();+ pump();+ char *j = lists_on ? fws_json() : NULL;+ if (!j) return JS_NULL;+ JSValue v = JS_ParseJSON(c, j, strlen(j), "<fw.lists>");+ free(j);+ return v;+}++static int arg_list_id(JSContext *c, JSValueConst v, char *out) {+ if (!JS_IsString(v)) return -1;+ const char *s = JS_ToCString(c, v);+ if (!s) return -1;+ size_t n = strlen(s);+ int ok = n > 0 && n < 32;+ for (size_t i = 0; ok && i < n; i++) ok = (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9') || s[i] == '-';+ if (ok) memcpy(out, s, n + 1);+ JS_FreeCString(c, s);+ return ok ? 0 : -1;+}++static JSValue f_set_list(JSContext *c, JSValueConst t, int argc, JSValueConst *argv) {+ (void)t;+ char id[32];+ if (argc < 2 || arg_list_id(c, argv[0], id)) return js_throw_code(c, "fw.list.unknown", NULL);+ lists_init();+ if (!lists_on || fws_set(id, JS_ToBool(c, argv[1]))) return js_throw_code(c, "fw.list.unknown", NULL);+ return JS_TRUE;+}++static JSValue f_add_list(JSContext *c, JSValueConst t, int argc, JSValueConst *argv) {+ (void)t;+ if (argc < 2 || !JS_IsString(argv[0]) || !JS_IsString(argv[1])) return js_throw_code(c, "fw.list.badUrl", NULL);+ lists_init();+ if (!lists_on) return js_throw_code(c, "fw.list.unavailable", NULL);+ const char *url = JS_ToCString(c, argv[0]), *fmt = JS_ToCString(c, argv[1]);+ char id[32] = "", err[64] = "fw.list.badUrl";+ int r = url && fmt && strlen(url) <= FWS_URL_MAX ? fws_add_custom(url, fmt, id, err, sizeof err) : -1;+ if (url) JS_FreeCString(c, url);+ if (fmt) JS_FreeCString(c, fmt);+ if (r) return js_throw_code(c, err, NULL);+ return JS_NewString(c, id);+}++static JSValue f_remove_list(JSContext *c, JSValueConst t, int argc, JSValueConst *argv) {+ (void)t;+ char id[32];+ if (argc < 1 || arg_list_id(c, argv[0], id)) return js_throw_code(c, "fw.list.unknown", NULL);+ lists_init();+ if (!lists_on || fws_remove_custom(id)) return js_throw_code(c, "fw.list.unknown", NULL);+ return JS_TRUE;+}++static JSValue f_update_lists(JSContext *c, JSValueConst t, int argc, JSValueConst *argv) {+ (void)t;+ lists_init();+ if (!lists_on) return JS_NewInt32(c, 1);+ return JS_NewInt32(c, fws_update(argc > 0 && JS_ToBool(c, argv[0])));+}++/* listMatch([names], addr): up to 8 names (each checked by the set's own normalizer) and an+ * address in text form, or null */+static JSValue f_list_match(JSContext *c, JSValueConst t, int argc, JSValueConst *argv) {+ (void)t;+ if (!lists_on) return JS_NULL;+ const char *names[8];+ int n = 0;+ if (argc > 0 && JS_IsArray(c, argv[0])) {+ for (uint32_t i = 0; i < 8; i++) {+ JSValue v = JS_GetPropertyUint32(c, argv[0], i);+ if (JS_IsString(v)) { const char *s = JS_ToCString(c, v); if (s && strlen(s) < FW_NAME_MAX) names[n++] = s; else if (s) JS_FreeCString(c, s); }+ JS_FreeValue(c, v);+ }+ }+ uint8_t a[16] = { 0 };+ int fam = 0;+ if (argc > 1 && JS_IsString(argv[1])) {+ const char *s = JS_ToCString(c, argv[1]);+ if (s && inet_pton(AF_INET, s, a) == 1) fam = 4;+ else if (s && inet_pton(AF_INET6, s, a) == 1) fam = 6;+ if (s) JS_FreeCString(c, s);+ }+ const char *id = fws_match(names, n, fam, fam ? a : NULL);+ for (int i = 0; i < n; i++) JS_FreeCString(c, names[i]);+ return id ? JS_NewString(c, id) : JS_NULL;+}+static const JSCFunctionListEntry fw_funcs[] = {+ JS_CFUNC_DEF("lists", 0, f_lists),+ JS_CFUNC_DEF("setList", 2, f_set_list),+ JS_CFUNC_DEF("addList", 2, f_add_list),+ JS_CFUNC_DEF("removeList", 1, f_remove_list),+ JS_CFUNC_DEF("updateLists", 1, f_update_lists),+ JS_CFUNC_DEF("listMatch", 2, f_list_match),JS_CFUNC_DEF("status", 0, f_status),JS_CFUNC_DEF("state", 0, f_state),JS_CFUNC_DEF("appRule", 1, f_app_rule),diff --git a/server/tests/fwsub_test.c b/server/tests/fwsub_test.cnew file mode 100644index 0000000..6e07c31--- /dev/null+++ b/server/tests/fwsub_test.c@@ -0,0 +1,188 @@+/* fwsub_test.c — blocklist subscriptions, lord-server's side (src/fw/fwsub.c) and the lord-fetch+ * helper's request checks (0.8.4, docs/FIREWALL.md "Blocklists"). No network: the downloads are+ * seeded in the cache folder; lord-fetch is only asked things it must refuse.+ * build/fwsub-test [path to build/lord-fetch] */+#include "fw/fwsub.h"+#include "fw/fwlists.h"+#include "doc/wire.h"+#include "fetch/fetchproto.h"++#include <arpa/inet.h>+#include <errno.h>+#include <fcntl.h>+#include <poll.h>+#include <signal.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/socket.h>+#include <sys/stat.h>+#include <sys/wait.h>+#include <time.h>+#include <unistd.h>++static int fails, checks;+#define CHECK(c) do { checks++; if (!(c)) { fails++; fprintf(stderr, "%s:%d: failed: %s\n", __FILE__, __LINE__, #c); } } while (0)++static void put(const char *p, const char *s) {+ FILE *f = fopen(p, "w");+ if (!f) { perror(p); exit(2); }+ fputs(s, f);+ fclose(f);+}++static char *slurp(const char *p) {+ FILE *f = fopen(p, "r");+ if (!f) return NULL;+ static char b[1 << 16];+ size_t n = fread(b, 1, sizeof b - 1, f);+ fclose(f);+ b[n] = 0;+ return b;+}++static void test_urls(void) {+ char h[256], p[512];+ int port;+ CHECK(!fws_split_url("https://lists.example.org/a/b.txt", h, sizeof h, &port, p, sizeof p) && !strcmp(h, "lists.example.org") && port == 443 && !strcmp(p, "/a/b.txt"));+ CHECK(!fws_split_url("https://Lists.Example.org:8443/x?y=1", h, sizeof h, &port, p, sizeof p) && !strcmp(h, "lists.example.org") && port == 8443 && !strcmp(p, "/x?y=1"));+ CHECK(!fws_split_url("https://example.org", h, sizeof h, &port, p, sizeof p) && !strcmp(p, "/"));+ const char *bad[] = { "http://example.org/x", "https://", "https:///x", "https://exa mple.org/", "https://example.org:0/", "https://example.org:70000/",+ "https://[email protected]/", "https://localhost/x", "https://.example.org/", "https://example..org/", "https://example.org/a b",+ "https://example.org/\x01", "https://example.org?x", "file:///etc/passwd", "https://[::1]/x", "", NULL };+ for (int i = 0; bad[i]; i++) CHECK(fws_split_url(bad[i], h, sizeof h, &port, p, sizeof p) == -1);+}++static int wait_job(void) {+ for (int i = 0; i < 300; i++) {+ if (fws_poll()) return 1;+ struct timespec ts = { 0, 10 * 1000 * 1000 };+ nanosleep(&ts, NULL);+ }+ return 0;+}++static void test_subscriptions(const char *root) {+ char p[1024], assets[256];+ snprintf(assets, sizeof assets, "%s/assets", root);+ snprintf(p, sizeof p, "%s/fw", assets);+ mkdir(assets, 0700);+ mkdir(p, 0700);+ snprintf(p, sizeof p, "%s/fw/blocklists.json", assets);+ put(p, "{\"lists\":[{\"id\":\"ads\",\"url\":\"https://lists.example.org/ads.txt\",\"format\":\"domains\",\"sub\":0},"+ "{\"id\":\"bad id\",\"url\":\"https://x.example/\",\"format\":\"domains\"},"+ "{\"id\":\"ips\",\"url\":\"http://plain.example/ips\",\"format\":\"ips\"},"+ "{\"id\":\"nets\",\"url\":\"https://lists.example.org/nets.txt\",\"format\":\"ips\"}]}");+ CHECK(!fws_init(root, assets, "/nonexistent"));+ /* seeded downloads (what lord-fetch would have written) */+ snprintf(p, sizeof p, "%s/.cache/lordos/fwlists/ads.txt", root);+ put(p, "d tracker.example\ns ads.example\n");+ snprintf(p, sizeof p, "%s/.cache/lordos/fwlists/nets.txt", root);+ put(p, "n 203.0.113.0/24\n");+ char *j = fws_json();+ CHECK(j && strstr(j, "\"id\":\"ads\"") && strstr(j, "\"id\":\"nets\"") && !strstr(j, "bad id") && !strstr(j, "plain.example"));+ free(j);+ CHECK(fws_set("nope", 1) == -1);+ CHECK(!fws_set("ads", 1));+ CHECK(wait_job());+ const char *n1[] = { "x.ads.example" }, *n2[] = { "site.example" };+ CHECK(fws_match(n1, 1, 0, NULL) && !strcmp(fws_match(n1, 1, 0, NULL), "ads"));+ CHECK(!fws_match(n2, 1, 0, NULL));+ uint8_t a[16] = { 0 };+ inet_pton(AF_INET, "203.0.113.7", a);+ CHECK(!fws_match(NULL, 0, 4, a)); /* nets is off */+ CHECK(!fws_set("nets", 1) && wait_job());+ CHECK(fws_match(NULL, 0, 4, a) && !strcmp(fws_match(NULL, 0, 4, a), "nets"));+ /* the compiled file lord-netperm reads */+ snprintf(p, sizeof p, "%s/.config/lordos/fwlists.txt", root);+ char *t = slurp(p);+ char err[64];+ FwLists *l = t ? fwl_load_text(t, strlen(t), err, sizeof err) : NULL;+ CHECK(l && fwl_domains(l) == 2 && fwl_nets(l) == 1);+ fwl_free(l);+ struct stat st;+ CHECK(!stat(p, &st) && (st.st_mode & 0777) == 0600);+ /* the config: survives, and a custom list */+ char id[32], e[64];+ CHECK(fws_add_custom("http://x.example/l", "domains", id, e, sizeof e) == -1 && !strcmp(e, "fw.list.badUrl"));+ CHECK(fws_add_custom("https://x.example/l", "csv", id, e, sizeof e) == -1 && !strcmp(e, "fw.list.badFormat"));+ CHECK(fws_add_custom("https://lists.example.org/ads.txt", "domains", id, e, sizeof e) == -1 && !strcmp(e, "fw.list.exists"));+ CHECK(!fws_add_custom("https://own.example/mine.txt", "hosts", id, e, sizeof e) && !strcmp(id, "custom-1"));+ wait_job(); /* it tries to download it (no lord-fetch here: an error, no copy) */+ snprintf(p, sizeof p, "%s/.config/lordos/fwlists.json", root);+ t = slurp(p);+ CHECK(t && strstr(t, "\"enabled\":[\"ads\",\"nets\",\"custom-1\"]") && strstr(t, "own.example"));+ j = fws_json();+ CHECK(j && strstr(j, "\"error\":\"helper\""));+ free(j);+ CHECK(fws_remove_custom("ads") == -1); /* only custom ones */+ CHECK(!fws_remove_custom("custom-1") && wait_job());+ t = slurp(p);+ CHECK(t && !strstr(t, "own.example"));+ /* turning a list off removes it from the compiled set */+ CHECK(!fws_set("ads", 0) && wait_job());+ CHECK(!fws_match(n1, 1, 0, NULL));+}++/* lord-fetch refuses what it must, without any network (it never opens a socket itself) */+static void test_helper(const char *bin) {+ if (access(bin, X_OK)) { printf("fwsub-test: lord-fetch skipped: needs %s\n", bin); return; }+ int sv[2];+ CHECK(!socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv));+ pid_t p = fork();+ if (p == 0) {+ dup2(sv[1], 3);+ char *argv[] = { "lord-fetch", NULL }, *envp[] = { NULL };+ execve(bin, argv, envp);+ _exit(127);+ }+ close(sv[1]);+ int pair[2];+ socketpair(AF_UNIX, SOCK_STREAM, 0, pair); /* a stand-in for the connected socket */+ const char *reqs[] = { "{\"host\":\"../etc\",\"path\":\"/x\",\"format\":\"domains\"}", "{\"host\":\"a.example\",\"path\":\"x\",\"format\":\"domains\"}",+ "{\"host\":\"a.example\",\"path\":\"/x\",\"format\":\"csv\"}", "not json", NULL };+ for (int i = 0; reqs[i]; i++) {+ int fd = dup(pair[0]);+ CHECK(!doc_send(sv[0], FQ_GET, (uint32_t)i + 1, reqs[i], strlen(reqs[i]), &fd, 1));+ close(fd);+ DocMsg m;+ struct pollfd pf = { sv[0], POLLIN, 0 };+ CHECK(poll(&pf, 1, 5000) == 1 && !doc_recv(sv[0], &m));+ CHECK(m.h.op == FR_ERR && m.h.id == (uint32_t)i + 1 && m.json && strstr(m.json, "bad-request"));+ doc_msg_free(&m);+ }+ /* a peer that isn't TLS: the handshake fails, it says so */+ int fd = dup(pair[0]);+ const char *ok = "{\"host\":\"a.example\",\"path\":\"/x\",\"format\":\"domains\"}";+ CHECK(!doc_send(sv[0], FQ_GET, 9, ok, strlen(ok), &fd, 1));+ close(fd);+ close(pair[0]);+ (void)!write(pair[1], "HTTP/1.1 200 OK\r\n\r\nnot tls\n", 27);+ close(pair[1]);+ DocMsg m;+ struct pollfd pf = { sv[0], POLLIN, 0 };+ CHECK(poll(&pf, 1, 35000) == 1 && !doc_recv(sv[0], &m));+ CHECK(m.h.op == FR_ERR && m.json && strstr(m.json, "\"tls\""));+ doc_msg_free(&m);+ doc_send(sv[0], FQ_END, 0, "{}", 2, NULL, 0);+ int st;+ waitpid(p, &st, 0);+ CHECK(WIFEXITED(st) && WEXITSTATUS(st) == 0);+ close(sv[0]);+ printf("fwsub-test: lord-fetch refuses bad requests and a peer without TLS: ok\n");+}++int main(int argc, char **argv) {+ signal(SIGPIPE, SIG_IGN);+ char root[] = "/tmp/fwsub-test.XXXXXX";+ if (!mkdtemp(root)) return 2;+ unsetenv("LORDOS_SESSIOND");+ test_urls();+ test_subscriptions(root);+ test_helper(argc > 1 ? argv[1] : "build/lord-fetch");+ char cmd[600];+ snprintf(cmd, sizeof cmd, "rm -rf '%s'", root);+ (void)!system(cmd);+ printf("fwsub-test: %d/%d passed\n", checks - fails, checks);+ return fails ? 1 : 0;+}diff --git a/server/tests/netconn_test.c b/server/tests/netconn_test.cindex 5497165..e7bb6fb 100644--- a/server/tests/netconn_test.c+++ b/server/tests/netconn_test.c@@ -1,7 +1,6 @@/* netconn_test.c — the components' network connectors (src/sec/netconn.c, 0.8.4):* request validation on both sides, the sockets the connector makes, the real connector process* (this binary re-executed with --net-connector, as lord-server is) and its fail-closed start. */-#define _GNU_SOURCE#include "sec/netconn.h"#include <arpa/inet.h>@@ -71,7 +70,7 @@ static int connected(int fd) {}static void test_sockets(int via_process) {- int port, l = listener(&port);+ int port = 0, l = listener(&port);CHECK(l >= 0);struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons((uint16_t)port) };inet_pton(AF_INET, "127.0.0.1", &a.sin_addr);diff --git a/tools/iso/mkdeb.sh b/tools/iso/mkdeb.shindex 86aea56..87473c3 100755--- a/tools/iso/mkdeb.sh+++ b/tools/iso/mkdeb.sh@@ -69,6 +69,9 @@ strip --strip-unneeded "$L/bin/lord-server" "$L/bin/lord-sessiond" "$L/bin/lord-# lord-maild: IMAP/SMTP/CalDAV/CardDAV with TLS for Mail (docs/MAIL.md); lord-server only launches the one in its own folder[ -x server/build/lord-maild ] && install -m 755 server/build/lord-maild "$L/bin/lord-maild"[ -x "$L/bin/lord-maild" ] && { strip --strip-unneeded "$L/bin/lord-maild" 2>/dev/null || true; }+# lord-fetch (0.8.4): downloads the subscribed blocklists over HTTPS, sandboxed (docs/FIREWALL.md, "Blocklists")+[ -x server/build/lord-fetch ] && install -m 755 server/build/lord-fetch "$L/bin/lord-fetch"+[ -x "$L/bin/lord-fetch" ] && { strip --strip-unneeded "$L/bin/lord-fetch" 2>/dev/null || true; }# FIDO2 security keys reachable by the session user (lord-sessiond sessions have no logind seat)install -d "$PKG/usr/lib/udev/rules.d"install -m 644 packaging/udev/71-lordos-fido.rules "$PKG/usr/lib/udev/rules.d/71-lordos-fido.rules"@@ -79,7 +82,7 @@ for d in themes/*/; doinstall -m 644 "$d/theme.json" "$d/theme.bundle.js" "$L/themes/$n/"[ -d "$d/assets" ] && cp -r "$d/assets" "$L/themes/$n/"done-cp -r assets/fonts assets/locales assets/shell assets/icons assets/mascot assets/monitor assets/help "$L/assets/"+cp -r assets/fonts assets/locales assets/shell assets/icons assets/mascot assets/monitor assets/help assets/fw "$L/assets/"# the wallpaper collection (assets/wallpapers/README.md): once, in the standard place for desktop# backgrounds; lord-server reaches it as asset:wallpapers/… through the link. Only the JPEGs, the# thumbnails and the README (index.json is bundled into the SDK at build time).