Project

General

Profile

Download (19.6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php 
2
/* $Id$ */
3
/*
4
	system_gateways_edit.php
5
	part of pfSense (http://pfsense.com)
6
	
7
	Copyright (C) 2010 Seth Mos <seth.mos@dds.nl>.
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-gateways-editgateway
37
##|*NAME=System: Gateways: Edit Gateway page
38
##|*DESCR=Allow access to the 'System: Gateways: Edit Gateway' page.
39
##|*MATCH=system_gateways_edit.php*
40
##|-PRIV
41

    
42
require("guiconfig.inc");
43
require("pkg-utils.inc");
44

    
45
$a_gateways = return_gateways_array(true);
46
$a_gateways_arr = array();
47
foreach($a_gateways as $gw) {
48
	$a_gateways_arr[] = $gw;
49
}
50
$a_gateways = $a_gateways_arr;
51

    
52
if (!is_array($config['gateways']['gateway_item']))
53
        $config['gateways']['gateway_item'] = array();
54
        
55
$a_gateway_item = &$config['gateways']['gateway_item'];
56

    
57
$id = $_GET['id'];
58
if (isset($_POST['id']))
59
	$id = $_POST['id'];
60

    
61
if (isset($_GET['dup'])) {
62
	$id = $_GET['dup'];
63
}
64

    
65
if (isset($id) && $a_gateways[$id]) {
66
	$pconfig['name'] = $a_gateways[$id]['name'];
67
	$pconfig['weight'] = $a_gateways[$id]['weight'];
68
	$pconfig['interface'] = $a_gateways[$id]['interface'];
69
	$pconfig['friendlyiface'] = $a_gateways[$id]['friendlyiface'];
70
	$pconfig['gateway'] = $a_gateways[$id]['gateway'];
71
	$pconfig['defaultgw'] = isset($a_gateways[$id]['defaultgw']);
72
	$pconfig['latencylow'] = $a_gateway_item[$id]['latencylow'];
73
        $pconfig['latencyhigh'] = $a_gateway_item[$id]['latencyhigh'];
74
        $pconfig['losslow'] = $a_gateway_item[$id]['losslow'];
75
        $pconfig['losshigh'] = $a_gateway_item[$id]['losshigh'];
76
        $pconfig['down'] = $a_gateway_item[$id]['down'];
77
	if (isset($a_gateways[$id]['dynamic']))
78
		$pconfig['dynamic'] = true;
79
	$pconfig['monitor'] = $a_gateways[$id]['monitor'];
80
	$pconfig['descr'] = $a_gateways[$id]['descr'];
81
	$pconfig['attribute'] = $a_gateways[$id]['attribute'];
82
}
83

    
84
if (isset($_GET['dup'])) {
85
	unset($id);
86
	unset($pconfig['attribute']);
87
}
88

    
89
if ($_POST) {
90

    
91
	unset($input_errors);
92

    
93
	/* input validation */
94
	$reqdfields = explode(" ", "name");
95
	$reqdfieldsn = array(gettext("Name"));
96

    
97
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
98

    
99
	if (! isset($_POST['name'])) {
100
		$input_errors[] = "A valid gateway name must be specified.";
101
	}
102
	if (! is_validaliasname($_POST['name'])) {
103
		$input_errors[] = gettext("The gateway name must not contain invalid characters.");
104
	}
105
	/* skip system gateways which have been automatically added */
106
	if (($_POST['gateway'] && (!is_ipaddr($_POST['gateway'])) && ($_POST['attribute'] != "system")) && ($_POST['gateway'] != "dynamic")) {
107
		$input_errors[] = gettext("A valid gateway IP address must be specified.");
108
	}
109

    
110
	if ($_POST['gateway'] && (is_ipaddr($_POST['gateway'])) && ($pconfig['attribute'] != "system") && !$_REQUEST['isAjax']) {
111
		$parent_ip = get_interface_ip($_POST['interface']);
112
		if (is_ipaddr($parent_ip)) {
113
			$parent_sn = get_interface_subnet($_POST['interface']);
114
			if(!ip_in_subnet($_POST['gateway'], gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn)) {
115
				$input_errors[] = sprintf(gettext("The gateway address %s does not lie within the chosen interface's subnet."), $_POST['gateway']);
116
			}
117
		}
118
	}
119
	if (($_POST['monitor'] <> "") && !is_ipaddr($_POST['monitor']) && $_POST['monitor'] != "dynamic") {
120
		$input_errors[] = gettext("A valid monitor IP address must be specified.");
121
	}
122

    
123
	if (isset($_POST['name'])) {
124
		/* check for overlaps */
125
		foreach ($a_gateways as $gateway) {
126
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
127
				continue;
128
			}
129
			if($_POST['name'] <> "") {
130
				if (($gateway['name'] <> "") && ($_POST['name'] == $gateway['name']) && ($gateway['attribute'] != "system")) {
131
					$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $_POST['name']);
132
					break;
133
				}
134
			}
135
			if(is_ipaddr($_POST['gateway'])) {
136
				if (($gateway['gateway'] <> "") && ($_POST['gateway'] == $gateway['gateway']) && ($gateway['attribute'] != "system")) {
137
					$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $_POST['gateway']);
138
					break;
139
				}
140
			}
