Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	system_gateways.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *	Copyright (c)  2010 Seth Mos <seth.mos@dds.nl>
8
 *
9
 *	Redistribution and use in source and binary forms, with or without modification,
10
 *	are permitted provided that the following conditions are met:
11
 *
12
 *	1. Redistributions of source code must retain the above copyright notice,
13
 *		this list of conditions and the following disclaimer.
14
 *
15
 *	2. Redistributions in binary form must reproduce the above copyright
16
 *		notice, this list of conditions and the following disclaimer in
17
 *		the documentation and/or other materials provided with the
18
 *		distribution.
19
 *
20
 *	3. All advertising materials mentioning features or use of this software
21
 *		must display the following acknowledgment:
22
 *		"This product includes software developed by the pfSense Project
23
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
24
 *
25
 *	4. The names "pfSense" and "pfSense Project" must not be used to
26
 *		 endorse or promote products derived from this software without
27
 *		 prior written permission. For written permission, please contact
28
 *		 coreteam@pfsense.org.
29
 *
30
 *	5. Products derived from this software may not be called "pfSense"
31
 *		nor may "pfSense" appear in their names without prior written
32
 *		permission of the Electric Sheep Fencing, LLC.
33
 *
34
 *	6. Redistributions of any form whatsoever must retain the following
35
 *		acknowledgment:
36
 *
37
 *	"This product includes software developed by the pfSense Project
38
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
39
 *
40
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
52
 *
53
 *	====================================================================
54
 *
55
 */
56

    
57
##|+PRIV
58
##|*IDENT=page-system-gateways
59
##|*NAME=System: Gateways
60
##|*DESCR=Allow access to the 'System: Gateways' page.
61
##|*MATCH=system_gateways.php*
62
##|-PRIV
63

    
64
require("guiconfig.inc");
65
require_once("functions.inc");
66
require_once("filter.inc");
67
require_once("shaper.inc");
68

    
69
$a_gateways = return_gateways_array(true, false, true);
70
$a_gateways_arr = array();
71
foreach ($a_gateways as $gw) {
72
	$a_gateways_arr[] = $gw;
73
}
74
$a_gateways = $a_gateways_arr;
75

    
76
if (!is_array($config['gateways']['gateway_item'])) {
77
	$config['gateways']['gateway_item'] = array();
78
}
79

    
80
$a_gateway_item = &$config['gateways']['gateway_item'];
81

    
82
if ($_POST) {
83

    
84
	$pconfig = $_POST;
85

    
86
	if ($_POST['apply']) {
87

    
88
		$retval = 0;
89

    
90
		$retval = system_routing_configure();
91
		$retval |= system_resolvconf_generate();
92
		$retval |= filter_configure();
93
		/* reconfigure our gateway monitor */
94
		setup_gateways_monitor();
95
		/* Dynamic DNS on gw groups may have changed */
96
		send_event("service reload dyndnsall");
97

    
98
		$savemsg = get_std_save_message($retval);
99
		if ($retval == 0) {
100
			clear_subsystem_dirty('staticroutes');
101
		}
102
	}
103
}
104

    
105
function can_delete_disable_gateway_item($id, $disable = false) {
106
	global $config, $input_errors, $a_gateways;
107

    
108
	if (!isset($a_gateways[$id])) {
109
		return false;
110
	}
111

    
112
	if (is_array($config['gateways']['gateway_group'])) {
113
		foreach ($config['gateways']['gateway_group'] as $group) {
114
			foreach ($group['item'] as $item) {
115
				$items = explode("|", $item);
116
				if ($items[0] == $a_gateways[$id]['name']) {
117
					if (!$disable) {
118
						$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']);
119
					} else {
120
						$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']);
121
					}
122
				}
123
			}
124
		}
125
	}
126

    
127
	if (is_array($config['staticroutes']['route'])) {
128
		foreach ($config['staticroutes']['route'] as $route) {
129
			if ($route['gateway'] == $a_gateways[$id]['name']) {
130
				if (!$disable) {
131
					// The user wants to delete this gateway, but there is a static route (enabled or disabled) that refers to the gateway.
132
					$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']);
133
				} else if (!isset($route['disabled'])) {
134
					// The user wants to disable this gateway.
135
					// But there is a static route that uses this gateway and is enabled (not disabled).
136
					$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']);
137
				}
138
			}
139
		}
140
	}
141

    
142
	if (isset($input_errors)) {
143
		return false;
144
	}
145

    
146
	return true;
147
}
148

    
149
function delete_gateway_item($id) {
150
	global $config, $a_gateways;
151

    
152
	if (!isset($a_gateways[$id])) {
153
		return;
154
	}
155

    
156
	/* NOTE: Cleanup static routes for the interface route if any */
157
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
158
	    $gateway['gateway'] != $a_gateways[$id]['gateway'] &&
159
	    isset($a_gateways[$id]["nonlocalgateway"])) {
160
		$realif = get_real_interface($a_gateways[$id]['interface']);
161
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? "-inet6" : "-inet");
162
		$cmd = "/sbin/route delete $inet " . escapeshellarg($a_gateways[$id]['gateway']) . " -iface " . escapeshellarg($realif);
