nx_host_recover.nx source
↩ module page · 56 lines · 3309 B
1// nx_host_recover.nx -- bring the live nishifamily host back up via the ecosystem's
2// OWN supervisor (nx_hostctl supervise), over our sovereign SSH client. The
3// supervisor was dead (no proc) -> sites.elf down -> nishifamily.com 0-byte TLS.
4// This relaunches it with the proven setsid idiom (survives the SSH channel close)
5// and reports the resulting process + listener state. Read-clean: only launches the
6// ecosystem's documented supervisor; no kill of anything (PROCS was already empty).
7// Reuses the vault-auth preamble from nx_aw_push/nx_cad_nas_probe. license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_ssh_lib.nx"
10
11const P_SECRET_OUT: *u8 = "/tmp/nxsecret.out" as *u8
12const P_VAULT_NV: *u8 = "/home/elderwesto/.nishi/secrets/nas.nv" as *u8
13
14func p_run(path: *u8, a1: *u8, a2: *u8) -> i64 {
15 let pid: i64 = sys_fork()
16 if pid == 0 {
17 let argv: *i64 = sys_mmap(64) as *i64
18 argv[0] = path as i64
19 var ai: i64 = 1
20 if (a1 as i64) != 0 { argv[ai] = a1 as i64; ai = ai + 1 }
21 if (a2 as i64) != 0 { argv[ai] = a2 as i64; ai = ai + 1 }
22 argv[ai] = 0
23 let envp: *i64 = sys_mmap(16) as *i64
24 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
25 sys_execve(path, argv, envp)
26 sys_exit(127)
27 }
28 let st: *i64 = sys_mmap(16) as *i64
29 sys_wait4(pid, st, 0)
30 return (st[0] >> 8) & 0xff
31}
32func p_unlink(path: *u8) -> i64 { __syscall(263, AT_FDCWD, path, 0, 0, 0, 0) return 0 }
33func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
34
35func main() -> i64 {
36 if p_run("_offc/nx_machine_key.elf" as *u8, 0 as *u8, 0 as *u8) != 0 { ssh_puts("recover: machine-key FAIL\n" as *u8); return 1 }
37 if p_run("_offc/nx_vault.elf" as *u8, "open" as *u8, P_VAULT_NV) != 0 { ssh_puts("recover: vault open FAIL\n" as *u8); return 2 }
38 p_unlink("/tmp/nxpass" as *u8)
39 let pwbox: *i64 = sys_mmap(16) as *i64
40 let pw: *u8 = sys_read_file(P_SECRET_OUT, pwbox)
41 if (pw as i64) == 0 { ssh_puts("recover: read secret FAIL\n" as *u8); return 3 }
42 var pwlen: i64 = pwbox[0]
43 while pwlen > 0 { if pw[pwlen-1] == 10 as u8 { pwlen = pwlen - 1 } else { if pw[pwlen-1] == 13 as u8 { pwlen = pwlen - 1 } else { break } } }
44
45 let st: *SshState = sys_mmap(SSH_STATE_BYTES) as *SshState
46 if ssh_open_session(st, (192 << 24) | (168 << 16) | (8 << 8) | 227) != 0 { ssh_puts("recover: SSH session FAIL\n" as *u8); p_unlink(P_SECRET_OUT); return 7 }
47 if ssh_userauth_password(st, "elderwesto" as *u8, 10, pw, pwlen) != 1 { ssh_puts("recover: SSH auth FAIL\n" as *u8); sys_close(st.fd); p_unlink(P_SECRET_OUT); return 8 }
48 p_unlink(P_SECRET_OUT)
49
50 // Relaunch the supervisor (the ecosystem's own nx_hostctl supervise), setsid so
51 // it outlives this SSH channel; then report processes + the :7443 listener.
52 let cmd: *u8 = "cd /volume1/homes/elderwesto/nishihost && setsid ./nx_hostctl supervise >> supervisor.log 2>&1 </dev/null & sleep 5; echo =AFTER=; ps w 2>/dev/null | grep -iE 'hostctl|sites|7443' | grep -v grep | head; echo =LISTEN=; (ss -tln 2>/dev/null || netstat -tln 2>/dev/null) | grep -E ':7443' | head; echo =SUPLOG=; tail -6 supervisor.log 2>/dev/null; echo =RECOVERDONE=" as *u8
53 ssh_exec(st, cmd, slen(cmd))
54 sys_close(st.fd)
55 return 0
56}