Project

General

Profile

Download (12.7 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 dbdd1456 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	guiconfig.inc
5 be81829f Scott Ullrich
	by Scott Ullrich, Copyright 2004, All rights reserved.
6
	originally based on of m0n0wall (http://m0n0.ch/wall)
7
8 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10 be81829f Scott Ullrich
11 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13 be81829f Scott Ullrich
14 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16 be81829f Scott Ullrich
17 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20 be81829f Scott Ullrich
21 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32
33
/* make sure nothing is cached */
34
if (!$omit_nocacheheaders) {
35
	header("Expires: 0");
36
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
37
	header("Cache-Control: no-store, no-cache, must-revalidate");
38
	header("Cache-Control: post-check=0, pre-check=0", false);
39
	header("Pragma: no-cache");
40
}
41
42
/* parse the configuration and include all configuration functions */
43
require_once("config.inc");
44
require_once("functions.inc");
45
46
$d_natconfdirty_path = $g['varrun_path'] . "/nat.conf.dirty";
47
$d_filterconfdirty_path = $g['varrun_path'] . "/filter.conf.dirty";
48
$d_ipsecconfdirty_path = $g['varrun_path'] . "/ipsec.conf.dirty";
49
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
50
$d_pptpuserdirty_path = $g['varrun_path'] . "/pptpd.user.dirty";
51
$d_hostsdirty_path = $g['varrun_path'] . "/hosts.dirty";
52
$d_staticmapsdirty_path = $g['varrun_path'] . "/staticmaps.dirty";
53
$d_staticroutesdirty_path = $g['varrun_path'] . "/staticroutes.dirty";
54
$d_aliasesdirty_path = $g['varrun_path'] . "/aliases.dirty";
55
$d_proxyarpdirty_path = $g['varrun_path'] . "/proxyarp.dirty";
56
$d_fwupenabled_path = $g['varrun_path'] . "/fwup.enabled";
57
$d_firmwarelock_path = $g['varrun_path'] . "/firmware.lock";
58
$d_sysrebootreqd_path = $g['varrun_path'] . "/sysreboot.reqd";
59
$d_passthrumacsdirty_path = $g['varrun_path'] . "/passthrumacs.dirty";
60
$d_allowedipsdirty_path = $g['varrun_path'] . "/allowedips.dirty";
61
$d_ovpnclidirty_path = $g['varrun_path'] . "/ovpnclient.dirty";
62
63
if (file_exists($d_firmwarelock_path)) {
64
	if (!$d_isfwfile) {
65
		header("Location: system_firmware.php");
66
		exit;
67
	} else {
68
		return;
69
	}
70
}
71
72
/* some well knows ports */
73 be81829f Scott Ullrich
$wkports = array(3389 => "MS RDP", 21 => "FTP", 22 => "SSH", 23 => "Telnet", 25 => "SMTP",
74
		53 => "DNS", 80 => "HTTP", 110 => "POP3", 143 => "IMAP", 443 => "HTTPS");
75 5b237745 Scott Ullrich
76 36de9000 Scott Ullrich
/* IP TOS flags */
77 3830eba6 Scott Ullrich
$iptos = array("lowdelay", "throughput", "reliability", "congestion");
78 36de9000 Scott Ullrich
79 5b237745 Scott Ullrich
/* TCP flags */
80
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg");
81
82
$specialnets = array("lan" => "LAN net", "pptp" => "PPTP clients");
83
84
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
85
	$specialnets['opt' . $i] = $config['interfaces']['opt' . $i]['descr'] . " net";
86
}
87 be81829f Scott Ullrich
88 5b237745 Scott Ullrich
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
89
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
90
	"10half" => "10BASE-T half-duplex");
91 be81829f Scott Ullrich
92 5b237745 Scott Ullrich
/* platforms that support firmware updating */
93 643a50c3 Scott Ullrich
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'wrap');
94 5b237745 Scott Ullrich
95
/* IPsec defines */
96
$my_identifier_list = array('myaddress' => 'My IP address',
97
							'address' => 'IP address',
98
							'fqdn' => 'Domain name',
99
							'user_fqdn' => 'User FQDN');
100
101 be81829f Scott Ullrich
$p1_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
102 5b237745 Scott Ullrich
					'cast128' => 'CAST128');
103
$p2_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
104
					'cast128' => 'CAST128', 'rijndael' => 'Rijndael (AES)');
