Project

General

Profile

Download (17.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
/*
51
 *   if user has selected a custom template, use it.
52
 *   otherwise default to pfsense tempalte
53
 */
54
if($config['theme'] <> "")
55
        $g['theme'] = $config['theme'];
56
else
57
        $g['theme'] = "pfsense";
58

    
59
$d_hostsdirty_path = $g['varrun_path'] . "/hosts.dirty";
60
$d_natconfdirty_path = $g['varrun_path'] . "/nat.conf.dirty";
61
$d_filterconfdirty_path = $g['varrun_path'] . "/filter.conf.dirty";
62
$d_ipsecconfdirty_path = $g['varrun_path'] . "/ipsec.conf.dirty";
63
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
64
$d_pptpuserdirty_path = $g['varrun_path'] . "/pptpd.user.dirty";
65
$d_dnsmasqdirty_path = $g['varrun_path'] . "/dnsmasq.dirty";
66
$d_staticmapsdirty_path = $g['varrun_path'] . "/staticmaps.dirty";
67
$d_staticroutesdirty_path = $g['varrun_path'] . "/staticroutes.dirty";
68
$d_aliasesdirty_path = $g['varrun_path'] . "/aliases.dirty";
69
$d_proxyarpdirty_path = $g['varrun_path'] . "/proxyarp.dirty";
70
$d_fwupenabled_path = $g['varrun_path'] . "/fwup.enabled";
71
$d_firmwarelock_path = $g['varrun_path'] . "/firmware.lock";
72
$d_sysrebootreqd_path = $g['varrun_path'] . "/sysreboot.reqd";
73
$d_passthrumacsdirty_path = $g['varrun_path'] . "/passthrumacs.dirty";
74
$d_allowedipsdirty_path = $g['varrun_path'] . "/allowedips.dirty";
75
$d_ovpnclidirty_path = $g['varrun_path'] . "/ovpnclient.dirty";
76
$d_vipconfdirty_path = $g['varrun_path'] . "/vip.conf.dirty";
77
/* used by progress bar */
78
$lastseen = "-1";
79

    
80
if (file_exists($d_firmwarelock_path)) {
81
	if (!$d_isfwfile) {
82
		header("Location: system_firmware.php");
83
		exit;
84
	} else {
85
		return;
86
	}
87
}
88

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

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

    
95
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
96
	$specialnets['opt' . $i] = $config['interfaces']['opt' . $i]['descr'] . " net";
97
}
98

    
99
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
100
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
101
	"10half" => "10BASE-T half-duplex");
102

    
103
/* platforms that support firmware updating */
104
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'wrap');
105

    
106
/* IPsec defines */
107
$my_identifier_list = array('myaddress' => 'My IP address',
108
				'address' => 'IP address',
109
				'fqdn' => 'Domain name',
110
				'user_fqdn' => 'User FQDN',
111
				'dyn_dns' => 'Dynamic DNS');
112

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

    
123
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
124

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

    
132
	for ($i = 0; $i < count($reqdfields); $i++) {
133
		if (!$_POST[$reqdfields[$i]]) {
134
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
135
		}
136
	}
