File: http_ischedule.c

package info (click to toggle)
cyrus-imapd 3.6.1-4%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,688 kB
  • sloc: ansic: 255,928; perl: 97,730; javascript: 9,266; sh: 5,537; yacc: 2,651; cpp: 2,128; makefile: 2,099; lex: 660; xml: 621; python: 388; awk: 303; asm: 262
file content (1196 lines) | stat: -rw-r--r-- 42,381 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
/* http_ischedule.c -- Routines for handling iSchedule in httpd
 *
 * Copyright (c) 1994-2012 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>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <sysexits.h>
#include <syslog.h>

#include <libical/ical.h>

#include "global.h"
#include "httpd.h"
#include "http_caldav_sched.h"
#include "http_dav.h"
#include "http_proxy.h"
#include "jcal.h"
#include "map.h"
#include "proxy.h"
#include "tok.h"
#include "util.h"
#include "xmalloc.h"
#include "xcal.h"
#include "xstrlcpy.h"

/* generated headers are not necessarily in current directory */
#include "imap/http_err.h"

#include <sasl/saslutil.h>

#define ISCHED_WELLKNOWN_URI "/.well-known/ischedule"

#ifdef WITH_DKIM
#include <dkim.h>

//#define TEST

#define BASE64_LEN(inlen) ((((inlen) + 2) / 3) * 4)

static DKIM_LIB *dkim_lib = NULL;
static struct buf privkey = BUF_INITIALIZER;
static struct buf tmpbuf = BUF_INITIALIZER;
static struct buf b64req = BUF_INITIALIZER;
#endif /* WITH_DKIM */

static void isched_init(struct buf *serverinfo);
static void isched_shutdown(void);

static int meth_get_isched(struct transaction_t *txn, void *params);
static int meth_options_isched(struct transaction_t *txn, void *params);
static int meth_post_isched(struct transaction_t *txn, void *params);
static int dkim_auth(struct transaction_t *txn);
static int meth_get_domainkey(struct transaction_t *txn, void *params);
static time_t compile_time;

