-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSession.hpp
201 lines (156 loc) · 5.72 KB
/
Session.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef SESSION_DOT_HPP
#define SESSION_DOT_HPP
#include <random>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include "CDB.hpp"
#include "DNS-fcrdns.hpp"
#include "Domain.hpp"
#include "Mailbox.hpp"
#include "MessageStore.hpp"
#include "SPF.hpp"
#include "Sock.hpp"
#include "TLD.hpp"
#include "message.hpp"
namespace Config {
constexpr size_t kibibyte = 1024;
constexpr size_t mebibyte = kibibyte * kibibyte;
constexpr size_t max_msg_size_initial = 15 * mebibyte;
constexpr size_t max_msg_size_bro = 150 * mebibyte;
} // namespace Config
class Session {
public:
using parameters_t = std::unordered_map<std::string, std::string>;
Session(Session const&) = delete;
Session& operator=(Session const&) = delete;
explicit Session(
fs::path config_path,
std::function<void(void)> read_hook = []() {},
int fd_in = STDIN_FILENO,
int fd_out = STDOUT_FILENO);
void greeting();
void ehlo(std::string_view client_identity) { lo_("EHLO", client_identity); }
void helo(std::string_view client_identity) { lo_("HELO", client_identity); }
void mail_from(Mailbox&& reverse_path, parameters_t const& parameters);
void rcpt_to(Mailbox&& forward_path, parameters_t const& parameters);
bool msg_new();
bool msg_write(char const* s, std::streamsize count);
bool data_start();
void data_done();
void data_size_error();
void data_error();
bool bdat_start(size_t n);
void bdat_done(size_t n, bool last);
void bdat_size_error();
void bdat_seq_error();
void bdat_io_error();
void rset();
void noop(std::string_view str);
void vrfy(std::string_view str);
void help(std::string_view str);
void quit() __attribute__((noreturn));
void auth() __attribute__((noreturn));
void error(std::string_view log_msg);
void cmd_unrecognized(std::string_view log_msg);
void bare_lf() __attribute__((noreturn));
void max_out() __attribute__((noreturn));
void time_out() __attribute__((noreturn));
void starttls();
bool maxed_out() { return sock_.maxed_out(); }
bool timed_out() { return sock_.timed_out(); }
std::istream& in() { return sock_.in(); }
void flush();
void last_in_group_(std::string_view verb);
void check_for_pipeline_error_(std::string_view verb);
size_t max_msg_size() const { return max_msg_size_; }
void max_msg_size(size_t max);
void log_stats() { sock_.log_stats(); }
enum class SpamStatus : bool { ham, spam };
private:
friend struct Session_test;
std::tuple<SpamStatus, std::string> spam_status_();
std::string added_headers_(MessageStore const& msg);
std::ostream& out_() { return sock_.out(); }
void lo_(char const* verb, std::string_view client_identity);
void bad_host_(char const* msg) const __attribute__((noreturn));
std::string const& server_id_() const { return server_identity_.ascii(); }
// bool forward_to_(std::string const& forward, Mailbox const& rcpt_to);
// bool reply_to_(Reply::from_to const& reply_info, Mailbox const& rcpt_to);
// bool do_forward_(message::parsed& msg);
// bool do_reply_(message::parsed& msg);
bool do_deliver_();
void xfer_response_(std::string_view success_msg);
// clear per transaction data, preserve per connection data
void reset_();
bool verify_ip_address_(std::string& error_msg);
bool verify_ip_address_dnsbl_(std::string& error_msg);
bool verify_client_(Domain const& client_identity, std::string& error_msg);
bool verify_recipient_(Mailbox const& recipient);
bool verify_sender_(Mailbox const& sender, std::string& error_msg);
bool verify_sender_domain_(Domain const& sender, std::string& error_msg);
bool verify_sender_domain_uribl_(std::string_view sender,
std::string& error_msg);
void do_spf_check_(Mailbox const& sender);
bool verify_from_params_(parameters_t const& parameters);
bool verify_rcpt_params_(parameters_t const& parameters);
// bool is_forwarding_() const
// {
// return forward_path_.empty() && !fwd_path_.empty();
// }
void exit_() __attribute__((noreturn));
private:
fs::path config_path_;
DNS::Resolver res_;
Sock sock_;
// forwarding and replies
// Send send_;
// Reply rep_;
// Mailbox fwd_path_;
// Mailbox fwd_from_;
// Reply::from_to rep_info_;
// per connection/session
Domain server_identity_; // who we identify as
std::vector<Domain> client_fcrdns_; // who they look-up as
std::vector<Domain> server_fcrdns_; // who we look-up as
std::string client_; // (fcrdns_ [sock_.them_c_str()])
// per transaction
Domain client_identity_; // from ehlo/helo
Mailbox reverse_path_; // "mail from"
std::vector<Mailbox> forward_path_; // for each "rcpt to"
std::string spf_received_;
std::unique_ptr<MessageStore> msg_;
TLD tld_db_;
std::random_device random_device_;
// Domains we receive mail for.
CDB accept_domains_;
// Allow and block lists for domains.
CDB allow_;
CDB block_;
// Forwards
CDB forward_;
size_t max_msg_size_;
int n_unrecognized_cmds_{0};
SPF::Result spf_result_;
Domain spf_sender_domain_;
// RFC 5321 section 3.3. Mail Transactions
enum class xact_step : int8_t {
helo,
mail,
rcpt,
data,
bdat, // RFC 3030
rset, // must now send RSET
};
// per transaction
xact_step state_ = xact_step::helo;
bool binarymime_{false};
bool extensions_{false};
bool smtputf8_{false};
bool prdr_{false};
// per connection
bool fcrdns_allowed_{false};
bool ip_allowed_{false};
};
#endif // SESSION_DOT_HPP