Skip to content

Commit eb25e27

Browse files
committed
Add the AGDBM error logs and integrate it with ModSecurity
1 parent f16c31e commit eb25e27

File tree

3 files changed

+67
-41
lines changed

3 files changed

+67
-41
lines changed

apache2/ag_mdb/ag_mdb.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,9 +1084,9 @@ int AGMDB_removeStale(struct agmdb_handler *dbm) {
10841084
*/
10851085

10861086
/**
1087-
** Get the detail information of an error.
1088-
** @param error_no: the error code returned by a function.
1089-
** return: The error information.
1087+
** Get the detail information of an return code.
1088+
** @param return_code: the code returned by a function.
1089+
** return: The string including the detailed information.
10901090
*/
10911091
const char* AGMDB_getErrorInfo(int error_no){
10921092
switch(error_no){
@@ -1192,6 +1192,19 @@ const char* AGMDB_getErrorInfo(int error_no){
11921192
}
11931193
}
11941194

1195+
/**
1196+
** Check whether a return_code is an error.
1197+
** @param return_code: the code returned by a AGMDB function.
1198+
** return: True if there is an error;
1199+
False if not.
1200+
*/
1201+
bool AGMDB_isError(int return_code){
1202+
if (return_code < AGMDB_ERROR)
1203+
return true;
1204+
else
1205+
return false;
1206+
}
1207+
11951208
/**
11961209
** Get the number of keys in a database.
11971210
** You have to get SHARED or EXCLUSIVE LOCK of the database before calling this function.

apache2/ag_mdb/ag_mdb_external.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
extern "C" {
66
#endif
77

8+
#include "stdbool.h"
9+
810
/**
911
**========================================================
1012
** AG Memory Database Limitation Definition
@@ -256,11 +258,19 @@ int AGMDB_removeStale(struct agmdb_handler *dbm);
256258
**========================================================
257259
*/
258260
/**
259-
** Get the detail information of an error.
260-
** @param error_no: the error code returned by a function.
261-
** return: The error information.
261+
** Get the detail information of an return code.
262+
** @param return_code: the code returned by a function.
263+
** return: The string including the detailed information.
264+
*/
265+
const char* AGMDB_getErrorInfo(int return_code);
266+
267+
/**
268+
** Check whether a return_code is an error.
269+
** @param return_code: the code returned by a AGMDB function.
270+
** return: True if there is an error;
271+
False if not.
262272
*/
263-
const char* AGMDB_getErrorInfo(int error_no);
273+
bool AGMDB_isError(int return_code);
264274
/**
265275
** Get the number of keys in a database.
266276
** You have to get SHARED or EXCLUSIVE LOCK of the database before calling this function.

apache2/persist_dbm.c

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static apr_table_t *collection_retrieve_ex(int db_option, void *existing_dbm, mo
166166
}
167167
else{
168168
if (msr->txcfg->data_dir == NULL) {
169-
msr_log(msr, 1, "[ERROR]collection_retrieve_ex_origin: Unable to retrieve collection (name \"%s\", key \"%s\"). Use "
169+
msr_log(msr, 1, "collection_retrieve_ex_origin: Unable to retrieve collection (name \"%s\", key \"%s\"). Use "
170170
"SecDataDir to define data directory first.", log_escape(msr->mp, col_name),
171171
log_escape_ex(msr->mp, col_key, col_key_len));
172172
goto cleanup;
@@ -218,22 +218,22 @@ static apr_table_t *collection_retrieve_ex(int db_option, void *existing_dbm, mo
218218
//if not called by collection_store(), need to get lock
219219
if(existing_dbm == NULL ){
220220
rc = AGMDB_getSharedLock(ag_dbm);
221-
if(rc != AGMDB_SUCCESS){
222-
msr_log(msr, 1, "[ERROR]collection_retrieve_ex_agmdb: Failed to get shared lock");
221+
if(AGMDB_isError(rc)){
222+
msr_log(msr, 1, "collection_retrieve_ex_agmdb: Failed to get shared lock. Error info: %s.", AGMDB_getErrorInfo(rc));
223223
goto cleanup;
224224
}
225225
}
226226
rc = AGMDB_get(ag_dbm, col_key, col_key_len, buffer, AGMDB_MAX_ENTRY_SIZE, &tmp_val_len);
227-
if(rc != AGMDB_SUCCESS) {
228-
msr_log(msr, 1, "[ERROR]collection_retrieve_ex_agmdb: Failed to read from database \"%s\": %s", log_escape(msr->mp,
229-
col_name), col_key);
227+
if(AGMDB_isError(rc)) {
228+
msr_log(msr, 1, "collection_retrieve_ex_agmdb: Failed to read from database \"%s\": %s. Error info: %s.", log_escape(msr->mp,
229+
col_name), col_key, AGMDB_getErrorInfo(rc));
230230
goto cleanup;
231231
}
232232

233233
if(existing_dbm == NULL ){
234234
rc = AGMDB_freeSharedLock(ag_dbm);
235-
if(rc != AGMDB_SUCCESS) {
236-
msr_log(msr, 1, "[ERROR]collection_retrieve_ex_agmdb: Failed to free shared lock");
235+
if(AGMDB_isError(rc)) {
236+
msr_log(msr, 1, "collection_retrieve_ex_agmdb: Failed to free shared lock. Error info: %s.", AGMDB_getErrorInfo(rc));
237237
goto cleanup;
238238
}
239239
}
@@ -316,19 +316,19 @@ static apr_table_t *collection_retrieve_ex(int db_option, void *existing_dbm, mo
316316
if(db_option == DB_OPT_AGMDB){
317317
if(existing_dbm == NULL){
318318
rc = AGMDB_getExclusiveLock(ag_dbm);
319-
if(rc != AGMDB_SUCCESS){
320-
msr_log(msr, 1, "collection_retrieve_ex: Failed to get exclusive lock");
319+
if(AGMDB_isError(rc)){
320+
msr_log(msr, 1, "collection_retrieve_ex: Failed to get exclusive lock. Error info: %s.", AGMDB_getErrorInfo(rc));
321321
goto cleanup;
322322
}
323323
}
324324
rc = AGMDB_delete(ag_dbm, col_key, col_key_len);
325-
if(rc != AGMDB_SUCCESS)
325+
if(AGMDB_isError(rc))
326326
fail_flag = 1;
327327

328328
if(existing_dbm == NULL){
329329
rc = AGMDB_freeExclusiveLock(ag_dbm);
330-
if(rc != AGMDB_SUCCESS){
331-
msr_log(msr, 1, "collection_retrieve_ex: Failed to free exclusive lock");
330+
if(AGMDB_isError(rc)){
331+
msr_log(msr, 1, "collection_retrieve_ex: Failed to free exclusive lock. Error info: %s.", AGMDB_getErrorInfo(rc));
332332
goto cleanup;
333333
}
334334
}
@@ -494,7 +494,7 @@ static int collection_store_ex(int db_option, modsec_rec *msr, apr_table_t *col)
494494
msc_string *var_name = NULL, *var_key = NULL;
495495
unsigned char *blob = NULL;
496496
unsigned int blob_size = 0, blob_offset = 0;
497-
int rc,rc2;
497+
int rc;
498498
apr_sdbm_datum_t key;
499499
apr_sdbm_datum_t value;
500500
const apr_array_header_t *arr;
@@ -598,7 +598,7 @@ static int collection_store_ex(int db_option, modsec_rec *msr, apr_table_t *col)
598598
root_dcfg = msr->dcfg1->root_config;
599599
dbm_filename = apr_pstrcat(root_dcfg->mp, root_dcfg->data_dir, "/", var_name->value, NULL);
600600
if(root_dcfg == NULL){
601-
msr_log_error(msr, "[ERROR]collection_retrieve_ex_agmdb: Cannot find root_config in msr->dcfg1.");
601+
msr_log(msr, 1, "collection_retrieve_ex_agmdb: Cannot find root_config in msr->dcfg1.");
602602
goto error;
603603
}
604604
new_handle = (struct agmdb_handle_entry *)apr_pcalloc(root_dcfg->mp, sizeof(struct agmdb_handle_entry));
@@ -607,8 +607,8 @@ static int collection_store_ex(int db_option, modsec_rec *msr, apr_table_t *col)
607607
strcpy((char*)(new_handle->col_name), var_name->value);
608608

609609
rc = AGMDB_openDB(new_handle->handle, dbm_filename, strlen(dbm_filename), MAXIMUM_AGMDB_ENTRY_NUM);
610-
if(rc != AGMDB_SUCCESS){
611-
msr_log(msr, 1, "[ERROR]collection_retrieve_ex_agmdb: Failed to create DBM name: %s", apr_psprintf(msr->mp, "%.*s", var_name->value_len, var_name->value));
610+
if(AGMDB_isError(rc)){
611+
msr_log(msr, 1, "collection_retrieve_ex_agmdb: Failed to create DBM name: %s. Error info: %s", apr_psprintf(msr->mp, "%.*s", var_name->value_len, var_name->value), AGMDB_getErrorInfo(rc));
612612
goto error;
613613
}
614614
ag_dbm = new_handle->handle;
@@ -619,7 +619,7 @@ static int collection_store_ex(int db_option, modsec_rec *msr, apr_table_t *col)
619619
// ENH: lowercase the var name in the filename
620620
dbm_filename = apr_pstrcat(msr->mp, msr->txcfg->data_dir, "/", var_name->value, NULL);
621621
if (msr->txcfg->debuglog_level >= 9) {
622-
msr_log(msr, 9, "[ERRNO]collection_store_ex_origin: Retrieving collection (name \"%s\", filename \"%s\")",log_escape(msr->mp, var_name->value),
622+
msr_log(msr, 9, "collection_store_ex_origin: Retrieving collection (name \"%s\", filename \"%s\")",log_escape(msr->mp, var_name->value),
623623
log_escape(msr->mp, dbm_filename));
624624
}
625625

@@ -639,7 +639,7 @@ static int collection_store_ex(int db_option, modsec_rec *msr, apr_table_t *col)
639639
#ifdef GLOBAL_COLLECTION_LOCK
640640
apr_global_mutex_unlock(msr->modsecurity->dbm_lock);
641641
#endif
642-
msr_log(msr, 1, "[ERRNO]collection_store_ex_origin: Failed to access DBM file \"%s\": %s", log_escape(msr->mp, dbm_filename),
642+
msr_log(msr, 1, "collection_store_ex_origin: Failed to access DBM file \"%s\": %s", log_escape(msr->mp, dbm_filename),
643643
get_apr_error(msr->mp, rc));
644644
apr_dbm = NULL;
645645
goto error;
@@ -653,12 +653,12 @@ static int collection_store_ex(int db_option, modsec_rec *msr, apr_table_t *col)
653653
//---------------------------------
654654
if(db_option == DB_OPT_AGMDB){
655655
rc = AGMDB_getExclusiveLock(ag_dbm);
656-
if (rc != AGMDB_SUCCESS) {
656+
if (AGMDB_isError(rc)) {
657657
#ifdef _WIN32
658658
int lasterr = (int)GetLastError();
659-
msr_log(msr, 1, "collection_store: Failed to getExclusiveLock, lasterr = %d", lasterr);
659+
msr_log(msr, 1, "collection_store: Failed to getExclusiveLock, lasterr = %d. Error info: %s", lasterr, AGMDB_getErrorInfo(rc));
660660
#else
661-
msr_log(msr, 1, "collection_store: Failed to getExclusiveLock, errno = %d", errno);
661+
msr_log(msr, 1, "collection_store: Failed to getExclusiveLock, errno = %d. Error info: %s", errno, AGMDB_getErrorInfo(rc));
662662
#endif
663663
goto error;
664664
}
@@ -668,7 +668,7 @@ static int collection_store_ex(int db_option, modsec_rec *msr, apr_table_t *col)
668668
/* Need to lock to pull in the stored data again and apply deltas. */
669669
rc = apr_sdbm_lock(apr_dbm, APR_FLOCK_EXCLUSIVE);
670670
if (rc != APR_SUCCESS) {
671-
msr_log(msr, 1, "[ERRNO]collection_store_ex_origin: Failed to exclusivly lock DBM file \"%s\": %s", log_escape(msr->mp, dbm_filename),
671+
msr_log(msr, 1, "collection_store_ex_origin: Failed to exclusivly lock DBM file \"%s\": %s", log_escape(msr->mp, dbm_filename),
672672
get_apr_error(msr->mp, rc));
673673
goto error;
674674
}
@@ -745,8 +745,10 @@ static int collection_store_ex(int db_option, modsec_rec *msr, apr_table_t *col)
745745
if (blob == NULL) {
746746
if (ag_dbm != NULL) {
747747
rc = AGMDB_freeExclusiveLock(ag_dbm);
748+
if(AGMDB_isError(rc))
749+
msr_log(msr, 1, "collection_stror_ex: Fail to free exclusive lock. Error info: %s", AGMDB_getErrorInfo(rc));
748750
}
749-
msr_log_error(msr, "[ERROR]collection_store_ex_agdb: fail to create blob");
751+
msr_log(msr, 1, "collection_store_ex_agdb: fail to create blob");
750752
return -1;
751753
}
752754
}
@@ -815,15 +817,14 @@ static int collection_store_ex(int db_option, modsec_rec *msr, apr_table_t *col)
815817
value.dsize = blob_size;
816818

817819
if(db_option == DB_OPT_AGMDB) {
818-
rc2 = AGMDB_set(ag_dbm, var_key->value, var_key->value_len, (char*)blob, blob_size);
819-
rc = AGMDB_freeExclusiveLock(ag_dbm);
820-
821-
if(rc2 != AGMDB_SUCCESS) {
822-
msr_log(msr, 1, "[ERROR]collection_store_ex_agmdb: Failed to write to database key: %s", var_key->value);
823-
return -1;
820+
rc = AGMDB_set(ag_dbm, var_key->value, var_key->value_len, (char*)blob, blob_size);
821+
if(AGMDB_isError(rc)) {
822+
msr_log(msr, 1, "collection_store_ex_agmdb: Failed to write to database key: %s. Error info: %s.", var_key->value, AGMDB_getErrorInfo(rc));
824823
}
825-
if(rc != AGMDB_SUCCESS){
826-
msr_log(msr, 1, "[ERROR]collection_store_ex_agmdb: Failed to free exclusive lock");
824+
825+
rc = AGMDB_freeExclusiveLock(ag_dbm);
826+
if(AGMDB_isError(rc)){
827+
msr_log(msr, 1, "collection_store_ex_agmdb: Failed to free exclusive lock. Error info: %s.", AGMDB_getErrorInfo(rc));
827828
return -1;
828829
}
829830
}
@@ -895,7 +896,7 @@ static int collections_remove_stale_ex(int db_option, modsec_rec *msr, const cha
895896
apr_array_header_t *keys_arr;
896897
char **keys;
897898
apr_time_t now = apr_time_sec(msr->request_time);
898-
int i;
899+
int i,rc2;
899900

900901
//---------------------------------
901902
//AGMDB
@@ -906,7 +907,9 @@ static int collections_remove_stale_ex(int db_option, modsec_rec *msr, const cha
906907
ag_dbm = dcfg_searchAGMDBhandler(col_name, (struct agmdb_handle_entry*)(root_dcfg->agmdb_handles));
907908
if(ag_dbm == NULL)
908909
return 1;
909-
return AGMDB_removeStale(ag_dbm);
910+
rc2 = AGMDB_removeStale(ag_dbm);
911+
if(AGMDB_isError(rc2))
912+
msr_log(msr, 1, "collections_remove_stale_ex_agmdb: error in remove stale. Error info: %s", AGMDB_getErrorInfo(rc2));
910913
}
911914
//---------------------------------
912915
//apr_sdbm

0 commit comments

Comments
 (0)