From 3b043950deaeecbbbd3e41ffaf560c296490e4ac Mon Sep 17 00:00:00 2001 From: Joseph Haig Date: Sat, 9 Jul 2016 01:26:13 +0100 Subject: [PATCH] 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"; - } - ?> -
-
-
- -
-
-
-