ignore "Anonymous query through public resolver" errors
[ghsmtp.git] / snd.cpp
blobb30fba7339d92ad2a1bb102fdf6b2afb1615d54b
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_size, true, "use SIZE extension");
42 DEFINE_bool(use_smtputf8, true, "use SMTPUTF8 extension");
43 DEFINE_bool(use_tls, true, "use STARTTLS extension");
45 // To force it, set if you have UTF8 in the local part of any RFC5321
46 // address.
47 DEFINE_bool(force_smtputf8, false, "force SMTPUTF8 extension");
49 DEFINE_string(sender, "", "FQDN of sending node");
51 DEFINE_string(local_address, "", "local address to bind");
52 DEFINE_string(mx_host, "", "FQDN of receiving node");
53 DEFINE_string(service, "smtp-test", "service name");
54 DEFINE_string(client_id, "", "client name (ID) for EHLO/HELO");
56 DEFINE_string(from, "", "RFC5322 From: address");
57 DEFINE_string(from_name, "", "RFC5322 From: name");
59 DEFINE_string(to, "", "RFC5322 To: address");
60 DEFINE_string(to_name, "", "RFC5322 To: name");
62 DEFINE_string(smtp_from, "", "RFC5321 MAIL FROM address");
63 DEFINE_string(smtp_to, "", "RFC5321 RCPT TO address");
64 DEFINE_string(smtp_to2, "", "second RFC5321 RCPT TO address");
66 DEFINE_string(content_type, "", "RFC5322 Content-Type");
67 DEFINE_string(content_transfer_encoding,
68 "",
69 "RFC5322 Content-Transfer-Encoding");
71 DEFINE_string(subject, "testing one, two, three...", "RFC5322 Subject");
72 DEFINE_string(keywords, "", "RFC5322 Keywords: header");
73 DEFINE_string(references, "", "RFC5322 References: header");
74 DEFINE_string(in_reply_to, "", "RFC5322 In-Reply-To: header");
75 DEFINE_string(reply_to, "", "RFC5322 Reply-To: header");
76 DEFINE_string(reply_2, "", "Second RFC5322 Reply-To: header");
78 DEFINE_bool(4, false, "use only IP version 4");
79 DEFINE_bool(6, false, "use only IP version 6");
81 DEFINE_string(username, "", "AUTH username");
82 DEFINE_string(password, "", "AUTH password");
84 DEFINE_bool(use_dkim, true, "sign with DKIM");
85 DEFINE_bool(bogus_dkim, false, "sign with bogus DKIM");
86 DEFINE_string(selector, "ghsmtp", "DKIM selector");
87 DEFINE_string(dkim_key_file, "", "DKIM key file");
89 #include "Base64.hpp"
90 #include "DNS-fcrdns.hpp"
91 #include "DNS.hpp"
92 #include "Domain.hpp"
93 #include "IP4.hpp"
94 #include "IP6.hpp"
95 #include "Magic.hpp"
96 #include "Mailbox.hpp"
97 #include "MessageStore.hpp"
98 #include "Now.hpp"
99 #include "OpenDKIM.hpp"
100 #include "Pill.hpp"
101 #include "Sock.hpp"
102 #include "fs.hpp"
103 #include "imemstream.hpp"
104 #include "osutil.hpp"
105 #include "sa.hpp"
107 #include <algorithm>
108 #include <fstream>
109 #include <functional>
110 #include <iomanip>
111 #include <iostream>
112 #include <iterator>
113 #include <random>
114 #include <string>
115 #include <string_view>
116 #include <unordered_map>
118 #include <netdb.h>
119 #include <sys/socket.h>
120 #include <sys/types.h>
122 #include <fmt/format.h>
123 #include <fmt/ostream.h>
125 #include <boost/algorithm/string/case_conv.hpp>
127 #include <boost/iostreams/device/mapped_file.hpp>
129 #include <tao/pegtl.hpp>
130 #include <tao/pegtl/contrib/abnf.hpp>
132 using namespace tao::pegtl;
133 using namespace tao::pegtl::abnf;
135 using namespace std::string_literals;
137 namespace Config {
138 constexpr auto read_timeout = std::chrono::minutes(24 * 60);
139 constexpr auto write_timeout = std::chrono::minutes(24 * 60);
140 } // namespace Config
142 // clang-format off
144 namespace chars {
145 struct tail : range<'\x80', '\xBF'> {};
147 struct ch_1 : range<'\x00', '\x7F'> {};
149 struct ch_2 : seq<range<'\xC2', '\xDF'>, tail> {};
151 struct ch_3 : sor<seq<one<'\xE0'>, range<'\xA0', '\xBF'>, tail>,
152 seq<range<'\xE1', '\xEC'>, rep<2, tail>>,
153 seq<one<'\xED'>, range<'\x80', '\x9F'>, tail>,
154 seq<range<'\xEE', '\xEF'>, rep<2, tail>>> {};
156 struct ch_4 : sor<seq<one<'\xF0'>, range<'\x90', '\xBF'>, rep<2, tail>>,
157 seq<range<'\xF1', '\xF3'>, rep<3, tail>>,
158 seq<one<'\xF4'>, range<'\x80', '\x8F'>, rep<2, tail>>> {};
160 struct u8char : sor<ch_1, ch_2, ch_3, ch_4> {};
162 struct non_ascii : sor<ch_2, ch_3, ch_4> {};
164 struct ascii_only : seq<star<ch_1>, eof> {};
166 struct utf8_only : seq<star<u8char>, eof> {};
169 namespace RFC5322 {
171 struct VUCHAR : sor<VCHAR, chars::non_ascii> {};
173 using dot = one<'.'>;
174 using colon = one<':'>;
176 // All 7-bit ASCII except NUL (0), LF (10) and CR (13).
177 struct text_ascii : ranges<1, 9, 11, 12, 14, 127> {};
179 // Short lines of ASCII text. LF or CRLF line separators.
180 struct body_ascii : seq<star<seq<rep_max<998, text_ascii>, eol>>,
181 opt<rep_max<998, text_ascii>>, eof> {};
183 struct text_utf8 : sor<text_ascii, chars::non_ascii> {};
185 // Short lines of UTF-8 text. LF or CRLF line separators.
186 struct body_utf8 : seq<star<seq<rep_max<998, text_utf8>, eol>>,
187 opt<rep_max<998, text_utf8>>, eof> {};
189 struct FWS : seq<opt<seq<star<WSP>, eol>>, plus<WSP>> {};
191 struct qtext : sor<one<33>, ranges<35, 91, 93, 126>, chars::non_ascii> {};
193 struct quoted_pair : seq<one<'\\'>, sor<VUCHAR, WSP>> {};
195 struct atext : sor<ALPHA, DIGIT,
196 one<'!', '#',
197 '$', '%',
198 '&', '\'',
199 '*', '+',
200 '-', '/',
201 '=', '?',
202 '^', '_',
203 '`', '{',
204 '|', '}',
205 '~'>,
206 chars::non_ascii> {};
208 // ctext is ASCII not '(' or ')' or '\\'
209 struct ctext : sor<ranges<33, 39, 42, 91, 93, 126>, chars::non_ascii> {};
211 struct comment;
213 struct ccontent : sor<ctext, quoted_pair, comment> {};
215 struct comment
216 : seq<one<'('>, star<seq<opt<FWS>, ccontent>>, opt<FWS>, one<')'>> {};
218 struct CFWS : sor<seq<plus<seq<opt<FWS>, comment>, opt<FWS>>>, FWS> {};
220 struct qcontent : sor<qtext, quoted_pair> {};
222 // Corrected in errata ID: 3135
223 struct quoted_string
224 : seq<opt<CFWS>,
225 DQUOTE,
226 sor<seq<star<seq<opt<FWS>, qcontent>>, opt<FWS>>, FWS>,
227 DQUOTE,
228 opt<CFWS>> {};
230 // *([FWS] VCHAR) *WSP
231 struct unstructured : seq<star<seq<opt<FWS>, VUCHAR>>, star<WSP>> {};
233 struct atom : seq<opt<CFWS>, plus<atext>, opt<CFWS>> {};
235 struct dot_atom_text : list<plus<atext>, dot> {};
237 struct dot_atom : seq<opt<CFWS>, dot_atom_text, opt<CFWS>> {};
239 struct word : sor<atom, quoted_string> {};
241 struct phrase : plus<word> {};
243 struct local_part : sor<dot_atom, quoted_string> {};
245 // from '!' to '~' excluding 91 92 93 '[' '\\' ']'
247 struct dtext : ranges<33, 90, 94, 126> {};
249 struct domain_literal : seq<opt<CFWS>,
250 one<'['>,
251 star<seq<opt<FWS>, dtext>>,
252 opt<FWS>,
253 one<']'>,
254 opt<CFWS>> {};
256 struct domain : sor<dot_atom, domain_literal> {};
258 struct addr_spec : seq<local_part, one<'@'>, domain> {};
260 struct postmaster : TAO_PEGTL_ISTRING("Postmaster") {};
262 struct addr_spec_or_postmaster : sor<addr_spec, postmaster> {};
264 struct addr_spec_only : seq<addr_spec_or_postmaster, eof> {};
266 struct display_name : phrase {};
268 struct display_name_only : seq<display_name, eof> {};
270 // clang-format on
272 // struct name_addr : seq<opt<display_name>, angle_addr> {};
274 // struct mailbox : sor<name_addr, addr_spec> {};
276 template <typename Rule>
277 struct inaction : nothing<Rule> {
280 template <typename Rule>
281 struct action : nothing<Rule> {
284 template <>
285 struct action<local_part> {
286 template <typename Input>
287 static void apply(Input const& in, Mailbox& mbx)
289 mbx.set_local(in.string());
293 template <>
294 struct action<domain> {
295 template <typename Input>
296 static void apply(Input const& in, Mailbox& mbx)
298 mbx.set_domain(in.string());
301 } // namespace RFC5322
303 namespace RFC5321 {
305 struct Connection {
306 Sock sock;
308 std::string server_id;
310 std::string ehlo_keyword;
311 std::vector<std::string> ehlo_param;
312 std::unordered_map<std::string, std::vector<std::string>> ehlo_params;
314 std::string reply_code;
316 bool greeting_ok{false};
317 bool ehlo_ok{false};
319 bool has_extension(char const* name) const
321 return ehlo_params.find(name) != end(ehlo_params);
324 Connection(int fd_in, int fd_out, std::function<void(void)> read_hook)
325 : sock(
326 fd_in, fd_out, read_hook, Config::read_timeout, Config::write_timeout)
331 // clang-format off
333 using dot = one<'.'>;
334 using colon = one<':'>;
335 using dash = one<'-'>;
336 using underscore = one<'_'>;
338 struct u_let_dig : sor<ALPHA, DIGIT, chars::non_ascii> {};
340 struct u_ldh_tail : star<sor<seq<plus<one<'-'>>, u_let_dig>, u_let_dig>> {};
342 struct u_label : seq<u_let_dig, u_ldh_tail> {};
344 struct let_dig : sor<ALPHA, DIGIT> {};
346 struct ldh_tail : star<sor<seq<plus<one<'-'>>, let_dig>, let_dig>> {};
348 struct ldh_str : seq<let_dig, ldh_tail> {};
350 struct label : ldh_str {};
352 struct sub_domain : sor<label, u_label> {};
354 struct domain : list<sub_domain, dot> {};
356 struct dec_octet : sor<seq<string<'2','5'>, range<'0','5'>>,
357 seq<one<'2'>, range<'0','4'>, DIGIT>,
358 seq<range<'0', '1'>, rep<2, DIGIT>>,
359 rep_min_max<1, 2, DIGIT>> {};
361 struct IPv4_address_literal
362 : seq<dec_octet, dot, dec_octet, dot, dec_octet, dot, dec_octet> {};
364 struct h16 : rep_min_max<1, 4, HEXDIG> {};
366 struct ls32 : sor<seq<h16, colon, h16>, IPv4_address_literal> {};
368 struct dcolon : two<':'> {};
370 struct IPv6address : sor<seq< rep<6, h16, colon>, ls32>,
371 seq< dcolon, rep<5, h16, colon>, ls32>,
372 seq<opt<h16 >, dcolon, rep<4, h16, colon>, ls32>,
373 seq<opt<h16, opt< colon, h16>>, dcolon, rep<3, h16, colon>, ls32>,
374 seq<opt<h16, rep_opt<2, colon, h16>>, dcolon, rep<2, h16, colon>, ls32>,
375 seq<opt<h16, rep_opt<3, colon, h16>>, dcolon, h16, colon, ls32>,
376 seq<opt<h16, rep_opt<4, colon, h16>>, dcolon, ls32>,
377 seq<opt<h16, rep_opt<5, colon, h16>>, dcolon, h16>,
378 seq<opt<h16, rep_opt<6, colon, h16>>, dcolon >> {};
380 struct IPv6_address_literal : seq<TAO_PEGTL_ISTRING("IPv6:"), IPv6address> {};
382 struct dcontent : ranges<33, 90, 94, 126> {};
384 struct standardized_tag : ldh_str {};
386 struct general_address_literal : seq<standardized_tag, colon, plus<dcontent>> {};
388 // See rfc 5321 Section 4.1.3
389 struct address_literal : seq<one<'['>,
390 sor<IPv4_address_literal,
391 IPv6_address_literal,
392 general_address_literal>,
393 one<']'>> {};
396 struct qtextSMTP : sor<ranges<32, 33, 35, 91, 93, 126>, chars::non_ascii> {};
397 struct graphic : range<32, 126> {};
398 struct quoted_pairSMTP : seq<one<'\\'>, graphic> {};
399 struct qcontentSMTP : sor<qtextSMTP, quoted_pairSMTP> {};
401 // excluded from atext: "(),.@[]"
402 struct atext : sor<ALPHA, DIGIT,
403 one<'!', '#',
404 '$', '%',
405 '&', '\'',
406 '*', '+',
407 '-', '/',
408 '=', '?',
409 '^', '_',
410 '`', '{',
411 '|', '}',
412 '~'>,
413 chars::non_ascii> {};
414 struct atom : plus<atext> {};
415 struct dot_string : list<atom, dot> {};
416 struct quoted_string : seq<one<'"'>, star<qcontentSMTP>, one<'"'>> {};
417 struct local_part : sor<dot_string, quoted_string> {};
418 struct non_local_part : sor<domain, address_literal> {};
419 struct mailbox : seq<local_part, one<'@'>, non_local_part> {};
421 struct at_domain : seq<one<'@'>, domain> {};
423 struct a_d_l : list<at_domain, one<','>> {};
425 struct path : seq<opt<seq<a_d_l, colon>>, mailbox> {};
427 struct path_only : seq<path, eof> {};
429 // textstring = 1*(%d09 / %d32-126) ; HT, SP, Printable US-ASCII
431 // Although not explicit in the grammar of RFC-6531, in practice UTF-8
432 // is used in the replies.
434 // struct textstring : plus<sor<one<9>, range<32, 126>>> {};
436 struct textstring : plus<sor<one<9>, range<32, 126>, chars::non_ascii>> {};
438 struct crap : plus<range<32, 126>> {};
440 struct server_id : sor<domain, address_literal, crap> {};
442 // Greeting = ( "220 " (Domain / address-literal) [ SP textstring ] CRLF )
443 // /
444 // ( "220-" (Domain / address-literal) [ SP textstring ] CRLF
445 // *( "220-" [ textstring ] CRLF )
446 // "220 " [ textstring ] CRLF )
448 struct greeting_ok
449 : sor<seq<TAO_PEGTL_ISTRING("220 "), server_id, opt<textstring>, CRLF>,
450 seq<TAO_PEGTL_ISTRING("220-"), server_id, opt<textstring>, CRLF,
451 star<seq<TAO_PEGTL_ISTRING("220-"), opt<textstring>, CRLF>>,
452 seq<TAO_PEGTL_ISTRING("220 "), opt<textstring>, CRLF>>> {};
454 // Reply-code = %x32-35 %x30-35 %x30-39
456 struct reply_code
457 : seq<range<0x32, 0x35>, range<0x30, 0x35>, range<0x30, 0x39>> {};
459 // Reply-line = *( Reply-code "-" [ textstring ] CRLF )
460 // Reply-code [ SP textstring ] CRLF
462 struct reply_lines
463 : seq<star<seq<reply_code, one<'-'>, opt<textstring>, CRLF>>,
464 seq<reply_code, opt<seq<SP, textstring>>, CRLF>> {};
466 struct greeting
467 : sor<greeting_ok, reply_lines> {};
469 // ehlo-greet = 1*(%d0-9 / %d11-12 / %d14-127)
470 // ; string of any characters other than CR or LF
472 struct ehlo_greet : plus<ranges<0, 9, 11, 12, 14, 127>> {};
474 // ehlo-keyword = (ALPHA / DIGIT) *(ALPHA / DIGIT / "-")
475 // ; additional syntax of ehlo-params depends on
476 // ; ehlo-keyword
478 // The '.' we also allow in ehlo-keyword since it has been seen in the
479 // wild at least at 263.net.
481 struct ehlo_keyword : seq<sor<ALPHA, DIGIT>, star<sor<ALPHA, DIGIT, dash, dot, underscore>>> {};
483 // ehlo-param = 1*(%d33-126)
484 // ; any CHAR excluding <SP> and all
485 // ; control characters (US-ASCII 0-31 and 127
486 // ; inclusive)
488 struct ehlo_param : plus<range<33, 126>> {};
490 // ehlo-line = ehlo-keyword *( SP ehlo-param )
492 // The AUTH= thing is so common with some servers (postfix) that I
493 // guess we have to accept it.
495 struct ehlo_line
496 : seq<ehlo_keyword, star<seq<sor<SP,one<'='>>, ehlo_param>>> {};
498 // ehlo-ok-rsp = ( "250 " Domain [ SP ehlo-greet ] CRLF )
499 // /
500 // ( "250-" Domain [ SP ehlo-greet ] CRLF
501 // *( "250-" ehlo-line CRLF )
502 // "250 " ehlo-line CRLF )
504 // The last line having the optional ehlo_line is not strictly correct.
505 // Was added to work with postfix/src/smtpstone/smtp-sink.c.
507 struct ehlo_ok_rsp
508 : sor<seq<TAO_PEGTL_ISTRING("250 "), server_id, opt<ehlo_greet>, CRLF>,
510 seq<TAO_PEGTL_ISTRING("250-"), server_id, opt<ehlo_greet>, CRLF,
511 star<seq<TAO_PEGTL_ISTRING("250-"), ehlo_line, CRLF>>,
512 seq<TAO_PEGTL_ISTRING("250 "), opt<ehlo_line>, CRLF>>
513 > {};
515 struct ehlo_rsp
516 : sor<ehlo_ok_rsp, reply_lines> {};
518 struct helo_ok_rsp
519 : seq<TAO_PEGTL_ISTRING("250 "), server_id, opt<ehlo_greet>, CRLF> {};
521 struct auth_login_username
522 : seq<TAO_PEGTL_STRING("334 VXNlcm5hbWU6"), CRLF> {};
524 struct auth_login_password
525 : seq<TAO_PEGTL_STRING("334 UGFzc3dvcmQ6"), CRLF> {};
527 // clang-format on
529 template <typename Rule>
530 struct inaction : nothing<Rule> {
533 template <typename Rule>
534 struct action : nothing<Rule> {
537 template <>
538 struct action<server_id> {
539 template <typename Input>
540 static void apply(Input const& in, Connection& cnn)
542 cnn.server_id = in.string();
546 template <>
547 struct action<local_part> {
548 template <typename Input>
549 static void apply(Input const& in, Mailbox& mbx)
551 mbx.set_local(in.string());
555 template <>
556 struct action<non_local_part> {
557 template <typename Input>
558 static void apply(Input const& in, Mailbox& mbx)
560 mbx.set_domain(in.string());
564 template <>
565 struct action<greeting_ok> {
566 template <typename Input>
567 static void apply(Input const& in, Connection& cnn)
569 cnn.greeting_ok = true;
570 imemstream stream{begin(in), size(in)};
571 std::string line;
572 while (std::getline(stream, line)) {
573 LOG(INFO) << " S: " << line;
578 template <>
579 struct action<ehlo_ok_rsp> {
580 template <typename Input>
581 static void apply(Input const& in, Connection& cnn)
583 cnn.ehlo_ok = true;
584 imemstream stream{begin(in), size(in)};
585 std::string line;
586 while (std::getline(stream, line)) {
587 LOG(INFO) << " S: " << line;
592 template <>
593 struct action<ehlo_keyword> {
594 template <typename Input>
595 static void apply(Input const& in, Connection& cnn)
597 cnn.ehlo_keyword = in.string();
598 boost::to_upper(cnn.ehlo_keyword);
602 template <>
603 struct action<ehlo_param> {
604 template <typename Input>
605 static void apply(Input const& in, Connection& cnn)
607 cnn.ehlo_param.push_back(in.string());
611 template <>
612 struct action<ehlo_line> {
613 template <typename Input>
614 static void apply(Input const& in, Connection& cnn)
616 cnn.ehlo_params.emplace(std::move(cnn.ehlo_keyword),
617 std::move(cnn.ehlo_param));
621 template <>
622 struct action<reply_lines> {
623 template <typename Input>
624 static void apply(Input const& in, Connection& cnn)
626 imemstream stream{begin(in), size(in)};
627 std::string line;
628 while (std::getline(stream, line)) {
629 LOG(INFO) << " S: " << line;
634 template <>
635 struct action<reply_code> {
636 template <typename Input>
637 static void apply(Input const& in, Connection& cnn)
639 cnn.reply_code = in.string();
642 } // namespace RFC5321
644 namespace {
646 int conn(DNS::Resolver& res, Domain const& node, uint16_t port)
648 auto const use_4{!FLAGS_6};
649 auto const use_6{!FLAGS_4};
651 if (use_6) {
652 auto const fd{socket(AF_INET6, SOCK_STREAM, 0)};
653 PCHECK(fd >= 0) << "socket() failed";
655 if (!FLAGS_local_address.empty()) {
656 auto loc{sockaddr_in6{}};
657 loc.sin6_family = AF_INET6;
658 if (1 != inet_pton(AF_INET6, FLAGS_local_address.c_str(),
659 reinterpret_cast<void*>(&loc.sin6_addr))) {
660 LOG(FATAL) << "can't interpret " << FLAGS_local_address
661 << " as IPv6 address";
663 PCHECK(0 == bind(fd, reinterpret_cast<sockaddr*>(&loc), sizeof(loc)));
666 auto addrs{std::vector<std::string>{}};
668 if (node.is_address_literal()) {
669 if (IP6::is_address(node.ascii())) {
670 addrs.push_back(node.ascii());
672 if (IP6::is_address_literal(node.ascii())) {
673 auto const addr = IP6::as_address(node.ascii());
674 addrs.push_back(std::string(addr.data(), addr.length()));
677 else {
678 addrs = res.get_strings(DNS::RR_type::AAAA, node.ascii());
680 for (auto const& addr : addrs) {
681 auto in6{sockaddr_in6{}};
682 in6.sin6_family = AF_INET6;
683 in6.sin6_port = htons(port);
684 CHECK_EQ(inet_pton(AF_INET6, addr.c_str(),
685 reinterpret_cast<void*>(&in6.sin6_addr)),
687 if (connect(fd, reinterpret_cast<const sockaddr*>(&in6), sizeof(in6))) {
688 PLOG(WARNING) << "connect failed [" << addr << "]:" << port;
689 continue;
692 LOG(INFO) << fd << " connected to [" << addr << "]:" << port;
693 return fd;
696 close(fd);
698 if (use_4) {
699 auto fd{socket(AF_INET, SOCK_STREAM, 0)};
700 PCHECK(fd >= 0) << "socket() failed";
702 if (!FLAGS_local_address.empty()) {
703 auto loc{sockaddr_in{}};
704 loc.sin_family = AF_INET;
705 if (1 != inet_pton(AF_INET, FLAGS_local_address.c_str(),
706 reinterpret_cast<void*>(&loc.sin_addr))) {
707 LOG(FATAL) << "can't interpret " << FLAGS_local_address
708 << " as IPv4 address";
710 LOG(INFO) << "bind " << FLAGS_local_address;
711 PCHECK(0 == bind(fd, reinterpret_cast<sockaddr*>(&loc), sizeof(loc)));
714 auto addrs{std::vector<std::string>{}};
715 if (node.is_address_literal()) {
716 if (IP4::is_address(node.ascii())) {
717 addrs.push_back(node.ascii());
719 if (IP4::is_address_literal(node.ascii())) {
720 auto const addr = IP4::as_address(node.ascii());
721 addrs.push_back(std::string(addr.data(), addr.length()));
724 else {
725 addrs = res.get_strings(DNS::RR_type::A, node.ascii());
727 for (auto addr : addrs) {
728 auto in4{sockaddr_in{}};
729 in4.sin_family = AF_INET;
730 in4.sin_port = htons(port);
731 CHECK_EQ(inet_pton(AF_INET, addr.c_str(),
732 reinterpret_cast<void*>(&in4.sin_addr)),
734 if (connect(fd, reinterpret_cast<const sockaddr*>(&in4), sizeof(in4))) {
735 PLOG(WARNING) << "connect failed " << addr << ":" << port;
736 continue;
739 LOG(INFO) << " connected to " << addr << ":" << port;
740 return fd;
743 close(fd);
746 return -1;
749 class Eml {
750 public:
751 void add_hdr(std::string name, std::string value)
753 hdrs_.push_back(std::make_pair(name, value));
756 void foreach_hdr(std::function<void(std::string const& name,
757 std::string const& value)> func)
759 for (auto const& [name, value] : hdrs_) {
760 func(name, value);
764 private:
765 std::vector<std::pair<std::string, std::string>> hdrs_;
767 friend std::ostream& operator<<(std::ostream& os, Eml const& eml)
769 for (auto const& [name, value] : eml.hdrs_) {
770 os << name << ": " << value << "\r\n";
772 // return os << "\r\n"; // end of headers
773 return os /* << "\r\n" */; // end of headers
777 // // clang-format off
778 // char const* const signhdrs[] = {
779 // "From",
781 // "Message-ID",
783 // "Cc",
784 // "Date",
785 // "In-Reply-To",
786 // "References",
787 // "Reply-To",
788 // "Sender",
789 // "Subject",
790 // "To",
792 // "MIME-Version",
793 // "Content-Type",
794 // "Content-Transfer-Encoding",
796 // nullptr
797 // };
798 // clang-format on
800 enum class transfer_encoding {
801 seven_bit,
802 quoted_printable,
803 base64,
804 eight_bit,
805 binary,
808 enum class data_type {
809 ascii, // 7bit, quoted-printable and base64
810 utf8, // 8bit
811 binary, // binary
814 data_type type(std::string_view d)
817 auto in{memory_input<>{d.data(), d.size(), "data"}};
818 if (parse<RFC5322::body_ascii>(in)) {
819 return data_type::ascii;
823 auto in{memory_input<>{d.data(), d.size(), "data"}};
824 if (parse<RFC5322::body_utf8>(in)) {
825 return data_type::utf8;
828 // anything else is
829 return data_type::binary;
832 class content {
833 public:
834 content(char const* path)
835 : path_(path)
837 auto const body_sz{fs::file_size(path_)};
838 CHECK(body_sz) << "no body";
839 file_.open(path_);
840 type_ = ::type(*this);
843 char const* data() const { return file_.data(); }
844 size_t size() const { return file_.size(); }
845 data_type type() const { return type_; }
847 bool empty() const { return size() == 0; }
848 operator std::string_view() const { return std::string_view(data(), size()); }
850 private:
851 data_type type_;
852 fs::path path_;
853 boost::iostreams::mapped_file_source file_;
856 template <typename Input>
857 void fail(Input& in, RFC5321::Connection& cnn)
859 LOG(INFO) << " C: QUIT";
860 cnn.sock.out() << "QUIT\r\n" << std::flush;
861 // we might have a few error replies stacked up if we're pipelining
862 // CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
863 exit(EXIT_FAILURE);
866 template <typename Input>
867 void check_for_fail(Input& in, RFC5321::Connection& cnn, std::string_view cmd)
869 cnn.sock.out() << std::flush;
870 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
871 if (cnn.reply_code.at(0) != '2') {
872 LOG(ERROR) << cmd << " returned " << cnn.reply_code;
873 fail(in, cnn);
875 in.discard();
878 bool validate_name(const char* flagname, std::string const& value)
880 if (value.empty()) // empty name needs to validate, but
881 return true; // will not be used
882 memory_input<> name_in(value.c_str(), "name");
883 if (!parse<RFC5322::display_name_only, RFC5322::inaction>(name_in)) {
884 LOG(ERROR) << "bad name syntax " << value;
885 return false;
887 return true;
890 DEFINE_validator(from_name, &validate_name);
891 DEFINE_validator(to_name, &validate_name);
893 bool validate_address_RFC5322(const char* flagname, std::string const& value)
895 if (value.empty()) // empty name needs to validate, but
896 return true; // will not be used
897 memory_input<> name_in(value.c_str(), "address");
898 if (!parse<RFC5322::addr_spec_only, RFC5322::inaction>(name_in)) {
899 LOG(ERROR) << "bad address syntax " << value;
900 return false;
902 return true;
905 DEFINE_validator(from, &validate_address_RFC5322);
906 DEFINE_validator(to, &validate_address_RFC5322);
908 bool validate_address_RFC5321(const char* flagname, std::string const& value)
910 if (value.empty()) // empty name needs to validate, but
911 return true; // will not be used
912 memory_input<> name_in(value.c_str(), "path");
913 if (!parse<RFC5321::path_only, RFC5321::inaction>(name_in)) {
914 LOG(ERROR) << "bad address syntax " << value;
915 return false;
917 return true;
920 DEFINE_validator(smtp_from, &validate_address_RFC5321);
921 DEFINE_validator(smtp_to, &validate_address_RFC5321);
922 DEFINE_validator(smtp_to2, &validate_address_RFC5321);
924 void selftest()
926 CHECK(validate_name("selftest", ""s));
927 CHECK(validate_name("selftest", "Elmer J Fudd"s));
928 CHECK(validate_name("selftest", "\"Elmer J. Fudd\""s));
929 CHECK(validate_name("selftest", "Elmer! J! Fudd!"s));
931 CHECK(validate_address_RFC5321("selftest", "foo@digilicious.com"s));
932 CHECK(validate_address_RFC5321("selftest", "\"foo\"@digilicious.com"s));
933 CHECK(validate_address_RFC5321(
934 "selftest",
935 "\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@digilicious.com"s));
937 CHECK(validate_address_RFC5322("selftest", "foo@digilicious.com"s));
938 CHECK(validate_address_RFC5322("selftest", "\"foo\"@digilicious.com"s));
939 CHECK(validate_address_RFC5322(
940 "selftest",
941 "\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@digilicious.com"s));
943 auto const read_hook{[]() {}};
945 const char* greet_list[]{
946 "220-mtaig-aak03.mx.aol.com ESMTP Internet Inbound\r\n"
947 "220-AOL and its affiliated companies do not\r\n"
948 "220-authorize the use of its proprietary computers and computer\r\n"
949 "220-networks to accept, transmit, or distribute unsolicited bulk\r\n"
950 "220-e-mail sent from the internet.\r\n"
951 "220-Effective immediately:\r\n"
952 "220-AOL may no longer accept connections from IP addresses\r\n"
953 "220 which no do not have reverse-DNS (PTR records) assigned.\r\n",
955 "421 mtaig-maa02.mx.aol.com Service unavailable - try again later\r\n",
958 for (auto i : greet_list) {
959 auto cnn{RFC5321::Connection(0, 1, read_hook)};
960 auto in{memory_input<>{i, i}};
961 if (!parse<RFC5321::greeting, RFC5321::action /*, tao::pegtl::tracer*/>(
962 in, cnn)) {
963 LOG(FATAL) << "Error parsing greeting \"" << i << "\"";
965 if (cnn.greeting_ok) {
966 LOG(WARNING) << "greeting ok";
968 else {
969 LOG(WARNING) << "greeting was not in the affirmative";
973 const char* ehlo_rsp_list[]{
974 "250-www77.totaalholding.nl Hello "
975 "ec2-18-205-224-193.compute-1.amazonaws.com [18.205.224.193]\r\n"
976 "250-SIZE 52428800\r\n"
977 "250-8BITMIME\r\n"
978 "250-PIPELINING\r\n"
979 "250-X_PIPE_CONNECT\r\n"
980 "250-STARTTLS\r\n"
981 "250 HELP\r\n",
983 "250-HELLO, SAILOR!\r\n"
984 "250-NO-SOLICITING\r\n"
985 "250 8BITMIME\r\n",
987 "250-digilicious.com at your service, localhost. [IPv6:::1]\r\n"
988 "250-SIZE 15728640\r\n"
989 "250-8BITMIME\r\n"
990 "250-STARTTLS\r\n"
991 "250-ENHANCEDSTATUSCODES\r\n"
992 "250-PIPELINING\r\n"
993 "250-BINARYMIME\r\n"
994 "250-CHUNKING\r\n"
995 "250-SMTPUTF8\r\n"
996 "250 OK\r\n",
998 "500 5.5.1 command unrecognized: \"EHLO digilicious.com\\r\\n\"\r\n",
1000 "250-263xmail at your service\r\n"
1001 "250-STARTTLS\r\n"
1002 "250-MAE-SMTP\r\n"
1003 "250-263.net\r\n" // the '.' is not RFC complaint
1004 "250-SIZE 104857600\r\n"
1005 "250-ETRN\r\n"
1006 "250-ENHANCEDSTATUSCODES\r\n"
1007 "250-8BITMIME\r\n"
1008 "250 DSN\r\n",
1011 for (auto i : ehlo_rsp_list) {
1012 auto cnn{RFC5321::Connection(0, 1, read_hook)};
1013 auto in{memory_input<>{i, i}};
1014 if (!parse<RFC5321::ehlo_rsp, RFC5321::action /*, tao::pegtl::tracer*/>(
1015 in, cnn)) {
1016 LOG(FATAL) << "Error parsing ehlo response \"" << i << "\"";
1018 if (cnn.ehlo_ok) {
1019 LOG(WARNING) << "ehlo ok";
1021 else {
1022 LOG(WARNING) << "ehlo response was not in the affirmative";
1027 auto get_sender()
1029 if (FLAGS_client_id.empty()) {
1030 FLAGS_client_id = [] {
1031 auto const id_from_env{getenv("GHSMTP_CLIENT_ID")};
1032 if (id_from_env)
1033 return std::string{id_from_env};
1035 auto const hostname{osutil::get_hostname()};
1036 if (hostname.find('.') != std::string::npos)
1037 return hostname;
1039 LOG(FATAL) << "hostname not a FQDN, set GHSMTP_CLIENT_ID maybe?";
1040 }();
1043 if (FLAGS_sender.empty()) {
1044 FLAGS_sender = FLAGS_client_id;
1047 auto const sender{Domain{FLAGS_sender}};
1049 if (FLAGS_from.empty()) {
1050 FLAGS_from = "test-it@"s + sender.utf8();
1053 if (FLAGS_to.empty()) {
1054 FLAGS_to = "test-it@"s + sender.utf8();
1057 return sender;
1060 bool is_localhost(DNS::RR const& rr)
1062 if (std::holds_alternative<DNS::RR_MX>(rr)) {
1063 if (iequal(std::get<DNS::RR_MX>(rr).exchange(), "localhost"))
1064 return true;
1066 return false;
1069 bool starts_with(std::string_view str, std::string_view prefix)
1071 if (str.size() >= prefix.size())
1072 if (str.compare(0, prefix.size(), prefix) == 0)
1073 return true;
1074 return false;
1077 bool sts_rec(std::string const& sts_rec)
1079 return starts_with(sts_rec, "v=STSv1");
1082 std::vector<Domain>
1083 get_receivers(DNS::Resolver& res, Mailbox const& to_mbx, bool& enforce_dane)
1085 auto receivers{std::vector<Domain>{}};
1087 // User provided explicit host to receive mail.
1088 if (!FLAGS_mx_host.empty()) {
1089 receivers.emplace_back(FLAGS_mx_host);
1090 return receivers;
1093 // Non-local part is an address literal.
1094 if (to_mbx.domain().is_address_literal()) {
1095 receivers.emplace_back(to_mbx.domain());
1096 return receivers;
1099 // RFC 5321 section 5.1 "Locating the Target Host"
1101 // “The lookup first attempts to locate an MX record associated with
1102 // the name. If a CNAME record is found, the resulting name is
1103 // processed as if it were the initial name.”
1105 // Our (full) resolver will traverse any CNAMEs for us and return
1106 // the CNAME and MX records all together.
1108 auto const& domain = to_mbx.domain().ascii();
1110 auto q_sts{DNS::Query{res, DNS::RR_type::TXT, "_mta-sts."s + domain}};
1111 if (q_sts.has_record()) {
1112 auto sts_records = q_sts.get_strings();
1113 sts_records.erase(std::remove_if(begin(sts_records), end(sts_records),
1114 std::not_fn(sts_rec)),
1115 end(sts_records));
1116 if (size(sts_records) == 1) {
1117 LOG(INFO) << "### This domain implements MTA-STS ###";
1120 else {
1121 LOG(INFO) << "MTA-STS record not found for domain " << domain;
1124 auto q{DNS::Query{res, DNS::RR_type::MX, domain}};
1125 if (q.has_record()) {
1126 if (q.authentic_data()) {
1127 LOG(INFO) << "### MX records authentic for domain " << domain << " ###";
1129 else {
1130 LOG(INFO) << "MX records can't be authenticated for domain " << domain;
1131 enforce_dane = false;
1134 auto mxs{q.get_records()};
1136 mxs.erase(std::remove_if(begin(mxs), end(mxs), is_localhost), end(mxs));
1138 auto const nmx = std::count_if(begin(mxs), end(mxs), [](auto const& rr) {
1139 return std::holds_alternative<DNS::RR_MX>(rr);
1142 if (nmx == 1) {
1143 for (auto const& mx : mxs) {
1144 if (std::holds_alternative<DNS::RR_MX>(mx)) {
1145 // RFC 7505 null MX record
1146 if ((std::get<DNS::RR_MX>(mx).preference() == 0) &&
1147 (std::get<DNS::RR_MX>(mx).exchange().empty() ||
1148 (std::get<DNS::RR_MX>(mx).exchange() == "."))) {
1149 LOG(INFO) << "domain " << domain << " does not accept mail";
1150 return receivers;
1156 if (nmx == 0) {
1157 // implicit MX RR
1158 receivers.emplace_back(domain);
1159 return receivers;
1162 // […] then the sender-SMTP MUST randomize them to spread the load
1163 // across multiple mail exchangers for a specific organization.
1164 std::shuffle(begin(mxs), end(mxs), std::random_device());
1165 std::sort(begin(mxs), end(mxs), [](auto const& a, auto const& b) {
1166 if (std::holds_alternative<DNS::RR_MX>(a) &&
1167 std::holds_alternative<DNS::RR_MX>(b)) {
1168 return std::get<DNS::RR_MX>(a).preference() <
1169 std::get<DNS::RR_MX>(b).preference();
1171 return false;
1174 if (nmx)
1175 LOG(INFO) << "MXs for " << domain << " are:";
1177 for (auto const& mx : mxs) {
1178 if (std::holds_alternative<DNS::RR_MX>(mx)) {
1179 receivers.emplace_back(std::get<DNS::RR_MX>(mx).exchange());
1180 LOG(INFO) << std::setfill(' ') << std::setw(3)
1181 << std::get<DNS::RR_MX>(mx).preference() << " "
1182 << std::get<DNS::RR_MX>(mx).exchange();
1186 return receivers;
1189 auto parse_mailboxes()
1191 auto from_mbx{Mailbox{}};
1192 auto from_in{memory_input<>{FLAGS_from, "from"}};
1193 if (!parse<RFC5322::addr_spec_only, RFC5322::action>(from_in, from_mbx)) {
1194 LOG(FATAL) << "bad From: address syntax <" << FLAGS_from << ">";
1196 LOG(INFO) << " from_mbx == " << from_mbx;
1198 auto local_from{memory_input<>{from_mbx.local_part(), "from.local"}};
1199 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_from);
1201 auto to_mbx{Mailbox{}};
1202 auto to_in{memory_input<>{FLAGS_to, "to"}};
1203 if (!parse<RFC5322::addr_spec_only, RFC5322::action>(to_in, to_mbx)) {
1204 LOG(FATAL) << "bad To: address syntax <" << FLAGS_to << ">";
1206 LOG(INFO) << " to_mbx == " << to_mbx;
1208 auto local_to{memory_input<>{to_mbx.local_part(), "to.local"}};
1209 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_to);
1211 auto smtp_from_mbx{Mailbox{}};
1212 if (!FLAGS_smtp_from.empty()) {
1213 auto smtp_from_in{memory_input<>{FLAGS_smtp_from, "SMTP.from"}};
1214 if (!parse<RFC5321::path_only, RFC5321::action>(smtp_from_in,
1215 smtp_from_mbx)) {
1216 LOG(FATAL) << "bad MAIL FROM: address syntax <" << FLAGS_smtp_from << ">";
1218 LOG(INFO) << " smtp_from_mbx == " << smtp_from_mbx;
1219 auto local_smtp_from{
1220 memory_input<>{smtp_from_mbx.local_part(), "SMTP.from.local"}};
1221 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_smtp_from);
1223 else {
1224 smtp_from_mbx = from_mbx;
1227 auto smtp_to_mbx{Mailbox{}};
1228 if (!FLAGS_smtp_to.empty()) {
1229 auto smtp_to_in{memory_input<>{FLAGS_smtp_to, "SMTP.to"}};
1230 if (!parse<RFC5321::path_only, RFC5321::action>(smtp_to_in, smtp_to_mbx)) {
1231 LOG(FATAL) << "bad RCPT TO: address syntax <" << FLAGS_smtp_to << ">";
1233 LOG(INFO) << " smtp_to_mbx == " << smtp_to_mbx;
1235 auto local_smtp_to{
1236 memory_input<>{smtp_to_mbx.local_part(), "SMTP.to.local"}};
1237 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_smtp_to);
1239 else {
1240 smtp_to_mbx = to_mbx;
1243 return std::tuple(from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx);
1246 auto create_eml(Domain const& sender,
1247 std::string const& from,
1248 std::string const& to,
1249 std::vector<content> const& bodies,
1250 bool ext_smtputf8)
1252 auto eml{Eml{}};
1253 auto const date{Now{}};
1254 auto const pill{Pill{}};
1256 auto mid_str = fmt::format("<{}.{}@{}>", date.sec(), pill, sender.ascii());
1257 eml.add_hdr("Message-ID", mid_str.c_str());
1258 eml.add_hdr("Date", date.c_str());
1260 if (!FLAGS_from_name.empty())
1261 eml.add_hdr("From", fmt::format("{} <{}>", FLAGS_from_name, from));
1262 else
1263 eml.add_hdr("From", from);
1265 eml.add_hdr("Subject", FLAGS_subject);
1267 if (!FLAGS_to_name.empty())
1268 eml.add_hdr("To", fmt::format("{} <{}>", FLAGS_to_name, to));
1269 else
1270 eml.add_hdr("To", to);
1272 if (!FLAGS_keywords.empty())
1273 eml.add_hdr("Keywords", FLAGS_keywords);
1275 if (!FLAGS_references.empty())
1276 eml.add_hdr("References", FLAGS_references);
1278 if (!FLAGS_in_reply_to.empty())
1279 eml.add_hdr("In-Reply-To", FLAGS_in_reply_to);
1281 if (!FLAGS_reply_to.empty())
1282 eml.add_hdr("Reply-To", FLAGS_reply_to);
1284 if (!FLAGS_reply_2.empty())
1285 eml.add_hdr("Reply-To", FLAGS_reply_2);
1287 eml.add_hdr("MIME-Version", "1.0");
1288 eml.add_hdr("Content-Language", "en-US");
1290 auto magic{Magic{}}; // to ID buffer contents
1292 if (!FLAGS_content_type.empty()) {
1293 eml.add_hdr("Content-Type", FLAGS_content_type);
1295 else {
1296 eml.add_hdr("Content-Type", magic.buffer(bodies[0]));
1299 if (!FLAGS_content_transfer_encoding.empty()) {
1300 eml.add_hdr("Content-Transfer-Encoding", FLAGS_content_transfer_encoding);
1303 return eml;
1306 void sign_eml(Eml& eml,
1307 std::string const& from_dom,
1308 std::vector<content> const& bodies)
1310 auto const body_type = (bodies[0].type() == data_type::binary)
1311 ? OpenDKIM::sign::body_type::binary
1312 : OpenDKIM::sign::body_type::text;
1314 auto const key_file = FLAGS_dkim_key_file.empty()
1315 ? (FLAGS_selector + ".private")
1316 : FLAGS_dkim_key_file;
1317 std::ifstream keyfs(key_file.c_str());
1318 CHECK(keyfs.good()) << "can't access " << key_file;
1319 std::string key(std::istreambuf_iterator<char>{keyfs}, {});
1320 OpenDKIM::sign dks(key.c_str(), FLAGS_selector.c_str(), from_dom.c_str(),
1321 body_type);
1322 eml.foreach_hdr([&dks](std::string const& name, std::string const& value) {
1323 auto const header = name + ": "s + value;
1324 dks.header(header.c_str());
1326 dks.eoh();
1327 for (auto const& body : bodies) {
1328 dks.body(body);
1330 dks.eom();
1331 eml.add_hdr("DKIM-Signature"s, dks.getsighdr());
1334 template <typename Input>
1335 void do_auth(Input& in, RFC5321::Connection& cnn)
1337 if (FLAGS_username.empty() && FLAGS_password.empty())
1338 return;
1340 auto const auth = cnn.ehlo_params.find("AUTH");
1341 if (auth == end(cnn.ehlo_params)) {
1342 LOG(ERROR) << "server doesn't support AUTH";
1343 fail(in, cnn);
1346 // Perfer PLAIN mechanism.
1347 if (std::find(begin(auth->second), end(auth->second), "PLAIN") !=
1348 end(auth->second)) {
1349 LOG(INFO) << "C: AUTH PLAIN";
1350 auto const tok = fmt::format("\0{}\0{}", FLAGS_username, FLAGS_password);
1351 cnn.sock.out() << "AUTH PLAIN " << Base64::enc(tok) << "\r\n" << std::flush;
1352 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1353 if (cnn.reply_code != "235") {
1354 LOG(ERROR) << "AUTH PLAIN returned " << cnn.reply_code;
1355 fail(in, cnn);
1358 // The LOGIN SASL mechanism is obsolete.
1359 else if (std::find(begin(auth->second), end(auth->second), "LOGIN") !=
1360 end(auth->second)) {
1361 LOG(INFO) << "C: AUTH LOGIN";
1362 cnn.sock.out() << "AUTH LOGIN\r\n" << std::flush;
1363 CHECK((parse<RFC5321::auth_login_username>(in)));
1364 cnn.sock.out() << Base64::enc(FLAGS_username) << "\r\n" << std::flush;
1365 CHECK((parse<RFC5321::auth_login_password>(in)));
1366 cnn.sock.out() << Base64::enc(FLAGS_password) << "\r\n" << std::flush;
1367 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1368 if (cnn.reply_code != "235") {
1369 LOG(ERROR) << "AUTH LOGIN returned " << cnn.reply_code;
1370 fail(in, cnn);
1373 else {
1374 LOG(ERROR) << "server doesn't support AUTH methods PLAIN or LOGIN";
1375 fail(in, cnn);
1379 // Do various bad things during the DATA transfer.
1381 template <typename Input>
1382 void bad_daddy(Input& in, RFC5321::Connection& cnn)
1384 LOG(INFO) << "C: DATA";
1385 cnn.sock.out() << "DATA\r\n";
1386 cnn.sock.out() << std::flush;
1388 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1389 if (cnn.reply_code != "354") {
1390 LOG(ERROR) << "DATA returned " << cnn.reply_code;
1391 fail(in, cnn);
1394 // cnn.sock.out() << "\r\nThis ->\n<- is a bare LF!\r\n";
1395 if (FLAGS_bare_lf)
1396 cnn.sock.out() << "\n.\n\r\n";
1398 if (FLAGS_to_the_neck) {
1399 for (;;) {
1400 cnn.sock.out() << "####################"
1401 "####################"
1402 "####################"
1403 << std::flush;
1407 if (FLAGS_long_line) {
1408 for (auto i{0}; i < 10000; ++i) {
1409 cnn.sock.out() << 'X';
1411 cnn.sock.out() << "\r\n" << std::flush;
1414 while (FLAGS_slow_strangle) {
1415 for (auto i{0}; i < 500; ++i) {
1416 cnn.sock.out() << 'X' << std::flush;
1417 sleep(1);
1419 cnn.sock.out() << "\r\n";
1422 // Done!
1423 cnn.sock.out() << ".\r\n" << std::flush;
1424 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1426 LOG(INFO) << "reply_code == " << cnn.reply_code;
1427 CHECK_EQ(cnn.reply_code.at(0), '2');
1429 LOG(INFO) << "C: QUIT";
1430 cnn.sock.out() << "QUIT\r\n" << std::flush;
1431 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1434 bool snd(fs::path config_path,
1435 int fd_in,
1436 int fd_out,
1437 Domain const& sender,
1438 Domain const& receiver,
1439 DNS::RR_collection const& tlsa_rrs,
1440 bool enforce_dane,
1441 Mailbox const& from_mbx,
1442 Mailbox const& to_mbx,
1443 Mailbox const& smtp_from_mbx,
1444 Mailbox const& smtp_to_mbx,
1445 std::vector<content> const& bodies)
1447 auto constexpr read_hook{[]() {}};
1449 auto cnn{RFC5321::Connection(fd_in, fd_out, read_hook)};
1451 auto in{
1452 istream_input<eol::crlf, 1>{cnn.sock.in(), FLAGS_bfr_size, "session"}};
1453 if (!parse<RFC5321::greeting, RFC5321::action>(in, cnn)) {
1454 LOG(WARNING) << "can't parse greeting";
1455 return false;
1458 if (!cnn.greeting_ok) {
1459 LOG(WARNING) << "greeting was not in the affirmative, skipping";
1460 return false;
1463 // try EHLO/HELO
1465 if (FLAGS_use_esmtp) {
1466 LOG(INFO) << "C: EHLO " << FLAGS_client_id;
1468 if (FLAGS_slow_strangle) {
1469 auto ehlo_str = fmt::format("EHLO {}\r\n", FLAGS_client_id);
1470 for (auto ch : ehlo_str) {
1471 cnn.sock.out() << ch << std::flush;
1472 sleep(1);
1475 else {
1476 cnn.sock.out() << "EHLO " << FLAGS_client_id << "\r\n" << std::flush;
1479 CHECK((parse<RFC5321::ehlo_rsp, RFC5321::action>(in, cnn)));
1480 if (!cnn.ehlo_ok) {
1482 if (FLAGS_force_smtputf8) {
1483 LOG(WARNING) << "ehlo response was not in the affirmative, skipping";
1484 return false;
1487 LOG(WARNING) << "ehlo response was not in the affirmative, trying HELO";
1488 FLAGS_use_esmtp = false;
1492 if (!FLAGS_use_esmtp) {
1493 LOG(INFO) << "C: HELO " << sender.ascii();
1494 cnn.sock.out() << "HELO " << sender.ascii() << "\r\n" << std::flush;
1495 if (!parse<RFC5321::helo_ok_rsp, RFC5321::action>(in, cnn)) {
1496 LOG(WARNING) << "HELO didn't work, skipping";
1497 return false;
1501 // Check extensions
1503 auto bad_dad = FLAGS_bare_lf || FLAGS_slow_strangle || FLAGS_to_the_neck;
1505 if (bad_dad) {
1506 FLAGS_use_chunking = false;
1507 FLAGS_use_size = false;
1510 auto const ext_8bitmime{FLAGS_use_8bitmime && cnn.has_extension("8BITMIME")};
1512 auto const ext_chunking{FLAGS_use_chunking && cnn.has_extension("CHUNKING")};
1514 auto const ext_binarymime{FLAGS_use_binarymime && ext_chunking &&
1515 cnn.has_extension("BINARYMIME")};
1517 auto const ext_deliverby{FLAGS_use_deliverby &&
1518 cnn.has_extension("DELIVERBY")};
1520 auto const ext_pipelining{FLAGS_use_pipelining &&
1521 cnn.has_extension("PIPELINING")};
1523 auto const ext_size{FLAGS_use_size && cnn.has_extension("SIZE")};
1525 auto const ext_smtputf8{FLAGS_use_smtputf8 && cnn.has_extension("SMTPUTF8")};
1527 auto const ext_starttls{FLAGS_use_tls && cnn.has_extension("STARTTLS")};
1529 if (FLAGS_force_smtputf8 && !ext_smtputf8) {
1530 LOG(WARNING) << "no SMTPUTF8, skipping";
1531 return false;
1534 if (ext_starttls) {
1535 LOG(INFO) << "C: STARTTLS";
1536 cnn.sock.out() << "STARTTLS\r\n" << std::flush;
1537 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1539 LOG(INFO) << "cnn.sock.starttls_client(\"" << receiver.ascii() << "\");";
1540 cnn.sock.starttls_client(config_path, sender.ascii().c_str(),
1541 receiver.ascii().c_str(), tlsa_rrs, enforce_dane);
1543 LOG(INFO) << "TLS: " << cnn.sock.tls_info();
1545 LOG(INFO) << "C: EHLO " << FLAGS_client_id;
1546 cnn.sock.out() << "EHLO " << FLAGS_client_id << "\r\n" << std::flush;
1547 CHECK((parse<RFC5321::ehlo_rsp, RFC5321::action>(in, cnn)));
1549 else if (FLAGS_require_tls) {
1550 LOG(ERROR) << "No TLS extension, won't send mail in plain text without "
1551 "--require_tls=false.";
1552 LOG(INFO) << "C: QUIT";
1553 cnn.sock.out() << "QUIT\r\n" << std::flush;
1554 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1555 exit(EXIT_FAILURE);
1558 if (FLAGS_noop) {
1559 LOG(INFO) << "C: NOOP";
1560 cnn.sock.out() << "NOOP\r\n" << std::flush;
1563 if (receiver != cnn.server_id) {
1564 LOG(INFO) << "server identifies as " << cnn.server_id;
1567 if (FLAGS_force_smtputf8 && !ext_smtputf8) {
1568 LOG(WARNING) << "does not support SMTPUTF8";
1569 return false;
1572 if (ext_smtputf8 && !ext_8bitmime) {
1573 LOG(ERROR)
1574 << "SMTPUTF8 requires 8BITMIME, see RFC-6531 section 3.1 item 8.";
1575 LOG(INFO) << "C: QUIT";
1576 cnn.sock.out() << "QUIT\r\n" << std::flush;
1577 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1578 exit(EXIT_FAILURE);
1581 auto max_msg_size{0u};
1582 if (ext_size) {
1583 if (!cnn.ehlo_params["SIZE"].empty()) {
1584 char* ep = nullptr;
1585 max_msg_size = strtoul(cnn.ehlo_params["SIZE"][0].c_str(), &ep, 10);
1586 if (ep && (*ep != '\0')) {
1587 LOG(WARNING) << "garbage in SIZE argument: "
1588 << cnn.ehlo_params["SIZE"][0];
1593 auto deliver_by_min{0u};
1594 if (ext_deliverby) {
1595 if (!cnn.ehlo_params["DELIVERBY"].empty()) {
1596 char* ep = nullptr;
1597 deliver_by_min =
1598 strtoul(cnn.ehlo_params["DELIVERBY"][0].c_str(), &ep, 10);
1599 if (ep && (*ep != '\0')) {
1600 LOG(WARNING) << "garbage in DELIVERBY argument: "
1601 << cnn.ehlo_params["DELIVERBY"][0];
1605 do_auth(in, cnn);
1607 in.discard();
1609 auto enc = FLAGS_force_smtputf8 ? Mailbox::domain_encoding::utf8
1610 : Mailbox::domain_encoding::ascii;
1612 std::string from =
1613 from_mbx.empty() ? smtp_from_mbx.as_string(enc) : from_mbx.as_string(enc);
1614 std::string to =
1615 to_mbx.empty() ? smtp_to_mbx.as_string(enc) : to_mbx.as_string(enc);
1617 auto eml{create_eml(sender, from, to, bodies, ext_smtputf8)};
1619 if (FLAGS_use_dkim) {
1620 auto const dom = Mailbox(from).domain().ascii();
1621 sign_eml(eml, dom.c_str(), bodies);
1623 else if (FLAGS_bogus_dkim) {
1624 eml.add_hdr("DKIM-Signature", "");
1627 // Get the header as one big string
1628 std::ostringstream hdr_stream;
1629 hdr_stream << eml;
1630 if (!FLAGS_rawmsg)
1631 hdr_stream << "\r\n";
1632 auto const& hdr_str = hdr_stream.str();
1634 // In the case of DATA style transfer, this total_size number is an
1635 // *estimate* only, as line endings may be translated or added
1636 // during transfer. In the BDAT case, this number must be exact.
1638 auto total_size = hdr_str.size();
1639 for (auto const& body : bodies)
1640 total_size += body.size();
1642 if (ext_size && max_msg_size && (total_size > max_msg_size)) {
1643 LOG(ERROR) << "message size " << total_size << " exceeds size limit of "
1644 << max_msg_size;
1645 LOG(INFO) << "C: QUIT";
1646 cnn.sock.out() << "QUIT\r\n" << std::flush;
1647 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1648 exit(EXIT_FAILURE);
1651 std::ostringstream param_stream;
1652 if (FLAGS_huge_size && ext_size) {
1653 // Claim some huge size.
1654 param_stream << " SIZE=" << std::numeric_limits<std::streamsize>::max();
1656 else if (ext_size) {
1657 param_stream << " SIZE=" << total_size;
1660 if (ext_binarymime) {
1661 param_stream << " BODY=BINARYMIME";
1663 else if (ext_8bitmime) {
1664 param_stream << " BODY=8BITMIME";
1667 if (ext_deliverby) {
1668 param_stream << " BY=1200;NT";
1671 if (ext_smtputf8) {
1672 param_stream << " SMTPUTF8";
1675 if (FLAGS_badpipline) {
1676 LOG(INFO) << "C: NOOP NOOP";
1677 cnn.sock.out() << "NOOP\r\nNOOP\r\n" << std::flush;
1680 auto param_str = param_stream.str();
1682 for (auto count = 0UL; count < FLAGS_reps; ++count) {
1684 LOG(INFO) << "C: MAIL FROM:<" << smtp_from_mbx.as_string(enc) << '>'
1685 << param_str;
1686 cnn.sock.out() << "MAIL FROM:<" << smtp_from_mbx.as_string(enc) << '>'
1687 << param_str << "\r\n";
1688 if (!ext_pipelining) {
1689 check_for_fail(in, cnn, "MAIL FROM");
1692 LOG(INFO) << "C: RCPT TO:<" << smtp_to_mbx.as_string(enc) << ">";
1693 cnn.sock.out() << "RCPT TO:<" << smtp_to_mbx.as_string(enc) << ">\r\n";
1694 if (!ext_pipelining) {
1695 check_for_fail(in, cnn, "RCPT TO");
1698 if (FLAGS_nosend) {
1699 LOG(INFO) << "C: QUIT";
1700 cnn.sock.out() << "QUIT\r\n" << std::flush;
1701 if (ext_pipelining) {
1702 check_for_fail(in, cnn, "MAIL FROM");
1703 check_for_fail(in, cnn, "RCPT TO");
1705 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1706 LOG(INFO) << "no-sending";
1707 exit(EXIT_SUCCESS);
1710 if (bad_dad) {
1711 if (ext_pipelining) {
1712 cnn.sock.out() << std::flush;
1713 check_for_fail(in, cnn, "MAIL FROM");
1714 check_for_fail(in, cnn, "RCPT TO");
1716 bad_daddy(in, cnn);
1717 return true;
1720 auto msg = std::make_unique<MessageStore>();
1722 // if service is smtp (i.e. sending real mail, not smtp-test)
1723 try {
1724 if (FLAGS_save) {
1725 msg->open(sender.ascii(), total_size * 2, ".Sent");
1726 msg->write(hdr_str.data(), hdr_str.size());
1727 for (auto const& body : bodies) {
1728 msg->write(body.data(), body.size());
1732 catch (std::system_error const& e) {
1733 switch (errno) {
1734 case ENOSPC:
1735 msg->trash();
1736 msg.reset();
1737 LOG(FATAL) << "no space";
1738 [[fallthrough]];
1740 default:
1741 msg->trash();
1742 msg.reset();
1743 LOG(ERROR) << "errno==" << errno << ": " << strerror(errno);
1744 LOG(FATAL) << e.what();
1747 catch (std::exception const& e) {
1748 msg->trash();
1749 msg.reset();
1750 LOG(FATAL) << e.what();
1753 if (ext_chunking) {
1754 std::ostringstream bdat_stream;
1755 bdat_stream << "BDAT " << total_size << " LAST";
1756 LOG(INFO) << "C: " << bdat_stream.str();
1758 cnn.sock.out() << bdat_stream.str() << "\r\n";
1759 cnn.sock.out().write(hdr_str.data(), hdr_str.size());
1760 CHECK(cnn.sock.out().good());
1762 for (auto const& body : bodies) {
1763 cnn.sock.out().write(body.data(), body.size());
1764 CHECK(cnn.sock.out().good());
1767 if (FLAGS_pipeline_quit) {
1768 LOG(INFO) << "C: QUIT";
1769 cnn.sock.out() << "QUIT\r\n" << std::flush;
1772 cnn.sock.out() << std::flush;
1773 CHECK(cnn.sock.out().good());
1775 // NOW check returns
1776 if (ext_pipelining) {
1777 check_for_fail(in, cnn, "MAIL FROM");
1778 check_for_fail(in, cnn, "RCPT TO");
1781 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1782 if (cnn.reply_code != "250") {
1783 LOG(ERROR) << "BDAT returned " << cnn.reply_code;
1784 fail(in, cnn);
1787 else {
1788 LOG(INFO) << "C: DATA";
1789 cnn.sock.out() << "DATA\r\n";
1791 // NOW check returns
1792 if (ext_pipelining) {
1793 check_for_fail(in, cnn, "MAIL FROM");
1794 check_for_fail(in, cnn, "RCPT TO");
1796 cnn.sock.out() << std::flush;
1797 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1798 if (cnn.reply_code != "354") {
1799 LOG(ERROR) << "DATA returned " << cnn.reply_code;
1800 fail(in, cnn);
1803 cnn.sock.out() << eml;
1804 if (!FLAGS_rawmsg)
1805 cnn.sock.out() << "\r\n";
1807 for (auto const& body : bodies) {
1808 auto lineno = 0;
1809 auto line{std::string{}};
1810 auto isbody{imemstream{body.data(), body.size()}};
1811 while (std::getline(isbody, line)) {
1812 ++lineno;
1813 if (!cnn.sock.out().good()) {
1814 cnn.sock.log_stats();
1815 LOG(FATAL) << "output no good at line " << lineno;
1817 if (FLAGS_rawdog) {
1818 // This adds a final newline at the end of the file, if no
1819 // line ending was present.
1820 cnn.sock.out() << line << '\n';
1822 else {
1823 // This code converts single LF line endings into CRLF.
1824 // This code does nothing to fix single CR characters not
1825 // part of a CRLF pair.
1827 // This loop adds a CRLF and the end of the transmission if
1828 // the file doesn't already end with one. This is a
1829 // requirement of the SMTP DATA protocol.
1831 if (line.length() && (line.at(0) == '.')) {
1832 cnn.sock.out() << '.';
1834 cnn.sock.out() << line;
1835 if (line.length() && line.back() != '\r')
1836 cnn.sock.out() << '\r';
1837 cnn.sock.out() << '\n';
1841 CHECK(cnn.sock.out().good());
1843 // Done!
1844 if (FLAGS_pipeline_quit) {
1845 LOG(INFO) << "C: QUIT";
1846 cnn.sock.out() << ".\r\nQUIT\r\n" << std::flush;
1848 else {
1849 cnn.sock.out() << ".\r\n" << std::flush;
1851 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1853 if (cnn.reply_code.at(0) == '2') {
1854 if (FLAGS_save) {
1855 msg->deliver();
1857 else {
1858 msg->trash();
1860 LOG(INFO) << "mail was sent successfully";
1862 in.discard();
1865 if (!FLAGS_pipeline_quit) {
1866 LOG(INFO) << "C: QUIT";
1867 cnn.sock.out() << "QUIT\r\n" << std::flush;
1869 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1871 return true;
1874 DNS::RR_collection
1875 get_tlsa_rrs(DNS::Resolver& res, Domain const& domain, uint16_t port)
1877 auto const tlsa = fmt::format("_{}._tcp.{}", port, domain.ascii());
1879 DNS::Query q(res, DNS::RR_type::TLSA, tlsa);
1881 if (q.nx_domain()) {
1882 LOG(INFO) << "TLSA data not found for " << domain << ':' << port;
1885 if (q.bogus_or_indeterminate()) {
1886 LOG(WARNING) << "TLSA data is bogus or indeterminate";
1889 if (q.authentic_data()) {
1890 LOG(INFO) << "### TLSA records authentic for domain " << domain << " ###";
1892 else {
1893 LOG(INFO) << "TLSA records can't be authenticated for domain " << domain;
1896 auto tlsa_rrs = q.get_records();
1897 if (!tlsa_rrs.empty()) {
1898 LOG(INFO) << "### TLSA data found for " << domain << ':' << port << " ###";
1901 return tlsa_rrs;
1903 } // namespace
1905 int main(int argc, char* argv[])
1907 std::ios::sync_with_stdio(false);
1909 { // Need to work with either namespace.
1910 using namespace gflags;
1911 using namespace google;
1912 ParseCommandLineFlags(&argc, &argv, true);
1915 auto const config_path = osutil::get_config_dir();
1917 auto sender = get_sender();
1919 if (FLAGS_selftest) {
1920 selftest();
1921 return 0;
1924 auto bodies{std::vector<content>{}};
1925 for (int a = 1; a < argc; ++a) {
1926 if (!fs::exists(argv[a]))
1927 LOG(FATAL) << "can't find mail body part " << argv[a];
1928 bodies.push_back(argv[a]);
1931 if (argc == 1)
1932 bodies.push_back("body.txt");
1934 CHECK_EQ(bodies.size(), 1) << "only one body part for now";
1935 CHECK(!(FLAGS_4 && FLAGS_6)) << "must use /some/ IP version";
1937 if (FLAGS_force_smtputf8)
1938 FLAGS_use_smtputf8 = true;
1940 auto&& [from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx] = parse_mailboxes();
1942 if (to_mbx.domain().empty() && FLAGS_mx_host.empty()) {
1943 LOG(ERROR) << "don't know who to send this mail to";
1944 return 0;
1947 auto const port{osutil::get_port(FLAGS_service.c_str(), "tcp")};
1949 auto res{DNS::Resolver{config_path}};
1950 auto tlsa_rrs{get_tlsa_rrs(res, to_mbx.domain(), port)};
1952 if (FLAGS_pipe) {
1953 return snd(config_path, STDIN_FILENO, STDOUT_FILENO, sender,
1954 to_mbx.domain(), tlsa_rrs, false, from_mbx, to_mbx,
1955 smtp_from_mbx, smtp_to_mbx, bodies)
1956 ? EXIT_SUCCESS
1957 : EXIT_FAILURE;
1960 bool enforce_dane = true;
1961 auto const receivers = get_receivers(res, to_mbx, enforce_dane);
1963 if (receivers.empty()) {
1964 LOG(INFO) << "no place to send this mail";
1965 return EXIT_SUCCESS;
1968 for (auto const& receiver : receivers) {
1969 LOG(INFO) << "trying " << receiver << ":" << FLAGS_service;
1971 if (FLAGS_noconn) {
1972 LOG(INFO) << "skipping";
1973 continue;
1976 auto fd = conn(res, receiver, port);
1977 if (fd == -1) {
1978 LOG(WARNING) << "no connection, skipping";
1979 continue;
1982 // Get our local IP address as "us".
1984 sa::sockaddrs us_addr{};
1985 socklen_t us_addr_len{sizeof us_addr};
1986 char us_addr_str[INET6_ADDRSTRLEN]{'\0'};
1987 std::vector<std::string> fcrdns;
1988 bool private_addr = false;
1990 if (-1 == getsockname(fd, &us_addr.addr, &us_addr_len)) {
1991 // Ignore ENOTSOCK errors from getsockname, useful for testing.
1992 PLOG_IF(WARNING, ENOTSOCK != errno) << "getsockname failed";
1994 else {
1995 switch (us_addr_len) {
1996 case sizeof(sockaddr_in):
1997 PCHECK(inet_ntop(AF_INET, &us_addr.addr_in.sin_addr, us_addr_str,
1998 sizeof us_addr_str) != nullptr);
1999 if (IP4::is_private(us_addr_str))
2000 private_addr = true;
2001 else
2002 fcrdns = DNS::fcrdns4(res, us_addr_str);
2003 break;
2005 case sizeof(sockaddr_in6):
2006 PCHECK(inet_ntop(AF_INET6, &us_addr.addr_in6.sin6_addr, us_addr_str,
2007 sizeof us_addr_str) != nullptr);
2008 if (IP6::is_private(us_addr_str))
2009 private_addr = true;
2010 else
2011 fcrdns = DNS::fcrdns6(res, us_addr_str);
2012 break;
2014 default:
2015 LOG(FATAL) << "bogus address length (" << us_addr_len
2016 << ") returned from getsockname";
2020 if (fcrdns.size()) {
2021 LOG(INFO) << "our names are:";
2022 for (auto const& fc : fcrdns) {
2023 LOG(INFO) << " " << fc;
2027 if (!private_addr) {
2028 // look at from_mbx.domain() and get SPF records
2030 // also look at our ID to get SPF records.
2033 if (to_mbx.domain() == receiver) {
2034 if (snd(config_path, fd, fd, sender, receiver, tlsa_rrs, enforce_dane,
2035 from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx, bodies)) {
2036 return EXIT_SUCCESS;
2039 else {
2040 auto tlsa_rrs_mx{get_tlsa_rrs(res, receiver, port)};
2041 tlsa_rrs_mx.insert(end(tlsa_rrs_mx), begin(tlsa_rrs), end(tlsa_rrs));
2042 if (snd(config_path, fd, fd, sender, receiver, tlsa_rrs_mx, enforce_dane,
2043 from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx, bodies)) {
2044 return EXIT_SUCCESS;
2048 close(fd);
2051 LOG(ERROR) << "we ran out of hosts to try";