Project

General

Profile

Download (17.5 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
$d_vipconfdirty_path = $g['varrun_path'] . "/vip.conf.dirty";
68
/* used by progress bar */
69
$lastseen = "-1";
70

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

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

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

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

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

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

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

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

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

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

    
120
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
121

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

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

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

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

    
147
	echo "</td></tr></table></p>";
148
}
149

    
150
function exec_rc_script($scriptname) {
151

    
152
	global $d_sysrebootreqd_path;
153

    
154
	if (file_exists($d_sysrebootreqd_path))
155
		return 0;
156

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

    
161
function exec_rc_script_async($scriptname) {
162

    
163
	global $d_sysrebootreqd_path;
164

    
165
	if (file_exists($d_sysrebootreqd_path))
166
		return 0;
167

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

    
172
function verify_gzip_file($fname) {
173

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

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

    
208
function print_info_box($msg) {
209
	echo "<p>";
210
	print_info_box_np($msg);
211
	echo "</p>";
212
}
213

    
214
function format_bytes($bytes) {
215
	if ($bytes >= 1073741824) {
216
		return sprintf("%.2f GB", $bytes/1073741824);
217
	} else if ($bytes >= 1048576) {
218
		return sprintf("%.2f MB", $bytes/1048576);
219
	} else if ($bytes >= 1024) {
220
		return sprintf("%.0f KB", $bytes/1024);
221
	} else {
222
		return sprintf("%d bytes", $bytes);
223
	}
224
}
225

    
226
function get_std_save_message($ok) {
227
	global $d_sysrebootreqd_path;
228

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

    
239
function pprint_address($adr) {
240
	global $specialnets;
241

    
242
	if (isset($adr['any'])) {
243
		$padr = "*";
244
	} else if ($adr['network']) {
245
		$padr = $specialnets[$adr['network']];
246
	} else {
247
		$padr = $adr['address'];
248
	}
249

    
250
	if (isset($adr['not']))
251
		$padr = "! " . $padr;
252

    
253
	return $padr;
254
}
255

    
256
function pprint_port($port) {
257
	global $wkports;
258

    
259
	$pport = "";
260

    
261
	if (!$port)
262
		echo "*";
263
	else {
264
		$srcport = explode("-", $port);
265
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
266
			$pport = $srcport[0];
267
			if ($wkports[$srcport[0]]) {
268
				$pport .= " (" . $wkports[$srcport[0]] . ")";
269
			}
270
		} else
271
			$pport .= $srcport[0] . " - " . $srcport[1];
272
	}
273

    
274
	return $pport;
275
}
276

    
277
/* sort by interface only, retain the original order of rules that apply to
278
   the same interface */
279
function filter_rules_sort() {
280
	global $config;
281

    
282
	/* mark each rule with the sequence number (to retain the order while sorting) */
283
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
284
		$config['filter']['rule'][$i]['seq'] = $i;
285

    
286
	function filtercmp($a, $b) {
287
		if ($a['interface'] == $b['interface'])
288
			return $a['seq'] - $b['seq'];
289
		else
290
			return -strcmp($a['interface'], $b['interface']);
291
	}
292

    
293
	usort($config['filter']['rule'], "filtercmp");
294

    
295
	/* strip the sequence numbers again */
296
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
297
		unset($config['filter']['rule'][$i]['seq']);
298
}
299

    
300
function nat_rules_sort() {
301
	global $config;
302

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

    
322
	usort($config['nat']['rule'], "natcmp");
323
}
324

    
325
function nat_1to1_rules_sort() {
326
	global $g, $config;
327

    
328
	function nat1to1cmp($a, $b) {
329
		return ipcmp($a['external'], $b['external']);
330
	}
331

    
332
	usort($config['nat']['onetoone'], "nat1to1cmp");
333
}
334

    
335
function nat_server_rules_sort() {
336
	global $g, $config;
337

    
338
	function natservercmp($a, $b) {
339
		return ipcmp($a['ipaddr'], $b['ipaddr']);
340
	}
341

    
342
	usort($config['nat']['servernat'], "natservercmp");
343
}
344

    
345
function nat_out_rules_sort() {
346
	global $g, $config;
347

    
348
	function natoutcmp($a, $b) {
349
		return strcmp($a['source']['network'], $b['source']['network']);
350
	}
351

    
352
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
353
}
354

    
355
function pptpd_users_sort() {
356
	global $g, $config;
357

    
358
	function usercmp($a, $b) {
359
		return strcasecmp($a['name'], $b['name']);
360
	}
361

    
362
	usort($config['pptpd']['user'], "usercmp");
363
}
364

    
365
function staticroutes_sort() {
366
	global $g, $config;
367

    
368
	function staticroutecmp($a, $b) {
369
		return strcmp($a['network'], $b['network']);
370
	}
371

    
372
	usort($config['staticroutes']['route'], "staticroutecmp");
373
}
374

    
375
function hosts_sort() {
376
	global $g, $config;
377

    
378
	function hostcmp($a, $b) {
379
		return strcasecmp($a['host'], $b['host']);
380
	}
381

    
382
	usort($config['dnsmasq']['hosts'], "hostcmp");
383
}
384

    
385
function staticmaps_sort($if) {
386
	global $g, $config;
387

    
388
	function staticmapcmp($a, $b) {
389
		return ipcmp($a['ipaddr'], $b['ipaddr']);
390
	}
391

    
392
	usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
393
}
394

    
395
function aliases_sort() {
396
	global $g, $config;
397

    
398
	function aliascmp($a, $b) {
399
		return strcmp($a['name'], $b['name']);
400
	}
401

    
402
	usort($config['aliases']['alias'], "aliascmp");
403
}
404

    
405
function ipsec_mobilekey_sort() {
406
	global $g, $config;
407

    
408
	function mobilekeycmp($a, $b) {
409
		return strcmp($a['ident'][0], $b['ident'][0]);
410
	}
411

    
412
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
413
}
414

    
415
function proxyarp_sort() {
416
	global $g, $config;
417

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

    
437
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
438
}
439

    
440
function passthrumacs_sort() {
441
	global $g, $config;
442

    
443
	function passthrumacscmp($a, $b) {
444
		return strcmp($a['mac'], $b['mac']);
445
	}
446

    
447
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
448
}
449

    
450
function allowedips_sort() {
451
	global $g, $config;
452

    
453
	function allowedipscmp($a, $b) {
454
		return strcmp($a['ip'], $b['ip']);
455
	}
456

    
457
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
458
}
459

    
460
function wol_sort() {
461
	global $g, $config;
462

    
463
	function wolcmp($a, $b) {
464
		return strcmp($a['descr'], $b['descr']);
465
	}
466

    
467
	usort($config['wol']['wolentry'], "wolcmp");
468
}
469

    
470
function gentitle($pgname) {
471
	global $config;
472
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
473
}
474

    
475
/* update the changedesc and changecount(er) variables */
476
function update_changedesc($update) {
477
	global $changedesc;
478
	global $changecount;
479

    
480
	$changedesc .= " {$update}";
481
	$changecount++;
482
}
483

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

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

    
528
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
529

    
530
        if (isset($adr['any']))
531
                $padr = "any";
532
        else if ($adr['network'])
533
                $padr = $adr['network'];
534
        else if ($adr['address']) {
535
                list($padr, $pmask) = explode("/", $adr['address']);
536
                if (!$pmask)
537
                        $pmask = 32;
538
        }
539

    
540
        if (isset($adr['not']))
541
                $pnot = 1;
542
        else
543
                $pnot = 0;
544

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

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

    
561
        $adr = array();
562

    
563
        if ($padr == "any")
564
                $adr['any'] = true;
565
        else if (is_specialnet($padr))
566
                $adr['network'] = $padr;
567
        else {
568
                $adr['address'] = $padr;
569
                if ($pmask != 32)
570
                        $adr['address'] .= "/" . $pmask;
571
        }
572

    
573
        if ($pnot)
574
                $adr['not'] = true;
575
        else
576
                unset($adr['not']);
577

    
578
        if (($pbeginport != 0) && ($pbeginport != "any")) {
579
                if ($pbeginport != $pendport)
580
                        $adr['port'] = $pbeginport . "-" . $pendport;
581
                else
582
                        $adr['port'] = $pbeginport;
583
        }
584

    
585
        if(alias_expand($pbeginport)) {
586
                $adr['port'] = $pbeginport;
587
        }
588
}
589

    
590
function is_specialnet($net) {
591
        global $specialsrcdst;
592

    
593
        if (in_array($net, $specialsrcdst) || strstr($net, "opt"))
594
                return true;
595
        else
596
                return false;
597
}
598

    
599
function ipsec_ca_sort() {
600
        global $g, $config;
601

    
602
        function ipseccacmp($a, $b) {
603
                return strcmp($a['ident'], $b['ident']);
604
        }
605

    
606
        usort($config['ipsec']['cacert'], "ipseccacmp");
607
}
608

    
609

    
610
?>
(51-51/133)