Project

General

Profile

Download (10.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * system_gateways.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2010 Seth Mos <seth.mos@dds.nl>
8
 * All rights reserved.
9
 *
10
 * 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
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * 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
 */
22

    
23
##|+PRIV
24
##|*IDENT=page-system-gateways
25
##|*NAME=System: Gateways
26
##|*DESCR=Allow access to the 'System: Gateways' page.
27
##|*MATCH=system_gateways.php*
28
##|-PRIV
29

    
30
require_once("guiconfig.inc");
31
require_once("functions.inc");
32
require_once("filter.inc");
33
require_once("shaper.inc");
34

    
35
$a_gateways = return_gateways_array(true, false, true);
36
$a_gateways_arr = array();
37
foreach ($a_gateways as $gw) {
38
	$a_gateways_arr[] = $gw;
39
}
40
$a_gateways = $a_gateways_arr;
41

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

    
46
$a_gateway_item = &$config['gateways']['gateway_item'];
47

    
48
if ($_POST) {
49

    
50
	$pconfig = $_POST;
51

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

    
54
		$retval = 0;
55

    
56
		$retval |= system_routing_configure();
57
		$retval |= system_resolvconf_generate();
58
		$retval |= filter_configure();
59
		/* reconfigure our gateway monitor */
60
		setup_gateways_monitor();
61
		/* Dynamic DNS on gw groups may have changed */
62
		send_event("service reload dyndnsall");
63

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

    
70
function can_delete_disable_gateway_item($id, $disable = false) {
71
	global $config, $input_errors, $a_gateways;
72

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

    
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
					if (!$disable) {
83
						$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
					} else {
85
						$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
					}
87
				}
88
			}
89
		}
90
	}
91

    
92
	if (is_array($config['staticroutes']['route'])) {
93
		foreach ($config['staticroutes']['route'] as $route) {
94
			if ($route['gateway'] == $a_gateways[$id]['name']) {
95
				if (!$disable) {
96
					// The user wants to delete this gateway, but there is a static route (enabled or disabled) that refers to the gateway.
97
					$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
				} 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
					$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
				}
103
			}
104
		}
105
	}
106

    
107
	if (isset($input_errors)) {
108
		return false;
109
	}
110

    
111
	return true;
112
}
113

    
114
function delete_gateway_item($id) {
115
	global $config, $a_gateways;
116

    
117
	if (!isset($a_gateways[$id])) {
118
		return;
119
	}
120

    
121
	/* 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
	/* NOTE: Cleanup static routes for the interface route if any */
130
	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
		$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
	/* NOTE: Cleanup static routes for the monitor ip if any */
139
	if (!empty($a_gateways[$id]['monitor']) &&
140
	    $a_gateways[$id]['monitor'] != "dynamic" &&
141
	    is_ipaddr($a_gateways[$id]['monitor']) &&
142
	    $a_gateways[$id]['gateway'] != $a_gateways[$id]['monitor']) {
143
		if (is_ipaddrv4($a_gateways[$id]['monitor'])) {
144
			mwexec("/sbin/route delete " . escapeshellarg($a_gateways[$id]['monitor']));
145
		} else {
146
			mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateways[$id]['monitor']));
147
		}
148
	}
149

    
150
	if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
151
		unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
152
	}
153
	unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
154
}
155

    
156
unset($input_errors);
157
if ($_GET['act'] == "del") {
158
	if (can_delete_disable_gateway_item($_GET['id'])) {
159
		$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
		foreach ($_POST['rule'] as $rulei) {
172
			if (!can_delete_disable_gateway_item($rulei)) {
173
				break;
174
			}
175
		}
176

    
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
				write_config(sprintf(gettext("Gateways: removed gateways %s", $items_deleted)));
185
				mark_subsystem_dirty('staticroutes');
186
			}
187
			header("Location: system_gateways.php");
188
			exit;
189
		}
190
	}
191

    
192
} else if ($_GET['act'] == "toggle" && $a_gateways[$_GET['id']]) {
193
	$realid = $a_gateways[$_GET['id']]['attribute'];
194
	$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
	} else {
199
		// The user wants to enable the gateway. That is always OK.
200
		$ok_to_toggle = true;
201
	}
