Project

General

Profile

Download (16 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * firewall_nat.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
##|+PRIV
29
##|*IDENT=page-firewall-nat-portforward
30
##|*NAME=Firewall: NAT: Port Forward
31
##|*DESCR=Allow access to the 'Firewall: NAT: Port Forward' page.
32
##|*MATCH=firewall_nat.php*
33
##|-PRIV
34

    
35
require_once("guiconfig.inc");
36
require_once("functions.inc");
37
require_once("filter.inc");
38
require_once("shaper.inc");
39
require_once("itemid.inc");
40

    
41
init_config_arr(array('filter', 'rule'));
42
init_config_arr(array('nat', 'separator'));
43
init_config_arr(array('nat', 'rule'));
44
$a_nat = &$config['nat']['rule'];
45
$a_separators = &$config['nat']['separator'];
46

    
47
/* update rule order, POST[rule] is an array of ordered IDs */
48
if (array_key_exists('order-store', $_REQUEST) && have_natpfruleint_access($natent['interface'])) {
49
	if (is_array($_REQUEST['rule']) && !empty($_REQUEST['rule'])) {
50
		$a_nat_new = array();
51

    
52
		// if a rule is not in POST[rule], it has been deleted by the user
53
		foreach ($_POST['rule'] as $id) {
54
			$a_nat_new[] = $a_nat[$id];
55
		}
56

    
57
		$a_nat = $a_nat_new;
58

    
59

    
60
		$config['nat']['separator'] = "";
61

    
62
		if ($_POST['separator']) {
63
			$idx = 0;
64

    
65
			if (!is_array($config['nat']['separator'])) {
66
				$config['nat']['separator'] = array();
67
			}
68

    
69
			foreach ($_POST['separator'] as $separator) {
70
				$config['nat']['separator']['sep' . $idx++] = $separator;
71
			}
72
		}
73

    
74
		if (write_config()) {
75
			mark_subsystem_dirty('filter');
76
		}
77

    
78
		header("Location: firewall_nat.php");
79
		exit;
80
	}
81
}
82

    
83
/* if a custom message has been passed along, lets process it */
84
if ($_REQUEST['savemsg']) {
85
	$savemsg = $_REQUEST['savemsg'];
86
}
87

    
88
if ($_POST['apply'] && have_natpfruleint_access($natent['interface'])) {
89

    
90
	$retval = 0;
91

    
92
	$retval |= filter_configure();
93

    
94
	pfSense_handle_custom_code("/usr/local/pkg/firewall_nat/apply");
95

    
96
	if ($retval == 0) {
97
		clear_subsystem_dirty('natconf');
98
		clear_subsystem_dirty('filter');
99
	}
100

    
101
}
102

    
103
if (($_POST['act'] == "del") && have_natpfruleint_access($natent['interface'])) {
104
	if ($a_nat[$_POST['id']]) {
105

    
106
		if (isset($a_nat[$_POST['id']]['associated-rule-id'])) {
107
			delete_id($a_nat[$_POST['id']]['associated-rule-id'], $config['filter']['rule']);
108
			$want_dirty_filter = true;
109
		}
110

    
111
		unset($a_nat[$_POST['id']]);
112

    
113
		// Update the separators
114
		$ridx = $_POST['id'];
115
		$mvnrows = -1;
116
		move_separators($a_separators, $ridx, $mvnrows);
117

    
118
		if (write_config()) {
119
			mark_subsystem_dirty('natconf');
120
			if ($want_dirty_filter) {
121
				mark_subsystem_dirty('filter');
122
			}
123
		}
124

    
125
		header("Location: firewall_nat.php");
126
		exit;
127
	}
128
}
129

    
130
if (isset($_POST['del_x']) && have_natpfruleint_access($natent['interface'])) {
131

    
132
	/* delete selected rules */
133
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
134
		$num_deleted = 0;
135

    
136
		foreach ($_POST['rule'] as $rulei) {
137
			$target = $rule['target'];
138

    
139
			// Check for filter rule associations
140
			if (isset($a_nat[$rulei]['associated-rule-id'])) {
141
				delete_id($a_nat[$rulei]['associated-rule-id'], $config['filter']['rule']);
142
				mark_subsystem_dirty('filter');
143
			}
144

    
145
			unset($a_nat[$rulei]);
146

    
147
			// Update the separators
148
			// As rules are deleted, $ridx has to be decremented or separator position will break
149
			$ridx = $rulei - $num_deleted;
150
			$mvnrows = -1;
151
			move_separators($a_separators, $ridx, $mvnrows);
152
			$num_deleted++;
153
		}
154

    
155
		if (write_config()) {
156
			mark_subsystem_dirty('natconf');
157
		}
158

    
159
		header("Location: firewall_nat.php");
160
		exit;
161
	}
162
} elseif (($_POST['act'] == "toggle") && have_natpfruleint_access($natent['interface'])) {
163
	if ($a_nat[$_POST['id']]) {
164
		if (isset($a_nat[$_POST['id']]['disabled'])) {
165
			unset($a_nat[$_POST['id']]['disabled']);
166
			$rule_status = true;
167
		} else {
168
			$a_nat[$_POST['id']]['disabled'] = true;
169
			$rule_status = false;
170
		}
171

    
172
		// Check for filter rule associations
173
		if (isset($a_nat[$_POST['id']]['associated-rule-id'])) {
174
			toggle_id($a_nat[$_POST['id']]['associated-rule-id'],
175
			    $config['filter']['rule'], $rule_status);
176
			unset($rule_status);
177
			mark_subsystem_dirty('filter');
178
		}
179

    
180
		if (write_config(gettext("Firewall: NAT: Port forward, enable/disable NAT rule"))) {
181
			mark_subsystem_dirty('natconf');
182
		}
183
		header("Location: firewall_nat.php");
184
		exit;
185
	}
186
}
187

    
188
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
189
$pglinks = array("", "@self", "@self");
190
include("head.inc");
191

    
192
if ($_POST['apply']) {
193
	print_apply_result_box($retval);
194
}
195

    
196
if (is_subsystem_dirty('natconf') && have_natpfruleint_access($natent['interface'])) {
197
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
198
					gettext('The changes must be applied for them to take effect.'));
