diff --git a/headers/modsecurity/transaction.h b/headers/modsecurity/transaction.h index 194b544c52..7fa422d7fe 100644 --- a/headers/modsecurity/transaction.h +++ b/headers/modsecurity/transaction.h @@ -718,6 +718,9 @@ int msc_process_logging(Transaction *transaction); /** @ingroup ModSecurity_C_API */ int msc_update_status_code(Transaction *transaction, int status); +/** @ingroup ModSecurity_C_API */ +const char *msc_get_transaction_variable(Transaction *transaction, const char *var_name); + #ifdef __cplusplus } } // namespace modsecurity diff --git a/src/transaction.cc b/src/transaction.cc index 678a89d9de..734bbe6fc1 100644 --- a/src/transaction.cc +++ b/src/transaction.cc @@ -2356,6 +2356,66 @@ extern "C" int msc_update_status_code(Transaction *transaction, int status) { return transaction->updateStatusCode(status); } +/** + * @name msc_get_transaction_variable + * @brief Retrieve variable value by name. + * + * This function returns tx_collection variable value by name. + * + * @param transaction ModSecurity transaction. + * @param var_name variable name + * @return variable value. + * + */ +extern "C" const char *msc_get_transaction_variable(Transaction *transaction, const char *var_name){ + std::vector<const VariableValue *> l; + modsecurity::collection::Collection *c = NULL; + std::string cn; + const char *p = NULL; + + if (var_name == NULL ){ + return NULL; + } + + p = strchr(var_name,':'); + + if ( p == NULL ){ + return NULL; + } + + cn = std::string(var_name,p-var_name); + transform(cn.begin(),cn.end(),cn.begin(),::tolower); + + if ( cn.compare("global") == 0){ + c = transaction->m_collections.m_global_collection; + }else if ( cn.compare("ip") == 0){ + c = transaction->m_collections.m_ip_collection; + }else if ( cn.compare("session") == 0){ + c = transaction->m_collections.m_session_collection; + }else if ( cn.compare("user") == 0){ + c = transaction->m_collections.m_user_collection; + }else if ( cn.compare("resource") == 0){ + c = transaction->m_collections.m_resource_collection; + }else if( cn.compare("tx") == 0){ + c = transaction->m_collections.m_tx_collection; + } + + if(c == NULL){ + return NULL; + } + + ++p; + cn = std::string(p); + transform(cn.begin(),cn.end(),cn.begin(),::tolower); + + c->resolveSingleMatch(cn.c_str(),&l); + + if ( l.size() < 1){ + return NULL; + } + + return l[0]->getValue().c_str(); +} } // namespace modsecurity