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