Coverage Report

Created: 2026-12-01 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/httpd/server/util_expr_eval.c
Line
Count
Source
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
 * contributor license agreements.  See the NOTICE file distributed with
3
 * this work for additional information regarding copyright ownership.
4
 * The ASF licenses this file to You under the Apache License, Version 2.0
5
 * (the "License"); you may not use this file except in compliance with
6
 * the License.  You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
/*
18
 *  ap_expr_eval.c, based on ssl_expr_eval.c from mod_ssl
19
 */
20
21
#include "httpd.h"
22
#include "http_log.h"
23
#include "http_core.h"
24
#include "http_protocol.h"
25
#include "http_request.h"
26
#include "http_ssl.h"
27
#include "ap_provider.h"
28
#include "util_varbuf.h"
29
#include "util_expr_private.h"
30
#include "util_md5.h"
31
#include "util_varbuf.h"
32
33
#include "apr_lib.h"
34
#include "apr_fnmatch.h"
35
#include "apr_base64.h"
36
#include "apr_sha1.h"
37
#include "apr_version.h"
38
#include "apr_strings.h"
39
#include "apr_strmatch.h"
40
#if APR_VERSION_AT_LEAST(1,5,0)
41
#include "apr_escape.h"
42
#endif
43
44
#include <limits.h>     /* for INT_MAX */
45
46
/* we know core's module_index is 0 */
47
#undef APLOG_MODULE_INDEX
48
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
49
50
APR_HOOK_STRUCT(
51
    APR_HOOK_LINK(expr_lookup)
52
)
53
54
AP_IMPLEMENT_HOOK_RUN_FIRST(int, expr_lookup, (ap_expr_lookup_parms *parms),
55
                            (parms), DECLINED)
56
57
#define  LOG_MARK(info)  __FILE__, __LINE__, (info)->module_index
58
59
static int ap_expr_eval_cond(ap_expr_eval_ctx_t *ctx, const ap_expr_t *node);
60
61
static const char *ap_expr_eval_string_func(ap_expr_eval_ctx_t *ctx,
62
                                            const ap_expr_t *info,
63
                                            const ap_expr_t *args);
64
static const char *ap_expr_eval_re_backref(ap_expr_eval_ctx_t *ctx,
65
                                           unsigned int n);
66
static const char *ap_expr_eval_var(ap_expr_eval_ctx_t *ctx,
67
                                    ap_expr_var_func_t *func,
68
                                    const void *data);
69
70
typedef struct {
71
    int flags;
72
    const ap_expr_t *subst;
73
} ap_expr_regctx_t;
74
75
static const char *ap_expr_regexec(const char *subject,
76
                                   const ap_expr_t *reg,
77
                                   apr_array_header_t *list,
78
                                   ap_expr_eval_ctx_t *ctx);
79
80
static apr_array_header_t *ap_expr_list_make(ap_expr_eval_ctx_t *ctx,
81
                                             const ap_expr_t *node);
82
83
/* define AP_EXPR_DEBUG to log the parse tree when parsing an expression */
84
#ifdef AP_EXPR_DEBUG
85
static void expr_dump_tree(const ap_expr_t *e, const server_rec *s,
86
                           int loglevel, int indent);
87
#endif
88
89
/*
90
 * To reduce counting overhead, we only count calls to
91
 * ap_expr_eval_word() and ap_expr_eval_cond(). The max number of
92
 * stack frames is larger by some factor.
93
 */
94
0
#define AP_EXPR_MAX_RECURSION   20
95
static int inc_rec(ap_expr_eval_ctx_t *ctx)
96
0
{
97
0
    if (ctx->reclvl < AP_EXPR_MAX_RECURSION) {
98
0
        ctx->reclvl++;
99
0
        return 0;
100
0
    }
101
0
    *ctx->err = "Recursion limit reached";
102
    /* short circuit further evaluation */
103
0
    ctx->reclvl = INT_MAX;
104
0
    return 1;
105
0
}
106
107
static const char *ap_expr_list_pstrcat(apr_pool_t *p,
108
                                        const apr_array_header_t *list,
109
                                        const char *sep)
110
0
{
111
0
    if (list->nelts <= 0) {
112
0
        return NULL;
113
0
    }
114
0
    else if (list->nelts == 1) {
115
0
        return APR_ARRAY_IDX(list, 0, const char*);
116
0
    }
117
0
    else {
118
0
        struct ap_varbuf vb;
119
0
        int n = list->nelts - 1, i;
120
0
        apr_size_t slen = strlen(sep), vlen;
121
0
        const char *val;
122
123
0
        ap_varbuf_init(p, &vb, 0);
124
0
        for (i = 0; i < n; ++i) {
125
0
            val = APR_ARRAY_IDX(list, i, const char*);
126
0
            vlen = strlen(val);
127
0
            ap_varbuf_strmemcat(&vb, val, vlen);
128
0
            ap_varbuf_strmemcat(&vb, sep, slen);
129
0
        }
130
0
        val = APR_ARRAY_IDX(list, n, const char*);
131
0
        ap_varbuf_strmemcat(&vb, val, strlen(val));
132
133
0
        return vb.buf;
134
0
    }
135
0
}
136
137
static const char *ap_expr_eval_word(ap_expr_eval_ctx_t *ctx,
138
                                     const ap_expr_t *node)
139
0
{
140
0
    const char *result = "";
141
0
    if (inc_rec(ctx))
142
0
        return result;
143
0
    switch (node->node_op) {
144
0
    case op_Digit:
145
0
    case op_String:
146
0
        result = node->node_arg1;
147
0
        break;
148
0
    case op_Word:
149
0
        result = ap_expr_eval_word(ctx, node->node_arg1);
150
0
        break;
151
0
    case op_Bool:
152
0
        result = ap_expr_eval_cond(ctx, node->node_arg1) ? "true" : "false";
153
0
        break;
154
0
    case op_Var:
155
0
        result = ap_expr_eval_var(ctx, (ap_expr_var_func_t *)node->node_arg1,
156
0
                                  node->node_arg2);
157
0
        break;
158
0
    case op_Concat:
159
0
        if (((ap_expr_t *)node->node_arg2)->node_op != op_Concat &&
160
0
            ((ap_expr_t *)node->node_arg1)->node_op != op_Concat) {
161
0
            const char *s1 = ap_expr_eval_word(ctx, node->node_arg1);
162
0
            const char *s2 = ap_expr_eval_word(ctx, node->node_arg2);
163
0
            if (!*s1)
164
0
                result = s2;
165
0
            else if (!*s2)
166
0
                result = s1;
167
0
            else
168
0
                result = apr_pstrcat(ctx->p, s1, s2, NULL);
169
0
        }
170
0
        else if (((ap_expr_t *)node->node_arg1)->node_op == op_Concat) {
171
0
            const ap_expr_t *nodep = node;
172
0
            int n;
173
0
            int i = 1;
174
0
            struct iovec *vec;
175
0
            do {
176
0
                nodep = nodep->node_arg1;
177
0
                i++;
178
0
            } while (nodep->node_op == op_Concat);
179
0
            vec = apr_palloc(ctx->p, i * sizeof(struct iovec));
180
0
            n = i;
181
0
            nodep = node;
182
0
            i--;
183
0
            do {
184
0
                vec[i].iov_base = (void *)ap_expr_eval_word(ctx,
185
0
                                                            nodep->node_arg2);
186
0
                vec[i].iov_len = strlen(vec[i].iov_base);
187
0
                i--;
188
0
                nodep = nodep->node_arg1;
189
0
            } while (nodep->node_op == op_Concat);
190
0
            vec[i].iov_base = (void *)ap_expr_eval_word(ctx, nodep);
191
0
            vec[i].iov_len = strlen(vec[i].iov_base);
192
0
            result = apr_pstrcatv(ctx->p, vec, n, NULL);
193
0
        }
194
0
        else {
195
0
            const ap_expr_t *nodep = node;
196
0
            int i = 1;
197
0
            struct iovec *vec;
198
0
            do {
199
0
                nodep = nodep->node_arg2;
200
0
                i++;
201
0
            } while (nodep->node_op == op_Concat);
202
0
            vec = apr_palloc(ctx->p, i * sizeof(struct iovec));
203
0
            nodep = node;
204
0
            i = 0;
205
0
            do {
206
0
                vec[i].iov_base = (void *)ap_expr_eval_word(ctx,
207
0
                                                            nodep->node_arg1);
208
0
                vec[i].iov_len = strlen(vec[i].iov_base);
209
0
                i++;
210
0
                nodep = nodep->node_arg2;
211
0
            } while (nodep->node_op == op_Concat);
212
0
            vec[i].iov_base = (void *)ap_expr_eval_word(ctx, nodep);
213
0
            vec[i].iov_len = strlen(vec[i].iov_base);
214
0
            i++;
215
0
            result = apr_pstrcatv(ctx->p, vec, i, NULL);
216
0
        }
217
0
        break;
218
0
    case op_StringFuncCall: {
219
0
        const ap_expr_t *info = node->node_arg1;
220
0
        const ap_expr_t *args = node->node_arg2;
221
0
        result = ap_expr_eval_string_func(ctx, info, args);
222
0
        break;
223
0
    }
224
0
    case op_Join: {
225
0
        const char *sep;
226
0
        apr_array_header_t *list = ap_expr_list_make(ctx, node->node_arg1);
227
0
        sep = node->node_arg2 ? ap_expr_eval_word(ctx, node->node_arg2) : "";
228
0
        result = ap_expr_list_pstrcat(ctx->p, list, sep);
229
0
        break;
230
0
    }
231
0
    case op_Sub: {
232
0
        const ap_expr_t *reg = node->node_arg2;
233
0
        const char *subject = ap_expr_eval_word(ctx, node->node_arg1);
234
0
        result = ap_expr_regexec(subject, reg, NULL, ctx);
235
0
        break;
236
0
    }
237
0
    case op_Backref: {
238
0
        const unsigned int *np = node->node_arg1;
239
0
        result = ap_expr_eval_re_backref(ctx, *np);
240
0
        break;
241
0
    }
242
0
    default:
243
0
        *ctx->err = "Internal evaluation error: Unknown word expression node";
244
0
        break;
245
0
    }
246
0
    if (!result)
247
0
        result = "";
248
0
    ctx->reclvl--;
249
0
    return result;
250
0
}
251
252
static const char *ap_expr_eval_var(ap_expr_eval_ctx_t *ctx,
253
                                    ap_expr_var_func_t *func,
254
                                    const void *data)
255
0
{
256
0
    AP_DEBUG_ASSERT(func != NULL);
257
0
    AP_DEBUG_ASSERT(data != NULL);
258
0
    return (*func)(ctx, data);
259
0
}
260
261
static const char *ap_expr_eval_re_backref(ap_expr_eval_ctx_t *ctx, unsigned int n)
262
0
{
263
0
    int len;
264
265
0
    if (!ctx->re_pmatch || !ctx->re_source || !*ctx->re_source
266
0
        || **ctx->re_source == '\0' || ctx->re_nmatch < n + 1)
267
0
        return "";
268
269
    /* A group which did not participate in the match has rm_so == rm_eo
270
     * == -1; expand only a group with a non-empty span in the source.
271
     */
272
0
    if (ctx->re_pmatch[n].rm_so < 0
273
0
        || ctx->re_pmatch[n].rm_eo <= ctx->re_pmatch[n].rm_so)
274
0
        return "";
275
0
    len = ctx->re_pmatch[n].rm_eo - ctx->re_pmatch[n].rm_so;
276
277
0
    return apr_pstrndup(ctx->p, *ctx->re_source + ctx->re_pmatch[n].rm_so, len);
278
0
}
279
280
static const char *ap_expr_eval_string_func(ap_expr_eval_ctx_t *ctx,
281
                                            const ap_expr_t *info,
282
                                            const ap_expr_t *arg)
