Project

General

Profile

Download (16.8 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-2021 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
if (array_key_exists('order-store', $_REQUEST) && have_natpfruleint_access($natent['interface'])) {
55
	$updated = false;
56
	$dirty = false;
57

    
58
	/* update rule order, POST[rule] is an array of ordered IDs */
59
	if (is_array($_POST['rule']) && !empty($_POST['rule'])) {
60
		$a_nat_new = array();
61

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

    
67
		if ($a_nat !== $a_nat_new) {
68
			$a_nat = $a_nat_new;
69
			$dirty = true;
70
		}
71
	}
72

    
73
	/* update separator order, POST[separator] is an array of ordered IDs */
74
	if (is_array($_POST['separator']) && !empty($_POST['separator'])) {
75
		$new_separator = array();
76
		$idx = 0;
77

    
78
		foreach ($_POST['separator'] as $separator) {
79
			$new_separator['sep' . $idx++] = $separator;
80
		}
81

    
82
		if ($a_separators !== $new_separator) {
83
			$a_separators = $new_separator;
84
			$updated = true;
85
		}			
86
	} else if (!empty($a_separators)) {
87
		$a_separators = "";
88
		$updated = true;
89
	}
90

    
91
	if ($updated || $dirty) {
92
		if (write_config("NAT: Rule order changed")) {
93
			if ($dirty) {
94
				mark_subsystem_dirty('natconf');
95
			}
96
		}
97
	}
98

    
99
	header("Location: firewall_nat.php");
100
	exit;
101
}
102

    
103
/* if a custom message has been passed along, lets process it */
104
if ($_REQUEST['savemsg']) {
105
	$savemsg = $_REQUEST['savemsg'];
106
}
107

    
108
if ($_POST['apply'] && have_natpfruleint_access($natent['interface'])) {
109

    
110
	$retval = 0;
111

    
112
	$retval |= filter_configure();
113

    
114
	pfSense_handle_custom_code("/usr/local/pkg/firewall_nat/apply");
115

    
116
	if ($retval == 0) {
117
		clear_subsystem_dirty('natconf');
118
		clear_subsystem_dirty('filter');
119
	}
120

    
121
}
122

    
123
if (($_POST['act'] == "del") && have_natpfruleint_access($natent['interface'])) {
124
	if ($a_nat[$_POST['id']]) {
125

    
126
		if (isset($a_nat[$_POST['id']]['associated-rule-id'])) {
127
			delete_id($a_nat[$_POST['id']]['associated-rule-id'], $config['filter']['rule']);
128
			$want_dirty_filter = true;
129
		}
130

    
131
		unset($a_nat[$_POST['id']]);
132

    
133
		// Update the separators
134
		$ridx = $_POST['id'];
135
		$mvnrows = -1;
136
		move_separators($a_separators, $ridx, $mvnrows);
137

    
138
		if (write_config("NAT: Rule deleted")) {
139
			mark_subsystem_dirty('natconf');
140
			if ($want_dirty_filter) {
141
				mark_subsystem_dirty('filter');
142
			}
143
		}
144

    
145
		header("Location: firewall_nat.php");
146
		exit;
147
	}
148
}
149

    
150
if (isset($_POST['del_x']) && have_natpfruleint_access($natent['interface'])) {
151

    
152
	/* delete selected rules */
153
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
154
		$num_deleted = 0;
155

    
156
		foreach ($_POST['rule'] as $rulei) {
157
			$target = $rule['target'];
158

    
159
			// Check for filter rule associations
160
			if (isset($a_nat[$rulei]['associated-rule-id'])) {
161
				delete_id($a_nat[$rulei]['associated-rule-id'], $config['filter']['rule']);
162
				mark_subsystem_dirty('filter');
163
			}
164

    
165
			unset($a_nat[$rulei]);
166

    
167
			// Update the separators
168
			// As rules are deleted, $ridx has to be decremented or separator position will break
169
			$ridx = $rulei - $num_deleted;
170
			$mvnrows = -1;
171
			move_separators($a_separators, $ridx, $mvnrows);
172
			$num_deleted++;
173
		}
174

    
175
		if (write_config("NAT: Rule deleted")) {
176
			mark_subsystem_dirty('natconf');
177
		}
178

    
179
		header("Location: firewall_nat.php");
180
		exit;
181
	}
182
} elseif (($_POST['act'] == "toggle") && have_natpfruleint_access($natent['interface'])) {
183
	if ($a_nat[$_POST['id']]) {
184
		if (isset($a_nat[$_POST['id']]['disabled'])) {
185
			unset($a_nat[$_POST['id']]['disabled']);
186
			$rule_status = true;
187
		} else {
188
			$a_nat[$_POST['id']]['disabled'] = true;
189
			$rule_status = false;
190
		}
191

    
192
		// Check for filter rule associations
193
		if (isset($a_nat[$_POST['id']]['associated-rule-id'])) {
194
			toggle_id($a_nat[$_POST['id']]['associated-rule-id'],
195
			    $config['filter']['rule'], $rule_status);
196
			unset($rule_status);
197
			mark_subsystem_dirty('filter');
198
		}
199

    
200
		if (write_config(gettext("Firewall: NAT: Port forward, enable/disable NAT rule"))) {
201
			mark_subsystem_dirty('natconf');
202
		}
203
		header("Location: firewall_nat.php");
204
		exit;
205
	}
206
}
207

    
208
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
209
$pglinks = array("", "@self", "@self");
210
include("head.inc");
211

    
212
if ($_POST['apply']) {
213
	print_apply_result_box($retval);
214
}
215

    
216
if (is_subsystem_dirty('natconf') && have_natpfruleint_access($natent['interface'])) {
217
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
218
					gettext('The changes must be applied for them to take effect.'));