137
}
138

    
139
function print_input_errors($input_errors) {
140
	global $g;
141
	echo "<p><table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
142
	echo "<tr><td bgcolor=\"#990000\" width=\"36\" align=\"center\" valign=\"top\"><img src=\"./themes/".$g['theme']."/images/icons/icon_error.gif\" width=\"28\" height=\"32\"></td>\n";
143
	echo "<td bgcolor=\"#FFD9D1\" style=\"padding-left: 8px; padding-top: 6px\">";
144

    
145
	echo "<span class=\"errmsg\"><p>The following input errors were detected:<ul>\n";
146
	foreach ($input_errors as $ierr) {
147
		echo "<li>" . htmlspecialchars($ierr) . "</li>\n";
148
	}
149
	echo "</ul></span>";
150

    
151
	echo "</td></tr></table></p>";
152
}
153

    
154
function exec_rc_script($scriptname) {
155

    
156
	global $d_sysrebootreqd_path;
157

    
158
	if (file_exists($d_sysrebootreqd_path))
159
		return 0;
160

    
161
	exec($scriptname . " >/dev/null 2>&1", $execoutput, $retval);
162
	return $retval;
163
}
164

    
165
function exec_rc_script_async($scriptname) {
166

    
167
	global $d_sysrebootreqd_path;
168

    
169
	if (file_exists($d_sysrebootreqd_path))
170
		return 0;
171

    
172
	exec("nohup " . $scriptname . " >/dev/null 2>&1 &", $execoutput, $retval);
173
	return $retval;
174
}
175

    
176
function verify_gzip_file($fname) {
177

    
178
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
179
	if ($returnvar != 0)
180
		return 0;
181
	else
182
		return 1;
183
}
184

    
185
function print_info_box_np($msg) {
186
	global $g;
187
	echo "<table height=\"32\" width=\"100%\">\n";
188
	echo "  <tr>\n";
189
	echo "   <td>\n";
190
	echo "      <div style='background-color:#990000' id='redbox'>\n";
191
	echo "       <table width='100%'><tr><td width='8%'>\n";
192
	echo "        &nbsp;&nbsp;&nbsp;<img style='vertical-align:middle' src=\"./themes/".$g['theme']."/images/icons/icon_exclam.gif\" width=\"28\" height=\"32\">\n";
193
	echo "        </td>\n";
194
	echo "        <td width='70%'><font color='white'><b>{$msg}</b></font>\n";
195
	echo "        </td>";
196
	if(stristr($msg, "apply") == true) {
197
		echo "         <td>";
198
		echo "           <input name=\"apply\" type=\"submit\" class=\"formbtn\" id=\"apply\" value=\"Apply changes\">\n";
199
		echo "         </td>";
200
	}
201
	echo "        </tr></table>\n";
202
	echo "       </div>\n";
203
	echo "    </td>\n";
204
	echo "</table>\n";
205
	echo "<script type=\"text/javascript\">\n";
206
	echo "NiftyCheck();\n";
207
	echo "Rounded(\"div#redbox\",\"all\",\"#FFF\",\"#990000\",\"smooth\");\n";
208
	echo "Rounded(\"td#blackbox\",\"all\",\"#FFF\",\"#000000\",\"smooth\");\n";
209
	echo "</script>\n";
210
	echo "\n<br>\n";
211
}
212

    
213
function print_info_box($msg) {
214
	echo "<p>";
215
	print_info_box_np($msg);
216
	echo "</p>";
217
}
218

    
219
function format_bytes($bytes) {
220
	if ($bytes >= 1073741824) {
221
		return sprintf("%.2f GB", $bytes/1073741824);
222
	} else if ($bytes >= 1048576) {
223
		return sprintf("%.2f MB", $bytes/1048576);
224
	} else if ($bytes >= 1024) {
225
		return sprintf("%.0f KB", $bytes/1024);
226
	} else {
227
		return sprintf("%d bytes", $bytes);
228
	}
229
}
230

    
231
function get_std_save_message($ok) {
232
	global $d_sysrebootreqd_path;
233

    
234
	if ($ok == 0) {
235
		if (file_exists($d_sysrebootreqd_path))
236
			return "The changes have been saved. You must <a color=\"#FFFF00\" href=\"/reboot.php\">reboot</a> your firewall for changes to take effect.";
237
		else
238
			return "The changes have been applied successfully.";
239
	} else {
240
		return "ERROR: the changes could not be applied (error code $ok).";
241
	}
242
}
243

    
244
function pprint_address($adr) {
245
	global $specialnets;
246

    
247
	if (isset($adr['any'])) {
248
		$padr = "*";
249
	} else if ($adr['network']) {
250
		$padr = $specialnets[$adr['network']];
251
	} else {
252
		$padr = $adr['address'];
253
	}
254

    
255
	if (isset($adr['not']))
256
		$padr = "! " . $padr;
257

    
258
	return $padr;
259
}
260

    
261
function pprint_port($port) {
262
	global $wkports;
263

    
264
	$pport = "";
265

    
266
	if (!$port)
267
		echo "*";
268
	else {
269
		$srcport = explode("-", $port);
270
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
271
			$pport = $srcport[0];
272
			if ($wkports[$srcport[0]]) {
273
				$pport .= " (" . $wkports[$srcport[0]] . ")";
274
			}
275
		} else
276
			$pport .= $srcport[0] . " - " . $srcport[1];
277
	}
278

    
279
	return $pport;
280
}
281

    
282
/* sort by interface only, retain the original order of rules that apply to
283
   the same interface */