141
			if(is_ipaddr($_POST['monitor'])) {
142
				if (($gateway['monitor'] <> "") && ($_POST['monitor'] == $gateway['monitor']) && ($gateway['attribute'] != "system")) {
143
					$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. You must choose a different monitor IP.'), $_POST['monitor']);
144
					break;
145
				}
146
			}
147
		}
148
	}
149

    
150
	/* input validation */
151
        if($_POST['latencylow']) {
152
                if (! is_numeric($_POST['latencylow'])) {
153
                        $input_errors[] = gettext("The low latency watermark needs to be a numeric value.");
154
                }
155
        }
156

    
157
        if($_POST['latencyhigh']) {
158
                if (! is_numeric($_POST['latencyhigh'])) {
159
                        $input_errors[] = gettext("The high latency watermark needs to be a numeric value.");
160
                }
161
        }
162
        if($_POST['losslow']) {
163
                if (! is_numeric($_POST['losslow'])) {
164
                        $input_errors[] = gettext("The low loss watermark needs to be a numeric value.");
165
                }
166
        }
167
        if($_POST['losshigh']) {
168
                if (! is_numeric($_POST['losshigh'])) {
169
                        $input_errors[] = gettext("The high loss watermark needs to be a numeric value.");
170
                }
171
        }
172

    
173
        if(($_POST['latencylow']) && ($_POST['latencyhigh'])){
174
                if(($_POST['latencylow'] > $_POST['latencyhigh'])) {
175
                        $input_errors[] = gettext("The High latency watermark needs to be higher then the low latency watermark");
176
                }
177
        }
178

    
179
        if(($_POST['losslow']) && ($_POST['losshigh'])){
180
                if($_POST['losslow'] > $_POST['losshigh']) {
181
                        $input_errors[] = gettext("The High packet loss watermark needs to be higher then the low packet loss watermark");
182
                }
183
        }
184
	if($_POST['down']) {
185
                if (! is_numeric($_POST['down']) || $_POST['down'] < 1) {
186
                        $input_errors[] = gettext("The low latency watermark needs to be a numeric value.");
187
                }
188
        }
