Project

General

Profile

Download (14.6 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
		} else {
165
			$a_nat[$_GET['id']]['disabled'] = true;
166
		}
167
		if (write_config(gettext("Firewall: NAT: Port forward, enable/disable NAT rule"))) {
168
			mark_subsystem_dirty('natconf');
169
		}
170
		header("Location: firewall_nat.php");
171
		exit;
172
	}
173
}
174

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

    
178
if ($savemsg) {
179
	print_info_box($savemsg, 'success');
180
}
181

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

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

    
194
$columns_in_table = 13;
195
?>
196

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

    
222
$nnats = $i = 0;
223
$separators = $config['nat']['separator'];
224

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

    
229
foreach ($a_nat as $natent):
230

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

    
236
	$localport = $natent['local-port'];
237

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

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

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

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

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

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

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

    
309
						<td>
310

    
311

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

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

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

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

    
429
endforeach;
430

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

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

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

    
475
events.push(function() {
476

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

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

    
492
		// Save the separator bar configuration
493
		save_separators();
494

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

    
498
	});
499

    
500
	// Globals
501
	saving = false;
502
	dirty = false;
503

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

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

    
528
<?php
529
}
530

    
531
include("foot.inc");
(39-39/226)