nx_ssh_deploy.nx source
↩ module page · 35 lines · 1996 B
1// nx_ssh_deploy.nx -- use the sovereign SSH client to authenticate to the NAS
2// (password, from /tmp/nxpw) and run a READ-ONLY recon command, so we learn
3// the daemon layout + how it's launched BEFORE any deploy. No destructive
4// action here -- recon first (verified facts, not assumptions).
5//
6// license_tier: ORIGINAL
7
8import "nx_syscalls.nx"
9import "nx_ssh_lib.nx"
10
11func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } return n }
12
13func main() -> i64 {
14 // password from /tmp/nxpw (written by the caller, deleted after)
15 let pwbox: *i64 = sys_mmap(16) as *i64
16 let pw: *u8 = sys_read_file("/tmp/nxpw" as *u8, pwbox)
17 if (pw as i64) == 0 { ssh_puts("no /tmp/nxpw\n" as *u8); return 1 }
18 var pwlen: i64 = pwbox[0]
19 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 } } }
20
21 let st: *SshState = sys_mmap(SSH_STATE_BYTES) as *SshState
22 let rc: i64 = ssh_open_session(st, (192 << 24) | (168 << 16) | (8 << 8) | 240)
23 if rc != 0 { ssh_puts("session open failed rc=" as *u8); ssh_phex((0 - rc) as *u8, 0); ssh_puts("(see code)\n" as *u8); return 2 }
24 ssh_puts("[1] KEX+transport+SERVICE_ACCEPT ok.\n" as *u8)
25
26 let ok: i64 = ssh_userauth_password(st, "elderwesto" as *u8, 10, pw, pwlen)
27 if ok != 1 { ssh_puts("[2] PASSWORD AUTH FAILED (wrong NAS password?).\n" as *u8); sys_close(st.fd); return 3 }
28 ssh_puts("[2] PASSWORD AUTH OK (sovereign client).\n[3] recon:\n--------\n" as *u8)
29
30 let cmd: *u8 = "mkdir -p ~/.ssh; chmod 700 ~/.ssh; grep -q nishi-ssh ~/.ssh/authorized_keys 2>/dev/null || echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGHnSmsmuRUMNDcknMGx2tQWE3TcaebtvOoR3XBns8JM nishi-ssh' >> ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys; echo KEY-INSTALLED; grep -c nishi-ssh ~/.ssh/authorized_keys" as *u8
31 ssh_exec(st, cmd, slen(cmd))
32 ssh_puts("--------\n[4] recon done.\n" as *u8)
33 sys_close(st.fd)
34 return 0
35}