mirror of
				https://github.com/billz/raspap-webgui.git
				synced 2025-03-01 10:31:47 +00:00 
			
		
		
		
	Merge branch 'feature/dhcp-eth0' of https://github.com/billz/raspap-webgui into feature/dhcp-eth0
This commit is contained in:
		| @@ -21,7 +21,7 @@ if (isset($interface)) { | ||||
|     $dhcpdata['dhcpHost'] = is_array($dhcpHost) ? $dhcpHost : [ $dhcpHost ]; | ||||
|     $upstreamServers = is_array($conf['server']) ? $conf['server'] : [ $conf['server'] ]; | ||||
|     $dhcpdata['upstreamServers'] = array_filter($upstreamServers); | ||||
|  | ||||
|     $dhcpdata['upstreamServersEnabled'] = empty($conf['server']) ? false: true; | ||||
|     preg_match('/([0-9]*)([a-z])/i', $dhcpdata['leaseTime'], $arrRangeLeaseTime); | ||||
|     $dhcpdata['leaseTime'] = $arrRangeLeaseTime[1]; | ||||
|     $dhcpdata['leaseTimeInterval'] = $arrRangeLeaseTime[2]; | ||||
|   | ||||
| @@ -255,6 +255,8 @@ function loadInterfaceDHCPSelect() { | ||||
|         $('#txtdns1').val(jsonData.DNS1); | ||||
|         $('#txtdns2').val(jsonData.DNS2); | ||||
|         $('#cbxrangeleasetimeunits').val(jsonData.leaseTimeInterval); | ||||
|         $('#no-resolv')[0].checked = jsonData.upstreamServersEnabled; | ||||
|         $('#cbxdhcpupstreamserver').val(jsonData.upstreamServers[0]); | ||||
|     }); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -8,142 +8,12 @@ require_once 'config.php'; | ||||
|  */ | ||||
| function DisplayDHCPConfig() | ||||
| { | ||||
|  | ||||
|     $status = new StatusMessages(); | ||||
|     if (!RASPI_MONITOR_ENABLED) { | ||||
|         if (isset($_POST['savedhcpdsettings'])) { | ||||
|             $errors = ''; | ||||
|             define('IFNAMSIZ', 16); | ||||
|             if (!preg_match('/^[a-zA-Z0-9]+$/', $_POST['interface'])  | ||||
|                 || strlen($_POST['interface']) >= IFNAMSIZ | ||||
|             ) { | ||||
|                 $errors .= _('Invalid interface name.').'<br />'.PHP_EOL; | ||||
|             } | ||||
|  | ||||
|             if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeStart'])  | ||||
|                 && !empty($_POST['RangeStart']) | ||||
|             ) {  // allow ''/null ? | ||||
|                 $errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL; | ||||
|             } | ||||
|  | ||||
|             if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeEnd'])  | ||||
|                 && !empty($_POST['RangeEnd']) | ||||
|             ) {  // allow ''/null ? | ||||
|                 $errors .= _('Invalid DHCP range end.').'<br />'.PHP_EOL; | ||||
|             } | ||||
|  | ||||
|             if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'infinite') { | ||||
|                 $errors .= _('Invalid DHCP lease time, not a number.').'<br />'.PHP_EOL; | ||||
|             } | ||||
|  | ||||
|             if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) { | ||||
|                 $errors .= _('Unknown DHCP lease time unit.').'<br />'.PHP_EOL; | ||||
|             } | ||||
|  | ||||
|             $return = 1; | ||||
|             if (empty($errors)) { | ||||
|                 $config = 'interface='.$_POST['interface'].PHP_EOL. | ||||
|                     'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd']. | ||||
|                     ',255.255.255.0,'; | ||||
|                 if ($_POST['RangeLeaseTimeUnits'] !== 'infinite') { | ||||
|                     $config .= $_POST['RangeLeaseTime']; | ||||
|                 } | ||||
|  | ||||
|                 $config .= $_POST['RangeLeaseTimeUnits'].PHP_EOL; | ||||
|  | ||||
|                 for ($i=0; $i < count($_POST["static_leases"]["mac"]); $i++) { | ||||
|                     $mac = trim($_POST["static_leases"]["mac"][$i]); | ||||
|                     $ip  = trim($_POST["static_leases"]["ip"][$i]); | ||||
|                     if ($mac != "" && $ip != "") { | ||||
|                         $config .= "dhcp-host=$mac,$ip".PHP_EOL; | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 if ($_POST['no-resolv'] == "1") { | ||||
|                     $config .= "no-resolv".PHP_EOL; | ||||
|                 } | ||||
|                 foreach ($_POST['server'] as $server) { | ||||
|                     $config .= "server=$server".PHP_EOL; | ||||
|                 } | ||||
|                 if ($_POST['log-dhcp'] == "1") { | ||||
|                   $config .= "log-dhcp".PHP_EOL; | ||||
|                 } | ||||
|                 if ($_POST['log-queries'] == "1") { | ||||
|                   $config .= "log-queries".PHP_EOL; | ||||
|                 } | ||||
|                 if ($_POST['DNS1']) { | ||||
|                     $config .= "dhcp-option=6," . $_POST['DNS1']; | ||||
|                     if ($_POST['DNS2']) { | ||||
|                         $config .= ','.$_POST['DNS2']; | ||||
|                     } | ||||
|                     $config .= PHP_EOL; | ||||
|                 } | ||||
|  | ||||
|                 $config .= "log-facility=/tmp/dnsmasq.log".PHP_EOL; | ||||
|                 $config .= "conf-dir=/etc/dnsmasq.d".PHP_EOL; | ||||
|                 file_put_contents("/tmp/dnsmasqdata", $config); | ||||
|  | ||||
|                 $iface = $_POST['interface']; | ||||
|                 // handle DHCP for selected interface option | ||||
|                 if ($_POST['dhcp-iface'] == "1") { | ||||
|                     $net_cfg = RASPI_CONFIG_NETWORKING.'/'.$iface.'.ini'; | ||||
|                     if (!file_exists($net_cfg) || filesize($net_cfg) ==0 ) { | ||||
|                         $status->addMessage('Static IP address for '.$iface.' not found.', 'danger'); | ||||
|                         $status->addMessage('Configure this interface in Networking > '.$iface.'.', 'danger'); | ||||
|                         $return = 1; | ||||
|                     } else { | ||||
|                         $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); | ||||
|                         if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) { | ||||
|                             // set dhcp values from ini | ||||
|                             $iface_cfg = parse_ini_file($net_cfg, false, INI_SCANNER_RAW); | ||||
|                             $ip_address = $iface_cfg['ip_address']; | ||||
|                             $domain_name_server = ($iface_cfg['domain_name_server'] =='') ? '1.1.1.1 8.8.8.8' : $iface_cfg['domain_name_server']; | ||||
|  | ||||
|                             // append interface config to dhcpcd.conf | ||||
|                             $cfg = $dhcp_conf; | ||||
|                             $cfg[] = '# RaspAP '.$_POST['interface'].' configuration'; | ||||
|                             $cfg[] = 'interface '.$_POST['interface']; | ||||
|                             $cfg[] = 'static ip_address='.$ip_address; | ||||
|                             $cfg[] = 'static domain_name_server='.$domain_name_server; | ||||
|                             $cfg[] = PHP_EOL; | ||||
|                             $cfg = join(PHP_EOL, $cfg); | ||||
|                             $dhcp_cfg .= $cfg; | ||||
|                             file_put_contents("/tmp/dhcpddata", $dhcp_cfg); | ||||
|                             system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return); | ||||
|                             $status->addMessage('DHCP configuration for '.$iface.' added.', 'success'); | ||||
|                             system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return); | ||||
|                             $status->addMessage('Dnsmasq configuration for '.$iface.' added.', 'success'); | ||||
|                         } else { | ||||
|                             $status->addMessage('DHCP for '.$iface.' already enabled.', 'danger'); | ||||
|                         } | ||||
|                     } | ||||
|                 } elseif (($_POST['dhcp-iface'] == "0") && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) { | ||||
|                     // remove dhcp conf for selected interface | ||||
|                     $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); | ||||
|                     // todo: improve by removing extra blank lines | ||||
|                     $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'.*\n(.*\n){3}/m', '', $dhcp_cfg); | ||||
|                     file_put_contents("/tmp/dhcpddata", $dhcp_cfg); | ||||
|                     system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return); | ||||
|                     $status->addMessage('DHCP configuration for '.$iface.'  removed.', 'success'); | ||||
|                     // remove dnsmasq eth0 conf | ||||
|                     system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return); | ||||
|                     $status->addMessage('Dnsmasq configuration for '.$iface.' removed.', 'success'); | ||||
|                 } else { | ||||
|                     system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_CONFIG, $return); | ||||
|                 } | ||||
|  | ||||
|             } else { | ||||
|                 $status->addMessage($errors, 'danger'); | ||||
|             } | ||||
|  | ||||
|             if ($return == 0) { | ||||
|                 $status->addMessage('Dnsmasq configuration updated successfully.', 'success'); | ||||
|             } else { | ||||
|                 $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); | ||||
|             } | ||||
|             SaveDHCPConfig($status); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     exec('pidof dnsmasq | wc -l', $dnsmasq); | ||||
|     $dnsmasq_state = ($dnsmasq[0] > 0); | ||||
|  | ||||
| @@ -191,3 +61,143 @@ function DisplayDHCPConfig() | ||||
|         ) | ||||
|     ); | ||||
| } | ||||
|  | ||||
| function SaveDHCPConfig($status) | ||||
| { | ||||
|     $iface = $_POST['interface']; | ||||
|     $return = 1; | ||||
|  | ||||
|     if (($_POST['dhcp-iface'] == "1")) { | ||||
|        $errors = ValidateDHCPInput(); | ||||
|         if (empty($errors)) { | ||||
|             $config = 'interface='.$iface.PHP_EOL. | ||||
|                 'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd']. | ||||
|                 ',255.255.255.0,'; | ||||
|             if ($_POST['RangeLeaseTimeUnits'] !== 'infinite') { | ||||
|                 $config .= $_POST['RangeLeaseTime']; | ||||
|             } | ||||
|             $config .= $_POST['RangeLeaseTimeUnits'].PHP_EOL; | ||||
|             for ($i=0; $i < count($_POST["static_leases"]["mac"]); $i++) { | ||||
|                 $mac = trim($_POST["static_leases"]["mac"][$i]); | ||||
|                 $ip  = trim($_POST["static_leases"]["ip"][$i]); | ||||
|                 if ($mac != "" && $ip != "") { | ||||
|                     $config .= "dhcp-host=$mac,$ip".PHP_EOL; | ||||
|                 } | ||||
|             } | ||||
|             if ($_POST['no-resolv'] == "1") { | ||||
|                 $config .= "no-resolv".PHP_EOL; | ||||
|             } | ||||
|             foreach ($_POST['server'] as $server) { | ||||
|                 $config .= "server=$server".PHP_EOL; | ||||
|             } | ||||
|             if ($_POST['log-dhcp'] == "1") { | ||||
|               $config .= "log-dhcp".PHP_EOL; | ||||
|             } | ||||
|             if ($_POST['log-queries'] == "1") { | ||||
|               $config .= "log-queries".PHP_EOL; | ||||
|             } | ||||
|             if ($_POST['DNS1']) { | ||||
|                 $config .= "dhcp-option=6," . $_POST['DNS1']; | ||||
|                 if ($_POST['DNS2']) { | ||||
|                     $config .= ','.$_POST['DNS2']; | ||||
|                 } | ||||
|                 $config .= PHP_EOL; | ||||
|             } | ||||
|             $config .= "log-facility=/tmp/dnsmasq.log".PHP_EOL; | ||||
|             $config .= "conf-dir=/etc/dnsmasq.d".PHP_EOL; | ||||
|             file_put_contents("/tmp/dnsmasqdata", $config); | ||||
|             $msg = file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf') ? 'updated' : 'added'; | ||||
|             system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return); | ||||
|         } else { | ||||
|             $status->addMessage($errors, 'danger'); | ||||
|         } | ||||
|  | ||||
|         if ($return == 0) { | ||||
|             $status->addMessage('Dnsmasq configuration for '.$iface.' '.$msg.'.', 'success'); | ||||
|         } else { | ||||
|             $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|         $net_cfg = RASPI_CONFIG_NETWORKING.'/'.$iface.'.ini'; | ||||
|         if (!file_exists($net_cfg) || filesize($net_cfg) ==0 ) { | ||||
|             $status->addMessage('Static IP address for '.$iface.' not found.', 'danger'); | ||||
|             $status->addMessage('Configure this interface in Networking > '.$iface.'.', 'danger'); | ||||
|             $return = 1; | ||||
|         } else { | ||||
|             $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); | ||||
|             if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) { | ||||
|                 // set dhcp values from ini | ||||
|                 $iface_cfg = parse_ini_file($net_cfg, false, INI_SCANNER_RAW); | ||||
|                 $ip_address = $iface_cfg['ip_address']; | ||||
|                 $domain_name_server = ($iface_cfg['domain_name_server'] =='') ? '1.1.1.1 8.8.8.8' : $iface_cfg['domain_name_server']; | ||||
|  | ||||
|                 // append interface config to dhcpcd.conf | ||||
|                 $cfg = $dhcp_conf; | ||||
|                 $cfg[] = '# RaspAP '.$_POST['interface'].' configuration'; | ||||
|                 $cfg[] = 'interface '.$_POST['interface']; | ||||
|                 $cfg[] = 'static ip_address='.$ip_address; | ||||
|                 $cfg[] = 'static domain_name_server='.$domain_name_server; | ||||
|                 $cfg[] = PHP_EOL; | ||||
|                 $cfg = join(PHP_EOL, $cfg); | ||||
|                 $dhcp_cfg .= $cfg; | ||||
|                 file_put_contents("/tmp/dhcpddata", $dhcp_cfg); | ||||
|                 system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return); | ||||
|                 $status->addMessage('DHCP configuration for '.$iface.' added.', 'success'); | ||||
|             } else { | ||||
|                 $status->addMessage('DHCP for '.$iface.' already enabled.', 'success'); | ||||
|             } | ||||
|         } | ||||
|     // process disable dhcp option | ||||
|     } elseif (($_POST['dhcp-iface'] == "0") && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) { | ||||
|         // remove dhcp conf for selected interface | ||||
|         $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); | ||||
|         // todo: improve by removing extra blank lines | ||||
|         $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'.*\n(.*\n){3}/m', '', $dhcp_cfg); | ||||
|         file_put_contents("/tmp/dhcpddata", $dhcp_cfg); | ||||
|         system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return); | ||||
|         $status->addMessage('DHCP configuration for '.$iface.'  removed.', 'success'); | ||||
|         // remove dnsmasq eth0 conf | ||||
|         system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return); | ||||
|         $status->addMessage('Dnsmasq configuration for '.$iface.' removed.', 'success'); | ||||
|     } else { | ||||
|         system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return); | ||||
|     } | ||||
|  | ||||
|     if ($return == 0) { | ||||
|         $status->addMessage('Dnsmasq configuration updated successfully.', 'success'); | ||||
|     } else { | ||||
|         $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); | ||||
|         return false; | ||||
|     } | ||||
|     return true; | ||||
| } | ||||
|  | ||||
| function ValidateDHCPInput() | ||||
| { | ||||
|     define('IFNAMSIZ', 16); | ||||
|     $iface = $_POST['interface']; | ||||
|     if (!preg_match('/^[a-zA-Z0-9]+$/', $iface) | ||||
|         || strlen($iface) >= IFNAMSIZ | ||||
|     ) { | ||||
|         $errors .= _('Invalid interface name.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeStart']) | ||||
|         && !empty($_POST['RangeStart']) | ||||
|     ) {  // allow ''/null ? | ||||
|         $errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeEnd']) | ||||
|         && !empty($_POST['RangeEnd']) | ||||
|     ) {  // allow ''/null ? | ||||
|         $errors .= _('Invalid DHCP range end.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'infinite') { | ||||
|         $errors .= _('Invalid DHCP lease time, not a number.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) { | ||||
|         $errors .= _('Unknown DHCP lease time unit.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     return $errors; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -18,7 +18,7 @@ set -o errtrace | ||||
| readonly raspap_dir="/etc/raspap" | ||||
| readonly raspap_user="www-data" | ||||
| readonly raspap_sudoers="/etc/sudoers.d/090_raspap" | ||||
| readonly raspap_dnsmasq="/etc/dnsmasq.d/090_raspap.conf" | ||||
| readonly raspap_dnsmasq="/etc/dnsmasq.d/090_wlan0.conf" | ||||
| readonly raspap_adblock="/etc/dnsmasq.d/090_adblock.conf" | ||||
| readonly raspap_sysctl="/etc/sysctl.d/90_raspap.conf" | ||||
| readonly rulesv4="/etc/iptables/rules.v4" | ||||
|   | ||||
| @@ -20,7 +20,6 @@ www-data ALL=(ALL) NOPASSWD:/bin/systemctl stop openvpn-client@client | ||||
| www-data ALL=(ALL) NOPASSWD:/bin/systemctl disable openvpn-client@client | ||||
| www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/ovpnclient.ovpn /etc/openvpn/client/client.conf | ||||
| www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/authdata /etc/openvpn/client/login.conf | ||||
| www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/dnsmasqdata /etc/dnsmasq.d/090_raspap.conf | ||||
| www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/dnsmasqdata /etc/dnsmasq.d/090_*.conf | ||||
| www-data ALL=(ALL) NOPASSWD:/bin/rm /etc/dnsmasq.d/090_*.conf | ||||
| www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/dhcpddata /etc/dhcpcd.conf | ||||
|   | ||||
| @@ -41,7 +41,7 @@ | ||||
|             <code class="text-muted"><?php echo htmlspecialchars("[/[<domain>]/[domain/]][<ipaddr>[#<port>][@<source-ip>|<interface>[#<port>]]"); ?></code> | ||||
|           </small> | ||||
|         </p> | ||||
|         <select class="custom-select custom-select-sm js-field-preset" data-field-preset-target="#add-dhcp-upstream-server-field"> | ||||
|         <select class="custom-select custom-select-sm js-field-preset" id="cbxdhcpupstreamserver" data-field-preset-target="#add-dhcp-upstream-server-field"> | ||||
|           <option value=""><?php echo _("Choose a hosted server") ?></option> | ||||
|           <option disabled="disabled"></option> | ||||
|           <?php echo optionsForSelect(dnsServers()) ?> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user