forked from nicm/fdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imap-common.c
1307 lines (1108 loc) · 30.7 KB
/
imap-common.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
/* $Id$ */
/*
* Copyright (c) 2006 Nicholas Marriott <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <ctype.h>
#include <resolv.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include "fdm.h"
#include "fetch.h"
void imap_free(void *);
int imap_parse(struct account *, int, char *);
int imap_pick_auth(struct account *, struct fetch_ctx *);
int imap_state_connect(struct account *, struct fetch_ctx *);
int imap_state_capability1(struct account *, struct fetch_ctx *);
int imap_state_capability2(struct account *, struct fetch_ctx *);
int imap_state_starttls(struct account *, struct fetch_ctx *);
int imap_state_oauthbearer_auth(struct account *, struct fetch_ctx *);
int imap_state_xoauth2_auth(struct account *, struct fetch_ctx *);
int imap_state_cram_md5_auth(struct account *, struct fetch_ctx *);
int imap_state_plain_auth(struct account *, struct fetch_ctx *);
int imap_state_login1(struct account *, struct fetch_ctx *);
int imap_state_login2(struct account *, struct fetch_ctx *);
int imap_state_user(struct account *, struct fetch_ctx *);
int imap_state_pass(struct account *, struct fetch_ctx *);
int imap_state_select2(struct account *, struct fetch_ctx *);
int imap_state_select3(struct account *, struct fetch_ctx *);
int imap_state_select4(struct account *, struct fetch_ctx *);
int imap_state_search1(struct account *, struct fetch_ctx *);
int imap_state_search2(struct account *, struct fetch_ctx *);
int imap_state_search3(struct account *, struct fetch_ctx *);
int imap_state_next(struct account *, struct fetch_ctx *);
int imap_state_body(struct account *, struct fetch_ctx *);
int imap_state_line(struct account *, struct fetch_ctx *);
int imap_state_mail(struct account *, struct fetch_ctx *);
int imap_state_gmext_start(struct account *, struct fetch_ctx *);
int imap_state_gmext_body(struct account *, struct fetch_ctx *);
int imap_state_gmext_done(struct account *, struct fetch_ctx *);
int imap_state_commit(struct account *, struct fetch_ctx *);
int imap_state_expunge(struct account *, struct fetch_ctx *);
int imap_state_close(struct account *, struct fetch_ctx *);
int imap_state_quit(struct account *, struct fetch_ctx *);
/* Does this contain special characters? */
int
imap_not_clean(const char *s)
{
return (s[strcspn(s, "\"{}")] != '\0');
}
/* Put line to server. */
int
imap_putln(struct account *a, const char *fmt, ...)
{
struct fetch_imap_data *data = a->data;
va_list ap;
int n;
va_start(ap, fmt);
n = data->putln(a, fmt, ap);
va_end(ap);
return (n);
}
/*
* Get line from server. Returns -1 on error, 0 on success, a NULL line when
* out of data.
*/
int
imap_getln(struct account *a, struct fetch_ctx *fctx, int type, char **line)
{
struct fetch_imap_data *data = a->data;
int n;
do {
if (data->getln(a, fctx, line) != 0)
return (-1);
if (*line == NULL)
return (0);
} while ((n = imap_parse(a, type, *line)) == 1);
return (n);
}
/* Free auxiliary data. */
void
imap_free(void *ptr)
{
xfree(ptr);
}
/* Check for okay from server. */
int
imap_okay(char *line)
{
char *ptr;
ptr = strchr(line, ' ');
if (ptr == NULL)
return (0);
if (ptr[1] != 'O' || ptr[2] != 'K' || (ptr[3] != ' ' && ptr[3] != '\0'))
return (0);
return (1);
}
/* Check for no from server. */
int
imap_no(char *line)
{
char *ptr;
ptr = strchr(line, ' ');
if (ptr == NULL)
return (0);
if (ptr[1] != 'N' || ptr[2] != 'O' || (ptr[3] != ' ' && ptr[3] != '\0'))
return (0);
return (1);
}
/*
* Parse line based on type. Returns -1 on error, 0 on success, 1 to ignore
* this line.
*/
int
imap_parse(struct account *a, int type, char *line)
{
struct fetch_imap_data *data = a->data;
int tag;
if (type == IMAP_RAW)
return (0);
tag = imap_tag(line);
switch (type) {
case IMAP_TAGGED:
if (tag == IMAP_TAG_NONE)
return (1);
if (tag == IMAP_TAG_CONTINUE)
goto invalid;
if (tag != data->tag)
goto invalid;
break;
case IMAP_UNTAGGED:
if (tag != IMAP_TAG_NONE)
goto invalid;
break;
case IMAP_CONTINUE:
if (tag == IMAP_TAG_NONE)
return (1);
if (tag != IMAP_TAG_CONTINUE)
goto invalid;
break;
}
return (0);
invalid:
imap_bad(a, line);
return (-1);
}
/* Parse IMAP tag. */
int
imap_tag(char *line)
{
int tag;
const char *errstr;
char *ptr;
if (line[0] == '*' && line[1] == ' ')
return (IMAP_TAG_NONE);
if (line[0] == '+')
return (IMAP_TAG_CONTINUE);
if ((ptr = strchr(line, ' ')) == NULL)
ptr = strchr(line, '\0');
*ptr = '\0';
tag = strtonum(line, 0, INT_MAX, &errstr);
*ptr = ' ';
if (errstr != NULL)
return (IMAP_TAG_ERROR);
return (tag);
}
/* Base64 encode data. */
char *
imap_base64_encode_n(const char *in, size_t inlen)
{
char *out;
size_t size;
size = (inlen * 2) + 1;
out = xcalloc(1, size);
if (b64_ntop(in, inlen, out, size) < 0) {
xfree(out);
return (NULL);
}
return (out);
}
/* Base64 encode string. */
char *
imap_base64_encode(const char *in)
{
return (imap_base64_encode_n(in, strlen(in)));
}
/* Base64 decode string. */
char *
imap_base64_decode(const char *in)
{
char *out;
size_t size;
size = (strlen(in) * 4) + 1;
out = xcalloc(1, size);
if (b64_pton(in, out, size) < 0) {
xfree(out);
return (NULL);
}
return (out);
}
int
imap_bad(struct account *a, const char *line)
{
log_warnx("%s: unexpected data: %s", a->name, line);
return (FETCH_ERROR);
}
int
imap_invalid(struct account *a, const char *line)
{
log_warnx("%s: invalid response: %s", a->name, line);
return (FETCH_ERROR);
}
/* Commit mail. */
int
imap_commit(struct account *a, struct mail *m)
{
struct fetch_imap_data *data = a->data;
struct fetch_imap_mail *aux = m->auxdata;
if (m->decision == DECISION_DROP)
ARRAY_ADD(&data->dropped, aux->uid);
else
ARRAY_ADD(&data->kept, aux->uid);
xfree(aux);
m->auxdata = m->auxfree = NULL;
return (FETCH_AGAIN);
}
/* Abort fetch. */
void
imap_abort(struct account *a)
{
struct fetch_imap_data *data = a->data;
ARRAY_FREE(&data->dropped);
ARRAY_FREE(&data->kept);
ARRAY_FREE(&data->wanted);
data->disconnect(a);
}
/* Return total mails available. */
u_int
imap_total(struct account *a)
{
struct fetch_imap_data *data = a->data;
return (data->folders_total);
}
/* Try an authentication method. */
int
imap_pick_auth(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
/* Try OAUTHBEARER, if requested by user and if server supports it. */
if (data->oauthbearer && (data->capa & IMAP_CAPA_AUTH_OAUTHBEARER)) {
fctx->state = imap_state_oauthbearer_auth;
return (FETCH_AGAIN);
}
/* Try XOAUTH2, if requested by user and if server supports it. */
if (data->xoauth2 && (data->capa & IMAP_CAPA_AUTH_XOAUTH2)) {
fctx->state = imap_state_xoauth2_auth;
return (FETCH_AGAIN);
}
/* Try CRAM-MD5, if server supports it and user allows it. */
if (!data->nocrammd5 && (data->capa & IMAP_CAPA_AUTH_CRAM_MD5)) {
if (imap_putln(a,
"%u AUTHENTICATE CRAM-MD5", ++data->tag) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_cram_md5_auth;
return (FETCH_BLOCK);
}
/* Try PLAIN, if server supports it and user allows it. */
if (!data->noplain && (data->capa & IMAP_CAPA_AUTH_PLAIN)) {
if (imap_putln(a,
"%u AUTHENTICATE PLAIN", ++data->tag) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_plain_auth;
return (FETCH_BLOCK);
}
/* Fall back to LOGIN, unless config disallows it. */
if (!data->nologin) {
if (imap_not_clean(data->user) || imap_not_clean(data->pass)) {
if (imap_putln(a, "%u LOGIN {%zu}", ++data->tag,
strlen(data->user)) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_login2;
} else {
if (imap_putln(a, "%u LOGIN \"%s\" \"%s\"", ++data->tag,
data->user, data->pass) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_login1;
}
return (FETCH_BLOCK);
}
log_warnx("%s: no authentication methods", a->name);
return (FETCH_ERROR);
}
/* Common initialisation state. */
int
imap_state_init(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
ARRAY_INIT(&data->dropped);
ARRAY_INIT(&data->kept);
ARRAY_INIT(&data->wanted);
data->tag = 0;
data->folder = 0;
data->folders_total = 0;
fctx->state = imap_state_connect;
return (FETCH_AGAIN);
}
/* Connect state. */
int
imap_state_connect(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
if (data->connect(a) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_connected;
return (FETCH_BLOCK);
}
/* Connected state: wait for initial line from server. */
int
imap_state_connected(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line;
if (imap_getln(a, fctx, IMAP_UNTAGGED, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
if (strncmp(line, "* PREAUTH", 9) == 0) {
fctx->state = imap_state_select1;
return (FETCH_AGAIN);
}
if (data->user == NULL || data->pass == NULL) {
log_warnx("%s: not PREAUTH and no user or password", a->name);
return (FETCH_ERROR);
}
if (imap_putln(a, "%u CAPABILITY", ++data->tag) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_capability1;
return (FETCH_BLOCK);
}
/* Capability state 1. Parse capabilities and set flags. */
int
imap_state_capability1(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line, *ptr;
if (imap_getln(a, fctx, IMAP_UNTAGGED, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
/* Convert to uppercase. */
for (ptr = line; *ptr != '\0'; ptr++)
*ptr = toupper((u_char) *ptr);
if (strstr(line, "IMAP4REV1") == NULL) {
log_warnx("%s: no IMAP4rev1 capability: %s", a->name, line);
return (FETCH_ERROR);
}
data->capa = 0;
if (strstr(line, "AUTH=PLAIN") != NULL)
data->capa |= IMAP_CAPA_AUTH_PLAIN;
if (strstr(line, "AUTH=CRAM-MD5") != NULL)
data->capa |= IMAP_CAPA_AUTH_CRAM_MD5;
if (strstr(line, "AUTH=OAUTHBEARER") != NULL)
data->capa |= IMAP_CAPA_AUTH_OAUTHBEARER;
if (strstr(line, "AUTH=XOAUTH2") != NULL)
data->capa |= IMAP_CAPA_AUTH_XOAUTH2;
/* Use XYZZY to detect Google brokenness. */
if (strstr(line, "XYZZY") != NULL)
data->capa |= IMAP_CAPA_XYZZY;
/* GMail IMAP extensions. */
if (strstr(line, "X-GM-EXT-1") != NULL)
data->capa |= IMAP_CAPA_GMEXT;
if (strstr(line, "STARTTLS") != NULL)
data->capa |= IMAP_CAPA_STARTTLS;
fctx->state = imap_state_capability2;
return (FETCH_AGAIN);
}
/* Capability state 2. Check capabilities and choose login type. */
int
imap_state_capability2(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line;
if (imap_getln(a, fctx, IMAP_TAGGED, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
if (!imap_okay(line))
return (imap_bad(a, line));
if (data->starttls) {
if (!(data->capa & IMAP_CAPA_STARTTLS)) {
log_warnx("%s: server doesn't support STARTTLS",
a->name);
return (FETCH_ERROR);
}
if (imap_putln(a, "%u STARTTLS", ++data->tag) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_starttls;
return (FETCH_BLOCK);
}
return (imap_pick_auth(a, fctx));
}
/* STARTTLS state. */
int
imap_state_starttls(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line, *cause;
if (imap_getln(a, fctx, IMAP_TAGGED, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
if (!imap_okay(line))
return (imap_bad(a, line));
data->io->ssl = makessl(&data->server, data->io->fd,
conf.verify_certs && data->server.verify, conf.timeout, &cause);
if (data->io->ssl == NULL) {
log_warnx("%s: STARTTLS failed: %s", a->name, cause);
xfree(cause);
return (FETCH_ERROR);
}
return (imap_pick_auth(a, fctx));
}
/* OAUTHBEARER auth state. */
int
imap_state_oauthbearer_auth(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *src, *b64;
xasprintf(&src,
"n,a=%s,\001host=%s\001port=%s\001auth=Bearer %s\001\001",
data->user, data->server.host, data->server.port, data->pass);
b64 = imap_base64_encode(src);
xfree(src);
if (imap_putln(a,
"%u AUTHENTICATE OAUTHBEARER %s", ++data->tag, b64) != 0) {
xfree(b64);
return (FETCH_ERROR);
}
xfree(b64);
fctx->state = imap_state_pass;
return (FETCH_BLOCK);
}
/* OAUTH auth state. */
int
imap_state_xoauth2_auth(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *src, *b64;
xasprintf(&src, "user=%s\001auth=Bearer %s\001\001", data->user,
data->pass);
b64 = imap_base64_encode(src);
xfree(src);
if (imap_putln(a, "%u AUTHENTICATE XOAUTH2 %s", ++data->tag, b64) != 0) {
xfree(b64);
return (FETCH_ERROR);
}
xfree(b64);
fctx->state = imap_state_pass;
return (FETCH_BLOCK);
}
/* PLAIN auth state. */
int
imap_state_plain_auth(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line, *ptr, *out;
size_t outlen;
char *b64;
if (imap_getln(a, fctx, IMAP_CONTINUE, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
ptr = line + 1;
while (isspace((u_char) *ptr))
ptr++;
if (*ptr != '\0' && strcmp(ptr, "\"\"") != 0)
return (imap_invalid(a, line));
outlen = 1 + strlen(data->user) + 1 + strlen(data->pass);
out = xcalloc(1, outlen);
memcpy(out + 1, data->user, strlen(data->user));
memcpy(out + 1 + strlen(data->user) + 1, data->pass,
strlen(data->pass));
b64 = imap_base64_encode_n(out, outlen);
xfree(out);
if (imap_putln(a, "%s", b64) != 0) {
xfree(b64);
return (FETCH_ERROR);
}
xfree(b64);
fctx->state = imap_state_pass;
return (FETCH_BLOCK);
}
/* CRAM-MD5 auth state. */
int
imap_state_cram_md5_auth(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line, *ptr, *src, *b64;
char out[EVP_MAX_MD_SIZE * 2 + 1];
u_char digest[EVP_MAX_MD_SIZE];
u_int i, n;
if (imap_getln(a, fctx, IMAP_CONTINUE, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
ptr = line + 1;
while (isspace((u_char) *ptr))
ptr++;
if (*ptr == '\0')
return (imap_invalid(a, line));
b64 = imap_base64_decode(ptr);
HMAC(EVP_md5(),
data->pass, strlen(data->pass), b64, strlen(b64), digest, &n);
xfree(b64);
for (i = 0; i < n; i++)
xsnprintf(out + i * 2, 3, "%02hhx", digest[i]);
xasprintf(&src, "%s %s", data->user, out);
b64 = imap_base64_encode(src);
xfree(src);
if (imap_putln(a, "%s", b64) != 0) {
xfree(b64);
return (FETCH_ERROR);
}
xfree(b64);
fctx->state = imap_state_pass;
return (FETCH_BLOCK);
}
/* Login state. */
int
imap_state_login1(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line;
if (imap_getln(a, fctx, IMAP_TAGGED, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
if (!imap_okay(line)) {
if (imap_putln(a, "%u LOGIN {%zu}", ++data->tag,
strlen(data->user)) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_login2;
return (FETCH_BLOCK);
}
fctx->state = imap_state_select1;
return (FETCH_AGAIN);
}
/* Login state. */
int
imap_state_login2(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line;
size_t passlen;
if (imap_getln(a, fctx, IMAP_CONTINUE, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
passlen = strlen(data->pass);
if (data->capa & IMAP_CAPA_NOSPACE) {
if (imap_putln(a, "%s{%zu}", data->user, passlen) != 0)
return (FETCH_ERROR);
} else {
if (imap_putln(a, "%s {%zu}", data->user, passlen) != 0)
return (FETCH_ERROR);
}
fctx->state = imap_state_user;
return (FETCH_BLOCK);
}
/* User state. */
int
imap_state_user(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line;
int tag;
if (data->capa & IMAP_CAPA_NOSPACE) {
if (imap_getln(a, fctx, IMAP_CONTINUE, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
} else {
for (;;) {
if (imap_getln(a, fctx, IMAP_RAW, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
tag = imap_tag(line);
if (tag == IMAP_TAG_NONE)
continue;
if (tag == IMAP_TAG_CONTINUE || tag == data->tag)
break;
return (FETCH_ERROR);
}
if (tag != IMAP_TAG_CONTINUE) {
log_debug("%s: didn't accept user (%s); "
"trying without space", a->name, line);
data->capa |= IMAP_CAPA_NOSPACE;
return (imap_pick_auth(a, fctx));
}
}
if (imap_putln(a, "%s", data->pass) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_pass;
return (FETCH_BLOCK);
}
/* Pass state. */
int
imap_state_pass(struct account *a, struct fetch_ctx *fctx)
{
char *line;
if (imap_getln(a, fctx, IMAP_TAGGED, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
if (!imap_okay(line))
return (imap_bad(a, line));
fctx->state = imap_state_select1;
return (FETCH_AGAIN);
}
/* Select state 1. */
int
imap_state_select1(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
const char *name = ARRAY_ITEM(data->folders, data->folder);
if (imap_not_clean(name)) {
if (imap_putln(a, "%u SELECT {%zu}", ++data->tag,
strlen(name)) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_select2;
} else {
if (imap_putln(a, "%u SELECT \"%s\"", ++data->tag, name) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_select3;
}
return (FETCH_BLOCK);
}
/* Select state 2. Wait for continuation and send folder name. */
int
imap_state_select2(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line;
if (imap_getln(a, fctx, IMAP_CONTINUE, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
if (imap_putln(a, "%s", ARRAY_ITEM(data->folders, data->folder)) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_select3;
return (FETCH_BLOCK);
}
/* Select state 3. Hold until select returns message count. */
int
imap_state_select3(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line;
for (;;) {
if (imap_getln(a, fctx, IMAP_UNTAGGED, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
if (sscanf(line, "* %u EXISTS", &data->total) == 1)
break;
}
fctx->state = imap_state_select4;
return (FETCH_AGAIN);
}
/* Select state 4. Hold until select completes. */
int
imap_state_select4(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line;
if (imap_getln(a, fctx, IMAP_TAGGED, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
if (!imap_okay(line))
return (imap_bad(a, line));
/* If no mails, stop early. */
if (data->total == 0) {
if (imap_putln(a, "%u CLOSE", ++data->tag) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_close;
return (FETCH_BLOCK);
}
fctx->state = imap_state_search1;
return (FETCH_AGAIN);
}
/* Search state 1. Request list of mail required. */
int
imap_state_search1(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
/* Search for a list of the mail UIDs to fetch. */
switch (data->only) {
case FETCH_ONLY_NEW:
if (imap_putln(a, "%u UID SEARCH UNSEEN", ++data->tag) != 0)
return (FETCH_ERROR);
break;
case FETCH_ONLY_OLD:
if (imap_putln(a, "%u UID SEARCH SEEN", ++data->tag) != 0)
return (FETCH_ERROR);
break;
default:
if (imap_putln(a, "%u UID SEARCH ALL", ++data->tag) != 0)
return (FETCH_ERROR);
break;
}
fctx->state = imap_state_search2;
return (FETCH_BLOCK);
}
/* Search state 2. */
int
imap_state_search2(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line, *ptr;
u_int uid;
if (imap_getln(a, fctx, IMAP_UNTAGGED, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
/* Skip the header. */
if (strncasecmp(line, "* SEARCH", 8) != 0)
return (imap_bad(a, line));
line += 8;
/* Read each UID and save it. */
do {
while (isspace((u_char) *line))
line++;
ptr = strchr(line, ' ');
if (ptr == NULL)
ptr = strchr(line, '\0');
if (ptr == line)
break;
if (sscanf(line, "%u", &uid) != 1)
return (imap_bad(a, line));
ARRAY_ADD(&data->wanted, uid);
log_debug3("%s: fetching UID: %u", a->name, uid);
line = ptr;
} while (*line == ' ');
fctx->state = imap_state_search3;
return (FETCH_AGAIN);
}
/* Search state 3. */
int
imap_state_search3(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
char *line;
if (imap_getln(a, fctx, IMAP_TAGGED, &line) != 0)
return (FETCH_ERROR);
if (line == NULL)
return (FETCH_BLOCK);
if (!imap_okay(line))
return (imap_bad(a, line));
/* Save the total. */
data->total = ARRAY_LENGTH(&data->wanted);
/* Update grand total. */
data->folders_total += data->total;
/* If no mails, or polling, stop here. */
if (data->total == 0 || fctx->flags & FETCH_POLL) {
if (imap_putln(a, "%u CLOSE", ++data->tag) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_close;
return (FETCH_BLOCK);
}
fctx->state = imap_state_next;
return (FETCH_AGAIN);
}
/*
* Next state. Get next mail. This is also the idle state when completed, so
* check for finished mail, exiting, and so on.
*/
int
imap_state_next(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_imap_data *data = a->data;
/* Handle dropped and kept mail. */
if (!ARRAY_EMPTY(&data->dropped)) {
if (imap_putln(a, "%u UID STORE %u +FLAGS.SILENT (\\Deleted)",
++data->tag, ARRAY_FIRST(&data->dropped)) != 0)
return (FETCH_ERROR);
ARRAY_REMOVE(&data->dropped, 0);
fctx->state = imap_state_commit;
return (FETCH_BLOCK);
}
if (!ARRAY_EMPTY(&data->kept)) {
/*
* GMail is broken and does not set the \Seen flag after mail
* is fetched, so set it explicitly for kept mail.
*/
if (imap_putln(a, "%u UID STORE %u +FLAGS.SILENT (\\Seen)",
++data->tag, ARRAY_FIRST(&data->kept)) != 0)
return (FETCH_ERROR);
ARRAY_REMOVE(&data->kept, 0);
fctx->state = imap_state_commit;
return (FETCH_BLOCK);
}
/* Need to purge, switch to purge state. */
if (fctx->flags & FETCH_PURGE) {
/*
* If can't purge now, loop through this state until there is
* no mail on the dropped queue and FETCH_EMPTY is set. Can't
* have a seperate state to loop through without returning
* here: mail could potentially be added to the dropped list
* while in that state.
*/
if (fctx->flags & FETCH_EMPTY) {
fctx->flags &= ~FETCH_PURGE;
if (imap_putln(a, "%u EXPUNGE", ++data->tag) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_expunge;
return (FETCH_BLOCK);
}
/*
* Must be waiting for delivery, so permit blocking even though
* we (fetch) aren't waiting for any data.
*/
return (FETCH_BLOCK);
}
/* If last mail, wait for everything to be committed then close down. */
if (ARRAY_EMPTY(&data->wanted)) {
if (data->committed != data->total)
return (FETCH_BLOCK);
if (imap_putln(a, "%u CLOSE", ++data->tag) != 0)
return (FETCH_ERROR);
fctx->state = imap_state_close;
return (FETCH_BLOCK);
}