try to fix git tangle
[ghsmtp.git] / snd.cpp
blob8cee3fe709fab246471506cab02dcf303e2eb35e
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 #include <range/v3/numeric/accumulate.hpp>
11 namespace rng = ranges;
13 DEFINE_uint64(reps, 1, "now many duplicate transactions per connection");
15 // This needs to be at least the length of each string it's trying to match.
16 DEFINE_uint64(bfr_size, 4 * 1024, "parser buffer size");
18 DEFINE_bool(selftest, false, "run a self test");
20 DEFINE_bool(pipeline_quit, false, "pipeline the QUIT command");
21 DEFINE_bool(badpipline, false, "send two NOOPs back-to-back");
22 DEFINE_bool(bare_lf, false, "send a bare LF");
23 DEFINE_bool(huge_size, false, "attempt with huge size");
24 DEFINE_bool(long_line, false, "super long text line");
25 DEFINE_bool(noconn, false, "don't connect to any host");
26 DEFINE_bool(noop, false, "send a NOOP right after EHLO");
27 DEFINE_bool(nosend, false, "don't actually send any mail");
28 DEFINE_bool(pipe, false, "send to stdin/stdout");
29 DEFINE_bool(rawmsg, false, "the body file includes the headers");
30 DEFINE_bool(rawdog,
31 false,
32 "send the body exactly as is, don't fix CRLF issues "
33 "or escape leading dots");
34 DEFINE_bool(require_tls, true, "use STARTTLS or die");
35 DEFINE_bool(save, false, "save mail in .Sent");
36 DEFINE_bool(slow_strangle, false, "super slow mo");
37 DEFINE_bool(to_the_neck, false, "shove data forever");
39 DEFINE_bool(use_8bitmime, true, "use 8BITMIME extension");
40 DEFINE_bool(use_binarymime, true, "use BINARYMIME extension");
41 DEFINE_bool(use_chunking, true, "use CHUNKING extension");
42 DEFINE_bool(use_deliverby, false, "use DELIVERBY extension");
43 DEFINE_bool(use_esmtp, true, "use ESMTP (EHLO)");
44 DEFINE_bool(use_pipelining, true, "use PIPELINING extension");
45 DEFINE_bool(use_prdr, true, "use PRDR extension");
46 DEFINE_bool(use_size, true, "use SIZE extension");
47 DEFINE_bool(use_smtputf8, true, "use SMTPUTF8 extension");
48 DEFINE_bool(use_tls, true, "use STARTTLS extension");
50 // To force it, set if you have UTF8 in the local part of any RFC5321
51 // address.
52 DEFINE_bool(force_smtputf8, false, "force SMTPUTF8 extension");
54 DEFINE_string(sender, "", "FQDN of sending node");
56 DEFINE_string(local_address, "", "local address to bind");
57 DEFINE_string(mx_host, "", "FQDN of receiving node");
58 DEFINE_string(service, "smtp-test", "service name");
59 DEFINE_string(client_id, "", "client name (ID) for EHLO/HELO");
61 DEFINE_string(from, "", "RFC5322 From: address");
62 DEFINE_string(from_name, "", "RFC5322 From: name");
64 DEFINE_string(to, "", "RFC5322 To: address");
65 DEFINE_string(to_name, "", "RFC5322 To: name");
67 DEFINE_string(smtp_from, "", "RFC5321 MAIL FROM address");
68 DEFINE_string(smtp_to, "", "RFC5321 RCPT TO address");
69 DEFINE_string(smtp_to2, "", "second RFC5321 RCPT TO address");
70 DEFINE_string(smtp_to3, "", "third RFC5321 RCPT TO address");
72 DEFINE_string(content_type, "", "RFC5322 Content-Type");
73 DEFINE_string(content_transfer_encoding,
74 "",
75 "RFC5322 Content-Transfer-Encoding");
77 DEFINE_string(subject, "testing one, two, three...", "RFC5322 Subject");
78 DEFINE_string(keywords, "", "RFC5322 Keywords: header");
79 DEFINE_string(references, "", "RFC5322 References: header");
80 DEFINE_string(in_reply_to, "", "RFC5322 In-Reply-To: header");
81 DEFINE_string(reply_to, "", "RFC5322 Reply-To: header");
82 DEFINE_string(reply_2, "", "Second RFC5322 Reply-To: header");
84 DEFINE_bool(4, false, "use only IP version 4");
85 DEFINE_bool(6, false, "use only IP version 6");
87 DEFINE_string(username, "", "AUTH username");
88 DEFINE_string(password, "", "AUTH password");
90 DEFINE_bool(use_dkim, true, "sign with DKIM");
91 DEFINE_bool(bogus_dkim, false, "sign with bogus DKIM");
92 DEFINE_string(selector, "ghsmtp", "DKIM selector");
93 DEFINE_string(dkim_key_file, "", "DKIM key file");
95 #include "Base64.hpp"
96 #include "DNS-fcrdns.hpp"
97 #include "DNS.hpp"
98 #include "Domain.hpp"
99 #include "IP4.hpp"
100 #include "IP6.hpp"
101 #include "Magic.hpp"
102 #include "Mailbox.hpp"
103 #include "MessageStore.hpp"
104 #include "Now.hpp"
105 #include "OpenDKIM.hpp"
106 #include "Pill.hpp"
107 #include "Sock.hpp"
108 #include "fs.hpp"
109 #include "imemstream.hpp"
110 #include "osutil.hpp"
111 #include "sa.hpp"
113 #include <algorithm>
114 #include <fstream>
115 #include <functional>
116 #include <iomanip>
117 #include <iostream>
118 #include <iterator>
119 #include <random>
120 #include <string>
121 #include <string_view>
122 #include <unordered_map>
124 #include <netdb.h>
125 #include <sys/socket.h>
126 #include <sys/types.h>
128 #include <fmt/format.h>
129 #include <fmt/ostream.h>
131 #include <boost/algorithm/string/case_conv.hpp>
133 #include <boost/iostreams/device/mapped_file.hpp>
135 #include <tao/pegtl.hpp>
136 #include <tao/pegtl/contrib/abnf.hpp>
138 using namespace tao::pegtl;
139 using namespace tao::pegtl::abnf;
141 using namespace std::string_literals;
143 namespace Config {
144 constexpr auto read_timeout = std::chrono::minutes(24 * 60);
145 constexpr auto write_timeout = std::chrono::minutes(24 * 60);
146 } // namespace Config
148 // clang-format off
150 namespace chars {
151 struct tail : range<'\x80', '\xBF'> {};
153 struct ch_1 : range<'\x00', '\x7F'> {};
155 struct ch_2 : seq<range<'\xC2', '\xDF'>, tail> {};
157 struct ch_3 : sor<seq<one<'\xE0'>, range<'\xA0', '\xBF'>, tail>,
158 seq<range<'\xE1', '\xEC'>, rep<2, tail>>,
159 seq<one<'\xED'>, range<'\x80', '\x9F'>, tail>,
160 seq<range<'\xEE', '\xEF'>, rep<2, tail>>> {};
162 struct ch_4 : sor<seq<one<'\xF0'>, range<'\x90', '\xBF'>, rep<2, tail>>,
163 seq<range<'\xF1', '\xF3'>, rep<3, tail>>,
164 seq<one<'\xF4'>, range<'\x80', '\x8F'>, rep<2, tail>>> {};
166 struct u8char : sor<ch_1, ch_2, ch_3, ch_4> {};
168 struct non_ascii : sor<ch_2, ch_3, ch_4> {};
170 struct ascii_only : seq<star<ch_1>, eof> {};
172 struct utf8_only : seq<star<u8char>, eof> {};
175 namespace RFC5322 {
177 struct VUCHAR : sor<VCHAR, chars::non_ascii> {};
179 using dot = one<'.'>;
180 using colon = one<':'>;
182 // All 7-bit ASCII except NUL (0), LF (10) and CR (13).
183 struct text_ascii : tao::pegtl::ranges<1, 9, 11, 12, 14, 127> {};
185 // Short lines of ASCII text. LF or CRLF line separators.
186 struct body_ascii : seq<star<seq<rep_max<998, text_ascii>, eol>>,
187 opt<rep_max<998, text_ascii>>, eof> {};
189 struct text_utf8 : sor<text_ascii, chars::non_ascii> {};
191 // Short lines of UTF-8 text. LF or CRLF line separators.
192 struct body_utf8 : seq<star<seq<rep_max<998, text_utf8>, eol>>,
193 opt<rep_max<998, text_utf8>>, eof> {};
195 struct FWS : seq<opt<seq<star<WSP>, eol>>, plus<WSP>> {};
197 struct qtext : sor<one<33>, tao::pegtl::ranges<35, 91, 93, 126>, chars::non_ascii> {};
199 struct quoted_pair : seq<one<'\\'>, sor<VUCHAR, WSP>> {};
201 struct atext : sor<ALPHA, DIGIT,
202 one<'!', '#',
203 '$', '%',
204 '&', '\'',
205 '*', '+',
206 '-', '/',
207 '=', '?',
208 '^', '_',
209 '`', '{',
210 '|', '}',
211 '~'>,
212 chars::non_ascii> {};
214 // ctext is ASCII not '(' or ')' or '\\'
215 struct ctext : sor<tao::pegtl::ranges<33, 39, 42, 91, 93, 126>, chars::non_ascii> {};
217 struct comment;
219 struct ccontent : sor<ctext, quoted_pair, comment> {};
221 struct comment
222 : seq<one<'('>, star<seq<opt<FWS>, ccontent>>, opt<FWS>, one<')'>> {};
224 struct CFWS : sor<seq<plus<seq<opt<FWS>, comment>, opt<FWS>>>, FWS> {};
226 struct qcontent : sor<qtext, quoted_pair> {};
228 // Corrected in errata ID: 3135
229 struct quoted_string
230 : seq<opt<CFWS>,
231 DQUOTE,
232 sor<seq<star<seq<opt<FWS>, qcontent>>, opt<FWS>>, FWS>,
233 DQUOTE,
234 opt<CFWS>> {};
236 // *([FWS] VCHAR) *WSP
237 struct unstructured : seq<star<seq<opt<FWS>, VUCHAR>>, star<WSP>> {};
239 struct atom : seq<opt<CFWS>, plus<atext>, opt<CFWS>> {};
241 struct dot_atom_text : list<plus<atext>, dot> {};
243 struct dot_atom : seq<opt<CFWS>, dot_atom_text, opt<CFWS>> {};
245 struct word : sor<atom, quoted_string> {};
247 struct phrase : plus<word> {};
249 struct local_part : sor<dot_atom, quoted_string> {};
251 // from '!' to '~' excluding 91 92 93 '[' '\\' ']'
253 struct dtext : tao::pegtl::ranges<33, 90, 94, 126> {};
255 struct domain_literal : seq<opt<CFWS>,
256 one<'['>,
257 star<seq<opt<FWS>, dtext>>,
258 opt<FWS>,
259 one<']'>,
260 opt<CFWS>> {};
262 struct domain : sor<dot_atom, domain_literal> {};
264 struct addr_spec : seq<local_part, one<'@'>, domain> {};
266 struct postmaster : TAO_PEGTL_ISTRING("Postmaster") {};
268 struct addr_spec_or_postmaster : sor<addr_spec, postmaster> {};
270 struct addr_spec_only : seq<addr_spec_or_postmaster, eof> {};
272 struct display_name : phrase {};
274 struct display_name_only : seq<display_name, eof> {};
276 // clang-format on
278 // struct name_addr : seq<opt<display_name>, angle_addr> {};
280 // struct mailbox : sor<name_addr, addr_spec> {};
282 template <typename Rule>
283 struct inaction : nothing<Rule> {};
285 template <typename Rule>
286 struct action : nothing<Rule> {};
288 template <>
289 struct action<addr_spec> {
290 template <typename Input>
291 static void apply(Input const& in, Mailbox& mbx)
293 auto addrspec = in.string();
294 // FIXME: strip comments and unfold
295 mbx = Mailbox{addrspec};
299 } // namespace RFC5322
301 namespace RFC5321 {
303 struct Connection {
304 Sock sock;
306 std::string server_id;
308 std::string ehlo_keyword;
309 std::vector<std::string> ehlo_param;
310 std::unordered_map<std::string, std::vector<std::string>> ehlo_params;
312 std::string reply_code;
314 bool greeting_ok{false};
315 bool ehlo_ok{false};
317 bool has_extension(char const* name) const
319 return ehlo_params.find(name) != end(ehlo_params);
322 Connection(int fd_in, int fd_out, std::function<void(void)> read_hook)
323 : sock(
324 fd_in, fd_out, read_hook, Config::read_timeout, Config::write_timeout)
329 // clang-format off
331 using dot = one<'.'>;
332 using colon = one<':'>;
333 using dash = one<'-'>;
334 using underscore = one<'_'>;
336 struct u_let_dig : sor<ALPHA, DIGIT, chars::non_ascii> {};
338 struct u_ldh_tail : star<sor<seq<plus<one<'-'>>, u_let_dig>, u_let_dig>> {};
340 struct u_label : seq<u_let_dig, u_ldh_tail> {};
342 struct let_dig : sor<ALPHA, DIGIT> {};
344 struct ldh_tail : star<sor<seq<plus<one<'-'>>, let_dig>, let_dig>> {};
346 struct ldh_str : seq<let_dig, ldh_tail> {};
348 struct label : ldh_str {};
350 struct sub_domain : sor<label, u_label> {};
352 struct domain : list<sub_domain, dot> {};
354 struct dec_octet : sor<seq<string<'2','5'>, range<'0','5'>>,
355 seq<one<'2'>, range<'0','4'>, DIGIT>,
356 seq<range<'0', '1'>, rep<2, DIGIT>>,
357 rep_min_max<1, 2, DIGIT>> {};
359 struct IPv4_address_literal
360 : seq<dec_octet, dot, dec_octet, dot, dec_octet, dot, dec_octet> {};
362 struct h16 : rep_min_max<1, 4, HEXDIG> {};
364 struct ls32 : sor<seq<h16, colon, h16>, IPv4_address_literal> {};
366 struct dcolon : two<':'> {};
368 struct IPv6address : sor<seq< rep<6, h16, colon>, ls32>,
369 seq< dcolon, rep<5, h16, colon>, ls32>,
370 seq<opt<h16 >, dcolon, rep<4, h16, colon>, ls32>,
371 seq<opt<h16, opt< colon, h16>>, dcolon, rep<3, h16, colon>, ls32>,
372 seq<opt<h16, rep_opt<2, colon, h16>>, dcolon, rep<2, h16, colon>, ls32>,
373 seq<opt<h16, rep_opt<3, colon, h16>>, dcolon, h16, colon, ls32>,
374 seq<opt<h16, rep_opt<4, colon, h16>>, dcolon, ls32>,
375 seq<opt<h16, rep_opt<5, colon, h16>>, dcolon, h16>,
376 seq<opt<h16, rep_opt<6, colon, h16>>, dcolon >> {};
378 struct IPv6_address_literal : seq<TAO_PEGTL_ISTRING("IPv6:"), IPv6address> {};
380 struct dcontent : tao::pegtl::ranges<33, 90, 94, 126> {};
382 struct standardized_tag : ldh_str {};
384 struct general_address_literal : seq<standardized_tag, colon, plus<dcontent>> {};
386 // See rfc 5321 Section 4.1.3
387 struct address_literal : seq<one<'['>,
388 sor<IPv4_address_literal,
389 IPv6_address_literal,
390 general_address_literal>,
391 one<']'>> {};
394 struct qtextSMTP : sor<tao::pegtl::ranges<32, 33, 35, 91, 93, 126>, chars::non_ascii> {};
395 struct graphic : range<32, 126> {};
396 struct quoted_pairSMTP : seq<one<'\\'>, graphic> {};
397 struct qcontentSMTP : sor<qtextSMTP, quoted_pairSMTP> {};
399 // excluded from atext: "(),.@[]"
400 struct atext : sor<ALPHA, DIGIT,
401 one<'!', '#',
402 '$', '%',
403 '&', '\'',
404 '*', '+',
405 '-', '/',
406 '=', '?',
407 '^', '_',
408 '`', '{',
409 '|', '}',
410 '~'>,
411 chars::non_ascii> {};
412 struct atom : plus<atext> {};
413 struct dot_string : list<atom, dot> {};
414 struct quoted_string : seq<one<'"'>, star<qcontentSMTP>, one<'"'>> {};
415 struct local_part : sor<dot_string, quoted_string> {};
416 struct non_local_part : sor<domain, address_literal> {};
417 struct mailbox : seq<local_part, one<'@'>, non_local_part> {};
419 struct at_domain : seq<one<'@'>, domain> {};
421 struct a_d_l : list<at_domain, one<','>> {};
423 struct path : seq<opt<seq<a_d_l, colon>>, mailbox> {};
425 struct postmaster : TAO_PEGTL_ISTRING("Postmaster") {};
427 struct path_or_postmaster : seq<sor<path, postmaster>, 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" [ SP 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<seq<SP, 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<tao::pegtl::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> {};
532 template <typename Rule>
533 struct action : nothing<Rule> {};
535 template <>
536 struct action<server_id> {
537 template <typename Input>
538 static void apply(Input const& in, Connection& cnn)
540 cnn.server_id = in.string();
544 template <>
545 struct action<mailbox> {
546 template <typename Input>
547 static void apply(Input const& in, Mailbox& mbx)
549 mbx = Mailbox{in.string()};
553 template <>
554 struct action<greeting_ok> {
555 template <typename Input>
556 static void apply(Input const& in, Connection& cnn)
558 cnn.greeting_ok = true;
559 imemstream stream{begin(in), size(in)};
560 std::string line;
561 while (std::getline(stream, line)) {
562 LOG(INFO) << " S: " << line;
567 template <>
568 struct action<ehlo_ok_rsp> {
569 template <typename Input>
570 static void apply(Input const& in, Connection& cnn)
572 cnn.ehlo_ok = true;
573 imemstream stream{begin(in), size(in)};
574 std::string line;
575 while (std::getline(stream, line)) {
576 LOG(INFO) << " S: " << line;
581 template <>
582 struct action<ehlo_keyword> {
583 template <typename Input>
584 static void apply(Input const& in, Connection& cnn)
586 cnn.ehlo_keyword = in.string();
587 boost::to_upper(cnn.ehlo_keyword);
591 template <>
592 struct action<ehlo_param> {
593 template <typename Input>
594 static void apply(Input const& in, Connection& cnn)
596 cnn.ehlo_param.push_back(in.string());
600 template <>
601 struct action<ehlo_line> {
602 template <typename Input>
603 static void apply(Input const& in, Connection& cnn)
605 cnn.ehlo_params.emplace(std::move(cnn.ehlo_keyword),
606 std::move(cnn.ehlo_param));
610 template <>
611 struct action<reply_lines> {
612 template <typename Input>
613 static void apply(Input const& in, Connection& cnn)
615 imemstream stream{begin(in), size(in)};
616 std::string line;
617 while (std::getline(stream, line)) {
618 LOG(INFO) << " S: " << line;
623 template <>
624 struct action<reply_code> {
625 template <typename Input>
626 static void apply(Input const& in, Connection& cnn)
628 cnn.reply_code = in.string();
631 } // namespace RFC5321
633 namespace {
635 int conn(DNS::Resolver& res, Domain const& node, uint16_t port)
637 auto const use_4{!FLAGS_6};
638 auto const use_6{!FLAGS_4};
640 if (use_6) {
641 auto const fd{socket(AF_INET6, SOCK_STREAM, 0)};
642 PCHECK(fd >= 0) << "socket() failed";
644 if (!FLAGS_local_address.empty()) {
645 auto loc{sockaddr_in6{}};
646 loc.sin6_family = AF_INET6;
647 if (1 != inet_pton(AF_INET6, FLAGS_local_address.c_str(),
648 reinterpret_cast<void*>(&loc.sin6_addr))) {
649 LOG(FATAL) << "can't interpret " << FLAGS_local_address
650 << " as IPv6 address";
652 PCHECK(0 == bind(fd, reinterpret_cast<sockaddr*>(&loc), sizeof(loc)));
655 auto addrs{std::vector<std::string>{}};
657 if (node.is_address_literal()) {
658 if (IP6::is_address(node.ascii())) {
659 addrs.push_back(node.ascii());
661 if (IP6::is_address_literal(node.ascii())) {
662 auto const addr = IP6::as_address(node.ascii());
663 addrs.push_back(std::string(addr.data(), addr.length()));
666 else {
667 addrs = res.get_strings(DNS::RR_type::AAAA, node.ascii());
669 for (auto const& addr : addrs) {
670 auto in6{sockaddr_in6{}};
671 in6.sin6_family = AF_INET6;
672 in6.sin6_port = htons(port);
673 CHECK_EQ(inet_pton(AF_INET6, addr.c_str(),
674 reinterpret_cast<void*>(&in6.sin6_addr)),
676 if (connect(fd, reinterpret_cast<const sockaddr*>(&in6), sizeof(in6))) {
677 PLOG(WARNING) << "connect failed [" << addr << "]:" << port;
678 continue;
681 LOG(INFO) << fd << " connected to [" << addr << "]:" << port;
682 return fd;
685 close(fd);
687 if (use_4) {
688 auto fd{socket(AF_INET, SOCK_STREAM, 0)};
689 PCHECK(fd >= 0) << "socket() failed";
691 if (!FLAGS_local_address.empty()) {
692 auto loc{sockaddr_in{}};
693 loc.sin_family = AF_INET;
694 if (1 != inet_pton(AF_INET, FLAGS_local_address.c_str(),
695 reinterpret_cast<void*>(&loc.sin_addr))) {
696 LOG(FATAL) << "can't interpret " << FLAGS_local_address
697 << " as IPv4 address";
699 LOG(INFO) << "bind " << FLAGS_local_address;
700 PCHECK(0 == bind(fd, reinterpret_cast<sockaddr*>(&loc), sizeof(loc)));
703 auto addrs{std::vector<std::string>{}};
704 if (node.is_address_literal()) {
705 if (IP4::is_address(node.ascii())) {
706 addrs.push_back(node.ascii());
708 if (IP4::is_address_literal(node.ascii())) {
709 auto const addr = IP4::as_address(node.ascii());
710 addrs.push_back(std::string(addr.data(), addr.length()));
713 else {
714 addrs = res.get_strings(DNS::RR_type::A, node.ascii());
716 for (auto const& addr : addrs) {
717 auto in4{sockaddr_in{}};
718 in4.sin_family = AF_INET;
719 in4.sin_port = htons(port);
720 CHECK_EQ(inet_pton(AF_INET, addr.c_str(),
721 reinterpret_cast<void*>(&in4.sin_addr)),
723 if (connect(fd, reinterpret_cast<const sockaddr*>(&in4), sizeof(in4))) {
724 PLOG(WARNING) << "connect failed " << addr << ":" << port;
725 continue;
728 LOG(INFO) << " connected to " << addr << ":" << port;
729 return fd;
732 close(fd);
735 return -1;
738 class Eml {
739 public:
740 void add_hdr(std::string name, std::string value)
742 assert(!name.starts_with("."s)); // Would mess up "dot" stuffing.
743 hdrs_.push_back(std::make_pair(name, value));
746 void foreach_hdr(std::function<void(std::string const& name,
747 std::string const& value)> func)
749 for (auto const& [name, value] : hdrs_) {
750 func(name, value);
754 private:
755 std::vector<std::pair<std::string, std::string>> hdrs_;
757 friend std::ostream& operator<<(std::ostream& os, Eml const& eml)
759 for (auto const& [name, value] : eml.hdrs_) {
760 os << name << ": " << value << "\r\n";
762 // return os << "\r\n"; // end of headers
763 return os /* << "\r\n" */; // end of headers
767 // // clang-format off
768 // char const* const signhdrs[] = {
769 // "From",
771 // "Message-ID",
773 // "Cc",
774 // "Date",
775 // "In-Reply-To",
776 // "References",
777 // "Reply-To",
778 // "Sender",
779 // "Subject",
780 // "To",
782 // "MIME-Version",
783 // "Content-Type",
784 // "Content-Transfer-Encoding",
786 // nullptr
787 // };
788 // clang-format on
790 enum class transfer_encoding {
791 seven_bit,
792 quoted_printable,
793 base64,
794 eight_bit,
795 binary,
798 enum class data_type {
799 ascii, // 7bit, quoted-printable and base64
800 utf8, // 8bit
801 binary, // binary
804 data_type type(std::string_view d)
807 auto in{memory_input<>{d.data(), d.size(), "data"}};
808 if (parse<RFC5322::body_ascii>(in)) {
809 return data_type::ascii;
813 auto in{memory_input<>{d.data(), d.size(), "data"}};
814 if (parse<RFC5322::body_utf8>(in)) {
815 return data_type::utf8;
818 // anything else is
819 return data_type::binary;
822 class content {
823 public:
824 content(char const* path)
825 : path_(path)
827 auto const body_sz{fs::file_size(path_)};
828 CHECK(body_sz) << "no body";
829 file_.open(path_);
830 type_ = ::type(*this);
833 char const* data() const { return file_.data(); }
834 size_t size() const { return file_.size(); }
835 data_type type() const { return type_; }
837 bool empty() const { return size() == 0; }
838 operator std::string_view() const { return std::string_view(data(), size()); }
840 private:
841 data_type type_;
842 fs::path path_;
843 boost::iostreams::mapped_file_source file_;
846 template <typename Input>
847 void fail(Input& in, RFC5321::Connection& cnn)
849 LOG(INFO) << " C: QUIT";
850 cnn.sock.out() << "QUIT\r\n" << std::flush;
851 // we might have a few error replies stacked up if we're pipelining
852 // CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
853 exit(EXIT_FAILURE);
856 template <typename Input>
857 void quit_on_fail(Input& in, RFC5321::Connection& cnn, std::string_view cmd)
859 cnn.sock.out() << std::flush;
860 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
861 if (cnn.reply_code.at(0) != '2') {
862 LOG(ERROR) << cmd << " returned " << cnn.reply_code;
863 fail(in, cnn);
865 in.discard();
868 bool validate_name(const char* flagname, std::string const& value)
870 if (value.empty()) // empty name needs to validate, but
871 return true; // will not be used
872 memory_input<> name_in(value.c_str(), "name");
873 if (!parse<RFC5322::display_name_only, RFC5322::inaction>(name_in)) {
874 LOG(ERROR) << "bad name syntax " << value;
875 return false;
877 return true;
880 DEFINE_validator(from_name, &validate_name);
881 DEFINE_validator(to_name, &validate_name);
883 bool validate_address_RFC5322(const char* flagname, std::string const& value)
885 if (value.empty()) // empty name needs to validate, but
886 return true; // will not be used
887 memory_input<> name_in(value.c_str(), "address");
888 if (!parse<RFC5322::addr_spec_only, RFC5322::inaction>(name_in)) {
889 LOG(ERROR) << "bad address syntax " << value;
890 return false;
892 return true;
895 DEFINE_validator(from, &validate_address_RFC5322);
896 DEFINE_validator(to, &validate_address_RFC5322);
898 bool validate_address_RFC5321(const char* flagname, std::string const& value)
900 if (value.empty()) { // empty name needs to validate, but
901 return true; // will not be used
903 memory_input<> name_in(value.c_str(), "path");
904 if (!parse<RFC5321::path_or_postmaster, RFC5321::inaction>(name_in)) {
905 LOG(ERROR) << "bad RFC-5321 address syntax " << value;
906 return false;
908 return true;
911 DEFINE_validator(smtp_from, &validate_address_RFC5321);
912 DEFINE_validator(smtp_to, &validate_address_RFC5321);
913 DEFINE_validator(smtp_to2, &validate_address_RFC5321);
914 DEFINE_validator(smtp_to3, &validate_address_RFC5321);
916 void selftest()
918 CHECK(validate_name("selftest", ""s));
919 CHECK(validate_name("selftest", "Elmer J Fudd"s));
920 CHECK(validate_name("selftest", "\"Elmer J. Fudd\""s));
921 CHECK(validate_name("selftest", "Elmer! J! Fudd!"s));
923 CHECK(validate_address_RFC5321("selftest", "foo@digilicious.com"s));
924 CHECK(validate_address_RFC5321("selftest", "\"foo\"@digilicious.com"s));
925 CHECK(validate_address_RFC5321(
926 "selftest",
927 "\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@digilicious.com"s));
929 CHECK(validate_address_RFC5322("selftest", "foo@digilicious.com"s));
930 CHECK(validate_address_RFC5322("selftest", "\"foo\"@digilicious.com"s));
931 CHECK(validate_address_RFC5322(
932 "selftest",
933 "\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@digilicious.com"s));
935 auto const read_hook{[]() {}};
937 const char* greet_list[]{
938 "220-mtaig-aak03.mx.aol.com ESMTP Internet Inbound\r\n"
939 "220-AOL and its affiliated companies do not\r\n"
940 "220-authorize the use of its proprietary computers and computer\r\n"
941 "220-networks to accept, transmit, or distribute unsolicited bulk\r\n"
942 "220-e-mail sent from the internet.\r\n"
943 "220-Effective immediately:\r\n"
944 "220-AOL may no longer accept connections from IP addresses\r\n"
945 "220 which no do not have reverse-DNS (PTR records) assigned.\r\n",
947 "421 mtaig-maa02.mx.aol.com Service unavailable - try again later\r\n",
950 for (auto const& i : greet_list) {
951 auto cnn{RFC5321::Connection(0, 1, read_hook)};
952 auto in{memory_input<>{i, i}};
953 if (!parse<RFC5321::greeting, RFC5321::action /*, tao::pegtl::tracer*/>(
954 in, cnn)) {
955 LOG(FATAL) << "Error parsing greeting \"" << i << "\"";
957 if (cnn.greeting_ok) {
958 LOG(WARNING) << "greeting ok";
960 else {
961 LOG(WARNING) << "greeting was not in the affirmative";
965 const char* ehlo_rsp_list[]{
966 "250-www77.totaalholding.nl Hello "
967 "ec2-18-205-224-193.compute-1.amazonaws.com [18.205.224.193]\r\n"
968 "250-SIZE 52428800\r\n"
969 "250-8BITMIME\r\n"
970 "250-PIPELINING\r\n"
971 "250-X_PIPE_CONNECT\r\n"
972 "250-STARTTLS\r\n"
973 "250 HELP\r\n",
975 "250-HELLO, SAILOR!\r\n"
976 "250-NO-SOLICITING\r\n"
977 "250 8BITMIME\r\n",
979 "250-digilicious.com at your service, localhost. [IPv6:::1]\r\n"
980 "250-SIZE 15728640\r\n"
981 "250-8BITMIME\r\n"
982 "250-STARTTLS\r\n"
983 "250-ENHANCEDSTATUSCODES\r\n"
984 "250-PIPELINING\r\n"
985 "250-BINARYMIME\r\n"
986 "250-CHUNKING\r\n"
987 "250-SMTPUTF8\r\n"
988 "250 OK\r\n",
990 "500 5.5.1 command unrecognized: \"EHLO digilicious.com\\r\\n\"\r\n",
992 "250-263xmail at your service\r\n"
993 "250-STARTTLS\r\n"
994 "250-MAE-SMTP\r\n"
995 "250-263.net\r\n" // the '.' is not RFC complaint
996 "250-SIZE 104857600\r\n"
997 "250-ETRN\r\n"
998 "250-ENHANCEDSTATUSCODES\r\n"
999 "250-8BITMIME\r\n"
1000 "250 DSN\r\n",
1003 for (auto const& i : ehlo_rsp_list) {
1004 auto cnn{RFC5321::Connection(0, 1, read_hook)};
1005 auto in{memory_input<>{i, i}};
1006 if (!parse<RFC5321::ehlo_rsp, RFC5321::action /*, tao::pegtl::tracer*/>(
1007 in, cnn)) {
1008 LOG(FATAL) << "Error parsing ehlo response \"" << i << "\"";
1010 if (cnn.ehlo_ok) {
1011 LOG(WARNING) << "ehlo ok";
1013 else {
1014 LOG(WARNING) << "ehlo response was not in the affirmative";
1019 auto get_sender()
1021 if (FLAGS_client_id.empty()) {
1022 FLAGS_client_id = [] {
1023 auto const id_from_env{getenv("GHSMTP_CLIENT_ID")};
1024 if (id_from_env)
1025 return std::string{id_from_env};
1027 auto const hostname{osutil::get_hostname()};
1028 if (hostname.find('.') != std::string::npos)
1029 return hostname;
1031 LOG(FATAL) << "hostname not a FQDN, set GHSMTP_CLIENT_ID maybe?";
1032 }();
1035 if (FLAGS_sender.empty()) {
1036 FLAGS_sender = FLAGS_client_id;
1039 auto const sender{Domain{FLAGS_sender}};
1041 if (FLAGS_from.empty()) {
1042 FLAGS_from = "test-it@"s + sender.utf8();
1045 if (FLAGS_to.empty()) {
1046 FLAGS_to = "test-it@"s + sender.utf8();
1049 return sender;
1052 bool is_localhost(DNS::RR const& rr)
1054 if (std::holds_alternative<DNS::RR_MX>(rr)) {
1055 if (iequal(std::get<DNS::RR_MX>(rr).exchange(), "localhost"))
1056 return true;
1058 return false;
1061 bool starts_with(std::string_view str, std::string_view prefix)
1063 if (str.size() >= prefix.size())
1064 if (str.compare(0, prefix.size(), prefix) == 0)
1065 return true;
1066 return false;
1069 bool sts_rec(std::string const& sts_rec)
1071 return starts_with(sts_rec, "v=STSv1");
1074 std::vector<Domain>
1075 get_receivers(DNS::Resolver& res, Mailbox const& to_mbx, bool& enforce_dane)
1077 auto receivers{std::vector<Domain>{}};
1079 // User provided explicit host to receive mail.
1080 if (!FLAGS_mx_host.empty()) {
1081 receivers.emplace_back(FLAGS_mx_host);
1082 return receivers;
1085 // Non-local part is an address literal.
1086 if (to_mbx.domain().is_address_literal()) {
1087 receivers.emplace_back(to_mbx.domain());
1088 return receivers;
1091 // RFC 5321 section 5.1 "Locating the Target Host"
1093 // “The lookup first attempts to locate an MX record associated with
1094 // the name. If a CNAME record is found, the resulting name is
1095 // processed as if it were the initial name.”
1097 // Our (full) resolver will traverse any CNAMEs for us and return
1098 // the CNAME and MX records all together.
1100 auto const& domain = to_mbx.domain().ascii();
1102 auto q_sts{DNS::Query{res, DNS::RR_type::TXT, "_mta-sts."s + domain}};
1103 if (q_sts.has_record()) {
1104 auto sts_records = q_sts.get_strings();
1105 sts_records.erase(std::remove_if(begin(sts_records), end(sts_records),
1106 std::not_fn(sts_rec)),
1107 end(sts_records));
1108 if (size(sts_records) == 1) {
1109 LOG(INFO) << "### This domain implements MTA-STS ###";
1112 else {
1113 LOG(INFO) << "MTA-STS record not found for domain " << domain;
1116 auto q{DNS::Query{res, DNS::RR_type::MX, domain}};
1117 if (q.has_record()) {
1118 if (q.authentic_data()) {
1119 LOG(INFO) << "### MX records authentic for domain " << domain << " ###";
1121 else {
1122 LOG(INFO) << "MX records can't be authenticated for domain " << domain;
1123 enforce_dane = false;
1126 else {
1127 LOG(INFO) << "no MX records found for domain " << domain;
1129 auto mxs{q.get_records()};
1131 mxs.erase(std::remove_if(begin(mxs), end(mxs), is_localhost), end(mxs));
1133 auto const nmx = std::count_if(begin(mxs), end(mxs), [](auto const& rr) {
1134 return std::holds_alternative<DNS::RR_MX>(rr);
1137 if (nmx == 1) {
1138 for (auto const& mx : mxs) {
1139 if (std::holds_alternative<DNS::RR_MX>(mx)) {
1140 // RFC 7505 null MX record
1141 if ((std::get<DNS::RR_MX>(mx).preference() == 0) &&
1142 (std::get<DNS::RR_MX>(mx).exchange().empty() ||
1143 (std::get<DNS::RR_MX>(mx).exchange() == "."))) {
1144 LOG(INFO) << "domain " << domain << " does not accept mail";
1145 return receivers;
1151 if (nmx == 0) {
1152 // implicit MX RR
1153 receivers.emplace_back(domain);
1154 return receivers;
1157 // […] then the sender-SMTP MUST randomize them to spread the load
1158 // across multiple mail exchangers for a specific organization.
1159 std::shuffle(begin(mxs), end(mxs), std::random_device());
1160 std::sort(begin(mxs), end(mxs), [](auto const& a, auto const& b) {
1161 if (std::holds_alternative<DNS::RR_MX>(a) &&
1162 std::holds_alternative<DNS::RR_MX>(b)) {
1163 return std::get<DNS::RR_MX>(a).preference() <
1164 std::get<DNS::RR_MX>(b).preference();
1166 return false;
1169 if (nmx)
1170 LOG(INFO) << "MXs for " << domain << " are:";
1172 for (auto const& mx : mxs) {
1173 if (std::holds_alternative<DNS::RR_MX>(mx)) {
1174 receivers.emplace_back(std::get<DNS::RR_MX>(mx).exchange());
1175 LOG(INFO) << std::setfill(' ') << std::setw(3)
1176 << std::get<DNS::RR_MX>(mx).preference() << " "
1177 << std::get<DNS::RR_MX>(mx).exchange();
1181 return receivers;
1184 auto parse_mailboxes()
1186 auto from_mbx{Mailbox{}};
1187 auto from_in{memory_input<>{FLAGS_from, "from"}};
1188 if (!parse<RFC5322::addr_spec_only, RFC5322::action>(from_in, from_mbx)) {
1189 LOG(FATAL) << "bad From: address syntax <" << FLAGS_from << ">";
1191 LOG(INFO) << " from_mbx == " << from_mbx;
1193 auto local_from{memory_input<>{from_mbx.local_part(), "from.local"}};
1194 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_from);
1196 auto to_mbx{Mailbox{}};
1197 auto to_in{memory_input<>{FLAGS_to, "to"}};
1198 if (!parse<RFC5322::addr_spec_only, RFC5322::action>(to_in, to_mbx)) {
1199 LOG(FATAL) << "bad To: address syntax <" << FLAGS_to << ">";
1201 LOG(INFO) << " to_mbx == " << to_mbx;
1203 auto local_to{memory_input<>{to_mbx.local_part(), "to.local"}};
1204 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_to);
1206 auto smtp_from_mbx{Mailbox{}};
1207 if (!FLAGS_smtp_from.empty()) {
1208 auto smtp_from_in{memory_input<>{FLAGS_smtp_from, "SMTP.from"}};
1209 if (!parse<RFC5321::path_or_postmaster, RFC5321::action>(smtp_from_in,
1210 smtp_from_mbx)) {
1211 LOG(FATAL) << "bad MAIL FROM: address syntax <" << FLAGS_smtp_from << ">";
1213 LOG(INFO) << " smtp_from_mbx == " << smtp_from_mbx;
1214 auto local_smtp_from{
1215 memory_input<>{smtp_from_mbx.local_part(), "SMTP.from.local"}};
1216 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_smtp_from);
1218 else {
1219 smtp_from_mbx = from_mbx;
1222 auto smtp_to_mbx{Mailbox{}};
1223 if (!FLAGS_smtp_to.empty()) {
1224 auto smtp_to_in{memory_input<>{FLAGS_smtp_to, "SMTP.to"}};
1225 if (!parse<RFC5321::path_or_postmaster, RFC5321::action>(smtp_to_in,
1226 smtp_to_mbx)) {
1227 LOG(FATAL) << "bad RCPT TO: address syntax <" << FLAGS_smtp_to << ">";
1229 LOG(INFO) << " smtp_to_mbx == " << smtp_to_mbx;
1231 auto local_smtp_to{
1232 memory_input<>{smtp_to_mbx.local_part(), "SMTP.to.local"}};
1233 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_smtp_to);
1235 else {
1236 smtp_to_mbx = to_mbx;
1239 auto smtp_to2_mbx{Mailbox{}};
1240 if (!FLAGS_smtp_to2.empty()) {
1241 auto smtp_to2_in{memory_input<>{FLAGS_smtp_to2, "SMTP.to"}};
1242 if (!parse<RFC5321::path_or_postmaster, RFC5321::action>(smtp_to2_in,
1243 smtp_to2_mbx)) {
1244 LOG(FATAL) << "bad RCPT TO: address syntax <" << FLAGS_smtp_to2 << ">";
1246 LOG(INFO) << " smtp_to2_mbx == " << smtp_to2_mbx;
1248 auto local_smtp_to2{
1249 memory_input<>{smtp_to2_mbx.local_part(), "SMTP.to.local"}};
1250 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_smtp_to2);
1253 auto smtp_to3_mbx{Mailbox{}};
1254 if (!FLAGS_smtp_to3.empty()) {
1255 auto smtp_to3_in{memory_input<>{FLAGS_smtp_to3, "SMTP.to"}};
1256 if (!parse<RFC5321::path_or_postmaster, RFC5321::action>(smtp_to3_in,
1257 smtp_to3_mbx)) {
1258 LOG(FATAL) << "bad RCPT TO: address syntax <" << FLAGS_smtp_to3 << ">";
1260 LOG(INFO) << " smtp_to3_mbx == " << smtp_to3_mbx;
1262 auto local_smtp_to3{
1263 memory_input<>{smtp_to3_mbx.local_part(), "SMTP.to.local"}};
1264 FLAGS_force_smtputf8 |= !parse<chars::ascii_only>(local_smtp_to3);
1267 return std::tuple(from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx, smtp_to2_mbx,
1268 smtp_to3_mbx);
1271 auto create_eml(Domain const& sender,
1272 std::string const& from,
1273 std::string const& to,
1274 std::vector<content> const& bodies,
1275 bool ext_smtputf8)
1277 auto eml{Eml{}};
1278 auto const date{Now{}};
1279 auto const pill{Pill{}};
1281 auto mid_str = fmt::format("<{}.{}@{}>", date.sec(), pill.as_string_view(),
1282 sender.ascii());
1283 eml.add_hdr("Message-ID", mid_str.c_str());
1284 eml.add_hdr("Date", date.c_str());
1286 if (!FLAGS_from_name.empty())
1287 eml.add_hdr("From", fmt::format("{} <{}>", FLAGS_from_name, from));
1288 else
1289 eml.add_hdr("From", from);
1291 eml.add_hdr("Subject", FLAGS_subject);
1293 if (!FLAGS_to_name.empty())
1294 eml.add_hdr("To", fmt::format("{} <{}>", FLAGS_to_name, to));
1295 else
1296 eml.add_hdr("To", to);
1298 if (!FLAGS_keywords.empty())
1299 eml.add_hdr("Keywords", FLAGS_keywords);
1301 if (!FLAGS_references.empty())
1302 eml.add_hdr("References", FLAGS_references);
1304 if (!FLAGS_in_reply_to.empty())
1305 eml.add_hdr("In-Reply-To", FLAGS_in_reply_to);
1307 if (!FLAGS_reply_to.empty())
1308 eml.add_hdr("Reply-To", FLAGS_reply_to);
1310 if (!FLAGS_reply_2.empty())
1311 eml.add_hdr("Reply-To", FLAGS_reply_2);
1313 eml.add_hdr("MIME-Version", "1.0");
1314 eml.add_hdr("Content-Language", "en-US");
1316 auto magic{Magic{}}; // to ID buffer contents
1318 if (!FLAGS_content_type.empty()) {
1319 eml.add_hdr("Content-Type", FLAGS_content_type);
1321 else {
1322 eml.add_hdr("Content-Type", magic.buffer(bodies[0]));
1325 if (!FLAGS_content_transfer_encoding.empty()) {
1326 eml.add_hdr("Content-Transfer-Encoding", FLAGS_content_transfer_encoding);
1329 return eml;
1332 void sign_eml(Eml& eml,
1333 std::string const& from_dom,
1334 std::vector<content> const& bodies)
1336 auto const body_type = (bodies[0].type() == data_type::binary)
1337 ? OpenDKIM::sign::body_type::binary
1338 : OpenDKIM::sign::body_type::text;
1340 auto const key_file = FLAGS_dkim_key_file.empty()
1341 ? (FLAGS_selector + ".private")
1342 : FLAGS_dkim_key_file;
1343 std::ifstream keyfs(key_file.c_str());
1344 CHECK(keyfs.good()) << "can't access " << key_file;
1345 std::string key(std::istreambuf_iterator<char>{keyfs}, {});
1346 OpenDKIM::sign dks(key.c_str(), FLAGS_selector.c_str(), from_dom.c_str(),
1347 body_type);
1348 eml.foreach_hdr([&dks](std::string const& name, std::string const& value) {
1349 auto const header = name + ": "s + value;
1350 dks.header(header.c_str());
1352 dks.eoh();
1353 for (auto const& body : bodies) {
1354 dks.body(body);
1356 dks.eom();
1357 eml.add_hdr("DKIM-Signature"s, dks.getsighdr());
1360 template <typename Input>
1361 void do_auth(Input& in, RFC5321::Connection& cnn)
1363 if (FLAGS_username.empty() && FLAGS_password.empty())
1364 return;
1366 auto const auth = cnn.ehlo_params.find("AUTH");
1367 if (auth == end(cnn.ehlo_params)) {
1368 LOG(ERROR) << "server doesn't support AUTH";
1369 fail(in, cnn);
1372 // Perfer PLAIN mechanism.
1373 if (std::find(cbegin(auth->second), cend(auth->second), "PLAIN") !=
1374 cend(auth->second)) {
1375 LOG(INFO) << "C: AUTH PLAIN";
1376 auto const tok = fmt::format("\0{}\0{}", FLAGS_username, FLAGS_password);
1377 cnn.sock.out() << "AUTH PLAIN " << Base64::enc(tok) << "\r\n" << std::flush;
1378 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1379 if (cnn.reply_code != "235") {
1380 LOG(ERROR) << "AUTH PLAIN returned " << cnn.reply_code;
1381 fail(in, cnn);
1384 // The LOGIN SASL mechanism is obsolete.
1385 else if (std::find(begin(auth->second), end(auth->second), "LOGIN") !=
1386 end(auth->second)) {
1387 LOG(INFO) << "C: AUTH LOGIN";
1388 cnn.sock.out() << "AUTH LOGIN\r\n" << std::flush;
1389 CHECK((parse<RFC5321::auth_login_username>(in)));
1390 cnn.sock.out() << Base64::enc(FLAGS_username) << "\r\n" << std::flush;
1391 CHECK((parse<RFC5321::auth_login_password>(in)));
1392 cnn.sock.out() << Base64::enc(FLAGS_password) << "\r\n" << std::flush;
1393 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1394 if (cnn.reply_code != "235") {
1395 LOG(ERROR) << "AUTH LOGIN returned " << cnn.reply_code;
1396 fail(in, cnn);
1399 else {
1400 LOG(ERROR) << "server doesn't support AUTH methods PLAIN or LOGIN";
1401 fail(in, cnn);
1405 // Do various bad things during the DATA transfer.
1407 template <typename Input>
1408 void bad_daddy(Input& in, RFC5321::Connection& cnn)
1410 LOG(INFO) << "C: DATA";
1411 cnn.sock.out() << "DATA\r\n";
1412 cnn.sock.out() << std::flush;
1414 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1415 if (cnn.reply_code != "354") {
1416 LOG(ERROR) << "DATA returned " << cnn.reply_code;
1417 fail(in, cnn);
1420 // cnn.sock.out() << "\r\nThis ->\n<- is a bare LF!\r\n";
1421 if (FLAGS_bare_lf)
1422 cnn.sock.out() << "\n.\n\r\n";
1424 if (FLAGS_to_the_neck) {
1425 for (;;) {
1426 cnn.sock.out() << "####################"
1427 "####################"
1428 "####################"
1429 << std::flush;
1433 if (FLAGS_long_line) {
1434 for (auto i{0}; i < 10000; ++i) {
1435 cnn.sock.out() << 'X';
1437 cnn.sock.out() << "\r\n" << std::flush;
1440 while (FLAGS_slow_strangle) {
1441 for (auto i{0}; i < 500; ++i) {
1442 cnn.sock.out() << 'X' << std::flush;
1443 sleep(1);
1445 cnn.sock.out() << "\r\n";
1448 // Done!
1449 cnn.sock.out() << ".\r\n" << std::flush;
1450 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1452 LOG(INFO) << "reply_code == " << cnn.reply_code;
1453 CHECK_EQ(cnn.reply_code.at(0), '2');
1455 LOG(INFO) << "C: QUIT";
1456 cnn.sock.out() << "QUIT\r\n" << std::flush;
1457 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1460 bool snd(fs::path config_path,
1461 int fd_in,
1462 int fd_out,
1463 Domain const& sender,
1464 Domain const& receiver,
1465 DNS::RR_collection const& tlsa_rrs,
1466 bool enforce_dane,
1467 Mailbox const& from_mbx,
1468 Mailbox const& to_mbx,
1469 Mailbox const& smtp_from_mbx,
1470 Mailbox const& smtp_to_mbx,
1471 Mailbox const& smtp_to2_mbx,
1472 Mailbox const& smtp_to3_mbx,
1473 std::vector<content> const& bodies)
1475 auto constexpr read_hook{[]() {}};
1477 auto cnn{RFC5321::Connection(fd_in, fd_out, read_hook)};
1479 auto in{
1480 istream_input<eol::crlf, 1>{cnn.sock.in(), FLAGS_bfr_size, "session"}};
1481 if (!parse<RFC5321::greeting, RFC5321::action>(in, cnn)) {
1482 LOG(WARNING) << "can't parse greeting";
1484 LOG(WARNING) << " us: " << cnn.sock.us_c_str();
1485 LOG(WARNING) << "them: " << cnn.sock.them_c_str();
1487 cnn.sock.log_stats();
1488 return false;
1491 if (!cnn.greeting_ok) {
1492 LOG(WARNING) << "greeting was not in the affirmative, skipping";
1493 return false;
1496 // try EHLO/HELO
1498 if (FLAGS_use_esmtp) {
1499 LOG(INFO) << "C: EHLO " << FLAGS_client_id;
1501 if (FLAGS_slow_strangle) {
1502 auto ehlo_str = fmt::format("EHLO {}\r\n", FLAGS_client_id);
1503 for (auto ch : ehlo_str) {
1504 cnn.sock.out() << ch << std::flush;
1505 sleep(1);
1508 else {
1509 cnn.sock.out() << "EHLO " << FLAGS_client_id << "\r\n" << std::flush;
1512 CHECK((parse<RFC5321::ehlo_rsp, RFC5321::action>(in, cnn)));
1513 if (!cnn.ehlo_ok) {
1515 if (FLAGS_force_smtputf8) {
1516 LOG(WARNING) << "ehlo response was not in the affirmative, skipping";
1517 return false;
1520 LOG(WARNING) << "ehlo response was not in the affirmative, trying HELO";
1521 FLAGS_use_esmtp = false;
1525 if (!FLAGS_use_esmtp) {
1526 LOG(INFO) << "C: HELO " << sender.ascii();
1527 cnn.sock.out() << "HELO " << sender.ascii() << "\r\n" << std::flush;
1528 if (!parse<RFC5321::helo_ok_rsp, RFC5321::action>(in, cnn)) {
1529 LOG(WARNING) << "HELO didn't work, skipping";
1530 return false;
1534 // Check extensions
1536 auto bad_dad = FLAGS_bare_lf || FLAGS_slow_strangle || FLAGS_to_the_neck;
1538 if (bad_dad) {
1539 FLAGS_use_chunking = false;
1540 FLAGS_use_size = false;
1543 auto const ext_8bitmime{FLAGS_use_8bitmime && cnn.has_extension("8BITMIME")};
1545 auto const ext_chunking{FLAGS_use_chunking && cnn.has_extension("CHUNKING")};
1547 auto const ext_binarymime{FLAGS_use_binarymime && ext_chunking &&
1548 cnn.has_extension("BINARYMIME")};
1550 auto const ext_deliverby{FLAGS_use_deliverby &&
1551 cnn.has_extension("DELIVERBY")};
1553 auto const ext_pipelining{FLAGS_use_pipelining &&
1554 cnn.has_extension("PIPELINING")};
1556 auto const ext_prdr{FLAGS_use_prdr && cnn.has_extension("PRDR")};
1558 auto const ext_size{FLAGS_use_size && cnn.has_extension("SIZE")};
1560 auto const ext_smtputf8{FLAGS_use_smtputf8 && cnn.has_extension("SMTPUTF8")};
1562 auto const ext_starttls{FLAGS_use_tls && cnn.has_extension("STARTTLS")};
1564 if (FLAGS_force_smtputf8 && !ext_smtputf8) {
1565 LOG(WARNING) << "no SMTPUTF8, skipping";
1566 return false;
1569 if (ext_starttls) {
1570 LOG(INFO) << "C: STARTTLS";
1571 cnn.sock.out() << "STARTTLS\r\n" << std::flush;
1572 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1574 LOG(INFO) << "cnn.sock.starttls_client(\"" << receiver.ascii() << "\");";
1575 cnn.sock.starttls_client(config_path, sender.ascii().c_str(),
1576 receiver.ascii().c_str(), tlsa_rrs, enforce_dane);
1578 LOG(INFO) << "TLS: " << cnn.sock.tls_info();
1580 LOG(INFO) << "C: EHLO " << FLAGS_client_id;
1581 cnn.sock.out() << "EHLO " << FLAGS_client_id << "\r\n" << std::flush;
1582 CHECK((parse<RFC5321::ehlo_rsp, RFC5321::action>(in, cnn)));
1584 else if (FLAGS_require_tls) {
1585 LOG(ERROR) << "No TLS extension, won't send mail in plain text without "
1586 "--require_tls=false.";
1587 LOG(INFO) << "C: QUIT";
1588 cnn.sock.out() << "QUIT\r\n" << std::flush;
1589 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1590 exit(EXIT_FAILURE);
1593 if (FLAGS_noop) {
1594 LOG(INFO) << "C: NOOP";
1595 cnn.sock.out() << "NOOP\r\n" << std::flush;
1598 if (receiver.ascii() != cnn.server_id) {
1599 LOG(INFO) << "server identifies as " << cnn.server_id;
1602 if (FLAGS_force_smtputf8 && !ext_smtputf8) {
1603 LOG(WARNING) << "does not support SMTPUTF8";
1604 return false;
1607 if (ext_smtputf8 && !ext_8bitmime) {
1608 LOG(ERROR)
1609 << "SMTPUTF8 requires 8BITMIME, see RFC-6531 section 3.1 item 8.";
1610 LOG(INFO) << "C: QUIT";
1611 cnn.sock.out() << "QUIT\r\n" << std::flush;
1612 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1613 exit(EXIT_FAILURE);
1616 auto max_msg_size{0u};
1617 if (ext_size) {
1618 if (!cnn.ehlo_params["SIZE"].empty()) {
1619 char* ep = nullptr;
1620 max_msg_size = strtoul(cnn.ehlo_params["SIZE"][0].c_str(), &ep, 10);
1621 if (ep && (*ep != '\0')) {
1622 LOG(WARNING) << "garbage in SIZE argument: "
1623 << cnn.ehlo_params["SIZE"][0];
1628 auto deliver_by_min{0u};
1629 if (ext_deliverby) {
1630 if (!cnn.ehlo_params["DELIVERBY"].empty()) {
1631 char* ep = nullptr;
1632 deliver_by_min =
1633 strtoul(cnn.ehlo_params["DELIVERBY"][0].c_str(), &ep, 10);
1634 if (ep && (*ep != '\0')) {
1635 LOG(WARNING) << "garbage in DELIVERBY argument: "
1636 << cnn.ehlo_params["DELIVERBY"][0];
1640 if (deliver_by_min) {
1641 LOG(INFO) << "DELIVERBY " << deliver_by_min;
1643 do_auth(in, cnn);
1645 in.discard();
1647 auto enc = FLAGS_force_smtputf8 ? Mailbox::domain_encoding::utf8
1648 : Mailbox::domain_encoding::ascii;
1650 std::string from =
1651 from_mbx.empty() ? smtp_from_mbx.as_string(enc) : from_mbx.as_string(enc);
1652 std::string to =
1653 to_mbx.empty() ? smtp_to_mbx.as_string(enc) : to_mbx.as_string(enc);
1655 auto eml{create_eml(sender, from, to, bodies, ext_smtputf8)};
1657 if (FLAGS_use_dkim) {
1658 auto const dom = Mailbox(from).domain().ascii();
1659 sign_eml(eml, dom.c_str(), bodies);
1661 else if (FLAGS_bogus_dkim) {
1662 eml.add_hdr("DKIM-Signature", "");
1665 // Get the header as one big string
1666 std::ostringstream hdr_stream;
1667 hdr_stream << eml;
1668 if (!FLAGS_rawmsg)
1669 hdr_stream << "\r\n";
1670 auto hdr_str = hdr_stream.view();
1672 // In the case of DATA style transfer, this total_size number is an
1673 // *estimate* only, as line endings may be translated or added
1674 // during transfer. In the BDAT case, this number must be exact.
1676 auto const total_size = rng::accumulate(
1677 bodies, hdr_str.size(),
1678 [](size_t s, const content& body) { return s + body.size(); });
1680 // auto total_size = hdr_str.size();
1681 // for (auto const& body : bodies)
1682 // total_size += body.size();
1684 if (ext_size && max_msg_size && (total_size > max_msg_size)) {
1685 LOG(ERROR) << "message size " << total_size << " exceeds size limit of "
1686 << max_msg_size;
1687 LOG(INFO) << "C: QUIT";
1688 cnn.sock.out() << "QUIT\r\n" << std::flush;
1689 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1690 exit(EXIT_FAILURE);
1693 std::ostringstream param_stream;
1694 if (FLAGS_huge_size && ext_size) {
1695 // Claim some huge size.
1696 param_stream << " SIZE=" << std::numeric_limits<std::streamsize>::max();
1698 else if (ext_size) {
1699 param_stream << " SIZE=" << total_size;
1702 if (ext_binarymime) {
1703 param_stream << " BODY=BINARYMIME";
1705 else if (ext_8bitmime) {
1706 param_stream << " BODY=8BITMIME";
1709 if (ext_prdr && (!smtp_to2_mbx.empty() || !smtp_to3_mbx.empty())) {
1710 param_stream << " PRDR";
1713 if (ext_deliverby) {
1714 param_stream << " BY=1200;NT";
1717 if (ext_smtputf8) {
1718 param_stream << " SMTPUTF8";
1721 if (FLAGS_badpipline) {
1722 LOG(INFO) << "C: NOOP NOOP";
1723 cnn.sock.out() << "NOOP\r\nNOOP\r\n" << std::flush;
1726 bool rcpt_to_ok = false;
1727 bool rcpt_to2_ok = false;
1728 bool rcpt_to3_ok = false;
1730 auto param_str = param_stream.str();
1732 for (auto count = 0UL; count < FLAGS_reps; ++count) {
1734 LOG(INFO) << "C: MAIL FROM:<" << smtp_from_mbx.as_string(enc) << '>'
1735 << param_str;
1736 cnn.sock.out() << "MAIL FROM:<" << smtp_from_mbx.as_string(enc) << '>'
1737 << param_str << "\r\n";
1738 if (!ext_pipelining) {
1739 quit_on_fail(in, cnn, "MAIL FROM");
1742 LOG(INFO) << "C: RCPT TO:<" << smtp_to_mbx.as_string(enc) << ">";
1743 cnn.sock.out() << "RCPT TO:<" << smtp_to_mbx.as_string(enc) << ">\r\n";
1744 if (!ext_pipelining) {
1745 // check RCPT TO #1
1746 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1747 rcpt_to_ok = cnn.reply_code.at(0) == '2';
1750 if (!smtp_to2_mbx.empty()) {
1751 LOG(INFO) << "C: RCPT TO:<" << smtp_to2_mbx.as_string(enc) << ">";
1752 cnn.sock.out() << "RCPT TO:<" << smtp_to2_mbx.as_string(enc) << ">\r\n";
1753 if (!ext_pipelining) {
1754 // check RCPT TO #2
1755 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1756 rcpt_to2_ok = cnn.reply_code.at(0) == '2';
1760 if (!smtp_to3_mbx.empty()) {
1761 LOG(INFO) << "C: RCPT TO:<" << smtp_to3_mbx.as_string(enc) << ">";
1762 cnn.sock.out() << "RCPT TO:<" << smtp_to3_mbx.as_string(enc) << ">\r\n";
1763 if (!ext_pipelining) {
1764 // check RCPT TO #3
1765 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1766 rcpt_to3_ok = cnn.reply_code.at(0) == '2';
1770 if (FLAGS_nosend) {
1771 if (ext_pipelining) {
1772 quit_on_fail(in, cnn, "MAIL FROM");
1773 if (rcpt_to_ok)
1774 quit_on_fail(in, cnn, "RCPT TO");
1775 if (rcpt_to2_ok)
1776 quit_on_fail(in, cnn, "RCPT TO");
1777 if (rcpt_to3_ok)
1778 quit_on_fail(in, cnn, "RCPT TO");
1780 LOG(INFO) << "C: QUIT";
1781 cnn.sock.out() << "QUIT\r\n" << std::flush;
1782 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1783 LOG(INFO) << "no-sending";
1784 exit(EXIT_SUCCESS);
1787 if (bad_dad) {
1788 if (ext_pipelining) {
1789 cnn.sock.out() << std::flush;
1790 quit_on_fail(in, cnn, "MAIL FROM");
1791 quit_on_fail(in, cnn, "RCPT TO");
1793 bad_daddy(in, cnn);
1794 return true;
1797 auto msg = std::make_unique<MessageStore>();
1799 // if service is smtp (i.e. sending real mail, not smtp-test)
1800 try {
1801 if (FLAGS_save) {
1802 msg->open(sender.ascii(), total_size * 2, ".Sent");
1803 msg->write(hdr_str.data(), hdr_str.size());
1804 for (auto const& body : bodies) {
1805 msg->write(body.data(), body.size());
1809 catch (std::system_error const& e) {
1810 switch (errno) {
1811 case ENOSPC:
1812 msg->trash();
1813 msg.reset();
1814 LOG(FATAL) << "no space";
1815 [[fallthrough]];
1817 default:
1818 msg->trash();
1819 msg.reset();
1820 LOG(ERROR) << "errno==" << errno << ": " << strerror(errno);
1821 LOG(FATAL) << e.what();
1824 catch (std::exception const& e) {
1825 msg->trash();
1826 msg.reset();
1827 LOG(FATAL) << e.what();
1830 if (ext_chunking) {
1832 if (ext_pipelining) {
1833 quit_on_fail(in, cnn, "MAIL FROM");
1835 // check RCPT TO #1
1836 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1837 rcpt_to_ok = cnn.reply_code.at(0) == '2';
1839 if (!smtp_to2_mbx.empty()) {
1840 // check RCPT TO #2
1841 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1842 rcpt_to2_ok = cnn.reply_code.at(0) == '2';
1845 if (!smtp_to3_mbx.empty()) {
1846 // check RCPT TO #3
1847 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1848 rcpt_to3_ok = cnn.reply_code.at(0) == '2';
1851 // If all RCPT TOs failed, we give up.
1852 if (!(rcpt_to_ok || rcpt_to2_ok || rcpt_to3_ok)) {
1853 fail(in, cnn);
1857 std::ostringstream bdat_stream;
1858 bdat_stream << "BDAT " << total_size << " LAST";
1859 LOG(INFO) << "C: " << bdat_stream.str();
1861 cnn.sock.out() << bdat_stream.str() << "\r\n";
1862 cnn.sock.out().write(hdr_str.data(), hdr_str.size());
1863 CHECK(cnn.sock.out().good());
1865 for (auto const& body : bodies) {
1866 cnn.sock.out().write(body.data(), body.size());
1867 CHECK(cnn.sock.out().good());
1870 // Done sending data
1871 if (FLAGS_pipeline_quit) {
1872 LOG(INFO) << "C: QUIT";
1873 cnn.sock.out() << "QUIT\r\n" << std::flush;
1875 else {
1876 cnn.sock.out() << std::flush;
1879 // Not CHUNKING
1880 else {
1881 LOG(INFO) << "C: DATA";
1882 cnn.sock.out() << "DATA\r\n";
1884 // Now check returns, after DATA
1885 if (ext_pipelining) {
1886 quit_on_fail(in, cnn, "MAIL FROM");
1888 // check RCPT TO #1
1889 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1890 rcpt_to_ok = cnn.reply_code.at(0) == '2';
1892 if (!smtp_to2_mbx.empty()) {
1893 // check RCPT TO #2
1894 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1895 rcpt_to2_ok = cnn.reply_code.at(0) == '2';
1898 if (!smtp_to3_mbx.empty()) {
1899 // check RCPT TO #3
1900 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1901 rcpt_to3_ok = cnn.reply_code.at(0) == '2';
1904 // If all RCPT TOs failed, we give up.
1905 if (!(rcpt_to_ok || rcpt_to2_ok || rcpt_to3_ok)) {
1906 fail(in, cnn);
1909 else {
1910 cnn.sock.out() << std::flush;
1913 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1914 if (cnn.reply_code != "354") {
1915 LOG(ERROR) << "DATA/BDAT #1 returned " << cnn.reply_code;
1916 fail(in, cnn);
1919 cnn.sock.out() << eml;
1920 if (!FLAGS_rawmsg)
1921 cnn.sock.out() << "\r\n";
1923 for (auto const& body : bodies) {
1924 auto isbody{imemstream{body.data(), body.size()}};
1925 auto line{std::string{}};
1926 for (auto lineno = 0; std::getline(isbody, line); ++lineno) {
1927 if (!cnn.sock.out().good()) {
1928 cnn.sock.log_stats();
1929 LOG(FATAL) << "output no good at line " << lineno;
1931 if (FLAGS_rawdog) {
1932 // This adds a final newline at the end of the file, if no
1933 // line ending was present.
1934 cnn.sock.out() << line << '\n';
1936 else {
1937 // "dot" stuffing:
1938 if (line.length() && (line.at(0) == '.')) {
1939 cnn.sock.out() << '.';
1942 // This code converts single LF line endings into CRLF.
1943 // This code does nothing to fix single CR characters not
1944 // part of a CRLF pair.
1946 // This loop adds a CRLF and the end of the transmission if
1947 // the file doesn't already end with one. This is a
1948 // requirement of the SMTP DATA protocol.
1950 cnn.sock.out() << line;
1951 if (line.length() && line.back() != '\r')
1952 cnn.sock.out() << '\r';
1953 cnn.sock.out() << '\n';
1957 CHECK(cnn.sock.out().good());
1959 // Done sending data
1960 if (FLAGS_pipeline_quit) {
1961 LOG(INFO) << "C: QUIT";
1962 cnn.sock.out() << ".\r\nQUIT\r\n" << std::flush;
1964 else {
1965 cnn.sock.out() << ".\r\n" << std::flush;
1969 auto success{false};
1970 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1972 // Now, either DATA or BDATs lets check replies
1973 if (ext_prdr) {
1974 if (cnn.reply_code == "353") {
1975 if (rcpt_to_ok) {
1976 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1977 success = cnn.reply_code.length() && cnn.reply_code.at(0) == '2';
1979 if (rcpt_to2_ok) {
1980 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1981 success = success &&
1982 (cnn.reply_code.length() && cnn.reply_code.at(0) == '2');
1984 if (rcpt_to3_ok) {
1985 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
1986 success = success &&
1987 (cnn.reply_code.length() && cnn.reply_code.at(0) == '2');
1990 else {
1991 // last (and useless?) response.
1992 LOG(INFO) << "DATA/BDAT #2 returned " << cnn.reply_code;
1993 success = cnn.reply_code.length() && cnn.reply_code.at(0) == '2';
1996 else {
1997 LOG(INFO) << "DATA/BDAT #3 returned " << cnn.reply_code;
1998 success = cnn.reply_code.length() && cnn.reply_code.at(0) == '2';
2001 if (success) {
2002 if (FLAGS_save) {
2003 msg->deliver();
2005 else {
2006 msg->trash();
2008 LOG(INFO) << "all mail was sent successfully";
2010 else {
2011 LOG(INFO) << "some mail was *NOT* sent successfully";
2014 in.discard();
2015 } // FLAGS_reps
2017 if (!FLAGS_pipeline_quit) {
2018 LOG(INFO) << "C: QUIT";
2019 cnn.sock.out() << "QUIT\r\n" << std::flush;
2021 CHECK((parse<RFC5321::reply_lines, RFC5321::action>(in, cnn)));
2023 return true;
2026 DNS::RR_collection
2027 get_tlsa_rrs(DNS::Resolver& res, Domain const& domain, uint16_t port)
2029 auto const tlsa = fmt::format("_{}._tcp.{}", port, domain.ascii());
2031 DNS::Query q(res, DNS::RR_type::TLSA, tlsa);
2033 if (q.nx_domain()) {
2034 LOG(INFO) << "TLSA data not found for " << domain << ':' << port;
2037 if (q.bogus_or_indeterminate()) {
2038 LOG(WARNING) << "TLSA data is bogus or indeterminate";
2041 if (q.authentic_data()) {
2042 LOG(INFO) << "### TLSA records authentic for domain " << domain << " ###";
2044 else {
2045 LOG(INFO) << "TLSA records can't be authenticated for domain " << domain;
2048 auto tlsa_rrs = q.get_records();
2049 if (!tlsa_rrs.empty()) {
2050 LOG(INFO) << "### TLSA data found for " << domain << ':' << port << " ###";
2053 return tlsa_rrs;
2055 } // namespace
2057 int main(int argc, char* argv[])
2059 std::ios::sync_with_stdio(false);
2061 { // Need to work with either namespace.
2062 using namespace gflags;
2063 using namespace google;
2064 ParseCommandLineFlags(&argc, &argv, true);
2067 auto const config_path = osutil::get_config_dir();
2069 auto sender = get_sender();
2071 if (FLAGS_selftest) {
2072 selftest();
2073 return 0;
2076 auto bodies{std::vector<content>{}};
2077 for (int a = 1; a < argc; ++a) {
2078 if (!fs::exists(argv[a]))
2079 LOG(FATAL) << "can't find mail body part " << argv[a];
2080 bodies.push_back(argv[a]);
2083 if (argc == 1)
2084 bodies.push_back("body.txt");
2086 CHECK_EQ(bodies.size(), 1) << "only one body part for now";
2087 CHECK(!(FLAGS_4 && FLAGS_6)) << "must use /some/ IP version";
2089 if (FLAGS_force_smtputf8)
2090 FLAGS_use_smtputf8 = true;
2092 auto&& [from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx, smtp_to2_mbx,
2093 smtp_to3_mbx] = parse_mailboxes();
2095 if (smtp_to_mbx.domain().empty() && smtp_to2_mbx.domain().empty() &&
2096 smtp_to3_mbx.domain().empty()) {
2097 smtp_to_mbx = to_mbx;
2100 if (smtp_to_mbx.domain().empty() && FLAGS_mx_host.empty()) {
2101 LOG(ERROR) << "don't know who to send this mail to";
2102 return 0;
2105 if (!smtp_to2_mbx.domain().empty() &&
2106 smtp_to2_mbx.domain() != smtp_to_mbx.domain()) {
2107 LOG(ERROR) << "can't send to both " << smtp_to_mbx.domain() << " and "
2108 << smtp_to2_mbx.domain();
2109 return 0;
2112 if (!smtp_to3_mbx.domain().empty() &&
2113 smtp_to3_mbx.domain() != smtp_to_mbx.domain()) {
2114 LOG(ERROR) << "can't send to both " << smtp_to_mbx.domain() << " and "
2115 << smtp_to3_mbx.domain();
2116 return 0;
2119 auto const port{osutil::get_port(FLAGS_service.c_str(), "tcp")};
2121 auto res{DNS::Resolver{config_path}};
2122 auto tlsa_rrs{get_tlsa_rrs(res, smtp_to_mbx.domain(), port)};
2124 if (FLAGS_pipe) {
2125 return snd(config_path, STDIN_FILENO, STDOUT_FILENO, sender,
2126 smtp_to_mbx.domain(), tlsa_rrs, false, from_mbx, to_mbx,
2127 smtp_from_mbx, smtp_to_mbx, smtp_to2_mbx, smtp_to3_mbx, bodies)
2128 ? EXIT_SUCCESS
2129 : EXIT_FAILURE;
2132 bool enforce_dane = true;
2133 auto const receivers = get_receivers(res, smtp_to_mbx, enforce_dane);
2135 if (receivers.empty()) {
2136 LOG(INFO) << "no place to send this mail";
2137 return EXIT_SUCCESS;
2140 for (auto const& receiver : receivers) {
2141 LOG(INFO) << "trying " << receiver << ":" << FLAGS_service;
2143 if (FLAGS_noconn) {
2144 LOG(INFO) << "skipping";
2145 continue;
2148 auto fd = conn(res, receiver, port);
2149 if (fd == -1) {
2150 LOG(WARNING) << "no connection, skipping";
2151 continue;
2154 // Get our local IP address as "us".
2156 sa::sockaddrs us_addr{};
2157 socklen_t us_addr_len{sizeof us_addr};
2158 char us_addr_str[INET6_ADDRSTRLEN]{'\0'};
2159 std::vector<std::string> fcrdns;
2160 bool private_addr = false;
2162 if (-1 == getsockname(fd, &us_addr.addr, &us_addr_len)) {
2163 // Ignore ENOTSOCK errors from getsockname, useful for testing.
2164 PLOG_IF(WARNING, ENOTSOCK != errno) << "getsockname failed";
2166 else {
2167 switch (us_addr_len) {
2168 case sizeof(sockaddr_in):
2169 PCHECK(inet_ntop(AF_INET, &us_addr.addr_in.sin_addr, us_addr_str,
2170 sizeof us_addr_str) != nullptr);
2171 if (IP4::is_private(us_addr_str))
2172 private_addr = true;
2173 else
2174 fcrdns = DNS::fcrdns4(res, us_addr_str);
2175 break;
2177 case sizeof(sockaddr_in6):
2178 PCHECK(inet_ntop(AF_INET6, &us_addr.addr_in6.sin6_addr, us_addr_str,
2179 sizeof us_addr_str) != nullptr);
2180 if (IP6::is_private(us_addr_str))
2181 private_addr = true;
2182 else
2183 fcrdns = DNS::fcrdns6(res, us_addr_str);
2184 break;
2186 default:
2187 LOG(FATAL) << "bogus address length (" << us_addr_len
2188 << ") returned from getsockname";
2192 if (fcrdns.size()) {
2193 LOG(INFO) << "our names are:";
2194 for (auto const& fc : fcrdns) {
2195 LOG(INFO) << " " << fc;
2199 if (!private_addr) {
2200 // look at from_mbx.domain() and get SPF records
2202 // also look at our ID to get SPF records.
2205 if (smtp_to_mbx.domain() == receiver) {
2206 if (snd(config_path, fd, fd, sender, receiver, tlsa_rrs, enforce_dane,
2207 from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx, smtp_to2_mbx,
2208 smtp_to3_mbx, bodies)) {
2209 return EXIT_SUCCESS;
2212 else {
2213 auto tlsa_rrs_mx{get_tlsa_rrs(res, receiver, port)};
2214 tlsa_rrs_mx.insert(end(tlsa_rrs_mx), begin(tlsa_rrs), end(tlsa_rrs));
2215 if (snd(config_path, fd, fd, sender, receiver, tlsa_rrs_mx, enforce_dane,
2216 from_mbx, to_mbx, smtp_from_mbx, smtp_to_mbx, smtp_to2_mbx,
2217 smtp_to3_mbx, bodies)) {
2218 return EXIT_SUCCESS;
2222 close(fd);
2225 LOG(ERROR) << "we ran out of hosts to try";