1
0
mirror of https://github.com/billz/raspap-webgui.git synced 2023-10-10 13:37:24 +02:00

Merge pull request #23 from jrmhaig/client

Redo wireless client configuration
This commit is contained in:
Bill Zimmerman 2016-08-16 11:36:24 -07:00 committed by GitHub
commit d47a939649
6 changed files with 259 additions and 241 deletions

View File

@ -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`.

73
dist/js/functions.js vendored
View File

@ -1,74 +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 += '<div id="Networkbox'+Networks+'" class="Networkboxes"><div class="row"><div class="col-lg-12"><h4>Network '+Networks+'</h4> \
<div class="row"><div class="form-group col-md-4"><label for="code">SSID</label><input type="text" class="form-control" name="ssid'+Networks+'" onkeyup="CheckSSID(this)"></div></div> \
<div class="row"><div class="form-group col-md-4"><label for="code">PSK</label><input type="password" class="form-control" name="psk'+Networks+'" onkeyup="CheckPSK(this)"></div></div> \
<div class="row"><div class="form-group col-md-4"><input type="button" class="btn btn-outline btn-primary" value="Cancel" onClick="DeleteNetwork('+Networks+')" /></div></div>';
Networks++;
document.getElementById('Networks').value=Networks;
}
function AddScanned(network) {
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 += '<div id="Networkbox'+Networks+'" class="Networkboxes"><div class="col-lg-12"><h4>Network '+Networks+'</h4> \
<div class="row"><div class="form-group col-md-4"><label for="code">SSID</</label><input type="text" class="form-control" name="ssid'+Networks+'" id="ssid'+Networks+'" onkeyup="CheckSSID(this)"></div></div> \
<div class="row"><div class="form-group col-md-4"><label for="code">PSK</label><input type="password" class="form-control" name="psk'+Networks+'" onkeyup="CheckPSK(this)"></div></div> \
<div class="row"><div class="form-group col-md-4"><input type="button" class="btn btn-outline btn-primary" value="Cancel" onClick="DeleteNetwork('+Networks+')" /></div></div>';
document.getElementById('ssid'+Networks).value = network;
if(existing == 0) {
Networks++
document.getElementById('Networks').value = Networks;
}
}
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) {
function CheckPSK(psk, id) {
if(psk.value.length < 8 || psk.value.length > 63) {
psk.style.background='#FFD0D0';
document.getElementById('Save').disabled = true;
document.getElementById(id).disabled = true;
} else {
psk.style.background='#D0FFD0';
document.getElementById('Save').disabled = false;
document.getElementById(id).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;
}

213
includes/configure_client.php Executable file
View File

@ -0,0 +1,213 @@
<?php
/**
*
*
*/
function DisplayWPAConfig(){
$status = new StatusMessages();
$scanned_networks = array();
// Find currently configured networks
exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $known_return);
$network = null;
$ssid = null;
foreach($known_return as $line) {
if (preg_match('/network\s*=/', $line)) {
$network = array('visible' => false, 'configured' => true, 'connected' => false);
} elseif ($network !== null) {
if (preg_match('/^\s*}\s*$/', $line)) {
$networks[$ssid] = $network;
$network = null;
$ssid = null;
} elseif ($lineArr = preg_split('/\s*=\s*/', trim($line))) {
switch(strtolower($lineArr[0])) {
case 'ssid':
$ssid = trim($lineArr[1], '"');
break;
case 'psk':
if (array_key_exists('passphrase', $network)) {
break;
}
case '#psk':
$network['protocol'] = 'WPA';
case 'wep_key0': // Untested
$network['passphrase'] = trim($lineArr[1], '"');
break;
case 'key_mgmt':
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,
'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;
}
}
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-signal fa-fw"></i> Configure client</div>
<!-- /.panel-heading -->
<div class="panel-body">
<p><?php $status->showMessages(); ?></p>
<h4>Client settings</h4>
<form method="POST" action="?page=wpa_conf" name="wpa_conf_form">
<?php CSRFToken() ?>
<input type="hidden" name="client_settings" ?>
<table class="table table-responsive table-striped">
<tr>
<th></th>
<th>SSID</th>
<th>Channel</th>
<th>Security</th>
<th>Passphrase</th>
<th></th>
</tr>
<?php $index = 0; ?>
<?php foreach ($networks as $ssid => $network) { ?>
<tr>
<td>
<?php if ($network['configured']) { ?>
<i class="fa fa-check-circle fa-fw"></i>
<?php } ?>
<?php if ($network['connected']) { ?>
<i class="fa fa-exchange fa-fw"></i>
<?php } ?>
</td>
<td>
<input type="hidden" name="ssid<?php echo $index ?>" value="<?php echo $ssid ?>" />
<?php echo $ssid ?>
</td>
<?php if ($network['visible']) { ?>
<td><?php echo $network['channel'] ?></td>
<?php } else { ?>
<td><span class="label label-warning">X</span></td>
<?php } ?>
<td><input type="hidden" name="protocol<?php echo $index ?>" value="<?php echo $network['protocol'] ?>" /><?php echo $network['protocol'] ?></td>
<?php if ($network['protocol'] === 'Open') { ?>
<td><input type="hidden" name="passphrase<?php echo $index ?>" value="" />---</td>
<?php } else { ?>
<td><input type="text" class="form-control" name="passphrase<?php echo $index ?>" value="<?php echo $network['passphrase'] ?>" onKeyUp="CheckPSK(this, 'update<?php echo $index?>')" />
<?php } ?>
<td>
<div class="btn-group btn-block">
<?php if ($network['configured']) { ?>
<input type="submit" class="col-md-6 btn btn-warning" value="Update" id="update<?php echo $index ?>" name="update<?php echo $index ?>"<?php echo ($network['protocol'] === 'Open' ? ' disabled' : '')?> />
<?php } else { ?>
<input type="submit" class="col-md-6 btn btn-info" value="Add" id="update<?php echo $index ?>" name="update<?php echo $index ?>" <?php echo ($network['protocol'] === 'Open' ? '' : ' disabled')?> />
<?php } ?>
<input type="submit" class="col-md-6 btn btn-danger" value="Delete" name="delete<?php echo $index ?>"<?php echo ($network['configured'] ? '' : ' disabled')?> />
</div>
</td>
</tr>
<?php $index += 1; ?>
<?php } ?>
</table>
</form>
</div><!-- ./ Panel body -->
<div class="panel-footer"><strong>Note,</strong> WEP access points appear as 'Open'. RaspAP does not currently support connecting to WEP.</div>
</div><!-- /.panel-primary -->
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
?>

View File

@ -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;
}
}
/**
@ -85,18 +90,12 @@ function ParseConfig( $arrConfig ) {
* @return $channel
*/
function ConvertToChannel( $freq ) {
$base = 2412;
$channel = 1;
for( $x = 0; $x < 13; $x++ ) {
if( $freq != $base ) {
$base = $base + 5;
$channel++;
} else {
$channel = ($freq - 2407)/5;
if ($channel > 0 && $channel < 14) {
return $channel;
} else {
return 'Invalid Channel';
}
}
return "Invalid Channel";
}
/**
@ -105,35 +104,27 @@ function ConvertToChannel( $freq ) {
* @return string
*/
function ConvertToSecurity( $security ) {
$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;
}
}
}
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;
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('<br />', $options);
}
}
@ -281,129 +272,6 @@ function DisplayDashboard(){
<?php
}
/**
*
*
*/
function DisplayWPAConfig(){
$status = '';
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-signal fa-fw"></i> Configure client
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<?php echo $status; ?>
<h4>Client settings</h4>
<div class="row">
<div class="col-lg-12">
<?php
// save WPA settings
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] );
$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 '<div class="alert alert-success alert-dismissable">Wifi settings updated successfully
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
} else {
echo '<div class="alert alert-danger alert-dismissable">Wifi settings failed to be updated
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
}
// 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 '<form method="POST" action="?page=wpa_conf" id="wpa_conf_form"><input type="hidden" id="Networks" name="Networks" /><div class="network" id="networkbox"></div>';
echo '<div class="row"><div class="col-lg-6"><input type="submit" class="btn btn-primary" value="Scan for networks" name="Scan" /> <input type="button" class="btn btn-primary" value="Add network" onClick="AddNetwork();" /> <input type="submit" class="btn btn-primary" value="Save" name="SaveWPAPSKSettings" onmouseover="UpdateNetworks(this)" id="Save" disabled /></div></div>';
echo '<h4>Networks found</h4><div class="table-responsive"><table class="table table-hover">';
echo '<thead><tr><th></th><th>SSID</th><th>Channel</th><th>Signal</th><th>Security</th></tr></thead><tbody>';
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 '<tr><td><input type="button" class="btn btn-outline btn-primary" value="Connect" onClick="AddScanned(\''.$ssid.'\')" /></td> <td><strong>' . $ssid . "</strong></td> <td>" . $channel . "</td><td>" . $signal . "</td><td>". ConvertToSecurity($security) ."</td></tr>";
}
echo '</tbody></table>';
} 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 = '<form method="POST" action="?page=wpa_conf" id="wpa_conf_form"><input type="hidden" id="Networks" name="Networks" /><div class="network" id="networkbox">';
if ( $numSSIDs > 0 ) {
for( $ssids = 0; $ssids < $numSSIDs; $ssids++ ) {
$output .= '<div id="Networkbox'.$ssids.'" class="NetworkBoxes">
<div class="row"><div class="form-group col-md-4"><label for="code">Network '.$ssids.'</label></div></div>
<div class="row"><div class="form-group col-md-4"><label for="code" id="lssid0">SSID</label><input type="text" class="form-control" id="ssid0" name="ssid'.$ssids.'" value="'.$ssid[$ssids].'" onkeyup="CheckSSID(this)" /></div></div>
<div class="row"><div class="form-group col-md-4"><label for="code" id="lpsk0">PSK</label><input type="password" class="form-control" id="psk0" name="psk'.$ssids.'" value="'.$psk[$ssids].'" onkeyup="CheckPSK(this)" /></div></div>
<div class="row"><div class="form-group col-md-4"><input type="button" class="btn btn-outline btn-primary" value="Delete" onClick="DeleteNetwork('.$ssids.')" /></div></div>';
}
$output .= '</div><!-- /#Networkbox -->';
} else {
$status = '<div class="alert alert-warning alert-dismissable">Not connected
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
}
$output .= '<div class="row"><div class="col-lg-6"><input type="submit" class="btn btn-primary" value="Scan for networks" name="Scan" /> <input type="button" class="btn btn-primary" value="Add network" onClick="AddNetwork();" /> <input type="submit" class="btn btn-primary" value="Save" name="SaveWPAPSKSettings" onmouseover="UpdateNetworks(this)" id="Save" disabled />';
$output .= '</form>';
echo $output;
}
?>
<script type="text/Javascript">UpdateNetworks(this)</script>
</form>
</div><!-- ./ Panel body -->
</div><!-- /.panel-primary -->
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
function DisplayOpenVPNConfig() {
exec( 'cat '. RASPI_OPENVPN_CLIENT_CONFIG, $returnClient );

View File

@ -44,6 +44,7 @@ include_once( 'includes/admin.php' );
include_once( 'includes/dhcp.php' );
include_once( 'includes/hostapd.php' );
include_once( 'includes/system.php' );
include_once( 'includes/configure_client.php' );
$output = $return = 0;
$page = $_GET['page'];

View File

@ -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"
}