Lines Matching full:s

150 #define ADVERTISE_TLS(s) \
151 ((s)->listener->flags & F_STARTTLS && !((s)->flags & SF_SECURE))
153 #define ADVERTISE_AUTH(s) \
154 ((s)->listener->flags & F_AUTH && (s)->flags & SF_SECURE && \
155 !((s)->flags & SF_AUTHENTICATED))
157 #define ADVERTISE_EXT_DSN(s) \
158 ((s)->listener->flags & F_EXT_DSN)
160 #define SESSION_FILTERED(s) \
161 ((s)->listener->flags & F_FILTERED)
163 #define SESSION_DATA_FILTERED(s) \
164 ((s)->listener->flags & F_FILTERED)
210 static int smtp_check_auth(struct smtp_session *s, const char *);
352 /* there's an address between brackets, just append domain */
371 if (snprintf(copy, sizeof copy, "%.*s@%s%s",
430 /* there's an address between brackets, just replace everything brackets */
469 if (smtp_message_printf(tx, "%s:", hdr) == -1)
508 if (smtp_message_printf(tx, "%s,", buffer) == -1)
522 if (smtp_message_printf(tx, "%s", buffer) == -1)
538 if (smtp_message_printf(tx, "%s", buffer) == -1)
558 smtp_message_printf(tx, "%s", buffer);
587 struct smtp_session *s;
591 if ((s = calloc(1, sizeof(*s))) == NULL)
594 s->id = generate_uid();
595 s->listener = listener;
596 memmove(&s->ss, ss, sizeof(*ss));
599 s->io = io;
601 s->io = io_new();
603 io_set_callback(s->io, smtp_io, s);
604 io_set_fd(s->io, sock);
605 io_set_timeout(s->io, SMTPD_SESSION_TIMEOUT * 1000);
606 io_set_write(s->io);
607 s->state = STATE_NEW;
609 (void)strlcpy(s->smtpname, listener->hostname, sizeof(s->smtpname));
612 "[hostname=%s, port=%d, tag=%s]", s, listener,
617 s->flags |= SF_AUTHENTICATED;
620 s->flags |= SF_BOUNCE;
621 (void)strlcpy(s->rdns, hostname, sizeof(s->rdns));
622 s->fcrdns = 1;
623 smtp_lookup_servername(s);
625 resolver_getnameinfo((struct sockaddr *)&s->ss,
626 NI_NAMEREQD | NI_NUMERICSERV, smtp_getnameinfo_cb, s);
637 struct smtp_session *s = arg;
641 (void)strlcpy(s->rdns, "<unknown>", sizeof(s->rdns));
644 s->fcrdns = 0;
646 log_warnx("getnameinfo: %s: %s", ss_to_text(&s->ss),
648 s->fcrdns = -1;
651 smtp_lookup_servername(s);
655 (void)strlcpy(s->rdns, host, sizeof(s->rdns));
658 hints.ai_family = s->ss.ss_family;
660 resolver_getaddrinfo(s->rdns, NULL, &hints, smtp_getaddrinfo_cb, s);
666 struct smtp_session *s = arg;
672 s->fcrdns = 0;
674 log_warnx("getaddrinfo: %s: %s", s->rdns,
676 s->fcrdns = -1;
680 strlcpy(rev, ss_to_text(&s->ss), sizeof(rev));
684 s->fcrdns = 1;
691 smtp_lookup_servername(s);
697 struct smtp_session *s;
717 s = tree_xpop(&wait_lka_mail, reqid);
720 smtp_tx_create_message(s->tx);
724 smtp_tx_free(s->tx);
725 smtp_reply(s, "%d %s", 530, "Sender rejected");
728 smtp_tx_free(s->tx);
729 smtp_reply(s, "421 %s Temporary Error",
741 s = tree_xpop(&wait_lka_rcpt, reqid);
744 if (s->tx->evp.rcpt.user[0]) {
745 (void)strlcpy(tmp, s->tx->evp.rcpt.user, sizeof tmp);
746 if (s->tx->evp.rcpt.domain[0]) {
748 (void)strlcat(tmp, s->tx->evp.rcpt.domain,
757 smtp_reply(s, "%s: <%s>", line, tmp);
760 smtp_reply(s, "%s: <%s>", line, tmp);
768 s = tree_xpop(&wait_lka_helo, reqid);
772 (void)strlcpy(s->smtpname, helo, sizeof(s->smtpname));
775 smtp_connected(s);
782 s = tree_xpop(&wait_queue_msg, reqid);
785 s->tx->msgid = msgid;
786 s->tx->evp.id = msgid_to_evpid(msgid);
787 s->tx->rcptcount = 0;
788 smtp_reply(s, "250 %s Ok",
791 smtp_reply(s, "421 %s Temporary Error",
793 smtp_tx_free(s->tx);
794 smtp_enter_state(s, STATE_QUIT);
806 s = tree_xpop(&wait_queue_fd, reqid);
810 smtp_reply(s, "421 %s Temporary Error",
812 smtp_enter_state(s, STATE_QUIT);
816 log_debug("smtp: %p: fd %d from queue", s, fd);
818 if (smtp_message_fd(s->tx, fd)) {
819 if (!SESSION_DATA_FILTERED(s))
820 smtp_message_begin(s->tx);
822 smtp_filter_data_begin(s);
833 s = tree_xpop(&wait_filter_fd, reqid);
837 smtp_reply(s, "421 %s Temporary Error",
839 smtp_enter_state(s, STATE_QUIT);
843 log_debug("smtp: %p: fd %d from lka", s, fd);
845 smtp_filter_fd(s->tx, fd);
846 smtp_message_begin(s->tx);
853 s = tree_xget(&wait_lka_rcpt, reqid);
856 s->tx->evp.id = evpid;
857 s->tx->destcount++;
858 smtp_report_tx_envelope(s, s->tx->msgid, evpid);
861 s->tx->error = TX_ERROR_ENVELOPE;
872 s = tree_xpop(&wait_lka_rcpt, reqid);
873 if (s->tx->error) {
879 smtp_reply(s, "421 %s Temporary failure",
881 smtp_enter_state(s, STATE_QUIT);
885 rcpt->evpid = s->tx->evp.id;
886 rcpt->destcount = s->tx->destcount;
887 rcpt->maddr = s->tx->evp.rcpt;
888 TAILQ_INSERT_TAIL(&s->tx->rcpts, rcpt, entry);
890 s->tx->destcount = 0;
891 s->tx->rcptcount++;
892 smtp_reply(s, "250 %s %s: Recipient ok",
903 s = tree_xpop(&wait_queue_commit, reqid);
905 smtp_reply(s, "421 %s Temporary failure",
907 smtp_tx_free(s->tx);
908 smtp_enter_state(s, STATE_QUIT);
912 smtp_reply(s, "250 %s %08x Message accepted for delivery",
914 s->tx->msgid);
915 smtp_report_tx_commit(s, s->tx->msgid, s->tx->odatalen);
916 smtp_report_tx_reset(s, s->tx->msgid);
919 "msgid=%08x size=%zu nrcpt=%zu proto=%s",
920 s->id,
921 s->tx->msgid,
922 s->tx->odatalen,
923 s->tx->rcptcount,
924 s->flags & SF_EHLO ? "ESMTP" : "SMTP");
925 TAILQ_FOREACH(rcpt, &s->tx->rcpts, entry) {
927 "evpid=%016"PRIx64" from=<%s%s%s> to=<%s%s%s>",
928 s->id,
930 s->tx->evp.sender.user,
931 s->tx->evp.sender.user[0] == '\0' ? "" : "@",
932 s->tx->evp.sender.domain,
937 smtp_tx_free(s->tx);
938 s->mailcount++;
939 smtp_enter_state(s, STATE_HELO);
948 s = tree_xpop(&wait_parent_auth, reqid);
949 strnvis(user, s->username, sizeof user, VIS_WHITE | VIS_SAFE);
952 "authentication user=%s "
954 s->id, user);
955 s->flags |= SF_AUTHENTICATED;
956 smtp_report_link_auth(s, user, "pass");
957 smtp_reply(s, "235 %s Authentication succeeded",
962 "authentication user=%s "
964 s->id, user);
965 smtp_report_link_auth(s, user, "fail");
966 smtp_auth_failure_pause(s);
971 "authentication user=%s "
973 s->id, user);
974 smtp_report_link_auth(s, user, "error");
975 smtp_reply(s, "421 %s Temporary failure",
981 smtp_enter_state(s, STATE_HELO);
995 s = tree_xpop(&wait_filters, reqid);
1006 smtp_report_filter_response(s, s->filter_phase,
1009 smtp_reply(s, "%s", filter_param);
1012 smtp_enter_state(s, STATE_QUIT);
1013 else if (s->filter_phase == FILTER_COMMIT)
1014 smtp_proceed_rollback(s, NULL);
1019 if (s->tx)
1020 s->tx->junk = 1;
1022 s->junk = 1;
1026 filter_param = s->filter_param;
1031 smtp_report_filter_response(s, s->filter_phase,
1033 filter_param == s->filter_param ? NULL : filter_param);
1034 if (s->filter_phase == FILTER_CONNECT) {
1035 smtp_proceed_connected(s);
1039 if (commands[i].filter_phase == s->filter_phase) {
1041 if (!commands[i].check(s, filter_param))
1043 commands[i].proceed(s, filter_param);
1051 log_warnx("smtp_session_imsg: unexpected %s imsg",
1057 smtp_tls_init(struct smtp_session *s)
1059 io_set_read(s->io);
1060 if (io_accept_tls(s->io, s->listener->tls) == -1) {
1063 s->id);
1064 smtp_free(s, "accept failed");
1069 smtp_tls_started(struct smtp_session *s)
1071 if (tls_peer_cert_provided(io_tls(s->io))) {
1073 "cert-check result=\"%s\" fingerprint=\"%s\"",
1074 s->id,
1075 (s->flags & SF_VERIFIED) ? "verified" : "unchecked",
1076 tls_peer_cert_hash(io_tls(s->io)));
1079 if (s->listener->flags & F_SMTPS) {
1081 io_set_write(s->io);
1082 smtp_send_banner(s);
1086 smtp_enter_state(s, STATE_HELO);
1093 struct smtp_session *s = arg;
1098 log_trace(TRACE_IO, "smtp: %p: %s %s", s, io_strevent(evt),
1104 log_info("%016"PRIx64" smtp tls ciphers=%s",
1105 s->id, tls_to_text(io_tls(s->io)));
1107 smtp_report_link_tls(s, tls_to_text(io_tls(s->io)));
1109 s->flags |= SF_SECURE;
1110 if (s->listener->flags & F_TLS_VERIFY)
1111 s->flags |= SF_VERIFIED;
1112 s->helo[0] = '\0';
1114 smtp_tls_started(s);
1119 line = io_getline(s->io, &len);
1120 if ((line == NULL && io_datalen(s->io) >= SMTP_LINE_MAX) ||
1122 s->flags |= SF_BADINPUT;
1123 smtp_reply(s, "500 %s Line too long",
1125 smtp_enter_state(s, STATE_QUIT);
1140 if (s->state == STATE_BODY) {
1142 s->tx->datain += strlen(line) + 1;
1143 if (s->tx->datain > env->sc_maxsize)
1144 s->tx->error = TX_ERROR_SIZE;
1146 eom = (s->tx->filter == NULL) ?
1147 smtp_tx_dataline(s->tx, line) :
1148 smtp_tx_filtered_dataline(s->tx, line);
1154 if (io_datalen(s->io)) {
1155 s->flags |= SF_BADINPUT;
1156 smtp_reply(s, "500 %s %s: Pipelining not supported",
1159 smtp_enter_state(s, STATE_QUIT);
1166 if (s->tx->filter == NULL)
1167 smtp_tx_eom(s->tx);
1172 if (strlcpy(s->cmd, line, sizeof(s->cmd)) >= sizeof(s->cmd)) {
1173 s->flags |= SF_BADINPUT;
1174 smtp_reply(s, "500 %s Command line too long",
1176 smtp_enter_state(s, STATE_QUIT);
1181 smtp_command(s, line);
1185 if (s->state == STATE_QUIT) {
1188 s->id);
1189 smtp_free(s, "done");
1194 if (s->state == STATE_TLS) {
1195 smtp_tls_init(s);
1205 s->id);
1206 smtp_report_timeout(s);
1207 smtp_free(s, "timeout");
1213 s->id);
1214 smtp_free(s, "disconnected");
1219 "reason=\"io-error: %s\"",
1220 s->id, io_error(io));
1221 smtp_free(s, "IO error");
1230 smtp_command(struct smtp_session *s, char *line)
1235 log_trace(TRACE_SMTP, "smtp: %p: <<< %s", s, line);
1240 if (s->state == STATE_AUTH_INIT) {
1241 smtp_report_protocol_client(s, "********");
1242 smtp_rfc4954_auth_plain(s, line);
1245 if (s->state == STATE_AUTH_USERNAME || s->state == STATE_AUTH_PASSWORD) {
1246 smtp_report_protocol_client(s, "********");
1247 smtp_rfc4954_auth_login(s, line);
1251 if (s->state == STATE_HELO && strncasecmp(line, "AUTH PLAIN ", 11) == 0)
1252 smtp_report_protocol_client(s, "AUTH PLAIN ********");
1254 smtp_report_protocol_client(s, line);
1280 s->last_cmd = cmd;
1286 if (!smtp_check_helo(s, args))
1288 smtp_filter_phase(FILTER_HELO, s, args);
1292 if (!smtp_check_ehlo(s, args))
1294 smtp_filter_phase(FILTER_EHLO, s, args);
1301 if (!smtp_check_starttls(s, args))
1304 smtp_filter_phase(FILTER_STARTTLS, s, NULL);
1308 if (!smtp_check_auth(s, args))
1310 smtp_filter_phase(FILTER_AUTH, s, args);
1314 if (!smtp_check_mail_from(s, args))
1316 smtp_filter_phase(FILTER_MAIL_FROM, s, args);
1323 if (!smtp_check_rcpt_to(s, args))
1325 smtp_filter_phase(FILTER_RCPT_TO, s, args);
1329 if (!smtp_check_rset(s, args))
1331 smtp_filter_phase(FILTER_RSET, s, NULL);
1335 if (!smtp_check_data(s, args))
1337 smtp_filter_phase(FILTER_DATA, s, NULL);
1344 if (!smtp_check_noparam(s, args))
1346 smtp_filter_phase(FILTER_QUIT, s, NULL);
1350 if (!smtp_check_noop(s, args))
1352 smtp_filter_phase(FILTER_NOOP, s, NULL);
1356 if (!smtp_check_noparam(s, args))
1358 smtp_proceed_help(s, NULL);
1362 if (!smtp_check_noparam(s, args))
1364 smtp_proceed_wiz(s, NULL);
1368 smtp_reply(s, "500 %s %s: Command unrecognized",
1376 smtp_check_rset(struct smtp_session *s, const char *args)
1378 if (!smtp_check_noparam(s, args))
1381 if (s->helo[0] == '\0') {
1382 smtp_reply(s, "503 %s %s: Command not allowed at this point.",
1391 smtp_check_helo(struct smtp_session *s, const char *args)
1393 if (!s->banner_sent) {
1394 smtp_reply(s, "503 %s %s: Command not allowed at this point.",
1400 if (s->helo[0]) {
1401 smtp_reply(s, "503 %s %s: Already identified",
1408 smtp_reply(s, "501 %s %s: HELO requires domain name",
1415 smtp_reply(s, "501 %s %s: Invalid domain name",
1425 smtp_check_ehlo(struct smtp_session *s, const char *args)
1427 if (!s->banner_sent) {
1428 smtp_reply(s, "503 %s %s: Command not allowed at this point.",
1434 if (s->helo[0]) {
1435 smtp_reply(s, "503 %s %s: Already identified",
1442 smtp_reply(s, "501 %s %s: EHLO requires domain name",
1449 smtp_reply(s, "501 %s %s: Invalid domain name",
1459 smtp_check_auth(struct smtp_session *s, const char *args)
1461 if (s->helo[0] == '\0' || s->tx) {
1462 smtp_reply(s, "503 %s %s: Command not allowed at this point.",
1468 if (s->flags & SF_AUTHENTICATED) {
1469 smtp_reply(s, "503 %s %s: Already authenticated",
1475 if (!ADVERTISE_AUTH(s)) {
1476 smtp_reply(s, "503 %s %s: Command not supported",
1483 smtp_reply(s, "501 %s %s: No parameters given",
1493 smtp_check_starttls(struct smtp_session *s, const char *args)
1495 if (s->helo[0] == '\0' || s->tx) {
1496 smtp_reply(s, "503 %s %s: Command not allowed at this point.",
1502 if (!(s->listener->flags & F_STARTTLS)) {
1503 smtp_reply(s, "503 %s %s: Command not supported",
1509 if (s->flags & SF_SECURE) {
1510 smtp_reply(s, "503 %s %s: Channel already secured",
1517 smtp_reply(s, "501 %s %s: No parameters allowed",
1527 smtp_check_mail_from(struct smtp_session *s, const char *args)
1536 if (s->helo[0] == '\0' || s->tx) {
1537 smtp_reply(s, "503 %s %s: Command not allowed at this point.",
1543 if (s->listener->flags & F_STARTTLS_REQUIRE &&
1544 !(s->flags & SF_SECURE)) {
1545 smtp_reply(s,
1546 "530 %s %s: Must issue a STARTTLS command first",
1552 if (s->listener->flags & F_AUTH_REQUIRE &&
1553 !(s->flags & SF_AUTHENTICATED)) {
1554 smtp_reply(s,
1555 "530 %s %s: Must issue an AUTH command first",
1561 if (s->mailcount >= env->sc_session_max_mails) {
1563 smtp_reply(s, "452 %s %s: Too many messages sent",
1570 s->smtpname) == 0) {
1571 smtp_reply(s, "553 %s Sender address syntax error",
1580 smtp_check_rcpt_to(struct smtp_session *s, const char *args)
1588 if (s->tx == NULL) {
1589 smtp_reply(s, "503 %s %s: Command not allowed at this point.",
1595 if (s->tx->rcptcount >= env->sc_session_max_rcpt) {
1596 smtp_reply(s->tx->session, "451 %s %s: Too many recipients",
1602 if (smtp_mailaddr(&s->tx->evp.rcpt, copy, 0, &copy,
1603 s->tx->session->smtpname) == 0) {
1604 smtp_reply(s->tx->session,
1605 "501 %s Recipient address syntax error",
1615 smtp_check_data(struct smtp_session *s, const char *args)
1617 if (!smtp_check_noparam(s, args))
1620 if (s->tx == NULL) {
1621 smtp_reply(s, "503 %s %s: Command not allowed at this point.",
1627 if (s->tx->rcptcount == 0) {
1628 smtp_reply(s, "503 %s %s: No recipient specified",
1638 smtp_check_noop(struct smtp_session *s, const char *args)
1644 smtp_check_noparam(struct smtp_session *s, const char *args)
1647 smtp_reply(s, "500 %s %s: command does not accept arguments.",
1656 smtp_query_filters(enum filter_phase phase, struct smtp_session *s, const char *args)
1659 m_add_id(p_lka, s->id);
1663 tree_xset(&wait_filters, s->id, s);
1667 smtp_filter_begin(struct smtp_session *s)
1669 if (!SESSION_FILTERED(s))
1673 m_add_id(p_lka, s->id);
1674 m_add_string(p_lka, s->listener->filter_name);
1679 smtp_filter_end(struct smtp_session *s)
1681 if (!SESSION_FILTERED(s))
1685 m_add_id(p_lka, s->id);
1690 smtp_filter_data_begin(struct smtp_session *s)
1692 if (!SESSION_FILTERED(s))
1696 m_add_id(p_lka, s->id);
1698 tree_xset(&wait_filter_fd, s->id, s);
1702 smtp_filter_data_end(struct smtp_session *s)
1704 if (!SESSION_FILTERED(s))
1707 if (s->tx->filter == NULL)
1710 io_free(s->tx->filter);
1711 s->tx->filter = NULL;
1714 m_add_id(p_lka, s->id);
1719 smtp_filter_phase(enum filter_phase phase, struct smtp_session *s, const char *param)
1723 s->filter_phase = phase;
1724 s->filter_param = param;
1726 if (SESSION_FILTERED(s)) {
1727 smtp_query_filters(phase, s, param ? param : "");
1731 if (s->filter_phase == FILTER_CONNECT) {
1732 smtp_proceed_connected(s);
1737 if (commands[i].filter_phase == s->filter_phase) {
1738 commands[i].proceed(s, param);
1744 smtp_proceed_rset(struct smtp_session *s, const char *args)
1746 smtp_reply(s, "250 %s Reset state",
1749 if (s->tx) {
1750 if (s->tx->msgid)
1751 smtp_tx_rollback(s->tx);
1752 smtp_tx_free(s->tx);
1757 smtp_proceed_helo(struct smtp_session *s, const char *args)
1759 (void)strlcpy(s->helo, args, sizeof(s->helo));
1760 s->flags &= SF_SECURE | SF_AUTHENTICATED | SF_VERIFIED;
1762 smtp_report_link_identify(s, "HELO", s->helo);
1764 smtp_enter_state(s, STATE_HELO);
1766 smtp_reply(s, "250 %s Hello %s %s%s%s, pleased to meet you",
1767 s->smtpname,
1768 s->helo,
1769 s->ss.ss_family == AF_INET6 ? "" : "[",
1770 ss_to_text(&s->ss),
1771 s->ss.ss_family == AF_INET6 ? "" : "]");
1775 smtp_proceed_ehlo(struct smtp_session *s, const char *args)
1777 (void)strlcpy(s->helo, args, sizeof(s->helo));
1778 s->flags &= SF_SECURE | SF_AUTHENTICATED | SF_VERIFIED;
1779 s->flags |= SF_EHLO;
1780 s->flags |= SF_8BITMIME;
1782 smtp_report_link_identify(s, "EHLO", s->helo);
1784 smtp_enter_state(s, STATE_HELO);
1785 smtp_reply(s, "250-%s Hello %s %s%s%s, pleased to meet you",
1786 s->smtpname,
1787 s->helo,
1788 s->ss.ss_family == AF_INET6 ? "" : "[",
1789 ss_to_text(&s->ss),
1790 s->ss.ss_family == AF_INET6 ? "" : "]");
1792 smtp_reply(s, "250-8BITMIME");
1793 smtp_reply(s, "250-ENHANCEDSTATUSCODES");
1794 smtp_reply(s, "250-SIZE %zu", env->sc_maxsize);
1795 if (ADVERTISE_EXT_DSN(s))
1796 smtp_reply(s, "250-DSN");
1797 if (ADVERTISE_TLS(s))
1798 smtp_reply(s, "250-STARTTLS");
1799 if (ADVERTISE_AUTH(s))
1800 smtp_reply(s, "250-AUTH PLAIN LOGIN");
1801 smtp_reply(s, "250 HELP");
1805 smtp_proceed_auth(struct smtp_session *s, const char *args)
1819 smtp_rfc4954_auth_plain(s, eom);
1821 smtp_rfc4954_auth_login(s, eom);
1823 smtp_reply(s, "504 %s %s: AUTH method \"%s\" not supported",
1830 smtp_proceed_starttls(struct smtp_session *s, const char *args)
1832 smtp_reply(s, "220 %s Ready to start TLS",
1834 smtp_enter_state(s, STATE_TLS);
1838 smtp_proceed_mail_from(struct smtp_session *s, const char *args)
1846 if (!smtp_tx(s)) {
1847 smtp_reply(s, "421 %s Temporary Error",
1849 smtp_enter_state(s, STATE_QUIT);
1853 if (smtp_mailaddr(&s->tx->evp.sender, copy, 1, &copy,
1854 s->smtpname) == 0) {
1855 smtp_reply(s, "553 %s Sender address syntax error",
1857 smtp_tx_free(s->tx);
1861 smtp_tx_mail_from(s->tx, args);
1865 smtp_proceed_rcpt_to(struct smtp_session *s, const char *args)
1867 smtp_tx_rcpt_to(s->tx, args);
1871 smtp_proceed_data(struct smtp_session *s, const char *args)
1873 smtp_tx_open_message(s->tx);
1877 smtp_proceed_quit(struct smtp_session *s, const char *args)
1879 smtp_reply(s, "221 %s Bye",
1881 smtp_enter_state(s, STATE_QUIT);
1885 smtp_proceed_noop(struct smtp_session *s, const char *args)
1887 smtp_reply(s, "250 %s Ok",
1892 smtp_proceed_help(struct smtp_session *s, const char *args)
1896 smtp_reply(s, "214-%s This is " SMTPD_NAME, code);
1897 smtp_reply(s, "214-%s To report bugs in the implementation, "
1899 smtp_reply(s, "214-%s with full details", code);
1900 smtp_reply(s, "214 %s End of HELP info", code);
1904 smtp_proceed_wiz(struct smtp_session *s, const char *args)
1906 smtp_reply(s, "500 %s %s: this feature is not supported yet ;-)",
1912 smtp_proceed_commit(struct smtp_session *s, const char *args)
1914 smtp_message_end(s->tx);
1918 smtp_proceed_rollback(struct smtp_session *s, const char *args)
1922 tx = s->tx;
1929 smtp_enter_state(s, STATE_HELO);
1933 smtp_rfc4954_auth_plain(struct smtp_session *s, char *arg)
1938 switch (s->state) {
1941 smtp_enter_state(s, STATE_AUTH_INIT);
1942 smtp_reply(s, "334 ");
1945 smtp_enter_state(s, STATE_AUTH_INIT);
1963 if (strlcpy(s->username, user, sizeof(s->username))
1964 >= sizeof(s->username))
1973 m_add_id(p_lka, s->id);
1974 m_add_string(p_lka, s->listener->authtable);
1978 tree_xset(&wait_parent_auth, s->id, s);
1986 smtp_reply(s, "501 %s %s: Syntax error",
1989 smtp_enter_state(s, STATE_HELO);
1993 smtp_rfc4954_auth_login(struct smtp_session *s, char *arg)
1997 switch (s->state) {
1999 smtp_enter_state(s, STATE_AUTH_USERNAME);
2001 smtp_rfc4954_auth_login(s, arg);
2004 smtp_reply(s, "334 VXNlcm5hbWU6");
2008 memset(s->username, 0, sizeof(s->username));
2009 if (base64_decode(arg, (unsigned char *)s->username,
2010 sizeof(s->username) - 1) == -1)
2013 smtp_enter_state(s, STATE_AUTH_PASSWORD);
2014 smtp_reply(s, "334 UGFzc3dvcmQ6");
2024 m_add_id(p_lka, s->id);
2025 m_add_string(p_lka, s->listener->authtable);
2026 m_add_string(p_lka, s->username);
2029 tree_xset(&wait_parent_auth, s->id, s);
2037 smtp_reply(s, "501 %s %s: Syntax error",
2040 smtp_enter_state(s, STATE_HELO);
2044 smtp_lookup_servername(struct smtp_session *s)
2046 if (s->listener->hostnametable[0]) {
2048 m_add_id(p_lka, s->id);
2049 m_add_string(p_lka, s->listener->hostnametable);
2050 m_add_sockaddr(p_lka, (struct sockaddr*)&s->listener->ss);
2052 tree_xset(&wait_lka_helo, s->id, s);
2056 smtp_connected(s);
2060 smtp_connected(struct smtp_session *s)
2062 smtp_enter_state(s, STATE_CONNECTED);
2064 log_info("%016"PRIx64" smtp connected address=%s host=%s",
2065 s->id, ss_to_text(&s->ss), s->rdns);
2067 smtp_filter_begin(s);
2069 smtp_report_link_connect(s, s->rdns, s->fcrdns, &s->ss,
2070 &s->listener->ss);
2072 smtp_filter_phase(FILTER_CONNECT, s, ss_to_text(&s->ss));
2076 smtp_proceed_connected(struct smtp_session *s)
2078 if (s->listener->flags & F_SMTPS)
2079 smtp_tls_init(s);
2081 smtp_send_banner(s);
2085 smtp_send_banner(struct smtp_session *s)
2087 smtp_reply(s, "220 %s ESMTP %s", s->smtpname, SMTPD_NAME);
2088 s->banner_sent = 1;
2089 smtp_report_link_greeting(s, s->smtpname);
2093 smtp_enter_state(struct smtp_session *s, int newstate)
2095 log_trace(TRACE_SMTP, "smtp: %p: %s -> %s", s,
2096 smtp_strstate(s->state),
2099 s->state = newstate;
2103 smtp_reply(struct smtp_session *s, char *fmt, ...)
2119 * buffer, it's ok to truncate.
2123 log_trace(TRACE_SMTP, "smtp: %p: >>> %s", s, buf);
2124 smtp_report_protocol_server(s, buf);
2128 if (s->tx) {
2129 if (s->last_cmd == CMD_MAIL_FROM) {
2130 smtp_report_tx_begin(s, s->tx->msgid);
2131 smtp_report_tx_mail(s, s->tx->msgid, s->cmd + 10, 1);
2133 else if (s->last_cmd == CMD_RCPT_TO)
2134 smtp_report_tx_rcpt(s, s->tx->msgid, s->cmd + 8, 1);
2138 if (s->tx) {
2139 if (s->last_cmd == CMD_DATA)
2140 smtp_report_tx_data(s, s->tx->msgid, 1);
2148 if (s->tx) {
2149 if (s->last_cmd == CMD_MAIL_FROM)
2150 smtp_report_tx_mail(s, s->tx->msgid,
2151 s->cmd + 10, buf[0] == '4' ? -1 : 0);
2152 else if (s->last_cmd == CMD_RCPT_TO)
2153 smtp_report_tx_rcpt(s,
2154 s->tx->msgid, s->cmd + 8, buf[0] == '4' ? -1 : 0);
2155 else if (s->last_cmd == CMD_DATA && s->tx->rcptcount)
2156 smtp_report_tx_data(s, s->tx->msgid,
2160 if (s->flags & SF_BADINPUT) {
2162 "bad-input result=\"%.*s\"",
2163 s->id, n, buf);
2165 else if (s->state == STATE_AUTH_INIT) {
2168 "command=\"AUTH PLAIN (...)\" result=\"%.*s\"",
2169 s->id, n, buf);
2171 else if (s->state == STATE_AUTH_USERNAME) {
2174 "command=\"AUTH LOGIN (username)\" result=\"%.*s\"",
2175 s->id, n, buf);
2177 else if (s->state == STATE_AUTH_PASSWORD) {
2180 "command=\"AUTH LOGIN (password)\" result=\"%.*s\"",
2181 s->id, n, buf);
2184 strnvis(tmp, s->cmd, sizeof tmp, VIS_SAFE | VIS_CSTYLE);
2186 "failed-command command=\"%s\" "
2187 "result=\"%.*s\"",
2188 s->id, tmp, n, buf);
2193 io_xprintf(s->io, "%s\r\n", buf);
2197 smtp_free(struct smtp_session *s, const char * reason)
2199 if (s->tx) {
2200 if (s->tx->msgid)
2201 smtp_tx_rollback(s->tx);
2202 smtp_tx_free(s->tx);
2205 smtp_report_link_disconnect(s);
2206 smtp_filter_end(s);
2208 if (s->flags & SF_SECURE && s->listener->flags & F_SMTPS)
2210 if (s->flags & SF_SECURE && s->listener->flags & F_STARTTLS)
2213 io_free(s->io);
2214 free(s);
2271 struct smtp_session *s = p;
2273 smtp_reply(s, "535 Authentication failed");
2274 smtp_enter_state(s, STATE_HELO);
2278 smtp_auth_failure_pause(struct smtp_session *s)
2286 evtimer_set(&s->pause, smtp_auth_failure_resume, s);
2287 evtimer_add(&s->pause, &tv);
2291 smtp_tx(struct smtp_session *s)
2301 s->tx = tx;
2302 tx->session = s;
2305 tx->evp.ss = s->ss;
2306 (void)strlcpy(tx->evp.tag, s->listener->tag, sizeof(tx->evp.tag));
2307 (void)strlcpy(tx->evp.smtpname, s->smtpname, sizeof(tx->evp.smtpname));
2308 (void)strlcpy(tx->evp.hostname, s->rdns, sizeof tx->evp.hostname);
2309 (void)strlcpy(tx->evp.helo, s->helo, sizeof(tx->evp.helo));
2310 (void)strlcpy(tx->evp.username, s->username, sizeof(tx->evp.username));
2312 if (s->flags & SF_BOUNCE)
2314 if (s->flags & SF_AUTHENTICATED)
2357 smtp_reply(tx->session, "553 %s Sender address syntax error",
2387 "503 %s %s: option too large, truncated: %s",
2394 smtp_reply(tx->session, "503 %s %s: Unsupported option %s",
2437 smtp_reply(tx->session, "451 %s %s: Too many recipients",
2446 "501 %s Recipient address syntax error",
2491 smtp_reply(tx->session, "503 Unsupported option %s", opt);
2541 log_trace(TRACE_SMTP, "<<< [MSG] %s", line);
2605 smtp_message_printf(tx, "%s:%s\n", res.hdr, res.value);
2616 smtp_message_printf(tx, "%s\n", res.value);
2633 smtp_message_printf(tx, "Date: %s\n",
2640 "Message-ID: <%016"PRIx64"@%s>\n",
2649 smtp_message_printf(tx, "%s\n", res.value);
2656 fatalx("%s", __func__);
2671 io_printf(tx->filter, "%s\n", line ? line : ".");
2684 struct smtp_session *s;
2686 s = tx->session;
2688 log_debug("smtp: %p: message fd %d", s, fd);
2692 smtp_reply(s, "421 %s Temporary Error",
2694 smtp_enter_state(s, STATE_QUIT);
2707 log_trace(TRACE_IO, "filter session io (smtp): %p: %s %s", tx, io_strevent(evt),
2730 struct smtp_session *s;
2732 s = tx->session;
2734 log_debug("smtp: %p: filter fd %d", s, fd);
2744 struct smtp_session *s;
2752 s = tx->session;
2754 log_debug("smtp: %p: message begin", s);
2756 smtp_reply(s, "354 Enter mail, end with \".\""
2759 if (s->junk || (s->tx && s->tx->junk))
2763 if (!(s->listener->flags & F_MASK_SOURCE)) {
2764 m_printf(tx, "from %s (%s %s%s%s)",
2765 s->helo,
2766 s->rdns,
2767 s->ss.ss_family == AF_INET6 ? "" : "[",
2768 ss_to_text(&s->ss),
2769 s->ss.ss_family == AF_INET6 ? "" : "]");
2771 m_printf(tx, "\n\tby %s (%s) with %sSMTP%s%s id %08x",
2772 s->smtpname,
2774 s->flags & SF_EHLO ? "E" : "",
2775 s->flags & SF_SECURE ? "S" : "",
2776 s->flags & SF_AUTHENTICATED ? "A" : "",
2779 if (s->flags & SF_SECURE) {
2780 m_printf(tx, " (%s:%s:%d:%s)",
2781 tls_conn_version(io_tls(s->io)),
2782 tls_conn_cipher(io_tls(s->io)),
2783 tls_conn_cipher_strength(io_tls(s->io)),
2784 (s->flags & SF_VERIFIED) ? "YES" : "NO");
2786 if (s->listener->flags & F_RECEIVEDAUTH) {
2787 m_printf(tx, " auth=%s",
2788 s->username[0] ? "yes" : "no");
2789 if (s->username[0])
2790 m_printf(tx, " user=%s", s->username);
2796 m_printf(tx, "\n\tfor <%s@%s>",
2801 m_printf(tx, ";\n\t%s\n", time_to_text(time(&tx->time)));
2803 smtp_enter_state(s, STATE_BODY);
2809 struct smtp_session *s;
2811 s = tx->session;
2813 log_debug("debug: %p: end of message, error=%d", s, tx->error);
2824 smtp_reply(s, "554 %s %s: Transaction failed, message too big",
2830 smtp_reply(s, "500 %s %s: Loop detected",
2836 smtp_reply(s, "550 %s %s: Message is not RFC 2822 compliant",
2843 smtp_reply(s, "421 %s Temporary Error",
2849 smtp_reply(s, "421 Internal server error");
2854 smtp_enter_state(s, STATE_HELO);
2929 smtp_report_link_connect(struct smtp_session *s, const char *rdns, int fcrdns,
2933 if (! SESSION_FILTERED(s))
2936 report_smtp_link_connect("smtp-in", s->id, rdns, fcrdns, ss_src, ss_dest);
2940 smtp_report_link_greeting(struct smtp_session *s,
2943 if (! SESSION_FILTERED(s))
2946 report_smtp_link_greeting("smtp-in", s->id, domain);
2950 smtp_report_link_identify(struct smtp_session *s, const char *method, const char *identity)
2952 if (! SESSION_FILTERED(s))
2955 report_smtp_link_identify("smtp-in", s->id, method, identity);
2959 smtp_report_link_tls(struct smtp_session *s, const char *ssl)
2961 if (! SESSION_FILTERED(s))
2964 report_smtp_link_tls("smtp-in", s->id, ssl);
2968 smtp_report_link_disconnect(struct smtp_session *s)
2970 if (! SESSION_FILTERED(s))
2973 report_smtp_link_disconnect("smtp-in", s->id);
2977 smtp_report_link_auth(struct smtp_session *s, const char *user, const char *result)
2979 if (! SESSION_FILTERED(s))
2982 report_smtp_link_auth("smtp-in", s->id, user, result);
2986 smtp_report_tx_reset(struct smtp_session *s, uint32_t msgid)
2988 if (! SESSION_FILTERED(s))
2991 report_smtp_tx_reset("smtp-in", s->id, msgid);
2995 smtp_report_tx_begin(struct smtp_session *s, uint32_t msgid)
2997 if (! SESSION_FILTERED(s))
3000 report_smtp_tx_begin("smtp-in", s->id, msgid);
3004 smtp_report_tx_mail(struct smtp_session *s, uint32_t msgid, const char *address, int ok)
3009 if (! SESSION_FILTERED(s))
3019 report_smtp_tx_mail("smtp-in", s->id, msgid, mailaddr, ok);
3023 smtp_report_tx_rcpt(struct smtp_session *s, uint32_t msgid, const char *address, int ok)
3028 if (! SESSION_FILTERED(s))
3038 report_smtp_tx_rcpt("smtp-in", s->id, msgid, mailaddr, ok);
3042 smtp_report_tx_envelope(struct smtp_session *s, uint32_t msgid, uint64_t evpid)
3044 if (! SESSION_FILTERED(s))
3047 report_smtp_tx_envelope("smtp-in", s->id, msgid, evpid);
3051 smtp_report_tx_data(struct smtp_session *s, uint32_t msgid, int ok)
3053 if (! SESSION_FILTERED(s))
3056 report_smtp_tx_data("smtp-in", s->id, msgid, ok);
3060 smtp_report_tx_commit(struct smtp_session *s, uint32_t msgid, size_t msgsz)
3062 if (! SESSION_FILTERED(s))
3065 report_smtp_tx_commit("smtp-in", s->id, msgid, msgsz);
3069 smtp_report_tx_rollback(struct smtp_session *s, uint32_t msgid)
3071 if (! SESSION_FILTERED(s))
3074 report_smtp_tx_rollback("smtp-in", s->id, msgid);
3078 smtp_report_protocol_client(struct smtp_session *s, const char *command)
3080 if (! SESSION_FILTERED(s))
3083 report_smtp_protocol_client("smtp-in", s->id, command);
3087 smtp_report_protocol_server(struct smtp_session *s, const char *response)
3089 if (! SESSION_FILTERED(s))
3092 report_smtp_protocol_server("smtp-in", s->id, response);
3096 smtp_report_filter_response(struct smtp_session *s, int phase, int response, const char *param)
3098 if (! SESSION_FILTERED(s))
3101 report_smtp_filter_response("smtp-in", s->id, phase, response, param);
3105 smtp_report_timeout(struct smtp_session *s)
3107 if (! SESSION_FILTERED(s))
3110 report_smtp_timeout("smtp-in", s->id);