Project

General

Profile

Download (14.4 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 Electric Sheep Fencing, LLC
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

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

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

    
140
				mark_subsystem_dirty('filter');
141
			}
142

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

    
145
			// Update the separators
146
			$ridx = $rulei;
147
			$mvnrows = -1;
148
			move_separators($a_separators, $ridx, $mvnrows);
149
		}
150

    
151
		if (write_config()) {
152
			mark_subsystem_dirty('natconf');
153
		}
154

    
155
		header("Location: firewall_nat.php");
156
		exit;
157
	}
158
} else if ($_GET['act'] == "toggle") {
159
	if ($a_nat[$_GET['id']]) {
160
		if (isset($a_nat[$_GET['id']]['disabled'])) {
161
			unset($a_nat[$_GET['id']]['disabled']);
162
		} else {
163
			$a_nat[$_GET['id']]['disabled'] = true;
164
		}
165
		if (write_config(gettext("Firewall: NAT: Port forward, enable/disable NAT rule"))) {
166
			mark_subsystem_dirty('natconf');
167
		}
168
		header("Location: firewall_nat.php");
169
		exit;
170
	}
171
}
172

    
173
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
174
include("head.inc");
175

    
176
if ($savemsg) {
177
	print_info_box($savemsg, 'success');
178
}
179

    
180
if (is_subsystem_dirty('natconf')) {
181
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
182
					gettext('The changes must be applied for them to take effect.'));
183
}
184

    
185
$tab_array = array();
186
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
187
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
188
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
189
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
190
display_top_tabs($tab_array);
191

    
192
$columns_in_table = 13;
193
?>
194

    
195
<form action="firewall_nat.php" method="post" name="iform">
196
	<div class="panel panel-default">
197
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Rules')?></h2></div>
198
		<div class="panel-body table-responsive">
199
			<table id="ruletable" class="table table-striped table-hover table-condensed">
200
				<thead>
201
					<tr>
202
						<th><!-- Checkbox --></th>
203
						<th><!-- Icon --></th>
204
						<th><!-- Rule type --></th>
205
						<th><?=gettext("Interface")?></th>
206
						<th><?=gettext("Protocol")?></th>
207
						<th><?=gettext("Source Address")?></th>
208
						<th><?=gettext("Source Ports")?></th>
209
						<th><?=gettext("Dest. Address")?></th>
210
						<th><?=gettext("Dest. Ports")?></th>
211
						<th><?=gettext("NAT IP")?></th>
212
						<th><?=gettext("NAT Ports")?></th>
213
						<th><?=gettext("Description")?></th>
214
						<th><?=gettext("Actions")?></th>
215
					</tr>
216
				</thead>
217
				<tbody class='user-entries'>
218
<?php
219

    
220
$nnats = $i = 0;
221
$separators = $config['nat']['separator'];
222

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

    
227
foreach ($a_nat as $natent):
228

    
229
	// Display separator(s) for section beginning at rule n
230
	if ($seprows[$nnats]) {
231
		display_separator($separators, $nnats, $columns_in_table);
232
	}
233

    
234
	$localport = $natent['local-port'];
235

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

    
238
	if ($dstendport) {
239
		$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
240
		$localport	 .= '-' . $localendport;
241
	}
242

    
243
	$alias = rule_columns_with_alias(
244
		$natent['source']['address'],
245
		pprint_port($natent['source']['port']),
246
		$natent['destination']['address'],
247
		pprint_port($natent['destination']['port']),
248
		$natent['target'],
249
		$localport
250
	);
251

    
252
	/* if user does not have access to edit an interface skip on to the next record */
253
	if (!have_natpfruleint_access($natent['interface'])) {
254
		continue;
255
	}
256

    
257
	if (isset($natent['disabled'])) {
258
		$iconfn = "pass_d";
259
		$trclass = 'class="disabled"';
260
	} else {
261
		$iconfn = "pass";
262
		$trclass = '';
263
	}
264
?>
265

    
266
					<tr id="fr<?=$nnats;?>" <?=$trclass?> onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_edit.php?id=<?=$i;?>';">
267
						<td >
268
							<input type="checkbox" id="frc<?=$nnats;?>" onClick="fr_toggle(<?=$nnats;?>)" name="rule[]" value="<?=$i;?>"/>
269
						</td>
270
						<td>
271
							<a href="?act=toggle&amp;id=<?=$i?>">
272
								<i class="fa <?= ($iconfn == "pass") ? "fa-check":"fa-times"?>" title="<?=gettext("click to toggle enabled/disabled status")?>"></i>
273
<?php 	if (isset($natent['nordr'])) { ?>
274
								&nbsp;<i class="fa fa-hand-stop-o text-danger" title="<?=gettext("Negated: This rule excludes NAT from a later rule")?>"></i>
275
<?php 	} ?>
276
							</a>
277
						</td>
278
						<td>
279
<?php
280
	if ($natent['associated-rule-id'] == "pass"):
281
?>
282
							<i class="fa fa-play" title="<?=gettext("All traffic matching this NAT entry is passed")?>"></i>
283
<?php
284
	elseif (!empty($natent['associated-rule-id'])):
285
?>
286
							<i class="fa fa-random" title="<?=sprintf(gettext("Firewall rule ID %s is managed by this rule"), htmlspecialchars($natent['associated-rule-id']))?>"></i>
287
<?php
288
	endif;
289
?>
290
						</td>
291
						<td>
292
							<?=$textss?>
