/* * splinter -- kid mode. * * Copyright (c) 2026 Konrad Lother * SPDX-License-Identifier: BSD-3-Clause * * A child may talk to splinter. Every action that could change something goes to * a parent first, and the model is told to stay friendly and age-appropriate. * * SPLINTER_KIDS_APPROVE_CMD program that asks the parent; exit 0 = yes, * anything else = no. It gets the request as its * first argument. docs/kids/ask.sh speaks Telegram. * SPLINTER_KIDS_TIMEOUT seconds to wait for the parent (default 120) * ~/.splinter/kids/allow what is allowed without asking, one rule per * line: "read *" or "run ls*" * * Two rules matter more than the rest: * * - a timeout is a NO. A parent who is not looking does not approve anything. * - the whitelist matches the tool and its arguments, never the mood. */ #include #include #include #include #include #include #include #include #include #include "splinter.h" #include "splinter_plugin.h" #include "ack.h" #include "mcp.h" #define KIDS_RULES 64 #define KIDS_RULE_LEN 256 static char allow_file[700]; static char approve_cmd[512]; static unsigned timeout_s = 120; static char child_name[64] = "das Kind"; static char usage_file[700]; static char domains_file[700]; static int search_mode; /* 0 approve, 1 allow, 2 deny */ static unsigned daily_minutes; /* 0 = no limit */ static long used_seconds; /* today */ static time_t last_seen; static char today[16]; /* One rule: " ", where * matches anything and a missing glob is "*". */ struct rule { char tool[32]; char pattern[KIDS_RULE_LEN]; }; static struct rule rules[KIDS_RULES]; static size_t nrules; /* * Daily budget. * * The time is counted where splinter actually works - one step at a time - and * stored per day in a plain file: "2026-11-08 4314". A new day starts at zero. * When the budget is gone the gate refuses every tool, including reading, and the * conversation is told to close the day: the child gets an answer that says so, * not a silent hang. */ static void usage_load(void) { FILE *f; char d[32]; long s; snprintf(today, sizeof(today), "%s", "1970-01-01"); used_seconds = 0; f = fopen(usage_file, "r"); if (f == NULL) return; if (fscanf(f, "%31s %ld", d, &s) == 2) { time_t now = time(NULL); struct tm tm; (void)localtime_r(&now, &tm); { char now_s[16]; strftime(now_s, sizeof(now_s), "%Y-%m-%d", &tm); snprintf(today, sizeof(today), "%s", now_s); if (strcmp(d, now_s) == 0) used_seconds = s; } } fclose(f); } static void usage_save(void) { FILE *f; f = fopen(usage_file, "w"); if (f == NULL) return; fprintf(f, "%s %ld\n", today, used_seconds); fclose(f); } /* Count the time since the last step, capped so a pause is not a punishment. */ static void usage_tick(void) { time_t now = time(NULL); usage_load(); if (last_seen != 0) { long delta = (long)(now - last_seen); if (delta > 0) { if (delta > 120) delta = 120; used_seconds += delta; } } last_seen = now; usage_save(); } static int quota_left(void) { if (daily_minutes == 0) return -1; /* no limit */ usage_load(); return used_seconds < (long)daily_minutes * 60; } /* * '*' matches any run of characters. Iterative, remembering only the last star: * the recursive version backtracked exponentially on model-written arguments. */ static int glob_match(const char *pattern, const char *text) { const char *star = NULL, *resume = NULL; while (*text != '\0') { if (*pattern == '*') { star = pattern++; resume = text; } else if (*pattern == *text) { pattern++; text++; } else if (star != NULL) { pattern = star + 1; text = ++resume; } else return 0; } while (*pattern == '*') pattern++; return *pattern == '\0'; } static void load_rules(void) { FILE *f; char line[512]; nrules = 0; f = fopen(allow_file, "r"); if (f == NULL) return; while (fgets(line, sizeof(line), f) != NULL && nrules < KIDS_RULES) { char *hash = strchr(line, '#'); char *p = line, *sp; if (hash != NULL) *hash = '\0'; line[strcspn(line, "\r\n")] = '\0'; while (*p == ' ' || *p == '\t') p++; if (*p == '\0') continue; sp = strchr(p, ' '); memset(&rules[nrules], 0, sizeof(rules[0])); if (sp == NULL) { snprintf(rules[nrules].tool, sizeof(rules[0].tool), "%.31s", p); snprintf(rules[nrules].pattern, sizeof(rules[0].pattern), "*"); } else { size_t n = (size_t)(sp - p); if (n >= sizeof(rules[0].tool)) n = sizeof(rules[0].tool) - 1; memcpy(rules[nrules].tool, p, n); rules[nrules].tool[n] = '\0'; while (*sp == ' ') sp++; snprintf(rules[nrules].pattern, sizeof(rules[0].pattern), "%.255s", sp); } nrules++; } fclose(f); } /* * What a rule is matched against: the argument that says what the call does - * the command of 'run', else a path or a URL - not the raw JSON. Against the * JSON ('{"command":"ls"}') a rule like "run ls*" never matched at all, and * only "*" rules ever had an effect. */ static void rule_subject(const char *tool, const char *args, char *buf, size_t len) { static const char *keys[] = { "command", "path", "url", NULL }; mcp_json *v; size_t k; snprintf(buf, len, "%s", args != NULL ? args : ""); if (args == NULL || args[0] != '{' || (v = mcp_parse(args)) == NULL) return; for (k = strcmp(tool, "run") == 0 ? 0 : 1; keys[k] != NULL; k++) { const char *val = mcp_arg_string(v, keys[k]); if (val != NULL) { snprintf(buf, len, "%s", val); break; } } mcp_json_free(v); } static int whitelisted(const char *tool, const char *args) { char subject[1024]; size_t i; rule_subject(tool, args, subject, sizeof(subject)); /* * "run ls*" is meant to allow ls, not "ls; anything". A shell command * that can chain, substitute or redirect is never on the list: a parent * decides it. */ if (strcmp(tool, "run") == 0 && strpbrk(subject, ";&|`$<>\n\r(") != NULL) return 0; for (i = 0; i < nrules; i++) { if (strcmp(rules[i].tool, tool) != 0 && strcmp(rules[i].tool, "*")) continue; if (glob_match(rules[i].pattern, subject)) return 1; } return 0; } /* * sh -c '' sh '': the command string is parsed, the argument is not. * Returns the wait status, like system(3). After 'secs' seconds the command and * everything it started (its own process group) are killed and *timed_out is * set: a parent who is not looking does not approve anything, and the child is * not kept waiting forever either. */ static int run_with_arg(const char *cmd, const char *arg, unsigned secs, int *timed_out) { pid_t child, r; int status; struct timespec start, now, nap = { 0, 50000000L }; *timed_out = 0; child = fork(); if (child < 0) return -1; if (child == 0) { (void)setpgid(0, 0); execl("/bin/sh", "sh", "-c", cmd, "sh", arg, (char *)NULL); _exit(127); } (void)setpgid(child, child); (void)clock_gettime(CLOCK_MONOTONIC, &start); for (;;) { r = waitpid(child, &status, WNOHANG); if (r == child) return status; if (r < 0 && errno != EINTR) return -1; (void)clock_gettime(CLOCK_MONOTONIC, &now); if ((now.tv_sec - start.tv_sec) * 1000L + (now.tv_nsec - start.tv_nsec) / 1000000L >= (long)secs * 1000L) { *timed_out = 1; (void)kill(-child, SIGKILL); (void)kill(child, SIGKILL); while (waitpid(child, &status, 0) < 0 && errno == EINTR) ; return -1; } (void)nanosleep(&nap, NULL); } } /* * Ask the parent. The command gets the request as one argument and answers with * its exit status; anything but 0 - including a timeout - means no. */ static int ask_parent(const char *tool, const char *args, char *reason, size_t reason_len) { char cmd[1024], request[1024]; int rc, timed_out; /* * The general ack mechanism first: a request file plus whichever channels are * configured. Then any process may answer - the gateway, the parent's phone, * or 'splinter --ack yes'. Only without it does the older single command * get used. */ if (getenv("SPLINTER_ACK_CHANNELS") != NULL || getenv("SPLINTER_ACK_DIR") != NULL) { char who[64] = ""; int id = sp_ack_request(tool, args, "kind will etwas aendern", (int)timeout_s); if (id < 0) { snprintf(reason, reason_len, "Zustimmung nicht moeglich"); return 0; } (void)sp_ack_notify(id, NULL); if (sp_ack_wait(id, (int)timeout_s, who, sizeof(who))) { snprintf(reason, reason_len, "zugestimmt von %s", who[0] != '\0' ? who : "einem Erwachsenen"); return 1; } snprintf(reason, reason_len, "ein Erwachsener hat nicht " "zugestimmt (Zeit abgelaufen)"); return 0; } /* * The command is the operator's and may carry its own arguments, so it * still goes through sh. The request is the model's and must not: it is * handed over as "$1", a positional parameter the shell never parses. A * quote in the tool arguments is then just a quote. */ snprintf(cmd, sizeof(cmd), "%s \"$1\"", approve_cmd); snprintf(request, sizeof(request), "Anfrage: %s %s", tool, args != NULL ? args : ""); rc = run_with_arg(cmd, request, timeout_s, &timed_out); if (rc == 0 && !timed_out) return 1; snprintf(reason, reason_len, "ein Erwachsener hat nicht zugestimmt (%s)", timed_out || (rc != -1 && WIFEXITED(rc) && WEXITSTATUS(rc) == 124) ? "Zeit abgelaufen" : "abgelehnt"); return 0; } /* * The web is not a filesystem. A child with a search box has the whole internet, * so search is its own decision, not "read, therefore allowed": * * SPLINTER_KIDS_SEARCH=approve every search asks a parent (default) * SPLINTER_KIDS_SEARCH=allow searching is fine, fetching still asks * SPLINTER_KIDS_SEARCH=deny no web at all * * webfetch additionally only reaches hosts listed in ~/.splinter/kids/domains, * because "the model picked this link" is not a safety argument. */ /* * The url out of the tool arguments, read with the same parser the tool itself * uses. A hand-rolled strstr took the first "url" key while the tool took the * one the parser picks, so two keys meant: check one host, fetch another. */ static void arg_url(const char *args, char *buf, size_t len) { mcp_json *v; const char *u; buf[0] = '\0'; if (args == NULL || args[0] != '{') return; v = mcp_parse(args); if (v == NULL) return; u = mcp_arg_string(v, "url"); if (u != NULL && strlen(u) < len) snprintf(buf, len, "%s", u); mcp_json_free(v); } static int host_allowed(const char *url) { FILE *f; char line[256]; const char *host; int ok = 0; if (url == NULL) return 0; if (strncmp(url, "http://", 7) == 0) host = url + 7; else if (strncmp(url, "https://", 8) == 0) host = url + 8; else return 0; /* * "https://allowed.org:x@evil.example/" names evil.example: whatever is * before an '@' is a user name, not the host. Rather than parse userinfo * correctly, a URL that has any is not on the list. */ { size_t auth = strcspn(host, "/?#"); const char *at = memchr(host, '@', auth); if (at != NULL || memchr(host, '\\', auth) != NULL) return 0; } f = fopen(domains_file, "r"); if (f == NULL) return 0; while (fgets(line, sizeof(line), f) != NULL) { size_t n, hlen; line[strcspn(line, "\r\n")] = '\0'; if (line[0] == '#' || line[0] == '\0') continue; n = strlen(line); hlen = strcspn(host, "/:?#"); if (hlen == n && strncmp(host, line, n) == 0) { ok = 1; break; } } fclose(f); return ok; } static int kids_gate(struct sp_host *h, const char *tool, int auth, const char *args, char *reason, size_t reason_len) { (void)h; /* * The budget comes first: when the day is over every tool is refused, * reading and the parent's own domain list included. */ if (quota_left() == 0) { snprintf(reason, reason_len, "fuer heute ist Schluss (%ld von %u " "Minuten)", used_seconds / 60, daily_minutes); return 0; } if (strcmp(tool, "websearch") == 0 || strcmp(tool, "webfetch") == 0) { /* * The curated list is the safe ground and works in every mode: a * page a parent wrote down is allowed even when the open web is * shut. Everything else follows the mode. */ if (strcmp(tool, "webfetch") == 0) { char url[512]; arg_url(args, url, sizeof(url)); if (url[0] != '\0' && host_allowed(url)) return 1; } if (search_mode == 2) { snprintf(reason, reason_len, "das Internet ist fuer %s " "nicht freigeschaltet", child_name); return 0; } if (strcmp(tool, "websearch") == 0 && search_mode == 1) return 1; /* fall through to the approval path below */ } else if (auth == SP_AUTH_READ) return 1; load_rules(); if (whitelisted(tool, args)) return 1; if (approve_cmd[0] == '\0') { snprintf(reason, reason_len, "kein Erwachsener erreichbar " "(SPLINTER_KIDS_APPROVE_CMD nicht gesetzt)"); return 0; } return ask_parent(tool, args, reason, reason_len) ? 1 : 0; } /* * The day ends here. Not "the model is asked to stop" - the model is not called * at all, so a chatty model cannot talk past the budget. */ static int kids_block_turn(struct sp_host *h, char *msg, size_t len) { (void)h; if (quota_left() != 0) return 0; snprintf(msg, len, "Fuer heute ist Schluss: %ld von %u Minuten sind " "verbraucht. Morgen geht es weiter.", used_seconds / 60, daily_minutes); return 1; } static const struct sp_gate gate = { "kids", kids_gate, kids_block_turn }; /* * Kid-safe conversation: a system message in front of every request, because a * rule stated once at the start is the first thing a long conversation buries. */ static int kids_compact(struct sp_host *h, struct sp_conv *conv, size_t budget, unsigned turn) { struct sp_msg m; char text[768]; size_t at, i; (void)h; (void)budget; (void)turn; usage_tick(); if (conv == NULL || conv->n == 0) return 0; if (quota_left() == 0) { struct sp_msg end; char txt[256]; snprintf(txt, sizeof(txt), "[kids] Die Zeit fuer heute ist um (%u " "Minuten). Sage %s freundlich, dass fuer heute Schluss ist und " "es morgen weitergeht - und biete nichts weiter an.", daily_minutes, child_name); memset(&end, 0, sizeof(end)); end.role = "system"; end.content = txt; if (sp_conv_add(conv, &end) != 0) return -1; return 1; } for (i = 0; i < conv->n; i++) if (conv->v[i].role != NULL && strcmp(conv->v[i].role, "system") == 0 && conv->v[i].content != NULL && strstr(conv->v[i].content, "[kids]") != NULL) return 0; /* already told, once is enough */ snprintf(text, sizeof(text), "[kids] Du sprichst mit %s. Bleib freundlich, geduldig und " "altersgerecht: keine Gewalt, kein Grusel, keine Beleidigungen, keine " "Themen fuer Erwachsene. Erklaer einfache Dinge einfach, biete Hilfe " "beim Lernen an, und sage klar, wenn etwas nicht fuer Kinder ist. " "Aenderungen an Dateien oder Befehle fragt immer ein Erwachsener frei.", child_name); at = conv->n; for (i = conv->n; i > 0; i--) { if (conv->v[i - 1].role != NULL && strcmp(conv->v[i - 1].role, "user") == 0) { at = i - 1; break; } } memset(&m, 0, sizeof(m)); m.role = "system"; m.content = text; if (sp_conv_add(conv, &m) != 0) return -1; if (at < conv->n - 1) { struct sp_msg moved = conv->v[conv->n - 1]; memmove(&conv->v[at + 1], &conv->v[at], sizeof(conv->v[0]) * (conv->n - 1 - at)); conv->v[at] = moved; } return 1; } static const struct sp_compactor compactor = { "kids", kids_compact }; static int kids_init(struct sp_host *h) { const char *home = getenv("HOME"); const char *cmd = getenv("SPLINTER_KIDS_APPROVE_CMD"); const char *to = getenv("SPLINTER_KIDS_TIMEOUT"); const char *name = getenv("SPLINTER_KIDS_NAME"); if (home == NULL || *home == '\0') { (void)sp_host_note(h, "kids: no HOME"); return -1; } snprintf(allow_file, sizeof(allow_file), "%s/.splinter/kids/allow", home); snprintf(usage_file, sizeof(usage_file), "%s/.splinter/kids/usage", home); snprintf(domains_file, sizeof(domains_file), "%s/.splinter/kids/domains", home); { const char *sm = getenv("SPLINTER_KIDS_SEARCH"); if (sm != NULL && strcmp(sm, "allow") == 0) search_mode = 1; else if (sm != NULL && strcmp(sm, "deny") == 0) search_mode = 2; } { const char *min = getenv("SPLINTER_KIDS_DAILY_MINUTES"); if (min != NULL && *min != '\0') daily_minutes = (unsigned)strtoul(min, NULL, 10); } if (cmd != NULL && *cmd != '\0') snprintf(approve_cmd, sizeof(approve_cmd), "%s", cmd); if (name != NULL && *name != '\0') snprintf(child_name, sizeof(child_name), "%s", name); if (to != NULL && *to != '\0') timeout_s = (unsigned)strtoul(to, NULL, 10); if (sp_host_add_gate(h, &gate) != 0) return -1; if (sp_host_add_compactor(h, &compactor) != 0) return -1; (void)sp_host_note(h, approve_cmd[0] != '\0' ? "kids: parents reachable" : "kids: NO parent channel - everything " "mutating will be refused"); return 0; } static const struct sp_plugin plugin = { SP_PLUGIN_ABI, "kids", "1.0", kids_init, NULL, NULL }; const struct sp_plugin * sp_plugin_describe(void) { return &plugin; }