From 2138dd1369f6b4c900f36b6610deb6ae893035ea Mon Sep 17 00:00:00 2001 From: Felipe Zimmerle Date: Thu, 9 Jul 2015 15:13:43 -0300 Subject: [PATCH] Adds method setConnectorInformation to ModSecurity class For the purpose of log it is necessary for modsecurity to understand which 'connector' is consuming the API. --- examples/simple_example_using_c/test.c | 3 ++ headers/modsecurity/modsecurity.h | 7 +++ src/modsecurity.cc | 63 +++++++++++++++++++++++++- test/benchmark/benchmark.cc | 3 ++ test/regression/regression.cc | 2 + 5 files changed, 77 insertions(+), 1 deletion(-) diff --git a/examples/simple_example_using_c/test.c b/examples/simple_example_using_c/test.c index f17ab187..4d49a75b 100644 --- a/examples/simple_example_using_c/test.c +++ b/examples/simple_example_using_c/test.c @@ -30,6 +30,9 @@ int main (int argc, char **argv) modsec = msc_init(); + msc_set_connector_info(modsec, "ModSecurity-test v0.0.1-alpha (Simple " \ + "example on how to use ModSecurity API"); + rules = msc_create_rules_set(); msc_rules_add_file(rules, main_rule_uri); diff --git a/headers/modsecurity/modsecurity.h b/headers/modsecurity/modsecurity.h index a2c70fa9..0016707d 100644 --- a/headers/modsecurity/modsecurity.h +++ b/headers/modsecurity/modsecurity.h @@ -124,6 +124,8 @@ class ModSecurity { ~ModSecurity() { } static std::string whoAmI(); + void setConnectorInformation(std::string connector); + const std::string& getConnectorInformation(); /** * @@ -200,6 +202,9 @@ class ModSecurity { */ NUMBER_OF_PHASES, }; + + private: + std::string m_connector; }; @@ -213,6 +218,8 @@ extern "C" { ModSecurity *msc_init(); /** @ingroup ModSecurity_C_API */ const char *msc_who_am_i(ModSecurity *msc); +/** @ingroup ModSecurity_C_API */ +void msc_set_connector_info(ModSecurity *msc, const char *connector); #ifdef __cplusplus } diff --git a/src/modsecurity.cc b/src/modsecurity.cc index 4bca23f8..3fde10d5 100644 --- a/src/modsecurity.cc +++ b/src/modsecurity.cc @@ -38,7 +38,8 @@ namespace ModSecurity { * * @endcode */ -ModSecurity::ModSecurity() { +ModSecurity::ModSecurity() + : m_connector("") { UniqueId::uniqueId(); srand(time(NULL)); } @@ -84,6 +85,66 @@ std::string ModSecurity::whoAmI() { } +/** + * @name setConnectorInformation + * @brief Set information about the connector that is using the library. + * + * For the purpose of log it is necessary for modsecurity to understand which + * 'connector' is consuming the API. + * + * @note It is strongly recommended to set a information in the following + * pattern: + * + * ConnectorName vX.Y.Z-tag (something else) + * + * For instance: ModSecurity-nginx v0.0.1-alpha (Whee) + * + * @param connector Information about the connector. + * + */ +void ModSecurity::setConnectorInformation(std::string connector) { + m_connector = connector; +} + + +/** + * @name getConnectorInformation + * @brief Returns the connector information. + * + * Returns whatever was set by 'setConnectorInformation'. Check + * setConnectorInformation documentation to understand the expected format. + * + * @retval "" Nothing was informed about the connector. + * @retval !="" Connector information. + */ +const std::string& ModSecurity::getConnectorInformation() { + return m_connector; +} + + +/** + * @name msc_set_connector_info + * @brief Set information about the connector that is using the library. + * + * For the purpose of log it is necessary for modsecurity to understand which + * 'connector' is consuming the API. + * + * @note It is strongly recommended to set a information in the following + * pattern: + * + * ConnectorName vX.Y.Z-tag (something else) + * + * For instance: ModSecurity-nginx v0.0.1-alpha (Whee) + * + * @param connector Information about the connector. + * + */ +extern "C" void msc_set_connector_info(ModSecurity *msc, + const char *connector) { + msc->setConnectorInformation(std::string(connector)); +} + + /** * @name msc_who_am_i * @brief Return information about this ModSecurity version and platform. diff --git a/test/benchmark/benchmark.cc b/test/benchmark/benchmark.cc index 50b8e010..379284c4 100644 --- a/test/benchmark/benchmark.cc +++ b/test/benchmark/benchmark.cc @@ -74,6 +74,9 @@ int main(int argc, char *argv[]) { ModSecurity::Rules *rules; modsec = new ModSecurity::ModSecurity(); + modsec->setConnectorInformation("ModSecurity-benchmark v0.0.1-alpha" \ + " (ModSecurity benchmark utility)"); + rules = new ModSecurity::Rules(); rules->loadFromUri(rules_file); diff --git a/test/regression/regression.cc b/test/regression/regression.cc index 38cf1544..17a667fb 100644 --- a/test/regression/regression.cc +++ b/test/regression/regression.cc @@ -70,6 +70,8 @@ void perform_unit_test(std::vector *tests, r.status = 200; modsec = new ModSecurity::ModSecurity(); + modsec->setConnectorInformation("ModSecurity-regression v0.0.1-alpha" \ + " (ModSecurity regression test utility)"); modsec_rules = new ModSecurity::Rules(debug_log); if (modsec_rules->load(t->rules.c_str()) == false) {