diff --git a/includes/admin.php b/includes/admin.php index 7e7e900f..ba927224 100755 --- a/includes/admin.php +++ b/includes/admin.php @@ -14,7 +14,7 @@ function DisplayAuthConfig($username, $password){ $status->addMessage('Username must not be empty', 'danger'); } else { if ($auth_file = fopen(RASPI_ADMIN_DETAILS, 'w')) { - fwrite($auth_file, $new_username.PHP_EOL); + fwrite($auth_file, $new_username.PHP_EOL); fwrite($auth_file, password_hash($_POST['newpass'], PASSWORD_BCRYPT).PHP_EOL); fclose($auth_file); $username = $new_username; diff --git a/includes/functions.php b/includes/functions.php index 3e9f4ddb..eb76ffca 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -20,6 +20,35 @@ function CSRFValidate() { return hash_equals($_POST['csrf_token'], $_SESSION['csrf_token']); } +/** +* Test whether array is associative +*/ +function isAssoc($arr) { + return array_keys($arr) !== range(0, count($arr) - 1); +} + +/** +* +* Display a selector field for a form. Arguments are: +* $name: Field name +* $options: Array of options +* $selected: Selected option (optional) +* If $options is an associative array this should be the key +* +*/ +function SelectorOptions($name, $options, $selected = null) { + echo ""; +} + /** * * @param string $input @@ -375,186 +404,6 @@ update_config=1 HostAPD is not running - '; - } else { - $status = '
HostAPD is running -
'; - } - - $arrConfig = array(); - $arrChannel = array('a','b','g'); - $arrSecurity = array( 1 => 'WPA', 2 => 'WPA2',3=> 'WPA+WPA2'); - $arrEncType = array('TKIP' => 'TKIP', 'CCMP' => 'CCMP', 'TKIP CCMP' => 'TKIP+CCMP'); - - foreach( $return as $a ) { - if( $a[0] != "#" ) { - $arrLine = explode( "=",$a) ; - $arrConfig[$arrLine[0]]=$arrLine[1]; - } - }; - ?> -
-
-
-
Configure hotspot -
- -
- - - - -
-

-
- -

Basic settings

-
-
-
- - -
-
-
-
- - -
-
-
-
- - -
-
-
-
- - -
-
-
-
-

Security settings

-
-
- - -
-
-
-
- - -
-
-
-
- - -
-
-
-
-

Advanced settings

-
-
- - -
-
-
- - - '; - } else { - echo ''; - }; - ?> - -
-
- -
-
- /tmp/hostapddata", $return ); - system( "sudo cp /tmp/hostapddata " . RASPI_HOSTAPD_CONFIG, $return ); - - if( $return == 0 ) { - echo "Wifi Hotspot settings saved"; - } else { - echo "Wifi Hotspot settings failed to be saved"; - } - } elseif( isset($_POST['SaveOpenVPNSettings']) ) { - // TODO - } elseif( isset($_POST['SaveTORProxySettings']) ) { - // TODO - } elseif( isset($_POST['StartHotspot']) ) { - echo "Attempting to start hotspot"; - exec( 'sudo /etc/init.d/hostapd start', $return ); - foreach( $return as $line ) { - echo $line."
"; - } - } elseif( isset($_POST['StopHotspot']) ) { - echo "Attempting to stop hotspot"; - exec( 'sudo /etc/init.d/hostapd stop', $return ); - foreach( $return as $line ) { - echo $line."
"; - } - } elseif( isset($_POST['StartOpenVPN']) ) { - echo "Attempting to start openvpn"; - exec( 'sudo /etc/init.d/openvpn start', $return ); - foreach( $return as $line ) { - echo $line."
"; - } - } elseif( isset($_POST['StopOpenVPN']) ) { - echo "Attempting to stop openvpn"; - exec( 'sudo /etc/init.d/openvpn stop', $return ); - foreach( $return as $line ) { - echo $line."
"; - } - } elseif( isset($_POST['StartTOR']) ) { - echo "Attempting to start TOR"; - exec( 'sudo /etc/init.d/tor start', $return ); - foreach( $return as $line ) { - echo $line."
"; - } - } elseif( isset($_POST['StopTOR']) ) { - echo "Attempting to stop TOR"; - exec( 'sudo /etc/init.d/tor stop', $return ); - foreach( $return as $line ) { - echo $line."
"; - } - } +function SaveTORAndVPNConfig(){ + if( isset($_POST['SaveOpenVPNSettings']) ) { + // TODO + } elseif( isset($_POST['SaveTORProxySettings']) ) { + // TODO + } elseif( isset($_POST['StartOpenVPN']) ) { + echo "Attempting to start openvpn"; + exec( 'sudo /etc/init.d/openvpn start', $return ); + foreach( $return as $line ) { + echo $line."
"; + } + } elseif( isset($_POST['StopOpenVPN']) ) { + echo "Attempting to stop openvpn"; + exec( 'sudo /etc/init.d/openvpn stop', $return ); + foreach( $return as $line ) { + echo $line."
"; + } + } elseif( isset($_POST['StartTOR']) ) { + echo "Attempting to start TOR"; + exec( 'sudo /etc/init.d/tor start', $return ); + foreach( $return as $line ) { + echo $line."
"; + } + } elseif( isset($_POST['StopTOR']) ) { + echo "Attempting to stop TOR"; + exec( 'sudo /etc/init.d/tor stop', $return ); + foreach( $return as $line ) { + echo $line."
"; + } + } } ?> - diff --git a/includes/hostapd.php b/includes/hostapd.php new file mode 100755 index 00000000..9d35785b --- /dev/null +++ b/includes/hostapd.php @@ -0,0 +1,227 @@ + 'WPA', 2 => 'WPA2',3=> 'WPA+WPA2'); + $arrEncType = array('TKIP' => 'TKIP', 'CCMP' => 'CCMP', 'TKIP CCMP' => 'TKIP+CCMP'); + exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces); + + + if( isset($_POST['SaveHostAPDSettings']) ) { + if (CSRFValidate()) { + SaveHostAPDConfig($arrSecurity, $arrEncType, $arrChannel, $interfaces, $status); + } else { + error_log('CSRF violation'); + } + } elseif( isset($_POST['StartHotspot']) ) { + if (CSRFValidate()) { + $status->addMessage('Attempting to start hotspot', 'info'); + exec( 'sudo /etc/init.d/hostapd start', $return ); + foreach( $return as $line ) { + $status->addMessage($line, 'info'); + } + } else { + error_log('CSRF violation'); + } + } elseif( isset($_POST['StopHotspot']) ) { + if (CSRFValidate()) { + $status->addMessage('Attempting to stop hotspot', 'info'); + exec( 'sudo /etc/init.d/hostapd stop', $return ); + foreach( $return as $line ) { + $status->addMessage($line, 'info'); + } + } else { + error_log('CSRF violation'); + } + } + + exec( 'cat '. RASPI_HOSTAPD_CONFIG, $return ); + exec( 'pidof hostapd | wc -l', $hostapdstatus); + + if( $hostapdstatus[0] == 0 ) { + $status->addMessage('HostAPD is not running', 'warning'); + } else { + $status->addMessage('HostAPD is running', 'success'); + } + + foreach( $return as $a ) { + if( $a[0] != "#" ) { + $arrLine = explode( "=",$a) ; + $arrConfig[$arrLine[0]]=$arrLine[1]; + } + }; + ?> +
+
+
+
Configure hotspot
+ +
+
+ + + + +
+

showMessages(); ?>

+
+ +

Basic settings

+ +
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+

Security settings

+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+

Advanced settings

+
+
+ + +
+
+
+ + + '; + } else { + echo ''; + }; + ?> + +
+ +
+
+ 14) { + error_log("Attempting to set channel to '".$_POST['channel']."'"); + return false; + } + + $good_input = true; + + // Verify input + if (strlen($_POST['ssid']) == 0 || strlen($_POST['ssid']) > 32) { + // Not sure of all the restrictions of SSID + $status->addMessage('SSID must be between 1 and 32 characters', 'danger'); + $good_input = false; + } + if (strlen($_POST['wpa_passphrase']) < 8 || strlen($_POST['wpa_passphrase']) > 63) { + $status->addMessage('WPA passphrase must be between 8 and 63 characters', 'danger'); + $good_input = false; + } + if (! in_array($_POST['interface'], $interfaces)) { + // The user is probably up to something here but it may also be a + // genuine error. + $status->addMessage('Unknown interface '.$_POST['interface'], 'danger'); + $good_input = false; + } + if (strlen($_POST['country_code']) != 0 && strlen($_POST['country_code']) != 2) { + $status->addMessage('Country code must be blank or two characters', 'danger'); + $good_input = false; + } + + if ($good_input) { + if ($tmp_file = fopen('/tmp/hostapddata', 'w')) { + // Fixed values + fwrite($tmp_file, 'driver=nl80211'.PHP_EOL); + fwrite($tmp_file, 'ctrl_interface='.RASPI_HOSTAPD_CTRL_INTERFACE.PHP_EOL); + fwrite($tmp_file, 'ctrl_interface_group=0'.PHP_EOL); + fwrite($tmp_file, 'beacon_int=100'.PHP_EOL); + fwrite($tmp_file, 'auth_algs=1'.PHP_EOL); + fwrite($tmp_file, 'wpa_key_mgmt=WPA-PSK'.PHP_EOL); + + fwrite($tmp_file, 'ssid='.$_POST['ssid'].PHP_EOL); + fwrite($tmp_file, 'channel='.$_POST['channel'].PHP_EOL); + fwrite($tmp_file, 'hw_mode='.$_POST['hw_mode'].PHP_EOL); + fwrite($tmp_file, 'wpa_passphrase='.$_POST['wpa_passphrase'].PHP_EOL); + fwrite($tmp_file, 'interface='.$_POST['interface'].PHP_EOL); + fwrite($tmp_file, 'wpa='.$_POST['wpa'].PHP_EOL); + fwrite($tmp_file, 'wpa_pairwise='.$_POST['wpa_pairwise'].PHP_EOL); + fwrite($tmp_file, 'country_code='.$_POST['country_code'].PHP_EOL); + fclose($tmp_file); + + system( "sudo cp /tmp/hostapddata " . RASPI_HOSTAPD_CONFIG, $return ); + if( $return == 0 ) { + $status->addMessage('Wifi Hotspot settings saved', 'success'); + } else { + $status->addMessage('Unable to save wifi hotspot settings', 'danger'); + } + } else { + $status->addMessage('Unable to save wifi hotspot settings', 'danger'); + return false; + } + } + return true; +} +?> diff --git a/index.php b/index.php index b53eec8c..2b066bef 100755 --- a/index.php +++ b/index.php @@ -42,6 +42,7 @@ include_once( 'includes/functions.php' ); include_once( 'includes/authenticate.php' ); include_once( 'includes/admin.php' ); include_once( 'includes/dhcp.php' ); +include_once( 'includes/hostapd.php' ); $output = $return = 0; $page = $_GET['page']; @@ -59,8 +60,8 @@ $csrf_token = $_SESSION['csrf_token']; - - + + @@ -96,106 +97,106 @@ $csrf_token = $_SESSION['csrf_token']; - - + + -
- - - -
-
-

- RaspAP -

-
-
+
- -
+ +
+
+

+ RaspAP +

+
+
+ + +
@@ -217,5 +218,5 @@ $csrf_token = $_SESSION['csrf_token']; - +