clavarium 0.2.243-next.1 → 0.2.243

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/config/launch/claude-code.sh +40 -19
  2. package/config/launch/codex.sh +37 -0
  3. package/config/launch/json-set.mjs +5 -2
  4. package/config/launch/mcp-relay.mjs +98 -0
  5. package/config/launch/mcp-sock.sh +31 -0
  6. package/config/launch/screen-watch.mjs +175 -0
  7. package/config/launch/toml-block.mjs +26 -0
  8. package/config/launch/tools-host.mjs +93 -0
  9. package/config/launch/vibe.sh +40 -0
  10. package/config/machine/clavarium-beat +44 -0
  11. package/config/machine/clavarium-beat-one +40 -0
  12. package/config/machine/clavarium-enter +60 -0
  13. package/config/tools.json +170 -12
  14. package/deploy/clavarium-host +385 -31
  15. package/deploy/clavarium-tunnel +14 -3
  16. package/deploy/clavarium.mjs +4 -0
  17. package/deploy/git-read +57 -5
  18. package/deploy/install.sh +11 -5
  19. package/deploy/maintenance.html +5 -2
  20. package/deploy/online.mjs +251 -8
  21. package/deploy/relay.sh +53 -27
  22. package/deploy/tunnel.mjs +7 -5
  23. package/deploy/updater.mjs +168 -25
  24. package/npm-shrinkwrap.json +18 -18
  25. package/package.json +6 -5
  26. package/server/agenda.mjs +140 -0
  27. package/server/api.mjs +384 -28
  28. package/server/apikeys.mjs +1372 -0
  29. package/server/beat.mjs +189 -0
  30. package/server/binders.mjs +42 -0
  31. package/server/boxcollect.mjs +276 -0
  32. package/server/boxes.mjs +348 -29
  33. package/server/bridge.mjs +13 -4
  34. package/server/browsers.mjs +364 -0
  35. package/server/capacity.mjs +37 -10
  36. package/server/carry.mjs +143 -0
  37. package/server/catalog.mjs +48 -0
  38. package/server/contactview.mjs +122 -0
  39. package/server/conversations.mjs +1122 -46
  40. package/server/copywork.mjs +34 -0
  41. package/server/demandes.mjs +251 -0
  42. package/server/desktop.mjs +102 -0
  43. package/server/devices.mjs +1 -0
  44. package/server/dictation-engines.mjs +91 -0
  45. package/server/dockets.mjs +58 -7
  46. package/server/door.mjs +19 -5
  47. package/server/events.mjs +3 -0
  48. package/server/features.mjs +366 -0
  49. package/server/files.mjs +43 -1
  50. package/server/gitaccess.mjs +12 -3
  51. package/server/gitchecks.mjs +17 -3
  52. package/server/gitkeeper.mjs +45 -10
  53. package/server/gitpush.mjs +286 -71
  54. package/server/gitrunner.mjs +144 -10
  55. package/server/grants.mjs +47 -11
  56. package/server/guide.mjs +31 -10
  57. package/server/health.mjs +138 -12
  58. package/server/jobs.mjs +138 -0
  59. package/server/linkguard.mjs +191 -0
  60. package/server/machinelist.mjs +58 -0
  61. package/server/mailbox.mjs +8 -0
  62. package/server/mailref.mjs +90 -0
  63. package/server/mailview.mjs +204 -0
  64. package/server/main.mjs +426 -48
  65. package/server/mcpoauth.mjs +338 -0
  66. package/server/mcpx.mjs +186 -0
  67. package/server/network.mjs +125 -13
  68. package/server/profile.mjs +129 -0
  69. package/server/relayjoin.mjs +67 -0
  70. package/server/resources.mjs +6 -0
  71. package/server/revive.mjs +70 -0
  72. package/server/rooms.mjs +496 -0
  73. package/server/secrets.mjs +25 -12
  74. package/server/serverlogs.mjs +30 -4
  75. package/server/settingswords.mjs +376 -0
  76. package/server/signin.mjs +25 -8
  77. package/server/signinhelp.mjs +253 -0
  78. package/server/speech.mjs +40 -4
  79. package/server/spoken.mjs +60 -0
  80. package/server/sshconsole.mjs +99 -0
  81. package/server/sshmachine.mjs +76 -0
  82. package/server/store.mjs +15 -1
  83. package/server/suggest.mjs +5 -1
  84. package/server/talk.mjs +229 -55
  85. package/server/tethers.mjs +223 -13
  86. package/server/toolbox.mjs +30 -7
  87. package/server/tools.mjs +38 -6
  88. package/server/updates.mjs +7 -2
  89. package/server/vm.mjs +215 -46
  90. package/server/vmwatch.mjs +82 -23
  91. package/server/voice.mjs +165 -51
  92. package/server/wakelearn.mjs +214 -0
  93. package/server/wakeups.mjs +42 -2
  94. package/server/watchdog.mjs +102 -0
  95. package/web/app.js +5491 -1242
  96. package/web/desk.js +531 -0
  97. package/web/device.html +15 -2
  98. package/web/features.js +480 -0
  99. package/web/index.html +877 -490
  100. package/web/kit.js +1142 -0
  101. package/web/links.js +2 -2
  102. package/web/mail-view.js +386 -0
  103. package/web/manifest.webmanifest +4 -0
  104. package/web/style.css +1554 -273
  105. package/web/voice-core.js +48 -0
  106. package/web/voice.js +13 -5
@@ -61,6 +61,26 @@ set -euo pipefail
61
61
  export LC_ALL=C
62
62
 
63
63
  MULTIPASS="multipass"
64
+ # SERVER LOGS: the journal reader, and how long it is given before what it said by then is shown.
65
+ JOURNALCTL="journalctl"
66
+ LOGS_SECONDS="60"
67
+ # THE WAY DOWN (vm halt, vm hardstop): how long each step is given before the next, in seconds --
68
+ # a polite stop, a stop by force asked of the VM manager, SIGTERM to the machine's own qemu, SIGKILL
69
+ # to it; then how long multipass may take to answer a list, and to see the machine stopped after.
70
+ HALT_POLITE="60"
71
+ HALT_FORCE="30"
72
+ HALT_TERM="5"
73
+ HALT_KILL="5"
74
+ HALT_LIST="20"
75
+ HALT_SETTLE="10"
76
+ # Where the qemu processes seen for each machine are noted (root's, in memory): a qemu ended but not
77
+ # reaped by a VM manager that hangs (a zombie) has no command line left to tell whose it was.
78
+ QEMU_SEEN="/run/clavarium/qemu"
79
+ # The bounds of the plain verbs (a VM manager that hangs must not hold a call for ever).
80
+ START_SECONDS="180"
81
+ STOP_SECONDS="120"
82
+ KILL_SECONDS="60"
83
+ SUSPEND_SECONDS="120"
64
84
  IMAGE="24.04"
65
85
  DISK="8G"
66
86
  LAUNCH_SECONDS="900"
@@ -82,11 +102,13 @@ SHELF_MEMORY="1536M"
82
102
  SHELF_STORE="/var/snap/multipass/common"
83
103
  SHELF_INSTANCES="$SHELF_STORE/data/multipassd/vault/instances"
84
104
  SHELF_CACHE="$SHELF_STORE/cache"
