-
Notifications
You must be signed in to change notification settings - Fork 271
/
smtp-client-command.c
1522 lines (1293 loc) · 40.3 KB
/
smtp-client-command.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 "buffer.h"
#include "array.h"
#include "str.h"
#include "llist.h"
#include "istream.h"
#include "istream-crlf.h"
#include "ostream.h"
#include "ostream-dot.h"
#include "smtp-common.h"
#include "smtp-syntax.h"
#include "smtp-params.h"
#include "smtp-client-private.h"
/*
*
*/
static const char *smtp_client_command_label
(struct smtp_client_command *cmd)
{
const unsigned char *p, *pend;
if (cmd->plug)
return "[plug]";
if (cmd->name != NULL)
return cmd->name;
if (cmd->data == NULL || cmd->data->used == 0) {
if (!cmd->has_stream)
return "[empty]";
cmd->name = "[data]";
return cmd->name;
}
p = cmd->data->data;
pend = p + cmd->data->used;
for (;p < pend; p++) {
if (*p == ' ' || *p == '\r' || *p == '\n')
break;
}
cmd->name = p_strdup(cmd->pool,
t_str_ucase(t_strdup_until(cmd->data->data, p)));
return cmd->name;
}
/*
* Logging
*/
static inline void ATTR_FORMAT(2, 3)
smtp_client_command_debug(struct smtp_client_command *cmd,
const char *format, ...)
{
struct smtp_client_connection *conn = cmd->conn;
va_list args;
if (conn->set.debug) {
va_start(args, format);
i_debug("%s-client: conn %s: command %s: %s",
smtp_protocol_name(conn->protocol),
smpt_client_connection_label(conn),
smtp_client_command_label(cmd),
t_strdup_vprintf(format, args));
va_end(args);
}
}
static inline void ATTR_FORMAT(2, 3)
smtp_client_command_error(struct smtp_client_command *cmd,
const char *format, ...)
{
struct smtp_client_connection *conn = cmd->conn;
va_list args;
va_start(args, format);
i_error("%s-client: conn %s: command %s: %s",
smtp_protocol_name(conn->protocol),
smpt_client_connection_label(conn),
smtp_client_command_label(cmd),
t_strdup_vprintf(format, args));
va_end(args);
}
/*
*
*/
static struct smtp_client_command *
smtp_client_command_create(struct smtp_client_connection *conn,
enum smtp_client_command_flags flags,
smtp_client_command_callback_t *callback, void *context)
{
struct smtp_client_command *cmd;
pool_t pool;
pool = pool_alloconly_create("smtp client command", 2048);
cmd = p_new(pool, struct smtp_client_command, 1);
cmd->pool = pool;
cmd->refcount = 1;
cmd->conn = conn;
cmd->flags = flags;
cmd->replies_expected = 1;
cmd->callback = callback;
cmd->context = context;
return cmd;
}
#undef smtp_client_command_new
struct smtp_client_command *
smtp_client_command_new(struct smtp_client_connection *conn,
enum smtp_client_command_flags flags,
smtp_client_command_callback_t *callback, void *context)
{
i_assert(callback != NULL);
return smtp_client_command_create(conn, flags, callback, context);
}
struct smtp_client_command *
smtp_client_command_plug(struct smtp_client_connection *conn,
struct smtp_client_command *after)
{
struct smtp_client_command *cmd;
cmd = smtp_client_command_create(conn, 0, NULL, NULL);
cmd->plug = TRUE;
smtp_client_command_submit_after(cmd, after);
return cmd;
}
void smtp_client_command_ref(struct smtp_client_command *cmd)
{
cmd->refcount++;
}
void smtp_client_command_unref(struct smtp_client_command **_cmd)
{
struct smtp_client_command *cmd = *_cmd;
struct smtp_client_connection *conn = cmd->conn;
i_assert(cmd->refcount > 0);
if (--cmd->refcount > 0)
return;
smtp_client_command_debug(cmd, "Destroy "
"(%u commands pending, %u commands queued)",
conn->cmd_wait_list_count, conn->cmd_send_queue_count);
i_assert(cmd->state >= SMTP_CLIENT_COMMAND_STATE_FINISHED);
i_stream_unref(&cmd->stream);
pool_unref(&cmd->pool);
*_cmd = NULL;
}
bool smtp_client_command_name_equals(struct smtp_client_command *cmd,
const char *name)
{
const unsigned char *data;
size_t name_len, data_len;
if (cmd->data == NULL)
return FALSE;
name_len = strlen(name);
data = cmd->data->data;
data_len = cmd->data->used;
if (data_len < name_len ||
i_memcasecmp(data, name, name_len) != 0)
return FALSE;
return (data_len == name_len ||
data[name_len] == ' ' || data[name_len] == '\r');
}
void smtp_client_command_lock(struct smtp_client_command *cmd)
{
if (cmd->plug)
return;
cmd->locked = TRUE;
}
void smtp_client_command_unlock(struct smtp_client_command *cmd)
{
if (cmd->plug)
return;
if (cmd->locked) {
cmd->locked = FALSE;
if (!cmd->conn->corked)
smtp_client_connection_trigger_output(cmd->conn);
}
}
void smtp_client_command_abort(struct smtp_client_command **_cmd)
{
struct smtp_client_command *cmd = *_cmd;
struct smtp_client_connection *conn = cmd->conn;
enum smtp_client_command_state state = cmd->state;
bool disconnected =
(conn->state == SMTP_CLIENT_CONNECTION_STATE_DISCONNECTED);
bool waslocked =
(state >= SMTP_CLIENT_COMMAND_STATE_SUBMITTED) &&
(cmd->locked || cmd->plug);
*_cmd = NULL;
smtp_client_command_drop_callback(cmd);
if ((!disconnected && !cmd->plug && cmd->aborting) ||
state >= SMTP_CLIENT_COMMAND_STATE_FINISHED)
return;
if (disconnected || state <= SMTP_CLIENT_COMMAND_STATE_SUBMITTED) {
smtp_client_command_debug(cmd, "Abort");
cmd->state = SMTP_CLIENT_COMMAND_STATE_ABORTED;
} else {
smtp_client_command_debug(cmd, "Abort (already sent)");
i_assert(state < SMTP_CLIENT_COMMAND_STATE_FINISHED);
cmd->aborting = TRUE;
}
cmd->locked = FALSE;
i_assert(!cmd->plug || state <= SMTP_CLIENT_COMMAND_STATE_SUBMITTED);
switch (state) {
case SMTP_CLIENT_COMMAND_STATE_NEW:
if (cmd->delaying_failure) {
DLLIST_REMOVE(&conn->cmd_fail_list, cmd);
if (conn->cmd_fail_list == NULL)
timeout_remove(&conn->to_cmd_fail);
}
break;
case SMTP_CLIENT_COMMAND_STATE_SENDING:
if (!disconnected) {
/* it is being sent; cannot truly abort it now */
break;
}
/* fall through */
case SMTP_CLIENT_COMMAND_STATE_SUBMITTED:
/* not yet sent */
smtp_client_command_debug(cmd, "Removed from send queue");
i_assert(conn->cmd_send_queue_count > 0);
DLLIST2_REMOVE(&conn->cmd_send_queue_head,
&conn->cmd_send_queue_tail, cmd);
i_assert(conn->cmd_send_queue_count > 1 ||
(cmd->prev == NULL && cmd->next == NULL));
conn->cmd_send_queue_count--;
break;
case SMTP_CLIENT_COMMAND_STATE_WAITING:
if (!disconnected) {
/* we're expecting a reply; cannot truly abort it now */
break;
}
smtp_client_command_debug(cmd, "Removed from wait list");
i_assert(conn->cmd_wait_list_count > 0);
DLLIST2_REMOVE(&conn->cmd_wait_list_head,
&conn->cmd_wait_list_tail, cmd);
conn->cmd_wait_list_count--;
break;
default:
i_unreached();
}
if (cmd->abort_callback != NULL) {
cmd->abort_callback(cmd->abort_context);
cmd->abort_callback = NULL;
}
if (disconnected || cmd->plug ||
state <= SMTP_CLIENT_COMMAND_STATE_SUBMITTED) {
/* can only destroy it when it is not pending */
smtp_client_command_unref(&cmd);
}
if (!disconnected && waslocked && !conn->corked)
smtp_client_connection_trigger_output(conn);
}
void smtp_client_command_drop_callback(struct smtp_client_command *cmd)
{
cmd->callback = NULL;
cmd->context = NULL;
}
void smtp_client_command_fail_reply(struct smtp_client_command **_cmd,
const struct smtp_reply *reply)
{
struct smtp_client_command *cmd = *_cmd, *tmp_cmd;
struct smtp_client_connection *conn = cmd->conn;
enum smtp_client_command_state state = cmd->state;
smtp_client_command_callback_t *callback = cmd->callback;
*_cmd = NULL;
if (state >= SMTP_CLIENT_COMMAND_STATE_FINISHED)
return;
if (cmd->delay_failure) {
i_assert(cmd->delayed_failure == NULL);
i_assert(state < SMTP_CLIENT_COMMAND_STATE_SUBMITTED);
smtp_client_command_debug(cmd, "Fail (delay)");
cmd->delayed_failure = smtp_reply_clone(cmd->pool, reply);
cmd->delaying_failure = TRUE;
if (conn->to_cmd_fail == NULL) {
conn->to_cmd_fail = timeout_add_short(0,
smtp_client_commands_fail_delayed, conn);
}
DLLIST_PREPEND(&conn->cmd_fail_list, cmd);
return;
}
cmd->callback = NULL;
smtp_client_connection_ref(conn);
smtp_client_command_ref(cmd);
if (!cmd->aborting) {
smtp_client_command_debug(cmd, "Fail");
if (callback != NULL)
(void)callback(reply, cmd->context);
}
tmp_cmd = cmd;
smtp_client_command_abort(&tmp_cmd);
smtp_client_command_unref(&cmd);
smtp_client_connection_unref(&conn);
}
void smtp_client_command_fail(struct smtp_client_command **_cmd,
unsigned int status, const char *error)
{
struct smtp_reply reply;
const char *text_lines[] = {error, NULL};
i_zero(&reply);
reply.status = status;
reply.text_lines = text_lines;
reply.enhanced_code.x = 9;
smtp_client_command_fail_reply(_cmd, &reply);
}
static void
smtp_client_command_fail_delayed(struct smtp_client_command **_cmd)
{
struct smtp_client_command *cmd = *_cmd;
smtp_client_command_debug(cmd, "Fail delayed");
i_assert(!cmd->delay_failure);
i_assert(cmd->state < SMTP_CLIENT_COMMAND_STATE_FINISHED);
smtp_client_command_fail_reply(_cmd, cmd->delayed_failure);
}
void smtp_client_commands_list_abort(struct smtp_client_command *cmds_list,
unsigned int cmds_list_count)
{
struct smtp_client_command *cmd;
ARRAY(struct smtp_client_command *) cmds_arr;
struct smtp_client_command **cmds;
unsigned int count, i;
if (cmds_list == NULL)
return;
i_assert(cmds_list_count > 0);
/* copy the array and reference the commands to be robust against more
than one command disappearing from the list */
t_array_init(&cmds_arr, cmds_list_count);
for (cmd = cmds_list; cmd != NULL; cmd = cmd->next) {
smtp_client_command_ref(cmd);
array_push_back(&cmds_arr, &cmd);
}
cmds = array_get_modifiable(&cmds_arr, &count);
for (i = 0; i < count; i++) {
cmd = cmds[i];
/* fail the reply */
smtp_client_command_abort(&cmds[i]);
/* drop our reference */
smtp_client_command_unref(&cmd);
}
}
void smtp_client_commands_list_fail_reply(
struct smtp_client_command *cmds_list, unsigned int cmds_list_count,
const struct smtp_reply *reply)
{
struct smtp_client_command *cmd;
ARRAY(struct smtp_client_command *) cmds_arr;
struct smtp_client_command **cmds;
unsigned int count, i;
if (cmds_list == NULL)
return;
i_assert(cmds_list_count > 0);
/* copy the array and reference the commands to be robust against more
than one command disappearing from the list */
t_array_init(&cmds_arr, cmds_list_count);
for (cmd = cmds_list; cmd != NULL; cmd = cmd->next) {
smtp_client_command_ref(cmd);
array_push_back(&cmds_arr, &cmd);
}
cmds = array_get_modifiable(&cmds_arr, &count);
for (i = 0; i < count; i++) {
cmd = cmds[i];
/* fail the reply */
smtp_client_command_fail_reply(&cmds[i], reply);
/* drop our reference */
smtp_client_command_unref(&cmd);
}
}
void smtp_client_commands_abort_delayed(struct smtp_client_connection *conn)
{
struct smtp_client_command *cmd;
timeout_remove(&conn->to_cmd_fail);
cmd = conn->cmd_fail_list;
conn->cmd_fail_list = NULL;
while (cmd != NULL) {
struct smtp_client_command *cmd_next = cmd->next;
cmd->delaying_failure = FALSE;
smtp_client_command_abort(&cmd);
cmd = cmd_next;
}
}
void smtp_client_commands_fail_delayed(struct smtp_client_connection *conn)
{
struct smtp_client_command *cmd;
timeout_remove(&conn->to_cmd_fail);
cmd = conn->cmd_fail_list;
conn->cmd_fail_list = NULL;
while (cmd != NULL) {
struct smtp_client_command *cmd_next = cmd->next;
cmd->delaying_failure = FALSE;
smtp_client_command_fail_delayed(&cmd);
cmd = cmd_next;
}
}
void smtp_client_command_set_abort_callback(struct smtp_client_command *cmd,
void (*callback)(void *context), void *context)
{
cmd->abort_callback = callback;
cmd->abort_context = context;
}
void smtp_client_command_set_sent_callback(struct smtp_client_command *cmd,
void (*callback)(void *context), void *context)
{
cmd->sent_callback = callback;
cmd->sent_context = context;
}
void smtp_client_command_set_replies(struct smtp_client_command *cmd,
unsigned int replies)
{
i_assert(cmd->replies_expected == 1 ||
cmd->replies_expected == replies);
i_assert(replies > 0);
i_assert(cmd->replies_seen <= 1);
cmd->replies_expected = replies;
}
static void
smtp_client_command_sent(struct smtp_client_command *cmd)
{
if (cmd->data == NULL)
smtp_client_command_debug(cmd, "Sent");
else {
i_assert(str_len(cmd->data) > 2);
str_truncate(cmd->data, str_len(cmd->data)-2);
smtp_client_command_debug(cmd, "Sent: %s", str_c(cmd->data));
}
if (smtp_client_command_name_equals(cmd, "QUIT"))
cmd->conn->sent_quit = TRUE;
if (cmd->sent_callback != NULL) {
cmd->sent_callback(cmd->sent_context);
cmd->sent_callback = NULL;
}
}
static int
smtp_client_command_finish_dot_stream(struct smtp_client_command *cmd)
{
struct smtp_client_connection *conn = cmd->conn;
int ret;
i_assert(cmd->stream_dot);
i_assert(conn->dot_output != NULL);
/* this concludes the dot stream with CRLF.CRLF */
if ((ret=o_stream_finish(conn->dot_output)) < 0) {
o_stream_unref(&conn->dot_output);
smtp_client_connection_handle_output_error(conn);
return -1;
}
if (ret == 0)
return 0;
o_stream_unref(&conn->dot_output);
return 1;
}
static void smtp_client_command_payload_input(struct smtp_client_command *cmd)
{
struct smtp_client_connection *conn = cmd->conn;
io_remove(&conn->io_cmd_payload);
smtp_client_connection_trigger_output(conn);
}
static int
smtp_client_command_send_stream(struct smtp_client_command *cmd)
{
struct smtp_client_connection *conn = cmd->conn;
struct istream *stream = cmd->stream;
struct ostream *output = conn->conn.output;
enum ostream_send_istream_result res;
int ret;
io_remove(&conn->io_cmd_payload);
if (cmd->stream_finished) {
if ((ret=smtp_client_command_finish_dot_stream(cmd)) <= 0)
return ret;
/* done sending payload */
smtp_client_command_debug(cmd, "Finished sending payload");
i_stream_unref(&cmd->stream);
return 1;
}
if (cmd->stream_dot) {
if (conn->dot_output == NULL)
conn->dot_output = o_stream_create_dot(output, FALSE);
output = conn->dot_output;
}
/* we're sending the stream now */
o_stream_set_max_buffer_size(output, IO_BLOCK_SIZE);
res = o_stream_send_istream(output, stream);
o_stream_set_max_buffer_size(output, (size_t)-1);
switch (res) {
case OSTREAM_SEND_ISTREAM_RESULT_FINISHED:
i_assert(cmd->stream_size == 0 ||
stream->v_offset == cmd->stream_size);
/* finished with the stream */
smtp_client_command_debug(cmd,
"Finished reading payload stream");
cmd->stream_finished = TRUE;
if (cmd->stream_dot) {
if ((ret=smtp_client_command_finish_dot_stream(cmd)) <= 0)
return ret;
}
/* done sending payload */
smtp_client_command_debug(cmd, "Finished sending payload");
i_stream_unref(&cmd->stream);
return 1;
case OSTREAM_SEND_ISTREAM_RESULT_WAIT_INPUT:
/* input is blocking (client needs to act; disable timeout) */
conn->io_cmd_payload = io_add_istream(
stream, smtp_client_command_payload_input, cmd);
return 0;
case OSTREAM_SEND_ISTREAM_RESULT_WAIT_OUTPUT:
smtp_client_command_debug(cmd, "Partially sent payload");
i_assert(cmd->stream_size == 0 ||
stream->v_offset < cmd->stream_size);
return 0;
case OSTREAM_SEND_ISTREAM_RESULT_ERROR_INPUT:
/* the provided payload stream is broken;
fail this command separately */
smtp_client_command_error(cmd, "read(%s) failed: %s",
i_stream_get_name(stream),
i_stream_get_error(stream));
smtp_client_command_fail(&cmd,
SMTP_CLIENT_COMMAND_ERROR_BROKEN_PAYLOAD,
"Broken payload stream");
/* we're in the middle of sending a command, so the connection
will also have to be aborted */
o_stream_unref(&conn->dot_output);
smtp_client_connection_fail(conn,
SMTP_CLIENT_COMMAND_ERROR_CONNECTION_LOST,
"Broken payload stream");
return -1;
case OSTREAM_SEND_ISTREAM_RESULT_ERROR_OUTPUT:
/* normal connection failure */
o_stream_unref(&conn->dot_output);
smtp_client_connection_handle_output_error(conn);
return -1;
}
i_unreached();
}
static int
smtp_client_command_send_line(struct smtp_client_command *cmd)
{
struct smtp_client_connection *conn = cmd->conn;
const char *data;
size_t size;
ssize_t sent;
if (cmd->data == NULL)
return 1;
while (cmd->send_pos < cmd->data->used) {
data = CONST_PTR_OFFSET(cmd->data->data, cmd->send_pos);
size = cmd->data->used - cmd->send_pos;
if ((sent=o_stream_send(conn->conn.output, data, size)) <= 0) {
if (sent < 0) {
smtp_client_connection_handle_output_error(conn);
return -1;
}
smtp_client_command_debug(cmd,
"Blocked while sending");
return 0;
}
cmd->send_pos += sent;
}
i_assert(cmd->send_pos == cmd->data->used);
return 1;
}
static bool
smtp_client_command_pipeline_is_open(struct smtp_client_connection *conn)
{
struct smtp_client_command *cmd = conn->cmd_send_queue_head;
if (cmd == NULL)
return TRUE;
if (cmd->plug) {
smtp_client_command_debug(cmd, "Pipeline is plugged");
return FALSE;
}
if (conn->state < SMTP_CLIENT_CONNECTION_STATE_READY &&
(cmd->flags & SMTP_CLIENT_COMMAND_FLAG_PRELOGIN) == 0) {
/* wait until we're fully connected */
smtp_client_command_debug(cmd,
"Connection not ready [state=%s]",
smtp_client_connection_state_names[conn->state]);
return FALSE;
}
cmd = conn->cmd_wait_list_head;
if (cmd != NULL &&
(conn->caps.standard & SMTP_CAPABILITY_PIPELINING) == 0) {
/* cannot pipeline; wait for reply */
smtp_client_command_debug(cmd, "Pipeline occupied");
return FALSE;
}
while (cmd != NULL) {
if ((conn->caps.standard & SMTP_CAPABILITY_PIPELINING) == 0 ||
(cmd->flags & SMTP_CLIENT_COMMAND_FLAG_PIPELINE) == 0 ||
cmd->locked) {
/* cannot pipeline with previous command;
wait for reply */
smtp_client_command_debug(cmd, "Pipeline blocked");
return FALSE;
}
cmd = cmd->next;
}
return TRUE;
}
static void smtp_cient_command_wait(struct smtp_client_command *cmd)
{
struct smtp_client_connection *conn = cmd->conn;
/* move command to wait list. */
i_assert(conn->cmd_send_queue_count > 0);
i_assert(conn->cmd_send_queue_count > 1 ||
(cmd->prev == NULL && cmd->next == NULL));
DLLIST2_REMOVE(&conn->cmd_send_queue_head,
&conn->cmd_send_queue_tail, cmd);
conn->cmd_send_queue_count--;
DLLIST2_APPEND(&conn->cmd_wait_list_head,
&conn->cmd_wait_list_tail, cmd);
conn->cmd_wait_list_count++;
}
static int smtp_client_command_do_send_more(struct smtp_client_connection *conn)
{
struct smtp_client_command *cmd;
int ret;
if (conn->cmd_streaming != NULL) {
cmd = conn->cmd_streaming;
i_assert(cmd->stream != NULL);
} else {
/* check whether we can send anything */
cmd = conn->cmd_send_queue_head;
if (cmd == NULL)
return 0;
if (!smtp_client_command_pipeline_is_open(conn))
return 0;
cmd->state = SMTP_CLIENT_COMMAND_STATE_SENDING;
conn->sending_command = TRUE;
if ((ret=smtp_client_command_send_line(cmd)) <= 0)
return ret;
/* command line sent. move command to wait list. */
smtp_cient_command_wait(cmd);
cmd->state = SMTP_CLIENT_COMMAND_STATE_WAITING;
}
if (cmd->stream != NULL &&
(ret=smtp_client_command_send_stream(cmd)) <= 0) {
if (ret < 0)
return -1;
smtp_client_command_debug(cmd, "Blocked while sending payload");
conn->cmd_streaming = cmd;
return 0;
}
conn->cmd_streaming = NULL;
conn->sending_command = FALSE;
smtp_client_command_sent(cmd);
return 1;
}
int smtp_client_command_send_more(struct smtp_client_connection *conn)
{
int ret;
while ((ret=smtp_client_command_do_send_more(conn)) > 0);
if (ret < 0)
return -1;
smtp_client_connection_update_cmd_timeout(conn);
return ret;
}
static void
smtp_client_command_disconnected(struct smtp_client_connection *conn)
{
smtp_client_connection_fail(conn,
SMTP_CLIENT_COMMAND_ERROR_CONNECTION_LOST, "Disconnected");
}
static void
smtp_client_command_insert_prioritized(struct smtp_client_command *cmd,
enum smtp_client_command_flags flag)
{
struct smtp_client_connection *conn = cmd->conn;
struct smtp_client_command *cmd_cur, *cmd_prev;
cmd_cur = conn->cmd_send_queue_head;
if (cmd_cur == NULL ||
(cmd_cur->flags & flag) == 0) {
DLLIST2_PREPEND(&conn->cmd_send_queue_head,
&conn->cmd_send_queue_tail, cmd);
conn->cmd_send_queue_count++;
} else {
cmd_prev = cmd_cur;
cmd_cur = cmd_cur->next;
while (cmd_cur != NULL &&
(cmd_cur->flags & flag) != 0) {
cmd_prev = cmd_cur;
cmd_cur = cmd_cur->next;
}
DLLIST2_INSERT_AFTER(&conn->cmd_send_queue_head,
&conn->cmd_send_queue_tail, cmd_prev, cmd);
conn->cmd_send_queue_count++;
}
}
void
smtp_client_command_submit_after(struct smtp_client_command *cmd,
struct smtp_client_command *after)
{
struct smtp_client_connection *conn = cmd->conn;
i_assert(after == NULL || cmd->conn == after->conn);
cmd->state = SMTP_CLIENT_COMMAND_STATE_SUBMITTED;
if (smtp_client_command_name_equals(cmd, "EHLO"))
cmd->ehlo = TRUE;
if (conn->state == SMTP_CLIENT_CONNECTION_STATE_DISCONNECTED) {
/* Add commands to send queue for delayed failure reply
from ioloop */
DLLIST2_APPEND(&conn->cmd_send_queue_head,
&conn->cmd_send_queue_tail, cmd);
conn->cmd_send_queue_count++;
if (conn->to_commands == NULL) {
conn->to_commands = timeout_add_short(0,
smtp_client_command_disconnected, conn);
}
smtp_client_command_debug(cmd,
"Submitted, but disconnected");
return;
}
if (cmd->data != NULL)
str_append(cmd->data, "\r\n");
if ((cmd->flags & SMTP_CLIENT_COMMAND_FLAG_PRELOGIN) != 0 &&
conn->state < SMTP_CLIENT_CONNECTION_STATE_READY) {
/* pre-login commands get inserted before everything else */
smtp_client_command_insert_prioritized(cmd,
SMTP_CLIENT_COMMAND_FLAG_PRELOGIN);
if (!conn->corked)
smtp_client_connection_trigger_output(conn);
smtp_client_command_debug(cmd, "Submitted with priority");
return;
}
if (after != NULL) {
if (after->state >= SMTP_CLIENT_COMMAND_STATE_WAITING) {
/* not in the send queue anymore; just prepend */
DLLIST2_PREPEND(&conn->cmd_send_queue_head,
&conn->cmd_send_queue_tail, cmd);
conn->cmd_send_queue_count++;
} else {
/* insert after indicated command */
DLLIST2_INSERT_AFTER(&conn->cmd_send_queue_head,
&conn->cmd_send_queue_tail, after, cmd);
conn->cmd_send_queue_count++;
}
} else if ((cmd->flags & SMTP_CLIENT_COMMAND_FLAG_PRIORITY) != 0) {
/* insert at beginning of queue for priority commands */
smtp_client_command_insert_prioritized
(cmd, SMTP_CLIENT_COMMAND_FLAG_PRIORITY);
} else {
/* just append at end of queue */
DLLIST2_APPEND(&conn->cmd_send_queue_head,
&conn->cmd_send_queue_tail, cmd);
conn->cmd_send_queue_count++;
}
if (conn->state >= SMTP_CLIENT_CONNECTION_STATE_READY)
smtp_client_connection_start_cmd_timeout(conn);
if (!conn->corked)
smtp_client_connection_trigger_output(conn);
smtp_client_command_debug(cmd, "Submitted");
}
void smtp_client_command_submit(struct smtp_client_command *cmd)
{
smtp_client_command_submit_after(cmd, NULL);
}
void smtp_client_command_set_flags(struct smtp_client_command *cmd,
enum smtp_client_command_flags flags)
{
cmd->flags = flags;
}
void smtp_client_command_write(struct smtp_client_command *cmd,
const char *cmd_str)
{
unsigned int len = strlen(cmd_str);
i_assert(cmd->state < SMTP_CLIENT_COMMAND_STATE_SUBMITTED);
if (cmd->data == NULL)
cmd->data = str_new(cmd->pool, len + 2);
str_append(cmd->data, cmd_str);
}
void smtp_client_command_printf(struct smtp_client_command *cmd,
const char *cmd_fmt, ...)
{
va_list args;
va_start(args, cmd_fmt);
smtp_client_command_vprintf(cmd, cmd_fmt, args);
va_end(args);
}
void
smtp_client_command_vprintf(struct smtp_client_command *cmd,
const char *cmd_fmt, va_list args)
{
if (cmd->data == NULL)
cmd->data = str_new(cmd->pool, 128);
str_vprintfa(cmd->data, cmd_fmt, args);
}
void
smtp_client_command_set_stream(struct smtp_client_command *cmd,
struct istream *input, bool dot)
{
int ret;
cmd->stream = input;
i_stream_ref(input);
if ((ret=i_stream_get_size(input, TRUE, &cmd->stream_size)) <= 0) {
if (ret < 0) {
smtp_client_command_error(cmd,
"i_stream_get_size(%s) failed: %s",
i_stream_get_name(input),
i_stream_get_error(input));
}
/* size must be known if stream is to be sent in chunks */
i_assert(dot);
cmd->stream_size = 0;
}
cmd->stream_dot = dot;
cmd->has_stream = TRUE;
}
int
smtp_client_command_input_reply(struct smtp_client_command *cmd,
const struct smtp_reply *reply)
{
struct smtp_client_connection *conn = cmd->conn;
bool finished;
i_assert(cmd->replies_seen < cmd->replies_expected);
finished = (++cmd->replies_seen == cmd->replies_expected);
smtp_client_command_debug(cmd, "Got reply (%u/%u): %s "
"(%u commands pending, %u commands queued)",
cmd->replies_seen, cmd->replies_expected,
smtp_reply_log(reply), conn->cmd_wait_list_count,
conn->cmd_send_queue_count);
if (finished) {
i_assert(conn->cmd_wait_list_count > 0);
DLLIST2_REMOVE(&conn->cmd_wait_list_head,
&conn->cmd_wait_list_tail, cmd);
conn->cmd_wait_list_count--;
if (cmd->aborting)
cmd->state = SMTP_CLIENT_COMMAND_STATE_ABORTED;
else if (cmd->state != SMTP_CLIENT_COMMAND_STATE_ABORTED)
cmd->state = SMTP_CLIENT_COMMAND_STATE_FINISHED;
smtp_client_connection_update_cmd_timeout(conn);
}
if (!cmd->aborting && cmd->callback != NULL)
cmd->callback(reply, cmd->context);
if (finished) {
smtp_client_command_drop_callback(cmd);
smtp_client_command_unref(&cmd);
smtp_client_connection_trigger_output(conn);
}
return 1;
}
enum smtp_client_command_state
smtp_client_command_get_state(struct smtp_client_command *cmd)
{
return cmd->state;
}
/*
* Standard commands
*/
/* NOTE: Pipelining is only enabled for certain commands:
From RFC 2920, Section 3.1:
Once the client SMTP has confirmed that support exists for the
pipelining extension, the client SMTP may then elect to transmit
groups of SMTP commands in batches without waiting for a response to
each individual command. In particular, the commands RSET, MAIL FROM,
SEND FROM, SOML FROM, SAML FROM, and RCPT TO can all appear anywhere
in a pipelined command group. The EHLO, DATA, VRFY, EXPN, TURN,
QUIT, and NOOP commands can only appear as the last command in a
group since their success or failure produces a change of state which
the client SMTP must accommodate. (NOOP is included in this group so
it can be used as a synchronization point.)
Additional commands added by other SMTP extensions may only appear as
the last command in a group unless otherwise specified by the
extensions that define the commands.
*/
/* NOOP */