Project

General

Profile

Download (9.36 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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
##|+PRIV
29
##|*IDENT=page-system-staticroutes
30
##|*NAME=System: Static Routes
31
##|*DESCR=Allow access to the 'System: Static Routes' page.
32
##|*MATCH=system_routes.php*
33
##|-PRIV
34

    
35
require_once("guiconfig.inc");
36
require_once("functions.inc");
37
require_once("filter.inc");
38
require_once("shaper.inc");
39

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

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

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

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

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

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

    
69
function delete_static_route($id) {
70
	global $config, $a_routes, $changedesc_prefix, $a_gateways;
71

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

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

    
94
	foreach ($targets as $tgt) {
95
		route_del($tgt);
96
	}
97

    
98
	unset($targets);
99
}
100

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

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

    
127
}
128

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

    
149
		if ($do_update_config) {
150
			if (write_config($changedesc)) {
151
				if (!$route_del) {
152
					mark_subsystem_dirty('staticroutes');
153
				}
154
			}
155
			header("Location: system_routes.php");
156
			exit;
157
		}
158
	}
159
}
160

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

    
174
		/* copy all routes < $movebtn and not selected */
175
		for ($i = 0; $i < $movebtn; $i++) {
176
			if (!in_array($i, $_POST['route'])) {
177
				$a_routes_new[] = $a_routes[$i];
178
			}
179
		}
180

    
181
		/* copy all selected routes */
182
		for ($i = 0; $i < count($a_routes); $i++) {
183
			if ($i == $movebtn) {
184
				continue;
185
			}
186
			if (in_array($i, $_POST['route'])) {
187
				$a_routes_new[] = $a_routes[$i];
188
			}
189
		}
190

    
191
		/* copy $movebtn route */
192
		if ($movebtn < count($a_routes)) {
193
			$a_routes_new[] = $a_routes[$movebtn];
194
		}
195

    
196
		/* copy all routes > $movebtn and not selected */
197
		for ($i = $movebtn+1; $i < count($a_routes); $i++) {
198
			if (!in_array($i, $_POST['route'])) {
199
				$a_routes_new[] = $a_routes[$i];
200
			}
201
		}
202
		if (count($a_routes_new) > 0) {
203
			$a_routes = $a_routes_new;
204
		}
205

    
206
		if (write_config(gettext("Saved static routes configuration."))) {
207
			mark_subsystem_dirty('staticroutes');
208
		}
209
		header("Location: system_routes.php");
210
		exit;
211
	}
212
}
213

    
214
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Static Routes"));
215
$pglinks = array("", "system_gateways.php", "@self");
216
$shortcut_section = "routing";
217

    
218
include("head.inc");
219

    
220
if ($input_errors) {
221
	print_input_errors($input_errors);
222
}
223
if ($_POST['apply']) {
224
	print_apply_result_box($retval);
225
}
226
if (is_subsystem_dirty('staticroutes')) {
227
	print_apply_box(gettext("The static route configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
228
}
229

    
230
$tab_array = array();
231
$tab_array[0] = array(gettext("Gateways"), false, "system_gateways.php");
232
$tab_array[1] = array(gettext("Static Routes"), true, "system_routes.php");
233
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
234
display_top_tabs($tab_array);
235

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

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

    
285
				<?php if (isset($route['disabled'])) {
286
				?>
287
						<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable route')?>" usepost></a>
288
				<?php } else {
289
				?>
290
						<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable route')?>" usepost></a>
291
				<?php }
292
				?>
293
						<a href="system_routes.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete route')?>" usepost></a>
294

    
295
					</td>
296
				</tr>
297
<?php endforeach; ?>
298
			</table>
299
		</div>
300
	</div>
301
</div>
302

    
303
<nav class="action-buttons">
304
	<a href="system_routes_edit.php" role="button" class="btn btn-success btn-sm">
305
		<i class="fa fa-plus icon-embed-btn"></i>
306
		<?=gettext("Add")?>
307
	</a>
308
</nav>
309
<div class="infoblock">
310
<?php
311
print_info_box(
312
	sprintf(gettext('%1$s Route is inactive, gateway interface is missing'), '<br /><strong><i class="fa fa-times-circle-o"></i></strong>') .
313
	sprintf(gettext('%1$s Route disabled'), '<br /><strong><i class="fa fa-ban"></i></strong>') .
314
	sprintf(gettext('%1$s Route enabled'), '<br /><strong><i class="fa fa-check-circle-o"></i></strong>')
315
	);
316
?>
317
</div>
318
<?php
319

    
320
include("foot.inc");
(203-203/227)