Project

General

Profile

Download (13.9 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);
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
}
77

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

    
81
if ($_POST) {
82

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

    
86
	/* input validation */
87
	$reqdfields = explode(" ", "network network_subnet gateway");
88
	$reqdfieldsn = explode(",",
89
			gettext("Destination network") . "," .
90
			gettext("Destination network bit count") . "," .
91
			gettext("Gateway"));		
92
	
93
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
94
	
95
	if (($_POST['network'] && !is_ipaddr($_POST['network']))) {
96
		$input_errors[] = gettext("A valid IPv4 or IPv6 destination network must be specified.");
97
	}
98
	if (($_POST['network_subnet'] && !is_numeric($_POST['network_subnet']))) {
99
		$input_errors[] = gettext("A valid destination network bit count must be specified.");
100
	}
101
	if (($_POST['gateway']) && is_ipaddr($_POST['network'])) {
102
		if (!isset($a_gateways[$_POST['gateway']]))
103
			$input_errors[] = gettext("A valid gateway must be specified.");
104
		if(!validate_address_family($_POST['network'], lookup_gateway_ip_by_name($_POST['gateway'])))
105
			$input_errors[] = gettext("The gateway '{$a_gateways[$_POST['gateway']]['gateway']}' is a different Address Family as network '{$_POST['network']}'.");
106
	}
107

    
108
	/* check for overlaps */
109
	if(is_ipaddrv6($_POST['network'])) {
110
		$osn = Net_IPv6::compress(gen_subnetv6($_POST['network'], $_POST['network_subnet'])) . "/" . $_POST['network_subnet'];
111
	}
112
	if(is_ipaddrv4($_POST['network'])) {
113
		if($_POST['network_subnet'] > 32)
114
			$input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
115
		else
116
			$osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
117
	}
118
	foreach ($a_routes as $route) {
119
		if (isset($id) && ($a_routes[$id]) && ($a_routes[$id] === $route))
120
			continue;
121

    
122
		if ($route['network'] == $osn) {
123
			$input_errors[] = gettext("A route to this destination network already exists.");
124
			break;
125
		}
126
	}
127

    
128
	if (!$input_errors) {
129
		$route = array();
130
		$route['network'] = $osn;
131
		$route['gateway'] = $_POST['gateway'];
132
		$route['descr'] = $_POST['descr'];
133

    
134
		if (!isset($id))
135
                        $id = count($a_routes);
136
                if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
137
                        $toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
138
                else
139
                        $toapplylist = array();
140
                $oroute = $a_routes[$id];
141

    
142
		$a_routes[$id] = $route;
143

    
144
		if (!empty($oroute)) {
145
			$osn = explode('/', $oroute['network']);
146
			$sn = explode('/', $route['network']);
147
			if ($oroute['network'] <> $route['network']) {
148
				if(is_ipaddrv6($oroute['network']))
149
					$family = "-inet6";
150
				$toapplylist[] = "/sbin/route delete {$family} {$oroute['network']}";
151
			}
152
		}
153
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
154
		staticroutes_sort();
155
		
156
		mark_subsystem_dirty('staticroutes');
157
		
158
		write_config();
159
		
160
		header("Location: system_routes.php");
161
		exit;
162
	}
163
}
164

    
165
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
166
include("head.inc");
167

    
168
?>
169

    
170
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
171
<?php include("fbegin.inc"); ?>
172
<?php if ($input_errors) print_input_errors($input_errors); ?>
173
            <form action="system_routes_edit.php" method="post" name="iform" id="iform">
174
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
175
				<tr>
176
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit route entry"); ?></td>
177
				</tr>	
178
                <tr>
179
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Destination network"); ?></td>
180
                  <td width="78%" class="vtable"> 
181
                    <input name="network" type="text" class="formfld unknown" id="network" size="20" value="<?=htmlspecialchars($pconfig['network']);?>"> 
182
				  / 