105
+ # The leases its DHCP server gave its machines: "<expiry> <mac> <ip> <name> <client id>" per line.
106
+ LEASES="$SHELF_STORE/data/multipassd/network/dnsmasq.leases"
85
107
  # Moved to another disk (deploy/move-machines.sh, Multipass's own way: its
86
108
  # MULTIPASS_STORAGE in a drop-in of its service): <path>/data, <path>/cache.
87
109
  # (No drop-in is the usual case: under pipefail a missing file must not stop this program.)
88
110
  MOVED_TO="$( { sed -n 's/^Environment=MULTIPASS_STORAGE=\(\/[A-Za-z0-9/._-]*\)$/\1/p' /etc/systemd/system/snap.multipass.multipassd.service.d/override.conf 2>/dev/null || true; } | head -1)"
89
- if [ -n "$MOVED_TO" ] && [ -d "$MOVED_TO/data" ]; then SHELF_STORE="$MOVED_TO"; SHELF_INSTANCES="$MOVED_TO/data/vault/instances"; SHELF_CACHE="$MOVED_TO/cache"; fi
111
+ if [ -n "$MOVED_TO" ] && [ -d "$MOVED_TO/data" ]; then SHELF_STORE="$MOVED_TO"; SHELF_INSTANCES="$MOVED_TO/data/vault/instances"; SHELF_CACHE="$MOVED_TO/cache"; LEASES="$MOVED_TO/data/network/dnsmasq.leases"; fi
90
112
  # Also the floor under a grown disk (grow_room): the server keeps the same
91
113
  # (MIN_FREE_MB in server/shelf.mjs, used by server/capacity.mjs).
92
114
  SHELF_MIN_FREE_MB="6144"
@@ -164,7 +186,7 @@ CADDYFILE="/etc/caddy/Caddyfile"
164
186
  MESH_CADDY="/etc/caddy/clavarium-meshcentral.caddy"
165
187
 
166
188
  refuse() { echo "REFUSED: $*" >&2; exit 78; }
167
- usage() { echo "usage: clavarium-host [--say-only [--data DIR] [--mnt DIR]] <vm|shelf|folder|work|view|tools|mounts|operator|machines|disks|net|git|server|update|wifi|meshcentral> <verb> [arguments]" >&2; exit 64; }
189
+ usage() { echo "usage: clavarium-host [--say-only [--data DIR] [--mnt DIR]] <vm|shelf|folder|work|view|tools|mounts|operator|machines|disks|net|git|server|update|wifi|meshcentral|relay> <verb> [arguments]" >&2; exit 64; }
168
190
 
169
191
  SAY="no"; DATA=""; MNT=""; TEST=""
