Project

General

Profile

Download (15.5 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?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 38936dc7 Ermal Lu?i
	Copyright (C) 2010 Scott Ullrich
8 5b237745 Scott Ullrich
	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 1d333258 Scott Ullrich
/*
32
	pfSense_MODULE:	routing
33
*/
34 5b237745 Scott Ullrich
35 6b07c15a Matthew Grooms
##|+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 4504a769 Ermal Lu?i
function staticroutecmp($a, $b) {
43
	return strcmp($a['network'], $b['network']);
44
}
45
46 0d64af59 Ermal Lu?i
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 6b07c15a Matthew Grooms
55 5b237745 Scott Ullrich
require("guiconfig.inc");
56
57
if (!is_array($config['staticroutes']['route']))
58
	$config['staticroutes']['route'] = array();
59
60
$a_routes = &$config['staticroutes']['route'];
61 6fdea6a2 smos
$a_gateways = return_gateways_array(true, true);
62 5b237745 Scott Ullrich
63
$id = $_GET['id'];
64
if (isset($_POST['id']))
65
	$id = $_POST['id'];
66
67 18f7352b Seth Mos
if (isset($_GET['dup'])) {
68
	$id = $_GET['dup'];
69
}
70
71 5b237745 Scott Ullrich
if (isset($id) && $a_routes[$id]) {
72
	list($pconfig['network'],$pconfig['network_subnet']) = 
73
		explode('/', $a_routes[$id]['network']);
74
	$pconfig['gateway'] = $a_routes[$id]['gateway'];
75
	$pconfig['descr'] = $a_routes[$id]['descr'];
76 bfe407e5 Warren Baker
	$pconfig['disabled'] = isset($a_routes[$id]['disabled']);
77 5b237745 Scott Ullrich
}
78
79 18f7352b Seth Mos
if (isset($_GET['dup']))
80
	unset($id);
81
82 5b237745 Scott Ullrich
if ($_POST) {
83
84
	unset($input_errors);
85
	$pconfig = $_POST;
86
87
	/* input validation */
88 dde169d9 Vinicius Coque
	$reqdfields = explode(" ", "network network_subnet gateway");
89 38fb1109 Vinicius Coque
	$reqdfieldsn = explode(",",
90
			gettext("Destination network") . "," .
91
			gettext("Destination network bit count") . "," .
92
			gettext("Gateway"));		
93 5b237745 Scott Ullrich
	
94
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
95
	
96 f898c1a9 jim-p
	if (($_POST['network'] && !is_ipaddr($_POST['network']) && !is_alias($_POST['network']))) {
97 ad700f39 Seth Mos
		$input_errors[] = gettext("A valid IPv4 or IPv6 destination network must be specified.");
98 5b237745 Scott Ullrich
	}
99
	if (($_POST['network_subnet'] && !is_numeric($_POST['network_subnet']))) {
100 169e0008 Carlos Eduardo Ramos
		$input_errors[] = gettext("A valid destination network bit count must be specified.");
101 5b237745 Scott Ullrich
	}
102 ad700f39 Seth Mos
	if (($_POST['gateway']) && is_ipaddr($_POST['network'])) {
103 a529aced Ermal
		if (!isset($a_gateways[$_POST['gateway']]))
104 169e0008 Carlos Eduardo Ramos
			$input_errors[] = gettext("A valid gateway must be specified.");
105 1831a00d Seth Mos
		if(!validate_address_family($_POST['network'], lookup_gateway_ip_by_name($_POST['gateway'])))
106
			$input_errors[] = gettext("The gateway '{$a_gateways[$_POST['gateway']]['gateway']}' is a different Address Family as network '{$_POST['network']}'.");
107 5b237745 Scott Ullrich
	}
108
109
	/* check for overlaps */
110 f898c1a9 jim-p
	$current_targets = get_staticroutes(true);
111
	$new_targets = array();
112 14f565b4 Seth Mos
	if(is_ipaddrv6($_POST['network'])) {
113
		$osn = Net_IPv6::compress(gen_subnetv6($_POST['network'], $_POST['network_subnet'])) . "/" . $_POST['network_subnet'];
114 f898c1a9 jim-p
		$new_targets[] = $osn;
115 14f565b4 Seth Mos
	}
116 71f4a2b7 smos
	if (is_ipaddrv4($_POST['network'])) {
117 1831a00d Seth Mos
		if($_POST['network_subnet'] > 32)
118
			$input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
119 f898c1a9 jim-p
		else {
120 1831a00d Seth Mos
			$osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
121 f898c1a9 jim-p
			$new_targets[] = $osn;
122
		}
123
	} elseif (is_alias($_POST['network'])) {
124
		$osn = $_POST['network'];
125
		foreach (filter_expand_alias_array($_POST['network']) as $tgt) {
126
			if (is_ipaddr($tgt))
127
				$tgt .= "/32";
128 71f4a2b7 smos
			if (is_ipaddr($tgt))
129
				$tgt .= "/128";
130 f898c1a9 jim-p
			if (!is_subnet($tgt))
131
				continue;
132 06392e40 jim-p
			if (!is_subnetv6($tgt))
133 71f4a2b7 smos
				continue;
134 f898c1a9 jim-p
			$new_targets[] = $tgt;
135
		}
136 14f565b4 Seth Mos
	}
137 f898c1a9 jim-p
	if (!isset($id))
138
		$id = count($a_routes);
139
	$oroute = $a_routes[$id];
140 71f4a2b7 smos
	$old_targets = array();
141 f898c1a9 jim-p
	if (!empty($oroute)) {
142
		if (is_alias($oroute['network'])) {
143
			foreach (filter_expand_alias_array($oroute['network']) as $tgt) {
144
				if (is_ipaddr($tgt))
145
					$tgt .= "/32";
146
				if (!is_subnet($tgt))
147
					continue;
148
				$old_targets[] = $tgt;
149
			}
150
		} else {
151
			$old_targets[] = $oroute['network'];
152 5b237745 Scott Ullrich
		}
153
	}
154
155 f898c1a9 jim-p
	$overlaps = array_intersect($current_targets, $new_targets);
156
	$overlaps = array_diff($overlaps, $old_targets);
157
	if (count($overlaps)) {
158
		$input_errors[] = gettext("A route to these destination networks already exists") . ": " . implode(", ", $overlaps);
159
	}
160
161 5b237745 Scott Ullrich
	if (!$input_errors) {
162
		$route = array();
163
		$route['network'] = $osn;
164
		$route['gateway'] = $_POST['gateway'];
165
		$route['descr'] = $_POST['descr'];
166 bfe407e5 Warren Baker
		if ($_POST['disabled'])
167
			$route['disabled'] = true;
168
		else
169
			unset($route['disabled']);
170 5b237745 Scott Ullrich
171 f898c1a9 jim-p
		if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
172
			$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
173
		else
174
			$toapplylist = array();
175 e8471084 Ermal
		$a_routes[$id] = $route;
176
177
		if (!empty($oroute)) {
178 f898c1a9 jim-p
			$delete_targets = array_diff($old_targets, $new_targets);
179
			if (count($delete_targets))
180
				foreach ($delete_targets as $dts) {
181
					if(is_ipaddrv6($dts))
182
						$family = "-inet6";
183
					$toapplylist[] = "/sbin/route delete {$family} {$dts}"; 
184
				}
185 e8471084 Ermal
		}
186
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
187 0e3aa71c Erik Fonnesbeck
		staticroutes_sort();
188 5b237745 Scott Ullrich
		
189 a368a026 Ermal Lu?i
		mark_subsystem_dirty('staticroutes');
190 5b237745 Scott Ullrich
		
191
		write_config();
192
		
193
		header("Location: system_routes.php");
194
		exit;
195
	}
196
}
197 4df96eff Scott Ullrich
198 169e0008 Carlos Eduardo Ramos
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
199 4df96eff Scott Ullrich
include("head.inc");
200 5b237745 Scott Ullrich
?>
201 4df96eff Scott Ullrich
202 5b237745 Scott Ullrich
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
203 87744d53 Darren Embry
<script type="text/javascript" src="/javascript/jquery.ipv4v6ify.js"></script>
204 f898c1a9 jim-p
<script type="text/javascript" src="/javascript/autosuggest.js">
205
</script>
206
<script type="text/javascript" src="/javascript/suggestions.js">
207
</script>
208
<?php include("fbegin.inc");?>
209 5b237745 Scott Ullrich
<?php if ($input_errors) print_input_errors($input_errors); ?>
210
            <form action="system_routes_edit.php" method="post" name="iform" id="iform">
211
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
212 0cece4a2 Scott Ullrich
				<tr>
213 169e0008 Carlos Eduardo Ramos
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit route entry"); ?></td>
214 0cece4a2 Scott Ullrich
				</tr>	
215 5b237745 Scott Ullrich
                <tr>
216 169e0008 Carlos Eduardo Ramos
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Destination network"); ?></td>
217 5b237745 Scott Ullrich
                  <td width="78%" class="vtable"> 
218 f898c1a9 jim-p
                    <input name="network" type="text" class="formfldalias ipv4v6" id="network" size="20" value="<?=htmlspecialchars($pconfig['network']);?>"> 
219 5b237745 Scott Ullrich
				  / 
220 87744d53 Darren Embry
                    <select name="network_subnet" class="formselect ipv4v6" id="network_subnet"
221 bb5a2d0e Seth Mos
                      <?php
222 3d36f9d1 smos
			for ($i = 129; $i >= 1; $i--): ?>
223 5b237745 Scott Ullrich
                      <option value="<?=$i;?>" <?php if ($i == $pconfig['network_subnet']) echo "selected"; ?>>
224
                      <?=$i;?>
225
                      </option>
226
                      <?php endfor; ?>
227
                    </select>
228 169e0008 Carlos Eduardo Ramos
                    <br> <span class="vexpl"><?=gettext("Destination network for this static route"); ?></span></td>
229 5b237745 Scott Ullrich
                </tr>
230 300e2c0b Vinicius Coque
                <tr>
231 169e0008 Carlos Eduardo Ramos
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Gateway"); ?></td>
232 d173230c Seth Mos
                  <td width="78%" class="vtable">
233 38936dc7 Ermal Lu?i
			<select name="gateway" id="gateway" class="formselect">
234 d173230c Seth Mos
			<?php
235 a529aced Ermal
				foreach ($a_gateways as $gateway) {
236 a63d867a Ermal
	                      		echo "<option value='{$gateway['name']}' ";
237
					if ($gateway['name'] == $pconfig['gateway'])
238
						echo "selected";
239 189ceb32 Chris Buechler
	                      		echo ">" . htmlspecialchars($gateway['name']) . " - " . htmlspecialchars($gateway['gateway']) . "</option>\n";
240 a529aced Ermal
				}
241
			?>
242 38936dc7 Ermal Lu?i
                    </select> <br />
243
			<div id='addgwbox'>
244 ea53e38f Renato Botelho
				<?=gettext("Choose which gateway this route applies to or"); ?> <a OnClick="show_add_gateway();" href="#"><?=gettext("add a new one.");?></a>
245 38936dc7 Ermal Lu?i
								</div>
246
								<div id='notebox'>
247
								</div>
248
								<div style="display:none" name ="status" id="status">
249
								</div>								
250
								<div style="display:none" id="addgateway" name="addgateway">
251
									<p> 
252
									<table border="1" style="background:#990000; border-style: none none none none; width:225px;"><tr><td>
253
										<table bgcolor="#990000" cellpadding="1" cellspacing="1">
254
											<tr><td>&nbsp;</td>
255
											<tr>
256 ea53e38f Renato Botelho
												<td colspan="2"><center><b><font color="white"><?=gettext("Add new gateway:"); ?></b></center></td>
257 38936dc7 Ermal Lu?i
											</tr>
258
											<tr><td>&nbsp;</td>
259
											<tr>
260 a980df9c Ermal
												<td width="45%" align="right"><font color="white"><?=gettext("Default gateway:"); ?></td><td><input type="checkbox" id="defaultgw" name="defaultgw"></td>
261 38936dc7 Ermal Lu?i
											</tr>												
262
											<tr>
263 ea53e38f Renato Botelho
												<td width="45%" align="right"><font color="white"><?=gettext("Interface:"); ?></td>
264 38936dc7 Ermal Lu?i
												<td><select name="addinterfacegw" id="addinterfacegw">
265
												<?php $gwifs = get_configured_interface_with_descr();
266
													foreach($gwifs as $fif => $dif)
267
														echo "<option value=\"{$fif}\">{$dif}</option>\n";
268
												?>
269
												</select></td>
270
											</tr>
271
											<tr>
272 ea53e38f Renato Botelho
												<td align="right"><font color="white"><?=gettext("Gateway Name:"); ?></td><td><input id="name" name="name" value="GW"></td>
273 38936dc7 Ermal Lu?i
											</tr>
274
											<tr>
275 ea53e38f Renato Botelho
												<td align="right"><font color="white"><?=gettext("Gateway IP:"); ?></td><td><input id="gatewayip" name="gatewayip"></td>
276 38936dc7 Ermal Lu?i
											</tr>
277
											<tr>
278 ea53e38f Renato Botelho
												<td align="right"><font color="white"><?=gettext("Description:"); ?></td><td><input id="gatewaydescr" name="gatewaydescr"></td>
279 38936dc7 Ermal Lu?i
											</tr>
280
											<tr><td>&nbsp;</td>
281
											<tr>
282
												<td colspan="2">
283
													<center>
284
														<div id='savebuttondiv'>
285
															<input type="hidden" name="addrtype" id="addrtype" value="IPv4" />
286 169e0008 Carlos Eduardo Ramos
															<input id="gwsave" type="Button" value="<?=gettext("Save Gateway"); ?>" onClick='hide_add_gatewaysave();'> 
287
															<input id="gwcancel" type="Button" value="<?=gettext("Cancel"); ?>" onClick='hide_add_gateway();'>
288 38936dc7 Ermal Lu?i
														</div>
289
													</center>
290
												</td>
291
											</tr>
292
											<tr><td>&nbsp;</td>
293
										</table>
294
										</td></tr></table>
295
									<p/>
296
								</div>
297 5b237745 Scott Ullrich
                </tr>
298 bfe407e5 Warren Baker
		<tr>
299
			<td width="22%" valign="top" class="vncell"><?=gettext("Disabled");?></td>
300
			<td width="78%" class="vtable">
301
				<input name="disabled" type="checkbox" id="disabled" value="yes" <?php if ($pconfig['disabled']) echo "checked"; ?>>
302
				<strong><?=gettext("Disable this static route");?></strong><br />
303
				<span class="vexpl"><?=gettext("Set this option to disable this static route without removing it from the list.");?></span>
304
			</td>
305
		</tr>
306 d173230c Seth Mos
		<tr>
307 169e0008 Carlos Eduardo Ramos
                  <td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
308 5b237745 Scott Ullrich
                  <td width="78%" class="vtable"> 
309 b5c78501 Seth Mos
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
310 ea53e38f Renato Botelho
                    <br> <span class="vexpl"><?=gettext("You may enter a description here for your reference (not parsed)."); ?></span></td>
311 5b237745 Scott Ullrich
                </tr>
312
                <tr>
313
                  <td width="22%" valign="top">&nbsp;</td>
314
                  <td width="78%"> 
315 bfb0b9dc Vinicius Coque
                    <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()">
316 5b237745 Scott Ullrich
                    <?php if (isset($id) && $a_routes[$id]): ?>
317 225a2f0b Scott Ullrich
                    <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>">
318 5b237745 Scott Ullrich
                    <?php endif; ?>
319
                  </td>
320
                </tr>
321
              </table>
322
</form>
323 38936dc7 Ermal Lu?i
<script type="text/javascript">
324
					var gatewayip;
325
					var name;
326
					function show_add_gateway() {
327
						document.getElementById("addgateway").style.display = '';
328
						document.getElementById("addgwbox").style.display = 'none';
329
						document.getElementById("gateway").style.display = 'none';
330
						document.getElementById("save").style.display = 'none';
331
						document.getElementById("cancel").style.display = 'none';
332
						document.getElementById("gwsave").style.display = '';
333
						document.getElementById("gwcancel").style.display = '';
334 300e2c0b Vinicius Coque
						jQuery('#notebox').html("");
335 38936dc7 Ermal Lu?i
					}
336
					function hide_add_gateway() {
337
						document.getElementById("addgateway").style.display = 'none';
338
						document.getElementById("addgwbox").style.display = '';	
339
						document.getElementById("gateway").style.display = '';
340
						document.getElementById("save").style.display = '';
341
						document.getElementById("cancel").style.display = '';
342
						document.getElementById("gwsave").style.display = '';
343
						document.getElementById("gwcancel").style.display = '';
344
					}
345
					function hide_add_gatewaysave() {
346
						document.getElementById("addgateway").style.display = 'none';
347 300e2c0b Vinicius Coque
						jQuery('#status').html('<img src="/themes/metallic/images/misc/loader.gif"> One moment please...');
348
						var iface = jQuery('#addinterfacegw').val();
349
						name = jQuery('#name').val();
350
						var descr = jQuery('#gatewaydescr').val();
351
						gatewayip = jQuery('#gatewayip').val();
352
						addrtype = jQuery('#addrtype').val();
353 a980df9c Ermal
						var defaultgw = '';
354 300e2c0b Vinicius Coque
						if (jQuery('#defaultgw').checked)
355 a980df9c Ermal
							defaultgw = 'yes';
356 38936dc7 Ermal Lu?i
						var url = "system_gateways_edit.php";
357
						var pars = 'isAjax=true&defaultgw=' + escape(defaultgw) + '&interface=' + escape(iface) + '&name=' + escape(name) + '&descr=' + escape(descr) + '&gateway=' + escape(gatewayip) + '&type=' + escape(addrtype);
358 300e2c0b Vinicius Coque
						jQuery.ajax(
359 38936dc7 Ermal Lu?i
							url,
360
							{
361 300e2c0b Vinicius Coque
								type: 'post',
362
								data: pars,
363
								error: report_failure,
364
								complete: save_callback
365
							});
366 38936dc7 Ermal Lu?i
					}
367
					function addOption(selectbox,text,value)
368
					{
369
						var optn = document.createElement("OPTION");
370
						optn.text = text;
371
						optn.value = value;
372 300e2c0b Vinicius Coque
						selectbox.append(optn);
373
						selectbox.prop('selectedIndex',selectbox.children('option').length-1);
374
						jQuery('#notebox').html("<p/><strong><?=gettext("NOTE:");?></strong> <?php printf(gettext("You can manage Gateways %shere%s."), "<a target='_new' href='system_gateways.php'>", "</a>");?> </strong>");
375 38936dc7 Ermal Lu?i
					}				
376
					function report_failure() {
377 ea53e38f Renato Botelho
						alert("<?=gettext("Sorry, we could not create your gateway at this time."); ?>");
378 38936dc7 Ermal Lu?i
						hide_add_gateway();
379
					}
380
					function save_callback(transport) {
381
						var response = transport.responseText;
382
						if (response) {
383
							document.getElementById("addgateway").style.display = 'none';
384
							hide_add_gateway();
385 300e2c0b Vinicius Coque
							jQuery('#status').html('');
386
							addOption(jQuery('#gateway'), name, name);
387 38936dc7 Ermal Lu?i
						} else {
388
							report_failure();
389
						}
390
					}
391 4dfd930e Darren Embry
					var addressarray = <?= json_encode(get_alias_list(array("host", "network"))) ?>;
392 f898c1a9 jim-p
					var oTextbox1 = new AutoSuggestControl(document.getElementById("network"), new StateSuggestions(addressarray));
393
394 38936dc7 Ermal Lu?i
				</script>
395 5b237745 Scott Ullrich
<?php include("fend.inc"); ?>
396
</body>
397
</html>