code wiki / _hdl_build / nx_put.nx
nx_put.nx source
↩ module page · 87 lines · 4327 B
1// nx_put.nx -- GENERIC sovereign file uploader over the team's own SSH (DRY: ssh_put_file was
2// copy-pasted into nx_vroom_deploy / nx_vroom_html / nx_translate_deploy = the 3+ threshold).
3// Reads three /tmp inputs and streams a local file to the NAS:
4// /tmp/nxpw -- ssh password
5// /tmp/nxput_src -- LOCAL path of the file to upload
6// /tmp/nxput_dst -- the remote command to receive it, e.g. "cat > /volume1/.../foo.elf.new"
7// license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_ssh_lib.nx"
10const K_MAGIC_1048576: i64 = 1048576
11const K_MAGIC_16384: i64 = 16384
12const K_MAGIC_65536: i64 = 65536
13const K_MAGIC_1024: i64 = 1024
14
15func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=0 as u8 { n=n+1 } return n }
16func rstrip(s: *u8, n: i64) -> i64 {
17 var m: i64 = n
18 while m > 0 { if s[m-1] == 10 as u8 { m = m - 1 } else { if s[m-1] == 13 as u8 { m = m - 1 } else { return m } } }
19 return m
20}
21
22// copied from nx_ssh_put.nx (per-tool fn, not exported by nx_ssh_lib)
23func ssh_put_file(st: *SshState, wcmd: *u8, wcmdlen: i64, data: *u8, datalen: i64) -> i64 {
24 let co: *u8 = sys_mmap(64); var c: i64 = 0
25 c = ssh_put_byte(co, c, 90); c = ssh_put_str(co, c, "session" as *u8, 7)
26 c = ssh_put_u32(co, c, 0); c = ssh_put_u32(co, c, K_MAGIC_1048576); c = ssh_put_u32(co, c, K_MAGIC_16384)
27 ssh_enc_send(st, co, c)
28 let rep: *u8 = sys_mmap(K_MAGIC_65536); var rcid: i64 = 0 - 1; var guard: i64 = 0
29 while rcid < 0 {
30 if guard > 16 { return 0 - 1 }
31 let rl: i64 = ssh_enc_recv(st, rep); if rl < 0 { return 0 - 1 }
32 if rep[0] == 91 as u8 { rcid = ssh_u32be(rep, 5) }
33 if rep[0] == 92 as u8 { return 0 - 2 }
34 guard = guard + 1
35 }
36 let cr: *u8 = sys_mmap(K_MAGIC_1024); var q: i64 = 0
37 q = ssh_put_byte(cr, q, 98); q = ssh_put_u32(cr, q, rcid); q = ssh_put_str(cr, q, "exec" as *u8, 4); q = ssh_put_byte(cr, q, 1); q = ssh_put_str(cr, q, wcmd, wcmdlen)
38 ssh_enc_send(st, cr, q)
39 var off: i64 = 0
40 while off < datalen {
41 var nn: i64 = datalen - off; if nn > K_MAGIC_16384 { nn = K_MAGIC_16384 }
42 let dp: *u8 = sys_mmap(nn + 64); var p: i64 = 0
43 p = ssh_put_byte(dp, p, 94); p = ssh_put_u32(dp, p, rcid); p = ssh_put_u32(dp, p, nn)
44 var i: i64 = 0; while i < nn { dp[p + i] = data[off + i]; i = i + 1 } p = p + nn
45 ssh_enc_send(st, dp, p)
46 off = off + nn
47 }
48 let eo: *u8 = sys_mmap(16); var e: i64 = 0; e = ssh_put_byte(eo, e, 96); e = ssh_put_u32(eo, e, rcid); ssh_enc_send(st, eo, e)
49 var done: i64 = 0; var loops: i64 = 0
50 while done == 0 {
51 if loops > K_MAGIC_16384 { done = 1 }
52 let rl: i64 = ssh_enc_recv(st, rep); if rl < 0 { done = 1 } else { if rep[0] == 97 as u8 { done = 1 } }
53 loops = loops + 1
54 }
55 let clo: *u8 = sys_mmap(16); var k: i64 = 0; k = ssh_put_byte(clo, k, 97); k = ssh_put_u32(clo, k, rcid); ssh_enc_send(st, clo, k)
56 return 0
57}
58
59func main() -> i64 {
60 let pwbox: *i64 = sys_mmap(16) as *i64
61 let pw: *u8 = sys_read_file("/tmp/nxpw" as *u8, pwbox)
62 if (pw as i64) == 0 { ssh_puts("no /tmp/nxpw\n" as *u8); return 1 }
63 var pwlen: i64 = rstrip(pw, pwbox[0])
64
65 let sbox: *i64 = sys_mmap(16) as *i64
66 let srcp: *u8 = sys_read_file("/tmp/nxput_src" as *u8, sbox)
67 if (srcp as i64) == 0 { ssh_puts("no /tmp/nxput_src\n" as *u8); return 5 }
68 srcp[rstrip(srcp, sbox[0])] = 0 as u8 // null-terminate the path
69
70 let dbox: *i64 = sys_mmap(16) as *i64
71 let dst: *u8 = sys_read_file("/tmp/nxput_dst" as *u8, dbox)
72 if (dst as i64) == 0 { ssh_puts("no /tmp/nxput_dst\n" as *u8); return 6 }
73 let dstlen: i64 = rstrip(dst, dbox[0])
74
75 let fbox: *i64 = sys_mmap(16) as *i64
76 let payload: *u8 = sys_read_file(srcp, fbox)
77 if (payload as i64) == 0 { ssh_puts("cannot read src file\n" as *u8); return 7 }
78 let plen: i64 = fbox[0]
79
80 let st: *SshState = sys_mmap(SSH_STATE_BYTES) as *SshState
81 if ssh_open_session(st, (192 << 24) | (168 << 16) | (8 << 8) | 227) != 0 { ssh_puts("session fail\n" as *u8); return 2 }
82 if ssh_userauth_password(st, "elderwesto" as *u8, 10, pw, pwlen) != 1 { ssh_puts("auth fail\n" as *u8); sys_close(st.fd); return 3 }
83 ssh_put_file(st, dst, dstlen, payload, plen)
84 ssh_puts("[nx_put uploaded]\n" as *u8)
85 sys_close(st.fd)
86 return 0
87}