summaryrefslogtreecommitdiff
path: root/modules/test/mod_policy.c
blob: 22184793e17dc59b0615f8458f242a28a6170399 (plain)
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
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Originally written @ BBC by Graham Leggett
 * (C) 2011 British Broadcasting Corporation
 */

/*
 * mod_policy.c --- Enforce specific policies on outgoing requests, logging
 *                  or rejecting requests as appropriate.
 *
 * To enable, add the corresponding filters like so:
 *
 * SetOutputFilter POLICY_TYPE,POLICY_LENGTH
 *
 */

#include "util_filter.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"

#include "apr_tables.h"
#include "apr_strings.h"
#include "apr_date.h"

module AP_MODULE_DECLARE_DATA policy_module;

#define POLICY_DEFAULT_TYPE "*/*"

typedef enum policy_result
{
    policy_ignore = 0, /* ignore this policy */
    policy_log, /* log the violation as a warning, but let it through */
    policy_enforce /* log the violation as an error, and decline */
} policy_result;

typedef struct policy_conf
{
    int policy; /* whether the filters should do anything at all */
    int policy_set;
    const char *environment; /* optional name of the subprocess environment variable that
     * controls whether the policies are enforced.
     */
    const char *environment_log; /* value to trigger logging only */
    const char *environment_ignore; /* value to suspend policy enforcement */
    int environment_set;
    policy_result type_action;
    apr_array_header_t *type_matches; /* content type default patterns to match */
    int type_set;
    const char *type_url;
    int type_url_set;
    policy_result length_action;
    int length_set;
    const char *length_url;
    int length_url_set;
    policy_result keepalive_action;
    int keepalive_set;
    const char *keepalive_url;
    int keepalive_url_set;
    policy_result vary_action;
    apr_array_header_t *vary_matches; /* Vary default patterns to match */
    int vary_set;
    const char *vary_url;
    int vary_url_set;
    policy_result validation_action;
    int validation_set;
    const char *validation_url;
    int validation_url_set;
    policy_result conditional_action;
    int conditional_set;
    const char *conditional_url;
    int conditional_url_set;
    policy_result nocache_action;
    int nocache_set;
    const char *nocache_url;
    int nocache_url_set;
    policy_result maxage_action;
    apr_int64_t maxage;
    int maxage_set;
    const char *maxage_url;
    int maxage_url_set;
    policy_result version_action;
    const char *version;
    int version_num;
    int version_set;
    const char *version_url;
    int version_url_set;
} policy_conf;

/**
 * Does the value of a flagpole override the original value?
 */
static int check_enabled(request_rec *r, policy_conf *conf,
        policy_result result)
{
    if (conf && !conf->policy) {
        return policy_ignore;
    }
    if (conf && result != policy_ignore && conf->environment) {
        const char *value = apr_table_get(r->subprocess_env, conf->environment);
        if (value) {
            /* downgrade enforce to log? */
            if (conf->environment_log && !strcmp(value, conf->environment_log)) {
                if (result == policy_enforce) {
                    return policy_log;
                }
            }
            /* downgrade enforce and log to ignore? */
            else if (conf->environment_ignore && !strcmp(value,
                    conf->environment_ignore)) {
                return policy_ignore;
            }
        }
    }
    return result;
}

static void handle_policy(request_rec *r, policy_result result,
        const char *message, const char *url, apr_bucket_brigade *bb,
        int status)
{
    apr_bucket *e;

    switch (result) {
    case policy_log: {
        ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(03041)
                      "mod_policy: violation: %s, uri: %s",
                      message, r->uri);
        apr_table_addn(r->headers_out, "Warning", apr_psprintf(r->pool,
                "299 %s \"%s\"", ap_get_server_name(r), message));
        break;
    }
    case policy_enforce: {

        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(03042)
                      "mod_policy: violation, rejecting request: %s, uri: %s",
                      message, r->uri);
        apr_table_addn(r->err_headers_out, "Warning", apr_psprintf(r->pool,
                "299 %s \"Rejected: %s\"", ap_get_server_name(r), message));
        apr_table_setn(r->notes, "error-notes",
                       url ? apr_pstrcat(r->pool, "<a href=\"", url, "\">",
                                         message, "</a>", NULL)
                           : message);

        apr_brigade_cleanup(bb);
        e = ap_bucket_error_create(status, NULL, r->pool,
                r->connection->bucket_alloc);
        APR_BRIGADE_INSERT_TAIL(bb, e);
        e = apr_bucket_eos_create(r->connection->bucket_alloc);
        APR_BRIGADE_INSERT_TAIL(bb, e);

    }
    case policy_ignore: {
    }
    }

}

/**
 * Policy for Content-Type.
 *
 * - It must be present.
 * - It must match the optional regex (default .* / .*)
 */
