Project

General

Profile

Download (16.6 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("guiconfig.inc");
56

    
57
if (!is_array($config['staticroutes']['route']))
58
	$config['staticroutes']['route'] = array();
59

    
60
$a_routes = &$config['staticroutes']['route'];
61
$a_gateways = return_gateways_array(true, true);
62

    
63
$id = $_GET['id'];
64
if (isset($_POST['id']))
65
	$id = $_POST['id'];
66

    
67
if (isset($_GET['dup'])) {
68
	$id = $_GET['dup'];
69
}
70

    
71
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
	$pconfig['disabled'] = isset($a_routes[$id]['disabled']);
77
}
78

    
79
if (isset($_GET['dup']))
80
	unset($id);
81

    
82
if ($_POST) {
83

    
84
	unset($input_errors);
85
	$pconfig = $_POST;
86

    
87
	/* input validation */
88
	$reqdfields = explode(" ", "network network_subnet gateway");
89
	$reqdfieldsn = explode(",",
90
			gettext("Destination network") . "," .
91
			gettext("Destination network bit count") . "," .
92
			gettext("Gateway"));		
93
	
94
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
95
	
96
	if (($_POST['network'] && !is_ipaddr($_POST['network']) && !is_alias($_POST['network']))) {
97
		$input_errors[] = gettext("A valid IPv4 or IPv6 destination network must be specified.");
98
	}
99
	if (($_POST['network_subnet'] && !is_numeric($_POST['network_subnet']))) {
100
		$input_errors[] = gettext("A valid destination network bit count must be specified.");
101
	}
102
	if (($_POST['gateway']) && is_ipaddr($_POST['network'])) {
103
		if (!isset($a_gateways[$_POST['gateway']]))
104
			$input_errors[] = gettext("A valid gateway must be specified.");
105
		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
	}
108

    
109
	/* check for overlaps */
110
	$current_targets = get_staticroutes(true);
111
	$new_targets = array();
112
	if(is_ipaddrv6($_POST['network'])) {
113
		$osn = gen_subnetv6($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
114
		$new_targets[] = $osn;
115
	}
116
	if (is_ipaddrv4($_POST['network'])) {
117
		if($_POST['network_subnet'] > 32)
118
			$input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
119
		else {
120
			$osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
121
			$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
			if (is_ipaddr($tgt))
129
				$tgt .= "/128";
130
			if (!is_subnet($tgt))
131
				continue;
132
			if (!is_subnetv6($tgt))
133
				continue;
134
			$new_targets[] = $tgt;
135
		}
136
	}
137
	if (!isset($id))
138
		$id = count($a_routes);
139
	$oroute = $a_routes[$id];
140
	$old_targets = array();
141
	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
		}
153
	}
154

    
155
	$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
	if (is_array($config['interfaces'])) {
162
		foreach ($config['interfaces'] as $if) {
163
			if (is_ipaddrv4($_POST['network'])
164
				&& isset($if['ipaddr']) && isset($if['subnet'])
165
				&& is_ipaddrv4($if['ipaddr']) && is_numeric($if['subnet'])
166
				&& ($_POST['network_subnet'] == $if['subnet'])
167
				&& (gen_subnet($_POST['network'], $_POST['network_subnet']) == gen_subnet($if['ipaddr'], $if['subnet'])))
168
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
169

    
170
			else if (is_ipaddrv6($_POST['network'])
171
				&& isset($if['ipaddrv6']) && isset($if['subnetv6'])
172
				&& is_ipaddrv6($if['ipaddrv6']) && is_numeric($if['subnetv6'])
173
				&& ($_POST['network_subnet'] == $if['subnetv6'])
174
				&& (gen_subnetv6($_POST['network'], $_POST['network_subnet']) == gen_subnetv6($if['ipaddrv6'], $if['subnetv6'])))
175
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
176
		}
177
	}
178

    
179
	if (!$input_errors) {
180
		$route = array();
181
		$route['network'] = $osn;
182
		$route['gateway'] = $_POST['gateway'];
183
		$route['descr'] = $_POST['descr'];
184
		if ($_POST['disabled'])
185
			$route['disabled'] = true;
186
		else
187
			unset($route['disabled']);
188

    
189
		if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
190
			$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
191
		else
192
			$toapplylist = array();
193
		$a_routes[$id] = $route;
194

    
195
		if (!empty($oroute)) {
196
			$delete_targets = array_diff($old_targets, $new_targets);
197
			if (count($delete_targets))
198
				foreach ($delete_targets as $dts) {
199
					if(is_ipaddrv6($dts))
200
						$family = "-inet6";
201
					$toapplylist[] = "/sbin/route delete {$family} {$dts}"; 
202
				}
203
		}
204
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
205
		staticroutes_sort();
206
		
207
		mark_subsystem_dirty('staticroutes');
208
		
209
		write_config();
210
		
211
		header("Location: system_routes.php");
212
		exit;
213
	}
214
}
215

    
216
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
217
$shortcut_section = "routing";
218
include("head.inc");
219
?>
220

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