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
init_config_arr(array('nat', 'separator'));
40
init_config_arr(array('nat', 'rule'));
41
$a_nat = &$config['nat']['rule'];
42

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

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

    
53
		$a_nat = $a_nat_new;
54

    
55

    
56
		$config['nat']['separator'] = "";
57

    
58
		if ($_POST['separator']) {
59
			$idx = 0;
60

    
61
			if (!is_array($config['nat']['separator'])) {
62
				$config['nat']['separator'] = array();
63
			}
64

    
65
			foreach ($_POST['separator'] as $separator) {
66
				$config['nat']['separator']['sep' . $idx++] = $separator;
67
			}
68
		}
69

    
70
		if (write_config()) {
71
			mark_subsystem_dirty('filter');
72
		}
73

    
74
		header("Location: firewall_nat.php");
75
		exit;
76
	}
77
}
78

    
79
/* if a custom message has been passed along, lets process it */
80
if ($_REQUEST['savemsg']) {
81
	$savemsg = $_REQUEST['savemsg'];
82
}
83

    
84
if ($_POST['apply'] && have_natpfruleint_access($natent['interface'])) {
85

    
86
	$retval = 0;
87

    
88
	$retval |= filter_configure();
89

    
90
	pfSense_handle_custom_code("/usr/local/pkg/firewall_nat/apply");
91

    
92
	if ($retval == 0) {
93
		clear_subsystem_dirty('natconf');
94
		clear_subsystem_dirty('filter');
95
	}
96

    
97
}
98

    
99
if (($_POST['act'] == "del") && have_natpfruleint_access($natent['interface'])) {
100
	if ($a_nat[$_POST['id']]) {
101

    
102
		if (isset($a_nat[$_POST['id']]['associated-rule-id'])) {
103
			delete_id($a_nat[$_POST['id']]['associated-rule-id'], $config['filter']['rule']);
104
			$want_dirty_filter = true;
105
		}
106

    
107
		unset($a_nat[$_POST['id']]);
108

    
109
		// Update the separators
110
		$a_separators = &$config['nat']['separator'];
111
		$ridx = $_POST['id'];
112
		$mvnrows = -1;
113
		move_separators($a_separators, $ridx, $mvnrows);
114

    
115
		if (write_config()) {
116
			mark_subsystem_dirty('natconf');
117
			if ($want_dirty_filter) {
118
				mark_subsystem_dirty('filter');
119
			}
120
		}
121

    
122
		header("Location: firewall_nat.php");
123
		exit;
124
	}
125
}
126

    
127
if (isset($_POST['del_x']) && have_natpfruleint_access($natent['interface'])) {
128

    
129
	/* delete selected rules */
130
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
131
		$a_separators = &$config['nat']['separator'];
132
		$num_deleted = 0;
133

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

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

    
143
			unset($a_nat[$rulei]);
144

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

    
153
		if (write_config()) {
154
			mark_subsystem_dirty('natconf');
155
		}
156

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

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

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

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

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

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

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

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

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

    
242
$nnats = $i = 0;
243
$separators = $config['nat']['separator'];
244

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

    
249
foreach ($a_nat as $natent):
250

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

    
256
	$localport = $natent['local-port'];
257

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

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

    
265
	$alias = rule_columns_with_alias(
266
		$natent['source']['address'],
267
		pprint_port($natent['source']['port']),
268
		$natent['destination']['address'],
269
		pprint_port($natent['destination']['port']),
270
		$natent['target'],
271
		$localport
272
	);
273

    
274
	if (isset($natent['disabled'])) {
275
		$iconfn = "pass_d";
276
		$trclass = 'class="disabled"';
277
	} else {
278
		$iconfn = "pass";
279
		$trclass = '';
280
	}
281
?>
282

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

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

    
328
						<td>
329

    
330

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

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

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

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

    
452
endforeach;
453

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

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

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

    
500
events.push(function() {
501

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

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

    
519
		// Save the separator bar configuration
520
		save_separators();
521

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

    
525
	});
526

    
527
	// Globals
528
	saving = false;
529
	dirty = false;
530

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

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

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

    
562
<?php
563
}
564

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