Project

General

Profile

Download (16.5 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
$specialsrcdst = explode(" ", "any pptp pppoe l2tp openvpn");
48
$ifdisp = get_configured_interface_with_descr();
49
foreach ($ifdisp as $kif => $kdescr) {
50
	$specialsrcdst[] = "{$kif}";
51
	$specialsrcdst[] = "{$kif}ip";
52
}
53

    
54
/* update rule order, POST[rule] is an array of ordered IDs */
55
if (array_key_exists('order-store', $_REQUEST) && have_natpfruleint_access($natent['interface'])) {
56
	if (is_array($_REQUEST['rule']) && !empty($_REQUEST['rule'])) {
57
		$a_nat_new = array();
58

    
59
		// if a rule is not in POST[rule], it has been deleted by the user
60
		foreach ($_POST['rule'] as $id) {
61
			$a_nat_new[] = $a_nat[$id];
62
		}
63

    
64
		$a_nat = $a_nat_new;
65

    
66

    
67
		$config['nat']['separator'] = "";
68

    
69
		if ($_POST['separator']) {
70
			$idx = 0;
71

    
72
			if (!is_array($config['nat']['separator'])) {
73
				$config['nat']['separator'] = array();
74
			}
75

    
76
			foreach ($_POST['separator'] as $separator) {
77
				$config['nat']['separator']['sep' . $idx++] = $separator;
78
			}
79
		}
80

    
81
		if (write_config("NAT: Rule order changed")) {
82
			mark_subsystem_dirty('filter');
83
		}
84

    
85
		header("Location: firewall_nat.php");
86
		exit;
87
	}
88
}
89

    
90
/* if a custom message has been passed along, lets process it */
91
if ($_REQUEST['savemsg']) {
92
	$savemsg = $_REQUEST['savemsg'];
93
}
94

    
95
if ($_POST['apply'] && have_natpfruleint_access($natent['interface'])) {
96

    
97
	$retval = 0;
98

    
99
	$retval |= filter_configure();
100

    
101
	pfSense_handle_custom_code("/usr/local/pkg/firewall_nat/apply");
102

    
103
	if ($retval == 0) {
104
		clear_subsystem_dirty('natconf');
105
		clear_subsystem_dirty('filter');
106
	}
107

    
108
}
109

    
110
if (($_POST['act'] == "del") && have_natpfruleint_access($natent['interface'])) {
111
	if ($a_nat[$_POST['id']]) {
112

    
113
		if (isset($a_nat[$_POST['id']]['associated-rule-id'])) {
114
			delete_id($a_nat[$_POST['id']]['associated-rule-id'], $config['filter']['rule']);
115
			$want_dirty_filter = true;
116
		}
117

    
118
		unset($a_nat[$_POST['id']]);
119

    
120
		// Update the separators
121
		$ridx = $_POST['id'];
122
		$mvnrows = -1;
123
		move_separators($a_separators, $ridx, $mvnrows);
124

    
125
		if (write_config("NAT: Rule deleted")) {
126
			mark_subsystem_dirty('natconf');
127
			if ($want_dirty_filter) {
128
				mark_subsystem_dirty('filter');
129
			}
130
		}
131

    
132
		header("Location: firewall_nat.php");
133
		exit;
134
	}
135
}
136

    
137
if (isset($_POST['del_x']) && have_natpfruleint_access($natent['interface'])) {
138

    
139
	/* delete selected rules */
140
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
141
		$num_deleted = 0;
142

    
143
		foreach ($_POST['rule'] as $rulei) {
144
			$target = $rule['target'];
145

    
146
			// Check for filter rule associations
147
			if (isset($a_nat[$rulei]['associated-rule-id'])) {
148
				delete_id($a_nat[$rulei]['associated-rule-id'], $config['filter']['rule']);
149
				mark_subsystem_dirty('filter');
150
			}
151

    
152
			unset($a_nat[$rulei]);
153

    
154
			// Update the separators
155
			// As rules are deleted, $ridx has to be decremented or separator position will break
156
			$ridx = $rulei - $num_deleted;
157
			$mvnrows = -1;
158
			move_separators($a_separators, $ridx, $mvnrows);
159
			$num_deleted++;
160
		}
161

    
162
		if (write_config("NAT: Rule deleted")) {
163
			mark_subsystem_dirty('natconf');
164
		}
165

    
166
		header("Location: firewall_nat.php");
167
		exit;
168
	}
169
} elseif (($_POST['act'] == "toggle") && have_natpfruleint_access($natent['interface'])) {
170
	if ($a_nat[$_POST['id']]) {
171
		if (isset($a_nat[$_POST['id']]['disabled'])) {
172
			unset($a_nat[$_POST['id']]['disabled']);
173
			$rule_status = true;
174
		} else {
175
			$a_nat[$_POST['id']]['disabled'] = true;
176
			$rule_status = false;
177
		}
178

    
179
		// Check for filter rule associations
180
		if (isset($a_nat[$_POST['id']]['associated-rule-id'])) {
181
			toggle_id($a_nat[$_POST['id']]['associated-rule-id'],
182
			    $config['filter']['rule'], $rule_status);
183
			unset($rule_status);
184
			mark_subsystem_dirty('filter');
185
		}
186

    
187
		if (write_config(gettext("Firewall: NAT: Port forward, enable/disable NAT rule"))) {
188
			mark_subsystem_dirty('natconf');
189
		}
190
		header("Location: firewall_nat.php");
191
		exit;
192
	}
193
}
194

    
195
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
196
$pglinks = array("", "@self", "@self");
197
include("head.inc");
198

    
199
if ($_POST['apply']) {
200
	print_apply_result_box($retval);
201
}
202

    
203
if (is_subsystem_dirty('natconf') && have_natpfruleint_access($natent['interface'])) {
204
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
205
					gettext('The changes must be applied for them to take effect.'));
