-
Notifications
You must be signed in to change notification settings - Fork 271
/
smtp-server-connection.c
1617 lines (1364 loc) · 42.6 KB
/
smtp-server-connection.c
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "llist.h"
#include "array.h"
#include "str.h"
#include "ioloop.h"
#include "istream.h"
#include "ostream.h"
#include "iostream.h"
#include "connection.h"
#include "iostream-rawlog.h"
#include "iostream-ssl.h"
#include "master-service.h"
#include "master-service-ssl.h"
#include "smtp-syntax.h"
#include "smtp-reply-parser.h"
#include "smtp-command-parser.h"
#include "smtp-server-private.h"
const char *const smtp_server_state_names[] = {
"GREETING",
"XCLIENT",
"HELO",
"STARTTLS",
"AUTH",
"READY",
"MAIL FROM",
"RCPT TO",
"DATA"
};
/*
* Logging
*/
void smtp_server_connection_debug(struct smtp_server_connection *conn,
const char *format, ...)
{
va_list args;
if (conn->set.debug) {
va_start(args, format);
i_debug("%s-server: conn %s: %s",
smtp_protocol_name(conn->set.protocol),
smtp_server_connection_label(conn),
t_strdup_vprintf(format, args));
va_end(args);
}
}
void smtp_server_connection_error(struct smtp_server_connection *conn,
const char *format, ...)
{
va_list args;
va_start(args, format);
i_error("%s-server: conn %s: %s",
smtp_protocol_name(conn->set.protocol),
smtp_server_connection_label(conn),
t_strdup_vprintf(format, args));
va_end(args);
}
/*
* Connection
*/
static void
smtp_server_connection_input(struct connection *_conn);
static int
smtp_server_connection_output(struct smtp_server_connection *conn);
static void
smtp_server_connection_disconnect(struct smtp_server_connection *conn,
const char *reason) ATTR_NULL(2);
static void
smtp_server_connection_update_stats(struct smtp_server_connection *conn)
{
if (conn->conn.input != NULL)
conn->stats.input = conn->conn.input->v_offset;
if (conn->conn.output != NULL)
conn->stats.output = conn->conn.output->offset;
}
const struct smtp_server_stats *
smtp_server_connection_get_stats(struct smtp_server_connection *conn)
{
smtp_server_connection_update_stats(conn);
return &conn->stats;
}
void smtp_server_connection_input_halt(struct smtp_server_connection *conn)
{
connection_input_halt(&conn->conn);
}
void smtp_server_connection_input_resume(struct smtp_server_connection *conn)
{
struct smtp_server_command *cmd;
bool cmd_locked = FALSE;
if (conn->conn.io == NULL) {
/* only resume when we actually can */
if (conn->input_locked || conn->input_broken ||
conn->disconnected)
return;
if (conn->command_queue_count >
conn->server->set.max_pipelined_commands)
return;
/* is queued command still blocking input? */
cmd = conn->command_queue_head;
while (cmd != NULL) {
if (cmd->input_locked) {
cmd_locked = TRUE;
break;
}
cmd = cmd->next;
}
if (cmd_locked)
return;
/* restore input handler */
connection_input_resume(&conn->conn);
}
if (conn->conn.io != NULL &&
i_stream_have_bytes_left(conn->conn.input)) {
io_set_pending(conn->conn.io);
}
}
void smtp_server_connection_input_lock(struct smtp_server_connection *conn)
{
conn->input_locked = TRUE;
smtp_server_connection_input_halt(conn);
}
void smtp_server_connection_input_unlock(struct smtp_server_connection *conn)
{
conn->input_locked = FALSE;
smtp_server_connection_input_resume(conn);
}
#undef smtp_server_connection_input_capture
void smtp_server_connection_input_capture(struct smtp_server_connection *conn,
smtp_server_input_callback_t *callback, void *context)
{
i_assert(!conn->input_broken && !conn->disconnected);
connection_input_halt(&conn->conn);
conn->conn.io = io_add_istream(conn->conn.input, *callback, context);
}
static void
smtp_server_connection_update_rawlog(struct smtp_server_connection *conn)
{
struct stat st;
if (conn->set.rawlog_dir == NULL)
return;
if (!conn->rawlog_checked) {
conn->rawlog_checked = TRUE;
if (stat(conn->set.rawlog_dir, &st) == 0)
conn->rawlog_enabled = TRUE;
}
if (conn->rawlog_enabled) {
iostream_rawlog_create(conn->set.rawlog_dir,
&conn->conn.input, &conn->conn.output);
}
}
static void
smtp_server_connection_streams_changed(struct smtp_server_connection *conn)
{
smtp_server_connection_update_rawlog(conn);
smtp_command_parser_set_stream(conn->smtp_parser, conn->conn.input);
o_stream_set_flush_callback(conn->conn.output,
smtp_server_connection_output, conn);
o_stream_set_flush_pending(conn->conn.output, TRUE);
}
void smtp_server_connection_set_streams(struct smtp_server_connection *conn,
struct istream *input, struct ostream *output)
{
struct istream *old_input = conn->conn.input;
struct ostream *old_output = conn->conn.output;
i_assert(conn->created_from_streams);
conn->conn.input = input;
i_stream_ref(conn->conn.input);
conn->conn.output = output;
o_stream_ref(conn->conn.output);
o_stream_set_no_error_handling(conn->conn.output, TRUE);
i_stream_unref(&old_input);
o_stream_unref(&old_output);
smtp_server_connection_streams_changed(conn);
}
void smtp_server_connection_set_ssl_streams(struct smtp_server_connection *conn,
struct istream *input, struct ostream *output)
{
conn->ssl_secured = TRUE;
conn->set.capabilities &= ~SMTP_CAPABILITY_STARTTLS;
smtp_server_connection_set_streams(conn, input, output);
}
static void
smtp_server_connection_idle_timeout(struct smtp_server_connection *conn)
{
smtp_server_connection_terminate(&conn,
"4.4.2", "Disconnected for inactivity");
}
void smtp_server_connection_timeout_stop(struct smtp_server_connection *conn)
{
if (conn->to_idle != NULL) {
smtp_server_connection_debug(conn, "Timeout stop");
timeout_remove(&conn->to_idle);
}
}
void smtp_server_connection_timeout_start(struct smtp_server_connection *conn)
{
if (conn->disconnected)
return;
if (conn->to_idle == NULL &&
conn->set.max_client_idle_time_msecs > 0) {
smtp_server_connection_debug(conn, "Timeout start");
conn->to_idle = timeout_add(
conn->set.max_client_idle_time_msecs,
smtp_server_connection_idle_timeout, conn);
}
}
void smtp_server_connection_timeout_reset(struct smtp_server_connection *conn)
{
if (conn->to_idle != NULL)
timeout_reset(conn->to_idle);
}
static void
smtp_server_connection_timeout_update(struct smtp_server_connection *conn)
{
struct smtp_server_command *cmd = conn->command_queue_head;
if (cmd == NULL) {
smtp_server_connection_timeout_start(conn);
return;
}
switch (cmd->state) {
case SMTP_SERVER_COMMAND_STATE_NEW:
smtp_server_connection_timeout_start(conn);
break;
case SMTP_SERVER_COMMAND_STATE_PROCESSING:
if (cmd->input_captured) {
/* command updates timeout internally */
return;
}
smtp_server_connection_timeout_stop(conn);
break;
case SMTP_SERVER_COMMAND_STATE_SUBMITTED_REPLY:
case SMTP_SERVER_COMMAND_STATE_READY_TO_REPLY:
smtp_server_connection_timeout_stop(conn);
break;
case SMTP_SERVER_COMMAND_STATE_FINISHED:
case SMTP_SERVER_COMMAND_STATE_ABORTED:
i_unreached();
}
}
static void
smtp_server_connection_ready(struct smtp_server_connection *conn)
{
conn->raw_input = conn->conn.input;
conn->raw_output = conn->conn.output;
smtp_server_connection_update_rawlog(conn);
conn->smtp_parser = smtp_command_parser_init(conn->conn.input,
&conn->set.command_limits);
o_stream_set_flush_callback(conn->conn.output,
smtp_server_connection_output, conn);
o_stream_cork(conn->conn.output);
if (conn->authenticated) {
/* RFC 4954, Section 4:
Should the client successfully complete the exchange, the
SMTP server issues a 235 reply. */
smtp_server_connection_send_line(conn,
"235 2.7.0 Logged in.");
} else {
smtp_server_connection_send_line(conn,
"220 %s %s", conn->set.hostname,
conn->set.login_greeting);
}
if (!conn->corked)
o_stream_uncork(conn->conn.output);
}
static void smtp_server_connection_destroy(struct connection *_conn)
{
struct smtp_server_connection *conn =
(struct smtp_server_connection *)_conn;
smtp_server_connection_disconnect(conn, NULL);
smtp_server_connection_unref(&conn);
}
static bool
smtp_server_connection_handle_command(struct smtp_server_connection *conn,
const char *cmd_name, const char *cmd_params)
{
struct smtp_server_connection *tmp_conn = conn;
struct smtp_server_command *cmd;
smtp_server_connection_ref(tmp_conn);
cmd = smtp_server_command_new(tmp_conn, cmd_name, cmd_params);
if (!smtp_server_connection_unref(&tmp_conn)) {
/* the command start callback managed to get this connection
destroyed */
return FALSE;
}
if (cmd != NULL && conn->command_queue_head == cmd)
(void)smtp_server_command_next_to_reply(&cmd);
smtp_server_connection_timeout_update(conn);
return (cmd == NULL || !cmd->input_locked);
}
static int
smtp_server_connection_init_ssl_ctx(struct smtp_server_connection *conn,
const char **error_r)
{
struct smtp_server *server = conn->server;
const char *error;
if (conn->ssl_ctx != NULL || conn->set.ssl == NULL)
return 0;
if (conn->set.ssl == server->set.ssl) {
if (smtp_server_init_ssl_ctx(server, error_r) < 0)
return -1;
conn->ssl_ctx = server->ssl_ctx;
ssl_iostream_context_ref(conn->ssl_ctx);
return 0;
}
if (ssl_iostream_server_context_cache_get(conn->set.ssl,
&conn->ssl_ctx, &error) < 0) {
*error_r = t_strdup_printf("Couldn't initialize SSL context: %s",
error);
return -1;
}
return 0;
}
int smtp_server_connection_ssl_init(struct smtp_server_connection *conn)
{
const char *error;
int ret;
if (smtp_server_connection_init_ssl_ctx(conn, &error) < 0) {
smtp_server_connection_error(conn,
"Couldn't initialize SSL: %s", error);
return -1;
}
smtp_server_connection_debug(conn, "Starting SSL handshake");
if (conn->raw_input != conn->conn.input) {
/* recreate rawlog after STARTTLS */
i_stream_ref(conn->raw_input);
o_stream_ref(conn->raw_output);
i_stream_destroy(&conn->conn.input);
o_stream_destroy(&conn->conn.output);
conn->conn.input = conn->raw_input;
conn->conn.output = conn->raw_output;
}
smtp_server_connection_input_halt(conn);
if (conn->ssl_ctx == NULL) {
ret = master_service_ssl_init(master_service,
&conn->conn.input, &conn->conn.output,
&conn->ssl_iostream, &error);
} else {
ret = io_stream_create_ssl_server(conn->ssl_ctx,
conn->set.ssl, &conn->conn.input, &conn->conn.output,
&conn->ssl_iostream, &error);
}
if (ret < 0) {
smtp_server_connection_error(conn,
"Couldn't initialize SSL server for %s: %s",
conn->conn.name, error);
return -1;
}
smtp_server_connection_input_resume(conn);
if (ssl_iostream_handshake(conn->ssl_iostream) < 0) {
smtp_server_connection_error(conn,
"SSL handshake failed: %s",
ssl_iostream_get_last_error(conn->ssl_iostream));
return -1;
}
conn->ssl_secured = TRUE;
conn->set.capabilities &= ~SMTP_CAPABILITY_STARTTLS;
if (conn->ssl_start)
smtp_server_connection_ready(conn);
else
smtp_server_connection_streams_changed(conn);
return 0;
}
static void
smtp_server_connection_handle_input(struct smtp_server_connection *conn)
{
struct smtp_server_command *pending_command;
enum smtp_command_parse_error error_code;
const char *cmd_name, *cmd_params, *error;
int ret;
/* check whether we are continuing a command */
pending_command = NULL;
if (conn->command_queue_tail != NULL) {
pending_command = (conn->command_queue_tail->state ==
SMTP_SERVER_COMMAND_STATE_SUBMITTED_REPLY ?
conn->command_queue_tail : NULL);
}
smtp_server_connection_timeout_reset(conn);
/* parse commands */
ret = 1;
while (!conn->closing && ret != 0) {
while ((ret = smtp_command_parse_next(conn->smtp_parser,
&cmd_name, &cmd_params, &error_code, &error)) > 0) {
if (pending_command != NULL) {
/* previous command is now fully read and ready
to reply */
smtp_server_command_ready_to_reply(pending_command);
pending_command = NULL;
}
smtp_server_connection_debug(conn,
"Received new command: %s %s",
cmd_name, cmd_params);
conn->stats.command_count++;
/* handle command
cmd may be destroyed after this */
if (!smtp_server_connection_handle_command(conn,
cmd_name, cmd_params))
return;
if (conn->disconnected)
return;
/* client indicated it will close after this command;
stop trying to read more. */
if (conn->closing)
break;
if (conn->command_queue_count >=
conn->server->set.max_pipelined_commands) {
smtp_server_connection_input_halt(conn);
return;
}
if (conn->command_queue_tail != NULL) {
pending_command = (conn->command_queue_tail->state ==
SMTP_SERVER_COMMAND_STATE_SUBMITTED_REPLY ?
conn->command_queue_tail : NULL);
}
}
if (ret < 0 && conn->conn.input->eof) {
int stream_errno = conn->conn.input->stream_errno;
if (stream_errno != 0 && stream_errno != EPIPE &&
stream_errno != ECONNRESET) {
smtp_server_connection_error(conn,
"Connection lost: read(%s) failed: %s",
i_stream_get_name(conn->conn.input),
i_stream_get_error(conn->conn.input));
smtp_server_connection_close(&conn,
"Read failure");
} else {
smtp_server_connection_debug(conn,
"Connection lost: Remote disconnected");
if (conn->command_queue_head == NULL) {
/* no pending commands; close */
smtp_server_connection_close(&conn,
"Remote closed connection");
} else if (conn->command_queue_head->state <
SMTP_SERVER_COMMAND_STATE_SUBMITTED_REPLY) {
/* unfinished command; close */
smtp_server_connection_close(&conn,
"Remote closed connection unexpectedly");
} else {
/* a command is still processing;
only drop input io for now */
conn->input_broken = TRUE;
smtp_server_connection_input_halt(conn);
}
}
return;
}
if (ret < 0) {
struct smtp_server_command *cmd;
smtp_server_connection_debug(conn,
"Client sent invalid command: %s", error);
switch (error_code) {
case SMTP_COMMAND_PARSE_ERROR_BROKEN_COMMAND:
conn->input_broken = TRUE;
/* fall through */
case SMTP_COMMAND_PARSE_ERROR_BAD_COMMAND:
cmd = smtp_server_command_alloc(conn);
smtp_server_command_fail(cmd,
500, "5.5.2", "Invalid command syntax");
break;
case SMTP_COMMAND_PARSE_ERROR_LINE_TOO_LONG:
cmd = smtp_server_command_alloc(conn);
smtp_server_command_fail(cmd,
500, "5.5.2", "Line too long");
break;
case SMTP_COMMAND_PARSE_ERROR_DATA_TOO_LARGE:
/* Command data size exceeds the absolute limit;
i.e. beyond which we don't even want to skip
data anymore. The command error is usually
already submitted by the application and sent
to the client. */
smtp_server_connection_close(&conn,
"Command data size exceeds absolute limit");
return;
case SMTP_COMMAND_PARSE_ERROR_BROKEN_STREAM:
smtp_server_connection_close(&conn,
"Command data ended prematurely");
return;
default:
i_unreached();
}
}
if (conn->disconnected)
return;
if (conn->input_broken || conn->closing) {
smtp_server_connection_input_halt(conn);
return;
}
if (ret == 0 && pending_command != NULL &&
!smtp_command_parser_pending_data(conn->smtp_parser)) {
/* previous command is now fully read and ready to
reply */
smtp_server_command_ready_to_reply(pending_command);
}
}
}
static void smtp_server_connection_input(struct connection *_conn)
{
struct smtp_server_connection *conn =
(struct smtp_server_connection *)_conn;
i_assert(!conn->input_broken);
if (conn->handling_input)
return;
smtp_server_connection_timeout_reset(conn);
if (conn->ssl_start && conn->ssl_iostream == NULL) {
if (smtp_server_connection_ssl_init(conn) < 0) {
smtp_server_connection_close(&conn,
"SSL Initialization failed");
return;
}
if (conn->halted) {
smtp_server_connection_input_lock(conn);
return;
}
}
i_assert(!conn->halted);
if (conn->command_queue_count >
conn->server->set.max_pipelined_commands) {
smtp_server_connection_input_halt(conn);
return;
}
smtp_server_connection_ref(conn);
conn->handling_input = TRUE;
if (conn->callbacks != NULL &&
conn->callbacks->conn_cmd_input_pre != NULL) {
conn->callbacks->conn_cmd_input_pre(conn->context);
}
smtp_server_connection_handle_input(conn);
if (conn->callbacks != NULL &&
conn->callbacks->conn_cmd_input_post != NULL) {
conn->callbacks->conn_cmd_input_post(conn->context);
}
conn->handling_input = FALSE;
smtp_server_connection_unref(&conn);
}
bool smtp_server_connection_pending_command_data(
struct smtp_server_connection *conn)
{
if (conn->smtp_parser == NULL)
return FALSE;
return smtp_command_parser_pending_data(conn->smtp_parser);
}
/*
* Command reply handling
*/
void smtp_server_connection_handle_output_error(
struct smtp_server_connection *conn)
{
struct ostream *output = conn->conn.output;
if (output->stream_errno != EPIPE &&
output->stream_errno != ECONNRESET) {
smtp_server_connection_error(conn,
"Connection lost: write(%s) failed: %s",
o_stream_get_name(output),
o_stream_get_error(output));
smtp_server_connection_close(&conn,
"Write failure");
} else {
smtp_server_connection_debug(conn,
"Connection lost: Remote disconnected");
smtp_server_connection_close(&conn,
"Remote closed connection unexpectedly");
}
}
static bool
smtp_server_connection_next_reply(struct smtp_server_connection *conn)
{
struct smtp_server_command *cmd;
unsigned int i;
cmd = conn->command_queue_head;
if (cmd == NULL) {
/* no commands pending */
smtp_server_connection_debug(conn,
"No more commands pending");
return FALSE;
}
if (cmd->state < SMTP_SERVER_COMMAND_STATE_READY_TO_REPLY) {
(void)smtp_server_command_next_to_reply(&cmd);
return FALSE;
}
i_assert(cmd->state == SMTP_SERVER_COMMAND_STATE_READY_TO_REPLY &&
array_is_created(&cmd->replies));
if (!smtp_server_command_completed(&cmd))
return TRUE;
/* send command replies */
// FIXME: handle LMTP DATA command with enormous number of recipients;
// i.e. don't keep filling output stream with replies indefinitely.
for (i = 0; i < cmd->replies_expected; i++) {
struct smtp_server_reply *reply;
reply = array_idx_modifiable(&cmd->replies, i);
if (!reply->submitted) {
i_assert(!reply->sent);
cmd->state = SMTP_SERVER_COMMAND_STATE_PROCESSING;
break;
}
if (smtp_server_reply_send(reply) < 0)
return FALSE;
}
if (cmd->state == SMTP_SERVER_COMMAND_STATE_PROCESSING)
return FALSE;
smtp_server_command_finished(cmd);
return TRUE;
}
void smtp_server_connection_cork(struct smtp_server_connection *conn)
{
conn->corked = TRUE;
if (conn->conn.output != NULL)
o_stream_cork(conn->conn.output);
}
void smtp_server_connection_uncork(struct smtp_server_connection *conn)
{
conn->corked = FALSE;
if (conn->conn.output != NULL) {
if (o_stream_uncork_flush(conn->conn.output) < 0) {
smtp_server_connection_handle_output_error(conn);
return;
}
smtp_server_connection_trigger_output(conn);
}
}
static void
smtp_server_connection_send_replies(struct smtp_server_connection *conn)
{
/* send more replies until no more replies remain, the output
blocks again, or the connection is closed */
while (!conn->disconnected && smtp_server_connection_next_reply(conn));
smtp_server_connection_timeout_update(conn);
/* accept more commands if possible */
smtp_server_connection_input_resume(conn);
}
int smtp_server_connection_flush(struct smtp_server_connection *conn)
{
struct ostream *output = conn->conn.output;
int ret;
if ((ret = o_stream_flush(output)) <= 0) {
if (ret < 0)
smtp_server_connection_handle_output_error(conn);
return ret;
}
return 1;
}
static int
smtp_server_connection_output(struct smtp_server_connection *conn)
{
int ret;
smtp_server_connection_debug(conn,
"Sending replies");
smtp_server_connection_ref(conn);
o_stream_cork(conn->conn.output);
if ((ret=smtp_server_connection_flush(conn)) > 0) {
smtp_server_connection_timeout_reset(conn);
smtp_server_connection_send_replies(conn);
}
if (ret >= 0 && !conn->corked && conn->conn.output != NULL) {
if ((ret=o_stream_uncork_flush(conn->conn.output)) < 0)
smtp_server_connection_handle_output_error(conn);
}
smtp_server_connection_unref(&conn);
return ret;
}
void smtp_server_connection_trigger_output(
struct smtp_server_connection *conn)
{
if (conn->conn.output != NULL) {
smtp_server_connection_debug(conn,
"Trigger output");
o_stream_set_flush_pending(conn->conn.output, TRUE);
}
}
/*
*
*/
static struct connection_settings smtp_server_connection_set = {
.input_max_size = (size_t)-1,
.output_max_size = (size_t)-1,
.client = FALSE,
.log_connection_id = TRUE,
};
static const struct connection_vfuncs smtp_server_connection_vfuncs = {
.destroy = smtp_server_connection_destroy,
.input = smtp_server_connection_input
};
struct connection_list *
smtp_server_connection_list_init(void)
{
return connection_list_init(&smtp_server_connection_set,
&smtp_server_connection_vfuncs);
}
static struct smtp_server_connection * ATTR_NULL(5, 6)
smtp_server_connection_alloc(struct smtp_server *server,
const struct smtp_server_settings *set, int fd_in, int fd_out,
const struct ip_addr *remote_ip, in_port_t remote_port,
const struct smtp_server_callbacks *callbacks, void *context)
{
static unsigned int id = 0;
struct smtp_server_connection *conn;
pool_t pool;
pool = pool_alloconly_create("smtp server", 1024);
conn = p_new(pool, struct smtp_server_connection, 1);
conn->pool = pool;
conn->refcount = 1;
conn->id = id++;
conn->server = server;
conn->callbacks = callbacks;
conn->context = context;
/* merge settings with global server settings */
conn->set = server->set;
if (set != NULL) {
conn->set.protocol = server->set.protocol;
if (set->rawlog_dir != NULL && *set->rawlog_dir != '\0')
conn->set.rawlog_dir = p_strdup(pool, set->rawlog_dir);
if (set->ssl != NULL)
conn->set.ssl = ssl_iostream_settings_dup(pool, set->ssl);
if (set->hostname != NULL && *set->hostname != '\0')
conn->set.hostname = p_strdup(pool, set->hostname);
if (set->login_greeting != NULL &&
*set->login_greeting != '\0') {
conn->set.login_greeting =
p_strdup(pool, set->login_greeting);
}
if (set->capabilities != 0)
conn->set.capabilities = set->capabilities;
conn->set.workarounds |= set->workarounds;
if (set->max_client_idle_time_msecs > 0) {
conn->set.max_client_idle_time_msecs =
set->max_client_idle_time_msecs;
}
if (set->max_pipelined_commands > 0) {
conn->set.max_pipelined_commands =
set->max_pipelined_commands;
}
if (set->max_bad_commands > 0) {
conn->set.max_bad_commands = set->max_bad_commands;
}
if (set->max_recipients > 0)
conn->set.max_recipients = set->max_recipients;
smtp_command_limits_merge(&conn->set.command_limits,
&set->command_limits);
conn->set.max_message_size = set->max_message_size;
if (set->max_message_size == 0 ||
set->max_message_size == (uoff_t)-1) {
conn->set.command_limits.max_data_size = UOFF_T_MAX;
} else if (conn->set.command_limits.max_data_size != 0) {
/* explicit limit given */
} else if (set->max_message_size >
(UOFF_T_MAX - SMTP_SERVER_DEFAULT_MAX_SIZE_EXCESS_LIMIT)) {
/* very high limit */
conn->set.command_limits.max_data_size = UOFF_T_MAX;
} else {
/* absolute maximum before connection is closed in DATA
command */
conn->set.command_limits.max_data_size =
set->max_message_size +
SMTP_SERVER_DEFAULT_MAX_SIZE_EXCESS_LIMIT;
}
if (set->mail_param_extensions != NULL) {
conn->set.mail_param_extensions =
p_strarray_dup(pool, set->mail_param_extensions);
}
if (set->rcpt_param_extensions != NULL) {
conn->set.rcpt_param_extensions =
p_strarray_dup(pool, set->rcpt_param_extensions);
}
if (set->xclient_extensions != NULL) {
server->set.xclient_extensions =
p_strarray_dup(pool, set->xclient_extensions);
}
if (set->socket_send_buffer_size > 0) {
conn->set.socket_send_buffer_size =
set->socket_send_buffer_size;
}
if (set->socket_recv_buffer_size > 0) {
conn->set.socket_recv_buffer_size =
set->socket_recv_buffer_size;
}
conn->set.tls_required =
conn->set.tls_required || set->tls_required;
conn->set.auth_optional =
conn->set.auth_optional || set->auth_optional;
conn->set.rcpt_domain_optional =
conn->set.rcpt_domain_optional ||
set->rcpt_domain_optional;
conn->set.debug = conn->set.debug || set->debug;
}
if (set != NULL && set->mail_param_extensions != NULL) {
const char *const *extp;
p_array_init(&conn->mail_param_extensions, pool,
str_array_length(set->mail_param_extensions) + 8);
for (extp = set->mail_param_extensions; *extp != NULL; extp++) {
const char *ext = p_strdup(pool, *extp);
array_push_back(&conn->mail_param_extensions, &ext);
}
array_append_zero(&conn->mail_param_extensions);
}
if (set != NULL && set->rcpt_param_extensions != NULL) {
const char *const *extp;
p_array_init(&conn->rcpt_param_extensions, pool,
str_array_length(set->rcpt_param_extensions) + 8);
for (extp = set->rcpt_param_extensions; *extp != NULL; extp++) {
const char *ext = p_strdup(pool, *extp);
array_push_back(&conn->rcpt_param_extensions, &ext);
}
array_append_zero(&conn->rcpt_param_extensions);
}
conn->remote_pid = (pid_t)-1;
conn->remote_uid = (uid_t)-1;
if (remote_ip != NULL && remote_ip->family != 0) {
conn->remote_ip = *remote_ip;
conn->remote_port = remote_port;
conn->socket_family = conn->remote_ip.family;
} else if (fd_in != fd_out || net_getpeername(fd_in,
&conn->remote_ip, &conn->remote_port) < 0) {
/* tty connection probably */
} else {
if (conn->remote_ip.family == 0) {
struct net_unix_cred cred;
/* unix */
conn->socket_family = AF_UNIX;
if (net_getunixcred(fd_in, &cred) >= 0) {
conn->remote_pid = cred.pid;
conn->remote_uid = cred.uid;
}
} else {
/* ip / ip6 */
conn->socket_family = conn->remote_ip.family;
}
}
net_set_nonblock(fd_in, TRUE);
if (fd_in != fd_out)
net_set_nonblock(fd_out, TRUE);
(void)net_set_tcp_nodelay(fd_out, TRUE);
set = &conn->set;
if (set->socket_send_buffer_size > 0) {
if (net_set_send_buffer_size(fd_out,
set->socket_send_buffer_size) < 0)
i_error("net_set_send_buffer_size(%"PRIuSIZE_T") failed: %m",
set->socket_send_buffer_size);
}
if (set->socket_recv_buffer_size > 0) {
if (net_set_recv_buffer_size(fd_in,
set->socket_recv_buffer_size) < 0)
i_error("net_set_recv_buffer_size(%"PRIuSIZE_T") failed: %m",
set->socket_recv_buffer_size);
}
return conn;
}
struct smtp_server_connection *
smtp_server_connection_create(struct smtp_server *server,
int fd_in, int fd_out,
const struct ip_addr *remote_ip, in_port_t remote_port,
bool ssl_start, const struct smtp_server_settings *set,
const struct smtp_server_callbacks *callbacks, void *context)
{
struct smtp_server_connection *conn;
conn = smtp_server_connection_alloc(server,
set, fd_in, fd_out, remote_ip, remote_port,
callbacks, context);
connection_init_server_ip(server->conn_list, &conn->conn, NULL,
fd_in, fd_out, remote_ip, remote_port);
conn->ssl_start = ssl_start;
if (ssl_start)
conn->set.capabilities &= ~SMTP_CAPABILITY_STARTTLS;