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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * ap_expr_eval.c, based on ssl_expr_eval.c from mod_ssl
23 #include "http_core.h"
24 #include "http_protocol.h"
25 #include "http_request.h"
26 #include "ap_provider.h"
27 #include "util_expr_private.h"
30 #include "apr_fnmatch.h"
32 #include <limits.h> /* for INT_MAX */
34 /* we know core's module_index is 0 */
35 #undef APLOG_MODULE_INDEX
36 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
39 APR_HOOK_LINK(expr_lookup)
42 AP_IMPLEMENT_HOOK_RUN_FIRST(int, expr_lookup, (ap_expr_lookup_parms *parms),
45 #define LOG_MARK(info) __FILE__, __LINE__, (info)->module_index
47 static const char *ap_expr_eval_string_func(ap_expr_eval_ctx_t *ctx,
48 const ap_expr_t *info,
49 const ap_expr_t *args);
50 static const char *ap_expr_eval_re_backref(ap_expr_eval_ctx_t *ctx,
52 static const char *ap_expr_eval_var(ap_expr_eval_ctx_t *ctx,
53 ap_expr_var_func_t *func,
56 /* define AP_EXPR_DEBUG to log the parse tree when parsing an expression */
58 static void expr_dump_tree(const ap_expr_t *e, const server_rec *s,
59 int loglevel, int indent);
63 * To reduce counting overhead, we only count calls to
64 * ap_expr_eval_word() and ap_expr_eval(). The max number of
65 * stack frames is larger by some factor.
67 #define AP_EXPR_MAX_RECURSION 20
68 static int inc_rec(ap_expr_eval_ctx_t *ctx)
70 if (ctx->reclvl < AP_EXPR_MAX_RECURSION) {
74 *ctx->err = "Recursion limit reached";
75 /* short circuit further evaluation */
76 ctx->reclvl = INT_MAX;
80 static const char *ap_expr_eval_word(ap_expr_eval_ctx_t *ctx,
81 const ap_expr_t *node)
83 const char *result = "";
86 switch (node->node_op) {
89 result = node->node_arg1;
92 result = ap_expr_eval_var(ctx, (ap_expr_var_func_t *)node->node_arg1,
96 if (((ap_expr_t *)node->node_arg2)->node_op != op_Concat) {
97 const char *s1 = ap_expr_eval_word(ctx, node->node_arg1);
98 const char *s2 = ap_expr_eval_word(ctx, node->node_arg2);
104 result = apr_pstrcat(ctx->p, s1, s2, NULL);
107 const ap_expr_t *nodep = node;
111 nodep = nodep->node_arg2;
113 } while (nodep->node_op == op_Concat);
114 vec = apr_palloc(ctx->p, i * sizeof(struct iovec));
118 vec[i].iov_base = (void *)ap_expr_eval_word(ctx,
120 vec[i].iov_len = strlen(vec[i].iov_base);
122 nodep = nodep->node_arg2;
123 } while (nodep->node_op == op_Concat);
124 vec[i].iov_base = (void *)ap_expr_eval_word(ctx, nodep);
125 vec[i].iov_len = strlen(vec[i].iov_base);
127 result = apr_pstrcatv(ctx->p, vec, i, NULL);
130 case op_StringFuncCall: {
131 const ap_expr_t *info = node->node_arg1;
132 const ap_expr_t *args = node->node_arg2;
133 result = ap_expr_eval_string_func(ctx, info, args);
136 case op_RegexBackref: {
137 const unsigned int *np = node->node_arg1;
138 result = ap_expr_eval_re_backref(ctx, *np);
142 *ctx->err = "Internal evaluation error: Unknown word expression node";
151 static const char *ap_expr_eval_var(ap_expr_eval_ctx_t *ctx,
152 ap_expr_var_func_t *func,
155 AP_DEBUG_ASSERT(func != NULL);
156 AP_DEBUG_ASSERT(data != NULL);
157 return (*func)(ctx, data);
160 static const char *ap_expr_eval_re_backref(ap_expr_eval_ctx_t *ctx, unsigned int n)
164 if (!ctx->re_pmatch || !ctx->re_source || *ctx->re_source == '\0' ||
165 ctx->re_nmatch < n + 1)
168 len = ctx->re_pmatch[n].rm_eo - ctx->re_pmatch[n].rm_so;
172 return apr_pstrndup(ctx->p, *ctx->re_source + ctx->re_pmatch[n].rm_so, len);
175 static const char *ap_expr_eval_string_func(ap_expr_eval_ctx_t *ctx,
176 const ap_expr_t *info,
177 const ap_expr_t *arg)
179 ap_expr_string_func_t *func = (ap_expr_string_func_t *)info->node_arg1;
180 const void *data = info->node_arg2;
182 AP_DEBUG_ASSERT(info->node_op == op_StringFuncInfo);
183 AP_DEBUG_ASSERT(func != NULL);
184 AP_DEBUG_ASSERT(data != NULL);
185 return (*func)(ctx, data, ap_expr_eval_word(ctx, arg));
188 static int intstrcmp(const char *s1, const char *s2)
190 apr_int64_t i1 = apr_atoi64(s1);
191 apr_int64_t i2 = apr_atoi64(s2);
201 static int ap_expr_eval_comp(ap_expr_eval_ctx_t *ctx, const ap_expr_t *node)
203 const ap_expr_t *e1 = node->node_arg1;
204 const ap_expr_t *e2 = node->node_arg2;
205 switch (node->node_op) {
207 return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) == 0);
209 return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) != 0);
211 return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) < 0);
213 return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) <= 0);
215 return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) > 0);
217 return (intstrcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) >= 0);
219 return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) == 0);
221 return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) != 0);
223 return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) < 0);
225 return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) <= 0);
227 return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) > 0);
229 return (strcmp(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) >= 0);
231 const char *needle = ap_expr_eval_word(ctx, e1);
232 if (e2->node_op == op_ListElement) {
234 const ap_expr_t *val = e2->node_arg1;
235 AP_DEBUG_ASSERT(e2->node_op == op_ListElement);
236 if (strcmp(needle, ap_expr_eval_word(ctx, val)) == 0) {
241 } while (e2 != NULL);
243 else if (e2->node_op == op_ListFuncCall) {
244 const ap_expr_t *info = e2->node_arg1;
245 const ap_expr_t *arg = e2->node_arg2;
246 ap_expr_list_func_t *func = (ap_expr_list_func_t *)info->node_arg1;
247 apr_array_header_t *haystack;
249 AP_DEBUG_ASSERT(func != NULL);
250 AP_DEBUG_ASSERT(info->node_op == op_ListFuncInfo);
251 haystack = (*func)(ctx, info->node_arg2, ap_expr_eval_word(ctx, arg));
252 if (haystack == NULL)
254 for (; i < haystack->nelts; i++) {
255 if (strcmp(needle, APR_ARRAY_IDX(haystack,i,char *)) == 0)
263 const char *word = ap_expr_eval_word(ctx, e1);
264 const ap_regex_t *regex = e2->node_arg1;
268 * $0 ... $9 may contain stuff the user wants to keep. Therefore
269 * we only set them if there are capturing parens in the regex.
271 if (regex->re_nsub > 0) {
272 result = (0 == ap_regexec(regex, word, ctx->re_nmatch,
274 *ctx->re_source = result ? word : NULL;
277 result = (0 == ap_regexec(regex, word, 0, NULL, 0));
280 if (node->node_op == op_REG)
286 *ctx->err = "Internal evaluation error: Unknown comp expression node";
291 /* combined string/int comparison for compatibility with ssl_expr */
292 static int strcmplex(const char *str1, const char *str2)
306 for (i = 0; i < n1; i++) {
307 if (str1[i] > str2[i])
309 if (str1[i] < str2[i])
315 static int ssl_expr_eval_comp(ap_expr_eval_ctx_t *ctx, const ap_expr_t *node)
317 const ap_expr_t *e1 = node->node_arg1;
318 const ap_expr_t *e2 = node->node_arg2;
319 switch (node->node_op) {
322 return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) == 0);
325 return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) != 0);
328 return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) < 0);
331 return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) <= 0);
334 return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) > 0);
337 return (strcmplex(ap_expr_eval_word(ctx, e1), ap_expr_eval_word(ctx, e2)) >= 0);
339 return ap_expr_eval_comp(ctx, node);
343 AP_DECLARE_NONSTD(int) ap_expr_lookup_default(ap_expr_lookup_parms *parms)
345 return ap_run_expr_lookup(parms);
348 AP_DECLARE(const char *) ap_expr_parse(apr_pool_t *pool, apr_pool_t *ptemp,
349 ap_expr_info_t *info, const char *expr,
350 ap_expr_lookup_fn_t *lookup_fn)
352 ap_expr_parse_ctx_t ctx;
358 ctx.inputlen = strlen(expr);
359 ctx.inputptr = ctx.inputbuf;
361 ctx.error = NULL; /* generic bison error message (XXX: usually not very useful, should be axed) */
362 ctx.error2 = NULL; /* additional error message */
363 ctx.flags = info->flags;
365 ctx.scan_buf[0] = '\0';
366 ctx.scan_ptr = ctx.scan_buf;
367 ctx.lookup_fn = lookup_fn ? lookup_fn : ap_expr_lookup_default;
370 ap_expr_yylex_init(&ctx.scanner);
371 ap_expr_yyset_extra(&ctx, ctx.scanner);
372 rc = ap_expr_yyparse(&ctx);
373 ap_expr_yylex_destroy(ctx.scanner);
376 return apr_psprintf(pool, "%s: %s", ctx.error, ctx.error2);
380 else if (ctx.error2) {
384 if (rc) /* XXX can this happen? */
385 return "syntax error";
389 expr_dump_tree(ctx.expr, NULL, APLOG_NOTICE, 2);
392 info->root_node = ctx.expr;
397 AP_DECLARE(ap_expr_info_t*) ap_expr_parse_cmd_mi(const cmd_parms *cmd,
401 ap_expr_lookup_fn_t *lookup_fn,
404 ap_expr_info_t *info = apr_pcalloc(cmd->pool, sizeof(ap_expr_info_t));
405 info->filename = cmd->directive->filename;
406 info->line_number = cmd->directive->line_num;
408 info->module_index = module_index;
409 *err = ap_expr_parse(cmd->pool, cmd->temp_pool, info, expr, lookup_fn);
417 ap_expr_t *ap_expr_make(ap_expr_node_op_e op, const void *a1, const void *a2,
418 ap_expr_parse_ctx_t *ctx)
420 ap_expr_t *node = apr_palloc(ctx->pool, sizeof(ap_expr_t));
422 node->node_arg1 = a1;
423 node->node_arg2 = a2;
427 static ap_expr_t *ap_expr_info_make(int type, const char *name,
428 ap_expr_parse_ctx_t *ctx,
429 const ap_expr_t *arg)
431 ap_expr_t *info = apr_palloc(ctx->pool, sizeof(ap_expr_t));
432 ap_expr_lookup_parms parms;
434 parms.flags = ctx->flags;
435 parms.pool = ctx->pool;
436 parms.ptemp = ctx->ptemp;
438 parms.func = &info->node_arg1;
439 parms.data = &info->node_arg2;
440 parms.err = &ctx->error2;
441 parms.arg = (arg && arg->node_op == op_String) ? arg->node_arg1 : NULL;
442 if (ctx->lookup_fn(&parms) != OK)
447 ap_expr_t *ap_expr_str_func_make(const char *name, const ap_expr_t *arg,
448 ap_expr_parse_ctx_t *ctx)
450 ap_expr_t *info = ap_expr_info_make(AP_EXPR_FUNC_STRING, name, ctx, arg);
454 info->node_op = op_StringFuncInfo;
455 return ap_expr_make(op_StringFuncCall, info, arg, ctx);
458 ap_expr_t *ap_expr_list_func_make(const char *name, const ap_expr_t *arg,
459 ap_expr_parse_ctx_t *ctx)
461 ap_expr_t *info = ap_expr_info_make(AP_EXPR_FUNC_LIST, name, ctx, arg);
465 info->node_op = op_ListFuncInfo;
466 return ap_expr_make(op_ListFuncCall, info, arg, ctx);
469 ap_expr_t *ap_expr_unary_op_make(const char *name, const ap_expr_t *arg,
470 ap_expr_parse_ctx_t *ctx)
472 ap_expr_t *info = ap_expr_info_make(AP_EXPR_FUNC_OP_UNARY, name, ctx, arg);
476 info->node_op = op_UnaryOpInfo;
477 return ap_expr_make(op_UnaryOpCall, info, arg, ctx);
480 ap_expr_t *ap_expr_binary_op_make(const char *name, const ap_expr_t *arg1,
481 const ap_expr_t *arg2, ap_expr_parse_ctx_t *ctx)
484 ap_expr_t *info = ap_expr_info_make(AP_EXPR_FUNC_OP_BINARY, name, ctx,
489 info->node_op = op_BinaryOpInfo;
490 args = ap_expr_make(op_BinaryOpArgs, arg1, arg2, ctx);
491 return ap_expr_make(op_BinaryOpCall, info, args, ctx);
495 ap_expr_t *ap_expr_var_make(const char *name, ap_expr_parse_ctx_t *ctx)
497 ap_expr_t *node = ap_expr_info_make(AP_EXPR_FUNC_VAR, name, ctx, NULL);
501 node->node_op = op_Var;
507 #define MARK APLOG_MARK,loglevel,0,s
508 #define DUMP_E_E(op, e1, e2) \
509 do { ap_log_error(MARK,"%*s%s: %pp %pp", indent, " ", op, e1, e2); \
510 if (e1) expr_dump_tree(e1, s, loglevel, indent + 2); \
511 if (e2) expr_dump_tree(e2, s, loglevel, indent + 2); \
513 #define DUMP_S_E(op, s1, e1) \
514 do { ap_log_error(MARK,"%*s%s: '%s' %pp", indent, " ", op, (char *)s1, e1); \
515 if (e1) expr_dump_tree(e1, s, loglevel, indent + 2); \
517 #define DUMP_S_P(op, s1, p1) \
518 ap_log_error(MARK,"%*s%s: '%s' %pp", indent, " ", op, (char *)s1, p1);
519 #define DUMP_P_P(op, p1, p2) \
520 ap_log_error(MARK,"%*s%s: %pp %pp", indent, " ", op, p1, p2);
521 #define DUMP_S_S(op, s1, s2) \
522 ap_log_error(MARK,"%*s%s: '%s' '%s'", indent, " ", op, (char *)s1, (char *)s2)
523 #define DUMP_P(op, p1) \
524 ap_log_error(MARK,"%*s%s: %pp", indent, " ", op, p1);
525 #define DUMP_IP(op, p1) \
526 ap_log_error(MARK,"%*s%s: %d", indent, " ", op, *(int *)p1);
527 #define DUMP_S(op, s1) \
528 ap_log_error(MARK,"%*s%s: '%s'", indent, " ", op, (char *)s1)
530 #define CASE_OP(op) case op: name = #op ; break;
532 static void expr_dump_tree(const ap_expr_t *e, const server_rec *s,
533 int loglevel, int indent)
535 switch (e->node_op) {
542 switch (e->node_op) {
549 ap_log_error(MARK, "%*s%s", indent, " ", name);
553 /* arg1: string, arg2: expr */
555 case op_BinaryOpCall:
556 case op_BinaryOpArgs:
559 switch (e->node_op) {
560 CASE_OP(op_BinaryOpCall);
561 CASE_OP(op_UnaryOpCall);
562 CASE_OP(op_BinaryOpArgs);
566 DUMP_S_E(name, e->node_arg1, e->node_arg2);
570 /* arg1: expr, arg2: expr */
591 case op_StringFuncCall:
592 case op_ListFuncCall:
596 switch (e->node_op) {
617 CASE_OP(op_StringFuncCall);
618 CASE_OP(op_ListFuncCall);
619 CASE_OP(op_ListElement);
623 DUMP_E_E(name, e->node_arg1, e->node_arg2);
631 switch (e->node_op) {
637 DUMP_S(name, e->node_arg1);
640 /* arg1: pointer, arg2: pointer */
642 case op_StringFuncInfo:
644 case op_BinaryOpInfo:
645 case op_ListFuncInfo:
648 switch (e->node_op) {
650 CASE_OP(op_StringFuncInfo);
651 CASE_OP(op_UnaryOpInfo);
652 CASE_OP(op_BinaryOpInfo);
653 CASE_OP(op_ListFuncInfo);
657 DUMP_P_P(name, e->node_arg1, e->node_arg2);
662 DUMP_P("op_Regex", e->node_arg1);
664 /* arg1: pointer to int */
665 case op_RegexBackref:
666 DUMP_IP("op_RegexBackref", e->node_arg1);
669 ap_log_error(MARK, "%*sERROR: INVALID OP %d", indent, " ", e->node_op);
673 #endif /* AP_EXPR_DEBUG */
675 static int ap_expr_eval_unary_op(ap_expr_eval_ctx_t *ctx, const ap_expr_t *info,
676 const ap_expr_t *arg)
678 ap_expr_op_unary_t *op_func = (ap_expr_op_unary_t *)info->node_arg1;
679 const void *data = info->node_arg2;
681 AP_DEBUG_ASSERT(info->node_op == op_UnaryOpInfo);
682 AP_DEBUG_ASSERT(op_func != NULL);
683 AP_DEBUG_ASSERT(data != NULL);
684 return (*op_func)(ctx, data, ap_expr_eval_word(ctx, arg));
687 static int ap_expr_eval_binary_op(ap_expr_eval_ctx_t *ctx,
688 const ap_expr_t *info,
689 const ap_expr_t *args)
691 ap_expr_op_binary_t *op_func = (ap_expr_op_binary_t *)info->node_arg1;
692 const void *data = info->node_arg2;
693 const ap_expr_t *a1 = args->node_arg1;
694 const ap_expr_t *a2 = args->node_arg2;
696 AP_DEBUG_ASSERT(info->node_op == op_BinaryOpInfo);
697 AP_DEBUG_ASSERT(args->node_op == op_BinaryOpArgs);
698 AP_DEBUG_ASSERT(op_func != NULL);
699 AP_DEBUG_ASSERT(data != NULL);
700 return (*op_func)(ctx, data, ap_expr_eval_word(ctx, a1),
701 ap_expr_eval_word(ctx, a2));
705 static int ap_expr_eval(ap_expr_eval_ctx_t *ctx, const ap_expr_t *node)
707 const ap_expr_t *e1 = node->node_arg1;
708 const ap_expr_t *e2 = node->node_arg2;
713 switch (node->node_op) {
726 if (e1->node_op == op_Not) {
727 if (!ap_expr_eval(ctx, e1->node_arg1)) {
733 if (ap_expr_eval(ctx, e1)) {
738 node = node->node_arg2;
739 e1 = node->node_arg1;
740 } while (node->node_op == op_Or);
744 if (e1->node_op == op_Not) {
745 if (ap_expr_eval(ctx, e1->node_arg1)) {
751 if (!ap_expr_eval(ctx, e1)) {
756 node = node->node_arg2;
757 e1 = node->node_arg1;
758 } while (node->node_op == op_And);
761 result ^= ap_expr_eval_unary_op(ctx, e1, e2);
763 case op_BinaryOpCall:
764 result ^= ap_expr_eval_binary_op(ctx, e1, e2);
767 if (ctx->info->flags & AP_EXPR_FLAG_SSL_EXPR_COMPAT)
768 result ^= ssl_expr_eval_comp(ctx, e1);
770 result ^= ap_expr_eval_comp(ctx, e1);
773 *ctx->err = "Internal evaluation error: Unknown expression node";
776 e1 = node->node_arg1;
777 e2 = node->node_arg2;
784 AP_DECLARE(int) ap_expr_exec(request_rec *r, const ap_expr_info_t *info,
787 return ap_expr_exec_re(r, info, 0, NULL, NULL, err);
790 AP_DECLARE(int) ap_expr_exec_ctx(ap_expr_eval_ctx_t *ctx)
794 AP_DEBUG_ASSERT(ctx->p != NULL);
795 /* XXX: allow r, c == NULL */
796 AP_DEBUG_ASSERT(ctx->r != NULL);
797 AP_DEBUG_ASSERT(ctx->c != NULL);
798 AP_DEBUG_ASSERT(ctx->s != NULL);
799 AP_DEBUG_ASSERT(ctx->err != NULL);
800 AP_DEBUG_ASSERT(ctx->info != NULL);
801 if (ctx->re_pmatch) {
802 AP_DEBUG_ASSERT(ctx->re_source != NULL);
803 AP_DEBUG_ASSERT(ctx->re_nmatch > 0);
808 if (ctx->info->flags & AP_EXPR_FLAG_STRING_RESULT) {
809 *ctx->result_string = ap_expr_eval_word(ctx, ctx->info->root_node);
810 if (*ctx->err != NULL) {
811 ap_log_rerror(LOG_MARK(ctx->info), APLOG_ERR, 0, ctx->r,
812 "Evaluation of expression from %s:%d failed: %s",
813 ctx->info->filename, ctx->info->line_number, *ctx->err);
816 ap_log_rerror(LOG_MARK(ctx->info), APLOG_TRACE4, 0, ctx->r,
817 "Evaluation of string expression from %s:%d gave: %s",
818 ctx->info->filename, ctx->info->line_number,
819 *ctx->result_string);
824 rc = ap_expr_eval(ctx, ctx->info->root_node);
825 if (*ctx->err != NULL) {
826 ap_log_rerror(LOG_MARK(ctx->info), APLOG_ERR, 0, ctx->r,
827 "Evaluation of expression from %s:%d failed: %s",
828 ctx->info->filename, ctx->info->line_number, *ctx->err);
832 ap_log_rerror(LOG_MARK(ctx->info), APLOG_TRACE4, 0, ctx->r,
833 "Evaluation of expression from %s:%d gave: %d",
834 ctx->info->filename, ctx->info->line_number, rc);
836 if (ctx->vary_this && *ctx->vary_this)
837 apr_table_merge(ctx->r->headers_out, "Vary", *ctx->vary_this);
844 AP_DECLARE(int) ap_expr_exec_re(request_rec *r, const ap_expr_info_t *info,
845 apr_size_t nmatch, ap_regmatch_t *pmatch,
846 const char **source, const char **err)
848 ap_expr_eval_ctx_t ctx;
849 int dont_vary = (info->flags & AP_EXPR_FLAG_DONT_VARY);
850 const char *tmp_source = NULL, *vary_this = NULL;
851 ap_regmatch_t tmp_pmatch[AP_MAX_REG_MATCH];
853 AP_DEBUG_ASSERT((info->flags & AP_EXPR_FLAG_STRING_RESULT) == 0);
856 ctx.c = r->connection;
861 ctx.re_nmatch = nmatch;
862 ctx.re_pmatch = pmatch;
863 ctx.re_source = source;
864 ctx.vary_this = dont_vary ? NULL : &vary_this;
868 ctx.re_nmatch = AP_MAX_REG_MATCH;
869 ctx.re_pmatch = tmp_pmatch;
870 ctx.re_source = &tmp_source;
873 return ap_expr_exec_ctx(&ctx);
876 AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r,
877 const ap_expr_info_t *info,
879 ap_regmatch_t *pmatch,
883 ap_expr_eval_ctx_t ctx;
885 const char *tmp_source = NULL, *vary_this = NULL;
886 ap_regmatch_t tmp_pmatch[AP_MAX_REG_MATCH];
889 AP_DEBUG_ASSERT(info->flags & AP_EXPR_FLAG_STRING_RESULT);
891 if (info->root_node->node_op == op_String) {
892 /* short-cut for constant strings */
894 return (const char *)info->root_node->node_arg1;
897 dont_vary = (info->flags & AP_EXPR_FLAG_DONT_VARY);
900 ctx.c = r->connection;
905 ctx.re_nmatch = nmatch;
906 ctx.re_pmatch = pmatch;
907 ctx.re_source = source;
908 ctx.vary_this = dont_vary ? NULL : &vary_this;
910 ctx.result_string = &result;
913 ctx.re_nmatch = AP_MAX_REG_MATCH;
914 ctx.re_pmatch = tmp_pmatch;
915 ctx.re_source = &tmp_source;
918 rc = ap_expr_exec_ctx(&ctx);
927 AP_DECLARE(const char *) ap_expr_str_exec(request_rec *r,
928 const ap_expr_info_t *info,
931 return ap_expr_str_exec_re(r, info, 0, NULL, NULL, err);
935 static void add_vary(ap_expr_eval_ctx_t *ctx, const char *name)
940 if (*ctx->vary_this) {
941 *ctx->vary_this = apr_pstrcat(ctx->p, *ctx->vary_this, ", ", name,
945 *ctx->vary_this = name;
949 static const char *req_table_func(ap_expr_eval_ctx_t *ctx, const void *data,
952 const char *name = (const char *)data;
957 if (name[2] == 's') { /* resp */
958 /* Try r->headers_out first, fall back on err_headers_out. */
959 const char *v = apr_table_get(ctx->r->headers_out, arg);
963 t = ctx->r->err_headers_out;
965 else if (name[0] == 'n') /* notes */
967 else if (name[3] == 'e') /* reqenv */
968 t = ctx->r->subprocess_env;
969 else { /* req, http */
970 t = ctx->r->headers_in;
973 return apr_table_get(t, arg);
976 static const char *env_func(ap_expr_eval_ctx_t *ctx, const void *data,
980 /* this order is for ssl_expr compatibility */
982 if ((res = apr_table_get(ctx->r->notes, arg)) != NULL)
984 else if ((res = apr_table_get(ctx->r->subprocess_env, arg)) != NULL)
990 static const char *osenv_func(ap_expr_eval_ctx_t *ctx, const void *data,
996 static const char *tolower_func(ap_expr_eval_ctx_t *ctx, const void *data,
999 char *result = apr_pstrdup(ctx->p, arg);
1000 ap_str_tolower(result);
1004 static const char *toupper_func(ap_expr_eval_ctx_t *ctx, const void *data,
1007 char *result = apr_pstrdup(ctx->p, arg);
1008 ap_str_toupper(result);
1012 static const char *escape_func(ap_expr_eval_ctx_t *ctx, const void *data,
1015 return ap_escape_uri(ctx->p, arg);
1018 #define MAX_FILE_SIZE 10*1024*1024
1019 static const char *file_func(ap_expr_eval_ctx_t *ctx, const void *data,
1028 if (apr_file_open(&fp, arg, APR_READ|APR_BUFFERED,
1029 APR_OS_DEFAULT, ctx->p) != APR_SUCCESS) {
1030 *ctx->err = apr_psprintf(ctx->p, "Cannot open file %s", arg);
1033 apr_file_info_get(&finfo, APR_FINFO_SIZE, fp);
1034 if (finfo.size > MAX_FILE_SIZE) {
1035 *ctx->err = apr_psprintf(ctx->p, "File %s too large", arg);
1039 len = (apr_size_t)finfo.size;
1045 if ((buf = (char *)apr_palloc(ctx->p, sizeof(char)*(len+1))) == NULL) {
1046 *ctx->err = "Cannot allocate memory";
1051 apr_file_seek(fp, APR_SET, &offset);
1052 if (apr_file_read(fp, buf, &len) != APR_SUCCESS) {
1053 *ctx->err = apr_psprintf(ctx->p, "Cannot read from file %s", arg);
1063 static const char *filesize_func(ap_expr_eval_ctx_t *ctx, const void *data,
1067 if (apr_stat(&sb, arg, APR_FINFO_MIN, ctx->p) == APR_SUCCESS
1068 && sb.filetype == APR_REG && sb.size > 0)
1069 return apr_psprintf(ctx->p, "%" APR_OFF_T_FMT, sb.size);
1074 static const char *unescape_func(ap_expr_eval_ctx_t *ctx, const void *data,
1077 char *result = apr_pstrdup(ctx->p, arg);
1078 int ret = ap_unescape_url_keep2f(result, 0);
1081 ap_log_rerror(LOG_MARK(ctx->info), APLOG_DEBUG, 0, ctx->r, APLOGNO(00538)
1082 "%s %% escape in unescape('%s') at %s:%d",
1083 ret == HTTP_BAD_REQUEST ? "Bad" : "Forbidden", arg,
1084 ctx->info->filename, ctx->info->line_number);
1088 static int op_nz(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1090 const char *name = (const char *)data;
1092 return (arg[0] == '\0');
1094 return (arg[0] != '\0');
1097 static int op_file_min(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1100 const char *name = (const char *)data;
1101 if (apr_stat(&sb, arg, APR_FINFO_MIN, ctx->p) != APR_SUCCESS)
1105 return (sb.filetype == APR_DIR);
1109 return (sb.filetype == APR_REG);
1111 return (sb.filetype == APR_REG && sb.size > 0);
1118 static int op_file_link(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1122 if (apr_stat(&sb, arg, APR_FINFO_MIN | APR_FINFO_LINK, ctx->p) == APR_SUCCESS
1123 && sb.filetype == APR_LNK) {
1130 static int op_file_xbit(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1133 if (apr_stat(&sb, arg, APR_FINFO_PROT| APR_FINFO_LINK, ctx->p) == APR_SUCCESS
1134 && (sb.protection & (APR_UEXECUTE | APR_GEXECUTE | APR_WEXECUTE))) {
1140 static int op_url_subr(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1143 request_rec *rsub, *r = ctx->r;
1146 /* avoid some infinite recursions */
1147 if (r->main && r->main->uri && r->uri && strcmp(r->main->uri, r->uri) == 0)
1150 rsub = ap_sub_req_lookup_uri(arg, r, NULL);
1151 if (rsub->status < 400) {
1154 ap_log_rerror(LOG_MARK(ctx->info), APLOG_TRACE5, 0, r,
1155 "Subrequest for -U %s at %s:%d gave status: %d",
1156 arg, ctx->info->filename, ctx->info->line_number,
1158 ap_destroy_sub_req(rsub);
1162 static int op_file_subr(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1166 request_rec *rsub, *r = ctx->r;
1169 rsub = ap_sub_req_lookup_file(arg, r, NULL);
1170 if (rsub->status < 300 &&
1171 /* double-check that file exists since default result is 200 */
1172 apr_stat(&sb, rsub->filename, APR_FINFO_MIN, ctx->p) == APR_SUCCESS) {
1175 ap_log_rerror(LOG_MARK(ctx->info), APLOG_TRACE5, 0, r,
1176 "Subrequest for -F %s at %s:%d gave status: %d",
1177 arg, ctx->info->filename, ctx->info->line_number,
1179 ap_destroy_sub_req(rsub);
1184 APR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *));
1185 static APR_OPTIONAL_FN_TYPE(ssl_is_https) *is_https = NULL;
1187 static const char *conn_var_names[] = {
1190 "CONN_LOG_ID", /* 2 */
1191 "CONN_REMOTE_ADDR", /* 3 */
1195 static const char *conn_var_fn(ap_expr_eval_ctx_t *ctx, const void *data)
1197 int index = ((const char **)data - conn_var_names);
1198 conn_rec *c = ctx->c;
1204 if (is_https && is_https(c))
1211 apr_sockaddr_t *addr = c->peer_addr;
1212 if (addr->family == AF_INET6
1213 && !IN6_IS_ADDR_V4MAPPED((struct in6_addr *)addr->ipaddr_ptr))
1231 static const char *request_var_names[] = {
1232 "REQUEST_METHOD", /* 0 */
1233 "REQUEST_SCHEME", /* 1 */
1234 "REQUEST_URI", /* 2 */
1235 "REQUEST_FILENAME", /* 3 */
1236 "REMOTE_HOST", /* 4 */
1237 "REMOTE_IDENT", /* 5 */
1238 "REMOTE_USER", /* 6 */
1239 "SERVER_ADMIN", /* 7 */
1240 "SERVER_NAME", /* 8 */
1241 "SERVER_PORT", /* 9 */
1242 "SERVER_PROTOCOL", /* 10 */
1243 "SCRIPT_FILENAME", /* 11 */
1244 "PATH_INFO", /* 12 */
1245 "QUERY_STRING", /* 13 */
1246 "IS_SUBREQ", /* 14 */
1247 "DOCUMENT_ROOT", /* 15 */
1248 "AUTH_TYPE", /* 16 */
1249 "THE_REQUEST", /* 17 */
1250 "CONTENT_TYPE", /* 18 */
1252 "REQUEST_LOG_ID", /* 20 */
1253 "SCRIPT_USER", /* 21 */
1254 "SCRIPT_GROUP", /* 22 */
1255 "DOCUMENT_URI", /* 23 */
1256 "LAST_MODIFIED", /* 24 */
1257 "CONTEXT_PREFIX", /* 25 */
1258 "CONTEXT_DOCUMENT_ROOT", /* 26 */
1259 "REQUEST_STATUS", /* 27 */
1260 "REMOTE_ADDR", /* 28 */
1264 static const char *request_var_fn(ap_expr_eval_ctx_t *ctx, const void *data)
1266 int index = ((const char **)data - request_var_names);
1267 request_rec *r = ctx->r;
1275 return ap_http_scheme(r);
1281 return ap_get_remote_host(r->connection, r->per_dir_config,
1284 return ap_get_remote_logname(r);
1288 return r->server->server_admin;
1290 return ap_get_server_name_for_url(r);
1292 return apr_psprintf(ctx->p, "%u", ap_get_server_port(r));
1298 return r->path_info;
1302 return (r->main != NULL ? "true" : "false");
1304 return ap_document_root(r);
1306 return r->ap_auth_type;
1308 return r->the_request;
1310 return r->content_type;
1318 if (r->finfo.valid & APR_FINFO_USER)
1319 apr_uid_name_get(&result, r->finfo.user, ctx->p);
1325 if (r->finfo.valid & APR_FINFO_USER)
1326 apr_gid_name_get(&result, r->finfo.group, ctx->p);
1334 apr_time_exp_lt(&tm, r->mtime);
1335 return apr_psprintf(ctx->p, "%02d%02d%02d%02d%02d%02d%02d",
1336 (tm.tm_year / 100) + 19, (tm.tm_year % 100),
1337 tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min,
1341 return ap_context_prefix(r);
1343 return ap_context_document_root(r);
1345 return r->status ? apr_psprintf(ctx->p, "%d", r->status) : "";
1347 return r->client_ip;
1354 static const char *req_header_var_names[] = {
1356 "HTTP_PROXY_CONNECTION",
1365 static const char *req_header_header_names[] = {
1375 static const char *req_header_var_fn(ap_expr_eval_ctx_t *ctx, const void *data)
1377 const char **varname = (const char **)data;
1378 int index = (varname - req_header_var_names);
1381 AP_DEBUG_ASSERT(index < 6);
1385 name = req_header_header_names[index];
1386 add_vary(ctx, name);
1387 return apr_table_get(ctx->r->headers_in, name);
1390 static const char *misc_var_names[] = {
1391 "TIME_YEAR", /* 0 */
1394 "TIME_HOUR", /* 3 */
1397 "TIME_WDAY", /* 6 */
1399 "SERVER_SOFTWARE", /* 8 */
1400 "API_VERSION", /* 9 */
1404 static const char *misc_var_fn(ap_expr_eval_ctx_t *ctx, const void *data)
1407 int index = ((const char **)data - misc_var_names);
1408 apr_time_exp_lt(&tm, apr_time_now());
1412 return apr_psprintf(ctx->p, "%02d%02d", (tm.tm_year / 100) + 19,
1415 return apr_psprintf(ctx->p, "%02d", tm.tm_mon+1);
1417 return apr_psprintf(ctx->p, "%02d", tm.tm_mday);
1419 return apr_psprintf(ctx->p, "%02d", tm.tm_hour);
1421 return apr_psprintf(ctx->p, "%02d", tm.tm_min);
1423 return apr_psprintf(ctx->p, "%02d", tm.tm_sec);
1425 return apr_psprintf(ctx->p, "%d", tm.tm_wday);
1427 return apr_psprintf(ctx->p, "%02d%02d%02d%02d%02d%02d%02d",
1428 (tm.tm_year / 100) + 19, (tm.tm_year % 100),
1429 tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min,
1432 return ap_get_server_banner();
1434 return apr_itoa(ctx->p, MODULE_MAGIC_NUMBER);
1442 static int subnet_parse_arg(ap_expr_lookup_parms *parms)
1444 apr_ipsubnet_t *subnet;
1445 const char *addr = parms->arg;
1450 *parms->err = apr_psprintf(parms->ptemp,
1451 "-%s requires subnet/netmask as constant argument",
1456 mask = ap_strchr_c(addr, '/');
1458 addr = apr_pstrmemdup(parms->ptemp, addr, mask - addr);
1462 ret = apr_ipsubnet_create(&subnet, addr, mask, parms->pool);
1463 if (ret != APR_SUCCESS) {
1464 *parms->err = "parsing of subnet/netmask failed";
1468 *parms->data = subnet;
1472 static int op_ipmatch(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg1,
1475 apr_ipsubnet_t *subnet = (apr_ipsubnet_t *)data;
1476 apr_sockaddr_t *saddr;
1478 AP_DEBUG_ASSERT(subnet != NULL);
1480 /* maybe log an error if this goes wrong? */
1481 if (apr_sockaddr_info_get(&saddr, arg1, APR_UNSPEC, 0, 0, ctx->p) != APR_SUCCESS)
1484 return apr_ipsubnet_test(subnet, saddr);
1487 static int op_R(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg1)
1489 apr_ipsubnet_t *subnet = (apr_ipsubnet_t *)data;
1491 AP_DEBUG_ASSERT(subnet != NULL);
1496 return apr_ipsubnet_test(subnet, ctx->r->client_addr);
1499 static int op_T(ap_expr_eval_ctx_t *ctx, const void *data, const char *arg)
1506 return strcasecmp(arg, "off") == 0 ? FALSE : TRUE;
1509 return strcasecmp(arg, "no") == 0 ? FALSE : TRUE;
1512 return strcasecmp(arg, "false") == 0 ? FALSE : TRUE;
1514 return arg[1] == '\0' ? FALSE : TRUE;
1520 static int op_fnmatch(ap_expr_eval_ctx_t *ctx, const void *data,
1521 const char *arg1, const char *arg2)
1523 return (APR_SUCCESS == apr_fnmatch(arg2, arg1, APR_FNM_PATHNAME));
1526 static int op_strmatch(ap_expr_eval_ctx_t *ctx, const void *data,
1527 const char *arg1, const char *arg2)
1529 return (APR_SUCCESS == apr_fnmatch(arg2, arg1, 0));
1532 static int op_strcmatch(ap_expr_eval_ctx_t *ctx, const void *data,
1533 const char *arg1, const char *arg2)
1535 return (APR_SUCCESS == apr_fnmatch(arg2, arg1, APR_FNM_CASE_BLIND));
1538 struct expr_provider_single {
1541 ap_expr_lookup_fn_t *arg_parsing_func;
1545 struct expr_provider_multi {
1550 static const struct expr_provider_multi var_providers[] = {
1551 { misc_var_fn, misc_var_names },
1552 { req_header_var_fn, req_header_var_names },
1553 { request_var_fn, request_var_names },
1554 { conn_var_fn, conn_var_names },
1558 static const struct expr_provider_single string_func_providers[] = {
1559 { osenv_func, "osenv", NULL, 0 },
1560 { env_func, "env", NULL, 0 },
1561 { req_table_func, "resp", NULL, 0 },
1562 { req_table_func, "req", NULL, 0 },
1563 /* 'http' as alias for 'req' for compatibility with ssl_expr */
1564 { req_table_func, "http", NULL, 0 },
1565 { req_table_func, "note", NULL, 0 },
1566 { req_table_func, "reqenv", NULL, 0 },
1567 { tolower_func, "tolower", NULL, 0 },
1568 { toupper_func, "toupper", NULL, 0 },
1569 { escape_func, "escape", NULL, 0 },
1570 { unescape_func, "unescape", NULL, 0 },
1571 { file_func, "file", NULL, 1 },
1572 { filesize_func, "filesize", NULL, 1 },
1575 /* XXX: base64 encode/decode ? */
1577 static const struct expr_provider_single unary_op_providers[] = {
1578 { op_nz, "n", NULL, 0 },
1579 { op_nz, "z", NULL, 0 },
1580 { op_R, "R", subnet_parse_arg, 0 },
1581 { op_T, "T", NULL, 0 },
1582 { op_file_min, "d", NULL, 1 },
1583 { op_file_min, "e", NULL, 1 },
1584 { op_file_min, "f", NULL, 1 },
1585 { op_file_min, "s", NULL, 1 },
1586 { op_file_link, "L", NULL, 1 },
1587 { op_file_link, "h", NULL, 1 },
1588 { op_file_xbit, "x", NULL, 1 },
1589 { op_file_subr, "F", NULL, 0 },
1590 { op_url_subr, "U", NULL, 0 },
1591 { op_url_subr, "A", NULL, 0 },
1592 { NULL, NULL, NULL }
1595 static const struct expr_provider_single binary_op_providers[] = {
1596 { op_ipmatch, "ipmatch", subnet_parse_arg, 0 },
1597 { op_fnmatch, "fnmatch", NULL, 0 },
1598 { op_strmatch, "strmatch", NULL, 0 },
1599 { op_strcmatch, "strcmatch", NULL, 0 },
1600 { NULL, NULL, NULL }
1603 static int core_expr_lookup(ap_expr_lookup_parms *parms)
1605 switch (parms->type) {
1606 case AP_EXPR_FUNC_VAR: {
1607 const struct expr_provider_multi *prov = var_providers;
1608 while (prov->func) {
1609 const char **name = prov->names;
1611 if (strcasecmp(*name, parms->name) == 0) {
1612 *parms->func = prov->func;
1613 *parms->data = name;
1622 case AP_EXPR_FUNC_STRING:
1623 case AP_EXPR_FUNC_OP_UNARY:
1624 case AP_EXPR_FUNC_OP_BINARY: {
1625 const struct expr_provider_single *prov;
1626 switch (parms->type) {
1627 case AP_EXPR_FUNC_STRING:
1628 prov = string_func_providers;
1630 case AP_EXPR_FUNC_OP_UNARY:
1631 prov = unary_op_providers;
1633 case AP_EXPR_FUNC_OP_BINARY:
1634 prov = binary_op_providers;
1639 while (prov->func) {
1641 if (parms->type == AP_EXPR_FUNC_OP_UNARY)
1642 match = !strcmp(prov->name, parms->name);
1644 match = !strcasecmp(prov->name, parms->name);
1646 if ((parms->flags & AP_EXPR_FLAG_RESTRICTED)
1647 && prov->restricted) {
1649 apr_psprintf(parms->ptemp,
1650 "%s%s not available in restricted context",
1651 (parms->type == AP_EXPR_FUNC_STRING) ? "" : "-",
1655 *parms->func = prov->func;
1656 if (prov->arg_parsing_func) {
1657 return prov->arg_parsing_func(parms);
1660 *parms->data = prov->name;
1674 static int expr_lookup_not_found(ap_expr_lookup_parms *parms)
1677 const char *prefix = "";
1679 switch (parms->type) {
1680 case AP_EXPR_FUNC_VAR:
1683 case AP_EXPR_FUNC_STRING:
1686 case AP_EXPR_FUNC_LIST:
1687 type = "List-returning function";
1689 case AP_EXPR_FUNC_OP_UNARY:
1690 type = "Unary operator";
1692 case AP_EXPR_FUNC_OP_BINARY:
1693 type = "Binary operator";
1696 *parms->err = "Inavalid expression type in expr_lookup";
1699 if ( parms->type == AP_EXPR_FUNC_OP_UNARY
1700 || parms->type == AP_EXPR_FUNC_OP_BINARY) {
1703 *parms->err = apr_psprintf(parms->ptemp, "%s '%s%s' does not exist", type,
1704 prefix, parms->name);
1708 static int ap_expr_post_config(apr_pool_t *pconf, apr_pool_t *plog,
1709 apr_pool_t *ptemp, server_rec *s)
1711 is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
1712 apr_pool_cleanup_register(pconf, &is_https, ap_pool_cleanup_set_null,
1713 apr_pool_cleanup_null);
1717 void ap_expr_init(apr_pool_t *p)
1719 ap_hook_expr_lookup(core_expr_lookup, NULL, NULL, APR_HOOK_MIDDLE);
1720 ap_hook_expr_lookup(expr_lookup_not_found, NULL, NULL, APR_HOOK_REALLY_LAST);
1721 ap_hook_post_config(ap_expr_post_config, NULL, NULL, APR_HOOK_MIDDLE);