206
}
207

    
208
$tab_array = array();
209
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
210
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
211
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
212
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
213
display_top_tabs($tab_array);
214

    
215
$columns_in_table = 13;
216
?>
217
<!-- Allow table to scroll when dragging outside of the display window -->
218
<style>
219
.table-responsive {
220
    clear: both;
221
    overflow-x: visible;
222
    margin-bottom: 0px;
223
}
224
</style>
225

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

    
251
$nnats = $i = 0;
252
$separators = $config['nat']['separator'];
253

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

    
258
foreach ($a_nat as $natent):
259

    
260
	// Display separator(s) for section beginning at rule n
261
	if ($seprows[$nnats]) {
262
		display_separator($separators, $nnats, $columns_in_table);
263
	}
264

    
265
	$localport = $natent['local-port'];
266

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

    
269
	if ($dstendport) {
270
		$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
271
		$localport	 .= '-' . $localendport;
272
	}
273

    
274
	$alias = rule_columns_with_alias(
275
		$natent['source']['address'],
276
		pprint_port($natent['source']['port']),
277
		$natent['destination']['address'],
278
		pprint_port($natent['destination']['port']),
279
		$natent['target'],
280
		$localport
281
	);
282

    
283
	if (isset($natent['disabled'])) {
284
		$iconfn = "pass_d";
285
		$trclass = 'class="disabled"';
286
	} else {
287
		$iconfn = "pass";
288
		$trclass = '';
289
	}
290

    
291
	if (is_specialnet($natent['target'])) {
292
		foreach ($ifdisp as $kif => $kdescr) {
293
			if ($natent['target'] == "{$kif}ip") {
294
				$natent['target'] = $kdescr . ' address';
295
				break;
296
			}
297
		}
298
	}
299
?>
300

    
301
					<tr id="fr<?=$nnats;?>" <?=$trclass?> onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_edit.php?id=<?=$i;?>';">
302
						<td >
303
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
304
							<input type="checkbox" id="frc<?=$nnats;?>" onClick="fr_toggle(<?=$nnats;?>)" name="rule[]" value="<?=$i;?>"/>
