Project

General

Profile

Download (11 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
	pfSense_MODULE: routing
58
*/
59

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

    
67
require("guiconfig.inc");
68
require_once("functions.inc");
69
require_once("filter.inc");
70
require_once("shaper.inc");
71

    
72
$a_gateways = return_gateways_array(true, false, true);
73
$a_gateways_arr = array();
74
foreach ($a_gateways as $gw) {
75
	$a_gateways_arr[] = $gw;
76
}
77
$a_gateways = $a_gateways_arr;
78

    
79
if (!is_array($config['gateways']['gateway_item'])) {
80
	$config['gateways']['gateway_item'] = array();
81
}
82

    
83
$a_gateway_item = &$config['gateways']['gateway_item'];
84

    
85
if ($_POST) {
86

    
87
	$pconfig = $_POST;
88

    
89
	if ($_POST['apply']) {
90

    
91
		$retval = 0;
92

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

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

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

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

    
114
	if (is_array($config['gateways']['gateway_group'])) {
115
		foreach ($config['gateways']['gateway_group'] as $group) {
116
			foreach ($group['item'] as $item) {
117
				$items = explode("|", $item);
118
				if ($items[0] == $a_gateways[$id]['name']) {
119
					if (!$disable) {
120
						$input_errors[] = sprintf(gettext("Gateway '%s' cannot be deleted because it is in use on Gateway Group '%s'"), $a_gateways[$id]['name'], $group['name']);
121
					} else {
122
						$input_errors[] = sprintf(gettext("Gateway '%s' cannot be disabled because it is in use on Gateway Group '%s'"), $a_gateways[$id]['name'], $group['name']);
123
					}
124
				}
125
			}
126
		}
127
	}
128

    
129
	if (is_array($config['staticroutes']['route'])) {
130
		foreach ($config['staticroutes']['route'] as $route) {
131
			if ($route['gateway'] == $a_gateways[$id]['name']) {
132
				if (!$disable) {
133
					// The user wants to delete this gateway, but there is a static route (enabled or disabled) that refers to the gateway.
134
					$input_errors[] = sprintf(gettext("Gateway '%s' cannot be deleted because it is in use on Static Route '%s'"), $a_gateways[$id]['name'], $route['network']);
135
				} else if (!isset($route['disabled'])) {
136
					// The user wants to disable this gateway.
137
					// But there is a static route that uses this gateway and is enabled (not disabled).
138
					$input_errors[] = sprintf(gettext("Gateway '%s' cannot be disabled because it is in use on Static Route '%s'"), $a_gateways[$id]['name'], $route['network']);
139
				}
140
			}
141
		}
142
	}
143

    
144
	if (isset($input_errors)) {
145
		return false;
146
	}
147

    
148
	return true;
149
}
150

    
151
function delete_gateway_item($id) {
152
	global $config, $a_gateways;
153

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

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

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

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

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

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

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

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

    
242
		header("Location: system_gateways.php");
243
		exit;
244
	}
245
}
246

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

    
250
include("head.inc");
251

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

    
257
if (is_subsystem_dirty('staticroutes'))
258
	print_info_box_np(gettext("The gateway configuration has been changed.") . "<br />" . gettext("You must apply the changes in order for them to take effect."));
259

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

    
266
?>
267
<table class="table">
268
<thead>
269
	<tr>
270
		<th></th>
271
		<th><?=gettext("Name")?></th>
272
		<th><?=gettext("Interface")?></th>
273
		<th><?=gettext("Gateway")?></th>
274
		<th><?=gettext("Monitor IP")?></th>
275
		<th><?=gettext("Description")?></th>
276
		<th><?=gettext("Actions")?></th>
277
	</tr>
278
</thead>
279
<tbody>
280
<?php
281
foreach ($a_gateways as $i => $gateway):
282
	if (isset($gateway['inactive']))
283
		$icon = 'fa-times-circle-o';
284
	elseif (isset($gateway['disabled']))
285
		$icon = 'fa-ban';
286
	else
287
		$icon = 'fa-check-circle-o';
288

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

    
319
<? if (is_numeric($gateway['attribute'])): ?>
320
	<?php if (isset($gateway['disabled'])) {
321
	?>
322
			<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable')?>"></a>
323
	<?php } else {
324
	?>
325
			<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable')?>"></a>
326
	<?php }
327
	?>
328
			<a href="system_gateways.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete')?>"></a>
329

    
330
<? endif?>
331
		</td>
332
	</tr>
333
<? endforeach?>
334
</tbody>
335
</table>
336

    
337
<nav class="action-buttons">
338
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
339
		<i class="fa fa-plus icon-embed-btn"></i>
340
		<?=gettext("Add");?>
341
	</a>
342
</nav>
343
<?php
344

    
345
include("foot.inc");
(199-199/228)