284
function filter_rules_sort() {
285
	global $config;
286

    
287
	/* mark each rule with the sequence number (to retain the order while sorting) */
288
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
289
		$config['filter']['rule'][$i]['seq'] = $i;
290

    
291
	function filtercmp($a, $b) {
292
		if ($a['interface'] == $b['interface'])
293
			return $a['seq'] - $b['seq'];
294
		else
295
			return -strcmp($a['interface'], $b['interface']);
296
	}
297

    
298
	usort($config['filter']['rule'], "filtercmp");
299

    
300
	/* strip the sequence numbers again */
301
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
302
		unset($config['filter']['rule'][$i]['seq']);
303
}
304

    
305
function nat_rules_sort() {
306
	global $config;
307

    
308
	function natcmp($a, $b) {
309
		if ($a['external-address'] == $b['external-address']) {
310
			if ($a['protocol'] == $b['protocol']) {
311
				if ($a['external-port'] == $b['external-port']) {
312
					return 0;
313
				} else {
314
					return ($a['external-port'] - $b['external-port']);
315
				}
316
			} else {
317
				return strcmp($a['protocol'], $b['protocol']);
318
			}
319
		} else if (!$a['external-address'])
320
			return 1;
321
		else if (!$b['external-address'])
322
			return -1;
323
		else
324
			return ipcmp($a['external-address'], $b['external-address']);
325
	}
326

    
327
	usort($config['nat']['rule'], "natcmp");
328
}
329

    
330
function nat_1to1_rules_sort() {
331
	global $g, $config;
332

    
333
	function nat1to1cmp($a, $b) {
334
		return ipcmp($a['external'], $b['external']);
335
	}
336

    
337
	usort($config['nat']['onetoone'], "nat1to1cmp");
338
}
339

    
340
function nat_server_rules_sort() {
341
	global $g, $config;
342

    
343
	function natservercmp($a, $b) {
344
		return ipcmp($a['ipaddr'], $b['ipaddr']);
345
	}
346

    
347
	usort($config['nat']['servernat'], "natservercmp");
348
}
349

    
350
function nat_out_rules_sort() {
351
	global $g, $config;
352

    
353
	function natoutcmp($a, $b) {
354
		return strcmp($a['source']['network'], $b['source']['network']);
355
	}
356

    
357
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
358
}
359

    
360
function pptpd_users_sort() {
361
	global $g, $config;
362

    
363
	function usercmp($a, $b) {
364
		return strcasecmp($a['name'], $b['name']);
365
	}
366

    
367
	usort($config['pptpd']['user'], "usercmp");
368
}
369

    
370
function staticroutes_sort() {
371
	global $g, $config;
372

    
373
	function staticroutecmp($a, $b) {
374
		return strcmp($a['network'], $b['network']);
375
	}
376

    
377
	usort($config['staticroutes']['route'], "staticroutecmp");
378
}
379

    
380
function hosts_sort() {
381
	global $g, $config;
382

    
383
	function hostcmp($a, $b) {
384
		return strcasecmp($a['host'], $b['host']);
385
	}
386

    
387
	usort($config['dnsmasq']['hosts'], "hostcmp");
388
}
389

    
390
function staticmaps_sort($if) {
391
	global $g, $config;
392

    
393
	function staticmapcmp($a, $b) {
394
		return ipcmp($a['ipaddr'], $b['ipaddr']);
395
	}
396

    
397
	usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
398
}
399

    
400
function aliases_sort() {
401
	global $g, $config;
402

    
403
	function aliascmp($a, $b) {
404
		return strcmp($a['name'], $b['name']);
405
	}
406

    
407
	usort($config['aliases']['alias'], "aliascmp");
408
}
409

    
410
function ipsec_mobilekey_sort() {
411
	global $g, $config;
412

    
413
	function mobilekeycmp($a, $b) {
414
		return strcmp($a['ident'][0], $b['ident'][0]);
415
	}
416

    
417
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
418
}
419

    
420
function proxyarp_sort() {
421
	global $g, $config;
422

    
423
	function proxyarpcmp($a, $b) {
424
		if (isset($a['network']))
425
			list($ast,$asn) = explode("/", $a['network']);
426
		else if (isset($a['range'])) {
427
			$ast = $a['range']['from'];
428
			$asn = 32;
429
		}
430
		if (isset($b['network']))
431
			list($bst,$bsn) = explode("/", $b['network']);
432
		else if (isset($b['range'])) {
433
			$bst = $b['range']['from'];
434
			$bsn = 32;
435
		}
436
		if (ipcmp($ast, $bst) == 0)
437
			return ($asn - $bsn);
438
		else
439
			return ipcmp($ast, $bst);
440
	}
441

    
442
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
443
}
444

    
445
function passthrumacs_sort() {
446
	global $g, $config;
447

    
448
	function passthrumacscmp($a, $b) {
449
		return strcmp($a['mac'], $b['mac']);
450
	}
451

    
452
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
453
}
454

    
455
function allowedips_sort() {
456
	global $g, $config;
457

    
458
	function allowedipscmp($a, $b) {
459
		return strcmp($a['ip'], $b['ip']);
460
	}
461

    
462
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
463
}
464

    
465
function wol_sort() {
466
	global $g, $config;
467

    
468
	function wolcmp($a, $b) {
469
		return strcmp($a['descr'], $b['descr']);
470
	}
471

    
472
	usort($config['wol']['wolentry'], "wolcmp");
473
}
474

    
475
function gentitle($pgname) {
476
	global $config;
477
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
478
}
479

    
480
/* update the changedesc and changecount(er) variables */
481
function update_changedesc($update) {
482
	global $changedesc;
483
	global $changecount;
484

    
485
	$changedesc .= " {$update}";
486
	$changecount++;
487
}
488

    
489
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
490
	global $g, $config;
