Project

General

Profile

Download (15 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-2016 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']['rule'])) {
40
	$config['nat']['rule'] = array();
41
}
42

    
43
$a_nat = &$config['nat']['rule'];
44

    
45
/* update rule order, POST[rule] is an array of ordered IDs */
46
if (array_key_exists('order-store', $_POST)) {
47
	if (is_array($_POST['rule']) && !empty($_POST['rule'])) {
48
		$a_nat_new = array();
49

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

    
55
		$a_nat = $a_nat_new;
56

    
57

    
58
		$config['nat']['separator'] = "";
59

    
60
		if ($_POST['separator']) {
61
			$idx = 0;
62
			foreach ($_POST['separator'] as $separator) {
63
				$config['nat']['separator']['sep' . $idx++] = $separator;
64
			}
65
		}
66

    
67
		if (write_config()) {
68
			mark_subsystem_dirty('filter');
69
		}
70

    
71
		header("Location: firewall_nat.php");
72
		exit;
73
	}
74
}
75

    
76
/* if a custom message has been passed along, lets process it */
77
if ($_GET['savemsg']) {
78
	$savemsg = $_GET['savemsg'];
79
}
80

    
81
if ($_POST) {
82
	$pconfig = $_POST;
83

    
84
	if ($_POST['apply']) {
85

    
86
		$retval = 0;
87

    
88
		$retval |= filter_configure();
89
		$savemsg = get_std_save_message($retval);
90

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

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

    
98
	}
99
}
100

    
101
if ($_GET['act'] == "del") {
102
	if ($a_nat[$_GET['id']]) {
103

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

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

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

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

    
128
if (isset($_POST['del_x'])) {
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
} else if ($_GET['act'] == "toggle") {
161
	if ($a_nat[$_GET['id']]) {
162
		if (isset($a_nat[$_GET['id']]['disabled'])) {
163
			unset($a_nat[$_GET['id']]['disabled']);
164
			$rule_status = true;
165
		} else {
166
			$a_nat[$_GET['id']]['disabled'] = true;
167
			$rule_status = false;
168
		}
169

    
170
		// Check for filter rule associations
171
		if (isset($a_nat[$_GET['id']]['associated-rule-id'])) {
172
			toggle_id($a_nat[$_GET['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
include("head.inc");
188

    
189
if ($savemsg) {
190
	print_info_box($savemsg, 'success');
191
}
192

    
193
if (is_subsystem_dirty('natconf')) {
194
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
195
					gettext('The changes must be applied for them to take effect.'));
196
}
197

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

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

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

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

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

    
248
foreach ($a_nat as $natent):
249

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

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

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

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

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

    
273
	/* if user does not have access to edit an interface skip on to the next record */
274
	if (!have_natpfruleint_access($natent['interface'])) {
275
		continue;
276
	}
277

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

    
287
					<tr id="fr<?=$nnats;?>" <?=$trclass?> onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_edit.php?id=<?=$i;?>';">
288
						<td >
289
							<input type="checkbox" id="frc<?=$nnats;?>" onClick="fr_toggle(<?=$nnats;?>)" name="rule[]" value="<?=$i;?>"/>
290
						</td>
291
						<td>
292
							<a href="?act=toggle&amp;id=<?=$i?>">
293
								<i class="fa fa-check" title="<?=gettext("click to toggle enabled/disabled status")?>"></i>
294
<?php 	if (isset($natent['nordr'])) { ?>
295
								&nbsp;<i class="fa fa-hand-stop-o text-danger" title="<?=gettext("Negated: This rule excludes NAT from a later rule")?>"></i>
296
<?php 	} ?>
297
							</a>
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('_', ' ', 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('_', ' ', 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('_', ' ', 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('_', ' ', 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('_', ' ', 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('_', ' ', 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
							<a class="fa fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
440
							<a class="fa fa-clone"	  title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
441
							<a class="fa fa-trash"	title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&amp;id=<?=$i?>"></a>
442
						</td>
443
					</tr>
444
<?php
445
	$i++;
446
	$nnats++;
447

    
448
endforeach;
449

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

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

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

    
494
events.push(function() {
495

    
496
	// Make rules sortable
497
	$('table tbody.user-entries').sortable({
498
		cursor: 'grabbing',
499
		update: function(event, ui) {
500
			$('#order-store').removeAttr('disabled');
501
			dirty = true;
502
			reindex_rules(ui.item.parent('tbody'));
503
			dirty = true;
504
		}
505
	});
506

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

    
511
		// Save the separator bar configuration
512
		save_separators();
513

    
514
		// Suppress the "Do you really want to leave the page" message
515
		saving = true;
516

    
517
	});
518

    
519
	// Globals
520
	saving = false;
521
	dirty = false;
522

    
523
	// provide a warning message if the user tries to change page before saving
524
	$(window).bind('beforeunload', function(){
525
		if (!saving && dirty) {
526
			return ("<?=gettext('One or more Port Forward rules have been moved but have not yet been saved')?>");
527
		} else {
528
			return undefined;
529
		}
530
	});
531
});
532
//]]>
533
</script>
534
<?php
535

    
536
if (count($a_nat) > 0) {
537
?>
538
<!-- Legend -->
539
<div>
540
	<dl class="dl-horizontal responsive">
541
		<dt><?=gettext('Legend')?></dt>					<dd></dd>
542
		<dt><i class="fa fa-play"></i></dt>			<dd><?=gettext('Pass')?></dd>
543
		<dt><i class="fa fa-random"></i></dt>		<dd><?=gettext('Linked rule')?></dd>
544
	</dl>
545
</div>
546

    
547
<?php
548
}
549

    
550
include("foot.inc");
(39-39/225)