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