199
}
200

    
201
$tab_array = array();
202
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
203
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
204
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
205
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
206
display_top_tabs($tab_array);
207

    
208
$columns_in_table = 13;
209
?>
210
<!-- Allow table to scroll when dragging outside of the display window -->
211
<style>
212
.table-responsive {
213
    clear: both;
214
    overflow-x: visible;
215
    margin-bottom: 0px;
216
}
217
</style>
218

    
219
<form action="firewall_nat.php" method="post" name="iform">
220
	<div class="panel panel-default">
221
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Rules')?></h2></div>
222
		<div class="panel-body table-responsive">
223
			<table id="ruletable" class="table table-striped table-hover table-condensed">
224
				<thead>
225
					<tr>
226
						<th style="padding-left:10px;">  <input type="checkbox" id="selectAll" name="selectAll" /></th>
227
						<th><!-- Icon --></th>
228
						<th><!-- Rule type --></th>
229
						<th><?=gettext("Interface")?></th>
230
						<th><?=gettext("Protocol")?></th>
231
						<th><?=gettext("Source Address")?></th>
232
						<th><?=gettext("Source Ports")?></th>
233
						<th><?=gettext("Dest. Address")?></th>
234
						<th><?=gettext("Dest. Ports")?></th>
235
						<th><?=gettext("NAT IP")?></th>
236
						<th><?=gettext("NAT Ports")?></th>
237
						<th><?=gettext("Description")?></th>
238
						<th><?=gettext("Actions")?></th>
239
					</tr>
240
				</thead>
241
				<tbody class='user-entries'>
242
<?php
243

    
244
$nnats = $i = 0;
245
$separators = $config['nat']['separator'];
246

    
247
// Get a list of separator rows and use it to call the display separator function only for rows which there are separator(s).
248
// More efficient than looping through the list of separators on every row.
249
$seprows = separator_rows($separators);
250

    
251
foreach ($a_nat as $natent):
252

    
253
	// Display separator(s) for section beginning at rule n
254
	if ($seprows[$nnats]) {
255
		display_separator($separators, $nnats, $columns_in_table);
256
	}
257

    
258
	$localport = $natent['local-port'];
259

    
260
	list($dstbeginport, $dstendport) = explode("-", $natent['destination']['port']);
