interplane.Get Omega
Read in your language

mailhand

mailhand: agents look after a person's email in seeded Omega. Maildir + MIME parsing, JSONL CLI and MCP, rule triage, policy-gated sending, hash-chained audit. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

mailhand / src / cr.om

553 lines · 11609 bytes Raw Blame History
1note ======== src/cr.om: remote account credentials (Credentials, prefix cr_) ========
2note The file HOME/accounts (or the config line "accounts PATH") is the human's: it must be a regular file (never followed through a
3note symlink), owned by the effective user, with no group or other permission bits (mode & 077 == 0);
4note anything else is a defined refusal naming the file and its mode, never its content. Format (one
5note "key value" per line; # comment lines and blank lines are skipped; README "Remote accounts"):
6note account NAME starts the block of one account
7note imap imaps://HOST[:PORT] implicit TLS (default port 993)
8note smtp smtps://HOST[:PORT] | submission://HOST[:PORT] (465 implicit TLS / 587 STARTTLS)
9note user NAME login name
10note password SECRET the rest of the line, exactly (a trailing CR is dropped)
11note roots PATH trust store (default /etc/ssl/certs/ca-certificates.crt)
12note connect 127.0.0.1 test-only: TCP goes to this IPv4 address; the certificate is still
13note checked against HOST
14note The whole file is read into ONE buffer that the caller owns (a local array of the op: there is
15note no heap), parsed in place and zeroed by cr_wipe before the op returns: the password is never
16note copied into a store, the output, the audit log or a file. AUTH PLAIN and LOGIN bytes are built
17note in small local windows that are zeroed after each write.
18const CR_BUF ulong 262144
19note CR_BUF: size of the credentials buffer (a local array: no heap in Omega). A larger file is the
20note defined refusal credentials_too_large; the gate's 25-cred-refusals scenario checks it.
21global cr_v ulong 24
22note cr_v: 0 account found, 1/2 imap host off/len, 3 imap port, 4 imap mode (1 imaps), 5/6 smtp host,
23note 7 smtp port, 8 smtp mode (1 smtps, 2 submission), 9/10 user, 11/12 password, 13/14 connect,
24note 15/16 roots, 17 file length, 18 why, 19 file mode
25text cr_kfile "/accounts"
26text cr_kaccts "accounts"
27text cr_kacct "account"
28text cr_kimap "imap"
29text cr_ksmtp "smtp"
30text cr_kuser "user"
31text cr_kpass "password"
32text cr_kroots "roots"
33text cr_kconn "connect"
34text cr_uimaps "imaps://"
35text cr_usmtps "smtps://"
36text cr_usub "submission://"
37text cr_droots "/etc/ssl/certs/ca-certificates.crt"
38text cr_b64a "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
39text cr_d1 "credentials file "
40text cr_d2 " refused: "
41text cr_w1 "no_credentials"
42text cr_w2 "credentials_symlink"
43text cr_w3 "credentials_not_regular"
44text cr_w4 "credentials_owner"
45text cr_w5 "credentials_mode"
46text cr_w6 "credentials_too_large"
47text cr_w7 "credentials_io"
48text cr_w8 "account_not_found"
49text cr_w9 "account_incomplete"
50text cr_dmode " (group or others have access; chmod 600 it)"
51text cr_down " (not owned by the user running mailhand)"
52text cr_kmode "mode"
53
54fn cr_reset to void
55for i ulong 24
56put cr_v i 0
57end
58end
59
60note cr_wipe: zero the whole credentials buffer
61fn cr_wipe sec edit uchar * to void
62for i ulong len sec bound 9223372036854775807
63put sec i 0
64end
65end
66
67note cr_o_why: the refusal as JSON fields (never the path: MCP output must not name it)
68fn cr_o_why to void
69w = get cr_v 18
70b = new uchar 4 0
71if eq w 1
72o_ks krefk cr_w1
73end
74if eq w 2
75o_ks krefk cr_w2
76end
77if eq w 3
78o_ks krefk cr_w3
79end
80if eq w 4
81o_ks krefk cr_w4
82end
83if eq w 5
84o_ks krefk cr_w5
85cr_oct b get cr_v 19
86o_ks cr_kmode b
87end
88if eq w 6
89o_ks krefk cr_w6
90end
91if eq w 7
92o_ks krefk cr_w7
93end
94if eq w 8
95o_ks krefk cr_w8
96end
97if eq w 9
98o_ks krefk cr_w9
99end
100end
101
102fn cr_oct b edit uchar 4 m ulong to void
103put b 0 uchar add 48 and shr m 9 7
104put b 1 uchar add 48 and shr m 6 7
105put b 2 uchar add 48 and shr m 3 7
106put b 3 uchar add 48 and m 7
107end
108
109note cr_diag: stderr "credentials file PATH refused: why[ mode NNNN ...]" with gp = the file path
110fn cr_diag why view uchar * extra view uchar * to void
111b = new uchar 10 0
112r = write 2 kpref 0 len kpref
113r = write 2 cr_d1 0 len cr_d1
114r = write 2 gp 0 get gv VPL
115r = write 2 cr_d2 0 len cr_d2
116r = write 2 why 0 len why
117if gt len extra 0
118put b 0 32
119for i ulong 4
120put b add 1 i get cr_kmode i
121end
122put b 5 32
123for i ulong 4
124put b add 6 i 48
125end
126m = get cr_v 19
127put b 6 uchar add 48 and shr m 9 7
128put b 7 uchar add 48 and shr m 6 7
129put b 8 uchar add 48 and shr m 3 7
130put b 9 uchar add 48 and m 7
131r = write 2 b 0 10
132r = write 2 extra 0 len extra
133end
134r = write 2 knl 0 1
135end
136
137fn cr_refuse w ulong code int to int
138put cr_v 18 w
139ret code
140end
141
142note cr_open: check and read HOME/accounts into sec; 0 or an exit code (cr_v 18 says why)
143fn cr_open sec edit uchar * to int
144cr_reset
145none = new uchar 1 0
146p_reset
147if not cf_get cr_kaccts
148p_h
149end
150if cf_get cr_kaccts
151p_s S_CFG get gv VCFO get gv VCFN
152else
153p_t cr_kfile
154end
155if not p_ok
156ret cr_refuse 7 EIO
157end
158fd = fopen gp 0 get gv VPL ORD 0
159if eq fd -2
160cr_diag cr_w1 part none 0 0
161ret cr_refuse 1 ENOSETUP
162end
163if eq fd -40
164cr_diag cr_w2 part none 0 0
165ret cr_refuse 2 EPATH
166end
167if lt fd 0
168cr_diag cr_w7 part none 0 0
169ret cr_refuse 7 EIO
170end
171k = cr_check int fd
172if ne k 0
173z = fclose int fd
174ret k
175end
176n = get cr_v 17
177p = ulong 0
178while lt p n bound 9223372036854775807
179r = read int fd sec p sub n p
180if or eq r -4 eq r -11
181continue
182end
183if le r 0
184z = fclose int fd
185cr_diag cr_w7 part none 0 0
186ret cr_refuse 7 EIO
187end
188p = add p ulong r
189end
190z = fclose int fd
191ret 0
192end
193
194note cr_check: fstat the open file: regular, owned by geteuid, mode & 077 == 0, fits CR_BUF
195fn cr_check fd int to int
196s = new uchar 128 0
197none = new uchar 1 0
198r = fstat fd s 0 128
199if lt r 0
200cr_diag cr_w7 part none 0 0
201ret cr_refuse 7 EIO
202end
203mode = le32b s 16
204put cr_v 19 and mode 4095
205if ne and mode 61440 32768
206cr_diag cr_w3 part none 0 0
207ret cr_refuse 3 EPATH
208end
209uid = le32b s 24
210eu = geteuid
211if ne long uid eu
212cr_diag cr_w4 cr_down
213ret cr_refuse 4 EPATH
214end
215if ne and mode 63 0
216cr_diag cr_w5 cr_dmode
217ret cr_refuse 5 EPATH
218end
219size = le64b s 48
220if gt size CR_BUF
221cr_diag cr_w6 part none 0 0
222ret cr_refuse 6 EPATH
223end
224put cr_v 17 size
225ret 0
226end
227
228note cr_find: select the block "account NAME" (NAME = store s range [o, +n)); 0 or an exit code
229fn cr_find sec edit uchar * s ulong o ulong n ulong to int
230put cr_v 20 s
231put cr_v 21 o
232put cr_v 22 n
233fl = get cr_v 17
234p = ulong 0
235cur = false
236while lt p fl bound 9223372036854775807
237e = cr_eol sec p fl
238cur = cr_line sec p e cur
239p = add e 1
240end
241if eq get cr_v 0 0
242diag cr_w8
243ret cr_refuse 8 ENOTFOUND
244end
245if or eq get cr_v 10 0 eq get cr_v 12 0
246ret cr_refuse 9 ENOSETUP
247end
248ret 0
249end
250
251fn cr_eol sec edit uchar * p ulong fl ulong to ulong
252q = p
253while lt q fl bound 9223372036854775807
254if eq get sec q 10
255ret q
256end
257q = add q 1
258end
259ret fl
260end
261
262note cr_line: one line [p, e); returns whether the selected block is still current
263fn cr_line sec edit uchar * p ulong e0 ulong cur bool to bool
264e = e0
265if and gt e p eq get sec sub e 1 13
266e = sub e 1
267end
268if or eq e p eq get sec p 35
269ret cur
270end
271k = p
272while lt k e bound 9223372036854775807
273if eq get sec k 32
274break
275end
276k = add k 1
277end
278v = k
279if lt v e
280v = add v 1
281end
282if cr_key sec p k cr_kacct
283if eq get cr_v 0 1
284ret false
285end
286if cr_sameas sec v e
287put cr_v 0 1
288ret true
289end
290ret false
291end
292if not cur
293ret false
294end
295cr_field sec p k v e
296ret true
297end
298
299fn cr_key sec edit uchar * p ulong k ulong t view uchar * to bool
300if ne sub k p len t
301ret false
302end
303for i ulong len t bound 64
304if ne get sec add p i get t i
305ret false
306end
307end
308ret true
309end
310
311fn cr_sameas sec edit uchar * v ulong e ulong to bool
312s = get cr_v 20
313o = get cr_v 21
314n = get cr_v 22
315if ne sub e v n
316ret false
317end
318for i ulong n bound 9223372036854775807
319if ne get sec add v i s_get s add o i
320ret false
321end
322end
323ret true
324end
325
326fn cr_field sec edit uchar * p ulong k ulong v ulong e ulong to void
327if cr_key sec p k cr_kpass
328put cr_v 11 v
329put cr_v 12 sub e v
330ret
331end
332t = cr_rtrim sec v e
333if cr_key sec p k cr_kuser
334put cr_v 9 v
335put cr_v 10 sub t v
336ret
337end
338if cr_key sec p k cr_kroots
339put cr_v 15 v
340put cr_v 16 sub t v
341ret
342end
343if cr_key sec p k cr_kconn
344put cr_v 13 v
345put cr_v 14 sub t v
346ret
347end
348if cr_key sec p k cr_kimap
349z = cr_url sec v t 1
350ret
351end
352if cr_key sec p k cr_ksmtp
353z = cr_url sec v t 5
354end
355end
356
357fn cr_rtrim sec edit uchar * v ulong e ulong to ulong
358t = e
359while gt t v bound 9223372036854775807
360c = get sec sub t 1
361if and ne c 32 ne c 9
362ret t
363end
364t = sub t 1
365end
366ret t
367end
368
369fn cr_pfx sec edit uchar * v ulong t ulong u view uchar * to bool
370if lt sub t v len u
371ret false
372end
373for i ulong len u bound 64
374if ne get sec add v i get u i
375ret false
376end
377end
378ret true
379end
380
381note cr_url: SCHEME://HOST[:PORT] into slots at base (1 imap, 5 smtp); false when malformed
382fn cr_url sec edit uchar * v ulong t ulong base ulong to bool
383mode = ulong 0
384h = v
385port = ulong 0
386if and eq base 1 cr_pfx sec v t cr_uimaps
387mode = 1
388h = add v len cr_uimaps
389port = 993
390end
391if eq base 5
392if cr_pfx sec v t cr_usmtps
393mode = 1
394h = add v len cr_usmtps
395port = 465
396end
397if cr_pfx sec v t cr_usub
398mode = 2
399h = add v len cr_usub
400port = 587
401end
402end
403if eq mode 0
404ret false
405end
406q = h
407while lt q t bound 9223372036854775807
408c = get sec q
409if eq c 58
410break
411end
412if not cr_hostc c
413ret false
414end
415q = add q 1
416end
417if or eq q h gt sub q h 253
418ret false
419end
420if lt q t
421port = cr_port sec add q 1 t
422if eq port 0
423ret false
424end
425end
426put cr_v base h
427put cr_v add base 1 sub q h
428put cr_v add base 2 port
429put cr_v add base 3 mode
430ret true
431end
432
433fn cr_hostc c uchar to bool
434if or and ge c 97 le c 122 and ge c 65 le c 90
435ret true
436end
437if and ge c 48 le c 57
438ret true
439end
440ret or eq c 46 eq c 45
441end
442
443fn cr_port sec edit uchar * p ulong t ulong to ulong
444if or eq p t gt sub t p 5
445ret 0
446end
447v = ulong 0
448for i ulong sub t p bound 5
449c = get sec add p i
450if or lt c 48 gt c 57
451ret 0
452end
453v = add mul v 10 ulong sub c 48
454end
455if gt v 65535
456ret 0
457end
458ret v
459end
460
461note cr_copy: bytes [o, +n) of sec into dst (non-secret fields: host, connect, roots); the length
462fn cr_copy sec edit uchar * o ulong n ulong dst edit uchar * to ulong
463m = n
464if gt m len dst
465m = len dst
466end
467for i ulong m bound 9223372036854775807
468put dst i get sec add o i
469end
470ret m
471end
472
473note cr_roots: load the trust store (roots line, else the system default); count or < 0
474fn cr_roots sec edit uchar * to long
475b = new uchar 4096 0
476if eq get cr_v 16 0
477ret ot_tl_roots cr_droots
478end
479n = cr_copy sec get cr_v 15 get cr_v 16 b
480ret ot_tl_roots part b 0 n
481end
482
483note cr_pb: byte i of "\0" user "\0" password (the SASL PLAIN message, RFC 4616)
484fn cr_pb sec edit uchar * i ulong to uchar
485un = get cr_v 10
486if eq i 0
487ret 0
488end
489if le i un
490ret get sec add get cr_v 9 sub i 1
491end
492if eq i add un 1
493ret 0
494end
495ret get sec add get cr_v 11 sub sub i un 2
496end
497
498note cr_wplain: send pre, then base64 of the PLAIN message, then CRLF, through the transport;
499note the 4096-byte window is zeroed after every write
500fn cr_wplain sec edit uchar * pre view uchar * to bool
501b = new uchar 4096 0
502if not tx_write pre
503ret false
504end
505n = add add get cr_v 10 get cr_v 12 2
506k = ulong 0
507i = ulong 0
508ok = true
509while lt i n bound 9223372036854775807
510x = shl ulong cr_pb sec i 16
511m = ulong 1
512if lt add i 1 n
513x = or x shl ulong cr_pb sec add i 1 8
514m = 2
515end
516if lt add i 2 n
517x = or x ulong cr_pb sec add i 2
518m = 3
519end
520cr_b64q b k x m
521k = add k 4
522i = add i 3
523if ge k 4092
524ok = and ok tx_write part b 0 k
525cr_zero b
526k = 0
527end
528end
529put b k 13
530put b add k 1 10
531ok = and ok tx_write part b 0 add k 2
532cr_zero b
533ret ok
534end
535
536fn cr_b64q b edit uchar * k ulong x ulong m ulong to void
537put b k get cr_b64a and shr x 18 63
538put b add k 1 get cr_b64a and shr x 12 63
539put b add k 2 61
540put b add k 3 61
541if ge m 2
542put b add k 2 get cr_b64a and shr x 6 63
543end
544if eq m 3
545put b add k 3 get cr_b64a and x 63
546end
547end
548
549fn cr_zero b edit uchar * to void
550for i ulong len b bound 9223372036854775807
551put b i 0
552end
553end