Project

General

Profile

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

    
82
/* OpenVPN Directories */
83
$d_ovpnsrvdirty_path = "/tmp/ovpn-srv.dirty";
84
$d_ovpncrldirty_path = "/tmp/ovpn-crl.dirty";
85
$d_ovpnclidirty_path = "/tmp/ovpn-cli.dirty";
86
/* used by progress bar */
87
$lastseen = "-1";
88

    
89
if (file_exists($d_firmwarelock_path)) {
90
	if (!$d_isfwfile) {
91
		header("Location: system_firmware.php");
92
		exit;
93
	} else {
94
		return;
95
	}
96
}
97

    
98
/* some well knows ports */
99
$wkports = array(3389 => "MS RDP",
100
	21 => "FTP",
101
	22 => "SSH",
102
	23 => "Telnet",
103
	25 => "SMTP",
104
	53 => "DNS",
105
	80 => "HTTP",
106
	110 => "POP3",
107
	113 => "IDENT/AUTH",
108
	123 => "NTP",
109
	143 => "IMAP",
110
	443 => "HTTPS",
111
	993 => "IMAP/S");
112

    
113
$specialnets = array("wanip" => "WAN address", "lanip" => "LAN address", "lan" => "LAN net", "pptp" => "PPTP clients");
114

    
115
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
116
	$specialnets['opt' . $i] = $config['interfaces']['opt' . $i]['descr'] . " net";
117
}
118

    
119
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
120
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
121
	"10half" => "10BASE-T half-duplex");
122

    
123
/* platforms that support firmware updating */
124
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'embedded', 'wrap');
125

    
126
/* IPsec defines */
127
$my_identifier_list = array('myaddress' => 'My IP address',
128
				'address' => 'IP address',
129
				'fqdn' => 'Domain name',
130
				'user_fqdn' => 'User FQDN',
131
				'dyn_dns' => 'Dynamic DNS');
132

    
133
$p1_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
134
		'cast128' => 'CAST128','rijndael' => 'Rijndael (AES)', 'rijndael 256' => 'Rijndael 256');
135
$p2_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
136
		'cast128' => 'CAST128', 'rijndael' => 'Rijndael (AES)', 'rijndael 256' => 'Rijndael 256');
137

    
138
$p1_halgos = array('sha1' => 'SHA1', 'md5' => 'MD5');
139
$p1_authentication_methods = array('pre_shared_key' => 'Pre-shared key', 'rsasig' => 'RSA signature');
140
$p2_halgos = array('hmac_sha1' => 'SHA1', 'hmac_md5' => 'MD5');
141
$p2_protos = array('esp' => 'ESP', 'ah' => 'AH');
142
$p2_pfskeygroups = array('0' => 'off', '1' => '1', '2' => '2', '5' => '5');
143

    
144
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
145

    
146
	/* check for bad control characters */
147
	foreach ($postdata as $pn => $pd) {
148
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
149
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
150
		}
151
	}
152

    
153
	for ($i = 0; $i < count($reqdfields); $i++) {
154
		if ($_POST[$reqdfields[$i]] == "") { 
155
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
156
		}
157
	}