170
192
  while [ $# -gt 0 ]; do
@@ -176,7 +198,9 @@ while [ $# -gt 0 ]; do
176
198
  TEST="${2:-}"; case "$TEST" in /*) : ;; *) refuse "--test takes an absolute path" ;; esac
177
199
  DATA="$TEST"; MNT="$TEST"; WIFI_STATE="$TEST/wifi"; NM_KEYFILES="$TEST/system-connections"; WIFI_WAIT="3"; WIFI_UP_WAIT="3"; WIFI_STEP="0.2"
178
200
  HAND_STATE="$TEST/network"; NETPLAN_DIR="$TEST/netplan"; HAND_WAIT="3"; SYS_NET="$TEST/sys-net"
179
- MESH_DATA="$TEST/mesh"; MESH_STATE="$TEST/meshcentral"; CADDYFILE="$TEST/Caddyfile"; MESH_CADDY="$TEST/clavarium-meshcentral.caddy"; shift 2 ;;
201
+ MESH_DATA="$TEST/mesh"; MESH_STATE="$TEST/meshcentral"; CADDYFILE="$TEST/Caddyfile"; MESH_CADDY="$TEST/clavarium-meshcentral.caddy"
202
+ HALT_POLITE="2"; HALT_FORCE="2"; HALT_TERM="1"; HALT_KILL="2"; HALT_LIST="2"; HALT_SETTLE="2"; QEMU_SEEN="$TEST/qemu"
203
+ JOURNALCTL="$TEST/journalctl"; LOGS_SECONDS="2"; shift 2 ;;
180
204
  *) break ;;
181
205
  esac
182
206
  done
@@ -217,12 +241,163 @@ vm_of() { is_name "${1#clv-}" && [ "$1" != "${1#clv-}" ] || refuse "not one of o
217
241
  vm_qemu_pids() {
218
242
  local want="$1" pid vm
219
243
  for pid in $(pgrep qemu-system 2>/dev/null || true); do
244
+ # A process that has ended but not yet been reaped (a zombie) holds no memory: it is gone.
245
+ [ "$(sed -n 's/^State:[[:space:]]*\([A-Z]\).*/\1/p' "/proc/$pid/status" 2>/dev/null)" = "Z" ] && continue
220
246
  vm="$(tr '\0' '\n' < "/proc/$pid/cmdline" 2>/dev/null | grep -oE '/instances/clv(shelf)?-[a-z0-9-]+/' | head -1 | cut -d/ -f3)" || vm=""
221
247
  [ "$vm" = "$want" ] && printf '%s\n' "$pid"
222
248
  done
249
+ # (Its status is never that of the last process looked at: callers assign it under set -e.)
250
+ return 0
223
251
  }
224
252
  # No qemu left for this machine after a short grace: its RAM has been freed.
225
253
  vm_freed() { local v="$1" i; for i in $(seq 1 24); do [ -z "$(vm_qemu_pids "$v")" ] && return 0; sleep 0.5; done; return 1; }
254
+ # THIS machine's qemu, PROVEN just before a signal: a process named qemu-system* whose command line
255
+ # holds a disk in this instance's own directory. Read again right before each signal, so that a
256
+ # process id reused meanwhile, or another machine's qemu, is never hit.
257
+ vm_qemu_is() {
258
+ local pid="$1" want="$2" comm vm
259
+ [[ "$pid" =~ ^[0-9]+$ ]] || return 1
260
+ comm="$(cat "/proc/$pid/comm" 2>/dev/null)" || return 1
261
+ [[ "$comm" == qemu-system* ]] || return 1
262
+ vm="$(tr '\0' '\n' < "/proc/$pid/cmdline" 2>/dev/null | grep -oE '/instances/clv(shelf)?-[a-z0-9-]+/' | head -1 | cut -d/ -f3)" || return 1
263
+ [ "$vm" = "$want" ]
264
+ }
265
+ # The memory its qemu holds (kB, its resident set), counted before the way down: what is freed.
266
+ vm_rss_kb() { local pid kb total=0; for pid in "$@"; do kb="$(sed -n 's/^VmRSS:[[:space:]]*\([0-9]*\) kB$/\1/p' "/proc/$pid/status" 2>/dev/null)" || kb=""; total=$((total + ${kb:-0})); done; echo "$total"; }
267
+ gb_of_kb() { awk -v k="$1" 'BEGIN { printf "%.1fG", k / 1048576 }'; }
268
+ # What multipass says of it, bounded: its state, "absent" (listed, not there), or "unknown" (mute).
269
+ vm_state_of() {
270
+ local out
271
+ out="$(timeout "$HALT_LIST" "$MULTIPASS" list --format csv 2>/dev/null)" || { echo "unknown"; return 0; }
272
+ out="$(printf '%s\n' "$out" | awk -F, -v n="$1" '$1 == n { print $2; exit }')"
273
+ echo "${out:-absent}"
274
+ }
275
+ # A process's state letter (R, S, D, T stopped, t traced, Z ended but not reaped), or nothing when gone.
276
+ proc_state() { sed -n 's/^State:[[:space:]]*\([A-Za-z]\).*/\1/p' "/proc/$1/status" 2>/dev/null || true; }
277
+ # Still running (frozen included): its /proc entry is there and it is not a zombie.
278
+ pid_alive() { local st; [ -d "/proc/$1" ] || return 1; st="$(proc_state "$1")"; [ -n "$st" ] && [ "$st" != "Z" ]; }
279
+ # Its qemu, noted as seen (vm_qemu_unreaped reads it back).
280
+ vm_qemu_note() {
281
+ local v="$1" pids
282
+ pids="$(vm_qemu_pids "$v")"
283
+ [ -n "$pids" ] || return 0
284
+ install -d -m 0700 "$QEMU_SEEN" 2>/dev/null || return 0
285
+ printf '%s\n' $pids > "$QEMU_SEEN/$v" 2>/dev/null || true
286
+ }
287
+ # A qemu of it that has ended but that the VM manager has not reaped (a zombie: it holds no memory,
288
+ # and no signal can do more to it). The VM manager is the one stuck.
289
+ vm_qemu_unreaped() {
290
+ local v="$1" pid
291
+ [ -r "$QEMU_SEEN/$v" ] || return 0
292
+ while read -r pid; do
293
+ [[ "$pid" =~ ^[0-9]+$ ]] || continue
294
+ [ "$(proc_state "$pid")" = "Z" ] && [[ "$(cat "/proc/$pid/comm" 2>/dev/null)" == qemu-system* ]] && printf '%s\n' "$pid"
295
+ done < "$QEMU_SEEN/$v"
296
+ return 0
297
+ }
298
+ # A qemu of it that is FROZEN (stopped, or traced): it answers nothing, a suspend asked of it hangs the VM manager.
299
+ vm_qemu_frozen() { local pid; for pid in $(vm_qemu_pids "$1"); do [[ "$(proc_state "$pid")" == [Tt] ]] && return 0; done; return 1; }
300
+ # NOTHING LEFT TO STOP -- the same proof as the signals: no live qemu of it (after a grace of $2
301
+ # seconds), none ended and left unreaped, and the VM manager saying Stopped (or not there, or not
302
+ # answering: then there is nothing more it can do). Asleep, or anything else (Suspending, Running,
303
+ # Unknown...) with no qemu: not gone -- the steps go on, and the check says what is wrong.
304
+ halt_gone() {
305
+ local v="$1" i state
306
+ for i in $(seq 0 "$2"); do [ -z "$(vm_qemu_pids "$v")" ] && break; [ "$i" -lt "$2" ] && sleep 1; done
307
+ [ -z "$(vm_qemu_pids "$v")" ] || return 1
308
+ [ -z "$(vm_qemu_unreaped "$v")" ] || return 1
309
+ state="$(vm_state_of "$v")"
310
+ [ "$state" = "Stopped" ] || [ "$state" = "absent" ] || [ "$state" = "unknown" ]
311
+ }
312
+ # One step that asks the VM manager, in the background, so that a TERM of this program (the server
313
+ # moving on: "Force now") ends it at once instead of leaving it to run its whole bound.
314
+ HALT_CHILD=""
315
+ halt_run() { local rc=0; "$@" >/dev/null 2>&1 & HALT_CHILD=$!; wait "$HALT_CHILD" || rc=$?; HALT_CHILD=""; return "$rc"; }
316
+ # A signal to its qemu, each process proven first; then waited for, up to $3 seconds, by each
317
+ # process's own /proc entry (gone, or ended) and by its name again. A FROZEN qemu (stopped: SIGSTOP,
318
+ # or traced) keeps a TERM pending until it runs: it is let run (SIGCONT) right after. A signal
319
+ # refused is said on the error output.
320
+ halt_signal() {
321
+ local v="$1" sig="$2" wait="$3" pid i st pids left
322
+ pids="$(vm_qemu_pids "$v")"
323
+ for pid in $pids; do
324
+ vm_qemu_is "$pid" "$v" || continue
325
+ st="$(proc_state "$pid")"
326
+ kill "-$sig" "$pid" 2>/dev/null || echo "signal $sig to $pid refused" >&2
327
+ if [[ "$st" == [Tt] ]]; then kill -CONT "$pid" 2>/dev/null || echo "signal CONT to $pid refused" >&2; fi
328
+ done
329
+ for i in $(seq 1 $((wait * 4))); do
330
+ left=""; for pid in $pids; do pid_alive "$pid" && left="$left $pid"; done
331
+ [ -z "$left" ] && [ -z "$(vm_qemu_pids "$v")" ] && return 0
332
+ sleep 0.25
333
+ done
334
+ return 1
335
+ }
336
+ # THE WAY DOWN, from step $2 to step $3 (polite, force, term, kill), then the check. One line per
337
+ # step on the output, for the server and its page:
338
+ # step <id> start
339
+ # step <id> <ok|fail|timeout|skip> <seconds>s [why]
340
+ # step check <ok|fail> <seconds>s [qemu-left|mute|state <S>] freed <G>
341
+ # Before each step: its qemu already gone (and the machine not asleep), the rest is skipped.
342
+ # Exit 0 only when no qemu is left for it and multipass says Stopped.
343
+ halt_steps() {
344
+ local v="$1" from="$2" until="$3" kb s on="no" t rc res grace=0 state
345
+ trap '[ -n "$HALT_CHILD" ] && kill "$HALT_CHILD" 2>/dev/null; exit 143' TERM INT
346
+ vm_qemu_note "$v"
347
+ # shellcheck disable=SC2046
348
+ kb="$(vm_rss_kb $(vm_qemu_pids "$v"))"
349
+ for s in polite force term kill; do
350
+ [ "$s" = "$from" ] && on="yes"
351
+ [ "$on" = "yes" ] || continue
352
+ if halt_gone "$v" "$grace"; then echo "step $s skip 0s gone"; break; fi
353
+ grace=0
354
+ # Nothing alive to signal: said why (a qemu ended but left unreaped: the VM manager is stuck).
355
+ if { [ "$s" = "term" ] || [ "$s" = "kill" ]; } && [ -z "$(vm_qemu_pids "$v")" ]; then
356
+ if [ -n "$(vm_qemu_unreaped "$v")" ]; then echo "step $s skip 0s unreaped"; else echo "step $s skip 0s none"; fi
357
+ [ "$s" = "$until" ] && break; continue
358
+ fi
359
+ echo "step $s start"; t=$SECONDS; rc=0
360
+ case "$s" in
361
+ polite) if [ -z "$(vm_qemu_pids "$v")" ] && [ "$(vm_state_of "$v")" = "Suspended" ]; then echo "step polite skip 0s asleep"; [ "$s" = "$until" ] && break; continue; fi
362
+ halt_run timeout "$HALT_POLITE" "$MULTIPASS" stop "$v" || rc=$? ;;
363
+ force) halt_run timeout "$HALT_FORCE" "$MULTIPASS" stop --force "$v" || rc=$? ;;
364
+ term|kill)
365
+ if [ "$s" = "term" ]; then halt_signal "$v" TERM "$HALT_TERM" || rc=124; else halt_signal "$v" KILL "$HALT_KILL" || rc=124; fi ;;
366
+ esac
367
+ case "$rc" in 0) res="ok"; grace=3 ;; 124) res="timeout" ;; *) res="fail" ;; esac
368
+ echo "step $s $res $((SECONDS - t))s"
369
+ [ "$s" = "$until" ] && break
370
+ done
371
+ echo "step check start"; t=$SECONDS
372
+ for _ in $(seq 0 "$HALT_SETTLE"); do
373
+ state="$(vm_state_of "$v")"
374
+ [ -z "$(vm_qemu_pids "$v")" ] && [ -z "$(vm_qemu_unreaped "$v")" ] && [ "$state" = "Stopped" ] && { echo "step check ok $((SECONDS - t))s freed $(gb_of_kb "$kb")"; exit 0; }
375
+ # A VM manager that does not answer is not asked again and again.
376
+ [ "$state" = "unknown" ] && break
377
+ sleep 1
378
+ done
379
+ if [ -n "$(vm_qemu_pids "$v")" ]; then echo "step check fail $((SECONDS - t))s qemu-left freed 0.0G"
380
+ elif [ -n "$(vm_qemu_unreaped "$v")" ]; then echo "step check fail $((SECONDS - t))s qemu-unreaped freed $(gb_of_kb "$kb")"
381
+ elif [ "$state" = "unknown" ]; then echo "step check fail $((SECONDS - t))s mute freed $(gb_of_kb "$kb")"
382
+ else echo "step check fail $((SECONDS - t))s state $state freed $(gb_of_kb "$kb")"; fi
383
+ exit 1
384
+ }
385
+ halt_plan() {
386
+ local v="$1" from="$2" until="$3" s on="no"
387
+ echo "before each step: if no qemu is left for $v (and it is not asleep), straight to the check"
388
+ for s in polite force term kill; do
389
+ [ "$s" = "$from" ] && on="yes"
390
+ [ "$on" = "yes" ] || continue
391
+ case "$s" in
392
+ polite) echo "step polite: timeout $HALT_POLITE $MULTIPASS stop $v" ;;
393
+ force) echo "step force: timeout $HALT_FORCE $MULTIPASS stop --force $v" ;;
394
+ term) echo "step term: kill -TERM <the qemu of $v, proven by its disk>, waited ${HALT_TERM}s" ;;
395
+ kill) echo "step kill: kill -KILL <the qemu of $v, proven by its disk>, waited ${HALT_KILL}s" ;;
396
+ esac
397
+ [ "$s" = "$until" ] && break
398
+ done
399
+ echo "step check: no qemu left for $v, and $MULTIPASS says Stopped (within ${HALT_SETTLE}s)"
400
+ }
226
401
  number() { [[ "$1" =~ ^[0-9]{1,6}$ ]] || refuse "not a number: ${1:-<empty>}"; }