189

    
190
	if (!$input_errors) {
191
		$reloadif = false;
192
		$save = false;
193
		if (($_POST['weight'] && $_POST['weight'] > 1) ||
194
		    $_POST['latencylow'] || $_POST['latencyhigh'] || $_POST['losslow'] || $_POST['losshigh'] || $_POST['down'] ||
195
		    $_POST['defaultgw'] || ($_POST['gateway'] && $_POST['gateway'] != "dynamic"))
196
			$save = true;
197
		/* if we are processing a system gateway only save the monitorip */
198
		if (!$save && (empty($_POST['gateway']) || $_POST['gateway'] == "dynamic")) {
199
			if (is_ipaddr($_POST['monitor'])) {
200
				if (empty($_POST['interface']))
201
					$interface = $pconfig['friendlyiface'];
202
				else
203
					$interface = $_POST['interface'];
204
				$config['interfaces'][$interface]['monitorip'] = $_POST['monitor'];
205
			}
206
			/* when dynamic gateway is not anymore a default the entry is no more needed. */
207
                        if (isset($id) && $a_gateway_item[$id])
208
                                unset($a_gateway_item[$id]);
209
		} else {
210

    
211
			/* Manual gateways are handled differently */
212
			/* rebuild the array with the manual entries only */
213

    
214
			$gateway = array();
215
			$gateway['interface'] = $_POST['interface'];
216
			$gateway['gateway'] = $_POST['gateway'];
217
			$gateway['name'] = $_POST['name'];
218
			$gateway['weight'] = $_POST['weight'];
219
			$gateway['descr'] = $_POST['descr'];
220
			if (is_ipaddr($_POST['monitor']))
221
				$gateway['monitor'] = $_POST['monitor'];
222
			else
223
				unset($gateway['monitor']);
224

    
225
			if ($_POST['defaultgw'] == "yes" || $_POST['defaultgw'] == "on") {
226
				$i = 0;
227
				foreach($a_gateway_item as $gw) {
228
					unset($config['gateways']['gateway_item'][$i]['defaultgw']);
229
					$i++;
230
				}
231
				$gateway['defaultgw'] = true;
232
				$reloadif = true;
233
			} else
234
				unset($gateway['defaultgw']);
235

    
236
			if ($_POST['latencylow'])
237
				$gateway['latencylow'] = $_POST['latencylow'];
238
			if ($_POST['latencyhigh'])
239
                		$gateway['latencyhigh'] = $_POST['latencyhigh'];
240
			if ($_POST['losslow'])
241
               			$gateway['losslow'] = $_POST['losslow'];
242
			if ($_POST['losshigh'])
243
                		$gateway['losshigh'] = $_POST['losshigh'];
244
			if ($_POST['down'])
245
                		$gateway['down'] = $_POST['down'];
246

    
247
			/* when saving the manual gateway we use the attribute which has the corresponding id */
248
			if (isset($id) && $a_gateway_item[$id])
249
				$a_gateway_item[$id] = $gateway;
250
			else
251
				$a_gateway_item[] = $gateway;
252
		}
253
		mark_subsystem_dirty('staticroutes');
254
		
255
		write_config();
256
		
257
		if($_REQUEST['isAjax']) {
258
			echo $_POST['name'];
259
			exit;
260
		} else if ($reloadif == true)
261
			send_event("interface reconfigure {$_POST['interface']}");
262
		
263
		header("Location: system_gateways.php");
264
		exit;
265
	} else {
266
		$pconfig = $_POST;
267
		if (empty($_POST['friendlyiface']))
268
			$pconfig['friendlyiface'] = $_POST['interface'];
269
	}
270
}
271

    
272

    
273
$pgtitle = array(gettext("System"),gettext("Gateways"),gettext("Edit gateway"));
274
$statusurl = "status_gateways.php";
275

    
276
include("head.inc");
277

    
278
?>
279

    
280
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
281
<?php include("fbegin.inc"); ?>
282
<script language="JavaScript">
283
function enable_change(obj) {
284
	if (document.iform.gateway.disabled) {
285
		if (obj.checked)
286
			document.iform.interface.disabled=false;
287
		else
288
			document.iform.interface.disabled=true;
289
	}	
290
	
291
}
292
function show_advanced_gateway() {
293
        document.getElementById("showadvgatewaybox").innerHTML='';
294
        aodiv = document.getElementById('showgatewayadv');
295
        aodiv.style.display = "block";
296
}
297
</script>
298
<?php if ($input_errors) print_input_errors($input_errors); ?>
299
            <form action="system_gateways_edit.php" method="post" name="iform" id="iform">
300
	<?php
301

    
302
	/* If this is a system gateway we need this var */
303
	if(($pconfig['attribute'] == "system") || is_numeric($pconfig['attribute'])) {
304
		echo "<input type='hidden' name='attribute' id='attribute' value='{$pconfig['attribute']}' >\n";
305
	}
306
	echo "<input type='hidden' name='friendlyiface' id='friendlyiface' value='{$pconfig['friendlyiface']}' >\n";
