From fe3b0e9513f16db464b2ced887de1a8a42272ba3 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Fri, 12 Aug 2016 17:00:43 +0000 Subject: [PATCH 1/7] Move client wifi configuration into separate file --- includes/configure_client.php | 126 ++++++++++++++++++++++++++++++++++ includes/functions.php | 123 --------------------------------- index.php | 1 + 3 files changed, 127 insertions(+), 123 deletions(-) create mode 100755 includes/configure_client.php diff --git a/includes/configure_client.php b/includes/configure_client.php new file mode 100755 index 0000000..4044dd7 --- /dev/null +++ b/includes/configure_client.php @@ -0,0 +1,126 @@ + +
+
+
+
Configure client +
+ +
+ +

Client settings

+
+
+ + 2 ) { + exec( 'wpa_passphrase '.$ssid. ' ' . $psk,$network ); + foreach($network as $b) { + $config .= "$b +"; + } + } + } + exec( "echo '$config' > /tmp/wifidata", $return ); + system( 'sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval ); + if( $returnval == 0 ) { + echo '
Wifi settings updated successfully +
'; + } else { + echo '
Wifi settings failed to be updated +
'; + } + + // scan networks + } elseif( isset($_POST['Scan']) ) { + $return = ''; + exec( 'sudo wpa_cli scan',$return ); + sleep(3); + exec( 'sudo wpa_cli scan_results',$return ); + for( $shift = 0; $shift < 4; $shift++ ) { + array_shift($return); + } + // display output + echo '
'; + echo '
'; + echo '

Networks found

'; + echo ''; + foreach( $return as $network ) { + $arrNetwork = preg_split("/[\t]+/",$network); + $bssid = $arrNetwork[0]; + $channel = ConvertToChannel($arrNetwork[1]); + $signal = $arrNetwork[2] . " dBm"; + $security = $arrNetwork[3]; + $ssid = $arrNetwork[4]; + echo '"; + } + echo '
SSIDChannelSignalSecurity
' . $ssid . " " . $channel . "" . $signal . "". ConvertToSecurity($security) ."
'; + + } else { + + // default action, output configured network(s) + exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $return); + $ssid = array(); + $psk = array(); + + foreach($return as $a) { + if(preg_match('/SSID/i',$a)) { + $arrssid = explode("=",$a); + $ssid[] = str_replace('"','',$arrssid[1]); + } + if(preg_match('/psk/i',$a)) { + $arrpsk = explode("=",$a); + $psk[] = str_replace('"','',$arrpsk[1]); + } + } + + $numSSIDs = count($ssid); + $output = '
'; + + if ( $numSSIDs > 0 ) { + for( $ssids = 0; $ssids < $numSSIDs; $ssids++ ) { + $output .= '
+
+
+
+
'; + } + $output .= '
'; + } else { + $status = '
Not connected +
'; + } + $output .= '
'; + $output .= ''; + echo $output; + } + ?> + + +
+
+
+
+ diff --git a/includes/functions.php b/includes/functions.php index 9d12869..21c88ea 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -281,129 +281,6 @@ function DisplayDashboard(){ -
-
-
-
Configure client -
- -
- -

Client settings

-
-
- - 2 ) { - exec( 'wpa_passphrase '.$ssid. ' ' . $psk,$network ); - foreach($network as $b) { - $config .= "$b -"; - } - } - } - exec( "echo '$config' > /tmp/wifidata", $return ); - system( 'sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval ); - if( $returnval == 0 ) { - echo '
Wifi settings updated successfully -
'; - } else { - echo '
Wifi settings failed to be updated -
'; - } - - // scan networks - } elseif( isset($_POST['Scan']) ) { - $return = ''; - exec( 'sudo wpa_cli scan',$return ); - sleep(3); - exec( 'sudo wpa_cli scan_results',$return ); - for( $shift = 0; $shift < 4; $shift++ ) { - array_shift($return); - } - // display output - echo '
'; - echo '
'; - echo '

Networks found

'; - echo ''; - foreach( $return as $network ) { - $arrNetwork = preg_split("/[\t]+/",$network); - $bssid = $arrNetwork[0]; - $channel = ConvertToChannel($arrNetwork[1]); - $signal = $arrNetwork[2] . " dBm"; - $security = $arrNetwork[3]; - $ssid = $arrNetwork[4]; - echo '"; - } - echo '
SSIDChannelSignalSecurity
' . $ssid . " " . $channel . "" . $signal . "". ConvertToSecurity($security) ."
'; - - } else { - - // default action, output configured network(s) - exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $return); - $ssid = array(); - $psk = array(); - - foreach($return as $a) { - if(preg_match('/SSID/i',$a)) { - $arrssid = explode("=",$a); - $ssid[] = str_replace('"','',$arrssid[1]); - } - if(preg_match('/psk/i',$a)) { - $arrpsk = explode("=",$a); - $psk[] = str_replace('"','',$arrpsk[1]); - } - } - - $numSSIDs = count($ssid); - $output = '
'; - - if ( $numSSIDs > 0 ) { - for( $ssids = 0; $ssids < $numSSIDs; $ssids++ ) { - $output .= '
-
-
-
-
'; - } - $output .= '
'; - } else { - $status = '
Not connected -
'; - } - $output .= '
'; - $output .= ''; - echo $output; - } - ?> - - -
-
-
-
- Date: Fri, 12 Aug 2016 17:29:56 +0000 Subject: [PATCH 2/7] Change method of calculating channel and security --- includes/configure_client.php | 208 +++++++++++++++++----------------- includes/functions.php | 70 +++++------- 2 files changed, 132 insertions(+), 146 deletions(-) diff --git a/includes/configure_client.php b/includes/configure_client.php index 4044dd7..a5e2aa1 100755 --- a/includes/configure_client.php +++ b/includes/configure_client.php @@ -5,121 +5,121 @@ * */ function DisplayWPAConfig(){ - $status = ''; - ?> -
-
-
-
Configure client -
- -
- -

Client settings

-
-
+ $status = ''; + ?> +
+
+
+
Configure client +
+ +
+ +

Client settings

+
+
- 2 ) { - exec( 'wpa_passphrase '.$ssid. ' ' . $psk,$network ); - foreach($network as $b) { - $config .= "$b + if ( strlen($psk) >2 ) { + exec( 'wpa_passphrase '.$ssid. ' ' . $psk,$network ); + foreach($network as $b) { + $config .= "$b "; - } - } - } - exec( "echo '$config' > /tmp/wifidata", $return ); - system( 'sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval ); - if( $returnval == 0 ) { - echo '
Wifi settings updated successfully -
'; - } else { - echo '
Wifi settings failed to be updated -
'; - } + } + } + } + exec( "echo '$config' > /tmp/wifidata", $return ); + system( 'sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval ); + if( $returnval == 0 ) { + echo '
Wifi settings updated successfully +
'; + } else { + echo '
Wifi settings failed to be updated +
'; + } - // scan networks - } elseif( isset($_POST['Scan']) ) { - $return = ''; - exec( 'sudo wpa_cli scan',$return ); - sleep(3); - exec( 'sudo wpa_cli scan_results',$return ); - for( $shift = 0; $shift < 4; $shift++ ) { - array_shift($return); - } - // display output - echo '
'; - echo '
'; - echo '

Networks found

'; - echo ''; - foreach( $return as $network ) { - $arrNetwork = preg_split("/[\t]+/",$network); - $bssid = $arrNetwork[0]; - $channel = ConvertToChannel($arrNetwork[1]); - $signal = $arrNetwork[2] . " dBm"; - $security = $arrNetwork[3]; - $ssid = $arrNetwork[4]; - echo '"; - } - echo '
SSIDChannelSignalSecurity
' . $ssid . " " . $channel . "" . $signal . "". ConvertToSecurity($security) ."
'; + // scan networks + } elseif( isset($_POST['Scan']) ) { + $return = ''; + exec( 'sudo wpa_cli scan',$return ); + sleep(3); + exec( 'sudo wpa_cli scan_results',$return ); + for( $shift = 0; $shift < 4; $shift++ ) { + array_shift($return); + } + // display output + echo '
'; + echo '
'; + echo '

Networks found

'; + echo ''; + foreach( $return as $network ) { + $arrNetwork = preg_split("/[\t]+/",$network); + $bssid = $arrNetwork[0]; + $channel = ConvertToChannel($arrNetwork[1]); + $signal = $arrNetwork[2] . " dBm"; + $security = $arrNetwork[3]; + $ssid = $arrNetwork[4]; + echo '"; + } + echo '
SSIDChannelSignalSecurity
' . $ssid . " " . $channel . "" . $signal . "". ConvertToSecurity($security) ."
'; - } else { + } else { - // default action, output configured network(s) - exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $return); - $ssid = array(); - $psk = array(); + // default action, output configured network(s) + exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $return); + $ssid = array(); + $psk = array(); - foreach($return as $a) { - if(preg_match('/SSID/i',$a)) { - $arrssid = explode("=",$a); - $ssid[] = str_replace('"','',$arrssid[1]); - } - if(preg_match('/psk/i',$a)) { - $arrpsk = explode("=",$a); - $psk[] = str_replace('"','',$arrpsk[1]); - } - } + foreach($return as $a) { + if(preg_match('/SSID/i',$a)) { + $arrssid = explode("=",$a); + $ssid[] = str_replace('"','',$arrssid[1]); + } + if(preg_match('/psk/i',$a)) { + $arrpsk = explode("=",$a); + $psk[] = str_replace('"','',$arrpsk[1]); + } + } - $numSSIDs = count($ssid); - $output = '
'; - - if ( $numSSIDs > 0 ) { - for( $ssids = 0; $ssids < $numSSIDs; $ssids++ ) { - $output .= '
-
-
-
-
'; - } - $output .= '
'; - } else { - $status = '
Not connected -
'; - } - $output .= '
'; - $output .= ''; - echo $output; - } - ?> - - -
-
-
-
+ $numSSIDs = count($ssid); + $output = '
'; + + if ( $numSSIDs > 0 ) { + for( $ssids = 0; $ssids < $numSSIDs; $ssids++ ) { + $output .= '
+
+
+
+
'; + } + $output .= '
'; + } else { + $status = '
Not connected +
'; + } + $output .= '
'; + $output .= ''; + echo $output; + } + ?> + + +
+
+
+
0 && $channel < 14) { + return $channel; + } else { + return 'Invalid Channel'; + } } /** @@ -105,36 +99,28 @@ function ConvertToChannel( $freq ) { * @return string */ function ConvertToSecurity( $security ) { - - switch( $security ) { - case "[WPA2-PSK-CCMP][ESS]": - return "WPA2-PSK (AES)"; - break; - case "[WPA2-PSK-TKIP][ESS]": - return "WPA2-PSK (TKIP)"; - break; - case "[WPA2-PSK-CCMP][WPS][ESS]": - return "WPA/WPA2-PSK (TKIP/AES)"; - break; - case "[WPA2-PSK-TKIP+CCMP][WPS][ESS]": - return "WPA2-PSK (TKIP/AES) with WPS"; - break; - case "[WPA-PSK-TKIP+CCMP][WPS][ESS]": - return "WPA-PSK (TKIP/AES) with WPS"; - break; - case "[WPA-PSK-TKIP][WPA2-PSK-CCMP][WPS][ESS]": - return "WPA/WPA2-PSK (TKIP/AES)"; - break; - case "[WPA-PSK-TKIP+CCMP][WPA2-PSK-TKIP+CCMP][ESS]": - return "WPA/WPA2-PSK (TKIP/AES)"; - break; - case "[WPA-PSK-TKIP][ESS]": - return "WPA-PSK (TKIP)"; - break; - case "[WEP][ESS]": - return "WEP"; - break; - } + $options = array(); + preg_match_all('/\[([^\]]+)\]/s', $security, $matches); + foreach($matches[1] as $match) { + if (preg_match('/^(WPA\d?)/', $match, $protocol_match)) { + $protocol = $protocol_match[1]; + $matchArr = explode('-', $match); + if (count($matchArr) > 2) { + $options[] = $protocol . ' ('. $matchArr[2] .')'; + } else { + $options[] = $protocol; + } + } + } + + if (count($options) === 0) { + // This could also be WEP but wpa_supplicant doesn't have a way to determine + // this. + // And you shouldn't be using WEP these days anyway. + return 'Open'; + } else { + return implode('
', $options); + } } /** From ad76f7651a05ee3a91de8ab4ab242b986a3b14b5 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Fri, 12 Aug 2016 20:53:44 +0000 Subject: [PATCH 3/7] Split out php from html --- includes/configure_client.php | 254 ++++++++++++++++++++-------------- 1 file changed, 153 insertions(+), 101 deletions(-) diff --git a/includes/configure_client.php b/includes/configure_client.php index a5e2aa1..2b1d118 100755 --- a/includes/configure_client.php +++ b/includes/configure_client.php @@ -5,119 +5,171 @@ * */ function DisplayWPAConfig(){ - $status = ''; - ?> -
-
-
-
Configure client -
- -
- -

Client settings

-
-
+ $status = new StatusMessages(); + $scanned_networks = array(); - 2 ) { - exec( 'wpa_passphrase '.$ssid. ' ' . $psk,$network ); - foreach($network as $b) { - $config .= "$b + if ( strlen($psk) >2 ) { + exec( 'wpa_passphrase '.$ssid. ' ' . $psk,$network ); + foreach($network as $b) { + $config .= "$b "; - } - } - } - exec( "echo '$config' > /tmp/wifidata", $return ); - system( 'sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval ); - if( $returnval == 0 ) { - echo '
Wifi settings updated successfully -
'; - } else { - echo '
Wifi settings failed to be updated -
'; - } + } + } + } + exec( "echo '$config' > /tmp/wifidata", $return ); + system( 'sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval ); + if( $returnval == 0 ) { + $status->addMessage('Wifi settings updated successfully', 'success'); + } else { + $status->addMessage('Wifi settings failed to be updated', 'danger'); + } + } elseif( isset($_POST['Scan']) ) { + $return = ''; + exec( 'sudo wpa_cli scan',$return ); + sleep(3); + exec( 'sudo wpa_cli scan_results',$return ); + for( $shift = 0; $shift < 4; $shift++ ) { + array_shift($return); + } + // display output + foreach( $return as $network ) { + $arrNetwork = preg_split("/[\t]+/",$network); + $scanned_networks[] = array( + 'bssid' => $arrNetwork[0], + 'channel' => ConvertToChannel($arrNetwork[1]), + 'signal' => $arrNetwork[2] . " dBm", + 'security' => ConvertToSecurity($arrNetwork[3]), + 'ssid' => $arrNetwork[4] + ); + } + echo ''; + } + + // default action, output configured network(s) + exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $supplicant_return); + $ssid = array(); + $psk = array(); + + foreach($supplicant_return as $a) { + if(preg_match('/SSID/i',$a)) { + $arrssid = explode("=",$a); + $ssid[] = str_replace('"','',$arrssid[1]); + } + if(preg_match('/psk/i',$a)) { + $arrpsk = explode("=",$a); + $psk[] = str_replace('"','',$arrpsk[1]); + } + } - // scan networks - } elseif( isset($_POST['Scan']) ) { - $return = ''; - exec( 'sudo wpa_cli scan',$return ); - sleep(3); - exec( 'sudo wpa_cli scan_results',$return ); - for( $shift = 0; $shift < 4; $shift++ ) { - array_shift($return); - } - // display output - echo '
'; - echo '
'; - echo '

Networks found

'; - echo ''; - foreach( $return as $network ) { - $arrNetwork = preg_split("/[\t]+/",$network); - $bssid = $arrNetwork[0]; - $channel = ConvertToChannel($arrNetwork[1]); - $signal = $arrNetwork[2] . " dBm"; - $security = $arrNetwork[3]; - $ssid = $arrNetwork[4]; - echo '"; - } - echo '
SSIDChannelSignalSecurity
' . $ssid . " " . $channel . "" . $signal . "". ConvertToSecurity($security) ."
'; + $numSSIDs = count($ssid); + ?> - } else { - // default action, output configured network(s) - exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $return); - $ssid = array(); - $psk = array(); +
+
+
+
Configure client
+ +
+

showMessages(); ?>

+

Client settings

+
+
- foreach($return as $a) { - if(preg_match('/SSID/i',$a)) { - $arrssid = explode("=",$a); - $ssid[] = str_replace('"','',$arrssid[1]); - } - if(preg_match('/psk/i',$a)) { - $arrpsk = explode("=",$a); - $psk[] = str_replace('"','',$arrpsk[1]); - } - } + + +
+
+
+ + + +
+
+

Networks found

+
+ + + + + + + + + + + + + + + + + + + + + +
SSIDChannelSignalSecurity
+
+ +
+
- $numSSIDs = count($ssid); - $output = '
'; - - if ( $numSSIDs > 0 ) { - for( $ssids = 0; $ssids < $numSSIDs; $ssids++ ) { - $output .= '
-
-
-
-
'; - } - $output .= '
'; - } else { - $status = '
Not connected -
'; - } - $output .= '
'; - $output .= ''; - echo $output; - } - ?> - - +
+
+
+ +
+ +
+
+
+ +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ +
+
+ +
+
+
+ + + + +
+
+ +
-
+
Date: Fri, 12 Aug 2016 21:22:21 +0000 Subject: [PATCH 4/7] Allow open networks --- dist/js/functions.js | 36 +++++++----- includes/configure_client.php | 107 ++++++++++++++++++++++++++-------- 2 files changed, 104 insertions(+), 39 deletions(-) diff --git a/dist/js/functions.js b/dist/js/functions.js index 5adf581..3588a3b 100644 --- a/dist/js/functions.js +++ b/dist/js/functions.js @@ -25,24 +25,32 @@ function AddNetwork() { } -function AddScanned(network) { +function AddScanned(network, open = false) { - existing = document.getElementById("networkbox").getElementsByTagName('div').length; + existing = document.getElementById("networkbox").getElementsByTagName('div').length; var Networks = document.getElementById('Networks').value; - //if(existing != 0) { - Networks++; - //} + //if(existing != 0) { + Networks++; + //} document.getElementById('Networks').value=Networks; - document.getElementById('networkbox').innerHTML += '

Network '+Networks+'

\ -
\ -
\ -
'; - document.getElementById('ssid'+Networks).value = network; - if(existing == 0) { - Networks++ - document.getElementById('Networks').value = Networks; - } + document.getElementById('networkbox').innerHTML += '

Network '+Networks+'

'; + document.getElementById('networkbox').innerHTML += '
'; + + if ( open ) { + document.getElementById('networkbox').innerHTML += ''; + document.getElementById('Save').disabled = false; + } else { + document.getElementById('networkbox').innerHTML += ''; + document.getElementById('networkbox').innerHTML += '
'; + } + + document.getElementById('networkbox').innerHTML += '
'; + document.getElementById('ssid'+Networks).value = network; + if(existing == 0) { + Networks++ + document.getElementById('Networks').value = Networks; + } } function CheckSSID(ssid) { diff --git a/includes/configure_client.php b/includes/configure_client.php index 2b1d118..7e8d347 100755 --- a/includes/configure_client.php +++ b/includes/configure_client.php @@ -17,16 +17,24 @@ update_config=1 for( $x = 0; $x < $networks; $x++ ) { $network = ''; $ssid = escapeshellarg( $_POST['ssid'.$x] ); - $protocol = escapeshellarg( $_POST['protocol'.$x] ); - $psk = escapeshellarg( $_POST['psk'.$x] ); + $protocol = $_POST['protocol'.$x]; + if ($protocol === 'Open') { + $config .= "network={".PHP_EOL; + $config .= "\tssid=\"".$ssid."\"".PHP_EOL; + $config .= "\tkey_mgmt=NONE".PHP_EOL; + $config .= "}".PHP_EOL; + } else { + $psk = escapeshellarg( $_POST['psk'.$x] ); - if ( strlen($psk) >2 ) { - exec( 'wpa_passphrase '.$ssid. ' ' . $psk,$network ); - foreach($network as $b) { - $config .= "$b + if ( strlen($psk) >2 ) { + exec( 'wpa_passphrase '.$ssid. ' ' . $psk,$network ); + foreach($network as $b) { + $config .= "$b "; + } } } + error_log($config); } exec( "echo '$config' > /tmp/wifidata", $return ); system( 'sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval ); @@ -57,25 +65,66 @@ update_config=1 echo ''; } - // default action, output configured network(s) - exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $supplicant_return); - $ssid = array(); - $psk = array(); +// // default action, output configured network(s) +// exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $supplicant_return); +// $ssid = array(); +// $psk = array(); +// +// foreach($supplicant_return as $a) { +// if(preg_match('/SSID/i',$a)) { +// $arrssid = explode("=",$a); +// $ssid[] = str_replace('"','',$arrssid[1]); +// } +// if(preg_match('/psk/i',$a)) { +// $arrpsk = explode("=",$a); +// $psk[] = str_replace('"','',$arrpsk[1]); +// } +// } +// +// $numSSIDs = count($ssid); + - foreach($supplicant_return as $a) { - if(preg_match('/SSID/i',$a)) { - $arrssid = explode("=",$a); - $ssid[] = str_replace('"','',$arrssid[1]); - } - if(preg_match('/psk/i',$a)) { - $arrpsk = explode("=",$a); - $psk[] = str_replace('"','',$arrpsk[1]); - } - } - $numSSIDs = count($ssid); - ?> + // Find currently configured networks$ + exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $known_return); + + $known_networks = array(); + $network_id = null; + + foreach($known_return as $line) { + error_log($line); + if (preg_match('/network\s*=/', $line)) { + $known_networks[] = array(); + $network_id = count($known_networks) - 1; + } elseif ($network_id !== null) { + if (preg_match('/^\s*}\s*$/', $line)) { + $network_id = null; + } elseif ($lineArr = preg_split('/\s*=\s*/', trim($line))) { + switch(strtolower($lineArr[0])) { + case 'ssid': + $known_networks[$network_id]['ssid'] = trim($lineArr[1], '"'); + break; + case 'psk': + if (array_key_exists('passphrase', $known_networks[$network_id])) { + break; + } + case '#psk': + $known_networks[$network_id]['protocol'] = 'WPA'; + case 'wep_key0': // Untested + $known_networks[$network_id]['passphrase'] = trim($lineArr[1], '"'); + break; + case 'key_mgmt': + if (! array_key_exists('passphrase', $known_networks[$network_id]) &&$lineArr[1] === 'NONE') { + $known_networks[$network_id]['protocol'] = 'Open'; + $known_networks[$network_id]['passphrase'] = '(Open)'; + } + break; + } + } + } + } +?>
@@ -132,7 +181,7 @@ update_config=1
- +
@@ -142,15 +191,23 @@ update_config=1
- + +
+
+
+
+ +
+
- +
+
From e953e685569a0cc35a96047bc74689b49e255ace Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Sun, 14 Aug 2016 16:40:59 +0000 Subject: [PATCH 5/7] Redo 'Configure client' page --- README.md | 1 + includes/configure_client.php | 331 +++++++++++++++------------------- includes/functions.php | 7 +- installers/common.sh | 4 +- 4 files changed, 158 insertions(+), 185 deletions(-) diff --git a/README.md b/README.md index f3b83e2..9d7cbe2 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ Add the following to the end of `/etc/sudoers`: ```sh www-data ALL=(ALL) NOPASSWD:/sbin/ifdown wlan0,/sbin/ifup wlan0,/bin/cat /etc/wpa_supplicant/wpa_supplicant.conf,/bin/cp /tmp/wifidata /etc/wpa_supplicant/wpa_supplicant.conf,/sbin/wpa_cli scan_results, /sbin/wpa_cli scan,/bin/cp /tmp/hostapddata /etc/hostapd/hostapd.conf, /etc/init.d/hostapd start,/etc/init.d/hostapd stop,/etc/init.d/dnsmasq start, /etc/init.d/dnsmasq stop,/bin/cp /tmp/dhcpddata /etc/dnsmasq.conf, /sbin/shutdown -h now, /sbin/reboot +www-data ALL=(ALL) NOPASSWD:/sbin/wpa_cli reconfigure ``` Once those modifications are done, git clone the files to `/var/www`. diff --git a/includes/configure_client.php b/includes/configure_client.php index 7e8d347..6233195 100755 --- a/includes/configure_client.php +++ b/includes/configure_client.php @@ -8,122 +8,128 @@ function DisplayWPAConfig(){ $status = new StatusMessages(); $scanned_networks = array(); - if( isset($_POST['SaveWPAPSKSettings']) ) { - - $config = 'ctrl_interface=DIR='. RASPI_WPA_CTRL_INTERFACE .' GROUP=netdev -update_config=1 -'; - $networks = $_POST['Networks']; - for( $x = 0; $x < $networks; $x++ ) { - $network = ''; - $ssid = escapeshellarg( $_POST['ssid'.$x] ); - $protocol = $_POST['protocol'.$x]; - if ($protocol === 'Open') { - $config .= "network={".PHP_EOL; - $config .= "\tssid=\"".$ssid."\"".PHP_EOL; - $config .= "\tkey_mgmt=NONE".PHP_EOL; - $config .= "}".PHP_EOL; - } else { - $psk = escapeshellarg( $_POST['psk'.$x] ); - - if ( strlen($psk) >2 ) { - exec( 'wpa_passphrase '.$ssid. ' ' . $psk,$network ); - foreach($network as $b) { - $config .= "$b -"; - } - } - } - error_log($config); - } - exec( "echo '$config' > /tmp/wifidata", $return ); - system( 'sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval ); - if( $returnval == 0 ) { - $status->addMessage('Wifi settings updated successfully', 'success'); - } else { - $status->addMessage('Wifi settings failed to be updated', 'danger'); - } - } elseif( isset($_POST['Scan']) ) { - $return = ''; - exec( 'sudo wpa_cli scan',$return ); - sleep(3); - exec( 'sudo wpa_cli scan_results',$return ); - for( $shift = 0; $shift < 4; $shift++ ) { - array_shift($return); - } - // display output - foreach( $return as $network ) { - $arrNetwork = preg_split("/[\t]+/",$network); - $scanned_networks[] = array( - 'bssid' => $arrNetwork[0], - 'channel' => ConvertToChannel($arrNetwork[1]), - 'signal' => $arrNetwork[2] . " dBm", - 'security' => ConvertToSecurity($arrNetwork[3]), - 'ssid' => $arrNetwork[4] - ); - } - echo ''; - } - -// // default action, output configured network(s) -// exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $supplicant_return); -// $ssid = array(); -// $psk = array(); -// -// foreach($supplicant_return as $a) { -// if(preg_match('/SSID/i',$a)) { -// $arrssid = explode("=",$a); -// $ssid[] = str_replace('"','',$arrssid[1]); -// } -// if(preg_match('/psk/i',$a)) { -// $arrpsk = explode("=",$a); -// $psk[] = str_replace('"','',$arrpsk[1]); -// } -// } -// -// $numSSIDs = count($ssid); - - - - - // Find currently configured networks$ + // Find currently configured networks exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $known_return); - $known_networks = array(); - $network_id = null; + $network = null; + $ssid = null; foreach($known_return as $line) { - error_log($line); if (preg_match('/network\s*=/', $line)) { - $known_networks[] = array(); - $network_id = count($known_networks) - 1; - } elseif ($network_id !== null) { + $network = array('visible' => false, 'configured' => true); + } elseif ($network !== null) { if (preg_match('/^\s*}\s*$/', $line)) { - $network_id = null; + $networks[$ssid] = $network; + $network = null; + $ssid = null; } elseif ($lineArr = preg_split('/\s*=\s*/', trim($line))) { switch(strtolower($lineArr[0])) { case 'ssid': - $known_networks[$network_id]['ssid'] = trim($lineArr[1], '"'); + $ssid = trim($lineArr[1], '"'); break; case 'psk': - if (array_key_exists('passphrase', $known_networks[$network_id])) { + if (array_key_exists('passphrase', $network)) { break; } case '#psk': - $known_networks[$network_id]['protocol'] = 'WPA'; + $network['protocol'] = 'WPA'; case 'wep_key0': // Untested - $known_networks[$network_id]['passphrase'] = trim($lineArr[1], '"'); + $network['passphrase'] = trim($lineArr[1], '"'); break; case 'key_mgmt': - if (! array_key_exists('passphrase', $known_networks[$network_id]) &&$lineArr[1] === 'NONE') { - $known_networks[$network_id]['protocol'] = 'Open'; - $known_networks[$network_id]['passphrase'] = '(Open)'; + if (! array_key_exists('passphrase', $network) && $lineArr[1] === 'NONE') { + $network['protocol'] = 'Open'; } break; } } } } + + if ( isset($_POST['client_settings']) && CSRFValidate() ) { + $tmp_networks = $networks; + if ($wpa_file = fopen('/tmp/wifidata', 'w')) { + fwrite($wpa_file, 'ctrl_interface=DIR=' . RASPI_WPA_CTRL_INTERFACE . ' GROUP=netdev' . PHP_EOL); + fwrite($wpa_file, 'update_config=1' . PHP_EOL); + + foreach(array_keys($_POST) as $post) { + if (preg_match('/delete(\d+)/', $post, $post_match)) { + unset($tmp_networks[$_POST['ssid' . $post_match[1]]]); + } elseif (preg_match('/update(\d+)/', $post, $post_match)) { + // NB, at the moment, the value of protocol from the form may + // contain HTML line breaks + $tmp_networks[$_POST['ssid' . $post_match[1]]] = array( + 'protocol' => ( $_POST['protocol' . $post_match[1]] === 'Open' ? 'Open' : 'WPA' ), + 'passphrase' => $_POST['passphrase' . $post_match[1]], + 'configured' => true + ); + } + } + + $ok = true; + foreach($tmp_networks as $ssid => $network) { + if ($network['protocol'] === 'Open') { + fwrite($wpa_file, "network={".PHP_EOL); + fwrite($wpa_file, "\tssid=\"".$ssid."\"".PHP_EOL); + fwrite($wpa_file, "\tkey_mgmt=NONE".PHP_EOL); + fwrite($wpa_file, "}".PHP_EOL); + } else { + if (strlen($network['passphrase']) >=8 && strlen($network['passphrase']) <= 63) { + exec( 'wpa_passphrase '.escapeshellarg($ssid). ' ' . escapeshellarg($network['passphrase']),$wpa_passphrase ); + foreach($wpa_passphrase as $line) { + fwrite($wpa_file, $line.PHP_EOL); + } + } else { + $status->addMessage('WPA passphrase must be between 8 and 63 characters', 'danger'); + $ok = false; + + } + } + + } + + if ($ok) { + system( 'sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval ); + if( $returnval == 0 ) { + exec('sudo wpa_cli reconfigure', $reconfigure_out, $reconfigure_return ); + if ($reconfigure_return == 0) { + $status->addMessage('Wifi settings updated successfully', 'success'); + $networks = $tmp_networks; + } else { + $status->addMessage('Wifi settings updated but cannot restart (cannon execute "wpa_cli reconfigure")', 'danger'); + } + } else { + $status->addMessage('Wifi settings failed to be updated', 'danger'); + } + } + } else { + $status->addMessage('Failed to updated wifi settings', 'danger'); + } + } + + exec( 'sudo wpa_cli scan' ); + sleep(3); + exec( 'sudo wpa_cli scan_results',$scan_return ); + for( $shift = 0; $shift < 2; $shift++ ) { + array_shift($scan_return); + } + // display output + foreach( $scan_return as $network ) { + $arrNetwork = preg_split("/[\t]+/",$network); + if (array_key_exists($arrNetwork[4], $networks)) { + $networks[$arrNetwork[4]]['visible'] = true; + $networks[$arrNetwork[4]]['channel'] = ConvertToChannel($arrNetwork[1]); + // TODO What if the security has changed? + } else { + $networks[$arrNetwork[4]] = array( + 'configured' => false, + 'protocol' => ConvertToSecurity($arrNetwork[3]), + 'channel' => ConvertToChannel($arrNetwork[1]), + 'passphrase' => '', + 'visible' => true + ); + } + } ?>
@@ -134,98 +140,59 @@ update_config=1

showMessages(); ?>

Client settings

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

Networks found

-
- - - - - - - - - - - - - - - - - - - - - -
SSIDChannelSignalSecurity
-
- -
-
-
-
-
- -
- -
-
-
- -
-
-
-
- - -
-
-
-
- - -
-
- -
-
- - -
-
- -
-
- -
+ + + + + + + + + + + + + + $network) { ?> + + + + + + + + + + + + + + + + + + + + +
SSIDChannelSecurityPassphrase
+ + + X--- + + +
+ + /> + + + + />
- - -
-
- - - - -
-
- - +
+
+
diff --git a/includes/functions.php b/includes/functions.php index 6538f57..739ebe1 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -17,7 +17,12 @@ function CSRFToken() { * */ function CSRFValidate() { - return hash_equals($_POST['csrf_token'], $_SESSION['csrf_token']); + if ( hash_equals($_POST['csrf_token'], $_SESSION['csrf_token']) ) { + return true; + } else { + error_log('CSRF violation'); + return false; + } } /** diff --git a/installers/common.sh b/installers/common.sh index dbf1695..917f3e4 100644 --- a/installers/common.sh +++ b/installers/common.sh @@ -97,7 +97,7 @@ function move_config_file() { function patch_system_files() { install_log "Patching system sudoers file" # patch /etc/sudoers file - sudo bash -c 'echo "www-data ALL=(ALL) NOPASSWD:/sbin/ifdown wlan0,/sbin/ifup wlan0,/bin/cat /etc/wpa_supplicant/wpa_supplicant.conf,/bin/cp /tmp/wifidata /etc/wpa_supplicant/wpa_supplicant.conf,/sbin/wpa_cli scan_results, /sbin/wpa_cli scan,/bin/cp /tmp/hostapddata /etc/hostapd/hostapd.conf, /etc/init.d/hostapd start,/etc/init.d/hostapd stop,/etc/init.d/dnsmasq start, /etc/init.d/dnsmasq stop,/bin/cp /tmp/dhcpddata /etc/dnsmasq.conf, /sbin/shutdown -h now, /sbin/reboot" | (EDITOR="tee -a" visudo)' \ + sudo bash -c 'echo "www-data ALL=(ALL) NOPASSWD:/sbin/ifdown wlan0,/sbin/ifup wlan0,/bin/cat /etc/wpa_supplicant/wpa_supplicant.conf,/bin/cp /tmp/wifidata /etc/wpa_supplicant/wpa_supplicant.conf,/sbin/wpa_cli scan_results, /sbin/wpa_cli scan,/sbin/wpa_cli reconfigure:/bin/cp /tmp/hostapddata /etc/hostapd/hostapd.conf, /etc/init.d/hostapd start,/etc/init.d/hostapd stop,/etc/init.d/dnsmasq start, /etc/init.d/dnsmasq stop,/bin/cp /tmp/dhcpddata /etc/dnsmasq.conf, /sbin/shutdown -h now, /sbin/reboot" | (EDITOR="tee -a" visudo)' \ || install_error "Unable to patch /etc/sudoers" } @@ -124,4 +124,4 @@ function install_raspap() { move_config_file patch_system_files install_complete -} \ No newline at end of file +} From b353dd77a895f26af4cd5057e661f332f4f0013e Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Sun, 14 Aug 2016 16:56:13 +0000 Subject: [PATCH 6/7] Add password length check Also remove most of the rest of the javascript as it is unused. --- dist/js/functions.js | 87 +++-------------------------------- includes/configure_client.php | 8 ++-- 2 files changed, 11 insertions(+), 84 deletions(-) diff --git a/dist/js/functions.js b/dist/js/functions.js index 3588a3b..e916430 100644 --- a/dist/js/functions.js +++ b/dist/js/functions.js @@ -1,82 +1,9 @@ -function WiFiDown() { - var down = confirm("Take down wlan0 ?"); - if(down) { - } else { - alert("Action cancelled"); - } -} - -function UpdateNetworks() { - var existing = document.getElementById("networkbox").getElementsByTagName('div').length; - document.getElementById("Networks").value = existing; -} - -function AddNetwork() { -// existing = document.getElementById("networkbox").getElementsByTagName('div').length; -// existing++; - Networks++ - var Networks = document.getElementById('Networks').value; - document.getElementById('networkbox').innerHTML += '

Network '+Networks+'

\ -
\ -
\ -
'; - Networks++; - document.getElementById('Networks').value=Networks; - -} - -function AddScanned(network, open = false) { - - existing = document.getElementById("networkbox").getElementsByTagName('div').length; - var Networks = document.getElementById('Networks').value; - //if(existing != 0) { - Networks++; - //} - - document.getElementById('Networks').value=Networks; - document.getElementById('networkbox').innerHTML += '

Network '+Networks+'

'; - document.getElementById('networkbox').innerHTML += '
'; - - if ( open ) { - document.getElementById('networkbox').innerHTML += ''; - document.getElementById('Save').disabled = false; - } else { - document.getElementById('networkbox').innerHTML += ''; - document.getElementById('networkbox').innerHTML += '
'; - } - - document.getElementById('networkbox').innerHTML += '
'; - document.getElementById('ssid'+Networks).value = network; - if(existing == 0) { - Networks++ - document.getElementById('Networks').value = Networks; +function CheckPSK(psk, id) { + if(psk.value.length < 8 || psk.value.length > 63) { + psk.style.background='#FFD0D0'; + document.getElementById(id).disabled = true; + } else { + psk.style.background='#D0FFD0'; + document.getElementById(id).disabled = false; } } - -function CheckSSID(ssid) { - if(ssid.value.length>31) { - ssid.style.background='#FFD0D0'; - document.getElementById('Save').disabled = true; - } else { - ssid.style.background='#D0FFD0' - document.getElementById('Save').disabled = false; - } -} - -function CheckPSK(psk) { - if(psk.value.length < 8) { - psk.style.background='#FFD0D0'; - document.getElementById('Save').disabled = true; - } else { - psk.style.background='#D0FFD0'; - document.getElementById('Save').disabled = false; - } -} - -function DeleteNetwork(network) { - element = document.getElementById('Networkbox'+network); - element.parentNode.removeChild(element); - var Networks = document.getElementById('Networks').value; - Networks-- - document.getElementById('Networks').value = Networks; -} diff --git a/includes/configure_client.php b/includes/configure_client.php index 6233195..cc9a1b5 100755 --- a/includes/configure_client.php +++ b/includes/configure_client.php @@ -174,14 +174,14 @@ function DisplayWPAConfig(){ --- - +
- /> + /> - + /> />
@@ -192,7 +192,7 @@ function DisplayWPAConfig(){
- +
From 9492330196f7c48c8952272e34ac6f8c9a62f64f Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Sun, 14 Aug 2016 17:25:53 +0000 Subject: [PATCH 7/7] Indicated connected network --- includes/configure_client.php | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/includes/configure_client.php b/includes/configure_client.php index cc9a1b5..ba2e9e6 100755 --- a/includes/configure_client.php +++ b/includes/configure_client.php @@ -16,7 +16,7 @@ function DisplayWPAConfig(){ foreach($known_return as $line) { if (preg_match('/network\s*=/', $line)) { - $network = array('visible' => false, 'configured' => true); + $network = array('visible' => false, 'configured' => true, 'connected' => false); } elseif ($network !== null) { if (preg_match('/^\s*}\s*$/', $line)) { $networks[$ssid] = $network; @@ -126,10 +126,18 @@ function DisplayWPAConfig(){ 'protocol' => ConvertToSecurity($arrNetwork[3]), 'channel' => ConvertToChannel($arrNetwork[1]), 'passphrase' => '', - 'visible' => true + 'visible' => true, + 'connected' => false ); } } + + exec( 'iwconfig wlan0', $iwconfig_return ); + foreach ($iwconfig_return as $line) { + if (preg_match( '/ESSID:\"(.+)\"/i',$line,$iwconfig_ssid )) { + $networks[$iwconfig_ssid[1]]['connected'] = true; + } + } ?>
@@ -156,11 +164,14 @@ function DisplayWPAConfig(){ $network) { ?> - - - - - + + + + + + + +