Project

General

Profile

Download (10.6 KB) Statistics
| Branch: | Tag: | Revision:
1 d173230c Seth Mos
<?php
2
/*
3 c5d81585 Renato Botelho
 * system_gateways.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * Copyright (c) 2010 Seth Mos <seth.mos@dds.nl>
8
 * All rights reserved.
9
 *
10 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13 c5d81585 Renato Botelho
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 c5d81585 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21 6ff05704 Stephen Beaver
 */
22 d173230c Seth Mos
23 6b07c15a Matthew Grooms
##|+PRIV
24
##|*IDENT=page-system-gateways
25 5230f468 jim-p
##|*NAME=System: Gateways
26 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'System: Gateways' page.
27
##|*MATCH=system_gateways.php*
28
##|-PRIV
29
30 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
31 7a927e67 Scott Ullrich
require_once("functions.inc");
32
require_once("filter.inc");
33
require_once("shaper.inc");
34 d173230c Seth Mos
35 e97df865 Renato Botelho
$a_gateways = return_gateways_array(true, false, true);
36 b92305a6 --global
$a_gateways_arr = array();
37 e0c7b2fe Phil Davis
foreach ($a_gateways as $gw) {
38 3df6d458 Seth Mos
	$a_gateways_arr[] = $gw;
39 e0c7b2fe Phil Davis
}
40 b92305a6 --global
$a_gateways = $a_gateways_arr;
41 616e1956 Seth Mos
42 e0c7b2fe Phil Davis
if (!is_array($config['gateways']['gateway_item'])) {
43 d251a8d4 Renato Botelho
	$config['gateways']['gateway_item'] = array();
44 e0c7b2fe Phil Davis
}
45 616e1956 Seth Mos
46
$a_gateway_item = &$config['gateways']['gateway_item'];
47
48 d173230c Seth Mos
if ($_POST) {
49
50
	$pconfig = $_POST;
51
52
	if ($_POST['apply']) {
53
54
		$retval = 0;
55
56 44c42356 Phil Davis
		$retval |= system_routing_configure();
57 138e4140 Renato Botelho
		$retval |= system_resolvconf_generate();
58 d173230c Seth Mos
		$retval |= filter_configure();
59 13bbe450 Seth Mos
		/* reconfigure our gateway monitor */
60
		setup_gateways_monitor();
61 acda1403 Chris Buechler
		/* Dynamic DNS on gw groups may have changed */
62
		send_event("service reload dyndnsall");
63 d173230c Seth Mos
64 e0c7b2fe Phil Davis
		if ($retval == 0) {
65 a368a026 Ermal Lu?i
			clear_subsystem_dirty('staticroutes');
66 e0c7b2fe Phil Davis
		}
67 d173230c Seth Mos
	}
68
}
69
70 028ff8f8 Phil Davis
function can_delete_disable_gateway_item($id, $disable = false) {
71 e97df865 Renato Botelho
	global $config, $input_errors, $a_gateways;
72 d251a8d4 Renato Botelho
73 e0c7b2fe Phil Davis
	if (!isset($a_gateways[$id])) {
74 e97df865 Renato Botelho
		return false;
75 e0c7b2fe Phil Davis
	}
76 e97df865 Renato Botelho
77
	if (is_array($config['gateways']['gateway_group'])) {
78
		foreach ($config['gateways']['gateway_group'] as $group) {
79
			foreach ($group['item'] as $item) {
80
				$items = explode("|", $item);
81
				if ($items[0] == $a_gateways[$id]['name']) {
82 205178aa Phil Davis
					if (!$disable) {
83 762faef5 Phil Davis
						$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be deleted because it is in use on Gateway Group "%2$s"'), $a_gateways[$id]['name'], $group['name']);
84 205178aa Phil Davis
					} else {
85 762faef5 Phil Davis
						$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be disabled because it is in use on Gateway Group "%2$s"'), $a_gateways[$id]['name'], $group['name']);
86 028ff8f8 Phil Davis
					}
87 f78302e8 Ermal
				}
88
			}
89
		}
90 e97df865 Renato Botelho
	}
91
92
	if (is_array($config['staticroutes']['route'])) {
93
		foreach ($config['staticroutes']['route'] as $route) {
94
			if ($route['gateway'] == $a_gateways[$id]['name']) {
95 205178aa Phil Davis
				if (!$disable) {
96 028ff8f8 Phil Davis
					// The user wants to delete this gateway, but there is a static route (enabled or disabled) that refers to the gateway.
97 762faef5 Phil Davis
					$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be deleted because it is in use on Static Route "%2$s"'), $a_gateways[$id]['name'], $route['network']);
98 205178aa Phil Davis
				} else if (!isset($route['disabled'])) {
99
					// The user wants to disable this gateway.
100
					// But there is a static route that uses this gateway and is enabled (not disabled).
101 762faef5 Phil Davis
					$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be disabled because it is in use on Static Route "%2$s"'), $a_gateways[$id]['name'], $route['network']);
102 028ff8f8 Phil Davis
				}
103 f78302e8 Ermal
			}
104
		}
105 e97df865 Renato Botelho
	}
106
107 e0c7b2fe Phil Davis
	if (isset($input_errors)) {
108 e97df865 Renato Botelho
		return false;
109 e0c7b2fe Phil Davis
	}
110 e97df865 Renato Botelho
111
	return true;
112
}
113
114
function delete_gateway_item($id) {
115 dde20226 Renato Botelho
	global $config, $a_gateways;
116
117 e0c7b2fe Phil Davis
	if (!isset($a_gateways[$id])) {
118 e97df865 Renato Botelho
		return;
119 e0c7b2fe Phil Davis
	}
120 32a9eb18 Ermal
121 1be1b87b jim-p
	/* If the removed gateway was the default route, remove the default route */
122
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
123
	    !isset($a_gateways[$id]['disabled']) &&
124
	    isset($a_gateways[$id]['defaultgw'])) {
125
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? '-inet6' : '-inet');
126
		mwexec("/sbin/route delete {$inet} default");