491
        $sor = isset($config['syslog']['reverse']) ? "-r" : "";
492
	exec("/usr/sbin/clog {$logfile} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
493
	if($grepfor <> "") {  	 
494
                 $i = 0; 	 
495
                 foreach($grepfor as $agrep) { 	 
496
                         $regexp = "/" . $agrep . "/i"; 	 
497
                         if($grepinvert[$i] == true) { 	 
498
                                 $logarr = preg_grep($regexp, $logarr, PREG_GREP_INVERT); 	 
499
                         } else { 	 
500
                                 $logarr = preg_grep($regexp, $logarr); 	 
501
                         } 	 
502
                         $i++; 	 
503
                 } 	 
504
        }
505
        foreach ($logarr as $logent) {
506
                $logent = preg_split("/\s+/", $logent, 6);
507
                echo "<tr valign=\"top\">\n";
508
                if ($withorig) {
509
                        echo "<td class=\"listlr\" nowrap>" . htmlspecialchars(join(" ", array_slice($logent, 0, 3))) . "</td>\n";
510
                        echo "<td class=\"listr\">" . htmlspecialchars($logent[4] . " " . $logent[5]) . "</td>\n";
511
                } else {
512
                        echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
513
                }
514
                echo "</tr>\n";
515
        }
516
}
517

    
518
/* Check if variable has changed, update and log if it has
519
 * returns true if var changed
520
 * varname = variable name in plain text
521
 * orig = original value
522
 * new = new value
523
 */
524
function update_if_changed($varname, & $orig, $new) {
525
	if ($orig != $new) {
526
		update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
527
		$orig = $new;
528
		return true;
529
	}
530
	return false;
531
}
532

    
533
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
534

    
535
        if (isset($adr['any']))
536
                $padr = "any";
537
        else if ($adr['network'])
538
                $padr = $adr['network'];
539
        else if ($adr['address']) {
540
                list($padr, $pmask) = explode("/", $adr['address']);
541
                if (!$pmask)
542
                        $pmask = 32;
543
        }
544

    
545
        if (isset($adr['not']))
546
                $pnot = 1;
547
        else
548
                $pnot = 0;
549

    
550
        if ($adr['port']) {
551
                list($pbeginport, $pendport) = explode("-", $adr['port']);
552
                if (!$pendport)
553
                        $pendport = $pbeginport;
554
        } else {
555
                if(alias_expand($pbeginport) <> "" || alias_expand($pendport) <> "") {
556
                        /* Item is a port alias */
557
                } else {
558
                        $pbeginport = "any";
559
                        $pendport = "any";
560
                }
561
        }
562
}
563

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

    
566
        $adr = array();
567

    
568
        if ($padr == "any")
569
                $adr['any'] = true;
570
        else if (is_specialnet($padr))
571
                $adr['network'] = $padr;
572
        else {
573
                $adr['address'] = $padr;
574
                if ($pmask != 32)
575
                        $adr['address'] .= "/" . $pmask;
576
        }
577

    
578
        if ($pnot)
579
                $adr['not'] = true;
580
        else
581
                unset($adr['not']);
582

    
583
        if (($pbeginport != 0) && ($pbeginport != "any")) {
584
                if ($pbeginport != $pendport)
585
                        $adr['port'] = $pbeginport . "-" . $pendport;
586
                else
587
                        $adr['port'] = $pbeginport;
588
        }
589

    
590
        if(alias_expand($pbeginport)) {
591
                $adr['port'] = $pbeginport;
592
        }
593
}
594

    
595
function is_specialnet($net) {
596
        global $specialsrcdst;
597

    
598
        if (in_array($net, $specialsrcdst) || strstr($net, "opt"))
599
                return true;
600
        else
601
                return false;
602
}
603

    
604
function ipsec_ca_sort() {
605
        global $g, $config;
606

    
607
        function ipseccacmp($a, $b) {
608
                return strcmp($a['ident'], $b['ident']);
609
        }
610

    
611
        usort($config['ipsec']['cacert'], "ipseccacmp");
612
}
613

    
614

    
615
?>
(51-51/134)