Project

General

Profile

Download (16.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	firewall_nat.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *	Some or all of this file is based on the m0n0wall project which is
9
 *	Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
10
 *
11
 *	Redistribution and use in source and binary forms, with or without modification,
12
 *	are permitted provided that the following conditions are met:
13
 *
14
 *	1. Redistributions of source code must retain the above copyright notice,
15
 *		this list of conditions and the following disclaimer.
16
 *
17
 *	2. Redistributions in binary form must reproduce the above copyright
18
 *		notice, this list of conditions and the following disclaimer in
19
 *		the documentation and/or other materials provided with the
20
 *		distribution.
21
 *
22
 *	3. All advertising materials mentioning features or use of this software
23
 *		must display the following acknowledgment:
24
 *		"This product includes software developed by the pfSense Project
25
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
26
 *
27
 *	4. The names "pfSense" and "pfSense Project" must not be used to
28
 *		 endorse or promote products derived from this software without
29
 *		 prior written permission. For written permission, please contact
30
 *		 coreteam@pfsense.org.
31
 *
32
 *	5. Products derived from this software may not be called "pfSense"
33
 *		nor may "pfSense" appear in their names without prior written
34
 *		permission of the Electric Sheep Fencing, LLC.
35
 *
36
 *	6. Redistributions of any form whatsoever must retain the following
37
 *		acknowledgment:
38
 *
39
 *	"This product includes software developed by the pfSense Project
40
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
41
 *
42
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
43
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
46
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
54
 *
55
 *	====================================================================
56
 *
57
 */
58

    
59
##|+PRIV
60
##|*IDENT=page-firewall-nat-portforward
61
##|*NAME=Firewall: NAT: Port Forward
62
##|*DESCR=Allow access to the 'Firewall: NAT: Port Forward' page.
63
##|*MATCH=firewall_nat.php*
64
##|-PRIV
65

    
66
require_once("guiconfig.inc");
67
require_once("functions.inc");
68
require_once("filter.inc");
69
require_once("shaper.inc");
70
require_once("itemid.inc");
71

    
72
if (!is_array($config['nat']['rule'])) {
73
	$config['nat']['rule'] = array();
74
}
75

    
76
$a_nat = &$config['nat']['rule'];
77

    
78
/* update rule order, POST[rule] is an array of ordered IDs */
79
if (array_key_exists('order-store', $_POST)) {
80
	if (is_array($_POST['rule']) && !empty($_POST['rule'])) {
81
		$a_nat_new = array();
82

    
83
		// if a rule is not in POST[rule], it has been deleted by the user
84
		foreach ($_POST['rule'] as $id) {
85
			$a_nat_new[] = $a_nat[$id];
86
		}
87

    
88
		$a_nat = $a_nat_new;
89

    
90

    
91
		$config['nat']['separator'] = "";
92

    
93
		if ($_POST['separator']) {
94
			$idx = 0;
95
			foreach ($_POST['separator'] as $separator) {
96
				$config['nat']['separator']['sep' . $idx++] = $separator;
97
			}
98
		}
99

    
100
		if (write_config()) {
101
			mark_subsystem_dirty('filter');
102
		}
103

    
104
		header("Location: firewall_nat.php");
105
		exit;
106
	}
107
}
108

    
109
/* if a custom message has been passed along, lets process it */
110
if ($_GET['savemsg']) {
111
	$savemsg = $_GET['savemsg'];
112
}
113

    
114
if ($_POST) {
115
	$pconfig = $_POST;
116

    
117
	if ($_POST['apply']) {
118

    
119
		$retval = 0;
120

    
121
		$retval |= filter_configure();
122
		$savemsg = get_std_save_message($retval);
123

    
124
		pfSense_handle_custom_code("/usr/local/pkg/firewall_nat/apply");
125

    
126
		if ($retval == 0) {
127
			clear_subsystem_dirty('natconf');
128
			clear_subsystem_dirty('filter');
129
		}
130

    
131
	}
132
}
133

    
134
if ($_GET['act'] == "del") {
135
	if ($a_nat[$_GET['id']]) {
136

    
137
		if (isset($a_nat[$_GET['id']]['associated-rule-id'])) {
138
			delete_id($a_nat[$_GET['id']]['associated-rule-id'], $config['filter']['rule']);
139
			$want_dirty_filter = true;
140
		}
141
		unset($a_nat[$_GET['id']]);
142

    
143
		// Update the separators
144
		$a_separators = &$config['nat']['separator'];
145
		$ridx = $_GET['id'];
146
		$mvnrows = -1;
147
		move_separators($a_separators, $ridx, $mvnrows);
148

    
149
		if (write_config()) {
150
			mark_subsystem_dirty('natconf');
151
			if ($want_dirty_filter) {
152
				mark_subsystem_dirty('filter');
153
			}
154
		}
155

    
156
		header("Location: firewall_nat.php");
157
		exit;
158
	}
159
}
160

    
161
if (isset($_POST['del_x'])) {
162
	/* delete selected rules */
163
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
164
		$a_separators = &$config['nat']['separator'];
165

    
166
		foreach ($_POST['rule'] as $rulei) {
167
			$target = $rule['target'];
168

    
169
			// Check for filter rule associations
170
			if (isset($a_nat[$rulei]['associated-rule-id'])) {
171
				delete_id($a_nat[$rulei]['associated-rule-id'], $config['filter']['rule']);
172

    
173
				mark_subsystem_dirty('filter');
174
			}
175

    
176
			unset($a_nat[$rulei]);
177

    
178
			// Update the separators
179
			$ridx = $rulei;
180
			$mvnrows = -1;
181
			move_separators($a_separators, $ridx, $mvnrows);
182
		}
183

    
184
		if (write_config()) {
185
			mark_subsystem_dirty('natconf');
186
		}
187

    
188
		header("Location: firewall_nat.php");
189
		exit;
190
	}
191
} else if ($_GET['act'] == "toggle") {
192
	if ($a_nat[$_GET['id']]) {
193
		if (isset($a_nat[$_GET['id']]['disabled'])) {
194
			unset($a_nat[$_GET['id']]['disabled']);
195
		} else {
196
			$a_nat[$_GET['id']]['disabled'] = true;
197
		}
198
		if (write_config(gettext("Firewall: NAT: Port forward, enable/disable NAT rule"))) {
199
			mark_subsystem_dirty('natconf');
200
		}
201
		header("Location: firewall_nat.php");
202
		exit;
203
	}
204
}
205

    
206
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
207
include("head.inc");
208

    
209
if ($savemsg) {
210
	print_info_box($savemsg, 'success');
211
}
212

    
213
if (is_subsystem_dirty('natconf')) {
214
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
215
					gettext('The changes must be applied for them to take effect.'));