127
	}
128
129 e75f0e7d PiBa-NL
	/* NOTE: Cleanup static routes for the interface route if any */
130 d61309a0 Phil Davis
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
131
	    $gateway['gateway'] != $a_gateways[$id]['gateway'] &&
132
	    isset($a_gateways[$id]["nonlocalgateway"])) {
133 e75f0e7d PiBa-NL
		$realif = get_real_interface($a_gateways[$id]['interface']);
134
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? "-inet6" : "-inet");
135
		$cmd = "/sbin/route delete $inet " . escapeshellarg($a_gateways[$id]['gateway']) . " -iface " . escapeshellarg($realif);
136
		mwexec($cmd);
137
	}
138 e97df865 Renato Botelho
	/* NOTE: Cleanup static routes for the monitor ip if any */
139
	if (!empty($a_gateways[$id]['monitor']) &&
140 d61309a0 Phil Davis
	    $a_gateways[$id]['monitor'] != "dynamic" &&
141
	    is_ipaddr($a_gateways[$id]['monitor']) &&
142
	    $a_gateways[$id]['gateway'] != $a_gateways[$id]['monitor']) {
143 e0c7b2fe Phil Davis
		if (is_ipaddrv4($a_gateways[$id]['monitor'])) {
144 e97df865 Renato Botelho
			mwexec("/sbin/route delete " . escapeshellarg($a_gateways[$id]['monitor']));
145 e0c7b2fe Phil Davis
		} else {
146 e97df865 Renato Botelho
			mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateways[$id]['monitor']));
147 e0c7b2fe Phil Davis
		}
148 e97df865 Renato Botelho
	}
149
150 e0c7b2fe Phil Davis
	if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
151 e97df865 Renato Botelho
		unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
152 e0c7b2fe Phil Davis
	}
153 e97df865 Renato Botelho
	unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
154
}
155
156
unset($input_errors);
157
if ($_GET['act'] == "del") {
158 028ff8f8 Phil Davis
	if (can_delete_disable_gateway_item($_GET['id'])) {
159 e97df865 Renato Botelho
		$realid = $a_gateways[$_GET['id']]['attribute'];
160
		delete_gateway_item($_GET['id']);
161
		write_config("Gateways: removed gateway {$realid}");
162
		mark_subsystem_dirty('staticroutes');
163
		header("Location: system_gateways.php");
164
		exit;
165
	}
166
}
167
168
if (isset($_POST['del_x'])) {
169
	/* delete selected items */
170
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
171 e0c7b2fe Phil Davis
		foreach ($_POST['rule'] as $rulei) {
172 028ff8f8 Phil Davis
			if (!can_delete_disable_gateway_item($rulei)) {
173 e97df865 Renato Botelho
				break;
174 e0c7b2fe Phil Davis
			}
175
		}
176 e97df865 Renato Botelho
177
		if (!isset($input_errors)) {
178
			$items_deleted = "";
179
			foreach ($_POST['rule'] as $rulei) {
180
				delete_gateway_item($rulei);
181
				$items_deleted .= "{$rulei} ";
182
			}
183
			if (!empty($items_deleted)) {
184 762faef5 Phil Davis
				write_config(sprintf(gettext("Gateways: removed gateways %s", $items_deleted)));
185 e97df865 Renato Botelho
				mark_subsystem_dirty('staticroutes');
186
			}
187 f78302e8 Ermal
			header("Location: system_gateways.php");
188
			exit;
189
		}
190 d173230c Seth Mos
	}