261

    
262
	if ($dstendport) {
263
		$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
264
		$localport	 .= '-' . $localendport;
265
	}
266

    
267
	$alias = rule_columns_with_alias(
268
		$natent['source']['address'],
269
		pprint_port($natent['source']['port']),
270
		$natent['destination']['address'],
271
		pprint_port($natent['destination']['port']),
272
		$natent['target'],
273
		$localport
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
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
288
							<input type="checkbox" id="frc<?=$nnats;?>" onClick="fr_toggle(<?=$nnats;?>)" name="rule[]" value="<?=$i;?>"/>
289
<?php	endif; ?>
290
						</td>
291
						<td>
292
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
293
							<a href="?act=toggle&amp;id=<?=$i?>" usepost>
294
								<i class="fa fa-check" title="<?=gettext("click to toggle enabled/disabled status")?>"></i>
295
							</a>
296
<?php	endif; ?>
297
<?php 	if (isset($natent['nordr'])) { ?>
298
								&nbsp;<i class="fa fa-hand-stop-o text-danger" title="<?=gettext("Negated: This rule excludes NAT from a later rule")?>"></i>
299
<?php 	} ?>
300
						</td>
301
						<td>
302
<?php
303
	if ($natent['associated-rule-id'] == "pass"):
304
?>
305
							<i class="fa fa-play" title="<?=gettext("All traffic matching this NAT entry is passed")?>"></i>
306
<?php
307
	elseif (!empty($natent['associated-rule-id'])):
308
?>
309
							<i class="fa fa-random" title="<?=sprintf(gettext("Firewall rule ID %s is managed by this rule"), htmlspecialchars($natent['associated-rule-id']))?>"></i>
310
<?php
311
	endif;
312
?>
313
						</td>
314
						<td>
315
							<?=$textss?>
316
<?php
317
	if (!$natent['interface']) {
318
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr("wan"));
319
	} else {
320
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr($natent['interface']));
321
	}
322
?>
323
							<?=$textse?>
324
						</td>
325

    
326
						<td>
327
							<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
328
						</td>
329

    
330
						<td>
331

    
332

    
333
<?php
334
	if (isset($alias['src'])):
335
?>
336
							<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">
337
<?php
338
	endif;
339
?>
340
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['source'])))?>
341
<?php
342
	if (isset($alias['src'])):
343
?>
344
							</a>
345
<?php
346
	endif;
347
?>
348
						</td>
349
						<td>
350
<?php
351
	if (isset($alias['srcport'])):
352
?>
353
							<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">
354
<?php
355
	endif;
356
?>
357
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['source']['port'])))?>
358
<?php
359
	if (isset($alias['srcport'])):
360
?>
361
							</a>
362
<?php
363
	endif;
364
?>
365
						</td>
366

    
367
						<td>
368
<?php
369
	if (isset($alias['dst'])):
370
?>
371
							<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">
372
<?php
373
	endif;
374
?>
375
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['destination'])))?>
376
<?php
377
	if (isset($alias['dst'])):
378
?>
379
							</a>
380
<?php
381
	endif;
382
?>
383
						</td>
384
						<td>
385
<?php
386
	if (isset($alias['dstport'])):
387
?>
388
							<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">
389
<?php
390
	endif;
391
?>
392
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['destination']['port'])))?>
393
<?php
394
	if (isset($alias['dstport'])):
395
?>
396
							</a>
397
<?php
398
	endif;
399
?>
400
						</td>
401
						<td>
402
<?php
403
	if (isset($alias['target'])):
404
?>
405
							<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" >
406
<?php
407
	endif;
408
?>
409

    
410
							<?=str_replace('_', '_<wbr>', htmlspecialchars($natent['target']))?>
411
<?php
412
	if (isset($alias['target'])):
413
?>
414
							</a>
415
<?php
416
	endif;
417
?>
418
						</td>
419
						<td>
420
<?php
421
	if (isset($alias['targetport'])):
422
?>
423
							<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">
424
<?php
425
	endif;
426
?>
427
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($localport)))?>
428
<?php
429
	if (isset($alias['targetport'])):
430
?>
431
							</a>
432
<?php
433
	endif;
434
?>
435
						</td>
436

    
437
						<td>
438
							<?=htmlspecialchars($natent['descr'])?>
439
						</td>
440
						<td>
441
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
442
							<a class="fa fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