227
402
  # A disk or partition named by its device: /dev/sdX, /dev/vdX, /dev/nvmeNnN(pN),
228
403
  # /dev/xvdX, /dev/mmcblkN(pN), with a digit or two at the end; a loop device only
@@ -735,7 +910,9 @@ AREA="$1"; VERB="$2"; shift 2
735
910
  # the server's account (its probes every few seconds made some 800 lines in ten minutes, 31-10). Reading
736
911
  # verbs, and commands run inside a machine (the server's own probes, mostly), are not written.
737
912
  case "$AREA $VERB" in
738
- "vm exec"|"vm ping"|"vm states"|"vm info"|"server logs"|"server room"|"server usage"|"shelf info"|"shelf room"|"shelf list"|"shelf size"|"shelf exec"|"git read"|"work room"|"disks list"|"disks room"|"machines places"|"machines moving"|"machines old"|"net taps"|"wifi status"|"wifi scan"|"path held"|"disk room"|"update check"|"meshcentral status"|"operator "*) : ;;
913
+ "vm exec"|"vm ping"|"vm states"|"vm info"|"server logs"|"server room"|"server usage"|"shelf info"|"shelf room"|"shelf list"|"shelf size"|"shelf exec"|"git read"|"work room"|"disks list"|"disks room"|"machines places"|"machines moving"|"machines old"|"net taps"|"net leases"|"wifi status"|"wifi scan"|"path held"|"disk room"|"update check"|"meshcentral status"|"operator "*) : ;;
914
+ # A one-time code is not written down, even spent.
915
+ "relay join") [ "$SAY" = yes ] || logger -t clavarium-host -- "relay join (a home asks to join the relay)" 2>/dev/null || true ;;
739
916
  *) [ "$SAY" = yes ] || logger -t clavarium-host -- "$AREA $VERB$(printf ' %.80s' "$@" | head -c 400)" 2>/dev/null || true ;;
740
917
  esac
741
918
 
@@ -772,12 +949,15 @@ case "$AREA $VERB" in
772
949
  run "$MULTIPASS" exec "$vm" -- "$@" ;;
773
950
  "vm ping")
774
951
  # Does the VM manager answer at all? Asked now and then by the server
775
- # (vmwatch.mjs); bounded, so that a VM manager that hangs is an answer.
952
+ # (vmwatch.mjs); bounded, so that a VM manager that hangs is an answer:
953
+ # mute for 30 s is mute.
776
954
  [ $# -eq 0 ] || usage
777
- run timeout 90 "$MULTIPASS" list --format csv ;;
955
+ run timeout 30 "$MULTIPASS" list --format csv ;;
778
956
  "vm revive")
779
- # The VM manager has not answered for minutes: its daemon restarted.
780
- # Asked by the server at most once in hours (vmwatch.mjs), never by a page.
957
+ # The VM manager has not answered for minutes: its daemon restarted
958
+ # (every machine is suspended and resumed). Asked by the server as its
959
+ # owner chose (vmwatch.mjs: at night, always, or never by itself), at
960
+ # most once in hours; or by its owner, confirmed in Settings, Machines.
781
961
  [ $# -eq 0 ] || usage
782
962
  run timeout 170 snap restart multipass.multipassd ;;
783
963
  "vm states")
@@ -790,9 +970,31 @@ case "$AREA $VERB" in
790
970
  "vm info")
