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 / tests / srv / smtp.om

304 lines · 5779 bytes Raw Blame History
1note ======== tests/srv/smtp.om: scriptable fake SMTP submission server (tests only, prefix fs_) ========
2note "srv smtp DIR SCRIPT COUNT": DIR/state holds "user NAME" and "pass SECRET"; COUNT connections
3note are served (COUNT 0: wait 3 seconds and fail with exit 5 if any connection arrives). SCRIPT:
4note smtps implicit TLS (the handshake first), then a normal session
5note sub STARTTLS submission: plaintext EHLO offers STARTTLS; after TLS, AUTH PLAIN
6note nostarttls plaintext EHLO does not offer STARTTLS
7note tls454 STARTTLS is answered "454"
8note inject STARTTLS is answered "220" and, in the same write, a plaintext "250 injected"
9note plainafter: STARTTLS is answered "220"; when the client's first TLS bytes arrive the server answers with a
10note plaintext SMTP line instead of a TLS record, and closes
11note authfail AUTH is answered "535"
12note noplain after TLS the server offers AUTH LOGIN only
13note Files: DIR/port, DIR/log (commands; credentials as ***, "PLAINTEXT AUTH" / "PLAINTEXT MAIL" when
14note they arrive before TLS), DIR/conns (connections accepted), DIR/N.eml (the DATA lines, dot-unstuffed,
15note each ended by CRLF) and DIR/N.env per message.
16global fs_v ulong 16
17note fs_v: 0 script, 1 authenticated, 2 messages, 3 connections
18text fs_s1 "smtps"
19text fs_s2 "sub"
20text fs_s3 "nostarttls"
21text fs_s4 "tls454"
22text fs_s5 "inject"
23text fs_s6 "plainafter"
24text fs_s7 "authfail"
25text fs_s8 "noplain"
26text fs_greet "220 smtp.example.test fake ESMTP"
27text fs_e1 "250-smtp.example.test"
28text fs_e2 "250-STARTTLS"
29text fs_e3 "250-AUTH PLAIN LOGIN"
30text fs_e3b "250-AUTH LOGIN"
31text fs_e4 "250 8BITMIME"
32text fs_go "220 go ahead"
33text fs_inj "250 injected"
34text fs_454 "454 4.7.0 TLS not available"
35text fs_nottls "250 plaintext, not TLS"
36text fs_235 "235 2.7.0 accepted"
37text fs_535 "535 5.7.8 refused"
38text fs_530 "530 5.7.0 authentication required"
39text fs_250 "250 ok"
40text fs_354 "354 go"
41text fs_250q "250 queued"
42text fs_221 "221 bye"
43text fs_500 "500 unknown"
44text fs_kehlo "EHLO "
45text fs_kstls "STARTTLS"
46text fs_kauth "AUTH PLAIN "
47text fs_kauthw "AUTH"
48text fs_kmail "MAIL FROM:"
49text fs_krcpt "RCPT TO:"
50text fs_kdata "DATA"
51text fs_kquit "QUIT"
52text fs_lauth "AUTH ***"
53text fs_lpauth "PLAINTEXT AUTH"
54text fs_lpmail "PLAINTEXT MAIL"
55text fs_ltlsf "TLS FAILED"
56text fs_ltls "TLS OK"
57text fs_lplain "PLAINTEXT REPLY TO CLIENT HELLO"
58text fs_lauthok "AUTH OK"
59text fs_lauthno "AUTH REFUSED"
60text fs_kconns "conns"
61text fs_keml ".eml"
62text fs_kenv ".env"
63
64fn fs_is t view uchar * to bool
65ret s_eqci S_Y 0 s_len S_Y t
66end
67
68fn fs_line t view uchar * to bool
69w_reset
70w_t t
71w_b 13
72w_b 10
73ret sv_ws S_W 0 s_len S_W
74end
75
76fn fs_pfx t view uchar * to bool
77if lt s_len S_AUX len t
78ret false
79end
80ret s_eqci S_AUX 0 len t t
81end
82
83fn fs_main scr ulong n ulong to int
84put fs_v 0 scr
85if not fi_load
86ret 2
87end
88if not sv_listen
89ret 3
90end
91if eq n 0
92if sv_accept 3000000000
93put fs_v 3 1
94fs_conns
95ret 5
96end
97fs_conns
98ret 0
99end
100for c ulong n bound 64
101if not sv_accept 60000000000
102fs_conns
103ret 4
104end
105put fs_v 3 add get fs_v 3 1
106fs_session
107sv_close
108end
109fs_conns
110ret 0
111end
112
113fn fs_conns to void
114w_reset
115w_u get fs_v 3
116w_b 10
117sv_path fs_kconns
118zl = f_put
119end
120
121fn fs_session to void
122put fs_v 1 0
123if eq get fs_v 0 1
124if not sv_tls
125sv_logt fs_ltlsf
126ret
127end
128sv_logt fs_ltls
129end
130if not fs_line fs_greet
131ret
132end
133while sv_line bound 9223372036854775807
134if not fs_cmd
135ret
136end
137end
138end
139
140fn fs_cmd to bool
141tls = eq get sv_v 1 1
142if fs_pfx fs_kauthw
143sv_logt fs_lauth
144if not tls
145sv_logt fs_lpauth
146end
147ret fs_auth
148end
149sv_logs S_AUX 0 s_len S_AUX
150if fs_pfx fs_kehlo
151ret fs_ehlo tls
152end
153if fs_pfx fs_kstls
154ret fs_starttls
155end
156if fs_pfx fs_kmail
157if not tls
158sv_logt fs_lpmail
159end
160if eq get fs_v 1 0
161ret fs_line fs_530
162end
163z = s_mem S_SEND
164ret fs_line fs_250
165end
166if fs_pfx fs_krcpt
167s_adds S_SEND S_AUX 0 s_len S_AUX
168z = s_add S_SEND 10
169ret fs_line fs_250
170end
171if fs_pfx fs_kdata
172ret fs_data
173end
174if fs_pfx fs_kquit
175z = fs_line fs_221
176ret false
177end
178ret fs_line fs_500
179end
180
181fn fs_ehlo tls bool to bool
182scr = get fs_v 0
183if not fs_line fs_e1
184ret false
185end
186if tls
187if eq scr 8
188z = fs_line fs_e3b
189else
190z = fs_line fs_e3
191end
192else
193if ne scr 3
194z = fs_line fs_e2
195end
196end
197ret fs_line fs_e4
198end
199
200fn fs_starttls to bool
201scr = get fs_v 0
202if eq scr 4
203ret fs_line fs_454
204end
205if eq scr 5
206w_reset
207w_t fs_go
208w_b 13
209w_b 10
210w_t fs_inj
211w_b 13
212w_b 10
213z = sv_ws S_W 0 s_len S_W
214if not sv_tls
215sv_logt fs_ltlsf
216ret false
217end
218ret true
219end
220if not fs_line fs_go
221ret false
222end
223if eq scr 6
224z = sv_until sv_fd 1 add monons 30000000000
225z = fs_line fs_nottls
226sv_logt fs_lplain
227ret false
228end
229if not sv_tls
230sv_logt fs_ltlsf
231ret false
232end
233sv_logt fs_ltls
234ret true
235end
236
237fn fs_auth to bool
238if not fs_pfx fs_kauth
239ret fs_line fs_500
240end
241z = s_mem S_TMP
242o = len fs_kauth
243if not sv_b64 S_AUX o sub s_len S_AUX o S_TMP
244sv_logt fs_lauthno
245ret fs_line fs_535
246end
247un = s_len S_RUL
248ok = eq s_len S_TMP add add un s_len S_SID 2
249ok = and ok eq s_get S_TMP 0 0
250ok = and ok s_eqss S_TMP 1 un S_RUL 0 un
251ok = and ok eq s_get S_TMP add un 1 0
252ok = and ok fi_passeq S_TMP add un 2 s_len S_SID
253if or not ok eq get fs_v 0 7
254sv_logt fs_lauthno
255ret fs_line fs_535
256end
257put fs_v 1 1
258sv_logt fs_lauthok
259ret fs_line fs_235
260end
261
262fn fs_data to bool
263if not fs_line fs_354
264ret false
265end
266z = s_mem S_MSG
267while true bound 9223372036854775807
268if not sv_line
269ret false
270end
271n = s_len S_AUX
272if and eq n 1 eq s_get S_AUX 0 46
273break
274end
275o = ulong 0
276if and gt n 0 eq s_get S_AUX 0 46
277o = 1
278end
279s_adds S_MSG S_AUX o sub n o
280z = s_add S_MSG 13
281z = s_add S_MSG 10
282end
283k = add get fs_v 2 1
284put fs_v 2 k
285w_reset
286w_s S_MSG 0 s_len S_MSG
287fs_pnum k fs_keml
288zl = f_put
289w_reset
290w_s S_SEND 0 s_len S_SEND
291fs_pnum k fs_kenv
292zl = f_put
293ret fs_line fs_250q
294end
295
296fn fs_pnum k ulong ext view uchar * to void
297p_reset
298for i ulong get sv_v 6 bound 4096
299p_c get sv_dir i
300end
301p_c 47
302p_u k
303p_t ext
304end