Adds support to the operator @ipMatchFromFile and @ipMatchF

This commit is contained in:
Felipe Zimmerle
2015-08-03 17:21:13 -03:00
parent 6cd4c0492a
commit 774d897351
9 changed files with 214 additions and 47 deletions

View File

@@ -25,6 +25,7 @@
#include <iostream>
#include "utils/geo_lookup.h"
#include "utils/https_client.h"
namespace ModSecurity {
namespace Utils {
@@ -77,16 +78,12 @@ IpTree::~IpTree() {
}
}
bool IpTree::addFromBuffer(const std::string& buffer, std::string *error) {
bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
char *error_msg = NULL;
std::stringstream ss;
std::string line;
ss << buffer;
int res = 0;
for (std::string line; std::getline(ss, line); ) {
res = ip_tree_from_param(buffer.c_str(), &m_tree, &error_msg);
for (std::string line; std::getline(*ss, line); ) {
res = ip_tree_from_param(line.c_str(), &m_tree, &error_msg);
if (res != 0) {
if (error_msg != NULL) {
error->assign(error_msg);
@@ -99,6 +96,40 @@ bool IpTree::addFromBuffer(const std::string& buffer, std::string *error) {
}
bool IpTree::addFromBuffer(const std::string& buffer, std::string *error) {
std::stringstream ss;
ss << buffer;
return addFromBuffer(&ss, error);
}
bool IpTree::addFromFile(const std::string& file, std::string *error) {
std::ifstream myfile(file, std::ios::in);
if (myfile.is_open() == false) {
error->assign("Failed to open file: " + file);
return false;
}
return addFromBuffer(&myfile, error);
}
bool IpTree::addFromUrl(const std::string& url, std::string *error) {
HttpsClient c;
bool ret = c.download(url);
if (ret == false) {
error->assign(c.error);
} else {
ret = addFromBuffer(c.content, error);
}
return ret;
}
bool IpTree::contains(const std::string& ip) {
int res = 0;
char *error_msg = NULL;

View File

@@ -37,8 +37,11 @@ class IpTree {
~IpTree();
bool contains(const std::string &ip);
bool addFromBuffer(const std::string& buffer, std::string *error);
void postOrderTraversal(TreeNode *node);
bool addFromBuffer(std::istream *ss, std::string *error);
bool addFromBuffer(const std::string& buffer, std::string *error);
bool addFromFile(const std::string& file, std::string *error);
bool addFromUrl(const std::string& url, std::string *error);
private:
TreeRoot *m_tree;
};