This commit is contained in:
billz 2018-10-11 21:16:17 +00:00
commit bba1e172d9
2 changed files with 85 additions and 39 deletions

View file

@ -89,8 +89,13 @@ function isAssoc($arr) {
* If $options is an associative array this should be the key * If $options is an associative array this should be the key
* *
*/ */
function SelectorOptions($name, $options, $selected = null) { function SelectorOptions($name, $options, $selected = null, $id = null) {
echo '<select class="form-control" name="'.htmlspecialchars($name, ENT_QUOTES).'">' , PHP_EOL; echo '<select class="form-control" name="'.htmlspecialchars($name, ENT_QUOTES).'"';
if (isset($id)) {
echo ' id="' . htmlspecialchars($id, ENT_QUOTES) .'"';
}
echo '>' , PHP_EOL;
foreach ( $options as $opt => $label) { foreach ( $options as $opt => $label) {
$select = ''; $select = '';
$key = isAssoc($options) ? $opt : $label; $key = isAssoc($options) ? $opt : $label;

View file

@ -6,22 +6,21 @@ include_once( 'includes/status_messages.php' );
* *
* *
*/ */
function DisplayHostAPDConfig(){ function DisplayHostAPDConfig()
{
$status = new StatusMessages(); $status = new StatusMessages();
$arrHostapdConf = parse_ini_file('/etc/raspap/hostapd.ini'); $arrHostapdConf = parse_ini_file('/etc/raspap/hostapd.ini');
$arrConfig = array(); $arrConfig = array();
$arrChannel = array('a','b','g'); $arr80211Standard = array('a','b','g','n');
$arrSecurity = array( 1 => 'WPA', 2 => 'WPA2',3=> 'WPA+WPA2'); $arrSecurity = array( 1 => 'WPA', 2 => 'WPA2',3=> 'WPA+WPA2');
$arrEncType = array('TKIP' => 'TKIP', 'CCMP' => 'CCMP', 'TKIP CCMP' => 'TKIP+CCMP'); $arrEncType = array('TKIP' => 'TKIP', 'CCMP' => 'CCMP', 'TKIP CCMP' => 'TKIP+CCMP');
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces); exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
if( isset($_POST['SaveHostAPDSettings']) ) { if( isset($_POST['SaveHostAPDSettings']) ) {
if (CSRFValidate()) { if (CSRFValidate()) {
SaveHostAPDConfig($arrSecurity, $arrEncType, $arrChannel, $interfaces, $status); SaveHostAPDConfig($arrSecurity, $arrEncType, $arr80211Standard, $interfaces, $status);
} else { } else {
error_log('CSRF violation'); error_log('CSRF violation');
} }
@ -47,7 +46,7 @@ function DisplayHostAPDConfig(){
} }
} }
exec( 'cat '. RASPI_HOSTAPD_CONFIG, $return ); exec( 'cat '. RASPI_HOSTAPD_CONFIG, $hostapdconfig );
exec( 'pidof hostapd | wc -l', $hostapdstatus); exec( 'pidof hostapd | wc -l', $hostapdstatus);
if( $hostapdstatus[0] == 0 ) { if( $hostapdstatus[0] == 0 ) {
@ -56,9 +55,13 @@ function DisplayHostAPDConfig(){
$status->addMessage('HostAPD is running', 'success'); $status->addMessage('HostAPD is running', 'success');
} }
foreach( $return as $a ) { foreach( $hostapdconfig as $hostapdconfigline ) {
if( $a[0] != "#" ) { if (strlen($hostapdconfigline) === 0) {
$arrLine = explode( "=",$a) ; continue;
}
if ($hostapdconfigline[0] != "#" ) {
$arrLine = explode("=", $hostapdconfigline) ;
$arrConfig[$arrLine[0]]=$arrLine[1]; $arrConfig[$arrLine[0]]=$arrLine[1];
} }
}; };
@ -88,28 +91,55 @@ function DisplayHostAPDConfig(){
<?php CSRFToken() ?> <?php CSRFToken() ?>
<div class="row"> <div class="row">
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<label for="code"><?php echo _("Interface") ;?></label> <label for="cbxinterface"><?php echo _("Interface") ;?></label>
<?php <?php
SelectorOptions('interface', $interfaces, $arrConfig['interface']); SelectorOptions('interface', $interfaces, $arrConfig['interface'], 'cbxinterface');
?> ?>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<label for="code"><?php echo _("SSID"); ?></label> <label for="txtssid"><?php echo _("SSID"); ?></label>
<input type="text" class="form-control" name="ssid" value="<?php echo htmlspecialchars($arrConfig['ssid'], ENT_QUOTES); ?>" /> <input type="text" id="txtssid" class="form-control" name="ssid" value="<?php echo htmlspecialchars($arrConfig['ssid'], ENT_QUOTES); ?>" />
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<label for="code"><?php echo _("Wireless Mode") ;?></label> <label for="cbxhwmode"><?php echo _("Wireless Mode") ;?></label>
<?php SelectorOptions('hw_mode', $arrChannel, $arrConfig['hw_mode']); ?> <?php
$selectedHwMode = $arrConfig['hw_mode'];
if (isset($arrConfig['ieee80211n'])) {
if (strval($arrConfig['ieee80211n']) === '1') {
$selectedHwMode = 'n';
}
}
SelectorOptions('hw_mode', $arr80211Standard, $selectedHwMode, 'cbxhwmode'); ?>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<label for="code"><?php echo _("Channel"); ?></label> <label for="cbxchannel"><?php echo _("Channel"); ?></label>
<?php SelectorOptions('channel', range(1, 14), intval($arrConfig['channel'])) ?> <?php
$selectablechannels = range(1, 13);
$countries_2_4Ghz_max11ch = array('AG', 'BS', 'BB', 'BZ', 'CR', 'CU', 'DM', 'DO', 'SV', 'GD', 'GT',
'HT', 'HN', 'JM', 'MX', 'NI', 'PA', 'KN', 'LC', 'VC', 'TT',
'US', 'CA', 'UZ', 'CO');
$countries_2_4Ghz_max14ch = array('JA');
if (in_array($arrConfig['country_code'], $countries_max11channels)) {
// In North America till channel 11 is the maximum allowed wi-fi 2.4Ghz channel.
// Except for the US that allows channel 12 & 13 in low power mode with additional restrictions.
// Canada that allows channel 12 in low power mode. Because it's unsure if low powered mode
// can be supported the channels are not selectable for those countries.
// source: https://en.wikipedia.org/wiki/List_of_WLAN_channels#Interference_concerns
// Also Uzbekistan and Colombia allow to select till channel 11 as maximum channel on the 2.4Ghz wi-fi band.
$selectablechannels = range(1, 11);
} elseif (in_array($arrConfig['country_code'], $countries_2_4Ghz_max14ch)) {
if ($arrConfig['hw_mode'] === 'b') {
$selectablechannels = range(1, 14);
}
}
SelectorOptions('channel', $selectablechannels, intval($arrConfig['channel']), 'cbxchannel') ?>
</div> </div>
</div> </div>
</div> </div>
@ -117,20 +147,20 @@ function DisplayHostAPDConfig(){
<h4><?php echo _("Security settings"); ?></h4> <h4><?php echo _("Security settings"); ?></h4>
<div class="row"> <div class="row">
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<label for="code"><?php echo _("Security type"); ?></label> <label for="cbxwpa"><?php echo _("Security type"); ?></label>
<?php SelectorOptions('wpa', $arrSecurity, $arrConfig['wpa']); ?> <?php SelectorOptions('wpa', $arrSecurity, $arrConfig['wpa'], 'cbxwpa'); ?>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<label for="code"><?php echo _("Encryption Type"); ?></label> <label for="cbxwpapairwise"><?php echo _("Encryption Type"); ?></label>
<?php SelectorOptions('wpa_pairwise', $arrEncType, $arrConfig['wpa_pairwise']); ?> <?php SelectorOptions('wpa_pairwise', $arrEncType, $arrConfig['wpa_pairwise'], 'cbxwpapairwise'); ?>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<label for="code"><?php echo _("PSK"); ?></label> <label for="txtwpapassphrase"><?php echo _("PSK"); ?></label>
<input type="text" class="form-control" name="wpa_passphrase" value="<?php echo htmlspecialchars($arrConfig['wpa_passphrase'], ENT_QUOTES); ?>" /> <input type="text" class="form-control" id="txtwpapassphrase" name="wpa_passphrase" value="<?php echo htmlspecialchars($arrConfig['wpa_passphrase'], ENT_QUOTES); ?>" />
</div> </div>
</div> </div>
</div> </div>
@ -154,24 +184,24 @@ function DisplayHostAPDConfig(){
<div class="row"> <div class="row">
<div class="col-md-4"> <div class="col-md-4">
<div class="form-check"> <div class="form-check">
<label class="form-check-label"> <label class="form-check-label" for="chxlogenable">
<?php echo _("Enable logging"); <?php echo _("Enable logging");
$checked = ''; $checkedLogEnabled = '';
if ($arrHostapdConf['LogEnable'] == 1) { if ($arrHostapdConf['LogEnable'] == 1) {
$checked = ' checked="checked"'; $checkedLogEnabled = ' checked="checked"';
} }
?> ?>
<input id="logEnable" name ="logEnable" type="checkbox" class="form-check-input" value="1"<?php echo $checked; ?> />
</label> </label>
<input id="chxlogenable" name="logEnable" type="checkbox" class="form-check-input" value="1"<?php echo $checkedLogEnabled; ?> />
</div> </div>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="form-group col-md-4"> <div class="form-group col-md-4">
<label for="code"><?php echo _("Country Code"); ?></label> <label for="cbxcountries"><?php echo _("Country Code"); ?></label>
<input type="hidden" id="selected_country" value="<?php echo htmlspecialchars($arrConfig['country_code'], ENT_QUOTES); ?>"> <input type="hidden" id="selected_country" value="<?php echo htmlspecialchars($arrConfig['country_code'], ENT_QUOTES); ?>">
<select class="form-control" id="countries" name="country_code"> <select class="form-control" id="cbxcountries" name="country_code">
<option value="AF">Afghanistan</option> <option value="AF">Afghanistan</option>
<option value="AX">Åland Islands</option> <option value="AX">Åland Islands</option>
<option value="AL">Albania</option> <option value="AL">Albania</option>
@ -424,7 +454,7 @@ if ($arrHostapdConf['LogEnable'] == 1) {
</select> </select>
<script type="text/javascript"> <script type="text/javascript">
var country = document.getElementById("selected_country").value; var country = document.getElementById("selected_country").value;
var countries = document.getElementById("countries"); var countries = document.getElementById("cbxcountries");
var ops = countries.getElementsByTagName("option"); var ops = countries.getElementsByTagName("option");
for (var i = 0; i < ops.length; ++i) { for (var i = 0; i < ops.length; ++i) {
if(ops[i].value == country){ if(ops[i].value == country){
@ -463,10 +493,13 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status)
return false; return false;
} }
if ((!filter_var($_POST['channel'], FILTER_VALIDATE_INT)) || if (!filter_var($_POST['channel'], FILTER_VALIDATE_INT)) {
intval($_POST['channel']) < 1 || error_log("Attempting to set channel to invalid number.");
intval($_POST['channel']) > 14) { return false;
error_log("Attempting to set channel to '".$_POST['channel']."'"); // FIXME: log injection }
if (intval($_POST['channel']) < 1 || intval($_POST['channel']) > 14) {
error_log("Attempting to set channel to '".$_POST['channel']."'");
return false; return false;
} }
@ -523,14 +556,22 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status)
fwrite($tmp_file, 'driver=nl80211'.PHP_EOL); fwrite($tmp_file, 'driver=nl80211'.PHP_EOL);
fwrite($tmp_file, 'ctrl_interface='.RASPI_HOSTAPD_CTRL_INTERFACE.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, '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, 'auth_algs=1'.PHP_EOL);
fwrite($tmp_file, 'wpa_key_mgmt=WPA-PSK'.PHP_EOL); fwrite($tmp_file, 'wpa_key_mgmt=WPA-PSK'.PHP_EOL);
fwrite($tmp_file, 'beacon_int=100'.PHP_EOL);
// TODO: deal with ini file value escaping. E.g. ssid=E=mc2 becomes ssid=E\=mc2
fwrite($tmp_file, 'ssid='.$_POST['ssid'].PHP_EOL); fwrite($tmp_file, 'ssid='.$_POST['ssid'].PHP_EOL);
fwrite($tmp_file, 'channel='.$_POST['channel'].PHP_EOL); fwrite($tmp_file, 'channel='.$_POST['channel'].PHP_EOL);
fwrite($tmp_file, 'hw_mode='.$_POST['hw_mode'].PHP_EOL); if ($_POST['hw_mode'] === 'n') {
fwrite($tmp_file, 'hw_mode=g'.PHP_EOL);
fwrite($tmp_file, 'ieee80211n=1'.PHP_EOL);
// Enable basic Quality of service
fwrite($tmp_file, 'wme_enabled=1'.PHP_EOL);
} else {
fwrite($tmp_file, 'hw_mode='.$_POST['hw_mode'].PHP_EOL);
fwrite($tmp_file, 'ieee80211n=0'.PHP_EOL);
}
fwrite($tmp_file, 'wpa_passphrase='.$_POST['wpa_passphrase'].PHP_EOL); fwrite($tmp_file, 'wpa_passphrase='.$_POST['wpa_passphrase'].PHP_EOL);
fwrite($tmp_file, 'interface='.$_POST['interface'].PHP_EOL); fwrite($tmp_file, 'interface='.$_POST['interface'].PHP_EOL);
fwrite($tmp_file, 'wpa='.$_POST['wpa'].PHP_EOL); fwrite($tmp_file, 'wpa='.$_POST['wpa'].PHP_EOL);