105
$p1_halgos = array('sha1' => 'SHA1', 'md5' => 'MD5');
106
$p2_halgos = array('hmac_sha1' => 'SHA1', 'hmac_md5' => 'MD5');
107
$p2_protos = array('esp' => 'ESP', 'ah' => 'AH');
108
$p2_pfskeygroups = array('0' => 'off', '1' => '1', '2' => '2', '5' => '5');
109
110
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
111 be81829f Scott Ullrich
112
	/* check for bad control characters */
113 5b237745 Scott Ullrich
	foreach ($postdata as $pn => $pd) {
114
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
115
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
116
		}
117
	}
118 be81829f Scott Ullrich
119 5b237745 Scott Ullrich
	for ($i = 0; $i < count($reqdfields); $i++) {
120
		if (!$_POST[$reqdfields[$i]]) {
121
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
122
		}
123 be81829f Scott Ullrich
	}
124 5b237745 Scott Ullrich
}
125
126
function print_input_errors($input_errors) {
127
	echo "<p><table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
128
	echo "<tr><td bgcolor=\"#A12A2A\" width=\"36\" align=\"center\" valign=\"top\"><img src=\"/err.gif\" width=\"28\" height=\"32\"></td>\n";
129
	echo "<td bgcolor=\"#FFD9D1\" style=\"padding-left: 8px; padding-top: 6px\">";
130 be81829f Scott Ullrich
131 5b237745 Scott Ullrich
	echo "<span class=\"errmsg\"><p>The following input errors were detected:<ul>\n";
132
	foreach ($input_errors as $ierr) {
133
		echo "<li>" . htmlspecialchars($ierr) . "</li>\n";
134
	}
135
	echo "</ul></span>";
136 be81829f Scott Ullrich
137 5b237745 Scott Ullrich
	echo "</td></tr></table></p>";
138
}
139
140
function exec_rc_script($scriptname) {
141 be81829f Scott Ullrich
142 5b237745 Scott Ullrich
	global $d_sysrebootreqd_path;
143 be81829f Scott Ullrich
144 5b237745 Scott Ullrich
	if (file_exists($d_sysrebootreqd_path))
145
		return 0;
146 be81829f Scott Ullrich
147 5b237745 Scott Ullrich
	exec($scriptname . " >/dev/null 2>&1", $execoutput, $retval);
148 be81829f Scott Ullrich
	return $retval;
149 5b237745 Scott Ullrich
}
150
151
function exec_rc_script_async($scriptname) {
152 be81829f Scott Ullrich
153 5b237745 Scott Ullrich
	global $d_sysrebootreqd_path;
154 be81829f Scott Ullrich
155 5b237745 Scott Ullrich
	if (file_exists($d_sysrebootreqd_path))
156
		return 0;
157 be81829f Scott Ullrich
158 5b237745 Scott Ullrich
	exec("nohup " . $scriptname . " >/dev/null 2>&1 &", $execoutput, $retval);
159 be81829f Scott Ullrich
	return $retval;
160 5b237745 Scott Ullrich
}
161
162
function verify_gzip_file($fname) {
163
164 1ba03d69 Scott Ullrich
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
165 5b237745 Scott Ullrich
	if ($returnvar != 0)
166
		return 0;
167
	else
168
		return 1;
169
}
170
171
function print_info_box_np($msg) {
172
	echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
173 698ba01f Scott Ullrich
	echo "<tr><td bgcolor=\"#990000\" align=\"center\" valign=\"top\" width=\"36\"><img src=\"/exclam.gif\" width=\"28\" height=\"32\"></td>\n";
174 7f5b4824 Scott Ullrich
	echo "<td bgcolor=\"#00000\" style=\"padding-left: 8px\"><font color=\"#ffffff\">";
175 5b237745 Scott Ullrich
	echo $msg;
176 7f5b4824 Scott Ullrich
	echo "</td></tr></table><br>";
177 5b237745 Scott Ullrich
}
178
179
function print_info_box($msg) {
180
	echo "<p>";
181
	print_info_box_np($msg);
182
	echo "</p>";
183
}
184
185
function format_bytes($bytes) {
186
	if ($bytes >= 1073741824) {
187
		return sprintf("%.2f GB", $bytes/1073741824);
188
	} else if ($bytes >= 1048576) {
189
		return sprintf("%.2f MB", $bytes/1048576);
190
	} else if ($bytes >= 1024) {
191
		return sprintf("%.0f KB", $bytes/1024);
192
	} else {
193
		return sprintf("%d bytes", $bytes);
194
	}
195
}
196
197
function get_std_save_message($ok) {
198
	global $d_sysrebootreqd_path;
199
200
	if ($ok == 0) {
201
		if (file_exists($d_sysrebootreqd_path))
202 ccc734c0 Scott Ullrich
			return "The changes have been saved. You must <a color=\"#FFFF00\" href=\"/reboot.php\">reboot</a> your firewall for changes to take effect.";
203 5b237745 Scott Ullrich
		else
204
			return "The changes have been applied successfully.";
205
	} else {
206
		return "ERROR: the changes could not be applied (error code $ok).";
207
	}
208
}
209
210
function pprint_address($adr) {
211
	global $specialnets;
212
213
	if (isset($adr['any'])) {
214
		$padr = "*";
215
	} else if ($adr['network']) {
216
		$padr = $specialnets[$adr['network']];
217
	} else {
218
		$padr = $adr['address'];
219
	}
220 be81829f Scott Ullrich
221 5b237745 Scott Ullrich
	if (isset($adr['not']))
222
		$padr = "! " . $padr;
223 be81829f Scott Ullrich
224 5b237745 Scott Ullrich
	return $padr;
225
}
226
227
function pprint_port($port) {
228
	global $wkports;
229
230
	$pport = "";
231 be81829f Scott Ullrich
232 5b237745 Scott Ullrich
	if (!$port)
233
		echo "*";
234
	else {
235
		$srcport = explode("-", $port);
236
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
237
			$pport = $srcport[0];
238
			if ($wkports[$srcport[0]]) {
239
				$pport .= " (" . $wkports[$srcport[0]] . ")";
240
			}
241
		} else
242
			$pport .= $srcport[0] . " - " . $srcport[1];
243
	}