219
}
220

    
221
$tab_array = array();
222
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
223
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
224
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
225
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
226
display_top_tabs($tab_array);
227

    
228
$columns_in_table = 13;
229
?>
230
<!-- Allow table to scroll when dragging outside of the display window -->
231
<style>
232
.table-responsive {
233
    clear: both;
234
    overflow-x: visible;
235
    margin-bottom: 0px;
236
}
237
</style>
238

    
239
<form action="firewall_nat.php" method="post" name="iform">
240
	<div class="panel panel-default">
241
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Rules')?></h2></div>
242
		<div class="panel-body table-responsive">
243
			<table id="ruletable" class="table table-striped table-hover table-condensed">
244
				<thead>
245
					<tr>
246
						<th style="padding-left:10px;">  <input type="checkbox" id="selectAll" name="selectAll" /></th>
247
						<th><!-- Icon --></th>
248
						<th><!-- Rule type --></th>
249
						<th><?=gettext("Interface")?></th>
250
						<th><?=gettext("Protocol")?></th>
251
						<th><?=gettext("Source Address")?></th>
252
						<th><?=gettext("Source Ports")?></th>
253
						<th><?=gettext("Dest. Address")?></th>
254
						<th><?=gettext("Dest. Ports")?></th>
255
						<th><?=gettext("NAT IP")?></th>
256
						<th><?=gettext("NAT Ports")?></th>
257
						<th><?=gettext("Description")?></th>
258
						<th><?=gettext("Actions")?></th>
259
					</tr>
260
				</thead>
261
				<tbody class='user-entries'>
262
<?php
263

    
264
$nnats = $i = 0;
265
$separators = $config['nat']['separator'];
266

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

    
271
foreach ($a_nat as $natent):
272

    
273
	// Display separator(s) for section beginning at rule n
274
	if ($seprows[$nnats]) {
275
		display_separator($separators, $nnats, $columns_in_table);
276
	}
277

    
278
	$localport = $natent['local-port'];
279

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

    
282
	if ($dstendport) {
283
		$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
284
		$localport	 .= '-' . $localendport;
285
	}
286

    
287
	$alias = rule_columns_with_alias(
288
		$natent['source']['address'],
289
		pprint_port($natent['source']['port']),
290
		$natent['destination']['address'],
291
		pprint_port($natent['destination']['port']),
292
		$natent['target'],
293
		$localport
294
	);
295

    
296
	if (isset($natent['disabled'])) {
297
		$iconfn = "pass_d";
298
		$trclass = 'class="disabled"';
299
	} else {
300
		$iconfn = "pass";
301
		$trclass = '';
302
	}
303

    
304
	if (is_specialnet($natent['target'])) {
305
		foreach ($ifdisp as $kif => $kdescr) {
306
			if ($natent['target'] == "{$kif}ip") {
307
				$natent['target'] = $kdescr . ' address';
308
				break;
309
			}
310
		}
311
	}
312
?>
313

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

    
355
						<td>
356
							<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
357
						</td>
358

    
359
						<td>
360

    
361

    
362
<?php
363
	if (isset($alias['src'])):
364
?>
365
							<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">
366
<?php
367
	endif;
368
?>
369
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['source'])))?>
370
<?php
371
	if (isset($alias['src'])):
372
?>
373
							</a>
374
<?php
375
	endif;
376
?>
377
						</td>
378
						<td>
379
<?php
380
	if (isset($alias['srcport'])):
381
?>
382
							<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">
383
<?php
384
	endif;
385
?>
386
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['source']['port'])))?>
387
<?php
388
	if (isset($alias['srcport'])):
389
?>
390
							</a>
391
<?php
392
	endif;
393
?>
394
						</td>
395

    
396
						<td>
397
<?php
398
	if (isset($alias['dst'])):
399
?>
400
							<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">
401
<?php
402
	endif;
403
?>
404
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['destination'])))?>
405
<?php
406
	if (isset($alias['dst'])):
407
?>
408
							</a>
409
<?php
410
	endif;
411
?>
412
						</td>
413
						<td>
414
<?php
415
	if (isset($alias['dstport'])):
416
?>
417
							<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">
418
<?php
419
	endif;
420
?>
421
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['destination']['port'])))?>
422
<?php
423
	if (isset($alias['dstport'])):
424
?>
425
							</a>
