| 1 |
// For all support, instructions and copyright go to: |
| 2 |
// http://e2guardian.org/ |
| 3 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 4 |
|
| 5 |
// INCLUDES |
| 6 |
|
| 7 |
#ifdef HAVE_CONFIG_H |
| 8 |
#include "e2config.h" |
| 9 |
#endif |
| 10 |
#include <cstdint> |
| 11 |
#include "OptionContainer.hpp" |
| 12 |
#include "FOptionContainer.hpp" |
| 13 |
#include "Logger.hpp" |
| 14 |
|
| 15 |
#include <iostream> |
| 16 |
#include <fstream> |
| 17 |
#include <sys/socket.h> |
| 18 |
#include <netinet/in.h> |
| 19 |
#include <arpa/inet.h> |
| 20 |
#include <algorithm> |
| 21 |
#include <memory> |
| 22 |
#include <list> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
// GLOBALS |
| 26 |
|
| 27 |
extern OptionContainer o; |
| 28 |
|
| 29 |
// INPLEMENTATION |
| 30 |
|
| 31 |
// clear out the list |
| 32 |
void IPList::reset() |
| 33 |
{ |
| 34 |
iplist.clear(); |
| 35 |
iprangelist.clear(); |
| 36 |
// ipsubnetlist.clear(); |
| 37 |
hostlist.clear(); |
| 38 |
} |
| 39 |
|
| 40 |
// search for IP in list of individual IPs, ranges, subnets and - if reverse lookups are enabled - hostnames. |
| 41 |
bool IPList::inList(const std::string &ipstr, std::string *&host) const |
| 42 |
{ |
| 43 |
struct in_addr addr; |
| 44 |
inet_aton(ipstr.c_str(), &addr); |
| 45 |
uint32_t ip = ntohl(addr.s_addr); |
| 46 |
// start with individual IPs |
| 47 |
if (std::binary_search(iplist.begin(), iplist.end(), ip)) { |
| 48 |
// only return a hostname if that's what we matched against |
| 49 |
delete host; |
| 50 |
host = NULL; |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
// ranges |
| 55 |
//for (std::vector<ipl_rangestruct>::const_iterator i = iprangelist.begin(); i != iprangelist.end(); ++i) { |
| 56 |
//if ((ip >= i->startaddr) && (ip <= i->endaddr)) { |
| 57 |
//delete host; |
| 58 |
//host = NULL; |
| 59 |
//return true; |
| 60 |
//} |
| 61 |
//} |
| 62 |
if (!iprangelist.empty()) |
| 63 |
{ |
| 64 |
ipl_rangestruct t; |
| 65 |
t.startaddr = ip; |
| 66 |
auto one_above = std::upper_bound(iprangelist.begin(), iprangelist.end(),t); |
| 67 |
if (one_above != iprangelist.begin()) { |
| 68 |
auto i = one_above; |
| 69 |
i--; // move pointer to record which is the highest value that is less or equal to ip. |
| 70 |
if ((ip >= i->startaddr) && (ip <= i->endaddr)) { |
| 71 |
delete host; |
| 72 |
host = NULL; |
| 73 |
return true; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// subnets |
| 79 |
//for (std::list<ipl_subnetstruct>::const_iterator i = ipsubnetlist.begin(); i != ipsubnetlist.end(); ++i) { |
| 80 |
//if (i->maskedaddr == (ip & i->mask)) { |
| 81 |
//delete host; |
| 82 |
//host = NULL; |
| 83 |
//return true; |
| 84 |
//} |
| 85 |
//} |
| 86 |
|
| 87 |
// hostnames |
| 88 |
// TODO - take in a suggested hostname, look up only if not supplied, and return suggestion if found |
| 89 |
if (o.conn.reverse_client_ip_lookups) { |
| 90 |
std::unique_ptr<std::deque<String> > hostnames; |
| 91 |
if (host == NULL) |
| 92 |
hostnames.reset(ipToHostname(ipstr.c_str())); |
| 93 |
else { |
| 94 |
hostnames.reset(new std::deque<String>); |
| 95 |
hostnames->push_back(*host); |
| 96 |
} |
| 97 |
for (std::deque<String>::iterator i = hostnames->begin(); i != hostnames->end(); ++i) { |
| 98 |
if (std::binary_search(hostlist.begin(), hostlist.end(), *i)) { |
| 99 |
delete host; |
| 100 |
host = new std::string(i->toCharArray()); |
| 101 |
return true; |
| 102 |
} |
| 103 |
} |
| 104 |
// Even if we don't match anything, return a hostname |
| 105 |
// if desired for logging and we don't already have one. |
| 106 |
if (o.log.log_client_hostnames && (host == NULL) && (hostnames->size() > 0)) |
| 107 |
host = new std::string(hostnames->front().toCharArray()); |
| 108 |
} |
| 109 |
|
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
bool IPList::ifsreadIPMelangeList(std::ifstream *input, bool checkendstring, const char *endstring) |
| 114 |
{ |
| 115 |
// compile regexps for determining whether a list entry is an IP, a subnet (IP + mask), or a range |
| 116 |
RegExp matchIP, matchSubnet, matchRange, matchCIDR; |
| 117 |
#ifdef HAVE_PCRE |
| 118 |
matchIP.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"); |
| 119 |
matchSubnet.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"); |
| 120 |
matchSubnet.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"); |
| 121 |
matchCIDR.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/\\d{1,2}$"); |
| 122 |
matchRange.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}-\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"); |
| 123 |
#else |
| 124 |
matchIP.comp("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"); |
| 125 |
matchSubnet.comp("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"); |
| 126 |
matchCIDR.comp("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/[0-9]{1,2}$"); |
| 127 |
matchRange.comp("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}-[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"); |
| 128 |
#endif |
| 129 |
RegResult Rre; |
| 130 |
|
| 131 |
// read in the file |
| 132 |
String line; |
| 133 |
char buffer[2048]; |
| 134 |
while (input) { |
| 135 |
if (!input->getline(buffer, sizeof(buffer))) { |
| 136 |
break; |
| 137 |
} |
| 138 |
line = buffer; |
| 139 |
if (checkendstring && line.startsWith(endstring)) { |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
// ignore comments |
| 144 |
if (buffer[0] == '#') |
| 145 |
continue; |
| 146 |
// ignore blank lines |
| 147 |
if (strlen(buffer) < 7) |
| 148 |
continue; |
| 149 |
|
| 150 |
DEBUG_trace(""); |
| 151 |
// store the IP address (numerically, not as a string) and filter group in either the IP list, subnet list or range list |
| 152 |
if (matchIP.match(line.toCharArray(),Rre)) { |
| 153 |
struct in_addr address; |
| 154 |
if (inet_aton(line.toCharArray(), &address)) { |
| 155 |
uint32_t addr = ntohl(address.s_addr); |
| 156 |
iplist.push_back(addr); |
| 157 |
} |
| 158 |
} else if (matchSubnet.match(line.toCharArray(),Rre)) { |
| 159 |
struct in_addr address; |
| 160 |
struct in_addr addressmask; |
| 161 |
String subnet(line.before("/")); |
| 162 |
String mask(line.after("/")); |
| 163 |
if (inet_aton(subnet.toCharArray(), &address) && inet_aton(mask.toCharArray(), &addressmask)) { |
| 164 |
ipl_rangestruct s; |
| 165 |
uint32_t addr = ntohl(address.s_addr); |
| 166 |
uint32_t imask = ntohl(addressmask.s_addr); |
| 167 |
s.startaddr = addr; |
| 168 |
s.endaddr = addr | ~imask; |
| 169 |
iprangelist.push_back(s); |
| 170 |
} |
| 171 |
} else if (matchCIDR.match(line.toCharArray(),Rre)) { |
| 172 |
struct in_addr address; |
| 173 |
struct in_addr addressmask; |
| 174 |
String subnet(line.before("/")); |
| 175 |
String cidr(line.after("/")); |
| 176 |
int m = cidr.toInteger(); |
| 177 |
int host_part = 32 - m; |
| 178 |
if (host_part > -1) { |
| 179 |
String mask = (0xFFFFFFFF << host_part); |
| 180 |
if (inet_aton(subnet.toCharArray(), &address) && inet_aton(mask.toCharArray(), &addressmask)) { |
| 181 |
ipl_rangestruct s; |
| 182 |
uint32_t addr = ntohl(address.s_addr); |
| 183 |
uint32_t imask = ntohl(addressmask.s_addr); |
| 184 |
s.startaddr = addr; |
| 185 |
s.endaddr = addr | ~imask; |
| 186 |
iprangelist.push_back(s); |
| 187 |
} |
| 188 |
} |
| 189 |
} else if (matchRange.match(line.toCharArray(),Rre)) { |
| 190 |
struct in_addr addressstart; |
| 191 |
struct in_addr addressend; |
| 192 |
String start(line.before("-")); |
| 193 |
String end(line.after("-")); |
| 194 |
if (inet_aton(start.toCharArray(), &addressstart) && inet_aton(end.toCharArray(), &addressend)) { |
| 195 |
ipl_rangestruct r; |
| 196 |
r.startaddr = ntohl(addressstart.s_addr); |
| 197 |
r.endaddr = ntohl(addressend.s_addr); |
| 198 |
iprangelist.push_back(r); |
| 199 |
} |
| 200 |
} |
| 201 |
// hmmm. the line didn't match any of our regular expressions. |
| 202 |
// assume it's a hostname. |
| 203 |
else { |
| 204 |
line.toLower(); |
| 205 |
hostlist.push_back(line); |
| 206 |
} |
| 207 |
} |
| 208 |
DEBUG_trace("starting sort"); |
| 209 |
std::sort(iplist.begin(), iplist.end()); |
| 210 |
std::sort(iprangelist.begin(), iprangelist.end()); |
| 211 |
std::sort(hostlist.begin(), hostlist.end()); |
| 212 |
DEBUG_trace("sort complete"); |
| 213 |
#ifdef DEBUG_LOW |
| 214 |
DEBUG_debug("ip list dump:"); |
| 215 |
std::vector<uint32_t>::iterator i = iplist.begin(); |
| 216 |
while (i < iplist.end()) { |
| 217 |
DEBUG_debug("IP: ", String(*i)); |
| 218 |
++i; |
| 219 |
} |
| 220 |
//DEBUG_debug("subnet list dump:"); |
| 221 |
//std::list<ipl_subnetstruct>::iterator j = ipsubnetlist.begin(); |
| 222 |
//while (j != ipsubnetlist.end()) { |
| 223 |
// DEBUG_debug("Masked IP: ", String(j->maskedaddr), " Mask: ", String(j->mask)); |
| 224 |
// ++j; |
| 225 |
// } |
| 226 |
DEBUG_debug("range list dump:"); |
| 227 |
std::vector<ipl_rangestruct>::iterator k = iprangelist.begin(); |
| 228 |
while (k < iprangelist.end()) { |
| 229 |
DEBUG_debug("Start IP: ", String(k->startaddr), " End IP: ", String(k->endaddr)); |
| 230 |
++k; |
| 231 |
} |
| 232 |
DEBUG_debug("host list dump:"); |
| 233 |
std::vector<String>::iterator l = hostlist.begin(); |
| 234 |
while (l < hostlist.end()) { |
| 235 |
DEBUG_debug("Hostname: ", *l ); |
| 236 |
++l; |
| 237 |
} |
| 238 |
#endif |
| 239 |
return true; |
| 240 |
} |
| 241 |
|
| 242 |
// read in a list linking IPs, subnets & IP ranges to filter groups |
| 243 |
bool IPList::readIPMelangeList(const char *filename) |
| 244 |
{ |
| 245 |
// load in the list file |
| 246 |
std::ifstream input(filename); |
| 247 |
if (!input) { |
| 248 |
E2LOGGER_error("Error reading file (does it exist?): ", filename); |
| 249 |
return false; |
| 250 |
} |
| 251 |
DEBUG_debug("reading: ", filename); |
| 252 |
if (ifsreadIPMelangeList(&input, false, NULL)) { |
| 253 |
input.close(); |
| 254 |
return true; |
| 255 |
} |
| 256 |
input.close(); |
| 257 |
return false; |
| 258 |
} |