Add actions (block or pass) to Captive Portal passtrumac

This commit is contained in:
Renato Botelho 2013-09-09 09:09:49 -03:00
parent cfd88fbc83
commit 666f88e0c8
3 changed files with 84 additions and 60 deletions

View File

@ -940,20 +940,52 @@ function captiveportal_passthrumac_configure_entry($macent) {
$bwDown = empty($macent['bw_down']) ? 0 : $macent['bw_down'];
$ruleno = captiveportal_get_next_ipfw_ruleno();
$pipeno = captiveportal_get_next_dn_ruleno();
$rules = "";
$pipeup = $pipeno;
$_gb = @pfSense_pipe_action("pipe {$pipeup} config bw {$bwUp}Kbit/s queue 100 buckets 16");
$pipedown = $pipeno + 1;
$_gb = @pfSense_pipe_action("pipe {$pipedown} config bw {$bwDown}Kbit/s queue 100 buckets 16");
$rules .= "add {$ruleno} pipe {$pipeup} ip from any to any MAC any {$macent['mac']}\n";
$ruleno++;
$rules .= "add {$ruleno} pipe {$pipedown} ip from any to any MAC {$macent['mac']} any\n";
if ($macent['action'] == 'pass') {
$pipeno = captiveportal_get_next_dn_ruleno();
$pipeup = $pipeno;
$_gb = @pfSense_pipe_action("pipe {$pipeno} config bw {$bwUp}Kbit/s queue 100 buckets 16");
$pipedown = $pipeno + 1;
$_gb = @pfSense_pipe_action("pipe {$pipedown} config bw {$bwDown}Kbit/s queue 100 buckets 16");
$rules = "add {$ruleno} pipe {$pipeup} ip from any to any MAC any {$macent['mac']}\n";
$ruleno++;
$rules .= "add {$ruleno} pipe {$pipedown} ip from any to any MAC {$macent['mac']} any\n";
} else
$rules = "add {$ruleno} deny ip from any to any MAC {$macent['mac']} any\n";
return $rules;
}
function captiveportal_passthrumac_delete_entry($macent) {
global $cpzone;
$ruleno = captiveportal_get_ipfw_passthru_ruleno($macent['mac']);
if (!$ruleno)
return false;
$cmd = "/sbin/ipfw -x {$cpzone} delete {$ruleno}";
captiveportal_free_ipfw_ruleno($ruleno, ($macent['action'] == 'block'));
if ($macent['action'] == 'pass') {
$cmd .= "; /sbin/ipfw -x {$cpzone} delete " . ++$ruleno;
$pipeno = captiveportal_get_dn_passthru_ruleno($macent['mac']);
if (!empty($pipeno)) {
captiveportal_free_dn_ruleno($pipeno);
$cmd .= "; /sbin/ipfw -x {$cpzone} pipe delete " . $pipeno;
$cmd .= "; /sbin/ipfw -x {$cpzone} pipe delete " . ++$pipeno;
}
}
mwexec($cmd);
return true;
}
function captiveportal_passthrumac_configure($lock = false) {
global $config, $g, $cpzone;
@ -1468,7 +1500,7 @@ function captiveportal_get_next_ipfw_ruleno($rulenos_start = 2, $rulenos_range_m
return $ruleno;
}
function captiveportal_free_ipfw_ruleno($ruleno) {
function captiveportal_free_ipfw_ruleno($ruleno, $single_rule = false) {
global $config, $g, $cpzone;
$cpcfg = $config['captiveportal'][$cpzone];
@ -1479,7 +1511,8 @@ function captiveportal_free_ipfw_ruleno($ruleno) {
if (file_exists("{$g['vardb_path']}/captiveportal_{$cpzone}.rules")) {
$rules = unserialize(file_get_contents("{$g['vardb_path']}/captiveportal_{$cpzone}.rules"));
$rules[$ruleno] = false;
$rules[++$ruleno] = false;
if (!$single_rule)
$rules[++$ruleno] = false;
file_put_contents("{$g['vardb_path']}/captiveportal_{$cpzone}.rules", serialize($rules));
}
unlock($cpruleslck);

View File

@ -102,17 +102,7 @@ if ($_POST) {
}
}
if ($found == true) {
$ruleno = captiveportal_get_ipfw_passthru_ruleno($_POST['delmac']);
if ($ruleno) {
captiveportal_free_ipfw_ruleno($ruleno);
$pipeno = captiveportal_get_dn_passthru_ruleno($_POST['delmac']);
if ($pipeno)
captiveportal_free_dn_ruleno($pipeno);
if (!empty($pipeno))
mwexec("/sbin/ipfw -x {$cpzone} -q delete {$ruleno}; /sbin/ipfw -x {$cpzone} -q delete " . ++$ruleno . "; /sbin/ipfw -q pipe delete {$pipeno}; /sbin/ipfw -q pipe delete " . (++$pipeno));
else
mwexec("/sbin/ipfw -x {$cpzone} -q delete {$ruleno}; /sbin/ipfw -x {$cpzone} -q delete " . ++$ruleno);
}
captiveportal_passthrumac_delete_entry($a_passthrumacs[$idx]);
unset($a_passthrumacs[$idx]);
write_config();
echo gettext("The entry was sucessfully deleted") . "\n";
@ -126,17 +116,7 @@ if ($_POST) {
if ($_GET['act'] == "del") {
$a_passthrumacs =& $a_cp[$cpzone]['passthrumac'];
if ($a_passthrumacs[$_GET['id']]) {
$ruleno = captiveportal_get_ipfw_passthru_ruleno($a_passthrumacs[$_GET['id']]['mac']);
if ($ruleno) {
captiveportal_free_ipfw_ruleno($ruleno);
$pipeno = captiveportal_get_dn_passthru_ruleno($a_passthrumacs[$_GET['id']]['mac']);
if ($pipeno)
captiveportal_free_dn_ruleno($pipeno);
if (!empty($pipeno))
mwexec("/sbin/ipfw -x {$cpzone} -q delete {$ruleno}; /sbin/ipfw -x {$cpzone} -q delete " . ++$ruleno . "; /sbin/ipfw -q pipe delete {$pipeno}; /sbin/ipfw -q pipe delete " . (++$pipeno));
else
mwexec("/sbin/ipfw -x {$cpzone} -q delete {$ruleno}; /sbin/ipfw -x {$cpzone} -q delete " . ++$ruleno);
}
captiveportal_passthrumac_delete_entry($a_passthrumacs[$_GET['id']]);
unset($a_passthrumacs[$_GET['id']]);
write_config();
header("Location: services_captiveportal_mac.php?zone={$cpzone}");
@ -172,7 +152,8 @@ include("head.inc");
<td class="tabcont">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="40%" class="listhdrr"><?=gettext("MAC address"); ?></td>
<td width="3%" class="list"></td>
<td width="37%" class="listhdrr"><?=gettext("MAC address"); ?></td>
<td width="50%" class="listhdr"><?=gettext("Description"); ?></td>
<td width="10%" class="list"></td>
</tr>
@ -182,6 +163,9 @@ include("head.inc");
foreach ($a_cp[$cpzone]['passthrumac'] as $mac):
?>
<tr ondblclick="document.location='services_captiveportal_mac_edit.php?zone=<?=$cpzone;?>&id=<?=$i;?>'">
<td valign="middle" nowrap class="list">
<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_<?=$mac['action'];?>.gif" width="11" height="11" border="0" alt="icon" />
</td>
<td class="listlr">
<?=strtolower($mac['mac']);?>
</td>
@ -204,7 +188,7 @@ include("head.inc");
endif;
?>
<tr>
<td class="list" colspan="2">&nbsp;</td>
<td class="list" colspan="3">&nbsp;</td>
<td class="list">
<a href="services_captiveportal_mac_edit.php?zone=<?=$cpzone;?>">
<img src="/themes/<?php echo $g['theme']; ?>/images/icons/icon_plus.gif" title="<?=gettext("add host"); ?>" width="17" height="17" border="0">
@ -212,10 +196,10 @@ include("head.inc");
</td>
</tr>
<tr>
<td colspan="2" class="list">
<td colspan="3" class="list">
<span class="vexpl">
<span class="red"><strong><?=gettext("Note:"); ?><br></strong></span>
<?=gettext("Adding MAC addresses as pass-through MACs allows them access through the captive portal automatically without being taken to the portal page."); ?>
<?=gettext("Adding MAC addresses as 'pass' MACs allows them access through the captive portal automatically without being taken to the portal page."); ?>
</span>
</td>
<td class="list">&nbsp;</td>

View File

@ -79,6 +79,7 @@ if (!is_array($a_cp[$cpzone]['passthrumac']))
$a_passthrumacs = &$a_cp[$cpzone]['passthrumac'];
if (isset($id) && $a_passthrumacs[$id]) {
$pconfig['action'] = $a_passthrumacs[$id]['action'];
$pconfig['mac'] = $a_passthrumacs[$id]['mac'];
$pconfig['bw_up'] = $a_passthrumacs[$id]['bw_up'];
$pconfig['bw_down'] = $a_passthrumacs[$id]['bw_down'];
@ -92,16 +93,15 @@ if ($_POST) {
$pconfig = $_POST;
/* input validation */
$reqdfields = explode(" ", "mac");
$reqdfieldsn = array(gettext("MAC address"));
$reqdfields = explode(" ", "action mac");
$reqdfieldsn = array(gettext("Action"), gettext("MAC address"));
do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
$_POST['mac'] = str_replace("-", ":", $_POST['mac']);
if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
if (($_POST['mac'] && !is_macaddr($_POST['mac'])))
$input_errors[] = sprintf("%s. [%s]", gettext("A valid MAC address must be specified"), $_POST['mac']);
}
if ($_POST['bw_up'] && !is_numeric($_POST['bw_up']))
$input_errors[] = gettext("Upload speed needs to be an integer");
if ($_POST['bw_down'] && !is_numeric($_POST['bw_down']))
@ -112,13 +112,14 @@ if ($_POST) {
continue;
if ($macent['mac'] == $_POST['mac']){
$input_errors[] = sprintf("[%s] %s.", $_POST['mac'], gettext("already allowed"));
$input_errors[] = sprintf("[%s] %s.", $_POST['mac'], gettext("already exists"));
break;
}
}
if (!$input_errors) {
$mac = array();
$mac['action'] = $_POST['action'];
$mac['mac'] = $_POST['mac'];
if ($_POST['bw_up'])
$mac['bw_up'] = $_POST['bw_up'];
@ -130,10 +131,10 @@ if ($_POST) {
$mac['descr'] = $_POST['descr'];
if (isset($id) && $a_passthrumacs[$id]) {
$oldmac = $a_passthrumacs[$id]['mac'];
$oldmac = $a_passthrumacs[$id];
$a_passthrumacs[$id] = $mac;
} else {
$oldmac = $mac['mac'];
$oldmac = $mac;
$a_passthrumacs[] = $mac;
}
passthrumacs_sort();
@ -141,21 +142,8 @@ if ($_POST) {
write_config();
if (isset($config['captiveportal'][$cpzone]['enable'])) {
$ruleno = captiveportal_get_ipfw_passthru_ruleno($oldmac);
if ($ruleno) {
captiveportal_free_ipfw_ruleno($ruleno);
$pipeno = captiveportal_get_dn_passthru_ruleno($oldmac);
if ($pipeno) {
captiveportal_free_dn_ruleno($pipeno);
$rules .= "pipe delete {$pipeno}\n";
++$pipeno;
$rules .= "pipe delete {$pipeno}\n";
}
$rules = "delete {$ruleno}\n";
$rules .= "delete " . ++$ruleno . "\n";
}
$rules .= captiveportal_passthrumac_configure_entry($mac);
captiveportal_passthrumac_delete_entry($mac);
$rules = captiveportal_passthrumac_configure_entry($mac);
$uniqid = uniqid("{$cpzone}_macedit");
file_put_contents("{$g['tmp_path']}/{$uniqid}_tmp", $rules);
mwexec("/sbin/ipfw -x {$cpzone} -q {$g['tmp_path']}/{$uniqid}_tmp");
@ -176,6 +164,25 @@ include("head.inc");
<tr>
<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit Pass-through MAC address");?></td>
</tr>
<tr>
<td width="22%" valign="top" class="vncellreq"><?=gettext("Action"); ?></td>
<td width="78%" class="vtable">
<select name="action" class="formselect">
<?php
$actions = explode(" ", "Pass Block");
foreach ($actions as $action):
?>
<option value="<?=strtolower($action);?>"<?php if (strtolower($action) == strtolower($pconfig['action'])) echo "selected=\"selected\""; ?>>
<?=htmlspecialchars($action);?>
</option>
<?php
endforeach;
?>
</select>
<br>
<span class="vexpl"><?=gettext("Choose what to do with packets coming from this MAC address"); ?>.</span>
</td>
</tr>
<tr>
<td width="22%" valign="top" class="vncellreq"><?=gettext("MAC address"); ?></td>
<td width="78%" class="vtable">