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
$statusurl = "status_gateways.php";
283

    
284
include("head.inc");
285

    
286
?>
287

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

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