1 #include <gflags/gflags.h>
5 // These need to be at least the length of any string it's trying to match.
6 DEFINE_uint64(cmd_bfr_size
, 4 * 1024, "command parser buffer size");
7 DEFINE_uint64(data_bfr_size
, 64 * 1024, "data parser buffer size");
9 DEFINE_uint64(max_xfer_size
, 64 * 1024, "maximum BDAT transfer size");
15 #include "Session.hpp"
18 #include "iobuffer.hpp"
26 #include <tao/pegtl.hpp>
27 #include <tao/pegtl/contrib/abnf.hpp>
29 using namespace tao::pegtl
;
30 using namespace tao::pegtl::abnf
;
40 std::pair
<std::string
, std::string
> param
;
41 std::unordered_map
<std::string
, std::string
> parameters
;
43 std::streamsize chunk_size
;
45 Ctx(fs::path config_path
, std::function
<void(void)> read_hook
)
46 : session(config_path
, read_hook
)
55 struct quoted_pair
: seq
<one
<'\\'>, sor
<VCHAR
, WSP
>> {};
58 using colon
= one
<':'>;
59 using dash
= one
<'-'>;
61 struct u_let_dig
: sor
<ALPHA
, DIGIT
, UTF8_non_ascii
> {};
63 struct u_ldh_tail
: star
<sor
<seq
<plus
<one
<'-'>>, u_let_dig
>, u_let_dig
>> {};
65 struct u_label
: seq
<u_let_dig
, u_ldh_tail
> {};
67 struct let_dig
: sor
<ALPHA
, DIGIT
> {};
69 struct ldh_tail
: star
<sor
<seq
<plus
<one
<'-'>>, let_dig
>, let_dig
>> {};
71 struct ldh_str
: seq
<let_dig
, ldh_tail
> {};
73 // struct label : seq<let_dig, opt<ldh_str>> {};
75 struct sub_domain
: u_label
{};
77 struct domain
: list_tail
<sub_domain
, dot
> {};
79 struct dec_octet
: sor
<seq
<string
<'2','5'>, range
<'0','5'>>,
80 seq
<one
<'2'>, range
<'0','4'>, DIGIT
>,
81 seq
<range
<'0', '1'>, rep
<2, DIGIT
>>,
82 rep_min_max
<1, 2, DIGIT
>> {};
84 struct IPv4_address_literal
85 : seq
<dec_octet
, dot
, dec_octet
, dot
, dec_octet
, dot
, dec_octet
> {};
87 struct h16
: rep_min_max
<1, 4, HEXDIG
> {};
89 struct ls32
: sor
<seq
<h16
, colon
, h16
>, IPv4_address_literal
> {};
91 struct dcolon
: two
<':'> {};
93 struct IPv6address
: sor
<seq
< rep
<6, h16
, colon
>, ls32
>,
94 seq
< dcolon
, rep
<5, h16
, colon
>, ls32
>,
95 seq
<opt
<h16
>, dcolon
, rep
<4, h16
, colon
>, ls32
>,
96 seq
<opt
<h16
, opt
< colon
, h16
>>, dcolon
, rep
<3, h16
, colon
>, ls32
>,
97 seq
<opt
<h16
, rep_opt
<2, colon
, h16
>>, dcolon
, rep
<2, h16
, colon
>, ls32
>,
98 seq
<opt
<h16
, rep_opt
<3, colon
, h16
>>, dcolon
, h16
, colon
, ls32
>,
99 seq
<opt
<h16
, rep_opt
<4, colon
, h16
>>, dcolon
, ls32
>,
100 seq
<opt
<h16
, rep_opt
<5, colon
, h16
>>, dcolon
, h16
>,
101 seq
<opt
<h16
, rep_opt
<6, colon
, h16
>>, dcolon
>> {};
103 struct IPv6_address_literal
: seq
<TAO_PEGTL_ISTRING("IPv6:"), IPv6address
> {};
105 struct dcontent
: ranges
<33, 90, 94, 126> {};
107 struct standardized_tag
: ldh_str
{};
109 struct general_address_literal
: seq
<standardized_tag
, colon
, plus
<dcontent
>> {};
111 // See rfc 5321 Section 4.1.3
112 struct address_literal
: seq
<one
<'['>,
113 sor
<IPv4_address_literal
,
114 IPv6_address_literal
,
115 general_address_literal
>,
118 struct at_domain
: seq
<one
<'@'>, domain
> {};
120 // The qtextSMTP rule explained: it's ASCII...
121 // excluding all the control chars below SPACE
122 // 34 '"' the double quote
123 // 92 '\\' the back slash
126 // 32-33: ' ' and '!'
127 // 35-91: "#$%&'()*+,-./" 0-9 ":;<=>?@" A-Z '['
128 // 93-126: "]^_`" a-z "{|}~"
130 struct qtextSMTP
: sor
<ranges
<32, 33, 35, 91, 93, 126>, UTF8_non_ascii
> {};
132 struct graphic
: range
<32, 126> {};
134 struct quoted_pairSMTP
: seq
<one
<'\\'>, graphic
> {};
136 struct qcontentSMTP
: sor
<qtextSMTP
, quoted_pairSMTP
> {};
138 struct quoted_string
: seq
<one
<'"'>, plus
<qcontentSMTP
>, one
<'"'>> {};
140 // excluded from atext are the “specials”: "()<>[]:;@\\,."
142 struct atext
: sor
<ALPHA
, DIGIT
,
155 struct atom
: plus
<atext
> {};
157 struct dot_string
: list
<atom
, dot
> {};
159 struct local_part
: sor
<dot_string
, quoted_string
> {};
161 struct non_local_part
: sor
<domain
, address_literal
> {};
163 struct mailbox
: seq
<local_part
, one
<'@'>, non_local_part
> {};
165 struct path
: seq
<one
<'<'>, mailbox
, one
<'>'>> {};
167 struct bounce_path
: TAO_PEGTL_ISTRING("<>") {};
169 struct reverse_path
: sor
<path
, bounce_path
> {};
171 struct magic_postmaster
: TAO_PEGTL_ISTRING("<Postmaster>") {};
173 struct forward_path
: sor
<path
, magic_postmaster
> {};
175 struct esmtp_keyword
: seq
<sor
<ALPHA
, DIGIT
>, star
<sor
<ALPHA
, DIGIT
, dash
>>> {};
177 struct esmtp_value
: plus
<sor
<range
<33, 60>, range
<62, 126>, UTF8_non_ascii
>> {};
179 struct esmtp_param
: seq
<esmtp_keyword
, opt
<seq
<one
<'='>, esmtp_value
>>> {};
181 struct mail_parameters
: list
<esmtp_param
, SP
> {};
183 struct rcpt_parameters
: list
<esmtp_param
, SP
> {};
185 struct string
: sor
<quoted_string
, atom
> {};
187 struct helo
: seq
<TAO_PEGTL_ISTRING("HELO"),
189 sor
<domain
, address_literal
>,
192 struct ehlo
: seq
<TAO_PEGTL_ISTRING("EHLO"),
194 sor
<domain
, address_literal
>,
197 struct mail_from
: seq
<TAO_PEGTL_ISTRING("MAIL"),
198 TAO_PEGTL_ISTRING(" FROM:"),
199 opt
<SP
>, // common enough error, we'll allow it
201 opt
<seq
<SP
, mail_parameters
>>,
204 struct rcpt_to
: seq
<TAO_PEGTL_ISTRING("RCPT"),
205 TAO_PEGTL_ISTRING(" TO:"),
206 opt
<SP
>, // common error
208 opt
<seq
<SP
, rcpt_parameters
>>,
211 struct chunk_size
: plus
<DIGIT
> {};
213 struct last
: TAO_PEGTL_ISTRING("LAST") {};
215 struct bdat
: seq
<TAO_PEGTL_ISTRING("BDAT"), SP
, chunk_size
, CRLF
> {};
217 struct bdat_last
: seq
<TAO_PEGTL_ISTRING("BDAT"), SP
, chunk_size
, SP
, last
, CRLF
> {};
219 struct data
: seq
<TAO_PEGTL_ISTRING("DATA"), CRLF
> {};
221 // ## below: DATA sub parse ##
223 struct data_end
: seq
<dot
, CRLF
> {};
225 struct data_blank
: CRLF
{};
227 // This particular crud will trigger an error return with the "no bare
230 struct data_not_end
: seq
<dot
, LF
> {};
232 struct data_also_not_end
: seq
<LF
, dot
, CRLF
> {};
235 // RFC 5321 text line length section 4.5.3.1.6.
236 struct data_plain
: seq
<rep_min_max
<1, 998, not_one
<'\r', '\n'>>, CRLF
> {};
238 struct data_dot
: seq
<one
<'.'>, rep_min_max
<1, 998, not_one
<'\r', '\n'>>, CRLF
> {};
240 struct data_long
: seq
<star
<not_one
<'\r', '\n'>>, CRLF
> {};
242 struct data_line
: seq
<sor
<data_blank
,
250 struct data_grammar
: until
<data_end
, data_line
> {};
252 // ## above: DATA sub parse ##
254 struct rset
: seq
<TAO_PEGTL_ISTRING("RSET"), CRLF
> {};
256 struct noop
: seq
<TAO_PEGTL_ISTRING("NOOP"), opt
<seq
<SP
, string
>>, CRLF
> {};
258 struct vrfy
: seq
<TAO_PEGTL_ISTRING("VRFY"), opt
<seq
<SP
, string
>>, CRLF
> {};
260 struct help
: seq
<TAO_PEGTL_ISTRING("HELP"), opt
<seq
<SP
, string
>>, CRLF
> {};
263 : seq
<TAO_PEGTL_ISTRING("STAR"), TAO_PEGTL_ISTRING("TTLS"), CRLF
> {};
265 struct quit
: seq
<TAO_PEGTL_ISTRING("QUIT"), CRLF
> {};
269 // base64-char = ALPHA / DIGIT / "+" / "/"
272 struct base64_char
: sor
<ALPHA
, DIGIT
, one
<'+', '/'>> {};
274 // base64-terminal = (2base64-char "==") / (3base64-char "=")
276 struct base64_terminal
: sor
<seq
<rep
<2, base64_char
>, TAO_PEGTL_ISTRING("==")>,
277 seq
<rep
<3, base64_char
>, one
<'='>>
280 // base64 = base64-terminal /
281 // ( 1*(4base64-char) [base64-terminal] )
283 struct base64
: sor
<base64_terminal
,
284 seq
<plus
<rep
<4, base64_char
>>,
285 opt
<base64_terminal
>>
288 // initial-response= base64 / "="
290 struct initial_response
: sor
<base64
, one
<'='>> {};
292 // cancel-response = "*"
294 struct cancel_response
: one
<'*'> {};
296 struct UPPER_ALPHA
: range
<'A', 'Z'> {};
298 using HYPHEN
= one
<'-'>;
299 using UNDERSCORE
= one
<'_'>;
301 struct mech_char
: sor
<UPPER_ALPHA
, DIGIT
, HYPHEN
, UNDERSCORE
> {};
302 struct sasl_mech
: rep_min_max
<1, 20, mech_char
> {};
304 // auth-command = "AUTH" SP sasl-mech [SP initial-response]
305 // *(CRLF [base64]) [CRLF cancel-response]
307 // ;; <sasl-mech> is defined in RFC 4422
309 struct auth
: seq
<TAO_PEGTL_ISTRING("AUTH"), SP
, sasl_mech
,
310 opt
<seq
<SP
, initial_response
>>,
311 // star<CRLF, opt<base64>>,
312 // opt<seq<CRLF, cancel_response>>,
316 struct bogus_cmd_short
: seq
<rep_min_max
<0, 3, not_one
<'\r', '\n'>>, CRLF
> {};
317 struct bogus_cmd_long
: seq
<rep_min_max
<4, 1000, not_one
<'\r', '\n'>>, CRLF
> {};
319 // commands in size order
321 struct any_cmd
: seq
<sor
<bogus_cmd_short
,
339 struct grammar
: plus
<any_cmd
> {};
343 template <typename Rule
>
344 struct action
: nothing
<Rule
> {
347 template <typename Rule
>
348 struct data_action
: nothing
<Rule
> {
352 struct action
<bogus_cmd_short
> {
353 template <typename Input
>
354 static void apply(Input
const& in
, Ctx
& ctx
)
356 LOG(INFO
) << "bogus_cmd_short";
357 ctx
.session
.cmd_unrecognized(in
.string());
362 struct action
<bogus_cmd_long
> {
363 template <typename Input
>
364 static void apply(Input
const& in
, Ctx
& ctx
)
366 LOG(INFO
) << "bogus_cmd_long";
367 ctx
.session
.cmd_unrecognized(in
.string());
372 struct action
<esmtp_keyword
> {
373 template <typename Input
>
374 static void apply(Input
const& in
, Ctx
& ctx
)
376 ctx
.param
.first
= in
.string();
381 struct action
<esmtp_value
> {
382 template <typename Input
>
383 static void apply(Input
const& in
, Ctx
& ctx
)
385 ctx
.param
.second
= in
.string();
390 struct action
<esmtp_param
> {
391 template <typename Input
>
392 static void apply(Input
const& in
, Ctx
& ctx
)
394 ctx
.parameters
.insert(ctx
.param
);
395 ctx
.param
.first
.clear();
396 ctx
.param
.second
.clear();
401 struct action
<local_part
> {
402 template <typename Input
>
403 static void apply(Input
const& in
, Ctx
& ctx
)
405 ctx
.mb_loc
= in
.string();
406 // RFC 5321, section 4.5.3.1.1.
407 if (ctx
.mb_loc
.length() > 64) {
408 LOG(WARNING
) << "local part length == " << ctx
.mb_loc
.length();
414 struct action
<non_local_part
> {
415 template <typename Input
>
416 static void apply(Input
const& in
, Ctx
& ctx
)
418 ctx
.mb_dom
= in
.string();
419 // RFC 5321, section 4.5.3.1.2.
420 if (ctx
.mb_dom
.length() > 255) {
421 LOG(WARNING
) << "domain name or number too long " << ctx
.mb_dom
;
427 struct action
<magic_postmaster
> {
428 static void apply0(Ctx
& ctx
)
430 ctx
.mb_loc
= std::string
{"Postmaster"};
436 struct action
<helo
> {
437 template <typename Input
>
438 static void apply(Input
const& in
, Ctx
& ctx
)
440 auto const b
= begin(in
) + 5; // +5 for the length of "HELO "
441 auto const e
= std::find(b
, end(in
) - 2, ' '); // -2 for the CRLF
442 ctx
.session
.helo(std::string_view(b
, e
- b
));
447 struct action
<ehlo
> {
448 template <typename Input
>
449 static void apply(Input
const& in
, Ctx
& ctx
)
451 auto const b
= begin(in
) + 5; // +5 for the length of "EHLO "
452 auto const e
= std::find(b
, end(in
) - 2, ' '); // -2 for the CRLF
453 ctx
.session
.ehlo(std::string_view(b
, e
- b
));
458 struct action
<mail_from
> {
459 static void apply0(Ctx
& ctx
)
461 ctx
.session
.mail_from(Mailbox
{ctx
.mb_loc
, ctx
.mb_dom
}, ctx
.parameters
);
464 ctx
.parameters
.clear();
469 struct action
<rcpt_to
> {
470 static void apply0(Ctx
& ctx
)
472 ctx
.session
.rcpt_to(Mailbox
{ctx
.mb_loc
, ctx
.mb_dom
}, ctx
.parameters
);
475 ctx
.parameters
.clear();
480 struct action
<chunk_size
> {
481 template <typename Input
>
482 static void apply(Input
const& in
, Ctx
& ctx
)
484 ctx
.chunk_size
= std::strtoull(in
.string().c_str(), nullptr, 10);
488 void bdat_act(Ctx
& ctx
, bool last
)
490 auto status_returned
= false;
492 if (!ctx
.session
.bdat_start(ctx
.chunk_size
))
493 status_returned
= true;
495 auto to_xfer
= ctx
.chunk_size
;
497 auto const bfr_size
{std::min(to_xfer
, std::streamsize(FLAGS_max_xfer_size
))};
498 iobuffer
<char> bfr(bfr_size
);
501 auto const xfer_sz
{std::min(to_xfer
, bfr_size
)};
503 ctx
.session
.in().read(bfr
.data(), xfer_sz
);
504 if (!ctx
.session
.in()) {
505 LOG(ERROR
) << "attempt to read " << xfer_sz
<< " octets but only got "
506 << ctx
.session
.in().gcount();
507 if (ctx
.session
.maxed_out()) {
508 LOG(ERROR
) << "input maxed out";
509 if (!status_returned
)
510 ctx
.session
.bdat_size_error();
512 else if (ctx
.session
.timed_out()) {
513 LOG(ERROR
) << "input timed out";
514 if (!status_returned
)
515 ctx
.session
.bdat_io_error();
517 else if (ctx
.session
.in().eof()) {
518 LOG(ERROR
) << "EOF in BDAT";
519 if (!status_returned
)
520 ctx
.session
.bdat_io_error();
523 LOG(ERROR
) << "I/O error in BDAT";
524 if (!status_returned
)
525 ctx
.session
.bdat_io_error();
529 if (!status_returned
&& !ctx
.session
.msg_write(bfr
.data(), xfer_sz
)) {
530 status_returned
= true;
536 if (!status_returned
) {
537 ctx
.session
.bdat_done(ctx
.chunk_size
, last
);
542 struct action
<bdat
> {
543 static void apply0(Ctx
& ctx
)
545 bdat_act(ctx
, false);
550 struct action
<bdat_last
> {
551 static void apply0(Ctx
& ctx
)
558 struct data_action
<data_end
> {
559 static void apply0(Ctx
& ctx
) { ctx
.session
.data_done(); }
563 struct data_action
<data_blank
> {
564 static void apply0(Ctx
& ctx
)
566 constexpr char CRLF
[]{'\r', '\n'};
567 ctx
.session
.msg_write(CRLF
, sizeof(CRLF
));
572 struct data_action
<data_plain
> {
573 template <typename Input
>
574 static void apply(Input
const& in
, Ctx
& ctx
)
576 auto const len
{end(in
) - begin(in
)};
577 ctx
.session
.msg_write(begin(in
), len
);
582 struct data_action
<data_dot
> {
583 template <typename Input
>
584 static void apply(Input
const& in
, Ctx
& ctx
)
586 auto const len
{end(in
) - begin(in
) - 1};
587 ctx
.session
.msg_write(begin(in
) + 1, len
);
592 struct data_action
<data_long
> {
593 template <typename Input
>
594 static void apply(Input
const& in
, Ctx
& ctx
)
596 LOG(WARNING
) << "garbage in data stream: \"" << esc(in
.string()) << "\"";
597 auto const len
{end(in
) - begin(in
)};
599 ctx
.session
.msg_write(begin(in
), len
);
601 LOG(WARNING
) << "line too long at " << len
<< " octets";
607 struct data_action
<data_not_end
> {
608 static void apply0(Ctx
& ctx
) __attribute__((noreturn
))
610 ctx
.session
.bare_lf();
615 struct data_action
<data_also_not_end
> {
616 static void apply0(Ctx
& ctx
) __attribute__((noreturn
))
618 ctx
.session
.bare_lf();
623 struct action
<data
> {
624 template <typename Input
>
625 static void apply(Input
const& in
, Ctx
& ctx
)
627 if (ctx
.session
.data_start()) {
628 auto din
= istream_input
<eol::crlf
, 1>(ctx
.session
.in(),
629 FLAGS_data_bfr_size
, "data");
631 if (!parse_nested
<RFC5321::data_grammar
, RFC5321::data_action
>(in
, din
,
633 ctx
.session
.log_stats();
634 if (!(ctx
.session
.maxed_out() || ctx
.session
.timed_out())) {
635 ctx
.session
.error("bad DATA syntax");
640 catch (parse_error
const& e
) {
641 LOG(WARNING
) << e
.what();
642 ctx
.session
.error("unable to parse DATA stream");
644 catch (std::exception
const& e
) {
645 LOG(WARNING
) << e
.what();
646 ctx
.session
.error("unknown problem in DATA stream");
653 struct action
<rset
> {
654 static void apply0(Ctx
& ctx
) { ctx
.session
.rset(); }
657 template <typename Input
>
658 std::string_view
get_string_view(Input
const& in
)
660 auto const b
= begin(in
) + 4;
661 auto const len
= end(in
) - b
;
662 auto str
= std::string_view(b
, len
);
663 if (str
.front() == ' ')
664 str
.remove_prefix(1);
669 struct action
<noop
> {
670 template <typename Input
>
671 static void apply(Input
const& in
, Ctx
& ctx
)
673 auto const str
= get_string_view(in
);
674 ctx
.session
.noop(str
);
679 struct action
<vrfy
> {
680 template <typename Input
>
681 static void apply(Input
const& in
, Ctx
& ctx
)
683 auto const str
= get_string_view(in
);
684 ctx
.session
.vrfy(str
);
689 struct action
<help
> {
690 template <typename Input
>
691 static void apply(Input
const& in
, Ctx
& ctx
)
693 auto const str
= get_string_view(in
);
694 ctx
.session
.help(str
);
699 struct action
<starttls
> {
700 static void apply0(Ctx
& ctx
) { ctx
.session
.starttls(); }
704 struct action
<quit
> {
705 static void apply0(Ctx
& ctx
) __attribute__((noreturn
)) { ctx
.session
.quit(); }
709 struct action
<auth
> {
710 static void apply0(Ctx
& ctx
) __attribute__((noreturn
)) { ctx
.session
.auth(); }
712 } // namespace RFC5321
714 void timeout(int signum
) __attribute__((noreturn
));
715 void timeout(int signum
)
717 const char errmsg
[] = "421 4.4.2 time-out\r\n";
718 write(STDOUT_FILENO
, errmsg
, sizeof errmsg
- 1);
722 void install_syscall_filter()
724 /// scmp_filter_ctx ctx = CHECK_NOTNULL(seccomp_init(SCMP_ACT_ERRNO(EPERM)));
725 scmp_filter_ctx ctx
= CHECK_NOTNULL(seccomp_init(SCMP_ACT_LOG
));
727 auto rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(write
), 0);
728 CHECK_EQ(rc
, 0) << "seccomp_rule_add write failed";
730 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(read
), 0);
731 CHECK_EQ(rc
, 0) << "seccomp_rule_add read failed";
733 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(close
), 0);
734 CHECK_EQ(rc
, 0) << "seccomp_rule_add close failed";
736 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(rt_sigprocmask
), 0);
737 CHECK_EQ(rc
, 0) << "seccomp_rule_add rt_sigprocmask failed";
739 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(rt_sigreturn
), 0);
740 CHECK_EQ(rc
, 0) << "seccomp_rule_add rt_sigreturn failed";
742 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(rt_sigaction
), 0);
743 CHECK_EQ(rc
, 0) << "seccomp_rule_add rt_sigaction failed";
745 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(newfstatat
), 0);
746 CHECK_EQ(rc
, 0) << "seccomp_rule_add newfstatat failed";
748 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(openat
), 0);
749 CHECK_EQ(rc
, 0) << "seccomp_rule_add openat failed";
751 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(socket
), 0);
752 CHECK_EQ(rc
, 0) << "seccomp_rule_add socket failed";
754 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(connect
), 0);
755 CHECK_EQ(rc
, 0) << "seccomp_rule_add connect failed";
757 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(poll
), 0);
758 CHECK_EQ(rc
, 0) << "seccomp_rule_add poll failed";
760 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(sendto
), 0);
761 CHECK_EQ(rc
, 0) << "seccomp_rule_add sendto failed";
763 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(recvfrom
), 0);
764 CHECK_EQ(rc
, 0) << "seccomp_rule_add recvfrom failed";
766 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(sigaltstack
), 0);
767 CHECK_EQ(rc
, 0) << "seccomp_rule_add sigaltstack failed";
769 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(prctl
), 0);
770 CHECK_EQ(rc
, 0) << "seccomp_rule_add prctl failed";
772 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(gettid
), 0);
773 CHECK_EQ(rc
, 0) << "seccomp_rule_add gettid failed";
775 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(getpid
), 0);
776 CHECK_EQ(rc
, 0) << "seccomp_rule_add getpid failed";
778 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(getppid
), 0);
779 CHECK_EQ(rc
, 0) << "seccomp_rule_add getppid failed";
781 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(newfstatat
), 0);
782 CHECK_EQ(rc
, 0) << "seccomp_rule_add newfstatat failed";
784 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(tgkill
), 0);
785 CHECK_EQ(rc
, 0) << "seccomp_rule_add tgkill failed";
787 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(dup
), 0);
788 CHECK_EQ(rc
, 0) << "seccomp_rule_add dup failed";
790 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(mmap
), 0);
791 CHECK_EQ(rc
, 0) << "seccomp_rule_add mmap failed";
793 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(munmap
), 0);
794 CHECK_EQ(rc
, 0) << "seccomp_rule_add munmap failed";
796 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(pipe2
), 0);
797 CHECK_EQ(rc
, 0) << "seccomp_rule_add pipe2 failed";
799 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(readlinkat
), 0);
800 CHECK_EQ(rc
, 0) << "seccomp_rule_add readlinkat failed";
802 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(pselect6
), 0);
803 CHECK_EQ(rc
, 0) << "seccomp_rule_add pselect6 failed";
805 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(uname
), 0);
806 CHECK_EQ(rc
, 0) << "seccomp_rule_add uname failed";
808 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(fcntl
), 0);
809 CHECK_EQ(rc
, 0) << "seccomp_rule_add fcntl failed";
811 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(unlink
), 0);
812 CHECK_EQ(rc
, 0) << "seccomp_rule_add unlink failed";
814 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(symlink
), 0);
815 CHECK_EQ(rc
, 0) << "seccomp_rule_add symlink failed";
817 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(madvise
), 0);
818 CHECK_EQ(rc
, 0) << "seccomp_rule_add madvise failed";
820 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(mprotect
), 0);
821 CHECK_EQ(rc
, 0) << "seccomp_rule_add mprotect failed";
823 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(futex
), 0);
824 CHECK_EQ(rc
, 0) << "seccomp_rule_add futex failed";
826 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(writev
), 0);
827 CHECK_EQ(rc
, 0) << "seccomp_rule_add writev failed";
829 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(sysinfo
), 0);
830 CHECK_EQ(rc
, 0) << "seccomp_rule_add sysinfo failed";
832 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(brk
), 0);
833 CHECK_EQ(rc
, 0) << "seccomp_rule_add brk failed";
835 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(getrandom
), 0);
836 CHECK_EQ(rc
, 0) << "seccomp_rule_add getrandom failed";
838 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(getdents64
), 0);
839 CHECK_EQ(rc
, 0) << "seccomp_rule_add getdents64 failed";
841 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(lseek
), 0);
842 CHECK_EQ(rc
, 0) << "seccomp_rule_add lseek failed";
844 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(setsockopt
), 0);
845 CHECK_EQ(rc
, 0) << "seccomp_rule_add setsockopt failed";
847 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(alarm
), 0);
848 CHECK_EQ(rc
, 0) << "seccomp_rule_add alarm failed";
850 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(rename
), 0);
851 CHECK_EQ(rc
, 0) << "seccomp_rule_add rename failed";
853 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(clock_gettime
), 0);
854 CHECK_EQ(rc
, 0) << "seccomp_rule_add clock_gettime failed";
856 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(exit_group
), 0);
857 CHECK_EQ(rc
, 0) << "seccomp_rule_add exit_group failed";
859 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(exit
), 0);
860 CHECK_EQ(rc
, 0) << "seccomp_rule_add exit failed";
864 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(clone
), 0);
865 CHECK_EQ(rc
, 0) << "seccomp_rule_add clone failed";
867 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(wait4
), 0);
868 CHECK_EQ(rc
, 0) << "seccomp_rule_add wait4 failed";
870 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(sched_yield
), 0);
871 CHECK_EQ(rc
, 0) << "seccomp_rule_add sched_yield failed";
873 rc
= seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, SCMP_SYS(ptrace
), 0);
874 CHECK_EQ(rc
, 0) << "seccomp_rule_add ptrace failed";
876 rc
= seccomp_load(ctx
);
877 CHECK_EQ(rc
, 0) << "seccomp_load failed";
879 // seccomp_export_pfc(ctx, STDERR_FILENO);
881 seccomp_release(ctx
);
884 int main(int argc
, char* argv
[])
886 std::ios::sync_with_stdio(false);
888 { // Need to work with either namespace.
889 using namespace gflags
;
890 using namespace google
;
891 ParseCommandLineFlags(&argc
, &argv
, true);
894 // Set timeout signal handler to limit total run time.
895 struct sigaction sact
{};
896 PCHECK(sigemptyset(&sact
.sa_mask
) == 0);
898 sact
.sa_handler
= timeout
;
899 PCHECK(sigaction(SIGALRM
, &sact
, nullptr) == 0);
901 auto const log_dir
{getenv("GOOGLE_LOG_DIR")};
904 fs::create_directories(log_dir
, ec
);
907 google::InitGoogleLogging(argv
[0]);
909 std::unique_ptr
<RFC5321::Ctx
> ctx
;
911 // Don't wait for STARTTLS to fail if no cert.
912 auto const config_path
= osutil::get_config_dir();
913 auto const certs
= osutil::list_directory(config_path
, Config::cert_fn_re
);
914 CHECK_GE(certs
.size(), 1) << "no certs found";
916 auto const read_hook
{[&ctx
]() { ctx
->session
.flush(); }};
917 ctx
= std::make_unique
<RFC5321::Ctx
>(config_path
, read_hook
);
919 ctx
->session
.greeting();
921 install_syscall_filter();
923 istream_input
<eol::crlf
, 1> in
{ctx
->session
.in(), FLAGS_cmd_bfr_size
,
928 ret
= !parse
<RFC5321::grammar
, RFC5321::action
>(in
, *ctx
);
930 catch (std::exception
const& e
) {
931 LOG(WARNING
) << e
.what();
934 if (ctx
->session
.maxed_out()) {
935 ctx
->session
.max_out();
937 else if (ctx
->session
.timed_out()) {
938 ctx
->session
.time_out();
941 // ctx->session.error("session end without QUIT command from client");