191
192 e97df865 Renato Botelho
} else if ($_GET['act'] == "toggle" && $a_gateways[$_GET['id']]) {
193
	$realid = $a_gateways[$_GET['id']]['attribute'];
194 028ff8f8 Phil Davis
	$disable_gw = !isset($a_gateway_item[$realid]['disabled']);
195
	if ($disable_gw) {
196
		// The user wants to disable the gateway, so check if that is OK.
197
		$ok_to_toggle = can_delete_disable_gateway_item($_GET['id'], $disable_gw);
198 e0c7b2fe Phil Davis
	} else {
199 028ff8f8 Phil Davis
		// The user wants to enable the gateway. That is always OK.
200
		$ok_to_toggle = true;
201 e0c7b2fe Phil Davis
	}
202 028ff8f8 Phil Davis
	if ($ok_to_toggle) {
203
		if ($disable_gw) {
204
			$a_gateway_item[$realid]['disabled'] = true;
205 1be1b87b jim-p
			/* If the disabled gateway was the default route, remove the default route */
206
			if (!empty($a_gateway_item[$realid]) && is_ipaddr($a_gateway_item[$realid]['gateway']) &&
207
			    isset($a_gateway_item[$realid]['defaultgw'])) {
208
				$inet = (!is_ipaddrv4($a_gateway_item[$realid]['gateway']) ? '-inet6' : '-inet');
209
				mwexec("/sbin/route delete {$inet} default");
210
			}
211 028ff8f8 Phil Davis
		} else {
212
			unset($a_gateway_item[$realid]['disabled']);
213
		}
214 e97df865 Renato Botelho
215 028ff8f8 Phil Davis
		if (write_config("Gateways: enable/disable")) {
216
			mark_subsystem_dirty('staticroutes');
217
		}
218 e97df865 Renato Botelho
219 028ff8f8 Phil Davis
		header("Location: system_gateways.php");
220
		exit;
221
	}
222 e97df865 Renato Botelho
}
223 124aee67 Chris Buechler
224 d036bc07 Stephen Beaver
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Gateways"));
225 edcd7535 Phil Davis
$pglinks = array("", "@self", "@self");
226 b32dd0a6 jim-p
$shortcut_section = "gateways";
227 02ca24c9 jim-p
228 d173230c Seth Mos
include("head.inc");
229
230 d61309a0 Phil Davis
if ($input_errors) {
231 c3c692a9 Sjon Hortensius
	print_input_errors($input_errors);
232 d61309a0 Phil Davis
}
233 44c42356 Phil Davis
234
if ($_POST['apply']) {
235
	print_apply_result_box($retval);
236 d61309a0 Phil Davis
}
237 f74457df Stephen Beaver
238 d61309a0 Phil Davis
if (is_subsystem_dirty('staticroutes')) {
239 7fdca5ff NOYB
	print_apply_box(gettext("The gateway configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
240 d61309a0 Phil Davis
}
241 c3c692a9 Sjon Hortensius
242
$tab_array = array();
243
$tab_array[0] = array(gettext("Gateways"), true, "system_gateways.php");
244 80b4d0c5 heper
$tab_array[1] = array(gettext("Static Routes"), false, "system_routes.php");
245
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
246 c3c692a9 Sjon Hortensius
display_top_tabs($tab_array);
247 d173230c Seth Mos
248 d251a8d4 Renato Botelho
?>
249 060ed238 Stephen Beaver
<div class="panel panel-default">
250
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Gateways')?></h2></div>
251
	<div class="panel-body">
252
		<div class="table-responsive">
253 47180823 Colin Fleming
			<table class="table table-striped table-hover table-condensed table-rowdblclickedit">
254 060ed238 Stephen Beaver
				<thead>
255
					<tr>
256
						<th></th>
257
						<th><?=gettext("Name")?></th>
258
						<th><?=gettext("Interface")?></th>
259
						<th><?=gettext("Gateway")?></th>
260
						<th><?=gettext("Monitor IP")?></th>
261
						<th><?=gettext("Description")?></th>
262
						<th><?=gettext("Actions")?></th>
263
					</tr>
264
				</thead>
265
				<tbody>
266 e97df865 Renato Botelho
<?php
267 c3c692a9 Sjon Hortensius
foreach ($a_gateways as $i => $gateway):
268 d61309a0 Phil Davis
	if (isset($gateway['inactive'])) {
269 1b7379f9 Jared Dillard
		$icon = 'fa-times-circle-o';
270 d61309a0 Phil Davis
	} elseif (isset($gateway['disabled'])) {
271 1b7379f9 Jared Dillard
		$icon = 'fa-ban';
272 d61309a0 Phil Davis
	} else {
273 1b7379f9 Jared Dillard
		$icon = 'fa-check-circle-o';
274 d61309a0 Phil Davis
	}
275 c3c692a9 Sjon Hortensius
276 d61309a0 Phil Davis
	if (isset($gateway['inactive'])) {
277 c3c692a9 Sjon Hortensius
		$title = gettext("This gateway is inactive because interface is missing");
278 d61309a0 Phil Davis
	} else {
279 c3c692a9 Sjon Hortensius
		$title = '';
280 d61309a0 Phil Davis
	}
281 e97df865 Renato Botelho
?>
282 060ed238 Stephen Beaver
				<tr<?=($icon != 'fa-check-circle-o')? ' class="disabled"' : ''?>>
283
					<td title="<?=$title?>"><i class="fa <?=$icon?>"></i></td>
284
					<td>
285
						<?=htmlspecialchars($gateway['name'])?>
286 e97df865 Renato Botelho
<?php
287 d61309a0 Phil Davis
			if (isset($gateway['defaultgw'])) {
288 c3c692a9 Sjon Hortensius
				echo " <strong>(default)</strong>";
289 d61309a0 Phil Davis
			}
290 e97df865 Renato Botelho
?>
291 060ed238 Stephen Beaver
						</td>
292
						<td>
293
							<?=htmlspecialchars(convert_friendly_interface_to_friendly_descr($gateway['friendlyiface']))?>
294
						</td>
295
						<td>
296
							<?=htmlspecialchars($gateway['gateway'])?>
297
						</td>
298
						<td>
299
							<?=htmlspecialchars($gateway['monitor'])?>
300
						</td>
301
						<td>
302
							<?=htmlspecialchars($gateway['descr'])?>
303
						</td>
304
						<td>
305 5154b00d Phil Davis
							<a href="system_gateways_edit.php?id=<?=$i?>" class="fa fa-pencil" title="<?=gettext('Edit gateway');?>"></a>
306
							<a href="system_gateways_edit.php?dup=<?=$i?>" class="fa fa-clone" title="<?=gettext('Copy gateway')?>"></a>
307 f74457df Stephen Beaver
308 fa172bc5 NewEraCracker
<?php if (is_numeric($gateway['attribute'])): ?>
309 1629e8ea heper
	<?php if (isset($gateway['disabled'])) {
310 f74457df Stephen Beaver
	?>
311 5154b00d Phil Davis
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable gateway')?>"></a>
312 1629e8ea heper
	<?php } else {
313
	?>
314 5154b00d Phil Davis
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable gateway')?>"></a>
315 1629e8ea heper
	<?php }
316 f74457df Stephen Beaver
	?>
317 5154b00d Phil Davis
							<a href="system_gateways.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete gateway')?>"></a>
318 f74457df Stephen Beaver
319 fa172bc5 NewEraCracker
<?php endif; ?>
320 060ed238 Stephen Beaver
						</td>
321
					</tr>
322 fa172bc5 NewEraCracker
<?php endforeach; ?>
323 060ed238 Stephen Beaver
				</tbody>
324
			</table>
325
		</div>
326
	</div>
327
</div>
328 c3c692a9 Sjon Hortensius
329 c10cb196 Stephen Beaver
<nav class="action-buttons">
330 c3c692a9 Sjon Hortensius
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
331 9d5a20cf heper
		<i class="fa fa-plus icon-embed-btn"></i>
332 f74457df Stephen Beaver
		<?=gettext("Add");?>
333 c3c692a9 Sjon Hortensius
	</a>
334
</nav>
335 e97df865 Renato Botelho
<?php
336 c3c692a9 Sjon Hortensius
337 1629e8ea heper
include("foot.inc");