Project

General

Profile

Download (8.66 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-2016 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']['route'])) {
39
	$config['staticroutes']['route'] = array();
40
}
41

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

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

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

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

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

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

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

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

    
77
	$targets = array();
78
	if (is_alias($a_routes[$id]['network'])) {
79
		foreach (filter_expand_alias_array($a_routes[$id]['network']) as $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
		$family = (is_subnetv6($tgt) ? "-inet6" : "-inet");
96
		mwexec("/sbin/route delete {$family} " . escapeshellarg($tgt));
97
	}
98

    
99
	unset($targets);
100
}
101

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

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

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

    
146
		if ($do_update_config) {
147
			if (write_config($changedesc)) {
148
				mark_subsystem_dirty('staticroutes');
149
			}
150
			header("Location: system_routes.php");
151
			exit;
152
		}
153
	}
154
} else {
155
	/* yuck - IE won't send value attributes for image buttons, while Mozilla does - so we use .x/.y to find move button clicks instead... */
156
	unset($movebtn);
157
	foreach ($_POST as $pn => $pd) {
158
		if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
159
			$movebtn = $matches[1];
160
			break;
161
		}
162
	}
163
	/* move selected routes before this route */
164
	if (isset($movebtn) && is_array($_POST['route']) && count($_POST['route'])) {
165
		$a_routes_new = array();
166

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

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

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

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

    
199
		if (write_config()) {
200
			mark_subsystem_dirty('staticroutes');
201
		}
202
		header("Location: system_routes.php");
203
		exit;
204
	}
205
}
206

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

    
211
include("head.inc");
212

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

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

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

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

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

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

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

    
299
include("foot.inc");
(200-200/223)