/
imap_proxy.c
1625 lines (1417 loc) · 52.5 KB
/
imap_proxy.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
/* imap_proxy.c - IMAP proxy support functions
*
* Copyright (c) 1994-2008 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any legal
* details, please contact
* Carnegie Mellon University
* Center for Technology Transfer and Enterprise Creation
* 4615 Forbes Avenue
* Suite 302
* Pittsburgh, PA 15213
* (412) 268-7393, fax: (412) 268-7395
* innovation@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF 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 <config.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <syslog.h>
#include <sys/un.h>
#include "assert.h"
#include "acl.h"
#include "annotate.h"
#include "backend.h"
#include "global.h"
#include "imap_proxy.h"
#include "proxy.h"
#include "mboxname.h"
#include "mupdate-client.h"
#include "partlist.h"
#include "prot.h"
#include "util.h"
#include "xmalloc.h"
/* generated headers are not necessarily in current directory */
#include "imap/imap_err.h"
extern unsigned int proxy_cmdcnt;
extern struct protstream *imapd_in, *imapd_out;
extern struct backend *backend_inbox, *backend_current;
extern ptrarray_t backend_cached;
extern char *imapd_userid, *proxy_userid;
extern struct namespace imapd_namespace;
static partlist_t *server_parts = NULL;
static void proxy_part_filldata(partlist_t *part_list, int idx);
static void imap_postcapability(struct backend *s)
{
if (CAPA(s, CAPA_SASL_IR)) {
/* server supports initial response in AUTHENTICATE command */
s->prot->u.std.sasl_cmd.maxlen = USHRT_MAX;
}
}
struct protocol_t imap_protocol =
{ "imap", "imap", TYPE_STD,
{ { { 1, NULL },
{ "C01 CAPABILITY", NULL, "C01 ", imap_postcapability,
CAPAF_MANY_PER_LINE,
{ { "AUTH", CAPA_AUTH },
{ "STARTTLS", CAPA_STARTTLS },
{ "COMPRESS=DEFLATE", CAPA_COMPRESS },
{ "IDLE", CAPA_IDLE },
{ "MUPDATE", CAPA_MUPDATE },
{ "MULTIAPPEND", CAPA_MULTIAPPEND },
{ "METADATA", CAPA_METADATA },
{ "RIGHTS=kxte", CAPA_ACLRIGHTS },
{ "LIST-EXTENDED", CAPA_LISTEXTENDED },
{ "SASL-IR", CAPA_SASL_IR },
{ "X-REPLICATION", CAPA_REPLICATION },
{ "X-SIEVE-MAILBOX", CAPA_SIEVE_MAILBOX },
/* Need to bump MAX_CAPA in protocol.h if this array is extended */
{ NULL, 0 } } },
{ "S01 STARTTLS", "S01 OK", "S01 NO", 0 },
{ "A01 AUTHENTICATE", 0, 0, "A01 OK", "A01 NO", "+ ", "*",
NULL, AUTO_CAPA_AUTH_OK },
{ "Z01 COMPRESS DEFLATE", "* ", "Z01 OK" },
{ "N01 NOOP", "* ", "N01 OK" },
{ "Q01 LOGOUT", "* ", "Q01 " } } }
};
void proxy_gentag(char *tag, size_t len)
{
snprintf(tag, len, "PROXY%d", proxy_cmdcnt++);
}
struct backend *proxy_findinboxserver(const char *userid)
{
mbentry_t *mbentry = NULL;
struct backend *s = NULL;
char *inbox = mboxname_user_mbox(userid, NULL);
int r = mboxlist_lookup(inbox, &mbentry, NULL);
free(inbox);
if (r) return NULL;
if (mbentry->mbtype & MBTYPE_REMOTE) {
s = proxy_findserver(mbentry->server, &imap_protocol,
proxy_userid, &backend_cached,
&backend_current, &backend_inbox, imapd_in);
}
mboxlist_entry_free(&mbentry);
return s;
}
/* pipe_response() reads from 's->in' until either the tagged response
starting with 'tag' appears, or if 'tag' is NULL, to the end of the
current line. If 'include_last' is set, the last/tagged line is included
in the output, otherwise the last/tagged line is stored in 's->last_result'.
In either case, the result of the tagged command is returned.
's->last_result' assumes that tagged responses don't contain literals.
Unfortunately, the IMAP grammar allows them
force_notfatal says to not fatal() if we lose connection to backend_current
even though it is in 95% of the cases, a good idea...
*/
static int pipe_response(struct backend *s, const char *tag, int include_last,
int force_notfatal)
{
char buf[2048];
char eol[128];
unsigned sl;
int cont = 0, last = !tag, r = PROXY_OK;
size_t taglen = 0;
s->timeout->mark = time(NULL) + IDLE_TIMEOUT;
if (tag) {
taglen = strlen(tag);
if(taglen >= sizeof(buf) + 1) {
fatal("tag too large",EX_TEMPFAIL);
}
}
buf_reset(&s->last_result);
/* the only complication here are literals */
do {
/* if 'cont' is set, we're looking at the continuation to a very
long line.
if 'last' is set, we've seen the tag we're looking for, we're
just reading the end of the line. */
if (!cont) eol[0] = '\0';
if (!prot_fgets(buf, sizeof(buf), s->in)) {
/* uh oh */
if(s == backend_current && !force_notfatal)
fatal("Lost connection to selected backend", EX_UNAVAILABLE);
proxy_downserver(s);
return PROXY_NOCONNECTION;
}
sl = strlen(buf);
if (tag) {
/* Check for the tagged line */
if (!cont && buf[taglen] == ' ' && !strncmp(tag, buf, taglen)) {
switch (buf[taglen + 1]) {
case 'O': case 'o':
r = PROXY_OK;
break;
case 'N': case 'n':
r = PROXY_NO;
break;
case 'B': case 'b':
r = PROXY_BAD;
break;
default: /* huh? no result? */
if(s == backend_current && !force_notfatal)
fatal("Lost connection to selected backend",
EX_UNAVAILABLE);
proxy_downserver(s);
r = PROXY_NOCONNECTION;
break;
}
last = 1;
}
if (last && !include_last) {
/* Store the tagged line */
buf_appendcstr(&s->last_result, buf+taglen+1);
buf_cstring(&s->last_result);
}
}
if (sl == (sizeof(buf) - 1) && buf[sl-1] != '\n') {
/* only got part of a line */
/* we save the last 64 characters in case it has important
literal information */
strcpy(eol, buf + sl - 64);
/* write out this part, but we have to keep reading until we
hit the end of the line */
if (!last || include_last) prot_write(imapd_out, buf, sl);
cont = 1;
continue;
} else { /* we got the end of the line */
int i;
int litlen = 0, islit = 0;
if (!last || include_last) prot_write(imapd_out, buf, sl);
/* now we have to see if this line ends with a literal */
if (sl < 64) {
strcat(eol, buf);
} else {
strcat(eol, buf + sl - 63);
}
/* eol now contains the last characters from the line; we want
to see if we've hit a literal */
i = strlen(eol);
if (i >= 4 &&
eol[i-1] == '\n' && eol[i-2] == '\r' && eol[i-3] == '}') {
/* possible literal */
i -= 4;
while (i > 0 && eol[i] != '{' && Uisdigit(eol[i])) {
i--;
}
if (eol[i] == '{') {
islit = 1;
litlen = atoi(eol + i + 1);
}
}
/* copy the literal over */
if (islit) {
while (litlen > 0) {
int j = (litlen > (int) sizeof(buf) ?
(int) sizeof(buf) : litlen);
j = prot_read(s->in, buf, j);
if(!j) {
/* EOF or other error */
return -1;
}
if (!last || include_last) prot_write(imapd_out, buf, j);
litlen -= j;
}
/* none of our saved information has any relevance now */
eol[0] = '\0';
/* have to keep going for the end of the line */
cont = 1;
continue;
}
}
/* ok, let's read another line */
cont = 0;
} while (!last || cont);
return r;
}
int pipe_until_tag(struct backend *s, const char *tag, int force_notfatal)
{
return pipe_response(s, tag, 0, force_notfatal);
}
int pipe_including_tag(struct backend *s, const char *tag, int force_notfatal)
{
int r;
r = pipe_response(s, tag, 1, force_notfatal);
if (r == PROXY_NOCONNECTION) {
/* don't have to worry about downing the server, since
* pipe_until_tag does that for us */
prot_printf(imapd_out, "%s NO %s\r\n", tag,
error_message(IMAP_SERVER_UNAVAILABLE));
}
return r;
}
static int pipe_to_end_of_response(struct backend *s, int force_notfatal)
{
return pipe_response(s, NULL, 1, force_notfatal);
}
/* copy our current input to 's' until we hit a true EOL.
'optimistic_literal' is how happy we should be about assuming
that a command will go through by converting synchronizing literals of
size less than optimistic_literal to nonsync
returns 0 on success, <0 on big failure, >0 on full command not sent */
int pipe_command(struct backend *s, int optimistic_literal)
{
char buf[2048];
char eol[128];
int sl;
s->timeout->mark = time(NULL) + IDLE_TIMEOUT;
eol[0] = '\0';
/* again, the complication here are literals */
for (;;) {
if (!prot_fgets(buf, sizeof(buf), imapd_in)) {
/* uh oh */
return -1;
}
sl = strlen(buf);
if (sl == (sizeof(buf) - 1) && buf[sl-1] != '\n') {
/* only got part of a line */
strcpy(eol, buf + sl - 64);
/* and write this out, except for what we've saved */
prot_write(s->out, buf, sl - 64);
continue;
} else {
int i, nonsynch = 0, islit = 0, litlen = 0;
if (sl < 64) {
strcat(eol, buf);
} else {
/* write out what we have, and copy the last 64 characters
to eol */
prot_printf(s->out, "%s", eol);
prot_write(s->out, buf, sl - 64);
strcpy(eol, buf + sl - 64);
}
/* now determine if eol has a literal in it */
i = strlen(eol);
if (i >= 4 &&
eol[i-1] == '\n' && eol[i-2] == '\r' && eol[i-3] == '}') {
/* possible literal */
i -= 4;
if (eol[i] == '+') {
nonsynch = 1;
i--;
}
while (i > 0 && eol[i] != '{' && Uisdigit(eol[i])) {
i--;
}
if (eol[i] == '{') {
islit = 1;
litlen = atoi(eol + i + 1);
}
}
if (islit) {
if (nonsynch) {
prot_write(s->out, eol, strlen(eol));
} else if (!nonsynch && (litlen <= optimistic_literal)) {
prot_printf(imapd_out, "+ i am an optimist\r\n");
prot_write(s->out, eol, strlen(eol) - 3);
/* need to insert a + to turn it into a nonsynch */
prot_printf(s->out, "+}\r\n");
} else {
/* we do a standard synchronizing literal */
prot_write(s->out, eol, strlen(eol));
/* but here the game gets tricky... */
prot_fgets(buf, sizeof(buf), s->in);
/* but for now we cheat */
prot_write(imapd_out, buf, strlen(buf));
if (buf[0] != '+' && buf[1] != ' ') {
/* char *p = strchr(buf, ' '); */
/* strncpy(s->last_result, p + 1, LAST_RESULT_LEN);*/
/* stop sending command now */
return 1;
}
}
/* gobble literal and sent it onward */
while (litlen > 0) {
int j = (litlen > (int) sizeof(buf) ?
(int) sizeof(buf) : litlen);
j = prot_read(imapd_in, buf, j);
if(!j) {
/* EOF or other error */
return -1;
}
prot_write(s->out, buf, j);
litlen -= j;
}
eol[0] = '\0';
/* have to keep going for the send of the command */
continue;
} else {
/* no literal, so we're done! */
prot_write(s->out, eol, strlen(eol));
return 0;
}
}
}
}
void print_listresponse(unsigned cmd, const char *extname, char hier_sep,
uint32_t attributes, struct buf *extraflags)
{
const struct mbox_name_attribute *attr;
const char *resp, *sep;
switch (cmd) {
case LIST_CMD_LSUB:
resp = "LSUB";
break;
case LIST_CMD_XLIST:
resp = "XLIST";
break;
default:
resp = "LIST";
break;
}
prot_printf(imapd_out, "* %s (", resp);
for (sep = "", attr = mbox_name_attributes; attr->id; attr++) {
if (attributes & attr->flag) {
prot_printf(imapd_out, "%s%s", sep, attr->id);
sep = " ";
}
}
if (extraflags && buf_len(extraflags)) {
prot_printf(imapd_out, "%s%s", sep, buf_cstring(extraflags));
}
prot_printf(imapd_out, ") \"%c\" ", hier_sep);
prot_printastring(imapd_out, extname);
if (attributes & MBOX_ATTRIBUTE_CHILDINFO_SUBSCRIBED) {
prot_puts(imapd_out, " (CHILDINFO (");
/* RFC 5258:
* ; Note 2: The selection options are always returned
* ; quoted, unlike their specification in
* ; the extended LIST command.
*/
if (attributes & MBOX_ATTRIBUTE_CHILDINFO_SUBSCRIBED)
prot_puts(imapd_out, "\"SUBSCRIBED\"");
prot_puts(imapd_out, "))");
}
prot_puts(imapd_out, "\r\n");
}
/* add subscription flags or filter out non-subscribed mailboxes */
static int check_subs(mbentry_t *mbentry, strarray_t *subs,
struct listargs *listargs, uint32_t *flags)
{
int i, namelen = strlen(mbentry->name);
for (i = 0; i < subs->count; i++) {
const char *name = strarray_nth(subs, i);
if (strncmp(mbentry->name, name, namelen)) continue;
else if (!name[namelen]) { /* exact match */
*flags |= MBOX_ATTRIBUTE_SUBSCRIBED;
break;
}
else if (name[namelen] == '.' &&
(listargs->sel & LIST_SEL_RECURSIVEMATCH)) {
*flags |= MBOX_ATTRIBUTE_CHILDINFO_SUBSCRIBED;
break;
}
}
/* check if we need to filter out this mailbox */
return (!(listargs->sel & LIST_SEL_SUBSCRIBED) ||
(*flags & (MBOX_ATTRIBUTE_SUBSCRIBED |
MBOX_ATTRIBUTE_CHILDINFO_SUBSCRIBED)));
}
/* add subscribed mailbox or ancestor of subscribed mailbox to our list */
static void add_sub(strarray_t *subs, mbentry_t *mbentry, uint32_t attributes)
{
if (attributes & MBOX_ATTRIBUTE_SUBSCRIBED) {
/* mailbox is subscribed */
strarray_append(subs, mbentry->name);
}
else {
/* a descendent of mailbox is subscribed */
struct buf child = BUF_INITIALIZER;
buf_printf(&child, "%s.", mbentry->name);
strarray_appendm(subs, buf_release(&child));
}
}
static int is_extended_resp(const char *cmd, struct listargs *listargs)
{
if (!(listargs->ret &
(LIST_RET_STATUS | LIST_RET_MYRIGHTS | LIST_RET_METADATA))) {
/* backend won't be sending extended response data */
return 0;
}
else if ((listargs->ret & LIST_RET_STATUS) &&
!strncasecmp("STATUS", cmd, 6)) {
return 1;
}
else if ((listargs->ret & LIST_RET_MYRIGHTS) &&
!strncasecmp("MYRIGHTS", cmd, 8)) {
return 1;
}
else if ((listargs->ret & LIST_RET_METADATA) &&
!strncasecmp("METADATA", cmd, 8)) {
return 1;
}
return 0;
}
/* This handles piping of the LSUB command, because we have to figure out
* what mailboxes actually exist before passing them to the end user.
*
* It is also needed if we are doing a LIST-EXTENDED, to capture subscriptions
* and/or return data that can only be obtained from the backends.
*/
int pipe_lsub(struct backend *s, const char *userid, const char *tag,
int force_notfatal, struct listargs *listargs, strarray_t *subs)
{
int taglen = strlen(tag);
int c;
int r = PROXY_OK;
int exist_r;
static struct buf tagb, cmd, sep, name, ext;
struct buf extraflags = BUF_INITIALIZER;
int build_list_only = subs && !(listargs->ret & LIST_RET_SUBSCRIBED);
int suppress_resp = 0;
assert(s);
assert(s->timeout);
s->timeout->mark = time(NULL) + IDLE_TIMEOUT;
while(1) {
c = getword(s->in, &tagb);
if(c == EOF) {
if(s == backend_current && !force_notfatal)
fatal("Lost connection to selected backend", EX_UNAVAILABLE);
proxy_downserver(s);
r = PROXY_NOCONNECTION;
goto out;
}
if(!strncmp(tag, tagb.s, taglen)) {
char buf[2048];
if(!prot_fgets(buf, sizeof(buf), s->in)) {
if(s == backend_current && !force_notfatal)
fatal("Lost connection to selected backend",
EX_UNAVAILABLE);
proxy_downserver(s);
r = PROXY_NOCONNECTION;
goto out;
}
/* Got the end of the response */
buf_appendcstr(&s->last_result, buf);
buf_cstring(&s->last_result);
switch (buf[0]) {
case 'O': case 'o':
r = PROXY_OK;
break;
case 'N': case 'n':
r = PROXY_NO;
break;
case 'B': case 'b':
r = PROXY_BAD;
break;
default: /* huh? no result? */
if(s == backend_current && !force_notfatal)
fatal("Lost connection to selected backend",
EX_UNAVAILABLE);
proxy_downserver(s);
r = PROXY_NOCONNECTION;
break;
}
break; /* we're done */
}
c = getword(s->in, &cmd);
if(c == EOF) {
if(s == backend_current && !force_notfatal)
fatal("Lost connection to selected backend", EX_UNAVAILABLE);
proxy_downserver(s);
r = PROXY_NOCONNECTION;
goto out;
}
if(strncasecmp("LSUB", cmd.s, 4) && strncasecmp("LIST", cmd.s, 4)) {
if (suppress_resp && is_extended_resp(cmd.s, listargs)) {
/* suppress extended return data for this mailbox */
eatline(s->in, c);
}
else {
prot_printf(imapd_out, "%s %s ", tagb.s, cmd.s);
r = pipe_to_end_of_response(s, force_notfatal);
if (r != PROXY_OK)
goto out;
}
} else {
/* build up the response bit by bit */
const struct mbox_name_attribute *attr;
uint32_t attributes = 0;
/* Get flags */
buf_reset(&extraflags);
c = prot_getc(s->in);
if (c == '(') {
do {
c = getword(s->in, &name);
for (attr = mbox_name_attributes;
attr->id && strcasecmp(name.s, attr->id); attr++);
if (attr->id) attributes |= attr->flag;
else {
if (buf_len(&extraflags)) buf_putc(&extraflags, ' ');
buf_appendcstr(&extraflags, name.s);
}
} while (c == ' ');
if (c == ')') {
/* end of flags - get the next character */
c = prot_getc(s->in);
}
}
if(c != ' ') {
if(s == backend_current && !force_notfatal)
fatal("Bad LSUB response from selected backend",
EX_UNAVAILABLE);
proxy_downserver(s);
r = PROXY_NOCONNECTION;
goto out;
}
/* Get separator */
c = getastring(s->in, s->out, &sep);
if(c != ' ') {
if(s == backend_current && !force_notfatal)
fatal("Bad LSUB response from selected backend",
EX_UNAVAILABLE);
proxy_downserver(s);
r = PROXY_NOCONNECTION;
goto out;
}
/* Get name */
c = getastring(s->in, s->out, &name);
/* Get extension(s) */
buf_reset(&ext);
if (c == ' ') {
do {
buf_putc(&ext, c);
c = prot_getc(s->in);
} while (c != '\r' && c != '\n' && c != EOF);
/* XXX Currently there are no other documented extensions */
attributes |= MBOX_ATTRIBUTE_CHILDINFO_SUBSCRIBED;
}
buf_cstring(&ext);
if(c == '\r') c = prot_getc(s->in);
if(c != '\n') {
if(s == backend_current && !force_notfatal)
fatal("Bad LSUB response from selected backend",
EX_UNAVAILABLE);
proxy_downserver(s);
r = PROXY_NOCONNECTION;
goto out;
}
/* lookup name */
exist_r = 1;
char *intname =
mboxname_from_external(name.s, &imapd_namespace, userid);
mbentry_t *mbentry = NULL;
exist_r = mboxlist_lookup(intname, &mbentry, NULL);
free(intname);
if(!exist_r && (mbentry->mbtype & MBTYPE_RESERVE))
exist_r = IMAP_MAILBOX_RESERVED;
/* suppress responses to client if we're just building subs list */
suppress_resp = build_list_only;
if (!exist_r) {
/* we need to remove \Noselect if it's in our mailboxes.db */
attributes &=
~(MBOX_ATTRIBUTE_NOSELECT | MBOX_ATTRIBUTE_NONEXISTENT);
if (subs) {
/* process subscriptions */
if (s != backend_inbox) {
/* backend server won't have subs info -
add sub flags or filter out non-sub mailboxes */
if (!check_subs(mbentry, subs, listargs, &attributes)) {
/* unsubscribed and we only want subscriptions */
suppress_resp = 1;
}
}
else if (listargs->sel & LIST_SEL_SUBSCRIBED) {
/* If we're just building subs list,
we want ALL subscriptions added to list.
Otherwise just add those NOT on Inbox server. */
if (build_list_only ||
strcmp(mbentry->server, backend_inbox->hostname)) {
add_sub(subs, mbentry, attributes);
/* suppress the response - those NOT added to the
list are sent to client in subsequent requests */
suppress_resp = 1;
}
}
}
}
if (!suppress_resp) {
/* send response to the client */
print_listresponse(listargs->cmd, name.s, sep.s[0],
attributes, &extraflags);
/* send any PROXY_ONLY metadata items */
for (c = 0; c < listargs->metaitems.count; c++) {
const char *entry = strarray_nth(&listargs->metaitems, c);
if (mbentry &&
!strcmp(entry, "/shared" IMAP_ANNOT_NS "server")) {
prot_puts(imapd_out, "* METADATA ");
prot_printastring(imapd_out, name.s);
prot_puts(imapd_out, " (");
prot_printstring(imapd_out, entry);
prot_puts(imapd_out, " ");
prot_printstring(imapd_out, mbentry->server);
prot_puts(imapd_out, ")\r\n");
}
}
}
mboxlist_entry_free(&mbentry);
}
} /* while(1) */
out:
buf_free(&extraflags);
return r;
}
/* xxx start of separate proxy-only code
(remove when we move to a unified environment) */
static int chomp(struct protstream *p, const char *s)
{
int c = prot_getc(p);
while (*s) {
if (tolower(c) != tolower(*s)) { break; }
s++;
c = prot_getc(p);
}
if (*s) {
if (c != EOF) prot_ungetc(c, p);
c = EOF;
}
return c;
}
#define BUFGROWSIZE 100
/* read characters from 'p' until 'end' is seen */
static char *grab(struct protstream *p, char end)
{
int alloc = BUFGROWSIZE, cur = 0;
int c = -1;
char *ret = (char *) xmalloc(alloc);
ret[0] = '\0';
while ((c = prot_getc(p)) != end) {
if (c == EOF) break;
if (cur == alloc - 1) {
alloc += BUFGROWSIZE;
ret = xrealloc(ret, alloc);
}
ret[cur++] = c;
}
if (cur) ret[cur] = '\0';
return ret;
}
/* remove \Recent from the flags */
static char *editflags(char *flags)
{
char *p;
p = flags;
while ((p = strchr(p, '\\')) != NULL) {
if (!strncasecmp(p + 1, "recent", 6)) {
if (p[7] == ' ') {
/* shift everything over so that \recent vanishes */
char *q;
q = p + 8;
while (*q) {
*p++ = *q++;
}
*p = '\0';
} else if (p[7] == '\0') {
/* last flag in line */
*p = '\0';
} else {
/* not really \recent, i guess */
p++;
}
} else {
p++;
}
}
return flags;
}
void proxy_copy(const char *tag, char *sequence, char *name, int myrights,
int usinguid, struct backend *s)
{
char mytag[128];
struct d {
char *idate;
char *flags;
unsigned int seqno, uid;
struct d *next;
} *head, *p, *q;
int c;
/* find out what the flags & internaldate for this message are */
proxy_gentag(mytag, sizeof(mytag));
prot_printf(backend_current->out,
"%s %s %s (Flags Internaldate)\r\n",
tag, usinguid ? "Uid Fetch" : "Fetch", sequence);
head = (struct d *) xmalloc(sizeof(struct d));
head->flags = NULL; head->idate = NULL;
head->seqno = head->uid = 0;
head->next = NULL;
p = head;
/* read all the responses into the linked list */
for (/* each FETCH response */;;) {
unsigned int seqno = 0, uidno = 0;
char *flags = NULL, *idate = NULL;
/* read a line */
c = prot_getc(backend_current->in);
if (c != '*') break;
c = prot_getc(backend_current->in);
if (c != ' ') { /* protocol error */ c = EOF; break; }
/* check for OK/NO/BAD/BYE response */
if (!isdigit(c = prot_getc(backend_current->in))) {
prot_printf(imapd_out, "* %c", c);
pipe_to_end_of_response(backend_current, 0);
continue;
}
/* read seqno */
prot_ungetc(c, backend_current->in);
c = getuint32(backend_current->in, &seqno);
if (seqno == 0 || c != ' ') {
/* we suck and won't handle this case */
c = EOF; break;
}
c = chomp(backend_current->in, "fetch (");
if (c == EOF) {
c = chomp(backend_current->in, "exists\r");
if (c == '\n') { /* got EXISTS response */
prot_printf(imapd_out, "* %d EXISTS\r\n", seqno);
continue;
}
}
if (c == EOF) {
/* XXX the "exists" check above will eat "ex" */
c = chomp(backend_current->in, "punge\r");
if (c == '\n') { /* got EXPUNGE response */
prot_printf(imapd_out, "* %d EXPUNGE\r\n", seqno);
continue;
}
}
if (c == EOF) {
c = chomp(backend_current->in, "recent\r");
if (c == '\n') { /* got RECENT response */
prot_printf(imapd_out, "* %d RECENT\r\n", seqno);
continue;
}
}
/* huh, don't get this response */
if (c == EOF) break;
for (/* each fetch item */;;) {
/* looking at the first character in an item */
switch (c) {
case 'f': case 'F': /* flags? */
c = chomp(backend_current->in, "lags");
if (c != ' ') { c = EOF; }
else c = prot_getc(backend_current->in);
if (c != '(') { c = EOF; }
else {
flags = grab(backend_current->in, ')');
c = prot_getc(backend_current->in);
}
break;
case 'i': case 'I': /* internaldate? */
c = chomp(backend_current->in, "nternaldate");
if (c != ' ') { c = EOF; }
else c = prot_getc(backend_current->in);
if (c != '"') { c = EOF; }
else {
idate = grab(backend_current->in, '"');
c = prot_getc(backend_current->in);
}
break;
case 'u': case 'U': /* uid */
c = chomp(backend_current->in, "id");
if (c != ' ') { c = EOF; }
else c = getuint32(backend_current->in, &uidno);
break;
default: /* hmm, don't like the smell of it */
c = EOF;
break;
}
/* looking at either SP separating items or a RPAREN */
if (c == ' ') { c = prot_getc(backend_current->in); }
else if (c == ')') break;
else { c = EOF; break; }
}
/* if c == EOF we have either a protocol error or a situation
we can't handle, and we should die. */
if (c == ')') c = prot_getc(backend_current->in);
if (c == '\r') c = prot_getc(backend_current->in);
if (c != '\n') {
c = EOF;
free(flags);
free(idate);
break;
}
/* if we're missing something, we should echo */
if (!flags || !idate) {
char sep = '(';
prot_printf(imapd_out, "* %d FETCH ", seqno);
if (uidno) {
prot_printf(imapd_out, "%cUID %d", sep, uidno);
sep = ' ';
}
if (flags) {
prot_printf(imapd_out, "%cFLAGS %s", sep, flags);
sep = ' ';
}
if (idate) {
prot_printf(imapd_out, "%cINTERNALDATE %s", sep, idate);
sep = ' ';