Project

General

Profile

Download (16.6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	guiconfig.inc
5
	by Scott Ullrich, Copyright 2004, All rights reserved.
6
	originally based on of m0n0wall (http://m0n0.ch/wall)
7

    
8
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	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

    
21
	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
/* Include authentication routines */
43
/* THIS MUST BE ABOVE ALL OTHER CODE */
44
require_once("auth.inc");
45

    
46
/* parse the configuration and include all configuration functions */
47
require_once("config.inc");
48
require_once("functions.inc");
49

    
50
$d_natconfdirty_path = $g['varrun_path'] . "/nat.conf.dirty";
51
$d_filterconfdirty_path = $g['varrun_path'] . "/filter.conf.dirty";
52
$d_ipsecconfdirty_path = $g['varrun_path'] . "/ipsec.conf.dirty";
53
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
54
$d_pptpuserdirty_path = $g['varrun_path'] . "/pptpd.user.dirty";
55
$d_hostsdirty_path = $g['varrun_path'] . "/hosts.dirty";
56
$d_staticmapsdirty_path = $g['varrun_path'] . "/staticmaps.dirty";
57
$d_staticroutesdirty_path = $g['varrun_path'] . "/staticroutes.dirty";
58
$d_aliasesdirty_path = $g['varrun_path'] . "/aliases.dirty";
59
$d_proxyarpdirty_path = $g['varrun_path'] . "/proxyarp.dirty";
60
$d_fwupenabled_path = $g['varrun_path'] . "/fwup.enabled";
61
$d_firmwarelock_path = $g['varrun_path'] . "/firmware.lock";
62
$d_sysrebootreqd_path = $g['varrun_path'] . "/sysreboot.reqd";
63
$d_passthrumacsdirty_path = $g['varrun_path'] . "/passthrumacs.dirty";
64
$d_allowedipsdirty_path = $g['varrun_path'] . "/allowedips.dirty";
65
$d_ovpnclidirty_path = $g['varrun_path'] . "/ovpnclient.dirty";
66
/* used by progress bar */
67
$lastseen = "-1";
68

    
69
if (file_exists($d_firmwarelock_path)) {
70
	if (!$d_isfwfile) {
71
		header("Location: system_firmware.php");
72
		exit;
73
	} else {
74
		return;
75
	}
76
}
77

    
78
/* some well knows ports */
79
$wkports = array(3389 => "MS RDP", 21 => "FTP", 22 => "SSH", 23 => "Telnet", 25 => "SMTP",
80
		53 => "DNS", 80 => "HTTP", 110 => "POP3", 143 => "IMAP", 443 => "HTTPS");
81

    
82
/* IP TOS flags */
83
$iptos = array("lowdelay", "throughput", "reliability", "congestion");
84

    
85
/* TCP flags */
86
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg");
87

    
88
$specialnets = array("lan" => "LAN net", "pptp" => "PPTP clients");
89

    
90
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
91
	$specialnets['opt' . $i] = $config['interfaces']['opt' . $i]['descr'] . " net";
92
}
93

    
94
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
95
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
96
	"10half" => "10BASE-T half-duplex");
97

    
98
/* platforms that support firmware updating */
99
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'wrap');
100

    
101
/* IPsec defines */
102
$my_identifier_list = array('myaddress' => 'My IP address',
103
							'address' => 'IP address',
104
							'fqdn' => 'Domain name',
105
							'user_fqdn' => 'User FQDN');
106

    
107
$p1_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
108
					'cast128' => 'CAST128');
109
$p2_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
110
					'cast128' => 'CAST128', 'rijndael' => 'Rijndael (AES)');
111
$p1_halgos = array('sha1' => 'SHA1', 'md5' => 'MD5');
112
$p2_halgos = array('hmac_sha1' => 'SHA1', 'hmac_md5' => 'MD5');
113
$p2_protos = array('esp' => 'ESP', 'ah' => 'AH');
114
$p2_pfskeygroups = array('0' => 'off', '1' => '1', '2' => '2', '5' => '5');
115

    
116
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
117

    
118
	/* check for bad control characters */
119
	foreach ($postdata as $pn => $pd) {
120
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
121
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
122
		}
123
	}
124

    
125
	for ($i = 0; $i < count($reqdfields); $i++) {
126
		if (!$_POST[$reqdfields[$i]]) {
127
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
128
		}
129
	}
