1 #ifndef SESSION_DOT_HPP
2 #define SESSION_DOT_HPP
7 #include <unordered_map>
11 #include "DNS-fcrdns.hpp"
13 #include "Mailbox.hpp"
14 #include "MessageStore.hpp"
18 #include "message.hpp"
21 constexpr size_t kibibyte
= 1024;
22 constexpr size_t mebibyte
= kibibyte
* kibibyte
;
23 constexpr size_t max_msg_size_initial
= 15 * mebibyte
;
24 constexpr size_t max_msg_size_bro
= 150 * mebibyte
;
29 using parameters_t
= std::unordered_map
<std::string
, std::string
>;
31 Session(Session
const&) = delete;
32 Session
& operator=(Session
const&) = delete;
36 std::function
<void(void)> read_hook
= []() {},
37 int fd_in
= STDIN_FILENO
,
38 int fd_out
= STDOUT_FILENO
);
41 void ehlo(std::string_view client_identity
) { lo_("EHLO", client_identity
); }
42 void helo(std::string_view client_identity
) { lo_("HELO", client_identity
); }
43 void mail_from(Mailbox
&& reverse_path
, parameters_t
const& parameters
);
44 void rcpt_to(Mailbox
&& forward_path
, parameters_t
const& parameters
);
47 bool msg_write(char const* s
, std::streamsize count
);
51 void data_size_error();
54 bool bdat_start(size_t n
);
55 void bdat_done(size_t n
, bool last
);
56 void bdat_size_error();
57 void bdat_seq_error();
61 void noop(std::string_view str
);
62 void vrfy(std::string_view str
);
63 void help(std::string_view str
);
64 void quit() __attribute__((noreturn
));
65 void auth() __attribute__((noreturn
));
66 void error(std::string_view log_msg
);
67 void cmd_unrecognized(std::string_view log_msg
);
68 void bare_lf() __attribute__((noreturn
));
70 void max_out() __attribute__((noreturn
));
71 void time_out() __attribute__((noreturn
));
74 bool maxed_out() { return sock_
.maxed_out(); }
75 bool timed_out() { return sock_
.timed_out(); }
76 std::istream
& in() { return sock_
.in(); }
79 void last_in_group_(std::string_view verb
);
80 void check_for_pipeline_error_(std::string_view verb
);
82 size_t max_msg_size() const { return max_msg_size_
; }
83 void max_msg_size(size_t max
);
85 void log_stats() { sock_
.log_stats(); }
87 enum class SpamStatus
: bool { ham
, spam
};
90 friend struct Session_test
;
92 std::tuple
<SpamStatus
, std::string
> spam_status_();
94 std::string
added_headers_(MessageStore
const& msg
);
96 std::ostream
& out_() { return sock_
.out(); }
97 void lo_(char const* verb
, std::string_view client_identity
);
99 void bad_host_(char const* msg
) const __attribute__((noreturn
));
101 std::string
const& server_id_() const { return server_identity_
.ascii(); }
103 // bool forward_to_(std::string const& forward, Mailbox const& rcpt_to);
104 // bool reply_to_(Reply::from_to const& reply_info, Mailbox const& rcpt_to);
105 // bool do_forward_(message::parsed& msg);
106 // bool do_reply_(message::parsed& msg);
108 void xfer_response_(std::string_view success_msg
);
110 // clear per transaction data, preserve per connection data
113 bool verify_ip_address_(std::string
& error_msg
);
114 bool verify_ip_address_dnsbl_(std::string
& error_msg
);
115 bool verify_client_(Domain
const& client_identity
, std::string
& error_msg
);
116 bool verify_recipient_(Mailbox
const& recipient
);
117 bool verify_sender_(Mailbox
const& sender
, std::string
& error_msg
);
118 bool verify_sender_domain_(Domain
const& sender
, std::string
& error_msg
);
119 bool verify_sender_domain_uribl_(std::string_view sender
,
120 std::string
& error_msg
);
121 void do_spf_check_(Mailbox
const& sender
);
122 bool verify_from_params_(parameters_t
const& parameters
);
123 bool verify_rcpt_params_(parameters_t
const& parameters
);
125 // bool is_forwarding_() const
127 // return forward_path_.empty() && !fwd_path_.empty();
130 void exit_() __attribute__((noreturn
));
133 fs::path config_path_
;
137 // forwarding and replies
140 // Mailbox fwd_path_;
141 // Mailbox fwd_from_;
142 // Reply::from_to rep_info_;
144 // per connection/session
145 Domain server_identity_
; // who we identify as
146 std::vector
<Domain
> client_fcrdns_
; // who they look-up as
147 std::vector
<Domain
> server_fcrdns_
; // who we look-up as
148 std::string client_
; // (fcrdns_ [sock_.them_c_str()])
151 Domain client_identity_
; // from ehlo/helo
152 Mailbox reverse_path_
; // "mail from"
153 std::vector
<Mailbox
> forward_path_
; // for each "rcpt to"
154 std::string spf_received_
;
155 std::unique_ptr
<MessageStore
> msg_
;
159 std::random_device random_device_
;
161 // Domains we receive mail for.
164 // Allow and block lists for domains.
171 size_t max_msg_size_
;
173 int n_unrecognized_cmds_
{0};
175 SPF::Result spf_result_
;
176 Domain spf_sender_domain_
;
178 // RFC 5321 section 3.3. Mail Transactions
179 enum class xact_step
: int8_t {
185 rset
, // must now send RSET
189 xact_step state_
= xact_step::helo
;
191 bool binarymime_
{false};
192 bool extensions_
{false};
193 bool smtputf8_
{false};
197 bool fcrdns_allowed_
{false};
198 bool ip_allowed_
{false};
201 #endif // SESSION_DOT_HPP