static struct mime_type_t isched_mime_types[] = {
    /* First item MUST be the default type and storage format */
    { "text/calendar; charset=utf-8", "2.0", "ics",
      (struct buf* (*)(void *)) &my_icalcomponent_as_ical_string,
      (void * (*)(const struct buf*)) &ical_string_as_icalcomponent,
      (void (*)(void *)) &icalcomponent_free, NULL, NULL
    },
    { "application/calendar+xml; charset=utf-8", NULL, "xcs",
      (struct buf* (*)(void *)) &icalcomponent_as_xcal_string,
      (void * (*)(const struct buf*)) &xcal_string_as_icalcomponent,
      NULL, NULL, NULL
    },
    { "application/calendar+json; charset=utf-8", NULL, "jcs",
      (struct buf* (*)(void *)) &icalcomponent_as_jcal_string,
      (void * (*)(const struct buf*)) &jcal_string_as_icalcomponent,
      NULL, NULL, NULL,
    },
    { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};

struct namespace_t namespace_ischedule = {
    URL_NS_ISCHEDULE, 0, "ischedule", "/ischedule", ISCHED_WELLKNOWN_URI,
    http_allow_noauth, /*authschemes*/0,
    /*mbtype*/0,
    (ALLOW_READ | ALLOW_POST | ALLOW_ISCHEDULE),
    isched_init, NULL, NULL, isched_shutdown, NULL,
    {
        { NULL,                 NULL }, /* ACL          */
        { NULL,                 NULL }, /* BIND         */
        { NULL,                 NULL }, /* CONNECT      */
        { NULL,                 NULL }, /* COPY         */
        { NULL,                 NULL }, /* DELETE       */
        { &meth_get_isched,     NULL }, /* GET          */
        { &meth_get_isched,     NULL }, /* HEAD         */
        { NULL,                 NULL }, /* LOCK         */
        { NULL,                 NULL }, /* MKCALENDAR   */
        { NULL,                 NULL }, /* MKCOL        */
        { NULL,                 NULL }, /* MOVE         */
        { &meth_options_isched, NULL }, /* OPTIONS      */
        { NULL,                 NULL }, /* PATCH        */
        { &meth_post_isched,    NULL }, /* POST         */
        { NULL,                 NULL }, /* PROPFIND     */
        { NULL,                 NULL }, /* PROPPATCH    */
        { NULL,                 NULL }, /* PUT          */
        { NULL,                 NULL }, /* REPORT       */
        { &meth_trace,          NULL }, /* TRACE        */
        { NULL,                 NULL }, /* UNBIND       */
        { NULL,                 NULL }  /* UNLOCK       */
    }
};

struct namespace_t namespace_domainkey = {
    URL_NS_DOMAINKEY, 0, "domainkey", "/domainkeys", "/.well-known/domainkey",
    http_allow_noauth, /*authschemes*/0,
    /*mbtype*/0,
    ALLOW_READ,
    NULL, NULL, NULL, NULL, NULL,
    {
        { NULL,                 NULL }, /* ACL          */
        { NULL,                 NULL }, /* BIND         */
        { NULL,                 NULL }, /* CONNECT      */
        { NULL,                 NULL }, /* COPY         */
        { NULL,                 NULL }, /* DELETE       */
        { &meth_get_domainkey,  NULL }, /* GET          */
        { &meth_get_domainkey,  NULL }, /* HEAD         */
        { NULL,                 NULL }, /* LOCK         */
        { NULL,                 NULL }, /* MKCALENDAR   */
        { NULL,                 NULL }, /* MKCOL        */
        { NULL,                 NULL }, /* MOVE         */
        { &meth_options,        NULL }, /* OPTIONS      */
        { NULL,                 NULL }, /* POST         */
        { NULL,                 NULL }, /* PROPFIND     */
        { NULL,                 NULL }, /* PROPPATCH    */
        { NULL,                 NULL }, /* PUT          */
        { NULL,                 NULL }, /* REPORT       */
        { &meth_trace,          NULL }, /* TRACE        */
        { NULL,                 NULL }, /* UNBIND       */
        { NULL,                 NULL }  /* UNLOCK       */
    }
};


void isched_capa_hdr(struct transaction_t *txn, time_t *lastmod, struct stat *sb)
{
    struct stat sbuf;
    time_t mtime;

    stat(config_filename, &sbuf);
    mtime = MAX(compile_time, sbuf.st_mtime);
    txn->resp_body.iserial = mtime +
        (rscale_calendars ? rscale_calendars->num_elements : 0);
    if (lastmod) *lastmod = mtime;
    if (sb) *sb = sbuf;
}


/* iSchedule Receiver Capabilities */
static int meth_get_isched(struct transaction_t *txn,
                           void *params __attribute__((unused)))
{
    int precond;
    struct strlist *action;
    static struct message_guid prev_guid;
    struct message_guid guid;
    const char *etag;
    static time_t lastmod = 0;
    struct stat sbuf;
    static xmlChar *buf = NULL;
    static int bufsiz = 0;

    /* Initialize */
    if (!lastmod) message_guid_set_null(&prev_guid);

    /* Fill in iSchedule-Capabilities */
    isched_capa_hdr(txn, &lastmod, &sbuf);

    /* We don't handle GET on a anything other than ?action=capabilities */
    action = hash_lookup("action", &txn->req_qparams);
    if (!action || action->next || strcmp(action->s, "capabilities")) {
        txn->error.desc = "Invalid action";
        return HTTP_BAD_REQUEST;
    }

    /* Generate ETag based on compile date/time of this source file,
       the number of available RSCALEs and the config file size/mtime */
    assert(!buf_len(&txn->buf));
    buf_printf(&txn->buf, TIME_T_FMT "-" SIZE_T_FMT "-" TIME_T_FMT "-" OFF_T_FMT, compile_time,
               rscale_calendars ? (size_t)rscale_calendars->num_elements : 0,
               sbuf.st_mtime, sbuf.st_size);

    message_guid_generate(&guid, buf_cstring(&txn->buf), buf_len(&txn->buf));
    etag = message_guid_encode(&guid);

    /* Check any preconditions, including range request */
    txn->flags.ranges = 1;
    precond = check_precond(txn, etag, lastmod);

    switch (precond) {
    case HTTP_OK:
    case HTTP_PARTIAL:
    case HTTP_NOT_MODIFIED:
        /* Fill in Etag,  Last-Modified, and Expires */
        txn->resp_body.etag = etag;
        txn->resp_body.lastmod = lastmod;
        txn->resp_body.maxage = 86400;  /* 24 hrs */
        txn->flags.cc |= CC_MAXAGE;

        if (precond != HTTP_NOT_MODIFIED) break;

        GCC_FALLTHROUGH

    default:
        /* We failed a precondition - don't perform the request */
        return precond;
    }

    if (!message_guid_equal(&prev_guid, &guid)) {
        xmlNodePtr root, capa, node, comp, meth;
        xmlNsPtr ns[NUM_NAMESPACE];
        struct mime_type_t *mime;
        struct icaltimetype date;
        icaltimezone *utc = icaltimezone_get_utc_timezone();
        int i, n, maxlen;

        /* Start construction of our query-result */
        if (!(root = init_xml_response("query-result", NS_ISCHED, NULL, ns))) {
            txn->error.desc = "Unable to create XML response";
            return HTTP_SERVER_ERROR;
        }

        capa = xmlNewChild(root, NULL, BAD_CAST "capabilities", NULL);

        buf_reset(&txn->buf);
        buf_printf(&txn->buf, TIME_T_FMT, txn->resp_body.iserial);
        xmlNewChild(capa, NULL, BAD_CAST "serial-number",
                           BAD_CAST buf_cstring(&txn->buf));

        node = xmlNewChild(capa, NULL, BAD_CAST "versions", NULL);
        xmlNewChild(node, NULL, BAD_CAST "version", BAD_CAST "1.0");

        node = xmlNewChild(capa, NULL,
                           BAD_CAST "scheduling-messages", NULL);
        comp = xmlNewChild(node, NULL, BAD_CAST "component", NULL);
        xmlNewProp(comp, BAD_CAST "name", BAD_CAST "VEVENT");
        meth = xmlNewChild(comp, NULL, BAD_CAST "method", NULL);
        xmlNewProp(meth, BAD_CAST "name", BAD_CAST "REQUEST");
        meth = xmlNewChild(comp, NULL, BAD_CAST "method", NULL);
        xmlNewProp(meth, BAD_CAST "name", BAD_CAST "REPLY");
        meth = xmlNewChild(comp, NULL, BAD_CAST "method", NULL);
        xmlNewProp(meth, BAD_CAST "name", BAD_CAST "CANCEL");

        comp = xmlNewChild(node, NULL, BAD_CAST "component", NULL);
        xmlNewProp(comp, BAD_CAST "name", BAD_CAST "VTODO");
        meth = xmlNewChild(comp, NULL, BAD_CAST "method", NULL);
        xmlNewProp(meth, BAD_CAST "name", BAD_CAST "REQUEST");
        meth = xmlNewChild(comp, NULL, BAD_CAST "method", NULL);
        xmlNewProp(meth, BAD_CAST "name", BAD_CAST "REPLY");
        meth = xmlNewChild(comp, NULL, BAD_CAST "method", NULL);
        xmlNewProp(meth, BAD_CAST "name", BAD_CAST "CANCEL");

        comp = xmlNewChild(node, NULL, BAD_CAST "component", NULL);
        xmlNewProp(comp, BAD_CAST "name", BAD_CAST "VPOLL");
        meth = xmlNewChild(comp, NULL, BAD_CAST "method", NULL);
        xmlNewProp(meth, BAD_CAST "name", BAD_CAST "POLLSTATUS");
        meth = xmlNewChild(comp, NULL, BAD_CAST "method", NULL);
        xmlNewProp(meth, BAD_CAST "name", BAD_CAST "REQUEST");
        meth = xmlNewChild(comp, NULL, BAD_CAST "method", NULL);
        xmlNewProp(meth, BAD_CAST "name", BAD_CAST "REPLY");
        meth = xmlNewChild(comp, NULL, BAD_CAST "method", NULL);
        xmlNewProp(meth, BAD_CAST "name", BAD_CAST "CANCEL");

        comp = xmlNewChild(node, NULL, BAD_CAST "component", NULL);
        xmlNewProp(comp, BAD_CAST "name", BAD_CAST "VFREEBUSY");
        meth = xmlNewChild(comp, NULL, BAD_CAST "method", NULL);
        xmlNewProp(meth, BAD_CAST "name", BAD_CAST "REQUEST");

        node = xmlNewChild(capa, NULL,
                           BAD_CAST "calendar-data-types", NULL);
        for (mime = isched_mime_types; mime->content_type; mime++) {
            xmlNodePtr type = xmlNewChild(node, NULL,
                                          BAD_CAST "calendar-data-type", NULL);

            /* Trim any charset from content-type */
            buf_reset(&txn->buf);
            buf_printf(&txn->buf, "%.*s",
                       (int) strcspn(mime->content_type, ";"),
                       mime->content_type);

            xmlNewProp(type, BAD_CAST "content-type",
                       BAD_CAST buf_cstring(&txn->buf));

            if (mime->version)
                xmlNewProp(type, BAD_CAST "version", BAD_CAST mime->version);
        }

        node = xmlNewChild(capa, NULL, BAD_CAST "attachments", NULL);
        xmlNewChild(node, NULL, BAD_CAST "inline", NULL);

        node = xmlNewChild(capa, NULL, BAD_CAST "rscales", NULL);
        if (rscale_calendars) {
            for (i = 0, n = rscale_calendars->num_elements; i < n; i++) {
                const char **rscale = icalarray_element_at(rscale_calendars, i);

                xmlNewChild(node, NULL, BAD_CAST "rscale", BAD_CAST *rscale);
            }
        }

        maxlen = config_getint(IMAPOPT_MAXMESSAGESIZE);
        if (!maxlen) maxlen = INT_MAX;
        buf_reset(&txn->buf);
        buf_printf(&txn->buf, "%d", maxlen);
        xmlNewChild(capa, NULL, BAD_CAST "max-content-length",
                    BAD_CAST buf_cstring(&txn->buf));

        date = icaltime_from_timet_with_zone(caldav_epoch, 0, utc);
        xmlNewChild(capa, NULL, BAD_CAST "min-date-time",
                    BAD_CAST icaltime_as_ical_string(date));

        date = icaltime_from_timet_with_zone(caldav_eternity, 0, utc);
        xmlNewChild(capa, NULL, BAD_CAST "max-date-time",
                    BAD_CAST icaltime_as_ical_string(date));

        /* XXX  need to fill these with values */
        xmlNewChild(capa, NULL, BAD_CAST "max-recipients", NULL);
        xmlNewChild(capa, NULL, BAD_CAST "administrator", NULL);

        /* Dump XML response tree into a text buffer */
        xmlDocDumpFormatMemoryEnc(root->doc, &buf, &bufsiz, "utf-8", 1);
        xmlFreeDoc(root->doc);

        if (!buf) {
            txn->error.desc = "Error dumping XML tree";
            return HTTP_SERVER_ERROR;
        }

        message_guid_copy(&prev_guid, &guid);
    }

    /* Output the XML response */
    txn->resp_body.type = "application/xml; charset=utf-8";
    write_body(precond, txn, (char *) buf, bufsiz);

    xmlFree(buf);

    return 0;
}


static int meth_options_isched(struct transaction_t *txn, void *params)
{
    /* Fill in iSchedule-Capabilities */
    isched_capa_hdr(txn, NULL, NULL);

    return meth_options(txn, params);
}


/* iSchedule Receiver */
static int meth_post_isched(struct transaction_t *txn,
                            void *params __attribute__((unused)))
{
    int ret = 0, r, authd = 0;
    const char **hdr, **recipients;
    struct mime_type_t *mime = NULL;
    icalcomponent *ical = NULL, *comp;
    icalcomponent_kind kind = 0;
    icalproperty_method meth = 0;
    icalproperty *prop = NULL;
    const char *uid = NULL;

    /* Fill in iSchedule-Capabilities */
    isched_capa_hdr(txn, NULL, NULL);

    /* Response should not be cached */
    txn->flags.cc |= CC_NOCACHE;

    /* Check iSchedule-Version */
    if (!(hdr = spool_getheader(txn->req_hdrs, "iSchedule-Version")) ||
        strcmp(hdr[0], "1.0")) {
        txn->error.precond = ISCHED_UNSUPP_VERSION;
        return HTTP_BAD_REQUEST;
    }

    /* Check Content-Type */
    if ((hdr = spool_getheader(txn->req_hdrs, "Content-Type"))) {
        for (mime = isched_mime_types; mime->content_type; mime++) {
            if (is_mediatype(mime->content_type, hdr[0])) break;
        }
    }
    if (!mime || !mime->content_type) {
        txn->error.precond = ISCHED_UNSUPP_DATA;
        return HTTP_BAD_REQUEST;
    }

    /* Check Originator */
    if (!(hdr = spool_getheader(txn->req_hdrs, "Originator"))) {
        txn->error.precond = ISCHED_ORIG_MISSING;
        return HTTP_BAD_REQUEST;
    }
    else if (hdr[1]) {
        /* Multiple Originators */
        txn->error.precond = ISCHED_MULTIPLE_ORIG;
        return HTTP_BAD_REQUEST;
    }

    /* Check Recipients */
    if (!(recipients = spool_getheader(txn->req_hdrs, "Recipient"))) {
        txn->error.precond = ISCHED_RECIP_MISSING;
        return HTTP_BAD_REQUEST;
    }

    /* Read body */
    txn->req_body.flags |= BODY_DECODE;
    r = http_read_req_body(txn);
    if (r) {
        txn->flags.conn = CONN_CLOSE;
        return r;
    }

    /* Make sure we have a body */
    if (!buf_len(&txn->req_body.payload)) {
        txn->error.desc = "Missing request body";
        return HTTP_BAD_REQUEST;
    }

    /* Check authorization */
    if (httpd_userid &&
        config_mupdate_server && config_getstring(IMAPOPT_PROXYSERVERS)) {
        /* Allow frontends to HTTP auth to backends and use iSchedule */
        authd = 1;
    }
    else if (!spool_getheader(txn->req_hdrs, "DKIM-Signature")) {
        if (!config_getswitch(IMAPOPT_ISCHEDULE_DKIM_REQUIRED)) authd = 1;
        else txn->error.desc = "No signature";
    }
    else {
        authd = dkim_auth(txn);
    }

    if (!authd) {
        ret = HTTP_FORBIDDEN;
        txn->error.precond = ISCHED_VERIFICATION_FAILED;
        goto done;
    }

    /* Parse the iCal data for important properties */
    ical = mime->to_object(&txn->req_body.payload);
    if (!ical || (icalcomponent_isa(ical) != ICAL_VCALENDAR_COMPONENT)) {
        txn->error.precond = ISCHED_INVALID_DATA;
        return HTTP_BAD_REQUEST;
    }

    icalrestriction_check(ical);
    if ((txn->error.desc = get_icalcomponent_errstr(ical))) {
        assert(!buf_len(&txn->buf));
        buf_setcstr(&txn->buf, txn->error.desc);
        txn->error.desc = buf_cstring(&txn->buf);
        txn->error.precond = ISCHED_INVALID_DATA;
        return HTTP_BAD_REQUEST;
    }

    meth = icalcomponent_get_method(ical);
    comp = icalcomponent_get_first_real_component(ical);
    if (comp) {
        uid = icalcomponent_get_uid(comp);
        kind = icalcomponent_isa(comp);
        prop = icalcomponent_get_first_property(comp, ICAL_ORGANIZER_PROPERTY);
    }

    /* Check method preconditions */
    if (!meth || !comp || !uid || !prop) {
        txn->error.precond = ISCHED_INVALID_SCHED;
        ret = HTTP_BAD_REQUEST;
        goto done;
    }

    switch (kind) {
    case ICAL_VFREEBUSY_COMPONENT:
        if (meth == ICAL_METHOD_REQUEST)
            ret = sched_busytime_query(txn, mime, ical);
        else goto invalid_meth;
        break;

    case ICAL_VEVENT_COMPONENT:
    case ICAL_VTODO_COMPONENT:
    case ICAL_VPOLL_COMPONENT:
        switch (meth) {
        case ICAL_METHOD_POLLSTATUS:
            if (kind != ICAL_VPOLL_COMPONENT) goto invalid_meth;

            GCC_FALLTHROUGH

        case ICAL_METHOD_REQUEST:
        case ICAL_METHOD_REPLY:
        case ICAL_METHOD_CANCEL: {
            unsigned flags = SCHEDFLAG_ISCHEDULE;
            if (meth == ICAL_METHOD_REPLY) flags |= SCHEDFLAG_IS_REPLY;
            struct sched_data sched_data =
                { flags, ical, NULL, NULL,
                  ICAL_SCHEDULEFORCESEND_NONE, NULL, NULL, NULL };
            xmlNodePtr root = NULL;
            xmlNsPtr ns[NUM_NAMESPACE];
            struct auth_state *authstate;
            int i;

            /* Start construction of our schedule-response */
            if (!(root = init_xml_response("schedule-response",
                                           NS_ISCHED, NULL, ns))) {
                ret = HTTP_SERVER_ERROR;
                txn->error.desc = "Unable to create XML response";
                goto done;
            }

            authstate = auth_newstate("anonymous");

            /* Process each recipient */
            for (i = 0; recipients[i]; i++) {
                tok_t tok =
                    TOK_INITIALIZER(recipients[i], ",", TOK_TRIMLEFT|TOK_TRIMRIGHT);
                char *recipient;

                while ((recipient = tok_next(&tok))) {
                    /* Is recipient remote or local? */
                    struct caldav_sched_param sparam;
                    int r = caladdress_lookup(recipient, &sparam, /*schedule_addresses*/NULL);

                    /* Don't allow scheduling with remote users via iSchedule */
                    if (sparam.flags & SCHEDTYPE_REMOTE) r = HTTP_FORBIDDEN;
                    sched_param_fini(&sparam);

                    if (r) sched_data.status = REQSTAT_NOUSER;
                    else sched_deliver(httpd_userid, httpd_userid, recipient, &sched_data, authstate);

                    xml_add_schedresponse(root, NULL, BAD_CAST recipient,
                                          BAD_CAST sched_data.status);
                }
                tok_fini(&tok);
            }

            xml_response(HTTP_OK, txn, root->doc);
            xmlFreeDoc(root->doc);

            auth_freestate(authstate);
        }
            break;

        default:
        invalid_meth:
            txn->error.desc = "Unsupported iTIP method";
            txn->error.precond = ISCHED_INVALID_SCHED;
            ret = HTTP_BAD_REQUEST;
            goto done;
        }
        break;

    default:
        txn->error.desc = "Unsupported iCalendar component";
        txn->error.precond = ISCHED_INVALID_SCHED;
        ret = HTTP_BAD_REQUEST;
    }

  done:
    if (ical) icalcomponent_free(ical);

    return ret;
}


int isched_send(struct caldav_sched_param *sparam, const char *recipient,
                icalcomponent *ical, xmlNodePtr *xml)
{
    int r = 0;
    struct backend *be;
    static unsigned send_count = 0;
    static struct buf hdrs = BUF_INITIALIZER;
    const char *body, *uri, *originator;
    size_t bodylen;
    icalproperty_method method;
    icalcomponent *comp;
    icalcomponent_kind kind;
    icalproperty *prop;
    unsigned code;
    struct transaction_t txn;
    struct http_connection conn;

    *xml = NULL;
    memset(&txn, 0, sizeof(struct transaction_t));

    if (sparam->flags & SCHEDTYPE_REMOTE) uri = ISCHED_WELLKNOWN_URI;
    else uri = namespace_ischedule.prefix;

    /* Open connection to iSchedule receiver.
       Use header buffer to construct remote server[:port][/tls][/noauth] */
    buf_setcstr(&txn.buf, sparam->server);
    if (sparam->port) buf_printf(&txn.buf, ":%u", sparam->port);
    if (sparam->flags & SCHEDTYPE_SSL) buf_appendcstr(&txn.buf, "/tls");
    if (sparam->flags & SCHEDTYPE_REMOTE) {
        /* Using DKIM rather than HTTP Auth */
        buf_appendcstr(&txn.buf, "/noauth");
    }
    be = proxy_findserver(buf_cstring(&txn.buf), &http_protocol, httpd_userid,
                          &backend_cached, NULL, NULL, httpd_in);
    if (!be) return HTTP_UNAVAILABLE;

    /* Setup HTTP connection for reading response */
    memset(&conn, 0, sizeof(struct http_connection));
    conn.pin = be->in;
    txn.conn = &conn;

    /* Create iSchedule request body */
    body = icalcomponent_as_ical_string(ical);
    bodylen = strlen(body);

    /* Create iSchedule request header.
     * XXX  Make sure that we don't use multiple headers of the same name
     *      or add WSP around commas in signed headers
     *      to obey ischedule-relaxed canonicalization.
     */
    buf_reset(&hdrs);
    buf_printf(&hdrs, "Host: %s", sparam->server);
    if (sparam->port) buf_printf(&hdrs, ":%u", sparam->port);
    buf_printf(&hdrs, "\r\n");
    buf_printf(&hdrs, "Cache-Control: no-cache, no-transform\r\n");
    if (config_serverinfo == IMAP_ENUM_SERVERINFO_ON) {
        buf_printf(&hdrs, "User-Agent: %s\r\n", buf_cstring(&serverinfo));
    }
    buf_printf(&hdrs, "iSchedule-Version: 1.0\r\n");
    buf_printf(&hdrs, "iSchedule-Message-ID: <cmu-ischedule-%u-" TIME_T_FMT "-%u@%s>\r\n",
               getpid(), time(NULL), send_count++, config_servername);
    buf_printf(&hdrs, "Content-Type: text/calendar; charset=utf-8");

    method = icalcomponent_get_method(ical);
    comp = icalcomponent_get_first_real_component(ical);
    kind = icalcomponent_isa(comp);
    buf_printf(&hdrs, "; method=%s; component=%s\r\n",
               icalproperty_method_to_string(method),
               icalcomponent_kind_to_string(kind));

    buf_printf(&hdrs, "Content-Length: %u\r\n", (unsigned) bodylen);

    /* Determine Originator based on method and component */
    if (method == ICAL_METHOD_REPLY) {
        prop = icalcomponent_get_first_invitee(comp);
        originator = icalproperty_get_invitee(prop);
    }
    else {
        prop = icalcomponent_get_first_property(comp, ICAL_ORGANIZER_PROPERTY);
        originator = icalproperty_get_organizer(prop);
    }
    buf_printf(&hdrs, "Originator: %s\r\n", originator);

    if (recipient) {
        /* Single recipient */
        buf_printf(&hdrs, "Recipient: %s\r\n", recipient);
    }
    else {
        /* VFREEBUSY REQUEST - use ATTENDEES as Recipients */
        char sep = ' ';

        buf_printf(&hdrs, "Recipient:");
        for (prop = icalcomponent_get_first_property(comp,
                                                     ICAL_ATTENDEE_PROPERTY);
             prop;
             prop = icalcomponent_get_next_property(comp,
                                                    ICAL_ATTENDEE_PROPERTY)) {
            buf_printf(&hdrs, "%c%s", sep, icalproperty_get_attendee(prop));
            sep = ',';
        }
        buf_printf(&hdrs, "\r\n");
    }

    /* Don't send body until told to */
    buf_appendcstr(&hdrs, "Expect: 100-continue\r\n");

    if (sparam->flags & SCHEDTYPE_REMOTE) {
#ifdef WITH_DKIM
        DKIM *dkim = NULL;
        DKIM_STAT stat;
        unsigned char *sig = NULL;
        size_t siglen;
        const char *selector = config_getstring(IMAPOPT_ISCHEDULE_DKIM_SELECTOR);
        const char *domain = config_getstring(IMAPOPT_ISCHEDULE_DKIM_DOMAIN);

        /* Create iSchedule/DKIM signature */
        if (dkim_lib &&
            (dkim = dkim_sign(dkim_lib, NULL /* id */, NULL,
                              (dkim_sigkey_t) buf_cstring(&privkey),
                              (const u_char *) selector,
                              (const u_char *) domain,
                              /* Requires modified version of OpenDKIM
                                 until we get OpenDOSETA */
                              DKIM_CANON_ISCHEDULE, DKIM_CANON_SIMPLE,
                              DKIM_SIGN_RSASHA256, -1 /* entire body */,
                              &stat))) {

            /* Suppress folding of DKIM header */
//          stat = dkim_set_margin(dkim, 0);

            /* Add our query method list */
            stat = dkim_add_querymethod(dkim, "private-exchange", NULL);
            stat = dkim_add_querymethod(dkim, "http", "well-known");
//          stat = dkim_add_querymethod(dkim, "dns", "txt");

            /* Process the headers and body */
            stat = dkim_chunk(dkim,
                              (u_char *) buf_cstring(&hdrs), buf_len(&hdrs));
            stat = dkim_chunk(dkim, (u_char *) "\r\n", 2);
            stat = dkim_chunk(dkim, (u_char *) body, bodylen);
            stat = dkim_chunk(dkim, NULL, 0);
            stat = dkim_eom(dkim, NULL);

            /* Generate the signature */
            stat = dkim_getsighdr_d(dkim, strlen(DKIM_SIGNHEADER) + 2,
                                    &sig, &siglen);
            /* Append a DKIM-Signature header */
            buf_printf(&hdrs, "%s: %s\r\n", DKIM_SIGNHEADER, sig);

            dkim_free(dkim);
        }
#else
        syslog(LOG_WARNING, "DKIM-Signature required, but DKIM isn't supported");
#endif /* WITH_DKIM */
    }

  redirect:
    /* Send request line */
    prot_printf(be->out, "POST %s %s\r\n", uri, HTTP_VERSION);

    /* Send request headers */
    prot_putbuf(be->out, &hdrs);
    prot_puts(be->out, "\r\n");

  response:
    /* Read response (req_hdr and req_body are actually the response) */
    txn.req_body.flags = BODY_DECODE;
    r = http_read_response(be, METH_POST, &code,
                           &txn.req_hdrs, &txn.req_body, &txn.error.desc);
    syslog(LOG_INFO, "isched_send(%s, %s) => %u",
           recipient, buf_cstring(&txn.buf), code);

    if (!r) {
        switch (code) {
        case 100:  /* Continue */
            /* Send request body (only ONCE for a given request) */
            prot_write(be->out, body, bodylen);
            bodylen = 0;

            GCC_FALLTHROUGH
            
        case 102:
        case 103:  /* Provisional */
            goto response;

        case 200:  /* Successful */
            /* Create XML parser context */
            if (!(conn.xml = xmlNewParserCtxt())) {
                fatal("Unable to create XML parser", EX_TEMPFAIL);
            }

            r = parse_xml_body(&txn, xml, NULL);
            xmlFreeParserCtxt(conn.xml);
            break;

        case 301:
        case 302:
        case 307:
        case 308:  /* Redirection */
            uri = spool_getheader(txn.req_hdrs, "Location")[0];
            if (txn.req_body.flags & BODY_CLOSE) {
                proxy_downserver(be);
                be = proxy_findserver(buf_cstring(&txn.buf), &http_protocol,
                                      NULL, &backend_cached,
                                      NULL, NULL, httpd_in);
                if (!be) {
                    r = HTTP_UNAVAILABLE;
                    break;
                }
            }

            if (!bodylen) bodylen = strlen(body);  /* Need to send body again */
            goto redirect;

        default:
            r = HTTP_UNAVAILABLE;
        }
    }

    if (txn.req_hdrs) spool_free_hdrcache(txn.req_hdrs);
    buf_free(&txn.req_body.payload);
    buf_free(&txn.buf);

    return r;
}


#ifdef WITH_DKIM
static DKIM_CBSTAT isched_get_key(DKIM *dkim, DKIM_SIGINFO *sig,
                                  u_char *buf, size_t buflen)
{
    DKIM_CBSTAT stat = DKIM_CBSTAT_NOTFOUND;
    const char *domain, *selector, *query;
    tok_t tok;
    char *type, *opts;

    assert(dkim != NULL);
    assert(sig != NULL);

    domain = (const char *) dkim_sig_getdomain(sig);
    selector = (const char *) dkim_sig_getselector(sig);
    if (!domain || !selector) return DKIM_CBSTAT_ERROR;

    query = (const char *) dkim_sig_gettagvalue(sig, 0, (u_char *) "q");
    if (!query) query = "dns/txt";  /* implicit default */

    /* Parse the q= tag */
    tok_init(&tok, query, ":", 0);
    while ((type = tok_next(&tok))) {
        /* Split type/options */
        if ((opts = strchr(type, '/'))) *opts++ = '\0';

#ifdef IOPTEST  /* CalConnect ioptest */
        if (1) {
#else
        if (!strcmp(type, "private-exchange")) {
#endif
            const char *prefix = config_getstring(IMAPOPT_HTTPDOCROOT);
            struct buf path = BUF_INITIALIZER;
            FILE *f;

            if (!prefix) continue;

            buf_setcstr(&path, prefix);
            buf_printf(&path, "%s/%s/%s",
                       namespace_domainkey.prefix, domain, selector);
            syslog(LOG_DEBUG, "using public key from path: %s",
                   buf_cstring(&path));

            if (!(f = fopen(buf_cstring(&path), "r"))) {
                syslog(LOG_NOTICE, "%s: fopen(): %s",
                       buf_cstring(&path), strerror(errno));
            }
            buf_free(&path);
            if (!f) continue;

            memset(buf, '\0', buflen);
            fgets((char *) buf, buflen, f);
            fclose(f);

            if (buf[0] != '\0') {
                stat = DKIM_CBSTAT_CONTINUE;
                break;
            }
        }
        else if (!strcmp(type, "http") && !strcmp(opts, "well-known")) {
        }
        else if (!strcmp(type, "dns") && !strcmp(opts, "txt")) {
            stat = DKIM_CBSTAT_DEFAULT;
            break;
        }
    }

    tok_fini(&tok);

    return stat;
}


static void dkim_cachehdr(const char *name, const char *contents,
                          const char *raw __attribute__((unused)), void *rock)
{
    struct buf *hdrfield = &tmpbuf;
    static const char *lastname = NULL;
    int dup_hdr = name && lastname && !strcmp(name, lastname);

    /* Ignore private headers in our cache */
    if (name && name[0] == ':') return;

    /* Combine header fields of the same name.
     * Our hash table will always feed us duplicate headers consecutively.
     */
    if (lastname && !dup_hdr) {
        dkim_header((DKIM *) rock,
                    (u_char *) buf_cstring(hdrfield), buf_len(hdrfield));
    }

    lastname = name;

    if (name) {
        tok_t tok;
        char *token, sep = ':';

        if (!dup_hdr) buf_setcstr(hdrfield, name);
        else sep = ',';

        /* Trim leading/trailing WSP around comma-separated values */
        tok_init(&tok, contents, ",", TOK_TRIMLEFT|TOK_TRIMRIGHT|TOK_EMPTY);
        while ((token = tok_next(&tok))) {
            buf_printf(hdrfield, "%c%s", sep, token);
            sep = ',';
        }
        tok_fini(&tok);
    }
}

static int dkim_auth(struct transaction_t *txn)
{
    int authd = 0;
    DKIM *dkim = NULL;
    DKIM_STAT stat;

    if (!dkim_lib) return 0;

    dkim = dkim_verify(dkim_lib, NULL /* id */, NULL, &stat);
    if (!dkim) return 0;

#ifdef TEST
    {
        /* XXX  Hack for local testing */
        dkim_query_t qtype = DKIM_QUERY_FILE;
        struct buf keyfile = BUF_INITIALIZER;

        stat = dkim_options(dkim_lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYMETHOD,
                            &qtype, sizeof(qtype));

        buf_printf(&keyfile, "%s/dkim.public", config_dir);
        stat = dkim_options(dkim_lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYINFO,
                            (void *) buf_cstring(&keyfile),
                            buf_len(&keyfile));
    }
#endif

    /* Process the cached headers and body */
    spool_enum_hdrcache(txn->req_hdrs, &dkim_cachehdr, dkim);
    dkim_cachehdr(NULL, NULL, dkim);  /* Force canon of last header */
    stat = dkim_eoh(dkim);
    if (stat == DKIM_STAT_OK) {
        stat = dkim_body(dkim, (u_char *) buf_cstring(&txn->req_body.payload),
                         buf_len(&txn->req_body.payload));
        stat = dkim_eom(dkim, NULL);
    }

    if (stat == DKIM_STAT_OK) authd = 1;
    else if (stat == DKIM_STAT_CBREJECT) {
        txn->error.desc =
            "Unable to verify: HTTP request-line mismatch";
    }
    else {
        DKIM_SIGINFO *sig = dkim_getsignature(dkim);

        if (sig) {
            const char *sigerr, *sslerr;

            if (dkim_sig_getbh(sig) == DKIM_SIGBH_MISMATCH)
                sigerr = "body hash mismatch";
            else {
                DKIM_SIGERROR err = dkim_sig_geterror(sig);

                sigerr = dkim_sig_geterrorstr(err);
            }

            assert(!buf_len(&txn->buf));
            buf_printf(&txn->buf, "%s: %s",
                       dkim_getresultstr(stat), sigerr);
            if ((sslerr = dkim_sig_getsslbuf(sig)))
                buf_printf(&txn->buf, ": %s", sslerr);
            txn->error.desc = buf_cstring(&txn->buf);
        }
        else txn->error.desc = dkim_getresultstr(stat);
    }

    dkim_free(dkim);

    return authd;
}
#else
static int dkim_auth(struct transaction_t *txn __attribute__((unused)))
{
    if (!config_getswitch(IMAPOPT_ISCHEDULE_DKIM_REQUIRED)) return 1;

    syslog(LOG_WARNING, "DKIM-Signature provided, but DKIM isn't supported");

    return 0;
}
#endif /* WITH_DKIM */


/* Perform a GET/HEAD request for a domainkey */
static int meth_get_domainkey(struct transaction_t *txn,
                              void *params __attribute__((unused)))
{
    int ret = 0, r, fd = -1, precond;
    const char *path;
    static struct buf pathbuf = BUF_INITIALIZER;
    struct stat sbuf;
    const char *msg_base = NULL;
    size_t msg_size = 0;
    struct resp_body_t *resp_body = &txn->resp_body;

    /* See if file exists and get Content-Length & Last-Modified time */
    buf_setcstr(&pathbuf, config_dir);
    buf_appendcstr(&pathbuf, txn->req_uri->path);
    path = buf_cstring(&pathbuf);
    r = stat(path, &sbuf);
    if (r || !S_ISREG(sbuf.st_mode)) return HTTP_NOT_FOUND;

    /* Generate Etag */
    assert(!buf_len(&txn->buf));
    buf_printf(&txn->buf, "%ld-%ld", (long) sbuf.st_mtime, (long) sbuf.st_size);

    /* Check any preconditions, including range request */
    txn->flags.ranges = 1;
    precond = check_precond(txn, buf_cstring(&txn->buf), sbuf.st_mtime);

    switch (precond) {
    case HTTP_OK:
    case HTTP_PARTIAL:
    case HTTP_NOT_MODIFIED:
        /* Fill in Content-Type, ETag, Last-Modified, and Expires */
        resp_body->type = "text/plain";
        resp_body->etag = buf_cstring(&txn->buf);
        resp_body->lastmod = sbuf.st_mtime;
        resp_body->maxage = 86400;  /* 24 hrs */
        txn->flags.cc |= CC_MAXAGE | CC_REVALIDATE;
        if (!httpd_userisanonymous) txn->flags.cc |= CC_PUBLIC;

        if (precond != HTTP_NOT_MODIFIED) break;

        GCC_FALLTHROUGH

    default:
        /* We failed a precondition - don't perform the request */
        resp_body->type = NULL;
        return precond;
    }

    if (txn->meth == METH_GET) {
        /* Open and mmap the file */
        if ((fd = open(path, O_RDONLY)) == -1) return HTTP_SERVER_ERROR;
        map_refresh(fd, 1, &msg_base, &msg_size, sbuf.st_size, path, NULL);
    }

    write_body(precond, txn, msg_base, sbuf.st_size);

    if (fd != -1) {
        map_free(&msg_base, &msg_size);
        close(fd);
    }

    return ret;
}


static void isched_init(struct buf *serverinfo)
{
    if (!(config_httpmodules & IMAP_ENUM_HTTPMODULES_CALDAV) ||
        !config_getenum(IMAPOPT_CALDAV_ALLOWSCHEDULING)) {
        /* Need CALDAV and CALDAV_SCHED in order to have ISCHEDULE */
        return;
    }

    compile_time = calc_compile_time(__TIME__, __DATE__);

    if (config_mupdate_server && config_getstring(IMAPOPT_PROXYSERVERS)) {
        /* If backend server, we require iSchedule (w/o DKIM) */
        namespace_ischedule.enabled = -1;
        buf_cstring(serverinfo);  // squash compiler warning when #undef WITH_DKIM
    }
    else {
        namespace_ischedule.enabled =
            config_httpmodules & IMAP_ENUM_HTTPMODULES_ISCHEDULE;
    }

#ifdef WITH_DKIM
    /* Add OpenDKIM version to serverinfo string */
    uint32_t ver = dkim_libversion();
    buf_printf(serverinfo, " OpenDKIM/%u.%u.%u",
               (ver >> 24) & 0xff, (ver >> 16) & 0xff, (ver >> 8) & 0xff);
    if (ver & 0xff) buf_printf(serverinfo, ".%u", ver & 0xff);

    if (namespace_ischedule.enabled) {
        int fd;
        unsigned flags = ( DKIM_LIBFLAGS_BADSIGHANDLES | DKIM_LIBFLAGS_CACHE |
//                         DKIM_LIBFLAGS_KEEPFILES | DKIM_LIBFLAGS_TMPFILES |
                           DKIM_LIBFLAGS_VERIFYONE );
        uint64_t ttl = 3600;  /* 1 hour */
        const char *requiredhdrs[] = { "Content-Type", "iSchedule-Version",
                                       "Originator", "Recipient", NULL };
        const char *signhdrs[] = { "iSchedule-Message-ID", "User-Agent", NULL };
        const char *skiphdrs[] = { "Cache-Control", "Connection",
                                   "Content-Length", "Host", "Keep-Alive",
                                   "Proxy-Authenticate", "Proxy-Authorization",
                                   "TE", "Trailer", "Transfer-Encoding",
                                   "Upgrade", "Via", NULL };
        const char *senderhdrs[] = { "Originator", NULL };
        const char *keyfile = config_getstring(IMAPOPT_ISCHEDULE_DKIM_KEY_FILE);
        unsigned need_dkim =
            namespace_ischedule.enabled == IMAP_ENUM_HTTPMODULES_ISCHEDULE;

        /* Initialize DKIM library */
        if (!(dkim_lib = dkim_init(NULL, NULL))) {
            syslog(LOG_ERR, "unable to initialize libopendkim");
            namespace_ischedule.enabled = !need_dkim;
            return;
        }

        /* Install our callback for doing key lookups */
        dkim_set_key_lookup(dkim_lib, isched_get_key);

        /* Setup iSchedule DKIM options */
#ifdef TEST
        flags |= ( DKIM_LIBFLAGS_SIGNLEN | DKIM_LIBFLAGS_ZTAGS );
#endif
        dkim_options(dkim_lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS,
                     &flags, sizeof(flags));
        dkim_options(dkim_lib, DKIM_OP_SETOPT, DKIM_OPTS_SIGNATURETTL,
                     &ttl, sizeof(ttl));
        dkim_options(dkim_lib, DKIM_OP_SETOPT, DKIM_OPTS_REQUIREDHDRS,
                     requiredhdrs, sizeof(const char **));
        dkim_options(dkim_lib, DKIM_OP_SETOPT, DKIM_OPTS_MUSTBESIGNED,
                     requiredhdrs, sizeof(const char **));
        dkim_options(dkim_lib, DKIM_OP_SETOPT, DKIM_OPTS_SIGNHDRS,
                     signhdrs, sizeof(const char **));
        dkim_options(dkim_lib, DKIM_OP_SETOPT, DKIM_OPTS_SKIPHDRS,
                     skiphdrs, sizeof(const char **));
        dkim_options(dkim_lib, DKIM_OP_SETOPT, DKIM_OPTS_SENDERHDRS,
                     senderhdrs, sizeof(const char **));

        /* Fetch DKIM private key for signing */
        if (keyfile && (fd = open(keyfile, O_RDONLY)) != -1) {
            const char *base = NULL;
            size_t len = 0;

            map_refresh(fd, 1, &base, &len, MAP_UNKNOWN_LEN, keyfile, NULL);
            buf_setmap(&privkey, base, len);
            map_free(&base, &len);
            close(fd);
        }
        else {
            syslog(LOG_ERR, "unable to open private key file %s", keyfile);
            namespace_ischedule.enabled = !need_dkim;
        }

        namespace_domainkey.enabled =
            config_httpmodules & IMAP_ENUM_HTTPMODULES_DOMAINKEY;
    }
#endif /* WITH_DKIM */
}


static void isched_shutdown(void)
{
#ifdef WITH_DKIM
    buf_free(&privkey);
    buf_free(&tmpbuf);
    buf_free(&b64req);
    if (dkim_lib) dkim_close(dkim_lib);
#endif
}