PageRenderTime 61ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/modsecurity-apache_2.6.1/apache2/modsecurity.h

http://vulture.googlecode.com/
C Header | 619 lines | 399 code | 147 blank | 73 comment | 3 complexity | 371e02edccca0c00edab85ef52f41e72 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * ModSecurity for Apache 2.x, http://www.modsecurity.org/
  3. * Copyright (c) 2004-2011 Trustwave Holdings, Inc. (http://www.trustwave.com/)
  4. *
  5. * 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. * If any of the files related to licensing are missing or if you have any
  11. * other questions related to licensing please contact Trustwave Holdings, Inc.
  12. * directly using the email address security@modsecurity.org.
  13. */
  14. #ifndef _MODSECURITY_H_
  15. #define _MODSECURITY_H_
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <limits.h>
  19. typedef struct rule_exception rule_exception;
  20. typedef struct modsec_rec modsec_rec;
  21. typedef struct directory_config directory_config;
  22. typedef struct error_message error_message;
  23. typedef struct msc_engine msc_engine;
  24. typedef struct msc_data_chunk msc_data_chunk;
  25. typedef struct msc_arg msc_arg;
  26. typedef struct msc_string msc_string;
  27. typedef struct msc_parm msc_parm;
  28. #include "msc_release.h"
  29. #include "msc_logging.h"
  30. #include "msc_multipart.h"
  31. #include "msc_pcre.h"
  32. #include "msc_util.h"
  33. #include "msc_xml.h"
  34. #include "msc_geo.h"
  35. #include "msc_gsb.h"
  36. #include "msc_unicode.h"
  37. #include "re.h"
  38. #include "ap_config.h"
  39. #include "apr_md5.h"
  40. #include "apr_strings.h"
  41. #include "apr_hash.h"
  42. #include "httpd.h"
  43. #include "http_config.h"
  44. #include "http_log.h"
  45. #include "http_protocol.h"
  46. #define PHASE_REQUEST_HEADERS 1
  47. #define PHASE_REQUEST_BODY 2
  48. #define PHASE_RESPONSE_HEADERS 3
  49. #define PHASE_RESPONSE_BODY 4
  50. #define PHASE_LOGGING 5
  51. #define PHASE_FIRST PHASE_REQUEST_HEADERS
  52. #define PHASE_LAST PHASE_LOGGING
  53. #define NOT_SET -1l
  54. #define NOT_SET_P ((void *)-1l)
  55. #define CREATEMODE ( APR_UREAD | APR_UWRITE | APR_GREAD )
  56. #define CREATEMODE_DIR ( APR_UREAD | APR_UWRITE | APR_UEXECUTE | APR_GREAD | APR_GEXECUTE )
  57. #if defined(NETWARE)
  58. #define CREATEMODE_UNISTD ( S_IREAD | S_IWRITE )
  59. #elif defined(WIN32)
  60. #define CREATEMODE_UNISTD ( _S_IREAD | _S_IWRITE )
  61. #else
  62. #define CREATEMODE_UNISTD ( S_IRUSR | S_IWUSR | S_IRGRP )
  63. #endif
  64. #if !defined(O_BINARY)
  65. #define O_BINARY (0)
  66. #endif
  67. #ifndef PIPE_BUF
  68. #define PIPE_BUF (512)
  69. #endif
  70. #define REQUEST_BODY_HARD_LIMIT 1073741824L
  71. #define REQUEST_BODY_DEFAULT_INMEMORY_LIMIT 131072
  72. #define REQUEST_BODY_DEFAULT_LIMIT 134217728
  73. #define REQUEST_BODY_NO_FILES_DEFAULT_LIMIT 1048576
  74. #define RESPONSE_BODY_DEFAULT_LIMIT 524288
  75. #define RESPONSE_BODY_HARD_LIMIT 1073741824L
  76. #define RESPONSE_BODY_LIMIT_ACTION_REJECT 0
  77. #define RESPONSE_BODY_LIMIT_ACTION_PARTIAL 1
  78. #define REQUEST_BODY_FORCEBUF_OFF 0
  79. #define REQUEST_BODY_FORCEBUF_ON 1
  80. #define REQUEST_BODY_LIMIT_ACTION_REJECT 0
  81. #define REQUEST_BODY_LIMIT_ACTION_PARTIAL 1
  82. #define SECACTION_TARGETS "REMOTE_ADDR"
  83. #define SECACTION_ARGS "@unconditionalMatch"
  84. #define SECMARKER_TARGETS "REMOTE_ADDR"
  85. #define SECMARKER_ARGS "@noMatch"
  86. #define SECMARKER_BASE_ACTIONS "t:none,pass,id:"
  87. #if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
  88. #include "unixd.h"
  89. #define __SET_MUTEX_PERMS
  90. #endif
  91. #define COOKIES_V0 0
  92. #define COOKIES_V1 1
  93. #ifdef WIN32
  94. #include <direct.h>
  95. #else
  96. #include <sys/types.h>
  97. #include <unistd.h>
  98. #endif
  99. #define NOTE_MSR "modsecurity-tx-context"
  100. #define FATAL_ERROR "ModSecurity: Fatal error (memory allocation or unexpected internal error)!"
  101. extern DSOLOCAL char *new_server_signature;
  102. extern DSOLOCAL char *chroot_dir;
  103. extern module AP_MODULE_DECLARE_DATA security2_module;
  104. extern DSOLOCAL const command_rec module_directives[];
  105. extern DSOLOCAL unsigned long int msc_pcre_match_limit;
  106. extern DSOLOCAL unsigned long int msc_pcre_match_limit_recursion;
  107. extern DSOLOCAL unsigned long int conn_read_state_limit;
  108. extern DSOLOCAL unsigned long int conn_write_state_limit;
  109. extern DSOLOCAL unsigned long int unicode_codepage;
  110. extern DSOLOCAL int *unicode_map_table;
  111. #define RESBODY_STATUS_NOT_READ 0 /* we were not configured to read the body */
  112. #define RESBODY_STATUS_ERROR 1 /* error occured while we were reading the body */
  113. #define RESBODY_STATUS_PARTIAL 2 /* partial body content available in the brigade */
  114. #define RESBODY_STATUS_READ_BRIGADE 3 /* body was read but not flattened */
  115. #define RESBODY_STATUS_READ 4 /* body was read and flattened */
  116. #define IF_STATUS_NONE 0
  117. #define IF_STATUS_WANTS_TO_RUN 1
  118. #define IF_STATUS_COMPLETE 2
  119. #define OF_STATUS_NOT_STARTED 0
  120. #define OF_STATUS_IN_PROGRESS 1
  121. #define OF_STATUS_COMPLETE 2
  122. #define MSC_REQBODY_NONE 0
  123. #define MSC_REQBODY_MEMORY 1
  124. #define MSC_REQBODY_DISK 2
  125. #define ACTION_NONE 0
  126. #define ACTION_DENY 1
  127. #define ACTION_REDIRECT 2
  128. #define ACTION_PROXY 3
  129. #define ACTION_DROP 4
  130. #define ACTION_ALLOW 5
  131. #define ACTION_ALLOW_REQUEST 6
  132. #define ACTION_ALLOW_PHASE 7
  133. #define MODSEC_DISABLED 0
  134. #define MODSEC_DETECTION_ONLY 1
  135. #define MODSEC_ENABLED 2
  136. #define MODSEC_CACHE_DISABLED 0
  137. #define MODSEC_CACHE_ENABLED 1
  138. #define MODSEC_OFFLINE 0
  139. #define MODSEC_ONLINE 1
  140. #define REGEX_CAPTURE_BUFLEN 1024
  141. #define KEEP_FILES_OFF 0
  142. #define KEEP_FILES_ON 1
  143. #define KEEP_FILES_RELEVANT_ONLY 2
  144. #define RULE_EXCEPTION_IMPORT_ID 1
  145. #define RULE_EXCEPTION_IMPORT_MSG 2
  146. #define RULE_EXCEPTION_REMOVE_ID 3
  147. #define RULE_EXCEPTION_REMOVE_MSG 4
  148. #define RULE_EXCEPTION_REMOVE_TAG 5
  149. #define NBSP 160
  150. struct rule_exception {
  151. int type;
  152. const char *param;
  153. void *param_data;
  154. };
  155. struct modsec_rec {
  156. apr_pool_t *mp;
  157. msc_engine *modsecurity;
  158. request_rec *r_early;
  159. request_rec *r;
  160. directory_config *dcfg1;
  161. directory_config *dcfg2;
  162. directory_config *usercfg;
  163. directory_config *txcfg;
  164. unsigned int reqbody_should_exist;
  165. unsigned int reqbody_chunked;
  166. unsigned int phase;
  167. unsigned int phase_request_headers_complete;
  168. unsigned int phase_request_body_complete;
  169. apr_bucket_brigade *if_brigade;
  170. unsigned int if_status;
  171. unsigned int if_started_forwarding;
  172. apr_size_t reqbody_length;
  173. apr_bucket_brigade *of_brigade;
  174. unsigned int of_status;
  175. unsigned int of_done_reading;
  176. unsigned int of_skipping;
  177. unsigned int of_partial;
  178. unsigned int of_is_error;
  179. unsigned int resbody_status;
  180. apr_size_t resbody_length;
  181. char *resbody_data;
  182. unsigned int resbody_contains_html;
  183. apr_size_t stream_input_length;
  184. char *stream_input_data;
  185. apr_size_t stream_output_length;
  186. char *stream_output_data;
  187. unsigned int of_stream_changed;
  188. unsigned int if_stream_changed;
  189. apr_array_header_t *error_messages;
  190. apr_array_header_t *alerts;
  191. const char *txid;
  192. const char *sessionid;
  193. const char *userid;
  194. const char *server_software;
  195. const char *local_addr;
  196. unsigned int local_port;
  197. const char *local_user;
  198. /* client */
  199. const char *remote_addr;
  200. unsigned int remote_port;
  201. const char *remote_user;
  202. /* request */
  203. const char *request_line;
  204. const char *request_method;
  205. const char *request_uri;
  206. const char *query_string;
  207. const char *request_protocol;
  208. const char *hostname;
  209. apr_table_t *request_headers;
  210. apr_off_t request_content_length;
  211. const char *request_content_type;
  212. apr_table_t *arguments;
  213. apr_table_t *arguments_to_sanitize;
  214. apr_table_t *request_headers_to_sanitize;
  215. apr_table_t *response_headers_to_sanitize;
  216. apr_table_t *request_cookies;
  217. apr_table_t *pattern_to_sanitize;
  218. unsigned int urlencoded_error;
  219. unsigned int inbound_error;
  220. unsigned int outbound_error;
  221. unsigned int is_relevant;
  222. apr_table_t *tx_vars;
  223. /* ENH: refactor to allow arbitrary var tables */
  224. apr_table_t *geo_vars;
  225. /* response */
  226. unsigned int response_status;
  227. const char *status_line;
  228. const char *response_protocol;
  229. apr_table_t *response_headers;
  230. unsigned int response_headers_sent;
  231. apr_off_t bytes_sent;
  232. /* modsecurity request body processing stuff */
  233. unsigned int msc_reqbody_storage; /* on disk or in memory */
  234. unsigned int msc_reqbody_spilltodisk;
  235. unsigned int msc_reqbody_read;
  236. apr_pool_t *msc_reqbody_mp; /* this is where chunks are allocated from */
  237. apr_array_header_t *msc_reqbody_chunks; /* data chunks when stored in memory */
  238. unsigned int msc_reqbody_length; /* the amount of data received */
  239. int msc_reqbody_chunk_position; /* used when retrieving the body */
  240. unsigned int msc_reqbody_chunk_offset; /* offset of the chunk currently in use */
  241. msc_data_chunk *msc_reqbody_chunk_current; /* current chunk */
  242. char *msc_reqbody_buffer;
  243. const char *msc_reqbody_filename; /* when stored on disk */
  244. int msc_reqbody_fd;
  245. msc_data_chunk *msc_reqbody_disk_chunk;
  246. const char *msc_reqbody_processor;
  247. int msc_reqbody_error;
  248. const char *msc_reqbody_error_msg;
  249. apr_size_t msc_reqbody_no_files_length;
  250. multipart_data *mpd; /* MULTIPART processor data structure */
  251. xml_data *xml; /* XML processor data structure */
  252. /* audit logging */
  253. char *new_auditlog_boundary;
  254. char *new_auditlog_filename;
  255. apr_file_t *new_auditlog_fd;
  256. unsigned int new_auditlog_size;
  257. apr_md5_ctx_t new_auditlog_md5ctx;
  258. unsigned int was_intercepted;
  259. unsigned int rule_was_intercepted;
  260. unsigned int intercept_phase;
  261. msre_actionset *intercept_actionset;
  262. const char *intercept_message;
  263. /* performance measurement */
  264. apr_time_t request_time;
  265. apr_time_t time_phase1;
  266. apr_time_t time_phase2;
  267. apr_time_t time_phase3;
  268. apr_time_t time_phase4;
  269. apr_time_t time_phase5;
  270. apr_time_t time_storage_read;
  271. apr_time_t time_storage_write;
  272. apr_time_t time_logging;
  273. apr_time_t time_gc;
  274. apr_array_header_t *matched_rules;
  275. msc_string *matched_var;
  276. int highest_severity;
  277. /* upload */
  278. int upload_extract_files;
  279. int upload_remove_files;
  280. int upload_files_count;
  281. /* other */
  282. apr_table_t *collections_original;
  283. apr_table_t *collections;
  284. apr_table_t *collections_dirty;
  285. /* rule processing temp pool */
  286. apr_pool_t *msc_rule_mptmp;
  287. /* content injection */
  288. const char *content_prepend;
  289. apr_off_t content_prepend_len;
  290. const char *content_append;
  291. apr_off_t content_append_len;
  292. /* data cache */
  293. apr_hash_t *tcache;
  294. apr_size_t tcache_items;
  295. /* removed rules */
  296. apr_array_header_t *removed_rules;
  297. apr_array_header_t *removed_rules_tag;
  298. /* When "allow" is executed the variable below is
  299. * updated to contain the scope of the allow action. Set
  300. * at 0 by default, it will have ACTION_ALLOW if we are
  301. * to allow phases 1-4 and ACTION_ALLOW_REQUEST if we
  302. * are to allow phases 1-2 only.
  303. */
  304. unsigned int allow_scope;
  305. /* matched vars */
  306. apr_table_t *matched_vars;
  307. /* Generic request body processor context to be used by custom parsers. */
  308. void *reqbody_processor_ctx;
  309. };
  310. struct directory_config {
  311. apr_pool_t *mp;
  312. msre_ruleset *ruleset;
  313. int is_enabled;
  314. int reqbody_access;
  315. int reqintercept_oe;
  316. int reqbody_buffering;
  317. long int reqbody_inmemory_limit;
  318. long int reqbody_limit;
  319. long int reqbody_no_files_limit;
  320. int resbody_access;
  321. long int of_limit;
  322. apr_table_t *of_mime_types;
  323. int of_mime_types_cleared;
  324. int of_limit_action;
  325. int if_limit_action;
  326. const char *debuglog_name;
  327. int debuglog_level;
  328. apr_file_t *debuglog_fd;
  329. int cookie_format;
  330. int argument_separator;
  331. int rule_inheritance;
  332. apr_array_header_t *rule_exceptions;
  333. /* -- Audit log -- */
  334. /* Whether audit log should be enabled in the context or not */
  335. int auditlog_flag;
  336. /* AUDITLOG_SERIAL (single file) or AUDITLOG_CONCURRENT (multiple files) */
  337. int auditlog_type;
  338. /* Mode for audit log directories and files */
  339. apr_fileperms_t auditlog_dirperms;
  340. apr_fileperms_t auditlog_fileperms;
  341. /* The name of the audit log file (for the old type), or the
  342. * name of the index file (for the new audit log type)
  343. */
  344. char *auditlog_name;
  345. /* The name of the secondary index file */
  346. char *auditlog2_name;
  347. /* The file descriptors for the files above */
  348. apr_file_t *auditlog_fd;
  349. apr_file_t *auditlog2_fd;
  350. /* For the new-style audit log only, the path where
  351. * audit log entries will be stored
  352. */
  353. char *auditlog_storage_dir;
  354. /* A list of parts to include in the new-style audit log
  355. * entry. By default, it contains 'ABCFHZ'. Have a look at
  356. * the AUDITLOG_PART_* constants above to decipher the
  357. * meaning.
  358. */
  359. char *auditlog_parts;
  360. /* A regular expression that determines if a response
  361. * status is treated as relevant.
  362. */
  363. msc_regex_t *auditlog_relevant_regex;
  364. /* Upload */
  365. const char *tmp_dir;
  366. const char *upload_dir;
  367. int upload_keep_files;
  368. int upload_validates_files;
  369. int upload_filemode; /* int only so NOT_SET works */
  370. int upload_file_limit;
  371. /* Used only in the configuration phase. */
  372. msre_rule *tmp_chain_starter;
  373. msre_actionset *tmp_default_actionset;
  374. apr_table_t *tmp_rule_placeholders;
  375. /* Misc */
  376. const char *data_dir;
  377. const char *webappid;
  378. /* Content injection. */
  379. int content_injection_enabled;
  380. /* Stream Inspection */
  381. int stream_inbody_inspection;
  382. int stream_outbody_inspection;
  383. /* Geo Lookup */
  384. geo_db *geo;
  385. /* Gsb Lookup */
  386. gsb_db *gsb;
  387. /* Unicode map */
  388. unicode_map *u_map;
  389. /* Cache */
  390. int cache_trans;
  391. int cache_trans_incremental;
  392. apr_size_t cache_trans_min;
  393. apr_size_t cache_trans_max;
  394. apr_size_t cache_trans_maxitems;
  395. /* Array to hold signatures of components, which will
  396. * appear in the ModSecurity signature in the audit log.
  397. */
  398. apr_array_header_t *component_signatures;
  399. /* Request character encoding. */
  400. const char *request_encoding;
  401. int disable_backend_compression;
  402. };
  403. struct error_message {
  404. const char *file;
  405. int line;
  406. int level;
  407. apr_status_t status;
  408. const char *message;
  409. };
  410. struct msc_engine {
  411. apr_pool_t *mp;
  412. apr_global_mutex_t *auditlog_lock;
  413. apr_global_mutex_t *geo_lock;
  414. msre_engine *msre;
  415. unsigned int processing_mode;
  416. };
  417. struct msc_data_chunk {
  418. char *data;
  419. apr_size_t length;
  420. unsigned int is_permanent;
  421. };
  422. struct msc_arg {
  423. const char *name;
  424. unsigned int name_len;
  425. unsigned int name_origin_offset;
  426. unsigned int name_origin_len;
  427. const char *value;
  428. unsigned int value_len;
  429. unsigned int value_origin_offset;
  430. unsigned int value_origin_len;
  431. const char *origin;
  432. };
  433. struct msc_string {
  434. char *name;
  435. unsigned int name_len;
  436. char *value;
  437. unsigned int value_len;
  438. };
  439. struct msc_parm {
  440. char *value;
  441. int pad_1;
  442. int pad_2;
  443. };
  444. /* Engine functions */
  445. msc_engine DSOLOCAL *modsecurity_create(apr_pool_t *mp, int processing_mode);
  446. int DSOLOCAL modsecurity_init(msc_engine *msce, apr_pool_t *mp);
  447. void DSOLOCAL modsecurity_child_init(msc_engine *msce);
  448. void DSOLOCAL modsecurity_shutdown(msc_engine *msce);
  449. apr_status_t DSOLOCAL modsecurity_tx_init(modsec_rec *msr);
  450. apr_status_t DSOLOCAL modsecurity_process_phase(modsec_rec *msr, unsigned int phase);
  451. /* Request body functions */
  452. apr_status_t DSOLOCAL modsecurity_request_body_start(modsec_rec *msr, char **error_msg);
  453. apr_status_t DSOLOCAL modsecurity_request_body_store(modsec_rec *msr,
  454. const char *data, apr_size_t length, char **error_msg);
  455. apr_status_t DSOLOCAL modsecurity_request_body_end(modsec_rec *msr, char **error_msg);
  456. apr_status_t DSOLOCAL modsecurity_request_body_to_stream(modsec_rec *msr, const char *buffer, int buflen, char **error_msg);
  457. apr_status_t DSOLOCAL modsecurity_request_body_retrieve_start(modsec_rec *msr, char **error_msg);
  458. apr_status_t DSOLOCAL modsecurity_request_body_retrieve_end(modsec_rec *msr);
  459. /* Retrieves up to nbytes bytes of the request body. Returns 1 on
  460. * success, 0 when there is no more data, or -1 on error. On return
  461. * nbytes will contain the number of bytes stored in the buffer.
  462. */
  463. apr_status_t DSOLOCAL modsecurity_request_body_retrieve(modsec_rec *msr, msc_data_chunk **chunk,
  464. long int nbytes, char **error_msg);
  465. void DSOLOCAL msc_add(modsec_rec *msr, int level, msre_actionset *actionset,
  466. const char *action_message, const char *rule_message);
  467. const char DSOLOCAL *msc_alert_message(modsec_rec *msr, msre_actionset *actionset, const char *action_message,
  468. const char *rule_message);
  469. void DSOLOCAL msc_alert(modsec_rec *msr, int level, msre_actionset *actionset, const char *action_message,
  470. const char *rule_message);
  471. apr_status_t DSOLOCAL modsecurity_request_body_clear(modsec_rec *msr, char **error_msg);
  472. #endif