305
<?php	endif; ?>
306
						</td>
307
						<td>
308
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
309
							<a href="?act=toggle&amp;id=<?=$i?>" usepost>
310
								<i class="fa fa-check" title="<?=gettext("click to toggle enabled/disabled status")?>"></i>
311
							</a>
312
<?php	endif; ?>
313
<?php 	if (isset($natent['nordr'])) { ?>
314
								&nbsp;<i class="fa fa-hand-stop-o text-danger" title="<?=gettext("Negated: This rule excludes NAT from a later rule")?>"></i>
315
<?php 	} ?>
316
						</td>
317
						<td>
318
<?php
319
	if ($natent['associated-rule-id'] == "pass"):
320
?>
321
							<i class="fa fa-play" title="<?=gettext("All traffic matching this NAT entry is passed")?>"></i>
322
<?php
323
	elseif (!empty($natent['associated-rule-id'])):
324
?>
325
							<i class="fa fa-random" title="<?=sprintf(gettext("Firewall rule ID %s is managed by this rule"), htmlspecialchars($natent['associated-rule-id']))?>"></i>
326
<?php
327
	endif;
328
?>
329
						</td>
330
						<td>
331
							<?=$textss?>
332
<?php
333
	if (!$natent['interface']) {
334
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr("wan"));
335
	} else {
336
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr($natent['interface']));
337
	}
338
?>
339
							<?=$textse?>
340
						</td>
341

    
342
						<td>
343
							<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
344
						</td>
345

    
346
						<td>
347

    
348

    
349
<?php
350
	if (isset($alias['src'])):
351
?>
352
							<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">
353
<?php
354
	endif;
355
?>
356
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['source'])))?>
357
<?php
358
	if (isset($alias['src'])):
359
?>
360
							</a>
361
<?php
362
	endif;
363
?>
364
						</td>
365
						<td>
366
<?php
367
	if (isset($alias['srcport'])):
368
?>
369
							<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">
370
<?php
371
	endif;
372
?>
373
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['source']['port'])))?>
374
<?php
375
	if (isset($alias['srcport'])):
376
?>
377
							</a>
378
<?php
379
	endif;
380
?>
381
						</td>
382

    
383
						<td>
384
<?php
385
	if (isset($alias['dst'])):
386
?>
387
							<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">
388
<?php
389
	endif;
390
?>
391
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['destination'])))?>
392
<?php
393
	if (isset($alias['dst'])):
394
?>
395
							</a>
396
<?php
397
	endif;
398
?>
399
						</td>
400
						<td>
401
<?php
402
	if (isset($alias['dstport'])):
403
?>
404
							<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">
405
<?php
406
	endif;
407
?>
408
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['destination']['port'])))?>
409
<?php
410
	if (isset($alias['dstport'])):
411
?>
412
							</a>
413
<?php
414
	endif;
415
?>
416
						</td>
417
						<td>
418
<?php
419
	if (isset($alias['target'])):
420
?>
421
							<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" >
422
<?php
423
	endif;
424
?>
425

    
426
							<?=str_replace('_', '_<wbr>', htmlspecialchars($natent['target']))?>
427
<?php
428
	if (isset($alias['target'])):
429
?>
430
							</a>
431
<?php
432
	endif;
433
?>
434
						</td>
435
						<td>
436
<?php
437
	if (isset($alias['targetport'])):
438
?>
439
							<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">
440
<?php
441
	endif;
442
?>
443
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($localport)))?>
444
<?php
445
	if (isset($alias['targetport'])):
446
?>
447
							</a>
448
<?php
449
	endif;
450
?>
451
						</td>
452

    
453
						<td>
454
							<?=htmlspecialchars($natent['descr'])?>
455
						</td>
456
						<td>
457
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
458
							<a class="fa fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
459
							<a class="fa fa-clone"	  title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
460
							<a class="fa fa-trash"	title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&amp;id=<?=$i?>" usepost></a>