426
<?php
427
	endif;
428
?>
429
						</td>
430
						<td>
431
<?php
432
	if (isset($alias['target'])):
433
?>
434
							<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" >
435
<?php
436
	endif;
437
?>
438

    
439
							<?=str_replace('_', '_<wbr>', htmlspecialchars($natent['target']))?>
440
<?php
441
	if (isset($alias['target'])):
442
?>
443
							</a>
444
<?php
445
	endif;
446
?>
447
						</td>
448
						<td>
449
<?php
450
	if (isset($alias['targetport'])):
451
?>
452
							<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">
453
<?php
454
	endif;
455
?>
456
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($localport)))?>
457
<?php
458
	if (isset($alias['targetport'])):
459
?>
460
							</a>
461
<?php
462
	endif;
463
?>
464
						</td>
465

    
466
						<td>
467
							<?=htmlspecialchars($natent['descr'])?>
468
						</td>
469
						<td>
470
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
471
							<a class="fa fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
472
							<a class="fa fa-clone"	  title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
473
							<a class="fa fa-trash"	title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&amp;id=<?=$i?>" usepost></a>
474
<?php	else: ?>
475
							-
476
<?php	endif; ?>
477
						</td>
478
					</tr>
479
<?php
480
	$i++;
481
	$nnats++;
482

    
483
endforeach;
484

    
485
// There can be separator(s) after the last rule listed.
486
if ($seprows[$nnats]) {
487
	display_separator($separators, $nnats, $columns_in_table);
488
}
489
?>
490
				</tbody>
491
			</table>
492
		</div>
493
	</div>
494

    
495
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
496
	<nav class="action-buttons">
497
		<a href="firewall_nat_edit.php?after=-1" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
498
			<i class="fa fa-level-up icon-embed-btn"></i>
499
			<?=gettext('Add')?>
500
		</a>
501
		<a href="firewall_nat_edit.php" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
502
			<i class="fa fa-level-down icon-embed-btn"></i>
503
			<?=gettext('Add')?>
504
		</a>
505
		<button name="del_x" type="submit" class="btn btn-danger btn-sm" title="<?=gettext('Delete selected rules')?>">
506
			<i class="fa fa-trash icon-embed-btn"></i>
507
			<?=gettext("Delete"); ?>
508
		</button>
509
		<button type="submit" id="order-store" name="order-store" class="btn btn-primary btn-sm" disabled title="<?=gettext('Save rule order')?>">
510
			<i class="fa fa-save icon-embed-btn"></i>
511
			<?=gettext("Save")?>
512
		</button>
513
		<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
514
			<i class="fa fa-plus icon-embed-btn"></i>
515
			<?=gettext("Separator")?>
516
		</button>
517
	</nav>
518
<?php	endif; ?>
519
</form>
520

    
521
<script type="text/javascript">
522
//<![CDATA[
523
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
524
iface = "<?=strtolower($if)?>";
525
cncltxt = '<?=gettext("Cancel")?>';
526
svtxt = '<?=gettext("Save")?>';
527
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
528
configsection = "nat";
529
dirty = false;
530

    
531
events.push(function() {
532

    
533
<?php if(!isset($config['system']['webgui']['roworderdragging'])): ?>
534
	// Make rules sortable
535
	$('table tbody.user-entries').sortable({
536
		cursor: 'grabbing',
537
		update: function(event, ui) {
538
			$('#order-store').removeAttr('disabled');
539
			dirty = true;
540
			reindex_rules(ui.item.parent('tbody'));
541
			dirty = true;
542
		}
543
	});
544
<?php endif; ?>
545

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

    
550
		// Save the separator bar configuration
551
		save_separators();
552

    
553
		// Suppress the "Do you really want to leave the page" message
554
		saving = true;
555

    
556
	});
557

    
558
	// Globals
559
	saving = false;
560
	dirty = false;
561

    
562
	// provide a warning message if the user tries to change page before saving
563
	$(window).bind('beforeunload', function(){
564
		if (!saving && dirty) {
565
			return ("<?=gettext('One or more Port Forward rules have been moved but have not yet been saved')?>");
566
		} else {
567
			return undefined;
568
		}
569
	});
570

    
571
	$('#selectAll').click(function() {
572
		var checkedStatus = this.checked;
573
		$('#ruletable tbody tr').find('td:first :checkbox').each(function() {
574
		$(this).prop('checked', checkedStatus);
575
		});
576
	});
577
});
578
//]]>
579
</script>
580
<?php
581

    
582
if (count($a_nat) > 0) {
583
?>
584
<!-- Legend -->
585
<div>
586
	<dl class="dl-horizontal responsive">
587
		<dt><?=gettext('Legend')?></dt>					<dd></dd>
588
		<dt><i class="fa fa-play"></i></dt>			<dd><?=gettext('Pass')?></dd>
589
		<dt><i class="fa fa-random"></i></dt>		<dd><?=gettext('Linked rule')?></dd>
590
	</dl>
591
</div>
592

    
593
<?php
594
}
595

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