283
0
{
284
0
    const void *data = info->node_arg2;
285
286
0
    AP_DEBUG_ASSERT(info->node_op == op_StringFuncInfo);
287
0
    AP_DEBUG_ASSERT(info->node_arg1 != NULL);
288
0
    AP_DEBUG_ASSERT(data != NULL);
289
0
    if (arg->node_op == op_ListElement) {
290
        /* Evaluate the list elements and store them in apr_array_header. */
291
0
        ap_expr_string_list_func_t *func = (ap_expr_string_list_func_t *)info->node_arg1;
292
0
        apr_array_header_t *args = ap_expr_list_make(ctx, arg);
293
0
        return (*func)(ctx, data, args);
294
0
    }
295
0
    else {
296
0
        ap_expr_string_func_t *func = (ap_expr_string_func_t *)info->node_arg1;
297
0
        return (*func)(ctx, data, ap_expr_eval_word(ctx, arg));
298
0
    }
299
0
}
300
301
static int intstrcmp(const char *s1, const char *s2)
302
0
{
303
0
    apr_int64_t i1 = apr_atoi64(s1);
304
0
    apr_int64_t i2 = apr_atoi64(s2);
305
306
0
    if (i1 < i2)
307
0
        return -1;
308
0
    else if (i1 == i2)
309
0
        return 0;
310
0
    else
311
0
        return 1;
312
0
}
313
314
static const char *ap_expr_regexec(const char *subject,
315
                                   const ap_expr_t *reg,
316
                                   apr_array_header_t *list,
317
                                   ap_expr_eval_ctx_t *ctx)
318
0
{
319
0
    struct ap_varbuf vb;
320
0
    const char *val = subject;
321
0
    const ap_regex_t *regex = reg->node_arg1;
322
0
    const ap_expr_regctx_t *regctx = reg->node_arg2;
323
0
    ap_regmatch_t *pmatch = NULL, match0;
324
0
    apr_size_t nmatch = 0;
325
0
    const char *str = "";
326
0
    apr_size_t len = 0;
327
0
    int empty = 0, rv;
328
329
0
    ap_varbuf_init(ctx->p, &vb, 0);
330
0
    if (ctx->re_nmatch > 0) {
331
0
        nmatch = ctx->re_nmatch;
332
0
        pmatch = ctx->re_pmatch;
333
0
    }
334
0
    else if (regctx->subst) {
335
0
        nmatch = 1;
336
0
        pmatch = &match0;
337
0
    }
338
0
    do {
339
        /* If previous match was empty, we can't issue the exact same one or
340
         * we'd loop indefinitely.  So let's instead ask for an anchored and
341
         * non-empty match (i.e. something not empty at the start of the value)
342
         * and if nothing is found advance by one character below.
343
         */
344
0
        rv = ap_regexec(regex, val, nmatch, pmatch, 
345
0
                        empty ? AP_REG_ANCHORED | AP_REG_NOTEMPTY : 0);
346
0
        if (rv == 0) {
347
0
            int pos = pmatch[0].rm_so,
348
0
                end = pmatch[0].rm_eo;
349
0
            AP_DEBUG_ASSERT(pos >= 0 && pos <= end);
350
351
0
            if (regctx->subst) {
352
0
                *ctx->re_source = val;
353
0
                str = ap_expr_eval_word(ctx, regctx->subst);
354
0
                len = strlen(str);
355
0
            }
356
0
            if (list) {
357
0
                char *tmp = apr_palloc(ctx->p, pos + len + 1);
358
0
                memcpy(tmp, val, pos);
359
0
                memcpy(tmp + pos, str, len + 1);
360
0
                APR_ARRAY_PUSH(list, const char*) = tmp;
361
0
            }
362
0
            else {
363
0
                ap_varbuf_grow(&vb, pos + len + 1);
364
0
                ap_varbuf_strmemcat(&vb, val, pos);
365
0
                ap_varbuf_strmemcat(&vb, str, len);
366
0
                if (!(regctx->flags & AP_REG_MULTI)) {
367
                    /* Single substitution, preserve remaining data */
368
0
                    ap_varbuf_strmemcat(&vb, val + end, strlen(val) - end);
369
0
                    break;
370
0
                }
371
0
            }
372
            /* Note an empty match */
373
0
            empty = (end == 0);
374
0
            val += end;
375
0
        }
376
0
        else if (empty) {
377
            /* Skip this non-matching character (or full CRLF) and restart
378
             * another "normal" match (possibly empty) from there.
379
             */
380
0
            if (val[0] == '\r' && val[1] == '\n') {
381
0
                val += 2;
382
0
            }
383
0
            else {
384
0
                val++;
385
0
            }
386
0
            empty = 0;
387
0
        }
388
0
        else {
389
0
            if (list) {
390
0
                APR_ARRAY_PUSH(list, const char*) = val;
391
0
            }
392
0
            else if (vb.avail) {
393
0
                ap_varbuf_strmemcat(&vb, val, strlen(val));
394
0
            }
395
0
            else {
396
0
                return val;
397
0
            }
398
0
            break;
399
0
        }
400
0
    } while (*val);
401
402
0
    return vb.buf;
403
0
}
404
405
static apr_array_header_t *ap_expr_list_make(ap_expr_eval_ctx_t *ctx,
406
                                             const ap_expr_t *node)
407
0
{
408
0
    apr_array_header_t *list = NULL;
409
410
0
    if (node->node_op == op_Split) {
411
0
        const ap_expr_t *arg = node->node_arg1;
412
0
        const ap_expr_t *reg = node->node_arg2;
413
0
        const apr_array_header_t *source = ap_expr_list_make(ctx, arg);
414
0
        int i;
415
416
0
        list = apr_array_make(ctx->p, source->nelts, sizeof(const char*));
417
0
        for (i = 0; i < source->nelts; ++i) {
418
0
            const char *val = APR_ARRAY_IDX(source, i, const char*);
419
0
            (void)ap_expr_regexec(val, reg, list, ctx);
420
0
        }
421
0
    }
422
0
    else if (node->node_op == op_ListElement) {
423
0
        int n = 0;
424
0
        const ap_expr_t *elem;
425
0
        for (elem = node; elem; elem = elem->node_arg2) {
426
0
            AP_DEBUG_ASSERT(elem->node_op == op_ListElement);
427
0
            n++;
428
0
        }
429
430
0
        list = apr_array_make(ctx->p, n, sizeof(const char*));
431
0
        for (elem = node; elem; elem = elem->node_arg2) {
432
0
            APR_ARRAY_PUSH(list, const char*) =
433
0
                ap_expr_eval_word(ctx, elem->node_arg1);
434
0
        }
435
0
    }
436
0
    else if (node->node_op == op_ListFuncCall) {
437
0
        const ap_expr_t *info = node->node_arg1;
438
0
        ap_expr_list_func_t *func = info->node_arg1;
439
440
0
        AP_DEBUG_ASSERT(func != NULL);
441
0
        AP_DEBUG_ASSERT(info->node_op == op_ListFuncInfo);
442
0
        list = (*func)(ctx, info->node_arg2,
443
0
                       ap_expr_eval_word(ctx, node->node_arg2));
444
0
    }
445
0
    else {
446
0
        list = apr_array_make(ctx->p, 1, sizeof(const char*));
447
0
        APR_ARRAY_PUSH(list, const char*) = ap_expr_eval_word(ctx, node);
448
0
    }
449
450
0
    return list;
451
0
}
452
453
static int ap_expr_eval_comp(ap_expr_eval_ctx_t *ctx, const ap_expr_t *node)
454
0
{
455
0
    const ap_expr_t *e1 = node->node_arg1;
456
0
    const ap_expr_t *e2 = node->node_arg2;
457
0
    switch (node->node_op) {
458
0
    case op_EQ:
459
0
        return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) == 0);
460
0
    case op_NE:
461
0
        return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) != 0);
462
0
    case op_LT:
463
0
        return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) <  0);
464
0
    case op_LE:
465
0
        return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) <= 0);
466
0
    case op_GT:
467
0
        return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) >  0);
468
0
    case op_GE:
469
0
        return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) >= 0);
470
0
    case op_STR_EQ:
471
0
        return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) == 0);
472
0
    case op_STR_NE:
473
0
        return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) != 0);
474
0
    case op_STR_LT:
475
0
        return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) <  0);
476
0
    case op_STR_LE:
477
0
        return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) <= 0);
478
0
    case op_STR_GT:
479
0
        return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) >  0);
480
0
    case op_STR_GE:
481
0
        return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) >= 0);
482
0
    case op_IN: {
483
0
            int n;
484
0
            const char *needle, *subject;
485
0
            apr_array_header_t *haystack;
486
0
            haystack = ap_expr_list_make(ctx, e2);
487
0
            if (haystack) {
488
0
                needle = ap_expr_eval_word(ctx, e1);
489
0
                for (n = 0; n < haystack->nelts; ++n) {
490
0
                    subject = APR_ARRAY_IDX(haystack, n, const char*);
491
0
                    if (strcmp(needle, subject) == 0) {
492
0
                        return 1;
493
0
                    }
494
0
                }
495
0
            }
496
0
            return 0;
497
0
        }
498
0
    case op_REG:
499
0
    case op_NRE: {
500
0
            const char *word = ap_expr_eval_word(ctx, e1);
501
0
            const ap_regex_t *regex = e2->node_arg1;
502
0
            int result;
503
504
            /*
505
             * $0 ... $9 may contain stuff the user wants to keep. Therefore
506
             * we only set them if there are capturing parens in the regex.
507
             */
508
0
            if (regex->re_nsub > 0) {
509
0
                result = (0 == ap_regexec(regex, word, ctx->re_nmatch,
510
0
                                          ctx->re_pmatch, 0));
511
0
                *ctx->re_source = result ? word : NULL;
512
0
                if (!result) {
513
                    /* ap_regexec() leaves the vector untouched on no
514
                     * match, don't let stale offsets linger
515
                     */
516
0
                    apr_size_t i;
517
0
                    for (i = 0; i < ctx->re_nmatch; ++i) {
518
0
                        ctx->re_pmatch[i].rm_so = ctx->re_pmatch[i].rm_eo = -1;
519
0
                    }
520
0
                }
521
0
            }
522
0
            else {
523
0
                result = (0 == ap_regexec(regex, word, 0, NULL, 0));
524
0
            }
525
526
0
            return result ^ (node->node_op == op_NRE);
527
0
        }
528
0
    default:
529
0
        *ctx->err = "Internal evaluation error: Unknown comp expression node";
530
0
        return -1;
531
0
    }
532
0
}
533
534
/* combined string/int comparison for compatibility with ssl_expr */
535
static int strcmplex(const char *str1, const char *str2)
536
0
{
537
0
    apr_size_t i, n1, n2;
538
539
0
    if (str1 == NULL)
540
0
        return -1;
541
0
    if (str2 == NULL)
542
0
        return +1;
543
0
    n1 = strlen(str1);
544
0
    n2 = strlen(str2);
545
0
    if (n1 > n2)
546
0
        return 1;
547
0
    if (n1 < n2)
548
0
        return -1;
549
0
    for (i = 0; i < n1; i++) {
550
0
        if (str1[i] > str2[i])
551
0
            return 1;
552
0
        if (str1[i] < str2[i])
553
0
            return -1;
554
0
    }
555
0
    return 0;
556
0
}
557
558
static int ssl_expr_eval_comp(ap_expr_eval_ctx_t *ctx, const ap_expr_t *node)
559
0
{
560
0
    const ap_expr_t *e1 = node->node_arg1;
561
0
    const ap_expr_t *e2 = node->node_arg2;
562
0
    switch (node->node_op) {
563
0
    case op_EQ:
564
0
    case op_STR_EQ:
565
0
        return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) == 0);
566
0
    case op_NE:
567
0
    case op_STR_NE:
568
0
        return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) != 0);
569
0
    case op_LT:
570
0
    case op_STR_LT:
571
0
        return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) <  0);
572
0
    case op_LE:
573
0
    case op_STR_LE:
574
0
        return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) <= 0);
575
0
    case op_GT:
576
0
    case op_STR_GT:
577
0
        return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) >  0);
578
0
    case op_GE:
579
0
    case op_STR_GE:
580
0
        return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) >= 0);
581
0
    default:
582
0
        return ap_expr_eval_comp(ctx, node);
583
0
    }
584
0
}
585
586
AP_DECLARE_NONSTD(int) ap_expr_lookup_default(ap_expr_lookup_parms *parms)
587
0
{
588
0
    return ap_run_expr_lookup(parms);
589
0
}
590
591
AP_DECLARE(const char *) ap_expr_parse(apr_pool_t *pool, apr_pool_t *ptemp,
592
                                       ap_expr_info_t *info, const char *expr,
593
                                       ap_expr_lookup_fn_t *lookup_fn)
