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
/* parse the configuration and include all configuration functions */
43
require_once("config.inc");
44
require_once("functions.inc");
45

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

    
65
if (file_exists($d_firmwarelock_path)) {
66
	if (!$d_isfwfile) {
67
		header("Location: system_firmware.php");
68
		exit;
69
	} else {
70
		return;
71
	}
72
}
73

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

    
78
/* IP TOS flags */
79
$iptos = array("lowdelay", "throughput", "reliability", "congestion");
80

    
81
/* TCP flags */
82
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg");
83

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

    
86
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
87
	$specialnets['opt' . $i] = $config['interfaces']['opt' . $i]['descr'] . " net";
88
}
89

    
90
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
91
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
92
	"10half" => "10BASE-T half-duplex");
93

    
94
/* platforms that support firmware updating */
95
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'wrap');
96

    
97
/* IPsec defines */
98
$my_identifier_list = array('myaddress' => 'My IP address',
99
							'address' => 'IP address',
100
							'fqdn' => 'Domain name',
101
							'user_fqdn' => 'User FQDN');
102

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

    
112
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
113

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

    
121
	for ($i = 0; $i < count($reqdfields); $i++) {
122
		if (!$_POST[$reqdfields[$i]]) {
123
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
124
		}
125
	}
126
}
127

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

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

    
139
	echo "</td></tr></table></p>";
