1 /* $OpenBSD: smtpd.h,v 1.679 2023/11/08 08:46:35 op Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> 5 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org> 6 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 #ifndef nitems 22 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) 23 #endif 24 25 #include <sys/queue.h> 26 #include <sys/tree.h> 27 #include <sys/socket.h> 28 #include <sys/time.h> 29 30 #include <event.h> 31 #include <imsg.h> 32 #include <limits.h> 33 #include <netdb.h> 34 #include <stdio.h> 35 36 #include "smtpd-defines.h" 37 #include "smtpd-api.h" 38 #include "ioev.h" 39 40 #define CHECK_IMSG_DATA_SIZE(imsg, expected_sz) do { \ 41 if ((imsg)->hdr.len - IMSG_HEADER_SIZE != (expected_sz)) \ 42 fatalx("smtpd: imsg %d: data size expected %zd got %zd",\ 43 (imsg)->hdr.type, \ 44 (expected_sz), (imsg)->hdr.len - IMSG_HEADER_SIZE); \ 45 } while (0) 46 47 #define CONF_FILE "/etc/mail/smtpd.conf" 48 #define MAILNAME_FILE "/etc/mail/mailname" 49 50 #define MAX_HOPS_COUNT 100 51 #define DEFAULT_MAX_BODY_SIZE (35*1024*1024) 52 53 #define EXPAND_BUFFER 1024 54 55 #define SMTPD_QUEUE_EXPIRY (4 * 24 * 60 * 60) 56 #define SMTPD_SOCKET "/var/run/smtpd.sock" 57 #define SMTPD_NAME "OpenSMTPD" 58 #define SMTPD_VERSION "7.4.0" 59 #define SMTPD_SESSION_TIMEOUT 300 60 #define SMTPD_BACKLOG 5 61 62 #define PATH_SMTPCTL "/usr/sbin/smtpctl" 63 64 #define PATH_OFFLINE "/offline" 65 #define PATH_PURGE "/purge" 66 #define PATH_TEMPORARY "/temporary" 67 68 #define PATH_LIBEXEC "/usr/local/libexec/smtpd" 69 70 71 /* 72 * RFC 5322 defines these characters as valid, some of them are 73 * potentially dangerous and need to be escaped. 74 */ 75 #define MAILADDR_ALLOWED "!#$%&'*/?^`{|}~+-=_" 76 #define MAILADDR_ESCAPE "!#$%&'*?`{|}~" 77 78 79 #define F_STARTTLS 0x01 80 #define F_SMTPS 0x02 81 #define F_SSL (F_STARTTLS | F_SMTPS) 82 #define F_AUTH 0x08 83 #define F_STARTTLS_REQUIRE 0x20 84 #define F_AUTH_REQUIRE 0x40 85 #define F_MASK_SOURCE 0x100 86 #define F_TLS_VERIFY 0x200 87 #define F_EXT_DSN 0x400 88 #define F_RECEIVEDAUTH 0x800 89 #define F_MASQUERADE 0x1000 90 #define F_FILTERED 0x2000 91 #define F_PROXY 0x4000 92 93 #define RELAY_TLS_OPPORTUNISTIC 0 94 #define RELAY_TLS_STARTTLS 1 95 #define RELAY_TLS_SMTPS 2 96 #define RELAY_TLS_NO 3 97 98 #define RELAY_AUTH 0x08 99 #define RELAY_LMTP 0x80 100 101 #define MTA_EXT_DSN 0x400 102 103 104 #define P_SENDMAIL 0 105 #define P_NEWALIASES 1 106 #define P_MAKEMAP 2 107 108 struct userinfo { 109 char username[SMTPD_VUSERNAME_SIZE]; 110 char directory[PATH_MAX]; 111 uid_t uid; 112 gid_t gid; 113 }; 114 115 struct netaddr { 116 struct sockaddr_storage ss; 117 int bits; 118 }; 119 120 struct relayhost { 121 uint16_t flags; 122 int tls; 123 char hostname[HOST_NAME_MAX+1]; 124 uint16_t port; 125 char authlabel[PATH_MAX]; 126 }; 127 128 struct credentials { 129 char username[LINE_MAX]; 130 char password[LINE_MAX]; 131 }; 132 133 struct destination { 134 char name[HOST_NAME_MAX+1]; 135 }; 136 137 struct source { 138 struct sockaddr_storage addr; 139 }; 140 141 struct addrname { 142 struct sockaddr_storage addr; 143 char name[HOST_NAME_MAX+1]; 144 }; 145 146 union lookup { 147 struct expand *expand; 148 struct credentials creds; 149 struct netaddr netaddr; 150 struct source source; 151 struct destination domain; 152 struct userinfo userinfo; 153 struct mailaddr mailaddr; 154 struct addrname addrname; 155 struct maddrmap *maddrmap; 156 char relayhost[LINE_MAX]; 157 }; 158 159 /* 160 * Bump IMSG_VERSION whenever a change is made to enum imsg_type. 161 * This will ensure that we can never use a wrong version of smtpctl with smtpd. 162 */ 163 #define IMSG_VERSION 16 164 165 enum imsg_type { 166 IMSG_NONE, 167 168 IMSG_CTL_OK, 169 IMSG_CTL_FAIL, 170 171 IMSG_CTL_GET_DIGEST, 172 IMSG_CTL_GET_STATS, 173 IMSG_CTL_LIST_MESSAGES, 174 IMSG_CTL_LIST_ENVELOPES, 175 IMSG_CTL_MTA_SHOW_HOSTS, 176 IMSG_CTL_MTA_SHOW_RELAYS, 177 IMSG_CTL_MTA_SHOW_ROUTES, 178 IMSG_CTL_MTA_SHOW_HOSTSTATS, 179 IMSG_CTL_MTA_BLOCK, 180 IMSG_CTL_MTA_UNBLOCK, 181 IMSG_CTL_MTA_SHOW_BLOCK, 182 IMSG_CTL_PAUSE_EVP, 183 IMSG_CTL_PAUSE_MDA, 184 IMSG_CTL_PAUSE_MTA, 185 IMSG_CTL_PAUSE_SMTP, 186 IMSG_CTL_PROFILE, 187 IMSG_CTL_PROFILE_DISABLE, 188 IMSG_CTL_PROFILE_ENABLE, 189 IMSG_CTL_RESUME_EVP, 190 IMSG_CTL_RESUME_MDA, 191 IMSG_CTL_RESUME_MTA, 192 IMSG_CTL_RESUME_SMTP, 193 IMSG_CTL_RESUME_ROUTE, 194 IMSG_CTL_REMOVE, 195 IMSG_CTL_SCHEDULE, 196 IMSG_CTL_SHOW_STATUS, 197 IMSG_CTL_TRACE_DISABLE, 198 IMSG_CTL_TRACE_ENABLE, 199 IMSG_CTL_UPDATE_TABLE, 200 IMSG_CTL_VERBOSE, 201 IMSG_CTL_DISCOVER_EVPID, 202 IMSG_CTL_DISCOVER_MSGID, 203 204 IMSG_CTL_SMTP_SESSION, 205 206 IMSG_GETADDRINFO, 207 IMSG_GETADDRINFO_END, 208 IMSG_GETNAMEINFO, 209 IMSG_RES_QUERY, 210 211 IMSG_SETUP_KEY, 212 IMSG_SETUP_PEER, 213 IMSG_SETUP_DONE, 214 215 IMSG_CONF_START, 216 IMSG_CONF_END, 217 218 IMSG_STAT_INCREMENT, 219 IMSG_STAT_DECREMENT, 220 IMSG_STAT_SET, 221 222 IMSG_LKA_AUTHENTICATE, 223 IMSG_LKA_OPEN_FORWARD, 224 IMSG_LKA_ENVELOPE_SUBMIT, 225 IMSG_LKA_ENVELOPE_COMMIT, 226 227 IMSG_QUEUE_DELIVER, 228 IMSG_QUEUE_DELIVERY_OK, 229 IMSG_QUEUE_DELIVERY_TEMPFAIL, 230 IMSG_QUEUE_DELIVERY_PERMFAIL, 231 IMSG_QUEUE_DELIVERY_LOOP, 232 IMSG_QUEUE_DISCOVER_EVPID, 233 IMSG_QUEUE_DISCOVER_MSGID, 234 IMSG_QUEUE_ENVELOPE_ACK, 235 IMSG_QUEUE_ENVELOPE_COMMIT, 236 IMSG_QUEUE_ENVELOPE_REMOVE, 237 IMSG_QUEUE_ENVELOPE_SCHEDULE, 238 IMSG_QUEUE_ENVELOPE_SUBMIT, 239 IMSG_QUEUE_HOLDQ_HOLD, 240 IMSG_QUEUE_HOLDQ_RELEASE, 241 IMSG_QUEUE_MESSAGE_COMMIT, 242 IMSG_QUEUE_MESSAGE_ROLLBACK, 243 IMSG_QUEUE_SMTP_SESSION, 244 IMSG_QUEUE_TRANSFER, 245 246 IMSG_MDA_DELIVERY_OK, 247 IMSG_MDA_DELIVERY_TEMPFAIL, 248 IMSG_MDA_DELIVERY_PERMFAIL, 249 IMSG_MDA_DELIVERY_LOOP, 250 IMSG_MDA_DELIVERY_HOLD, 251 IMSG_MDA_DONE, 252 IMSG_MDA_FORK, 253 IMSG_MDA_HOLDQ_RELEASE, 254 IMSG_MDA_LOOKUP_USERINFO, 255 IMSG_MDA_KILL, 256 IMSG_MDA_OPEN_MESSAGE, 257 258 IMSG_MTA_DELIVERY_OK, 259 IMSG_MTA_DELIVERY_TEMPFAIL, 260 IMSG_MTA_DELIVERY_PERMFAIL, 261 IMSG_MTA_DELIVERY_LOOP, 262 IMSG_MTA_DELIVERY_HOLD, 263 IMSG_MTA_DNS_HOST, 264 IMSG_MTA_DNS_HOST_END, 265 IMSG_MTA_DNS_MX, 266 IMSG_MTA_DNS_MX_PREFERENCE, 267 IMSG_MTA_HOLDQ_RELEASE, 268 IMSG_MTA_LOOKUP_CREDENTIALS, 269 IMSG_MTA_LOOKUP_SOURCE, 270 IMSG_MTA_LOOKUP_HELO, 271 IMSG_MTA_LOOKUP_SMARTHOST, 272 IMSG_MTA_OPEN_MESSAGE, 273 IMSG_MTA_SCHEDULE, 274 275 IMSG_SCHED_ENVELOPE_BOUNCE, 276 IMSG_SCHED_ENVELOPE_DELIVER, 277 IMSG_SCHED_ENVELOPE_EXPIRE, 278 IMSG_SCHED_ENVELOPE_INJECT, 279 IMSG_SCHED_ENVELOPE_REMOVE, 280 IMSG_SCHED_ENVELOPE_TRANSFER, 281 282 IMSG_SMTP_AUTHENTICATE, 283 IMSG_SMTP_MESSAGE_COMMIT, 284 IMSG_SMTP_MESSAGE_CREATE, 285 IMSG_SMTP_MESSAGE_ROLLBACK, 286 IMSG_SMTP_MESSAGE_OPEN, 287 IMSG_SMTP_CHECK_SENDER, 288 IMSG_SMTP_EXPAND_RCPT, 289 IMSG_SMTP_LOOKUP_HELO, 290 291 IMSG_SMTP_REQ_CONNECT, 292 IMSG_SMTP_REQ_HELO, 293 IMSG_SMTP_REQ_MAIL, 294 IMSG_SMTP_REQ_RCPT, 295 IMSG_SMTP_REQ_DATA, 296 IMSG_SMTP_REQ_EOM, 297 IMSG_SMTP_EVENT_RSET, 298 IMSG_SMTP_EVENT_COMMIT, 299 IMSG_SMTP_EVENT_ROLLBACK, 300 IMSG_SMTP_EVENT_DISCONNECT, 301 302 IMSG_LKA_PROCESSOR_FORK, 303 IMSG_LKA_PROCESSOR_ERRFD, 304 305 IMSG_REPORT_SMTP_LINK_CONNECT, 306 IMSG_REPORT_SMTP_LINK_DISCONNECT, 307 IMSG_REPORT_SMTP_LINK_GREETING, 308 IMSG_REPORT_SMTP_LINK_IDENTIFY, 309 IMSG_REPORT_SMTP_LINK_TLS, 310 IMSG_REPORT_SMTP_LINK_AUTH, 311 IMSG_REPORT_SMTP_TX_RESET, 312 IMSG_REPORT_SMTP_TX_BEGIN, 313 IMSG_REPORT_SMTP_TX_MAIL, 314 IMSG_REPORT_SMTP_TX_RCPT, 315 IMSG_REPORT_SMTP_TX_ENVELOPE, 316 IMSG_REPORT_SMTP_TX_DATA, 317 IMSG_REPORT_SMTP_TX_COMMIT, 318 IMSG_REPORT_SMTP_TX_ROLLBACK, 319 IMSG_REPORT_SMTP_PROTOCOL_CLIENT, 320 IMSG_REPORT_SMTP_PROTOCOL_SERVER, 321 IMSG_REPORT_SMTP_FILTER_RESPONSE, 322 IMSG_REPORT_SMTP_TIMEOUT, 323 324 IMSG_FILTER_SMTP_BEGIN, 325 IMSG_FILTER_SMTP_END, 326 IMSG_FILTER_SMTP_PROTOCOL, 327 IMSG_FILTER_SMTP_DATA_BEGIN, 328 IMSG_FILTER_SMTP_DATA_END, 329 330 IMSG_CA_RSA_PRIVENC, 331 IMSG_CA_RSA_PRIVDEC, 332 IMSG_CA_ECDSA_SIGN, 333 }; 334 335 enum smtp_proc_type { 336 PROC_PARENT = 0, 337 PROC_LKA, 338 PROC_QUEUE, 339 PROC_CONTROL, 340 PROC_SCHEDULER, 341 PROC_DISPATCHER, 342 PROC_CA, 343 PROC_PROCESSOR, 344 PROC_CLIENT, 345 }; 346 347 enum table_type { 348 T_NONE = 0, 349 T_DYNAMIC = 0x01, /* table with external source */ 350 T_LIST = 0x02, /* table holding a list */ 351 T_HASH = 0x04, /* table holding a hash table */ 352 }; 353 354 struct table { 355 char t_name[LINE_MAX]; 356 enum table_type t_type; 357 char t_config[PATH_MAX]; 358 359 void *t_handle; 360 struct table_backend *t_backend; 361 }; 362 363 struct table_backend { 364 const char *name; 365 const unsigned int services; 366 int (*config)(struct table *); 367 int (*add)(struct table *, const char *, const char *); 368 void (*dump)(struct table *); 369 int (*open)(struct table *); 370 int (*update)(struct table *); 371 void (*close)(struct table *); 372 int (*lookup)(struct table *, enum table_service, const char *, char **); 373 int (*fetch)(struct table *, enum table_service, char **); 374 }; 375 376 377 enum bounce_type { 378 B_FAILED, 379 B_DELAYED, 380 B_DELIVERED 381 }; 382 383 enum dsn_ret { 384 DSN_RETFULL = 1, 385 DSN_RETHDRS 386 }; 387 388 struct delivery_bounce { 389 enum bounce_type type; 390 time_t delay; 391 time_t ttl; 392 enum dsn_ret dsn_ret; 393 int mta_without_dsn; 394 }; 395 396 enum expand_type { 397 EXPAND_INVALID, 398 EXPAND_USERNAME, 399 EXPAND_FILENAME, 400 EXPAND_FILTER, 401 EXPAND_INCLUDE, 402 EXPAND_ADDRESS, 403 EXPAND_ERROR, 404 }; 405 406 enum filter_phase { 407 FILTER_CONNECT, 408 FILTER_HELO, 409 FILTER_EHLO, 410 FILTER_STARTTLS, 411 FILTER_AUTH, 412 FILTER_MAIL_FROM, 413 FILTER_RCPT_TO, 414 FILTER_DATA, 415 FILTER_DATA_LINE, 416 FILTER_RSET, 417 FILTER_QUIT, 418 FILTER_NOOP, 419 FILTER_HELP, 420 FILTER_WIZ, 421 FILTER_COMMIT, 422 FILTER_PHASES_COUNT /* must be last */ 423 }; 424 425 struct expandnode { 426 RB_ENTRY(expandnode) entry; 427 TAILQ_ENTRY(expandnode) tq_entry; 428 enum expand_type type; 429 int sameuser; 430 int realuser; 431 int forwarded; 432 struct rule *rule; 433 struct expandnode *parent; 434 unsigned int depth; 435 union { 436 /* 437 * user field handles both expansion user and system user 438 * so we MUST make it large enough to fit a mailaddr user 439 */ 440 char user[SMTPD_MAXLOCALPARTSIZE]; 441 char buffer[EXPAND_BUFFER]; 442 struct mailaddr mailaddr; 443 } u; 444 char subaddress[SMTPD_SUBADDRESS_SIZE]; 445 }; 446 447 struct expand { 448 RB_HEAD(expandtree, expandnode) tree; 449 TAILQ_HEAD(xnodes, expandnode) *queue; 450 size_t nb_nodes; 451 struct rule *rule; 452 struct expandnode *parent; 453 }; 454 455 struct maddrnode { 456 TAILQ_ENTRY(maddrnode) entries; 457 struct mailaddr mailaddr; 458 }; 459 460 struct maddrmap { 461 TAILQ_HEAD(xmaddr, maddrnode) queue; 462 }; 463 464 #define DSN_SUCCESS 0x01 465 #define DSN_FAILURE 0x02 466 #define DSN_DELAY 0x04 467 #define DSN_NEVER 0x08 468 469 #define DSN_ENVID_LEN 100 470 471 #define SMTPD_ENVELOPE_VERSION 3 472 struct envelope { 473 TAILQ_ENTRY(envelope) entry; 474 475 char dispatcher[HOST_NAME_MAX+1]; 476 477 char tag[SMTPD_TAG_SIZE]; 478 479 uint32_t version; 480 uint64_t id; 481 enum envelope_flags flags; 482 483 char smtpname[HOST_NAME_MAX+1]; 484 char helo[HOST_NAME_MAX+1]; 485 char hostname[HOST_NAME_MAX+1]; 486 char username[SMTPD_MAXMAILADDRSIZE]; 487 char errorline[LINE_MAX]; 488 struct sockaddr_storage ss; 489 490 struct mailaddr sender; 491 struct mailaddr rcpt; 492 struct mailaddr dest; 493 494 char mda_user[SMTPD_VUSERNAME_SIZE]; 495 char mda_subaddress[SMTPD_SUBADDRESS_SIZE]; 496 char mda_exec[LINE_MAX]; 497 498 enum delivery_type type; 499 union { 500 struct delivery_bounce bounce; 501 } agent; 502 503 uint16_t retry; 504 time_t creation; 505 time_t ttl; 506 time_t lasttry; 507 time_t nexttry; 508 time_t lastbounce; 509 510 struct mailaddr dsn_orcpt; 511 char dsn_envid[DSN_ENVID_LEN+1]; 512 uint8_t dsn_notify; 513 enum dsn_ret dsn_ret; 514 515 uint8_t esc_class; 516 uint8_t esc_code; 517 }; 518 519 struct listener { 520 uint16_t flags; 521 int fd; 522 struct sockaddr_storage ss; 523 in_port_t port; 524 struct timeval timeout; 525 struct event ev; 526 char filter_name[PATH_MAX]; 527 char pki_name[PATH_MAX]; 528 char ca_name[PATH_MAX]; 529 char tag[SMTPD_TAG_SIZE]; 530 char authtable[LINE_MAX]; 531 char hostname[HOST_NAME_MAX+1]; 532 char hostnametable[PATH_MAX]; 533 char sendertable[PATH_MAX]; 534 535 TAILQ_ENTRY(listener) entry; 536 537 int local; /* there must be a better way */ 538 539 char *tls_protocols; 540 char *tls_ciphers; 541 struct tls *tls; 542 struct pki **pki; 543 int pkicount; 544 }; 545 546 struct smtpd { 547 char sc_conffile[PATH_MAX]; 548 size_t sc_maxsize; 549 550 #define SMTPD_OPT_VERBOSE 0x00000001 551 #define SMTPD_OPT_NOACTION 0x00000002 552 uint32_t sc_opts; 553 554 #define SMTPD_EXITING 0x00000001 /* unused */ 555 #define SMTPD_MDA_PAUSED 0x00000002 556 #define SMTPD_MTA_PAUSED 0x00000004 557 #define SMTPD_SMTP_PAUSED 0x00000008 558 #define SMTPD_MDA_BUSY 0x00000010 559 #define SMTPD_MTA_BUSY 0x00000020 560 #define SMTPD_BOUNCE_BUSY 0x00000040 561 #define SMTPD_SMTP_DISABLED 0x00000080 562 uint32_t sc_flags; 563 564 #define QUEUE_COMPRESSION 0x00000001 565 #define QUEUE_ENCRYPTION 0x00000002 566 #define QUEUE_EVPCACHE 0x00000004 567 uint32_t sc_queue_flags; 568 char *sc_queue_key; 569 size_t sc_queue_evpcache_size; 570 571 size_t sc_session_max_rcpt; 572 size_t sc_session_max_mails; 573 574 struct dict *sc_mda_wrappers; 575 size_t sc_mda_max_session; 576 size_t sc_mda_max_user_session; 577 size_t sc_mda_task_hiwat; 578 size_t sc_mda_task_lowat; 579 size_t sc_mda_task_release; 580 581 size_t sc_mta_max_deferred; 582 583 size_t sc_scheduler_max_inflight; 584 size_t sc_scheduler_max_evp_batch_size; 585 size_t sc_scheduler_max_msg_batch_size; 586 size_t sc_scheduler_max_schedule; 587 588 struct dict *sc_filter_processes_dict; 589 590 int sc_ttl; 591 #define MAX_BOUNCE_WARN 4 592 time_t sc_bounce_warn[MAX_BOUNCE_WARN]; 593 char sc_hostname[HOST_NAME_MAX+1]; 594 struct stat_backend *sc_stat; 595 struct compress_backend *sc_comp; 596 597 time_t sc_uptime; 598 599 /* This is a listener for a local socket used by smtp_enqueue(). */ 600 struct listener *sc_sock_listener; 601 602 TAILQ_HEAD(listenerlist, listener) *sc_listeners; 603 604 TAILQ_HEAD(rulelist, rule) *sc_rules; 605 606 607 struct dict *sc_filters_dict; 608 struct dict *sc_dispatchers; 609 struct dispatcher *sc_dispatcher_bounce; 610 611 struct dict *sc_ca_dict; 612 struct dict *sc_pki_dict; 613 struct dict *sc_ssl_dict; 614 615 struct dict *sc_tables_dict; /* keyed lookup */ 616 617 struct dict *sc_limits_dict; 618 619 char *sc_tls_ciphers; 620 621 char *sc_subaddressing_delim; 622 623 char *sc_srs_key; 624 char *sc_srs_key_backup; 625 int sc_srs_ttl; 626 627 char *sc_admd; 628 }; 629 630 #define TRACE_DEBUG 0x0001 631 #define TRACE_IMSG 0x0002 632 #define TRACE_IO 0x0004 633 #define TRACE_SMTP 0x0008 634 #define TRACE_FILTERS 0x0010 635 #define TRACE_MTA 0x0020 636 #define TRACE_BOUNCE 0x0040 637 #define TRACE_SCHEDULER 0x0080 638 #define TRACE_LOOKUP 0x0100 639 #define TRACE_STAT 0x0200 640 #define TRACE_RULES 0x0400 641 #define TRACE_MPROC 0x0800 642 #define TRACE_EXPAND 0x1000 643 #define TRACE_TABLES 0x2000 644 #define TRACE_QUEUE 0x4000 645 646 #define PROFILE_TOSTAT 0x0001 647 #define PROFILE_IMSG 0x0002 648 #define PROFILE_QUEUE 0x0004 649 650 struct forward_req { 651 uint64_t id; 652 uint8_t status; 653 654 char user[SMTPD_VUSERNAME_SIZE]; 655 uid_t uid; 656 gid_t gid; 657 char directory[PATH_MAX]; 658 }; 659 660 struct deliver { 661 char dispatcher[EXPAND_BUFFER]; 662 663 struct mailaddr sender; 664 struct mailaddr rcpt; 665 struct mailaddr dest; 666 667 char mda_subaddress[SMTPD_SUBADDRESS_SIZE]; 668 char mda_exec[LINE_MAX]; 669 670 struct userinfo userinfo; 671 }; 672 673 struct mta_host { 674 SPLAY_ENTRY(mta_host) entry; 675 struct sockaddr *sa; 676 char *ptrname; 677 int refcount; 678 size_t nconn; 679 time_t lastconn; 680 time_t lastptrquery; 681 682 #define HOST_IGNORE 0x01 683 int flags; 684 }; 685 686 struct mta_mx { 687 TAILQ_ENTRY(mta_mx) entry; 688 struct mta_host *host; 689 char *mxname; 690 int preference; 691 }; 692 693 struct mta_domain { 694 SPLAY_ENTRY(mta_domain) entry; 695 char *name; 696 int as_host; 697 TAILQ_HEAD(, mta_mx) mxs; 698 int mxstatus; 699 int refcount; 700 size_t nconn; 701 time_t lastconn; 702 time_t lastmxquery; 703 }; 704 705 struct mta_source { 706 SPLAY_ENTRY(mta_source) entry; 707 struct sockaddr *sa; 708 int refcount; 709 size_t nconn; 710 time_t lastconn; 711 }; 712 713 struct mta_connector { 714 struct mta_source *source; 715 struct mta_relay *relay; 716 717 #define CONNECTOR_ERROR_FAMILY 0x0001 718 #define CONNECTOR_ERROR_SOURCE 0x0002 719 #define CONNECTOR_ERROR_MX 0x0004 720 #define CONNECTOR_ERROR_ROUTE_NET 0x0008 721 #define CONNECTOR_ERROR_ROUTE_SMTP 0x0010 722 #define CONNECTOR_ERROR_ROUTE 0x0018 723 #define CONNECTOR_ERROR_BLOCKED 0x0020 724 #define CONNECTOR_ERROR 0x00ff 725 726 #define CONNECTOR_LIMIT_HOST 0x0100 727 #define CONNECTOR_LIMIT_ROUTE 0x0200 728 #define CONNECTOR_LIMIT_SOURCE 0x0400 729 #define CONNECTOR_LIMIT_RELAY 0x0800 730 #define CONNECTOR_LIMIT_CONN 0x1000 731 #define CONNECTOR_LIMIT_DOMAIN 0x2000 732 #define CONNECTOR_LIMIT 0xff00 733 734 #define CONNECTOR_NEW 0x10000 735 #define CONNECTOR_WAIT 0x20000 736 int flags; 737 738 int refcount; 739 size_t nconn; 740 time_t lastconn; 741 }; 742 743 struct mta_route { 744 SPLAY_ENTRY(mta_route) entry; 745 uint64_t id; 746 struct mta_source *src; 747 struct mta_host *dst; 748 #define ROUTE_NEW 0x01 749 #define ROUTE_RUNQ 0x02 750 #define ROUTE_KEEPALIVE 0x04 751 #define ROUTE_DISABLED 0xf0 752 #define ROUTE_DISABLED_NET 0x10 753 #define ROUTE_DISABLED_SMTP 0x20 754 int flags; 755 int nerror; 756 int penalty; 757 int refcount; 758 size_t nconn; 759 time_t lastconn; 760 time_t lastdisc; 761 time_t lastpenalty; 762 }; 763 764 struct mta_limits { 765 size_t maxconn_per_host; 766 size_t maxconn_per_route; 767 size_t maxconn_per_source; 768 size_t maxconn_per_connector; 769 size_t maxconn_per_relay; 770 size_t maxconn_per_domain; 771 772 time_t conndelay_host; 773 time_t conndelay_route; 774 time_t conndelay_source; 775 time_t conndelay_connector; 776 time_t conndelay_relay; 777 time_t conndelay_domain; 778 779 time_t discdelay_route; 780 781 size_t max_mail_per_session; 782 time_t sessdelay_transaction; 783 time_t sessdelay_keepalive; 784 785 size_t max_failures_per_session; 786 787 int family; 788 789 int task_hiwat; 790 int task_lowat; 791 int task_release; 792 }; 793 794 struct mta_relay { 795 SPLAY_ENTRY(mta_relay) entry; 796 uint64_t id; 797 798 struct dispatcher *dispatcher; 799 struct mta_domain *domain; 800 struct mta_limits *limits; 801 int tls; 802 int flags; 803 char *backupname; 804 int backuppref; 805 char *sourcetable; 806 uint16_t port; 807 char *pki_name; 808 char *ca_name; 809 char *authtable; 810 char *authlabel; 811 char *helotable; 812 char *heloname; 813 char *secret; 814 int srs; 815 816 int state; 817 size_t ntask; 818 TAILQ_HEAD(, mta_task) tasks; 819 820 struct tree connectors; 821 size_t sourceloop; 822 time_t lastsource; 823 time_t nextsource; 824 825 int fail; 826 char *failstr; 827 828 #define RELAY_WAIT_MX 0x01 829 #define RELAY_WAIT_PREFERENCE 0x02 830 #define RELAY_WAIT_SECRET 0x04 831 #define RELAY_WAIT_LIMITS 0x08 832 #define RELAY_WAIT_SOURCE 0x10 833 #define RELAY_WAIT_CONNECTOR 0x20 834 #define RELAY_WAIT_SMARTHOST 0x40 835 #define RELAY_WAITMASK 0x7f 836 int status; 837 838 int refcount; 839 size_t nconn; 840 size_t nconn_ready; 841 time_t lastconn; 842 }; 843 844 struct mta_envelope { 845 TAILQ_ENTRY(mta_envelope) entry; 846 uint64_t id; 847 uint64_t session; 848 time_t creation; 849 char *smtpname; 850 char *dest; 851 char *rcpt; 852 struct mta_task *task; 853 int delivery; 854 855 int ext; 856 char *dsn_orcpt; 857 char dsn_envid[DSN_ENVID_LEN+1]; 858 uint8_t dsn_notify; 859 enum dsn_ret dsn_ret; 860 861 char status[LINE_MAX]; 862 }; 863 864 struct mta_task { 865 TAILQ_ENTRY(mta_task) entry; 866 struct mta_relay *relay; 867 uint32_t msgid; 868 TAILQ_HEAD(, mta_envelope) envelopes; 869 char *sender; 870 }; 871 872 struct passwd; 873 874 struct queue_backend { 875 int (*init)(struct passwd *, int, const char *); 876 }; 877 878 struct compress_backend { 879 size_t (*compress_chunk)(void *, size_t, void *, size_t); 880 size_t (*uncompress_chunk)(void *, size_t, void *, size_t); 881 int (*compress_file)(FILE *, FILE *); 882 int (*uncompress_file)(FILE *, FILE *); 883 }; 884 885 /* auth structures */ 886 enum auth_type { 887 AUTH_BSD, 888 AUTH_PWD, 889 }; 890 891 struct auth_backend { 892 int (*authenticate)(char *, char *); 893 }; 894 895 struct scheduler_backend { 896 int (*init)(const char *); 897 898 int (*insert)(struct scheduler_info *); 899 size_t (*commit)(uint32_t); 900 size_t (*rollback)(uint32_t); 901 902 int (*update)(struct scheduler_info *); 903 int (*delete)(uint64_t); 904 int (*hold)(uint64_t, uint64_t); 905 int (*release)(int, uint64_t, int); 906 907 int (*batch)(int, int*, size_t*, uint64_t*, int*); 908 909 size_t (*messages)(uint32_t, uint32_t *, size_t); 910 size_t (*envelopes)(uint64_t, struct evpstate *, size_t); 911 int (*schedule)(uint64_t); 912 int (*remove)(uint64_t); 913 int (*suspend)(uint64_t); 914 int (*resume)(uint64_t); 915 int (*query)(uint64_t); 916 }; 917 918 enum stat_type { 919 STAT_COUNTER, 920 STAT_TIMESTAMP, 921 STAT_TIMEVAL, 922 STAT_TIMESPEC, 923 }; 924 925 struct stat_value { 926 enum stat_type type; 927 union stat_v { 928 size_t counter; 929 time_t timestamp; 930 struct timeval tv; 931 struct timespec ts; 932 } u; 933 }; 934 935 #define STAT_KEY_SIZE 1024 936 struct stat_kv { 937 void *iter; 938 char key[STAT_KEY_SIZE]; 939 struct stat_value val; 940 }; 941 942 struct stat_backend { 943 void (*init)(void); 944 void (*close)(void); 945 void (*increment)(const char *, size_t); 946 void (*decrement)(const char *, size_t); 947 void (*set)(const char *, const struct stat_value *); 948 int (*iter)(void **, char **, struct stat_value *); 949 }; 950 951 struct stat_digest { 952 time_t startup; 953 time_t timestamp; 954 955 size_t clt_connect; 956 size_t clt_disconnect; 957 958 size_t evp_enqueued; 959 size_t evp_dequeued; 960 961 size_t evp_expired; 962 size_t evp_removed; 963 size_t evp_bounce; 964 965 size_t dlv_ok; 966 size_t dlv_permfail; 967 size_t dlv_tempfail; 968 size_t dlv_loop; 969 }; 970 971 972 struct mproc { 973 pid_t pid; 974 char *name; 975 int proc; 976 void (*handler)(struct mproc *, struct imsg *); 977 struct imsgbuf imsgbuf; 978 979 char *m_buf; 980 size_t m_alloc; 981 size_t m_pos; 982 uint32_t m_type; 983 uint32_t m_peerid; 984 pid_t m_pid; 985 int m_fd; 986 987 int enable; 988 short events; 989 struct event ev; 990 void *data; 991 }; 992 993 struct msg { 994 const uint8_t *pos; 995 const uint8_t *end; 996 }; 997 998 extern enum smtp_proc_type smtpd_process; 999 1000 extern int tracing; 1001 extern int foreground_log; 1002 extern int profiling; 1003 1004 extern struct mproc *p_control; 1005 extern struct mproc *p_parent; 1006 extern struct mproc *p_lka; 1007 extern struct mproc *p_queue; 1008 extern struct mproc *p_scheduler; 1009 extern struct mproc *p_dispatcher; 1010 extern struct mproc *p_ca; 1011 1012 extern struct smtpd *env; 1013 extern void (*imsg_callback)(struct mproc *, struct imsg *); 1014 1015 /* inter-process structures */ 1016 1017 struct bounce_req_msg { 1018 uint64_t evpid; 1019 time_t timestamp; 1020 struct delivery_bounce bounce; 1021 }; 1022 1023 enum dns_error { 1024 DNS_OK = 0, 1025 DNS_RETRY, 1026 DNS_EINVAL, 1027 DNS_ENONAME, 1028 DNS_ENOTFOUND, 1029 /* RFC 7505 */ 1030 DNS_NULLMX, 1031 }; 1032 1033 enum lka_resp_status { 1034 LKA_OK, 1035 LKA_TEMPFAIL, 1036 LKA_PERMFAIL 1037 }; 1038 1039 enum filter_type { 1040 FILTER_TYPE_BUILTIN, 1041 FILTER_TYPE_PROC, 1042 FILTER_TYPE_CHAIN, 1043 }; 1044 1045 enum filter_subsystem { 1046 FILTER_SUBSYSTEM_SMTP_IN = 1<<0, 1047 FILTER_SUBSYSTEM_SMTP_OUT = 1<<1, 1048 }; 1049 1050 struct filter_proc { 1051 const char *command; 1052 const char *user; 1053 const char *group; 1054 const char *chroot; 1055 int errfd; 1056 enum filter_subsystem filter_subsystem; 1057 }; 1058 1059 struct filter_config { 1060 char *name; 1061 enum filter_subsystem filter_subsystem; 1062 enum filter_type filter_type; 1063 enum filter_phase phase; 1064 char *reject; 1065 char *disconnect; 1066 char *rewrite; 1067 char *report; 1068 uint8_t junk; 1069 uint8_t bypass; 1070 char *proc; 1071 1072 const char **chain; 1073 size_t chain_size; 1074 struct dict chain_procs; 1075 1076 int8_t not_fcrdns; 1077 int8_t fcrdns; 1078 1079 int8_t not_rdns; 1080 int8_t rdns; 1081 1082 int8_t not_rdns_table; 1083 struct table *rdns_table; 1084 1085 int8_t not_rdns_regex; 1086 struct table *rdns_regex; 1087 1088 int8_t not_src_table; 1089 struct table *src_table; 1090 1091 int8_t not_src_regex; 1092 struct table *src_regex; 1093 1094 int8_t not_helo_table; 1095 struct table *helo_table; 1096 1097 int8_t not_helo_regex; 1098 struct table *helo_regex; 1099 1100 int8_t not_auth; 1101 int8_t auth; 1102 1103 int8_t not_auth_table; 1104 struct table *auth_table; 1105 1106 int8_t not_auth_regex; 1107 struct table *auth_regex; 1108 1109 int8_t not_mail_from_table; 1110 struct table *mail_from_table; 1111 1112 int8_t not_mail_from_regex; 1113 struct table *mail_from_regex; 1114 1115 int8_t not_rcpt_to_table; 1116 struct table *rcpt_to_table; 1117 1118 int8_t not_rcpt_to_regex; 1119 struct table *rcpt_to_regex; 1120 1121 }; 1122 1123 enum filter_status { 1124 FILTER_PROCEED, 1125 FILTER_REWRITE, 1126 FILTER_REJECT, 1127 FILTER_DISCONNECT, 1128 FILTER_JUNK, 1129 }; 1130 1131 enum ca_resp_status { 1132 CA_OK, 1133 CA_FAIL 1134 }; 1135 1136 enum mda_resp_status { 1137 MDA_OK, 1138 MDA_TEMPFAIL, 1139 MDA_PERMFAIL 1140 }; 1141 1142 struct msg_walkinfo { 1143 struct event ev; 1144 uint32_t msgid; 1145 uint32_t peerid; 1146 size_t n_evp; 1147 void *data; 1148 int done; 1149 }; 1150 1151 1152 enum dispatcher_type { 1153 DISPATCHER_LOCAL, 1154 DISPATCHER_REMOTE, 1155 DISPATCHER_BOUNCE, 1156 }; 1157 1158 struct dispatcher_local { 1159 uint8_t is_mbox; /* only for MBOX */ 1160 1161 uint8_t expand_only; 1162 uint8_t forward_only; 1163 1164 char *mda_wrapper; 1165 char *command; 1166 1167 char *table_alias; 1168 char *table_virtual; 1169 char *table_userbase; 1170 1171 char *user; 1172 }; 1173 1174 struct dispatcher_remote { 1175 char *helo; 1176 char *helo_source; 1177 1178 char *source; 1179 1180 struct tls_config *tls_config; 1181 char *ca; 1182 char *pki; 1183 1184 char *mail_from; 1185 1186 char *smarthost; 1187 int smarthost_domain; 1188 1189 char *auth; 1190 int tls_required; 1191 int tls_verify; 1192 char *tls_protocols; 1193 char *tls_ciphers; 1194 1195 int backup; 1196 char *backupmx; 1197 1198 char *filtername; 1199 1200 int srs; 1201 }; 1202 1203 struct dispatcher_bounce { 1204 }; 1205 1206 struct dispatcher { 1207 enum dispatcher_type type; 1208 union dispatcher_agent { 1209 struct dispatcher_local local; 1210 struct dispatcher_remote remote; 1211 struct dispatcher_bounce bounce; 1212 } u; 1213 1214 time_t ttl; 1215 }; 1216 1217 struct rule { 1218 TAILQ_ENTRY(rule) r_entry; 1219 1220 uint8_t reject; 1221 1222 int8_t flag_tag; 1223 int8_t flag_from; 1224 int8_t flag_for; 1225 int8_t flag_from_rdns; 1226 int8_t flag_from_socket; 1227 1228 int8_t flag_tag_regex; 1229 int8_t flag_from_regex; 1230 int8_t flag_for_regex; 1231 1232 int8_t flag_smtp_helo; 1233 int8_t flag_smtp_starttls; 1234 int8_t flag_smtp_auth; 1235 int8_t flag_smtp_mail_from; 1236 int8_t flag_smtp_rcpt_to; 1237 1238 int8_t flag_smtp_helo_regex; 1239 int8_t flag_smtp_starttls_regex; 1240 int8_t flag_smtp_auth_regex; 1241 int8_t flag_smtp_mail_from_regex; 1242 int8_t flag_smtp_rcpt_to_regex; 1243 1244 1245 char *table_tag; 1246 char *table_from; 1247 char *table_for; 1248 1249 char *table_smtp_helo; 1250 char *table_smtp_auth; 1251 char *table_smtp_mail_from; 1252 char *table_smtp_rcpt_to; 1253 1254 char *dispatcher; 1255 }; 1256 1257 1258 /* aliases.c */ 1259 int aliases_get(struct expand *, const char *); 1260 int aliases_virtual_get(struct expand *, const struct mailaddr *); 1261 int alias_parse(struct expandnode *, const char *); 1262 1263 1264 /* auth.c */ 1265 struct auth_backend *auth_backend_lookup(enum auth_type); 1266 1267 1268 /* bounce.c */ 1269 void bounce_add(uint64_t); 1270 void bounce_fd(int); 1271 1272 1273 /* ca.c */ 1274 int ca(void); 1275 int ca_X509_verify(void *, void *, const char *, const char *, const char **); 1276 void ca_imsg(struct mproc *, struct imsg *); 1277 void ca_init(void); 1278 void ca_engine_init(void); 1279 1280 1281 /* compress_backend.c */ 1282 struct compress_backend *compress_backend_lookup(const char *); 1283 size_t compress_chunk(void *, size_t, void *, size_t); 1284 size_t uncompress_chunk(void *, size_t, void *, size_t); 1285 int compress_file(FILE *, FILE *); 1286 int uncompress_file(FILE *, FILE *); 1287 1288 /* config.c */ 1289 #define PURGE_LISTENERS 0x01 1290 #define PURGE_TABLES 0x02 1291 #define PURGE_RULES 0x04 1292 #define PURGE_PKI 0x08 1293 #define PURGE_PKI_KEYS 0x10 1294 #define PURGE_DISPATCHERS 0x20 1295 #define PURGE_EVERYTHING 0xff 1296 struct smtpd *config_default(void); 1297 void purge_config(uint8_t); 1298 void config_process(enum smtp_proc_type); 1299 void config_peer(enum smtp_proc_type); 1300 1301 1302 /* control.c */ 1303 int control(void); 1304 int control_create_socket(void); 1305 1306 1307 /* crypto.c */ 1308 int crypto_setup(const char *, size_t); 1309 int crypto_encrypt_file(FILE *, FILE *); 1310 int crypto_decrypt_file(FILE *, FILE *); 1311 size_t crypto_encrypt_buffer(const char *, size_t, char *, size_t); 1312 size_t crypto_decrypt_buffer(const char *, size_t, char *, size_t); 1313 1314 1315 /* dns.c */ 1316 void dns_imsg(struct mproc *, struct imsg *); 1317 1318 1319 /* enqueue.c */ 1320 int enqueue(int, char **, FILE *); 1321 1322 1323 /* envelope.c */ 1324 void envelope_set_errormsg(struct envelope *, char *, ...) 1325 __attribute__((__format__ (printf, 2, 3))); 1326 void envelope_set_esc_class(struct envelope *, enum enhanced_status_class); 1327 void envelope_set_esc_code(struct envelope *, enum enhanced_status_code); 1328 int envelope_load_buffer(struct envelope *, const char *, size_t); 1329 int envelope_dump_buffer(const struct envelope *, char *, size_t); 1330 1331 1332 /* expand.c */ 1333 int expand_cmp(struct expandnode *, struct expandnode *); 1334 void expand_insert(struct expand *, struct expandnode *); 1335 struct expandnode *expand_lookup(struct expand *, struct expandnode *); 1336 void expand_clear(struct expand *); 1337 void expand_free(struct expand *); 1338 int expand_line(struct expand *, const char *, int); 1339 int expand_to_text(struct expand *, char *, size_t); 1340 RB_PROTOTYPE(expandtree, expandnode, nodes, expand_cmp); 1341 1342 1343 /* forward.c */ 1344 int forwards_get(int, struct expand *); 1345 1346 1347 /* limit.c */ 1348 void limit_mta_set_defaults(struct mta_limits *); 1349 int limit_mta_set(struct mta_limits *, const char*, int64_t); 1350 1351 1352 /* lka.c */ 1353 int lka(void); 1354 1355 1356 /* lka_proc.c */ 1357 int lka_proc_ready(void); 1358 void lka_proc_forked(const char *, uint32_t, int); 1359 void lka_proc_errfd(const char *, int); 1360 struct io *lka_proc_get_io(const char *); 1361 1362 1363 /* lka_report.c */ 1364 void lka_report_init(void); 1365 void lka_report_register_hook(const char *, const char *); 1366 void lka_report_smtp_link_connect(const char *, struct timeval *, uint64_t, const char *, int, 1367 const struct sockaddr_storage *, const struct sockaddr_storage *); 1368 void lka_report_smtp_link_disconnect(const char *, struct timeval *, uint64_t); 1369 void lka_report_smtp_link_greeting(const char *, uint64_t, struct timeval *, 1370 const char *); 1371 void lka_report_smtp_link_identify(const char *, struct timeval *, uint64_t, const char *, const char *); 1372 void lka_report_smtp_link_tls(const char *, struct timeval *, uint64_t, const char *); 1373 void lka_report_smtp_link_auth(const char *, struct timeval *, uint64_t, const char *, const char *); 1374 void lka_report_smtp_tx_reset(const char *, struct timeval *, uint64_t, uint32_t); 1375 void lka_report_smtp_tx_begin(const char *, struct timeval *, uint64_t, uint32_t); 1376 void lka_report_smtp_tx_mail(const char *, struct timeval *, uint64_t, uint32_t, const char *, int); 1377 void lka_report_smtp_tx_rcpt(const char *, struct timeval *, uint64_t, uint32_t, const char *, int); 1378 void lka_report_smtp_tx_envelope(const char *, struct timeval *, uint64_t, uint32_t, uint64_t); 1379 void lka_report_smtp_tx_commit(const char *, struct timeval *, uint64_t, uint32_t, size_t); 1380 void lka_report_smtp_tx_data(const char *, struct timeval *, uint64_t, uint32_t, int); 1381 void lka_report_smtp_tx_rollback(const char *, struct timeval *, uint64_t, uint32_t); 1382 void lka_report_smtp_protocol_client(const char *, struct timeval *, uint64_t, const char *); 1383 void lka_report_smtp_protocol_server(const char *, struct timeval *, uint64_t, const char *); 1384 void lka_report_smtp_filter_response(const char *, struct timeval *, uint64_t, 1385 int, int, const char *); 1386 void lka_report_smtp_timeout(const char *, struct timeval *, uint64_t); 1387 void lka_report_filter_report(uint64_t, const char *, int, const char *, 1388 struct timeval *, const char *); 1389 void lka_report_proc(const char *, const char *); 1390 1391 1392 /* lka_filter.c */ 1393 void lka_filter_init(void); 1394 void lka_filter_register_hook(const char *, const char *); 1395 void lka_filter_ready(void); 1396 int lka_filter_proc_in_session(uint64_t, const char *); 1397 void lka_filter_begin(uint64_t, const char *); 1398 void lka_filter_end(uint64_t); 1399 void lka_filter_protocol(uint64_t, enum filter_phase, const char *); 1400 void lka_filter_data_begin(uint64_t); 1401 void lka_filter_data_end(uint64_t); 1402 int lka_filter_response(uint64_t, const char *, const char *); 1403 1404 1405 /* lka_session.c */ 1406 void lka_session(uint64_t, struct envelope *); 1407 void lka_session_forward_reply(struct forward_req *, int); 1408 1409 1410 /* log.c */ 1411 void vlog(int, const char *, va_list); 1412 void logit(int, const char *, ...) __attribute__((format (printf, 2, 3))); 1413 1414 1415 /* mda.c */ 1416 void mda_postfork(void); 1417 void mda_postprivdrop(void); 1418 void mda_imsg(struct mproc *, struct imsg *); 1419 1420 1421 /* mda_mbox.c */ 1422 void mda_mbox_init(struct deliver *); 1423 void mda_mbox(struct deliver *); 1424 1425 1426 /* mda_unpriv.c */ 1427 void mda_unpriv(struct dispatcher *, struct deliver *, const char *, const char *); 1428 1429 1430 /* mda_variables.c */ 1431 ssize_t mda_expand_format(char *, size_t, const struct deliver *, 1432 const struct userinfo *, const char *); 1433 1434 1435 /* makemap.c */ 1436 int makemap(int, int, char **); 1437 1438 1439 /* mailaddr.c */ 1440 int mailaddr_line(struct maddrmap *, const char *); 1441 void maddrmap_init(struct maddrmap *); 1442 void maddrmap_insert(struct maddrmap *, struct maddrnode *); 1443 void maddrmap_free(struct maddrmap *); 1444 1445 1446 /* mproc.c */ 1447 int mproc_fork(struct mproc *, const char*, char **); 1448 void mproc_init(struct mproc *, int); 1449 void mproc_clear(struct mproc *); 1450 void mproc_enable(struct mproc *); 1451 void mproc_disable(struct mproc *); 1452 void mproc_event_add(struct mproc *); 1453 void m_compose(struct mproc *, uint32_t, uint32_t, pid_t, int, void *, size_t); 1454 void m_composev(struct mproc *, uint32_t, uint32_t, pid_t, int, 1455 const struct iovec *, int); 1456 void m_forward(struct mproc *, struct imsg *); 1457 void m_create(struct mproc *, uint32_t, uint32_t, pid_t, int); 1458 void m_add(struct mproc *, const void *, size_t); 1459 void m_add_int(struct mproc *, int); 1460 void m_add_u32(struct mproc *, uint32_t); 1461 void m_add_size(struct mproc *, size_t); 1462 void m_add_time(struct mproc *, time_t); 1463 void m_add_timeval(struct mproc *, struct timeval *tv); 1464 void m_add_string(struct mproc *, const char *); 1465 void m_add_data(struct mproc *, const void *, size_t); 1466 void m_add_evpid(struct mproc *, uint64_t); 1467 void m_add_msgid(struct mproc *, uint32_t); 1468 void m_add_id(struct mproc *, uint64_t); 1469 void m_add_sockaddr(struct mproc *, const struct sockaddr *); 1470 void m_add_mailaddr(struct mproc *, const struct mailaddr *); 1471 void m_add_envelope(struct mproc *, const struct envelope *); 1472 void m_add_params(struct mproc *, struct dict *); 1473 void m_close(struct mproc *); 1474 void m_flush(struct mproc *); 1475 1476 void m_msg(struct msg *, struct imsg *); 1477 int m_is_eom(struct msg *); 1478 void m_end(struct msg *); 1479 void m_get_int(struct msg *, int *); 1480 void m_get_size(struct msg *, size_t *); 1481 void m_get_u32(struct msg *, uint32_t *); 1482 void m_get_time(struct msg *, time_t *); 1483 void m_get_timeval(struct msg *, struct timeval *); 1484 void m_get_string(struct msg *, const char **); 1485 void m_get_data(struct msg *, const void **, size_t *); 1486 void m_get_evpid(struct msg *, uint64_t *); 1487 void m_get_msgid(struct msg *, uint32_t *); 1488 void m_get_id(struct msg *, uint64_t *); 1489 void m_get_sockaddr(struct msg *, struct sockaddr *); 1490 void m_get_mailaddr(struct msg *, struct mailaddr *); 1491 void m_get_envelope(struct msg *, struct envelope *); 1492 void m_get_params(struct msg *, struct dict *); 1493 void m_clear_params(struct dict *); 1494 1495 1496 /* mta.c */ 1497 void mta_postfork(void); 1498 void mta_postprivdrop(void); 1499 void mta_imsg(struct mproc *, struct imsg *); 1500 void mta_route_ok(struct mta_relay *, struct mta_route *); 1501 void mta_route_error(struct mta_relay *, struct mta_route *); 1502 void mta_route_down(struct mta_relay *, struct mta_route *); 1503 void mta_route_collect(struct mta_relay *, struct mta_route *); 1504 void mta_source_error(struct mta_relay *, struct mta_route *, const char *); 1505 void mta_delivery_log(struct mta_envelope *, const char *, const char *, int, const char *); 1506 void mta_delivery_notify(struct mta_envelope *); 1507 struct mta_task *mta_route_next_task(struct mta_relay *, struct mta_route *); 1508 const char *mta_host_to_text(struct mta_host *); 1509 const char *mta_relay_to_text(struct mta_relay *); 1510 1511 1512 /* mta_session.c */ 1513 void mta_session(struct mta_relay *, struct mta_route *, const char *); 1514 void mta_session_imsg(struct mproc *, struct imsg *); 1515 1516 1517 /* parse.y */ 1518 int parse_config(struct smtpd *, const char *, int); 1519 int cmdline_symset(char *); 1520 1521 1522 /* queue.c */ 1523 int queue(void); 1524 1525 1526 /* queue_backend.c */ 1527 uint32_t queue_generate_msgid(void); 1528 uint64_t queue_generate_evpid(uint32_t); 1529 int queue_init(const char *, int); 1530 int queue_close(void); 1531 int queue_message_create(uint32_t *); 1532 int queue_message_delete(uint32_t); 1533 int queue_message_commit(uint32_t); 1534 int queue_message_fd_r(uint32_t); 1535 int queue_message_fd_rw(uint32_t); 1536 int queue_envelope_create(struct envelope *); 1537 int queue_envelope_delete(uint64_t); 1538 int queue_envelope_load(uint64_t, struct envelope *); 1539 int queue_envelope_update(struct envelope *); 1540 int queue_envelope_walk(struct envelope *); 1541 int queue_message_walk(struct envelope *, uint32_t, int *, void **); 1542 1543 1544 /* report_smtp.c */ 1545 void report_smtp_link_connect(const char *, uint64_t, const char *, int, 1546 const struct sockaddr_storage *, const struct sockaddr_storage *); 1547 void report_smtp_link_disconnect(const char *, uint64_t); 1548 void report_smtp_link_greeting(const char *, uint64_t, const char *); 1549 void report_smtp_link_identify(const char *, uint64_t, const char *, const char *); 1550 void report_smtp_link_tls(const char *, uint64_t, const char *); 1551 void report_smtp_link_auth(const char *, uint64_t, const char *, const char *); 1552 void report_smtp_tx_reset(const char *, uint64_t, uint32_t); 1553 void report_smtp_tx_begin(const char *, uint64_t, uint32_t); 1554 void report_smtp_tx_mail(const char *, uint64_t, uint32_t, const char *, int); 1555 void report_smtp_tx_rcpt(const char *, uint64_t, uint32_t, const char *, int); 1556 void report_smtp_tx_envelope(const char *, uint64_t, uint32_t, uint64_t); 1557 void report_smtp_tx_data(const char *, uint64_t, uint32_t, int); 1558 void report_smtp_tx_commit(const char *, uint64_t, uint32_t, size_t); 1559 void report_smtp_tx_rollback(const char *, uint64_t, uint32_t); 1560 void report_smtp_protocol_client(const char *, uint64_t, const char *); 1561 void report_smtp_protocol_server(const char *, uint64_t, const char *); 1562 void report_smtp_filter_response(const char *, uint64_t, int, int, const char *); 1563 void report_smtp_timeout(const char *, uint64_t); 1564 1565 1566 /* ruleset.c */ 1567 struct rule *ruleset_match(const struct envelope *); 1568 1569 1570 /* scheduler.c */ 1571 int scheduler(void); 1572 1573 1574 /* scheduler_bakend.c */ 1575 struct scheduler_backend *scheduler_backend_lookup(const char *); 1576 void scheduler_info(struct scheduler_info *, struct envelope *); 1577 1578 1579 /* dispatcher.c */ 1580 int dispatcher(void); 1581 void dispatcher_imsg(struct mproc *, struct imsg *); 1582 1583 1584 /* resolver.c */ 1585 void resolver_getaddrinfo(const char *, const char *, const struct addrinfo *, 1586 void(*)(void *, int, struct addrinfo*), void *); 1587 void resolver_getnameinfo(const struct sockaddr *, int, 1588 void(*)(void *, int, const char *, const char *), void *); 1589 void resolver_res_query(const char *, int, int, 1590 void (*cb)(void *, int, int, int, const void *, int), void *); 1591 void resolver_dispatch_request(struct mproc *, struct imsg *); 1592 void resolver_dispatch_result(struct mproc *, struct imsg *); 1593 1594 1595 /* smtp.c */ 1596 void smtp_postfork(void); 1597 void smtp_postprivdrop(void); 1598 void smtp_imsg(struct mproc *, struct imsg *); 1599 void smtp_configure(void); 1600 void smtp_collect(void); 1601 1602 1603 /* smtp_session.c */ 1604 int smtp_session(struct listener *, int, const struct sockaddr_storage *, 1605 const char *, struct io *); 1606 void smtp_session_imsg(struct mproc *, struct imsg *); 1607 1608 1609 /* smtpf_session.c */ 1610 int smtpf_session(struct listener *, int, const struct sockaddr_storage *, 1611 const char *); 1612 void smtpf_session_imsg(struct mproc *, struct imsg *); 1613 1614 1615 /* smtpd.c */ 1616 void imsg_dispatch(struct mproc *, struct imsg *); 1617 const char *proc_name(enum smtp_proc_type); 1618 const char *proc_title(enum smtp_proc_type); 1619 const char *imsg_to_str(int); 1620 void log_imsg(int, int, struct imsg *); 1621 int fork_proc_backend(const char *, const char *, const char *); 1622 1623 1624 /* srs.c */ 1625 const char *srs_encode(const char *, const char *); 1626 const char *srs_decode(const char *); 1627 1628 1629 /* stat_backend.c */ 1630 struct stat_backend *stat_backend_lookup(const char *); 1631 void stat_increment(const char *, size_t); 1632 void stat_decrement(const char *, size_t); 1633 void stat_set(const char *, const struct stat_value *); 1634 struct stat_value *stat_counter(size_t); 1635 struct stat_value *stat_timestamp(time_t); 1636 struct stat_value *stat_timeval(struct timeval *); 1637 struct stat_value *stat_timespec(struct timespec *); 1638 1639 1640 /* table.c */ 1641 struct table *table_find(struct smtpd *, const char *); 1642 struct table *table_create(struct smtpd *, const char *, const char *, 1643 const char *); 1644 int table_config(struct table *); 1645 int table_open(struct table *); 1646 int table_update(struct table *); 1647 void table_close(struct table *); 1648 void table_dump(struct table *); 1649 int table_check_use(struct table *, uint32_t, uint32_t); 1650 int table_check_type(struct table *, uint32_t); 1651 int table_check_service(struct table *, uint32_t); 1652 int table_match(struct table *, enum table_service, const char *); 1653 int table_lookup(struct table *, enum table_service, const char *, 1654 union lookup *); 1655 int table_fetch(struct table *, enum table_service, union lookup *); 1656 void table_destroy(struct smtpd *, struct table *); 1657 void table_add(struct table *, const char *, const char *); 1658 int table_domain_match(const char *, const char *); 1659 int table_netaddr_match(const char *, const char *); 1660 int table_mailaddr_match(const char *, const char *); 1661 int table_regex_match(const char *, const char *); 1662 void table_open_all(struct smtpd *); 1663 void table_dump_all(struct smtpd *); 1664 void table_close_all(struct smtpd *); 1665 1666 1667 /* to.c */ 1668 int email_to_mailaddr(struct mailaddr *, char *); 1669 int text_to_netaddr(struct netaddr *, const char *); 1670 int text_to_mailaddr(struct mailaddr *, const char *); 1671 int text_to_relayhost(struct relayhost *, const char *); 1672 int text_to_userinfo(struct userinfo *, const char *); 1673 int text_to_credentials(struct credentials *, const char *); 1674 int text_to_expandnode(struct expandnode *, const char *); 1675 uint64_t text_to_evpid(const char *); 1676 uint32_t text_to_msgid(const char *); 1677 const char *sa_to_text(const struct sockaddr *); 1678 const char *ss_to_text(const struct sockaddr_storage *); 1679 const char *time_to_text(time_t); 1680 const char *duration_to_text(time_t); 1681 const char *rule_to_text(struct rule *); 1682 const char *sockaddr_to_text(const struct sockaddr *); 1683 const char *mailaddr_to_text(const struct mailaddr *); 1684 const char *expandnode_to_text(struct expandnode *); 1685 const char *tls_to_text(struct tls *); 1686 1687 1688 /* util.c */ 1689 typedef struct arglist arglist; 1690 struct arglist { 1691 char **list; 1692 uint num; 1693 uint nalloc; 1694 }; 1695 void addargs(arglist *, char *, ...) 1696 __attribute__((format(printf, 2, 3))); 1697 int bsnprintf(char *, size_t, const char *, ...) 1698 __attribute__((format (printf, 3, 4))); 1699 int safe_fclose(FILE *); 1700 int hostname_match(const char *, const char *); 1701 int mailaddr_match(const struct mailaddr *, const struct mailaddr *); 1702 int valid_localpart(const char *); 1703 int valid_domainpart(const char *); 1704 int valid_domainname(const char *); 1705 int valid_smtp_response(const char *); 1706 int secure_file(int, char *, char *, uid_t, int); 1707 int lowercase(char *, const char *, size_t); 1708 void xlowercase(char *, const char *, size_t); 1709 int uppercase(char *, const char *, size_t); 1710 uint64_t generate_uid(void); 1711 int availdesc(void); 1712 int ckdir(const char *, mode_t, uid_t, gid_t, int); 1713 int rmtree(char *, int); 1714 int mvpurge(char *, char *); 1715 int mktmpfile(void); 1716 const char *parse_smtp_response(char *, size_t, char **, int *); 1717 int xasprintf(char **, const char *, ...) 1718 __attribute__((__format__ (printf, 2, 3))); 1719 void *xmalloc(size_t); 1720 void *xcalloc(size_t, size_t); 1721 char *xstrdup(const char *); 1722 void *xmemdup(const void *, size_t); 1723 char *strip(char *); 1724 int io_xprint(struct io *, const char *); 1725 int io_xprintf(struct io *, const char *, ...) 1726 __attribute__((__format__ (printf, 2, 3))); 1727 void log_envelope(const struct envelope *, const char *, const char *, 1728 const char *); 1729 int session_socket_error(int); 1730 int getmailname(char *, size_t); 1731 int base64_encode(unsigned char const *, size_t, char *, size_t); 1732 int base64_decode(char const *, unsigned char *, size_t); 1733 int base64_encode_rfc3548(unsigned char const *, size_t, 1734 char *, size_t); 1735 1736 void log_trace_verbose(int); 1737 void log_trace0(const char *, ...) 1738 __attribute__((format (printf, 1, 2))); 1739 #define log_trace(m, ...) do { if (tracing & (m)) log_trace0(__VA_ARGS__); } while (0) 1740 1741 /* waitq.c */ 1742 int waitq_wait(void *, void (*)(void *, void *, void *), void *); 1743 void waitq_run(void *, void *); 1744 1745 1746 /* runq.c */ 1747 struct runq; 1748 1749 int runq_init(struct runq **, void (*)(struct runq *, void *)); 1750 int runq_schedule(struct runq *, time_t, void *); 1751 int runq_schedule_at(struct runq *, time_t, void *); 1752 int runq_cancel(struct runq *, void *); 1753 int runq_pending(struct runq *, void *, time_t *); 1754