build the sockaddr_un without copy
[ghsmtp.git] / snd.cpp
blobf227eac9cdadec0014b0791f1ac4e7fd43a0147c
1 // Toy program to send email. This is used to test my SMTP server,
2 // mostly. It's overgrown a bit.
4 #include <gflags/gflags.h>
5 namespace gflags {
6 // in case we didn't have one
9 DEFINE_uint64(reps, 1, "now many duplicate transactions per connection");
11 // This needs to be at least the length of each string it's trying to match.
12 DEFINE_uint64(bfr_size, 4 * 1024, "parser buffer size");
14 DEFINE_bool(selftest, false, "run a self test");
16 DEFINE_bool(pipeline_quit, false, "pipeline the QUIT command");
17 DEFINE_bool(badpipline, false, "send two NOOPs back-to-back");
18 DEFINE_bool(bare_lf, false, "send a bare LF");
19 DEFINE_bool(huge_size, false, "attempt with huge size");
20 DEFINE_bool(long_line, false, "super long text line");
21 DEFINE_bool(noconn, false, "don't connect to any host");
22 DEFINE_bool(noop, false, "send a NOOP right after EHLO");
23 DEFINE_bool(nosend, false, "don't actually send any mail");
24 DEFINE_bool(pipe, false, "send to stdin/stdout");
25 DEFINE_bool(rawmsg, false, "the body file includes the headers");
26 DEFINE_bool(rawdog,
27 false,
28 "send the body exactly as is, don't fix CRLF issues "
29 "or escape leading dots");
30 DEFINE_bool(require_tls, true, "use STARTTLS or die");
31 DEFINE_bool(save, false, "save mail in .Sent");
32 DEFINE_bool(slow_strangle, false, "super slow mo");
33 DEFINE_bool(to_the_neck, false, "shove data forever");
35 DEFINE_bool(use_8bitmime, true, "use 8BITMIME extension");
36 DEFINE_bool(use_binarymime, true, "use BINARYMIME extension");
37 DEFINE_bool(use_chunking, true, "use CHUNKING extension");
38 DEFINE_bool(use_deliverby, false, "use DELIVERBY extension");
39 DEFINE_bool(use_esmtp, true, "use ESMTP (EHLO)");
40 DEFINE_bool(use_pipelining, true, "use PIPELINING extension");
41 DEFINE_bool(use_prdr, true, "use PRDR extension");
42 DEFINE_bool(use_size, true, "use SIZE extension");
43 DEFINE_bool(use_smtputf8, true, "use SMTPUTF8 extension");
44 DEFINE_bool(use_tls, true, "use STARTTLS extension");
46 // To force it, set if you have UTF8 in the local part of any RFC5321
47 // address.
48 DEFINE_bool(force_smtputf8, false, "force SMTPUTF8 extension");
50 DEFINE_string(sender, "", "FQDN of sending node");
52 DEFINE_string(local_address, "", "local address to bind");
53 DEFINE_string(mx_host, "", "FQDN of receiving node");
54 DEFINE_string(service, "smtp-test", "service name");
55 DEFINE_string(client_id, "", "client name (ID) for EHLO/HELO");
57 DEFINE_string(from, "", "RFC5322 From: address");
58 DEFINE_string(from_name, "", "RFC5322 From: name");
60 DEFINE_string(to, "", "RFC5322 To: address");
61 DEFINE_string(to_name, "", "RFC5322 To: name");
63 DEFINE_string(smtp_from, "", "RFC5321 MAIL FROM address");
64 DEFINE_string(smtp_to, "", "RFC5321 RCPT TO address");
65 DEFINE_string(smtp_to2, "", "second RFC5321 RCPT TO address");
66 DEFINE_string(smtp_to3, "", "third RFC5321 RCPT TO address");
68 DEFINE_string(content_type, "", "RFC5322 Content-Type");
69 DEFINE_string(content_transfer_encoding,
70 "",
71 "RFC5322 Content-Transfer-Encoding");
73 DEFINE_string(subject, "testing one, two, three...", "RFC5322 Subject");
74 DEFINE_string(keywords, "", "RFC5322 Keywords: header");
75 DEFINE_string(references, "", "RFC5322 References: header");
76 DEFINE_string(in_reply_to, "", "RFC5322 In-Reply-To: header");
77 DEFINE_string(reply_to, "", "RFC5322 Reply-To: header");
78 DEFINE_string(reply_2, "", "Second RFC5322 Reply-To: header");
80 DEFINE_bool(4, false, "use only IP version 4");
81 DEFINE_bool(6, false, "use only IP version 6");
83 DEFINE_string(username, "", "AUTH username");
84 DEFINE_string(password, "", "AUTH password");
86 DEFINE_bool(use_dkim, true, "sign with DKIM");
87 DEFINE_bool(bogus_dkim, false, "sign with bogus DKIM");
88 DEFINE_string(selector, "ghsmtp", "DKIM selector");
89 DEFINE_string(dkim_key_file, "", "DKIM key file");
91 #include "Base64.hpp"
92 #include "DNS-fcrdns.hpp"
93 #include "DNS.hpp"
94 #include "Domain.hpp"
95 #include "IP4.hpp"
96 #include "IP6.hpp"
97 #include "Magic.hpp"
98 #include "Mailbox.hpp"
99 #include "MessageStore.hpp"
100 #include "Now.hpp"
101 #include "OpenDKIM.hpp"
102 #include "Pill.hpp"
103 #include "Sock.hpp"
104 #include "fs.hpp"
105 #include "imemstream.hpp"
106 #include "osutil.hpp"
107 #include "sa.hpp"
109 #include <algorithm>
110 #include <fstream>
111 #include <functional>
112 #include <iomanip>
113 #include <iostream>
114 #include <iterator>
115 #include <random>
116 #include <string>
117 #include <string_view>
118 #include <unordered_map>
120 #include <netdb.h>
121 #include <sys/socket.h>
122 #include <sys/types.h>
124 #include <fmt/format.h>
125 #include <fmt/ostream.h>
127 #include <boost/algorithm/string/case_conv.hpp>
129 #include <boost/iostreams/device/mapped_file.hpp>
131 #include <tao/pegtl.hpp>
132 #include <tao/pegtl/contrib/abnf.hpp>
134 using namespace tao::pegtl;
135 using namespace tao::pegtl::abnf;
137 using namespace std::string_literals;
139 namespace Config {
140 constexpr auto read_timeout = std::chrono::minutes(24 * 60);
141 constexpr auto write_timeout = std::chrono::minutes(24 * 60);
142 } // namespace Config
144 // clang-format off
146 namespace chars {
147 struct tail : range<'\x80', '\xBF'> {};
149 struct ch_1 : range<'\x00', '\x7F'> {};
151 struct ch_2 : seq<range<'\xC2', '\xDF'>, tail> {};
153 struct ch_3 : sor<seq<one<'\xE0'>, range<'\xA0', '\xBF'>, tail>,
154 seq<range<'\xE1', '\xEC'>, rep<2, tail>>,
155 seq<one<'\xED'>, range<'\x80', '\x9F'>, tail>,
156 seq<range<'\xEE', '\xEF'>, rep<2, tail>>> {};
158 struct ch_4 : sor<seq<one<'\xF0'>, range<'\x90', '\xBF'>, rep<2, tail>>,
159 seq<range<'\xF1', '\xF3'>, rep<3, tail>>,
160 seq<one<'\xF4'>, range<'\x80', '\x8F'>, rep<2, tail>>> {};
162 struct u8char : sor<ch_1, ch_2, ch_3, ch_4> {};
164 struct non_ascii : sor<ch_2, ch_3, ch_4> {};
166 struct ascii_only : seq<star<ch_1>, eof> {};
168 struct utf8_only : seq<star<u8char>, eof> {};
171 namespace RFC5322 {
173 struct VUCHAR : sor<VCHAR, chars::non_ascii> {};
175 using dot = one<'.'>;
176 using colon = one<':'>;
178 // All 7-bit ASCII except NUL (0), LF (10) and CR (13).
179 struct text_ascii : ranges<1, 9, 11, 12, 14, 127> {};
181 // Short lines of ASCII text. LF or CRLF line separators.
182 struct body_ascii : seq<star<seq<rep_max<998, text_ascii>, eol>>,
183 opt<rep_max<998, text_ascii>>, eof> {};
185 struct text_utf8 : sor<text_ascii, chars::non_ascii> {};
187 // Short lines of UTF-8 text. LF or CRLF line separators.
188 struct body_utf8 : seq<star<seq<rep_max<998, text_utf8>, eol>>,
189 opt<rep_max<998, text_utf8>>, eof> {};
191 struct FWS : seq<opt<seq<star<WSP>, eol>>, plus<WSP>> {};
193 struct qtext : sor<one<33>, ranges<35, 91, 93, 126>, chars::non_ascii> {};
195 struct quoted_pair : seq<one<'\\'>, sor<VUCHAR, WSP>> {};
197 struct atext : sor<ALPHA, DIGIT,
198 one<'!', '#',
199 '$', '%',
200 '&', '\'',
201 '*', '+',
202 '-', '/',
203 '=', '?',
204 '^', '_',
205 '`', '{',
206 '|', '}',
207 '~'>,
208 chars::non_ascii> {};
210 // ctext is ASCII not '(' or ')' or '\\'
211 struct ctext : sor<ranges<33, 39, 42, 91, 93, 126>, chars::non_ascii> {};
213 struct comment;
215 struct ccontent : sor<ctext, quoted_pair, comment> {};
217 struct comment
218 : seq<one<'('>, star<seq<opt<FWS>, ccontent>>, opt<FWS>, one<')'>> {};
220 struct CFWS : sor<seq<plus<seq<opt<FWS>, comment>, opt<FWS>>>, FWS> {};
222 struct qcontent : sor<qtext, quoted_pair> {};
224 // Corrected in errata ID: 3135
225 struct quoted_string
226 : seq<opt<CFWS>,
227 DQUOTE,
228 sor<seq<star<seq<opt<FWS>, qcontent>>, opt<FWS>>, FWS>,
229 DQUOTE,
230 opt<CFWS>> {};
232 // *([FWS] VCHAR) *WSP
233 struct unstructured : seq<star<seq<opt<FWS>, VUCHAR>>, star<WSP>> {};
235 struct atom : seq<opt<CFWS>, plus<atext>, opt<CFWS>> {};
237 struct dot_atom_text : list<plus<atext>, dot> {};
239 struct dot_atom : seq<opt<CFWS>, dot_atom_text, opt<CFWS>> {};
241 struct word : sor<atom, quoted_string> {};
243 struct phrase : plus<word> {};
245 struct local_part : sor<dot_atom, quoted_string> {};
247 // from '!' to '~' excluding 91 92 93 '[' '\\' ']'
249 struct dtext : ranges<33, 90, 94, 126> {};
251 struct domain_literal : seq<opt<CFWS>,
252 one<'['>,
253 star<seq<opt<FWS>, dtext>>,
254 opt<FWS>,
255 one<']'>,
256 opt<CFWS>> {};
258 struct domain : sor<dot_atom, domain_literal> {};
260 struct addr_spec : seq<local_part, one<'@'>, domain> {};
262 struct postmaster : TAO_PEGTL_ISTRING("Postmaster") {};
264 struct addr_spec_or_postmaster : sor<addr_spec, postmaster> {};
266 struct addr_spec_only : seq<addr_spec_or_postmaster, eof> {};
268 struct display_name : phrase {};
270 struct display_name_only : seq<display_name, eof> {};
272 // clang-format on
274 // struct name_addr : seq<opt<display_name>, angle_addr> {};
276 // struct mailbox : sor<name_addr, addr_spec> {};
278 template <typename Rule>
279 struct inaction : nothing<Rule> {
282 template <typename Rule>
283 struct action : nothing<Rule> {
286 template <>
287 struct action<local_part> {
288 template <typename Input>
289 static void apply(Input const& in, Mailbox& mbx)
291 mbx.set_local(in.string());
295 template <>
296 struct action<domain> {
297 template <typename Input>
298 static void apply(Input const& in, Mailbox& mbx)
300 mbx.set_domain(in.string());
303 } // namespace RFC5322
305 namespace RFC5321 {
307 struct Connection {
308 Sock sock;
310 std::string server_id;
312 std::string ehlo_keyword;
313 std::vector<std::string> ehlo_param;
314 std::unordered_map<std::string, std::vector<std::string>> ehlo_params;
316 std::string reply_code;
318 bool greeting_ok{false};
319 bool ehlo_ok{false};
321 bool has_extension(char const* name) const
323 return ehlo_params.find(name) != end(ehlo_params);
326 Connection(int fd_in, int fd_out, std::function<void(void)> read_hook)
327 : sock(
328 fd_in, fd_out, read_hook, Config::read_timeout, Config::write_timeout)
333 // clang-format off
335 using dot = one<'.'>;
336 using colon = one<':'>;
337 using dash = one<'-'>;
338 using underscore = one<'_'>;
340 struct u_let_dig : sor<ALPHA, DIGIT, chars::non_ascii> {};
342 struct u_ldh_tail : star<sor<seq<plus<one<'-'>>, u_let_dig>, u_let_dig>> {};
344 struct u_label : seq<u_let_dig, u_ldh_tail> {};
346 struct let_dig : sor<ALPHA, DIGIT> {};
348 struct ldh_tail : star<sor<seq<plus<one<'-'>>, let_dig>, let_dig>> {};
350 struct ldh_str : seq<let_dig, ldh_tail> {};
352 struct label : ldh_str {};
354 struct sub_domain : sor<label, u_label> {};
356 struct domain : list<sub_domain, dot> {};
358 struct dec_octet : sor<seq<string<'2','5'>, range<'0','5'>>,
359 seq<one<'2'>, range<'0','4'>, DIGIT>,
360 seq<range<'0', '1'>, rep<2, DIGIT>>,
361 rep_min_max<1, 2, DIGIT>> {};
363 struct IPv4_address_literal
364 : seq<dec_octet, dot, dec_octet, dot, dec_octet, dot, dec_octet> {};
366 struct h16 : rep_min_max<1, 4, HEXDIG> {};
368 struct ls32 : sor<seq<h16, colon, h16>, IPv4_address_literal> {};
370 struct dcolon : two<':'> {};
372 struct IPv6address : sor<seq< rep<6, h16, colon>, ls32>,
373 seq< dcolon, rep<5, h16, colon>, ls32>,
374 seq<opt<h16 >, dcolon, rep<4, h16, colon>, ls32>,
375 seq<opt<h16, opt< colon, h16>>, dcolon, rep<3, h16, colon>, ls32>,
376 seq<opt<h16, rep_opt<2, colon, h16>>, dcolon, rep<2, h16, colon>, ls32>,
377 seq<opt<h16, rep_opt<3, colon, h16>>, dcolon, h16, colon, ls32>,
378 seq<opt<h16, rep_opt<4, colon, h16>>, dcolon, ls32>,
379 seq<opt<h16, rep_opt<5, colon, h16>>, dcolon, h16>,
380 seq<opt<h16, rep_opt<6, colon, h16>>, dcolon >> {};
382 struct IPv6_address_literal : seq<TAO_PEGTL_ISTRING("IPv6:"), IPv6address> {};
384 struct dcontent : ranges<33, 90, 94, 126> {};
386 struct standardized_tag : ldh_str {};
388 struct general_address_literal : seq<standardized_tag, colon, plus<dcontent>> {};
390 // See rfc 5321 Section 4.1.3
391 struct address_literal : seq<one<'['>,
392 sor<IPv4_address_literal,
393 IPv6_address_literal,
394 general_address_literal>,
395 one<']'>> {};
398 struct qtextSMTP : sor<ranges<32, 33, 35, 91, 93, 126>, chars::non_ascii> {};
399 struct graphic : range<32, 126> {};
400 struct quoted_pairSMTP : seq<one<'\\'>, graphic> {};
401 struct qcontentSMTP : sor<qtextSMTP, quoted_pairSMTP> {};
403 // excluded from atext: "(),.@[]"
404 struct atext : sor<ALPHA, DIGIT,
405 one<'!', '#',
406 '$', '%',
407 '&', '\'',
408 '*', '+',
409 '-', '/',
410 '=', '?',
411 '^', '_',
412 '`', '{',
413 '|', '}',
414 '~'>,
415 chars::non_ascii> {};
416 struct atom : plus<atext> {};
417 struct dot_string : list<atom, dot> {};
418 struct quoted_string : seq<one<'"'>, star<qcontentSMTP>, one<'"'>> {};
419 struct local_part : sor<dot_string, quoted_string> {};
420 struct non_local_part : sor<domain, address_literal> {};
421 struct mailbox : seq<local_part, one<'@'>, non_local_part> {};
423 struct at_domain : seq<one<'@'>, domain> {};
425 struct a_d_l : list<at_domain, one<','>> {};
427 struct path : seq<opt<seq<a_d_l, colon>>, mailbox> {};
429 struct postmaster : TAO_PEGTL_ISTRING("Postmaster") {};
431 struct path_or_postmaster : seq<sor<path, postmaster>, eof> {};
433 // textstring = 1*(%d09 / %d32-126) ; HT, SP, Printable US-ASCII
435 // Although not explicit in the grammar of RFC-6531, in practice UTF-8
436 // is used in the replies.
438 // struct textstring : plus<sor<one<9>, range<32, 126>>> {};
440 struct textstring : plus<sor<one<9>, range<32, 126>, chars::non_ascii>> {};
442 struct crap : plus<range<32, 126>> {};
444 struct server_id : sor<domain, address_literal, crap> {};
446 // Greeting = ( "220 " (Domain / address-literal) [ SP textstring ] CRLF )
447 // /
448 // ( "220-" (Domain / address-literal) [ SP textstring ] CRLF
449 // *( "220-" [ textstring ] CRLF )
450 // "220" [ SP textstring ] CRLF )
452 struct greeting_ok
453 : sor<seq<TAO_PEGTL_ISTRING("220 "), server_id, opt<textstring>, CRLF>,
454 seq<TAO_PEGTL_ISTRING("220-"), server_id, opt<textstring>, CRLF,
455 star<seq<TAO_PEGTL_ISTRING("220-"), opt<textstring>, CRLF>>,
456 seq<TAO_PEGTL_ISTRING("220"), opt<seq<SP, textstring>>, CRLF>>> {};
458 // Reply-code = %x32-35 %x30-35 %x30-39
460 struct reply_code
461 : seq<range<0x32, 0x35>, range<0x30, 0x35>, range<0x30, 0x39>> {};
463 // Reply-line = *( Reply-code "-" [ textstring ] CRLF )
464 // Reply-code [ SP textstring ] CRLF
466 struct reply_lines
467 : seq<star<seq<reply_code, one<'-'>, opt<textstring>, CRLF>>,
468 seq<reply_code, opt<seq<SP, textstring>>, CRLF>> {};
470 struct greeting
471 : sor<greeting_ok, reply_lines> {};
473 // ehlo-greet = 1*(%d0-9 / %d11-12 / %d14-127)
474 // ; string of any characters other than CR or LF
476 struct ehlo_greet : plus<ranges<0, 9, 11, 12, 14, 127>> {};
478 // ehlo-keyword = (ALPHA / DIGIT) *(ALPHA / DIGIT / "-")
479 // ; additional syntax of ehlo-params depends on
480 // ; ehlo-keyword
482 // The '.' we also allow in ehlo-keyword since it has been seen in the
483 // wild at least at 263.net.
485 struct ehlo_keyword : seq<sor<ALPHA, DIGIT>, star<sor<ALPHA, DIGIT, dash, dot, underscore>>> {};
487 // ehlo-param = 1*(%d33-126)
488 // ; any CHAR excluding <SP> and all
489 // ; control characters (US-ASCII 0-31 and 127
490 // ; inclusive)
492 struct ehlo_param : plus<range<33, 126>> {};
494 // ehlo-line = ehlo-keyword *( SP ehlo-param )
496 // The AUTH= thing is so common with some servers (postfix) that I
497 // guess we have to accept it.
499 struct ehlo_line
500 : seq<ehlo_keyword, star<seq<sor<SP,one<'='>>, ehlo_param>>> {};
502 // ehlo-ok-rsp = ( "250 " Domain [ SP ehlo-greet ] CRLF )
503 // /
504 // ( "250-" Domain [ SP ehlo-greet ] CRLF
505 // *( "250-" ehlo-line CRLF )
506 // "250 " ehlo-line CRLF )
508 // The last line having the optional ehlo_line is not strictly correct.
509 // Was added to work with postfix/src/smtpstone/smtp-sink.c.
511 struct ehlo_ok_rsp
512 : sor<seq<TAO_PEGTL_ISTRING("250 "), server_id, opt<ehlo_greet>, CRLF>,
514 seq<TAO_PEGTL_ISTRING("250-"), server_id, opt<ehlo_greet>, CRLF,
515 star<seq<TAO_PEGTL_ISTRING("250-"), ehlo_line, CRLF>>,
516 seq<TAO_PEGTL_ISTRING("250 "), opt<ehlo_line>, CRLF>>
517 > {};
519 struct ehlo_rsp
520 : sor<ehlo_ok_rsp, reply_lines> {};
522 struct helo_ok_rsp
523 : seq<TAO_PEGTL_ISTRING("250 "), server_id, opt<ehlo_greet>, CRLF> {};
525 struct auth_login_username
526 : seq<TAO_PEGTL_STRING("334 VXNlcm5hbWU6"), CRLF> {};
528 struct auth_login_password
529 : seq<TAO_PEGTL_STRING("334 UGFzc3dvcmQ6"), CRLF> {};
531 // clang-format on
533 template <typename Rule>
534 struct inaction : nothing<Rule> {
537 template <typename Rule>
538 struct action : nothing<Rule> {
541 template <>
542 struct action<server_id> {
543 template <typename Input>
544 static void apply(Input const& in, Connection& cnn)
546 cnn.server_id = in.string();
550 template <>
551 struct action<local_part> {
552 template <typename Input>
553 static void apply(Input const& in, Mailbox& mbx)
555 mbx.set_local(in.string());
559 template <>
560 struct action<non_local_part> {
561 template <typename Input>
562 static void apply(Input const& in, Mailbox& mbx)
564 mbx.set_domain(in.string());
568 template <>
569 struct action<greeting_ok> {
570 template <typename Input>
571 static void apply(Input const& in, Connection& cnn)
573 cnn.greeting_ok = true;
574 imemstream stream{begin(in), size(in)};
575 std::string line;
576 while (std::getline(stream, line)) {
577 LOG(INFO) << " S: " << line;
582 template <>
583 struct action<ehlo_ok_rsp> {
584 template <typename Input>
585 static void apply(Input const& in, Connection& cnn)
587 cnn.ehlo_ok = true;
588 imemstream stream{begin(in), size(in)};
589 std::string line;
590 while (std::getline(stream, line)) {
591 LOG(INFO) << " S: " << line;
596 template <>
597 struct action<ehlo_keyword> {
598 template <typename Input>
599 static void apply(Input const& in, Connection& cnn)
601 cnn.ehlo_keyword = in.string();
602 boost::to_upper(cnn.ehlo_keyword);
606 template <>
607 struct action<ehlo_param> {
608 template <typename Input>
609 static void apply(Input const& in, Connection& cnn)
611 cnn.ehlo_param.push_back(in.string());
615 template <>
616 struct action<ehlo_line> {
617 template <typename Input>
618 static void apply(Input const& in, Connection& cnn)
620 cnn.ehlo_params.emplace(std::move(cnn.ehlo_keyword),
621 std::move(cnn.ehlo_param));
625 template <>
626 struct action<reply_lines> {
627 template <typename Input>
628 static void apply(Input const& in, Connection& cnn)
630 imemstream stream{begin(in), size(in)};
631 std::string line;
632 while (std::getline(stream, line)) {
633 LOG(INFO) << " S: " << line;
638 template <>
639 struct action<reply_code> {
640 template <typename Input>
641 static void apply(Input const& in, Connection& cnn)
643 cnn.reply_code = in.string();
646 } // namespace RFC5321
648 namespace {
650 int conn(DNS::Resolver& res, Domain const& node, uint16_t port)
652 auto const use_4{!FLAGS_6};
653 auto const use_6{!FLAGS_4};
655 if (use_6) {
656 auto const fd{socket(AF_INET6, SOCK_STREAM, 0)};
657 PCHECK(fd >= 0) << "socket() failed";
659 if (!FLAGS_local_address.empty()) {
660 auto loc{sockaddr_in6{}};
661 loc.sin6_family = AF_INET6;
662 if (1 != inet_pton(AF_INET6, FLAGS_local_address.c_str(),
663 reinterpret_cast<void*>(&loc.sin6_addr))) {
664 LOG(FATAL) << "can't interpret " << FLAGS_local_address
665 << " as IPv6 address";
667 PCHECK(0 == bind(fd, reinterpret_cast<sockaddr*>(&loc), sizeof(loc)));
670 auto addrs{std::vector<std::string>{}};
672 if (node.is_address_literal()) {
673 if (IP6::is_address(node.ascii())) {
674 addrs.push_back(node.ascii());
676 if (IP6::is_address_literal(node.ascii())) {
677 auto const addr = IP6::as_address(node.ascii());
678 addrs.push_back(std::string(addr.data(), addr.length()));
681 else {
682 addrs = res.get_strings(DNS::RR_type::AAAA, node.ascii());
684 for (auto const& addr : addrs) {
685 auto in6{sockaddr_in6{}};
686 in6.sin6_family = AF_INET6;
687 in6.sin6_port = htons(port);
688 CHECK_EQ(inet_pton(AF_INET6, addr.c_str(),
689 reinterpret_cast<void*>(&in6.sin6_addr)),
691 if (connect(fd, reinterpret_cast<const sockaddr*>(&in6), sizeof(in6))) {
692 PLOG(WARNING) << "connect failed [" << addr << "]:" << port;
693 continue;
696 LOG(INFO) << fd << " connected to [" << addr << "]:" << port;
697 return fd;
700 close(fd);
702 if (use_4) {
703 auto fd{socket(AF_INET, SOCK_STREAM, 0)};
704 PCHECK(fd >= 0) << "socket() failed";
706 if (!FLAGS_local_address.empty()) {
707 auto loc{sockaddr_in{}};
708 loc.sin_family = AF_INET;
709 if (1 != inet_pton(AF_INET, FLAGS_local_address.c_str(),
710 reinterpret_cast<void*>(&loc.sin_addr))) {
711 LOG(FATAL) << "can't interpret " << FLAGS_local_address
712 << " as IPv4 address";
714 LOG(INFO) << "bind " << FLAGS_local_address;
715 PCHECK(0 == bind(fd, reinterpret_cast<sockaddr*>(&loc), sizeof(loc)));
718 auto addrs{std::vector<std::string>{}};
719 if (node.is_address_literal()) {
720 if (IP4::is_address(node.ascii())) {
721 addrs.push_back(node.ascii());
723 if (IP4::is_address_literal(node.ascii())) {
724 auto const addr = IP4::as_address(node.ascii());
725 addrs.push_back(std::string(addr.data(), addr.length()));
728 else {
729 addrs = res.get_strings(DNS::RR_type::A, node.ascii());
731 for (auto addr : addrs) {
732 auto in4{sockaddr_in{}};
733 in4.sin_family = AF_INET;
734 in4.sin_port = htons(port);
735 CHECK_EQ(inet_pton(AF_INET, addr.c_str(),
736 reinterpret_cast<void*>(&in4.sin_addr)),
738 if (connect(fd, reinterpret_cast<const sockaddr*>(&in4), sizeof(in4))) {
739 PLOG(WARNING) << "connect failed " << addr << ":" << port;
740 continue;
743 LOG(INFO) << " connected to " << addr << ":" << port;
744 return fd;
747 close(fd);
750 return -1;
753 class Eml {
754 public:
755 void add_hdr(std::string name, std::string value)
757 hdrs_.push_back(std::make_pair(name, value));
760 void foreach_hdr(std::function<void(std::string const& name,
761 std::string const& value)> func)
763 for (auto const& [name, value] : hdrs_) {
764 func(name, value);
768 private:
769 std::vector<std::pair<std::string, std::string>> hdrs_;
771 friend std::ostream& operator<<(std::ostream& os, Eml const& eml)
773 for (auto const& [name, value] : eml.hdrs_) {
774 os << name << ": " << value << "\r\n";
776 // return os << "\r\n"; // end of headers
777 return os /* << "\r\n" */; // end of headers
781 // // clang-format off
782 // char const* const signhdrs[] = {
783 // "From",
785 // "Message-ID",
787 // "Cc",
788 // "Date",
789 // "In-Reply-To",
790 // "References",
791 // "Reply-To",
792 // "Sender",
793 // "Subject",
794 // "To",
796 // "MIME-Version",
797 // "Content-Type",
798 // "Content-Transfer-Encoding",
800 // nullptr
801 // };
802 // clang-format on
804 enum class transfer_encoding {
805 seven_bit,
806 quoted_printable,
807 base64,
808 eight_bit,
809 binary,
812 enum class data_type {
813 ascii, // 7bit, quoted-printable and base64
814 utf8, // 8bit
815 binary, // binary
818 data_type type(std::string_view d)
821 auto in{memory_input<>{d.data(), d.size(), "data"}};
822 if (parse<RFC5322::body_ascii>(in)) {
823 return data_type::ascii;
827 auto in{memory_input<>{d.data(), d.size(), "data"}};
828 if (parse<RFC5322::body_utf8>(in)) {
829 return data_type::utf8;
832 // anything else is
833 return data_type::binary;
836 class content {
837 public:
838 content(char const* path)
839 : path_(path)
841 auto const body_sz{fs::file_size(path_)};
842 CHECK(body_sz) << "no body";
843 file_.open(path_);
844 type_ = ::type(*this);
847 char const* data() const { return file_.data(); }
848 size_t size() const { return file_.size(); }
849 data_type type() const { return type_; }
851 bool empty() const { return size() == 0; }
852 operator std::string_view() const { return std::string_view(data(), size()); }
854 private:
855 data_type type_;
856 fs::path path_;
857 boost::iostreams::mapped_file_source file_;
860 template <typename Input>
861 void fail(Input& in, RFC5321::Connection& cnn)
863 LOG(INFO) << " C: QUIT";
864 cnn.sock.out() << "QUIT\r\n" << std::flush;
865 // we might have a few error replies stacked up if we're pipelining
866 // CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
867 exit(EXIT_FAILURE);
870 template <typename Input>
871 void quit_on_fail(Input& in, RFC5321::Connection& cnn, std::string_view cmd)
873 cnn.sock.out() << std::flush;
874 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
875 if (cnn.reply_code.at(0) != '2') {
876 LOG(ERROR) << cmd << " returned " << cnn.reply_code;
877 fail(in, cnn);
879 in.discard();
882 bool validate_name(const char* flagname, std::string const& value)
884 if (value.empty()) // empty name needs to validate, but
885 return true; // will not be used
886 memory_input<> name_in(value.c_str(), "name");
887 if (!parse<RFC5322::display_name_only, RFC5322::inaction>(name_in)) {
888 LOG(ERROR) << "bad name syntax " << value;
889 return false;
891 return true;
894 DEFINE_validator(from_name, &validate_name);
895 DEFINE_validator(to_name, &validate_name);
897 bool validate_address_RFC5322(const char* flagname, std::string const& value)
899 if (value.empty()) // empty name needs to validate, but
900 return true; // will not be used
901 memory_input<> name_in(value.c_str(), "address");
902 if (!parse<RFC5322::addr_spec_only, RFC5322::inaction>(name_in)) {
903 LOG(ERROR) << "bad address syntax " << value;
904 return false;
906 return true;
909 DEFINE_validator(from, &validate_address_RFC5322);
910 DEFINE_validator(to, &validate_address_RFC5322);
912 bool validate_address_RFC5321(const char* flagname, std::string const& value)
914 if (value.empty()) { // empty name needs to validate, but
915 return true; // will not be used
917 memory_input<> name_in(value.c_str(), "path");
918 if (!parse<RFC5321::path_or_postmaster, RFC5321::inaction>(name_in)) {
919 LOG(ERROR) << "bad RFC-5321 address syntax " << value;
920 return false;
922 return true;
925 DEFINE_validator(smtp_from, &validate_address_RFC5321);
926 DEFINE_validator(smtp_to, &validate_address_RFC5321);
927 DEFINE_validator(smtp_to2, &validate_address_RFC5321);
928 DEFINE_validator(smtp_to3, &validate_address_RFC5321);
930 void selftest()
932 CHECK(validate_name("selftest", ""s));
933 CHECK(validate_name("selftest", "Elmer J Fudd"s));
934 CHECK(validate_name("selftest", "\"Elmer J. Fudd\""s));
935 CHECK(validate_name("selftest", "Elmer! J! Fudd!"s));
937 CHECK(validate_address_RFC5321("selftest", "foo@digilicious.com"s));
938 CHECK(validate_address_RFC5321("selftest", "\"foo\"@digilicious.com"s));
939 CHECK(validate_address_RFC5321(
940 "selftest",
941 "\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@digilicious.com"s));
943 CHECK(validate_address_RFC5322("selftest", "foo@digilicious.com"s));
944 CHECK(validate_address_RFC5322("selftest", "\"foo\"@digilicious.com"s));
945 CHECK(validate_address_RFC5322(
946 "selftest",
947 "\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@digilicious.com"s));
949 auto const read_hook{[]() {}};
951 const char* greet_list[]{
952 "220-mtaig-aak03.mx.aol.com ESMTP Internet Inbound\r\n"
953 "220-AOL and its affiliated companies do not\r\n"
954 "220-authorize the use of its proprietary computers and computer\r\n"
955 "220-networks to accept, transmit, or distribute unsolicited bulk\r\n"
956 "220-e-mail sent from the internet.\r\n"
957 "220-Effective immediately:\r\n"
958 "220-AOL may no longer accept connections from IP addresses\r\n"
959 "220 which no do not have reverse-DNS (PTR records) assigned.\r\n",
961 "421 mtaig-maa02.mx.aol.com Service unavailable - try again later\r\n",
964 for (auto i : greet_list) {
965 auto cnn{RFC5321::Connection(0, 1, read_hook)};
966 auto in{memory_input<>{i, i}};
967 if (!parse<RFC5321::greeting, RFC5321::action /*, tao::pegtl::tracer*/>(
968 in, cnn)) {
969 LOG(FATAL) << "Error parsing greeting \"" << i << "\"";
971 if (cnn.greeting_ok) {
972 LOG(WARNING) << "greeting ok";
974 else {
975 LOG(WARNING) << "greeting was not in the affirmative";
979 const char* ehlo_rsp_list[]{
980 "250-www77.totaalholding.nl Hello "
981 "ec2-18-205-224-193.compute-1.amazonaws.com [18.205.224.193]\r\n"
982 "250-SIZE 52428800\r\n"
983 "250-8BITMIME\r\n"
984 "250-PIPELINING\r\n"
985 "250-X_PIPE_CONNECT\r\n"
986 "250-STARTTLS\r\n"
987 "250 HELP\r\n",
989 "250-HELLO, SAILOR!\r\n"
990 "250-NO-SOLICITING\r\n"
991 "250 8BITMIME\r\n",
993 "250-digilicious.com at your service, localhost. [IPv6:::1]\r\n"
994 "250-SIZE 15728640\r\n"
995 "250-8BITMIME\r\n"
996 "250-STARTTLS\r\n"
997 "250-ENHANCEDSTATUSCODES\r\n"
998 "250-PIPELINING\r\n"
999 "250-BINARYMIME\r\n"
1000 "250-CHUNKING\r\n"
1001 "250-SMTPUTF8\r\n"
1002 "250 OK\r\n",
1004 "500 5.5.1 command unrecognized: \"EHLO digilicious.com\\r\\n\"\r\n",
1006 "250-263xmail at your service\r\n"
1007 "250-STARTTLS\r\n"
1008 "250-MAE-SMTP\r\n"
1009 "250-263.net\r\n" // the '.' is not RFC complaint
1010 "250-SIZE 104857600\r\n"
1011 "250-ETRN\r\n"
1012 "250-ENHANCEDSTATUSCODES\r\n"
1013 "250-8BITMIME\r\n"
1014 "250 DSN\r\n",
1017 for (auto i : ehlo_rsp_list) {
1018 auto cnn{RFC5321::Connection(0, 1, read_hook)};
1019 auto in{memory_input<>{i, i}};
1020 if (!parse<RFC5321::ehlo_rsp, RFC5321::action /*, tao::pegtl::tracer*/>(
1021 in, cnn)) {
1022 LOG(FATAL) << "Error parsing ehlo response \"" << i << "\"";
1024 if (cnn.ehlo_ok) {
1025 LOG(WARNING) << "ehlo ok";
1027 else {
1028 LOG(WARNING) << "ehlo response was not in the affirmative";
1033 auto get_sender()
1035 if (FLAGS_client_id.empty()) {
1036 FLAGS_client_id = [] {
1037 auto const id_from_env{getenv("GHSMTP_CLIENT_ID")};
1038 if (id_from_env)
1039 return std::string{id_from_env};
1041 auto const hostname{osutil::get_hostname()};
1042 if (hostname.find('.') != std::string::npos)
1043 return hostname;
1045 LOG(FATAL) << "hostname not a FQDN, set GHSMTP_CLIENT_ID maybe?";
1046 }();
1049 if (FLAGS_sender.empty()) {
1050 FLAGS_sender = FLAGS_client_id;
1053 auto const sender{Domain{FLAGS_sender}};
1055 if (FLAGS_from.empty()) {
1056 FLAGS_from = "test-it@"s + sender.utf8();
1059 if (FLAGS_to.empty()) {
1060 FLAGS_to = "test-it@"s + sender.utf8();
1063 return sender;
1066 bool is_localhost(DNS::RR const& rr)
1068 if (std::holds_alternative<DNS::RR_MX>(rr)) {
1069 if (iequal(std::get<DNS::RR_MX>(rr).exchange(), "localhost"))
1070 return true;
1072 return false;
1075 bool starts_with(std::string_view str, std::string_view prefix)
1077 if (str.size() >= prefix.size())
1078 if (str.compare(0, prefix.size(), prefix) == 0)
1079 return true;
1080 return false;
1083 bool sts_rec(std::string const& sts_rec)
1085 return starts_with(sts_rec, "v=STSv1");
1088 std::vector<Domain>
1089 get_receivers(DNS::Resolver& res, Mailbox const& to_mbx, bool& enforce_dane)
1091 auto receivers{std::vector<Domain>{}};
1093 // User provided explicit host to receive mail.
1094 if (!FLAGS_mx_host.empty()) {
1095 receivers.emplace_back(FLAGS_mx_host);
1096 return receivers;
1099 // Non-local part is an address literal.
1100 if (to_mbx.domain().is_address_literal()) {
1101 receivers.emplace_back(to_mbx.domain());
1102 return receivers;
1105 // RFC 5321 section 5.1 "Locating the Target Host"
1107 // “The lookup first attempts to locate an MX record associated with
1108 // the name. If a CNAME record is found, the resulting name is
1109 // processed as if it were the initial name.”
1111 // Our (full) resolver will traverse any CNAMEs for us and return
1112 // the CNAME and MX records all together.
1114 auto const& domain = to_mbx.domain().ascii();
1116 auto q_sts{DNS::Query{res, DNS::RR_type::TXT, "_mta-sts."s + domain}};
1117 if (q_sts.has_record()) {
1118 auto sts_records = q_sts.get_strings();
1119 sts_records.erase(std::remove_if(begin(sts_records), end(sts_records),
1120 std::not_fn(sts_rec)),
1121 end(sts_records));
1122 if (size(sts_records) == 1) {
1123 LOG(INFO) << "### This domain implements MTA-STS ###";
1126 else {
1127 LOG(INFO) << "MTA-STS record not found for domain " << domain;
1130 auto q{DNS::Query{res, DNS::RR_type::MX, domain}};
1131 if (q.has_record()) {
1132 if (q.authentic_data()) {
1133 LOG(INFO) << "### MX records authentic for domain " << domain << " ###";
1135 else {
1136 LOG(INFO) << "MX records can't be authenticated for domain " << domain;
1137 enforce_dane = false;
1140 else {
1141 LOG(INFO) << "no MX records found for domain " << domain;
1143 auto mxs{q.get_records()};
1145 mxs.erase(std::remove_if(begin(mxs), end(mxs), is_localhost), end(mxs));
1147 auto const nmx = std::count_if(begin(mxs), end(mxs), [](auto const& rr) {
1148 return std::holds_alternative<DNS::RR_MX>(rr);
1151 if (nmx == 1) {
1152 for (auto const& mx : mxs) {
1153 if (std::holds_alternative<DNS::RR_MX>(mx)) {
1154 // RFC 7505 null MX record
1155 if ((std::get<DNS::RR_MX>(mx).preference() == 0) &&
1156 (std::get<DNS::RR_MX>(mx).exchange().empty() ||
1157 (std::get<DNS::RR_MX>(mx).exchange() == "."))) {
1158 LOG(INFO) << "domain " << domain << " does not accept mail";
1159 return receivers;
1165 if (nmx == 0) {
1166 // implicit MX RR
1167 receivers.emplace_back(domain);
1168 return receivers;
1171 // […] then the sender-SMTP MUST randomize them to spread the load
1172 // across multiple mail exchangers for a specific organization.
1173 std::shuffle(begin(mxs), end(mxs), std::random_device());
1174 std::sort(begin(mxs), end(mxs), [](auto const& a, auto const& b) {
1175 if (std::holds_alternative<DNS::RR_MX>(a) &&
1176 std::holds_alternative<DNS::RR_MX>(b)) {
1177 return std::get<DNS::RR_MX>(a).preference() <
1178 std::get<DNS::RR_MX>(b).preference();
1180 return false;
1183 if (nmx)
1184 LOG(INFO) << "MXs for " << domain << " are:";
1186 for (auto const& mx : mxs) {
1187 if (std::holds_alternative<DNS::RR_MX>(mx)) {
1188 receivers.emplace_back(std::get<DNS::RR_MX>(mx).exchange());
1189 LOG(INFO) << std::setfill(' ') << std::setw(3)
1190 << std::get<DNS::RR_MX>(mx).preference() << " "
1191 << std::get<DNS::RR_MX>(mx).exchange();
1195 return receivers;
1198 auto parse_mailboxes()
1200 auto from_mbx{Mailbox{}};
1201 auto from_in{memory_input<>{FLAGS_from, "from"}};
1202 if (!parse<RFC5322::addr_spec_only, RFC5322::action>(from_in, from_mbx)) {
1203 LOG(FATAL) << "bad From: address syntax <" << FLAGS_from << ">";
1205 LOG(INFO) << " from_mbx == " << from_mbx;
1207 auto local_from{memory_input<>{from_mbx.local_part(), "from.local"}};
1208 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_from);
1210 auto to_mbx{Mailbox{}};
1211 auto to_in{memory_input<>{FLAGS_to, "to"}};
1212 if (!parse<RFC5322::addr_spec_only, RFC5322::action>(to_in, to_mbx)) {
1213 LOG(FATAL) << "bad To: address syntax <" << FLAGS_to << ">";
1215 LOG(INFO) << " to_mbx == " << to_mbx;
1217 auto local_to{memory_input<>{to_mbx.local_part(), "to.local"}};
1218 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_to);
1220 auto smtp_from_mbx{Mailbox{}};
1221 if (!FLAGS_smtp_from.empty()) {
1222 auto smtp_from_in{memory_input<>{FLAGS_smtp_from, "SMTP.from"}};
1223 if (!parse<RFC5321::path_or_postmaster, RFC5321::action>(smtp_from_in,
1224 smtp_from_mbx)) {
1225 LOG(FATAL) << "bad MAIL FROM: address syntax <" << FLAGS_smtp_from << ">";
1227 LOG(INFO) << " smtp_from_mbx == " << smtp_from_mbx;
1228 auto local_smtp_from{
1229 memory_input<>{smtp_from_mbx.local_part(), "SMTP.from.local"}};
1230 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_smtp_from);
1232 else {
1233 smtp_from_mbx = from_mbx;
1236 auto smtp_to_mbx{Mailbox{}};
1237 if (!FLAGS_smtp_to.empty()) {
1238 auto smtp_to_in{memory_input<>{FLAGS_smtp_to, "SMTP.to"}};
1239 if (!parse<RFC5321::path_or_postmaster, RFC5321::action>(smtp_to_in,
1240 smtp_to_mbx)) {
1241 LOG(FATAL) << "bad RCPT TO: address syntax <" << FLAGS_smtp_to << ">";
1243 LOG(INFO) << " smtp_to_mbx == " << smtp_to_mbx;
1245 auto local_smtp_to{
1246 memory_input<>{smtp_to_mbx.local_part(), "SMTP.to.local"}};
1247 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_smtp_to);
1249 else {
1250 smtp_to_mbx = to_mbx;
1253 auto smtp_to2_mbx{Mailbox{}};
1254 if (!FLAGS_smtp_to2.empty()) {
1255 auto smtp_to2_in{memory_input<>{FLAGS_smtp_to2, "SMTP.to"}};
1256 if (!parse<RFC5321::path_or_postmaster, RFC5321::action>(smtp_to2_in,
1257 smtp_to2_mbx)) {
1258 LOG(FATAL) << "bad RCPT TO: address syntax <" << FLAGS_smtp_to2 << ">";
1260 LOG(INFO) << " smtp_to2_mbx == " << smtp_to2_mbx;
1262 auto local_smtp_to2{
1263 memory_input<>{smtp_to2_mbx.local_part(), "SMTP.to.local"}};
1264 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_smtp_to2);
1267 auto smtp_to3_mbx{Mailbox{}};
1268 if (!FLAGS_smtp_to3.empty()) {
1269 auto smtp_to3_in{memory_input<>{FLAGS_smtp_to3, "SMTP.to"}};
1270 if (!parse<RFC5321::path_or_postmaster, RFC5321::action>(smtp_to3_in,
1271 smtp_to3_mbx)) {
1272 LOG(FATAL) << "bad RCPT TO: address syntax <" << FLAGS_smtp_to3 << ">";
1274 LOG(INFO) << " smtp_to3_mbx == " << smtp_to3_mbx;
1276 auto local_smtp_to3{
1277 memory_input<>{smtp_to3_mbx.local_part(), "SMTP.to.local"}};
1278 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_smtp_to3);
1281 return std::tuple(from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx, smtp_to2_mbx,
1282 smtp_to3_mbx);
1285 auto create_eml(Domain const& sender,
1286 std::string const& from,
1287 std::string const& to,
1288 std::vector<content> const& bodies,
1289 bool ext_smtputf8)
1291 auto eml{Eml{}};
1292 auto const date{Now{}};
1293 auto const pill{Pill{}};
1295 auto mid_str = fmt::format("<{}.{}@{}>", date.sec(), pill.as_string_view(),
1296 sender.ascii());
1297 eml.add_hdr("Message-ID", mid_str.c_str());
1298 eml.add_hdr("Date", date.c_str());
1300 if (!FLAGS_from_name.empty())
1301 eml.add_hdr("From", fmt::format("{} <{}>", FLAGS_from_name, from));
1302 else
1303 eml.add_hdr("From", from);
1305 eml.add_hdr("Subject", FLAGS_subject);
1307 if (!FLAGS_to_name.empty())
1308 eml.add_hdr("To", fmt::format("{} <{}>", FLAGS_to_name, to));
1309 else
1310 eml.add_hdr("To", to);
1312 if (!FLAGS_keywords.empty())
1313 eml.add_hdr("Keywords", FLAGS_keywords);
1315 if (!FLAGS_references.empty())
1316 eml.add_hdr("References", FLAGS_references);
1318 if (!FLAGS_in_reply_to.empty())
1319 eml.add_hdr("In-Reply-To", FLAGS_in_reply_to);
1321 if (!FLAGS_reply_to.empty())
1322 eml.add_hdr("Reply-To", FLAGS_reply_to);
1324 if (!FLAGS_reply_2.empty())
1325 eml.add_hdr("Reply-To", FLAGS_reply_2);
1327 eml.add_hdr("MIME-Version", "1.0");
1328 eml.add_hdr("Content-Language", "en-US");
1330 auto magic{Magic{}}; // to ID buffer contents
1332 if (!FLAGS_content_type.empty()) {
1333 eml.add_hdr("Content-Type", FLAGS_content_type);
1335 else {
1336 eml.add_hdr("Content-Type", magic.buffer(bodies[0]));
1339 if (!FLAGS_content_transfer_encoding.empty()) {
1340 eml.add_hdr("Content-Transfer-Encoding", FLAGS_content_transfer_encoding);
1343 return eml;
1346 void sign_eml(Eml& eml,
1347 std::string const& from_dom,
1348 std::vector<content> const& bodies)
1350 auto const body_type = (bodies[0].type() == data_type::binary)
1351 ? OpenDKIM::sign::body_type::binary
1352 : OpenDKIM::sign::body_type::text;
1354 auto const key_file = FLAGS_dkim_key_file.empty()
1355 ? (FLAGS_selector + ".private")
1356 : FLAGS_dkim_key_file;
1357 std::ifstream keyfs(key_file.c_str());
1358 CHECK(keyfs.good()) << "can't access " << key_file;
1359 std::string key(std::istreambuf_iterator<char>{keyfs}, {});
1360 OpenDKIM::sign dks(key.c_str(), FLAGS_selector.c_str(), from_dom.c_str(),
1361 body_type);
1362 eml.foreach_hdr([&dks](std::string const& name, std::string const& value) {
1363 auto const header = name + ": "s + value;
1364 dks.header(header.c_str());
1366 dks.eoh();
1367 for (auto const& body : bodies) {
1368 dks.body(body);
1370 dks.eom();
1371 eml.add_hdr("DKIM-Signature"s, dks.getsighdr());
1374 template <typename Input>
1375 void do_auth(Input& in, RFC5321::Connection& cnn)
1377 if (FLAGS_username.empty() && FLAGS_password.empty())
1378 return;
1380 auto const auth = cnn.ehlo_params.find("AUTH");
1381 if (auth == end(cnn.ehlo_params)) {
1382 LOG(ERROR) << "server doesn't support AUTH";
1383 fail(in, cnn);
1386 // Perfer PLAIN mechanism.
1387 if (std::find(begin(auth->second), end(auth->second), "PLAIN") !=
1388 end(auth->second)) {
1389 LOG(INFO) << "C: AUTH PLAIN";
1390 auto const tok = fmt::format("\0{}\0{}", FLAGS_username, FLAGS_password);
1391 cnn.sock.out() << "AUTH PLAIN " << Base64::enc(tok) << "\r\n" << std::flush;
1392 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1393 if (cnn.reply_code != "235") {
1394 LOG(ERROR) << "AUTH PLAIN returned " << cnn.reply_code;
1395 fail(in, cnn);
1398 // The LOGIN SASL mechanism is obsolete.
1399 else if (std::find(begin(auth->second), end(auth->second), "LOGIN") !=
1400 end(auth->second)) {
1401 LOG(INFO) << "C: AUTH LOGIN";
1402 cnn.sock.out() << "AUTH LOGIN\r\n" << std::flush;
1403 CHECK((parse<RFC5321::auth_login_username>(in)));
1404 cnn.sock.out() << Base64::enc(FLAGS_username) << "\r\n" << std::flush;
1405 CHECK((parse<RFC5321::auth_login_password>(in)));
1406 cnn.sock.out() << Base64::enc(FLAGS_password) << "\r\n" << std::flush;
1407 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1408 if (cnn.reply_code != "235") {
1409 LOG(ERROR) << "AUTH LOGIN returned " << cnn.reply_code;
1410 fail(in, cnn);
1413 else {
1414 LOG(ERROR) << "server doesn't support AUTH methods PLAIN or LOGIN";
1415 fail(in, cnn);
1419 // Do various bad things during the DATA transfer.
1421 template <typename Input>
1422 void bad_daddy(Input& in, RFC5321::Connection& cnn)
1424 LOG(INFO) << "C: DATA";
1425 cnn.sock.out() << "DATA\r\n";
1426 cnn.sock.out() << std::flush;
1428 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1429 if (cnn.reply_code != "354") {
1430 LOG(ERROR) << "DATA returned " << cnn.reply_code;
1431 fail(in, cnn);
1434 // cnn.sock.out() << "\r\nThis ->\n<- is a bare LF!\r\n";
1435 if (FLAGS_bare_lf)
1436 cnn.sock.out() << "\n.\n\r\n";
1438 if (FLAGS_to_the_neck) {
1439 for (;;) {
1440 cnn.sock.out() << "####################"
1441 "####################"
1442 "####################"
1443 << std::flush;
1447 if (FLAGS_long_line) {
1448 for (auto i{0}; i < 10000; ++i) {
1449 cnn.sock.out() << 'X';
1451 cnn.sock.out() << "\r\n" << std::flush;
1454 while (FLAGS_slow_strangle) {
1455 for (auto i{0}; i < 500; ++i) {
1456 cnn.sock.out() << 'X' << std::flush;
1457 sleep(1);
1459 cnn.sock.out() << "\r\n";
1462 // Done!
1463 cnn.sock.out() << ".\r\n" << std::flush;
1464 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1466 LOG(INFO) << "reply_code == " << cnn.reply_code;
1467 CHECK_EQ(cnn.reply_code.at(0), '2');
1469 LOG(INFO) << "C: QUIT";
1470 cnn.sock.out() << "QUIT\r\n" << std::flush;
1471 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1474 bool snd(fs::path config_path,
1475 int fd_in,
1476 int fd_out,
1477 Domain const& sender,
1478 Domain const& receiver,
1479 DNS::RR_collection const& tlsa_rrs,
1480 bool enforce_dane,
1481 Mailbox const& from_mbx,
1482 Mailbox const& to_mbx,
1483 Mailbox const& smtp_from_mbx,
1484 Mailbox const& smtp_to_mbx,
1485 Mailbox const& smtp_to2_mbx,
1486 Mailbox const& smtp_to3_mbx,
1487 std::vector<content> const& bodies)
1489 auto constexpr read_hook{[]() {}};
1491 auto cnn{RFC5321::Connection(fd_in, fd_out, read_hook)};
1493 auto in{
1494 istream_input<eol::crlf, 1>{cnn.sock.in(), FLAGS_bfr_size, "session"}};
1495 if (!parse<RFC5321::greeting, RFC5321::action>(in, cnn)) {
1496 LOG(WARNING) << "can't parse greeting";
1498 LOG(WARNING) << " us: " << cnn.sock.us_c_str();
1499 LOG(WARNING) << "them: " << cnn.sock.them_c_str();
1501 cnn.sock.log_stats();
1502 return false;
1505 if (!cnn.greeting_ok) {
1506 LOG(WARNING) << "greeting was not in the affirmative, skipping";
1507 return false;
1510 // try EHLO/HELO
1512 if (FLAGS_use_esmtp) {
1513 LOG(INFO) << "C: EHLO " << FLAGS_client_id;
1515 if (FLAGS_slow_strangle) {
1516 auto ehlo_str = fmt::format("EHLO {}\r\n", FLAGS_client_id);
1517 for (auto ch : ehlo_str) {
1518 cnn.sock.out() << ch << std::flush;
1519 sleep(1);
1522 else {
1523 cnn.sock.out() << "EHLO " << FLAGS_client_id << "\r\n" << std::flush;
1526 CHECK((parse<RFC5321::ehlo_rsp, RFC5321::action>(in, cnn)));
1527 if (!cnn.ehlo_ok) {
1529 if (FLAGS_force_smtputf8) {
1530 LOG(WARNING) << "ehlo response was not in the affirmative, skipping";
1531 return false;
1534 LOG(WARNING) << "ehlo response was not in the affirmative, trying HELO";
1535 FLAGS_use_esmtp = false;
1539 if (!FLAGS_use_esmtp) {
1540 LOG(INFO) << "C: HELO " << sender.ascii();
1541 cnn.sock.out() << "HELO " << sender.ascii() << "\r\n" << std::flush;
1542 if (!parse<RFC5321::helo_ok_rsp, RFC5321::action>(in, cnn)) {
1543 LOG(WARNING) << "HELO didn't work, skipping";
1544 return false;
1548 // Check extensions
1550 auto bad_dad = FLAGS_bare_lf || FLAGS_slow_strangle || FLAGS_to_the_neck;
1552 if (bad_dad) {
1553 FLAGS_use_chunking = false;
1554 FLAGS_use_size = false;
1557 auto const ext_8bitmime{FLAGS_use_8bitmime && cnn.has_extension("8BITMIME")};
1559 auto const ext_chunking{FLAGS_use_chunking && cnn.has_extension("CHUNKING")};
1561 auto const ext_binarymime{FLAGS_use_binarymime && ext_chunking &&
1562 cnn.has_extension("BINARYMIME")};
1564 auto const ext_deliverby{FLAGS_use_deliverby &&
1565 cnn.has_extension("DELIVERBY")};
1567 auto const ext_pipelining{FLAGS_use_pipelining &&
1568 cnn.has_extension("PIPELINING")};
1570 auto const ext_prdr{FLAGS_use_prdr && cnn.has_extension("PRDR")};
1572 auto const ext_size{FLAGS_use_size && cnn.has_extension("SIZE")};
1574 auto const ext_smtputf8{FLAGS_use_smtputf8 && cnn.has_extension("SMTPUTF8")};
1576 auto const ext_starttls{FLAGS_use_tls && cnn.has_extension("STARTTLS")};
1578 if (FLAGS_force_smtputf8 && !ext_smtputf8) {
1579 LOG(WARNING) << "no SMTPUTF8, skipping";
1580 return false;
1583 if (ext_starttls) {
1584 LOG(INFO) << "C: STARTTLS";
1585 cnn.sock.out() << "STARTTLS\r\n" << std::flush;
1586 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1588 LOG(INFO) << "cnn.sock.starttls_client(\"" << receiver.ascii() << "\");";
1589 cnn.sock.starttls_client(config_path, sender.ascii().c_str(),
1590 receiver.ascii().c_str(), tlsa_rrs, enforce_dane);
1592 LOG(INFO) << "TLS: " << cnn.sock.tls_info();
1594 LOG(INFO) << "C: EHLO " << FLAGS_client_id;
1595 cnn.sock.out() << "EHLO " << FLAGS_client_id << "\r\n" << std::flush;
1596 CHECK((parse<RFC5321::ehlo_rsp, RFC5321::action>(in, cnn)));
1598 else if (FLAGS_require_tls) {
1599 LOG(ERROR) << "No TLS extension, won't send mail in plain text without "
1600 "--require_tls=false.";
1601 LOG(INFO) << "C: QUIT";
1602 cnn.sock.out() << "QUIT\r\n" << std::flush;
1603 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1604 exit(EXIT_FAILURE);
1607 if (FLAGS_noop) {
1608 LOG(INFO) << "C: NOOP";
1609 cnn.sock.out() << "NOOP\r\n" << std::flush;
1612 if (receiver != cnn.server_id) {
1613 LOG(INFO) << "server identifies as " << cnn.server_id;
1616 if (FLAGS_force_smtputf8 && !ext_smtputf8) {
1617 LOG(WARNING) << "does not support SMTPUTF8";
1618 return false;
1621 if (ext_smtputf8 && !ext_8bitmime) {
1622 LOG(ERROR)
1623 << "SMTPUTF8 requires 8BITMIME, see RFC-6531 section 3.1 item 8.";
1624 LOG(INFO) << "C: QUIT";
1625 cnn.sock.out() << "QUIT\r\n" << std::flush;
1626 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1627 exit(EXIT_FAILURE);
1630 auto max_msg_size{0u};
1631 if (ext_size) {
1632 if (!cnn.ehlo_params["SIZE"].empty()) {
1633 char* ep = nullptr;
1634 max_msg_size = strtoul(cnn.ehlo_params["SIZE"][0].c_str(), &ep, 10);
1635 if (ep && (*ep != '\0')) {
1636 LOG(WARNING) << "garbage in SIZE argument: "
1637 << cnn.ehlo_params["SIZE"][0];
1642 auto deliver_by_min{0u};
1643 if (ext_deliverby) {
1644 if (!cnn.ehlo_params["DELIVERBY"].empty()) {
1645 char* ep = nullptr;
1646 deliver_by_min =
1647 strtoul(cnn.ehlo_params["DELIVERBY"][0].c_str(), &ep, 10);
1648 if (ep && (*ep != '\0')) {
1649 LOG(WARNING) << "garbage in DELIVERBY argument: "
1650 << cnn.ehlo_params["DELIVERBY"][0];
1654 if (deliver_by_min) {
1655 LOG(INFO) << "DELIVERBY " << deliver_by_min;
1657 do_auth(in, cnn);
1659 in.discard();
1661 auto enc = FLAGS_force_smtputf8 ? Mailbox::domain_encoding::utf8
1662 : Mailbox::domain_encoding::ascii;
1664 std::string from =
1665 from_mbx.empty() ? smtp_from_mbx.as_string(enc) : from_mbx.as_string(enc);
1666 std::string to =
1667 to_mbx.empty() ? smtp_to_mbx.as_string(enc) : to_mbx.as_string(enc);
1669 auto eml{create_eml(sender, from, to, bodies, ext_smtputf8)};
1671 if (FLAGS_use_dkim) {
1672 auto const dom = Mailbox(from).domain().ascii();
1673 sign_eml(eml, dom.c_str(), bodies);
1675 else if (FLAGS_bogus_dkim) {
1676 eml.add_hdr("DKIM-Signature", "");
1679 // Get the header as one big string
1680 std::ostringstream hdr_stream;
1681 hdr_stream << eml;
1682 if (!FLAGS_rawmsg)
1683 hdr_stream << "\r\n";
1684 auto const& hdr_str = hdr_stream.str();
1686 // In the case of DATA style transfer, this total_size number is an
1687 // *estimate* only, as line endings may be translated or added
1688 // during transfer. In the BDAT case, this number must be exact.
1690 auto total_size = hdr_str.size();
1691 for (auto const& body : bodies)
1692 total_size += body.size();
1694 if (ext_size && max_msg_size && (total_size > max_msg_size)) {
1695 LOG(ERROR) << "message size " << total_size << " exceeds size limit of "
1696 << max_msg_size;
1697 LOG(INFO) << "C: QUIT";
1698 cnn.sock.out() << "QUIT\r\n" << std::flush;
1699 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1700 exit(EXIT_FAILURE);
1703 std::ostringstream param_stream;
1704 if (FLAGS_huge_size && ext_size) {
1705 // Claim some huge size.
1706 param_stream << " SIZE=" << std::numeric_limits<std::streamsize>::max();
1708 else if (ext_size) {
1709 param_stream << " SIZE=" << total_size;
1712 if (ext_binarymime) {
1713 param_stream << " BODY=BINARYMIME";
1715 else if (ext_8bitmime) {
1716 param_stream << " BODY=8BITMIME";
1719 if (ext_prdr && (!smtp_to2_mbx.empty() || !smtp_to3_mbx.empty())) {
1720 param_stream << " PRDR";
1723 if (ext_deliverby) {
1724 param_stream << " BY=1200;NT";
1727 if (ext_smtputf8) {
1728 param_stream << " SMTPUTF8";
1731 if (FLAGS_badpipline) {
1732 LOG(INFO) << "C: NOOP NOOP";
1733 cnn.sock.out() << "NOOP\r\nNOOP\r\n" << std::flush;
1736 bool rcpt_to_ok = false;
1737 bool rcpt_to2_ok = false;
1738 bool rcpt_to3_ok = false;
1740 auto param_str = param_stream.str();
1742 for (auto count = 0UL; count < FLAGS_reps; ++count) {
1744 LOG(INFO) << "C: MAIL FROM:<" << smtp_from_mbx.as_string(enc) << '>'
1745 << param_str;
1746 cnn.sock.out() << "MAIL FROM:<" << smtp_from_mbx.as_string(enc) << '>'
1747 << param_str << "\r\n";
1748 if (!ext_pipelining) {
1749 quit_on_fail(in, cnn, "MAIL FROM");
1752 LOG(INFO) << "C: RCPT TO:<" << smtp_to_mbx.as_string(enc) << ">";
1753 cnn.sock.out() << "RCPT TO:<" << smtp_to_mbx.as_string(enc) << ">\r\n";
1754 if (!ext_pipelining) {
1755 // check RCPT TO #1
1756 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1757 rcpt_to_ok = cnn.reply_code.at(0) == '2';
1760 if (!smtp_to2_mbx.empty()) {
1761 LOG(INFO) << "C: RCPT TO:<" << smtp_to2_mbx.as_string(enc) << ">";
1762 cnn.sock.out() << "RCPT TO:<" << smtp_to2_mbx.as_string(enc) << ">\r\n";
1763 if (!ext_pipelining) {
1764 // check RCPT TO #2
1765 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1766 rcpt_to2_ok = cnn.reply_code.at(0) == '2';
1770 if (!smtp_to3_mbx.empty()) {
1771 LOG(INFO) << "C: RCPT TO:<" << smtp_to3_mbx.as_string(enc) << ">";
1772 cnn.sock.out() << "RCPT TO:<" << smtp_to3_mbx.as_string(enc) << ">\r\n";
1773 if (!ext_pipelining) {
1774 // check RCPT TO #3
1775 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1776 rcpt_to3_ok = cnn.reply_code.at(0) == '2';
1780 if (FLAGS_nosend) {
1781 if (ext_pipelining) {
1782 quit_on_fail(in, cnn, "MAIL FROM");
1783 if (rcpt_to_ok)
1784 quit_on_fail(in, cnn, "RCPT TO");
1785 if (rcpt_to2_ok)
1786 quit_on_fail(in, cnn, "RCPT TO");
1787 if (rcpt_to3_ok)
1788 quit_on_fail(in, cnn, "RCPT TO");
1790 LOG(INFO) << "C: QUIT";
1791 cnn.sock.out() << "QUIT\r\n" << std::flush;
1792 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1793 LOG(INFO) << "no-sending";
1794 exit(EXIT_SUCCESS);
1797 if (bad_dad) {
1798 if (ext_pipelining) {
1799 cnn.sock.out() << std::flush;
1800 quit_on_fail(in, cnn, "MAIL FROM");
1801 quit_on_fail(in, cnn, "RCPT TO");
1803 bad_daddy(in, cnn);
1804 return true;
1807 auto msg = std::make_unique<MessageStore>();
1809 // if service is smtp (i.e. sending real mail, not smtp-test)
1810 try {
1811 if (FLAGS_save) {
1812 msg->open(sender.ascii(), total_size * 2, ".Sent");
1813 msg->write(hdr_str.data(), hdr_str.size());
1814 for (auto const& body : bodies) {
1815 msg->write(body.data(), body.size());
1819 catch (std::system_error const& e) {
1820 switch (errno) {
1821 case ENOSPC:
1822 msg->trash();
1823 msg.reset();
1824 LOG(FATAL) << "no space";
1825 [[fallthrough]];
1827 default:
1828 msg->trash();
1829 msg.reset();
1830 LOG(ERROR) << "errno==" << errno << ": " << strerror(errno);
1831 LOG(FATAL) << e.what();
1834 catch (std::exception const& e) {
1835 msg->trash();
1836 msg.reset();
1837 LOG(FATAL) << e.what();
1840 if (ext_chunking) {
1842 if (ext_pipelining) {
1843 quit_on_fail(in, cnn, "MAIL FROM");
1845 // check RCPT TO #1
1846 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1847 rcpt_to_ok = cnn.reply_code.at(0) == '2';
1849 if (!smtp_to2_mbx.empty()) {
1850 // check RCPT TO #2
1851 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1852 rcpt_to2_ok = cnn.reply_code.at(0) == '2';
1855 if (!smtp_to3_mbx.empty()) {
1856 // check RCPT TO #3
1857 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1858 rcpt_to3_ok = cnn.reply_code.at(0) == '2';
1861 // If all RCPT TOs failed, we give up.
1862 if (!(rcpt_to_ok || rcpt_to2_ok || rcpt_to3_ok)) {
1863 fail(in, cnn);
1867 std::ostringstream bdat_stream;
1868 bdat_stream << "BDAT " << total_size << " LAST";
1869 LOG(INFO) << "C: " << bdat_stream.str();
1871 cnn.sock.out() << bdat_stream.str() << "\r\n";
1872 cnn.sock.out().write(hdr_str.data(), hdr_str.size());
1873 CHECK(cnn.sock.out().good());
1875 for (auto const& body : bodies) {
1876 cnn.sock.out().write(body.data(), body.size());
1877 CHECK(cnn.sock.out().good());
1880 // Done sending data
1881 if (FLAGS_pipeline_quit) {
1882 LOG(INFO) << "C: QUIT";
1883 cnn.sock.out() << "QUIT\r\n" << std::flush;
1885 else {
1886 cnn.sock.out() << std::flush;
1889 // Not CHUNKING
1890 else {
1891 LOG(INFO) << "C: DATA";
1892 cnn.sock.out() << "DATA\r\n";
1894 // Now check returns, after DATA
1895 if (ext_pipelining) {
1896 quit_on_fail(in, cnn, "MAIL FROM");
1898 // check RCPT TO #1
1899 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1900 rcpt_to_ok = cnn.reply_code.at(0) == '2';
1902 if (!smtp_to2_mbx.empty()) {
1903 // check RCPT TO #2
1904 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1905 rcpt_to2_ok = cnn.reply_code.at(0) == '2';
1908 if (!smtp_to3_mbx.empty()) {
1909 // check RCPT TO #3
1910 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1911 rcpt_to3_ok = cnn.reply_code.at(0) == '2';
1914 // If all RCPT TOs failed, we give up.
1915 if (!(rcpt_to_ok || rcpt_to2_ok || rcpt_to3_ok)) {
1916 fail(in, cnn);
1919 else {
1920 cnn.sock.out() << std::flush;
1923 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1924 if (cnn.reply_code != "354") {
1925 LOG(ERROR) << "DATA returned " << cnn.reply_code;
1926 fail(in, cnn);
1929 cnn.sock.out() << eml;
1930 if (!FLAGS_rawmsg)
1931 cnn.sock.out() << "\r\n";
1933 for (auto const& body : bodies) {
1934 auto lineno = 0;
1935 auto line{std::string{}};
1936 auto isbody{imemstream{body.data(), body.size()}};
1937 while (std::getline(isbody, line)) {
1938 ++lineno;
1939 if (!cnn.sock.out().good()) {
1940 cnn.sock.log_stats();
1941 LOG(FATAL) << "output no good at line " << lineno;
1943 if (FLAGS_rawdog) {
1944 // This adds a final newline at the end of the file, if no
1945 // line ending was present.
1946 cnn.sock.out() << line << '\n';
1948 else {
1949 // This code converts single LF line endings into CRLF.
1950 // This code does nothing to fix single CR characters not
1951 // part of a CRLF pair.
1953 // This loop adds a CRLF and the end of the transmission if
1954 // the file doesn't already end with one. This is a
1955 // requirement of the SMTP DATA protocol.
1957 if (line.length() && (line.at(0) == '.')) {
1958 cnn.sock.out() << '.';
1960 cnn.sock.out() << line;
1961 if (line.length() && line.back() != '\r')
1962 cnn.sock.out() << '\r';
1963 cnn.sock.out() << '\n';
1967 CHECK(cnn.sock.out().good());
1969 // Done sending data
1970 if (FLAGS_pipeline_quit) {
1971 LOG(INFO) << "C: QUIT";
1972 cnn.sock.out() << ".\r\nQUIT\r\n" << std::flush;
1974 else {
1975 cnn.sock.out() << ".\r\n" << std::flush;
1979 auto success{false};
1980 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1982 // Now, either DATA or BDATs lets check replies
1983 if (ext_prdr) {
1984 if (cnn.reply_code == "353") {
1985 if (rcpt_to_ok) {
1986 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1987 success = cnn.reply_code.length() && cnn.reply_code.at(0) == '2';
1989 if (rcpt_to2_ok) {
1990 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1991 success = success &&
1992 (cnn.reply_code.length() && cnn.reply_code.at(0) == '2');
1994 if (rcpt_to3_ok) {
1995 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1996 success = success &&
1997 (cnn.reply_code.length() && cnn.reply_code.at(0) == '2');
2000 else {
2001 // last (and useless?) response.
2002 LOG(INFO) << "DATA returned " << cnn.reply_code;
2003 success = cnn.reply_code.length() && cnn.reply_code.at(0) == '2';
2006 else {
2007 LOG(INFO) << "DATA returned " << cnn.reply_code;
2008 success = cnn.reply_code.length() && cnn.reply_code.at(0) == '2';
2011 if (success) {
2012 if (FLAGS_save) {
2013 msg->deliver();
2015 else {
2016 msg->trash();
2018 LOG(INFO) << "all mail was sent successfully";
2020 else {
2021 LOG(INFO) << "some mail was *NOT* sent successfully";
2024 in.discard();
2025 } // FLAGS_reps
2027 if (!FLAGS_pipeline_quit) {
2028 LOG(INFO) << "C: QUIT";
2029 cnn.sock.out() << "QUIT\r\n" << std::flush;
2031 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
2033 return true;
2036 DNS::RR_collection
2037 get_tlsa_rrs(DNS::Resolver& res, Domain const& domain, uint16_t port)
2039 auto const tlsa = fmt::format("_{}._tcp.{}", port, domain.ascii());
2041 DNS::Query q(res, DNS::RR_type::TLSA, tlsa);
2043 if (q.nx_domain()) {
2044 LOG(INFO) << "TLSA data not found for " << domain << ':' << port;
2047 if (q.bogus_or_indeterminate()) {
2048 LOG(WARNING) << "TLSA data is bogus or indeterminate";
2051 if (q.authentic_data()) {
2052 LOG(INFO) << "### TLSA records authentic for domain " << domain << " ###";
2054 else {
2055 LOG(INFO) << "TLSA records can't be authenticated for domain " << domain;
2058 auto tlsa_rrs = q.get_records();
2059 if (!tlsa_rrs.empty()) {
2060 LOG(INFO) << "### TLSA data found for " << domain << ':' << port << " ###";
2063 return tlsa_rrs;
2065 } // namespace
2067 int main(int argc, char* argv[])
2069 std::ios::sync_with_stdio(false);
2071 { // Need to work with either namespace.
2072 using namespace gflags;
2073 using namespace google;
2074 ParseCommandLineFlags(&argc, &argv, true);
2077 auto const config_path = osutil::get_config_dir();
2079 auto sender = get_sender();
2081 if (FLAGS_selftest) {
2082 selftest();
2083 return 0;
2086 auto bodies{std::vector<content>{}};
2087 for (int a = 1; a < argc; ++a) {
2088 if (!fs::exists(argv[a]))
2089 LOG(FATAL) << "can't find mail body part " << argv[a];
2090 bodies.push_back(argv[a]);
2093 if (argc == 1)
2094 bodies.push_back("body.txt");
2096 CHECK_EQ(bodies.size(), 1) << "only one body part for now";
2097 CHECK(!(FLAGS_4 && FLAGS_6)) << "must use /some/ IP version";
2099 if (FLAGS_force_smtputf8)
2100 FLAGS_use_smtputf8 = true;
2102 auto&& [from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx, smtp_to2_mbx,
2103 smtp_to3_mbx] = parse_mailboxes();
2105 if (to_mbx.domain().empty() && FLAGS_mx_host.empty()) {
2106 LOG(ERROR) << "don't know who to send this mail to";
2107 return 0;
2110 if (!smtp_to2_mbx.domain().empty() &&
2111 smtp_to2_mbx.domain() != smtp_to_mbx.domain()) {
2112 LOG(ERROR) << "can't send to both " << smtp_to_mbx.domain() << " and "
2113 << smtp_to2_mbx.domain();
2114 return 0;
2117 if (!smtp_to3_mbx.domain().empty() &&
2118 smtp_to3_mbx.domain() != smtp_to_mbx.domain()) {
2119 LOG(ERROR) << "can't send to both " << smtp_to_mbx.domain() << " and "
2120 << smtp_to3_mbx.domain();
2121 return 0;
2124 auto const port{osutil::get_port(FLAGS_service.c_str(), "tcp")};
2126 auto res{DNS::Resolver{config_path}};
2127 auto tlsa_rrs{get_tlsa_rrs(res, to_mbx.domain(), port)};
2129 if (FLAGS_pipe) {
2130 return snd(config_path, STDIN_FILENO, STDOUT_FILENO, sender,
2131 to_mbx.domain(), tlsa_rrs, false, from_mbx, to_mbx,
2132 smtp_from_mbx, smtp_to_mbx, smtp_to2_mbx, smtp_to3_mbx, bodies)
2133 ? EXIT_SUCCESS
2134 : EXIT_FAILURE;
2137 bool enforce_dane = true;
2138 auto const receivers = get_receivers(res, to_mbx, enforce_dane);
2140 if (receivers.empty()) {
2141 LOG(INFO) << "no place to send this mail";
2142 return EXIT_SUCCESS;
2145 for (auto const& receiver : receivers) {
2146 LOG(INFO) << "trying " << receiver << ":" << FLAGS_service;
2148 if (FLAGS_noconn) {
2149 LOG(INFO) << "skipping";
2150 continue;
2153 auto fd = conn(res, receiver, port);
2154 if (fd == -1) {
2155 LOG(WARNING) << "no connection, skipping";
2156 continue;
2159 // Get our local IP address as "us".
2161 sa::sockaddrs us_addr{};
2162 socklen_t us_addr_len{sizeof us_addr};
2163 char us_addr_str[INET6_ADDRSTRLEN]{'\0'};
2164 std::vector<std::string> fcrdns;
2165 bool private_addr = false;
2167 if (-1 == getsockname(fd, &us_addr.addr, &us_addr_len)) {
2168 // Ignore ENOTSOCK errors from getsockname, useful for testing.
2169 PLOG_IF(WARNING, ENOTSOCK != errno) << "getsockname failed";
2171 else {
2172 switch (us_addr_len) {
2173 case sizeof(sockaddr_in):
2174 PCHECK(inet_ntop(AF_INET, &us_addr.addr_in.sin_addr, us_addr_str,
2175 sizeof us_addr_str) != nullptr);
2176 if (IP4::is_private(us_addr_str))
2177 private_addr = true;
2178 else
2179 fcrdns = DNS::fcrdns4(res, us_addr_str);
2180 break;
2182 case sizeof(sockaddr_in6):
2183 PCHECK(inet_ntop(AF_INET6, &us_addr.addr_in6.sin6_addr, us_addr_str,
2184 sizeof us_addr_str) != nullptr);
2185 if (IP6::is_private(us_addr_str))
2186 private_addr = true;
2187 else
2188 fcrdns = DNS::fcrdns6(res, us_addr_str);
2189 break;
2191 default:
2192 LOG(FATAL) << "bogus address length (" << us_addr_len
2193 << ") returned from getsockname";
2197 if (fcrdns.size()) {
2198 LOG(INFO) << "our names are:";
2199 for (auto const& fc : fcrdns) {
2200 LOG(INFO) << " " << fc;
2204 if (!private_addr) {
2205 // look at from_mbx.domain() and get SPF records
2207 // also look at our ID to get SPF records.
2210 if (to_mbx.domain() == receiver) {
2211 if (snd(config_path, fd, fd, sender, receiver, tlsa_rrs, enforce_dane,
2212 from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx, smtp_to2_mbx,
2213 smtp_to3_mbx, bodies)) {
2214 return EXIT_SUCCESS;
2217 else {
2218 auto tlsa_rrs_mx{get_tlsa_rrs(res, receiver, port)};
2219 tlsa_rrs_mx.insert(end(tlsa_rrs_mx), begin(tlsa_rrs), end(tlsa_rrs));
2220 if (snd(config_path, fd, fd, sender, receiver, tlsa_rrs_mx, enforce_dane,
2221 from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx, smtp_to2_mbx,
2222 smtp_to3_mbx, bodies)) {
2223 return EXIT_SUCCESS;
2227 close(fd);
2230 LOG(ERROR) << "we ran out of hosts to try";