Project

General

Profile

Download (15.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 (!$input_errors) {
162
		$route = array();
163
		$route['network'] = $osn;
164
		$route['gateway'] = $_POST['gateway'];
165
		$route['descr'] = $_POST['descr'];
166
		if ($_POST['disabled'])
167
			$route['disabled'] = true;
168
		else
169
			unset($route['disabled']);
170

    
171
		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
		$a_routes[$id] = $route;
176

    
177
		if (!empty($oroute)) {
178
			$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
		}
186
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
187
		staticroutes_sort();
188
		
189
		mark_subsystem_dirty('staticroutes');
190
		
191
		write_config();
192
		
193
		header("Location: system_routes.php");
194
		exit;
195
	}
196
}
197

    
198
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
199
$shortcut_section = "routing";
200
include("head.inc");
201
?>
202

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

    
395
				</script>
396
<?php include("fend.inc"); ?>
397
</body>
398
</html>
(222-222/249)