Przeglądaj źródła

Persist the chat session in /data when available

The Chat client now defaults its config file to /data/CHAT.CFG when a writable /data mount exists, so the server address and session cookie survive a reboot.  Without /data it keeps the old RAM-only /share/chat.cfg; an explicit 'chat <path>' argument still overrides both.

- ucmd_chat.c: pick the default path from the mount table, and copy a session left in /share/chat.cfg into /data once.
- AGENTS.md, docs: filesystems, network and userland chat/session paths.

make test-chat and make test-chat-e2e pass; the e2e run covers the /share fallback, saved-session relaunch and sign-out.
Johannes Zellner 1 tydzień temu
rodzic
commit
156bac7fc6
5 zmienionych plików z 52 dodań i 14 usunięć
  1. 8 5
      AGENTS.md
  2. 1 0
      docs/filesystems.html
  3. 7 4
      docs/network.html
  4. 1 1
      docs/userland.html
  5. 35 4
      ucmd_chat.c

+ 8 - 5
AGENTS.md

@@ -1312,8 +1312,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   is parsed and posted) and validating with `/api/me`.  External
   SSO and passkey accounts use the pasted-cookie screen.  The password is
   wiped after each attempt and never stored; the Chat host's cookie header
-  and the server go to `/share/chat.cfg` (or the given path) and are
-  re-saved when the server rolls the cookie.  `uhttp_cookie.c/h` is the jar
+  and the server go to `/data/CHAT.CFG` when the persistent `/data` mount
+  exists, otherwise `/share/chat.cfg` (or the given path), and are
+  re-saved when the server rolls the cookie.  A session left in the old
+  `/share/chat.cfg` is copied to `/data` once.  `uhttp_cookie.c/h` is the jar
   (24 host-only cookies, Path prefix match, Secure, Max-Age/1970-Expires
   deletion); uhttp stores every `Set-Cookie` (redirects too), sends matching
   cookies on each hop, splits requests/bodies into 1024-byte TLS records or
@@ -2240,9 +2242,10 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     TOTP; passkeys and external SSO need the pasted `connect.sid` cookie.
     The login depends on Cloudron's `cloudron-config` login API
     (`chat_login.c` isolates it).  TLS is not verified (the login screen
-    says so), and the saved cookie in `/share/chat.cfg` is a bearer
-    credential (RAM-only unless a FAT path is chosen; concurrent chat
-    windows share the file).  No threads, reactions, polls, search,
+    says so), and the saved cookie in `/data/CHAT.CFG` (or
+    `/share/chat.cfg` without a `/data` mount) is a bearer credential
+    (persistent on `/data`, RAM-only on `/share`; concurrent chat windows
+    share the file).  No threads, reactions, polls, search,
     attachments/images/GIFs, edits or deletes beyond what a reload shows;
     messages over 4 KB are truncated, history holds 240 messages, times are
     UTC and text is Latin-1.  Network calls block the UI for their duration.

+ 1 - 0
docs/filesystems.html

@@ -312,6 +312,7 @@
 			<li><strong>First install is destructive.</strong> Creating P4 formats the rest of the target disk; an existing <code>NEBOSDATA</code> volume is detected and left untouched.</li>
 			<li><strong>8.3 names.</strong> The FAT driver creates and reads short names only; long names need VFAT LFN write support.</li>
 			<li><strong>WiFi credentials.</strong> A successful WiFi connect stores the SSID/passphrase in <code>WIFI.CFG</code>; the boot-time <code>wifi autoconnect</code> helper reads it. The file is plaintext and 8.3, like the rest of the volume.</li>
+			<li><strong>Chat session.</strong> The Chat client stores its server and session cookie in <code>CHAT.CFG</code> when this volume is mounted; without it the client falls back to the RAM-only <code>/share/chat.cfg</code>. The cookie is a bearer credential, and the password is never stored.</li>
 			<li><strong>Fixed start.</strong> P4 starts at LBA 34816. A BIOS-only install leaves the space between P2 and P4 unpartitioned, and a later disk clone does not resize P4.</li>
 			<li><strong>No fsck.</strong> Like the other FAT mounts, the volume has no journal, repair tool or permissions; it is writable by every process.</li>
 		</ul>

+ 7 - 4
docs/network.html

@@ -427,9 +427,12 @@
 		<p>
 			The password lives only in the form field and the request buffer;
 			both are wiped after the attempt.  The Chat host's cookie header is