130
}
131

    
132
function print_input_errors($input_errors) {
133
	echo "<p><table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
134
	echo "<tr><td bgcolor=\"#990000\" width=\"36\" align=\"center\" valign=\"top\"><img src=\"/err.gif\" width=\"28\" height=\"32\"></td>\n";
135
	echo "<td bgcolor=\"#FFD9D1\" style=\"padding-left: 8px; padding-top: 6px\">";
136

    
137
	echo "<span class=\"errmsg\"><p>The following input errors were detected:<ul>\n";
138
	foreach ($input_errors as $ierr) {
139
		echo "<li>" . htmlspecialchars($ierr) . "</li>\n";
140
	}
141
	echo "</ul></span>";
142

    
143
	echo "</td></tr></table></p>";
144
}
145

    
146
function exec_rc_script($scriptname) {
147

    
148
	global $d_sysrebootreqd_path;
149

    
150
	if (file_exists($d_sysrebootreqd_path))
151
		return 0;
152

    
153
	exec($scriptname . " >/dev/null 2>&1", $execoutput, $retval);
154
	return $retval;
155
}
156

    
157
function exec_rc_script_async($scriptname) {
158

    
159
	global $d_sysrebootreqd_path;
160

    
161
	if (file_exists($d_sysrebootreqd_path))
162
		return 0;
163

    
164
	exec("nohup " . $scriptname . " >/dev/null 2>&1 &", $execoutput, $retval);
165
	return $retval;
166
}
167

    
168
function verify_gzip_file($fname) {
169

    
170
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
171
	if ($returnvar != 0)
172
		return 0;
173
	else
174
		return 1;
175
}
176

    
177
function print_info_box_np($msg) {
178
	echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
179
	echo "<tr><td bgcolor=\"#990000\" align=\"center\" valign=\"top\" width=\"36\"><img src=\"/exclam.gif\" width=\"28\" height=\"32\"></td>\n";
180
	echo "<td bgcolor=\"#00000\" style=\"padding-left: 8px\"><font color=\"#ffffff\">";
181
	echo $msg;
182
	echo "</td></tr></table><br>";
183
}
184

    
185
function print_info_box($msg) {
186
	echo "<p>";
187
	print_info_box_np($msg);
188
	echo "</p>";
189
}
190

    
191
function format_bytes($bytes) {
192
	if ($bytes >= 1073741824) {
193
		return sprintf("%.2f GB", $bytes/1073741824);
194
	} else if ($bytes >= 1048576) {
195
		return sprintf("%.2f MB", $bytes/1048576);
196
	} else if ($bytes >= 1024) {
197
		return sprintf("%.0f KB", $bytes/1024);
198
	} else {
199
		return sprintf("%d bytes", $bytes);
200
	}
201
}
202

    
203
function get_std_save_message($ok) {
204
	global $d_sysrebootreqd_path;
205

    
206
	if ($ok == 0) {
207
		if (file_exists($d_sysrebootreqd_path))
208
			return "The changes have been saved. You must <a color=\"#FFFF00\" href=\"/reboot.php\">reboot</a> your firewall for changes to take effect.";
209
		else
210
			return "The changes have been applied successfully.";
211
	} else {
212
		return "ERROR: the changes could not be applied (error code $ok).";
213
	}
214
}
215

    
216
function pprint_address($adr) {
217
	global $specialnets;
218

    
219
	if (isset($adr['any'])) {
220
		$padr = "*";
221
	} else if ($adr['network']) {
222
		$padr = $specialnets[$adr['network']];
223
	} else {
224
		$padr = $adr['address'];
225
	}
226

    
227
	if (isset($adr['not']))
228
		$padr = "! " . $padr;
229

    
230
	return $padr;
231
}
232

    
233
function pprint_port($port) {
234
	global $wkports;
235

    
236
	$pport = "";
237

    
238
	if (!$port)
239
		echo "*";
240
	else {
241
		$srcport = explode("-", $port);
242
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
243
			$pport = $srcport[0];
244
			if ($wkports[$srcport[0]]) {
245
				$pport .= " (" . $wkports[$srcport[0]] . ")";
246
			}
247
		} else
248
			$pport .= $srcport[0] . " - " . $srcport[1];
249
	}
250

    
251
	return $pport;
252
}
253

    
254
/* sort by interface only, retain the original order of rules that apply to
255
   the same interface */