static apr_status_t policy_type_out_filter(ap_filter_t *f,
        apr_bucket_brigade *bb)
{

    policy_conf *conf = ap_get_module_config(f->r->per_dir_config,
            &policy_module);
    policy_result result = check_enabled(f->r, conf, conf->type_action);

    if (result != policy_ignore) {
        int fail = 1;

        /* content type present and valid? */
        if (f->r->content_type) {
            const char *type = f->r->content_type;
            const char *end = ap_strchr_c(type, ';');
            if (end) {
                type = apr_pstrmemdup(f->r->pool, type, end - type);
            }
            if (!conf->type_matches) {
                if (!ap_strcmp_match(type, POLICY_DEFAULT_TYPE)) {
                    fail = 0;
                }
            }
            else {
                int i;
                for (i = 0; i < conf->type_matches->nelts; i++) {
                    if (!ap_strcmp_match(type,
                            ((char **) conf->type_matches->elts)[i])) {
                        fail = 0;
                        break;
                    }
                }
            }
        }

        if (fail) {
            const char *types = NULL;
            if (conf->type_matches) {
                int i;
                for (i = 0; i < conf->type_matches->nelts; i++) {
                    types = apr_pstrcat(f->r->pool, types ? ", " : "",
                            ((char **) conf->type_matches->elts)[i], NULL);
                }
            }
            else {
                types = POLICY_DEFAULT_TYPE;
            }

            handle_policy(
                    f->r,
                    result,
                    apr_psprintf(
                            f->r->pool,
                            "Content-Type of '%s' should be RFC compliant and match one of: %s",
                            f->r->content_type, types), conf->type_url, bb, HTTP_BAD_GATEWAY);

        }

    }

    ap_remove_output_filter(f);
    return ap_pass_brigade(f->next, bb);
}

/**
 * Policy for Content-Length.
 *
 * - It must be present (missing, or Transfer-Encoding: chunked would be rejected)
 * - Only applies to 2xx result codes
 */
static apr_status_t policy_length_out_filter(ap_filter_t *f,
        apr_bucket_brigade *bb)
{
    request_rec *r = f->r;

    policy_conf *conf = ap_get_module_config(r->per_dir_config,
            &policy_module);
    policy_result result = check_enabled(r, conf, conf->length_action);

    if (result != policy_ignore && r->status >= 200 && r->status < 300
            && r->status != HTTP_NO_CONTENT) {

        if (!apr_table_get(r->headers_out, "Content-Length")) {

            handle_policy(r, result, "Content-Length should be present",
                    conf->length_url, bb, HTTP_BAD_GATEWAY);

        }

    }

    ap_remove_output_filter(f);
    return ap_pass_brigade(f->next, bb);
}

/**
 * Policy for Content-Length / Chunked Encoding.
 *
 * We follow a subset of the algorithm httpd uses, which is:
 *
 *   IF  we have not marked this connection as errored;
 *   and the client isn't expecting 100-continue (PR47087 - more
 *       input here could be the client continuing when we're
 *       closing the request).
 *   and the response status does not require a close;
 *   and the response body has a defined length due to the status code
 *       being 304 or 204, the request method being HEAD, already
 *       having defined Content-Length or Transfer-Encoding: chunked, or
 *       the request version being HTTP/1.1 and thus capable of being set
 *       as chunked
 *   THEN  we support keepalive.
 *
 * Note: The server may choose to turn off keepalive for various reasons,
 * such as an imminent shutdown, or a Connection: close from the client,
 * but for our purposes we only care that keepalive was possible from
 * the application, not that keepalive actually took place.
 */
static apr_status_t policy_keepalive_out_filter(ap_filter_t *f,
        apr_bucket_brigade *bb)
{
    request_rec *r = f->r;

    policy_conf *conf = ap_get_module_config(r->per_dir_config,
            &policy_module);
    policy_result result = check_enabled(r, conf, conf->keepalive_action);

    if (result != policy_ignore && r->connection->keepalive != AP_CONN_CLOSE
            && !r->expecting_100 && !ap_status_drops_connection(r->status)) {

        if (!((r->status == HTTP_NOT_MODIFIED)
                || (r->status == HTTP_NO_CONTENT)
                || r->header_only
                || apr_table_get(r->headers_out, "Content-Length")
                || ap_find_last_token(r->pool, apr_table_get(r->headers_out,
                        "Transfer-Encoding"), "chunked")
                || r->proto_num >= HTTP_VERSION(1, 1))) {

            handle_policy(r, result, "Keepalive should be possible (supply Content-Length or HTTP/1.1 Transfer-Encoding)",
                    conf->keepalive_url, bb, HTTP_BAD_GATEWAY);

        }

    }

    ap_remove_output_filter(f);
    return ap_pass_brigade(f->next, bb);
}

static int vary_test(void *rec, const char *key, const char *value)
{
    request_rec *r = (request_rec *)rec;
    char *token = apr_pstrdup(r->pool, value);
    char *last;

    policy_conf *conf = ap_get_module_config(r->per_dir_config,
            &policy_module);

    token = apr_strtok(token, ", \t", &last);
    while (token) {
        int i;
        for (i = 0; i < conf->vary_matches->nelts; i++) {
            if (!ap_strcasecmp_match(token,
                    ((char **) conf->vary_matches->elts)[i])) {
                return 0;
            }
        }

        token = apr_strtok(NULL, ", \t", &last);
    }
    return 1;
}

