1616#include "msc_status_engine.h"
1717
1818#include <apr_thread_pool.h>
19+
20+ #ifdef WITH_CURL
1921#include <curl/curl.h>
22+ #endif
2023
2124#include <apu.h>
25+
26+ #ifdef WITH_REMOTE_RULES
2227#include <apr_crypto.h>
2328#include <apr_sha1.h>
29+ #endif
2430
2531#ifndef AP_MAX_ARGC
2632#define AP_MAX_ARGC 64
2733#endif
2834
29- #ifdef WITH_REMOTE_RULES_SUPPORT
3035
3136/**
3237 * @brief Insert a new SecRule to be processed by ModSecurity
@@ -201,6 +206,7 @@ const char *msc_remote_invoke_cmd(const command_rec *cmd, cmd_parms *parms,
201206 NULL );
202207 }
203208}
209+
204210/**
205211 * @brief Fetch an URL and fill the content into a memory buffer.
206212 *
@@ -225,21 +231,25 @@ const char *msc_remote_invoke_cmd(const command_rec *cmd, cmd_parms *parms,
225231 *
226232 * @retval n>=0 everything went fine.
227233 * @retval n<-1 Something wrong happened, further details on error_msg.
234+ * n=-2 Download failed, but operation should not be aborted.
235+ * n=-3 ModSecurity was not compiled with curl support.
228236 *
229237 */
230- int msc_remote_grab_content (apr_pool_t * mp , const char * uri , const char * key ,
238+ int msc_remote_download_content (apr_pool_t * mp , const char * uri , const char * key ,
231239 struct msc_curl_memory_buffer_t * chunk , char * * error_msg )
232240{
241+ #ifdef WITH_CURL
233242 CURL * curl ;
234243 CURLcode res ;
235244
236245 char id [(APR_SHA1_DIGESTSIZE * 2 ) + 1 ];
237246 char * apr_id = NULL ;
238247 char * beacon_str = NULL ;
239248 char * beacon_apr = NULL ;
240- char * header_key = NULL ;
241249 int beacon_str_len = 0 ;
242250
251+ chunk -> size = 0 ;
252+
243253 memset (id , '\0' , sizeof (id ));
244254 if (msc_status_engine_unique_id (id ))
245255 {
@@ -266,11 +276,6 @@ int msc_remote_grab_content(apr_pool_t *mp, const char *uri, const char *key,
266276 free (beacon_str );
267277 }
268278
269- if (key != NULL )
270- {
271- header_key = apr_psprintf (mp , "ModSec-key: %s" , key );
272- }
273-
274279 if (curl )
275280 {
276281 struct curl_slist * headers_chunk = NULL ;
@@ -279,12 +284,14 @@ int msc_remote_grab_content(apr_pool_t *mp, const char *uri, const char *key,
279284 char * ptr = NULL ;
280285 DWORD res_len ;
281286#endif
282- curl_easy_setopt (curl , CURLOPT_URL , remote_rules_server -> uri );
287+ curl_easy_setopt (curl , CURLOPT_URL , uri );
283288
284289 headers_chunk = curl_slist_append (headers_chunk , apr_id );
285290 headers_chunk = curl_slist_append (headers_chunk , beacon_apr );
286291 if (key != NULL )
287292 {
293+ char * header_key = NULL ;
294+ header_key = apr_psprintf (mp , "ModSec-key: %s" , key );
288295 headers_chunk = curl_slist_append (headers_chunk , header_key );
289296 }
290297
@@ -321,17 +328,19 @@ int msc_remote_grab_content(apr_pool_t *mp, const char *uri, const char *key,
321328 if (remote_rules_fail_action == REMOTE_RULES_WARN_ON_FAIL )
322329 {
323330 ap_log_error (APLOG_MARK , APLOG_NOTICE , 0 , NULL ,
324- "Failed to fetch \"%s\" error: %s " ,
325- remote_rules_server -> uri , curl_easy_strerror (res ));
331+ "Failed to download \"%s\" error: %s " ,
332+ uri , curl_easy_strerror (res ));
333+
334+ return -2 ;
326335 }
327336 else
328337 {
329- * error_msg = apr_psprintf (mp , "Failed to fetch \"%s\" " \
338+ * error_msg = apr_psprintf (mp , "Failed to download \"%s\" " \
330339 "error: %s " ,
331- remote_rules_server -> uri , curl_easy_strerror (res ));
332- }
340+ uri , curl_easy_strerror (res ));
333341
334- return -1 ;
342+ return -1 ;
343+ }
335344 }
336345
337346 curl_slist_free_all (headers_chunk );
@@ -341,8 +350,12 @@ int msc_remote_grab_content(apr_pool_t *mp, const char *uri, const char *key,
341350
342351 curl_global_cleanup ();
343352 return 0 ;
353+ #else
354+ return -3 ;
355+ #endif
344356}
345357
358+
346359/**
347360 * @brief Setup an apr_crypto_key_t from a given password and salt.
348361 *
@@ -369,6 +382,7 @@ int msc_remote_grab_content(apr_pool_t *mp, const char *uri, const char *key,
369382 * @retval n<-1 Something wrong happened, check error_msg for further details.
370383 *
371384 */
385+ #ifdef WITH_APU_CRYPTO
372386int msc_remote_enc_key_setup (apr_pool_t * pool ,
373387 const char * key ,
374388 apr_crypto_key_t * * apr_key ,
@@ -411,11 +425,6 @@ int msc_remote_enc_key_setup(apr_pool_t *pool,
411425 * error_msg = "Internal error - apr_crypto_passphrase: APR_EKEYTYPE" ;
412426 return -1 ;
413427 }
414- else if (rv == APR_EKEYTYPE )
415- {
416- * error_msg = "Internal error - apr_crypto_passphrase: APR_EKEYTYPE" ;
417- return -1 ;
418- }
419428 else if (rv != APR_SUCCESS )
420429 {
421430 * error_msg = "Internal error - apr_crypto_passphrase: Unknown error" ;
@@ -424,6 +433,7 @@ int msc_remote_enc_key_setup(apr_pool_t *pool,
424433
425434 return 0 ;
426435}
436+ #endif
427437
428438/**
429439 * @brief Decrypt an buffer into a memory buffer.
@@ -449,6 +459,7 @@ int msc_remote_enc_key_setup(apr_pool_t *pool,
449459 * @retval n<-1 Something wrong happened, further details on error_msg.
450460 *
451461 */
462+ #ifdef WITH_APU_CRYPTO
452463int msc_remote_decrypt (apr_pool_t * pool ,
453464 const char * key ,
454465 struct msc_curl_memory_buffer_t * chunk ,
@@ -488,12 +499,9 @@ int msc_remote_decrypt(apr_pool_t *pool,
488499 return -1 ;
489500 }
490501
491- #ifndef APU_CRYPTO_RECOMMENDED_DRIVER
492- rv = apr_crypto_get_driver (& driver , "openssl" , NULL , & err , pool );
493- #else
494502 rv = apr_crypto_get_driver (& driver , APU_CRYPTO_RECOMMENDED_DRIVER , NULL ,
495503 & err , pool );
496- #endif
504+
497505 if (rv != APR_SUCCESS || driver == NULL )
498506 {
499507 * error_msg = "Internal error - apr_crypto_get_driver: Unknown error" ;
@@ -573,7 +581,7 @@ int msc_remote_decrypt(apr_pool_t *pool,
573581
574582 return 0 ;
575583}
576-
584+ #endif
577585
578586/**
579587 * @brief Add SecRules from a given URI.
@@ -598,6 +606,8 @@ int msc_remote_add_rules_from_uri(cmd_parms *orig_parms,
598606 msc_remote_rules_server * remote_rules_server ,
599607 char * * error_msg )
600608{
609+
610+ #ifdef WITH_REMOTE_RULES
601611 struct msc_curl_memory_buffer_t chunk_encrypted ;
602612 unsigned char * plain_text = NULL ;
603613 int len = 0 ;
@@ -612,13 +622,12 @@ int msc_remote_add_rules_from_uri(cmd_parms *orig_parms,
612622 chunk_encrypted .size = 0 ;
613623 chunk_encrypted .memory = NULL ;
614624
615- res = msc_remote_grab_content (mp , remote_rules_server -> uri ,
625+ res = msc_remote_download_content (mp , remote_rules_server -> uri ,
616626 remote_rules_server -> key , & chunk_encrypted , error_msg );
617627 if (* error_msg != NULL )
618628 {
619629 return -1 ;
620630 }
621-
622631 /* error_msg is not filled when the user set SecRemoteRulesFailAction
623632 * to warn
624633 */
@@ -629,14 +638,21 @@ int msc_remote_add_rules_from_uri(cmd_parms *orig_parms,
629638
630639 if (remote_rules_server -> crypto == 1 )
631640 {
641+ #ifdef WITH_APU_CRYPTO
632642 msc_remote_decrypt (mp , remote_rules_server -> key , & chunk_encrypted ,
633643 & plain_text ,
634644 & plain_text_len ,
635645 error_msg );
636646 if (* error_msg != NULL )
637647 {
648+ msc_remote_clean_chunk (& chunk_encrypted );
638649 return -1 ;
639650 }
651+ #else
652+ * error_msg = "ModSecurity was not compiled with crypto support.\n" ;
653+ msc_remote_clean_chunk (& chunk_encrypted );
654+ return -1 ;
655+ #endif
640656
641657 msc_remote_clean_chunk (& chunk_encrypted );
642658 }
@@ -725,12 +741,17 @@ int msc_remote_add_rules_from_uri(cmd_parms *orig_parms,
725741 {
726742 msc_remote_clean_chunk (& chunk_encrypted );
727743 }
744+ #else
745+ * error_msg = "SecRemoteRules was not enabled during ModSecurity " \
746+ "compilation." ;
747+ return -1 ;
748+ #endif
728749}
729750
730751
731752int msc_remote_clean_chunk (struct msc_curl_memory_buffer_t * chunk )
732753{
733- if (chunk -> size < = 0 )
754+ if (chunk -> size = = 0 )
734755 {
735756 goto end ;
736757 }
@@ -747,4 +768,3 @@ int msc_remote_clean_chunk(struct msc_curl_memory_buffer_t *chunk)
747768 return 0 ;
748769}
749770
750- #endif
0 commit comments