183
                    <select name="network_subnet" class="formselect" id="network_subnet"
184
                      <?php
185
			if(is_ipaddrv4($pconfig['network'])) {
186
				$size = 32;
187
			} else {
188
				$size = 128;
189
			}
190
			for ($i = $size; $i >= 1; $i--): ?>
191
                      <option value="<?=$i;?>" <?php if ($i == $pconfig['network_subnet']) echo "selected"; ?>>
192
                      <?=$i;?>
193
                      </option>
194
                      <?php endfor; ?>
195
                    </select>
196
                    <br> <span class="vexpl"><?=gettext("Destination network for this static route"); ?></span></td>
197
                </tr>
198
                <tr>
199
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Gateway"); ?></td>
200
                  <td width="78%" class="vtable">
201
			<select name="gateway" id="gateway" class="formselect">
202
			<?php
203
				foreach ($a_gateways as $gateway) {
204
	                      		echo "<option value='{$gateway['name']}' ";
205
					if ($gateway['name'] == $pconfig['gateway'])
206
						echo "selected";
207
	                      		echo ">" . htmlspecialchars($gateway['name']) . " - " . htmlspecialchars($gateway['gateway']) . "</option>\n";
208
				}
209
			?>
210
                    </select> <br />
211
			<div id='addgwbox'>
212
				<?=gettext("Choose which gateway this route applies to or"); ?> <a OnClick="show_add_gateway();" href="#"><?=gettext("add a new one.");?></a>
213
								</div>
214
								<div id='notebox'>
215
								</div>
216
								<div style="display:none" name ="status" id="status">
217
								</div>								
218
								<div style="display:none" id="addgateway" name="addgateway">
219
									<p> 
220
									<table border="1" style="background:#990000; border-style: none none none none; width:225px;"><tr><td>
221
										<table bgcolor="#990000" cellpadding="1" cellspacing="1">
222
											<tr><td>&nbsp;</td>
223
											<tr>
224
												<td colspan="2"><center><b><font color="white"><?=gettext("Add new gateway:"); ?></b></center></td>
225
											</tr>
226
											<tr><td>&nbsp;</td>
227
											<tr>
228
												<td width="45%" align="right"><font color="white"><?=gettext("Default gateway:"); ?></td><td><input type="checkbox" id="defaultgw" name="defaultgw"></td>
229
											</tr>												
230
											<tr>
231
												<td width="45%" align="right"><font color="white"><?=gettext("Interface:"); ?></td>
232
												<td><select name="addinterfacegw" id="addinterfacegw">
233
												<?php $gwifs = get_configured_interface_with_descr();
234
													foreach($gwifs as $fif => $dif)
235
														echo "<option value=\"{$fif}\">{$dif}</option>\n";
236
												?>
237
												</select></td>
238
											</tr>
239
											<tr>
240
												<td align="right"><font color="white"><?=gettext("Gateway Name:"); ?></td><td><input id="name" name="name" value="GW"></td>
241
											</tr>
242
											<tr>
243
												<td align="right"><font color="white"><?=gettext("Gateway IP:"); ?></td><td><input id="gatewayip" name="gatewayip"></td>
244
											</tr>
245
											<tr>
246
												<td align="right"><font color="white"><?=gettext("Description:"); ?></td><td><input id="gatewaydescr" name="gatewaydescr"></td>
247
											</tr>
248
											<tr><td>&nbsp;</td>
249
											<tr>
250
												<td colspan="2">
251
													<center>
252
														<div id='savebuttondiv'>
253
															<input type="hidden" name="addrtype" id="addrtype" value="IPv4" />
254
															<input id="gwsave" type="Button" value="<?=gettext("Save Gateway"); ?>" onClick='hide_add_gatewaysave();'> 
255
															<input id="gwcancel" type="Button" value="<?=gettext("Cancel"); ?>" onClick='hide_add_gateway();'>