/**
 * Policy for Vary.
 *
 * - If an element matches the optional regex (no default), the request is rejected.
 * Typically used to reject Varying on User-Agent.
 */
static apr_status_t policy_vary_out_filter(ap_filter_t *f,
        apr_bucket_brigade *bb)
{

    policy_conf *conf = ap_get_module_config(f->r->per_dir_config,
            &policy_module);
    policy_result result = check_enabled(f->r, conf, conf->vary_action);

    if (result != policy_ignore) {

        /* Vary present and valid? */
        if (!apr_table_do(vary_test, f->r, f->r->headers_out, "Vary", NULL)) {
            const char *varys = NULL;
            if (conf->vary_matches) {
                int i;
                for (i = 0; i < conf->vary_matches->nelts; i++) {
                    varys = apr_pstrcat(f->r->pool, varys ? ", " : "",
                            ((char **) conf->vary_matches->elts)[i], NULL);
                }
            }

            handle_policy(f->r, result, apr_psprintf(f->r->pool,
                    "Vary header(s) should NOT match any of: %s", varys),
                    conf->vary_url, bb, HTTP_BAD_GATEWAY);

        }

    }

    ap_remove_output_filter(f);
    return ap_pass_brigade(f->next, bb);
}

/**
 * Policy for Validation.
 *
 * Validation is possible through either the ETag or Last-Modified header, as described
 * in http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.
 *
 * - Either must be present
 * - Last-Modified, if present, must parse to a valid date
 * - ETag, if present, must parse to a valid ETag.
 */
static apr_status_t policy_validation_out_filter(ap_filter_t *f,
        apr_bucket_brigade *bb)
{

    policy_conf *conf = ap_get_module_config(f->r->per_dir_config,
            &policy_module);
    policy_result result = check_enabled(f->r, conf, conf->validation_action);

    if (result != policy_ignore) {
        int fail = 1, etagfail = 0, lmfail = 0;
        const char *etag = apr_table_get(f->r->headers_out, "ETag");
        const char *lastmodified = apr_table_get(f->r->headers_out,
                "Last-Modified");

        if (etag) {
            int len = strlen(etag);
            if (len > 1) {
                if (etag[0] == '\"' && etag[len - 1] == '\"') {
                    fail = 0;
                }
                else if (etag[0] == 'W' && etag[1] == '/' && etag[2] == '\"'
                        && etag[len - 1] == '\"') {
                    fail = 0;
                }
            }
            if (fail) {
                etagfail = 1;
            }
        }

        if (lastmodified) {
            apr_time_t lastmod = apr_date_parse_http(lastmodified);
            if (lastmod != APR_DATE_BAD) {
                fail = 0;
            }
            if (fail) {
                lmfail = 1;
            }
        }

        if (fail) {
            const char *error = NULL;
            if (!etag && !lastmodified) {
                error = apr_psprintf(f->r->pool,
                        "Etag and Last Modified missing");
            }
            else {
                error = apr_pstrcat(f->r->pool,
                        (etagfail ? "ETag syntax error (check quotes)" : ""),
                        (etagfail && lmfail ? " / " : ""),
                        (lmfail ? "Last-Modified could not be parsed" : ""),
                        NULL);
            }

            handle_policy(f->r, result, error, conf->validation_url, bb,
                    HTTP_BAD_GATEWAY);

        }

    }

    ap_remove_output_filter(f);
    return ap_pass_brigade(f->next, bb);
}

/**
 * Policy for Revalidation through Conditional Requests.
 *
 * The If-None-Match header is described in
 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26. Over and above the
 * checks done in the validation filter, the conditional filter detects when an
 * If-None-Match header is present in the request, an ETag is present in the response,
 * and the response code is unexpected given the match. A result code is unexpected
 * where a 304 Not Modified or 412 Precondition Failed was expected, but a 200 response
 * was seen instead.
 */
static apr_status_t policy_conditional_out_filter(ap_filter_t *f,
        apr_bucket_brigade *bb)
{

    policy_conf *conf = ap_get_module_config(f->r->per_dir_config,
            &policy_module);
    policy_result result = check_enabled(f->r, conf, conf->conditional_action);

    if (result != policy_ignore) {
        int code = ap_meets_conditions(f->r);

        if (OK != code && code != f->r->status) {

            handle_policy(
                    f->r,
                    result,
                    apr_psprintf(
                            f->r->pool,
                            "Conditional request should have returned %d, instead returned %d",
                            code, f->r->status), conf->conditional_url, bb,
                    HTTP_BAD_GATEWAY);

        }

    }

    ap_remove_output_filter(f);
    return ap_pass_brigade(f->next, bb);
}

/**
 * Policy for No-Cache Requests.
 *
 * If the Cache-Control and/or Pragma header specifies that the content is not
 * cacheable, the request will be rejected.
 *
 * - If Cache-Control: no-cache
 * - If Pragma: no-cache
 * - If Cache-Control: no-store
 * - If Cache-Control: private
 */
