Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

    
140
function print_input_errors($input_errors) {
141
	global $g;
142
	echo "<p><table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
143
	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";
144
	echo "<td bgcolor=\"#FFD9D1\" style=\"padding-left: 8px; padding-top: 6px\">";
145

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

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

    
155
function exec_rc_script($scriptname) {
156

    
157
	global $d_sysrebootreqd_path;
158

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

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

    
166
function exec_rc_script_async($scriptname) {
167

    
168
	global $d_sysrebootreqd_path;
169

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

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

    
177
function verify_gzip_file($fname) {
178

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

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

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

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

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

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

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

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

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

    
259
	return $padr;
260
}
261

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

    
265
	$pport = "";
266

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

    
280
	return $pport;
281
}
282

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
371
function pppoe_users_sort() {
372
	global $g, $config;
373

    
374
	function usercmp($a, $b) {
375
		return strcasecmp($a['name'], $b['name']);
376
	}
377

    
378
	usort($config['pppoe']['user'], "usercmp");
379
}
380

    
381
function staticroutes_sort() {
382
	global $g, $config;
383

    
384
	function staticroutecmp($a, $b) {
385
		return strcmp($a['network'], $b['network']);
386
	}
387

    
388
	usort($config['staticroutes']['route'], "staticroutecmp");
389
}
390

    
391
function hosts_sort() {
392
	global $g, $config;
393

    
394
	function hostcmp($a, $b) {
395
		return strcasecmp($a['host'], $b['host']);
396
	}
397

    
398
	usort($config['dnsmasq']['hosts'], "hostcmp");
399
}
400

    
401
function staticmaps_sort($if) {
402
	global $g, $config;
403

    
404
	function staticmapcmp($a, $b) {
405
		return ipcmp($a['ipaddr'], $b['ipaddr']);
406
	}
407

    
408
	usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
409
}
410

    
411
function aliases_sort() {
412
	global $g, $config;
413

    
414
	function aliascmp($a, $b) {
415
		return strcmp($a['name'], $b['name']);
416
	}
417

    
418
	usort($config['aliases']['alias'], "aliascmp");
419
}
420

    
421
function ipsec_mobilekey_sort() {
422
	global $g, $config;
423

    
424
	function mobilekeycmp($a, $b) {
425
		return strcmp($a['ident'][0], $b['ident'][0]);
426
	}
427

    
428
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
429
}
430

    
431
function proxyarp_sort() {
432
	global $g, $config;
433

    
434
	function proxyarpcmp($a, $b) {
435
		if (isset($a['network']))
436
			list($ast,$asn) = explode("/", $a['network']);
437
		else if (isset($a['range'])) {
438
			$ast = $a['range']['from'];
439
			$asn = 32;
440
		}
441
		if (isset($b['network']))
442
			list($bst,$bsn) = explode("/", $b['network']);
443
		else if (isset($b['range'])) {
444
			$bst = $b['range']['from'];
445
			$bsn = 32;
446
		}
447
		if (ipcmp($ast, $bst) == 0)
448
			return ($asn - $bsn);
449
		else
450
			return ipcmp($ast, $bst);
451
	}
452

    
453
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
454
}
455

    
456
function passthrumacs_sort() {
457
	global $g, $config;
458

    
459
	function passthrumacscmp($a, $b) {
460
		return strcmp($a['mac'], $b['mac']);
461
	}
462

    
463
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
464
}
465

    
466
function allowedips_sort() {
467
	global $g, $config;
468

    
469
	function allowedipscmp($a, $b) {
470
		return strcmp($a['ip'], $b['ip']);
471
	}
472

    
473
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
474
}
475

    
476
function wol_sort() {
477
	global $g, $config;
478

    
479
	function wolcmp($a, $b) {
480
		return strcmp($a['descr'], $b['descr']);
481
	}
482

    
483
	usort($config['wol']['wolentry'], "wolcmp");
484
}
485

    
486
function gentitle($pgname) {
487
	global $config;
488
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
489
}
490

    
491
/* update the changedesc and changecount(er) variables */
492
function update_changedesc($update) {
493
	global $changedesc;
494
	global $changecount;
495

    
496
	$changedesc .= " {$update}";
497
	$changecount++;
498
}
499

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

    
529
/* Check if variable has changed, update and log if it has
530
 * returns true if var changed
531
 * varname = variable name in plain text
532
 * orig = original value
533
 * new = new value
534
 */
535
function update_if_changed($varname, & $orig, $new) {
536
	if ($orig != $new) {
537
		update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
538
		$orig = $new;
539
		return true;
540
	}
541
	return false;
542
}
543

    
544
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
545

    
546
        if (isset($adr['any']))
547
                $padr = "any";
548
        else if ($adr['network'])
549
                $padr = $adr['network'];
550
        else if ($adr['address']) {
551
                list($padr, $pmask) = explode("/", $adr['address']);
552
                if (!$pmask)
553
                        $pmask = 32;
554
        }
555

    
556
        if (isset($adr['not']))
557
                $pnot = 1;
558
        else
559
                $pnot = 0;
560

    
561
        if ($adr['port']) {
562
                list($pbeginport, $pendport) = explode("-", $adr['port']);
563
                if (!$pendport)
564
                        $pendport = $pbeginport;
565
        } else {
566
                if(alias_expand($pbeginport) <> "" || alias_expand($pendport) <> "") {
567
                        /* Item is a port alias */
568
                } else {
569
                        $pbeginport = "any";
570
                        $pendport = "any";
571
                }
572
        }
573
}
574

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

    
577
        $adr = array();
578

    
579
        if ($padr == "any")
580
                $adr['any'] = true;
581
        else if (is_specialnet($padr))
582
                $adr['network'] = $padr;
583
        else {
584
                $adr['address'] = $padr;
585
                if ($pmask != 32)
586
                        $adr['address'] .= "/" . $pmask;
587
        }
588

    
589
        if ($pnot)
590
                $adr['not'] = true;
591
        else
592
                unset($adr['not']);
593

    
594
        if (($pbeginport != 0) && ($pbeginport != "any")) {
595
                if ($pbeginport != $pendport)
596
                        $adr['port'] = $pbeginport . "-" . $pendport;
597
                else
598
                        $adr['port'] = $pbeginport;
599
        }
600

    
601
        if(alias_expand($pbeginport)) {
602
                $adr['port'] = $pbeginport;
603
        }
604
}
605

    
606
function is_specialnet($net) {
607
        global $specialsrcdst;
608

    
609
        if (in_array($net, $specialsrcdst) || strstr($net, "opt"))
610
                return true;
611
        else
612
                return false;
613
}
614

    
615
function ipsec_ca_sort() {
616
        global $g, $config;
617

    
618
        function ipseccacmp($a, $b) {
619
                return strcmp($a['ident'], $b['ident']);
620
        }
621

    
622
        usort($config['ipsec']['cacert'], "ipseccacmp");
623
}
624

    
625

    
626
?>
(51-51/137)