594
1.49k
{
595
1.49k
    ap_expr_parse_ctx_t ctx;
596
1.49k
    int rc;
597
598
1.49k
    memset(&ctx, 0, sizeof ctx);
599
1.49k
    ctx.pool     = pool;
600
1.49k
    ctx.ptemp    = ptemp;
601
1.49k
    ctx.inputbuf = expr;
602
1.49k
    ctx.inputlen = strlen(expr);
603
1.49k
    ctx.inputptr = ctx.inputbuf;
604
1.49k
    ctx.flags    = info->flags;
605
1.49k
    ctx.lookup_fn   = lookup_fn ? lookup_fn : ap_expr_lookup_default;
606
1.49k
    ctx.at_start    = 1;
607
608
1.49k
    rc = ap_expr_yylex_init(&ctx.scanner);
609
1.49k
    if (rc)
610
0
        return "ap_expr_yylex_init error";
611
612
1.49k
    ap_expr_yyset_extra(&ctx, ctx.scanner);
613
1.49k
    rc = ap_expr_yyparse(&ctx);
614
1.49k
    ap_expr_yylex_destroy(ctx.scanner);
615
616
    /* ctx.error: the generic bison error message
617
     *            (XXX: usually not very useful, should be axed)
618
     * ctx.error2: an additional error message
619
     */
620
1.49k
    if (ctx.error) {
621
8
        if (ctx.error2)
622
8
            return apr_psprintf(pool, "%s: %s", ctx.error, ctx.error2);
623
0
        else
624
0
            return ctx.error;
625
8
    }
626
1.48k
    else if (ctx.error2) {
627
189
        return ctx.error2;
628
189
    }
629
630
1.29k
    if (rc) /* XXX can this happen? */
631
0
        return "syntax error";
632
633
#ifdef AP_EXPR_DEBUG
634
    if (ctx.expr)
635
        expr_dump_tree(ctx.expr, NULL, APLOG_NOTICE, 2);
636
#endif
637
638
1.29k
    info->root_node = ctx.expr;
639
640
1.29k
    return NULL;
641
1.29k
}
642
643
AP_DECLARE(ap_expr_info_t*) ap_expr_parse_cmd_mi(const cmd_parms *cmd,
644
                                                 const char *expr,
645
                                                 unsigned int flags,
646
                                                 const char **err,
647
                                                 ap_expr_lookup_fn_t *lookup_fn,
648
                                                 int module_index)
649
0
{
650
0
    ap_expr_info_t *info = apr_pcalloc(cmd->pool, sizeof(ap_expr_info_t));
651
0
    info->filename = cmd->directive->filename;
652
0
    info->line_number = cmd->directive->line_num;
653
0
    info->flags = flags;
654
0
    info->module_index = module_index;
655
656
    /* Use restricted-contents ap_expr() parser in htaccess context. */
657
0
    if (cmd->pool == cmd->temp_pool) {
658
0
        info->flags |= AP_EXPR_FLAG_RESTRICTED_FILE_FUNC;
659
0
    }
660
661
0
    *err = ap_expr_parse(cmd->pool, cmd->temp_pool, info, expr, lookup_fn);
662
663
0
    if (*err)
664
0
        return NULL;
665
666
0
    return info;
667
0
}
668
669
ap_expr_t *ap_expr_make(ap_expr_node_op_e op, const void *a1, const void *a2,
670
                        ap_expr_parse_ctx_t *ctx)
671
105k
{
672
105k
    ap_expr_t *node = apr_palloc(ctx->pool, sizeof(ap_expr_t));
673
105k
    node->node_op   = op;
674
105k
    node->node_arg1 = a1;
675
105k
    node->node_arg2 = a2;
676
105k
    return node;
677
105k
}
678
679
ap_expr_t *ap_expr_concat_make(const void *a1, const void *a2,
680
                               ap_expr_parse_ctx_t *ctx)
681
52.3k
{
682
52.3k
    const ap_expr_t *node;
683
684
    /* Optimize out empty string(s) concatenation */
685
52.3k
    if ((node = a1)
686
52.3k
            && node->node_op == op_String
687
154
            && !*(const char *)node->node_arg1) {
688
0
        return (ap_expr_t *)a2;
689
0
    }
690
52.3k
    if ((node = a2)
691
52.3k
            && node->node_op == op_String
692
25.9k
            && !*(const char *)node->node_arg1) {
693
101
        return (ap_expr_t *)a1;
694
101
    }
695
696
52.2k
    return ap_expr_make(op_Concat, a1, a2, ctx);
697
52.3k
}
698
699
ap_expr_t *ap_expr_regex_make(const char *pattern, const ap_expr_t *subst,
700
                              const char *flags, ap_expr_parse_ctx_t *ctx)
701
0
{
702
0
    ap_expr_t *node = NULL;
703
0
    ap_expr_regctx_t *regctx;
704
0
    ap_regex_t *regex;
705
706
0
    regctx = apr_pcalloc(ctx->pool, sizeof *regctx);
707
0
    regctx->subst = subst;
708
0
    if (flags) {
709
0
        for (; *flags; ++flags) {
710
0
            switch (*flags) {
711
0
            case 'i':
712
0
                regctx->flags |= AP_REG_ICASE;
713
0
                break;
714
0
            case 'm':
715
0
                regctx->flags |= AP_REG_NEWLINE;
716
0
                break;
717
0
            case 's':
718
0
                regctx->flags |= AP_REG_DOTALL;
719
0
                break;
720
0
            case 'g':
721
0
                regctx->flags |= AP_REG_MULTI;
722
0
                break;
723
0
            }
724
0
        }
725
0
    }
726
0
    regex = ap_pregcomp(ctx->pool, pattern, regctx->flags);
727
0
    if (!regex) {
728
0
        return NULL;
729
0
    }
730
731
0
    node = apr_palloc(ctx->pool, sizeof(ap_expr_t));
732
0
    node->node_op   = op_Regex;
733
0
    node->node_arg1 = regex;
734
0
    node->node_arg2 = regctx;
735
0
    return node;
736
0
}
737
738
static ap_expr_t *ap_expr_info_make(int type, const char *name,
739
                                  ap_expr_parse_ctx_t *ctx,
740
                                  const ap_expr_t *arg)
741
0
{
742
0
    ap_expr_t *info = apr_palloc(ctx->pool, sizeof(ap_expr_t));
743
0
    ap_expr_lookup_parms parms;
744
0
    parms.type  = type;
745
0
    parms.flags = ctx->flags;
746
0
    parms.pool  = ctx->pool;
747
0
    parms.ptemp = ctx->ptemp;
748
0
    parms.name  = name;
749
0
    parms.func  = &info->node_arg1;
750
0
    parms.data  = &info->node_arg2;
751
0
    parms.err   = &ctx->error2;
752
0
    parms.arg   = NULL;
753
0
    if (arg) {
754
0
        switch(arg->node_op) {
755
0
            case op_String:
756
0
                parms.arg = arg->node_arg1;
757
0
                break;
758
0
            case op_ListElement:
759
                /* save the first literal/simple string argument */
760
0
                do {
761
0
                    const ap_expr_t *val = arg->node_arg1;
762
0
                    if (val && val->node_op == op_String) {
763
0
                        parms.arg = val->node_arg1;
764
0
                        break;
765
0
                    }
766
0
                    arg = arg->node_arg2;
767
0
                } while (arg != NULL);
768
0
                break;
769
0
            default:
770
0
                break;
771
0
        }
772
0
    }
773
0
    if (ctx->lookup_fn(&parms) != OK)
774
0
        return NULL;
775
0
    return info;
776
0
}
777
778
ap_expr_t *ap_expr_str_func_make(const char *name, const ap_expr_t *arg,
779
                               ap_expr_parse_ctx_t *ctx)
780
0
{
781
0
    ap_expr_t *info = ap_expr_info_make(AP_EXPR_FUNC_STRING, name, ctx, arg);
782
0
    if (!info)
783
0
        return NULL;
784
785
0
    info->node_op = op_StringFuncInfo;
786
0
    return ap_expr_make(op_StringFuncCall, info, arg, ctx);
787
0
}
788
789
ap_expr_t *ap_expr_list_func_make(const char *name, const ap_expr_t *arg,
790
                                ap_expr_parse_ctx_t *ctx)
791
0
{
792
0
    ap_expr_t *info = ap_expr_info_make(AP_EXPR_FUNC_LIST, name, ctx, arg);
793
0
    if (!info)
794
0
        return NULL;
795
796
0
    info->node_op = op_ListFuncInfo;
797
0
    return ap_expr_make(op_ListFuncCall, info, arg, ctx);
798
0
}
799
800
ap_expr_t *ap_expr_unary_op_make(const char *name, const ap_expr_t *arg,
801
                               ap_expr_parse_ctx_t *ctx)
802
0
{
803
0
    ap_expr_t *info = ap_expr_info_make(AP_EXPR_FUNC_OP_UNARY, name, ctx, arg);
804
0
    if (!info)
805
0
        return NULL;
806
807
0
    info->node_op = op_UnaryOpInfo;
808
0
    return ap_expr_make(op_UnaryOpCall, info, arg, ctx);
809
0
}
810
811
ap_expr_t *ap_expr_binary_op_make(const char *name, const ap_expr_t *arg1,
812
                                const ap_expr_t *arg2, ap_expr_parse_ctx_t *ctx)
813
0
{
814
0
    ap_expr_t *args;
815
0
    ap_expr_t *info = ap_expr_info_make(AP_EXPR_FUNC_OP_BINARY, name, ctx,
816
0
                                        arg2);
817
0
    if (!info)
818
0
        return NULL;
819
820
0
    info->node_op = op_BinaryOpInfo;
821
0
    args = ap_expr_make(op_BinaryOpArgs, arg1, arg2, ctx);
822
0
    return ap_expr_make(op_BinaryOpCall, info, args, ctx);
823
0
}
824
825
826
ap_expr_t *ap_expr_var_make(const char *name, ap_expr_parse_ctx_t *ctx)
827
0
{
828
0
    ap_expr_t *node = ap_expr_info_make(AP_EXPR_FUNC_VAR, name, ctx, NULL);
829
0
    if (!node)
830
0
        return NULL;
831
832
0
    node->node_op = op_Var;
833
0
    return node;
834
0
}
835
836
ap_expr_t *ap_expr_backref_make(int num, ap_expr_parse_ctx_t *ctx)
837
26.4k
{
838
26.4k
    int *n = apr_pmemdup(ctx->pool, &num, sizeof(num));
839
26.4k
    return ap_expr_make(op_Backref, n, NULL, ctx);
840
26.4k
}
841
842
#ifdef AP_EXPR_DEBUG
843
844
#define MARK                        APLOG_MARK,loglevel,0,s
845
#define DUMP_E_E(op, e1, e2)                                                \
846
    do { ap_log_error(MARK,"%*s%s: %pp %pp", indent, " ", op, e1, e2);      \
847
         if (e1) expr_dump_tree(e1, s, loglevel, indent + 2);               \
848
         if (e2) expr_dump_tree(e2, s, loglevel, indent + 2);               \
849
    } while (0)
850
#define DUMP_S_E(op, s1, e1)                                                    \
851
    do { ap_log_error(MARK,"%*s%s: '%s' %pp", indent, " ", op, (char *)s1, e1); \
852
         if (e1) expr_dump_tree(e1, s, loglevel, indent + 2);                   \
853
    } while (0)
854
#define DUMP_S_P(op, s1, p1)                                                \
855
    ap_log_error(MARK,"%*s%s: '%s' %pp", indent, " ", op, (char *)s1, p1);
856
#define DUMP_P_P(op, p1, p2)                                                \
857
    ap_log_error(MARK,"%*s%s: %pp %pp", indent, " ", op, p1, p2);
858
#define DUMP_S_S(op, s1, s2)                                                       \
859
    ap_log_error(MARK,"%*s%s: '%s' '%s'", indent, " ", op, (char *)s1, (char *)s2)
860
#define DUMP_P(op, p1)                                                      \
861
    ap_log_error(MARK,"%*s%s: %pp", indent, " ", op, p1);
862
#define DUMP_IP(op, p1)                                                     \
863
    ap_log_error(MARK,"%*s%s: %d", indent, " ", op, *(int *)p1);
864
#define DUMP_S(op, s1)                                                      \
865
    ap_log_error(MARK,"%*s%s: '%s'", indent, " ", op, (char *)s1)
866
867
#define CASE_OP(op)                  case op: name = #op ; break;
868
869
static void expr_dump_tree(const ap_expr_t *e, const server_rec *s,
870
                           int loglevel, int indent)