static apr_status_t policy_nocache_out_filter(ap_filter_t *f,
        apr_bucket_brigade *bb)
{

    policy_conf *conf = ap_get_module_config(f->r->per_dir_config,
            &policy_module);
    policy_result result = check_enabled(f->r, conf, conf->nocache_action);

    if (result != policy_ignore) {
        request_rec *r = f->r;
        const char *cc_header = apr_table_get(r->headers_out, "Cache-Control");
        const char *pragma_header = apr_table_get(r->headers_out, "Pragma");
        int fail = 0;
        char *last;

        if (pragma_header) {
            char *header = apr_pstrdup(r->pool, pragma_header);
            const char *token = apr_strtok(header, ", ", &last);
            while (token) {
                if (!ap_cstr_casecmp(token, "no-cache")) {
                    fail = 1;
                }
                token = apr_strtok(NULL, ", ", &last);
            }
        }

        if (cc_header) {
            char *header = apr_pstrdup(r->pool, cc_header);
            const char *token = apr_strtok(header, ", ", &last);
            while (token) {
                switch (token[0]) {
                case 'n':
                case 'N': {
                    if (!ap_cstr_casecmpn(token, "no-cache", 8)) {
                        if (token[8] == '=') {
                        }
                        else if (!token[8]) {
                            fail = 1;
                        }
                        break;
                    }
                    else if (!ap_cstr_casecmp(token, "no-store")) {
                        fail = 1;
                    }
                    break;
                }
                case 'p':
                case 'P': {
                    if (!ap_cstr_casecmpn(token, "private", 7)) {
                        if (token[7] == '=') {
                        }
                        else if (!token[7]) {
                            fail = 1;
                        }
                        break;
                    }
                    break;
                }
                }
                token = apr_strtok(NULL, ", ", &last);
            }
        }

        if (fail) {

            handle_policy(r, result, "Response is marked uncacheable",
                    conf->nocache_url, bb, HTTP_BAD_GATEWAY);

        }

    }

    ap_remove_output_filter(f);
    return ap_pass_brigade(f->next, bb);
}

/**
 * Policy for Maxage.
 *
 * If the effective maxage of the request is less than the parameter provided,
 * the request will be rejected.
 *
 * - If Cache-Control: s-maxage is less than the limit
 * - If Cache-Control: maxage is less than the limit
 * - If Expires - Date is less than the limit
 * - If none of the above, reject the request, as maxage is heuristic
 *
 * As soon as a test passes, we stop, as HTTP maxage handling follows a given
 * set of priorities (s-maxage beats maxage, maxage beats Expires).
 */
static apr_status_t policy_maxage_out_filter(ap_filter_t *f,
        apr_bucket_brigade *bb)
{

    policy_conf *conf = ap_get_module_config(f->r->per_dir_config,
            &policy_module);
    policy_result result = check_enabled(f->r, conf, conf->maxage_action);

    if (result != policy_ignore) {
        request_rec *r = f->r;
        const char *cc_header;
        const char *expires_header;
        const char *date_header;
        char *last;

        int max_age = 0;
        apr_int64_t max_age_value = 0;
        int s_maxage = 0;
        apr_int64_t s_maxage_value = 0;

        /* parse Cache-Control */
        cc_header = apr_table_get(r->headers_out, "Cache-Control");
        if (cc_header) {
            char *header = apr_pstrdup(r->pool, cc_header);
            const char *token = apr_strtok(header, ", ", &last);
            while (token) {
                switch (token[0]) {
                case 'm':
                case 'M': {
                    if (!ap_cstr_casecmpn(token, "max-age", 7)) {
                        if (token[7] == '=') {
                            max_age = 1;
                            max_age_value = apr_atoi64(token + 8);
                        }
                        break;
                    }
                    break;
                }
                case 's':
                case 'S': {
                    if (!ap_cstr_casecmpn(token, "s-maxage", 8)) {
                        if (token[8] == '=') {
                            s_maxage = 1;
                            s_maxage_value = apr_atoi64(token + 9);
                        }
                        break;
                    }
                    break;
                }
                }
                token = apr_strtok(NULL, ", ", &last);
            }
        }

        /* test s-maxage, if present */
        if (s_maxage) {
            if (s_maxage_value < conf->maxage) {

                handle_policy(
                        f->r,
                        result,
                        apr_psprintf(
                                f->r->pool,
                                "Response s-maxage of %" APR_INT64_T_FMT " must be at least %" APR_INT64_T_FMT,
                                s_maxage_value, conf->maxage), conf->maxage_url,
                        bb, HTTP_BAD_GATEWAY);

            }

            /* decision is made, leave */
            ap_remove_output_filter(f);
            return ap_pass_brigade(f->next, bb);
        }

        /* test max-age, if present */
        if (max_age) {
            if (max_age_value < conf->maxage) {

                handle_policy(
                        f->r,
                        result,
                        apr_psprintf(
                                f->r->pool,
                                "Response max-age of %" APR_INT64_T_FMT " must be at least %" APR_INT64_T_FMT,
                                max_age_value, conf->maxage), conf->maxage_url,
                        bb, HTTP_BAD_GATEWAY);

            }

            /* decision is made, leave */
            ap_remove_output_filter(f);
            return ap_pass_brigade(f->next, bb);
        }

        /* test expires, if present */
        expires_header = apr_table_get(r->headers_out, "Expires");
        date_header = apr_table_get(r->headers_out, "Date");
        if (expires_header && date_header) {
            apr_time_t expires = apr_date_parse_http(expires_header);
            apr_time_t date = apr_date_parse_http(date_header);
            apr_int64_t fresh = apr_time_sec(expires - date);

            if (expires == APR_DATE_BAD) {

                handle_policy(
                        f->r,
                        result,
                        apr_psprintf(
                                f->r->pool,
                                "Response Expires of '%s' is invalid, maxage %" APR_INT64_T_FMT " required",
                                expires_header, conf->maxage),
                        conf->maxage_url, bb, HTTP_BAD_GATEWAY);

            }

            else if (date == APR_DATE_BAD) {

                handle_policy(
                        f->r,
                        result,
                        apr_psprintf(
                                f->r->pool,
                                "Response Date of '%s' is invalid, maxage %" APR_INT64_T_FMT " required",
                                date_header, conf->maxage),
                        conf->maxage_url, bb, HTTP_BAD_GATEWAY);

            }

            else if (conf->maxage > 0 && fresh < conf->maxage) {

                handle_policy(
                        f->r,
                        result,
                        apr_psprintf(
                                f->r->pool,
                                "Response expires in %" APR_INT64_T_FMT " seconds, must be at least %" APR_INT64_T_FMT,
                                fresh, conf->maxage),
                        conf->maxage_url, bb, HTTP_BAD_GATEWAY);

            }

            /* decision is made, leave */
            ap_remove_output_filter(f);
            return ap_pass_brigade(f->next, bb);
        }

        /* no explicit maxage defined, so fail */
        handle_policy(r, result, "Response has no explicit freshness lifetime (s-maxage, max-age or Expires/Date)",
                conf->maxage_url, bb, HTTP_BAD_GATEWAY);

    }

    ap_remove_output_filter(f);
    return ap_pass_brigade(f->next, bb);
}

