From 74b6e80728203673ca54d6ebebccd944188bb2ab Mon Sep 17 00:00:00 2001 From: ReadmeCritic Date: Sat, 18 Jun 2016 10:30:46 -0700 Subject: [PATCH 01/34] Update README URLs based on HTTP redirects --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 66c7cd43..f3b83e2f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # `$ raspap-webgui` [![Release 1.1](https://img.shields.io/badge/Release-1.1-green.svg)](https://github.com/billz/raspap-webgui/releases) A simple, responsive web interface to control wifi, hostapd and related services on the Raspberry Pi. -This project was inspired by a [**blog post**](http://sirlagz.net/2013/02/06/script-web-configuration-page-for-raspberry-pi/) by SirLagz about using a web page rather than ssh to configure wifi and hostapd settings on the Raspberry Pi. I mostly just prettified the UI by wrapping it in [**SB Admin 2**](https://github.com/IronSummitMedia/startbootstrap-sb-admin-2), a Bootstrap based admin theme. +This project was inspired by a [**blog post**](http://sirlagz.net/2013/02/06/script-web-configuration-page-for-raspberry-pi/) by SirLagz about using a web page rather than ssh to configure wifi and hostapd settings on the Raspberry Pi. I mostly just prettified the UI by wrapping it in [**SB Admin 2**](https://github.com/BlackrockDigital/startbootstrap-sb-admin-2), a Bootstrap based admin theme. We'd be curious to hear about how you use this with your own Pi-powered access points. Ping us on Twitter ([**@billzimmerman**](https://twitter.com/billzimmerman) and [**@SirLagz**](https://twitter.com/SirLagz)). Until then, here are some screenshots: From 098caa1262773aebfc49008993902b36cb83e857 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Sun, 19 Jun 2016 22:16:32 +0100 Subject: [PATCH 02/34] Remove spurious ' character --- includes/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/functions.php b/includes/functions.php index 182795e4..333b2244 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -677,7 +677,7 @@ function DisplayDHCPConfig() { - ' + Date: Sun, 19 Jun 2016 22:50:36 +0100 Subject: [PATCH 03/34] Correctly find Received Bytes --- includes/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/functions.php b/includes/functions.php index 333b2244..0a5f85f8 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -110,7 +110,7 @@ function DisplayDashboard(){ $strRxPackets = $result[1]; preg_match( '/TX packets:(\d+)/',$strWlan0,$result ); $strTxPackets = $result[1]; - preg_match( '//RX bytes:(\d+)/i',$strWlan0,$result ); + preg_match( '/RX bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result ); $strRxBytes = $result[1]; preg_match( '/TX Bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result ); $strTxBytes = $result[1]; From 443c75390a89289873de1fb6468a2df9ee5a01bb Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Sun, 19 Jun 2016 23:01:22 +0100 Subject: [PATCH 04/34] Set $status to avoid error --- includes/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/functions.php b/includes/functions.php index 0a5f85f8..7d9765c6 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -236,7 +236,7 @@ function DisplayDashboard(){ * */ function DisplayWPAConfig(){ - + $status = ''; ?>
From d431c3f767a8cbf8beef76f7ad2546b458bcd5e8 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Sun, 19 Jun 2016 23:23:03 +0100 Subject: [PATCH 05/34] Permit flags in the config --- includes/functions.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 7d9765c6..0edc4939 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -21,9 +21,10 @@ function GetDistString( $input,$string,$offset,$separator ) { function ParseConfig( $arrConfig ) { $config = array(); foreach( $arrConfig as $line ) { - if( $line[0] != "#" ) { + $line = trim($line); + if( $line != "" && $line[0] != "#" ) { $arrLine = explode( "=",$line ); - $config[$arrLine[0]] = $arrLine[1]; + $config[$arrLine[0]] = ( count($arrLine) > 1 ? $arrLine[1] : true ); } } return $config; From 5c2492e78599760b256f6f3958e5e30ff40bf54f Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Fri, 24 Jun 2016 22:39:39 +0100 Subject: [PATCH 06/34] Add CSRF token to password change page --- includes/admin.php | 39 ++++++++++++++++++++++----------------- includes/functions.php | 20 ++++++++++++++++++++ index.php | 10 ++++++++++ 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/includes/admin.php b/includes/admin.php index a13f9bbd..513473e4 100755 --- a/includes/admin.php +++ b/includes/admin.php @@ -13,26 +13,30 @@ function Status($message, $level='success', $dismissable=true) { function DisplayAuthConfig($username, $password){ $status = ''; if (isset($_POST['UpdateAdminPassword'])) { - if (password_verify($_POST['oldpass'], $password)) { - $new_username=trim($_POST['username']); - if ($_POST['newpass'] != $_POST['newpassagain']) { - $status = Status('New passwords do not match', 'danger'); - } else if ($new_username == '') { - $status = Status('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, password_hash($_POST['newpass'], PASSWORD_BCRYPT).PHP_EOL); - fclose($auth_file); - $username = $new_username; - $status = Status('Admin password updated'); + if (CSRFValidate()) { + if (password_verify($_POST['oldpass'], $password)) { + $new_username=trim($_POST['username']); + if ($_POST['newpass'] != $_POST['newpassagain']) { + $status = Status('New passwords do not match', 'danger'); + } else if ($new_username == '') { + $status = Status('Username must not be empty', 'danger'); } else { - $status = Status('Failed to update admin password', 'danger'); + if ($auth_file = fopen(RASPI_ADMIN_DETAILS, 'w')) { + fwrite($auth_file, $new_username.PHP_EOL); + fwrite($auth_file, password_hash($_POST['newpass'], PASSWORD_BCRYPT).PHP_EOL); + fclose($auth_file); + $username = $new_username; + $status = Status('Admin password updated'); + } else { + $status = Status('Failed to update admin password', 'danger'); + } } + } else { + $status = Status('Old password does not match', 'danger'); } - } else { - $status = Status('Old password does not match', 'danger'); - } + } else { + // Log something + } } ?>
@@ -42,6 +46,7 @@ function DisplayAuthConfig($username, $password){

+
diff --git a/includes/functions.php b/includes/functions.php index 0edc4939..72abadcc 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -1,5 +1,25 @@ + + From 0e162074225922ba22d71da9dcf252b2e7197bb0 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Sun, 26 Jun 2016 17:22:07 +0100 Subject: [PATCH 07/34] Fix password update form --- includes/admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/admin.php b/includes/admin.php index a13f9bbd..5025cf5f 100755 --- a/includes/admin.php +++ b/includes/admin.php @@ -41,7 +41,7 @@ function DisplayAuthConfig($username, $password){
Configure Auth

- +
From d92b01e8f273992826a4bfbbbd5a5ce7665aeec1 Mon Sep 17 00:00:00 2001 From: Joseph Haig Date: Sun, 26 Jun 2016 17:25:59 +0100 Subject: [PATCH 08/34] Fix tabbing --- includes/admin.php | 14 +++++++------- includes/functions.php | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/includes/admin.php b/includes/admin.php index 513473e4..f9a8a4ff 100755 --- a/includes/admin.php +++ b/includes/admin.php @@ -11,10 +11,10 @@ function Status($message, $level='success', $dismissable=true) { } function DisplayAuthConfig($username, $password){ - $status = ''; - if (isset($_POST['UpdateAdminPassword'])) { - if (CSRFValidate()) { - if (password_verify($_POST['oldpass'], $password)) { + $status = ''; + if (isset($_POST['UpdateAdminPassword'])) { + if (CSRFValidate()) { + if (password_verify($_POST['oldpass'], $password)) { $new_username=trim($_POST['username']); if ($_POST['newpass'] != $_POST['newpassagain']) { $status = Status('New passwords do not match', 'danger'); @@ -35,7 +35,7 @@ function DisplayAuthConfig($username, $password){ $status = Status('Old password does not match', 'danger'); } } else { - // Log something + error_log('CSRF violation'); } } ?> @@ -45,8 +45,8 @@ function DisplayAuthConfig($username, $password){
Configure Auth

- - + +
diff --git a/includes/functions.php b/includes/functions.php index 72abadcc..bd32ec65 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -7,7 +7,7 @@ */ function CSRFToken() { ?> - + Date: Sat, 9 Jul 2016 00:55:03 +0100 Subject: [PATCH 09/34] Move status messages into new class --- includes/admin.php | 24 ++++++++---------------- includes/status_messages.php | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 includes/status_messages.php diff --git a/includes/admin.php b/includes/admin.php index f9a8a4ff..8aacf8f2 100755 --- a/includes/admin.php +++ b/includes/admin.php @@ -1,38 +1,30 @@ '.$message; - if ($dismissable) $status .= ''; - $status .= '
'; - - return $status; -} +include_once( 'includes/status_messages.php' ); function DisplayAuthConfig($username, $password){ - $status = ''; + $status = new StatusMessages(); if (isset($_POST['UpdateAdminPassword'])) { if (CSRFValidate()) { if (password_verify($_POST['oldpass'], $password)) { $new_username=trim($_POST['username']); if ($_POST['newpass'] != $_POST['newpassagain']) { - $status = Status('New passwords do not match', 'danger'); + $status->addMessage('New passwords do not match', 'danger'); } else if ($new_username == '') { - $status = Status('Username must not be empty', 'danger'); + $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, password_hash($_POST['newpass'], PASSWORD_BCRYPT).PHP_EOL); fclose($auth_file); $username = $new_username; - $status = Status('Admin password updated'); + $status->addMessage('Admin password updated'); } else { - $status = Status('Failed to update admin password', 'danger'); + $status->addMessage('Failed to update admin password', 'danger'); } } } else { - $status = Status('Old password does not match', 'danger'); + $status->addMessage('Old password does not match', 'danger'); } } else { error_log('CSRF violation'); @@ -44,7 +36,7 @@ function DisplayAuthConfig($username, $password){
Configure Auth
-

+

showMessages(); ?>

diff --git a/includes/status_messages.php b/includes/status_messages.php new file mode 100644 index 00000000..d58b8f5a --- /dev/null +++ b/includes/status_messages.php @@ -0,0 +1,22 @@ +'.$message; + if ($dismissable) $status .= ''; + $status .= '
'; + + array_push($this->messages, $status); + } + + public function showMessages($clear = true) { + foreach($this->messages as $message) { + echo $message; + } + if ( $clear ) $this->messages = array(); + } +} +?> From c65ddd010fc9311292a2a4b41025945b66a72d3b Mon Sep 17 00:00:00 2001 From: Joseph Haig Date: Sat, 9 Jul 2016 01:00:53 +0100 Subject: [PATCH 10/34] Tabs to spaces --- includes/admin.php | 124 +++++++++++++++++------------------ includes/status_messages.php | 6 +- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/includes/admin.php b/includes/admin.php index 8aacf8f2..7e7e900f 100755 --- a/includes/admin.php +++ b/includes/admin.php @@ -3,72 +3,72 @@ include_once( 'includes/status_messages.php' ); function DisplayAuthConfig($username, $password){ - $status = new StatusMessages(); - if (isset($_POST['UpdateAdminPassword'])) { - if (CSRFValidate()) { - if (password_verify($_POST['oldpass'], $password)) { - $new_username=trim($_POST['username']); - if ($_POST['newpass'] != $_POST['newpassagain']) { - $status->addMessage('New passwords do not match', 'danger'); - } else if ($new_username == '') { - $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, password_hash($_POST['newpass'], PASSWORD_BCRYPT).PHP_EOL); - fclose($auth_file); - $username = $new_username; - $status->addMessage('Admin password updated'); - } else { - $status->addMessage('Failed to update admin password', 'danger'); - } - } - } else { - $status->addMessage('Old password does not match', 'danger'); - } + $status = new StatusMessages(); + if (isset($_POST['UpdateAdminPassword'])) { + if (CSRFValidate()) { + if (password_verify($_POST['oldpass'], $password)) { + $new_username=trim($_POST['username']); + if ($_POST['newpass'] != $_POST['newpassagain']) { + $status->addMessage('New passwords do not match', 'danger'); + } else if ($new_username == '') { + $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, password_hash($_POST['newpass'], PASSWORD_BCRYPT).PHP_EOL); + fclose($auth_file); + $username = $new_username; + $status->addMessage('Admin password updated'); + } else { + $status->addMessage('Failed to update admin password', 'danger'); + } + } + } else { + $status->addMessage('Old password does not match', 'danger'); + } } else { - error_log('CSRF violation'); + error_log('CSRF violation'); } } ?> -
-
-
-
Configure Auth
-
-

showMessages(); ?>

- - -
-
- - -
-
-
-
- - -
-
-
-
- - -
-
-
-
- - -
-
- - -
-
-
-
+
+
+
+
Configure Auth
+
+

showMessages(); ?>

+
+ +
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+ +
+
+
+
+
messages as $message) { echo $message; - } - if ( $clear ) $this->messages = array(); - } + } + if ( $clear ) $this->messages = array(); + } } ?> From 3b043950deaeecbbbd3e41ffaf560c296490e4ac Mon Sep 17 00:00:00 2001 From: Joseph Haig Date: Sat, 9 Jul 2016 01:26:13 +0100 Subject: [PATCH 11/34] Add CSRF to DHCP form Also, separate out into separate file and refactor --- includes/dhcp.php | 220 +++++++++++++++++++++++++++++++++++++++++ includes/functions.php | 182 ---------------------------------- index.php | 3 +- 3 files changed, 222 insertions(+), 183 deletions(-) create mode 100755 includes/dhcp.php diff --git a/includes/dhcp.php b/includes/dhcp.php new file mode 100755 index 00000000..48b122b3 --- /dev/null +++ b/includes/dhcp.php @@ -0,0 +1,220 @@ + /tmp/dhcpddata',$temp ); + system( 'sudo cp /tmp/dhcpddata '. RASPI_DNSMASQ_CONFIG, $return ); + + if( $return == 0 ) { + $status->addMessage('Dnsmasq configuration updated successfully', 'success'); + } else { + $status->addMessage('Dnsmasq configuration failed to be updated', 'danger'); + } + } else { + error_log('CSRF violation'); + } + } + + exec( 'pidof dnsmasq | wc -l',$dnsmasq ); + $dnsmasq_state = ($dnsmasq[0] > 0); + + if( isset( $_POST['startdhcpd'] ) ) { + if (CSRFValidate()) { + if ($dnsmasq_state) { + $status->addMessage('dnsmasq already running', 'info'); + } else { + $line = system('sudo /etc/init.d/dnsmasq start',$return); + exec( 'pidof dnsmasq | wc -l',$dnsmasq ); + if ($dnsmasq[0] == 0) { + $status->addMessage('Failed to start dnsmasq', 'danger'); + } else { + $status->addMessage('Successfully started dnsmasq', 'success'); + } + } + } else { + error_log('CSRF violation'); + } + } elseif( isset($_POST['stopdhcpd'] ) ) { + if (CSRFValidate()) { + if ($dnsmasq_state) { + $line = system('sudo /etc/init.d/dnsmasq stop',$return); + exec( 'pidof dnsmasq | wc -l',$dnsmasq ); + if ($dnsmasq[0] == 0) { + $status->addMessage('Successfully stopped dnsmasq', 'success'); + } else { + $status->addMessage('Failed to stop dnsmasq', 'danger'); + } + } else { + $status->addMessage('dnsmasq already stopped', 'info'); + } + } else { + error_log('CSRF violation'); + } + } else { + if( $dnsmasq_state ) { + $status->addMessage('Dnsmasq is running', 'success'); + } else { + $status->addMessage('Dnsmasq is not running', 'warning'); + } + } + + exec( 'cat '. RASPI_DNSMASQ_CONFIG, $return ); + $conf = ParseConfig($return); + $arrRange = explode( ",", $conf['dhcp-range'] ); + $RangeStart = $arrRange[0]; + $RangeEnd = $arrRange[1]; + $RangeMask = $arrRange[2]; + preg_match( '/([0-9]*)([a-z])/i', $arrRange[3], $arrRangeLeaseTime ); + + switch( $arrRangeLeaseTime[2] ) { + case "h": + $hselected = " selected"; + break; + case "m": + $mselected = " selected"; + break; + case "d": + $dselected = " selected"; + break; + } + + ?> +
+
+
+
Configure DHCP +
+ +
+

showMessages(); ?>

+ + + +
+
+

DHCP server settings

+
+ +
+
+ + +
+
+
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ + +
+
+ + + '; + } else { + echo''; + } + ?> +
+
+ +
+

Client list

+
+
+
+ Active DHCP leases +
+ +
+
+ + + + + + + + + + + + + ' . $lease_item . ''; + } + echo ''; + }; + ?> + + +
Expire timeMAC AddressIP AddressHost nameClient ID
+
+
+
+
+
+
+
+ +
+
+
+ + diff --git a/includes/functions.php b/includes/functions.php index bd32ec65..3e9f4ddb 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -551,188 +551,6 @@ function DisplayHostAPDConfig(){ Dnsmasq is not running
'; - } else { - $status = '
Dnsmasq is running
'; - } - ?> -
-
-
-
Configure DHCP -
- -
- - - -
-

-
-

DHCP server settings

-
-
-
- - -
-
-
-
- - -
-
- -
-
- - -
-
- -
-
- - -
-
- - -
-
- - - '; - } else { - echo ''; - } - ?> -
-
- -
-

Client list

-
-
-
- Active DHCP leases -
- -
-
- - - - - - - - - - - - - ' . $lease_item . ''; - } - echo ''; - }; - ?> - - -
Expire timeMAC AddressIP AddressHost nameClient ID
-
-
-
-
- /tmp/dhcpddata',$temp ); - system( 'sudo cp /tmp/dhcpddata '. RASPI_DNSMASQ_CONFIG, $return ); - - if( $return == 0 ) { - echo "Dnsmasq configuration updated successfully"; - } else { - echo "Dnsmasq configuration failed to be updated"; - } - } - - if( isset( $_POST['startdhcpd'] ) ) { - $line = system('sudo /etc/init.d/dnsmasq start',$return); - echo "Attempting to start dnsmasq"; - } - - if( isset($_POST['stopdhcpd'] ) ) { - $line = system('sudo /etc/init.d/dnsmasq stop',$return); - echo "Stopping dnsmasq"; - } - ?> -
-
-
- -
-
-
- Date: Sat, 9 Jul 2016 03:34:48 +0100 Subject: [PATCH 12/34] Fix checking dnsmasq starting/stopping --- includes/dhcp.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/includes/dhcp.php b/includes/dhcp.php index 48b122b3..6dfa93c9 100755 --- a/includes/dhcp.php +++ b/includes/dhcp.php @@ -36,11 +36,18 @@ function DisplayDHCPConfig() { $status->addMessage('dnsmasq already running', 'info'); } else { $line = system('sudo /etc/init.d/dnsmasq start',$return); - exec( 'pidof dnsmasq | wc -l',$dnsmasq ); - if ($dnsmasq[0] == 0) { + $n_tries = 0; + while ($n_tries < 5) { + exec( 'pidof dnsmasq | wc -l',$dnsmasq ); + if (end($dnsmasq) > 0) break; + sleep(1); + $n_tries += 1; + } + if (end($dnsmasq) == 0) { $status->addMessage('Failed to start dnsmasq', 'danger'); } else { $status->addMessage('Successfully started dnsmasq', 'success'); + $dnsmasq_state = true; } } } else { @@ -50,9 +57,16 @@ function DisplayDHCPConfig() { if (CSRFValidate()) { if ($dnsmasq_state) { $line = system('sudo /etc/init.d/dnsmasq stop',$return); - exec( 'pidof dnsmasq | wc -l',$dnsmasq ); - if ($dnsmasq[0] == 0) { + $n_tries = 0; + while ($n_tries < 5) { + exec( 'pidof dnsmasq | wc -l',$dnsmasq ); + if (end($dnsmasq) == 0) break; + sleep(1); + $n_tries += 1; + } + if (end($dnsmasq) == 0) { $status->addMessage('Successfully stopped dnsmasq', 'success'); + $dnsmasq_state = false; } else { $status->addMessage('Failed to stop dnsmasq', 'danger'); } From bfae272b0f20708d52b54a00a716668399d088ce Mon Sep 17 00:00:00 2001 From: Joseph Haig Date: Sat, 9 Jul 2016 03:46:21 +0100 Subject: [PATCH 13/34] Better checking --- includes/dhcp.php | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/includes/dhcp.php b/includes/dhcp.php index 6dfa93c9..d173547a 100755 --- a/includes/dhcp.php +++ b/includes/dhcp.php @@ -35,19 +35,12 @@ function DisplayDHCPConfig() { if ($dnsmasq_state) { $status->addMessage('dnsmasq already running', 'info'); } else { - $line = system('sudo /etc/init.d/dnsmasq start',$return); - $n_tries = 0; - while ($n_tries < 5) { - exec( 'pidof dnsmasq | wc -l',$dnsmasq ); - if (end($dnsmasq) > 0) break; - sleep(1); - $n_tries += 1; - } - if (end($dnsmasq) == 0) { - $status->addMessage('Failed to start dnsmasq', 'danger'); - } else { + exec('sudo /etc/init.d/dnsmasq start', $dnsmasq, $return); + if ($return == 0) { $status->addMessage('Successfully started dnsmasq', 'success'); $dnsmasq_state = true; + } else { + $status->addMessage('Failed to start dnsmasq', 'danger'); } } } else { @@ -56,15 +49,8 @@ function DisplayDHCPConfig() { } elseif( isset($_POST['stopdhcpd'] ) ) { if (CSRFValidate()) { if ($dnsmasq_state) { - $line = system('sudo /etc/init.d/dnsmasq stop',$return); - $n_tries = 0; - while ($n_tries < 5) { - exec( 'pidof dnsmasq | wc -l',$dnsmasq ); - if (end($dnsmasq) == 0) break; - sleep(1); - $n_tries += 1; - } - if (end($dnsmasq) == 0) { + exec('sudo /etc/init.d/dnsmasq stop', $dnsmasq, $return); + if ($return == 0) { $status->addMessage('Successfully stopped dnsmasq', 'success'); $dnsmasq_state = false; } else { From 2131842fe7c62304fe5968f8de00c75e60859a7f Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Wed, 27 Jul 2016 20:34:56 +0000 Subject: [PATCH 14/34] Tidy up some whitespace Sorry, I don't like tab indentation. (and I definitely don't like a mixture of tab and space indentation) --- index.php | 198 +++++++++++++++++++++++++++--------------------------- 1 file changed, 99 insertions(+), 99 deletions(-) diff --git a/index.php b/index.php index b53eec8c..16103132 100755 --- a/index.php +++ b/index.php @@ -59,8 +59,8 @@ $csrf_token = $_SESSION['csrf_token']; - - + + @@ -96,106 +96,106 @@ $csrf_token = $_SESSION['csrf_token']; - - + + -
- - - -
-
-

- RaspAP -

-
-
+
- -
+ +
+
+

+ RaspAP +

+
+
+ + +
@@ -217,5 +217,5 @@ $csrf_token = $_SESSION['csrf_token']; - + From bfb1332cdfdcd5269dbd5f60608b01408702dda2 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Wed, 27 Jul 2016 20:43:40 +0000 Subject: [PATCH 15/34] Move HostAPD into separate file --- includes/functions.php | 180 ----------------------------------------- includes/hostapd.php | 177 ++++++++++++++++++++++++++++++++++++++++ index.php | 1 + 3 files changed, 178 insertions(+), 180 deletions(-) create mode 100755 includes/hostapd.php diff --git a/includes/functions.php b/includes/functions.php index 3e9f4ddb..4a19a0c7 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -375,186 +375,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 ''; - }; - ?> - -
-
- -
-
-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 ''; + }; + ?> + +
+
+ +
+
+ diff --git a/index.php b/index.php index 16103132..2dea62f9 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']; From 15a4ece43390e27ebe71bdffa976f00165ce84a2 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Wed, 27 Jul 2016 20:48:27 +0000 Subject: [PATCH 16/34] Move function to save HostAPD config too --- includes/functions.php | 73 ---------------------------------------- includes/hostapd.php | 76 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 73 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 4a19a0c7..20944244 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -741,77 +741,4 @@ function DisplaySystem(){ /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."
"; - } - } -} ?> - diff --git a/includes/hostapd.php b/includes/hostapd.php index 30517e69..1d7e1408 100755 --- a/includes/hostapd.php +++ b/includes/hostapd.php @@ -175,3 +175,79 @@ function DisplayHostAPDConfig(){ + /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."
"; + } + } +} +?> + From 671016e6859fee3ae4e9bff81312b5e0d43e2cce Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Fri, 5 Aug 2016 15:50:05 +0100 Subject: [PATCH 17/34] Add CSRF to hostapd config And tidy things up a bit --- includes/functions.php | 29 ++++ includes/hostapd.php | 321 +++++++++++++++++++---------------------- 2 files changed, 180 insertions(+), 170 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 20944244..20a44d2f 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 diff --git a/includes/hostapd.php b/includes/hostapd.php index 1d7e1408..1eb4aa6b 100755 --- a/includes/hostapd.php +++ b/includes/hostapd.php @@ -1,19 +1,73 @@ /tmp/hostapddata", $return ); + system( "sudo cp /tmp/hostapddata " . RASPI_HOSTAPD_CONFIG, $return ); + + if( $return == 0 ) { + $status->addMessage('Wifi Hotspot settings saved', 'success'); + } else { + $status->addMessage('Wifi Hotspot settings failed to be saved', 'danger'); + } + } 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 = '
HostAPD is not running -
'; + $status->addMessage('HostAPD is not running', 'warning'); } else { - $status = '
HostAPD is running -
'; + $status->addMessage('HostAPD is running', 'success'); } $arrConfig = array(); @@ -29,148 +83,111 @@ function DisplayHostAPDConfig(){ }; ?>
-
+
-
Configure hotspot -
+
Configure hotspot
- +
+ -
-

+
+

showMessages(); ?>

- +

Basic settings

- -
-
- - -
-
-
-
- - -
-
-
-
- - -
-
-
-
- - -
-
-
-
+ +
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+

Security settings

-
- - -
-
-
-
- - -
-
-
-
- - -
-
+
+ + +
+
+
+
+ + 'TKIP+CCMP' but I am not yet sure what + * exactly is correct. + * At I read it, 'TKIP CCMP' would get written to the + * hostapd.conf file when it is saved but the correct option + * would only be selected if it reads 'TKIP+CCMP'. This is + * clearly broken. + * Now it is consistent, albeit possibly still broken. + */ + ?> + +
+
+
+
+ + +
+
-
-

Advanced settings

-
-
- - -
-
-
+
+

Advanced settings

+
+
+ + +
+
+
- - '; - } else { - echo ''; - }; - ?> - -
-
+ + '; + } 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']) ) { + if( 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 ); From bff9dfbbbc1ace6c68b467928856e420391c3d1b Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Fri, 5 Aug 2016 20:38:02 +0000 Subject: [PATCH 18/34] Some validation on POST data --- includes/admin.php | 2 +- includes/functions.php | 35 ++++++++++ includes/hostapd.php | 155 ++++++++++++++++++++--------------------- index.php | 2 +- 4 files changed, 111 insertions(+), 83 deletions(-) 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 20a44d2f..eb76ffca 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -770,4 +770,39 @@ function DisplaySystem(){ "; + } + } 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 index 1eb4aa6b..9d35785b 100755 --- a/includes/hostapd.php +++ b/includes/hostapd.php @@ -10,32 +10,16 @@ function DisplayHostAPDConfig(){ $status = new StatusMessages(); + $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'); + exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces); + + if( isset($_POST['SaveHostAPDSettings']) ) { if (CSRFValidate()) { - $config = 'driver=nl80211'.PHP_EOL - .'ctrl_interface='.RASPI_HOSTAPD_CTRL_INTERFACE.PHP_EOL - .'ctrl_interface_group=0'.PHP_EOL - .'beacon_int=100'.PHP_EOL - .'auth_algs=1'.PHP_EOL - .'wpa_key_mgmt=WPA-PSK'.PHP_EOL; - - $config .= "interface=".$_POST['interface'].PHP_EOL; - $config .= "ssid=".$_POST['ssid'].PHP_EOL; - $config .= "hw_mode=".$_POST['hw_mode'].PHP_EOL; - $config .= "channel=".$_POST['channel'].PHP_EOL; - $config .= "wpa=".$_POST['wpa'].PHP_EOL; - $config .='wpa_passphrase='.$_POST['wpa_passphrase'].PHP_EOL; - $config .="wpa_pairwise=".$_POST['wpa_pairwise'].PHP_EOL; - $config .="country_code=".$_POST['country_code']; - - exec( "echo '$config' > /tmp/hostapddata", $return ); - system( "sudo cp /tmp/hostapddata " . RASPI_HOSTAPD_CONFIG, $return ); - - if( $return == 0 ) { - $status->addMessage('Wifi Hotspot settings saved', 'success'); - } else { - $status->addMessage('Wifi Hotspot settings failed to be saved', 'danger'); - } + SaveHostAPDConfig($arrSecurity, $arrEncType, $arrChannel, $interfaces, $status); } else { error_log('CSRF violation'); } @@ -70,11 +54,6 @@ function DisplayHostAPDConfig(){ $status->addMessage('HostAPD is running', 'success'); } - $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) ; @@ -84,7 +63,7 @@ function DisplayHostAPDConfig(){ ?>
-
+
Configure hotspot
@@ -107,7 +86,6 @@ function DisplayHostAPDConfig(){
@@ -142,20 +120,6 @@ function DisplayHostAPDConfig(){
- 'TKIP+CCMP' but I am not yet sure what - * exactly is correct. - * At I read it, 'TKIP CCMP' would get written to the - * hostapd.conf file when it is saved but the correct option - * would only be selected if it reads 'TKIP+CCMP'. This is - * clearly broken. - * Now it is consistent, albeit possibly still broken. - */ - ?>
@@ -191,44 +155,73 @@ function DisplayHostAPDConfig(){
-"; - } - } 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 SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) { + // It should not be possible to send bad data for these fields so clearly + // someone is up to something if they fail. Fail silently. + if (!(array_key_exists($_POST['wpa'], $wpa_array) && array_key_exists($_POST['wpa_pairwise'], $enc_types) && in_array($_POST['hw_mode'], $modes))) { + error_log("Attempting to set hostapd config with wpa='".$_POST['wpa']."', wpa_pairwise='".$_POST['wpa_pairwise']."' and hw_mode='".$_POST['hw_mode']."'"); + return false; + } + if ((!filter_var($_POST['channel'], FILTER_VALIDATE_INT)) || intval($_POST['channel']) < 1 || intval($_POST['channel']) > 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 2dea62f9..2b066bef 100755 --- a/index.php +++ b/index.php @@ -187,7 +187,7 @@ $csrf_token = $_SESSION['csrf_token']; DisplayAuthConfig($config['admin_user'], $config['admin_pass']); break; case "save_hostapd_conf": - SaveHostAPDConfig(); + SaveTORAndVPNConfig(); break; case "system_info": DisplaySystem(); From 7406a5050c2a7fc9f74d39cdb4761fbc6d08354f Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Mon, 8 Aug 2016 12:31:19 +0000 Subject: [PATCH 19/34] Move system page into separate file --- includes/functions.php | 97 ----------------------------------------- includes/system.php | 98 ++++++++++++++++++++++++++++++++++++++++++ index.php | 1 + 3 files changed, 99 insertions(+), 97 deletions(-) create mode 100755 includes/system.php diff --git a/includes/functions.php b/includes/functions.php index eb76ffca..9d12869d 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -673,103 +673,6 @@ function DisplayTorProxyConfig(){ 1)? 's ':' '); } - if ($hours != 0) { $uptime .= $hours . ' hour' . (($hours > 1)? 's ':' '); } - if ($minutes != 0) { $uptime .= $minutes . ' minute' . (($minutes > 1)? 's ':' '); } - - // mem used - exec("free -m | awk '/Mem:/ { total=$2 } /buffers\/cache/ { used=$3 } END { print used/total*100}'", $memarray); - $memused = floor($memarray[0]); - if ($memused > 90) { $memused_status = "danger"; } - elseif ($memused > 75) { $memused_status = "warning"; } - elseif ($memused > 0) { $memused_status = "success"; } - - // cpu load - $cores = exec("grep -c ^processor /proc/cpuinfo"); - $loadavg = exec("awk '{print $1}' /proc/loadavg"); - $cpuload = floor(($loadavg * 100) / $cores); - if ($cpuload > 90) { $cpuload_status = "danger"; } - elseif ($cpuload > 75) { $cpuload_status = "warning"; } - elseif ($cpuload > 0) { $cpuload_status = "success"; } - - ?> -
-
-
-
System
-
- - System Rebooting Now!
'; - $result = shell_exec("sudo /sbin/reboot"); - } - if (isset($_POST['system_shutdown'])) { - echo '
System Shutting Down Now!
'; - $result = shell_exec("sudo /sbin/shutdown -h now"); - } - ?> - -
-
-
-
-

System Information

-
Hostname

-
Uptime


-
Memory Used
-
-
% -
-
-
CPU Load
-
-
% -
-
-
-
-
-
- -
- - - -
- -
-
-
-
- 1)? 's ':' '); } + if ($hours != 0) { $uptime .= $hours . ' hour' . (($hours > 1)? 's ':' '); } + if ($minutes != 0) { $uptime .= $minutes . ' minute' . (($minutes > 1)? 's ':' '); } + + // mem used + exec("free -m | awk '/Mem:/ { total=$2 } /buffers\/cache/ { used=$3 } END { print used/total*100}'", $memarray); + $memused = floor($memarray[0]); + if ($memused > 90) { $memused_status = "danger"; } + elseif ($memused > 75) { $memused_status = "warning"; } + elseif ($memused > 0) { $memused_status = "success"; } + + // cpu load + $cores = exec("grep -c ^processor /proc/cpuinfo"); + $loadavg = exec("awk '{print $1}' /proc/loadavg"); + $cpuload = floor(($loadavg * 100) / $cores); + if ($cpuload > 90) { $cpuload_status = "danger"; } + elseif ($cpuload > 75) { $cpuload_status = "warning"; } + elseif ($cpuload > 0) { $cpuload_status = "success"; } + + ?> +
+
+
+
System
+
+ + System Rebooting Now!
'; + $result = shell_exec("sudo /sbin/reboot"); + } + if (isset($_POST['system_shutdown'])) { + echo '
System Shutting Down Now!
'; + $result = shell_exec("sudo /sbin/shutdown -h now"); + } + ?> + +
+
+
+
+

System Information

+
Hostname

+
Uptime


+
Memory Used
+
+
% +
+
+
CPU Load
+
+
% +
+
+
+
+
+
+ +
+ + + +
+ +
+
+
+
+ diff --git a/index.php b/index.php index 2b066bef..f1fa7e6d 100755 --- a/index.php +++ b/index.php @@ -43,6 +43,7 @@ include_once( 'includes/authenticate.php' ); include_once( 'includes/admin.php' ); include_once( 'includes/dhcp.php' ); include_once( 'includes/hostapd.php' ); +include_once( 'includes/system.php' ); $output = $return = 0; $page = $_GET['page']; From bdfcf270a78da41b2ef0f5fd006b83cda81918c3 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Mon, 8 Aug 2016 12:38:15 +0000 Subject: [PATCH 20/34] Show Raspberry Pi revision information --- includes/system.php | 201 ++++++++++++++++++++++++++------------------ 1 file changed, 121 insertions(+), 80 deletions(-) diff --git a/includes/system.php b/includes/system.php index a94d7c8a..005110be 100755 --- a/includes/system.php +++ b/includes/system.php @@ -1,98 +1,139 @@ 'Model B Revision 1.0', + '0003' => 'Model B Revision 1.0 + ECN0001', + '0004' => 'Model B Revision 2.0 (256 MB)', + '0005' => 'Model B Revision 2.0 (256 MB)', + '0006' => 'Model B Revision 2.0 (256 MB)', + '0007' => 'Model A', + '0008' => 'Model A', + '0009' => 'Model A', + '000d' => 'Model B Revision 2.0 (512 MB)', + '000e' => 'Model B Revision 2.0 (512 MB)', + '000f' => 'Model B Revision 2.0 (512 MB)', + '0010' => 'Model B+', + '0013' => 'Model B+', + '0011' => 'Compute Module', + '0012' => 'Model A+', + 'a01041' => 'a01041', + 'a21041' => 'a21041', + '900092' => 'PiZero', + 'a02082' => 'Pi 3 Model B', + 'a22082' => 'Pi 3 Model B' + ); + exec('cat /proc/cpuinfo', $cpuinfo_array); + $rev = trim(array_pop(explode(':',array_pop(preg_grep("/^Revision/", $cpuinfo_array))))); + if (array_key_exists($rev, $revisions)) { + return $revisions[$rev]; + } else { + return 'Unknown Pi'; + } +} + /** * * */ function DisplaySystem(){ - // hostname - exec("hostname -f", $hostarray); - $hostname = $hostarray[0]; + // hostname + exec("hostname -f", $hostarray); + $hostname = $hostarray[0]; - // uptime - $uparray = explode(" ", exec("cat /proc/uptime")); - $seconds = round($uparray[0], 0); - $minutes = $seconds / 60; - $hours = $minutes / 60; - $days = floor($hours / 24); - $hours = floor($hours - ($days * 24)); - $minutes = floor($minutes - ($days * 24 * 60) - ($hours * 60)); - $uptime= ''; - if ($days != 0) { $uptime .= $days . ' day' . (($days > 1)? 's ':' '); } - if ($hours != 0) { $uptime .= $hours . ' hour' . (($hours > 1)? 's ':' '); } - if ($minutes != 0) { $uptime .= $minutes . ' minute' . (($minutes > 1)? 's ':' '); } + // uptime + $uparray = explode(" ", exec("cat /proc/uptime")); + $seconds = round($uparray[0], 0); + $minutes = $seconds / 60; + $hours = $minutes / 60; + $days = floor($hours / 24); + $hours = floor($hours - ($days * 24)); + $minutes = floor($minutes - ($days * 24 * 60) - ($hours * 60)); + $uptime= ''; + if ($days != 0) { $uptime .= $days . ' day' . (($days > 1)? 's ':' '); } + if ($hours != 0) { $uptime .= $hours . ' hour' . (($hours > 1)? 's ':' '); } + if ($minutes != 0) { $uptime .= $minutes . ' minute' . (($minutes > 1)? 's ':' '); } - // mem used - exec("free -m | awk '/Mem:/ { total=$2 } /buffers\/cache/ { used=$3 } END { print used/total*100}'", $memarray); - $memused = floor($memarray[0]); - if ($memused > 90) { $memused_status = "danger"; } - elseif ($memused > 75) { $memused_status = "warning"; } - elseif ($memused > 0) { $memused_status = "success"; } + // mem used + exec("free -m | awk '/Mem:/ { total=$2 } /buffers\/cache/ { used=$3 } END { print used/total*100}'", $memarray); + $memused = floor($memarray[0]); + if ($memused > 90) { $memused_status = "danger"; } + elseif ($memused > 75) { $memused_status = "warning"; } + elseif ($memused > 0) { $memused_status = "success"; } - // cpu load - $cores = exec("grep -c ^processor /proc/cpuinfo"); + // cpu load + $cores = exec("grep -c ^processor /proc/cpuinfo"); $loadavg = exec("awk '{print $1}' /proc/loadavg"); - $cpuload = floor(($loadavg * 100) / $cores); - if ($cpuload > 90) { $cpuload_status = "danger"; } - elseif ($cpuload > 75) { $cpuload_status = "warning"; } - elseif ($cpuload > 0) { $cpuload_status = "success"; } + $cpuload = floor(($loadavg * 100) / $cores); + if ($cpuload > 90) { $cpuload_status = "danger"; } + elseif ($cpuload > 75) { $cpuload_status = "warning"; } + elseif ($cpuload > 0) { $cpuload_status = "success"; } - ?> -
-
-
-
System
-
+ ?> +
+
+
+
System
+
- System Rebooting Now!
'; - $result = shell_exec("sudo /sbin/reboot"); - } - if (isset($_POST['system_shutdown'])) { - echo '
System Shutting Down Now!
'; - $result = shell_exec("sudo /sbin/shutdown -h now"); - } - ?> + System Rebooting Now!
'; + $result = shell_exec("sudo /sbin/reboot"); + } + if (isset($_POST['system_shutdown'])) { + echo '
System Shutting Down Now!
'; + $result = shell_exec("sudo /sbin/shutdown -h now"); + } + ?> -
-
-
-
-

System Information

-
Hostname

-
Uptime


-
Memory Used
-
-
% -
-
-
CPU Load
-
-
% -
-
-
-
-
-
+
+
+
+
+

System Information

+
Hostname

+
Pi Revision

+
Uptime


+
Memory Used
+
+
% +
+
+
CPU Load
+
+
% +
+
+
+
+
+
-
- - - -
+
+ + + +
-
-
-
-
- +
+
+
+ From 095e1afa8ce44fd934d48c1840a1bb4287b58fa9 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Mon, 8 Aug 2016 12:48:16 +0000 Subject: [PATCH 21/34] Move Dashboard function to separate file --- includes/dashboard.php | 147 +++++++++++++++++++++++++++++++++++++++++ includes/functions.php | 144 ---------------------------------------- index.php | 1 + 3 files changed, 148 insertions(+), 144 deletions(-) create mode 100755 includes/dashboard.php diff --git a/includes/dashboard.php b/includes/dashboard.php new file mode 100755 index 00000000..7fffa208 --- /dev/null +++ b/includes/dashboard.php @@ -0,0 +1,147 @@ +Interface is up +
'; + $wlan0up = true; + } else { + $status = '
Interface is down +
'; + } + + if( isset($_POST['ifdown_wlan0']) ) { + exec( 'ifconfig wlan0 | grep -i running | wc -l',$test ); + if($test[0] == 1) { + exec( 'sudo ifdown wlan0',$return ); + } else { + echo 'Interface already down'; + } + } elseif( isset($_POST['ifup_wlan0']) ) { + exec( 'ifconfig wlan0 | grep -i running | wc -l',$test ); + if($test[0] == 0) { + exec( 'sudo ifup wlan0',$return ); + } else { + echo 'Interface already up'; + } + } + ?> +
+
+
+
Dashboard
+
+

+
+
+
+
+

Interface Information

+
Interface Name
wlan0
+
IP Address

+
Subnet Mask

+
Mac Address


+ +

Interface Statistics

+
Received Packets

+
Received Bytes


+
Transferred Packets

+
Transferred Bytes

+
+
+
+ +
+
+
+

Wireless Information

+
Connected To

+
AP Mac Address

+
Bitrate

+
Transmit Power

+
Frequency


+
Link Quality
+
+
% +
+
+
Signal Level
+
+
% +
+
+
+
+
+
+ +
+
+
+ '; + } else { + echo ''; + } + ?> + +
+
+
+ +
+ +
+
+
+ diff --git a/includes/functions.php b/includes/functions.php index eb76ffca..bf1ab228 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -137,150 +137,6 @@ function ConvertToSecurity( $security ) { } } -/** -* -* -*/ -function DisplayDashboard(){ - - exec( 'ifconfig wlan0', $return ); - exec( 'iwconfig wlan0', $return ); - - $strWlan0 = implode( " ", $return ); - $strWlan0 = preg_replace( '/\s\s+/', ' ', $strWlan0 ); - - // Parse results from ifconfig/iwconfig - preg_match( '/HWaddr ([0-9a-f:]+)/i',$strWlan0,$result ); - $strHWAddress = $result[1]; - preg_match( '/inet addr:([0-9.]+)/i',$strWlan0,$result ); - $strIPAddress = $result[1]; - preg_match( '/Mask:([0-9.]+)/i',$strWlan0,$result ); - $strNetMask = $result[1]; - preg_match( '/RX packets:(\d+)/',$strWlan0,$result ); - $strRxPackets = $result[1]; - preg_match( '/TX packets:(\d+)/',$strWlan0,$result ); - $strTxPackets = $result[1]; - preg_match( '/RX bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result ); - $strRxBytes = $result[1]; - preg_match( '/TX Bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result ); - $strTxBytes = $result[1]; - preg_match( '/ESSID:\"([a-zA-Z0-9\s]+)\"/i',$strWlan0,$result ); - $strSSID = str_replace( '"','',$result[1] ); - preg_match( '/Access Point: ([0-9a-f:]+)/i',$strWlan0,$result ); - $strBSSID = $result[1]; - preg_match( '/Bit Rate=([0-9]+ Mb\/s)/i',$strWlan0,$result ); - $strBitrate = $result[1]; - preg_match( '/Tx-Power=([0-9]+ dBm)/i',$strWlan0,$result ); - $strTxPower = $result[1]; - preg_match( '/Link Quality=([0-9]+)/i',$strWlan0,$result ); - $strLinkQuality = $result[1]; - preg_match( '/Signal Level=([0-9]+)/i',$strWlan0,$result ); - $strSignalLevel = $result[1]; - preg_match('/Frequency:(\d+.\d+ GHz)/i',$strWlan0,$result); - $strFrequency = $result[1]; - - if(strpos( $strWlan0, "UP" ) !== false && strpos( $strWlan0, "RUNNING" ) !== false ) { - $status = '
Interface is up -
'; - $wlan0up = true; - } else { - $status = '
Interface is down -
'; - } - - if( isset($_POST['ifdown_wlan0']) ) { - exec( 'ifconfig wlan0 | grep -i running | wc -l',$test ); - if($test[0] == 1) { - exec( 'sudo ifdown wlan0',$return ); - } else { - echo 'Interface already down'; - } - } elseif( isset($_POST['ifup_wlan0']) ) { - exec( 'ifconfig wlan0 | grep -i running | wc -l',$test ); - if($test[0] == 0) { - exec( 'sudo ifup wlan0',$return ); - } else { - echo 'Interface already up'; - } - } - ?> -
-
-
-
Dashboard
-
-

-
-
-
-
-

Interface Information

-
Interface Name
wlan0
-
IP Address

-
Subnet Mask

-
Mac Address


- -

Interface Statistics

-
Received Packets

-
Received Bytes


-
Transferred Packets

-
Transferred Bytes

-
-
-
- -
-
-
-

Wireless Information

-
Connected To

-
AP Mac Address

-
Bitrate

-
Transmit Power

-
Frequency


-
Link Quality
-
-
% -
-
-
Signal Level
-
-
% -
-
-
-
-
-
- -
-
-
- '; - } else { - echo ''; - } - ?> - -
-
-
- -
- -
-
-
- Date: Mon, 8 Aug 2016 21:37:44 +0100 Subject: [PATCH 22/34] Change tabs to spaces --- includes/dashboard.php | 256 ++++++++++++++++++++--------------------- 1 file changed, 128 insertions(+), 128 deletions(-) diff --git a/includes/dashboard.php b/includes/dashboard.php index 7fffa208..981833c7 100755 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -6,142 +6,142 @@ */ function DisplayDashboard(){ - exec( 'ifconfig wlan0', $return ); - exec( 'iwconfig wlan0', $return ); + exec( 'ifconfig wlan0', $return ); + exec( 'iwconfig wlan0', $return ); - $strWlan0 = implode( " ", $return ); - $strWlan0 = preg_replace( '/\s\s+/', ' ', $strWlan0 ); + $strWlan0 = implode( " ", $return ); + $strWlan0 = preg_replace( '/\s\s+/', ' ', $strWlan0 ); - // Parse results from ifconfig/iwconfig - preg_match( '/HWaddr ([0-9a-f:]+)/i',$strWlan0,$result ); - $strHWAddress = $result[1]; - preg_match( '/inet addr:([0-9.]+)/i',$strWlan0,$result ); - $strIPAddress = $result[1]; - preg_match( '/Mask:([0-9.]+)/i',$strWlan0,$result ); - $strNetMask = $result[1]; - preg_match( '/RX packets:(\d+)/',$strWlan0,$result ); - $strRxPackets = $result[1]; - preg_match( '/TX packets:(\d+)/',$strWlan0,$result ); - $strTxPackets = $result[1]; - preg_match( '/RX bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result ); - $strRxBytes = $result[1]; - preg_match( '/TX Bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result ); - $strTxBytes = $result[1]; - preg_match( '/ESSID:\"([a-zA-Z0-9\s]+)\"/i',$strWlan0,$result ); - $strSSID = str_replace( '"','',$result[1] ); - preg_match( '/Access Point: ([0-9a-f:]+)/i',$strWlan0,$result ); - $strBSSID = $result[1]; - preg_match( '/Bit Rate=([0-9]+ Mb\/s)/i',$strWlan0,$result ); - $strBitrate = $result[1]; - preg_match( '/Tx-Power=([0-9]+ dBm)/i',$strWlan0,$result ); - $strTxPower = $result[1]; - preg_match( '/Link Quality=([0-9]+)/i',$strWlan0,$result ); - $strLinkQuality = $result[1]; - preg_match( '/Signal Level=([0-9]+)/i',$strWlan0,$result ); - $strSignalLevel = $result[1]; - preg_match('/Frequency:(\d+.\d+ GHz)/i',$strWlan0,$result); - $strFrequency = $result[1]; + // Parse results from ifconfig/iwconfig + preg_match( '/HWaddr ([0-9a-f:]+)/i',$strWlan0,$result ); + $strHWAddress = $result[1]; + preg_match( '/inet addr:([0-9.]+)/i',$strWlan0,$result ); + $strIPAddress = $result[1]; + preg_match( '/Mask:([0-9.]+)/i',$strWlan0,$result ); + $strNetMask = $result[1]; + preg_match( '/RX packets:(\d+)/',$strWlan0,$result ); + $strRxPackets = $result[1]; + preg_match( '/TX packets:(\d+)/',$strWlan0,$result ); + $strTxPackets = $result[1]; + preg_match( '/RX bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result ); + $strRxBytes = $result[1]; + preg_match( '/TX Bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result ); + $strTxBytes = $result[1]; + preg_match( '/ESSID:\"([a-zA-Z0-9\s]+)\"/i',$strWlan0,$result ); + $strSSID = str_replace( '"','',$result[1] ); + preg_match( '/Access Point: ([0-9a-f:]+)/i',$strWlan0,$result ); + $strBSSID = $result[1]; + preg_match( '/Bit Rate=([0-9]+ Mb\/s)/i',$strWlan0,$result ); + $strBitrate = $result[1]; + preg_match( '/Tx-Power=([0-9]+ dBm)/i',$strWlan0,$result ); + $strTxPower = $result[1]; + preg_match( '/Link Quality=([0-9]+)/i',$strWlan0,$result ); + $strLinkQuality = $result[1]; + preg_match( '/Signal Level=([0-9]+)/i',$strWlan0,$result ); + $strSignalLevel = $result[1]; + preg_match('/Frequency:(\d+.\d+ GHz)/i',$strWlan0,$result); + $strFrequency = $result[1]; - if(strpos( $strWlan0, "UP" ) !== false && strpos( $strWlan0, "RUNNING" ) !== false ) { - $status = '
Interface is up -
'; - $wlan0up = true; - } else { - $status = '
Interface is down -
'; - } + if(strpos( $strWlan0, "UP" ) !== false && strpos( $strWlan0, "RUNNING" ) !== false ) { + $status = '
Interface is up +
'; + $wlan0up = true; + } else { + $status = '
Interface is down +
'; + } - if( isset($_POST['ifdown_wlan0']) ) { - exec( 'ifconfig wlan0 | grep -i running | wc -l',$test ); - if($test[0] == 1) { - exec( 'sudo ifdown wlan0',$return ); - } else { - echo 'Interface already down'; - } - } elseif( isset($_POST['ifup_wlan0']) ) { - exec( 'ifconfig wlan0 | grep -i running | wc -l',$test ); - if($test[0] == 0) { - exec( 'sudo ifup wlan0',$return ); - } else { - echo 'Interface already up'; - } - } - ?> -
-
-
-
Dashboard
-
-

-
-
-
-
-

Interface Information

-
Interface Name
wlan0
-
IP Address

-
Subnet Mask

-
Mac Address


+ if( isset($_POST['ifdown_wlan0']) ) { + exec( 'ifconfig wlan0 | grep -i running | wc -l',$test ); + if($test[0] == 1) { + exec( 'sudo ifdown wlan0',$return ); + } else { + echo 'Interface already down'; + } + } elseif( isset($_POST['ifup_wlan0']) ) { + exec( 'ifconfig wlan0 | grep -i running | wc -l',$test ); + if($test[0] == 0) { + exec( 'sudo ifup wlan0',$return ); + } else { + echo 'Interface already up'; + } + } + ?> +
+
+
+
Dashboard
+
+

+
+
+
+
+

Interface Information

+
Interface Name
wlan0
+
IP Address

+
Subnet Mask

+
Mac Address


-

Interface Statistics

-
Received Packets

-
Received Bytes


-
Transferred Packets

-
Transferred Bytes

-
-
-
+

Interface Statistics

+
Received Packets

+
Received Bytes


+
Transferred Packets

+
Transferred Bytes

+
+
+
-
-
-
-

Wireless Information

-
Connected To

-
AP Mac Address

-
Bitrate

-
Transmit Power

-
Frequency


-
Link Quality
-
-
% -
-
-
Signal Level
-
-
% -
-
-
-
-
-
+
+
+
+

Wireless Information

+
Connected To

+
AP Mac Address

+
Bitrate

+
Transmit Power

+
Frequency


+
Link Quality
+
+
% +
+
+
Signal Level
+
+
% +
+
+
+
+
+
-
-
-
- '; - } else { - echo ''; - } - ?> - -
-
-
+
+
+
+ '; + } else { + echo ''; + } + ?> + +
+
+
-
- -
-
-
- + +
+
+
+ From fe3b0e9513f16db464b2ced887de1a8a42272ba3 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Fri, 12 Aug 2016 17:00:43 +0000 Subject: [PATCH 23/34] 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 00000000..4044dd74 --- /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 9d12869d..21c88eac 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 24/34] 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 4044dd74..a5e2aa15 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 25/34] Split out php from html --- includes/configure_client.php | 262 ++++++++++++++++++++-------------- 1 file changed, 157 insertions(+), 105 deletions(-) diff --git a/includes/configure_client.php b/includes/configure_client.php index a5e2aa15..2b1d1188 100755 --- a/includes/configure_client.php +++ b/includes/configure_client.php @@ -5,119 +5,171 @@ * */ function DisplayWPAConfig(){ - $status = ''; + $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 = escapeshellarg( $_POST['protocol'.$x] ); + $psk = escapeshellarg( $_POST['psk'.$x] ); + + 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 ) { + $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); ?> + +
-
-
Configure client -
- -
- -

Client settings

+
+
Configure client
+ +
+

showMessages(); ?>

+

Client settings

- + +
+
+
+ + + +
+
+

Networks found

+
+ + + + + + + + + + + + + + + + + + + + + +
SSIDChannelSignalSecurity
+
+ +
+
- $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] ); - $psk = escapeshellarg( $_POST['psk'.$x] ); - - 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 -
'; - } - - // 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 21:22:21 +0000 Subject: [PATCH 26/34] Allow open networks --- dist/js/functions.js | 36 ++++++----- includes/configure_client.php | 109 ++++++++++++++++++++++++++-------- 2 files changed, 105 insertions(+), 40 deletions(-) diff --git a/dist/js/functions.js b/dist/js/functions.js index 5adf581a..3588a3b5 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 2b1d1188..7e8d3478 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]); + + + + // 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; + } + } } } - - $numSSIDs = count($ssid); - ?> - +?>
@@ -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 27/34] 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 f3b83e2f..9d7cbe2f 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 7e8d3478..62331951 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

-
-
- - -
-
-
- - - + + + + + + + + + + + + + + $network) { ?> + + + + + + + + + + + + + + + + + + + + +
SSIDChannelSecurityPassphrase
+ + + X--- + + +
+ + /> + + + + />
- -

Networks found

-
- - - - - - - - - - - - - - - - - - - - - -
SSIDChannelSignalSecurity
-
- - - - -
-
-
- -
- -
-
-
- -
-
-
-
- - -
-
-
-
- - -
-
- -
-
- - -
-
- -
-
- -
-
- -
-
-
- - - - -
-
- -
+
+
+
diff --git a/includes/functions.php b/includes/functions.php index 6538f579..739ebe18 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 dbf1695e..917f3e43 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 28/34] 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 3588a3b5..e9164308 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 62331951..cc9a1b5b 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 29/34] 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 cc9a1b5b..ba2e9e6f 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) { ?> - - - - - + + + + + + + + From f730aa1ffbaa3585bfa4342ef3fc0ea5a0c13d4f Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Mon, 15 Aug 2016 12:25:20 +0000 Subject: [PATCH 30/34] Use status class --- includes/dashboard.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/dashboard.php b/includes/dashboard.php index 981833c7..90b4fb14 100755 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -6,6 +6,8 @@ */ function DisplayDashboard(){ + $status = new StatusMessages(); + exec( 'ifconfig wlan0', $return ); exec( 'iwconfig wlan0', $return ); @@ -43,12 +45,10 @@ function DisplayDashboard(){ $strFrequency = $result[1]; if(strpos( $strWlan0, "UP" ) !== false && strpos( $strWlan0, "RUNNING" ) !== false ) { - $status = '
Interface is up -
'; + $status->addMessage('Interface is up', 'success'); $wlan0up = true; } else { - $status = '
Interface is down -
'; + $status->addMessage('Interface is down', 'warning'); } if( isset($_POST['ifdown_wlan0']) ) { @@ -72,7 +72,7 @@ function DisplayDashboard(){
Dashboard
-

+

showMessages(); ?>

From 66d6f3766c58c33a1e2113f5964c932b264dc2e7 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Mon, 15 Aug 2016 12:45:37 +0000 Subject: [PATCH 31/34] Find bitrate and signal level correctly Also, display signal level as just a number rather than a graph --- includes/dashboard.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/includes/dashboard.php b/includes/dashboard.php index 90b4fb14..69ed7912 100755 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -33,13 +33,13 @@ function DisplayDashboard(){ $strSSID = str_replace( '"','',$result[1] ); preg_match( '/Access Point: ([0-9a-f:]+)/i',$strWlan0,$result ); $strBSSID = $result[1]; - preg_match( '/Bit Rate=([0-9]+ Mb\/s)/i',$strWlan0,$result ); + preg_match( '/Bit Rate=([0-9\.]+ Mb\/s)/i',$strWlan0,$result ); $strBitrate = $result[1]; preg_match( '/Tx-Power=([0-9]+ dBm)/i',$strWlan0,$result ); $strTxPower = $result[1]; preg_match( '/Link Quality=([0-9]+)/i',$strWlan0,$result ); $strLinkQuality = $result[1]; - preg_match( '/Signal Level=([0-9]+)/i',$strWlan0,$result ); + preg_match( '/Signal level=(-?[0-9]+ dBm)/i',$strWlan0,$result ); $strSignalLevel = $result[1]; preg_match('/Frequency:(\d+.\d+ GHz)/i',$strWlan0,$result); $strFrequency = $result[1]; @@ -99,6 +99,7 @@ function DisplayDashboard(){
Connected To

AP Mac Address

Bitrate

+
Signal Level

Transmit Power

Frequency


Link Quality
@@ -108,15 +109,7 @@ function DisplayDashboard(){ aria-valuenow="" aria-valuemin="0" aria-valuemax="100" style="width: %;">%
-
-
Signal Level
-
-
% -
-
+
From 9ceaa905306a1a63a8ec17771f06daef3548968e Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Wed, 31 Aug 2016 09:45:00 +0100 Subject: [PATCH 32/34] Change lighttpd default directory --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9d7cbe2f..fa4c3a59 100644 --- a/README.md +++ b/README.md @@ -55,18 +55,21 @@ www-data ALL=(ALL) NOPASSWD:/sbin/ifdown wlan0,/sbin/ifup wlan0,/bin/cat /etc/wp www-data ALL=(ALL) NOPASSWD:/sbin/wpa_cli reconfigure ``` -Once those modifications are done, git clone the files to `/var/www`. +Once those modifications are done, git clone the files to `/var/www/html`. +'''Note,''' for older versions of Raspbian (before Jessie, May 2016) use +`var/www` instead. ```sh -sudo git clone https://github.com/billz/raspap-webgui /var/www +sudo rm -rf /var/www/html +sudo git clone https://github.com/billz/raspap-webgui /var/www/html ``` Set the files ownership to `www-data` user. ```sh -sudo chown -R www-data:www-data /var/www +sudo chown -R www-data:www-data /var/www/html ``` Move the RaspAP configuration file to the correct location ```sh sudo mkdir /etc/raspap -sudo mv /var/www/raspap.php /etc/raspap/ +sudo mv /var/www/html/raspap.php /etc/raspap/ sudo chown -R www-data:www-data /etc/raspap ``` Reboot and it should be up and running! From 0a9b2363de6863dd8d2023b47ef6642bf1d7e18e Mon Sep 17 00:00:00 2001 From: Joseph Haig Date: Wed, 31 Aug 2016 09:48:02 +0100 Subject: [PATCH 33/34] Correct markdown for bold --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fa4c3a59..f8892d0e 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ www-data ALL=(ALL) NOPASSWD:/sbin/wpa_cli reconfigure ``` Once those modifications are done, git clone the files to `/var/www/html`. -'''Note,''' for older versions of Raspbian (before Jessie, May 2016) use +**Note,** for older versions of Raspbian (before Jessie, May 2016) use `var/www` instead. ```sh sudo rm -rf /var/www/html From dc6bdc028ca085883876f64cfcc83f8cbc3b58bb Mon Sep 17 00:00:00 2001 From: Joseph Haig Date: Wed, 31 Aug 2016 09:48:56 +0100 Subject: [PATCH 34/34] Add missing / --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f8892d0e..039044b3 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ www-data ALL=(ALL) NOPASSWD:/sbin/wpa_cli reconfigure Once those modifications are done, git clone the files to `/var/www/html`. **Note,** for older versions of Raspbian (before Jessie, May 2016) use -`var/www` instead. +`/var/www` instead. ```sh sudo rm -rf /var/www/html sudo git clone https://github.com/billz/raspap-webgui /var/www/html