871
{
872
    switch (e->node_op) {
873
    /* no arg */
874
    case op_NOP:
875
    case op_True:
876
    case op_False:
877
        {
878
            char *name;
879
            switch (e->node_op) {
880
            CASE_OP(op_NOP);
881
            CASE_OP(op_True);
882
            CASE_OP(op_False);
883
            default:
884
                ap_assert(0);
885
            }
886
            ap_log_error(MARK, "%*s%s", indent, " ", name);
887
        }
888
        break;
889
890
    /* arg1: string, arg2: expr */
891
    case op_UnaryOpCall:
892
    case op_BinaryOpCall:
893
    case op_BinaryOpArgs:
894
        {
895
            char *name;
896
            switch (e->node_op) {
897
            CASE_OP(op_BinaryOpCall);
898
            CASE_OP(op_UnaryOpCall);
899
            CASE_OP(op_BinaryOpArgs);
900
            default:
901
                ap_assert(0);
902
            }
903
            DUMP_S_E(name, e->node_arg1, e->node_arg2);
904
        }
905
        break;
906
907
    /* arg1: expr, arg2: expr */
908
    case op_Comp:
909
    case op_Not:
910
    case op_Or:
911
    case op_And:
912
    case op_EQ:
913
    case op_NE:
914
    case op_LT:
915
    case op_LE:
916
    case op_GT:
917
    case op_GE:
918
    case op_STR_EQ:
919
    case op_STR_NE:
920
    case op_STR_LT:
921
    case op_STR_LE:
922
    case op_STR_GT:
923
    case op_STR_GE:
924
    case op_IN:
925
    case op_REG:
926
    case op_NRE:
927
    case op_Word:
928
    case op_Bool:
929
    case op_Sub:
930
    case op_Join:
931
    case op_Split:
932
    case op_Concat:
933
    case op_StringFuncCall:
934
    case op_ListFuncCall:
935
    case op_ListElement:
936
        {
937
            char *name;
938
            switch (e->node_op) {
939
            CASE_OP(op_Comp);
940
            CASE_OP(op_Not);
941
            CASE_OP(op_Or);
942
            CASE_OP(op_And);
943
            CASE_OP(op_EQ);
944
            CASE_OP(op_NE);
945
            CASE_OP(op_LT);
946
            CASE_OP(op_LE);
947
            CASE_OP(op_GT);
948
            CASE_OP(op_GE);
949
            CASE_OP(op_STR_EQ);
950
            CASE_OP(op_STR_NE);
951
            CASE_OP(op_STR_LT);
952
            CASE_OP(op_STR_LE);
953
            CASE_OP(op_STR_GT);
954
            CASE_OP(op_STR_GE);
955
            CASE_OP(op_IN);
956
            CASE_OP(op_REG);
957
            CASE_OP(op_NRE);
958
            CASE_OP(op_Word);
959
            CASE_OP(op_Bool);
960
            CASE_OP(op_Sub);
961
            CASE_OP(op_Join);
962
            CASE_OP(op_Split);
963
            CASE_OP(op_Concat);
964
            CASE_OP(op_StringFuncCall);
965
            CASE_OP(op_ListFuncCall);
966
            CASE_OP(op_ListElement);
967
            default:
968
                ap_assert(0);
969
            }
970
            DUMP_E_E(name, e->node_arg1, e->node_arg2);
971
        }
972
        break;
973
    /* arg1: string */
974
    case op_Digit:
975
    case op_String:
976
        {
977
            char *name;
978
            switch (e->node_op) {
979
            CASE_OP(op_Digit);
980
            CASE_OP(op_String);
981
            default:
982
                ap_assert(0);
983
            }
984
            DUMP_S(name, e->node_arg1);
985
        }
986
        break;
987
    /* arg1: pointer, arg2: pointer */
988
    case op_Var:
989
    case op_StringFuncInfo:
990
    case op_UnaryOpInfo:
991
    case op_BinaryOpInfo:
992
    case op_ListFuncInfo:
993
        {
994
            char *name;
995
            switch (e->node_op) {
996
            CASE_OP(op_Var);
997
            CASE_OP(op_StringFuncInfo);
998
            CASE_OP(op_UnaryOpInfo);
999
            CASE_OP(op_BinaryOpInfo);
1000
            CASE_OP(op_ListFuncInfo);
1001
            default:
1002
                ap_assert(0);
1003
            }
1004
            DUMP_P_P(name, e->node_arg1, e->node_arg2);
1005
        }
1006
        break;
1007
    /* arg1: pointer */
1008
    case op_Regex:
1009
        DUMP_P("op_Regex", e->node_arg1);
1010
        break;
1011
    /* arg1: pointer to int */
1012
    case op_Backref:
1013
        DUMP_IP("op_Backref", e->node_arg1);
1014
        break;
1015
    default:
1016
        ap_log_error(MARK, "%*sERROR: INVALID OP %d", indent, " ", e->node_op);
1017
        break;
1018
    }
1019
}
1020
#endif /* AP_EXPR_DEBUG */
1021
1022
0
#define expr_eval_log(ctx, level, ...) do { \
1023
0
    ap_expr_eval_ctx_t *x = (ctx); \
1024
0
    if (x->r) { \
1025
0
        ap_log_rerror(LOG_MARK(x->info), (level), 0, x->r, __VA_ARGS__); \
1026
0
    } \
1027
0
    else if (x->c) { \
1028
0
        ap_log_cerror(LOG_MARK(x->info), (level), 0, x->c, __VA_ARGS__); \
1029
0
    } \
1030
0
    else { \
1031
0
        ap_log_error(LOG_MARK(x->info), (level), 0, x->s, __VA_ARGS__); \
1032
0
    } \
1033
0
} while (0)
1034
1035
static int ap_expr_eval_unary_op(ap_expr_eval_ctx_t *ctx, const ap_expr_t *info,
1036
                                 const ap_expr_t *arg)
1037
0
{
1038
0
    ap_expr_op_unary_t *op_func = (ap_expr_op_unary_t *)info->node_arg1;
1039
0
    const void *data = info->node_arg2;
1040
1041
0
    AP_DEBUG_ASSERT(info->node_op == op_UnaryOpInfo);
1042
0
    AP_DEBUG_ASSERT(op_func != NULL);
1043
0
    AP_DEBUG_ASSERT(data != NULL);
1044
0
    return (*op_func)(ctx, data, ap_expr_eval_word(ctx, arg));
1045
0
}
1046
1047
static int ap_expr_eval_binary_op(ap_expr_eval_ctx_t *ctx,
1048
                                  const ap_expr_t *info,
1049
                                  const ap_expr_t *args)
1050
0
{
1051
0
    ap_expr_op_binary_t *op_func = (ap_expr_op_binary_t *)info->node_arg1;
1052
0
    const void *data = info->node_arg2;
1053
0
    const ap_expr_t *a1 = args->node_arg1;
1054
0
    const ap_expr_t *a2 = args->node_arg2;
1055
1056
0
    AP_DEBUG_ASSERT(info->node_op == op_BinaryOpInfo);
1057
0
    AP_DEBUG_ASSERT(args->node_op == op_BinaryOpArgs);
1058
0
    AP_DEBUG_ASSERT(op_func != NULL);
1059
0
    AP_DEBUG_ASSERT(data != NULL);
1060
0
    return (*op_func)(ctx, data, ap_expr_eval_word(ctx, a1),
1061
0
                      ap_expr_eval_word(ctx, a2));
1062
0
}
1063
1064
1065
static int ap_expr_eval_cond(ap_expr_eval_ctx_t *ctx, const ap_expr_t *node)
1066
0
{
1067
0
    const ap_expr_t *e1 = node->node_arg1;
1068
0
    const ap_expr_t *e2 = node->node_arg2;
1069
0
    int result = FALSE;
1070
0
    if (inc_rec(ctx))
1071
0
        return result;
1072
0
    while (1) {
1073
0
        switch (node->node_op) {
1074
0
        case op_True:
1075
0
            result ^= TRUE;
1076
0
            goto out;
1077
0
        case op_False:
1078
0
            result ^= FALSE;
1079
0
            goto out;
1080
0
        case op_Not:
1081
0
            result = !result;
1082
0
            node = e1;
1083
0
            break;
1084
0
        case op_Or:
1085
0
            do {
1086
0
                if (e1->node_op == op_Not) {
1087
0
                    if (!ap_expr_eval_cond(ctx, e1->node_arg1)) {
1088
0
                        result ^= TRUE;
1089
0
                        goto out;
1090
0
                    }
1091
0
                }
1092
0
                else {
1093
0
                    if (ap_expr_eval_cond(ctx, e1)) {
1094
0
                        result ^= TRUE;
1095
0
                        goto out;
1096
0
                    }
1097
0
                }
1098
0
                node = node->node_arg2;
1099
0
                e1 = node->node_arg1;
1100
0
            } while (node->node_op == op_Or);
1101
0
            break;
1102
0
        case op_And:
1103
0
            do {
1104
0
                if (e1->node_op == op_Not) {
1105
0
                    if (ap_expr_eval_cond(ctx, e1->node_arg1)) {
1106
0
                        result ^= FALSE;
1107
0
                        goto out;
1108
0
                    }
1109
0
                }
1110
0
                else {
1111
0
                    if (!ap_expr_eval_cond(ctx, e1)) {
1112
0
                        result ^= FALSE;
1113
0
                        goto out;
1114
0
                    }
1115
0
                }
1116
0
                node = node->node_arg2;
1117
0
                e1 = node->node_arg1;
1118
0
            } while (node->node_op == op_And);
1119
0
            break;
1120
0
        case op_UnaryOpCall:
1121
0
            result ^= ap_expr_eval_unary_op(ctx, e1, e2);
1122
0
            goto out;
1123
0
        case op_BinaryOpCall:
1124
0
            result ^= ap_expr_eval_binary_op(ctx, e1, e2);
1125
0
            goto out;
1126
0
        case op_Comp:
1127
0
            if (ctx->info->flags & AP_EXPR_FLAG_SSL_EXPR_COMPAT)
1128
0
                result ^= ssl_expr_eval_comp(ctx, e1);
1129
0
            else
1130
0
                result ^= ap_expr_eval_comp(ctx, e1);
1131
0
            goto out;
1132
0
        default:
1133
0
            *ctx->err = "Internal evaluation error: Unknown expression node";
1134
0
            goto out;
1135
0
        }
1136
0
        e1 = node->node_arg1;
1137
0
        e2 = node->node_arg2;
1138
0
    }
1139
0
out:
1140
0
    ctx->reclvl--;
1141
0
    return result;
1142
0
}
1143
1144
AP_DECLARE(int) ap_expr_exec(request_rec *r, const ap_expr_info_t *info,
1145
                             const char **err)
