forked from vandry/mairix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
imap.c
1323 lines (1261 loc) · 30.4 KB
/
imap.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <time.h>
#include <poll.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/wait.h>
#ifdef USE_OPENSSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
#if defined(__FreeBSD__)
#define __BSD_VISIBLE 1
#endif
#include "imap.h"
struct imap_ll {
int w_socket;
int r_socket;
time_t timeout_time;
char buffer[1024];
char *bufferp;
size_t buffer_left;
int current_tag_number;
#ifdef USE_OPENSSL
SSL *ssl;
#endif
char *selected_folder;
size_t selected_folder_namelen;
char *selected_folder_uidvalidity;
int selected_folder_writable;
long selected_folder_exists;
};
struct imap_ll *
imap_ll_connect(const char *host, const char *port)
{
struct imap_ll *ll;
struct addrinfo hints, *res, *res0;
int error;
int s;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo(host, port, &hints, &res0);
if (error) {
fprintf(stderr, "getaddrinfo \"%s\":\"%s\": %s\n",
host, port, gai_strerror(error)
);
return NULL;
}
s = -1;
for (res = res0; res; res = res->ai_next) {
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0) {
fprintf(stderr, "socket \"%s\":\"%s\": %s\n",
host, port, strerror(errno)
);
continue;
}
if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
fprintf(stderr, "connect \"%s\":\"%s\": %s\n",
host, port, strerror(errno)
);
close(s);
s = -1;
continue;
}
break;
}
if (s == -1) {
if (!res0) {
fprintf(stderr, "server \"%s\":\"%s\": no available addresses\n",
host, port
);
}
return NULL;
}
ll = malloc(sizeof(*ll));
if (!ll) {
fprintf(stderr, "malloc failed\n");
close(s);
return NULL;
}
memset(ll, 0, sizeof(*ll));
ll->w_socket = ll->r_socket = s;
ll->current_tag_number = -1;
#ifdef USE_OPENSSL
ll->ssl = NULL;
#endif
fcntl(s, F_SETFL, O_NONBLOCK);
return ll;
}
struct imap_ll *
imap_ll_pipe_connect(const char *cmd)
{
struct imap_ll *ll;
pid_t child;
int status;
int r[2], w[2];
if (pipe(&(r[0])) < 0) {
fprintf(stderr, "pipe1 failed\n");
return NULL;
}
if (pipe(&(w[0])) < 0) {
fprintf(stderr, "pipe2 failed\n");
close(r[0]);
close(r[1]);
return NULL;
}
if ((child = fork()) < 0) {
fprintf(stderr, "fork failed\n");
return NULL;
}
if (child == 0) {
close(r[0]);
close(w[1]);
if (r[1] != STDOUT_FILENO) {
dup2(r[1], STDOUT_FILENO);
close(r[1]);
}
if (w[0] != STDIN_FILENO) {
dup2(w[0], STDIN_FILENO);
close(w[0]);
}
execl("/bin/sh", "sh", "-c", cmd, NULL);
_exit(1);
}
close(r[1]);
close(w[0]);
ll = malloc(sizeof(*ll));
if (!ll) {
fprintf(stderr, "malloc failed\n");
close(r[0]);
close(w[1]);
kill(child, SIGTERM);
waitpid(child, &status, 0);
return NULL;
}
memset(ll, 0, sizeof(*ll));
ll->w_socket = w[1];
ll->r_socket = r[0];
ll->current_tag_number = -1;
#ifdef USE_OPENSSL
ll->ssl = NULL;
#endif
fcntl(w[1], F_SETFL, O_NONBLOCK);
fcntl(r[0], F_SETFL, O_NONBLOCK);
return ll;
}
void
imap_ll_timeout(struct imap_ll *ll, int seconds)
{
ll->timeout_time = time(NULL) + seconds;
}
static struct imap_ll_tokenlist *
mktoken(int type, char *leaf, size_t len)
{
struct imap_ll_tokenlist *tl;
tl = malloc(sizeof(*tl));
if (!tl) {
fprintf(stderr, "imap_ll mktoken: malloc failed (fatal)\n");
sleep(1);
_exit(0);
}
tl->type = type;
tl->first = tl->last = NULL;
tl->parent = NULL;
tl->leaf = leaf;
tl->leaflen = len;
if (leaf) leaf[len] = 0;
return tl;
}
static void
growlist(struct imap_ll_tokenlist **nodep, struct imap_ll_tokenlist *tl)
{
if ((*nodep)->last) {
(*nodep)->last->next = tl;
} else {
(*nodep)->first = tl;
}
(*nodep)->last = tl;
tl->parent = *nodep;
tl->next = NULL;
if (!(tl->leaf)) {
*nodep = tl;
}
}
void
imap_ll_freeline(struct imap_ll_tokenlist *t)
{
struct imap_ll_tokenlist *t2, *next;
if (t) {
if (t->leaf) free(t->leaf);
for (t2 = t->first; t2; t2 = next) {
next = t2->next;
imap_ll_freeline(t2);
}
free(t);
}
}
#define MKPRETOKEN(alloc_size) { \
curtoken = malloc((alloc_size) + 1); \
if (!curtoken) { \
fprintf(stderr, "imap_ll_waitline(%d): malloc failed (fatal)\n", (alloc_size)); \
sleep(1); \
_exit(0); \
} \
curtoken_now = 0; \
curtoken_max = (alloc_size); \
}
struct imap_ll_tokenlist *
imap_ll_waitline(struct imap_ll *ll)
{
int timeout, n;
#ifdef USE_OPENSSL
int ret2;
#endif
char cur;
struct imap_ll_tokenlist *line = NULL;
struct imap_ll_tokenlist *node = NULL;
int linestate = 0;
int recv_tag = 0;
char *curtoken = NULL;
size_t curtoken_now = 0;
size_t curtoken_max = 0;
struct pollfd pfd;
for (;;) {
while (ll->buffer_left) {
cur = *(ll->bufferp);
ll->bufferp++;
ll->buffer_left--;
if ((cur == 10) && (linestate < 7)) {
if (curtoken && (linestate == 4)) {
growlist(&node, mktoken(TLTYPE_ATOM, curtoken, curtoken_now));
} else if (curtoken) {
free(curtoken);
}
curtoken = NULL;
return line;
}
switch (linestate) {
case 0:
/* expect start of line */
if (cur == '*') {
linestate = 1;
} else if (cur == 't') {
linestate = 2;
recv_tag = 0;
} else if (cur == '+') {
line = mktoken(TLTYPE_CONTINUATION, NULL, 0);
linestate = 3;
} else {
linestate = 3;
}
break;
case 1:
/* expect space after untagged prefix */
if (cur == ' ') {
node = line = mktoken(TLTYPE_UNTAGGED, NULL, 0);
linestate = 4;
} else {
linestate = 3;
}
break;
case 2:
/* expect tag */
if (cur == ' ') {
if (recv_tag == ll->current_tag_number) {
node = line = mktoken(TLTYPE_TAGGED, NULL, 0);
linestate = 4;
} else {
/* wrong tag */
linestate = 3;
}
} else if ((cur >= '0') && (cur <= '9')) {
recv_tag *= 10;
recv_tag += cur - '0';
} else {
linestate = 3;
}
break;
case 3:
/* error -- discard until end of line */
break;
case 4:
/* looking for start of next "thing" */
if (cur == 34) {
linestate = 5;
MKPRETOKEN(50);
} else if (cur == 123) {
linestate = 6;
recv_tag = 0;
} else if (
(cur == 91) /* square bracket */ &&
(line == node) /* top level */ &&
(line->first) /* not first in list */ &&
/* list has exactly 1 */
(line->first == line->last)
) {
growlist(&node, mktoken(TLTYPE_SQLIST, NULL, 0));
} else if (cur == 40) {
growlist(&node, mktoken(TLTYPE_LIST, NULL, 0));
} else if ((
(node->type == TLTYPE_LIST) &&
(cur == 41)
) || (
(node->type == TLTYPE_SQLIST) &&
(cur == 93)
)) {
if (curtoken) {
growlist(&node, mktoken(TLTYPE_ATOM, curtoken, curtoken_now));
curtoken = NULL;
}
node = node->parent;
} else if (
(cur == ' ') ||
(cur == 9) ||
(cur == 13)
) {
if (curtoken) {
growlist(&node, mktoken(TLTYPE_ATOM, curtoken, curtoken_now));
curtoken = NULL;
}
} else {
if (!curtoken) {
MKPRETOKEN(25);
}
token_extend:
if (curtoken_now >= curtoken_max) {
curtoken_max *= 2;
curtoken = realloc(curtoken, curtoken_max + 1);
if (!curtoken) {
fprintf(stderr, "imap_ll_waitline(size=%d): malloc failed (fatal)\n", (int)curtoken_max);
sleep(1);
_exit(0);
}
}
curtoken[curtoken_now++] = cur;
}
break;
case 5:
/* quoted string */
if (cur == 34) {
growlist(&node, mktoken(TLTYPE_STRING, curtoken, curtoken_now));
curtoken = NULL;
linestate = 4;
} else {
goto token_extend;
}
break;
case 6:
/* literal count */
if (cur == 125) {
linestate = 7;
} else if ((cur >= '0') && (cur <= '9')) {
recv_tag *= 10;
recv_tag += cur - '0';
} else {
linestate = 3;
}
break;
case 7:
/* CRLF after {nn} */
if (cur == 13) break;
if (cur != 10) {
linestate = 3;
}
MKPRETOKEN(recv_tag);
if (recv_tag == 0) {
growlist(&node, mktoken(TLTYPE_STRING, curtoken, curtoken_now));
curtoken = NULL;
linestate = 4;
} else {
linestate = 8;
}
break;
case 8:
/* literal contents */
curtoken[curtoken_now++] = cur;
if (curtoken_now == curtoken_max) {
growlist(&node, mktoken(TLTYPE_STRING, curtoken, curtoken_now));
curtoken = NULL;
linestate = 4;
}
break;
}
}
#ifdef USE_OPENSSL
if (ll->ssl) {
while ((n = SSL_read(ll->ssl, &((ll->buffer)[0]), sizeof(ll->buffer))) <= 0) {
ret2 = SSL_get_error(ll->ssl, n);
if ((ret2 == SSL_ERROR_WANT_READ) || (ret2 == SSL_ERROR_WANT_WRITE)) {
if (ret2 == SSL_ERROR_WANT_READ) {
pfd.fd = ll->r_socket;
pfd.events = POLLIN;
} else {
pfd.fd = ll->w_socket;
pfd.events = POLLOUT;
}
timeout = ll->timeout_time - time(NULL);
if (timeout < 0) timeout = 0;
if ((n = poll(&pfd, 1, timeout*1000)) < 0) {
if (errno == EINTR) continue;
goto pollerr;
}
if (n == 0) goto polltimeout;
} else {
ERR_print_errors_fp(stderr);
imap_ll_freeline(line);
return NULL;
}
}
} else {
#endif
pfd.fd = ll->r_socket;
pfd.events = POLLIN;
repoll:
timeout = ll->timeout_time - time(NULL);
if (timeout < 0) timeout = 0;
if ((n = poll(&pfd, 1, timeout*1000)) < 0) {
if (errno == EINTR) goto repoll;
#ifdef USE_OPENSSL
pollerr:
#endif
fprintf(stderr, "imap_ll_waitline: poll: %s\n", strerror(errno));
imap_ll_freeline(line);
return NULL;
}
if (n == 0) {
#ifdef USE_OPENSSL
polltimeout:
#endif
fprintf(stderr, "imap_ll_waitline: timeout\n");
imap_ll_freeline(line);
return NULL;
}
if ((n = read(ll->r_socket, &((ll->buffer)[0]), sizeof(ll->buffer))) < 0) {
if (errno == EAGAIN) continue;
if (errno == EINTR) continue;
fprintf(stderr, "imap_ll_waitline: read: %s\n", strerror(errno));
imap_ll_freeline(line);
return NULL;
}
if (n == 0) {
fprintf(stderr, "imap_ll_waitline: read: EOF\n");
imap_ll_freeline(line);
return NULL;
}
#ifdef USE_OPENSSL
}
#endif
ll->buffer_left = n;
ll->bufferp = &((ll->buffer)[0]);
}
}
/* this may overestimate the size a bit */
static size_t
string_len(struct imap_ll_tokenlist *t)
{
size_t result;
switch (t->type) {
case TLTYPE_UNTAGGED:
result = 2;
break;
case TLTYPE_TAGGED:
result = 15;
break;
case TLTYPE_LIST:
case TLTYPE_SQLIST:
result = 2;
break;
case TLTYPE_ATOM:
result = t->leaflen;
break;
case TLTYPE_STRING:
/* worst case is if it's sent as a literal */
result = 20 + t->leaflen;
break;
case TLTYPE_CONTINUATION:
/* shouldn't be used */
result = 2;
break;
case TLTYPE_POP:
case TLTYPE_END:
case TLTYPE_SUB:
default:
result = 0;
}
for (t = t->first; t; t = t->next) {
result++;
result += string_len(t);
}
return result;
}
static char *
format_cmd(char *out, struct imap_ll *ll, struct imap_ll_tokenlist *t)
{
int i;
char close = 0;
switch (t->type) {
case TLTYPE_UNTAGGED:
*(out++) = '*';
*(out++) = ' ';
case TLTYPE_TAGGED:
out += sprintf(out, "t%d ", ++(ll->current_tag_number));
break;
case TLTYPE_LIST:
*(out++) = '('; close = ')';
break;
case TLTYPE_SQLIST:
*(out++) = '['; close = ']';
break;
case TLTYPE_ATOM:
memcpy(out, t->leaf, t->leaflen);
out += t->leaflen;
break;
case TLTYPE_STRING:
for (i = 0; i < t->leaflen; i++) {
if (
(t->leaf[i] < 32) ||
(t->leaf[i] == 34) ||
(t->leaf[i] > 126)
) break;
}
if (i < t->leaflen) {
/* send as literal */
out += sprintf(out, "{%d+}\015\012", (int)(t->leaflen));
memcpy(out, t->leaf, t->leaflen);
out += t->leaflen;
} else {
/* send as regular string */
*(out++) = 34;
memcpy(out, t->leaf, t->leaflen);
out += t->leaflen;
*(out++) = 34;
}
break;
case TLTYPE_CONTINUATION:
*(out++) = '+';
*(out++) = ' ';
break;
case TLTYPE_END:
case TLTYPE_POP:
case TLTYPE_SUB:
break;
}
i = 0;
for (t = t->first; t; t = t->next) {
if (i) {
*(out++) = ' ';
} else {
i = 1;
}
out = format_cmd(out, ll, t);
}
if (close) {
*(out++) = close;
}
return out;
}
static int
imap_ll_write(struct imap_ll *ll, const char *p, const char *cmdend)
{
ssize_t len;
struct pollfd pfd;
int ctimeout, n;
#ifdef USE_OPENSSL
int ret2;
if (ll->ssl) {
while (p < cmdend) {
n = SSL_write(ll->ssl, p, cmdend-p);
if (n > 0) {
p += n;
} else {
ret2 = SSL_get_error(ll->ssl, n);
if ((ret2 == SSL_ERROR_WANT_READ) || (ret2 == SSL_ERROR_WANT_WRITE)) {
if (ret2 == SSL_ERROR_WANT_READ) {
pfd.fd = ll->r_socket;
pfd.events = POLLIN;
} else {
pfd.fd = ll->w_socket;
pfd.events = POLLOUT;
}
ctimeout = ll->timeout_time - time(NULL);
if (ctimeout < 0) ctimeout = 0;
if ((n = poll(&pfd, 1, ctimeout*1000)) < 0) {
if (errno == EINTR) continue;
goto pollerr;
}
if (n == 0) goto polltimeout;
} else {
ERR_print_errors_fp(stderr);
return 0;
}
}
}
} else {
#endif
pfd.fd = ll->w_socket;
pfd.events = POLLOUT;
while (p < cmdend) {
repoll:
ctimeout = ll->timeout_time - time(NULL);
if (ctimeout < 0) ctimeout = 0;
if ((n = poll(&pfd, 1, ctimeout*1000)) < 0) {
if (errno == EINTR) goto repoll;
#ifdef USE_OPENSSL
pollerr:
#endif
fprintf(stderr, "imap_ll_command: poll: %s\n", strerror(errno));
return 0;
}
if (n == 0) {
#ifdef USE_OPENSSL
polltimeout:
#endif
fprintf(stderr, "imap_ll_command: timeout\n");
return 0;
}
len = write(ll->w_socket, p, cmdend-p);
if (len < 0) {
if (errno == EAGAIN) continue;
if (errno == EINTR) continue;
fprintf(stderr, "imap_ll_command: write: %s\n", strerror(errno));
return 0;
} else if (len == 0) {
fprintf(stderr, "imap_ll_command: short write\n");
return 0;
}
p += len;
}
#ifdef USE_OPENSSL
}
#endif
return 1;
}
struct imap_ll_tokenlist *
imap_ll_command(struct imap_ll *ll, struct imap_ll_tokenlist *cmd, int timeout)
{
size_t len;
char *cmds, *cmdend;
struct imap_ll_tokenlist *result, *cur;
len = string_len(cmd);
cmds = malloc(len+1);
if (!cmds) {
fprintf(stderr, "imap_ll_command: malloc failed\n");
return NULL;
}
cmdend = format_cmd(cmds, ll, cmd);
*(cmdend++) = 13;
*(cmdend++) = 10;
imap_ll_timeout(ll, timeout);
if (!imap_ll_write(ll, cmds, cmdend)) {
free(cmds);
return NULL;
}
free(cmds);
result = mktoken(TLTYPE_LIST, NULL, 0);
for (;;) {
if (!(cur = imap_ll_waitline(ll))) {
imap_ll_freeline(result);
return NULL;
}
cur->parent = result;
cur->next = NULL;
if (result->first) {
result->last->next = cur;
} else {
result->first = cur;
}
result->last = cur;
if (cur->type == TLTYPE_TAGGED) {
cur->next = NULL;
return result;
}
}
}
void
imap_ll_logout(struct imap_ll *ll)
{
size_t len;
struct imap_ll_tokenlist *cur;
char cmd[25];
len = sprintf(cmd, "t%d LOGOUT\015\012", ++(ll->current_tag_number));
imap_ll_timeout(ll, 5);
if (!imap_ll_write(ll, cmd, cmd+len)) return;
imap_ll_timeout(ll, 5);
for (;;) {
if (!(cur = imap_ll_waitline(ll))) {
return;
}
if (cur->type == TLTYPE_TAGGED) {
break;
}
imap_ll_freeline(cur);
}
imap_ll_freeline(cur);
}
struct imap_ll_tokenlist *
imap_ll_build(enum imap_ll_tltype maintype, ...)
{
va_list ap;
struct imap_ll_tokenlist *top = NULL;
struct imap_ll_tokenlist *cur = NULL;
struct imap_ll_tokenlist *l;
char *s;
size_t slen;
int descend;
enum imap_ll_tltype next_type = maintype;
va_start(ap, maintype);
for (;;) {
switch (next_type) {
default:
case TLTYPE_END:
va_end(ap);
return top;
case TLTYPE_SUB:
l = va_arg(ap, struct imap_ll_tokenlist *);
descend = 0;
break;
case TLTYPE_ATOM:
case TLTYPE_STRING:
s = va_arg(ap, char *);
slen = va_arg(ap, size_t);
if (slen == -1) slen = strlen(s);
l = malloc(sizeof(*l));
if (!l) {
nomem:
fprintf(stderr, "imap_ll_build: malloc failure\n");
imap_ll_freeline(top);
va_end(ap);
return NULL;
}
l->type = next_type;
l->leaf = malloc(slen+1);
if (!(l->leaf)) {
free(l);
goto nomem;
}
memcpy(l->leaf, s, slen);
l->leaf[slen] = 0;
l->leaflen = slen;
descend = 0;
break;
case TLTYPE_TAGGED:
case TLTYPE_UNTAGGED:
case TLTYPE_LIST:
case TLTYPE_SQLIST:
l = malloc(sizeof(*l));
if (!l) goto nomem;
l->type = next_type;
l->leaf = NULL;
descend = 1;
break;
case TLTYPE_CONTINUATION:
l = malloc(sizeof(*l));
if (!l) goto nomem;
l->type = TLTYPE_CONTINUATION;
l->leaf = NULL;
descend = 0;
break;
case TLTYPE_POP:
break;
}
if (next_type == TLTYPE_POP) {
cur = cur->parent;
} else {
if (next_type != TLTYPE_SUB) l->first = l->last = NULL;
l->parent = cur;
l->next = NULL;
if (cur) {
if (cur->last) {
cur->last->next = l;
cur->last = l;
} else {
cur->first = cur->last = l;
}
} else {
top = cur = l;
}
if (descend) cur = l;
}
next_type = va_arg(ap, int);
}
}
void
imap_ll_append(struct imap_ll_tokenlist *list, struct imap_ll_tokenlist *item)
{
if (list->last) {
list->last->next = item;
} else {
list->first = item;
}
list->last = item;
item->parent = list;
}
const char *
imap_ll_status(struct imap_ll_tokenlist *t)
{
t = t->last->first;
if (!t) return "BAD";
if ((t->type) != TLTYPE_ATOM) return "BAD";
return t->leaf;
}
int
imap_ll_is_trycreate(struct imap_ll_tokenlist *t)
{
return (
(0 == strcmp(imap_ll_status(t), "NO")) &&
(t->last->first->next) &&
((t->last->first->next->type) == TLTYPE_SQLIST) &&
(t->last->first->next->first) &&
((t->last->first->next->first->type) == TLTYPE_ATOM) &&
(0 == strcmp(t->last->first->next->first->leaf, "TRYCREATE"))
);
}
#ifdef USE_OPENSSL
enum imap_ll_starttls_result
imap_ll_starttls(struct imap_ll *ll, SSL_CTX *sslctx, const char *servername)
{
struct imap_ll_tokenlist *cmd, *l;
const char *status;
struct pollfd pfd;
int ret, ret2, timeout;
SSL *ssl;
long vr;
X509 *peer;
size_t servernamelen, certnamelen;
char buf[256];
char buf2[256];
ssl = SSL_new(sslctx);
if (!ssl) {
ERR_print_errors_fp(stderr);
}
cmd = imap_ll_build(TLTYPE_TAGGED, TLTYPE_ATOM, "STARTTLS", (size_t)-1, TLTYPE_END);
if (!cmd) {
fprintf(stderr, "could not build STARTTLS command\n");
SSL_free(ll->ssl); ll->ssl = NULL;
return IMAP_LL_STARTTLS_FAILED_PROCEED;
}
l = imap_ll_command(ll, cmd, 5);
imap_ll_freeline(cmd);
status = imap_ll_status(l);
if (0 == strcmp(status, "NO")) {
fprintf(stderr, "STARTTLS not accepted by server\n");
imap_ll_freeline(l);
SSL_free(ll->ssl); ll->ssl = NULL;
return IMAP_LL_STARTTLS_FAILED_PROCEED;
} else if (0 != strcmp(status, "OK")) {
fprintf(stderr, "STARTTLS not understood by server\n");
imap_ll_freeline(l);
SSL_free(ll->ssl); ll->ssl = NULL;
return IMAP_LL_STARTTLS_FAILED_PROCEED;
}
imap_ll_freeline(l);
SSL_set_rfd(ssl, ll->r_socket);
SSL_set_wfd(ssl, ll->w_socket);
imap_ll_timeout(ll, 10); /* reset clock */
for (;;) {
ret = SSL_connect(ssl);
if (ret == 1) break;
ret2 = SSL_get_error(ssl, ret);
if ((ret2 == SSL_ERROR_WANT_READ) || (ret2 == SSL_ERROR_WANT_WRITE)) {
if (ret2 == SSL_ERROR_WANT_READ) {
pfd.fd = ll->r_socket;
pfd.events = POLLIN;
} else {
pfd.fd = ll->w_socket;
pfd.events = POLLOUT;
}
timeout = ll->timeout_time - time(NULL);
if (timeout < 0) timeout = 0;
if ((ret = poll(&pfd, 1, timeout*1000)) < 0) {
if (errno == EINTR) continue;
fprintf(stderr, "STARTTLS poll error: %s\n", strerror(errno));
SSL_free(ssl);
return IMAP_LL_STARTTLS_FAILED;
}
if (ret == 0) {
fprintf(stderr, "STARTTLS timeout\n");
SSL_free(ssl);
return IMAP_LL_STARTTLS_FAILED;
}
} else {
ERR_error_string(ERR_get_error(), &(buf[0]));
fprintf(stderr, "STARTTLS: %s\n", buf);
SSL_free(ssl);
return IMAP_LL_STARTTLS_FAILED;
}
}
if ((vr = SSL_get_verify_result(ssl)) != X509_V_OK) {
fprintf(stderr, "STARTTLS: ceritifcate verification failed (%ld)\n", vr);
certfail:
SSL_free(ssl);
return IMAP_LL_STARTTLS_FAILED_CERT;
}
if (!(peer = SSL_get_peer_certificate(ssl))) {
fprintf(stderr, "STARTTLS: no peer certificate?\n");
goto certfail;
}
if (X509_NAME_get_text_by_NID(X509_get_subject_name(peer), NID_commonName, buf, sizeof(buf)) == -1) {
fprintf(stderr, "STARTTLS: no common name in certificate?\n");
goto certfail;
}
if (!servername) goto certok; /* caller does not want us to check */
if (0 != strcasecmp(buf, servername)){
/* Certificate name does not match exactly.
Check for a wildcard certificate name.
The only valid form for certificate naming wildcards is
an asterisk as the first character followed by any number
of non-wild characters */
if (buf[0] == '*') {
servernamelen = strlen(servername);
certnamelen = strlen(buf+1);
if (
(servernamelen >= certnamelen) &&
(0 == strcasecmp(
buf+1,
servername + (servernamelen-certnamelen))
)
) {
goto certok;
}
}
snprintf(buf2, sizeof(buf2), "%s", servername);
fprintf(stderr, "STARTTLS: server name \"%s\" != certificate common name \"%s\"\n", buf2, buf);
goto certfail;
}
certok:
ll->ssl = ssl;
return IMAP_LL_STARTTLS_SUCCESS;
}
#endif
void
imap_ll_pprint(struct imap_ll_tokenlist *t, int indent, FILE *out)
{
int plusindent = 0;
int i;
const char *close = NULL;
switch (t->type) {
case TLTYPE_UNTAGGED:
for (i = 0; i < indent; i++) fputc(9, out);
fprintf(out, "*\n");
break;
case TLTYPE_TAGGED:
break;
case TLTYPE_LIST:
plusindent = 1;
for (i = 0; i < indent; i++) fputc(9, out);
fprintf(out, "(\n"); close = ")\n";
break;