216
}
217

    
218
$tab_array = array();
219
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
220
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
221
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
222
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
223
display_top_tabs($tab_array);
224

    
225
$columns_in_table = 13;
226
?>
227

    
228
<form action="firewall_nat.php" method="post" name="iform">
229
	<div class="panel panel-default">
230
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Rules')?></h2></div>
231
		<div class="panel-body table-responsive">
232
			<table id="ruletable" class="table table-striped table-hover table-condensed">
233
				<thead>
234
					<tr>
235
						<th><!-- Checkbox --></th>
236
						<th><!-- Icon --></th>
237
						<th><!-- Rule type --></th>
238
						<th><?=gettext("Interface")?></th>
239
						<th><?=gettext("Protocol")?></th>
240
						<th><?=gettext("Source Address")?></th>
241
						<th><?=gettext("Source Ports")?></th>
242
						<th><?=gettext("Dest. Address")?></th>
243
						<th><?=gettext("Dest. Ports")?></th>
244
						<th><?=gettext("NAT IP")?></th>
245
						<th><?=gettext("NAT Ports")?></th>
246
						<th><?=gettext("Description")?></th>
247
						<th><?=gettext("Actions")?></th>
248
					</tr>
249
				</thead>
250
				<tbody class='user-entries'>
251
<?php
252

    
253
$nnats = $i = 0;
254
$separators = $config['nat']['separator'];
255

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

    
260
foreach ($a_nat as $natent):
261

    
262
	// Display separator(s) for section beginning at rule n
263
	if ($seprows[$nnats]) {
264
		display_separator($separators, $nnats, $columns_in_table);
265
	}
266

    
267
	$localport = $natent['local-port'];
268

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

    
271
	if ($dstendport) {
272
		$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
273
		$localport	 .= '-' . $localendport;
274
	}
275

    
276
	$alias = rule_columns_with_alias(
277
		$natent['source']['address'],
278
		pprint_port($natent['source']['port']),
279
		$natent['destination']['address'],
280
		pprint_port($natent['destination']['port']),
281
		$natent['target'],
282
		$localport
283
	);
284

    
285
	/* if user does not have access to edit an interface skip on to the next record */
286
	if (!have_natpfruleint_access($natent['interface'])) {
287
		continue;
288
	}
289

    
290
	if (isset($natent['disabled'])) {
291
		$iconfn = "pass_d";
292
		$trclass = 'class="disabled"';
293
	} else {
294
		$iconfn = "pass";
295
		$trclass = '';
296
	}
297
?>
298

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

    
336
						<td>
337
							<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
338
						</td>
339

    
340
						<td>
341

    
342

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

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

    
420
							<?=str_replace('_', ' ', htmlspecialchars($natent['target']))?>
421
<?php
422
	if (isset($alias['target'])):
423
?>
424
							</a>
425
<?php
426
	endif;
427
?>
428
						</td>
429
						<td>
430
<?php
431
	if (isset($alias['targetport'])):
432
?>
433
							<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">
434
<?php
435
	endif;
436
?>
437
							<?=str_replace('_', ' ', htmlspecialchars(pprint_port($localport)))?>
438
<?php
439
	if (isset($alias['targetport'])):
440
?>
441
							</a>
442
<?php
443
	endif;
444
?>
445
						</td>
446

    
447
						<td>
448
							<?=htmlspecialchars($natent['descr'])?>
449
						</td>
450
						<td>
451
							<a class="fa fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
452
							<a class="fa fa-clone"	  title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
453
							<a class="fa fa-trash"	title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&amp;id=<?=$i?>"></a>
454
						</td>
455
					</tr>
456
<?php
457
	$i++;
458
	$nnats++;
459

    
460
endforeach;
461

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

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

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

    
506
events.push(function() {
507

    
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

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

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

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

    
529
	});
530

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

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

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

    
559
<?php
560
}
561

    
562
include("foot.inc");
(41-41/226)