Adds support to GeoIP operator and variables.

This commit is contained in:
Felipe Zimmerle
2015-07-20 21:04:21 -03:00
parent 41bf1490b7
commit e189055ec3
17 changed files with 890 additions and 53 deletions

62
src/utils/geo_lookup.cc Normal file
View File

@@ -0,0 +1,62 @@
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address security@modsecurity.org.
*
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <fstream>
#include <iostream>
#include "utils/geo_lookup.h"
#include <GeoIPCity.h>
namespace ModSecurity {
namespace Utils {
bool GeoLookup::setDataBase(std::string file_path) {
m_gi = GeoIP_open(file_path.c_str(), GEOIP_INDEX_CACHE);
if (m_gi == NULL) {
return false;
}
return true;
}
bool GeoLookup::lookup(const std::string& target, GeoIPRecord **gir,
std::function<bool(int, std::string)> debug) {
if (m_gi == NULL) {
debug(4, "GeoIP: Database is not open. Use: SecGeoLookupDb directive.");
return false;
}
*gir = GeoIP_record_by_name(m_gi, target.c_str());
if (*gir == NULL) {
return false;
}
return true;
}
} // namespace Utils
} // namespace ModSecurity

56
src/utils/geo_lookup.h Normal file
View File

@@ -0,0 +1,56 @@
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address security@modsecurity.org.
*
*/
#include <iostream>
#include <fstream>
#include <string>
#include <functional>
#include <GeoIPCity.h>
#ifndef SRC_UTILS_GEO_LOOKUP_H_
#define SRC_UTILS_GEO_LOOKUP_H_
#include "modsecurity/assay.h"
namespace ModSecurity {
namespace Utils {
class GeoLookup {
public:
static GeoLookup& getInstance() {
static GeoLookup instance;
return instance;
}
bool setDataBase(std::string file_path);
bool lookup(const std::string& target, GeoIPRecord **georec,
std::function<bool(int, std::string)> callback);
private:
GeoLookup() : m_gi(NULL) {}
GeoLookup(GeoLookup const&);
void operator=(GeoLookup const&);
GeoIP *m_gi;
};
} // namespace Utils
} // namespace ModSecurity
#endif // SRC_UTILS_GEO_LOOKUP_H_