461
<?php	else: ?>
462
							-
463
<?php	endif; ?>
464
						</td>
465
					</tr>
466
<?php
467
	$i++;
468
	$nnats++;
469

    
470
endforeach;
471

    
472
// There can be separator(s) after the last rule listed.
473
if ($seprows[$nnats]) {
474
	display_separator($separators, $nnats, $columns_in_table);
475
}
476
?>
477
				</tbody>
478
			</table>
479
		</div>
480
	</div>
481

    
482
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
483
	<nav class="action-buttons">
484
		<a href="firewall_nat_edit.php?after=-1" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
485
			<i class="fa fa-level-up icon-embed-btn"></i>
486
			<?=gettext('Add')?>
487
		</a>
488
		<a href="firewall_nat_edit.php" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
489
			<i class="fa fa-level-down icon-embed-btn"></i>
490
			<?=gettext('Add')?>
491
		</a>
492
		<button name="del_x" type="submit" class="btn btn-danger btn-sm" title="<?=gettext('Delete selected rules')?>">
493
			<i class="fa fa-trash icon-embed-btn"></i>
494
			<?=gettext("Delete"); ?>
495
		</button>
496
		<button type="submit" id="order-store" name="order-store" class="btn btn-primary btn-sm" disabled title="<?=gettext('Save rule order')?>">
497
			<i class="fa fa-save icon-embed-btn"></i>
498
			<?=gettext("Save")?>
499
		</button>
500
		<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
501
			<i class="fa fa-plus icon-embed-btn"></i>
502
			<?=gettext("Separator")?>
503
		</button>
504
	</nav>
505
<?php	endif; ?>
506
</form>
507

    
508
<script type="text/javascript">
509
//<![CDATA[
510
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
511
iface = "<?=strtolower($if)?>";
512
cncltxt = '<?=gettext("Cancel")?>';
513
svtxt = '<?=gettext("Save")?>';
514
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
515
configsection = "nat";
516
dirty = false;
517

    
518
events.push(function() {
519

    
520
<?php if(!isset($config['system']['webgui']['roworderdragging'])): ?>
521
	// Make rules sortable
522
	$('table tbody.user-entries').sortable({
523
		cursor: 'grabbing',
524
		update: function(event, ui) {
525
			$('#order-store').removeAttr('disabled');
526
			dirty = true;
527
			reindex_rules(ui.item.parent('tbody'));
528
			dirty = true;
529
		}
530
	});
531
<?php endif; ?>
532

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

    
537
		// Save the separator bar configuration
538
		save_separators();
539

    
540
		// Suppress the "Do you really want to leave the page" message
541
		saving = true;
542

    
543
	});
544

    
545
	// Globals
546
	saving = false;
547
	dirty = false;
548

    
549
	// provide a warning message if the user tries to change page before saving
550
	$(window).bind('beforeunload', function(){
551
		if (!saving && dirty) {
552
			return ("<?=gettext('One or more Port Forward rules have been moved but have not yet been saved')?>");
553
		} else {
554
			return undefined;
555
		}
556
	});
557

    
558
	$('#selectAll').click(function() {
559
		var checkedStatus = this.checked;
560
		$('#ruletable tbody tr').find('td:first :checkbox').each(function() {
561
		$(this).prop('checked', checkedStatus);
562
		});
563
	});
564
});
565
//]]>
566
</script>
567
<?php
568

    
569
if (count($a_nat) > 0) {
570
?>
571
<!-- Legend -->
572
<div>
573
	<dl class="dl-horizontal responsive">
574
		<dt><?=gettext('Legend')?></dt>					<dd></dd>
575
		<dt><i class="fa fa-play"></i></dt>			<dd><?=gettext('Pass')?></dd>
576
		<dt><i class="fa fa-random"></i></dt>		<dd><?=gettext('Linked rule')?></dd>
577
	</dl>
578
</div>
579

    
580
<?php
581
}
582

    
583
include("foot.inc");
(42-42/229)