158
}
159

    
160
function print_input_errors($input_errors) {
161
	global $g;
162
	echo "<p><table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
163
	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";
164
	echo "<td bgcolor=\"#FFD9D1\" style=\"padding-left: 8px; padding-top: 6px\">";
165

    
166
	echo "<span class=\"errmsg\"><p>The following input errors were detected:<ul>\n";
167
	foreach ($input_errors as $ierr) {
168
		echo "<li>" . htmlspecialchars($ierr) . "</li>\n";
169
	}
170
	echo "</ul></span>";
171

    
172
	echo "</td></tr></table></p>&nbsp;<br>";
173
}
174

    
175
function exec_rc_script($scriptname) {
176

    
177
	global $d_sysrebootreqd_path;
178

    
179
	if (file_exists($d_sysrebootreqd_path))
180
		return 0;
181
	$execoutput = "";
182
	$retval = "";
183
	exec($scriptname . " >/dev/null 2>&1", $execoutput, $retval);
184
	return $retval;
185
}
186

    
187
function exec_rc_script_async($scriptname) {
188

    
189
	global $d_sysrebootreqd_path;
190
	$execoutput = "";
191
	$retval = "";
192
	if (file_exists($d_sysrebootreqd_path))
193
		return 0;
194

    
195
	exec("nohup " . $scriptname . " >/dev/null 2>&1 &", $execoutput, $retval);
196
	return $retval;
197
}
198

    
199
function verify_gzip_file($fname) {
200

    
201
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
202
	if ($returnvar != 0)
203
		return 0;
204
	else
205
		return 1;
206
}
207

    
208
function print_info_box_np($msg) {
209
	global $g;
210
	echo "<table height=\"32\" width=\"100%\">\n";
211
	echo "  <tr>\n";
212
	echo "   <td>\n";
213
	echo "      <div style='background-color:#990000' id='redbox'>\n";
214
	echo "       <table width='100%'><tr><td width='8%'>\n";
215
	echo "        &nbsp;&nbsp;&nbsp;<img style='vertical-align:middle' src=\"/themes/".$g['theme']."/images/icons/icon_exclam.gif\" width=\"28\" height=\"32\">\n";
216
	echo "        </td>\n";
217
	echo "        <td width='70%'><font color='white'><b>{$msg}</b></font>\n";
218
	echo "        </td>";
219
	if(stristr($msg, "apply") == true) {
220
		echo "         <td>";
221
		echo "           <input name=\"apply\" type=\"submit\" class=\"formbtn\" id=\"apply\" value=\"Apply changes\">\n";
222
		echo "         </td>";
223
	}
224
	echo "        </tr></table>\n";
225
	echo "       </div>\n";
226
	echo "    </td>\n";
227
	echo "</table>\n";
228
	echo "<script type=\"text/javascript\">\n";
229
	echo "NiftyCheck();\n";
230
	echo "Rounded(\"div#redbox\",\"all\",\"#FFF\",\"#990000\",\"smooth\");\n";
231
	echo "Rounded(\"td#blackbox\",\"all\",\"#FFF\",\"#000000\",\"smooth\");\n";
232
	echo "</script>\n";
233
	echo "\n<br>\n";
234
}
235

    
236
function print_info_box($msg) {
237
	echo "<p>";
238
	print_info_box_np($msg);
239
	echo "</p>";
240
}
241

    
242
function format_bytes($bytes) {
243
	if ($bytes >= 1073741824) {
244
		return sprintf("%.2f GB", $bytes/1073741824);
245
	} else if ($bytes >= 1048576) {
246
		return sprintf("%.2f MB", $bytes/1048576);
247
	} else if ($bytes >= 1024) {
248
		return sprintf("%.0f KB", $bytes/1024);
249
	} else {
250
		return sprintf("%d bytes", $bytes);
251
	}
252
}
253

    
254
function get_std_save_message($ok) {
255
	global $d_sysrebootreqd_path;
256

    
257
	return "The changes have been applied successfully.  You can also <a href='status_filter_reload.php'>monitor</a> the filter reload progress.";
258
}
259

    
260
function pprint_address($adr) {
261
	global $specialnets;
262

    
263
	if (isset($adr['any'])) {
264
		$padr = "*";
265
	} else if ($adr['network']) {
266
		$padr = $specialnets[$adr['network']];
267
	} else {
268
		$padr = $adr['address'];
269
	}
270

    
271
	if (isset($adr['not']))
272
		$padr = "! " . $padr;
273

    
274
	return $padr;
275
}
276

    
277
function pprint_port($port) {
278
	global $wkports;
279

    
280
	$pport = "";
281

    
282
	if (!$port)
283
		echo "*";
284
	else {
285
		$srcport = explode("-", $port);
286
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
287
			$pport = $srcport[0];
288
			if ($wkports[$srcport[0]]) {
289
				$pport .= " (" . $wkports[$srcport[0]] . ")";
290
			}
291
		} else
292
			$pport .= $srcport[0] . " - " . $srcport[1];
293
	}
294

    
295
	return $pport;
296
}
297

    
298
function captiveportal_users_sort() {
299
	global $g, $config;
300
	
301
	function cpusercmp($a, $b) {
302
		return strcasecmp($a['name'], $b['name']);
303
	}
304
	
305
	usort($config['captiveportal']['user'], "cpusercmp");
306
}
307

    
308
/* sort by interface only, retain the original order of rules that apply to
309
   the same interface */
