Files
ESPEasy/src/WebServer_AccessControl.ino
T
Gijs Noorlander 3ef39ca6c1 [Webserver] Fix ambiguous function definitions for selectors
The form selectors were quite ambiguous, which could lead to build issues
2020-04-09 15:52:40 +02:00

121 lines
3.0 KiB
Arduino

// ********************************************************************************
// Allowed IP range check
// ********************************************************************************
#define ALL_ALLOWED 0
#define LOCAL_SUBNET_ALLOWED 1
#define ONLY_IP_RANGE_ALLOWED 2
boolean ipLessEqual(const IPAddress& ip, const IPAddress& high)
{
for (byte i = 0; i < 4; ++i) {
if (ip[i] > high[i]) { return false; }
}
return true;
}
boolean ipInRange(const IPAddress& ip, const IPAddress& low, const IPAddress& high)
{
return ipLessEqual(low, ip) && ipLessEqual(ip, high);
}
String describeAllowedIPrange() {
String reply;
switch (SecuritySettings.IPblockLevel) {
case ALL_ALLOWED:
reply += F("All Allowed");
break;
default:
{
IPAddress low, high;
getIPallowedRange(low, high);
reply += formatIP(low);
reply += F(" - ");
reply += formatIP(high);
}
}
return reply;
}
bool getIPallowedRange(IPAddress& low, IPAddress& high)
{
switch (SecuritySettings.IPblockLevel) {
case LOCAL_SUBNET_ALLOWED:
if (WifiIsAP(WiFi.getMode())) {
// WiFi is active as accesspoint, do not check.
return false;
}
return getSubnetRange(low, high);
case ONLY_IP_RANGE_ALLOWED:
low = SecuritySettings.AllowedIPrangeLow;
high = SecuritySettings.AllowedIPrangeHigh;
break;
default:
low = IPAddress(0, 0, 0, 0);
high = IPAddress(255, 255, 255, 255);
return false;
}
return true;
}
bool clientIPinSubnet() {
IPAddress low, high;
if (!getSubnetRange(low, high)) {
// Could not determine subnet.
return false;
}
WiFiClient client(web_server.client());
return ipInRange(client.remoteIP(), low, high);
}
boolean clientIPallowed()
{
// TD-er Must implement "safe time after boot"
IPAddress low, high;
if (!getIPallowedRange(low, high))
{
// No subnet range determined, cannot filter on IP
return true;
}
WiFiClient client(web_server.client());
if (ipInRange(client.remoteIP(), low, high)) {
return true;
}
if (WifiIsAP(WiFi.getMode())) {
// @TD-er Fixme: Should match subnet of SoftAP.
return true;
}
String response = F("IP blocked: ");
response += formatIP(client.remoteIP());
web_server.send(403, F("text/html"), response);
if (loglevelActiveFor(LOG_LEVEL_ERROR)) {
response += F(" Allowed: ");
response += formatIP(low);
response += F(" - ");
response += formatIP(high);
addLog(LOG_LEVEL_ERROR, response);
}
return false;
}
void clearAccessBlock()
{
SecuritySettings.IPblockLevel = ALL_ALLOWED;
}
// ********************************************************************************
// Add a IP Access Control select dropdown list
// ********************************************************************************
void addIPaccessControlSelect(const String& name, int choice)
{
String options[3] = { F("Allow All"), F("Allow Local Subnet"), F("Allow IP range") };
addSelector(name, 3, options, NULL, NULL, choice);
}