256
														</div>
257
													</center>
258
												</td>
259
											</tr>
260
											<tr><td>&nbsp;</td>
261
										</table>
262
										</td></tr></table>
263
									<p/>
264
								</div>
265
                </tr>
266
		<tr>
267
                  <td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
268
                  <td width="78%" class="vtable"> 
269
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
270
                    <br> <span class="vexpl"><?=gettext("You may enter a description here for your reference (not parsed)."); ?></span></td>
271
                </tr>
272
                <tr>
273
                  <td width="22%" valign="top">&nbsp;</td>
274
                  <td width="78%"> 
275
                    <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()">
276
                    <?php if (isset($id) && $a_routes[$id]): ?>
277
                    <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>">
278
                    <?php endif; ?>
279
                  </td>
280
                </tr>
281
              </table>
282
</form>
283
<script type="text/javascript">
284
					var gatewayip;
285
					var name;
286
					function show_add_gateway() {
287
						document.getElementById("addgateway").style.display = '';
288
						document.getElementById("addgwbox").style.display = 'none';
289
						document.getElementById("gateway").style.display = 'none';
290
						document.getElementById("save").style.display = 'none';
291
						document.getElementById("cancel").style.display = 'none';
292
						document.getElementById("gwsave").style.display = '';
293
						document.getElementById("gwcancel").style.display = '';
294
						jQuery('#notebox').html("");
295
					}
296
					function hide_add_gateway() {
297
						document.getElementById("addgateway").style.display = 'none';
298
						document.getElementById("addgwbox").style.display = '';	
299
						document.getElementById("gateway").style.display = '';
300
						document.getElementById("save").style.display = '';
301
						document.getElementById("cancel").style.display = '';
302
						document.getElementById("gwsave").style.display = '';
303
						document.getElementById("gwcancel").style.display = '';
304
					}
305
					function hide_add_gatewaysave() {
306
						document.getElementById("addgateway").style.display = 'none';
307
						jQuery('#status').html('<img src="/themes/metallic/images/misc/loader.gif"> One moment please...');
308
						var iface = jQuery('#addinterfacegw').val();
309
						name = jQuery('#name').val();
310
						var descr = jQuery('#gatewaydescr').val();
311
						gatewayip = jQuery('#gatewayip').val();
312
						addrtype = jQuery('#addrtype').val();
313
						var defaultgw = '';
314
						if (jQuery('#defaultgw').checked)
315
							defaultgw = 'yes';
316
						var url = "system_gateways_edit.php";
317
						var pars = 'isAjax=true&defaultgw=' + escape(defaultgw) + '&interface=' + escape(iface) + '&name=' + escape(name) + '&descr=' + escape(descr) + '&gateway=' + escape(gatewayip) + '&type=' + escape(addrtype);
318
						jQuery.ajax(
319
							url,
320
							{
321
								type: 'post',
322
								data: pars,
323
								error: report_failure,
324
								complete: save_callback
325
							});
326
					}
327
					function addOption(selectbox,text,value)
328
					{
329
						var optn = document.createElement("OPTION");
330
						optn.text = text;
331
						optn.value = value;
332
						selectbox.append(optn);
333
						selectbox.prop('selectedIndex',selectbox.children('option').length-1);
334
						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>");
335
					}				
336
					function report_failure() {
337
						alert("<?=gettext("Sorry, we could not create your gateway at this time."); ?>");
338
						hide_add_gateway();
339
					}
340
					function save_callback(transport) {
341
						var response = transport.responseText;
342
						if (response) {
343
							document.getElementById("addgateway").style.display = 'none';
344
							hide_add_gateway();
345
							jQuery('#status').html('');
346
							addOption(jQuery('#gateway'), name, name);
347
						} else {
348
							report_failure();
349
						}
350
					}
351
				</script>
352
<?php include("fend.inc"); ?>
353
</body>
354
</html>
(210-210/238)