This repository was archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmtp_protocol_class.hpp
More file actions
543 lines (512 loc) · 11.3 KB
/
smtp_protocol_class.hpp
File metadata and controls
543 lines (512 loc) · 11.3 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#ifndef __SMTP_PROTOCOL_CLASS_HPP__
#define __SMTP_PROTOCOL_CLASS_HPP__
#include <vector>
#include <string>
#include "pystring_extend/pystring_class.h"
class smtp_info_class
{
public:
pystring_ext session_command_helo;
pystring_ext session_command_ehlo;
pystring_ext session_command_mail_from;
std::vector<pystring_ext> session_command_rcpt_to;
pystring_ext session_data;
smtp_info_class()
{
}
smtp_info_class(const smtp_info_class& input_info)
:
session_command_helo(input_info.session_command_helo),
session_command_ehlo(input_info.session_command_ehlo),
session_command_mail_from(input_info.session_command_mail_from),
session_command_rcpt_to(input_info.session_command_rcpt_to),
session_data(input_info.session_data)
{
}
void clear()
{
session_command_helo.clear();
session_command_ehlo.clear();
session_command_mail_from.clear();
session_command_rcpt_to.clear();
session_data.clear();
}
};
class smtp_client_protocol_class
{
public:
smtp_info_class smtp_info;
enum
{
//SEND_SETP_CONNECTED = 1,
SEND_SETP_EHLO,
SEND_SETP_MAILFROM,
SEND_SETP_RCPTTO,
SEND_SETP_DATA,
SEND_SETP_DATAEND,
SEND_SETP_WAIT_DATA_END,
SEND_SETP_WAIT_QUIT,
SEND_SETP_QUIT
};
int n_send_setp;
enum
{
STATUS_NORMAL,
STATUS_ERROR
};
int n_session_status;
smtp_client_protocol_class(smtp_info_class input_info)
: smtp_info(input_info), n_send_setp(SEND_SETP_EHLO),
n_session_status(STATUS_NORMAL)
{
}
bool is_can_start_ehlo(int status_code)
{
//smtp 状态码 220为正常
//220 filter.local ESMTP Postfix (Ubuntu)
if (status_code == 220)
{
return true;
}
n_session_status = STATUS_ERROR;
return false;
}
pystring_ext send_cmd_ehlo()
{
n_send_setp = SEND_SETP_MAILFROM;
return "ehlo filter.local\r\n";
}
bool is_can_start_mail_from(int status_code)
{
//smtp 状态码 250为正常
//250-filter.local
//250 2.1.0 Ok
if (status_code == 250)
{
return true;
}
n_session_status = STATUS_ERROR;
return false;
}
pystring_ext send_cmd_mail_from()
{
n_send_setp = SEND_SETP_RCPTTO;
pystring_ext pystr_mailfrom = "mail FROM:<%s>\r\n";
pystr_mailfrom = pystr_mailfrom.py_replace("%s",
smtp_info.session_command_mail_from);
return pystr_mailfrom;
}
bool is_can_start_rcpt_to(int status_code)
{
//smtp 状态码 250为正常
//250 2.1.0 Ok
if (status_code == 250)
{
return true;
}
n_session_status = STATUS_ERROR;
return false;
}
pystring_ext send_cmd_rcpt_to()
{
n_send_setp = SEND_SETP_DATA;
pystring_ext pystr_rcpt = "rcpt TO:<%s>\r\n";
if (smtp_info.session_command_rcpt_to.size() == 0)
{
n_session_status = STATUS_ERROR;
return "";
}
pystr_rcpt = pystr_rcpt.py_replace("%s", smtp_info.session_command_rcpt_to[0]);
return pystr_rcpt;
}
bool is_can_start_data(int status_code)
{
//smtp 状态码 250为正常
//250 2.1.0 Ok
if (status_code == 250)
{
return true;
}
n_session_status = STATUS_ERROR;
return false;
}
pystring_ext send_cmd_data_start()
{
n_send_setp = SEND_SETP_DATAEND;
return "data\r\n";
}
bool is_can_start_data_end(int status_code)
{
//smtp 状态码 354为正常
//354 End data with <CR><LF>.<CR><LF>
if (status_code == 354)
{
return true;
}
n_session_status = STATUS_ERROR;
return false;
}
pystring_ext send_cmd_data_end()
{
n_send_setp = SEND_SETP_WAIT_DATA_END;
pystring_ext send_data = smtp_info.session_data;
send_data.append("\r\n.\r\n");
return send_data;
}
bool is_can_start_quit(int status_code)
{
//smtp 状态码 250为正常
//250 2.0.0 Ok: queued as 561416C01FD
if (status_code == 250)
{
return true;
}
n_session_status = STATUS_ERROR;
return false;
}
pystring_ext send_cmd_quit()
{
n_send_setp = SEND_SETP_WAIT_QUIT;
return "quit\r\n";
}
bool is_can_quit(int status_code)
{
//smtp 状态码 221为正常
//221 2.0.0 Bye
if (status_code == 221)
{
n_send_setp = SEND_SETP_QUIT;
return true;
}
n_session_status = STATUS_ERROR;
return false;
}
pystring_ext from_revc_get_next_data(const pystring_ext &rawdata)
{
std::vector<pystring_ext> vec_command = rawdata.py_split(" ", 1);
if (vec_command.size() < 2)
{
n_session_status = STATUS_ERROR;
return "";
}
pystring_ext str_status_code = vec_command[0];
int status_code = (int)str_status_code.to_long64();
switch (n_send_setp)
{
case SEND_SETP_EHLO:
if (is_can_start_ehlo(status_code) == true)
{
return send_cmd_ehlo();
}
break;
case SEND_SETP_MAILFROM:
if (is_can_start_mail_from(status_code) == true)
{
return send_cmd_mail_from();
}
break;
case SEND_SETP_RCPTTO:
if (is_can_start_rcpt_to(status_code) == true)
{
return send_cmd_rcpt_to();
}
break;
case SEND_SETP_DATA:
if (is_can_start_data(status_code) == true)
{
return send_cmd_data_start();
}
break;
case SEND_SETP_DATAEND:
if (is_can_start_data_end(status_code) == true)
{
return send_cmd_data_end();
}
break;
case SEND_SETP_WAIT_DATA_END:
if (is_can_start_quit(status_code) == true)
{
return send_cmd_quit();
}
break;
case SEND_SETP_WAIT_QUIT:
if (is_can_quit(status_code) == true)
{
return "";
}
default:
break;
}
n_session_status = STATUS_ERROR;
return "";
}
bool is_command_line(const pystring_ext &rawdata)
{
if (rawdata.py_endswith("\n") == true)
{
//250-SIZE 33554432
//250 HELP
if (rawdata.py_startswith("250-") == true)
{
if (rawdata.find("250 ") != std::string::npos)
{
return true;
}
else
{
return false;
}
}
if (rawdata.find(" ") != std::string::npos)
{
return true;
}
}
return false;
}
bool is_full_data(const pystring_ext &rawdata)
{
if (rawdata.py_endswith("\n") == true)
{
if (rawdata.find(" ") != std::string::npos)
{
return true;
}
}
return false;
}
/*
bool is_should_close()
{
if (n_send_setp == SEND_SETP_QUIT || n_session_status == STATUS_ERROR)
{
return true;
}
return false;
}
*/
};
class smtp_server_protocol_class
{
public:
enum { SMTP_STATUS_COMMAND, SMTP_STATUS_DATA, SMTP_STATUS_DATAEND, SMTP_STATUS_QUIT };
volatile int smtp_session_status;
int session_data_length;
smtp_info_class smtp_info;
std::vector<smtp_info_class> vec_smtp_info;
smtp_server_protocol_class()
: smtp_session_status(SMTP_STATUS_COMMAND),
session_data_length(0),
smtp_info(),
vec_smtp_info()
{}
pystring_ext command_helo(const pystring_ext &info)
{
smtp_info.session_command_helo = info;
return "250 OK\r\n";
}
pystring_ext command_ehlo(const pystring_ext &info)
{
smtp_info.session_command_ehlo = info;
return "250-SMTP Protocol\n250 OK\r\n";
}
pystring_ext command_mail_form(
const pystring_ext &str_info,
const pystring_ext &str_extend_info
)
{
smtp_info.session_command_mail_from =
str_info.py_between_start_and_end_string(":<", ">");
if (smtp_info.session_command_mail_from.empty() == true)
{
return "501 Syntax error\r\n";
}
if (str_extend_info.empty() != true)
{
pystring_ext str_datalen =
str_extend_info.py_lower()
.py_between_start_and_end_string("size=")
.py_rstrip();
if (str_datalen.empty() != true)
{
session_data_length = (int)str_datalen.to_long64();
}
}
return "250 2.0 OK\r\n";
}
pystring_ext command_rcpt_to(const pystring_ext &str_info)
{
pystring_ext str_rcpt_to_addr = str_info.py_between_start_and_end_string(":<",
">");
if (str_rcpt_to_addr.empty() == true)
{
return "501 Syntax error\r\n";
}
smtp_info.session_command_rcpt_to.push_back(str_rcpt_to_addr);
return "250 2.1 OK\r\n";
}
pystring_ext command_data_start()
{
smtp_session_status = SMTP_STATUS_DATA;
return "354 End data with <CR><LF>.<CR><LF>\r\n";
}
pystring_ext command_data_end()
{
smtp_session_status = SMTP_STATUS_DATAEND;
vec_smtp_info.push_back(smtp_info);
return "250 2.3 OK\r\n";
}
pystring_ext command_noop()
{
return "250 OK\r\n";
}
pystring_ext command_rset()
{
smtp_session_status = SMTP_STATUS_COMMAND;
smtp_info.clear();
return "250 OK\r\n";
}
pystring_ext command_quit()
{
smtp_session_status = SMTP_STATUS_QUIT;
return "221 Bye\r\n";
}
pystring_ext read_command(const std::vector<pystring_ext> &vec_command)
{
if (vec_command.empty() == true)
{
return "501 Syntax error\r\n";
}
//for example:
//mail FROM:<Kristie@check-us-out.com> size=204326
pystring_ext str_cmd = vec_command[0];
pystring_ext str_argv = "";
if (vec_command.size() >= 2)
{
str_argv = vec_command[1];
str_argv = str_argv.py_strip();
}
pystring_ext str_argv_ext = "";
if (vec_command.size() == 3)
{
str_argv_ext = vec_command[2];
str_argv_ext = str_argv_ext.py_strip();
}
//转换cmd为小写方便比较
str_cmd = str_cmd.py_lower();
//去除末尾的\r\n
str_cmd = str_cmd.py_strip();
if (str_cmd == "helo")
{
return command_helo(str_argv);
}
if (str_cmd == "ehlo")
{
return command_helo(str_argv);
}
if (str_cmd == "mail")
{
return command_mail_form(str_argv, str_argv_ext);
}
if (str_cmd == "rcpt")
{
return command_rcpt_to(str_argv);
}
if (str_cmd == "data")
{
return command_data_start();
}
if (str_cmd == "noop")
{
return command_noop();
}
if (str_cmd == "rset")
{
return command_rset();
}
if (str_cmd == "quit")
{
return command_quit();
}
return "501 Syntax error\r\n";
}
pystring_ext read_raw_data(const pystring_ext &data)
{
if (smtp_session_status == SMTP_STATUS_DATAEND)
{
smtp_session_status = SMTP_STATUS_COMMAND;
}
//|| smtp_session_status == SMTP_STATUS_DATAEND
if (smtp_session_status == SMTP_STATUS_COMMAND)
{
std::vector<pystring_ext> vec_command = data.py_split(" ");
return read_command(vec_command);
}
else if (smtp_session_status == SMTP_STATUS_DATA)
{
if (data.py_endswith("\r\n.\r\n") == true)
{
//data.slice(0,-5)去掉末尾的\r\n.\r\n
smtp_info.session_data.append(data.py_slice(0, -5));
if (session_data_length != 0 &&
smtp_info.session_data.size() != session_data_length
)
{
command_rset();
return "500 Requested action not taken:data length error\r\n";
}
return command_data_end();
}
smtp_info.session_data.append(data);
return "";
}
else
{
return "";
}
}
bool is_data_end()
{
if (SMTP_STATUS_DATAEND == smtp_session_status)
{
return true;
}
return false;
}
bool is_protocol_close()
{
if (SMTP_STATUS_QUIT == smtp_session_status)
{
return true;
}
return false;
}
pystring_ext find_last_sender_ip()
{
//Received: from mail.begood.com.cn (unknown [111.75.205.179])
pystring_ext last_sender_ip_range =
smtp_info.session_data.py_between_start_and_end_string("(", ")");
pystring_ext last_sender_ip =
last_sender_ip_range.py_between_start_and_end_string("[", "]");
//#define INET6_ADDRSTRLEN 46
//验证IP长度,IPV4应该大于4,IPV6应该小于46
if (last_sender_ip.size() > 3 && last_sender_ip.size() < 46)
{
return last_sender_ip;
}
return "";
}
void add_last_sender_ip_to_data_header(const pystring_ext &pystr_ip)
{
pystring_ext pystr_header_line = "X-REAL-SENDER-IP: ";
pystr_header_line.append(pystr_ip);
#ifdef _MSC_VER
pystr_header_line.append("\r\n");
#else
pystr_header_line.append("\n");
#endif
pystr_header_line.append(smtp_info.session_data);
smtp_info.session_data = pystr_header_line;
}
};
#endif // __SMTP_PROTOCOL_CLASS_HPP__