202
	if ($ok_to_toggle) {
203
		if ($disable_gw) {
204
			$a_gateway_item[$realid]['disabled'] = true;
205
			/* 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
		} else {
212
			unset($a_gateway_item[$realid]['disabled']);
213
		}
214

    
215
		if (write_config("Gateways: enable/disable")) {
216
			mark_subsystem_dirty('staticroutes');
217
		}
218

    
219
		header("Location: system_gateways.php");
220
		exit;
221
	}
222
}
223

    
224
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Gateways"));
225
$shortcut_section = "gateways";
226

    
227
include("head.inc");
228

    
229
if ($input_errors) {
230
	print_input_errors($input_errors);
231
}
232

    
233
if ($_POST['apply']) {
234
	print_apply_result_box($retval);
235
}
236

    
237
if (is_subsystem_dirty('staticroutes')) {
238
	print_apply_box(gettext("The gateway configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
239
}
240

    
241
$tab_array = array();
242
$tab_array[0] = array(gettext("Gateways"), true, "system_gateways.php");
243
$tab_array[1] = array(gettext("Static Routes"), false, "system_routes.php");
244
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
245
display_top_tabs($tab_array);
246

    
247
?>
248
<div class="panel panel-default">
249
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Gateways')?></h2></div>
250
	<div class="panel-body">
251
		<div class="table-responsive">
252
			<table class="table table-striped table-hover table-condensed table-rowdblclickedit">
253
				<thead>
254
					<tr>
255
						<th></th>
256
						<th><?=gettext("Name")?></th>
257
						<th><?=gettext("Interface")?></th>
258
						<th><?=gettext("Gateway")?></th>
259
						<th><?=gettext("Monitor IP")?></th>
260
						<th><?=gettext("Description")?></th>
261
						<th><?=gettext("Actions")?></th>
262
					</tr>
263
				</thead>
264
				<tbody>
265
<?php
266
foreach ($a_gateways as $i => $gateway):
267
	if (isset($gateway['inactive'])) {
268
		$icon = 'fa-times-circle-o';
269
	} elseif (isset($gateway['disabled'])) {
270
		$icon = 'fa-ban';
271
	} else {
272
		$icon = 'fa-check-circle-o';
273
	}
274

    
275
	if (isset($gateway['inactive'])) {
276
		$title = gettext("This gateway is inactive because interface is missing");
277
	} else {
278
		$title = '';
279
	}
280
?>
281
				<tr<?=($icon != 'fa-check-circle-o')? ' class="disabled"' : ''?>>
282
					<td title="<?=$title?>"><i class="fa <?=$icon?>"></i></td>
283
					<td>
284
						<?=htmlspecialchars($gateway['name'])?>
285
<?php
286
			if (isset($gateway['defaultgw'])) {
287
				echo " <strong>(default)</strong>";
288
			}
289
?>
290
						</td>
291
						<td>
292
							<?=htmlspecialchars(convert_friendly_interface_to_friendly_descr($gateway['friendlyiface']))?>
293
						</td>
294
						<td>
295
							<?=htmlspecialchars($gateway['gateway'])?>
296
						</td>
297
						<td>
298
							<?=htmlspecialchars($gateway['monitor'])?>
299
						</td>
300
						<td>
301
							<?=htmlspecialchars($gateway['descr'])?>
302
						</td>
303
						<td>
304
							<a href="system_gateways_edit.php?id=<?=$i?>" class="fa fa-pencil" title="<?=gettext('Edit gateway');?>"></a>
305
							<a href="system_gateways_edit.php?dup=<?=$i?>" class="fa fa-clone" title="<?=gettext('Copy gateway')?>"></a>
306

    
307
<?php if (is_numeric($gateway['attribute'])): ?>
308
	<?php if (isset($gateway['disabled'])) {
309
	?>
310
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable gateway')?>"></a>
311
	<?php } else {
312
	?>
313
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable gateway')?>"></a>
314
	<?php }
315
	?>
316
							<a href="system_gateways.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete gateway')?>"></a>
317

    
318
<?php endif; ?>
319
						</td>
320
					</tr>
321
<?php endforeach; ?>
322
				</tbody>
323
			</table>
324
		</div>
325
	</div>
326
</div>
327

    
328
<nav class="action-buttons">
329
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
330
		<i class="fa fa-plus icon-embed-btn"></i>
331
		<?=gettext("Add");?>
332
	</a>
333
</nav>
334
<?php
335

    
336
include("foot.inc");
(197-197/225)