791
971
  [ $# -eq 1 ] || usage; vm_of "$1"
792
972
  run "$MULTIPASS" info "$1" --format json ;;
793
- "vm start"|"vm stop")
973
+ "vm start")
974
+ [ $# -eq 1 ] || usage; vm_of "$1"
975
+ run timeout "$START_SECONDS" "$MULTIPASS" start "$1" ;;
976
+ "vm stop")
794
977
  [ $# -eq 1 ] || usage; vm_of "$1"
795
- run "$MULTIPASS" "$VERB" "$1" ;;
978
+ run timeout "$STOP_SECONDS" "$MULTIPASS" stop "$1" ;;
979
+ "vm halt")
980
+ # <vm> [<from> [<until>]]: STOPPED FOR SURE, step by step (halt_steps): a polite stop, a stop
981
+ # by force, then its own qemu ended -- SIGTERM, SIGKILL -- without the VM manager, which may be
982
+ # the thing that hangs. <from>: where to begin (the server moves on with it, "Force now");
983
+ # <until>: the last step tried before the check (force: the VM manager's own ways only).
984
+ [ $# -ge 1 ] && [ $# -le 3 ] || usage; vm_of "$1"
985
+ from="${2:-polite}"; until="${3:-kill}"
986
+ [[ "$from" =~ ^(polite|force|term|kill)$ ]] || refuse "not a step: $from"
987
+ [[ "$until" =~ ^(force|kill)$ ]] || refuse "not a last step: $until"
988
+ [ "$from" = "kill" ] && [ "$until" = "force" ] && refuse "kill comes after force"
989
+ [ "$from" = "term" ] && [ "$until" = "force" ] && refuse "term comes after force"
990
+ if [ "$SAY" = "yes" ]; then halt_plan "$1" "$from" "$until"; exit 0; fi
991
+ halt_steps "$1" "$from" "$until" ;;
992
+ "vm hardstop")
993
+ # <vm>: "Force stop", as a power cut: nothing is asked of the VM manager. Its own qemu,
994
+ # proven by its disk, is sent SIGTERM, then SIGKILL; then the check, as for vm halt.
995
+ [ $# -eq 1 ] || usage; vm_of "$1"
996
+ if [ "$SAY" = "yes" ]; then halt_plan "$1" term kill; exit 0; fi
997
+ halt_steps "$1" term kill ;;
796
998
  "vm suspend")
797
999
  # Veille: suspend must save the machine's state AND free its RAM by ending
798
1000
  # its qemu process. Multipass saves the state reliably but at times leaves
@@ -804,17 +1006,25 @@ case "$AREA $VERB" in
804
1006
  # gone after the first suspend and none of this runs.
805
1007
  [ $# -eq 1 ] || usage; vm_of "$1"
806
1008
  if [ "$SAY" = "yes" ]; then
807
- printf '%s\n' "$MULTIPASS suspend $1" \
1009
+ printf '%s\n' "timeout $SUSPEND_SECONDS $MULTIPASS suspend $1" \
808
1010
  "if its qemu still holds the RAM: $MULTIPASS start $1 && $MULTIPASS suspend $1 (wake+resleep)" \
809
1011
  "if it still does: $MULTIPASS stop $1 (free it; it wakes by a boot)"
810
1012
  exit 0
811
1013
  fi
812
- "$MULTIPASS" suspend "$1" || refuse "suspend of $1 failed"
813
- vm_freed "$1" && exit 0
814
- "$MULTIPASS" start "$1" >/dev/null 2>&1 && "$MULTIPASS" suspend "$1" >/dev/null 2>&1
815
- vm_freed "$1" && exit 0
816
- "$MULTIPASS" stop "$1" >/dev/null 2>&1 || true
817
- vm_freed "$1" && exit 0
1014
+ # FROZEN (its qemu stopped): a suspend would hang the VM manager ("Suspending" for good). Refused
1015
+ # at once: what frees it is Force stop.
1016
+ if vm_qemu_frozen "$1"; then echo "step suspend start"; echo "step suspend fail 0s frozen"; refuse "its qemu is frozen: a suspend would hang the VM manager; use Force stop"; fi
1017
+ # One line per step, as vm halt says them (the server shows them to the page).
1018
+ echo "step suspend start"; t=$SECONDS
1019
+ timeout "$SUSPEND_SECONDS" "$MULTIPASS" suspend "$1" || { echo "step suspend fail $((SECONDS - t))s"; refuse "suspend of $1 failed"; }
1020
+ vm_freed "$1" && { echo "step suspend ok $((SECONDS - t))s"; exit 0; }
1021
+ echo "step suspend ok $((SECONDS - t))s qemu-left"; echo "step resleep start"; t=$SECONDS
1022
+ timeout "$START_SECONDS" "$MULTIPASS" start "$1" >/dev/null 2>&1 && timeout "$SUSPEND_SECONDS" "$MULTIPASS" suspend "$1" >/dev/null 2>&1
1023
+ vm_freed "$1" && { echo "step resleep ok $((SECONDS - t))s"; exit 0; }
1024
+ echo "step resleep fail $((SECONDS - t))s"; echo "step stop start"; t=$SECONDS
1025
+ timeout "$STOP_SECONDS" "$MULTIPASS" stop "$1" >/dev/null 2>&1 || true
1026
+ vm_freed "$1" && { echo "step stop ok $((SECONDS - t))s"; exit 0; }
1027
+ echo "step stop fail $((SECONDS - t))s"
818
1028
  refuse "suspended $1 but its memory was not freed (its qemu stayed); the VM manager likely needs a restart" ;;
819
1029
  "vm reclaim")
820
1030
  # Free a machine that is ASLEEP but whose qemu still holds the RAM -- a
@@ -830,6 +1040,8 @@ case "$AREA $VERB" in
830
1040
  fi
831
1041
  # Each step that frees the RAM says how, so the server can tell the owner.
832
1042
  vm_freed "$1" && { echo "freed-already"; exit 0; }
1043
+ # Frozen: waking it and putting it back to sleep would hang the VM manager.
1044
+ vm_qemu_frozen "$1" && refuse "its qemu is frozen: use Force stop"
833
1045
  "$MULTIPASS" start "$1" >/dev/null 2>&1 && "$MULTIPASS" suspend "$1" >/dev/null 2>&1
834
1046
  vm_freed "$1" && { echo "freed-resleep"; exit 0; }
835
1047
  "$MULTIPASS" stop "$1" >/dev/null 2>&1 || true
@@ -839,7 +1051,7 @@ case "$AREA $VERB" in
839
1051
  # A stop that does not wait for the system inside: asked only after a
840
1052
  # clean stop hung or failed (a restart the owner asked for).
841
1053
  [ $# -eq 1 ] || usage; vm_of "$1"
842
- run "$MULTIPASS" stop --force "$1" ;;
1054
+ run timeout "$KILL_SECONDS" "$MULTIPASS" stop --force "$1" ;;
843
1055
  "vm set")
844
1056
  # <vm> memory <megabytes> | <vm> disk <gigabytes> | <vm> cpus <n>. The VM manager
845
1057
  # takes it only from a stopped machine, and a disk only ever grows:
@@ -898,6 +1110,43 @@ case "$AREA $VERB" in
898
1110
  *) refuse "a mount is of a folder, of a view, of a work folder or of the tools" ;;
899
1111
  esac
900
1112
  run "$MULTIPASS" mount -u "$4:$VM_ID" -g "$5:$VM_ID" "$source" "$target" ;;