256
function filter_rules_sort() {
257
	global $g, $config;
258

    
259
	/* mark each rule with the sequence number (to retain the order while sorting) */
260
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
261
		$config['filter']['rule'][$i]['seq'] = $i;
262

    
263
	function filtercmp($a, $b) {
264
		if ($a['interface'] == $b['interface'])
265
			return $a['seq'] - $b['seq'];
266
		else
267
			return -strcmp($a['interface'], $b['interface']);
268
	}
269

    
270
	usort($config['filter']['rule'], "filtercmp");
271

    
272
	/* strip the sequence numbers again */
273
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
274
		unset($config['filter']['rule'][$i]['seq']);
275
}
276

    
277
function nat_rules_sort() {
278
	global $g, $config;
279

    
280
	function natcmp($a, $b) {
281
		if ($a['external-address'] == $b['external-address']) {
282
			if ($a['protocol'] == $b['protocol']) {
283
				if ($a['external-port'] == $b['external-port']) {
284
					return 0;
285
				} else {
286
					return ($a['external-port'] - $b['external-port']);
287
				}
288
			} else {
289
				return strcmp($a['protocol'], $b['protocol']);
290
			}
291
		} else if (!$a['external-address'])
292
			return 1;
293
		else if (!$b['external-address'])
294
			return -1;
295
		else
296
			return ipcmp($a['external-address'], $b['external-address']);
297
	}
298

    
299
	usort($config['nat']['rule'], "natcmp");
300
}
301

    
302
function nat_1to1_rules_sort() {
303
	global $g, $config;
304

    
305
	function nat1to1cmp($a, $b) {
306
		return ipcmp($a['external'], $b['external']);
307
	}
308

    
309
	usort($config['nat']['onetoone'], "nat1to1cmp");
310
}
311

    
312
function nat_server_rules_sort() {
313
	global $g, $config;
314

    
315
	function natservercmp($a, $b) {
316
		return ipcmp($a['ipaddr'], $b['ipaddr']);
317
	}
318

    
319
	usort($config['nat']['servernat'], "natservercmp");
320
}
321

    
322
function nat_out_rules_sort() {
323
	global $g, $config;
324

    
325
	function natoutcmp($a, $b) {
326
		return strcmp($a['source']['network'], $b['source']['network']);
327
	}
328

    
329
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
330
}
331

    
332
function pptpd_users_sort() {
333
	global $g, $config;
334

    
335
	function usercmp($a, $b) {
336
		return strcasecmp($a['name'], $b['name']);
337
	}
338

    
339
	usort($config['pptpd']['user'], "usercmp");
340
}
341

    
342
function staticroutes_sort() {
343
	global $g, $config;
344

    
345
	function staticroutecmp($a, $b) {
346
		return strcmp($a['network'], $b['network']);
347
	}
348

    
349
	usort($config['staticroutes']['route'], "staticroutecmp");
350
}
351

    
352
function hosts_sort() {
353
	global $g, $config;
354

    
355
	function hostcmp($a, $b) {
356
		return strcasecmp($a['host'], $b['host']);
357
	}
358

    
359
	usort($config['dnsmasq']['hosts'], "hostcmp");
360
}
361

    
362
function staticmaps_sort($if) {
363
	global $g, $config;
364

    
365
	function staticmapcmp($a, $b) {
366
		return ipcmp($a['ipaddr'], $b['ipaddr']);
367
	}
368

    
369
	usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
370
}
371

    
372
function aliases_sort() {
373
	global $g, $config;
374

    
375
	function aliascmp($a, $b) {
376
		return strcmp($a['name'], $b['name']);
377
	}
378

    
379
	usort($config['aliases']['alias'], "aliascmp");
380
}
381

    
382
function ipsec_mobilekey_sort() {
383
	global $g, $config;
384

    
385
	function mobilekeycmp($a, $b) {
386
		return strcmp($a['ident'][0], $b['ident'][0]);
387
	}
388

    
389
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
390
}
391

    
392
function proxyarp_sort() {
393
	global $g, $config;
394

    
395
	function proxyarpcmp($a, $b) {
396
		if (isset($a['network']))
397
			list($ast,$asn) = explode("/", $a['network']);
398
		else if (isset($a['range'])) {
399
			$ast = $a['range']['from'];
400
			$asn = 32;
401
		}
402
		if (isset($b['network']))
403
			list($bst,$bsn) = explode("/", $b['network']);
404
		else if (isset($b['range'])) {
405
			$bst = $b['range']['from'];
406
			$bsn = 32;
407
		}
408
		if (ipcmp($ast, $bst) == 0)
409
			return ($asn - $bsn);
410
		else
411
			return ipcmp($ast, $bst);
412
	}
413

    
414
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
415
}
416

    
417
function passthrumacs_sort() {
418
	global $g, $config;
419

    
420
	function passthrumacscmp($a, $b) {
421
		return strcmp($a['mac'], $b['mac']);
422
	}
423

    
424
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
425
}
426

    
427
function allowedips_sort() {
428
	global $g, $config;
429

    
430
	function allowedipscmp($a, $b) {
431
		return strcmp($a['ip'], $b['ip']);
432
	}
433

    
434
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
435
}
436

    
437
function wol_sort() {
438
	global $g, $config;
439

    
440
	function wolcmp($a, $b) {
441
		return strcmp($a['descr'], $b['descr']);
442
	}
443

    
444
	usort($config['wol']['wolentry'], "wolcmp");
445
}
446

    
447
function gentitle($pgname) {
448
	global $config;
449
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
450
}
451

    
452
/* update the changedesc and changecount(er) variables */
453
function update_changedesc($update) {
454
	global $changedesc;
455
	global $changecount;
456

    
457
	$changedesc .= " {$update}";
458
	$changecount++;
459
}
460

    
461
function dump_clog($logfile, $tail, $grepfor = "", $grepinvert = false, $withorig = true, $ignorepkgs = true) {
462
        global $g, $config;
463
        $sor = isset($config['syslog']['reverse']) ? "-r" : "";
464
	exec("/usr/sbin/clog {$logfile} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
465
	if(!is_array($grepfor)) $grepfor = array($grepfor);
466
	if(!is_array($grepinvert)) $grepinvert = array($grepinvert);
467
	if($ignorepkgs == true and $config['installedpackages']['package'] != "") {
468
		foreach($config['installedpackages']['package'] as $pkg) {
469
			$pkg_config = parse_xml_config("/usr/local/pkg/" . $pkg['configurationfile'], "packagegui");
470
			if($pkg_config['logging'] != "" and $pkg_config['logging']['grepfor'] != "") {
471
				$grepfor[] = $pkg_config['logging']['grepfor'];
472
				$grepinvert[] = true;
473
			}
474
		}
475
	}
476
        if($grepfor <> "") {
477
		$i = 0;
478
		foreach($grepfor as $agrep) {
479
			$regexp = "/" . $agrep . "/i";
480
                	if($grepinvert[$i] == true) {
481
                		$logarr = preg_grep($regexp, $logarr, PREG_GREP_INVERT);
482
			} else {
483
				$logarr = preg_grep($regexp, $logarr);
484
			}
485
			$i++;
486
		}
487
	}
488

    
489
        foreach ($logarr as $logent) {
490
                $logent = preg_split("/\s+/", $logent, 6);
491
                echo "<tr valign=\"top\">\n";
492
                if ($withorig) {
493
                        echo "<td class=\"listlr\" nowrap>" . htmlspecialchars(join(" ", array_slice($logent, 0, 3))) . "</td>\n";
494
                        echo "<td class=\"listr\">" . htmlspecialchars($logent[4] . " " . $logent[5]) . "</td>\n";
495
                } else {
496
                        echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
497
                }
498
                echo "</tr>\n";
499
        }
500
}
501

    
502
/* Check if variable has changed, update and log if it has
503
 * returns true if var changed
504
 * varname = variable name in plain text
505
 * orig = original value
506
 * new = new value
507
 */
508
function update_if_changed($varname, & $orig, $new) {
509
	if ($orig != $new) {
510
		update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
511
		$orig = $new;
512
		return true;
513
	}
514
	return false;
515
}
516

    
517
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
518

    
519
        if (isset($adr['any']))
520
                $padr = "any";
521
        else if ($adr['network'])
522
                $padr = $adr['network'];
523
        else if ($adr['address']) {
524
                list($padr, $pmask) = explode("/", $adr['address']);
525
                if (!$pmask)
526
                        $pmask = 32;
527
        }
528

    
529
        if (isset($adr['not']))
530
                $pnot = 1;
531
        else
532
                $pnot = 0;
533

    
534
        if ($adr['port']) {
535
                list($pbeginport, $pendport) = explode("-", $adr['port']);
536
                if (!$pendport)
537
                        $pendport = $pbeginport;
538
        } else {
539
                if(alias_expand($pbeginport) <> "" || alias_expand($pendport) <> "") {
540
                        /* Item is a port alias */
541
                } else {
542
                        $pbeginport = "any";
543
                        $pendport = "any";
544
                }
545
        }
546
}
547

    
548

    
549
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
550

    
551
        $adr = array();
552

    
553
        if ($padr == "any")
554
                $adr['any'] = true;
555
        else if (is_specialnet($padr))
556
                $adr['network'] = $padr;
557
        else {
558
                $adr['address'] = $padr;
559
                if ($pmask != 32)
560
                        $adr['address'] .= "/" . $pmask;
561
        }
562

    
563
        if ($pnot)
564
                $adr['not'] = true;
565
        else
566
                unset($addr['not']);
567

    
568
        if (($pbeginport != 0) && ($pbeginport != "any")) {
569
                if ($pbeginport != $pendport)
570
                        $adr['port'] = $pbeginport . "-" . $pendport;
571
                else
572
                        $adr['port'] = $pbeginport;
573
        }
574

    
575
        if(alias_expand($pbeginport)) {
576
                $adr['port'] = $pbeginport;
577
        }
578
}
579

    
580
function is_specialnet($net) {
581
        global $specialsrcdst;
582

    
583
        if (in_array($net, $specialsrcdst) || strstr($net, "opt"))
584
                return true;
585
        else
586
                return false;
587
}
588

    
589
?>
(45-45/117)