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 4611e283 Steve Beaver
$pconfig = $_REQUEST;
49 d173230c Seth Mos
50 4611e283 Steve Beaver
if ($_POST['apply']) {
51 d173230c Seth Mos
52 4611e283 Steve Beaver
	$retval = 0;
53 d173230c Seth Mos
54 4611e283 Steve Beaver
	$retval |= system_routing_configure();
55
	$retval |= system_resolvconf_generate();
56
	$retval |= filter_configure();
57
	/* reconfigure our gateway monitor */
58
	setup_gateways_monitor();
59
	/* Dynamic DNS on gw groups may have changed */
60
	send_event("service reload dyndnsall");
61 d173230c Seth Mos
62 4611e283 Steve Beaver
	if ($retval == 0) {
63
		clear_subsystem_dirty('staticroutes');
64 d173230c Seth Mos
	}
65
}
66
67 4611e283 Steve Beaver
68 028ff8f8 Phil Davis
function can_delete_disable_gateway_item($id, $disable = false) {
69 e97df865 Renato Botelho
	global $config, $input_errors, $a_gateways;
70 d251a8d4 Renato Botelho
71 e0c7b2fe Phil Davis
	if (!isset($a_gateways[$id])) {
72 e97df865 Renato Botelho
		return false;
73 e0c7b2fe Phil Davis
	}
74 e97df865 Renato Botelho
75
	if (is_array($config['gateways']['gateway_group'])) {
76
		foreach ($config['gateways']['gateway_group'] as $group) {
77
			foreach ($group['item'] as $item) {
78
				$items = explode("|", $item);
79
				if ($items[0] == $a_gateways[$id]['name']) {
80 205178aa Phil Davis
					if (!$disable) {
81 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']);
82 205178aa Phil Davis
					} else {
83 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']);
84 028ff8f8 Phil Davis
					}
85 f78302e8 Ermal
				}
86
			}
87
		}
88 e97df865 Renato Botelho
	}
89
90
	if (is_array($config['staticroutes']['route'])) {
91
		foreach ($config['staticroutes']['route'] as $route) {
92
			if ($route['gateway'] == $a_gateways[$id]['name']) {
93 205178aa Phil Davis
				if (!$disable) {
94 028ff8f8 Phil Davis
					// The user wants to delete this gateway, but there is a static route (enabled or disabled) that refers to the gateway.
95 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']);
96 205178aa Phil Davis
				} else if (!isset($route['disabled'])) {
97
					// The user wants to disable this gateway.
98
					// But there is a static route that uses this gateway and is enabled (not disabled).
99 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']);
100 028ff8f8 Phil Davis
				}
101 f78302e8 Ermal
			}
102
		}
103 e97df865 Renato Botelho
	}
104
105 e0c7b2fe Phil Davis
	if (isset($input_errors)) {
106 e97df865 Renato Botelho
		return false;
107 e0c7b2fe Phil Davis
	}
108 e97df865 Renato Botelho
109
	return true;
110
}
111
112
function delete_gateway_item($id) {
113 dde20226 Renato Botelho
	global $config, $a_gateways;
114
115 e0c7b2fe Phil Davis
	if (!isset($a_gateways[$id])) {
116 e97df865 Renato Botelho
		return;
117 e0c7b2fe Phil Davis
	}
118 32a9eb18 Ermal
119 1be1b87b jim-p
	/* If the removed gateway was the default route, remove the default route */
120
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
121
	    !isset($a_gateways[$id]['disabled']) &&
122
	    isset($a_gateways[$id]['defaultgw'])) {
123
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? '-inet6' : '-inet');
124
		mwexec("/sbin/route delete {$inet} default");
125
	}
126
127 e75f0e7d PiBa-NL
	/* NOTE: Cleanup static routes for the interface route if any */
128 d61309a0 Phil Davis
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
129
	    $gateway['gateway'] != $a_gateways[$id]['gateway'] &&
