1
|
<?php
|
2
|
/*
|
3
|
* firewall_nat.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
|
7
|
* All rights reserved.
|
8
|
*
|
9
|
* originally based on m0n0wall (http://m0n0.ch/wall)
|
10
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
11
|
* All rights reserved.
|
12
|
*
|
13
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
14
|
* you may not use this file except in compliance with the License.
|
15
|
* You may obtain a copy of the License at
|
16
|
*
|
17
|
* http://www.apache.org/licenses/LICENSE-2.0
|
18
|
*
|
19
|
* Unless required by applicable law or agreed to in writing, software
|
20
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
21
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
22
|
* See the License for the specific language governing permissions and
|
23
|
* limitations under the License.
|
24
|
*/
|
25
|
|
26
|
##|+PRIV
|
27
|
##|*IDENT=page-firewall-nat-portforward
|
28
|
##|*NAME=Firewall: NAT: Port Forward
|
29
|
##|*DESCR=Allow access to the 'Firewall: NAT: Port Forward' page.
|
30
|
##|*MATCH=firewall_nat.php*
|
31
|
##|-PRIV
|
32
|
|
33
|
require_once("guiconfig.inc");
|
34
|
require_once("functions.inc");
|
35
|
require_once("filter.inc");
|
36
|
require_once("shaper.inc");
|
37
|
require_once("itemid.inc");
|
38
|
|
39
|
if (!is_array($config['nat']['rule'])) {
|
40
|
$config['nat']['rule'] = array();
|
41
|
}
|
42
|
|
43
|
$a_nat = &$config['nat']['rule'];
|
44
|
|
45
|
/* update rule order, POST[rule] is an array of ordered IDs */
|
46
|
if (array_key_exists('order-store', $_REQUEST)) {
|
47
|
if (is_array($_REQUEST['rule']) && !empty($_REQUEST['rule'])) {
|
48
|
$a_nat_new = array();
|
49
|
|
50
|
// if a rule is not in POST[rule], it has been deleted by the user
|
51
|
foreach ($_POST['rule'] as $id) {
|
52
|
$a_nat_new[] = $a_nat[$id];
|
53
|
}
|
54
|
|
55
|
$a_nat = $a_nat_new;
|
56
|
|
57
|
|
58
|
$config['nat']['separator'] = "";
|
59
|
|
60
|
if ($_POST['separator']) {
|
61
|
$idx = 0;
|
62
|
foreach ($_POST['separator'] as $separator) {
|
63
|
$config['nat']['separator']['sep' . $idx++] = $separator;
|
64
|
}
|
65
|
}
|
66
|
|
67
|
if (write_config()) {
|
68
|
mark_subsystem_dirty('filter');
|
69
|
}
|
70
|
|
71
|
header("Location: firewall_nat.php");
|
72
|
exit;
|
73
|
}
|
74
|
}
|
75
|
|
76
|
/* if a custom message has been passed along, lets process it */
|
77
|
if ($_REQUEST['savemsg']) {
|
78
|
$savemsg = $_REQUEST['savemsg'];
|
79
|
}
|
80
|
|
81
|
if ($_POST['apply']) {
|
82
|
|
83
|
$retval = 0;
|
84
|
|
85
|
$retval |= filter_configure();
|
86
|
|
87
|
pfSense_handle_custom_code("/usr/local/pkg/firewall_nat/apply");
|
88
|
|
89
|
if ($retval == 0) {
|
90
|
clear_subsystem_dirty('natconf');
|
91
|
clear_subsystem_dirty('filter');
|
92
|
}
|
93
|
|
94
|
}
|
95
|
|
96
|
if ($_POST['act'] == "del") {
|
97
|
if ($a_nat[$_POST['id']]) {
|
98
|
|
99
|
if (isset($a_nat[$_POST['id']]['associated-rule-id'])) {
|
100
|
delete_id($a_nat[$_POST['id']]['associated-rule-id'], $config['filter']['rule']);
|
101
|
$want_dirty_filter = true;
|
102
|
}
|
103
|
|
104
|
unset($a_nat[$_POST['id']]);
|
105
|
|
106
|
// Update the separators
|
107
|
$a_separators = &$config['nat']['separator'];
|
108
|
$ridx = $_POST['id'];
|
109
|
$mvnrows = -1;
|
110
|
move_separators($a_separators, $ridx, $mvnrows);
|
111
|
|
112
|
if (write_config()) {
|
113
|
mark_subsystem_dirty('natconf');
|
114
|
if ($want_dirty_filter) {
|
115
|
mark_subsystem_dirty('filter');
|
116
|
}
|
117
|
}
|
118
|
|
119
|
header("Location: firewall_nat.php");
|
120
|
exit;
|
121
|
}
|
122
|
}
|
123
|
|
124
|
if (isset($_POST['del_x'])) {
|
125
|
|
126
|
/* delete selected rules */
|
127
|
if (is_array($_POST['rule']) && count($_POST['rule'])) {
|
128
|
$a_separators = &$config['nat']['separator'];
|
129
|
$num_deleted = 0;
|
130
|
|
131
|
foreach ($_POST['rule'] as $rulei) {
|
132
|
$target = $rule['target'];
|
133
|
|
134
|
// Check for filter rule associations
|
135
|
if (isset($a_nat[$rulei]['associated-rule-id'])) {
|
136
|
delete_id($a_nat[$rulei]['associated-rule-id'], $config['filter']['rule']);
|
137
|
mark_subsystem_dirty('filter');
|
138
|
}
|
139
|
|
140
|
unset($a_nat[$rulei]);
|
141
|
|
142
|
// Update the separators
|
143
|
// As rules are deleted, $ridx has to be decremented or separator position will break
|
144
|
$ridx = $rulei - $num_deleted;
|
145
|
$mvnrows = -1;
|
146
|
move_separators($a_separators, $ridx, $mvnrows);
|
147
|
$num_deleted++;
|
148
|
}
|
149
|
|
150
|
if (write_config()) {
|
151
|
mark_subsystem_dirty('natconf');
|
152
|
}
|
153
|
|
154
|
header("Location: firewall_nat.php");
|
155
|
exit;
|
156
|
}
|
157
|
} else if ($_POST['act'] == "toggle") {
|
158
|
if ($a_nat[$_POST['id']]) {
|
159
|
if (isset($a_nat[$_POST['id']]['disabled'])) {
|
160
|
unset($a_nat[$_POST['id']]['disabled']);
|
161
|
$rule_status = true;
|
162
|
} else {
|
163
|
$a_nat[$_POST['id']]['disabled'] = true;
|
164
|
$rule_status = false;
|
165
|
}
|
166
|
|
167
|
// Check for filter rule associations
|
168
|
if (isset($a_nat[$_POST['id']]['associated-rule-id'])) {
|
169
|
toggle_id($a_nat[$_POST['id']]['associated-rule-id'],
|
170
|
$config['filter']['rule'], $rule_status);
|
171
|
unset($rule_status);
|
172
|
mark_subsystem_dirty('filter');
|
173
|
}
|
174
|
|
175
|
if (write_config(gettext("Firewall: NAT: Port forward, enable/disable NAT rule"))) {
|
176
|
mark_subsystem_dirty('natconf');
|
177
|
}
|
178
|
header("Location: firewall_nat.php");
|
179
|
exit;
|
180
|
}
|
181
|
}
|
182
|
|
183
|
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
|
184
|
$pglinks = array("", "@self", "@self");
|
185
|
include("head.inc");
|
186
|
|
187
|
if ($_POST['apply']) {
|
188
|
print_apply_result_box($retval);
|
189
|
}
|
190
|
|
191
|
if (is_subsystem_dirty('natconf')) {
|
192
|
print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
|
193
|
gettext('The changes must be applied for them to take effect.'));
|
194
|
}
|
195
|
|
196
|
$tab_array = array();
|
197
|
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
|
198
|
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
|
199
|
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
|
200
|
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
|
201
|
display_top_tabs($tab_array);
|
202
|
|
203
|
$columns_in_table = 13;
|
204
|
?>
|
205
|
<!-- Allow table to scroll when dragging outside of the display window -->
|
206
|
<style>
|
207
|
.table-responsive {
|
208
|
clear: both;
|
209
|
overflow-x: visible;
|
210
|
margin-bottom: 0px;
|
211
|
}
|
212
|
</style>
|
213
|
|
214
|
<form action="firewall_nat.php" method="post" name="iform">
|
215
|
<div class="panel panel-default">
|
216
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Rules')?></h2></div>
|
217
|
<div class="panel-body table-responsive">
|
218
|
<table id="ruletable" class="table table-striped table-hover table-condensed">
|
219
|
<thead>
|
220
|
<tr>
|
221
|
<th><!-- Checkbox --></th>
|
222
|
<th><!-- Icon --></th>
|
223
|
<th><!-- Rule type --></th>
|
224
|
<th><?=gettext("Interface")?></th>
|
225
|
<th><?=gettext("Protocol")?></th>
|
226
|
<th><?=gettext("Source Address")?></th>
|
227
|
<th><?=gettext("Source Ports")?></th>
|
228
|
<th><?=gettext("Dest. Address")?></th>
|
229
|
<th><?=gettext("Dest. Ports")?></th>
|
230
|
<th><?=gettext("NAT IP")?></th>
|
231
|
<th><?=gettext("NAT Ports")?></th>
|
232
|
<th><?=gettext("Description")?></th>
|
233
|
<th><?=gettext("Actions")?></th>
|
234
|
</tr>
|
235
|
</thead>
|
236
|
<tbody class='user-entries'>
|
237
|
<?php
|
238
|
|
239
|
$nnats = $i = 0;
|
240
|
$separators = $config['nat']['separator'];
|
241
|
|
242
|
// Get a list of separator rows and use it to call the display separator function only for rows which there are separator(s).
|
243
|
// More efficient than looping through the list of separators on every row.
|
244
|
$seprows = separator_rows($separators);
|
245
|
|
246
|
foreach ($a_nat as $natent):
|
247
|
|
248
|
// Display separator(s) for section beginning at rule n
|
249
|
if ($seprows[$nnats]) {
|
250
|
display_separator($separators, $nnats, $columns_in_table);
|
251
|
}
|
252
|
|
253
|
$localport = $natent['local-port'];
|
254
|
|
255
|
list($dstbeginport, $dstendport) = explode("-", $natent['destination']['port']);
|
256
|
|
257
|
if ($dstendport) {
|
258
|
$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
|
259
|
$localport .= '-' . $localendport;
|
260
|
}
|
261
|
|
262
|
$alias = rule_columns_with_alias(
|
263
|
$natent['source']['address'],
|
264
|
pprint_port($natent['source']['port']),
|
265
|
$natent['destination']['address'],
|
266
|
pprint_port($natent['destination']['port']),
|
267
|
$natent['target'],
|
268
|
$localport
|
269
|
);
|
270
|
|
271
|
/* if user does not have access to edit an interface skip on to the next record */
|
272
|
if (!have_natpfruleint_access($natent['interface'])) {
|
273
|
continue;
|
274
|
}
|
275
|
|
276
|
if (isset($natent['disabled'])) {
|
277
|
$iconfn = "pass_d";
|
278
|
$trclass = 'class="disabled"';
|
279
|
} else {
|
280
|
$iconfn = "pass";
|
281
|
$trclass = '';
|
282
|
}
|
283
|
?>
|
284
|
|
285
|
<tr id="fr<?=$nnats;?>" <?=$trclass?> onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_edit.php?id=<?=$i;?>';">
|
286
|
<td >
|
287
|
<input type="checkbox" id="frc<?=$nnats;?>" onClick="fr_toggle(<?=$nnats;?>)" name="rule[]" value="<?=$i;?>"/>
|
288
|
</td>
|
289
|
<td>
|
290
|
<a href="?act=toggle&id=<?=$i?>" usepost>
|
291
|
<i class="fa fa-check" title="<?=gettext("click to toggle enabled/disabled status")?>"></i>
|
292
|
<?php if (isset($natent['nordr'])) { ?>
|
293
|
<i class="fa fa-hand-stop-o text-danger" title="<?=gettext("Negated: This rule excludes NAT from a later rule")?>"></i>
|
294
|
<?php } ?>
|
295
|
</a>
|
296
|
</td>
|
297
|
<td>
|
298
|
<?php
|
299
|
if ($natent['associated-rule-id'] == "pass"):
|
300
|
?>
|
301
|
<i class="fa fa-play" title="<?=gettext("All traffic matching this NAT entry is passed")?>"></i>
|
302
|
<?php
|
303
|
elseif (!empty($natent['associated-rule-id'])):
|
304
|
?>
|
305
|
<i class="fa fa-random" title="<?=sprintf(gettext("Firewall rule ID %s is managed by this rule"), htmlspecialchars($natent['associated-rule-id']))?>"></i>
|
306
|
<?php
|
307
|
endif;
|
308
|
?>
|
309
|
</td>
|
310
|
<td>
|
311
|
<?=$textss?>
|
312
|
<?php
|
313
|
if (!$natent['interface']) {
|
314
|
echo htmlspecialchars(convert_friendly_interface_to_friendly_descr("wan"));
|
315
|
} else {
|
316
|
echo htmlspecialchars(convert_friendly_interface_to_friendly_descr($natent['interface']));
|
317
|
}
|
318
|
?>
|
319
|
<?=$textse?>
|
320
|
</td>
|
321
|
|
322
|
<td>
|
323
|
<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
|
324
|
</td>
|
325
|
|
326
|
<td>
|
327
|
|
328
|
|
329
|
<?php
|
330
|
if (isset($alias['src'])):
|
331
|
?>
|
332
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['src']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['src'])?>" data-html="true">
|
333
|
<?php
|
334
|
endif;
|
335
|
?>
|
336
|
<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['source'])))?>
|
337
|
<?php
|
338
|
if (isset($alias['src'])):
|
339
|
?>
|
340
|
</a>
|
341
|
<?php
|
342
|
endif;
|
343
|
?>
|
344
|
</td>
|
345
|
<td>
|
346
|
<?php
|
347
|
if (isset($alias['srcport'])):
|
348
|
?>
|
349
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['srcport']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['srcport'])?>" data-html="true">
|
350
|
<?php
|
351
|
endif;
|
352
|
?>
|
353
|
<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['source']['port'])))?>
|
354
|
<?php
|
355
|
if (isset($alias['srcport'])):
|
356
|
?>
|
357
|
</a>
|
358
|
<?php
|
359
|
endif;
|
360
|
?>
|
361
|
</td>
|
362
|
|
363
|
<td>
|
364
|
<?php
|
365
|
if (isset($alias['dst'])):
|
366
|
?>
|
367
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['dst']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['dst'])?>" data-html="true">
|
368
|
<?php
|
369
|
endif;
|
370
|
?>
|
371
|
<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['destination'])))?>
|
372
|
<?php
|
373
|
if (isset($alias['dst'])):
|
374
|
?>
|
375
|
</a>
|
376
|
<?php
|
377
|
endif;
|
378
|
?>
|
379
|
</td>
|
380
|
<td>
|
381
|
<?php
|
382
|
if (isset($alias['dstport'])):
|
383
|
?>
|
384
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['dstport']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['dstport'])?>" data-html="true">
|
385
|
<?php
|
386
|
endif;
|
387
|
?>
|
388
|
<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['destination']['port'])))?>
|
389
|
<?php
|
390
|
if (isset($alias['dstport'])):
|
391
|
?>
|
392
|
</a>
|
393
|
<?php
|
394
|
endif;
|
395
|
?>
|
396
|
</td>
|
397
|
<td>
|
398
|
<?php
|
399
|
if (isset($alias['target'])):
|
400
|
?>
|
401
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['target']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['target'])?>" data-html="true" >
|
402
|
<?php
|
403
|
endif;
|
404
|
?>
|
405
|
|
406
|
<?=str_replace('_', '_<wbr>', htmlspecialchars($natent['target']))?>
|
407
|
<?php
|
408
|
if (isset($alias['target'])):
|
409
|
?>
|
410
|
</a>
|
411
|
<?php
|
412
|
endif;
|
413
|
?>
|
414
|
</td>
|
415
|
<td>
|
416
|
<?php
|
417
|
if (isset($alias['targetport'])):
|
418
|
?>
|
419
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['targetport']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['targetport'])?>" data-html="true">
|
420
|
<?php
|
421
|
endif;
|
422
|
?>
|
423
|
<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($localport)))?>
|
424
|
<?php
|
425
|
if (isset($alias['targetport'])):
|
426
|
?>
|
427
|
</a>
|
428
|
<?php
|
429
|
endif;
|
430
|
?>
|
431
|
</td>
|
432
|
|
433
|
<td>
|
434
|
<?=htmlspecialchars($natent['descr'])?>
|
435
|
</td>
|
436
|
<td>
|
437
|
<a class="fa fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
|
438
|
<a class="fa fa-clone" title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
|
439
|
<a class="fa fa-trash" title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&id=<?=$i?>" usepost></a>
|
440
|
</td>
|
441
|
</tr>
|
442
|
<?php
|
443
|
$i++;
|
444
|
$nnats++;
|
445
|
|
446
|
endforeach;
|
447
|
|
448
|
// There can be separator(s) after the last rule listed.
|
449
|
if ($seprows[$nnats]) {
|
450
|
display_separator($separators, $nnats, $columns_in_table);
|
451
|
}
|
452
|
?>
|
453
|
</tbody>
|
454
|
</table>
|
455
|
</div>
|
456
|
</div>
|
457
|
|
458
|
<nav class="action-buttons">
|
459
|
<a href="firewall_nat_edit.php?after=-1" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
|
460
|
<i class="fa fa-level-up icon-embed-btn"></i>
|
461
|
<?=gettext('Add')?>
|
462
|
</a>
|
463
|
<a href="firewall_nat_edit.php" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
|
464
|
<i class="fa fa-level-down icon-embed-btn"></i>
|
465
|
<?=gettext('Add')?>
|
466
|
</a>
|
467
|
<button name="del_x" type="submit" class="btn btn-danger btn-sm" title="<?=gettext('Delete selected rules')?>">
|
468
|
<i class="fa fa-trash icon-embed-btn"></i>
|
469
|
<?=gettext("Delete"); ?>
|
470
|
</button>
|
471
|
<button type="submit" id="order-store" name="order-store" class="btn btn-primary btn-sm" disabled title="<?=gettext('Save rule order')?>">
|
472
|
<i class="fa fa-save icon-embed-btn"></i>
|
473
|
<?=gettext("Save")?>
|
474
|
</button>
|
475
|
<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
|
476
|
<i class="fa fa-plus icon-embed-btn"></i>
|
477
|
<?=gettext("Separator")?>
|
478
|
</button>
|
479
|
</nav>
|
480
|
</form>
|
481
|
|
482
|
<script type="text/javascript">
|
483
|
//<![CDATA[
|
484
|
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
|
485
|
iface = "<?=strtolower($if)?>";
|
486
|
cncltxt = '<?=gettext("Cancel")?>';
|
487
|
svtxt = '<?=gettext("Save")?>';
|
488
|
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
|
489
|
configsection = "nat";
|
490
|
dirty = false;
|
491
|
|
492
|
events.push(function() {
|
493
|
|
494
|
// Make rules sortable
|
495
|
$('table tbody.user-entries').sortable({
|
496
|
cursor: 'grabbing',
|
497
|
update: function(event, ui) {
|
498
|
$('#order-store').removeAttr('disabled');
|
499
|
dirty = true;
|
500
|
reindex_rules(ui.item.parent('tbody'));
|
501
|
dirty = true;
|
502
|
}
|
503
|
});
|
504
|
|
505
|
// Check all of the rule checkboxes so that their values are posted
|
506
|
$('#order-store').click(function () {
|
507
|
$('[id^=frc]').prop('checked', true);
|
508
|
|
509
|
// Save the separator bar configuration
|
510
|
save_separators();
|
511
|
|
512
|
// Suppress the "Do you really want to leave the page" message
|
513
|
saving = true;
|
514
|
|
515
|
});
|
516
|
|
517
|
// Globals
|
518
|
saving = false;
|
519
|
dirty = false;
|
520
|
|
521
|
// provide a warning message if the user tries to change page before saving
|
522
|
$(window).bind('beforeunload', function(){
|
523
|
if (!saving && dirty) {
|
524
|
return ("<?=gettext('One or more Port Forward rules have been moved but have not yet been saved')?>");
|
525
|
} else {
|
526
|
return undefined;
|
527
|
}
|
528
|
});
|
529
|
});
|
530
|
//]]>
|
531
|
</script>
|
532
|
<?php
|
533
|
|
534
|
if (count($a_nat) > 0) {
|
535
|
?>
|
536
|
<!-- Legend -->
|
537
|
<div>
|
538
|
<dl class="dl-horizontal responsive">
|
539
|
<dt><?=gettext('Legend')?></dt> <dd></dd>
|
540
|
<dt><i class="fa fa-play"></i></dt> <dd><?=gettext('Pass')?></dd>
|
541
|
<dt><i class="fa fa-random"></i></dt> <dd><?=gettext('Linked rule')?></dd>
|
542
|
</dl>
|
543
|
</div>
|
544
|
|
545
|
<?php
|
546
|
}
|
547
|
|
548
|
include("foot.inc");
|