1146
0
{
1147
0
    return ap_expr_exec_re(r, info, 0, NULL, NULL, err);
1148
0
}
1149
1150
AP_DECLARE(int) ap_expr_exec_ctx(ap_expr_eval_ctx_t *ctx)
1151
0
{
1152
0
    int rc;
1153
1154
0
    AP_DEBUG_ASSERT(ctx->p != NULL);
1155
0
    AP_DEBUG_ASSERT(ctx->err != NULL);
1156
0
    AP_DEBUG_ASSERT(ctx->info != NULL);
1157
0
    if (ctx->re_pmatch) {
1158
0
        AP_DEBUG_ASSERT(ctx->re_source != NULL);
1159
0
        AP_DEBUG_ASSERT(ctx->re_nmatch > 0);
1160
0
    }
1161
0
    if (!ctx->s) {
1162
0
        if (ctx->r) {
1163
0
            ctx->s = ctx->r->server;
1164
0
        }
1165
0
        else if (ctx->c) {
1166
0
            ctx->s = ctx->c->base_server;
1167
0
        }
1168
0
    }
1169
0
    if (!ctx->c) {
1170
0
        if (ctx->r) {
1171
0
            ctx->c = ctx->r->connection;
1172
0
        }
1173
0
    }
1174
0
    AP_DEBUG_ASSERT(ctx->s != NULL);
1175
1176
0
    ctx->reclvl = 0;
1177
0
    *ctx->err = NULL;
1178
0
    if (ctx->info->flags & AP_EXPR_FLAG_STRING_RESULT) {
1179
0
        *ctx->result_string = ap_expr_eval_word(ctx, ctx->info->root_node);
1180
0
        if (*ctx->err != NULL) {
1181
0
            expr_eval_log(ctx, APLOG_ERR, APLOGNO(03298)
1182
0
                          "Evaluation of string expression from %s:%d failed: %s",
1183
0
                          ctx->info->filename, ctx->info->line_number, *ctx->err);
1184
0
            return -1;
1185
0
        } else {
1186
0
            expr_eval_log(ctx, APLOG_TRACE4,
1187
0
                          "Evaluation of string expression from %s:%d gave: %s",
1188
0
                          ctx->info->filename, ctx->info->line_number,
1189
0
                          *ctx->result_string);
1190
0
            return 1;
1191
0
        }
1192
0
    }
1193
0
    else {
1194
0
        rc = ap_expr_eval_cond(ctx, ctx->info->root_node);
1195
0
        if (*ctx->err != NULL) {
1196
0
            expr_eval_log(ctx, APLOG_ERR, APLOGNO(03299)
1197
0
                          "Evaluation of expression from %s:%d failed: %s",
1198
0
                          ctx->info->filename, ctx->info->line_number, *ctx->err);
1199
0
            return -1;
1200
0
        } else {
1201
0
            rc = rc ? 1 : 0;
1202
0
            expr_eval_log(ctx, APLOG_TRACE4,
1203
0
                          "Evaluation of expression from %s:%d gave: %d",
1204
0
                          ctx->info->filename, ctx->info->line_number, rc);
1205
1206
0
            if (ctx->r && ctx->vary_this && *ctx->vary_this)
1207
0
                apr_table_merge(ctx->r->headers_out, "Vary", *ctx->vary_this);
1208
1209
0
            return rc;
1210
0
        }
1211
0
    }
1212
0
}
1213
1214
AP_DECLARE(int) ap_expr_exec_re(request_rec *r, const ap_expr_info_t *info,
1215
                                apr_size_t nmatch, ap_regmatch_t *pmatch,
1216
                                const char **source, const char **err)
1217
0
{
1218
0
    ap_expr_eval_ctx_t ctx;
1219
0
    int dont_vary = (info->flags & AP_EXPR_FLAG_DONT_VARY);
1220
0
    const char *tmp_source = NULL, *vary_this = NULL;
1221
0
    ap_regmatch_t tmp_pmatch[AP_MAX_REG_MATCH];
1222
1223
0
    AP_DEBUG_ASSERT((info->flags & AP_EXPR_FLAG_STRING_RESULT) == 0);
1224
1225
0
    ctx.r = r;
1226
0
    ctx.c = r->connection;
1227
0
    ctx.s = r->server;
1228
0
    ctx.p = r->pool;
1229
0
    ctx.err  = err;
1230
0
    ctx.info = info;
1231
0
    ctx.re_nmatch = nmatch;
1232
0
    ctx.re_pmatch = pmatch;
1233
0
    ctx.re_source = source;
1234
0
    ctx.vary_this = dont_vary ? NULL : &vary_this;
1235
0
    ctx.data = NULL;
1236
1237
0
    if (!pmatch) {
1238
0
        ctx.re_nmatch = AP_MAX_REG_MATCH;
1239
0
        ctx.re_pmatch = tmp_pmatch;
1240
0
        ctx.re_source = &tmp_source;
1241
0
    }
1242
1243
0
    return ap_expr_exec_ctx(&ctx);
1244
0
}
1245
1246
AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r,
1247
                                             const ap_expr_info_t *info,
1248
                                             apr_size_t nmatch,
1249
                                             ap_regmatch_t *pmatch,
1250
                                             const char **source,
1251
                                             const char **err)
1252
0
{
1253
0
    ap_expr_eval_ctx_t ctx;
1254
0
    int dont_vary, rc;
1255
0
    const char *tmp_source, *vary_this;
1256
0
    ap_regmatch_t tmp_pmatch[AP_MAX_REG_MATCH];
1257
0
    const char *result;
1258
1259
0
    AP_DEBUG_ASSERT(info->flags & AP_EXPR_FLAG_STRING_RESULT);
1260
1261
0
    if (info->root_node->node_op == op_String) {
1262
        /* short-cut for constant strings */
1263
0
        *err = NULL;
1264
0
        return (const char *)info->root_node->node_arg1;
1265
0
    }
1266
1267
0
    tmp_source = NULL;
1268
0
    vary_this = NULL;
1269
1270
0
    dont_vary = (info->flags & AP_EXPR_FLAG_DONT_VARY);
1271
1272
0
    ctx.r = r;
1273
0
    ctx.c = r->connection;
1274
0
    ctx.s = r->server;
1275
0
    ctx.p = r->pool;
1276
0
    ctx.err  = err;
1277
0
    ctx.info = info;
1278
0
    ctx.re_nmatch = nmatch;
1279
0
    ctx.re_pmatch = pmatch;
1280
0
    ctx.re_source = source;
1281
0
    ctx.vary_this = dont_vary ? NULL : &vary_this;
1282
0
    ctx.data = NULL;
1283
0
    ctx.result_string = &result;
1284
1285
0
    if (!pmatch) {
1286
0
        ctx.re_nmatch = AP_MAX_REG_MATCH;
1287
0
        ctx.re_pmatch = tmp_pmatch;
1288
0
        ctx.re_source = &tmp_source;
1289
0
    }
1290
1291
0
    rc = ap_expr_exec_ctx(&ctx);
1292
0
    if (rc > 0)
1293
0
        return result;
1294
0
    else if (rc < 0)
1295
0
        return NULL;
1296
0
    else
1297
0
        ap_assert(0);
1298
    /* Not reached */
1299
0
    return NULL;
1300
0
}
1301
1302
AP_DECLARE(const char *) ap_expr_str_exec(request_rec *r,
1303
                                          const ap_expr_info_t *info,
1304
                                          const char **err)
1305
0
{
1306
0
    return ap_expr_str_exec_re(r, info, 0, NULL, NULL, err);
1307
0
}
1308
1309
1310
static void add_vary(ap_expr_eval_ctx_t *ctx, const char *name)
1311
0
{
1312
0
    if (!ctx->vary_this)
1313
0
        return;
1314
1315
0
    if (*ctx->vary_this) {
1316
0
        *ctx->vary_this = apr_pstrcat(ctx->p, *ctx->vary_this, ", ", name,
1317
0
                                      NULL);
1318
0
    }
1319
0
    else {
1320
0
        *ctx->vary_this = name;
1321
0
    }
1322
0
}
1323
1324
static const char *req_table_func(ap_expr_eval_ctx_t *ctx, const void *data,
1325
                                  const char *arg)
1326
0
{
1327
0
    const char *name = (const char *)data;
1328
0
    apr_table_t *t;
1329
0
    if (!ctx->r)
1330
0
        return "";
1331
1332
0
    if (name[2] == 's') {           /* resp */
1333
        /* Try r->headers_out first, fall back on err_headers_out. */
1334
0
        const char *v = apr_table_get(ctx->r->headers_out, arg);
1335
0
        if (v) {
1336
0
            return v;
1337
0
        }
1338
0
        t = ctx->r->err_headers_out;
1339
0
    }
1340
0
    else if (name[0] == 'n')        /* notes */
1341
0
        t = ctx->r->notes;
1342
0
    else if (name[3] == 'e')        /* reqenv */
1343
0
        t = ctx->r->subprocess_env;
1344
0
    else if (name[3] == '_')        /* req_novary */
1345
0
        t = ctx->r->headers_in;
1346
0
    else {                          /* req, http */
1347
0
        t = ctx->r->headers_in;
1348
        /* Skip the 'Vary: Host' header combination
1349
         * as indicated in rfc7231 section-7.1.4
1350
         */
1351
0
        if (strcasecmp(arg, "Host")){
1352
0
            add_vary(ctx, arg);
1353
0
        }
1354
0
    }
1355
0
    return apr_table_get(t, arg);
1356
0
}
1357
1358
static const char *env_func(ap_expr_eval_ctx_t *ctx, const void *data,
1359
                            const char *arg)
1360
0
{
1361
0
    const char *res;
1362
    /* this order is for ssl_expr compatibility */
1363
0
    if (ctx->r) {
1364
0
        if ((res = apr_table_get(ctx->r->notes, arg)) != NULL)
1365
0
            return res;
1366
0
        else if ((res = apr_table_get(ctx->r->subprocess_env, arg)) != NULL)
1367
0
            return res;
1368
0
    }
1369
0
    return getenv(arg);
1370
0
}
1371
1372
static const char *osenv_func(ap_expr_eval_ctx_t *ctx, const void *data,
1373
                              const char *arg)
1374
0
{
1375
0
    return getenv(arg);
1376
0
}
1377
1378
static const char *tolower_func(ap_expr_eval_ctx_t *ctx, const void *data,
1379
                                const char *arg)
1380
0
{
1381
0
    char *result = apr_pstrdup(ctx->p, arg);
1382
0
    ap_str_tolower(result);
1383
0
    return result;
1384
0
}
1385
1386
static const char *toupper_func(ap_expr_eval_ctx_t *ctx, const void *data,
1387
                                const char *arg)
1388
0
{
1389
0
    char *result = apr_pstrdup(ctx->p, arg);
1390
0
    ap_str_toupper(result);
1391
0
    return result;
1392
0
}
1393
1394
static const char *escape_func(ap_expr_eval_ctx_t *ctx, const void *data,
1395
                               const char *arg)
1396
0
{
1397
0
    return ap_escape_uri(ctx->p, arg);
1398
0
}
1399
1400
static const char *base64_func(ap_expr_eval_ctx_t *ctx, const void *data,
1401
                               const char *arg)
1402
0
{
1403
0
    return ap_pbase64encode(ctx->p, (char *)arg);
1404
0
}
1405
1406
static const char *unbase64_func(ap_expr_eval_ctx_t *ctx, const void *data,
1407
                               const char *arg)
1408
0
{
1409
0
    return ap_pbase64decode(ctx->p, arg);
1410
0
}
1411
1412
static const char *sha1_func(ap_expr_eval_ctx_t *ctx, const void *data,
1413
                               const char *arg)
1414
0
{
1415
0
    apr_sha1_ctx_t context;
1416
0
    apr_byte_t sha1[APR_SHA1_DIGESTSIZE];
1417
0
    char *out;
1418
1419
0
    out = apr_palloc(ctx->p, APR_SHA1_DIGESTSIZE*2+1);
1420
1421
0
    apr_sha1_init(&context);
1422
0
    apr_sha1_update(&context, arg, (unsigned int)strlen(arg));
1423
0
    apr_sha1_final(sha1, &context);
1424
1425
0
    ap_bin2hex(sha1, APR_SHA1_DIGESTSIZE, out);
1426
1427
0
    return out;
1428
0
}
1429
1430
static const char *md5_func(ap_expr_eval_ctx_t *ctx, const void *data,
1431
                               const char *arg)
1432
0
{
1433
0
    return ap_md5(ctx->p, (const unsigned char *)arg);
1434
0
}
1435
1436
#if APR_VERSION_AT_LEAST(1,6,0)
1437
static const char *ldap_func(ap_expr_eval_ctx_t *ctx, const void *data,
1438
                               const char *arg)
1439
0
{
1440
0
    return apr_pescape_ldap(ctx->p, arg, APR_ESCAPE_STRING, APR_ESCAPE_LDAP_ALL);
1441
0
}
1442
#endif
1443
1444
static const char *escapehtml_func(ap_expr_eval_ctx_t *ctx, const void *data,
1445
                                   const char *arg)
1446
0
{
1447
0
    return ap_escape_html(ctx->p, arg);
1448
0
}
1449
1450
static int replace_func_parse_arg(ap_expr_lookup_parms *parms)
1451
0
{
1452
0
    const char *original = parms->arg;
1453
0
    const apr_strmatch_pattern *pattern;
1454
1455
0
    if (!parms->arg) {
1456
0
        *parms->err = apr_psprintf(parms->ptemp, "replace() function needs an argument");
1457
0
        return !OK;
1458
0
    }
1459
1460
0
    pattern = apr_strmatch_precompile(parms->pool, original, 0);
1461
0
    *parms->data = pattern;
1462
0
    return OK;
1463
0
}
1464
1465
static const char *replace_func(ap_expr_eval_ctx_t *ctx, const void *data,
1466
                               const apr_array_header_t *args)
