Project

General

Profile

« Previous | Next » 

Revision 8fb4972c

Added by Steve Beaver over 4 years ago

Refactor 1 to 1 NAT for MVC

View differences:

src/usr/local/pfSense/include/www/firewall_nat_1to1.inc
1
<?php
2
/*
3
 * firewall_nat_1to1.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

  
22
// Functions to support firewall_nat_1to1.php and firewall_nat_1to1_edit.php
23

  
24
require_once("config.gui.inc");
25
require_once("interfaces.inc");
26
require_once("util.inc");
27
require_once("pfsense-utils.inc");
28
require_once("ipsec.inc");
29
require_once("filter.inc");
30

  
31
// Toggle enabled/disabled status of a 1 to 1 rule
32
function toggle1to1NATrule($post, $json = false) {
33
	global $config;
34

  
35
	init_config_arr(array('nat', 'onetoone'));
36
	$a_1to1 = &$config['nat']['onetoone'];
37

  
38
	if (isset($a_1to1[$_POST['id']]['disabled'])) {
39
		unset($a_1to1[$_POST['id']]['disabled']);
40
		$wc_msg = gettext('Firewall: NAT: 1:1 - enabled a NAT 1:1 rule.');
41
	} else {
42
		$a_1to1[$_POST['id']]['disabled'] = true;
43
		$wc_msg = gettext('Firewall: NAT: 1:1 - disabled a NAT 1:1 rule.');
44
	}
45

  
46
	if (write_config($wc_msg) && !$json) {
47
		mark_subsystem_dirty('natconf');
48
	}
49

  
50
	if (!$json) {
51
		header("Location: firewall_nat_1to1.php");
52
		exit;
53
	}
54
}
55

  
56
// Delete multiple 1 to 1 rules
57
function deleteMultiple1to1NATrules($post, $json = false) {
58
	global $config;
59

  
60
	init_config_arr(array('nat', 'onetoone'));
61
	$a_1to1 = &$config['nat']['onetoone'];
62

  
63
	foreach ($_POST['rule'] as $rulei) {
64
		unset($a_1to1[$rulei]);
65
	}
66

  
67
	if (write_config(gettext("Firewall: NAT: 1:1 - deleted selected NAT 1:1 mappings.")) && !$json) {
68
		mark_subsystem_dirty('natconf');
69
	}
70

  
71
	if (!$json) {
72
		header("Location: firewall_nat_1to1.php");
73
		exit;
74
	}
75
}
76

  
77
// Delete 1 to 1 rule
78
function delete1to1NATrule($post, $json = false) {
79
	global $config;
80

  
81
	init_config_arr(array('nat', 'onetoone'));
82
	$a_1to1 = &$config['nat']['onetoone'];
83

  
84
	unset($a_1to1[$post['id']]);
85
	if (write_config(gettext("Firewall: NAT: 1:1 - deleted NAT 1:1 mapping.")) && !$json) {
86
		mark_subsystem_dirty('natconf');
87
	}
88

  
89
	if(!$json) {
90
		header("Location: firewall_nat_1to1.php");
91
		exit;
92
	}
93
}
94

  
95
// Re-order the 1 to 1 NAT rules per the array of iindicies passed in $post
96
function reorder1to1NATrules($post, $json = false) {
97
	global $config;
98

  
99
	if (is_array($post['rule']) && !empty($post['rule'])) {
100
		init_config_arr(array('nat', 'onetoone'));
101
		$a_1to1 = &$config['nat']['onetoone'];
102
		$a_1to1_new = array();
103

  
104
		// if a rule is not in POST[rule], it has been deleted by the user
105
		foreach ($post['rule'] as $id) {
106
			$a_1to1_new[] = $a_1to1[$id];
107
		}
108

  
109
		$a_1to1 = $a_1to1_new;
110

  
111
		if (write_config(gettext("Firewall: NAT: 1:1 - reordered NAT 1:1 mappings.")) && !$json) {
112
			mark_subsystem_dirty('natconf');
113
		}
114

  
115
		if (!$json) {
116
			header("Location: firewall_nat_1to1.php");
117
			exit;
118
		}
119
	}
120
}
121

  
122
function apply1to1NATrules() {
123
	$retval = 0;
124
	$retval |= filter_configure();
125

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

  
131
	return $retval;
132
}
133
?>
src/usr/local/www/firewall_nat_1to1.php
36 36
require_once("functions.inc");
37 37
require_once("filter.inc");
38 38
require_once("shaper.inc");
39
require_once("firewall_nat_1to1.inc");
39 40

  
40 41
init_config_arr(array('nat', 'onetoone'));
41 42
$a_1to1 = &$config['nat']['onetoone'];
......
48 49
	$specialsrcdst[] = "{$kif}ip";
49 50
}
50 51

  
51
/* update rule order, POST[rule] is an array of ordered IDs */
52
if (array_key_exists('order-store', $_POST)) {
53
	if (is_array($_POST['rule']) && !empty($_POST['rule'])) {
54
		$a_1to1_new = array();
55

  
56
		// if a rule is not in POST[rule], it has been deleted by the user
57
		foreach ($_POST['rule'] as $id) {
58
			$a_1to1_new[] = $a_1to1[$id];
59
		}
60

  
61
		$a_1to1 = $a_1to1_new;
62

  
63
		if (write_config(gettext("Firewall: NAT: 1:1 - reordered NAT 1:1 mappings."))) {
64
			mark_subsystem_dirty('natconf');
65
		}
66

  
67
		header("Location: firewall_nat_1to1.php");
68
		exit;
69
	}
70
}
71

  
72

  
73
if ($_POST['apply']) {
74
	$retval = 0;
75
	$retval |= filter_configure();
76

  
77
	if ($retval == 0) {
78
		clear_subsystem_dirty('natconf');
79
		clear_subsystem_dirty('filter');
80
	}
52
// Process $_POST/$_REQUEST =======================================================================
53
if ($_REQUEST['savemsg']) {
54
	$savemsg = $_REQUEST['savemsg'];
81 55
}
82 56

  
83
if ($_POST['act'] == "del") {
57
if (array_key_exists('order-store', $_REQUEST)) {
58
	reorder1to1NATrules($_POST);
59
} else if ($_POST['apply']) {
60
	$retval = apply1to1NATrules();
61
} else if (($_POST['act'] == "del")) {
84 62
	if ($a_1to1[$_POST['id']]) {
85
		unset($a_1to1[$_POST['id']]);
86
		if (write_config(gettext("Firewall: NAT: 1:1 - deleted NAT 1:1 mapping."))) {
87
			mark_subsystem_dirty('natconf');
88
		}
89

  
90
		header("Location: firewall_nat_1to1.php");
91
		exit;
63
		delete1to1NATrule($_POST);
92 64
	}
93
}
94

  
95
if (isset($_POST['del_x'])) {
65
} else if (isset($_POST['del_x'])) {
96 66
	/* delete selected rules */
97 67
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
98
		foreach ($_POST['rule'] as $rulei) {
99
			unset($a_1to1[$rulei]);
100
		}
101

  
102
		if (write_config(gettext("Firewall: NAT: 1:1 - deleted selected NAT 1:1 mappings."))) {
103
			mark_subsystem_dirty('natconf');
104
		}
105

  
106
		header("Location: firewall_nat_1to1.php");
107
		exit;
68
		deleteMultiple1to1NATrules($_POST);
108 69
	}
109

  
110
} else if ($_POST['act'] == "toggle") {
70
} elseif (($_POST['act'] == "toggle")) {
111 71
	if ($a_1to1[$_POST['id']]) {
112
		if (isset($a_1to1[$_POST['id']]['disabled'])) {
113
			unset($a_1to1[$_POST['id']]['disabled']);
114
			$wc_msg = gettext('Firewall: NAT: 1:1 - enabled a NAT 1:1 rule.');
115
		} else {
116
			$a_1to1[$_POST['id']]['disabled'] = true;
117
			$wc_msg = gettext('Firewall: NAT: 1:1 - disabled a NAT 1:1 rule.');
118
		}
119
		if (write_config($wc_msg)) {
120
			mark_subsystem_dirty('natconf');
121
		}
122
		header("Location: firewall_nat_1to1.php");
123
		exit;
72
		toggle1to1NATrule($_POST);
124 73
	}
125 74
}
126 75

  
76
// Construct/display the form =====================================================================
127 77
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("1:1"));
128 78
$pglinks = array("", "firewall_nat.php", "@self");
129 79
include("head.inc");
......
134 84

  
135 85
if (is_subsystem_dirty('natconf')) {
136 86
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
137
					gettext('The changes must be applied for them to take effect.'));
87
	   gettext('The changes must be applied for them to take effect.'));
138 88
}
139 89

  
140 90
$tab_array = array();

Also available in: Unified diff