Project

General

Profile

Download (16.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	system_routes_edit.php
4
	part of m0n0wall (http://m0n0.ch/wall)
5

    
6
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
7
	Copyright (C) 2010 Scott Ullrich
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31
/*
32
	pfSense_MODULE:	routing
33
*/
34

    
35
##|+PRIV
36
##|*IDENT=page-system-staticroutes-editroute
37
##|*NAME=System: Static Routes: Edit route page
38
##|*DESCR=Allow access to the 'System: Static Routes: Edit route' page.
39
##|*MATCH=system_routes_edit.php*
40
##|-PRIV
41

    
42
function staticroutecmp($a, $b) {
43
	return strcmp($a['network'], $b['network']);
44
}
45

    
46
function staticroutes_sort() {
47
	global $g, $config;
48

    
49
	if (!is_array($config['staticroutes']['route']))
50
		return;
51

    
52
	usort($config['staticroutes']['route'], "staticroutecmp");
53
}
54

    
55
require_once("guiconfig.inc");
56
require_once("filter.inc");
57
require_once("util.inc");
58
require_once("gwlb.inc");
59

    
60
if (!is_array($config['staticroutes']['route']))
61
	$config['staticroutes']['route'] = array();
62

    
63
$a_routes = &$config['staticroutes']['route'];
64
$a_gateways = return_gateways_array(true, true);
65

    
66
$id = $_GET['id'];
67
if (isset($_POST['id']))
68
	$id = $_POST['id'];
69

    
70
if (isset($_GET['dup'])) {
71
	$id = $_GET['dup'];
72
}
73

    
74
if (isset($id) && $a_routes[$id]) {
75
	list($pconfig['network'],$pconfig['network_subnet']) =
76
		explode('/', $a_routes[$id]['network']);
77
	$pconfig['gateway'] = $a_routes[$id]['gateway'];
78
	$pconfig['descr'] = $a_routes[$id]['descr'];
79
	$pconfig['disabled'] = isset($a_routes[$id]['disabled']);
80
}
81

    
82
if (isset($_GET['dup']))
83
	unset($id);
84

    
85
if ($_POST) {
86

    
87
	global $aliastable;
88

    
89
	unset($input_errors);
90
	$pconfig = $_POST;
91

    
92
	/* input validation */
93
	$reqdfields = explode(" ", "network network_subnet gateway");
94
	$reqdfieldsn = explode(",",
95
			gettext("Destination network") . "," .
96
			gettext("Destination network bit count") . "," .
97
			gettext("Gateway"));
98

    
99
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
100

    
101
	if (($_POST['network'] && !is_ipaddr($_POST['network']) && !is_alias($_POST['network']))) {
102
		$input_errors[] = gettext("A valid IPv4 or IPv6 destination network must be specified.");
103
	}
104
	if (($_POST['network_subnet'] && !is_numeric($_POST['network_subnet']))) {
105
		$input_errors[] = gettext("A valid destination network bit count must be specified.");
106
	}
107
	if (($_POST['gateway']) && is_ipaddr($_POST['network'])) {
108
		if (!isset($a_gateways[$_POST['gateway']]))
109
			$input_errors[] = gettext("A valid gateway must be specified.");
110
		if(!validate_address_family($_POST['network'], lookup_gateway_ip_by_name($_POST['gateway'])))
111
			$input_errors[] = gettext("The gateway '{$a_gateways[$_POST['gateway']]['gateway']}' is a different Address Family as network '{$_POST['network']}'.");
112
	}
113

    
114
	/* check for overlaps */
115
	$current_targets = get_staticroutes(true);
116
	$new_targets = array();
117
	if(is_ipaddrv6($_POST['network'])) {
118
		$osn = gen_subnetv6($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
119
		$new_targets[] = $osn;
120
	}
121
	if (is_ipaddrv4($_POST['network'])) {
122
		if($_POST['network_subnet'] > 32)
123
			$input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
124
		else {
125
			$osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
126
			$new_targets[] = $osn;
127
		}
128
	} elseif (is_alias($_POST['network'])) {
129
		$osn = $_POST['network'];
130
		$fqdn_found = 0;
131
		foreach (preg_split('/\s+/', $aliastable[$osn]) as $tgt) {
132
			if (!is_ipaddr($tgt)) {
133
				if ($fqdn_found === 0) {
134
					$input_errors[] = sprintf(gettext("The alias (%s) has one or more FQDNs configured and cannot be used to configure a static route."), $_POST['network']);
135
					$fqdn_found = 1;
136
				}
137
				continue;
138
			}
139
			if (is_ipaddrv4($tgt))
140
				$tgt .= "/32";
141
			if (is_ipaddrv6($tgt))
142
				$tgt .= "/128";
143
			if (!is_subnet($tgt))
144
				continue;
145
			if (!is_subnetv6($tgt))
146
				continue;
147
			$new_targets[] = $tgt;
148
		}
149
	}
150
	if (!isset($id))
151
		$id = count($a_routes);
152
	$oroute = $a_routes[$id];
153
	$old_targets = array();
154
	if (!empty($oroute)) {
155
		if (is_alias($oroute['network'])) {
156
			foreach (filter_expand_alias_array($oroute['network']) as $tgt) {
157
				if (is_ipaddrv4($tgt))
158
					$tgt .= "/32";
159
				else if (is_ipaddrv6($tgt))
160
					$tgt .= "/128";
161
				if (!is_subnet($tgt))
162
					continue;
163
				$old_targets[] = $tgt;
164
			}
165
		} else {
166
			$old_targets[] = $oroute['network'];
167
		}
168
	}
169

    
170
	$overlaps = array_intersect($current_targets, $new_targets);
171
	$overlaps = array_diff($overlaps, $old_targets);
172
	if (count($overlaps)) {
173
		$input_errors[] = gettext("A route to these destination networks already exists") . ": " . implode(", ", $overlaps);
174
	}
175

    
176
	if (is_array($config['interfaces'])) {
177
		foreach ($config['interfaces'] as $if) {
178
			if (is_ipaddrv4($_POST['network'])
179
				&& isset($if['ipaddr']) && isset($if['subnet'])
180
				&& is_ipaddrv4($if['ipaddr']) && is_numeric($if['subnet'])
181
				&& ($_POST['network_subnet'] == $if['subnet'])
182
				&& (gen_subnet($_POST['network'], $_POST['network_subnet']) == gen_subnet($if['ipaddr'], $if['subnet'])))
183
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
184

    
185
			else if (is_ipaddrv6($_POST['network'])
186
				&& isset($if['ipaddrv6']) && isset($if['subnetv6'])
187
				&& is_ipaddrv6($if['ipaddrv6']) && is_numeric($if['subnetv6'])
188
				&& ($_POST['network_subnet'] == $if['subnetv6'])
189
				&& (gen_subnetv6($_POST['network'], $_POST['network_subnet']) == gen_subnetv6($if['ipaddrv6'], $if['subnetv6'])))
190
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
191
		}
192
	}
193

    
194
	if (!$input_errors) {
195
		$route = array();
196
		$route['network'] = $osn;
197
		$route['gateway'] = $_POST['gateway'];
198
		$route['descr'] = $_POST['descr'];
199
		if ($_POST['disabled'])
200
			$route['disabled'] = true;
201
		else
202
			unset($route['disabled']);
203

    
204
		if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
205
			$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
206
		else
207
			$toapplylist = array();
208
		$a_routes[$id] = $route;
209

    
210
		if (!empty($oroute)) {
211
			$delete_targets = array_diff($old_targets, $new_targets);
212
			if (count($delete_targets))
213
				foreach ($delete_targets as $dts) {
214
					if(is_ipaddrv6($dts))
215
						$family = "-inet6";
216
					$toapplylist[] = "/sbin/route delete {$family} {$dts}";
217
				}
218
		}
219
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
220
		staticroutes_sort();
221

    
222
		mark_subsystem_dirty('staticroutes');
223

    
224
		write_config();
225

    
226
		header("Location: system_routes.php");
227
		exit;
228
	}
229
}
230

    
231
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
232
$shortcut_section = "routing";
233
include("head.inc");
234
?>
235

    
236
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
237
<script type="text/javascript" src="/javascript/jquery.ipv4v6ify.js"></script>
238
<script type="text/javascript" src="/javascript/autosuggest.js"></script>
239
<script type="text/javascript" src="/javascript/suggestions.js"></script>
240
<?php include("fbegin.inc");?>
241
<?php if ($input_errors) print_input_errors($input_errors); ?>
242
	<form action="system_routes_edit.php" method="post" name="iform" id="iform">
243
		<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="system routes edit">
244
			<tr>
245
				<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit route entry"); ?></td>
246
			</tr>
247
			<tr>
248
				<td width="22%" valign="top" class="vncellreq"><?=gettext("Destination network"); ?></td>
249
				<td width="78%" class="vtable">
250
					<input name="network" type="text" class="formfldalias ipv4v6" id="network" size="20" value="<?=htmlspecialchars($pconfig['network']);?>" />
251
					/
252
					<select name="network_subnet" class="formselect ipv4v6" id="network_subnet">
253
					<?php for ($i = 129; $i >= 1; $i--): ?>
254
						<option value="<?=$i;?>" <?php if ($i == $pconfig['network_subnet']) echo "selected=\"selected\""; ?>>
255
							<?=$i;?>
256
						</option>
257
					<?php endfor; ?>
258
					</select>
259
					<br/><span class="vexpl"><?=gettext("Destination network for this static route"); ?></span>
260
				</td>
261
			</tr>
262
			<tr>
263
				<td width="22%" valign="top" class="vncellreq"><?=gettext("Gateway"); ?></td>
264
				<td width="78%" class="vtable">
265
					<select name="gateway" id="gateway" class="formselect">
266
					<?php
267
						foreach ($a_gateways as $gateway) {
268
							echo "<option value='{$gateway['name']}' ";
269
							if ($gateway['name'] == $pconfig['gateway'])
270
								echo "selected=\"selected\"";
271
							echo ">" . htmlspecialchars($gateway['name']) . " - " . htmlspecialchars($gateway['gateway']) . "</option>\n";
272
						}
273
					?>
274
					</select> <br />
275
					<div id='addgwbox'>
276
						<?=gettext("Choose which gateway this route applies to or"); ?> <a onclick="show_add_gateway();" href="#"><?=gettext("add a new one.");?></a>
277
					</div>
278
					<div id='notebox'>
279
					</div>
280
					<div style="display:none" id="status">
281
					</div>
282
					<div style="display:none" id="addgateway">
283
						<table border="1" style="background:#990000; border-style: none none none none; width:225px;" summary="add gateway">
284
							<tr>
285
								<td>
286
									<table bgcolor="#990000" cellpadding="1" cellspacing="1" summary="add">
287
										<tr><td>&nbsp;</td></tr>
288
										<tr>
289
											<td colspan="2" align="center"><b><font color="white"><?=gettext("Add new gateway:"); ?></font></b></td>
290
										</tr>
291
										<tr><td>&nbsp;</td></tr>
292
										<tr>
293
											<td width="45%" align="right"><font color="white"><?=gettext("Default gateway:"); ?></font></td><td><input type="checkbox" id="defaultgw" name="defaultgw" /></td>
294
										</tr>
295
										<tr>
296
											<td width="45%" align="right"><font color="white"><?=gettext("Interface:"); ?></font></td>
297
											<td>
298
												<select name="addinterfacegw" id="addinterfacegw">
299
												<?php $gwifs = get_configured_interface_with_descr();
300
													foreach($gwifs as $fif => $dif)
301
														echo "<option value=\"{$fif}\">{$dif}</option>\n";
302
												?>
303
												</select>
304
											</td>
305
										</tr>
306
										<tr>
307
											<td align="right"><font color="white"><?=gettext("Gateway Name:"); ?></font></td><td><input id="name" name="name" value="GW" /></td>
308
										</tr>
309
										<tr>
310
											<td align="right"><font color="white"><?=gettext("Gateway IP:"); ?></font></td><td><input id="gatewayip" name="gatewayip" /></td>
311
										</tr>
312
										<tr>
313
											<td align="right"><font color="white"><?=gettext("Description:"); ?></font></td><td><input id="gatewaydescr" name="gatewaydescr" /></td>
314
										</tr>
315
										<tr><td>&nbsp;</td></tr>
316
										<tr>
317
											<td colspan="2" align="center">
318
												<div id='savebuttondiv'>
319
													<input type="hidden" name="addrtype" id="addrtype" value="IPv4" />
320
													<input id="gwsave" type="button" value="<?=gettext("Save Gateway"); ?>" onclick='hide_add_gatewaysave();' />
321
													<input id="gwcancel" type="button" value="<?=gettext("Cancel"); ?>" onclick='hide_add_gateway();' />
322
												</div>
323
											</td>
324
										</tr>
325
										<tr><td>&nbsp;</td></tr>
326
									</table>
327
								</td>
328
							</tr>
329
						</table>
330
					</div>
331
				</td>
332
			</tr>
333
			<tr>
334
				<td width="22%" valign="top" class="vncell"><?=gettext("Disabled");?></td>
335
				<td width="78%" class="vtable">
336
					<input name="disabled" type="checkbox" id="disabled" value="yes" <?php if ($pconfig['disabled']) echo "checked=\"checked\""; ?> />
337
					<strong><?=gettext("Disable this static route");?></strong><br />
338
					<span class="vexpl"><?=gettext("Set this option to disable this static route without removing it from the list.");?></span>
339
				</td>
340
			</tr>
341
			<tr>
342
				<td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
343
				<td width="78%" class="vtable">
344
					<input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>" />
345
					<br/><span class="vexpl"><?=gettext("You may enter a description here for your reference (not parsed)."); ?></span>
346
				</td>
347
			</tr>
348
			<tr>
349
				<td width="22%" valign="top">&nbsp;</td>
350
				<td width="78%">
351
					<input id="save" name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>" /> <input id="cancel" type="button" value="<?=gettext("Cancel"); ?>" class="formbtn"  onclick="history.back()" />
352
					<?php if (isset($id) && $a_routes[$id]): ?>
353
						<input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
354
					<?php endif; ?>
355
				</td>
356
			</tr>
357
		</table>
358
	</form>
359
<script type="text/javascript">
360
//<![CDATA[
361
	var gatewayip;
362
	var name;
363
	function show_add_gateway() {
364
		document.getElementById("addgateway").style.display = '';
365
		document.getElementById("addgwbox").style.display = 'none';
366
		document.getElementById("gateway").style.display = 'none';
367
		document.getElementById("save").style.display = 'none';
368
		document.getElementById("cancel").style.display = 'none';
369
		document.getElementById("gwsave").style.display = '';
370
		document.getElementById("gwcancel").style.display = '';
371
		jQuery('#notebox').html("");
372
	}
373
	function hide_add_gateway() {
374
		document.getElementById("addgateway").style.display = 'none';
375
		document.getElementById("addgwbox").style.display = '';
376
		document.getElementById("gateway").style.display = '';
377
		document.getElementById("save").style.display = '';
378
		document.getElementById("cancel").style.display = '';
379
		document.getElementById("gwsave").style.display = '';
380
		document.getElementById("gwcancel").style.display = '';
381
	}
382
	function hide_add_gatewaysave() {
383
		document.getElementById("addgateway").style.display = 'none';
384
		jQuery('#status').html('<img src="/themes/metallic/images/misc/loader.gif"> One moment please...');
385
		var iface = jQuery('#addinterfacegw').val();
386
		name = jQuery('#name').val();
387
		var descr = jQuery('#gatewaydescr').val();
388
		gatewayip = jQuery('#gatewayip').val();
389
		addrtype = jQuery('#addrtype').val();
390
		var defaultgw = '';
391
		if (jQuery('#defaultgw').checked)
392
			defaultgw = 'yes';
393
		var url = "system_gateways_edit.php";
394
		var pars = 'isAjax=true&defaultgw=' + escape(defaultgw) + '&interface=' + escape(iface) + '&name=' + escape(name) + '&descr=' + escape(descr) + '&gateway=' + escape(gatewayip) + '&type=' + escape(addrtype);
395
		jQuery.ajax(
396
			url,
397
		{
398
			type: 'post',
399
				data: pars,
400
				error: report_failure,
401
				complete: save_callback
402
		});
403
	}
404
	function addOption(selectbox,text,value)
405
	{
406
		var optn = document.createElement("OPTION");
407
		optn.text = text;
408
		optn.value = value;
409
		selectbox.append(optn);
410
		selectbox.prop('selectedIndex',selectbox.children('option').length-1);
411
		jQuery('#notebox').html("<p><strong><?=gettext("NOTE:");?><\/strong> <?php printf(gettext("You can manage Gateways %shere%s."), "<a target='_blank' href='system_gateways.php'>", "<\/a>");?> <\/strong><\/p>");
412
	}
413
	function report_failure() {
414
		alert("<?=gettext("Sorry, we could not create your gateway at this time."); ?>");
415
		hide_add_gateway();
416
	}
417
	function save_callback(transport) {
418
		var response = transport.responseText;
419
		if (response) {
420
			document.getElementById("addgateway").style.display = 'none';
421
			hide_add_gateway();
422
			jQuery('#status').html('');
423
			addOption(jQuery('#gateway'), name, name);
424
		} else {
425
			report_failure();
426
		}
427
	}
428
	var addressarray = <?= json_encode(get_alias_list(array("host", "network"))) ?>;
429
	var oTextbox1 = new AutoSuggestControl(document.getElementById("network"), new StateSuggestions(addressarray));
430
//]]>
431
</script>
432
<?php include("fend.inc"); ?>
433
</body>
434
</html>
(219-219/246)