-
Notifications
You must be signed in to change notification settings - Fork 271
/
dsync-ibc-stream.c
2113 lines (1896 loc) · 65.8 KB
/
dsync-ibc-stream.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-2017 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "fd-set-nonblock.h"
#include "safe-mkstemp.h"
#include "ioloop.h"
#include "istream.h"
#include "istream-seekable.h"
#include "istream-dot.h"
#include "ostream.h"
#include "str.h"
#include "strescape.h"
#include "master-service.h"
#include "mail-cache.h"
#include "mail-storage-private.h"
#include "dsync-serializer.h"
#include "dsync-deserializer.h"
#include "dsync-mail.h"
#include "dsync-mailbox.h"
#include "dsync-mailbox-state.h"
#include "dsync-mailbox-tree.h"
#include "dsync-ibc-private.h"
#define DSYNC_IBC_STREAM_OUTBUF_THROTTLE_SIZE (1024*128)
#define DSYNC_PROTOCOL_VERSION_MAJOR 3
#define DSYNC_PROTOCOL_VERSION_MINOR 5
#define DSYNC_HANDSHAKE_VERSION "VERSION\tdsync\t3\t5\n"
#define DSYNC_PROTOCOL_MINOR_HAVE_ATTRIBUTES 1
#define DSYNC_PROTOCOL_MINOR_HAVE_SAVE_GUID 2
#define DSYNC_PROTOCOL_MINOR_HAVE_FINISH 3
#define DSYNC_PROTOCOL_MINOR_HAVE_HDR_HASH_V2 4
#define DSYNC_PROTOCOL_MINOR_HAVE_HDR_HASH_V3 5
enum item_type {
ITEM_NONE,
ITEM_DONE,
ITEM_HANDSHAKE,
ITEM_MAILBOX_STATE,
ITEM_MAILBOX_TREE_NODE,
ITEM_MAILBOX_DELETE,
ITEM_MAILBOX,
ITEM_MAILBOX_ATTRIBUTE,
ITEM_MAIL_CHANGE,
ITEM_MAIL_REQUEST,
ITEM_MAIL,
ITEM_FINISH,
ITEM_MAILBOX_CACHE_FIELD,
ITEM_END_OF_LIST
};
#define END_OF_LIST_LINE "."
static const struct {
/* full human readable name of the item */
const char *name;
/* unique character identifying the item */
char chr;
const char *required_keys;
const char *optional_keys;
unsigned int min_minor_version;
} items[ITEM_END_OF_LIST+1] = {
{ NULL, '\0', NULL, NULL, 0 },
{ .name = "done",
.chr = 'X',
.optional_keys = ""
},
{ .name = "handshake",
.chr = 'H',
.required_keys = "hostname",
.optional_keys = "sync_ns_prefix sync_box sync_box_guid sync_type "
"debug sync_visible_namespaces exclude_mailboxes "
"send_mail_requests backup_send backup_recv lock_timeout "
"no_mail_sync no_mailbox_renames no_backup_overwrite purge_remote "
"no_notify sync_since_timestamp sync_max_size sync_flags sync_until_timestamp "
"virtual_all_box empty_hdr_workaround import_commit_msgs_interval"
},
{ .name = "mailbox_state",
.chr = 'S',
.required_keys = "mailbox_guid last_uidvalidity last_common_uid "
"last_common_modseq last_common_pvt_modseq",
.optional_keys = "last_messages_count changes_during_sync"
},
{ .name = "mailbox_tree_node",
.chr = 'N',
.required_keys = "name existence",
.optional_keys = "mailbox_guid uid_validity uid_next "
"last_renamed_or_created subscribed last_subscription_change"
},
{ .name = "mailbox_delete",
.chr = 'D',
.required_keys = "hierarchy_sep",
.optional_keys = "mailboxes dirs unsubscribes"
},
{ .name = "mailbox",
.chr = 'B',
.required_keys = "mailbox_guid uid_validity uid_next messages_count "
"first_recent_uid highest_modseq highest_pvt_modseq",
.optional_keys = "mailbox_lost mailbox_ignore "
"cache_fields have_guids have_save_guids have_only_guid128"
},
{ .name = "mailbox_attribute",
.chr = 'A',
.required_keys = "type key",
.optional_keys = "value stream deleted last_change modseq",
.min_minor_version = DSYNC_PROTOCOL_MINOR_HAVE_ATTRIBUTES
},
{ .name = "mail_change",
.chr = 'C',
.required_keys = "type uid",
.optional_keys = "guid hdr_hash modseq pvt_modseq "
"add_flags remove_flags final_flags "
"keywords_reset keyword_changes received_timestamp virtual_size"
},
{ .name = "mail_request",
.chr = 'R',
.optional_keys = "guid uid"
},
{ .name = "mail",
.chr = 'M',
.optional_keys = "guid uid pop3_uidl pop3_order received_date saved_date stream"
},
{ .name = "finish",
.chr = 'F',
.optional_keys = "error mail_error require_full_resync",
.min_minor_version = DSYNC_PROTOCOL_MINOR_HAVE_FINISH
},
{ .name = "mailbox_cache_field",
.chr = 'c',
.required_keys = "name decision",
.optional_keys = "last_used"
},
{ "end_of_list", '\0', NULL, NULL, 0 }
};
struct dsync_ibc_stream {
struct dsync_ibc ibc;
char *name, *temp_path_prefix;
unsigned int timeout_secs;
struct istream *input;
struct ostream *output;
struct io *io;
struct timeout *to;
unsigned int minor_version;
struct dsync_serializer *serializers[ITEM_END_OF_LIST];
struct dsync_deserializer *deserializers[ITEM_END_OF_LIST];
pool_t ret_pool;
struct dsync_deserializer_decoder *cur_decoder;
struct istream *value_output, *value_input;
struct dsync_mail *cur_mail;
struct dsync_mailbox_attribute *cur_attr;
char value_output_last;
enum item_type last_recv_item, last_sent_item;
unsigned int last_recv_item_eol:1;
unsigned int last_sent_item_eol:1;
unsigned int version_received:1;
unsigned int handshake_received:1;
unsigned int has_pending_data:1;
unsigned int finish_received:1;
unsigned int done_received:1;
unsigned int stopped:1;
};
static const char *dsync_ibc_stream_get_state(struct dsync_ibc_stream *ibc)
{
if (!ibc->version_received)
return "version not received";
else if (!ibc->handshake_received)
return "handshake not received";
return t_strdup_printf("last sent=%s%s, last recv=%s%s",
items[ibc->last_sent_item].name,
ibc->last_sent_item_eol ? " (EOL)" : "",
items[ibc->last_recv_item].name,
ibc->last_recv_item_eol ? " (EOL)" : "");
}
static void dsync_ibc_stream_stop(struct dsync_ibc_stream *ibc)
{
ibc->stopped = TRUE;
i_stream_close(ibc->input);
o_stream_close(ibc->output);
io_loop_stop(current_ioloop);
}
static int dsync_ibc_stream_read_mail_stream(struct dsync_ibc_stream *ibc)
{
do {
i_stream_skip(ibc->value_input,
i_stream_get_data_size(ibc->value_input));
} while (i_stream_read(ibc->value_input) > 0);
if (ibc->value_input->eof) {
if (ibc->value_input->stream_errno != 0) {
i_error("dsync(%s): read(%s) failed: %s (%s)", ibc->name,
i_stream_get_name(ibc->value_input),
i_stream_get_error(ibc->value_input),
dsync_ibc_stream_get_state(ibc));
dsync_ibc_stream_stop(ibc);
return -1;
}
/* finished reading the mail stream */
i_assert(ibc->value_input->eof);
i_stream_seek(ibc->value_input, 0);
ibc->has_pending_data = TRUE;
ibc->value_input = NULL;
return 1;
}
return 0;
}
static void dsync_ibc_stream_input(struct dsync_ibc_stream *ibc)
{
timeout_reset(ibc->to);
if (ibc->value_input != NULL) {
if (dsync_ibc_stream_read_mail_stream(ibc) == 0)
return;
}
o_stream_cork(ibc->output);
ibc->ibc.io_callback(ibc->ibc.io_context);
o_stream_uncork(ibc->output);
}
static int dsync_ibc_stream_send_value_stream(struct dsync_ibc_stream *ibc)
{
const unsigned char *data;
unsigned char add;
size_t i, size;
int ret;
while ((ret = i_stream_read_data(ibc->value_output,
&data, &size, 0)) > 0) {
add = '\0';
for (i = 0; i < size; i++) {
if (data[i] == '.' &&
((i == 0 && ibc->value_output_last == '\n') ||
(i > 0 && data[i-1] == '\n'))) {
/* escape the dot */
add = '.';
break;
}
}
if (i > 0) {
o_stream_nsend(ibc->output, data, i);
ibc->value_output_last = data[i-1];
i_stream_skip(ibc->value_output, i);
}
if (o_stream_get_buffer_used_size(ibc->output) >= 4096) {
if ((ret = o_stream_flush(ibc->output)) < 0) {
dsync_ibc_stream_stop(ibc);
return -1;
}
if (ret == 0) {
/* continue later */
o_stream_set_flush_pending(ibc->output, TRUE);
return 0;
}
}
if (add != '\0') {
o_stream_nsend(ibc->output, &add, 1);
ibc->value_output_last = add;
}
}
i_assert(ret == -1);
if (ibc->value_output->stream_errno != 0) {
i_error("dsync(%s): read(%s) failed: %s (%s)",
ibc->name, i_stream_get_name(ibc->value_output),
i_stream_get_error(ibc->value_output),
dsync_ibc_stream_get_state(ibc));
dsync_ibc_stream_stop(ibc);
return -1;
}
/* finished sending the stream. use "CRLF." instead of "LF." just in
case we're sending binary data that ends with CR. */
o_stream_nsend_str(ibc->output, "\r\n.\r\n");
i_stream_unref(&ibc->value_output);
return 1;
}
static int dsync_ibc_stream_output(struct dsync_ibc_stream *ibc)
{
struct ostream *output = ibc->output;
int ret;
o_stream_cork(ibc->output);
if ((ret = o_stream_flush(output)) < 0)
ret = 1;
else if (ibc->value_output != NULL) {
if (dsync_ibc_stream_send_value_stream(ibc) < 0)
ret = 1;
}
timeout_reset(ibc->to);
if (!dsync_ibc_is_send_queue_full(&ibc->ibc))
ibc->ibc.io_callback(ibc->ibc.io_context);
o_stream_uncork(ibc->output);
return ret;
}
static void dsync_ibc_stream_timeout(struct dsync_ibc_stream *ibc)
{
i_error("dsync(%s): I/O has stalled, no activity for %u seconds (%s)",
ibc->name, ibc->timeout_secs, dsync_ibc_stream_get_state(ibc));
ibc->ibc.timeout = TRUE;
dsync_ibc_stream_stop(ibc);
}
static void dsync_ibc_stream_init(struct dsync_ibc_stream *ibc)
{
unsigned int i;
ibc->io = io_add_istream(ibc->input, dsync_ibc_stream_input, ibc);
o_stream_set_no_error_handling(ibc->output, TRUE);
o_stream_set_flush_callback(ibc->output, dsync_ibc_stream_output, ibc);
ibc->to = timeout_add(ibc->timeout_secs * 1000,
dsync_ibc_stream_timeout, ibc);
o_stream_cork(ibc->output);
o_stream_nsend_str(ibc->output, DSYNC_HANDSHAKE_VERSION);
/* initialize serializers and send their headers to remote */
for (i = ITEM_DONE + 1; i < ITEM_END_OF_LIST; i++) T_BEGIN {
const char *keys;
keys = items[i].required_keys == NULL ? items[i].optional_keys :
t_strconcat(items[i].required_keys, " ",
items[i].optional_keys, NULL);
if (keys != NULL) {
i_assert(items[i].chr != '\0');
ibc->serializers[i] =
dsync_serializer_init(t_strsplit_spaces(keys, " "));
o_stream_nsend(ibc->output, &items[i].chr, 1);
o_stream_nsend_str(ibc->output,
dsync_serializer_encode_header_line(ibc->serializers[i]));
}
} T_END;
o_stream_nsend_str(ibc->output, ".\n");
o_stream_uncork(ibc->output);
}
static void dsync_ibc_stream_deinit(struct dsync_ibc *_ibc)
{
struct dsync_ibc_stream *ibc = (struct dsync_ibc_stream *)_ibc;
unsigned int i;
for (i = ITEM_DONE + 1; i < ITEM_END_OF_LIST; i++) {
if (ibc->serializers[i] != NULL)
dsync_serializer_deinit(&ibc->serializers[i]);
if (ibc->deserializers[i] != NULL)
dsync_deserializer_deinit(&ibc->deserializers[i]);
}
if (ibc->cur_decoder != NULL)
dsync_deserializer_decode_finish(&ibc->cur_decoder);
if (ibc->value_output != NULL)
i_stream_unref(&ibc->value_output);
else {
/* If the remote has not told us that they are closing we
notify remote that we're closing. this is mainly to avoid
"read() failed: EOF" errors on failing dsyncs.
Avoid a race condition:
We do not tell the remote we are closing if they have
already told us because they close the
connection after sending ITEM_DONE and will
not be ever receive anything else from us unless
it just happens to get combined into the same packet
as a previous response and is already in the buffer.
*/
if (!ibc->done_received && !ibc->finish_received) {
o_stream_nsend_str(ibc->output,
t_strdup_printf("%c\n", items[ITEM_DONE].chr));
}
(void)o_stream_nfinish(ibc->output);
}
timeout_remove(&ibc->to);
if (ibc->io != NULL)
io_remove(&ibc->io);
i_stream_destroy(&ibc->input);
o_stream_destroy(&ibc->output);
pool_unref(&ibc->ret_pool);
i_free(ibc->temp_path_prefix);
i_free(ibc->name);
i_free(ibc);
}
static int dsync_ibc_stream_next_line(struct dsync_ibc_stream *ibc,
const char **line_r)
{
string_t *error;
const char *line;
ssize_t ret;
line = i_stream_next_line(ibc->input);
if (line != NULL) {
*line_r = line;
return 1;
}
/* try reading some */
if ((ret = i_stream_read(ibc->input)) == -1) {
if (ibc->stopped)
return -1;
error = t_str_new(128);
if (ibc->input->stream_errno != 0) {
str_printfa(error, "read(%s) failed: %s", ibc->name,
i_stream_get_error(ibc->input));
} else {
i_assert(ibc->input->eof);
str_printfa(error, "read(%s) failed: EOF", ibc->name);
}
str_printfa(error, " (%s)", dsync_ibc_stream_get_state(ibc));
i_error("%s", str_c(error));
dsync_ibc_stream_stop(ibc);
return -1;
}
i_assert(ret >= 0);
*line_r = i_stream_next_line(ibc->input);
if (*line_r == NULL) {
ibc->has_pending_data = FALSE;
return 0;
}
ibc->has_pending_data = TRUE;
return 1;
}
static void ATTR_FORMAT(3, 4) ATTR_NULL(2)
dsync_ibc_input_error(struct dsync_ibc_stream *ibc,
struct dsync_deserializer_decoder *decoder,
const char *fmt, ...)
{
va_list args;
const char *error;
va_start(args, fmt);
error = t_strdup_vprintf(fmt, args);
if (decoder == NULL)
i_error("dsync(%s): %s", ibc->name, error);
else {
i_error("dsync(%s): %s: %s", ibc->name,
dsync_deserializer_decoder_get_name(decoder), error);
}
va_end(args);
dsync_ibc_stream_stop(ibc);
}
static void
dsync_ibc_stream_send_string(struct dsync_ibc_stream *ibc,
const string_t *str)
{
i_assert(ibc->value_output == NULL);
o_stream_nsend(ibc->output, str_data(str), str_len(str));
}
static int seekable_fd_callback(const char **path_r, void *context)
{
struct dsync_ibc_stream *ibc = context;
string_t *path;
int fd;
path = t_str_new(128);
str_append(path, ibc->temp_path_prefix);
fd = safe_mkstemp(path, 0600, (uid_t)-1, (gid_t)-1);
if (fd == -1) {
i_error("safe_mkstemp(%s) failed: %m", str_c(path));
return -1;
}
/* we just want the fd, unlink it */
if (i_unlink(str_c(path)) < 0) {
/* shouldn't happen.. */
i_close_fd(&fd);
return -1;
}
*path_r = str_c(path);
return fd;
}
static struct istream *
dsync_ibc_stream_input_stream(struct dsync_ibc_stream *ibc)
{
struct istream *inputs[2];
inputs[0] = i_stream_create_dot(ibc->input, FALSE);
inputs[1] = NULL;
ibc->value_input = i_stream_create_seekable(inputs, MAIL_READ_FULL_BLOCK_SIZE,
seekable_fd_callback, ibc);
i_stream_unref(&inputs[0]);
return ibc->value_input;
}
static int
dsync_ibc_check_missing_deserializers(struct dsync_ibc_stream *ibc)
{
unsigned int i;
int ret = 0;
for (i = ITEM_DONE + 1; i < ITEM_END_OF_LIST; i++) {
if (ibc->deserializers[i] == NULL &&
ibc->minor_version >= items[i].min_minor_version &&
(items[i].required_keys != NULL ||
items[i].optional_keys != NULL)) {
dsync_ibc_input_error(ibc, NULL,
"Remote didn't handshake deserializer for %s",
items[i].name);
ret = -1;
}
}
return ret;
}
static bool
dsync_ibc_stream_handshake(struct dsync_ibc_stream *ibc, const char *line)
{
enum item_type item = ITEM_NONE;
const char *const *required_keys, *error;
unsigned int i;
if (ibc->handshake_received)
return TRUE;
if (!ibc->version_received) {
if (!version_string_verify_full(line, "dsync",
DSYNC_PROTOCOL_VERSION_MAJOR,
&ibc->minor_version)) {
dsync_ibc_input_error(ibc, NULL,
"Remote dsync doesn't use compatible protocol");
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
ibc->version_received = TRUE;
return FALSE;
}
if (strcmp(line, END_OF_LIST_LINE) == 0) {
/* finished handshaking */
if (dsync_ibc_check_missing_deserializers(ibc) < 0)
return FALSE;
ibc->handshake_received = TRUE;
ibc->last_recv_item = ITEM_HANDSHAKE;
return FALSE;
}
for (i = 1; i < ITEM_END_OF_LIST; i++) {
if (items[i].chr == line[0]) {
item = i;
break;
}
}
if (item == ITEM_NONE) {
/* unknown deserializer, ignore */
return FALSE;
}
required_keys = items[item].required_keys == NULL ? NULL :
t_strsplit(items[item].required_keys, " ");
if (dsync_deserializer_init(items[item].name,
required_keys, line + 1,
&ibc->deserializers[item], &error) < 0) {
dsync_ibc_input_error(ibc, NULL,
"Remote sent invalid handshake for %s: %s",
items[item].name, error);
}
return FALSE;
}
static enum dsync_ibc_recv_ret
dsync_ibc_stream_input_next(struct dsync_ibc_stream *ibc, enum item_type item,
struct dsync_deserializer_decoder **decoder_r)
{
enum item_type line_item = ITEM_NONE;
const char *line, *error;
unsigned int i;
i_assert(ibc->value_input == NULL);
timeout_reset(ibc->to);
do {
if (dsync_ibc_stream_next_line(ibc, &line) <= 0)
return DSYNC_IBC_RECV_RET_TRYAGAIN;
} while (!dsync_ibc_stream_handshake(ibc, line));
ibc->last_recv_item = item;
ibc->last_recv_item_eol = FALSE;
if (strcmp(line, END_OF_LIST_LINE) == 0) {
/* end of this list */
ibc->last_recv_item_eol = TRUE;
return DSYNC_IBC_RECV_RET_FINISHED;
}
if (line[0] == items[ITEM_DONE].chr) {
/* remote cleanly closed the connection, possibly because of
some failure (which it should have logged). we don't want to
log any stream errors anyway after this. */
ibc->done_received = TRUE;
dsync_ibc_stream_stop(ibc);
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
for (i = 1; i < ITEM_END_OF_LIST; i++) {
if (*line == items[i].chr) {
line_item = i;
break;
}
}
if (line_item != item) {
dsync_ibc_input_error(ibc, NULL,
"Received unexpected input %c != %c",
*line, items[item].chr);
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
if (ibc->cur_decoder != NULL)
dsync_deserializer_decode_finish(&ibc->cur_decoder);
if (dsync_deserializer_decode_begin(ibc->deserializers[item],
line+1, &ibc->cur_decoder,
&error) < 0) {
dsync_ibc_input_error(ibc, NULL, "Invalid input to %s: %s",
items[item].name, error);
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
*decoder_r = ibc->cur_decoder;
return DSYNC_IBC_RECV_RET_OK;
}
static struct dsync_serializer_encoder *
dsync_ibc_send_encode_begin(struct dsync_ibc_stream *ibc, enum item_type item)
{
ibc->last_sent_item = item;
ibc->last_sent_item_eol = FALSE;
return dsync_serializer_encode_begin(ibc->serializers[item]);
}
static void
dsync_ibc_stream_send_handshake(struct dsync_ibc *_ibc,
const struct dsync_ibc_settings *set)
{
struct dsync_ibc_stream *ibc = (struct dsync_ibc_stream *)_ibc;
struct dsync_serializer_encoder *encoder;
string_t *str = t_str_new(128);
char sync_type[2];
str_append_c(str, items[ITEM_HANDSHAKE].chr);
encoder = dsync_ibc_send_encode_begin(ibc, ITEM_HANDSHAKE);
dsync_serializer_encode_add(encoder, "hostname", set->hostname);
if (set->sync_ns_prefixes != NULL) {
dsync_serializer_encode_add(encoder, "sync_ns_prefix",
set->sync_ns_prefixes);
}
if (set->sync_box != NULL)
dsync_serializer_encode_add(encoder, "sync_box", set->sync_box);
if (set->virtual_all_box != NULL) {
dsync_serializer_encode_add(encoder, "virtual_all_box",
set->virtual_all_box);
}
if (set->exclude_mailboxes != NULL) {
string_t *substr = t_str_new(64);
unsigned int i;
for (i = 0; set->exclude_mailboxes[i] != NULL; i++) {
if (i != 0)
str_append_c(substr, '\t');
str_append_tabescaped(substr, set->exclude_mailboxes[i]);
}
dsync_serializer_encode_add(encoder, "exclude_mailboxes",
str_c(substr));
}
if (!guid_128_is_empty(set->sync_box_guid)) {
dsync_serializer_encode_add(encoder, "sync_box_guid",
guid_128_to_string(set->sync_box_guid));
}
sync_type[0] = sync_type[1] = '\0';
switch (set->sync_type) {
case DSYNC_BRAIN_SYNC_TYPE_UNKNOWN:
break;
case DSYNC_BRAIN_SYNC_TYPE_FULL:
sync_type[0] = 'f';
break;
case DSYNC_BRAIN_SYNC_TYPE_CHANGED:
sync_type[0] = 'c';
break;
case DSYNC_BRAIN_SYNC_TYPE_STATE:
sync_type[0] = 's';
break;
}
if (sync_type[0] != '\0')
dsync_serializer_encode_add(encoder, "sync_type", sync_type);
if (set->lock_timeout > 0) {
dsync_serializer_encode_add(encoder, "lock_timeout",
t_strdup_printf("%u", set->lock_timeout));
}
if (set->import_commit_msgs_interval > 0) {
dsync_serializer_encode_add(encoder, "import_commit_msgs_interval",
t_strdup_printf("%u", set->import_commit_msgs_interval));
}
if (set->sync_since_timestamp > 0) {
dsync_serializer_encode_add(encoder, "sync_since_timestamp",
t_strdup_printf("%ld", (long)set->sync_since_timestamp));
}
if (set->sync_until_timestamp > 0) {
dsync_serializer_encode_add(encoder, "sync_until_timestamp",
t_strdup_printf("%ld", (long)set->sync_since_timestamp));
}
if (set->sync_max_size > 0) {
dsync_serializer_encode_add(encoder, "sync_max_size",
t_strdup_printf("%llu", (unsigned long long)set->sync_max_size));
}
if (set->sync_flags != NULL) {
dsync_serializer_encode_add(encoder, "sync_flags",
set->sync_flags);
}
if ((set->brain_flags & DSYNC_BRAIN_FLAG_SEND_MAIL_REQUESTS) != 0)
dsync_serializer_encode_add(encoder, "send_mail_requests", "");
if ((set->brain_flags & DSYNC_BRAIN_FLAG_BACKUP_SEND) != 0)
dsync_serializer_encode_add(encoder, "backup_send", "");
if ((set->brain_flags & DSYNC_BRAIN_FLAG_BACKUP_RECV) != 0)
dsync_serializer_encode_add(encoder, "backup_recv", "");
if ((set->brain_flags & DSYNC_BRAIN_FLAG_DEBUG) != 0)
dsync_serializer_encode_add(encoder, "debug", "");
if ((set->brain_flags & DSYNC_BRAIN_FLAG_SYNC_VISIBLE_NAMESPACES) != 0)
dsync_serializer_encode_add(encoder, "sync_visible_namespaces", "");
if ((set->brain_flags & DSYNC_BRAIN_FLAG_NO_MAIL_SYNC) != 0)
dsync_serializer_encode_add(encoder, "no_mail_sync", "");
if ((set->brain_flags & DSYNC_BRAIN_FLAG_NO_MAILBOX_RENAMES) != 0)
dsync_serializer_encode_add(encoder, "no_mailbox_renames", "");
if ((set->brain_flags & DSYNC_BRAIN_FLAG_NO_BACKUP_OVERWRITE) != 0)
dsync_serializer_encode_add(encoder, "no_backup_overwrite", "");
if ((set->brain_flags & DSYNC_BRAIN_FLAG_PURGE_REMOTE) != 0)
dsync_serializer_encode_add(encoder, "purge_remote", "");
if ((set->brain_flags & DSYNC_BRAIN_FLAG_NO_NOTIFY) != 0)
dsync_serializer_encode_add(encoder, "no_notify", "");
if ((set->brain_flags & DSYNC_BRAIN_FLAG_EMPTY_HDR_WORKAROUND) != 0)
dsync_serializer_encode_add(encoder, "empty_hdr_workaround", "");
dsync_serializer_encode_finish(&encoder, str);
dsync_ibc_stream_send_string(ibc, str);
}
static enum dsync_ibc_recv_ret
dsync_ibc_stream_recv_handshake(struct dsync_ibc *_ibc,
const struct dsync_ibc_settings **set_r)
{
struct dsync_ibc_stream *ibc = (struct dsync_ibc_stream *)_ibc;
struct dsync_deserializer_decoder *decoder;
struct dsync_ibc_settings *set;
const char *value;
pool_t pool = ibc->ret_pool;
enum dsync_ibc_recv_ret ret;
ret = dsync_ibc_stream_input_next(ibc, ITEM_HANDSHAKE, &decoder);
if (ret != DSYNC_IBC_RECV_RET_OK) {
if (ret != DSYNC_IBC_RECV_RET_TRYAGAIN) {
i_error("dsync(%s): Unexpected input in handshake",
ibc->name);
dsync_ibc_stream_stop(ibc);
}
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
p_clear(pool);
set = p_new(pool, struct dsync_ibc_settings, 1);
value = dsync_deserializer_decode_get(decoder, "hostname");
set->hostname = p_strdup(pool, value);
/* now that we know the remote's hostname, use it for the
stream's name */
i_free(ibc->name);
ibc->name = i_strdup(set->hostname);
if (dsync_deserializer_decode_try(decoder, "sync_ns_prefix", &value))
set->sync_ns_prefixes = p_strdup(pool, value);
if (dsync_deserializer_decode_try(decoder, "sync_box", &value))
set->sync_box = p_strdup(pool, value);
if (dsync_deserializer_decode_try(decoder, "virtual_all_box", &value))
set->virtual_all_box = p_strdup(pool, value);
if (dsync_deserializer_decode_try(decoder, "sync_box_guid", &value) &&
guid_128_from_string(value, set->sync_box_guid) < 0) {
dsync_ibc_input_error(ibc, decoder,
"Invalid sync_box_guid: %s", value);
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
if (dsync_deserializer_decode_try(decoder, "exclude_mailboxes", &value) &&
*value != '\0') {
char **boxes = p_strsplit_tabescaped(pool, value);
set->exclude_mailboxes = (const void *)boxes;
}
if (dsync_deserializer_decode_try(decoder, "sync_type", &value)) {
switch (value[0]) {
case 'f':
set->sync_type = DSYNC_BRAIN_SYNC_TYPE_FULL;
break;
case 'c':
set->sync_type = DSYNC_BRAIN_SYNC_TYPE_CHANGED;
break;
case 's':
set->sync_type = DSYNC_BRAIN_SYNC_TYPE_STATE;
break;
default:
dsync_ibc_input_error(ibc, decoder,
"Unknown sync_type: %s", value);
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
}
if (dsync_deserializer_decode_try(decoder, "lock_timeout", &value)) {
if (str_to_uint(value, &set->lock_timeout) < 0 ||
set->lock_timeout == 0) {
dsync_ibc_input_error(ibc, decoder,
"Invalid lock_timeout: %s", value);
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
}
if (dsync_deserializer_decode_try(decoder, "import_commit_msgs_interval", &value)) {
if (str_to_uint(value, &set->import_commit_msgs_interval) < 0 ||
set->import_commit_msgs_interval == 0) {
dsync_ibc_input_error(ibc, decoder,
"Invalid import_commit_msgs_interval: %s", value);
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
}
if (dsync_deserializer_decode_try(decoder, "sync_since_timestamp", &value)) {
if (str_to_time(value, &set->sync_since_timestamp) < 0 ||
set->sync_since_timestamp == 0) {
dsync_ibc_input_error(ibc, decoder,
"Invalid sync_since_timestamp: %s", value);
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
}
if (dsync_deserializer_decode_try(decoder, "sync_until_timestamp", &value)) {
if (str_to_time(value, &set->sync_until_timestamp) < 0 ||
set->sync_until_timestamp == 0) {
dsync_ibc_input_error(ibc, decoder,
"Invalid sync_until_timestamp: %s", value);
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
}
if (dsync_deserializer_decode_try(decoder, "sync_max_size", &value)) {
if (str_to_uoff(value, &set->sync_max_size) < 0 ||
set->sync_max_size == 0) {
dsync_ibc_input_error(ibc, decoder,
"Invalid sync_max_size: %s", value);
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
}
if (dsync_deserializer_decode_try(decoder, "sync_flags", &value))
set->sync_flags = p_strdup(pool, value);
if (dsync_deserializer_decode_try(decoder, "send_mail_requests", &value))
set->brain_flags |= DSYNC_BRAIN_FLAG_SEND_MAIL_REQUESTS;
if (dsync_deserializer_decode_try(decoder, "backup_send", &value))
set->brain_flags |= DSYNC_BRAIN_FLAG_BACKUP_SEND;
if (dsync_deserializer_decode_try(decoder, "backup_recv", &value))
set->brain_flags |= DSYNC_BRAIN_FLAG_BACKUP_RECV;
if (dsync_deserializer_decode_try(decoder, "debug", &value))
set->brain_flags |= DSYNC_BRAIN_FLAG_DEBUG;
if (dsync_deserializer_decode_try(decoder, "sync_visible_namespaces", &value))
set->brain_flags |= DSYNC_BRAIN_FLAG_SYNC_VISIBLE_NAMESPACES;
if (dsync_deserializer_decode_try(decoder, "no_mail_sync", &value))
set->brain_flags |= DSYNC_BRAIN_FLAG_NO_MAIL_SYNC;
if (dsync_deserializer_decode_try(decoder, "no_mailbox_renames", &value))
set->brain_flags |= DSYNC_BRAIN_FLAG_NO_MAILBOX_RENAMES;
if (dsync_deserializer_decode_try(decoder, "no_backup_overwrite", &value))
set->brain_flags |= DSYNC_BRAIN_FLAG_NO_BACKUP_OVERWRITE;
if (dsync_deserializer_decode_try(decoder, "purge_remote", &value))
set->brain_flags |= DSYNC_BRAIN_FLAG_PURGE_REMOTE;
if (dsync_deserializer_decode_try(decoder, "no_notify", &value))
set->brain_flags |= DSYNC_BRAIN_FLAG_NO_NOTIFY;
if (dsync_deserializer_decode_try(decoder, "empty_hdr_workaround", &value))
set->brain_flags |= DSYNC_BRAIN_FLAG_EMPTY_HDR_WORKAROUND;
set->hdr_hash_v2 = ibc->minor_version >= DSYNC_PROTOCOL_MINOR_HAVE_HDR_HASH_V2;
set->hdr_hash_v3 = ibc->minor_version >= DSYNC_PROTOCOL_MINOR_HAVE_HDR_HASH_V3;
*set_r = set;
return DSYNC_IBC_RECV_RET_OK;
}
static void
dsync_ibc_stream_send_end_of_list(struct dsync_ibc *_ibc,
enum dsync_ibc_eol_type type)
{
struct dsync_ibc_stream *ibc = (struct dsync_ibc_stream *)_ibc;
i_assert(ibc->value_output == NULL);
switch (type) {
case DSYNC_IBC_EOL_MAILBOX_ATTRIBUTE:
if (ibc->minor_version < DSYNC_PROTOCOL_MINOR_HAVE_ATTRIBUTES)
return;
break;
default:
break;
}
ibc->last_sent_item_eol = TRUE;
o_stream_nsend_str(ibc->output, END_OF_LIST_LINE"\n");
}
static void
dsync_ibc_stream_send_mailbox_state(struct dsync_ibc *_ibc,
const struct dsync_mailbox_state *state)
{
struct dsync_ibc_stream *ibc = (struct dsync_ibc_stream *)_ibc;
struct dsync_serializer_encoder *encoder;
string_t *str = t_str_new(128);
str_append_c(str, items[ITEM_MAILBOX_STATE].chr);
encoder = dsync_ibc_send_encode_begin(ibc, ITEM_MAILBOX_STATE);
dsync_serializer_encode_add(encoder, "mailbox_guid",
guid_128_to_string(state->mailbox_guid));
dsync_serializer_encode_add(encoder, "last_uidvalidity",
dec2str(state->last_uidvalidity));
dsync_serializer_encode_add(encoder, "last_common_uid",
dec2str(state->last_common_uid));
dsync_serializer_encode_add(encoder, "last_common_modseq",
dec2str(state->last_common_modseq));
dsync_serializer_encode_add(encoder, "last_common_pvt_modseq",
dec2str(state->last_common_pvt_modseq));
dsync_serializer_encode_add(encoder, "last_messages_count",
dec2str(state->last_messages_count));
if (state->changes_during_sync)
dsync_serializer_encode_add(encoder, "changes_during_sync", "");
dsync_serializer_encode_finish(&encoder, str);
dsync_ibc_stream_send_string(ibc, str);
}
static enum dsync_ibc_recv_ret
dsync_ibc_stream_recv_mailbox_state(struct dsync_ibc *_ibc,
struct dsync_mailbox_state *state_r)
{
struct dsync_ibc_stream *ibc = (struct dsync_ibc_stream *)_ibc;
struct dsync_deserializer_decoder *decoder;
const char *value;
enum dsync_ibc_recv_ret ret;
i_zero(state_r);
ret = dsync_ibc_stream_input_next(ibc, ITEM_MAILBOX_STATE, &decoder);
if (ret != DSYNC_IBC_RECV_RET_OK)
return ret;
value = dsync_deserializer_decode_get(decoder, "mailbox_guid");
if (guid_128_from_string(value, state_r->mailbox_guid) < 0) {
dsync_ibc_input_error(ibc, decoder, "Invalid mailbox_guid");
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
value = dsync_deserializer_decode_get(decoder, "last_uidvalidity");
if (str_to_uint32(value, &state_r->last_uidvalidity) < 0) {
dsync_ibc_input_error(ibc, decoder, "Invalid last_uidvalidity");
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
value = dsync_deserializer_decode_get(decoder, "last_common_uid");
if (str_to_uint32(value, &state_r->last_common_uid) < 0) {
dsync_ibc_input_error(ibc, decoder, "Invalid last_common_uid");
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
value = dsync_deserializer_decode_get(decoder, "last_common_modseq");
if (str_to_uint64(value, &state_r->last_common_modseq) < 0) {
dsync_ibc_input_error(ibc, decoder, "Invalid last_common_modseq");
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
value = dsync_deserializer_decode_get(decoder, "last_common_pvt_modseq");
if (str_to_uint64(value, &state_r->last_common_pvt_modseq) < 0) {
dsync_ibc_input_error(ibc, decoder, "Invalid last_common_pvt_modseq");
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
if (dsync_deserializer_decode_try(decoder, "last_messages_count", &value) &&
str_to_uint32(value, &state_r->last_messages_count) < 0) {
dsync_ibc_input_error(ibc, decoder, "Invalid last_messages_count");
return DSYNC_IBC_RECV_RET_TRYAGAIN;
}
if (dsync_deserializer_decode_try(decoder, "changes_during_sync", &value))
state_r->changes_during_sync = TRUE;
return DSYNC_IBC_RECV_RET_OK;
}
static void
dsync_ibc_stream_send_mailbox_tree_node(struct dsync_ibc *_ibc,
const char *const *name,
const struct dsync_mailbox_node *node)
{
struct dsync_ibc_stream *ibc = (struct dsync_ibc_stream *)_ibc;