293
<?php
294
	if (!$natent['interface']) {
295
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr("wan"));
296
	} else {
297
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr($natent['interface']));
298
	}
299
?>
300
							<?=$textse?>
301
						</td>
302

    
303
						<td>
304
							<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
305
						</td>
306

    
307
						<td>
308

    
309

    
310
<?php
311
	if (isset($alias['src'])):
312
?>
313
							<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">
314
<?php
315
	endif;
316
?>
317
							<?=str_replace('_', ' ', htmlspecialchars(pprint_address($natent['source'])))?>
318
<?php
319
	if (isset($alias['src'])):
320
?>
321
							</a>
322
<?php
323
	endif;
324
?>
325
						</td>
326
						<td>
327
<?php
328
	if (isset($alias['srcport'])):
329
?>
330
							<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">
331
<?php
332
	endif;
333
?>
334
							<?=str_replace('_', ' ', htmlspecialchars(pprint_port($natent['source']['port'])))?>
335
<?php
336
	if (isset($alias['srcport'])):
337
?>
338
							</a>
339
<?php
340
	endif;
341
?>
342
						</td>
343

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

    
387
							<?=str_replace('_', ' ', htmlspecialchars($natent['target']))?>
388
<?php
389
	if (isset($alias['target'])):
390
?>
391
							</a>
392
<?php
393
	endif;
394
?>
395
						</td>
396
						<td>
397
<?php
398
	if (isset($alias['targetport'])):
399
?>
400
							<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">
401
<?php
402
	endif;
403
?>
404
							<?=str_replace('_', ' ', htmlspecialchars(pprint_port($localport)))?>
405
<?php
406
	if (isset($alias['targetport'])):
407
?>
408
							</a>
409
<?php
410
	endif;
411
?>
412
						</td>
413

    
414
						<td>
415
							<?=htmlspecialchars($natent['descr'])?>
416
						</td>
417
						<td>
418
							<a class="fa fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
419
							<a class="fa fa-clone"	  title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
420
							<a class="fa fa-trash"	title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&amp;id=<?=$i?>"></a>
421
						</td>
422
					</tr>
423
<?php
424
	$i++;
425
	$nnats++;
426

    
427
endforeach;
428

    
429
// There can be separator(s) after the last rule listed.
430
if ($seprows[$nnats]) {
431
	display_separator($separators, $nnats, $columns_in_table);
432
}
433
?>
434
				</tbody>
435
			</table>
436
		</div>
437
	</div>
438

    
439
	<nav class="action-buttons">
440
		<a href="firewall_nat_edit.php?after=-1" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
441
			<i class="fa fa-level-up icon-embed-btn"></i>
442
			<?=gettext('Add')?>
443
		</a>
444
		<a href="firewall_nat_edit.php" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
445
			<i class="fa fa-level-down icon-embed-btn"></i>
446
			<?=gettext('Add')?>
447
		</a>
448
		<button name="del_x" type="submit" class="btn btn-danger btn-sm" title="<?=gettext('Delete selected rules')?>">
449
			<i class="fa fa-trash icon-embed-btn"></i>
450
			<?=gettext("Delete"); ?>
451
		</button>
452
		<button type="submit" id="order-store" name="order-store" class="btn btn-primary btn-sm" disabled title="<?=gettext('Save rule order')?>">
453
			<i class="fa fa-save icon-embed-btn"></i>
454
			<?=gettext("Save")?>
455
		</button>
456
		<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
457
			<i class="fa fa-plus icon-embed-btn"></i>
458
			<?=gettext("Separator")?>
459
		</button>
460
	</nav>
461
</form>
462

    
463
<script type="text/javascript">
464
//<![CDATA[
465
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
466
iface = "<?=strtolower($if)?>";
467
cncltxt = '<?=gettext("Cancel")?>';
468
svtxt = '<?=gettext("Save")?>';
469
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
470
configsection = "nat";
471
dirty = false;
472

    
473
events.push(function() {
474

    
475
	// Make rules sortable
476
	$('table tbody.user-entries').sortable({
477
		cursor: 'grabbing',
478
		update: function(event, ui) {
479
			$('#order-store').removeAttr('disabled');
480
			dirty = true;
481
			reindex_rules(ui.item.parent('tbody'));
482
			dirty = true;
483
		}
484
	});
485

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

    
490
		// Save the separator bar configuration
491
		save_separators();
492

    
493
		// Suppress the "Do you really want to leave the page" message
494
		saving = true;
495

    
496
	});
497

    
498
	// Globals
499
	saving = false;
500
	dirty = false;
501

    
502
	// provide a warning message if the user tries to change page before saving
503
	$(window).bind('beforeunload', function(){
504
		if (!saving && dirty) {
505
			return ("<?=gettext('One or more Port Forward rules have been moved but have not yet been saved')?>");
506
		} else {
507
			return undefined;
508
		}
509
	});
510
});
511
//]]>
512
</script>
513
<?php
514

    
515
if (count($a_nat) > 0) {
516
?>
517
<!-- Legend -->
518
<div>
519
	<dl class="dl-horizontal responsive">
520
		<dt><?=gettext('Legend')?></dt>					<dd></dd>
521
		<dt><i class="fa fa-play"></i></dt>			<dd><?=gettext('Pass')?></dd>
522
		<dt><i class="fa fa-random"></i></dt>		<dd><?=gettext('Linked rule')?></dd>
523
	</dl>
524
</div>
525

    
526
<?php
527
}
528

    
529
include("foot.inc");
(40-40/227)