Project

General

Profile

Download (8.77 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-2018 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
if (!is_array($config['staticroutes'])) {
39
	$config['staticroutes'] = array();
40
}
41

    
42
if (!is_array($config['staticroutes']['route'])) {
43
	$config['staticroutes']['route'] = array();
44
}
45

    
46
$a_routes = &$config['staticroutes']['route'];
47
$a_gateways = return_gateways_array(true, true, true);
48
$changedesc_prefix = gettext("Static Routes") . ": ";
49
unset($input_errors);
50

    
51
if ($_POST['apply']) {
52
	$pconfig = $_POST;
53
	$retval = 0;
54

    
55
	if (file_exists("{$g['tmp_path']}/.system_routes.apply")) {
56
		$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
57
		foreach ($toapplylist as $toapply) {
58
			mwexec("{$toapply}");
59
		}
60

    
61
		@unlink("{$g['tmp_path']}/.system_routes.apply");
62
	}
63

    
64
	$retval |= system_routing_configure();
65
	$retval |= filter_configure();
66
	/* reconfigure our gateway monitor */
67
	setup_gateways_monitor();
68

    
69
	if ($retval == 0) {
70
		clear_subsystem_dirty('staticroutes');
71
	}
72
}
73

    
74
function delete_static_route($id) {
75
	global $config, $a_routes, $changedesc_prefix;
76

    
77
	if (!isset($a_routes[$id])) {
78
		return;
79
	}
80

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

    
98
	foreach ($targets as $tgt) {
99
		$family = (is_subnetv6($tgt) ? "-inet6" : "-inet");
100
		mwexec("/sbin/route delete {$family} " . escapeshellarg($tgt));
101
	}
102

    
103
	unset($targets);
104
}
105

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

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

    
132
}
133

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

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

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

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

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

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

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

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

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

    
219
include("head.inc");
220

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

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

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

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

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

    
291
					</td>
292
				</tr>
293
<?php endforeach; ?>
294
			</table>
295
		</div>
296
	</div>
297
</div>
298

    
299
<nav class="action-buttons">
300
	<a href="system_routes_edit.php" role="button" class="btn btn-success btn-sm">
301
		<i class="fa fa-plus icon-embed-btn"></i>
302
		<?=gettext("Add")?>
303
	</a>
304
</nav>
305
<?php
306

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