Project

General

Profile

Download (8.62 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) {
48

    
49
	$pconfig = $_POST;
50

    
51
	if ($_POST['apply']) {
52

    
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
		$savemsg = get_std_save_message($retval);
70
		if ($retval == 0) {
71
			clear_subsystem_dirty('staticroutes');
72
		}
73
	}
74
}
75

    
76
function delete_static_route($id) {
77
	global $config, $a_routes, $changedesc_prefix;
78

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

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

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

    
105
	unset($targets);
106
}
107

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

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

    
134
} else if ($_GET['act'] == "toggle") {
135
	if ($a_routes[$_GET['id']]) {
136
		$do_update_config = true;
137
		if (isset($a_routes[$_GET['id']]['disabled'])) {
138
			// Do not enable a route whose gateway is disabled
139
			if (isset($a_gateways[$a_routes[$_GET['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[$_GET['id']]['network']);
142
			} else {
143
				unset($a_routes[$_GET['id']]['disabled']);
144
				$changedesc = $changedesc_prefix . sprintf(gettext("enabled route to %s"), $a_routes[$_GET['id']]['network']);
145
			}
146
		} else {
147
			delete_static_route($_GET['id']);
148
			$a_routes[$_GET['id']]['disabled'] = true;
149
			$changedesc = $changedesc_prefix . sprintf(gettext("disabled route to %s"), $a_routes[$_GET['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
} else {
161
	/* yuck - IE won't send value attributes for image buttons, while Mozilla does - so we use .x/.y to find move button clicks instead... */
162
	unset($movebtn);
163
	foreach ($_POST as $pn => $pd) {
164
		if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
165
			$movebtn = $matches[1];
166
			break;
167
		}
168
	}
169
	/* move selected routes before this route */
170
	if (isset($movebtn) && is_array($_POST['route']) && count($_POST['route'])) {
171
		$a_routes_new = array();
172

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

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

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

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

    
205
		if (write_config()) {
206
			mark_subsystem_dirty('staticroutes');
207
		}
208
		header("Location: system_routes.php");
209
		exit;
210
	}
211
}
212

    
213
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Static Routes"));
214
$shortcut_section = "routing";
215

    
216
include("head.inc");
217

    
218
if ($input_errors) {
219
	print_input_errors($input_errors);
220
}
221
if ($savemsg) {
222
	print_info_box($savemsg, 'success');
223
}
224
if (is_subsystem_dirty('staticroutes')) {
225
	print_apply_box(gettext("The static route configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
226
}
227

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

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

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

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

    
288
					</td>
289
				</tr>
290
<?php endforeach; ?>
291
			</table>
292
		</div>
293
	</div>
294
</div>
295

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

    
304
include("foot.inc");
(202-202/225)