163
		mwexec($cmd);
164
	}
165
	/* NOTE: Cleanup static routes for the monitor ip if any */
166
	if (!empty($a_gateways[$id]['monitor']) &&
167
	    $a_gateways[$id]['monitor'] != "dynamic" &&
168
	    is_ipaddr($a_gateways[$id]['monitor']) &&
169
	    $a_gateways[$id]['gateway'] != $a_gateways[$id]['monitor']) {
170
		if (is_ipaddrv4($a_gateways[$id]['monitor'])) {
171
			mwexec("/sbin/route delete " . escapeshellarg($a_gateways[$id]['monitor']));
172
		} else {
173
			mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateways[$id]['monitor']));
174
		}
175
	}
176

    
177
	if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
178
		unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
179
	}
180
	unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
181
}
182

    
183
unset($input_errors);
184
if ($_GET['act'] == "del") {
185
	if (can_delete_disable_gateway_item($_GET['id'])) {
186
		$realid = $a_gateways[$_GET['id']]['attribute'];
187
		delete_gateway_item($_GET['id']);
188
		write_config("Gateways: removed gateway {$realid}");
189
		mark_subsystem_dirty('staticroutes');
190
		header("Location: system_gateways.php");
191
		exit;
192
	}
193
}
194

    
195
if (isset($_POST['del_x'])) {
196
	/* delete selected items */
197
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
198
		foreach ($_POST['rule'] as $rulei) {
199
			if (!can_delete_disable_gateway_item($rulei)) {
200
				break;
201
			}
202
		}
203

    
204
		if (!isset($input_errors)) {
205
			$items_deleted = "";
206
			foreach ($_POST['rule'] as $rulei) {
207
				delete_gateway_item($rulei);
208
				$items_deleted .= "{$rulei} ";
209
			}
210
			if (!empty($items_deleted)) {
211
				write_config(sprintf(gettext("Gateways: removed gateways %s", $items_deleted)));
212
				mark_subsystem_dirty('staticroutes');
213
			}
214
			header("Location: system_gateways.php");
215
			exit;
216
		}
217
	}
