Project

General

Profile

Download (16 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
		foreach (preg_split('/\s+/', $aliastable[$osn]) as $tgt) {
131
			if (is_ipaddrv4($tgt))
132
				$tgt .= "/32";
133
			if (is_ipaddrv6($tgt))
134
				$tgt .= "/128";
135
			if (!is_subnet($tgt))
136
				continue;
137
			if (!is_subnetv6($tgt))
138
				continue;
139
			$new_targets[] = $tgt;
140
		}
141
	}
142
	if (!isset($id))
143
		$id = count($a_routes);
144
	$oroute = $a_routes[$id];
145
	$old_targets = array();
146
	if (!empty($oroute)) {
147
		if (is_alias($oroute['network'])) {
148
			foreach (filter_expand_alias_array($oroute['network']) as $tgt) {
149
				if (is_ipaddrv4($tgt))
150
					$tgt .= "/32";
151
				else if (is_ipaddrv6($tgt))
152
					$tgt .= "/128";
153
				if (!is_subnet($tgt))
154
					continue;
155
				$old_targets[] = $tgt;
156
			}
157
		} else {
158
			$old_targets[] = $oroute['network'];
159
		}
160
	}
161

    
162
	$overlaps = array_intersect($current_targets, $new_targets);
163
	$overlaps = array_diff($overlaps, $old_targets);
164
	if (count($overlaps)) {
165
		$input_errors[] = gettext("A route to these destination networks already exists") . ": " . implode(", ", $overlaps);
166
	}
167

    
168
	if (is_array($config['interfaces'])) {
169
		foreach ($config['interfaces'] as $if) {
170
			if (is_ipaddrv4($_POST['network'])
171
				&& isset($if['ipaddr']) && isset($if['subnet'])
172
				&& is_ipaddrv4($if['ipaddr']) && is_numeric($if['subnet'])
173
				&& ($_POST['network_subnet'] == $if['subnet'])
174
				&& (gen_subnet($_POST['network'], $_POST['network_subnet']) == gen_subnet($if['ipaddr'], $if['subnet'])))
175
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
176

    
177
			else if (is_ipaddrv6($_POST['network'])
178
				&& isset($if['ipaddrv6']) && isset($if['subnetv6'])
179
				&& is_ipaddrv6($if['ipaddrv6']) && is_numeric($if['subnetv6'])
180
				&& ($_POST['network_subnet'] == $if['subnetv6'])
181
				&& (gen_subnetv6($_POST['network'], $_POST['network_subnet']) == gen_subnetv6($if['ipaddrv6'], $if['subnetv6'])))
182
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
183
		}
184
	}
185

    
186
	if (!$input_errors) {
187
		$route = array();
188
		$route['network'] = $osn;
189
		$route['gateway'] = $_POST['gateway'];
190
		$route['descr'] = $_POST['descr'];
191
		if ($_POST['disabled'])
192
			$route['disabled'] = true;
193
		else
194
			unset($route['disabled']);
195

    
196
		if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
197
			$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
198
		else
199
			$toapplylist = array();
200
		$a_routes[$id] = $route;
201

    
202
		if (!empty($oroute)) {
203
			$delete_targets = array_diff($old_targets, $new_targets);
204
			if (count($delete_targets))
205
				foreach ($delete_targets as $dts) {
206
					if(is_ipaddrv6($dts))
207
						$family = "-inet6";
208
					$toapplylist[] = "/sbin/route delete {$family} {$dts}";
209
				}
210
		}
211
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
212
		staticroutes_sort();
213

    
214
		mark_subsystem_dirty('staticroutes');
215

    
216
		write_config();
217

    
218
		header("Location: system_routes.php");
219
		exit;
220
	}
221
}
222

    
223
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
224
$shortcut_section = "routing";
225
include("head.inc");
226
?>
227

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