307
	?>
308
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
309
				<tr>
310
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit gateway"); ?></td>
311
				</tr>	
312
                <tr> 
313
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Interface"); ?></td>
314
                  <td width="78%" class="vtable">
315
				  <select name="interface" class="formselect" <?php if ($pconfig['dynamic'] == true && $pconfig['attribute'] == "system") echo "disabled"; ?>>
316
		<?php 
317
                      	$interfaces = get_configured_interface_with_descr(false, true);
318
			foreach ($interfaces as $iface => $ifacename) {
319
				echo "<option value=\"{$iface}\"";
320
				if ($iface == $pconfig['friendlyiface'])
321
					echo " selected";
322
				echo ">" . htmlspecialchars($ifacename) . "</option>";
323
			}
324
			if (is_package_installed("openbgpd") == 1) {
325
				echo "<option value=\"bgpd\"";
326
				if ($pconfig['interface'] == "bgpd") 
327
					echo " selected";
328
				echo ">" . gettext("Use BGPD") . "</option>";
329
			}
330
 		  ?>
331
                    </select> <br>
332
                    <span class="vexpl"><?=gettext("Choose which interface this gateway applies to."); ?></span></td>
333
                </tr>
334
                <tr>
335
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Name"); ?></td>
336
                  <td width="78%" class="vtable"> 
337
                    <input name="name" type="text" class="formfld unknown" id="name" size="20" value="<?=htmlspecialchars($pconfig['name']);?>"> 
338
                    <br> <span class="vexpl"><?=gettext("Gateway name"); ?></span></td>
339
                </tr>
340
		<tr>
341
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Gateway"); ?></td>
342
                  <td width="78%" class="vtable"> 
343
                    <input name="gateway" type="text" class="formfld host" id="gateway" size="40" value="<?php if ($pconfig['dynamic']) echo "dynamic"; else echo $pconfig['gateway']; ?>" <?php if ($pconfig['dynamic'] && $pconfig['attribute'] == "system") echo "disabled"; ?>>
344
                    <br> <span class="vexpl"><?=gettext("Gateway IP address"); ?></span></td>
345
                </tr>
346
		<tr>
347
		  <td width="22%" valign="top" class="vncell"><?=gettext("Default Gateway"); ?></td>
348
		  <td width="78%" class="vtable">
349
			<input name="defaultgw" type="checkbox" id="defaultgw" value="yes" <?php if ($pconfig['defaultgw'] == true) echo "checked"; ?> onclick="enable_change(this)" />
350
			<strong><?=gettext("Default Gateway"); ?></strong><br />
351
			<?=gettext("This will select the above gateway as the default gateway"); ?>
352
		  </td>
353
		</tr>
354
		<tr>
355
		  <td width="22%" valign="top" class="vncell"><?=gettext("Monitor IP"); ?></td>
356
		  <td width="78%" class="vtable">
357
			<?php
358
				if(($pconfig['attribute'] == "system") && ($pconfig['gateway'] == "dynamic") && ($pconfig['monitor'] == "")) {
359
					$monitor = "";
360
				} else {
361
					$monitor = htmlspecialchars($pconfig['monitor']);
362
				}
363
			?>
364
			<input name="monitor" type="text" id="monitor" value="<?php echo $monitor; ?>" />
365
			<strong><?=gettext("Alternative monitor IP"); ?></strong> <br />
366
			<?=gettext("Enter an alternative address here to be used to monitor the link. This is used for the " .
367
			"quality RRD graphs as well as the load balancer entries. Use this if the gateway does not respond " .
368
			"to ICMP echo requests (pings)"); ?>.</strong>
369
			<br />
370
		  </td>
371
		</tr>
372
		<tr>
373
		  <td width="22%" valign="top" class="vncell"><?=gettext("Advanced");?></td>
374
		  <td width="78%" class="vtable">
375
			<div id="showadvgatewaybox" <? if (!empty($pconfig['latencylow']) || !empty($pconfig['latencyhigh']) || !empty($pconfig['losslow']) || !empty($pconfig['losshigh']) || (isset($pconfig['weight']) && $pconfig['weight'] > 1)) echo "style='display:none'"; ?>>