-			saved with the server address in <code>/share/chat.cfg</code>
-			(RAM-only; another path can be given as the argument or on the login
-			screen) and is re-saved when the server rolls the cookie.  The saved
+			saved with the server address in <code>/data/CHAT.CFG</code> when
+			the persistent <code>/data</code> volume is mounted, or
+			<code>/share/chat.cfg</code> (RAM-only) otherwise; another path can
+			be given as the argument or on the login screen, and a session left
+			in the old <code>/share</code> file is copied over once.  The
+			config is re-saved when the server rolls the cookie.  The saved
 			cookie is a bearer credential.  Passkey and external-SSO accounts
 			paste a <code>connect.sid</code> cookie from a signed-in browser
 			instead.
@@ -460,7 +463,7 @@
 					<tr><td>TLS certificates</td><td>Not verified</td><td>Encryption without authentication; MITM is undetected.</td></tr>
 					<tr><td>TLS DRBG</td><td>Weak (RDTSC/ticks/MAC)</td><td>Not suitable for high-value secrets.</td></tr>
 					<tr><td>WebDAV</td><td>No authentication</td><td>Anyone on the LAN can read mounts and write <code>/share</code>.</td></tr>
-					<tr><td>Chat session</td><td>Cookie saved in <code>/share/chat.cfg</code></td><td>A bearer credential in RAM until sign-out; the password is sent over unverified TLS and never stored.</td></tr>
+					<tr><td>Chat session</td><td>Cookie saved in <code>/data/CHAT.CFG</code>, or <code>/share/chat.cfg</code> without a <code>/data</code> mount</td><td>A bearer credential on disk while <code>/data</code> is mounted, otherwise RAM until sign-out; the password is sent over unverified TLS and never stored.</td></tr>
 					<tr><td>WiFi WPA2</td><td>PSK/CCMP, handshake in ring 3</td><td>The SNonce comes from the weak TLS DRBG. The PTK/replay are kept in the kernel (<code>SYS_WIFI_SET_PTK</code>/<code>SYS_WIFI_GET_PTK</code>) and fetched by the <code>wifi __keep</code> helper by connection ID for group rekeys; no WPA3/PMF.</td></tr>
 					<tr><td>Saved WiFi credentials</td><td>Plaintext <code>/data/WIFI.CFG</code></td><td>The SSID and passphrase are stored in an 8.3 FAT file so <code>wifi autoconnect</code> can reconnect at boot; anyone who can read the disk can read the passphrase. <code>wifi forget</code> removes it.</td></tr>
 					<tr><td>NTP</td><td>Unauthenticated SNTP</td><td>Anyone who can answer for the server name can set the clock.</td></tr>

+ 1 - 1
docs/userland.html

@@ -340,7 +340,7 @@
 		<ul>
 			<li><strong>files</strong> — browser for the VFS; opens text in <code>edit</code>, images in <code>view</code>, HTML in <code>browser</code>. First user of ngui.</li>
 			<li><strong>gallery</strong> — live widget and primitive showcase; the real app ships in the pack.</li>
-			<li><strong>chat</strong> — native Cloudron Chat client (top-bar <em>Chat</em> button, or <code>chat [config-path]</code>): username/password/TOTP login through the Cloudron OIDC API or a pasted session cookie, channels and DMs with unread badges and presence, history with older paging, sending, REST polling, read markers, browse/join and new DM.  The session cookie (never the password) is saved in <code>/share/chat.cfg</code> by default.  Keys: Enter sends, PgUp/PgDn scroll, Home at the top pages in older history, Ctrl+B browse, Ctrl+N new DM, Ctrl+R refresh, and with the composer unfocused (Esc) Up/Down switch conversations.  See <a href="network.html#chat">Network → Chat client</a>.</li>
+			<li><strong>chat</strong> — native Cloudron Chat client (top-bar <em>Chat</em> button, or <code>chat [config-path]</code>): username/password/TOTP login through the Cloudron OIDC API or a pasted session cookie, channels and DMs with unread badges and presence, history with older paging, sending, REST polling, read markers, browse/join and new DM.  The session cookie (never the password) is saved in <code>/data/CHAT.CFG</code> when the persistent volume is mounted, or in the RAM-only <code>/share/chat.cfg</code> otherwise.  Keys: Enter sends, PgUp/PgDn scroll, Home at the top pages in older history, Ctrl+B browse, Ctrl+N new DM, Ctrl+R refresh, and with the composer unfocused (Esc) Up/Down switch conversations.  See <a href="network.html#chat">Network → Chat client</a>.</li>
 			<li><strong>solitaire</strong> — Klondike with drag-and-drop, scoring and a win animation.</li>
 			<li><strong>mines</strong> — second ngui canvas app; shares the toolkit and event model.</li>
 			<li><strong>view</strong> — fit/1:1/zoom/pan image viewer, animated GIF playback.</li>

