diff --git a/ajax/networking/get_netcfg.php b/ajax/networking/get_netcfg.php index cd36a8a3..2dd330bb 100644 --- a/ajax/networking/get_netcfg.php +++ b/ajax/networking/get_netcfg.php @@ -43,10 +43,10 @@ if (isset($interface)) { preg_match('/metric\s(\d*)/', $matched[0], $metric); preg_match('/static\sip_address=(.*)/', $matched[0], $static_ip); preg_match('/static\srouters=(.*)/', $matched[0], $static_routers); - preg_match('/static\sdomain_name_servers=(.*)/', $matched[0], $static_dns); + preg_match('/static\sdomain_name_server=(.*)/', $matched[0], $static_dns); preg_match('/fallback\sstatic_'.$interface.'/', $matched[0], $fallback); $dhcpdata['Metric'] = $metric[1]; - $dhcpdata['StaticIP'] = substr($static_ip[1], 0, strpos($static_ip[1],'/')); + $dhcpdata['StaticIP'] = strpos($static_ip[1],'/') ? substr($static_ip[1], 0, strpos($static_ip[1],'/')) : $static_ip[1]; $dhcpdata['SubnetMask'] = cidr2mask($static_ip[1]); $dhcpdata['StaticRouters'] = $static_routers[1]; $dhcpdata['StaticDNS'] = $static_dns[1]; diff --git a/config/090_wlan0.conf b/config/090_wlan0.conf new file mode 100644 index 00000000..56234784 --- /dev/null +++ b/config/090_wlan0.conf @@ -0,0 +1,6 @@ +# RaspAP wlan0 configuration for wired (ethernet) AP mode +interface=wlan0 +domain-needed +dhcp-range=10.3.141.50,10.3.141.255,255.255.255.0,12h +dhcp-option=6,1.1.1.1,8.8.8.8 + diff --git a/config/config.php b/config/config.php index 04fb56bc..31bb9376 100755 --- a/config/config.php +++ b/config/config.php @@ -2,7 +2,7 @@ define('RASPI_BRAND_TEXT', 'RaspAP'); define('RASPI_CONFIG', '/etc/raspap'); -define('RASPI_CONFIG_NETWORKING', RASPI_CONFIG.'/networking'); +define('RASPI_CONFIG_NETWORK', RASPI_CONFIG.'/networking/defaults.json'); define('RASPI_ADMIN_DETAILS', RASPI_CONFIG.'/raspap.auth'); define('RASPI_WIFI_AP_INTERFACE', 'wlan0'); define('RASPI_CACHE_PATH', sys_get_temp_dir() . '/raspap'); diff --git a/config/defaults.json b/config/defaults.json new file mode 100644 index 00000000..24f2138e --- /dev/null +++ b/config/defaults.json @@ -0,0 +1,22 @@ +{ + "dhcp": { + "wlan0": { + "static ip_address": [ "10.3.141.1/24" ], + "static routers": [ "10.3.141.1" ], + "static domain_name_server": [ "1.1.1.1 8.8.8.8" ] + }, + "uap0": { + "static ip_address": [ "192.168.50.1/24" ], + "static routers": [ "192.168.50.1" ], + "static domain_name_server": [ "1.1.1.1 8.8.8.8" ] + } + }, + "dnsmasq": { + "wlan0": { + "dhcp-range": [ "10.3.141.50,10.3.141.255,255.255.255.0,12h" ] + }, + "uap0": { + "dhcp-range": [ "192.168.50.50,192.168.50.150,12h" ] + } + } +} diff --git a/includes/defaults.php b/includes/defaults.php index ce1b4731..88b52fe9 100755 --- a/includes/defaults.php +++ b/includes/defaults.php @@ -7,7 +7,7 @@ if (!defined('RASPI_CONFIG')) { $defaults = [ 'RASPI_BRAND_TEXT' => 'RaspAP', 'RASPI_VERSION' => '2.5.1', - 'RASPI_CONFIG_NETWORKING' => RASPI_CONFIG.'/networking', + 'RASPI_CONFIG_NETWORK' => RASPI_CONFIG.'/networking/defaults.json', 'RASPI_ADMIN_DETAILS' => RASPI_CONFIG.'/raspap.auth', 'RASPI_WIFI_AP_INTERFACE' => 'wlan0', 'RASPI_CACHE_PATH' => sys_get_temp_dir() . '/raspap', diff --git a/includes/dhcp.php b/includes/dhcp.php index fb2e4709..45ea156f 100755 --- a/includes/dhcp.php +++ b/includes/dhcp.php @@ -11,7 +11,7 @@ function DisplayDHCPConfig() $status = new StatusMessages(); if (!RASPI_MONITOR_ENABLED) { if (isset($_POST['savedhcpdsettings'])) { - SaveDHCPConfig($status); + saveDHCPConfig($status); } } exec('pidof dnsmasq | wc -l', $dnsmasq); @@ -63,19 +63,25 @@ function DisplayDHCPConfig() ); } -function SaveDHCPConfig($status) +/** + * Saves a DHCP configuration + * + * @return object $status + */ +function saveDHCPConfig($status) { $iface = $_POST['interface']; $return = 1; // handle disable dhcp option if (!isset($_POST['dhcp-iface']) && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) { - // remove dhcp conf for selected interface - $return = RemoveDHCPConfig($iface,$status); + // remove dhcp + dnsmasq configs for selected interface + $return = removeDHCPConfig($iface,$status); + $return = removeDnsmasqConfig($iface,$status); } else { - $errors = ValidateDHCPInput(); + $errors = validateDHCPInput(); if (empty($errors)) { - $return = UpdateDHCPConfig($iface,$status); + $return = updateDHCPConfig($iface,$status); } else { $status->addMessage($errors, 'danger'); } @@ -85,9 +91,8 @@ function SaveDHCPConfig($status) } if (($_POST['dhcp-iface'] == "1")) { - $return = UpdateDnsmasqConfig($iface,$status); + $return = updateDnsmasqConfig($iface,$status); } - if ($return == 0) { $status->addMessage('Dnsmasq configuration updated successfully.', 'success'); } else { @@ -98,7 +103,12 @@ function SaveDHCPConfig($status) } } -function ValidateDHCPInput() +/** + * Validates DHCP user input from the $_POST object + * + * @return string $errors + */ +function validateDHCPInput() { define('IFNAMSIZ', 16); $iface = $_POST['interface']; @@ -138,9 +148,17 @@ function ValidateDHCPInput() return $errors; } -function UpdateDnsmasqConfig($iface,$status) +/** + * Updates a dnsmasq configuration + * + * @param string $iface + * @param object $status + * @return boolean $result + */ +function updateDnsmasqConfig($iface,$status) { - $config = 'interface='.$iface.PHP_EOL. + $config = '# RaspAP '.$iface.' configuration'.PHP_EOL; + $config .= 'interface='.$iface.PHP_EOL. 'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd']. ',255.255.255.0,'; if ($_POST['RangeLeaseTimeUnits'] !== 'infinite') { @@ -173,11 +191,6 @@ function UpdateDnsmasqConfig($iface,$status) } $config .= PHP_EOL; } - // enable these settings on the default interface - if ($iface == "wlan0") { - $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', $result); @@ -187,12 +200,20 @@ function UpdateDnsmasqConfig($iface,$status) return $result; } -function UpdateDHCPConfig($iface,$status) +/** + * Updates a dhcp configuration + * + * @param string $iface + * @param object $status + * @return boolean $result + */ +function updateDHCPConfig($iface,$status) { $cfg[] = '# RaspAP '.$iface.' configuration'; $cfg[] = 'interface '.$iface; if (isset($_POST['StaticIP'])) { - $cfg[] = 'static ip_address='.$_POST['StaticIP'].'/'.mask2cidr($_POST['SubnetMask']); + $mask = ($_POST['SubnetMask'] !== '' && $_POST['SubnetMask'] !== '0.0.0.0') ? '/'.mask2cidr($_POST['SubnetMask']) : null; + $cfg[] = 'static ip_address='.$_POST['StaticIP'].$mask; } if (isset($_POST['DefaultGateway'])) { $cfg[] = 'static routers='.$_POST['DefaultGateway']; @@ -208,10 +229,6 @@ function UpdateDHCPConfig($iface,$status) $cfg[] = 'fallback static_'.$iface; } $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); - if (!preg_match('/^$\s*\z/m', $dhcp_cfg) && !preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) { - echo '===== no ending newline found ====
'; - } - if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) { $cfg[] = PHP_EOL; $cfg = join(PHP_EOL, $cfg); @@ -228,26 +245,3 @@ function UpdateDHCPConfig($iface,$status) return $result; } -function RemoveDHCPConfig($iface,$status) -{ - $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); - $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)([\s]+)/ms', '', $dhcp_cfg, 1); - file_put_contents("/tmp/dhcpddata", $dhcp_cfg); - system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); - if ($result == 0) { - $status->addMessage('DHCP configuration for '.$iface.' removed.', 'success'); - } else { - $status->addMessage('Failed to remove DHCP configuration for '.$iface.'.', 'danger'); - return $result; - } - // remove dnsmasq conf - system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $result); - if ($result == 0) { - $status->addMessage('Dnsmasq configuration for '.$iface.' removed.', 'success'); - } else { - $status->addMessage('Failed to remove dnsmasq configuration for '.$iface.'.', 'danger'); - } - return $result; -} - - diff --git a/includes/functions.php b/includes/functions.php index 9ed854e7..73f06600 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -19,7 +19,7 @@ function mask2cidr($mask) * * @param string $cidr * @return string -*/ + */ function cidr2mask($cidr) { $ta = substr ($cidr, strpos ($cidr, '/') + 1) * 1; @@ -29,6 +29,116 @@ function cidr2mask($cidr) return join ('.', $netmask); } +/** + * Returns a dhcp default config header + * + * @return array $config + */ +function defaultHeader() +{ + $config = [ '# RaspAP default configuration' ]; + $config[] = 'hostname'; + $config[] = 'clientid'; + $config[] = 'persistent'; + $config[] = 'option rapid_commit'; + $config[] = 'option domain_name_servers, domain_name, domain_search, host_name'; + $config[] = 'option classless_static_routes'; + $config[] = 'option ntp_servers'; + $config[] = 'require dhcp_server_identifier'; + $config[] = 'slaac private'; + $config[] = 'nohook lookup-hostname'; + return $config; +} + +/** + * Removes a dhcp configuration block for the specified interface + * + * @param string $iface + * @param object $status + * @return boolean $result + */ +function removeDHCPConfig($iface,$status) +{ + $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); + $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)([\s]+)/ms', '', $dhcp_cfg, 1); + file_put_contents("/tmp/dhcpddata", $dhcp_cfg); + system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); + if ($result == 0) { + $status->addMessage('DHCP configuration for '.$iface.' removed.', 'success'); + } else { + $status->addMessage('Failed to remove DHCP configuration for '.$iface.'.', 'danger'); + return $result; + } +} + +/** + * Removes a dhcp configuration block for the specified interface + * + * @param string $dhcp_cfg + * @param string $iface + * @return string $dhcp_cfg + */ +function removeDHCPIface($dhcp_cfg,$iface) +{ + $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)([\s]+)/ms', '', $dhcp_cfg, 1); + return $dhcp_cfg; +} + +/** + * Removes a dnsmasq configuration block for the specified interface + * + * @param string $iface + * @param object $status + * @return boolean $result + */ +function removeDnsmasqConfig($iface,$status) +{ + system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $result); + if ($result == 0) { + $status->addMessage('Dnsmasq configuration for '.$iface.' removed.', 'success'); + } else { + $status->addMessage('Failed to remove dnsmasq configuration for '.$iface.'.', 'danger'); + } + return $result; +} + +/** + * Scans dnsmasq configuration dir for the specified interface + * Non-matching configs are removed, optional adblock.conf is protected + * + * @param string $dir_conf + * @param string $interface + * @param object $status + */ +function scanConfigDir($dir_conf,$interface,$status) +{ + $syscnf = preg_grep('~\.(conf)$~', scandir($dir_conf)); + foreach ($syscnf as $cnf) { + if ($cnf !== '090_adblock.conf' && !preg_match('/.*_'.$interface.'.conf/', $cnf)) { + system('sudo rm /etc/dnsmasq.d/'.$cnf, $result); + } + } + return $status; +} + +/** + * Returns a default (fallback) value for the selected service, interface & setting + * from /etc/raspap/networking/defaults.json + * + * @param string $svc + * @param string $iface + * @return string $value + */ +function getDefaultNetValue($svc,$iface,$key) +{ + $json = json_decode(file_get_contents(RASPI_CONFIG_NETWORK), true); + if ($json === null) { + return false; + } else { + return $json[$svc][$iface][$key][0]; + } +} + /* Functions to write ini files */ function write_php_ini($array, $file) @@ -234,6 +344,19 @@ function ParseConfig($arrConfig) return $config; } +/** + * Fetches DHCP configuration for an interface, returned as JSON data + * + * @param string $interface + * @return json $jsonData + */ +function getNetConfig($interface) +{ + $URI = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'] .'/ajax/networking/get_netcfg.php?iface='.$interface; + $jsonData = file_get_contents($URI); + return $jsonData; +} + /** * * @param string $freq diff --git a/includes/hostapd.php b/includes/hostapd.php index 30883f48..0c335b94 100755 --- a/includes/hostapd.php +++ b/includes/hostapd.php @@ -8,7 +8,7 @@ require_once 'includes/config.php'; getWifiInterface(); /** - * + * Initialize hostapd values, display interface * */ function DisplayHostAPDConfig() @@ -34,7 +34,6 @@ function DisplayHostAPDConfig() SaveHostAPDConfig($arrSecurity, $arrEncType, $arr80211Standard, $interfaces, $status); } } - $arrHostapdConf = parse_ini_file('/etc/raspap/hostapd.ini'); if (!RASPI_MONITOR_ENABLED) { @@ -71,7 +70,6 @@ function DisplayHostAPDConfig() if (strlen($hostapdconfigline) === 0) { continue; } - if ($hostapdconfigline[0] != "#") { $arrLine = explode("=", $hostapdconfigline); $arrConfig[$arrLine[0]]=$arrLine[1]; @@ -107,6 +105,16 @@ function DisplayHostAPDConfig() ); } +/** + * Validate user input, save configs for hostapd, dnsmasq & dhcp + * + * @param array $wpa_array + * @param array $enc_types + * @param array $modes + * @param string $interface + * @param object $status + * @return boolean + */ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) { // It should not be possible to send bad data for these fields so clearly @@ -125,12 +133,10 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $status->addMessage('Attempting to set channel to invalid number.', 'danger'); $good_input = false; } - if (intval($_POST['channel']) < 1 || intval($_POST['channel']) > RASPI_5GHZ_MAX_CHANNEL) { $status->addMessage('Attempting to set channel outside of permitted range', 'danger'); $good_input = false; } - // Check for Bridged AP mode checkbox $bridgedEnable = 0; if ($arrHostapdConf['BridgedEnable'] == 0) { @@ -142,7 +148,6 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $bridgedEnable = 1; } } - // Check for WiFi client AP mode checkbox $wifiAPEnable = 0; if ($bridgedEnable == 0) { // enable client mode actions when not bridged @@ -156,7 +161,6 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) } } } - // Check for Logfile output checkbox $logEnable = 0; if ($arrHostapdConf['LogEnable'] == 0) { @@ -174,13 +178,17 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) exec('sudo '.RASPI_CONFIG.'/hostapd/disablelog.sh'); } } + // set AP interface default, override for ap-sta & bridged options + $ap_iface = $_POST['interface']; + if ($wifiAPEnable) { $ap_iface = 'uap0'; } + if ($bridgedEnable) { $ap_iface = 'br0'; } + // persist user options to /etc/raspap $cfg = []; $cfg['WifiInterface'] = $_POST['interface']; $cfg['LogEnable'] = $logEnable; // Save previous Client mode status when Bridged - $cfg['WifiAPEnable'] = ($bridgedEnable == 1 ? - $arrHostapdConf['WifiAPEnable'] : $wifiAPEnable); + $cfg['WifiAPEnable'] = ($bridgedEnable == 1 ? $arrHostapdConf['WifiAPEnable'] : $wifiAPEnable); $cfg['BridgedEnable'] = $bridgedEnable; $cfg['WifiManaged'] = $_POST['interface']; write_php_ini($cfg, RASPI_CONFIG.'/hostapd.ini'); @@ -238,153 +246,98 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $_POST['max_num_sta'] = $_POST['max_num_sta'] < 1 ? null : $_POST['max_num_sta']; if ($good_input) { - // Fixed values - $country_code = $_POST['country_code']; - $config = 'driver=nl80211'.PHP_EOL; - $config.= 'ctrl_interface='.RASPI_HOSTAPD_CTRL_INTERFACE.PHP_EOL; - $config.= 'ctrl_interface_group=0'.PHP_EOL; - $config.= 'auth_algs=1'.PHP_EOL; - $config.= 'wpa_key_mgmt=WPA-PSK'.PHP_EOL; - if (isset($_POST['beaconintervalEnable'])) { - $config.= 'beacon_int='.$_POST['beacon_interval'].PHP_EOL; - } - if (isset($_POST['disassoc_low_ackEnable'])) { - $config.= 'disassoc_low_ack=0'.PHP_EOL; - } - $config.= 'ssid='.$_POST['ssid'].PHP_EOL; - $config.= 'channel='.$_POST['channel'].PHP_EOL; - if ($_POST['hw_mode'] === 'n') { - $config.= 'hw_mode=g'.PHP_EOL; - $config.= 'ieee80211n=1'.PHP_EOL; - // Enable basic Quality of service - $config.= 'wmm_enabled=1'.PHP_EOL; - } elseif ($_POST['hw_mode'] === 'ac') { - $config.= 'hw_mode=a'.PHP_EOL.PHP_EOL; - $config.= '# N'.PHP_EOL; - $config.= 'ieee80211n=1'.PHP_EOL; - $config.= 'require_ht=1'.PHP_EOL; - $config.= 'ht_capab=[MAX-AMSDU-3839][HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40]'.PHP_EOL.PHP_EOL; - $config.= '# AC'.PHP_EOL; - $config.= 'ieee80211ac=1'.PHP_EOL; - $config.= 'require_vht=1'.PHP_EOL; - $config.= 'ieee80211d=0'.PHP_EOL; - $config.= 'ieee80211h=0'.PHP_EOL; - $config.= 'vht_capab=[MAX-AMSDU-3839][SHORT-GI-80]'.PHP_EOL; - $config.= 'vht_oper_chwidth=1'.PHP_EOL; - $config.= 'vht_oper_centr_freq_seg0_idx=42'.PHP_EOL.PHP_EOL; - } elseif ($_POST['hw_mode'] === 'w') { - $config.= 'ieee80211w=2'.PHP_EOL; - $config.= 'wpa_key_mgmt=WPA-EAP-SHA256'.PHP_EOL; - } else { - $config.= 'hw_mode='.$_POST['hw_mode'].PHP_EOL; - $config.= 'ieee80211n=0'.PHP_EOL; - } - if ($_POST['wpa'] !== 'none') { - $config.= 'wpa_passphrase='.$_POST['wpa_passphrase'].PHP_EOL; - } - if ($wifiAPEnable == 1) { - $config.= 'interface=uap0'.PHP_EOL; - } elseif ($bridgedEnable == 1) { - $config.='interface='.$_POST['interface'].PHP_EOL; - $config.= 'bridge=br0'.PHP_EOL; - } else { - $config.= 'interface='.$_POST['interface'].PHP_EOL; - } - $config.= 'wpa='.$_POST['wpa'].PHP_EOL; - $config.= 'wpa_pairwise='.$_POST['wpa_pairwise'].PHP_EOL; - $config.= 'country_code='.$_POST['country_code'].PHP_EOL; - $config.= 'ignore_broadcast_ssid='.$ignore_broadcast_ssid.PHP_EOL; - if (isset($_POST['max_num_sta'])) { - $config.= 'max_num_sta='.$_POST['max_num_sta'].PHP_EOL; - } - - file_put_contents("/tmp/hostapddata", $config); - system("sudo cp /tmp/hostapddata " . RASPI_HOSTAPD_CONFIG, $return); + $return = updateHostapdConfig($ignore_broadcast_ssid); // Fetch dhcp-range, lease time from system config - $dhcpConfig = parse_ini_file(RASPI_DNSMASQ_PREFIX.$_SESSION['ap_interface'].'conf', false, INI_SCANNER_RAW); + $syscfg = parse_ini_file(RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', false, INI_SCANNER_RAW); if ($wifiAPEnable == 1) { - // Enable uap0 configuration in dnsmasq for Wifi client AP mode - // Set dhcp-range from system config. If undefined, fallback to default - $dhcp_range = ($dhcpConfig['dhcp-range'] =='10.3.141.50,10.3.141.255,255.255.255.0,12h' || - $dhcpConfig['dhcp-range'] =='') ? '192.168.50.50,192.168.50.150,12h' : $dhcpConfig['dhcp-range']; - $config = 'interface=lo,uap0 # Enable uap0 interface for wireless client AP mode'.PHP_EOL; - $config.= 'bind-dynamic # Hybrid between --bind-interfaces and default'.PHP_EOL; - $config.= 'server=8.8.8.8 # Forward DNS requests to Google DNS'.PHP_EOL; - $config.= 'domain-needed # Don\'t forward short names'.PHP_EOL; - $config.= 'bogus-priv # Never forward addresses in the non-routed address spaces'.PHP_EOL; - $config.= 'dhcp-range='.$dhcp_range.PHP_EOL; - if (!empty($dhcpConfig['dhcp-option'])) { - $config.= 'dhcp-option='.$dhcpConfig['dhcp-option'].PHP_EOL; + // Enable uap0 configuration for ap-sta mode + // Set dhcp-range from system config, fallback to default if undefined + $dhcp_range = ($syscfg['dhcp-range'] == '') ? getDefaultNetValue('dnsmasq','uap0','dhcp-range') : $syscfg['dhcp-range']; + $config = [ '# RaspAP uap0 configuration' ]; + $config[] = 'interface=lo,uap0 # Enable uap0 interface for wireless client AP mode'; + $config[] = 'bind-dynamic # Hybrid between --bind-interfaces and default'; + $config[] = 'server=8.8.8.8 # Forward DNS requests to Google DNS'; + $config[] = 'domain-needed # Don\'t forward short names'; + $config[] = 'bogus-priv # Never forward addresses in the non-routed address spaces'; + $config[] = 'dhcp-range='.$dhcp_range; + if (!empty($syscfg['dhcp-option'])) { + $config[] = 'dhcp-option='.$syscfg['dhcp-option']; } - } else { - // Set dhcp-range from system config. If undefined, fallback to default - $dhcp_range = ($dhcpConfig['dhcp-range'] =='192.168.50.50,192.168.50.150,12h' || - $dhcpConfig['dhcp-range'] =='') ? '10.3.141.50,10.3.141.255,255.255.255.0,12h' : $dhcpConfig['dhcp-range']; - $config = 'domain-needed'.PHP_EOL; - $config.= 'interface='.$_POST['interface'].PHP_EOL; - $config.= 'dhcp-range='.$dhcp_range.PHP_EOL; - if (!empty($dhcpConfig['dhcp-option'])) { - $config.= 'dhcp-option='.$dhcpConfig['dhcp-option'].PHP_EOL; + $config[] = PHP_EOL; + scanConfigDir('/etc/dnsmasq.d/','uap0',$status); + $config = join(PHP_EOL, $config); + file_put_contents("/tmp/dnsmasqdata", $config); + system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return); + } elseif ($bridgedEnable !==1) { + $dhcp_range = ($syscfg['dhcp-range'] =='') ? getDefaultNetValue('dnsmasq','wlan0','dhcp-range') : $syscfg['dhcp-range']; + $config = [ '# RaspAP '.$_POST['interface'].' configuration' ]; + $config[] = 'interface='.$_POST['interface']; + $config[] = 'domain-needed'; + $config[] = 'dhcp-range='.$dhcp_range; + if (!empty($syscfg['dhcp-option'])) { + $config[] = 'dhcp-option='.$syscfg['dhcp-option']; } + $config[] = PHP_EOL; + $config = join(PHP_EOL, $config); + file_put_contents("/tmp/dnsmasqdata", $config); + system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return); } - file_put_contents("/tmp/dnsmasqdata", $config); - system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$_SESSION['ap_interface'].'conf', $return); - - // Set dnsmasq values from ini, fallback to default if undefined - $intConfig = parse_ini_file(RASPI_CONFIG_NETWORKING.'/'.$_POST['interface'].'.ini', false, INI_SCANNER_RAW); - $domain_name_server = ($intConfig['domain_name_server'] =='') ? '1.1.1.1 8.8.8.8' : $intConfig['domain_name_server']; - $routers = ($intConfig['routers'] == '') ? '10.3.141.1' : $intConfig['routers']; - - // write options to dhcpcd.conf - $config = [ '# RaspAP '.$_POST['interface'].' configuration' ]; - $config[] = 'hostname'; - $config[] = 'clientid'; - $config[] = 'persistent'; - $config[] = 'option rapid_commit'; - $config[] = 'option domain_name_servers, domain_name, domain_search, host_name'; - $config[] = 'option classless_static_routes'; - $config[] = 'option ntp_servers'; - $config[] = 'require dhcp_server_identifier'; - $config[] = 'slaac private'; - $config[] = 'nohook lookup-hostname'; + // Set dhcp values from system config, fallback to default if undefined + $jsonData = json_decode(getNetConfig($ap_iface), true); + $domain_name_server = ($jsonData['StaticDNS'] =='') ? getDefaultNetValue('dhcp','wlan0','static domain_name_server') : $jsonData['StaticDNS']; + $routers = ($jsonData['StaticRouters'] == '') ? getDefaultNetValue('dhcp','wlan0','static routers') : $jsonData['StaticRouters']; if ($bridgedEnable == 1) { - $config[] = 'denyinterfaces eth0 wlan0'; + $config = defaultHeader(); + $config[] = PHP_EOL.'# RaspAP br0 configuration'; $config[] = 'interface br0'; + $config[] = 'denyinterfaces eth0 wlan0'; + $config[] = PHP_EOL; } elseif ($wifiAPEnable == 1) { - // Enable uap0 configuration in dhcpcd for Wifi client AP mode - $intConfig = parse_ini_file(RASPI_CONFIG_NETWORKING.'/uap0.ini', false, INI_SCANNER_RAW); - $ip_address = ($intConfig['ip_address'] == '') ? '192.168.50.1/24' : $intConfig['ip_address']; + // Enable uap0 configuration for ap-sta + $ip_address = ($jsonData['StaticIP'] == '') ? getDefaultNetValue('dhcp','uap0','static ip_address') : $jsonData['StaticIP']; + $config = defaultHeader(); + $config[] = PHP_EOL.'# RaspAP uap0 configuration'; $config[] = 'interface uap0'; $config[] = 'static ip_address='.$ip_address; $config[] = 'nohook wpa_supplicant'; + $config[] = PHP_EOL; } else { - // Default config - $ip_address = "10.3.141.1/24"; // fallback IP - // default IP of the AP xxx.xxx.xxx.1/24 of the selected dhcp range + // Default wlan0 config + $ip_address = ($jsonData['StaticIP'] == '') ? getDefaultNetValue('dhcp','wlan0','static ip_address') : $jsonData['StaticIP']; $def_ip = array(); if (preg_match("/^([0-9]{1,3}\.){3}/",$dhcp_range,$def_ip) ) $ip_address = $def_ip[0]."1/24"; // use static IP assigned to interface only, if consistent with the selected dhcp range - if (preg_match("/^([0-9]{1,3}\.){3}/",$intConfig['ip_address'],$int_ip) && $def_ip[0] === $int_ip[0]) $ip_address = $intConfig['ip_address']; - $config[] = 'interface '.$_POST['interface']; + if (preg_match("/^([0-9]{1,3}\.){3}/",$jsonData['StaticIP'],$int_ip) && $def_ip[0] === $int_ip[0]) $ip_address = $jsonData['StaticIP']; + $config = [ '# RaspAP wlan0 configuration' ]; + $config[] = 'interface wlan0'; $config[] = 'static ip_address='.$ip_address; + $config[] = 'static routers='.$routers; $config[] = 'static domain_name_server='.$domain_name_server; - - // write the static IP back to the $_POST['interface'].ini file - $intConfig['interface'] = $_POST['interface']; - $intConfig['ip_address'] = $ip_address; - $intConfig['domain_name_server'] = $domain_name_server; - $intConfig['routers'] = $routers; - $intConfig['static'] = "true"; - $intConfig['failover'] = "false"; - write_php_ini($intConfig, RASPI_CONFIG_NETWORKING.'/'.$_POST['interface'].".ini"); + if (! is_null($jsonData['Metric'])) { $config[] = 'metric '.$jsonData['Metric']; } } - $config = join(PHP_EOL, $config); + $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); - $config = preg_replace('/^#\sRaspAP\s.*?(?=\s*^\s*$)/ms', $config, $dhcp_cfg, 1); - file_put_contents("/tmp/dhcpddata", $config); + if ($bridgedEnable == 1 || $wifiAPEnable == 1) { + $dhcp_cfg = join(PHP_EOL, $config); + $status->addMessage('DHCP configuration for '.$ap_iface.' enabled.', 'success'); + } elseif (!preg_match('/^interface\s'.$ap_iface.'$/m', $dhcp_cfg)) { + $config[] = PHP_EOL; + $config= join(PHP_EOL, $config); + $dhcp_cfg = removeDHCPIface($dhcp_cfg,'br0'); + $dhcp_cfg = removeDHCPIface($dhcp_cfg,'uap0'); + $dhcp_cfg .= $config; + $status->addMessage('DHCP configuration for '.$ap_iface.' added.', 'success'); + } else { + $config = join(PHP_EOL, $config); + $dhcp_cfg = removeDHCPIface($dhcp_cfg,'br0'); + $dhcp_cfg = removeDHCPIface($dhcp_cfg,'uap0'); + $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$ap_iface.'\s.*?(?=\s*^\s*$)/ms', $config, $dhcp_cfg, 1); + $status->addMessage('DHCP configuration for '.$ap_iface.' updated.', 'success'); + } + file_put_contents("/tmp/dhcpddata", $dhcp_cfg); system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return); if ($return == 0) { @@ -396,6 +349,78 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $status->addMessage('Unable to save wifi hotspot settings', 'danger'); return false; } - return true; } + +/** + * Updates a hostapd configuration + * + * @return boolean $result + */ +function updateHostapdConfig($ignore_broadcast_ssid) +{ + // Fixed values + $country_code = $_POST['country_code']; + $config = 'driver=nl80211'.PHP_EOL; + $config.= 'ctrl_interface='.RASPI_HOSTAPD_CTRL_INTERFACE.PHP_EOL; + $config.= 'ctrl_interface_group=0'.PHP_EOL; + $config.= 'auth_algs=1'.PHP_EOL; + $config.= 'wpa_key_mgmt=WPA-PSK'.PHP_EOL; + if (isset($_POST['beaconintervalEnable'])) { + $config.= 'beacon_int='.$_POST['beacon_interval'].PHP_EOL; + } + if (isset($_POST['disassoc_low_ackEnable'])) { + $config.= 'disassoc_low_ack=0'.PHP_EOL; + } + $config.= 'ssid='.$_POST['ssid'].PHP_EOL; + $config.= 'channel='.$_POST['channel'].PHP_EOL; + if ($_POST['hw_mode'] === 'n') { + $config.= 'hw_mode=g'.PHP_EOL; + $config.= 'ieee80211n=1'.PHP_EOL; + // Enable basic Quality of service + $config.= 'wmm_enabled=1'.PHP_EOL; + } elseif ($_POST['hw_mode'] === 'ac') { + $config.= 'hw_mode=a'.PHP_EOL.PHP_EOL; + $config.= '# N'.PHP_EOL; + $config.= 'ieee80211n=1'.PHP_EOL; + $config.= 'require_ht=1'.PHP_EOL; + $config.= 'ht_capab=[MAX-AMSDU-3839][HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40]'.PHP_EOL.PHP_EOL; + $config.= '# AC'.PHP_EOL; + $config.= 'ieee80211ac=1'.PHP_EOL; + $config.= 'require_vht=1'.PHP_EOL; + $config.= 'ieee80211d=0'.PHP_EOL; + $config.= 'ieee80211h=0'.PHP_EOL; + $config.= 'vht_capab=[MAX-AMSDU-3839][SHORT-GI-80]'.PHP_EOL; + $config.= 'vht_oper_chwidth=1'.PHP_EOL; + $config.= 'vht_oper_centr_freq_seg0_idx=42'.PHP_EOL.PHP_EOL; + } elseif ($_POST['hw_mode'] === 'w') { + $config.= 'ieee80211w=2'.PHP_EOL; + $config.= 'wpa_key_mgmt=WPA-EAP-SHA256'.PHP_EOL; + } else { + $config.= 'hw_mode='.$_POST['hw_mode'].PHP_EOL; + $config.= 'ieee80211n=0'.PHP_EOL; + } + if ($_POST['wpa'] !== 'none') { + $config.= 'wpa_passphrase='.$_POST['wpa_passphrase'].PHP_EOL; + } + if ($wifiAPEnable == 1) { + $config.= 'interface=uap0'.PHP_EOL; + } elseif ($bridgedEnable == 1) { + $config.='interface='.$_POST['interface'].PHP_EOL; + $config.= 'bridge=br0'.PHP_EOL; + } else { + $config.= 'interface='.$_POST['interface'].PHP_EOL; + $ap_iface = $_POST['interface']; + } + $config.= 'wpa='.$_POST['wpa'].PHP_EOL; + $config.= 'wpa_pairwise='.$_POST['wpa_pairwise'].PHP_EOL; + $config.= 'country_code='.$_POST['country_code'].PHP_EOL; + $config.= 'ignore_broadcast_ssid='.$ignore_broadcast_ssid.PHP_EOL; + if (isset($_POST['max_num_sta'])) { + $config.= 'max_num_sta='.$_POST['max_num_sta'].PHP_EOL; + } + file_put_contents("/tmp/hostapddata", $config); + system("sudo cp /tmp/hostapddata " . RASPI_HOSTAPD_CONFIG, $result); + return $result; +} + diff --git a/installers/common.sh b/installers/common.sh index 960b42d2..e012a238 100755 --- a/installers/common.sh +++ b/installers/common.sh @@ -21,6 +21,7 @@ readonly raspap_sudoers="/etc/sudoers.d/090_raspap" 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 raspap_network="$raspap_dir/networking/" readonly rulesv4="/etc/iptables/rules.v4" readonly notracking_url="https://raw.githubusercontent.com/notracking/hosts-blocklists/master/" webroot_dir="/var/www/html" @@ -144,11 +145,7 @@ function _create_raspap_directories() { # Create a directory to store networking configs echo "Creating $raspap_dir/networking" sudo mkdir -p "$raspap_dir/networking" - # Copy existing dhcpcd.conf to use as base config - echo "Adding /etc/dhcpcd.conf as base configuration" - cat /etc/dhcpcd.conf | sudo tee -a /etc/raspap/networking/defaults > /dev/null - echo "Changing file ownership of $raspap_dir" - sudo chown -R $raspap_user:$raspap_user "$raspap_dir" || _install_status 1 "Unable to change file ownership for '$raspap_dir'" + } # Generate hostapd logging and service control scripts @@ -235,7 +232,7 @@ function _install_adblock() { echo "addn-hosts=$raspap_dir/adblock/hostnames.txt" | sudo tee -a "$raspap_adblock" > /dev/null || _install_status 1 "Unable to write to $raspap_adblock" fi - # Remove dhcp-option=6 in dnsmasq.d/090_raspap.conf to force local DNS resolution for DHCP clients + # Remove dhcp-option=6 in dnsmasq.d/090_wlan0.conf to force local DNS resolution for DHCP clients echo "Enabling local DNS name resolution for DHCP clients" sudo sed -i '/dhcp-option=6/d' $raspap_dnsmasq || _install_status 1 "Unable to modify $raspap_dnsmasq" @@ -336,8 +333,8 @@ function _check_for_old_configs() { fi if [ -f $raspap_dnsmasq ]; then - sudo cp $raspap_dnsmasq "$raspap_dir/backups/dnsmasq.conf.`date +%F-%R`" - sudo ln -sf "$raspap_dir/backups/dnsmasq.conf.`date +%F-%R`" "$raspap_dir/backups/dnsmasq.conf" + sudo cp $raspap_dnsmasq "$raspap_dir/backups/090_wlan0.conf.`date +%F-%R`" + sudo ln -sf "$raspap_dir/backups/090_wlan0.conf.`date +%F-%R`" "$raspap_dir/backups/090_wlan0.conf" fi if [ -f /etc/dhcpcd.conf ]; then @@ -376,8 +373,12 @@ function _default_configuration() { fi sudo cp $webroot_dir/config/default_hostapd /etc/default/hostapd || _install_status 1 "Unable to move hostapd defaults file" sudo cp $webroot_dir/config/hostapd.conf /etc/hostapd/hostapd.conf || _install_status 1 "Unable to move hostapd configuration file" - sudo cp $webroot_dir/config/dnsmasq.conf $raspap_dnsmasq || _install_status 1 "Unable to move dnsmasq configuration file" + sudo cp $webroot_dir/config/090_wlan0.conf $raspap_dnsmasq || _install_status 1 "Unable to move dnsmasq configuration file" sudo cp $webroot_dir/config/dhcpcd.conf /etc/dhcpcd.conf || _install_status 1 "Unable to move dhcpcd configuration file" + sudo cp $webroot_dir/config/defaults.json $raspap_network || _install_status 1 "Unable to move defaults.json settings" + + echo "Changing file ownership of $raspap_dir" + sudo chown -R $raspap_user:$raspap_user "$raspap_dir" || _install_status 1 "Unable to change file ownership for '$raspap_dir'" echo "Checking for existence of /etc/dnsmasq.d" [ -d /etc/dnsmasq.d ] || sudo mkdir /etc/dnsmasq.d diff --git a/installers/uninstall.sh b/installers/uninstall.sh index 77afda6c..7b33ea25 100755 --- a/installers/uninstall.sh +++ b/installers/uninstall.sh @@ -140,7 +140,9 @@ function _remove_raspap_directories() { # Removes raspapd.service function _remove_raspap_service() { _install_log "Removing raspapd.service" - sudo rm /lib/systemd/system/raspapd.service || _install_error "Unable to remove raspap.service file" + if [ -f /lib/systemd/system/raspapd.service ]; then + sudo rm /lib/systemd/system/raspapd.service || _install_error "Unable to remove raspap.service file" + fi sudo systemctl daemon-reload sudo systemctl disable raspapd.service || _install_error "Failed to disable raspap.service" echo "Done." @@ -172,8 +174,10 @@ function _restore_networking() { fi echo "Done." # Remove dnsmasq and bridge configs - echo "Removing 090_raspap.conf from dnsmasq" - sudo rm "$raspap_dnsmasq" || _install_error "Unable to remove $raspap_dnsmasq" + echo "Removing 090_wlan0.conf from dnsmasq" + if [ -f $raspap_dnsmasq ]; then + sudo rm "$raspap_dnsmasq" || _install_error "Unable to remove $raspap_dnsmasq" + fi echo "Removing raspap bridge configurations" sudo rm "$raspap_network"/raspap* || _install_error "Unable to remove bridge config" if [ -f $raspap_adblock ]; then diff --git a/templates/wifi_stations.php b/templates/wifi_stations.php index bd34246c..176050c4 100755 --- a/templates/wifi_stations.php +++ b/templates/wifi_stations.php @@ -1,6 +1,8 @@ -

-

+
+

+

+