1467
0
{
1468
0
    char *buff, *original, *replacement;
1469
0
    struct ap_varbuf vb;
1470
0
    apr_size_t repl_len, orig_len;
1471
0
    const char *repl;
1472
0
    apr_size_t bytes;
1473
0
    apr_size_t len;
1474
0
    const apr_strmatch_pattern *pattern = data;
1475
0
    if (args->nelts != 3) {
1476
0
        *ctx->err = apr_psprintf(ctx->p, "replace() function needs "
1477
0
                                 "exactly 3 arguments, got %d", args->nelts);
1478
0
        return "";
1479
0
    }
1480
1481
0
    buff = APR_ARRAY_IDX(args, 0, char *);
1482
0
    original = APR_ARRAY_IDX(args, 1, char *);
1483
0
    replacement = APR_ARRAY_IDX(args, 2, char *);
1484
0
    repl_len = strlen(replacement);
1485
0
    orig_len = strlen(original);
1486
0
    bytes = strlen(buff);
1487
1488
0
    ap_varbuf_init(ctx->p, &vb, 0);
1489
0
    vb.strlen = 0;
1490
    
1491
0
    while ((repl = apr_strmatch(pattern, buff, bytes))) {
1492
0
        len = (apr_size_t) (repl - buff);
1493
0
        ap_varbuf_strmemcat(&vb, buff, len);
1494
0
        ap_varbuf_strmemcat(&vb, replacement, repl_len);
1495
1496
0
        len += orig_len;
1497
0
        bytes -= len;
1498
0
        buff += len;
1499
0
    }
1500
1501
0
    return ap_varbuf_pdup(ctx->p, &vb, NULL, 0, buff, bytes, &len);
1502
0
}
1503
1504
0
#define MAX_FILE_SIZE 10*1024*1024
1505
static const char *file_func(ap_expr_eval_ctx_t *ctx, const void *data,
1506
                             char *arg)