140
}
141

    
142
function exec_rc_script($scriptname) {
143

    
144
	global $d_sysrebootreqd_path;
145

    
146
	if (file_exists($d_sysrebootreqd_path))
147
		return 0;
148

    
149
	exec($scriptname . " >/dev/null 2>&1", $execoutput, $retval);
150
	return $retval;
151
}
152

    
153
function exec_rc_script_async($scriptname) {
154

    
155
	global $d_sysrebootreqd_path;
156

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

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

    
164
function verify_gzip_file($fname) {
165

    
166
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
167
	if ($returnvar != 0)
168
		return 0;
169
	else
170
		return 1;
171
}
172

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

    
181
function print_info_box($msg) {
182
	echo "<p>";
183
	print_info_box_np($msg);
184
	echo "</p>";
185
}
186

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

    
199
function get_std_save_message($ok) {
200
	global $d_sysrebootreqd_path;
201

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

    
212
function pprint_address($adr) {
213
	global $specialnets;
214

    
215
	if (isset($adr['any'])) {
216
		$padr = "*";
217
	} else if ($adr['network']) {
218
		$padr = $specialnets[$adr['network']];
219
	} else {
220
		$padr = $adr['address'];
221
	}
222

    
223
	if (isset($adr['not']))
224
		$padr = "! " . $padr;
225

    
226
	return $padr;
227
}
228

    
229
function pprint_port($port) {
230
	global $wkports;
231

    
232
	$pport = "";
233

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

    
247
	return $pport;
248
}
249

    
250
/* sort by interface only, retain the original order of rules that apply to
251
   the same interface */
252
function filter_rules_sort() {
253
	global $g, $config;
254

    
255
	/* mark each rule with the sequence number (to retain the order while sorting) */
256
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
257
		$config['filter']['rule'][$i]['seq'] = $i;
258

    
259
	function filtercmp($a, $b) {
260
		if ($a['interface'] == $b['interface'])
261
			return $a['seq'] - $b['seq'];
262
		else
263
			return -strcmp($a['interface'], $b['interface']);
264
	}
265

    
266
	usort($config['filter']['rule'], "filtercmp");
267

    
268
	/* strip the sequence numbers again */
269
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
270
		unset($config['filter']['rule'][$i]['seq']);
271
}
272

    
273
function nat_rules_sort() {
274
	global $g, $config;
275

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

    
295
	usort($config['nat']['rule'], "natcmp");
296
}
297

    
298
function nat_1to1_rules_sort() {
299
	global $g, $config;
300

    
301
	function nat1to1cmp($a, $b) {
302
		return ipcmp($a['external'], $b['external']);
303
	}
304

    
305
	usort($config['nat']['onetoone'], "nat1to1cmp");
306
}
307

    
308
function nat_server_rules_sort() {
309
	global $g, $config;
310

    
311
	function natservercmp($a, $b) {
312
		return ipcmp($a['ipaddr'], $b['ipaddr']);
313
	}
314

    
315
	usort($config['nat']['servernat'], "natservercmp");
316
}
317

    
318
function nat_out_rules_sort() {
319
	global $g, $config;
320

    
321
	function natoutcmp($a, $b) {
322
		return strcmp($a['source']['network'], $b['source']['network']);
323
	}
324

    
325
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
326
}
327

    
328
function pptpd_users_sort() {
329
	global $g, $config;
330

    
331
	function usercmp($a, $b) {
332
		return strcasecmp($a['name'], $b['name']);
333
	}
334

    
335
	usort($config['pptpd']['user'], "usercmp");
336
}
337

    
338
function staticroutes_sort() {
339
	global $g, $config;
340

    
341
	function staticroutecmp($a, $b) {
342
		return strcmp($a['network'], $b['network']);
343
	}
344

    
345
	usort($config['staticroutes']['route'], "staticroutecmp");
346
}
347

    
348
function hosts_sort() {
349
	global $g, $config;
350

    
351
	function hostcmp($a, $b) {
352
		return strcasecmp($a['host'], $b['host']);
353
	}
354

    
355
	usort($config['dnsmasq']['hosts'], "hostcmp");
356
}
357

    
358
function staticmaps_sort($if) {
359
	global $g, $config;
360

    
361
	function staticmapcmp($a, $b) {
362
		return ipcmp($a['ipaddr'], $b['ipaddr']);
363
	}
364

    
365
	usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
366
}
367

    
368
function aliases_sort() {
369
	global $g, $config;
370

    
371
	function aliascmp($a, $b) {
372
		return strcmp($a['name'], $b['name']);
373
	}
374

    
375
	usort($config['aliases']['alias'], "aliascmp");
376
}
377

    
378
function ipsec_mobilekey_sort() {
379
	global $g, $config;
380

    
381
	function mobilekeycmp($a, $b) {
382
		return strcmp($a['ident'][0], $b['ident'][0]);
383
	}
384

    
385
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
386
}
387

    
388
function proxyarp_sort() {
389
	global $g, $config;
390

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

    
410
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
411
}
412

    
413
function passthrumacs_sort() {
414
	global $g, $config;
415

    
416
	function passthrumacscmp($a, $b) {
417
		return strcmp($a['mac'], $b['mac']);
418
	}
419

    
420
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
421
}
422

    
423
function allowedips_sort() {
424
	global $g, $config;
425

    
426
	function allowedipscmp($a, $b) {
427
		return strcmp($a['ip'], $b['ip']);
428
	}
429

    
430
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
431
}
432

    
433
function wol_sort() {
434
	global $g, $config;
435

    
436
	function wolcmp($a, $b) {
437
		return strcmp($a['descr'], $b['descr']);
438
	}
439

    
440
	usort($config['wol']['wolentry'], "wolcmp");
441
}
442

    
443
function gentitle($pgname) {
444
	global $config;
445
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
446
}
447

    
448
/* update the changedesc and changecount(er) variables */
449
function update_changedesc($update) {
450
	global $changedesc;
451
	global $changecount;
452

    
453
	$changedesc .= " {$update}";
454
	$changecount++;
455
}
456

    
457
function dump_clog($logfile, $tail, $grepfor = "", $grepinvert = false, $withorig = true) {
458
        global $g, $config;
459

    
460
        $sor = isset($config['syslog']['reverse']) ? "-r" : "";
461

    
462
	exec("/usr/sbin/clog {$logfile} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
463
	if(!is_array($grepfor)) $grepfor = array($grepfor);
464
        if($grepfor <> "") {
465
		$i = 0;
466
		foreach($grepfor as $agrep) {
467
			$regexp = "/" . $agrep . "/i";
468
                	if($grepinvert[$i] == true) {
469
                		$logarr = preg_grep($regexp, $logarr, PREG_GREP_INVERT);
470
			} else {
471
				$logarr = preg_grep($regexp, $logarr);
472
			}
473
			$i++;
474
		}
475
	}
476

    
477
        foreach ($logarr as $logent) {
478
                $logent = preg_split("/\s+/", $logent, 6);
479
                echo "<tr valign=\"top\">\n";
480
                if ($withorig) {
481
                        echo "<td class=\"listlr\" nowrap>" . htmlspecialchars(join(" ", array_slice($logent, 0, 3))) . "</td>\n";
482
                        echo "<td class=\"listr\">" . htmlspecialchars($logent[4] . " " . $logent[5]) . "</td>\n";
483
                } else {
484
                        echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
485
                }
486
                echo "</tr>\n";
487
        }
488
}
489

    
490
/* Check if variable has changed, update and log if it has
491
 * returns true if var changed
492
 * varname = variable name in plain text
493
 * orig = original value
494
 * new = new value
495
 */
496
function update_if_changed($varname, & $orig, $new) {
497
	if ($orig != $new) {
498
		update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
499
		$orig = $new;
500
		return true;
501
	}
502
	return false;
503
}
504

    
505
function download_file_with_progress_bar($url_file, $destination_file) {
506
        global $ch, $fout, $file_size, $downloaded, $counter;
507
        $file_size  = 1;
508
        $downloaded = 1;
509
        /* open destination file */
510
        $fout = fopen($destination_file, "wb");
511
                
512
        /*
513
                Originally by Author: Keyvan Minoukadeh
514
                Modified by Scott Ullrich to return Content-Length size
515
        */
516
                        
517
        $ch = curl_init();
518
        curl_setopt($ch, CURLOPT_URL, $url_file);
519
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
520
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
521
        curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
522
                
523
        curl_exec($ch);
524
        fclose($fout);
525
        return 1;
526

    
527
        if ($error = curl_error($ch)) {
528
            return -1;
529
        }
530
}
531

    
532
function read_header($ch, $string) {
533
        global $file_size, $ch, $fout;
534
        $length = strlen($string);
535
        ereg("(Content-Length:) (.*)", $string, $regs);
536
        if($regs[2] <> "") {
537
                $file_size = intval($regs[2]);
538
        }
539
        return $length;
540
}
541

    
542
function read_body($ch, $string) {
543
        global $fout, $file_size, $downloaded, $counter, $sendto, $static_output, $lastseen;
544
        $length = strlen($string);
545
        $downloaded += intval($length);
546
        $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
547
        $downloadProgress = 100 - $downloadProgress;
548
	/*
549
	   lastseen is used to prevent from spamming firefox with hundreds of
550
	   unnecessary javascript update messages which sends the clients
551
           firefox utilization to 100%
552
        */
553
        if($lastseen <> $downloadProgress and $downloadProgress < 101) {
554
		if($sendto == "status") {
555
                	$tostatus = $static_status . $downloadProgress . "%";
556
                	update_status($tostatus);
557
		} else {
558
			$tooutput = $static_output . $downloadProgress . "%"; 
559
			update_output_window($tooutput);
560
		}
561
                update_progress_bar($downloadProgress);
562
                $lastseen = $downloadProgress;
563
        }
564
        fwrite($fout, $string);
565
        return $length;
566
}
567

    
568
function pkg_fetch_recursive($pkgname, $filename, $dependlevel = 0, $base_url = 'http://ftp2.freebsd.org/pub/FreeBSD/ports/i386/packages-5-stable/Latest') {
569
	global $static_status, $static_output, $g, $fd_log;
570
	$pkg_extension = strrchr($filename, '.');
571
	$static_output .= "\n" . str_repeat(" ", $dependlevel * 2) . $pkgname . " ";
572
	$fetchto = "/tmp/apkg_" . $pkgname . $pkg_extension;
573
        download_file_with_progress_bar($base_url . "/" . $filename, $fetchto);
574
//	update_output_window($static_output . "\n\n" . $pkg_progress);
575
        exec("/usr/bin/tar -O -f {$fetchto} -x +CONTENTS", $slaveout);
576
        $workingdir = preg_grep("/instmp/", $slaveout);
577
        $workingdir = $workingdir[0];
578
        $raw_depends_list = array_values(preg_grep("/\@pkgdep/", $slaveout));
579
	if($raw_depends_list != "") {
580
                foreach($raw_depends_list as $adepend) {
581
			$working_depend = explode(" ", $adepend);
582
                        $working_depend = explode("-", $working_depend[1]);
583
			$depend_filename = $working_depend[0] . $pkg_extension;
584
			exec("ls /var/db/pkg", $is_installed);
585
			$is_installed = array_values(preg_grep("/{$working_depend[0]}/i", $is_installed));
586
			if($is_installed[0] == "") pkg_fetch_recursive($working_depend[0], $depend_filename, $dependlevel + 1);
587
		}
588
        }
589
	exec("cat {$g['tmp_path']}/y | /usr/sbin/pkg_add -v {$fetchto} 2>&1", $pkgaddout);
590
	fwrite($fd_log, $pkgname . " " . print_r($pkgaddout, true) . "\n");
591
	return true;
592
}
593
?>
(42-42/111)