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