1507
0
{
1508
0
    apr_file_t *fp;
1509
0
    char *buf;
1510
0
    apr_off_t offset;
1511
0
    apr_size_t len;
1512
0
    apr_finfo_t finfo;
1513
1514
0
    if (apr_file_open(&fp, arg, APR_READ|APR_BUFFERED,
1515
0
                      APR_OS_DEFAULT, ctx->p) != APR_SUCCESS) {
1516
0
        *ctx->err = apr_psprintf(ctx->p, "Cannot open file %s", arg);
1517
0
        return "";
1518
0
    }
1519
0
    apr_file_info_get(&finfo, APR_FINFO_SIZE, fp);
1520
0
    if (finfo.size > MAX_FILE_SIZE) {
1521
0
        *ctx->err = apr_psprintf(ctx->p, "File %s too large", arg);
1522
0
        apr_file_close(fp);
1523
0
        return "";
1524
0
    }
1525
0
    len = (apr_size_t)finfo.size;
1526
0
    if (len == 0) {
1527
0
        apr_file_close(fp);
1528
0
        return "";
1529
0
    }
1530
0
    else {
1531
0
        if ((buf = (char *)apr_palloc(ctx->p, sizeof(char)*(len+1))) == NULL) {
1532
0
            *ctx->err = "Cannot allocate memory";
1533
0
            apr_file_close(fp);
1534
0
            return "";
1535
0
        }
1536
0
        offset = 0;
1537
0
        apr_file_seek(fp, APR_SET, &offset);
1538
0
        if (apr_file_read(fp, buf, &len) != APR_SUCCESS) {
1539
0
            *ctx->err = apr_psprintf(ctx->p, "Cannot read from file %s", arg);
1540
0
            apr_file_close(fp);
1541
0
            return "";
1542
0
        }
1543
0
        buf[len] = '\0';
1544
0
    }
1545
0
    apr_file_close(fp);
1546
0
    return buf;
1547
0
}
1548
1549
static apr_status_t stat_check(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1550
0
{
1551
0
    apr_status_t rv = APR_SUCCESS;
1552
0
    if (APR_SUCCESS != (rv = ap_stat_check(arg, ctx->p))) {
1553
0
        *ctx->err = apr_psprintf(ctx->p, "stat of %s not allowed", arg);
1554
0
    }
1555
0
    return rv;
1556
0
}
1557
static const char *filesize_func(ap_expr_eval_ctx_t *ctx, const void *data,
1558
                                  char *arg)
1559
0
{
1560
0
    apr_finfo_t sb;
1561
0
    if (APR_SUCCESS != stat_check(ctx, data, arg)) {
1562
0
        return "";
1563
0
    }
1564
0
    if (apr_stat(&sb, arg, APR_FINFO_MIN, ctx->p) == APR_SUCCESS
1565
0
        && sb.filetype == APR_REG && sb.size > 0)
1566
0
        return apr_psprintf(ctx->p, "%" APR_OFF_T_FMT, sb.size);
1567
0
    else
1568
0
        return "0";
1569
0
}
1570
1571
static const char *filemod_func(ap_expr_eval_ctx_t *ctx, const void *data,
1572
                                  char *arg)
1573
0
{
1574
0
    apr_finfo_t sb;
1575
0
    if (APR_SUCCESS != stat_check(ctx, data, arg)) {
1576
0
        return "";
1577
0
    }
1578
0
    if (apr_stat(&sb, arg, APR_FINFO_MIN, ctx->p) == APR_SUCCESS
1579
0
        && sb.filetype == APR_REG && sb.mtime > 0)
1580
0
        return apr_psprintf(ctx->p, "%" APR_OFF_T_FMT, (apr_off_t)sb.mtime);
1581
0
    else
1582
0
        return "0";
1583
0
}
1584
1585
1586
static const char *unescape_func(ap_expr_eval_ctx_t *ctx, const void *data,
1587
                                 const char *arg)
1588
0
{
1589
0
    char *result = apr_pstrdup(ctx->p, arg);
1590
0
    int ret = ap_unescape_url_keep2f(result, 0);
1591
0
    if (ret == OK)
1592
0
        return result;
1593
0
    expr_eval_log(ctx, APLOG_DEBUG, APLOGNO(00538)
1594
0
                  "%s %% escape in unescape('%s') at %s:%d",
1595
0
                  ret == HTTP_BAD_REQUEST ? "Bad" : "Forbidden", arg,
1596
0
                  ctx->info->filename, ctx->info->line_number);
1597
0
    return "";
1598
0
}
1599
1600
static int op_nz(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1601
0
{
1602
0
    const char *name = (const char *)data;
1603
0
    if (name[0] == 'z')
1604
0
        return (arg[0] == '\0');
1605
0
    else
1606
0
        return (arg[0] != '\0');
1607
0
}
1608
1609
static int op_file_min(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1610
0
{
1611
0
    apr_finfo_t sb;
1612
0
    const char *name = (const char *)data;
1613
0
    if (APR_SUCCESS != stat_check(ctx, data, arg)) {
1614
0
        return FALSE;
1615
0
    }
1616
0
    if (apr_stat(&sb, arg, APR_FINFO_MIN, ctx->p) != APR_SUCCESS)
1617
0
        return FALSE;
1618
0
    switch (name[0]) {
1619
0
    case 'd':
1620
0
        return (sb.filetype == APR_DIR);
1621
0
    case 'e':
1622
0
        return TRUE;
1623
0
    case 'f':
1624
0
        return (sb.filetype == APR_REG);
1625
0
    case 's':
1626
0
        return (sb.filetype == APR_REG && sb.size > 0);
1627
0
    default:
1628
0
        ap_assert(0);
1629
0
    }
1630
0
    return FALSE;
1631
0
}
1632
1633
static int op_file_link(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1634
0
{
1635
0
#if !defined(OS2)
1636
0
    apr_finfo_t sb;
1637
0
    if (APR_SUCCESS != stat_check(ctx, data, arg)) {
1638
0
        return FALSE;
1639
0
    }
1640
0
    if (apr_stat(&sb, arg, APR_FINFO_MIN | APR_FINFO_LINK, ctx->p) == APR_SUCCESS
1641
0
        && sb.filetype == APR_LNK) {
1642
0
        return TRUE;
1643
0
    }
1644
0
#endif
1645
0
    return FALSE;
1646
0
}
1647
1648
static int op_file_xbit(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1649
0
{
1650
0
    apr_finfo_t sb;
1651
0
    if (APR_SUCCESS != stat_check(ctx, data, arg)) {
1652
0
        return FALSE;
1653
0
    }
1654
0
    if (apr_stat(&sb, arg, APR_FINFO_PROT| APR_FINFO_LINK, ctx->p) == APR_SUCCESS
1655
0
        && (sb.protection & (APR_UEXECUTE | APR_GEXECUTE | APR_WEXECUTE))) {
1656
0
        return TRUE;
1657
0
    }
1658
0
    return FALSE;
1659
0
}
1660
1661
static int op_url_subr(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1662
0
{
1663
0
    int rc = FALSE;
1664
0
    request_rec  *rsub, *r = ctx->r;
1665
0
    if (!r)
1666
0
        return FALSE;
1667
    /* avoid some infinite recursions */
1668
0
    if (r->main && r->main->uri && r->uri && strcmp(r->main->uri, r->uri) == 0)
1669
0
        return FALSE;
1670
1671
0
    rsub = ap_sub_req_lookup_uri(arg, r, NULL);
1672
0
    if (rsub->status < HTTP_BAD_REQUEST) {
1673
0
            rc = TRUE;
1674
0
    }
1675
0
    expr_eval_log(ctx, APLOG_TRACE5,
1676
0
                  "Subrequest for -U %s at %s:%d gave status: %d",
1677
0
                  arg, ctx->info->filename, ctx->info->line_number,
1678
0
                  rsub->status);
1679
0
    ap_destroy_sub_req(rsub);
1680
0
    return rc;
1681
0
}
1682
1683
static int op_file_subr(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1684
0
{
1685
0
    int rc = FALSE;
1686
0
    apr_finfo_t sb;
1687
0
    request_rec *rsub, *r = ctx->r;
1688
0
    if (!r)
1689
0
        return FALSE;
1690
0
    if (APR_SUCCESS != stat_check(ctx, data, arg)) {
1691
0
        return FALSE;
1692
0
    }
1693
0
    rsub = ap_sub_req_lookup_file(arg, r, NULL);
1694
0
    if (rsub->status < HTTP_MULTIPLE_CHOICES &&
1695
        /* double-check that file exists since default result is 200 */
1696
0
        apr_stat(&sb, rsub->filename, APR_FINFO_MIN, ctx->p) == APR_SUCCESS) {
1697
0
        rc = TRUE;
1698
0
    }
1699
0
    expr_eval_log(ctx, APLOG_TRACE5,
1700
0
                  "Subrequest for -F %s at %s:%d gave status: %d",
1701
0
                  arg, ctx->info->filename, ctx->info->line_number,
1702
0
                  rsub->status);
1703
0
    ap_destroy_sub_req(rsub);
1704
0
    return rc;
1705
0
}
1706
1707
1708
APR_DECLARE_OPTIONAL_FN(int, http2_is_h2, (conn_rec *));
1709
static APR_OPTIONAL_FN_TYPE(http2_is_h2) *is_http2 = NULL;
1710
1711
static const char *const conn_var_names[] = {
1712
    "HTTPS",                    /*  0 */
1713
    "IPV6",                     /*  1 */
1714
    "CONN_LOG_ID",              /*  2 */
1715
    "CONN_REMOTE_ADDR",         /*  3 */
1716
    "HTTP2",                    /*  4 */
1717
    NULL
1718
};
1719
1720
static const char *conn_var_fn(ap_expr_eval_ctx_t *ctx, const void *data)
1721
0
{
1722
0
    int index = ((const char **)data - conn_var_names);
1723
0
    conn_rec *c = ctx->c;
1724
0
    if (!c)
1725
0
        return "";
1726
1727
0
    switch (index) {
1728
0
    case 0:
1729
0
        if (ap_ssl_conn_is_ssl(c))
1730
0
            return "on";
1731
0
        else
1732
0
            return "off";
1733
0
    case 1:
1734
0
#if APR_HAVE_IPV6
1735
0
        {
1736
0
            apr_sockaddr_t *addr = c->client_addr;
1737
0
            if (addr->family == AF_INET6
1738
0
                && !IN6_IS_ADDR_V4MAPPED((struct in6_addr *)addr->ipaddr_ptr))
1739
0
                return "on";
1740
0
            else
1741
0
                return "off";
1742
0
        }
1743
#else
1744
        return "off";
1745
#endif
1746
0
    case 2:
1747
0
        return c->log_id;
1748
0
    case 3:
1749
0
        return c->client_ip;
1750
0
    case 4:
1751
0
        if (is_http2 && is_http2(c))
1752
0
            return "on";
1753
0
        else
1754
0
            return "off";
1755
0
    default:
1756
0
        ap_assert(0);
1757
0
        return NULL;
1758
0
    }
1759
0
}
1760
1761
static const char *const request_var_names[] = {
1762
    "REQUEST_METHOD",           /*  0 */
1763
    "REQUEST_SCHEME",           /*  1 */
1764
    "REQUEST_URI",              /*  2 */
1765
    "REQUEST_FILENAME",         /*  3 */
1766
    "REMOTE_HOST",              /*  4 */
1767
    "REMOTE_IDENT",             /*  5 */
1768
    "REMOTE_USER",              /*  6 */
1769
    "SERVER_ADMIN",             /*  7 */
1770
    "SERVER_NAME",              /*  8 */
1771
    "SERVER_PORT",              /*  9 */
1772
    "SERVER_PROTOCOL",          /* 10 */
1773
    "SCRIPT_FILENAME",          /* 11 */
1774
    "PATH_INFO",                /* 12 */
1775
    "QUERY_STRING",             /* 13 */
1776
    "IS_SUBREQ",                /* 14 */
1777
    "DOCUMENT_ROOT",            /* 15 */
1778
    "AUTH_TYPE",                /* 16 */
1779
    "THE_REQUEST",              /* 17 */
1780
    "CONTENT_TYPE",             /* 18 */
1781
    "HANDLER",                  /* 19 */
1782
    "REQUEST_LOG_ID",           /* 20 */
1783
    "SCRIPT_USER",              /* 21 */
1784
    "SCRIPT_GROUP",             /* 22 */
1785
    "DOCUMENT_URI",             /* 23 */
1786
    "LAST_MODIFIED",            /* 24 */
1787
    "CONTEXT_PREFIX",           /* 25 */
1788
    "CONTEXT_DOCUMENT_ROOT",    /* 26 */
1789
    "REQUEST_STATUS",           /* 27 */
1790
    "REMOTE_ADDR",              /* 28 */
1791
    "SERVER_PROTOCOL_VERSION",  /* 29 */
1792
    "SERVER_PROTOCOL_VERSION_MAJOR",  /* 30 */
1793
    "SERVER_PROTOCOL_VERSION_MINOR",  /* 31 */
1794
    "REMOTE_PORT",                    /* 32 */
1795
    NULL
1796
};
1797
1798
static const char *request_var_fn(ap_expr_eval_ctx_t *ctx, const void *data)
1799
0
{
1800
0
    int index = ((const char **)data - request_var_names);
1801
0
    request_rec *r = ctx->r;
1802
0
    if (!r)
1803
0
        return "";
1804
1805
0
    switch (index) {
1806
0
    case 0:
1807
0
        return r->method;
1808
0
    case 1:
1809
0
        return ap_http_scheme(r);
1810
0
    case 2:
1811
0
        return r->uri;
1812
0
    case 3:
1813
0
        return r->filename;
1814
0
    case 4:
1815
0
        return ap_get_useragent_host(r, REMOTE_NAME, NULL);
1816
0
    case 5:
1817
0
        return ap_get_remote_logname(r);
1818
0
    case 6:
1819
0
        return r->user;
1820
0
    case 7:
1821
0
        return r->server->server_admin;
1822
0
    case 8:
1823
0
        return ap_get_server_name_for_url(r);
1824
0
    case 9:
1825
0
        return apr_psprintf(ctx->p, "%u", ap_get_server_port(r));
1826
0
    case 10:
1827
0
        return r->protocol;
1828
0
    case 11:
1829
0
        return r->filename;
1830
0
    case 12:
1831
0
        return r->path_info;
1832
0
    case 13:
1833
0
        return r->args;
1834
0
    case 14:
1835
0
        return (r->main != NULL ? "true" : "false");
1836
0
    case 15:
1837
0
        return ap_document_root(r);
1838
0
    case 16:
1839
0
        return r->ap_auth_type;
1840
0
    case 17:
1841
0
        return r->the_request;
1842
0
    case 18:
1843
0
        return r->content_type;
1844
0
    case 19:
1845
0
        return r->handler;
1846
0
    case 20:
1847
0
        return r->log_id;
1848
0
    case 21:
1849
0
        {
1850
0
            char *result = "";
1851
0
            if (r->finfo.valid & APR_FINFO_USER)
1852
0
                apr_uid_name_get(&result, r->finfo.user, ctx->p);
1853
0
            return result;
1854
0
        }
1855
0
    case 22:
1856
0
        {
1857
0
            char *result = "";
1858
0
            if (r->finfo.valid & APR_FINFO_USER)
1859
0
                apr_gid_name_get(&result, r->finfo.group, ctx->p);
1860
0
            return result;
1861
0
        }
1862
0
    case 23:
1863
0
        {
1864
0
            const char *uri = apr_table_get(r->subprocess_env, "DOCUMENT_URI");
1865
0
            return uri ? uri : r->uri;
1866
0
        }
1867
0
    case 24:
1868
0
        {
1869
0
            apr_time_exp_t tm;
1870
0
            apr_time_exp_lt(&tm, r->mtime);
1871
0
            return apr_psprintf(ctx->p, "%02d%02d%02d%02d%02d%02d%02d",
1872
0
                                (tm.tm_year / 100) + 19, (tm.tm_year % 100),
1873
0
                                tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min,
1874
0
                                tm.tm_sec);
1875
0
        }
1876
0
    case 25:
1877
0
        return ap_context_prefix(r);
1878
0
    case 26:
1879
0
        return ap_context_document_root(r);
1880
0
    case 27:
1881
0
        return r->status ? apr_psprintf(ctx->p, "%d", r->status) : "";
1882
0
    case 28:
1883
0
        return r->useragent_ip;
1884
0
    case 29:
1885
0
        switch (r->proto_num) {
1886
0
        case 1001:  return "1001";   /* 1.1 */
1887
0
        case 1000:  return "1000";   /* 1.0 */
1888
0
        case 9:     return "9";      /* 0.9 */
1889
0
        }
1890
0
        return apr_psprintf(ctx->p, "%d", r->proto_num);
1891
0
    case 30:
1892
0
        switch (HTTP_VERSION_MAJOR(r->proto_num)) {
1893
0
        case 0:     return "0";
1894
0
        case 1:     return "1";
1895
0
        }
1896
0
        return apr_psprintf(ctx->p, "%d", HTTP_VERSION_MAJOR(r->proto_num));
1897
0
    case 31:
1898
0
        switch (HTTP_VERSION_MINOR(r->proto_num)) {
1899
0
        case 0:     return "0";
1900
0
        case 1:     return "1";
1901
0
        case 9:     return "9";
1902
0
        }
1903
0
        return apr_psprintf(ctx->p, "%d", HTTP_VERSION_MINOR(r->proto_num));
1904
0
    case 32:
1905
0
        return apr_psprintf(ctx->p, "%u", r->useragent_addr->port);
1906
0
    default:
1907
0
        ap_assert(0);
1908
0
        return NULL;
1909
0
    }
1910
0
}
1911
1912
static const char *const req_header_var_names[] = {
1913
    "HTTP_USER_AGENT",       /* 0 */
1914
    "HTTP_PROXY_CONNECTION", /* 1 */
1915
    "HTTP_REFERER",          /* 2 */
1916
    "HTTP_COOKIE",           /* 3 */
1917
    "HTTP_FORWARDED",        /* 4 */
1918
    "HTTP_HOST",             /* 5 */
1919
    "HTTP_ACCEPT",           /* 6 */
1920
    NULL
1921
};
1922
1923
static const char *const req_header_header_names[] = {
1924
    "User-Agent",
1925
    "Proxy-Connection",
1926
    "Referer",
1927
    "Cookie",
1928
    "Forwarded",
1929
    "Host",
1930
    "Accept"
1931
};
1932
1933
static const char *req_header_var_fn(ap_expr_eval_ctx_t *ctx, const void *data)
1934
0
{
1935
0
    const char **const varname = (const char **)data;
1936
0
    int index = (varname - req_header_var_names);
1937
0
    const char *name;
1938
1939
0
    AP_DEBUG_ASSERT(index < 7);
1940
0
    if (!ctx->r)
1941
0
        return "";
1942
1943
0
    name = req_header_header_names[index];
1944
    /* Skip the 'Vary: Host' header combination
1945
     * as indicated in rfc7231 section-7.1.4
1946
     */
1947
0
    if (strcasecmp(name, "Host")){
1948
0
        add_vary(ctx, name);
1949
0
    }
1950
0
    return apr_table_get(ctx->r->headers_in, name);
1951
0
}
1952
1953
static const char *const misc_var_names[] = {
1954
    "TIME_YEAR",        /* 0 */
1955
    "TIME_MON",         /* 1 */
1956
    "TIME_DAY",         /* 2 */
1957
    "TIME_HOUR",        /* 3 */
1958
    "TIME_MIN",         /* 4 */
1959
    "TIME_SEC",         /* 5 */
1960
    "TIME_WDAY",        /* 6 */
1961
    "TIME",             /* 7 */
1962
    "SERVER_SOFTWARE",  /* 8 */
1963
    "API_VERSION",      /* 9 */
1964
    NULL
1965
};
1966
1967
static const char *misc_var_fn(ap_expr_eval_ctx_t *ctx, const void *data)
1968
0
{
1969
0
    apr_time_exp_t tm;
1970
0
    int index = ((const char **)data - misc_var_names);
1971
0
    apr_time_exp_lt(&tm, apr_time_now());
1972
1973
0
    switch (index) {
1974
0
    case 0:
1975
0
        return apr_psprintf(ctx->p, "%02d%02d", (tm.tm_year / 100) + 19,
1976
0
                            tm.tm_year % 100);
1977
0
    case 1:
1978
0
        return apr_psprintf(ctx->p, "%02d", tm.tm_mon+1);
1979
0
    case 2:
1980
0
        return apr_psprintf(ctx->p, "%02d", tm.tm_mday);
1981
0
    case 3:
1982
0
        return apr_psprintf(ctx->p, "%02d", tm.tm_hour);
1983
0
    case 4:
1984
0
        return apr_psprintf(ctx->p, "%02d", tm.tm_min);
1985
0
    case 5:
1986
0
        return apr_psprintf(ctx->p, "%02d", tm.tm_sec);
1987
0
    case 6:
1988
0
        return apr_psprintf(ctx->p, "%d", tm.tm_wday);
1989
0
    case 7:
1990
0
        return apr_psprintf(ctx->p, "%02d%02d%02d%02d%02d%02d%02d",
1991
0
                            (tm.tm_year / 100) + 19, (tm.tm_year % 100),
1992
0
                            tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min,
1993
0
                            tm.tm_sec);
1994
0
    case 8:
1995
0
        return ap_get_server_banner();
1996
0
    case 9:
1997
0
        return apr_itoa(ctx->p, MODULE_MAGIC_NUMBER_MAJOR);
1998
0
    default:
1999
0
        ap_assert(0);
2000
0
    }
2001
2002
0
    return NULL;
2003
0
}
2004
2005
static int subnet_parse_arg(ap_expr_lookup_parms *parms)
2006
0
{
2007
0
    apr_ipsubnet_t *subnet;
2008
0
    const char *addr = parms->arg;
2009
0
    const char *mask;
2010
0
    apr_status_t ret;
2011
2012
0
    if (!parms->arg) {
2013
0
        *parms->err = apr_psprintf(parms->ptemp,
2014
0
                                   "-%s requires subnet/netmask as constant argument",
2015
0
                                   parms->name);
2016
0
        return !OK;
2017
0
    }
2018
2019
0
    mask = ap_strchr_c(addr, '/');
2020
0
    if (mask) {
2021
0
        addr = apr_pstrmemdup(parms->ptemp, addr, mask - addr);
2022
0
        mask++;
2023
0
    }
2024
2025
0
    ret = apr_ipsubnet_create(&subnet, addr, mask, parms->pool);
2026
0
    if (ret != APR_SUCCESS) {
2027
0
        *parms->err = "parsing of subnet/netmask failed";
2028
0
        return !OK;
2029
0
    }
2030
2031
0
    *parms->data = subnet;
2032
0
    return OK;
2033
0
}
2034
2035
static int op_ipmatch(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg1,
2036
                const char *arg2)
2037
0
{
2038
0
    apr_ipsubnet_t *subnet = (apr_ipsubnet_t *)data;
2039
0
    apr_sockaddr_t *saddr;
2040
2041
0
    AP_DEBUG_ASSERT(subnet != NULL);
2042
2043
    /* maybe log an error if this goes wrong? */
2044
0
    if (apr_sockaddr_info_get(&saddr, arg1, APR_UNSPEC, 0, 0, ctx->p) != APR_SUCCESS)
2045
0
        return FALSE;
2046
2047
0
    return apr_ipsubnet_test(subnet, saddr);
2048
0
}
2049
2050
static int op_R(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg1)
2051
0
{
2052
0
    apr_ipsubnet_t *subnet = (apr_ipsubnet_t *)data;
2053
2054
0
    AP_DEBUG_ASSERT(subnet != NULL);
2055
2056
0
    if (!ctx->r)
2057
0
        return FALSE;
2058
2059
0
    return apr_ipsubnet_test(subnet, ctx->r->useragent_addr);
2060
0
}
2061
2062
static int op_T(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
2063
0
{
2064
0
    switch (arg[0]) {
2065
0
    case '\0':
2066
0
        return FALSE;
2067
0
    case 'o':
2068
0
    case 'O':
2069
0
        return strcasecmp(arg, "off") == 0 ? FALSE : TRUE;
2070
0
    case 'n':
2071
0
    case 'N':
2072
0
        return strcasecmp(arg, "no") == 0 ? FALSE : TRUE;
2073
0
    case 'f':
2074
0
    case 'F':
2075
0
        return strcasecmp(arg, "false") == 0 ? FALSE : TRUE;
2076
0
    case '0':
2077
0
        return arg[1] == '\0' ? FALSE : TRUE;
2078
0
    default:
2079
0
        return TRUE;
2080
0
    }
2081
0
}
2082
2083
static int op_fnmatch(ap_expr_eval_ctx_t *ctx, const void *data,
2084
                      const char *arg1, const char *arg2)
2085
0
{
2086
0
    return (APR_SUCCESS == apr_fnmatch(arg2, arg1, APR_FNM_PATHNAME));
2087
0
}
2088
2089
static int op_strmatch(ap_expr_eval_ctx_t *ctx, const void *data,
2090
                       const char *arg1, const char *arg2)
2091
0
{
2092
0
    return (APR_SUCCESS == apr_fnmatch(arg2, arg1, 0));
2093
0
}
2094
2095
static int op_strcmatch(ap_expr_eval_ctx_t *ctx, const void *data,
2096
                        const char *arg1, const char *arg2)
2097
0
{
2098
0
    return (APR_SUCCESS == apr_fnmatch(arg2, arg1, APR_FNM_CASE_BLIND));
2099
0
}
2100
2101
0
#define RESTRICTED_FILE_TEST 0x01
2102
0
#define RESTRICTED_FILE_FUNC 0x02
2103
0
#define RESTRICTED_ALL      (RESTRICTED_FILE_TEST | RESTRICTED_FILE_FUNC)
2104
2105
struct expr_provider_single {
2106
    const void *func;
2107
    const char *name;
2108
    ap_expr_lookup_fn_t *arg_parsing_func;
2109
    unsigned int restricted;
2110
};
2111
2112
struct expr_provider_multi {
2113
    const void *func;
2114
    const char *const *names;
2115
};
2116
2117
static const struct expr_provider_multi var_providers[] = {
2118
    { misc_var_fn, misc_var_names },
2119
    { req_header_var_fn, req_header_var_names },
2120
    { request_var_fn, request_var_names },
2121
    { conn_var_fn, conn_var_names },
2122
    { NULL, NULL }
2123
};
2124
2125
static const struct expr_provider_single string_func_providers[] = {
2126
    { osenv_func,           "osenv",          NULL, 0 },
2127
    { env_func,             "env",            NULL, 0 },
2128
    { req_table_func,       "resp",           NULL, 0 },
2129
    { req_table_func,       "req",            NULL, 0 },
2130
    /* 'http' as alias for 'req' for compatibility with ssl_expr */
2131
    { req_table_func,       "http",           NULL, 0 },
2132
    { req_table_func,       "note",           NULL, 0 },
2133
    { req_table_func,       "reqenv",         NULL, 0 },
2134
    { req_table_func,       "req_novary",     NULL, 0 },
2135
    { tolower_func,         "tolower",        NULL, 0 },
2136
    { toupper_func,         "toupper",        NULL, 0 },
2137
    { escape_func,          "escape",         NULL, 0 },
2138
    { unescape_func,        "unescape",       NULL, 0 },
2139
    { file_func,            "file",           NULL, RESTRICTED_FILE_FUNC },
2140
    { filesize_func,        "filesize",       NULL, RESTRICTED_FILE_FUNC },
2141
    { filemod_func,         "filemod",        NULL, RESTRICTED_FILE_FUNC },
2142
    { base64_func,          "base64",         NULL, 0 },
2143
    { unbase64_func,        "unbase64",       NULL, 0 },
2144
    { sha1_func,            "sha1",           NULL, 0 },
2145
    { md5_func,             "md5",            NULL, 0 },
2146
#if APR_VERSION_AT_LEAST(1,6,0)
2147
    { ldap_func,            "ldap",           NULL, 0 },
2148
#endif
2149
    { replace_func,         "replace",        replace_func_parse_arg, 0 },
2150
    { escapehtml_func,      "escapehtml",     NULL, 0 },
2151
    { NULL, NULL, NULL}
2152
};
2153
2154
static const struct expr_provider_single unary_op_providers[] = {
2155
    { op_nz,        "n", NULL,             0 },
2156
    { op_nz,        "z", NULL,             0 },
2157
    { op_R,         "R", subnet_parse_arg, 0 },
2158
    { op_T,         "T", NULL,             0 },
2159
    { op_file_min,  "d", NULL,             RESTRICTED_FILE_TEST },
2160
    { op_file_min,  "e", NULL,             RESTRICTED_FILE_TEST },
2161
    { op_file_min,  "f", NULL,             RESTRICTED_FILE_TEST },
2162
    { op_file_min,  "s", NULL,             RESTRICTED_FILE_TEST },
2163
    { op_file_link, "L", NULL,             RESTRICTED_FILE_TEST },
2164
    { op_file_link, "h", NULL,             RESTRICTED_FILE_TEST },
2165
    { op_file_xbit, "x", NULL,             RESTRICTED_FILE_TEST },
2166
    { op_file_subr, "F", NULL,             0 },
2167
    { op_url_subr,  "U", NULL,             0 },
2168
    { op_url_subr,  "A", NULL,             0 },
2169
    { NULL, NULL, NULL }
2170
};
2171
2172
static const struct expr_provider_single binary_op_providers[] = {
2173
    { op_ipmatch,   "ipmatch",      subnet_parse_arg, 0 },
2174
    { op_fnmatch,   "fnmatch",      NULL,             0 },
2175
    { op_strmatch,  "strmatch",     NULL,             0 },
2176
    { op_strcmatch, "strcmatch",    NULL,             0 },
2177
    { NULL, NULL, NULL }
2178
};
2179
2180
static int core_expr_lookup(ap_expr_lookup_parms *parms)
2181
0
{
2182
0
    switch (parms->type) {
2183
0
    case AP_EXPR_FUNC_VAR: {
2184
0
            const struct expr_provider_multi *prov = var_providers;
2185
0
            while (prov->func) {
2186
0
                const char *const *name = prov->names;
2187
0
                while (*name) {
2188
0
                    if (ap_cstr_casecmp(*name, parms->name) == 0) {
2189
0
                        *parms->func = prov->func;
2190
0
                        *parms->data = name;
2191
0
                        return OK;
2192
0
                    }
2193
0
                    name++;
2194
0
                }
2195
0
                prov++;
2196
0
            }
2197
0
        }
2198
0
        break;
2199
0
    case AP_EXPR_FUNC_STRING:
2200
0
    case AP_EXPR_FUNC_OP_UNARY:
2201
0
    case AP_EXPR_FUNC_OP_BINARY: {
2202
0
            const struct expr_provider_single *prov = NULL;
2203
0
            switch (parms->type) {
2204
0
            case AP_EXPR_FUNC_STRING:
2205
0
                prov = string_func_providers;
2206
0
                break;
2207
0
            case AP_EXPR_FUNC_OP_UNARY:
2208
0
                prov = unary_op_providers;
2209
0
                break;
2210
0
            case AP_EXPR_FUNC_OP_BINARY:
2211
0
                prov = binary_op_providers;
2212
0
                break;
2213
0
            default:
2214
0
                ap_assert(0);
2215
0
            }
2216
0
            while (prov && prov->func) {
2217
0
                int match;
2218
0
                if (parms->type == AP_EXPR_FUNC_OP_UNARY)
2219
0
                    match = !strcmp(prov->name, parms->name);
2220
0
                else
2221
0
                    match = !ap_cstr_casecmp(prov->name, parms->name);
2222
0
                if (match) {
2223
0
                    if (((parms->flags & AP_EXPR_FLAG_RESTRICTED)
2224
0
                         && (prov->restricted & RESTRICTED_ALL))
2225
0
                        || ((parms->flags & AP_EXPR_FLAG_RESTRICTED_FILE_FUNC)
2226
0
                            && (prov->restricted & RESTRICTED_FILE_FUNC))) {
2227
0
                        *parms->err =
2228
0
                            apr_psprintf(parms->ptemp,
2229
0
                                         "%s%s not available in restricted context",
2230
0
                                         (parms->type == AP_EXPR_FUNC_STRING) ? "" : "-",
2231
0
                                         prov->name);
2232
0
                        return !OK;
2233
0
                    }
2234
0
                    *parms->func = prov->func;
2235
0
                    if (prov->arg_parsing_func) {
2236
0
                        return prov->arg_parsing_func(parms);
2237
0
                    }
2238
0
                    else {
2239
0
                        *parms->data = prov->name;
2240
0
                        return OK;
2241
0
                    }
2242
0
                }
2243
0
                prov++;
2244
0
            }
2245
0
        }
2246
0
        break;
2247
0
    default:
2248
0
        break;
2249
0
    }
2250
0
    return DECLINED;
2251
0
}
2252
2253
static int expr_lookup_not_found(ap_expr_lookup_parms *parms)
2254
0
{
2255
0
    const char *type;
2256
0
    const char *prefix = "";
2257
2258
0
    switch (parms->type) {
2259
0
    case AP_EXPR_FUNC_VAR:
2260
0
        type = "Variable";
2261
0
        break;
2262
0
    case AP_EXPR_FUNC_STRING:
2263
0
        type = "Function";
2264
0
        break;
2265
0
    case AP_EXPR_FUNC_LIST:
2266
0
        type = "List-returning function";
2267
0
        break;
2268
0
    case AP_EXPR_FUNC_OP_UNARY:
2269
0
        type = "Unary operator";
2270
0
        break;
2271
0
    case AP_EXPR_FUNC_OP_BINARY:
2272
0
        type = "Binary operator";
2273
0
        break;
2274
0
    default:
2275
0
        *parms->err = "Invalid expression type in expr_lookup";
2276
0
        return !OK;
2277
0
    }
2278
0
    if (   parms->type == AP_EXPR_FUNC_OP_UNARY
2279
0
        || parms->type == AP_EXPR_FUNC_OP_BINARY) {
2280
0
        prefix = "-";
2281
0
    }
2282
0
    *parms->err = apr_psprintf(parms->ptemp, "%s '%s%s' does not exist", type,
2283
0
                               prefix, parms->name);
2284
0
    return !OK;
2285
0
}
2286
2287
static int ap_expr_post_config(apr_pool_t *pconf, apr_pool_t *plog,
2288
                               apr_pool_t *ptemp, server_rec *s)
2289
0
{
2290
0
    is_http2 = APR_RETRIEVE_OPTIONAL_FN(http2_is_h2);
2291
0
    return OK;
2292
0
}
2293
2294
void ap_expr_init(apr_pool_t *p)
2295
0
{
2296
0
    ap_hook_expr_lookup(core_expr_lookup, NULL, NULL, APR_HOOK_MIDDLE);
2297
0
    ap_hook_expr_lookup(expr_lookup_not_found, NULL, NULL, APR_HOOK_REALLY_LAST);
2298
0
    ap_hook_post_config(ap_expr_post_config, NULL, NULL, APR_HOOK_MIDDLE);
2299
0
}
2300