130
	    isset($a_gateways[$id]["nonlocalgateway"])) {
131 e75f0e7d PiBa-NL
		$realif = get_real_interface($a_gateways[$id]['interface']);
132
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? "-inet6" : "-inet");
133
		$cmd = "/sbin/route delete $inet " . escapeshellarg($a_gateways[$id]['gateway']) . " -iface " . escapeshellarg($realif);
134
		mwexec($cmd);
135
	}
136 e97df865 Renato Botelho
	/* NOTE: Cleanup static routes for the monitor ip if any */
137
	if (!empty($a_gateways[$id]['monitor']) &&
138 d61309a0 Phil Davis
	    $a_gateways[$id]['monitor'] != "dynamic" &&
139
	    is_ipaddr($a_gateways[$id]['monitor']) &&
140
	    $a_gateways[$id]['gateway'] != $a_gateways[$id]['monitor']) {
141 e0c7b2fe Phil Davis
		if (is_ipaddrv4($a_gateways[$id]['monitor'])) {
142 e97df865 Renato Botelho
			mwexec("/sbin/route delete " . escapeshellarg($a_gateways[$id]['monitor']));
143 e0c7b2fe Phil Davis
		} else {
144 e97df865 Renato Botelho
			mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateways[$id]['monitor']));
145 e0c7b2fe Phil Davis
		}
146 e97df865 Renato Botelho
	}
147
148 e0c7b2fe Phil Davis
	if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
149 e97df865 Renato Botelho
		unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
150 e0c7b2fe Phil Davis
	}
151 e97df865 Renato Botelho
	unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
