PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/modsecurity-apache_2.6.1/apache2/apache2_config.c

http://vulture.googlecode.com/
C | 2656 lines | 1961 code | 459 blank | 236 comment | 607 complexity | 352f7e69bbf4a663c7588539ba0ab021 MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  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. #include <limits.h>
  15. #include "modsecurity.h"
  16. #include "msc_logging.h"
  17. #include "msc_util.h"
  18. #include "http_log.h"
  19. #if defined(WITH_LUA)
  20. #include "msc_lua.h"
  21. #endif
  22. /* -- Directory context creation and initialisation -- */
  23. /**
  24. * Creates a fresh directory configuration.
  25. */
  26. void *create_directory_config(apr_pool_t *mp, char *path)
  27. {
  28. directory_config *dcfg = (directory_config *)apr_pcalloc(mp, sizeof(directory_config));
  29. if (dcfg == NULL) return NULL;
  30. #ifdef DEBUG_CONF
  31. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, mp, "Created directory config %pp path %s", dcfg, path);
  32. #endif
  33. dcfg->mp = mp;
  34. dcfg->is_enabled = NOT_SET;
  35. dcfg->reqbody_access = NOT_SET;
  36. dcfg->reqintercept_oe = NOT_SET;
  37. dcfg->reqbody_buffering = NOT_SET;
  38. dcfg->reqbody_inmemory_limit = NOT_SET;
  39. dcfg->reqbody_limit = NOT_SET;
  40. dcfg->reqbody_no_files_limit = NOT_SET;
  41. dcfg->resbody_access = NOT_SET;
  42. dcfg->debuglog_name = NOT_SET_P;
  43. dcfg->debuglog_level = NOT_SET;
  44. dcfg->debuglog_fd = NOT_SET_P;
  45. dcfg->of_limit = NOT_SET;
  46. dcfg->if_limit_action = NOT_SET;
  47. dcfg->of_limit_action = NOT_SET;
  48. dcfg->of_mime_types = NOT_SET_P;
  49. dcfg->of_mime_types_cleared = NOT_SET;
  50. dcfg->cookie_format = NOT_SET;
  51. dcfg->argument_separator = NOT_SET;
  52. dcfg->rule_inheritance = NOT_SET;
  53. dcfg->rule_exceptions = apr_array_make(mp, 16, sizeof(rule_exception *));
  54. /* audit log variables */
  55. dcfg->auditlog_flag = NOT_SET;
  56. dcfg->auditlog_type = NOT_SET;
  57. dcfg->auditlog_dirperms = NOT_SET;
  58. dcfg->auditlog_fileperms = NOT_SET;
  59. dcfg->auditlog_name = NOT_SET_P;
  60. dcfg->auditlog2_name = NOT_SET_P;
  61. dcfg->auditlog_fd = NOT_SET_P;
  62. dcfg->auditlog2_fd = NOT_SET_P;
  63. dcfg->auditlog_storage_dir = NOT_SET_P;
  64. dcfg->auditlog_parts = NOT_SET_P;
  65. dcfg->auditlog_relevant_regex = NOT_SET_P;
  66. dcfg->ruleset = NULL;
  67. /* Upload */
  68. dcfg->tmp_dir = NOT_SET_P;
  69. dcfg->upload_dir = NOT_SET_P;
  70. dcfg->upload_keep_files = NOT_SET;
  71. dcfg->upload_validates_files = NOT_SET;
  72. dcfg->upload_filemode = NOT_SET;
  73. dcfg->upload_file_limit = NOT_SET;
  74. /* These are only used during the configuration process. */
  75. dcfg->tmp_chain_starter = NULL;
  76. dcfg->tmp_default_actionset = NULL;
  77. dcfg->tmp_rule_placeholders = NULL;
  78. /* Misc */
  79. dcfg->data_dir = NOT_SET_P;
  80. dcfg->webappid = NOT_SET_P;
  81. /* Content injection. */
  82. dcfg->content_injection_enabled = NOT_SET;
  83. /* Stream inspection */
  84. dcfg->stream_inbody_inspection = NOT_SET;
  85. dcfg->stream_outbody_inspection = NOT_SET;
  86. /* Geo Lookups */
  87. dcfg->geo = NOT_SET_P;
  88. /* Gsb Lookups */
  89. dcfg->gsb = NOT_SET_P;
  90. /* Unicode Map */
  91. dcfg->u_map = NOT_SET_P;
  92. /* Cache */
  93. dcfg->cache_trans = NOT_SET;
  94. dcfg->cache_trans_incremental = NOT_SET;
  95. dcfg->cache_trans_min = NOT_SET;
  96. dcfg->cache_trans_max = NOT_SET;
  97. dcfg->cache_trans_maxitems = NOT_SET;
  98. dcfg->component_signatures = apr_array_make(mp, 16, sizeof(char *));
  99. dcfg->request_encoding = NOT_SET_P;
  100. dcfg->disable_backend_compression = NOT_SET;
  101. return dcfg;
  102. }
  103. /**
  104. * Copies rules between one phase of two configuration contexts,
  105. * taking exceptions into account.
  106. */
  107. static void copy_rules_phase(apr_pool_t *mp,
  108. apr_array_header_t *parent_phase_arr,
  109. apr_array_header_t *child_phase_arr,
  110. apr_array_header_t *exceptions_arr)
  111. {
  112. rule_exception **exceptions;
  113. msre_rule **rules;
  114. int i, j;
  115. int mode = 0;
  116. rules = (msre_rule **)parent_phase_arr->elts;
  117. for(i = 0; i < parent_phase_arr->nelts; i++) {
  118. msre_rule *rule = (msre_rule *)rules[i];
  119. int copy = 1;
  120. if (mode == 0) {
  121. /* First rule in the chain. */
  122. exceptions = (rule_exception **)exceptions_arr->elts;
  123. for(j = 0; j < exceptions_arr->nelts; j++) {
  124. /* Process exceptions. */
  125. switch(exceptions[j]->type) {
  126. case RULE_EXCEPTION_REMOVE_ID :
  127. if ((rule->actionset != NULL)&&(rule->actionset->id != NULL)) {
  128. int ruleid = atoi(rule->actionset->id);
  129. if (rule_id_in_range(ruleid, exceptions[j]->param)) copy--;
  130. }
  131. break;
  132. case RULE_EXCEPTION_REMOVE_MSG :
  133. if ((rule->actionset != NULL)&&(rule->actionset->msg != NULL)) {
  134. char *my_error_msg = NULL;
  135. int rc = msc_regexec(exceptions[j]->param_data,
  136. rule->actionset->msg, strlen(rule->actionset->msg),
  137. &my_error_msg);
  138. if (rc >= 0) copy--;
  139. }
  140. break;
  141. case RULE_EXCEPTION_REMOVE_TAG :
  142. if ((rule->actionset != NULL)&&(apr_is_empty_table(rule->actionset->actions) == 0)) {
  143. char *my_error_msg = NULL;
  144. const apr_array_header_t *tarr = NULL;
  145. const apr_table_entry_t *telts = NULL;
  146. int c;
  147. tarr = apr_table_elts(rule->actionset->actions);
  148. telts = (const apr_table_entry_t*)tarr->elts;
  149. for (c = 0; c < tarr->nelts; c++) {
  150. msre_action *action = (msre_action *)telts[c].val;
  151. if(strcmp("tag", action->metadata->name) == 0) {
  152. int rc = msc_regexec(exceptions[j]->param_data,
  153. action->param, strlen(action->param),
  154. &my_error_msg);
  155. if (rc >= 0) copy--;
  156. }
  157. }
  158. }
  159. break;
  160. }
  161. }
  162. if (copy > 0) {
  163. #ifdef DEBUG_CONF
  164. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, mp, "Copy rule %pp [id \"%s\"]", rule, rule->actionset->id);
  165. #endif
  166. /* Copy the rule. */
  167. *(msre_rule **)apr_array_push(child_phase_arr) = rule;
  168. if (rule->actionset->is_chained) mode = 2;
  169. } else {
  170. if (rule->actionset->is_chained) mode = 1;
  171. }
  172. } else {
  173. if (mode == 2) {
  174. #ifdef DEBUG_CONF
  175. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, mp, "Copy chain %pp for rule %pp [id \"%s\"]", rule, rule->chain_starter, rule->chain_starter->actionset->id);
  176. #endif
  177. /* Copy the rule (it belongs to the chain we want to include. */
  178. *(msre_rule **)apr_array_push(child_phase_arr) = rule;
  179. }
  180. if ((rule->actionset == NULL)||(rule->actionset->is_chained == 0)) mode = 0;
  181. }
  182. }
  183. }
  184. /**
  185. * Copies rules between two configuration contexts,
  186. * taking exceptions into account.
  187. */
  188. static int copy_rules(apr_pool_t *mp, msre_ruleset *parent_ruleset,
  189. msre_ruleset *child_ruleset,
  190. apr_array_header_t *exceptions_arr)
  191. {
  192. copy_rules_phase(mp, parent_ruleset->phase_request_headers,
  193. child_ruleset->phase_request_headers, exceptions_arr);
  194. copy_rules_phase(mp, parent_ruleset->phase_request_body,
  195. child_ruleset->phase_request_body, exceptions_arr);
  196. copy_rules_phase(mp, parent_ruleset->phase_response_headers,
  197. child_ruleset->phase_response_headers, exceptions_arr);
  198. copy_rules_phase(mp, parent_ruleset->phase_response_body,
  199. child_ruleset->phase_response_body, exceptions_arr);
  200. copy_rules_phase(mp, parent_ruleset->phase_logging,
  201. child_ruleset->phase_logging, exceptions_arr);
  202. return 1;
  203. }
  204. /**
  205. * Merges two directory configurations.
  206. */
  207. void *merge_directory_configs(apr_pool_t *mp, void *_parent, void *_child)
  208. {
  209. directory_config *parent = (directory_config *)_parent;
  210. directory_config *child = (directory_config *)_child;
  211. directory_config *merged = create_directory_config(mp, NULL);
  212. #ifdef DEBUG_CONF
  213. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, mp, "Merge parent %pp child %pp RESULT %pp", _parent, _child, merged);
  214. #endif
  215. if (merged == NULL) return NULL;
  216. /* Use values from the child configuration where possible,
  217. * otherwise use the parent's.
  218. */
  219. merged->is_enabled = (child->is_enabled == NOT_SET
  220. ? parent->is_enabled : child->is_enabled);
  221. /* IO parameters */
  222. merged->reqbody_access = (child->reqbody_access == NOT_SET
  223. ? parent->reqbody_access : child->reqbody_access);
  224. merged->reqbody_buffering = (child->reqbody_buffering == NOT_SET
  225. ? parent->reqbody_buffering : child->reqbody_buffering);
  226. merged->reqbody_inmemory_limit = (child->reqbody_inmemory_limit == NOT_SET
  227. ? parent->reqbody_inmemory_limit : child->reqbody_inmemory_limit);
  228. merged->reqbody_limit = (child->reqbody_limit == NOT_SET
  229. ? parent->reqbody_limit : child->reqbody_limit);
  230. merged->reqbody_no_files_limit = (child->reqbody_no_files_limit == NOT_SET
  231. ? parent->reqbody_no_files_limit : child->reqbody_no_files_limit);
  232. merged->resbody_access = (child->resbody_access == NOT_SET
  233. ? parent->resbody_access : child->resbody_access);
  234. merged->of_limit = (child->of_limit == NOT_SET
  235. ? parent->of_limit : child->of_limit);
  236. merged->if_limit_action = (child->if_limit_action == NOT_SET
  237. ? parent->if_limit_action : child->if_limit_action);
  238. merged->of_limit_action = (child->of_limit_action == NOT_SET
  239. ? parent->of_limit_action : child->of_limit_action);
  240. merged->reqintercept_oe = (child->reqintercept_oe == NOT_SET
  241. ? parent->reqintercept_oe : child->reqintercept_oe);
  242. if (child->of_mime_types != NOT_SET_P) {
  243. /* Child added to the table */
  244. if (child->of_mime_types_cleared == 1) {
  245. /* The list of MIME types was cleared in the child,
  246. * which means the parent's MIME types went away and
  247. * we should not take them into consideration here.
  248. */
  249. merged->of_mime_types = child->of_mime_types;
  250. merged->of_mime_types_cleared = 1;
  251. } else {
  252. /* Add MIME types defined in the child to those
  253. * defined in the parent context.
  254. */
  255. if (parent->of_mime_types == NOT_SET_P) {
  256. merged->of_mime_types = child->of_mime_types;
  257. merged->of_mime_types_cleared = NOT_SET;
  258. } else {
  259. merged->of_mime_types = apr_table_overlay(mp, parent->of_mime_types,
  260. child->of_mime_types);
  261. if (merged->of_mime_types == NULL) return NULL;
  262. }
  263. }
  264. } else {
  265. /* Child did not add to the table */
  266. if (child->of_mime_types_cleared == 1) {
  267. merged->of_mime_types_cleared = 1;
  268. } else {
  269. merged->of_mime_types = parent->of_mime_types;
  270. merged->of_mime_types_cleared = parent->of_mime_types_cleared;
  271. }
  272. }
  273. /* debug log */
  274. if (child->debuglog_fd == NOT_SET_P) {
  275. merged->debuglog_name = parent->debuglog_name;
  276. merged->debuglog_fd = parent->debuglog_fd;
  277. } else {
  278. merged->debuglog_name = child->debuglog_name;
  279. merged->debuglog_fd = child->debuglog_fd;
  280. }
  281. merged->debuglog_level = (child->debuglog_level == NOT_SET
  282. ? parent->debuglog_level : child->debuglog_level);
  283. merged->cookie_format = (child->cookie_format == NOT_SET
  284. ? parent->cookie_format : child->cookie_format);
  285. merged->argument_separator = (child->argument_separator == NOT_SET
  286. ? parent->argument_separator : child->argument_separator);
  287. /* rule inheritance */
  288. if ((child->rule_inheritance == NOT_SET)||(child->rule_inheritance == 1)) {
  289. merged->rule_inheritance = parent->rule_inheritance;
  290. if ((child->ruleset == NULL)&&(parent->ruleset == NULL)) {
  291. #ifdef DEBUG_CONF
  292. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, mp, "No rules in this context.");
  293. #endif
  294. /* Do nothing, there are no rules in either context. */
  295. } else
  296. if (child->ruleset == NULL) {
  297. #ifdef DEBUG_CONF
  298. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, mp, "Using parent rules in this context.");
  299. #endif
  300. /* Copy the rules from the parent context. */
  301. merged->ruleset = msre_ruleset_create(parent->ruleset->engine, mp);
  302. copy_rules(mp, parent->ruleset, merged->ruleset, child->rule_exceptions);
  303. } else
  304. if (parent->ruleset == NULL) {
  305. #ifdef DEBUG_CONF
  306. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, mp, "Using child rules in this context.");
  307. #endif
  308. /* Copy child rules. */
  309. merged->ruleset = msre_ruleset_create(child->ruleset->engine, mp);
  310. merged->ruleset->phase_request_headers = apr_array_copy(mp,
  311. child->ruleset->phase_request_headers);
  312. merged->ruleset->phase_request_body = apr_array_copy(mp,
  313. child->ruleset->phase_request_body);
  314. merged->ruleset->phase_response_headers = apr_array_copy(mp,
  315. child->ruleset->phase_response_headers);
  316. merged->ruleset->phase_response_body = apr_array_copy(mp,
  317. child->ruleset->phase_response_body);
  318. merged->ruleset->phase_logging = apr_array_copy(mp,
  319. child->ruleset->phase_logging);
  320. } else {
  321. #ifdef DEBUG_CONF
  322. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, mp, "Using parent then child rules in this context.");
  323. #endif
  324. /* Copy parent rules, then add child rules to it. */
  325. merged->ruleset = msre_ruleset_create(parent->ruleset->engine, mp);
  326. copy_rules(mp, parent->ruleset, merged->ruleset, child->rule_exceptions);
  327. apr_array_cat(merged->ruleset->phase_request_headers,
  328. child->ruleset->phase_request_headers);
  329. apr_array_cat(merged->ruleset->phase_request_body,
  330. child->ruleset->phase_request_body);
  331. apr_array_cat(merged->ruleset->phase_response_headers,
  332. child->ruleset->phase_response_headers);
  333. apr_array_cat(merged->ruleset->phase_response_body,
  334. child->ruleset->phase_response_body);
  335. apr_array_cat(merged->ruleset->phase_logging,
  336. child->ruleset->phase_logging);
  337. }
  338. } else {
  339. merged->rule_inheritance = 0;
  340. if (child->ruleset != NULL) {
  341. /* Copy child rules. */
  342. merged->ruleset = msre_ruleset_create(child->ruleset->engine, mp);
  343. merged->ruleset->phase_request_headers = apr_array_copy(mp,
  344. child->ruleset->phase_request_headers);
  345. merged->ruleset->phase_request_body = apr_array_copy(mp,
  346. child->ruleset->phase_request_body);
  347. merged->ruleset->phase_response_headers = apr_array_copy(mp,
  348. child->ruleset->phase_response_headers);
  349. merged->ruleset->phase_response_body = apr_array_copy(mp,
  350. child->ruleset->phase_response_body);
  351. merged->ruleset->phase_logging = apr_array_copy(mp,
  352. child->ruleset->phase_logging);
  353. }
  354. }
  355. /* Merge rule exceptions. */
  356. merged->rule_exceptions = apr_array_append(mp, parent->rule_exceptions,
  357. child->rule_exceptions);
  358. /* audit log variables */
  359. merged->auditlog_flag = (child->auditlog_flag == NOT_SET
  360. ? parent->auditlog_flag : child->auditlog_flag);
  361. merged->auditlog_type = (child->auditlog_type == NOT_SET
  362. ? parent->auditlog_type : child->auditlog_type);
  363. merged->auditlog_dirperms = (child->auditlog_dirperms == NOT_SET
  364. ? parent->auditlog_dirperms : child->auditlog_dirperms);
  365. merged->auditlog_fileperms = (child->auditlog_fileperms == NOT_SET
  366. ? parent->auditlog_fileperms : child->auditlog_fileperms);
  367. if (child->auditlog_fd != NOT_SET_P) {
  368. merged->auditlog_fd = child->auditlog_fd;
  369. merged->auditlog_name = child->auditlog_name;
  370. } else {
  371. merged->auditlog_fd = parent->auditlog_fd;
  372. merged->auditlog_name = parent->auditlog_name;
  373. }
  374. if (child->auditlog2_fd != NOT_SET_P) {
  375. merged->auditlog2_fd = child->auditlog2_fd;
  376. merged->auditlog2_name = child->auditlog2_name;
  377. } else {
  378. merged->auditlog2_fd = parent->auditlog2_fd;
  379. merged->auditlog2_name = parent->auditlog2_name;
  380. }
  381. merged->auditlog_storage_dir = (child->auditlog_storage_dir == NOT_SET_P
  382. ? parent->auditlog_storage_dir : child->auditlog_storage_dir);
  383. merged->auditlog_parts = (child->auditlog_parts == NOT_SET_P
  384. ? parent->auditlog_parts : child->auditlog_parts);
  385. merged->auditlog_relevant_regex = (child->auditlog_relevant_regex == NOT_SET_P
  386. ? parent->auditlog_relevant_regex : child->auditlog_relevant_regex);
  387. /* Upload */
  388. merged->tmp_dir = (child->tmp_dir == NOT_SET_P
  389. ? parent->tmp_dir : child->tmp_dir);
  390. merged->upload_dir = (child->upload_dir == NOT_SET_P
  391. ? parent->upload_dir : child->upload_dir);
  392. merged->upload_keep_files = (child->upload_keep_files == NOT_SET
  393. ? parent->upload_keep_files : child->upload_keep_files);
  394. merged->upload_validates_files = (child->upload_validates_files == NOT_SET
  395. ? parent->upload_validates_files : child->upload_validates_files);
  396. merged->upload_filemode = (child->upload_filemode == NOT_SET
  397. ? parent->upload_filemode : child->upload_filemode);
  398. merged->upload_file_limit = (child->upload_file_limit == NOT_SET
  399. ? parent->upload_file_limit : child->upload_file_limit);
  400. /* Misc */
  401. merged->data_dir = (child->data_dir == NOT_SET_P
  402. ? parent->data_dir : child->data_dir);
  403. merged->webappid = (child->webappid == NOT_SET_P
  404. ? parent->webappid : child->webappid);
  405. /* Content injection. */
  406. merged->content_injection_enabled = (child->content_injection_enabled == NOT_SET
  407. ? parent->content_injection_enabled : child->content_injection_enabled);
  408. /* Stream inspection */
  409. merged->stream_inbody_inspection = (child->stream_inbody_inspection == NOT_SET
  410. ? parent->stream_inbody_inspection : child->stream_inbody_inspection);
  411. merged->stream_outbody_inspection = (child->stream_outbody_inspection == NOT_SET
  412. ? parent->stream_outbody_inspection : child->stream_outbody_inspection);
  413. /* Geo Lookup */
  414. merged->geo = (child->geo == NOT_SET_P
  415. ? parent->geo : child->geo);
  416. /* Gsb Lookup */
  417. merged->gsb = (child->gsb == NOT_SET_P
  418. ? parent->gsb : child->gsb);
  419. /* Unicode Map */
  420. merged->u_map = (child->u_map == NOT_SET_P
  421. ? parent->u_map : child->u_map);
  422. /* Cache */
  423. merged->cache_trans = (child->cache_trans == NOT_SET
  424. ? parent->cache_trans : child->cache_trans);
  425. merged->cache_trans_incremental = (child->cache_trans_incremental == NOT_SET
  426. ? parent->cache_trans_incremental : child->cache_trans_incremental);
  427. merged->cache_trans_min = (child->cache_trans_min == (apr_size_t)NOT_SET
  428. ? parent->cache_trans_min : child->cache_trans_min);
  429. merged->cache_trans_max = (child->cache_trans_max == (apr_size_t)NOT_SET
  430. ? parent->cache_trans_max : child->cache_trans_max);
  431. merged->cache_trans_maxitems = (child->cache_trans_maxitems == (apr_size_t)NOT_SET
  432. ? parent->cache_trans_maxitems : child->cache_trans_maxitems);
  433. /* Merge component signatures. */
  434. merged->component_signatures = apr_array_append(mp, parent->component_signatures,
  435. child->component_signatures);
  436. merged->request_encoding = (child->request_encoding == NOT_SET_P
  437. ? parent->request_encoding : child->request_encoding);
  438. merged->disable_backend_compression = (child->disable_backend_compression == NOT_SET
  439. ? parent->disable_backend_compression : child->disable_backend_compression);
  440. return merged;
  441. }
  442. /**
  443. * Initialise directory configuration. This function is *not* meant
  444. * to be called for directory configuration instances created during
  445. * the configuration phase. It can only be called on copies of those
  446. * (created fresh for every transaction).
  447. */
  448. void init_directory_config(directory_config *dcfg)
  449. {
  450. if (dcfg == NULL) return;
  451. if (dcfg->is_enabled == NOT_SET) dcfg->is_enabled = 0;
  452. if (dcfg->reqbody_access == NOT_SET) dcfg->reqbody_access = 0;
  453. if (dcfg->reqintercept_oe == NOT_SET) dcfg->reqintercept_oe = 0;
  454. if (dcfg->reqbody_buffering == NOT_SET) dcfg->reqbody_buffering = REQUEST_BODY_FORCEBUF_OFF;
  455. if (dcfg->reqbody_inmemory_limit == NOT_SET)
  456. dcfg->reqbody_inmemory_limit = REQUEST_BODY_DEFAULT_INMEMORY_LIMIT;
  457. if (dcfg->reqbody_limit == NOT_SET) dcfg->reqbody_limit = REQUEST_BODY_DEFAULT_LIMIT;
  458. if (dcfg->reqbody_no_files_limit == NOT_SET) dcfg->reqbody_no_files_limit = REQUEST_BODY_NO_FILES_DEFAULT_LIMIT;
  459. if (dcfg->resbody_access == NOT_SET) dcfg->resbody_access = 0;
  460. if (dcfg->of_limit == NOT_SET) dcfg->of_limit = RESPONSE_BODY_DEFAULT_LIMIT;
  461. if (dcfg->if_limit_action == NOT_SET) dcfg->if_limit_action = REQUEST_BODY_LIMIT_ACTION_REJECT;
  462. if (dcfg->of_limit_action == NOT_SET) dcfg->of_limit_action = RESPONSE_BODY_LIMIT_ACTION_REJECT;
  463. if (dcfg->of_mime_types == NOT_SET_P) {
  464. dcfg->of_mime_types = apr_table_make(dcfg->mp, 3);
  465. if (dcfg->of_mime_types_cleared != 1) {
  466. apr_table_setn(dcfg->of_mime_types, "text/plain", "1");
  467. apr_table_setn(dcfg->of_mime_types, "text/html", "1");
  468. }
  469. }
  470. if (dcfg->debuglog_fd == NOT_SET_P) dcfg->debuglog_fd = NULL;
  471. if (dcfg->debuglog_name == NOT_SET_P) dcfg->debuglog_name = NULL;
  472. if (dcfg->debuglog_level == NOT_SET) dcfg->debuglog_level = 0;
  473. if (dcfg->cookie_format == NOT_SET) dcfg->cookie_format = 0;
  474. if (dcfg->argument_separator == NOT_SET) dcfg->argument_separator = '&';
  475. if (dcfg->rule_inheritance == NOT_SET) dcfg->rule_inheritance = 1;
  476. /* audit log variables */
  477. if (dcfg->auditlog_flag == NOT_SET) dcfg->auditlog_flag = 0;
  478. if (dcfg->auditlog_type == NOT_SET) dcfg->auditlog_type = AUDITLOG_SERIAL;
  479. if (dcfg->auditlog_dirperms == NOT_SET) dcfg->auditlog_dirperms = CREATEMODE_DIR;
  480. if (dcfg->auditlog_fileperms == NOT_SET) dcfg->auditlog_fileperms = CREATEMODE;
  481. if (dcfg->auditlog_fd == NOT_SET_P) dcfg->auditlog_fd = NULL;
  482. if (dcfg->auditlog2_fd == NOT_SET_P) dcfg->auditlog2_fd = NULL;
  483. if (dcfg->auditlog_name == NOT_SET_P) dcfg->auditlog_name = NULL;
  484. if (dcfg->auditlog2_name == NOT_SET_P) dcfg->auditlog2_name = NULL;
  485. if (dcfg->auditlog_storage_dir == NOT_SET_P) dcfg->auditlog_storage_dir = NULL;
  486. if (dcfg->auditlog_parts == NOT_SET_P) dcfg->auditlog_parts = "ABCFHZ";
  487. if (dcfg->auditlog_relevant_regex == NOT_SET_P) dcfg->auditlog_relevant_regex = NULL;
  488. /* Upload */
  489. if (dcfg->tmp_dir == NOT_SET_P) dcfg->tmp_dir = guess_tmp_dir(dcfg->mp);
  490. if (dcfg->upload_dir == NOT_SET_P) dcfg->upload_dir = NULL;
  491. if (dcfg->upload_keep_files == NOT_SET) dcfg->upload_keep_files = KEEP_FILES_OFF;
  492. if (dcfg->upload_validates_files == NOT_SET) dcfg->upload_validates_files = 0;
  493. if (dcfg->upload_filemode == NOT_SET) dcfg->upload_filemode = 0600;
  494. if (dcfg->upload_file_limit == NOT_SET) dcfg->upload_file_limit = 100;
  495. /* Misc */
  496. if (dcfg->data_dir == NOT_SET_P) dcfg->data_dir = NULL;
  497. if (dcfg->webappid == NOT_SET_P) dcfg->webappid = "default";
  498. /* Content injection. */
  499. if (dcfg->content_injection_enabled == NOT_SET) dcfg->content_injection_enabled = 0;
  500. /* Stream inspection */
  501. if (dcfg->stream_inbody_inspection == NOT_SET) dcfg->stream_inbody_inspection = 0;
  502. if (dcfg->stream_outbody_inspection == NOT_SET) dcfg->stream_outbody_inspection = 0;
  503. /* Geo Lookup */
  504. if (dcfg->geo == NOT_SET_P) dcfg->geo = NULL;
  505. /* Gsb Lookup */
  506. if (dcfg->gsb == NOT_SET_P) dcfg->gsb = NULL;
  507. /* Unicode Map */
  508. if (dcfg->u_map == NOT_SET_P) dcfg->u_map = NULL;
  509. /* Cache */
  510. if (dcfg->cache_trans == NOT_SET) dcfg->cache_trans = MODSEC_CACHE_DISABLED;
  511. if (dcfg->cache_trans_incremental == NOT_SET) dcfg->cache_trans_incremental = 0;
  512. if (dcfg->cache_trans_min == (apr_size_t)NOT_SET) dcfg->cache_trans_min = 32;
  513. if (dcfg->cache_trans_max == (apr_size_t)NOT_SET) dcfg->cache_trans_max = 1024;
  514. if (dcfg->cache_trans_maxitems == (apr_size_t)NOT_SET) dcfg->cache_trans_maxitems = 512;
  515. if (dcfg->request_encoding == NOT_SET_P) dcfg->request_encoding = NULL;
  516. if (dcfg->disable_backend_compression == NOT_SET) dcfg->disable_backend_compression = 0;
  517. }
  518. /**
  519. *
  520. */
  521. static const char *add_rule(cmd_parms *cmd, directory_config *dcfg, int type,
  522. const char *p1, const char *p2, const char *p3)
  523. {
  524. char *my_error_msg = NULL;
  525. msre_rule *rule = NULL;
  526. extern msc_engine *modsecurity;
  527. #ifdef DEBUG_CONF
  528. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, cmd->pool,
  529. "Rule: type=%d p1='%s' p2='%s' p3='%s'", type, p1, p2, p3);
  530. #endif
  531. /* Create a ruleset if one does not exist. */
  532. if ((dcfg->ruleset == NULL)||(dcfg->ruleset == NOT_SET_P)) {
  533. dcfg->ruleset = msre_ruleset_create(modsecurity->msre, cmd->pool);
  534. if (dcfg->ruleset == NULL) return FATAL_ERROR;
  535. }
  536. /* Create the rule now. */
  537. switch(type) {
  538. #if defined(WITH_LUA)
  539. case RULE_TYPE_LUA :
  540. rule = msre_rule_lua_create(dcfg->ruleset, cmd->directive->filename,
  541. cmd->directive->line_num, p1, p2, &my_error_msg);
  542. break;
  543. #endif
  544. default :
  545. rule = msre_rule_create(dcfg->ruleset, type, cmd->directive->filename,
  546. cmd->directive->line_num, p1, p2, p3, &my_error_msg);
  547. break;
  548. }
  549. if (rule == NULL) {
  550. return my_error_msg;
  551. }
  552. /* Create default actionset if one does not already exist. */
  553. if (dcfg->tmp_default_actionset == NULL) {
  554. dcfg->tmp_default_actionset = msre_actionset_create_default(modsecurity->msre);
  555. if (dcfg->tmp_default_actionset == NULL) return FATAL_ERROR;
  556. }
  557. /* Check some cases prior to merging so we know where it came from */
  558. /* Check syntax for chained rules */
  559. if ((rule->actionset != NULL) && (dcfg->tmp_chain_starter != NULL)) {
  560. /* Must NOT specify a disruptive action. */
  561. if (rule->actionset->intercept_action != NOT_SET) {
  562. return apr_psprintf(cmd->pool, "ModSecurity: Disruptive actions can only "
  563. "be specified by chain starter rules.");
  564. }
  565. /* Must NOT specify a skipafter action. */
  566. if (rule->actionset->skip_after != NOT_SET_P) {
  567. return apr_psprintf(cmd->pool, "ModSecurity: SkipAfter actions can only "
  568. "be specified by chain starter rules.");
  569. }
  570. /* Must NOT specify a phase. */
  571. if (rule->actionset->phase != NOT_SET) {
  572. return apr_psprintf(cmd->pool, "ModSecurity: Execution phases can only be "
  573. "specified by chain starter rules.");
  574. }
  575. /* Must NOT use metadata actions. */
  576. /* ENH: loop through to check for tags */
  577. if ((rule->actionset->id != NOT_SET_P)
  578. ||(rule->actionset->rev != NOT_SET_P)
  579. ||(rule->actionset->msg != NOT_SET_P)
  580. ||(rule->actionset->severity != NOT_SET)
  581. ||(rule->actionset->logdata != NOT_SET_P))
  582. {
  583. return apr_psprintf(cmd->pool, "ModSecurity: Metadata actions (id, rev, msg, tag, severity, logdata) "
  584. " can only be specified by chain starter rules.");
  585. }
  586. /* Must NOT use skip. */
  587. if (rule->actionset->skip_count != NOT_SET) {
  588. return apr_psprintf(cmd->pool, "ModSecurity: The skip action can only be used "
  589. " by chain starter rules. ");
  590. }
  591. }
  592. /* Merge actions with the parent.
  593. *
  594. * ENH Probably do not want this done fully for chained rules.
  595. */
  596. rule->actionset = msre_actionset_merge(modsecurity->msre, dcfg->tmp_default_actionset,
  597. rule->actionset, 1);
  598. /* Keep track of the parent action for "block" */
  599. rule->actionset->parent_intercept_action_rec = dcfg->tmp_default_actionset->intercept_action_rec;
  600. rule->actionset->parent_intercept_action = dcfg->tmp_default_actionset->intercept_action;
  601. /* Must NOT specify a disruptive action in logging phase. */
  602. if ((rule->actionset != NULL)
  603. && (rule->actionset->phase == PHASE_LOGGING)
  604. && (rule->actionset->intercept_action != ACTION_ALLOW)
  605. && (rule->actionset->intercept_action != ACTION_ALLOW_REQUEST)
  606. && (rule->actionset->intercept_action != ACTION_NONE)
  607. ) {
  608. return apr_psprintf(cmd->pool, "ModSecurity: Disruptive actions "
  609. "cannot be specified in the logging phase.");
  610. }
  611. if (dcfg->tmp_chain_starter != NULL) {
  612. rule->chain_starter = dcfg->tmp_chain_starter;
  613. rule->actionset->phase = rule->chain_starter->actionset->phase;
  614. }
  615. if (rule->actionset->is_chained != 1) {
  616. /* If this rule is part of the chain but does
  617. * not want more rules to follow in the chain
  618. * then cut it (the chain).
  619. */
  620. dcfg->tmp_chain_starter = NULL;
  621. } else {
  622. /* On the other hand, if this rule wants other
  623. * rules to follow it, then start a new chain
  624. * if there isn't one already.
  625. */
  626. if (dcfg->tmp_chain_starter == NULL) {
  627. dcfg->tmp_chain_starter = rule;
  628. }
  629. }
  630. /* Optimisation */
  631. if ((rule->op_name != NULL)&&(strcasecmp(rule->op_name, "inspectFile") == 0)) {
  632. dcfg->upload_validates_files = 1;
  633. }
  634. /* Create skip table if one does not already exist. */
  635. if (dcfg->tmp_rule_placeholders == NULL) {
  636. dcfg->tmp_rule_placeholders = apr_table_make(cmd->pool, 10);
  637. if (dcfg->tmp_rule_placeholders == NULL) return FATAL_ERROR;
  638. }
  639. /* Keep track of any rule IDs we need to skip after */
  640. if (rule->actionset->skip_after != NOT_SET_P) {
  641. char *tmp_id = apr_pstrdup(cmd->pool, rule->actionset->skip_after);
  642. apr_table_setn(dcfg->tmp_rule_placeholders, tmp_id, tmp_id);
  643. #ifdef DEBUG_CONF
  644. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, cmd->pool,
  645. "Watching for skipafter target rule id=\"%s\".", tmp_id);
  646. #endif
  647. }
  648. #ifdef DEBUG_CONF
  649. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, cmd->pool,
  650. "Adding rule %pp phase=%d id=\"%s\".", rule, rule->actionset->phase, (rule->actionset->id == NOT_SET_P
  651. ? "(none)" : rule->actionset->id));
  652. #endif
  653. /* Add rule to the recipe. */
  654. if (msre_ruleset_rule_add(dcfg->ruleset, rule, rule->actionset->phase) < 0) {
  655. return "Internal Error: Failed to add rule to the ruleset.";
  656. }
  657. /* Add an additional placeholder if this rule ID is on the list */
  658. if ((rule->actionset->id != NULL) && apr_table_get(dcfg->tmp_rule_placeholders, rule->actionset->id)) {
  659. msre_rule *phrule = apr_palloc(rule->ruleset->mp, sizeof(msre_rule));
  660. if (phrule == NULL) {
  661. return FATAL_ERROR;
  662. }
  663. #ifdef DEBUG_CONF
  664. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, cmd->pool,
  665. "Adding placeholder %pp for rule %pp id=\"%s\".", phrule, rule, rule->actionset->id);
  666. #endif
  667. /* shallow copy of original rule with placeholder marked as target */
  668. memcpy(phrule, rule, sizeof(msre_rule));
  669. phrule->placeholder = RULE_PH_SKIPAFTER;
  670. /* Add placeholder. */
  671. if (msre_ruleset_rule_add(dcfg->ruleset, phrule, phrule->actionset->phase) < 0) {
  672. return "Internal Error: Failed to add placeholder to the ruleset.";
  673. }
  674. /* No longer need to search for the ID */
  675. apr_table_unset(dcfg->tmp_rule_placeholders, rule->actionset->id);
  676. }
  677. /* Update the unparsed rule */
  678. rule->unparsed = msre_rule_generate_unparsed(dcfg->ruleset->mp, rule, NULL, NULL, NULL);
  679. return NULL;
  680. }
  681. /**
  682. *
  683. */
  684. static const char *add_marker(cmd_parms *cmd, directory_config *dcfg,
  685. const char *p1, const char *p2, const char *p3)
  686. {
  687. char *my_error_msg = NULL;
  688. msre_rule *rule = NULL;
  689. extern msc_engine *modsecurity;
  690. int p;
  691. #ifdef DEBUG_CONF
  692. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, cmd->pool,
  693. "Rule: type=%d p1='%s' p2='%s' p3='%s'", RULE_TYPE_MARKER, p1, p2, p3);
  694. #endif
  695. /* Create a ruleset if one does not exist. */
  696. if ((dcfg->ruleset == NULL)||(dcfg->ruleset == NOT_SET_P)) {
  697. dcfg->ruleset = msre_ruleset_create(modsecurity->msre, cmd->pool);
  698. if (dcfg->ruleset == NULL) return FATAL_ERROR;
  699. }
  700. /* Create the rule now. */
  701. rule = msre_rule_create(dcfg->ruleset, RULE_TYPE_MARKER, cmd->directive->filename, cmd->directive->line_num, p1, p2, p3, &my_error_msg);
  702. if (rule == NULL) {
  703. return my_error_msg;
  704. }
  705. /* This is a marker */
  706. rule->placeholder = RULE_PH_MARKER;
  707. /* Add placeholder to each phase */
  708. for (p = PHASE_FIRST; p <= PHASE_LAST; p++) {
  709. #ifdef DEBUG_CONF
  710. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, cmd->pool,
  711. "Adding marker %pp phase=%d id=\"%s\".", rule, p, (rule->actionset->id == NOT_SET_P
  712. ? "(none)" : rule->actionset->id));
  713. #endif
  714. if (msre_ruleset_rule_add(dcfg->ruleset, rule, p) < 0) {
  715. return "Internal Error: Failed to add marker to the ruleset.";
  716. }
  717. }
  718. /* No longer need to search for the ID */
  719. if (dcfg->tmp_rule_placeholders != NULL) {
  720. apr_table_unset(dcfg->tmp_rule_placeholders, rule->actionset->id);
  721. }
  722. return NULL;
  723. }
  724. /**
  725. *
  726. */
  727. static const char *update_rule_action(cmd_parms *cmd, directory_config *dcfg,
  728. const char *p1, const char *p2, int offset)
  729. {
  730. char *my_error_msg = NULL;
  731. msre_rule *rule = NULL;
  732. msre_actionset *new_actionset = NULL;
  733. msre_ruleset *ruleset = dcfg->ruleset;
  734. extern msc_engine *modsecurity;
  735. /* Get the ruleset if one exists */
  736. if ((ruleset == NULL)||(ruleset == NOT_SET_P)) {
  737. return NULL;
  738. }
  739. #ifdef DEBUG_CONF
  740. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, cmd->pool,
  741. "Update rule id=\"%s\" with action \"%s\".", p1, p2);
  742. #endif
  743. /* Fetch the rule */
  744. rule = msre_ruleset_fetch_rule(ruleset, p1, offset);
  745. if (rule == NULL) {
  746. #ifdef DEBUG_CONF
  747. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, cmd->pool,
  748. "Update rule id=\"%s\" with action \"%s\" failed: Rule not found.", p1, p2);
  749. #endif
  750. return NULL;
  751. }
  752. /* Check the rule actionset */
  753. /* ENH: Can this happen? */
  754. if (rule->actionset == NULL) {
  755. return apr_psprintf(cmd->pool, "ModSecurity: Attempt to update action for rule \"%s\" failed: Rule does not have an actionset.", p1);
  756. }
  757. /* Create a new actionset */
  758. new_actionset = msre_actionset_create(modsecurity->msre, p2, &my_error_msg);
  759. if (new_actionset == NULL) return FATAL_ERROR;
  760. if (my_error_msg != NULL) return my_error_msg;
  761. /* Must NOT change an id */
  762. if ((new_actionset->id != NOT_SET_P) && (rule->actionset->id != NULL) && (strcmp(rule->actionset->id, new_actionset->id) != 0)) {
  763. return apr_psprintf(cmd->pool, "ModSecurity: Rule IDs cannot be updated via SecRuleUpdateActionById.");
  764. }
  765. /* Must NOT alter the phase */
  766. if ((new_actionset->phase != NOT_SET) && (rule->actionset->phase != new_actionset->phase)) {
  767. return apr_psprintf(cmd->pool, "ModSecurity: Rule phases cannot be updated via SecRuleUpdateActionById.");
  768. }
  769. #ifdef DEBUG_CONF
  770. {
  771. char *actions = msre_actionset_generate_action_string(ruleset->mp, rule->actionset);
  772. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, cmd->pool,
  773. "Update rule %pp id=\"%s\" old action: \"%s\"",
  774. rule,
  775. (rule->actionset->id == NOT_SET_P ? "(none)" : rule->actionset->id),
  776. actions);
  777. }
  778. #endif
  779. /* Merge new actions with the rule */
  780. /* ENH: Will this leak the old actionset? */
  781. rule->actionset = msre_actionset_merge(modsecurity->msre, rule->actionset,
  782. new_actionset, 1);
  783. msre_actionset_set_defaults(rule->actionset);
  784. /* Update the unparsed rule */
  785. rule->unparsed = msre_rule_generate_unparsed(ruleset->mp, rule, NULL, NULL, NULL);
  786. #ifdef DEBUG_CONF
  787. {
  788. char *actions = msre_actionset_generate_action_string(ruleset->mp, rule->actionset);
  789. ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, cmd->pool,
  790. "Update rule %pp id=\"%s\" new action: \"%s\"",
  791. rule,
  792. (rule->actionset->id == NOT_SET_P ? "(none)" : rule->actionset->id),
  793. actions);
  794. }
  795. #endif
  796. return NULL;
  797. }
  798. /* -- Configuration directives -- */
  799. static const char *cmd_action(cmd_parms *cmd, void *_dcfg, const char *p1)
  800. {
  801. return add_rule(cmd, (directory_config *)_dcfg, RULE_TYPE_ACTION, SECACTION_TARGETS, SECACTION_ARGS, p1);
  802. }
  803. static const char *cmd_marker(cmd_parms *cmd, void *_dcfg, const char *p1)
  804. {
  805. directory_config *dcfg = (directory_config *)_dcfg;
  806. const char *action = apr_pstrcat(dcfg->mp, SECMARKER_BASE_ACTIONS, p1, NULL);
  807. return add_marker(cmd, (directory_config *)_dcfg, SECMARKER_TARGETS, SECMARKER_ARGS, action);
  808. }
  809. static const char *cmd_argument_separator(cmd_parms *cmd, void *_dcfg,
  810. const char *p1)
  811. {
  812. directory_config *dcfg = (directory_config *)_dcfg;
  813. if (strlen(p1) != 1) {
  814. return apr_psprintf(cmd->pool, "ModSecurity: Invalid argument separator: %s", p1);
  815. }
  816. dcfg->argument_separator = p1[0];
  817. return NULL;
  818. }
  819. static const char *cmd_audit_engine(cmd_parms *cmd, void *_dcfg, const char *p1)
  820. {
  821. directory_config *dcfg = _dcfg;
  822. if (strcasecmp(p1, "On") == 0) dcfg->auditlog_flag = AUDITLOG_ON;
  823. else
  824. if (strcasecmp(p1, "Off") == 0) dcfg->auditlog_flag = AUDITLOG_OFF;
  825. else
  826. if (strcasecmp(p1, "RelevantOnly") == 0) dcfg->auditlog_flag = AUDITLOG_RELEVANT;
  827. else
  828. return (const char *)apr_psprintf(cmd->pool,
  829. "ModSecurity: Unrecognised parameter value for SecAuditEngine: %s", p1);
  830. return NULL;
  831. }
  832. static const char *cmd_audit_log(cmd_parms *cmd, void *_dcfg, const char *p1)
  833. {
  834. directory_config *dcfg = _dcfg;
  835. dcfg->auditlog_name = (char *)p1;
  836. if (dcfg->auditlog_name[0] == '|') {
  837. const char *pipe_name = dcfg->auditlog_name + 1;
  838. piped_log *pipe_log;
  839. pipe_log = ap_open_piped_log(cmd->pool, pipe_name);
  840. if (pipe_log == NULL) {
  841. return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the audit log pipe: %s",
  842. pipe_name);
  843. }
  844. dcfg->auditlog_fd = ap_piped_log_write_fd(pipe_log);
  845. }
  846. else {
  847. const char *file_name = ap_server_root_relative(cmd->pool, dcfg->auditlog_name);
  848. apr_status_t rc;
  849. rc = apr_file_open(&dcfg->auditlog_fd, file_name,
  850. APR_WRITE | APR_APPEND | APR_CREATE | APR_BINARY,
  851. CREATEMODE, cmd->pool);
  852. if (rc != APR_SUCCESS) {
  853. return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the audit log file: %s",
  854. file_name);
  855. }
  856. }
  857. return NULL;
  858. }
  859. static const char *cmd_audit_log2(cmd_parms *cmd, void *_dcfg, const char *p1)
  860. {
  861. directory_config *dcfg = _dcfg;
  862. if (dcfg->auditlog_name == NOT_SET_P) {
  863. return apr_psprintf(cmd->pool, "ModSecurity: Cannot configure a secondary audit log without a primary defined: %s", p1);
  864. }
  865. dcfg->auditlog2_name = (char *)p1;
  866. if (dcfg->auditlog2_name[0] == '|') {
  867. const char *pipe_name = ap_server_root_relative(cmd->pool, dcfg->auditlog2_name + 1);
  868. piped_log *pipe_log;
  869. pipe_log = ap_open_piped_log(cmd->pool, pipe_name);
  870. if (pipe_log == NULL) {
  871. return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the secondary audit log pipe: %s",
  872. pipe_name);
  873. }
  874. dcfg->auditlog2_fd = ap_piped_log_write_fd(pipe_log);
  875. }
  876. else {
  877. const char *file_name = ap_server_root_relative(cmd->pool, dcfg->auditlog2_name);
  878. apr_status_t rc;
  879. rc = apr_file_open(&dcfg->auditlog2_fd, file_name,
  880. APR_WRITE | APR_APPEND | APR_CREATE | APR_BINARY,
  881. CREATEMODE, cmd->pool);
  882. if (rc != APR_SUCCESS) {
  883. return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the secondary audit log file: %s",
  884. file_name);
  885. }
  886. }
  887. return NULL;
  888. }
  889. static const char *cmd_audit_log_parts(cmd_parms *cmd, void *_dcfg,
  890. const char *p1)
  891. {
  892. directory_config *dcfg = _dcfg;
  893. if (is_valid_parts_specification((char *)p1) != 1) {
  894. return apr_psprintf(cmd->pool, "Invalid parts specification for SecAuditLogParts: %s", p1);
  895. }
  896. dcfg->auditlog_parts = (char *)p1;
  897. return NULL;
  898. }
  899. static const char *cmd_audit_log_relevant_status(cmd_parms *cmd, void *_dcfg,
  900. const char *p1)
  901. {
  902. directory_config *dcfg = _dcfg;
  903. dcfg->auditlog_relevant_regex = msc_pregcomp(cmd->pool, p1, PCRE_DOTALL, NULL, NULL);
  904. if (dcfg->auditlog_relevant_regex == NULL) {
  905. return apr_psprintf(cmd->pool, "ModSecurity: Invalid regular expression: %s", p1);
  906. }
  907. return NULL;
  908. }
  909. static const char *cmd_audit_log_type(cmd_parms *cmd, void *_dcfg,
  910. const char *p1)
  911. {
  912. directory_config *dcfg = _dcfg;
  913. if (strcasecmp(p1, "Serial") == 0) dcfg->auditlog_type = AUDITLOG_SERIAL;
  914. else
  915. if (strcasecmp(p1, "Concurrent") == 0) dcfg->auditlog_type = AUDITLOG_CONCURRENT;
  916. else
  917. return (const char *)apr_psprintf(cmd->pool,
  918. "ModSecurity: Unrecognised parameter value for SecAuditLogType: %s", p1);
  919. return NULL;
  920. }
  921. static const char *cmd_audit_log_dirmode(cmd_parms *cmd, void *_dcfg,
  922. const char *p1)
  923. {
  924. directory_config *dcfg = (directory_config *)_dcfg;
  925. if (dcfg == NULL) return NULL;
  926. if (strcasecmp(p1, "default") == 0) {
  927. dcfg->auditlog_dirperms = NOT_SET;
  928. }
  929. else {
  930. long int mode = strtol(p1, NULL, 8); /* expects octal mode */
  931. if ((mode == LONG_MAX)||(mode == LONG_MIN)||(mode <= 0)||(mode > 07777)) {
  932. return apr_psprintf(cmd->pool, "ModSecurity: Invalid value for SecAuditLogDirMode: %s", p1);
  933. }
  934. dcfg->auditlog_dirperms = mode2fileperms(mode);
  935. }
  936. return NULL;
  937. }
  938. static const char *cmd_audit_log_filemode(cmd_parms *cmd, void *_dcfg,
  939. const char *p1)
  940. {
  941. directory_config *dcfg = (directory_config *)_dcfg;
  942. if (dcfg == NULL) return NULL;
  943. if (strcasecmp(p1, "default") == 0) {
  944. dcfg->auditlog_fileperms = NOT_SET;
  945. }
  946. else {
  947. long int mode = strtol(p1, NULL, 8); /* expects octal mode */
  948. if ((mode == LONG_MAX)||(mode == LONG_MIN)||(mode <= 0)||(mode > 07777)) {
  949. return apr_psprintf(cmd->pool, "ModSecurity: Invalid value for SecAuditLogFileMode: %s", p1);
  950. }
  951. dcfg->auditlog_fileperms = mode2fileperms(mode);
  952. }
  953. return NULL;
  954. }
  955. static const char *cmd_audit_log_storage_dir(cmd_parms *cmd, void *_dcfg,
  956. const char *p1)
  957. {
  958. directory_config *dcfg = _dcfg;
  959. dcfg->auditlog_storage_dir = ap_server_root_relative(cmd->pool, p1);
  960. return NULL;
  961. }
  962. static const char *cmd_cookie_format(cmd_parms *cmd, void *_dcfg,
  963. const char *p1)
  964. {
  965. directory_config *dcfg = (directory_config *)_dcfg;
  966. if (strcmp(p1, "0") == 0) dcfg->cookie_format = COOKIES_V0;
  967. else
  968. if (strcmp(p1, "1") == 0) dcfg->cookie_format = COOKIES_V1;
  969. else {
  970. return apr_psprintf(cmd->pool, "ModSecurity: Invalid cookie format: %s", p1);
  971. }
  972. return NULL;
  973. }
  974. static const char *cmd_chroot_dir(cmd_parms *cmd, void *_dcfg, const char *p1)
  975. {
  976. char cwd[1025] = "";
  977. if (cmd->server->is_virtual) {
  978. return "ModSecurity: SecChrootDir not allowed in VirtualHost";
  979. }
  980. chroot_dir = (char *)p1;
  981. if (getcwd(cwd, 1024) == NULL) {
  982. return "ModSecurity: Failed to get the current working directory";
  983. }
  984. if (chdir(chroot_dir) < 0) {
  985. return apr_psprintf(cmd->pool, "ModSecurity: Failed to chdir to %s, errno=%d (%s)",
  986. chroot_dir, errno, strerror(errno));
  987. }
  988. if (chdir(cwd) < 0) {
  989. return apr_psprintf(cmd->pool, "ModSecurity: Failed to chdir to %s, errno=%d (%s)",
  990. cwd, errno, strerror(errno));
  991. }
  992. return NULL;
  993. }
  994. /**
  995. * Adds component signature to the list of signatures kept in configuration.
  996. */
  997. static const char *cmd_component_signature(cmd_parms *cmd, void *_dcfg,
  998. const char *p1)
  999. {
  1000. directory_config *dcfg = (directory_config *)_dcfg;
  1001. /* ENH Enforce "Name/VersionX.Y.Z (comment)" format. */
  1002. *(char **)apr_array_push(dcfg->component_signatures) = (char *)p1;
  1003. return NULL;
  1004. }
  1005. static const char *cmd_content_injection(cmd_parms *cmd, void *_dcfg, int flag)
  1006. {
  1007. directory_config *dcfg = (directory_config *)_dcfg;
  1008. if (dcfg == NULL) return NULL;
  1009. dcfg->content_injection_enabled = flag;
  1010. return NULL;
  1011. }
  1012. static const char *cmd_data_dir(cmd_parms *cmd, void *_dcfg, const char *p1)
  1013. {
  1014. directory_config *dcfg = (directory_config *)_dcfg;
  1015. if (cmd->server->is_virtual) {
  1016. return "ModSecurity: SecDataDir not allowed in VirtualHost.";
  1017. }
  1018. dcfg->data_dir = ap_server_root_relative(cmd->pool, p1);
  1019. return NULL;
  1020. }
  1021. static const char *cmd_debug_log(cmd_parms *cmd, void *_dcfg, const char *p1)
  1022. {
  1023. directory_config *dcfg = (directory_config *)_dcfg;
  1024. apr_status_t rc;
  1025. dcfg->debuglog_name = ap_server_root_relative(cmd->pool, p1);
  1026. rc = apr_file_open(&dcfg->debuglog_fd, dcfg->debuglog_name,
  1027. APR_WRITE | APR_APPEND | APR_CREATE | APR_BINARY,
  1028. CREATEMODE, cmd->pool);
  1029. if (rc != APR_SUCCESS) {
  1030. return apr_psprintf(cmd->pool, "ModSecurity: Failed to open debug log file: %s",
  1031. dcfg->debuglog_name);
  1032. }
  1033. return NULL;
  1034. }
  1035. static const char *cmd_debug_log_level(cmd_parms *cmd, void *_dcfg,
  1036. const char *p1)
  1037. {
  1038. directory_config *dcfg = (directory_config *)_dcfg;
  1039. dcfg->debuglog_level = atoi(p1);
  1040. if ((dcfg->debuglog_level >= 0)&&(dcfg->debuglog_level <= 9)) return NULL;
  1041. return apr_psprintf(cmd->pool, "ModSecurity: Invalid value for SecDebugLogLevel: %s", p1);
  1042. }
  1043. static const char *cmd_default_action(cmd_parms *cmd, void *_dcfg,
  1044. const char *p1)
  1045. {
  1046. directory_config *dcfg = (directory_config *)_dcfg;
  1047. extern msc_engine *modsecurity;
  1048. char *my_error_msg = NULL;
  1049. dcfg->tmp_default_actionset = msre_actionset_create(modsecurity->msre, p1, &my_error_msg);
  1050. if (dcfg->tmp_default_actionset == NULL) {
  1051. if (my_error_msg != NULL) return my_error_msg;
  1052. else return FATAL_ERROR;
  1053. }
  1054. /* Must specify a disruptive action. */
  1055. /* ENH: Remove this requirement? */
  1056. if (dcfg->tmp_default_actionset->intercept_action == NOT_SET) {
  1057. return apr_psprintf(cmd->pool, "ModSecurity: SecDefaultAction must specify a disruptive action.");
  1058. }
  1059. /* Must specify a phase. */
  1060. /* ENH: Remove this requirement? */
  1061. if (dcfg->tmp_default_actionset->phase == NOT_SET) {
  1062. return apr_psprintf(cmd->pool, "ModSecurity: SecDefaultAction must specify a phase.");
  1063. }
  1064. /* Must not use metadata actions. */
  1065. /* ENH: loop through to check for tags */
  1066. if ((dcfg->tmp_default_actionset->id != NOT_SET_P)
  1067. ||(dcfg->tmp_default_actionset->rev != NOT_SET_P)
  1068. ||(dcfg->tmp_default_actionset->msg != NOT_SET_P))
  1069. {
  1070. return apr_psprintf(cmd->pool, "ModSecurity: SecDefaultAction must not "
  1071. "contain any metadata actions (id, rev, msg, tag, severity, logdata).");
  1072. }
  1073. /* These are just a warning for now. */
  1074. if ((dcfg->tmp_default_actionset->severity != NOT_SET)
  1075. ||(dcfg->tmp_default_actionset->logdata != NOT_SET_P))
  1076. {
  1077. ap_log_perror(APLOG_MARK,
  1078. APLOG_STARTUP|APLOG_WARNING|APLOG_NOERRNO, 0, cmd->pool,
  1079. "ModSecurity: WARNING Using \"severity\" or \"logdata\" in "
  1080. "SecDefaultAction is deprecated (%s:%d).",
  1081. cmd->directive->filename, cmd->directive->line_num);
  1082. }
  1083. /* Must not use chain. */
  1084. if (dcfg->tmp_default_actionset->is_chained != NOT_SET) {
  1085. return apr_psprintf(cmd->pool, "ModSecurity: SecDefaultAction must not "
  1086. "contain a chain action.");
  1087. }
  1088. /* Must not use…

Large files files are truncated, but you can click here to view the full file