310
function filter_rules_sort() {
311
	global $config;
312

    
313
	/* mark each rule with the sequence number (to retain the order while sorting) */
314
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
315
		$config['filter']['rule'][$i]['seq'] = $i;
316

    
317
	function filtercmp($a, $b) {
318
		if ($a['interface'] == $b['interface'])
319
			return $a['seq'] - $b['seq'];
320
		else
321
			return -strcmp($a['interface'], $b['interface']);
322
	}
323

    
324
	usort($config['filter']['rule'], "filtercmp");
325

    
326
	/* strip the sequence numbers again */
327
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
328
		unset($config['filter']['rule'][$i]['seq']);
329
}
330

    
331
function nat_rules_sort() {
332
	global $config;
333

    
334
	function natcmp($a, $b) {
335
		if ($a['external-address'] == $b['external-address']) {
336
			if ($a['protocol'] == $b['protocol']) {
337
				if ($a['external-port'] == $b['external-port']) {
338
					return 0;
339
				} else {
340
					return ($a['external-port'] - $b['external-port']);
341
				}
342
			} else {
343
				return strcmp($a['protocol'], $b['protocol']);
344
			}
345
		} else if (!$a['external-address'])
346
			return 1;
347
		else if (!$b['external-address'])
348
			return -1;
349
		else
350
			return ipcmp($a['external-address'], $b['external-address']);
351
	}
352

    
353
	usort($config['nat']['rule'], "natcmp");
354
}
355

    
356
function nat_1to1_rules_sort() {
357
	global $g, $config;
358

    
359
	function nat1to1cmp($a, $b) {
360
		return ipcmp($a['external'], $b['external']);
361
	}
362

    
363
	usort($config['nat']['onetoone'], "nat1to1cmp");
364
}
365

    
366
function nat_server_rules_sort() {
367
	global $g, $config;
368

    
369
	function natservercmp($a, $b) {
370
		return ipcmp($a['ipaddr'], $b['ipaddr']);
371
	}
372

    
373
	usort($config['nat']['servernat'], "natservercmp");
374
}
375

    
376
function nat_out_rules_sort() {
377
	global $g, $config;
378

    
379
	function natoutcmp($a, $b) {
380
		return strcmp($a['source']['network'], $b['source']['network']);
381
	}
382

    
383
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
384
}
385

    
386
function pptpd_users_sort() {
387
	global $g, $config;
388

    
389
	function usercmp($a, $b) {
390
		return strcasecmp($a['name'], $b['name']);
391
	}
392

    
393
	usort($config['pptpd']['user'], "usercmp");
394
}
395

    
396
function pppoe_users_sort() {
397
	global $g, $config;
398

    
399
	function usercmp($a, $b) {
400
		return strcasecmp($a['name'], $b['name']);
401
	}
402

    
403
	usort($config['pppoe']['user'], "usercmp");
404
}
405

    
406
function staticroutes_sort() {
407
	global $g, $config;
408

    
409
	function staticroutecmp($a, $b) {
410
		return strcmp($a['network'], $b['network']);
411
	}
412

    
413
	usort($config['staticroutes']['route'], "staticroutecmp");
414
}
415

    
416
function hosts_sort() {
417
	global $g, $config;
418

    
419
	function hostcmp($a, $b) {
420
		return strcasecmp($a['host'], $b['host']);
421
	}
422

    
423
	usort($config['dnsmasq']['hosts'], "hostcmp");
424
}
425

    
426
function staticmaps_sort($if) {
427
	global $g, $config;
428

    
429
	function staticmapcmp($a, $b) {
430
		return ipcmp($a['ipaddr'], $b['ipaddr']);
431
	}
432

    
433
	usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
434
}
435

    
436
function aliases_sort() {
437
	global $g, $config;
438

    
439
	function aliascmp($a, $b) {
440
		return strcmp($a['name'], $b['name']);
441
	}
442

    
443
	usort($config['aliases']['alias'], "aliascmp");
444
}
445

    
446
function ipsec_mobilekey_sort() {
447
	global $g, $config;
448

    
449
	function mobilekeycmp($a, $b) {
450
		return strcmp($a['ident'][0], $b['ident'][0]);
451
	}
452

    
453
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
454
}
455

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

    
459
	function proxyarpcmp($a, $b) {
460
		if (isset($a['network']))
461
			list($ast,$asn) = explode("/", $a['network']);
462
		else if (isset($a['range'])) {
463
			$ast = $a['range']['from'];
464
			$asn = 32;
465
		}
466
		if (isset($b['network']))
467
			list($bst,$bsn) = explode("/", $b['network']);
468
		else if (isset($b['range'])) {
469
			$bst = $b['range']['from'];
470
			$bsn = 32;
471
		}
472
		if (ipcmp($ast, $bst) == 0)
473
			return ($asn - $bsn);
474
		else
475
			return ipcmp($ast, $bst);
476
	}