152
}
153
154
unset($input_errors);
155 4611e283 Steve Beaver
if ($_REQUEST['act'] == "del") {
156
	if (can_delete_disable_gateway_item($_REQUEST['id'])) {
157
		$realid = $a_gateways[$_REQUEST['id']]['attribute'];
158
		delete_gateway_item($_REQUEST['id']);
159 e97df865 Renato Botelho
		write_config("Gateways: removed gateway {$realid}");
160
		mark_subsystem_dirty('staticroutes');
161
		header("Location: system_gateways.php");
162
		exit;
163
	}
164
}
165
166 4611e283 Steve Beaver
if (isset($_REQUEST['del_x'])) {
167 e97df865 Renato Botelho
	/* delete selected items */
168 4611e283 Steve Beaver
	if (is_array($_REQUEST['rule']) && count($_REQUEST['rule'])) {
169
		foreach ($_REQUEST['rule'] as $rulei) {
170 028ff8f8 Phil Davis
			if (!can_delete_disable_gateway_item($rulei)) {
171 e97df865 Renato Botelho
				break;
172 e0c7b2fe Phil Davis
			}
173
		}
174 e97df865 Renato Botelho
175
		if (!isset($input_errors)) {
176
			$items_deleted = "";
177 4611e283 Steve Beaver
			foreach ($_REQUEST['rule'] as $rulei) {
178 e97df865 Renato Botelho
				delete_gateway_item($rulei);
179
				$items_deleted .= "{$rulei} ";
180
			}
181
			if (!empty($items_deleted)) {
182 762faef5 Phil Davis
				write_config(sprintf(gettext("Gateways: removed gateways %s", $items_deleted)));
183 e97df865 Renato Botelho
				mark_subsystem_dirty('staticroutes');
184
			}
185 f78302e8 Ermal
			header("Location: system_gateways.php");
186
			exit;
187
		}
188 d173230c Seth Mos
	}
189
190 4611e283 Steve Beaver
} else if ($_REQUEST['act'] == "toggle" && $a_gateways[$_REQUEST['id']]) {
191
	$realid = $a_gateways[$_REQUEST['id']]['attribute'];
192 028ff8f8 Phil Davis
	$disable_gw = !isset($a_gateway_item[$realid]['disabled']);
193
	if ($disable_gw) {
194
		// The user wants to disable the gateway, so check if that is OK.
195 4611e283 Steve Beaver
		$ok_to_toggle = can_delete_disable_gateway_item($_REQUEST['id'], $disable_gw);
196 e0c7b2fe Phil Davis
	} else {
197 028ff8f8 Phil Davis
		// The user wants to enable the gateway. That is always OK.
198
		$ok_to_toggle = true;
199 e0c7b2fe Phil Davis
	}
200 028ff8f8 Phil Davis
	if ($ok_to_toggle) {
201
		if ($disable_gw) {
202
			$a_gateway_item[$realid]['disabled'] = true;
203 1be1b87b jim-p
			/* If the disabled gateway was the default route, remove the default route */
204
			if (!empty($a_gateway_item[$realid]) && is_ipaddr($a_gateway_item[$realid]['gateway']) &&
205
			    isset($a_gateway_item[$realid]['defaultgw'])) {
206
				$inet = (!is_ipaddrv4($a_gateway_item[$realid]['gateway']) ? '-inet6' : '-inet');
207
				mwexec("/sbin/route delete {$inet} default");
208
			}
209 028ff8f8 Phil Davis
		} else {
210
			unset($a_gateway_item[$realid]['disabled']);
211
		}
212 e97df865 Renato Botelho
213 028ff8f8 Phil Davis
		if (write_config("Gateways: enable/disable")) {
214
			mark_subsystem_dirty('staticroutes');
215
		}
216 e97df865 Renato Botelho
217 028ff8f8 Phil Davis
		header("Location: system_gateways.php");
218
		exit;
219
	}
220 e97df865 Renato Botelho
}
221 124aee67 Chris Buechler
222 d036bc07 Stephen Beaver
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Gateways"));
223 edcd7535 Phil Davis
$pglinks = array("", "@self", "@self");
224 b32dd0a6 jim-p
$shortcut_section = "gateways";
225 02ca24c9 jim-p
226 d173230c Seth Mos
include("head.inc");
227
228 d61309a0 Phil Davis
if ($input_errors) {
229 c3c692a9 Sjon Hortensius
	print_input_errors($input_errors);
230 d61309a0 Phil Davis
}
231 44c42356 Phil Davis
232
if ($_POST['apply']) {
233
	print_apply_result_box($retval);
234 d61309a0 Phil Davis
}
235 f74457df Stephen Beaver
236 d61309a0 Phil Davis
if (is_subsystem_dirty('staticroutes')) {
237 7fdca5ff NOYB
	print_apply_box(gettext("The gateway configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
238 d61309a0 Phil Davis
}
239 c3c692a9 Sjon Hortensius
240
$tab_array = array();
241
$tab_array[0] = array(gettext("Gateways"), true, "system_gateways.php");
242 80b4d0c5 heper
$tab_array[1] = array(gettext("Static Routes"), false, "system_routes.php");
243
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
244 c3c692a9 Sjon Hortensius
display_top_tabs($tab_array);
245 d173230c Seth Mos
246 d251a8d4 Renato Botelho
?>
247 060ed238 Stephen Beaver
<div class="panel panel-default">
248
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Gateways')?></h2></div>
249
	<div class="panel-body">
250
		<div class="table-responsive">
251 47180823 Colin Fleming
			<table class="table table-striped table-hover table-condensed table-rowdblclickedit">
252 060ed238 Stephen Beaver
				<thead>
253
					<tr>
254
						<th></th>
255
						<th><?=gettext("Name")?></th>
256
						<th><?=gettext("Interface")?></th>
257
						<th><?=gettext("Gateway")?></th>
258
						<th><?=gettext("Monitor IP")?></th>
259
						<th><?=gettext("Description")?></th>
260
						<th><?=gettext("Actions")?></th>
261
					</tr>
262
				</thead>
263
				<tbody>
264 e97df865 Renato Botelho
<?php
265 c3c692a9 Sjon Hortensius
foreach ($a_gateways as $i => $gateway):
266 d61309a0 Phil Davis
	if (isset($gateway['inactive'])) {
267 1b7379f9 Jared Dillard
		$icon = 'fa-times-circle-o';
268 d61309a0 Phil Davis
	} elseif (isset($gateway['disabled'])) {
269 1b7379f9 Jared Dillard
		$icon = 'fa-ban';
270 d61309a0 Phil Davis
	} else {
271 1b7379f9 Jared Dillard
		$icon = 'fa-check-circle-o';
272 d61309a0 Phil Davis
	}
273 c3c692a9 Sjon Hortensius
274 d61309a0 Phil Davis
	if (isset($gateway['inactive'])) {
275 c3c692a9 Sjon Hortensius
		$title = gettext("This gateway is inactive because interface is missing");
276 d61309a0 Phil Davis
	} else {
277 c3c692a9 Sjon Hortensius
		$title = '';
278 d61309a0 Phil Davis
	}
279 e97df865 Renato Botelho
?>
280 060ed238 Stephen Beaver
				<tr<?=($icon != 'fa-check-circle-o')? ' class="disabled"' : ''?>>
281
					<td title="<?=$title?>"><i class="fa <?=$icon?>"></i></td>
282
					<td>
283
						<?=htmlspecialchars($gateway['name'])?>
284 e97df865 Renato Botelho
<?php
285 d61309a0 Phil Davis
			if (isset($gateway['defaultgw'])) {
286 c3c692a9 Sjon Hortensius
				echo " <strong>(default)</strong>";
287 d61309a0 Phil Davis
			}
288 e97df865 Renato Botelho
?>
289 060ed238 Stephen Beaver
						</td>
290
						<td>
291
							<?=htmlspecialchars(convert_friendly_interface_to_friendly_descr($gateway['friendlyiface']))?>
292
						</td>
293
						<td>
294
							<?=htmlspecialchars($gateway['gateway'])?>
295
						</td>
296
						<td>
297
							<?=htmlspecialchars($gateway['monitor'])?>
298
						</td>
299
						<td>
300
							<?=htmlspecialchars($gateway['descr'])?>
301
						</td>
302
						<td>
303 4611e283 Steve Beaver
							<a href="system_gateways_edit.php?id=<?=$i?>" class="fa fa-pencil" title="<?=gettext('Edit gateway');?>"></a>
304
							<a href="system_gateways_edit.php?dup=<?=$i?>" class="fa fa-clone" title="<?=gettext('Copy gateway')?>"></a>
305 f74457df Stephen Beaver
306 fa172bc5 NewEraCracker
<?php if (is_numeric($gateway['attribute'])): ?>
307 1629e8ea heper
	<?php if (isset($gateway['disabled'])) {
308 f74457df Stephen Beaver
	?>
309 a04f6658 Steve Beaver
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable gateway')?>" usepost></a>
310 1629e8ea heper
	<?php } else {
311
	?>
312 a04f6658 Steve Beaver
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable gateway')?>" usepost></a>
313 1629e8ea heper
	<?php }
314 f74457df Stephen Beaver
	?>
315 a04f6658 Steve Beaver
							<a href="system_gateways.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete gateway')?>" usepost></a>
316 f74457df Stephen Beaver
317 fa172bc5 NewEraCracker
<?php endif; ?>
318 060ed238 Stephen Beaver
						</td>
319
					</tr>
320 fa172bc5 NewEraCracker
<?php endforeach; ?>
321 060ed238 Stephen Beaver
				</tbody>
322
			</table>
323
		</div>
324
	</div>
325
</div>
326 c3c692a9 Sjon Hortensius
327 c10cb196 Stephen Beaver
<nav class="action-buttons">
328 4611e283 Steve Beaver
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
329 9d5a20cf heper
		<i class="fa fa-plus icon-embed-btn"></i>
330 f74457df Stephen Beaver
		<?=gettext("Add");?>
331 c3c692a9 Sjon Hortensius
	</a>
332
</nav>
333 e97df865 Renato Botelho
<?php
334 c3c692a9 Sjon Hortensius
335 1629e8ea heper
include("foot.inc");