code wiki / _hdl_build / nx_relate_daemon.nx

nx_relate_daemon.nx source

↩ module page · 86 lines · 5083 B

1// nx_relate_daemon.nx -- the DEPLOYABLE Relationship OS daemon (the ops shell around the gated pure core 2// nx_relate_serve). Binds 127.0.0.1:8027 and serves the interactive app from the DURABLE store prefix 3// knowledge/status/relate_ (main() self-heals the knowledge/status/ parent via sys_mkdir on startup -- the NAS's 4// root-owned knowledge/ lacks it; manifest auto-created on first commit). R2 AUTH-GATED: every request resolves 5// X-Nishi-Session (or the hidden `sess` form field) via olg_whoami against the SITE login plane (opaque_keys.bin, 6// realm nishi_site_admin) -- valid session = the app (forms carry the synchronizer token), none = bootstrap/401. 7// Route /relate -> this port on the sovereign edge = the operator-gated 8// deploy step (same pattern as the email webmail daemon :8025). Runs forever (accept loop) -- build with 9// --build-only; run deliberately. license_tier: ORIGINAL 10import "nx_relate_serve.nx" 11import "nx_opaque_login.nx" // olg_ctx_setup / olg_whoami -- the SAME keys+realm the /login minter signs with 12const RD_MAGIC_65536: i64 = 65536 13const RD_MAGIC_262144: i64 = 262144 14 15const RD_PORT: i64 = 0x1f5b // 8027 16const RD_PREFIX: *u8 = "knowledge/status/relate_" 17// R2 AUTH: validate sessions against the SITE login plane (minter = nx_opaque_login.elf :9091). Keys + store are 18// the login daemon's own files in the nishihost cwd; realm MUST equal the minter's ("nishi_site_admin", 16) or 19// every real token gets rejected (the /wiki key-mismatch lesson). Daemon runs as root -> files readable. 20const RD_KEYS: *u8 = "opaque_keys.bin" 21const RD_ASTORE: *u8 = "opaque_store.log" 22const RD_IDX: *u8 = "nishi_uid_handle.tsv" // the login daemon's uid(hex)->handle index (the ACL keys off handle) 23 24func d_addr(out: *u8, port: i64) -> i64 { 25 out[0] = 2 as u8; out[1] = 0 as u8 26 out[2] = ((port >> 8) & 0xff) as u8; out[3] = (port & 0xff) as u8 27 out[4] = 127 as u8; out[5] = 0 as u8; out[6] = 0 as u8; out[7] = 1 as u8 28 var i: i64 = 8 29 while i < 16 { out[i] = 0 as u8; i = i + 1 } 30 return 0 31} 32 33func main() -> i64 { 34 // self-heal the durable store dir: the prefix knowledge/status/relate_ needs knowledge/status/ to EXIST 35 // (on the NAS the root-owned knowledge/ has no status/ subdir yet -> ss_commit silently fails and every POST 36 // returns 200 but persists NOTHING). mkdir is idempotent (EEXIST < 0 is harmless, ignored); the daemon runs 37 // as root so it can create it under root-owned knowledge/. Never make the operator do what the system can. 38 sys_mkdir("knowledge" as *u8, 0x1ed) 39 sys_mkdir("knowledge/status" as *u8, 0x1ed) 40 // FAIL-CLOSED auth bootstrap: no keys -> exit(1) -> the hostctl guard backs off. NEVER serve unauthed. 41 let ctx: *NxAuthContext = sys_mmap(256) as *NxAuthContext 42 if olg_ctx_setup(ctx, RD_KEYS, RD_ASTORE, "nishi_site_admin" as *u8, 16, "Nishi site admin" as *u8, 16, 256, 2, 1) != 0 { 43 p("NX-RELATE-DAEMON auth ctx FAILED (opaque_keys.bin unreadable?) -- fail closed, fail loud\n" as *u8) 44 return 1 45 } 46 let addr: *u8 = sys_mmap(16) 47 d_addr(addr, RD_PORT) 48 let lfd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0) 49 if lfd < 0 { p("NX-RELATE-DAEMON socket FAILED -- fail loud\n" as *u8); return 1 } 50 if sys_bind(lfd, addr, 16) < 0 { p("NX-RELATE-DAEMON bind 127.0.0.1:8027 FAILED (port busy?) -- fail loud\n" as *u8); return 1 } 51 if sys_listen(lfd, 16) < 0 { p("NX-RELATE-DAEMON listen FAILED -- fail loud\n" as *u8); return 1 } 52 p("NX-RELATE-DAEMON serving http://127.0.0.1:8027/ store=knowledge/status/relate_ AUTH=nishi_site_admin (accept loop)\n" as *u8) 53 54 let reqb: *u8 = sys_mmap(RD_MAGIC_65536) 55 let resb: *u8 = sys_mmap(RD_MAGIC_262144) 56 let tokb: *u8 = sys_mmap(600) 57 let uid: *u8 = sys_mmap(64) 58 let uidn: *i64 = sys_mmap(16) as *i64 59 let hbuf: *u8 = sys_mmap(256) 60 let auth: *i64 = sys_mmap(8 * 4) as *i64 // [0]=tok-or-0, [1]=resolved-handle-or-empty (the ACL bundle) 61 var go: i64 = 1 62 while go == 1 { 63 let cfd: i64 = sys_accept(lfd) 64 if cfd >= 0 { 65 let rn: i64 = rv_read_req(cfd, reqb, RD_MAGIC_65536) 66 if rn > 0 { 67 // auth verdict here in the shell (crypto), ACL semantics in the core: header or `sess` form field 68 // -> olg_whoami against the site realm -> uid -> handle. Invalid/absent -> tok=0 -> bootstrap/401. 69 auth[0] = 0 70 hbuf[0] = 0 as u8 71 auth[1] = hbuf as i64 72 let tl: i64 = rv_get_sess(reqb, rn, tokb, 600) 73 if tl > 0 { 74 if olg_whoami(ctx, tokb, tl, sys_now_realtime_sec(), uid, 64, uidn) == NX_MAUTH_OK { 75 auth[0] = tokb as i64 76 rv_resolve_handle(RD_IDX, uid, uidn[0], hbuf, 256) // unmapped -> empty -> zero grants (deny-all) 77 } 78 } 79 let on: i64 = rv_handle_auth(RD_PREFIX, reqb, rn, resb, RD_MAGIC_262144, auth) 80 rv_write_all(cfd, resb, on) 81 } 82 sys_close(cfd) 83 } 84 } 85 return 0 86}