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 / ss.om

208 lines · 4359 bytes Raw Blame History
1note ======== src/ss.om: SMTP submission over TLS (SmtpTls, prefix ss_) ========
2note Config "smtp account:NAME" names an account in HOME/accounts (src/cr.om) whose smtp line is
3note smtps://HOST[:465] implicit TLS: the TLS handshake comes first
4note submission://HOST[:587] STARTTLS (RFC 3207): greeting, EHLO, the STARTTLS capability is
5note required, STARTTLS, 220, the TLS handshake, then EHLO again
6note ANY failure before the TLS handshake completes aborts the session: AUTH and MAIL are never sent
7note in plaintext. Authentication is AUTH PLAIN (RFC 4954, RFC 4616) over TLS only. The send gate
8note (sd_gate in src/send.om: policy, exact-draft approval, audit) runs before sm_submit is called,
9note so a refused send opens no connection at all.
10global ss_v ulong 4
11note ss_v: 0 capabilities of the last EHLO reply (1 STARTTLS, 2 AUTH PLAIN)
12text ss_kstls "STARTTLS"
13text ss_kauth "AUTH"
14text ss_kplain "PLAIN"
15text ss_auth "AUTH PLAIN "
16text ss_d "smtp: "
17text ss_e1 "the account has no smtp line"
18text ss_e2 "trust store could not be loaded"
19text ss_e3 "server does not offer STARTTLS: aborted before any credential or mail was sent"
20text ss_e4 "STARTTLS refused by the server: aborted before any credential or mail was sent"
21text ss_e5 "server does not offer AUTH PLAIN"
22text ss_e6 "authentication refused by the server"
23text ss_e7 "TLS is not established: refused to authenticate"
24text ss_e8 "bad connect address"
25
26note ss_submit: deliver S_MSG to the envelope s[o, +n) through account name S_CFG[ao, +an)
27fn ss_submit ao ulong an ulong s ulong o ulong n ulong to bool
28sec = new uchar 262144 0
29ok = false
30k = cr_open sec
31if eq k 0
32k = cr_find sec S_CFG ao an
33end
34if eq k 0
35if ss_session sec
36ok = sm_mail s o n
37if not ok
38tx_diag ss_d
39end
40end
41end
42cr_wipe sec
43tx_close
44ret ok
45end
46
47note ss_session: connect, TLS (implicit or STARTTLS), EHLO and AUTH PLAIN; the secret is used once
48fn ss_session sec edit uchar * to bool
49h = new uchar 256 0
50via = new uchar 16 0
51mode = get cr_v 8
52if eq mode 0
53diag2 ss_d ss_e1
54ret false
55end
56if lt cr_roots sec 0
57diag2 ss_d ss_e2
58ret false
59end
60hn = cr_copy sec get cr_v 5 get cr_v 6 h
61vn = cr_copy sec get cr_v 13 get cr_v 14 via
62if gt get cr_v 14 15
63diag2 ss_d ss_e8
64ret false
65end
66port = get cr_v 7
67if eq mode 1
68if ne tx_tls part h 0 hn port part via 0 vn 0
69tx_diag ss_d
70ret false
71end
72if not sm_reply 220
73tx_diag ss_d
74ret false
75end
76else
77if ne tx_tcp part h 0 hn port part via 0 vn 0
78tx_diag ss_d
79ret false
80end
81if not ss_starttls part h 0 hn
82ret false
83end
84end
85if not ss_caps
86tx_diag ss_d
87ret false
88end
89ret ss_login sec
90end
91
92note ss_starttls: plaintext greeting and EHLO, STARTTLS required, 220, then the handshake
93fn ss_starttls h view uchar * to bool
94if not sm_reply 220
95tx_diag ss_d
96ret false
97end
98if not ss_caps
99tx_diag ss_d
100ret false
101end
102if eq and get ss_v 0 1 0
103diag2 ss_d ss_e3
104ret false
105end
106w_reset
107w_t ss_kstls
108sm_crlf
109if not tx_wstore S_W 0 s_len S_W
110tx_diag ss_d
111ret false
112end
113if not sm_reply 220
114diag2 ss_d ss_e4
115ret false
116end
117if ne tx_starttls h 0
118tx_diag ss_d
119ret false
120end
121ret true
122end
123
124note ss_caps: send EHLO and read the reply, noting STARTTLS and AUTH ... PLAIN
125fn ss_caps to bool
126put ss_v 0 0
127if not sm_ehlo
128ret false
129end
130while true bound 9223372036854775807
131if not sm_line
132ret false
133end
134if not sm_code 250
135ret false
136end
137ss_capline
138if eq s_get S_AUX 3 32
139ret true
140end
141end
142ret false
143end
144
145fn ss_capline to void
146n = s_len S_AUX
147e = n
148while gt e 4 bound 9223372036854775807
149c = s_get S_AUX sub e 1
150if and and ne c 10 ne c 13 ne c 32
151break
152end
153e = sub e 1
154end
155if s_eqci S_AUX 4 sub e 4 ss_kstls
156put ss_v 0 or get ss_v 0 1
157ret
158end
159if lt e 9
160ret
161end
162if not s_eqci S_AUX 4 4 ss_kauth
163ret
164end
165p = ulong 8
166while lt p e bound 9223372036854775807
167while lt p e bound 9223372036854775807
168if ne s_get S_AUX p 32
169break
170end
171p = add p 1
172end
173q = p
174while lt q e bound 9223372036854775807
175if eq s_get S_AUX q 32
176break
177end
178q = add q 1
179end
180if s_eqci S_AUX p sub q p ss_kplain
181put ss_v 0 or get ss_v 0 2
182end
183p = q
184end
185end
186
187note ss_login: AUTH PLAIN with SASL initial response, only over TLS; 235 expected
188fn ss_login sec edit uchar * to bool
189if ne get tx_v 0 2
190diag2 ss_d ss_e7
191ret false
192end
193if eq and get ss_v 0 2 0
194diag2 ss_d ss_e5
195ret false
196end
197ok = cr_wplain sec ss_auth
198cr_wipe sec
199if not ok
200tx_diag ss_d
201ret false
202end
203if not sm_reply 235
204diag2 ss_d ss_e6
205ret false
206end
207ret true
208end