244 be81829f Scott Ullrich
245 5b237745 Scott Ullrich
	return $pport;
246
}
247
248
/* sort by interface only, retain the original order of rules that apply to
249
   the same interface */
250
function filter_rules_sort() {
251
	global $g, $config;
252 be81829f Scott Ullrich
253 5b237745 Scott Ullrich
	/* mark each rule with the sequence number (to retain the order while sorting) */
254
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
255
		$config['filter']['rule'][$i]['seq'] = $i;
256 be81829f Scott Ullrich
257 5b237745 Scott Ullrich
	function filtercmp($a, $b) {
258
		if ($a['interface'] == $b['interface'])
259
			return $a['seq'] - $b['seq'];
260
		else
261
			return -strcmp($a['interface'], $b['interface']);
262
	}
263 be81829f Scott Ullrich
264 5b237745 Scott Ullrich
	usort($config['filter']['rule'], "filtercmp");
265 be81829f Scott Ullrich
266 5b237745 Scott Ullrich
	/* strip the sequence numbers again */
267
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
268
		unset($config['filter']['rule'][$i]['seq']);
269
}
270
271
function nat_rules_sort() {
272
	global $g, $config;
273 be81829f Scott Ullrich
274 5b237745 Scott Ullrich
	function natcmp($a, $b) {
275
		if ($a['external-address'] == $b['external-address']) {
276
			if ($a['protocol'] == $b['protocol']) {
277
				if ($a['external-port'] == $b['external-port']) {
278
					return 0;
279
				} else {
280
					return ($a['external-port'] - $b['external-port']);
281
				}
282
			} else {
283
				return strcmp($a['protocol'], $b['protocol']);
284
			}
285
		} else if (!$a['external-address'])
286
			return 1;
287
		else if (!$b['external-address'])
288
			return -1;
289
		else
290
			return ipcmp($a['external-address'], $b['external-address']);
291
	}
292 be81829f Scott Ullrich
293 5b237745 Scott Ullrich
	usort($config['nat']['rule'], "natcmp");
294
}
295
296
function nat_1to1_rules_sort() {
297
	global $g, $config;
298 be81829f Scott Ullrich
299 5b237745 Scott Ullrich
	function nat1to1cmp($a, $b) {
300
		return ipcmp($a['external'], $b['external']);
301
	}
302 be81829f Scott Ullrich
303 5b237745 Scott Ullrich
	usort($config['nat']['onetoone'], "nat1to1cmp");
304
}
305
306
function nat_server_rules_sort() {
307
	global $g, $config;
308 be81829f Scott Ullrich
309 5b237745 Scott Ullrich
	function natservercmp($a, $b) {
310
		return ipcmp($a['ipaddr'], $b['ipaddr']);
311
	}
312 be81829f Scott Ullrich
313 5b237745 Scott Ullrich
	usort($config['nat']['servernat'], "natservercmp");
314
}
315
316
function nat_out_rules_sort() {
317
	global $g, $config;
318 be81829f Scott Ullrich
319 5b237745 Scott Ullrich
	function natoutcmp($a, $b) {
320
		return strcmp($a['source']['network'], $b['source']['network']);
321
	}
322 be81829f Scott Ullrich
323 5b237745 Scott Ullrich
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
324
}
325
326
function pptpd_users_sort() {
327
	global $g, $config;
328 be81829f Scott Ullrich
329 5b237745 Scott Ullrich
	function usercmp($a, $b) {
330
		return strcasecmp($a['name'], $b['name']);
331
	}
332 be81829f Scott Ullrich
333 5b237745 Scott Ullrich
	usort($config['pptpd']['user'], "usercmp");
334
}
335
336
function staticroutes_sort() {
337
	global $g, $config;
338
339
	function staticroutecmp($a, $b) {
340
		return strcmp($a['network'], $b['network']);
341
	}
342
343
	usort($config['staticroutes']['route'], "staticroutecmp");
344
}
345
346
function hosts_sort() {
347
	global $g, $config;
348
349
	function hostcmp($a, $b) {
350
		return strcasecmp($a['host'], $b['host']);
351
	}
352
353
	usort($config['dnsmasq']['hosts'], "hostcmp");
354
}
355
356
function staticmaps_sort($if) {
357
	global $g, $config;
358
359
	function staticmapcmp($a, $b) {
360
		return ipcmp($a['ipaddr'], $b['ipaddr']);
361
	}
362
363
	usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
364
}
365
366
function aliases_sort() {
367
	global $g, $config;
368
369
	function aliascmp($a, $b) {
370
		return strcmp($a['name'], $b['name']);
371
	}
372
373
	usort($config['aliases']['alias'], "aliascmp");
374
}
375
376
function ipsec_mobilekey_sort() {
377
	global $g, $config;
378
379
	function mobilekeycmp($a, $b) {
380
		return strcmp($a['ident'][0], $b['ident'][0]);
381
	}
382
383
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
384
}
385
386
function proxyarp_sort() {
387
	global $g, $config;
388
389
	function proxyarpcmp($a, $b) {
390
		if (isset($a['network']))
391
			list($ast,$asn) = explode("/", $a['network']);
392
		else if (isset($a['range'])) {
393
			$ast = $a['range']['from'];
394
			$asn = 32;
395
		}
396
		if (isset($b['network']))
397
			list($bst,$bsn) = explode("/", $b['network']);
398
		else if (isset($b['range'])) {
399
			$bst = $b['range']['from'];
400
			$bsn = 32;
401
		}
402
		if (ipcmp($ast, $bst) == 0)
403
			return ($asn - $bsn);
404
		else
405
			return ipcmp($ast, $bst);
406
	}
407
408
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
409
}
410
411
function passthrumacs_sort() {
412
	global $g, $config;
413
414
	function passthrumacscmp($a, $b) {
415
		return strcmp($a['mac'], $b['mac']);
416
	}
417 be81829f Scott Ullrich
418 5b237745 Scott Ullrich
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
419
}
420
421
function allowedips_sort() {
422
	global $g, $config;
423
424
	function allowedipscmp($a, $b) {
425
		return strcmp($a['ip'], $b['ip']);
426
	}
427 be81829f Scott Ullrich
428 5b237745 Scott Ullrich
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
429
}
430
431
function wol_sort() {
432
	global $g, $config;
433
434
	function wolcmp($a, $b) {
435
		return strcmp($a['descr'], $b['descr']);
436
	}
437 be81829f Scott Ullrich
438 5b237745 Scott Ullrich
	usort($config['wol']['wolentry'], "wolcmp");
439
}
440
441
function gentitle($pgname) {
442
	global $config;
443
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
444
}
445
446 d345bd39 Bill Marquette
/* update the changedesc and changecount(er) variables */
447
function update_changedesc($update) {
448
	global $changedesc;
449
	global $changecount;
450
451
	$changedesc .= " {$update}";
452
	$changecount++;
453
}
454
455 4d762703 Bill Marquette
/* Check if variable has changed, update and log if it has
456 edf14245 Bill Marquette
 * returns true if var changed
457 4d762703 Bill Marquette
 * varname = variable name in plain text
458
 * orig = original value
459
 * new = new value
460
 */
461 9eab73da Bill Marquette
function update_if_changed($varname, & $orig, $new) {
462 4d762703 Bill Marquette
	if ($orig != $new) {
463 d345bd39 Bill Marquette
		update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
464 4d762703 Bill Marquette
		$orig = $new;
465 edf14245 Bill Marquette
		return true;
466 4d762703 Bill Marquette
	}
467 edf14245 Bill Marquette
	return false;
468 4d762703 Bill Marquette
}
469
470 5b237745 Scott Ullrich
?>