1113
+ "vm share"|"vm unshare")
1114
+ # A SHARED MACHINE (#221): the conversations that live in it, kept here (DATA/shared/<vm>), so that a
1115
+ # work folder or a resource only ever goes into the machine its conversation lives in. <vm> <conversation>
1116
+ [ $# -eq 2 ] || usage; vm_of "$1"; is_name "$2" || refuse "not a conversation name: $2"
1117
+ [ "$1" != "clv-$2" ] || refuse "a conversation of its own is not shared"
1118
+ list="$DATA/shared/$1"
1119
+ if [ "$SAY" = "yes" ]; then printf '%s %s %s\n' "$VERB" "$list" "$2"; exit 0; fi
1120
+ mkdir -p "$DATA/shared"; touch "$list"
1121
+ if [ "$VERB" = "share" ]; then
1122
+ grep -qx "$2" "$list" || printf '%s\n' "$2" >> "$list"
1123
+ else
1124
+ grep -vx "$2" "$list" > "$list.new" || true; mv "$list.new" "$list"
1125
+ fi
1126
+ exit 0 ;;
1127
+ "vm smount")
1128
+ # <vm> <conversation> <work|folder|view> <resource> <uid> <gid> <its uid> <its gid>: into the conversation's
1129
+ # own place in a shared machine (/srv/clavarium/<conversation>/...), where its door (clavarium-enter) finds
1130
+ # it, owned there by ITS user (not the machine's admin) -- only for a conversation that lives in that machine.
1131
+ [ $# -eq 8 ] || usage; vm_of "$1"; is_name "$2" || refuse "not a conversation name: $2"; is_name "$4" || refuse "not a folder name: $4"; number "$5"; number "$6"; number "$7"; number "$8"
1132
+ [ "$7" -ge 1001 ] && [ "$8" -ge 1001 ] || refuse "its own user is not the machine's admin"
1133
+ [ -f "$DATA/shared/$1" ] && grep -qx "$2" "$DATA/shared/$1" || refuse "$2 does not live in $1"
1134
+ case "$3" in
1135
+ work) [ "$4" = "$2" ] || refuse "a work folder belongs to its own conversation"
1136
+ kept work; source="$MNT/work/$2"; held "$source"
1137
+ [ "$SAY" = "yes" ] || mountpoint -q "$source" || refuse "the work disk of $2 is not in place"
1138
+ target="$1:/srv/clavarium/$2/work" ;;
1139
+ folder) folder_of "$4"; source="$MNT/resources/$4"; target="$1:/srv/clavarium/$2/resources/$4" ;;
1140
+ view) kept views; source="$MNT/views/$4"; held "$source"
1141
+ [ "$SAY" = "yes" ] || mountpoint -q "$source" || refuse "the read-only view of $4 is not in place"
1142
+ target="$1:/srv/clavarium/$2/resources/$4" ;;
1143
+ *) refuse "a shared mount is of a work folder, a folder or a view" ;;
1144
+ esac
1145
+ run "$MULTIPASS" mount -u "$5:$7" -g "$6:$8" "$source" "$target" ;;
1146
+ "vm sumount")
1147
+ # <vm> <conversation> <work|resource name>: taken out of its place in a shared machine.
1148
+ [ $# -eq 3 ] || usage; vm_of "$1"; is_name "$2" || refuse "not a conversation name: $2"; is_name "$3" || refuse "not a name: $3"
1149
+ if [ "$3" = "work" ]; then run "$MULTIPASS" umount "$1:/srv/clavarium/$2/work"; else run "$MULTIPASS" umount "$1:/srv/clavarium/$2/resources/$3"; fi ;;
901
1150
  "vm unwork")
902
1151
  # The work folder is taken out of its machine (its work moves to the
903
1152
  # machine's own disk, or a move is undone).
@@ -971,21 +1220,41 @@ case "$AREA $VERB" in
971
1220
  # source, over a span; read-only, bounded. The page masks what looks
972
1221
  # like a secret before showing it.
973
1222
  # server logs <clavarium|machines|updates|web|signin|tunnel|system> <10m|1|6|24|168|boot>
1223
+ # 17-11: a journal flooded by sudo (a line a second, the server's own calls of this program)
1224
+ # took more than the page waits. The server's unit is read by indexed fields, its own output
1225
+ # only (stdout), not sudo's syslog lines; sudo and PAM session lines are left out of any
1226
+ # source; the read is given LOGS_SECONDS, then what it said by then is shown, with a NOTE
1227
+ # on stderr -- never a bare failure.
974
1228
  [ $# -eq 2 ] || usage
1229
+ # A unit by its fields: what it wrote, and what systemd said of it.
1230
+ unit() { printf '%s\n' "_SYSTEMD_UNIT=$1" + "UNIT=$1" "_PID=1" +; }
975
1231
  case "$1" in
976
- clavarium) units=(-u clavarium.service -u 'clavarium-view-*' -u clavarium-tools-view.service -u clavarium-move-machines.service) ;;
977
- machines) units=(-u snap.multipass.multipassd.service) ;;
978
- updates) units=(-u clavarium-update.service -u clavarium-certificate.service) ;;
979
- web) units=(-u caddy.service -u oauth2-proxy.service) ;;
980
- signin) units=(-u dex.service) ;;
981
- tunnel) units=(-u clavarium-tunnel.service) ;;
982
- system) units=(-k) ;;
1232
+ clavarium) matches=(_SYSTEMD_UNIT=clavarium.service _TRANSPORT=stdout _TRANSPORT=journal + UNIT=clavarium.service _PID=1 +)
1233
+ views=()
1234
+ if [ "$SAY" = "no" ]; then mapfile -t views < <(systemctl list-units --all --plain --no-legend 'clavarium-view-*' 2>/dev/null | awk '$1 ~ /^clavarium-view-[A-Za-z0-9_.@-]+$/ {print $1}' | head -50); fi
1235
+ for u in "${views[@]}" clavarium-tools-view.service clavarium-move-machines.service; do mapfile -t -O "${#matches[@]}" matches < <(unit "$u"); done
1236
+ unset 'matches[-1]' ;;
1237
+ machines) matches=(-u snap.multipass.multipassd.service) ;;
1238
+ updates) matches=(-u clavarium-update.service -u clavarium-certificate.service) ;;
1239
+ web) matches=(-u caddy.service -u oauth2-proxy.service) ;;
1240
+ signin) matches=(-u dex.service) ;;
1241
+ tunnel) matches=(-u clavarium-tunnel.service) ;;
1242
+ system) matches=(-k) ;;
983
1243
  *) refuse "not a source of logs: $1" ;;
984
1244
  esac
985
1245
  case "$2" in 10m) since=(--since "-10min") ;; 1|6|24|168) since=(--since "-$2h") ;; boot) since=(-b) ;; *) refuse "not a span: $2 (10m, 1, 6, 24, 168 hours, or boot)" ;; esac
986
1246
  # The newest 20000 lines at most: more is too much to read on a page, and to send.
987
- if [ "$SAY" = "yes" ]; then echo "journalctl ${units[*]} ${since[*]} -o short-iso --no-pager (the last 20000 lines)"; exit 0; fi
988
- journalctl "${units[@]}" "${since[@]}" -o short-iso --no-pager -n 20000 2>/dev/null | cut -c1-2000 || true
1247
+ if [ "$SAY" = "yes" ]; then echo "journalctl ${matches[*]} ${since[*]} -o short-iso --no-pager -q -n 20000 (sudo and PAM session lines left out; ${LOGS_SECONDS}s at most)"; exit 0; fi
1248
+ out="$(mktemp)"; err="$(mktemp)"
1249
+ trap 'rm -f -- "$out" "$err"' EXIT
1250
+ code=0
1251
+ timeout -k 5 "$LOGS_SECONDS" "$JOURNALCTL" "${matches[@]}" "${since[@]}" -o short-iso --no-pager -q -n 20000 >"$out" 2>"$err" || code=$?
1252
+ grep -avE ' (sudo|pkexec)\[[0-9]+\]: | pam_unix\((sudo|sudo-i|pkexec):session\)' "$out" | cut -c1-2000 || true
1253
+ case "$code" in
1254
+ 0) : ;;
1255
+ 124|137) echo "NOTE: the journal took more than ${LOGS_SECONDS}s: what it said by then" >&2 ;;
1256
+ *) echo "NOTE: journalctl ended with $code: $(grep -av '^$' "$err" | tail -n 1 | cut -c1-200)" >&2 ;;
1257
+ esac
989
1258
  exit 0 ;;