443
							<a class="fa fa-clone"	  title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
444
							<a class="fa fa-trash"	title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&amp;id=<?=$i?>" usepost></a>
445
<?php	else: ?>
446
							-
447
<?php	endif; ?>
448
						</td>
449
					</tr>
450
<?php
451
	$i++;
452
	$nnats++;
453

    
454
endforeach;
455

    
456
// There can be separator(s) after the last rule listed.
457
if ($seprows[$nnats]) {
458
	display_separator($separators, $nnats, $columns_in_table);
459
}
460
?>
461
				</tbody>
462
			</table>
463
		</div>
464
	</div>
465

    
466
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
467
	<nav class="action-buttons">
468
		<a href="firewall_nat_edit.php?after=-1" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
469
			<i class="fa fa-level-up icon-embed-btn"></i>
470
			<?=gettext('Add')?>
471
		</a>
472
		<a href="firewall_nat_edit.php" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
473
			<i class="fa fa-level-down icon-embed-btn"></i>
474
			<?=gettext('Add')?>
475
		</a>
476
		<button name="del_x" type="submit" class="btn btn-danger btn-sm" title="<?=gettext('Delete selected rules')?>">
477
			<i class="fa fa-trash icon-embed-btn"></i>
478
			<?=gettext("Delete"); ?>
479
		</button>
480
		<button type="submit" id="order-store" name="order-store" class="btn btn-primary btn-sm" disabled title="<?=gettext('Save rule order')?>">
481
			<i class="fa fa-save icon-embed-btn"></i>
482
			<?=gettext("Save")?>
483
		</button>
484
		<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
485
			<i class="fa fa-plus icon-embed-btn"></i>
486
			<?=gettext("Separator")?>
487
		</button>
488
	</nav>
489
<?php	endif; ?>
490
</form>
491

    
492
<script type="text/javascript">
493
//<![CDATA[
494
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
495
iface = "<?=strtolower($if)?>";
496
cncltxt = '<?=gettext("Cancel")?>';
497
svtxt = '<?=gettext("Save")?>';
498
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
499
configsection = "nat";
500
dirty = false;
501

    
502
events.push(function() {
503

    
504
<?php if(!isset($config['system']['webgui']['roworderdragging'])): ?>
505
	// Make rules sortable
506
	$('table tbody.user-entries').sortable({
507
		cursor: 'grabbing',
508
		update: function(event, ui) {
509
			$('#order-store').removeAttr('disabled');
510
			dirty = true;
511
			reindex_rules(ui.item.parent('tbody'));
512
			dirty = true;
513
		}
514
	});
515
<?php endif; ?>
516

    
517
	// Check all of the rule checkboxes so that their values are posted
518
	$('#order-store').click(function () {
519
	   $('[id^=frc]').prop('checked', true);
520

    
521
		// Save the separator bar configuration
522
		save_separators();
523

    
524
		// Suppress the "Do you really want to leave the page" message
525
		saving = true;
526

    
527
	});
528

    
529
	// Globals
530
	saving = false;
531
	dirty = false;
532

    
533
	// provide a warning message if the user tries to change page before saving
534
	$(window).bind('beforeunload', function(){
535
		if (!saving && dirty) {
536
			return ("<?=gettext('One or more Port Forward rules have been moved but have not yet been saved')?>");
537
		} else {
538
			return undefined;
539
		}
540
	});
541

    
542
	$('#selectAll').click(function() {
543
		var checkedStatus = this.checked;
544
		$('#ruletable tbody tr').find('td:first :checkbox').each(function() {
545
		$(this).prop('checked', checkedStatus);
546
		});
547
	});
548
});
549
//]]>
550
</script>
551
<?php
552

    
553
if (count($a_nat) > 0) {
554
?>
555
<!-- Legend -->
556
<div>
557
	<dl class="dl-horizontal responsive">
558
		<dt><?=gettext('Legend')?></dt>					<dd></dd>
559
		<dt><i class="fa fa-play"></i></dt>			<dd><?=gettext('Pass')?></dd>
560
		<dt><i class="fa fa-random"></i></dt>		<dd><?=gettext('Linked rule')?></dd>
561
	</dl>
562
</div>
563

    
564
<?php
565
}
566

    
567
include("foot.inc");
(43-43/230)