ap_expr.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. * @file ap_expr.h
  18. * @brief Expression parser
  19. *
  20. * @defgroup AP_EXPR Expression parser
  21. * @ingroup APACHE_CORE
  22. * @{
  23. */
  24. #ifndef AP_EXPR_H
  25. #define AP_EXPR_H
  26. #include "httpd.h"
  27. #include "http_config.h"
  28. #include "ap_regex.h"
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /** A node in the expression parse tree */
  33. typedef struct ap_expr_node ap_expr_t;
  34. /** Struct describing a parsed expression */
  35. typedef struct {
  36. /** The root of the actual expression parse tree */
  37. ap_expr_t *root_node;
  38. /** The filename where the expression has been defined (for logging).
  39. * May be NULL
  40. */
  41. const char *filename;
  42. /** The line number where the expression has been defined (for logging). */
  43. unsigned int line_number;
  44. /** Flags relevant for the expression, see AP_EXPR_FLAG_* */
  45. unsigned int flags;
  46. /** The module that is used for loglevel configuration */
  47. int module_index;
  48. } ap_expr_info_t;
  49. /** Use ssl_expr compatibility mode (changes the meaning of the comparison
  50. * operators)
  51. */
  52. #define AP_EXPR_FLAG_SSL_EXPR_COMPAT 1
  53. /** Don't add siginificant request headers to the Vary response header */
  54. #define AP_EXPR_FLAG_DONT_VARY 2
  55. /** Don't allow functions/vars that bypass the current request's access
  56. * restrictions or would otherwise leak confidential information.
  57. * Used by e.g. mod_include.
  58. */
  59. #define AP_EXPR_FLAG_RESTRICTED 4
  60. /** Expression evaluates to a string, not to a bool */
  61. #define AP_EXPR_FLAG_STRING_RESULT 8
  62. /**
  63. * Evaluate a parse tree, simple interface
  64. * @param r The current request
  65. * @param expr The expression to be evaluated
  66. * @param err Where an error message should be stored
  67. * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
  68. * @note err will be set to NULL on success, or to an error message on error
  69. * @note request headers used during evaluation will be added to the Vary:
  70. * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
  71. */
  72. AP_DECLARE(int) ap_expr_exec(request_rec *r, const ap_expr_info_t *expr,
  73. const char **err);
  74. /**
  75. * Evaluate a parse tree, with access to regexp backreference
  76. * @param r The current request
  77. * @param expr The expression to be evaluated
  78. * @param nmatch size of the regex match vector pmatch
  79. * @param pmatch information about regex matches
  80. * @param source the string that pmatch applies to
  81. * @param err Where an error message should be stored
  82. * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
  83. * @note err will be set to NULL on success, or to an error message on error
  84. * @note nmatch/pmatch/source can be used both to make previous matches
  85. * available to ap_expr_exec_re and to use ap_expr_exec_re's matches
  86. * later on.
  87. * @note request headers used during evaluation will be added to the Vary:
  88. * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
  89. */
  90. AP_DECLARE(int) ap_expr_exec_re(request_rec *r, const ap_expr_info_t *expr,
  91. apr_size_t nmatch, ap_regmatch_t *pmatch,
  92. const char **source, const char **err);
  93. /** Context used during evaluation of a parse tree, created by ap_expr_exec */
  94. typedef struct {
  95. /** the current request */
  96. request_rec *r;
  97. /** the current connection */
  98. conn_rec *c;
  99. /** the current virtual host */
  100. server_rec *s;
  101. /** the pool to use */
  102. apr_pool_t *p;
  103. /** where to store the error string */
  104. const char **err;
  105. /** ap_expr_info_t for the expression */
  106. const ap_expr_info_t *info;
  107. /** regex match information for back references */
  108. ap_regmatch_t *re_pmatch;
  109. /** size of the vector pointed to by re_pmatch */
  110. apr_size_t re_nmatch;
  111. /** the string corresponding to the re_pmatch */
  112. const char **re_source;
  113. /** A string where the comma separated names of headers are stored
  114. * to be later added to the Vary: header. If NULL, the caller is not
  115. * interested in this information.
  116. */
  117. const char **vary_this;
  118. /** where to store the result string */
  119. const char **result_string;
  120. /** Arbitrary context data provided by the caller for custom functions */
  121. void *data;
  122. /** The current recursion level */
  123. int reclvl;
  124. } ap_expr_eval_ctx_t;
  125. /**
  126. * Evaluate a parse tree, full featured version
  127. * @param ctx The evaluation context with all data filled in
  128. * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
  129. * @note *ctx->err will be set to NULL on success, or to an error message on
  130. * error
  131. * @note request headers used during evaluation will be added to the Vary:
  132. * response header if ctx->vary_this is set.
  133. */
  134. AP_DECLARE(int) ap_expr_exec_ctx(ap_expr_eval_ctx_t *ctx);
  135. /**
  136. * Evaluate a parse tree of a string valued expression
  137. * @param r The current request
  138. * @param expr The expression to be evaluated
  139. * @param err Where an error message should be stored
  140. * @return The result string, NULL on error
  141. * @note err will be set to NULL on success, or to an error message on error
  142. * @note request headers used during evaluation will be added to the Vary:
  143. * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
  144. */
  145. AP_DECLARE(const char *) ap_expr_str_exec(request_rec *r,
  146. const ap_expr_info_t *expr,
  147. const char **err);
  148. /**
  149. * Evaluate a parse tree of a string valued expression
  150. * @param r The current request
  151. * @param expr The expression to be evaluated
  152. * @param nmatch size of the regex match vector pmatch
  153. * @param pmatch information about regex matches
  154. * @param source the string that pmatch applies to
  155. * @param err Where an error message should be stored
  156. * @return The result string, NULL on error
  157. * @note err will be set to NULL on success, or to an error message on error
  158. * @note nmatch/pmatch/source can be used both to make previous matches
  159. * available to ap_expr_exec_re and to use ap_expr_exec_re's matches
  160. * later on.
  161. * @note request headers used during evaluation will be added to the Vary:
  162. * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
  163. */
  164. AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r,
  165. const ap_expr_info_t *expr,
  166. apr_size_t nmatch,
  167. ap_regmatch_t *pmatch,
  168. const char **source,
  169. const char **err);
  170. /**
  171. * The parser can be extended with variable lookup, functions, and
  172. * and operators.
  173. *
  174. * During parsing, the parser calls the lookup function to resolve a
  175. * name into a function pointer and an opaque context for the function.
  176. * If the argument to a function or operator is constant, the lookup function
  177. * may also parse that argument and store the parsed data in the context.
  178. *
  179. * The default lookup function is the hook ::ap_expr_lookup_default which just
  180. * calls ap_run_expr_lookup. Modules can use it to make functions and
  181. * variables generally available.
  182. *
  183. * An ap_expr consumer can also provide its own custom lookup function to
  184. * modify the set of variables and functions that are available. The custom
  185. * lookup function can in turn call 'ap_run_expr_lookup'.
  186. */
  187. /** Unary operator, takes one string argument and returns a bool value.
  188. * The name must have the form '-z' (one letter only).
  189. * @param ctx The evaluation context
  190. * @param data An opaque context provided by the lookup hook function
  191. * @param arg The (right) operand
  192. * @return 0 or 1
  193. */
  194. typedef int ap_expr_op_unary_t(ap_expr_eval_ctx_t *ctx, const void *data,
  195. const char *arg);
  196. /** Binary operator, takes two string arguments and returns a bool value.
  197. * The name must have the form '-cmp' (at least two letters).
  198. * @param ctx The evaluation context
  199. * @param data An opaque context provided by the lookup hook function
  200. * @param arg1 The left operand
  201. * @param arg2 The right operand
  202. * @return 0 or 1
  203. */
  204. typedef int ap_expr_op_binary_t(ap_expr_eval_ctx_t *ctx, const void *data,
  205. const char *arg1, const char *arg2);
  206. /** String valued function, takes a string argument and returns a string
  207. * @param ctx The evaluation context
  208. * @param data An opaque context provided by the lookup hook function
  209. * @param arg The argument
  210. * @return The functions result string, may be NULL for 'empty string'
  211. */
  212. typedef const char *(ap_expr_string_func_t)(ap_expr_eval_ctx_t *ctx,
  213. const void *data,
  214. const char *arg);
  215. /** List valued function, takes a string argument and returns a list of strings
  216. * Can currently only be called following the builtin '-in' operator.
  217. * @param ctx The evaluation context
  218. * @param data An opaque context provided by the lookup hook function
  219. * @param arg The argument
  220. * @return The functions result list of strings, may be NULL for 'empty array'
  221. */
  222. typedef apr_array_header_t *(ap_expr_list_func_t)(ap_expr_eval_ctx_t *ctx,
  223. const void *data,
  224. const char *arg);
  225. /** Variable lookup function, takes no argument and returns a string
  226. * @param ctx The evaluation context
  227. * @param data An opaque context provided by the lookup hook function
  228. * @return The expanded variable
  229. */
  230. typedef const char *(ap_expr_var_func_t)(ap_expr_eval_ctx_t *ctx,
  231. const void *data);
  232. /** parameter struct passed to the lookup hook functions */
  233. typedef struct {
  234. /** type of the looked up object */
  235. int type;
  236. #define AP_EXPR_FUNC_VAR 0
  237. #define AP_EXPR_FUNC_STRING 1
  238. #define AP_EXPR_FUNC_LIST 2
  239. #define AP_EXPR_FUNC_OP_UNARY 3
  240. #define AP_EXPR_FUNC_OP_BINARY 4
  241. /** name of the looked up object */
  242. const char *name;
  243. int flags;
  244. apr_pool_t *pool;
  245. apr_pool_t *ptemp;
  246. /** where to store the function pointer */
  247. const void **func;
  248. /** where to store the function's context */
  249. const void **data;
  250. /** where to store the error message (if any) */
  251. const char **err;
  252. /** arg for pre-parsing (only if a simple string).
  253. * For binary ops, this is the right argument. */
  254. const char *arg;
  255. } ap_expr_lookup_parms;
  256. /** Function for looking up the provider function for a variable, operator
  257. * or function in an expression.
  258. * @param parms The parameter struct, also determins where the result is
  259. * stored.
  260. * @return OK on success,
  261. * !OK on failure,
  262. * DECLINED if the requested name is not handled by this function
  263. */
  264. typedef int (ap_expr_lookup_fn_t)(ap_expr_lookup_parms *parms);
  265. /** Default lookup function which just calls ap_run_expr_lookup().
  266. * ap_run_expr_lookup cannot be used directly because it has the wrong
  267. * calling convention under Windows.
  268. */
  269. AP_DECLARE_NONSTD(int) ap_expr_lookup_default(ap_expr_lookup_parms *parms);
  270. AP_DECLARE_HOOK(int, expr_lookup, (ap_expr_lookup_parms *parms))
  271. /**
  272. * Parse an expression into a parse tree
  273. * @param pool Pool
  274. * @param ptemp temp pool
  275. * @param info The ap_expr_info_t struct (with values filled in)
  276. * @param expr The expression string to parse
  277. * @param lookup_fn The lookup function to use, NULL for default
  278. * @return NULL on success, error message on error.
  279. * A pointer to the resulting parse tree will be stored in
  280. * info->root_node.
  281. */
  282. AP_DECLARE(const char *) ap_expr_parse(apr_pool_t *pool, apr_pool_t *ptemp,
  283. ap_expr_info_t *info, const char *expr,
  284. ap_expr_lookup_fn_t *lookup_fn);
  285. /**
  286. * High level interface to ap_expr_parse that also creates ap_expr_info_t and
  287. * uses info from cmd_parms to fill in most of it.
  288. * @param cmd The cmd_parms struct
  289. * @param expr The expression string to parse
  290. * @param flags The flags to use, see AP_EXPR_FLAG_*
  291. * @param err Set to NULL on success, error message on error
  292. * @param lookup_fn The lookup function used to lookup vars, functions, and
  293. * operators
  294. * @param module_index The module_index to set for the expression
  295. * @return The parsed expression
  296. * @note Usually ap_expr_parse_cmd() should be used
  297. */
  298. AP_DECLARE(ap_expr_info_t *) ap_expr_parse_cmd_mi(const cmd_parms *cmd,
  299. const char *expr,
  300. unsigned int flags,
  301. const char **err,
  302. ap_expr_lookup_fn_t *lookup_fn,
  303. int module_index);
  304. /**
  305. * Convenience wrapper for ap_expr_parse_cmd_mi() that sets
  306. * module_index = APLOG_MODULE_INDEX
  307. */
  308. #define ap_expr_parse_cmd(cmd, expr, flags, err, lookup_fn) \
  309. ap_expr_parse_cmd_mi(cmd, expr, flags, err, lookup_fn, APLOG_MODULE_INDEX)
  310. /**
  311. * Internal initialisation of ap_expr (for httpd internal use)
  312. */
  313. void ap_expr_init(apr_pool_t *pool);
  314. #ifdef __cplusplus
  315. }
  316. #endif
  317. #endif /* AP_EXPR_H */
  318. /** @} */