990
1259
  "server restart")
991
1260
  # THIS SERVER STARTED AGAIN, asked from the page: in two seconds, by
@@ -994,6 +1263,36 @@ case "$AREA $VERB" in
994
1263
  if [ "$SAY" = "yes" ]; then echo "systemd-run --on-active=2 systemctl restart clavarium.service"; exit 0; fi
995
1264
  systemd-run --quiet --collect --unit="clavarium-restart-$(date +%s)" --on-active=2 /usr/bin/systemctl restart clavarium.service
996
1265
  exit 0 ;;
1266
+ "server reboot")
1267
+ # THE WHOLE SERVER STARTED AGAIN, asked from the page (Settings, This server), after a
1268
+ # confirmation there: in five seconds, by systemd, so that the page is answered first.
1269
+ # Every machine stops with it (cleanly, by the VM manager) and starts again after.
1270
+ [ $# -eq 0 ] || usage
1271
+ if [ "$SAY" = "yes" ]; then echo "systemd-run --on-active=5 systemctl reboot"; exit 0; fi
1272
+ systemd-run --quiet --collect --unit="clavarium-reboot-$(date +%s)" --on-active=5 /usr/bin/systemctl reboot
1273
+ exit 0 ;;
1274
+ "server protect")
1275
+ # WHAT KEEPS THE SERVER IN HAND (04-11), at each start of the server: should memory run out,
1276
+ # the kernel stops a machine first, never this server, its front or ssh. Made so for the next
1277
+ # start of each (a drop-in) and at once for what runs (choom). No argument; done again is the same.
1278
+ [ $# -eq 0 ] || usage
1279
+ if [ "$SAY" = "yes" ]; then echo "OOMScoreAdjust=-900 for clavarium caddy dex oauth2-proxy ssh"; exit 0; fi
1280
+ changed=no
1281
+ for unit in clavarium caddy dex oauth2-proxy ssh; do
1282
+ systemctl cat "$unit.service" >/dev/null 2>&1 || continue
1283
+ dir="/etc/systemd/system/$unit.service.d"; file="$dir/clavarium-oom.conf"
1284
+ want='# Written by clavarium-host (server protect): stopped last should memory run out.
1285
+ [Service]
1286
+ OOMScoreAdjust=-900'
1287
+ if [ "$(cat "$file" 2>/dev/null)" != "$want" ]; then
1288
+ mkdir -p "$dir" && printf '%s\n' "$want" > "$file" && changed=yes
1289
+ fi
1290
+ pid=$(systemctl show -p MainPID --value "$unit.service" 2>/dev/null)
1291
+ [ -n "$pid" ] && [ "$pid" != 0 ] && choom -n -900 -p "$pid" >/dev/null 2>&1 || true
1292
+ done
1293
+ [ "$changed" = no ] || systemctl daemon-reload
1294
+ echo "protected"
1295
+ exit 0 ;;
997
1296
  "server room")
998
1297
  # WHAT THE SERVER HAS LEFT, for Settings (This server) and for the
999
1298
  # reserve that creating, starting, waking or growing a machine must
@@ -1002,6 +1301,7 @@ case "$AREA $VERB" in
1002
1301
  # place <machines|data|folders> <device> <size MB> <free MB>
1003
1302
  # memory <total MB> <available MB>
1004
1303
  # machine <one of our VMs> <resident MB>
1304
+ # machineage <one of our VMs> <seconds its qemu has run>
1005
1305
  # cpu <total ticks> <idle ticks> <cores> (for the curves: a difference of two looks)
1006
1306
  # machinecpu <one of our VMs> <ticks it used>
1007
1307
  [ $# -eq 0 ] || usage
@@ -1020,6 +1320,8 @@ case "$AREA $VERB" in
1020
1320
  [ -n "$vm" ] || continue
1021
1321
  rss="$(awk '/^VmRSS:/{print int($2/1024)}' "/proc/$pid/status" 2>/dev/null)" || rss=""
1022
1322
  [ -n "$rss" ] && printf 'machine %s %s\n' "$vm" "$rss"
1323
+ age="$(ps -o etimes= -p "$pid" 2>/dev/null | tr -d ' ')" || age=""
1324
+ case "$age" in ''|*[!0-9]*) ;; *) printf 'machineage %s %s\n' "$vm" "$age" ;; esac
1023
1325
  ticks="$(awk '{print $14 + $15}' "/proc/$pid/stat" 2>/dev/null)" || ticks=""
1024
1326
  [ -n "$ticks" ] && printf 'machinecpu %s %s\n' "$vm" "$ticks"
1025
1327
  done
@@ -1359,14 +1661,38 @@ case "$AREA $VERB" in
1359
1661
  "net taps")
1360
1662
  [ $# -eq 0 ] || usage
1361
1663
  run pgrep -a qemu-system ;;
1664
+ "net leases")
1665
+ # Which address the VM manager's DHCP server gave which MAC: one "<mac> <ip>" per
1666
+ # line, each checked; nothing else of that file is printed (the name in it is the
1667
+ # VM's own word, never trusted). Read-only. No file yet: nothing.
1668
+ [ $# -eq 0 ] || usage
1669
+ if [ "$SAY" = "yes" ]; then echo "<mac> <ip> of each lease in $LEASES"; exit 0; fi
1670
+ [ -f "$LEASES" ] || exit 0
1671
+ while read -r _ mac ip _; do
1672
+ if [[ "$mac" =~ ^[0-9a-f]{2}(:[0-9a-f]{2}){5}$ && "$ip" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ ]]; then printf '%s %s\n' "$mac" "$ip"; fi
1673
+ done < "$LEASES"
1674
+ exit 0 ;;
1362
1675
  "net bridge")
1676
+ # The bridge the machines' taps are on: multipass 1.17 names it per zone
1677
+ # (mpqemubrzone1, ...), older ones mpqemubr0 (04-11: the offline rules
1678
+ # were never written, their bridge not found). The first tap's says.
1363
1679
  [ $# -eq 0 ] || usage
1364
- run ip -4 -o addr show "$BRIDGE" ;;
1680
+ if [ "$SAY" = "yes" ]; then echo "ip -4 -o addr show <the bridge of the taps, else $BRIDGE>"; exit 0; fi
1681
+ bridge="$BRIDGE"
1682
+ for t in /sys/class/net/tap-*; do
1683
+ [ -e "$t/master" ] || continue
1684
+ m=$(basename "$(readlink "$t/master")"); [[ "$m" =~ ^mpqemubr[a-z0-9]{1,12}$ ]] && { bridge="$m"; break; }
1685
+ done
1686
+ if [ "$bridge" = "$BRIDGE" ] && ! [ -e "/sys/class/net/$BRIDGE" ]; then
1687
+ for b in /sys/class/net/mpqemubr*; do [ -e "$b" ] && { bridge=$(basename "$b"); break; }; done
1688
+ fi
1689
+ run ip -4 -o addr show "$bridge" ;;
1365
1690
  "net apply")
