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-2018 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'])) {
40
	$config['nat'] = array();
41
}
42
if (!is_array($config['nat']['rule'])) {
43
	$config['nat']['rule'] = array();
44
}
45

    
46
$a_nat = &$config['nat']['rule'];
47

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

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

    
58
		$a_nat = $a_nat_new;
59

    
60

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

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

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

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

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

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

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

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

    
91
	$retval = 0;
92

    
93
	$retval |= filter_configure();
94

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

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

    
102
}
103

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

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

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

    
114
		// Update the separators
115
		$a_separators = &$config['nat']['separator'];
116
		$ridx = $_POST['id'];
117
		$mvnrows = -1;
118
		move_separators($a_separators, $ridx, $mvnrows);
119

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

    
127
		header("Location: firewall_nat.php");
128
		exit;
129
	}
130
}
131

    
132
if (isset($_POST['del_x']) && have_natpfruleint_access($natent['interface'])) {
133

    
134
	/* delete selected rules */
135
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
136
		$a_separators = &$config['nat']['separator'];
137
		$num_deleted = 0;
138

    
139
		foreach ($_POST['rule'] as $rulei) {
140
			$target = $rule['target'];
141

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

    
148
			unset($a_nat[$rulei]);
149

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

    
158
		if (write_config()) {
159
			mark_subsystem_dirty('natconf');
160
		}
161

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

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

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

    
191
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
192
$pglinks = array("", "@self", "@self");
193
include("head.inc");
194

    
195
if ($_POST['apply']) {
196
	print_apply_result_box($retval);
197
}
198

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

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

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

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

    
247
$nnats = $i = 0;
248
$separators = $config['nat']['separator'];
249

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

    
254
foreach ($a_nat as $natent):
255

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

    
261
	$localport = $natent['local-port'];
262

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

    
265
	if ($dstendport) {
266
		$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
267
		$localport	 .= '-' . $localendport;
268
	}
269

    
270
	$alias = rule_columns_with_alias(
271
		$natent['source']['address'],
272
		pprint_port($natent['source']['port']),
273
		$natent['destination']['address'],
274
		pprint_port($natent['destination']['port']),
275
		$natent['target'],
276
		$localport
277
	);
278

    
279
	if (isset($natent['disabled'])) {
280
		$iconfn = "pass_d";
281
		$trclass = 'class="disabled"';
282
	} else {
283
		$iconfn = "pass";
284
		$trclass = '';
285
	}
286
?>
287

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

    
329
						<td>
330
							<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
331
						</td>
332

    
333
						<td>
334

    
335

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

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

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

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

    
457
endforeach;
458

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

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

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

    
505
events.push(function() {
506

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

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

    
524
		// Save the separator bar configuration
525
		save_separators();
526

    
527
		// Suppress the "Do you really want to leave the page" message
528
		saving = true;
529

    
530
	});
531

    
532
	// Globals
533
	saving = false;
534
	dirty = false;
535

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

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

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

    
567
<?php
568
}
569

    
570
include("foot.inc");
(42-42/234)