Project

General

Profile

Download (19.7 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) 2007 Seth Mos <seth.mos@xs4all.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();
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
	if($a_gateways[$id]['monitor'] <> "") {
80
		$pconfig['monitor'] = $a_gateways[$id]['monitor'];
81
	} else {
82
		$pconfig['monitor'] == "";
83
	}
84
	$pconfig['descr'] = $a_gateways[$id]['descr'];
85
	$pconfig['attribute'] = $a_gateways[$id]['attribute'];
86
}
87

    
88
if (isset($_GET['dup'])) {
89
	unset($id);
90
	unset($pconfig['attribute']);
91
}
92

    
93
if ($_POST) {
94

    
95
	unset($input_errors);
96

    
97
	/* input validation */
98
	$reqdfields = explode(" ", "name");
99
	$reqdfieldsn = array(gettext("Name"));
100

    
101
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
102

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

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

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

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

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

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

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

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

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

    
214
			$gateway = array();
215
			if ($_POST['attribute'] == "system") {
216
				$gateway['interface'] = $pconfig['friendlyiface'];
217
				$gateway['gateway'] = "dynamic";
218
			} else {
219
				$gateway['interface'] = $_POST['interface'];
220
				$gateway['gateway'] = $_POST['gateway'];
221
			}
222
			$gateway['name'] = $_POST['name'];
223
			$gateway['weight'] = $_POST['weight'];
224
			$gateway['descr'] = $_POST['descr'];
225
			if(is_ipaddr($_POST['monitor'])) {
226
				$gateway['monitor'] = $_POST['monitor'];
227
			} else {
228
				unset($gateway['monitor']);
229
			}			
230
			if ($_POST['defaultgw'] == "yes" or $_POST['defaultgw'] == "on") {
231
				$i = 0;
232
				foreach($a_gateway_item as $gw) {
233
					unset($config['gateways']['gateway_item'][$i]['defaultgw']);
234
					$i++;
235
				}
236
				$gateway['defaultgw'] = true;
237
				$reloadif = true;
238
			} else {
239
				unset($gateway['defaultgw']);
240
			}
241

    
242
			if ($_POST['latencylow'])
243
				$gateway['latencylow'] = $_POST['latencylow'];
244
			if ($_POST['latencyhigh'])
245
                		$gateway['latencyhigh'] = $_POST['latencyhigh'];
246
			if ($_POST['losslow'])
247
               			$gateway['losslow'] = $_POST['losslow'];
248
			if ($_POST['losshigh'])
249
                		$gateway['losshigh'] = $_POST['losshigh'];
250
			if ($_POST['down'])
251
                		$gateway['down'] = $_POST['down'];
252

    
253
			/* when saving the manual gateway we use the attribute which has the corresponding id */
254
			if (isset($id) && $a_gateway_item[$id]) {
255
				$a_gateway_item[$id] = $gateway;
256
			} else {
257
				$a_gateway_item[] = $gateway;
258
			}
259
		}
260
		system_resolvconf_generate();
261
		mark_subsystem_dirty('staticroutes');
262
		
263
		write_config();
264
		
265
		if($_REQUEST['isAjax']) {
266
			echo $_POST['name'];
267
			exit;
268
		} else if ($reloadif == true)
269
			interface_configure($_POST['interface']);
270
		
271
		header("Location: system_gateways.php");
272
		exit;
273
	}  else {
274
		$pconfig = $_POST;
275
		if (empty($_POST['friendlyiface']))
276
			$pconfig['friendlyiface'] = $_POST['interface'];
277
	}
278
}
279

    
280

    
281
$pgtitle = array(gettext("System"),gettext("Gateways"),gettext("Edit gateway"));
282
include("head.inc");
283

    
284
?>
285

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

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