Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

    
118
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
119

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

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

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

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

    
145
	echo "</td></tr></table></p>";
146
}
147

    
148
function exec_rc_script($scriptname) {
149

    
150
	global $d_sysrebootreqd_path;
151

    
152
	if (file_exists($d_sysrebootreqd_path))
153
		return 0;
154

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

    
159
function exec_rc_script_async($scriptname) {
160

    
161
	global $d_sysrebootreqd_path;
162

    
163
	if (file_exists($d_sysrebootreqd_path))
164
		return 0;
165

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

    
170
function verify_gzip_file($fname) {
171

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

    
179
function print_info_box_np($msg) {
180
	echo "<table height=\"32\" width=\"100%\">\n";
181
	echo "  <tr>";
182
	echo "   <td>";
183
	echo "      <div style='background-color:#990000' id='redbox'>";
184
	echo "       &nbsp;&nbsp;&nbsp;<img style='vertical-align:middle' src=\"/exclam.gif\" width=\"28\" height=\"32\">";
185
	echo "         &nbsp;&nbsp;&nbsp;<font color='white'><b>{$msg}</b></font>";
186
	echo "      </div>";
187
	echo "    </td>";
188
	echo "</table>";
189
	echo "<br>";
190
	echo "<script type=\"text/javascript\">";
191
	echo "NiftyCheck();\n";
192
	echo "Rounded(\"div#redbox\",\"all\",\"#FFF\",\"#990000\",\"smooth\");\n";
193
	echo "Rounded(\"td#blackbox\",\"all\",\"#FFF\",\"#000000\",\"smooth\");\n";
194
	echo "</script>";	
195
}
196

    
197
function print_info_box($msg) {
198
	echo "<p>";
199
	print_info_box_np($msg);
200
	echo "</p>";
201
}
202

    
203
function format_bytes($bytes) {
204
	if ($bytes >= 1073741824) {
205
		return sprintf("%.2f GB", $bytes/1073741824);
206
	} else if ($bytes >= 1048576) {
207
		return sprintf("%.2f MB", $bytes/1048576);
208
	} else if ($bytes >= 1024) {
209
		return sprintf("%.0f KB", $bytes/1024);
210
	} else {
211
		return sprintf("%d bytes", $bytes);
212
	}
213
}
214

    
215
function get_std_save_message($ok) {
216
	global $d_sysrebootreqd_path;
217

    
218
	if ($ok == 0) {
219
		if (file_exists($d_sysrebootreqd_path))
220
			return "The changes have been saved. You must <a color=\"#FFFF00\" href=\"/reboot.php\">reboot</a> your firewall for changes to take effect.";
221
		else
222
			return "The changes have been applied successfully.";
223
	} else {
224
		return "ERROR: the changes could not be applied (error code $ok).";
225
	}
226
}
227

    
228
function pprint_address($adr) {
229
	global $specialnets;
230

    
231
	if (isset($adr['any'])) {
232
		$padr = "*";
233
	} else if ($adr['network']) {
234
		$padr = $specialnets[$adr['network']];
235
	} else {
236
		$padr = $adr['address'];
237
	}
238

    
239
	if (isset($adr['not']))
240
		$padr = "! " . $padr;
241

    
242
	return $padr;
243
}
244

    
245
function pprint_port($port) {
246
	global $wkports;
247

    
248
	$pport = "";
249

    
250
	if (!$port)
251
		echo "*";
252
	else {
253
		$srcport = explode("-", $port);
254
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
255
			$pport = $srcport[0];
256
			if ($wkports[$srcport[0]]) {
257
				$pport .= " (" . $wkports[$srcport[0]] . ")";
258
			}
259
		} else
260
			$pport .= $srcport[0] . " - " . $srcport[1];
261
	}
262

    
263
	return $pport;
264
}
265

    
266
/* sort by interface only, retain the original order of rules that apply to
267
   the same interface */
268
function filter_rules_sort() {
269
	global $g, $config;
270

    
271
	/* mark each rule with the sequence number (to retain the order while sorting) */
272
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
273
		$config['filter']['rule'][$i]['seq'] = $i;
274

    
275
	function filtercmp($a, $b) {
276
		if ($a['interface'] == $b['interface'])
277
			return $a['seq'] - $b['seq'];
278
		else
279
			return -strcmp($a['interface'], $b['interface']);
280
	}
281

    
282
	usort($config['filter']['rule'], "filtercmp");
283

    
284
	/* strip the sequence numbers again */
285
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
286
		unset($config['filter']['rule'][$i]['seq']);
287
}
288

    
289
function nat_rules_sort() {
290
	global $g, $config;
291

    
292
	function natcmp($a, $b) {
293
		if ($a['external-address'] == $b['external-address']) {
294
			if ($a['protocol'] == $b['protocol']) {
295
				if ($a['external-port'] == $b['external-port']) {
296
					return 0;
297
				} else {
298
					return ($a['external-port'] - $b['external-port']);
299
				}
300
			} else {
301
				return strcmp($a['protocol'], $b['protocol']);
302
			}
303
		} else if (!$a['external-address'])
304
			return 1;
305
		else if (!$b['external-address'])
306
			return -1;
307
		else
308
			return ipcmp($a['external-address'], $b['external-address']);
309
	}
310

    
311
	usort($config['nat']['rule'], "natcmp");
312
}
313

    
314
function nat_1to1_rules_sort() {
315
	global $g, $config;
316

    
317
	function nat1to1cmp($a, $b) {
318
		return ipcmp($a['external'], $b['external']);
319
	}
320

    
321
	usort($config['nat']['onetoone'], "nat1to1cmp");
322
}
323

    
324
function nat_server_rules_sort() {
325
	global $g, $config;
326

    
327
	function natservercmp($a, $b) {
328
		return ipcmp($a['ipaddr'], $b['ipaddr']);
329
	}
330

    
331
	usort($config['nat']['servernat'], "natservercmp");
332
}
333

    
334
function nat_out_rules_sort() {
335
	global $g, $config;
336

    
337
	function natoutcmp($a, $b) {
338
		return strcmp($a['source']['network'], $b['source']['network']);
339
	}
340

    
341
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
342
}
343

    
344
function pptpd_users_sort() {
345
	global $g, $config;
346

    
347
	function usercmp($a, $b) {
348
		return strcasecmp($a['name'], $b['name']);
349
	}
350

    
351
	usort($config['pptpd']['user'], "usercmp");
352
}
353

    
354
function staticroutes_sort() {
355
	global $g, $config;
356

    
357
	function staticroutecmp($a, $b) {
358
		return strcmp($a['network'], $b['network']);
359
	}
360

    
361
	usort($config['staticroutes']['route'], "staticroutecmp");
362
}
363

    
364
function hosts_sort() {
365
	global $g, $config;
366

    
367
	function hostcmp($a, $b) {
368
		return strcasecmp($a['host'], $b['host']);
369
	}
370

    
371
	usort($config['dnsmasq']['hosts'], "hostcmp");
372
}
373

    
374
function staticmaps_sort($if) {
375
	global $g, $config;
376

    
377
	function staticmapcmp($a, $b) {
378
		return ipcmp($a['ipaddr'], $b['ipaddr']);
379
	}
380

    
381
	usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
382
}
383

    
384
function aliases_sort() {
385
	global $g, $config;
386

    
387
	function aliascmp($a, $b) {
388
		return strcmp($a['name'], $b['name']);
389
	}
390

    
391
	usort($config['aliases']['alias'], "aliascmp");
392
}
393

    
394
function ipsec_mobilekey_sort() {
395
	global $g, $config;
396

    
397
	function mobilekeycmp($a, $b) {
398
		return strcmp($a['ident'][0], $b['ident'][0]);
399
	}
400

    
401
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
402
}
403

    
404
function proxyarp_sort() {
405
	global $g, $config;
406

    
407
	function proxyarpcmp($a, $b) {
408
		if (isset($a['network']))
409
			list($ast,$asn) = explode("/", $a['network']);
410
		else if (isset($a['range'])) {
411
			$ast = $a['range']['from'];
412
			$asn = 32;
413
		}
414
		if (isset($b['network']))
415
			list($bst,$bsn) = explode("/", $b['network']);
416
		else if (isset($b['range'])) {
417
			$bst = $b['range']['from'];
418
			$bsn = 32;
419
		}
420
		if (ipcmp($ast, $bst) == 0)
421
			return ($asn - $bsn);
422
		else
423
			return ipcmp($ast, $bst);
424
	}
425

    
426
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
427
}
428

    
429
function passthrumacs_sort() {
430
	global $g, $config;
431

    
432
	function passthrumacscmp($a, $b) {
433
		return strcmp($a['mac'], $b['mac']);
434
	}
435

    
436
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
437
}
438

    
439
function allowedips_sort() {
440
	global $g, $config;
441

    
442
	function allowedipscmp($a, $b) {
443
		return strcmp($a['ip'], $b['ip']);
444
	}
445

    
446
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
447
}
448

    
449
function wol_sort() {
450
	global $g, $config;
451

    
452
	function wolcmp($a, $b) {
453
		return strcmp($a['descr'], $b['descr']);
454
	}
455

    
456
	usort($config['wol']['wolentry'], "wolcmp");
457
}
458

    
459
function gentitle($pgname) {
460
	global $config;
461
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
462
}
463

    
464
/* update the changedesc and changecount(er) variables */
465
function update_changedesc($update) {
466
	global $changedesc;
467
	global $changecount;
468

    
469
	$changedesc .= " {$update}";
470
	$changecount++;
471
}
472

    
473
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
474
	global $g, $config;
475
        $sor = isset($config['syslog']['reverse']) ? "-r" : "";
476
	exec("/usr/sbin/clog {$logfile} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
477
	if($grepfor <> "") {  	 
478
                 $i = 0; 	 
479
                 foreach($grepfor as $agrep) { 	 
480
                         $regexp = "/" . $agrep . "/i"; 	 
481
                         if($grepinvert[$i] == true) { 	 
482
                                 $logarr = preg_grep($regexp, $logarr, PREG_GREP_INVERT); 	 
483
                         } else { 	 
484
                                 $logarr = preg_grep($regexp, $logarr); 	 
485
                         } 	 
486
                         $i++; 	 
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
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
549

    
550
        $adr = array();
551

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

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

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

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

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

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

    
588
function ipsec_ca_sort() {
589
        global $g, $config;
590

    
591
        function ipseccacmp($a, $b) {
592
                return strcmp($a['ident'], $b['ident']);
593
        }
594

    
595
        usort($config['ipsec']['cacert'], "ipseccacmp");
596
}
597

    
598

    
599
?>
(46-46/127)