static const char *version_string(int proto_num)
{
    switch (proto_num) {
    case HTTP_VERSION(0, 9): {
        return "HTTP/0.9";
    }
    case HTTP_VERSION(1, 0): {
        return "HTTP/1.0";
    }
    case HTTP_VERSION(1, 1): {
        return "HTTP/1.1";
    }
    default: {
        return "(unknown)";
    }
    }
}

/**
 * Policy for HTTP Version.
 *
 * - The HTTP version of the response must be at least the level specified.
 */
static apr_status_t policy_version_out_filter(ap_filter_t *f,
        apr_bucket_brigade *bb)
{
    request_rec *r = f->r;

    policy_conf *conf = ap_get_module_config(r->per_dir_config,
            &policy_module);
    policy_result result = check_enabled(r, conf, conf->version_action);

    if (result != policy_ignore) {

        if (r->proto_num > 0 && r->proto_num < conf->version_num) {

            handle_policy(f->r, result, apr_psprintf(f->r->pool,
                    "Request HTTP version '%s' should be at least '%s'",
                    version_string(r->proto_num), conf->version),
                    conf->version_url, bb, HTTP_VERSION_NOT_SUPPORTED);

        }

    }

    ap_remove_output_filter(f);
    return ap_pass_brigade(f->next, bb);
}

static void *create_policy_config(apr_pool_t *p, char *dummy)
{
    policy_conf *new = (policy_conf *) apr_pcalloc(p, sizeof(policy_conf));

    new->policy = 1;
    new->type_action = policy_log;
    new->length_action = policy_log;
    new->vary_action = policy_log;
    new->validation_action = policy_log;
    new->conditional_action = policy_log;
    new->nocache_action = policy_log;
    new->maxage_action = policy_log;
    new->version_action = policy_log;
    new->version_num = HTTP_VERSION(0, 9);
    new->version = "HTTP/0.9";

    return (void *) new;
}

