From 0720fd4790de924ea20d28dfd076590ed54940c6 Mon Sep 17 00:00:00 2001 From: Felipe Zimmerle Date: Tue, 4 Aug 2015 13:55:07 -0300 Subject: [PATCH] Adds operator @pmFromFile and @pmF --- src/operators/pm_f.cc | 16 +------------- src/operators/pm_f.h | 10 +++++---- src/operators/pm_from_file.cc | 39 +++++++++++++++++++++++++---------- src/operators/pm_from_file.h | 12 +++++++---- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/operators/pm_f.cc b/src/operators/pm_f.cc index 0480bd8e..760d6383 100644 --- a/src/operators/pm_f.cc +++ b/src/operators/pm_f.cc @@ -17,25 +17,11 @@ #include -#include "operators/operator.h" +#include "operators/pm_from_file.h" namespace ModSecurity { namespace operators { -bool PmF::evaluate(Assay *assay) { - /** - * @todo Implement the operator PmF. - * Reference: https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#pmf - */ - return true; -} - - -PmF::PmF(std::string op, std::string param, bool negation) - : Operator() { - this->op = op; - this->param = param; -} } // namespace operators } // namespace ModSecurity diff --git a/src/operators/pm_f.h b/src/operators/pm_f.h index ff2e6722..960b6c28 100644 --- a/src/operators/pm_f.h +++ b/src/operators/pm_f.h @@ -18,19 +18,21 @@ #include -#include "operators/operator.h" +#include "operators/pm_from_file.h" #ifdef __cplusplus namespace ModSecurity { namespace operators { -class PmF : public Operator { + +class PmF : public PmFromFile { public: /** @ingroup ModSecurity_Operator */ - PmF(std::string o, std::string p, bool i); - bool evaluate(Assay *assay); + PmF(std::string op, std::string param, bool negation) + : PmFromFile(op, param, negation) { } }; + } // namespace operators } // namespace ModSecurity #endif diff --git a/src/operators/pm_from_file.cc b/src/operators/pm_from_file.cc index 59743406..acff9b9b 100644 --- a/src/operators/pm_from_file.cc +++ b/src/operators/pm_from_file.cc @@ -18,24 +18,41 @@ #include #include "operators/operator.h" +#include "utils/https_client.h" namespace ModSecurity { namespace operators { -bool PmFromFile::evaluate(Assay *assay) { - /** - * @todo Implement the operator PmFromFile. - * Reference: https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#pmfromfile - */ + +bool PmFromFile::init(const char **error) { + std::istream *iss; + + if (param.compare(0, 8, "https://") == 0) { + Utils::HttpsClient client; + bool ret = client.download(param); + if (ret == false) { + *error = client.error.c_str(); + return false; + } + iss = new std::stringstream(client.content); + } else { + iss = new std::ifstream(param, std::ios::in); + + if (((std::ifstream *)iss)->is_open() == false) { + *error = std::string("Failed to open file: " + param).c_str(); + return false; + } + } + + for (std::string line; std::getline(*iss, line); ) { + acmp_add_pattern(m_p, line.c_str(), NULL, NULL, line.length()); + } + + acmp_prepare(m_p); + return true; } -PmFromFile::PmFromFile(std::string op, std::string param, bool negation) - : Operator() { - this->op = op; - this->param = param; -} - } // namespace operators } // namespace ModSecurity diff --git a/src/operators/pm_from_file.h b/src/operators/pm_from_file.h index d0dc1d97..e39452ce 100644 --- a/src/operators/pm_from_file.h +++ b/src/operators/pm_from_file.h @@ -18,19 +18,23 @@ #include -#include "operators/operator.h" +#include "operators/pm.h" #ifdef __cplusplus namespace ModSecurity { namespace operators { -class PmFromFile : public Operator { + +class PmFromFile : public Pm { public: /** @ingroup ModSecurity_Operator */ - PmFromFile(std::string o, std::string p, bool i); - bool evaluate(Assay *assay); + PmFromFile(std::string op, std::string param, bool negation) + : Pm(op, param, negation) { } + + bool init(const char **error) override; }; + } // namespace operators } // namespace ModSecurity