16
16
#include "msc_status_engine.h"
17
17
18
18
#include <apr_thread_pool.h>
19
+
20
+ #ifdef WITH_CURL
19
21
#include <curl/curl.h>
22
+ #endif
20
23
21
24
#include <apu.h>
25
+
26
+ #ifdef WITH_REMOTE_RULES
22
27
#include <apr_crypto.h>
23
28
#include <apr_sha1.h>
29
+ #endif
24
30
25
31
#ifndef AP_MAX_ARGC
26
32
#define AP_MAX_ARGC 64
27
33
#endif
28
34
29
- #ifdef WITH_REMOTE_RULES_SUPPORT
30
35
31
36
/**
32
37
* @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,
201
206
NULL );
202
207
}
203
208
}
209
+
204
210
/**
205
211
* @brief Fetch an URL and fill the content into a memory buffer.
206
212
*
@@ -225,21 +231,25 @@ const char *msc_remote_invoke_cmd(const command_rec *cmd, cmd_parms *parms,
225
231
*
226
232
* @retval n>=0 everything went fine.
227
233
* @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.
228
236
*
229
237
*/
230
238
int msc_remote_grab_content (apr_pool_t * mp , const char * uri , const char * key ,
231
239
struct msc_curl_memory_buffer_t * chunk , char * * error_msg )
232
240
{
241
+ #ifdef WITH_CURL
233
242
CURL * curl ;
234
243
CURLcode res ;
235
244
236
245
char id [(APR_SHA1_DIGESTSIZE * 2 ) + 1 ];
237
246
char * apr_id = NULL ;
238
247
char * beacon_str = NULL ;
239
248
char * beacon_apr = NULL ;
240
- char * header_key = NULL ;
241
249
int beacon_str_len = 0 ;
242
250
251
+ chunk -> size = 0 ;
252
+
243
253
memset (id , '\0' , sizeof (id ));
244
254
if (msc_status_engine_unique_id (id ))
245
255
{
@@ -266,11 +276,6 @@ int msc_remote_grab_content(apr_pool_t *mp, const char *uri, const char *key,
266
276
free (beacon_str );
267
277
}
268
278
269
- if (key != NULL )
270
- {
271
- header_key = apr_psprintf (mp , "ModSec-key: %s" , key );
272
- }
273
-
274
279
if (curl )
275
280
{
276
281
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,
279
284
char * ptr = NULL ;
280
285
DWORD res_len ;
281
286
#endif
282
- curl_easy_setopt (curl , CURLOPT_URL , remote_rules_server -> uri );
287
+ curl_easy_setopt (curl , CURLOPT_URL , uri );
283
288
284
289
headers_chunk = curl_slist_append (headers_chunk , apr_id );
285
290
headers_chunk = curl_slist_append (headers_chunk , beacon_apr );
286
291
if (key != NULL )
287
292
{
293
+ char * header_key = NULL ;
294
+ header_key = apr_psprintf (mp , "ModSec-key: %s" , key );
288
295
headers_chunk = curl_slist_append (headers_chunk , header_key );
289
296
}
290
297
@@ -321,17 +328,19 @@ int msc_remote_grab_content(apr_pool_t *mp, const char *uri, const char *key,
321
328
if (remote_rules_fail_action == REMOTE_RULES_WARN_ON_FAIL )
322
329
{
323
330
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 ;
326
335
}
327
336
else
328
337
{
329
- * error_msg = apr_psprintf (mp , "Failed to fetch \"%s\" " \
338
+ * error_msg = apr_psprintf (mp , "Failed to download \"%s\" " \
330
339
"error: %s " ,
331
- remote_rules_server -> uri , curl_easy_strerror (res ));
332
- }
340
+ uri , curl_easy_strerror (res ));
333
341
334
- return -1 ;
342
+ return -1 ;
343
+ }
335
344
}
336
345
337
346
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,
341
350
342
351
curl_global_cleanup ();
343
352
return 0 ;
353
+ #else
354
+ return -3 ;
355
+ #endif
344
356
}
345
357
358
+
346
359
/**
347
360
* @brief Setup an apr_crypto_key_t from a given password and salt.
348
361
*
@@ -369,6 +382,7 @@ int msc_remote_grab_content(apr_pool_t *mp, const char *uri, const char *key,
369
382
* @retval n<-1 Something wrong happened, check error_msg for further details.
370
383
*
371
384
*/
385
+ #ifdef WITH_APU_CRYPTO
372
386
int msc_remote_enc_key_setup (apr_pool_t * pool ,
373
387
const char * key ,
374
388
apr_crypto_key_t * * apr_key ,
@@ -411,11 +425,6 @@ int msc_remote_enc_key_setup(apr_pool_t *pool,
411
425
* error_msg = "Internal error - apr_crypto_passphrase: APR_EKEYTYPE" ;
412
426
return -1 ;
413
427
}
414
- else if (rv == APR_EKEYTYPE )
415
- {
416
- * error_msg = "Internal error - apr_crypto_passphrase: APR_EKEYTYPE" ;
417
- return -1 ;
418
- }
419
428
else if (rv != APR_SUCCESS )
420
429
{
421
430
* error_msg = "Internal error - apr_crypto_passphrase: Unknown error" ;
@@ -424,6 +433,7 @@ int msc_remote_enc_key_setup(apr_pool_t *pool,
424
433
425
434
return 0 ;
426
435
}
436
+ #endif
427
437
428
438
/**
429
439
* @brief Decrypt an buffer into a memory buffer.
@@ -449,6 +459,7 @@ int msc_remote_enc_key_setup(apr_pool_t *pool,
449
459
* @retval n<-1 Something wrong happened, further details on error_msg.
450
460
*
451
461
*/
462
+ #ifdef WITH_APU_CRYPTO
452
463
int msc_remote_decrypt (apr_pool_t * pool ,
453
464
const char * key ,
454
465
struct msc_curl_memory_buffer_t * chunk ,
@@ -488,12 +499,9 @@ int msc_remote_decrypt(apr_pool_t *pool,
488
499
return -1 ;
489
500
}
490
501
491
- #ifndef APU_CRYPTO_RECOMMENDED_DRIVER
492
- rv = apr_crypto_get_driver (& driver , "openssl" , NULL , & err , pool );
493
- #else
494
502
rv = apr_crypto_get_driver (& driver , APU_CRYPTO_RECOMMENDED_DRIVER , NULL ,
495
503
& err , pool );
496
- #endif
504
+
497
505
if (rv != APR_SUCCESS || driver == NULL )
498
506
{
499
507
* error_msg = "Internal error - apr_crypto_get_driver: Unknown error" ;
@@ -573,7 +581,7 @@ int msc_remote_decrypt(apr_pool_t *pool,
573
581
574
582
return 0 ;
575
583
}
576
-
584
+ #endif
577
585
578
586
/**
579
587
* @brief Add SecRules from a given URI.
@@ -598,6 +606,8 @@ int msc_remote_add_rules_from_uri(cmd_parms *orig_parms,
598
606
msc_remote_rules_server * remote_rules_server ,
599
607
char * * error_msg )
600
608
{
609
+
610
+ #ifdef WITH_REMOTE_RULES
601
611
struct msc_curl_memory_buffer_t chunk_encrypted ;
602
612
unsigned char * plain_text = NULL ;
603
613
int len = 0 ;
@@ -618,7 +628,6 @@ int msc_remote_add_rules_from_uri(cmd_parms *orig_parms,
618
628
{
619
629
return -1 ;
620
630
}
621
-
622
631
/* error_msg is not filled when the user set SecRemoteRulesFailAction
623
632
* to warn
624
633
*/
@@ -629,14 +638,21 @@ int msc_remote_add_rules_from_uri(cmd_parms *orig_parms,
629
638
630
639
if (remote_rules_server -> crypto == 1 )
631
640
{
641
+ #ifdef WITH_APU_CRYPTO
632
642
msc_remote_decrypt (mp , remote_rules_server -> key , & chunk_encrypted ,
633
643
& plain_text ,
634
644
& plain_text_len ,
635
645
error_msg );
636
646
if (* error_msg != NULL )
637
647
{
648
+ msc_remote_clean_chunk (& chunk_encrypted );
638
649
return -1 ;
639
650
}
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
640
656
641
657
msc_remote_clean_chunk (& chunk_encrypted );
642
658
}
@@ -725,12 +741,17 @@ int msc_remote_add_rules_from_uri(cmd_parms *orig_parms,
725
741
{
726
742
msc_remote_clean_chunk (& chunk_encrypted );
727
743
}
744
+ #else
745
+ * error_msg = "SecRemoteRules was not enabled during ModSecurity " \
746
+ "compilation." ;
747
+ return -1 ;
748
+ #endif
728
749
}
729
750
730
751
731
752
int msc_remote_clean_chunk (struct msc_curl_memory_buffer_t * chunk )
732
753
{
733
- if (chunk -> size < = 0 )
754
+ if (chunk -> size = = 0 )
734
755
{
735
756
goto end ;
736
757
}
@@ -747,4 +768,3 @@ int msc_remote_clean_chunk(struct msc_curl_memory_buffer_t *chunk)
747
768
return 0 ;
748
769
}
749
770
750
- #endif
0 commit comments