static void *merge_policy_config(apr_pool_t *p, void *basev, void *addv)
{
    policy_conf *new = (policy_conf *) apr_pcalloc(p, sizeof(policy_conf));
    policy_conf *add = (policy_conf *) addv;
    policy_conf *base = (policy_conf *) basev;

    new->policy = (add->policy_set == 0) ? base->policy : add->policy;
    new->policy_set = add->policy_set || base->policy_set;
    new->environment = (add->environment_set == 0) ? base->environment : add->environment;
    new->environment_log = (add->environment_set == 0) ? base->environment_log
            : add->environment_log;
    new->environment_ignore = (add->environment_set == 0) ? base->environment_ignore
            : add->environment_ignore;
    new->environment_set = add->environment_set || base->environment_set;
    new->type_action = (add->type_set == 0) ? base->type_action
            : add->type_action;
    new->type_matches = (add->type_set == 0) ? base->type_matches
            : add->type_matches;
    new->type_set = add->type_set || base->type_set;
    new->type_url = (add->type_url_set == 0) ? base->type_url : add->type_url;
    new->type_url_set = add->type_url_set || base->type_url_set;
    new->length_action = (add->length_set == 0) ? base->length_action
            : add->length_action;
    new->length_set = add->length_set || base->length_set;
    new->length_url = (add->length_url_set == 0) ? base->length_url
            : add->length_url;
    new->length_url_set = add->length_url_set || base->length_url_set;
    new->vary_action = (add->vary_set == 0) ? base->vary_action
            : add->vary_action;
    new->vary_matches = (add->vary_set == 0) ? base->vary_matches
            : add->vary_matches;
    new->vary_set = add->vary_set || base->vary_set;
    new->vary_url = (add->vary_url_set == 0) ? base->vary_url : add->vary_url;
    new->vary_url_set = add->vary_url_set || base->vary_url_set;
    new->validation_action
            = (add->validation_set == 0) ? base->validation_action
                    : add->validation_action;
    new->validation_set = add->validation_set || base->validation_set;
    new->validation_url = (add->validation_url_set == 0) ? base->validation_url
            : add->validation_url;
    new->validation_url_set = add->validation_url_set
            || base->validation_url_set;
    new->conditional_action
            = (add->conditional_set == 0) ? base->conditional_action
                    : add->conditional_action;
    new->conditional_set = add->conditional_set || base->conditional_set;
    new->conditional_url
            = (add->conditional_url_set == 0) ? base->conditional_url
                    : add->conditional_url;
    new->conditional_url_set = add->conditional_url_set
            || base->conditional_url_set;
    new->nocache_action = (add->nocache_set == 0) ? base->nocache_action
            : add->nocache_action;
    new->nocache_set = add->nocache_set || base->nocache_set;
    new->nocache_url = (add->nocache_url_set == 0) ? base->nocache_url
            : add->nocache_url;
    new->nocache_url_set = add->nocache_url_set || base->nocache_url_set;
    new->maxage_action = (add->maxage_set == 0) ? base->maxage_action
            : add->maxage_action;
    new->maxage = (add->maxage_set == 0) ? base->maxage : add->maxage;
    new->maxage_set = add->maxage_set || base->maxage_set;
    new->maxage_url = (add->maxage_url_set == 0) ? base->maxage_url
            : add->maxage_url;
    new->maxage_url_set = add->maxage_url_set || base->maxage_url_set;
    new->version_action = (add->version_set == 0) ? base->version_action
            : add->version_action;
    new->version = (add->version_set == 0) ? base->version : add->version;
    new->version_num = (add->version_set == 0) ? base->version_num : add->version_num;
    new->version_set = add->version_set || base->version_set;
    new->version_url = (add->version_url_set == 0) ? base->version_url
            : add->version_url;
    new->version_url_set = add->version_url_set || base->version_url_set;


    return new;
}

static const char *parse_action(apr_pool_t *pool, const char *action,
        policy_result *result)
{
    if (!strcmp(action, "enforce")) {
        *result = policy_enforce;
    }
    else if (!strcmp(action, "log")) {
        *result = policy_log;
    }
    else if (!strcmp(action, "ignore")) {
        *result = policy_ignore;
    }
    else {
        return apr_psprintf(pool,
                "'%s' must be one of 'enforce, 'log' or 'ignore'.", action);
    }
    return NULL;
}

static const char *set_policy(cmd_parms *cmd, void *dconf, int flag)
{
    policy_conf *conf = dconf;

    conf->policy = flag;
    conf->policy_set = 1;

    return NULL;
}

static const char *set_environment(cmd_parms *cmd, void *dconf,
        const char *environment, const char *log, const char *ignore)
{
    policy_conf *conf = dconf;

    conf->environment = environment;
    conf->environment_log = log;
    conf->environment_ignore = ignore;
    conf->environment_set = 1;

    return NULL;
}

static const char *set_type(cmd_parms *cmd, void *dconf, const char *action,
        const char *type)
{
    policy_conf *conf = dconf;

    if (type) {
        const char **match_ptr;
        if (!conf->type_matches) {
            conf->type_matches = apr_array_make(cmd->pool, 2,
                    sizeof(const char *));
        }
        match_ptr = apr_array_push(conf->type_matches);
        *match_ptr = type;
    }
    conf->type_set = 1;

    return parse_action(cmd->pool, action, &conf->type_action);
}

static const char *set_type_url(cmd_parms *cmd, void *dconf, const char *url)
{
    policy_conf *conf = dconf;

    /* url is only used inside <a href="...">, escape accordingly */
    conf->type_url = ap_escape_html(cmd->pool, url);
    conf->type_url_set = 1;

    return NULL;
}

static const char *set_length(cmd_parms *cmd, void *dconf, const char *action)
{
    policy_conf *conf = dconf;

    conf->length_set = 1;

    return parse_action(cmd->pool, action, &conf->length_action);
}