218

    
219
} else if ($_GET['act'] == "toggle" && $a_gateways[$_GET['id']]) {
220
	$realid = $a_gateways[$_GET['id']]['attribute'];
221
	$disable_gw = !isset($a_gateway_item[$realid]['disabled']);
222
	if ($disable_gw) {
223
		// The user wants to disable the gateway, so check if that is OK.
224
		$ok_to_toggle = can_delete_disable_gateway_item($_GET['id'], $disable_gw);
225
	} else {
226
		// The user wants to enable the gateway. That is always OK.
227
		$ok_to_toggle = true;
228
	}
229
	if ($ok_to_toggle) {
230
		if ($disable_gw) {
231
			$a_gateway_item[$realid]['disabled'] = true;
232
		} else {
233
			unset($a_gateway_item[$realid]['disabled']);
234
		}
235

    
236
		if (write_config("Gateways: enable/disable")) {
237
			mark_subsystem_dirty('staticroutes');
238
		}
239

    
240
		header("Location: system_gateways.php");
241
		exit;
242
	}
243
}
244

    
245
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Gateways"));
246
$shortcut_section = "gateways";
247

    
248
include("head.inc");
249

    
250
if ($input_errors) {
251
	print_input_errors($input_errors);
252
}
253
if ($savemsg) {
254
	print_info_box($savemsg, 'success');
255
}
256

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

    
261
$tab_array = array();
262
$tab_array[0] = array(gettext("Gateways"), true, "system_gateways.php");
263
$tab_array[1] = array(gettext("Static Routes"), false, "system_routes.php");
264
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
265
display_top_tabs($tab_array);
266

    
267
?>
268
<div class="panel panel-default">
269
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Gateways')?></h2></div>
270
	<div class="panel-body">
271
		<div class="table-responsive">
272
			<table class="table table-striped tabel-hover table-condensed">
273
				<thead>
274
					<tr>
275
						<th></th>
276
						<th><?=gettext("Name")?></th>
277
						<th><?=gettext("Interface")?></th>
278
						<th><?=gettext("Gateway")?></th>
279
						<th><?=gettext("Monitor IP")?></th>
280
						<th><?=gettext("Description")?></th>
281
						<th><?=gettext("Actions")?></th>
282
					</tr>
283
				</thead>
284
				<tbody>
285
<?php
286
foreach ($a_gateways as $i => $gateway):
287
	if (isset($gateway['inactive'])) {
288
		$icon = 'fa-times-circle-o';
289
	} elseif (isset($gateway['disabled'])) {
290
		$icon = 'fa-ban';
291
	} else {
292
		$icon = 'fa-check-circle-o';
293
	}
294

    
295
	if (isset($gateway['inactive'])) {
296
		$title = gettext("This gateway is inactive because interface is missing");
297
	} else {
298
		$title = '';
299
	}
300
?>
301
				<tr<?=($icon != 'fa-check-circle-o')? ' class="disabled"' : ''?>>
302
					<td title="<?=$title?>"><i class="fa <?=$icon?>"></i></td>
303
					<td>
304
						<?=htmlspecialchars($gateway['name'])?>
305
<?php
306
			if (isset($gateway['defaultgw'])) {
307
				echo " <strong>(default)</strong>";
308
			}
309
?>
310
						</td>
311
						<td>
312
							<?=htmlspecialchars(convert_friendly_interface_to_friendly_descr($gateway['friendlyiface']))?>
313
						</td>
314
						<td>
315
							<?=htmlspecialchars($gateway['gateway'])?>
316
						</td>
317
						<td>
318
							<?=htmlspecialchars($gateway['monitor'])?>
319
						</td>
320
						<td>
321
							<?=htmlspecialchars($gateway['descr'])?>
322
						</td>
323
						<td>
324
							<a href="system_gateways_edit.php?id=<?=$i?>" class="fa fa-pencil" title="<?=gettext('Edit gateway');?>"></a>
325
							<a href="system_gateways_edit.php?dup=<?=$i?>" class="fa fa-clone" title="<?=gettext('Copy gateway')?>"></a>
326

    
327
<?php if (is_numeric($gateway['attribute'])): ?>
328
	<?php if (isset($gateway['disabled'])) {
329
	?>
330
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable gateway')?>"></a>
331
	<?php } else {
332
	?>
333
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable gateway')?>"></a>
334
	<?php }
335
	?>
336
							<a href="system_gateways.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete gateway')?>"></a>
337

    
338
<?php endif; ?>
339
						</td>
340
					</tr>
341
<?php endforeach; ?>
342
				</tbody>
343
			</table>
344
		</div>
345
	</div>
346
</div>
347

    
348
<nav class="action-buttons">
349
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
350
		<i class="fa fa-plus icon-embed-btn"></i>
351
		<?=gettext("Add");?>
352
	</a>
353
</nav>
354
<?php
355

    
356
include("foot.inc");
(198-198/225)