Project

General

Profile

Download (8.64 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * system_routes.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2019 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-system-staticroutes
28
##|*NAME=System: Static Routes
29
##|*DESCR=Allow access to the 'System: Static Routes' page.
30
##|*MATCH=system_routes.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

    
38
init_config_arr(array('staticroutes', 'route'));
39
$a_routes = &$config['staticroutes']['route'];
40
$a_gateways = return_gateways_array(true, true, true);
41
$changedesc_prefix = gettext("Static Routes") . ": ";
42
unset($input_errors);
43

    
44
if ($_POST['apply']) {
45
	$pconfig = $_POST;
46
	$retval = 0;
47

    
48
	if (file_exists("{$g['tmp_path']}/.system_routes.apply")) {
49
		$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
50
		foreach ($toapplylist as $toapply) {
51
			mwexec("{$toapply}");
52
		}
53

    
54
		@unlink("{$g['tmp_path']}/.system_routes.apply");
55
	}
56

    
57
	$retval |= system_routing_configure();
58
	$retval |= filter_configure();
59
	/* reconfigure our gateway monitor */
60
	setup_gateways_monitor();
61

    
62
	if ($retval == 0) {
63
		clear_subsystem_dirty('staticroutes');
64
	}
65
}
66

    
67
function delete_static_route($id) {
68
	global $config, $a_routes, $changedesc_prefix;
69

    
70
	if (!isset($a_routes[$id])) {
71
		return;
72
	}
73

    
74
	$targets = array();
75
	if (is_alias($a_routes[$id]['network'])) {
76
		foreach (filter_expand_alias_array($a_routes[$id]['network']) as $tgt) {
77
			if (is_ipaddrv4($tgt)) {
78
				$tgt .= "/32";
79
			} else if (is_ipaddrv6($tgt)) {
80
				$tgt .= "/128";
81
			}
82
			if (!is_subnet($tgt)) {
83
				continue;
84
			}
85
			$targets[] = $tgt;
86
		}
87
	} else {
88
		$targets[] = $a_routes[$id]['network'];
89
	}
90

    
91
	foreach ($targets as $tgt) {
92
		$family = (is_subnetv6($tgt) ? "-inet6" : "-inet");
93
		mwexec("/sbin/route delete {$family} " . escapeshellarg($tgt));
94
	}
95

    
96
	unset($targets);
97
}
98

    
99
if ($_POST['act'] == "del") {
100
	if ($a_routes[$_POST['id']]) {
101
		$changedesc = $changedesc_prefix . sprintf(gettext("removed route to %s"), $a_routes[$_POST['id']]['network']);
102
		delete_static_route($_POST['id']);
103
		unset($a_routes[$_POST['id']]);
104
		write_config($changedesc);
105
		header("Location: system_routes.php");
106
		exit;
107
	}
108
}
109

    
110
if (isset($_POST['del_x'])) {
111
	/* delete selected routes */
112
	if (is_array($_POST['route']) && count($_POST['route'])) {
113
		$deleted_routes = "";
114
		foreach ($_POST['route'] as $routei) {
115
			$deleted_routes .= " " . $a_routes[$routei]['network'];
116
			delete_static_route($routei);
117
			unset($a_routes[$routei]);
118
		}
119
		$changedesc = $changedesc_prefix . sprintf(gettext("removed route to%s"), $deleted_routes);
120
		write_config($changedesc);
121
		header("Location: system_routes.php");
122
		exit;
123
	}
124

    
125
}
126

    
127
if ($_POST['act'] == "toggle") {
128
	if ($a_routes[$_POST['id']]) {
129
		$do_update_config = true;
130
		if (isset($a_routes[$_POST['id']]['disabled'])) {
131
			// Do not enable a route whose gateway is disabled
132
			if (isset($a_gateways[$a_routes[$_POST['id']]['gateway']]['disabled'])) {
133
				$do_update_config = false;
134
				$input_errors[] = $changedesc_prefix . sprintf(gettext("gateway is disabled, cannot enable route to %s"), $a_routes[$_POST['id']]['network']);
135
			} else {
136
				unset($a_routes[$_POST['id']]['disabled']);
137
				$changedesc = $changedesc_prefix . sprintf(gettext("enabled route to %s"), $a_routes[$_POST['id']]['network']);
138
			}
139
		} else {
140
			delete_static_route($_POST['id']);
141
			$a_routes[$_POST['id']]['disabled'] = true;
142
			$changedesc = $changedesc_prefix . sprintf(gettext("disabled route to %s"), $a_routes[$_POST['id']]['network']);
143
		}
144

    
145
		if ($do_update_config) {
146
			if (write_config($changedesc)) {
147
				mark_subsystem_dirty('staticroutes');
148
			}
149
			header("Location: system_routes.php");
150
			exit;
151
		}
152
	}
153
}
154

    
155
if($_POST['save']) {
156
	/* yuck - IE won't send value attributes for image buttons, while Mozilla does - so we use .x/.y to find move button clicks instead... */
157
	unset($movebtn);
158
	foreach ($_POST as $pn => $pd) {
159
		if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
160
			$movebtn = $matches[1];
161
			break;
162
		}
163
	}
164
	/* move selected routes before this route */
165
	if (isset($movebtn) && is_array($_POST['route']) && count($_POST['route'])) {
166
		$a_routes_new = array();
167

    
168
		/* copy all routes < $movebtn and not selected */
169
		for ($i = 0; $i < $movebtn; $i++) {
170
			if (!in_array($i, $_POST['route'])) {
171
				$a_routes_new[] = $a_routes[$i];
172
			}
173
		}
174

    
175
		/* copy all selected routes */
176
		for ($i = 0; $i < count($a_routes); $i++) {
177
			if ($i == $movebtn) {
178
				continue;
179
			}
180
			if (in_array($i, $_POST['route'])) {
181
				$a_routes_new[] = $a_routes[$i];
182
			}
183
		}
184

    
185
		/* copy $movebtn route */
186
		if ($movebtn < count($a_routes)) {
187
			$a_routes_new[] = $a_routes[$movebtn];
188
		}
189

    
190
		/* copy all routes > $movebtn and not selected */
191
		for ($i = $movebtn+1; $i < count($a_routes); $i++) {
192
			if (!in_array($i, $_POST['route'])) {
193
				$a_routes_new[] = $a_routes[$i];
194
			}
195
		}
196
		if (count($a_routes_new) > 0) {
197
			$a_routes = $a_routes_new;
198
		}
199

    
200
		if (write_config(gettext("Saved static routes configuration."))) {
201
			mark_subsystem_dirty('staticroutes');
202
		}
203
		header("Location: system_routes.php");
204
		exit;
205
	}
206
}
207

    
208
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Static Routes"));
209
$pglinks = array("", "system_gateways.php", "@self");
210
$shortcut_section = "routing";
211

    
212
include("head.inc");
213

    
214
if ($input_errors) {
215
	print_input_errors($input_errors);
216
}
217
if ($_POST['apply']) {
218
	print_apply_result_box($retval);
219
}
220
if (is_subsystem_dirty('staticroutes')) {
221
	print_apply_box(gettext("The static route configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
222
}
223

    
224
$tab_array = array();
225
$tab_array[0] = array(gettext("Gateways"), false, "system_gateways.php");
226
$tab_array[1] = array(gettext("Static Routes"), true, "system_routes.php");
227
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
228
display_top_tabs($tab_array);
229

    
230
?>
231
<div class="panel panel-default">
232
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Static Routes')?></h2></div>
233
	<div class="panel-body">
234
		<div class="table-responsive">
235
			<table class="table table-striped table-hover table-condensed table-rowdblclickedit">
236
				<thead>
237
					<tr>
238
						<th></th>
239
						<th><?=gettext("Network")?></th>
240
						<th><?=gettext("Gateway")?></th>
241
						<th><?=gettext("Interface")?></th>
242
						<th><?=gettext("Description")?></th>
243
						<th><?=gettext("Actions")?></th>
244
					</tr>
245
				</thead>
246
				<tbody>
247
<?php
248
foreach ($a_routes as $i => $route):
249
	if (isset($route['disabled'])) {
250
		$icon = 'fa-ban';
251
	} else {
252
		$icon = 'fa-check-circle-o';
253
	}
254
?>
255
				<tr<?=($icon != 'fa-check-circle-o')? ' class="disabled"' : ''?>>
256
					<td><i class="fa <?=$icon?>"></i></td>
257
					<td>
258
						<?=strtolower($route['network'])?>
259
					</td>
260
					<td>
261
						<?=htmlentities($a_gateways[$route['gateway']]['name']) . " - " . htmlentities($a_gateways[$route['gateway']]['gateway'])?>
262
					</td>
263
					<td>
264
						<?=convert_friendly_interface_to_friendly_descr($a_gateways[$route['gateway']]['friendlyiface'])?>
265
					</td>
266
					<td>
267
						<?=htmlspecialchars($route['descr'])?>
268
					</td>
269
					<td>
270
						<a href="system_routes_edit.php?id=<?=$i?>" class="fa fa-pencil" title="<?=gettext('Edit route')?>"></a>
271

    
272
						<a href="system_routes_edit.php?dup=<?=$i?>" class="fa fa-clone" title="<?=gettext('Copy route')?>"></a>
273

    
274
				<?php if (isset($route['disabled'])) {
275
				?>
276
						<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable route')?>" usepost></a>
277
				<?php } else {
278
				?>
279
						<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable route')?>" usepost></a>
280
				<?php }
281
				?>
282
						<a href="system_routes.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete route')?>" usepost></a>
283

    
284
					</td>
285
				</tr>
286
<?php endforeach; ?>
287
			</table>
288
		</div>
289
	</div>
290
</div>
291

    
292
<nav class="action-buttons">
293
	<a href="system_routes_edit.php" role="button" class="btn btn-success btn-sm">
294
		<i class="fa fa-plus icon-embed-btn"></i>
295
		<?=gettext("Add")?>
296
	</a>
297
</nav>
298
<?php
299

    
300
include("foot.inc");
(210-210/234)