static const char *set_length_url(cmd_parms *cmd, void *dconf, const char *url)
{
    policy_conf *conf = dconf;

    conf->length_url = url;
    conf->length_url_set = 1;

    return NULL;
}

static const char *set_keepalive(cmd_parms *cmd, void *dconf, const char *action)
{
    policy_conf *conf = dconf;

    conf->keepalive_set = 1;

    return parse_action(cmd->pool, action, &conf->keepalive_action);
}

static const char *set_keepalive_url(cmd_parms *cmd, void *dconf, const char *url)
{
    policy_conf *conf = dconf;

    conf->keepalive_url = url;
    conf->keepalive_url_set = 1;

    return NULL;
}

static const char *set_vary(cmd_parms *cmd, void *dconf, const char *action,
        const char *vary)
{
    policy_conf *conf = dconf;

    if (vary) {
        const char **match_ptr;
        if (!conf->vary_matches) {
            conf->vary_matches = apr_array_make(cmd->pool, 2,
                    sizeof(const char *));
        }
        match_ptr = apr_array_push(conf->vary_matches);
        *match_ptr = vary;
    }
    conf->vary_set = 1;

    return parse_action(cmd->pool, action, &conf->vary_action);
}

static const char *set_vary_url(cmd_parms *cmd, void *dconf, const char *url)
{
    policy_conf *conf = dconf;

    conf->vary_url = url;
    conf->vary_url_set = 1;

    return NULL;
}

static const char *set_validation(cmd_parms *cmd, void *dconf,
        const char *action)
{
    policy_conf *conf = dconf;

    conf->validation_set = 1;

    return parse_action(cmd->pool, action, &conf->validation_action);
}

static const char *set_validation_url(cmd_parms *cmd, void *dconf,
        const char *url)
{
    policy_conf *conf = dconf;

    conf->validation_url = url;
    conf->validation_url_set = 1;

    return NULL;
}

static const char *set_conditional(cmd_parms *cmd, void *dconf,
        const char *action)
{
    policy_conf *conf = dconf;

    conf->conditional_set = 1;

    return parse_action(cmd->pool, action, &conf->conditional_action);
}

static const char *set_conditional_url(cmd_parms *cmd, void *dconf,
        const char *url)
{
    policy_conf *conf = dconf;

    conf->conditional_url = url;
    conf->conditional_url_set = 1;

    return NULL;
}

static const char *set_nocache(cmd_parms *cmd, void *dconf, const char *action)
{
    policy_conf *conf = dconf;

    conf->nocache_set = 1;

    return parse_action(cmd->pool, action, &conf->nocache_action);
}

static const char *set_nocache_url(cmd_parms *cmd, void *dconf, const char *url)
{
    policy_conf *conf = dconf;

    conf->nocache_url = url;
    conf->nocache_url_set = 1;

    return NULL;
}

static const char *set_maxage(cmd_parms *cmd, void *dconf, const char *action, const char *maxage)
{
    policy_conf *conf = dconf;

    conf->maxage_set = 1;
    conf->maxage = apr_atoi64(maxage);
    if (conf->maxage < 0) {
        return apr_psprintf(cmd->pool,
                "'%s' must be a positive integer.", maxage);
    }

    return parse_action(cmd->pool, action, &conf->maxage_action);
}

static const char *set_maxage_url(cmd_parms *cmd, void *dconf, const char *url)
{
    policy_conf *conf = dconf;

    conf->maxage_url = url;
    conf->maxage_url_set = 1;

    return NULL;
}

static const char *set_version(cmd_parms *cmd, void *dconf, const char *action, const char *version)
{
    policy_conf *conf = dconf;

    conf->version_set = 1;

    if (!strcmp(version, "HTTP/1.1")) {
        conf->version = "HTTP/1.1";
        conf->version_num = HTTP_VERSION(1, 1);
    }
    else if (!strcmp(version, "HTTP/1.0")) {
        conf->version = "HTTP/1.0";
        conf->version_num = HTTP_VERSION(1, 0);
    }
    else if (!strcmp(version, "HTTP/0.9")) {
        conf->version = "HTTP/0.9";
        conf->version_num = HTTP_VERSION(0, 9);
    }
    else {
        return apr_psprintf(cmd->pool,
                "'%s' must be one of 'HTTP/1.1', 'HTTP/1.0' or 'HTTP/0.9'.", version);
    }

    return parse_action(cmd->pool, action, &conf->version_action);
}

static const char *set_version_url(cmd_parms *cmd, void *dconf, const char *url)
{
    policy_conf *conf = dconf;

    conf->version_url = url;
    conf->version_url_set = 1;

    return NULL;
}