477

    
478
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
479
}
480

    
481
function passthrumacs_sort() {
482
	global $g, $config;
483

    
484
	function passthrumacscmp($a, $b) {
485
		return strcmp($a['mac'], $b['mac']);
486
	}
487

    
488
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
489
}
490

    
491
function cpelements_sort() {
492
	global $g, $config;
493

    
494
	function cpelementscmp($a, $b) {
495
		return strcasecmp($a['name'], $b['name']);
496
	}
497
	
498
	usort($config['captiveportal']['element'],"cpelementscmp");
499
}
500

    
501
function allowedips_sort() {
502
	global $g, $config;
503

    
504
	function allowedipscmp($a, $b) {
505
		return strcmp($a['ip'], $b['ip']);
506
	}
507

    
508
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
509
}
510

    
511
function wol_sort() {
512
	global $g, $config;
513

    
514
	function wolcmp($a, $b) {
515
		return strcmp($a['descr'], $b['descr']);
516
	}
517

    
518
	usort($config['wol']['wolentry'], "wolcmp");
519
}
520

    
521
function sort_rule_by_queue_priority() {
522
	global $g, $config;
523
	global $queues;
524

    
525
	function rqpcmp($a, $b) {
526
		global $queues;
527

    
528
		$ra = $queues[$a['outqueue']];
529
		$rb = $queues[$b['outqueue']];
530

    
531
		if ($ra == $rb)
532
			return 0;
533
		/* reverse sort - we want higher priority first */	
534
		return ($ra < $rb) ? 1 : -1;
535
	}
536

    
537
	foreach($config['shaper']['queue'] as $q) {
538
		$queues[$q['name']] = $q['priority'];
539
	}
540

    
541
	usort($config['shaper']['rule'], "rqpcmp");
542
}
543
function gentitle($pgname) {
544
	global $config;
545
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
546
}
547

    
548
/* update the changedesc and changecount(er) variables */
549
function update_changedesc($update) {
550
	global $changedesc;
551
	global $changecount;
552

    
553
	$changedesc .= " {$update}";
554
	$changecount++;
555
}
556

    
557
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
558
	global $g, $config;
559
    $sor = isset($config['syslog']['reverse']) ? "-r" : "";
560
    $logarr = "";