+ 35 - 4
ucmd_chat.c

@@ -3,7 +3,8 @@
  * Login is the native Cloudron OIDC driver in chat_login.c (username,
  * password, optional TOTP) or a pasted session cookie.  The session cookie is
  * kept in a uhttp cookie jar and saved with the server address in a small
- * config file (/share/chat.cfg by default, or the path given as argument).
+ * config file: /data/CHAT.CFG on an installed disk, /share/chat.cfg when
+ * there is no persistent /data mount, or the path given as argument.
  * The password is never stored.
  *
  * Everything else is the Chat REST API over uhttp: the channel and DM
@@ -25,7 +26,8 @@
 #include <stdlib.h>
 #include <string.h>
 
-#define DEFAULT_CFG    "/share/chat.cfg"
+#define DEFAULT_CFG_DATA  "/data/CHAT.CFG"
+#define DEFAULT_CFG_SHARE "/share/chat.cfg"
 #define RESP_CAP       (512u * 1024u)
 #define JSON_NODES     16384u
 #define JSON_SCAP      (RESP_CAP + 2u * JSON_NODES)
@@ -108,6 +110,7 @@ static enum modal modal = MODAL_NONE;
 static int action, action_arg;
 
 static char cfg_path[CHAT_PATH_MAX];
+static int cfg_is_default;
 static char server[CHAT_SERVER_MAX];
 static char server_host[UHTTP_HOST_MAX];
 static int server_secure;
@@ -342,6 +345,19 @@ static int write_file(const char *path, const char *buf, unsigned int len)
 	return u_syscall3(SYS_WRITE_FILE, (int)path, (int)buf, (int)len);
 }
 
+/* The persistent data volume wins when it is mounted and writable; the
+ * volatile /share is the fallback (live USB, or a board without /data). */
+static const char *default_cfg_path(void)
+{
+	struct sys_mount m;
+	unsigned int i;
+
+	for (i = 0; u_syscall2(SYS_LIST_MOUNTS, (int)i, (int)&m) == 1; i++)
+		if (strcmp(m.name, "data") == 0 && !m.ro)
+			return DEFAULT_CFG_DATA;
+	return DEFAULT_CFG_SHARE;
+}
+
 static int set_server(const char *s)
 {
 	char norm[CHAT_SERVER_MAX];
@@ -1311,8 +1327,16 @@ static void load_config(void)
 {
 	static char buf[CHAT_COOKIE_MAX + CHAT_SERVER_MAX + 256];
 	char srv[CHAT_SERVER_MAX];
-	int n = read_file(cfg_path, buf, sizeof(buf));
+	int n, migrated = 0;
 
+	n = read_file(cfg_path, buf, sizeof(buf));
+	/* First start on a persistent volume: carry a session that an older
+	 * build saved in /share into /data once. */
+	if (n <= 0 && cfg_is_default &&
+	    strcmp(cfg_path, DEFAULT_CFG_DATA) == 0) {
+		n = read_file(DEFAULT_CFG_SHARE, buf, sizeof(buf));
+		migrated = n > 0;
+	}
 	ngui_input_set(&cfg_in, cfg_path);
 	if (n <= 0 || chat_config_parse(buf, (unsigned int)n, srv, sizeof(srv),
 					saved_cookie,
@@ -1328,6 +1352,8 @@ static void load_config(void)
 		return;
 	uhttp_jar_import(&jar, server_host, server_secure, saved_cookie);
 	saved_jar_changes = jar.changes;
+	if (migrated)
+		save_config();
 	check_session(1);
 }
 
@@ -2461,7 +2487,12 @@ void _start(const char *arg)
 	pass_in.secret = 1;
 	totp_in.placeholder = "123456";
 	cookie_in.placeholder = "connect.sid=...";
-	s_copy(cfg_path, arg && arg[0] ? arg : DEFAULT_CFG, sizeof(cfg_path));
+	if (arg && arg[0]) {
+		s_copy(cfg_path, arg, sizeof(cfg_path));
+	} else {
+		s_copy(cfg_path, default_cfg_path(), sizeof(cfg_path));
+		cfg_is_default = 1;
+	}
 	ngui_set_focus(&ui, ID_SERVER);
 	last_input_ms = 0;