1366
1691
  # <ruleset>: the whole table, as text. Each line must be one of the
1367
1692
  # shapes the server writes; one unknown line refuses the whole.
1368
1693
  [ $# -eq 1 ] || usage
1369
1694
  tap='"tap-[0-9a-f]{1,12}"'; addr='[0-9]{1,3}(\.[0-9]{1,3}){3}'
1695
+ mac='[0-9a-f]{2}(:[0-9a-f]{2}){5}'; addrs="\\{ $addr(, $addr)* \\}"
1370
1696
  while IFS= read -r line; do
1371
1697
  [[ -z "$line"
1372
1698
  || "$line" =~ ^(delete\ )?table\ bridge\ $TABLE(\ \{)?$
@@ -1374,6 +1700,13 @@ case "$AREA $VERB" in
1374
1700
  || "$line" =~ ^\ \ \ \ type\ filter\ hook\ (forward|input)\ priority\ 0\;$
1375
1701
  || "$line" =~ ^\ \ \ \ iifname\ $tap\ (drop|udp\ dport\ 67\ accept|ether\ type\ arp\ accept)$
1376
1702
  || "$line" =~ ^\ \ \ \ iifname\ $tap\ ip\ daddr\ $addr\ (accept|meta\ l4proto\ \{\ tcp,\ udp\ \}\ th\ dport\ 53\ drop)$
1703
+ || "$line" =~ ^\ \ \ \ iifname\ $tap\ (ether\ saddr|arp\ saddr\ ether)\ !=\ $mac\ drop$
1704
+ || "$line" =~ ^\ \ \ \ iifname\ $tap\ ether\ type\ !=\ \{\ ip,\ arp\ \}\ drop$
1705
+ || "$line" =~ ^\ \ \ \ iifname\ $tap\ udp\ dport\ 67\ @th,288,48\ !=\ 0x[0-9a-f]{12}\ drop$
1706
+ || "$line" =~ ^\ \ \ \ iifname\ $tap\ ip\ saddr\ 0\.0\.0\.0\ ip\ daddr\ 255\.255\.255\.255\ udp\ dport\ 67\ accept$
1707
+ || "$line" =~ ^\ \ \ \ iifname\ $tap\ ip\ saddr\ !=\ $addr\ drop$
1708
+ || "$line" =~ ^\ \ \ \ iifname\ $tap\ arp\ saddr\ ip\ !=\ \{\ 0\.0\.0\.0,\ $addr\ \}\ drop$
1709
+ || "$line" =~ ^\ \ \ \ iifname\ $tap\ (ip\ saddr|arp\ saddr\ ip)\ $addrs\ drop$
1377
1710
  || "$line" =~ ^(\ \ )?\}$ ]] || refuse "a network rule of an unknown shape: $line"
1378
1711
  done <<< "$1"
1379
1712
  run nft "$1" ;;
@@ -1391,9 +1724,21 @@ case "$AREA $VERB" in
1391
1724
  # unreadable; 78 refused before anything ran; 79 the unit did not
1392
1725
  # start or its state is unknown; 80 time limit; 81 memory limit;
1393
1726
  # 82 killed by a signal.
1394
- [ $# -eq 1 ] || usage; is_name "$1" || refuse "not a folder name: $1"
1727
+ #
1728
+ # <name> --tips: the list of branches alone, no pack (deploy/git-read).
1729
+ # <name> --have <id>...: the pack leaves out what those commits reach
1730
+ # (at most 256, each a full object id): the server has it already.
1731
+ [ $# -ge 1 ] || usage; is_name "$1" || refuse "not a folder name: $1"
1395
1732
  case "$1" in *-origin) refuse "the reference of a repository is never read this way: $1" ;; esac
1396
- name="$1"
1733
+ name="$1"; shift
1734
+ how=()
1735
+ if [ $# -eq 1 ] && [ "$1" = "--tips" ]; then how=(--tips)
1736
+ elif [ $# -ge 2 ] && [ $# -le 257 ] && [ "$1" = "--have" ]; then
1737
+ shift
1738
+ for id in "$@"; do [[ "$id" =~ ^[0-9a-f]{40}$ ]] || refuse "not an object id: $id"; done
1739
+ how=(--have "$@")
1740
+ elif [ $# -ne 0 ]; then usage
1741
+ fi
1397
1742
  rand="RANDOM16"
1398
1743
  [ "$SAY" = "yes" ] || rand="$(od -An -N8 -tx1 /dev/urandom | tr -d ' \n')"
1399
1744
  unit="clvread-${name}_${rand}"
@@ -1413,7 +1758,7 @@ case "$AREA $VERB" in
1413
1758
  -E PATH=/usr/bin -E HOME=/nonexistent -E XDG_CONFIG_HOME=/nonexistent -E LC_ALL=C -E GIT_CONFIG_NOSYSTEM=1
1414
1759
  -E GIT_CONFIG_GLOBAL=/dev/null -E GIT_CEILING_DIRECTORIES=/resources -E GIT_NO_LAZY_FETCH=1
1415
1760
  -E GIT_TERMINAL_PROMPT=0 -E GIT_OPTIONAL_LOCKS=0 -E GIT_NO_REPLACE_OBJECTS=1
1416
- "$GIT_READ" "$name")
1761
+ "$GIT_READ" "$name" "${how[@]}")
1417
1762
  if [ "$SAY" = "yes" ]; then printf '%s\n' "${vector[@]}"; exit 0; fi
1418
1763
  folder_of "$name"
1419
1764
  [ -d "$RUNNER_ROOT" ] && [ ! -L "$RUNNER_ROOT" ] && [ -x "$GIT_READ" ] || refuse "the git reader is not installed (run install.sh again)"
@@ -1458,6 +1803,15 @@ case "$AREA $VERB" in
1458
1803
  systemctl stop "clvread-$1_*.service" >/dev/null 2>&1 || true
1459
1804
  systemctl reset-failed "clvread-$1_*.service" >/dev/null 2>&1 || true
1460
1805
  exit 0 ;;
1806
+ "relay join")
1807
+ # A HOME ASKS TO JOIN THIS MACHINE'S RELAY (server/relayjoin.mjs, POST /c/relay-join): <code>
1808
+ # ssh-ed25519 <key>. The code is one the owner made here as root (`sudo clavarium relay invite
1809
+ # NAME`); its hash, its name and its end are kept where the server cannot read them. Checked,
1810
+ # spent and acted on as root by deploy/online.mjs (relay join): a wrong, used or expired code is
1811
+ # refused (78) and counted; a good one adds this PUBLIC key to the relay, bounded to two ports
1812
+ # of its own. What it prints holds nothing secret: the relay's address, fingerprint, two ports.
1813
+ [ $# -eq 3 ] && [[ "$1" =~ ^[A-HJKMNP-Z2-9]{8}$ ]] && [ "$2" = "ssh-ed25519" ] && [[ "$3" =~ ^[A-Za-z0-9+/]{68}$ ]] || usage
1814
+ run "$UPDATER_NODE" "$CODE/current/app/deploy/clavarium.mjs" relay join "$1" "$2 $3" ;;
1461
1815
  "update check")
1462
1816
  # Asks the registry what is out on the channel, now, and prints what
1463
1817
  # the updater knows (state.json). No argument: the registry, the name