summaryrefslogtreecommitdiff
path: root/includes/utils.php
diff options
context:
space:
mode:
authorGuillaume Mazoyer <respawneral@gmail.com>2014-09-03 11:19:56 +0200
committerGuillaume Mazoyer <respawneral@gmail.com>2014-09-03 11:19:56 +0200
commit0512ffb7f70f85223a71ffd539b74459793178af (patch)
tree9c97d86b9b3f4ba908f2f217c70a777dbb03b5a5 /includes/utils.php
parentd9fcd2d238eaf8ab61de5c8a1ef5ad6fbd5d1778 (diff)
Add config to disallow the use of private and reserved IP ranges.
Diffstat (limited to 'includes/utils.php')
-rw-r--r--includes/utils.php88
1 files changed, 88 insertions, 0 deletions
diff --git a/includes/utils.php b/includes/utils.php
index 1319690..01b034c 100644
--- a/includes/utils.php
+++ b/includes/utils.php
@@ -22,6 +22,40 @@
require_once 'config.php';
/**
+ * Test if a given parameter is a private IPv4 or IPv6.
+ *
+ * @param string $ip the parameter to test.
+ * @return boolean true if the parameter is a private IP address, false
+ * otherwise.
+ */
+function match_private_ip_range($ip) {
+ if (empty($ip)) {
+ return false;
+ }
+
+ $is_private = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE);
+
+ return (!$is_private ? true : false);
+}
+
+/**
+ * Test if a given parameter is a reserved IPv4.
+ *
+ * @param string $ip the parameter to test.
+ * @return boolean true if the parameter is a reserved IPv4 address, false
+ * otherwise.
+ */
+function match_reserved_ip_range($ip) {
+ if (empty($ip)) {
+ return false;
+ }
+
+ $is_reserved = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE);
+
+ return (!$is_reserved ? true : false);
+}
+
+/**
* Test if a given parameter is an IPv4 or not.
*
* @param string $ip the parameter to test.
@@ -33,12 +67,38 @@ require_once 'config.php';
* otherwise.
*/
function match_ipv4($ip, $ip_only = true) {
+ global $config;
+
+ if (empty($ip)) {
+ return false;
+ }
+
if (strrpos($ip, '/') && !$ip_only) {
$ip_and_mask = explode('/', $ip, 2);
+ if (!$config['misc']['allow_private_ip'] &&
+ match_private_ip_range($ip_and_mask[0])) {
+ return false;
+ }
+
+ if (!$config['misc']['allow_reserved_ip'] &&
+ match_reserved_ip_range($ip_and_mask[0])) {
+ return false;
+ }
+
return filter_var($ip_and_mask[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) &&
filter_var($ip_and_mask[1], FILTER_VALIDATE_INT);
} else {
+ if (!$config['misc']['allow_private_ip'] &&
+ match_private_ip_range($ip)) {
+ return false;
+ }
+
+ if (!$config['misc']['allow_reserved_ip'] &&
+ match_reserved_ip_range($ip)) {
+ return false;
+ }
+
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}
}
@@ -55,12 +115,28 @@ function match_ipv4($ip, $ip_only = true) {
* otherwise.
*/
function match_ipv6($ip, $ip_only = true) {
+ global $config;
+
+ if (empty($ip)) {
+ return false;
+ }
+
if (strrpos($ip, '/') && !$ip_only) {
$ip_and_mask = explode('/', $ip, 2);
+ if (!$config['misc']['allow_private_ip'] &&
+ match_private_ip_range($ip_and_mask[0])) {
+ return false;
+ }
+
return filter_var($ip_and_mask[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) &&
filter_var($ip_and_mask[1], FILTER_VALIDATE_INT);
} else {
+ if (!$config['misc']['allow_private_ip'] &&
+ match_private_ip_range($ip)) {
+ return false;
+ }
+
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
}
}
@@ -74,6 +150,10 @@ function match_ipv6($ip, $ip_only = true) {
function match_fqdn($fqdn) {
$regex = '/(?=^.{4,255}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)/';
+ if (empty($fqdn)) {
+ return false;
+ }
+
if ((preg_match($regex, $fqdn) === false) ||
(preg_match($regex, $fqdn) === 0)) {
return false;
@@ -101,6 +181,10 @@ function match_as($as) {
'options' => array('min_range' => 4200000000, 'max_range' => 4294967294)
);
+ if (empty($as)) {
+ return false;
+ }
+
if (!filter_var($as, FILTER_VALIDATE_INT, $options_wide_range)) {
return false;
}
@@ -119,6 +203,10 @@ function match_as($as) {
}
function match_aspath_regex($aspath_regex) {
+ if (empty($aspath_regex)) {
+ return false;
+ }
+
// TODO: validate a regex with a regex?
return true;
}