561
	exec("/usr/sbin/clog {$logfile} | grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
562

    
563
	if(is_array($grepfor)) {
564
		 $i = 0;
565
                 foreach($grepfor as $agrep) { 	 
566
                         $regexp = "/" . $agrep . "/i"; 	 
567
                         if($grepinvert[$i]) { 	 
568
                                 $logarr = preg_grep($regexp, $logarr, PREG_GREP_INVERT); 	 
569
                         } else { 	 
570
                                 $logarr = preg_grep($regexp, $logarr); 	 
571
                         }
572
			 $i++;
573
                 } 	 
574
        }
575
        foreach ($logarr as $logent) {
576
                $logent = preg_split("/\s+/", $logent, 6);
577
                echo "<tr valign=\"top\">\n";
578
                if ($withorig) {
579
                		$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
580
                		if(strlen($entry_date_time) > 17) 
581
                			continue;
582
                        echo "<td class=\"listlr\" nowrap>" . $entry_date_time  . "</td>\n";
583
                        echo "<td class=\"listr\">" . htmlspecialchars($logent[4] . " " . $logent[5]) . "</td>\n";
584
                } else {
585
                        echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
586
                }
587
                echo "</tr>\n";
588
        }
589
}
590

    
591
function return_clog($logfile, $tail, $grepfor = "", $grepinvert = "", $grepreverse = false) {
592
	global $g, $config;
593
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
594
	$sor = isset($grepreverse) ? "-r" : "";
595
	$logarr = "";
596
	exec("/usr/sbin/clog {$logfile} | grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
597

    
598
	if(is_array($grepfor)) {
599
		$i = 0;
600
		foreach($grepfor as $agrep) {
601
			$regexp = "/" . $agrep . "/i";
602
			if($grepinvert[$i]) { 	 
603
				$logarr = preg_grep($regexp, $logarr, PREG_GREP_INVERT); 	 
604
			} else {
605
				$logarr = preg_grep($regexp, $logarr); 	 
606
			}
607
			$i++;
608
		}
609
	}
610
	$i = 0;
611
	foreach ($logarr as $logent) {
612
		$logarray[$i] = "$logent";
613
		$i++;
614
	}
615
	return($logarray);
616
}
617

    
618
/* Check if variable has changed, update and log if it has
619
 * returns true if var changed
620
 * varname = variable name in plain text
621
 * orig = original value
622
 * new = new value
623
 */
624
function update_if_changed($varname, & $orig, $new) {
625
	if (is_array($orig) && is_array($new)) {
626
		$a_diff = array_diff($orig, $new);
627
		foreach ($a_diff as $diff) {
628
			update_changedesc("removed {$varname}: \"{$diff}\"");
629
		}
630
		$a_diff = array_diff($new, $orig);
631
		foreach ($a_diff as $diff) {
632
			update_changedesc("added {$varname}: \"{$diff}\"");
633
		}
634
		$orig = $new;
635
		return true;
636
			
637
	} else { 
638
		if ($orig != $new) {
639
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
640
			$orig = $new;
641
			return true;
642
		}
643
	}
644
	return false;
645
}
646

    
647
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
648

    
649
        if (isset($adr['any']))
650
                $padr = "any";
651
        else if ($adr['network'])
652
                $padr = $adr['network'];
653
        else if ($adr['address']) {
654
                list($padr, $pmask) = explode("/", $adr['address']);
655
                if (!$pmask)
656
                        $pmask = 32;
657
        }
658

    
659
        if (isset($adr['not']))
660
                $pnot = 1;
661
        else
662
                $pnot = 0;
663

    
664
        if ($adr['port']) {
665
                list($pbeginport, $pendport) = explode("-", $adr['port']);
666
                if (!$pendport)
667
                        $pendport = $pbeginport;
668
        } else {
669
                if(alias_expand($pbeginport) <> "" || alias_expand($pendport) <> "") {
670
                        /* Item is a port alias */
671
                } else {
672
                        $pbeginport = "any";
673
                        $pendport = "any";
674
                }
675
        }
676
}
677

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

    
680
        $adr = array();
681

    
682
        if ($padr == "any")
683
                $adr['any'] = true;
684
        else if (is_specialnet($padr))
685
                $adr['network'] = $padr;
686
        else {
687
                $adr['address'] = $padr;
688
                if ($pmask != 32)
689
                        $adr['address'] .= "/" . $pmask;
690
        }
691

    
692
        if ($pnot)
693
                $adr['not'] = true;
694
        else
695
                unset($adr['not']);
696

    
697
        if (($pbeginport != 0) && ($pbeginport != "any")) {
698
                if ($pbeginport != $pendport)
699
                        $adr['port'] = $pbeginport . "-" . $pendport;
700
                else
701
                        $adr['port'] = $pbeginport;
702
        }
703

    
704
        if(alias_expand($pbeginport)) {
705
                $adr['port'] = $pbeginport;
706
        }
707
}
708

    
709
function is_specialnet($net) {
710
        global $specialsrcdst;
711

    
712
        if (in_array($net, $specialsrcdst) || strstr($net, "opt"))
713
                return true;
714
        else
715
                return false;
716
}
717

    
718
function ipsec_ca_sort() {
719
        global $g, $config;
720

    
721
        function ipseccacmp($a, $b) {
722
                return strcmp($a['ident'], $b['ident']);
723
        }
724

    
725
        usort($config['ipsec']['cacert'], "ipseccacmp");
726
}
727

    
728

    
729
?>
(57-57/164)