376
				<input type="button" onClick="show_advanced_gateway()" value="Advanced"></input> - Show advanced option</a>
377
			</div>
378
			<div id="showgatewayadv" <? if (empty($pconfig['latencylow']) && empty($pconfig['latencyhigh']) && empty($pconfig['losslow']) && empty($pconfig['losshigh']) && (empty($pconfig['weight']) || $pconfig['weight'] == 1)) echo "style='display:none'"; ?>>
379
                        <table class="tabcont" width="100%" border="0" cellspacing="0" cellpadding="6">
380
			<tr>
381
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Weight");?></td>
382
                                <td width="78%" class="vtable">
383
					<select name='weight' class='formfldselect' id='weight'>
384
				<?php
385
					for ($i = 1; $i < 6; $i++) {
386
                                        	$selected = "";
387
                                        	if ($pconfig['weight'] == $i)
388
                                                	$selected = "selected";
389
                                        	echo "<option value='{$i}' {$selected} >{$i}</option>";
390
                                	}
391
				?>
392
					</select>
393
					<br /><?=gettext("Weight for this gateway when used in a Gateway Group.");?> <br />
394
		   		</td>
395
			</tr>
396
                        <tr>
397
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Latency thresholds");?></td>
398
                                <td width="78%" class="vtable">
399
                                <?=gettext("From");?>
400
                                    <input name="latencylow" type="text" class="formfld unknown" id="latencylow" size="2"
401
                                        value="<?=htmlspecialchars($pconfig['latencylow']);?>">
402
                                <?=gettext("To");?>
403
                                    <input name="latencyhigh" type="text" class="formfld unknown" id="latencyhigh" size="2"
404
                                        value="<?=htmlspecialchars($pconfig['latencyhigh']);?>">
405
                                    <br> <span class="vexpl"><?=gettext("These define the low and high water marks for latency in milliseconds.");?></span></td>
406
                                </td>
407
                        </tr>
408
                        <tr>
409
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Packet Loss thresholds");?></td>
410
                                <td width="78%" class="vtable">
411
                                <?=gettext("From");?>
412
                                    <input name="losslow" type="text" class="formfld unknown" id="losslow" size="2"
413
                                        value="<?=htmlspecialchars($pconfig['losslow']);?>">
414
                                <?=gettext("To");?>
415
                                    <input name="losshigh" type="text" class="formfld unknown" id="losshigh" size="2"
416
                                        value="<?=htmlspecialchars($pconfig['losshigh']);?>">
417
                                    <br> <span class="vexpl"><?=gettext("These define the low and high water marks for packet loss in %.");?></span></td>
418
                                </td>
419
                        </tr>
420
			<tr>
421
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Down");?></td>
422
                                <td width="78%" class="vtable">
423
                                    <input name="down" type="text" class="formfld unknown" id="down" size="2"
424
                                        value="<?=htmlspecialchars($pconfig['down']);?>">
425
                                    <br> <span class="vexpl"><?=gettext("This defines the down time for the alarm to fire, in seconds.");?></span></td>
426
                                </td>
427
                        </tr>
428
                        </table>
429
			</div>
430
		   </td>
431
		</tr>
432
		<tr>
433
                  <td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
434
                  <td width="78%" class="vtable"> 
435
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
436
                    <br> <span class="vexpl"><?=gettext("You may enter a description here for your reference (not parsed)"); ?>.</span></td>
437
                </tr>
438
                <tr>
439
                  <td width="22%" valign="top">&nbsp;</td>
440
                  <td width="78%"> 
441
                    <input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>"> <input type="button" value="<?=gettext("Cancel");?>" class="formbtn"  onclick="history.back()">
442
                    <?php if (isset($id) && $a_gateways[$id]): ?>
443
                    <input name="id" type="hidden" value="<?=$id;?>">
444
                    <?php endif; ?>
445
                  </td>
446
                </tr>
447
              </table>
448
</form>
449
<?php include("fend.inc"); ?>
450
<script language="JavaScript">
451
enable_change(document.iform.defaultgw);
452
</script>
453
</body>
454
</html>
(188-188/219)