static const command_rec
        policy_cmds[] =
        {
                AP_INIT_FLAG("PolicyFilter", set_policy, NULL, RSRC_CONF
                        | ACCESS_CONF,
                        "Whether policies should be applied. Defaults to 'on'."),
                AP_INIT_TAKE1(
                        "PolicyConditional",
                        set_conditional,
                        NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "Action to take (enforce, ignore, log) if a conditional request was not honoured. Defaults to 'log'."),
                AP_INIT_TAKE1("PolicyConditionalURL", set_conditional_url,
                        NULL, RSRC_CONF | ACCESS_CONF,
                        "URL describing the conditional request policy."),
                AP_INIT_TAKE3(
                        "PolicyEnvironment",
                        set_environment,
                        NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "Environment variable to control policy enforcement, followed by the variable value for logging only, and the value for policy suspension."),
                AP_INIT_TAKE1(
                        "PolicyLength",
                        set_length,
                        NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "Action to take (enforce, ignore, log) if Content-Length missing. Defaults to 'log'."),
                AP_INIT_TAKE1("PolicyLengthURL", set_length_url, NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "URL describing the content length policy."),
                AP_INIT_TAKE1(
                        "PolicyKeepalive",
                        set_keepalive,
                        NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "Action to take (enforce, ignore, log) if keepalive is not possible. Defaults to 'log'."),
                AP_INIT_TAKE1("PolicyKeepaliveURL", set_keepalive_url, NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "URL describing the keepalive policy."),
                AP_INIT_ITERATE2(
                        "PolicyType",
                        set_type,
                        NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "Action to take (enforce, ignore, log), followed by one or more valid content types containing optional wildcards ? and *"),
                AP_INIT_TAKE1("PolicyTypeURL", set_type_url, NULL, RSRC_CONF
                        | ACCESS_CONF,
                        "URL describing the content type policy."),
                AP_INIT_ITERATE2(
                        "PolicyVary",
                        set_vary,
                        NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "Action to take (enforce, ignore, log), followed by one or more headers containing optional wildcards ? and * that are NOT to appear in a Vary header"),
                AP_INIT_TAKE1("PolicyVaryURL", set_vary_url, NULL, RSRC_CONF
                        | ACCESS_CONF,
                        "URL describing the vary header policy."),
                AP_INIT_TAKE1(
                        "PolicyValidation",
                        set_validation,
                        NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "Action to take (enforce, ignore, log) if Last-Modified or Etag is missing or invalid. Defaults to 'log'."),
                AP_INIT_TAKE1("PolicyValidationURL", set_validation_url, NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "URL describing the content validation policy."),
                AP_INIT_TAKE1(
                        "PolicyNocache",
                        set_nocache,
                        NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "Action to take (enforce, ignore, log) if a response is not cacheable. Defaults to 'log'."),
                AP_INIT_TAKE1("PolicyNocacheURL", set_nocache_url, NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "URL describing the no cache policy."),
                AP_INIT_TAKE2(
                        "PolicyMaxage",
                        set_maxage,
                        NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "Action to take (enforce, ignore, log) if a response has an effective maxage less than the age provided. Defaults to 'log'."),
                AP_INIT_TAKE1("PolicyMaxageURL", set_maxage_url, NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "URL describing the maxage policy."),
                AP_INIT_TAKE2(
                        "PolicyVersion",
                        set_version,
                        NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "Action to take (enforce, ignore, log) if a response has an HTTP version less than the version provided. Defaults to 'log HTTP/0.9'."),
                AP_INIT_TAKE1("PolicyVersionURL", set_version_url, NULL,
                        RSRC_CONF | ACCESS_CONF,
                        "URL describing the version policy."),
                { NULL } };

static void register_hooks(apr_pool_t *p)
{
    ap_register_output_filter("POLICY_TYPE", policy_type_out_filter, NULL,
            AP_FTYPE_CONTENT_SET + 5);
    ap_register_output_filter("POLICY_LENGTH", policy_length_out_filter, NULL,
            AP_FTYPE_CONTENT_SET + 5);
    ap_register_output_filter("POLICY_KEEPALIVE", policy_keepalive_out_filter, NULL,
            AP_FTYPE_CONTENT_SET + 5);
    ap_register_output_filter("POLICY_VARY", policy_vary_out_filter, NULL,
            AP_FTYPE_CONTENT_SET + 5);
    ap_register_output_filter("POLICY_VALIDATION",
            policy_validation_out_filter, NULL, AP_FTYPE_CONTENT_SET + 5);
    ap_register_output_filter("POLICY_CONDITIONAL",
            policy_conditional_out_filter, NULL, AP_FTYPE_CONTENT_SET + 5);
    ap_register_output_filter("POLICY_NOCACHE", policy_nocache_out_filter,
            NULL, AP_FTYPE_CONTENT_SET + 5);
    ap_register_output_filter("POLICY_MAXAGE", policy_maxage_out_filter,
            NULL, AP_FTYPE_CONTENT_SET + 5);
    ap_register_output_filter("POLICY_VERSION", policy_version_out_filter,
            NULL, AP_FTYPE_CONTENT_SET + 5);
}

AP_DECLARE_MODULE(policy) =
{
    STANDARD20_MODULE_STUFF, create_policy_config, /* create per-directory config structure */
    merge_policy_config, /* merge per-directory config structures */
    NULL, /* create per-server config structure */
    NULL, /* merge per-server config structures */
    policy_cmds, /* command apr_table_t */
    register_hooks /* register hooks */
};