Project

General

Profile

Download (48.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * syslog.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally part of m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
require_once('config.inc');
29

    
30
global $buffer_rules_rdr, $buffer_rules_normal;
31
$buffer_rules_rdr = array();
32
$buffer_rules_normal = array();
33

    
34
global $syslog_formats;
35
$syslog_formats = array(
36
	'rfc3164' => gettext('BSD (RFC 3164, default)'),
37
	'rfc5424' => gettext('syslog (RFC 5424, with RFC 3339 microsecond-precision timestamps)'),
38
);
39

    
40
function system_syslogd_fixup_server($server) {
41
	/* If it's an IPv6 IP alone, encase it in brackets */
42
	if (is_ipaddrv6($server)) {
43
		return "[$server]";
44
	} else {
45
		return $server;
46
	}
47
}
48

    
49
function system_syslogd_get_remote_servers($syslogcfg, $facility = "*.*") {
50
	// Rather than repeatedly use the same code, use this function to build a list of remote servers.
51
	$facility .= " ".
52
	$remote_servers = "";
53
	$pad_to  = max(strlen($facility), 56);
54
	$padding = ceil(($pad_to - strlen($facility))/8)+1;
55
	if (isset($syslogcfg['enable'])) {
56
		if ($syslogcfg['remoteserver']) {
57
			$remote_servers .= "{$facility}" . str_repeat("\t", $padding) . "@" . system_syslogd_fixup_server($syslogcfg['remoteserver']) . "\n";
58
		}
59
		if ($syslogcfg['remoteserver2']) {
60
			$remote_servers .= "{$facility}" . str_repeat("\t", $padding) . "@" . system_syslogd_fixup_server($syslogcfg['remoteserver2']) . "\n";
61
		}
62
		if ($syslogcfg['remoteserver3']) {
63
			$remote_servers .= "{$facility}" . str_repeat("\t", $padding) . "@" . system_syslogd_fixup_server($syslogcfg['remoteserver3']) . "\n";
64
		}
65
	}
66
	return $remote_servers;
67
}
68

    
69
function system_syslogd_get_all_logfilenames($add_extension = false) {
70
	global $config, $g, $system_log_files;
71

    
72
	if ($add_extension) {
73
		$all_syslog_files = array();
74
		foreach ($system_log_files as $syslogfile) {
75
			$all_syslog_files[] = "{$syslogfile}.log";
76
		}
77
	} else {
78
		$all_syslog_files = $system_log_files;
79
	}
80

    
81
	if ($config['installedpackages']['package']) {
82
		foreach ($config['installedpackages']['package'] as $package) {
83
			if (isset($package['logging']['logfilename'])) {
84
				$all_syslog_files[] = $package['logging']['logfilename'];
85
			}
86
		}
87
	}
88

    
89
	return $all_syslog_files;
90
}
91

    
92
/* For a given log filename prefix, return a list with the filename and
93
 * rotated copies sorted in a way that utilities such as cat/bzcat/bzgrep will
94
 * see all log entries in chronological order (e.g. name.log.2 name.log.1 name.log)
95
 */
96
function sort_related_log_files($logfile = "/var/log/system.log", $string = true, $escape = false) {
97
	$related_files = glob("{$logfile}*");
98
	usort($related_files, "rsort_log_filename");
99

    
100
	if ($escape) {
101
		$related_files = array_map("escapeshellarg", $related_files);
102
	}
103

    
104
	if ($string) {
105
		$related_files = implode(" ", $related_files);
106
	}
107

    
108
	return $related_files;
109
}
110

    
111
function rsort_log_filename($a, $b) {
112
	/* Break the filename up into components by '.', it could be in one
113
	 * Of the following formats:
114
	 * Compressed:   /var/log/<name>.log.<number>.<compressed extension>
115
	 * Uncompressed: /var/log/<name>.log.<number>
116
	 *
117
	 * See: https://redmine.pfsense.org/issues/11639
118
	 */
119
	$aparts = explode('.', $a);
120
	/* If the last part is not a number, it's a compressed log extension,
121
	 * pop it off. */
122
	if (!is_numeric($aparts[count($aparts)-1])) {
123
		array_pop($aparts);
124
	}
125

    
126
	$bparts = explode('.', $b);
127
	if (!is_numeric($bparts[count($bparts)-1])) {
128
		array_pop($bparts);
129
	}
130

    
131
	/* Compare the last entry in each filename array which should now be
132
	 * the log number */
133
	return ($aparts[count($aparts)-1] > $bparts[count($bparts)-1]) ? -1 : 1;
134
}
135

    
136
function clear_log_file($logfile = "/var/log/system.log", $restart_syslogd = true, $remove_rotated = true) {
137
	global $config, $g;
138

    
139
	if ($restart_syslogd) {
140
		if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
141
			sigkillbypid("{$g['varrun_path']}/syslog.pid", "KILL");
142
		}
143
	}
144
	exec("/usr/bin/truncate -s 0 " . escapeshellarg($logfile));
145
	if ($remove_rotated) {
146
		unlink_if_exists("{$logfile}.*");
147
	}
148
	if ($restart_syslogd) {
149
		system_syslogd_start();
150
	}
151
	// Bug #6915
152
	if ($logfile == "/var/log/resolver.log") {
153
		services_unbound_configure(true);
154
	}
155
}
156

    
157
function clear_all_log_files($restart = false) {
158
	global $g, $system_log_files, $system_log_non_syslog_files;
159
	if ($restart) {
160
		if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
161
			sigkillbypid("{$g['varrun_path']}/syslog.pid", "KILL");
162
		}
163
	}
164

    
165
	/* Combine list of syslog and non-syslog log files */
166
	/* exclude dmesg.boot, see https://redmine.pfsense.org/issues/11428 */
167
	foreach (array_merge(system_syslogd_get_all_logfilenames(true), array_diff($system_log_non_syslog_files, array('dmesg.boot'))) as $lfile) {
168
		clear_log_file("{$g['varlog_path']}/{$lfile}", false);
169
	}
170

    
171
	if ($restart) {
172
		system_syslogd_start();
173
		killbyname("dhcpd");
174
		if (!function_exists('services_dhcpd_configure')) {
175
			require_once('services.inc');
176
		}
177
		services_dhcpd_configure();
178
		// Bug #6915
179
		services_unbound_configure(false);
180
	}
181
	return;
182
}
183

    
184
function system_syslogd_start($sighup = false) {
185
	global $config, $g;
186
	if (isset($config['system']['developerspew'])) {
187
		$mt = microtime();
188
		echo "system_syslogd_start() being called $mt\n";
189
	}
190

    
191
	mwexec("/etc/rc.d/hostid start");
192

    
193
	$syslogcfg = $config['syslog'];
194

    
195
	if (platform_booting()) {
196
		echo gettext("Starting syslog...");
197
	}
198

    
199
	$mainsyslogconf = <<<EOD
200
# Automatically generated, do not edit!
201
# Place configuration files in {$g['varetc_path']}/syslog.d
202
!*\n
203
include						{$g['varetc_path']}/syslog.d
204
# /* Manually added files with non-conflicting names will not be automatically removed */
205

    
206
EOD;
207

    
208
	if (!@file_put_contents("{$g['etc_path']}/syslog.conf", $mainsyslogconf)) {
209
		printf(gettext("Error: cannot open syslog.conf in system_syslogd_start().%s"), "\n");
210
		unset($syslogconf);
211
		return 1;
212
	}
213
	safe_mkdir("{$g['varetc_path']}/syslog.d");
214

    
215
	$syslogd_extra = "";
216
	if (isset($syslogcfg)) {
217
		$separatelogfacilities = array('ntp', 'ntpd', 'ntpdate', 'charon', 'ipsec_starter', 'openvpn', 'poes', 'l2tps', 'hostapd', 'dnsmasq', 'named', 'filterdns', 'unbound', 'dhcpd', 'dhcrelay', 'dhclient', 'dhcp6c', 'dpinger', 'radvd', 'routed', 'zebra', 'ospfd', 'ospf6d', 'bgpd', 'watchfrr', 'miniupnpd', 'igmpproxy', 'filterlog');
218
		$syslogconf = "# Automatically generated, do not edit!\n";
219

    
220
		if ($config['installedpackages']['package']) {
221
			foreach ($config['installedpackages']['package'] as $package) {
222
				if (isset($package['logging']['facilityname']) && isset($package['logging']['logfilename'])) {
223
					array_push($separatelogfacilities, $package['logging']['facilityname']);
224
					if (!is_file($g['varlog_path'].'/'.$package['logging']['logfilename'])) {
225
						mwexec("/usr/bin/touch {$g['varlog_path']}/{$package['logging']['logfilename']}");
226
					}
227
					$pkgsyslogconf = "# Automatically generated for package {$package['name']}. Do not edit.\n";
228
					$pkgsyslogconf .= "!{$package['logging']['facilityname']}\n*.*\t\t\t\t\t\t {$g['varlog_path']}/{$package['logging']['logfilename']}\n";
229
					@file_put_contents("{$g['varetc_path']}/syslog.d/" . basename($package['logging']['logfilename']) . ".conf", $pkgsyslogconf);
230
				}
231
			}
232
		}
233
		$facilitylist = implode(',', array_unique($separatelogfacilities));
234

    
235
		$syslogconf .= "!*\n";
236
		if (!isset($syslogcfg['disablelocallogging'])) {
237
			$syslogconf .= "auth.*;authpriv.* 						{$g['varlog_path']}/auth.log\n";
238
		}
239
		if (isset($syslogcfg['auth'])) {
240
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "auth.*;authpriv.*");
241
		}
242

    
243
		$syslogconf .= "!radvd,routed,zebra,ospfd,ospf6d,bgpd,watchfrr,miniupnpd,igmpproxy\n";
244
		if (!isset($syslogcfg['disablelocallogging'])) {
245
			$syslogconf .= "*.*								{$g['varlog_path']}/routing.log\n";
246
		}
247
		if (isset($syslogcfg['routing'])) {
248
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
249
		}
250

    
251
		$syslogconf .= "!ntp,ntpd,ntpdate\n";
252
		if (!isset($syslogcfg['disablelocallogging'])) {
253
			$syslogconf .= "*.*								{$g['varlog_path']}/ntpd.log\n";
254
		}
255
		if (isset($syslogcfg['ntpd'])) {
256
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
257
		}
258

    
259
		$syslogconf .= "!ppp\n";
260
		if (!isset($syslogcfg['disablelocallogging'])) {
261
			$syslogconf .= "*.*								{$g['varlog_path']}/ppp.log\n";
262
		}
263
		if (isset($syslogcfg['ppp'])) {
264
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
265
		}
266

    
267
		$syslogconf .= "!poes\n";
268
		if (!isset($syslogcfg['disablelocallogging'])) {
269
			$syslogconf .= "*.*								{$g['varlog_path']}/poes.log\n";
270
		}
271
		if (isset($syslogcfg['vpn'])) {
272
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
273
		}
274

    
275
		$syslogconf .= "!l2tps\n";
276
		if (!isset($syslogcfg['disablelocallogging'])) {
277
			$syslogconf .= "*.*								{$g['varlog_path']}/l2tps.log\n";
278
		}
279
		if (isset($syslogcfg['vpn'])) {
280
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
281
		}
282

    
283
		$syslogconf .= "!charon,ipsec_starter\n";
284
		if (!isset($syslogcfg['disablelocallogging'])) {
285
			$syslogconf .= "*.*								{$g['varlog_path']}/ipsec.log\n";
286
		}
287
		if (isset($syslogcfg['vpn'])) {
288
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
289
		}
290

    
291
		$syslogconf .= "!openvpn\n";
292
		if (!isset($syslogcfg['disablelocallogging'])) {
293
			$syslogconf .= "*.*								{$g['varlog_path']}/openvpn.log\n";
294
		}
295
		if (isset($syslogcfg['vpn'])) {
296
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
297
		}
298

    
299
		$syslogconf .= "!dpinger\n";
300
		if (!isset($syslogcfg['disablelocallogging'])) {
301
			$syslogconf .= "*.*								{$g['varlog_path']}/gateways.log\n";
302
		}
303
		if (isset($syslogcfg['dpinger'])) {
304
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
305
		}
306

    
307
		$syslogconf .= "!dnsmasq,named,filterdns,unbound\n";
308
		if (!isset($syslogcfg['disablelocallogging'])) {
309
			$syslogconf .= "*.*								{$g['varlog_path']}/resolver.log\n";
310
		}
311
		if (isset($syslogcfg['resolver'])) {
312
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
313
		}
314

    
315
		$syslogconf .= "!dhcpd,dhcrelay,dhclient,dhcp6c,dhcpleases,dhcpleases6\n";
316
		if (!isset($syslogcfg['disablelocallogging'])) {
317
			$syslogconf .= "*.*								{$g['varlog_path']}/dhcpd.log\n";
318
		}
319
		if (isset($syslogcfg['dhcp'])) {
320
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
321
		}
322

    
323
		$syslogconf .= "!hostapd\n";
324
		if (!isset($syslogcfg['disablelocallogging'])) {
325
			$syslogconf .= "*.* 								{$g['varlog_path']}/wireless.log\n";
326
		}
327
		if (isset($syslogcfg['hostapd'])) {
328
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
329
		}
330

    
331
		$syslogconf .= "!filterlog\n";
332
		if (!isset($syslogcfg['disablelocallogging'])) {
333
			$syslogconf .= "*.* 								{$g['varlog_path']}/filter.log\n";
334
		}
335
		if (isset($syslogcfg['filter'])) {
336
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
337
		}
338

    
339
		$syslogconf .= "!logportalauth\n";
340
		if (!isset($syslogcfg['disablelocallogging'])) {
341
			$syslogconf .= "*.* 								{$g['varlog_path']}/portalauth.log\n";
342
		}
343
		if (isset($syslogcfg['portalauth'])) {
344
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
345
		}
346

    
347
		$syslogconf .= "!-{$facilitylist}\n";
348
		if (!isset($syslogcfg['disablelocallogging'])) {
349
			$syslogconf .= <<<EOD
350
local3.*							{$g['varlog_path']}/vpn.log
351
local5.*							{$g['varlog_path']}/nginx.log
352
*.notice;kern.debug;lpr.info;mail.crit;daemon.none;news.err;local0.none;local3.none;local4.none;local7.none;security.*;auth.info;authpriv.info;daemon.info	{$g['varlog_path']}/system.log
353
auth.info;authpriv.info 					|exec /usr/local/sbin/sshguard -i /var/run/sshguard.pid
354
*.emerg								*
355

    
356
EOD;
357
		}
358
		if (isset($syslogcfg['vpn'])) {
359
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "local3.*");
360
		}
361
		if (isset($syslogcfg['system'])) {
362
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.emerg;*.notice;kern.debug;lpr.info;mail.crit;news.err;local0.none;local3.none;local7.none;security.*;auth.info;authpriv.info;daemon.info");
363
		}
364
		if (isset($syslogcfg['logall'])) {
365
			// Make everything mean everything, including facilities excluded above.
366
			$syslogconf .= "!*\n";
367
			$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
368
		}
369

    
370
		if (isset($syslogcfg['zmqserver'])) {
371
				$syslogconf .= <<<EOD
372
*.*								^{$syslogcfg['zmqserver']}
373

    
374
EOD;
375
		}
376
		/* write syslog config to pfSense.conf */
377
		if (!@file_put_contents("{$g['varetc_path']}/syslog.d/pfSense.conf", $syslogconf)) {
378
			printf(gettext("Error: cannot open syslog config file in system_syslogd_start().%s"), "\n");
379
			unset($syslogconf);
380
			return 1;
381
		}
382
		unset($syslogconf);
383

    
384
		$sourceip = "";
385
		if (!empty($syslogcfg['sourceip'])) {
386
			if ($syslogcfg['ipproto'] == "ipv6") {
387
				$ifaddr = is_ipaddr($syslogcfg['sourceip']) ? $syslogcfg['sourceip'] : get_interface_ipv6($syslogcfg['sourceip']);
388
				if (!is_ipaddr($ifaddr)) {
389
					$ifaddr = get_interface_ip($syslogcfg['sourceip']);
390
				}
391
			} else {
392
				$ifaddr = is_ipaddr($syslogcfg['sourceip']) ? $syslogcfg['sourceip'] : get_interface_ip($syslogcfg['sourceip']);
393
				if (!is_ipaddr($ifaddr)) {
394
					$ifaddr = get_interface_ipv6($syslogcfg['sourceip']);
395
				}
396
			}
397
			if (is_ipaddr($ifaddr)) {
398
				$sourceip = "-b {$ifaddr}";
399
			}
400
		}
401

    
402
		$syslogd_extra = "-f {$g['etc_path']}/syslog.conf {$sourceip}";
403
	}
404

    
405
	$log_sockets = array("{$g['dhcpd_chroot_path']}/var/run/log");
406

    
407
	if (isset($config['installedpackages']['package'])) {
408
		foreach ($config['installedpackages']['package'] as $package) {
409
			if (isset($package['logging']['logsocket']) && $package['logging']['logsocket'] != '' &&
410
			    !in_array($package['logging']['logsocket'], $log_sockets)) {
411
				$log_sockets[] = $package['logging']['logsocket'];
412
			}
413
		}
414
	}
415

    
416
	$syslogd_format = empty($config['syslog']['format']) ? '' : '-O ' . escapeshellarg($config['syslog']['format']);
417

    
418
	$syslogd_sockets = "";
419
	foreach ($log_sockets as $log_socket) {
420
		// Ensure that the log directory exists
421
		$logpath = dirname($log_socket);
422
		safe_mkdir($logpath);
423
		$syslogd_sockets .= " -l {$log_socket}";
424
	}
425

    
426
	/* Setup log rotation */
427
	system_log_rotation_setup();
428

    
429
	/* If HUP was requested, but syslogd is not running, restart it instead. */
430
	if ($sighup && !isvalidpid("{$g['varrun_path']}/syslog.pid")) {
431
		$sighup = false;
432
	}
433

    
434
	$sshguard_whitelist = array();
435
	if (!empty($config['system']['sshguard_whitelist'])) {
436
		$sshguard_whitelist = explode(' ',
437
		    $config['system']['sshguard_whitelist']);
438
	}
439

    
440
	$sshguard_config = array();
441
	$sshguard_config[] = 'LOGREADER="/bin/cat"' . "\n";
442
	$sshguard_config[] = 'BACKEND="/usr/local/libexec/sshg-fw-pf"' . "\n";
443
	if (!empty($config['system']['sshguard_threshold'])) {
444
		$sshguard_config[] = 'THRESHOLD=' .
445
		    $config['system']['sshguard_threshold'] . "\n";
446
	}
447
	if (!empty($config['system']['sshguard_blocktime'])) {
448
		$sshguard_config[] = 'BLOCK_TIME=' .
449
		    $config['system']['sshguard_blocktime'] . "\n";
450
	}
451
	if (!empty($config['system']['sshguard_detection_time'])) {
452
		$sshguard_config[] = 'DETECTION_TIME=' .
453
		    $config['system']['sshguard_detection_time'] . "\n";
454
	}
455
	if (!empty($sshguard_whitelist)) {
456
		@file_put_contents("/usr/local/etc/sshguard.whitelist",
457
		    implode(PHP_EOL, $sshguard_whitelist));
458
		$sshguard_config[] =
459
		    'WHITELIST_FILE=/usr/local/etc/sshguard.whitelist' . "\n";
460
	} else {
461
		unlink_if_exists("/usr/local/etc/sshguard.whitelist");
462
	}
463
	file_put_contents("/usr/local/etc/sshguard.conf", $sshguard_config);
464

    
465
	if (!$sighup) {
466
		sigkillbyname("sshguard", "TERM");
467
		if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
468
			sigkillbypid("{$g['varrun_path']}/syslog.pid", "TERM");
469
			usleep(100000); // syslogd often doesn't respond to a TERM quickly enough for the starting of syslogd below to be successful
470
		}
471

    
472
		if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
473
			// if it still hasn't responded to the TERM, KILL it.
474
			sigkillbypid("{$g['varrun_path']}/syslog.pid", "KILL");
475
			usleep(100000);
476
		}
477

    
478
		$retval = mwexec_bg("/usr/sbin/syslogd {$syslogd_format} -s -c -c {$syslogd_sockets} -P {$g['varrun_path']}/syslog.pid {$syslogd_extra}");
479
	} else {
480
		$retval = sigkillbypid("{$g['varrun_path']}/syslog.pid", "HUP");
481
	}
482

    
483
	if (platform_booting()) {
484
		echo gettext("done.") . "\n";
485
	}
486

    
487
	return $retval;
488
}
489

    
490
function system_log_get_compression() {
491
	global $config, $g, $system_log_compression_types;
492
	/* Default is bzip2 */
493
	if (empty($config['syslog']['logcompressiontype']) ||
494
	    !array_key_exists($config['syslog']['logcompressiontype'], $system_log_compression_types)) {
495
		$compressiontype = 'bzip2';
496
	} else {
497
		$compressiontype = $config['syslog']['logcompressiontype'];
498
	}
499
	return $compressiontype;
500
}
501

    
502
function system_log_get_cat() {
503
	global $system_log_compression_types;
504
	return $system_log_compression_types[system_log_get_compression()]['cat'];
505
}
506

    
507
/* Setup newsyslog log rotation */
508
function system_log_rotation_setup() {
509
	global $config, $g, $system_log_files, $system_log_compression_types;
510
	$syslogcfg = $config['syslog'];
511

    
512
	$mainnewsyslogconf = <<<EOD
513
# Automatically generated, do not edit!
514
# Place configuration files in {$g['varetc_path']}/newsyslog.conf.d
515
<include> {$g['varetc_path']}/newsyslog.conf.d/*
516
# /* Manually added files with non-conflicting names will not be automatically removed */
517

    
518
EOD;
519

    
520
	file_put_contents("{$g['etc_path']}/newsyslog.conf", $mainnewsyslogconf);
521
	safe_mkdir("{$g['varetc_path']}/newsyslog.conf.d");
522
	$log_size = isset($syslogcfg['logfilesize']) ? $syslogcfg['logfilesize'] : $g['default_log_size'];
523
	$log_size = ($log_size < (2**32)/2) ? $log_size : $g['default_log_size'];
524
	$log_size = (int)$log_size/1024;
525

    
526
	$rotatecount = is_numericint($syslogcfg['rotatecount']) ? $syslogcfg['rotatecount'] : 7;
527

    
528
	$compression_flag = $system_log_compression_types[system_log_get_compression()]['flag'];
529

    
530

    
531
	if (isset($syslogcfg)) {
532
		$separatelogfacilities = array('ntp', 'ntpd', 'ntpdate', 'charon', 'ipsec_starter', 'openvpn', 'poes', 'l2tps', 'hostapd', 'dnsmasq', 'named', 'filterdns', 'unbound', 'dhcpd', 'dhcrelay', 'dhclient', 'dhcp6c', 'dpinger', 'radvd', 'routed', 'zebra', 'ospfd', 'ospf6d', 'bgpd', 'watchfrr', 'miniupnpd', 'igmpproxy', 'filterlog');
533
		$newsyslogconf = <<<EOD
534
/var/log/userlog		root:wheel	600	3	{$log_size}	*	B
535
/var/log/utx.log		root:wheel	644	3	{$log_size}	*	B
536

    
537
EOD;
538

    
539
		if ($config['installedpackages']['package']) {
540
			foreach ($config['installedpackages']['package'] as $package) {
541

    
542
				if (isset($package['logging']['logfilename'])) {
543

    
544
					$pkg_log_owner = isset($package['logging']['logowner']) ? $package['logging']['logowner'] : 'root:wheel';
545
					$pkg_log_mode = isset($package['logging']['logmode']) ? $package['logging']['logmode'] : 600;
546
					$pkg_rotate_count = isset($package['logging']['rotatecount']) ? (int) $package['logging']['rotatecount'] : $rotatecount;
547
					$pkg_log_size = isset($package['logging']['logfilesize']) ? (int) $package['logging']['logfilesize'] / 1024: $log_size;
548
					$pkg_rotate_time = isset($package['logging']['rotatetime']) ? $package['logging']['rotatetime'] : '*';
549
					$pkg_extra_flags = isset($package['logging']['rotateflags']) ? $package['logging']['rotateflags'] : '';
550
					$pkg_pidcmd = isset($package['logging']['pidcmd']) ? $package['logging']['pidcmd'] : '';
551
					$pkg_signal = isset($package['logging']['signal']) ? $package['logging']['signal'] : '';
552

    
553
					$pkgnewsyslogconf = "# Automatically generated for package {$package['name']}. Do not edit.\n";
554
					$pkgnewsyslogconf .= system_log_rotation_make_line("{$g['varlog_path']}/{$package['logging']['logfilename']}",
555
												$pkg_log_owner,
556
												$pkg_log_mode,
557
												$pkg_rotate_count,
558
												$pkg_log_size,
559
												$pkg_rotate_time,
560
												"{$compression_flag}C{$pkg_extra_flags}",
561
												$pkg_pidcmd,
562
												$pkg_signal);
563
					@file_put_contents("{$g['varetc_path']}/newsyslog.conf.d/" . basename($package['logging']['logfilename']) . ".conf", $pkgnewsyslogconf);
564
				}
565
			}
566
		}
567

    
568
		foreach($system_log_files as $logfile) {
569
			$local_log_size = isset($syslogcfg[basename($logfile, '.log') . '_settings']['logfilesize']) ? (int) $syslogcfg[basename($logfile, '.log') . '_settings']['logfilesize'] / 1024: $log_size;
570
			$local_rotate_count = isset($syslogcfg[basename($logfile, '.log') . '_settings']['rotatecount']) ? (int) $syslogcfg[basename($logfile, '.log') . '_settings']['rotatecount'] : $rotatecount;
571

    
572
			$newsyslogconf .= system_log_rotation_make_line("{$g['varlog_path']}/{$logfile}.log", 'root:wheel', 600, $local_rotate_count, $local_log_size, '*', "{$compression_flag}C");
573
		}
574

    
575
		if (!@file_put_contents("{$g['varetc_path']}/newsyslog.conf.d/pfSense.conf", $newsyslogconf)) {
576
			printf(gettext("Error: cannot open syslog config file in system_log_rotation_setup().%s"), "\n");
577
		}
578
		unset($newsyslogconf);
579
	}
580
}
581

    
582
function system_log_rotation_make_line($filename, $owner = 'root:wheel', $mode='644', $count=7, $size=1000, $time='*', $flags='C', $pidcmd = '', $signal = '') {
583
	/* TODO: Fix default size, flags, etc. */
584
	$nslline = $filename;
585
	$nslline .= "		{$owner}";
586
	$nslline .= "	{$mode}";
587
	$nslline .= "	{$count}";
588
	$nslline .= "	{$size}";
589
	$nslline .= "	{$time}";
590
	$nslline .= "	{$flags}";
591
	if (!empty($pidcmd)) {
592
		$nslline .= "	{$pidcmd}";
593
	}
594
	if (!empty($signal)) {
595
		$nslline .= "	{$signal}";
596
	}
597
	$nslline .= "\n";
598
	return $nslline;
599
}
600

    
601
function dump_log($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $format = 'table', $grepreverse = false) {
602
	global $g, $config;
603
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
604
	$specific_log = basename($logfile, '.log') . '_settings';
605
	if (($config['syslog'][$specific_log]['cronorder'] == 'forward') && !$grepreverse) $sor = "";
606
	if (($config['syslog'][$specific_log]['cronorder'] == 'reverse') ||  $grepreverse) $sor = "-r";
607
	$logarr = array();
608
	$grepline = "  ";
609
	if (is_array($grepfor)) {
610
		$invert = '';
611
		if ((strpos($grepfor[0], '!') === 0)) {
612
			$grepfor[0] = substr($grepfor[0], 1);
613
			$invert = '-v';
614
		}
615
		$grepline .= " | /usr/bin/egrep {$invert} " . escapeshellarg(implode("|", $grepfor));
616
	}
617
	if (is_array($grepinvert)) {
618
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
619
	}
620
	if (is_dir($logfile)) {
621
		$logarr = array(sprintf(gettext("File %s is a directory."), $logfile));
622
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
623
		$logarr = array(gettext("Log file started."));
624
	} else {
625
		exec(system_log_get_cat() . ' ' . sort_related_log_files($logfile, true, true) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
626
	}
627

    
628
	if ($format == 'none') {
629
		return($logarr);
630
	}
631

    
632
	$rows = 0;
633
	foreach ($logarr as $logent) {
634
		$rows++;
635
		$entry_date_time = "";
636

    
637
		/* Determine log entry content */
638
		$splitlogent = preg_split("/\s+/", $logent, 10);
639
		if (strpos($splitlogent[0], '>') === false) {
640
			/* RFC 3164 Format */
641
			$syslogformat = 'rfc3164';
642
		} else {
643
			/* RFC 5424 Format */
644
			$syslogformat = 'rfc5424';
645
		}
646

    
647
		if ($format == 'raw') {
648
			$entry_text = $logent;
649
		} elseif ($withorig) {
650
			if ($syslogformat == 'rfc3164') {
651
				$entry_date_time = htmlspecialchars(join(" ", array_slice($splitlogent, 0, 3)));
652
				$entry_text = ($splitlogent[3] == $config['system']['hostname']) ? "" : $splitlogent[3] . " ";
653
				$entry_text .= implode(' ', array_slice($splitlogent, 4));
654
			} else {
655
				$entry_date_time = htmlspecialchars($splitlogent[1]);
656
				$entry_text = ($splitlogent[2] == "{$config['system']['hostname']}.{$config['system']['domain']}" ) ? '' : $splitlogent[2] . ' ';
657
				$entry_text .= implode(' ', array_slice($splitlogent, 3));
658
			}
659
		} else {
660
			$entry_text = implode(' ', array_slice($splitlogent, ($syslogformat == 'rfc3164') ? 5 : 9));
661
		}
662
		$entry_text = htmlspecialchars($entry_text);
663

    
664
		/* Output content in desired format. */
665
		switch ($format) {
666
			case 'notable':
667
				echo implode(' ', array($entry_date_time, $entry_text)) . "\n";
668
				break;
669
			case 'raw':
670
				$span = 'colspan="2"';
671
			case 'table':
672
			default:
673
				echo "<tr>\n";
674
				if (!empty($entry_date_time)) {
675
					echo "	<td class=\"text-nowrap\">{$entry_date_time}</td>\n";
676
				}
677
				echo "	<td {$span} style=\"word-wrap:break-word; word-break:break-all; white-space:normal\">{$entry_text}</td>\n";
678
				echo "</tr>\n";
679
				break;
680
		}
681
	}
682

    
683
	return($rows);
684
}
685

    
686
/* format filter logs */
687
function conv_log_filter($logfile, $nentries, $tail = 500, $filtertext = "", $filterinterface = null) {
688
	global $config, $g, $pattern;
689

    
690
	/* Make sure this is a number before using it in a system call */
691
	if (!(is_numeric($tail))) {
692
		return;
693
	}
694

    
695
	/* Safety belt to ensure we get enough lines for filtering without overloading the parsing code */
696
	if ($filtertext) {
697
		$tail = 10000;
698
	}
699

    
700
	/* Always do a reverse tail, to be sure we're grabbing the 'end' of the log. */
701
	$logarr = "";
702

    
703
	$logtypecheck = preg_replace('/\.log$/', '', basename($logfile));
704
	if (in_array($logtypecheck, array('system', 'gateways', 'routing', 'resolver', 'wireless', 'nginx', 'dmesg.boot', 'portalauth', 'dhcpd', 'ipsec', 'ppp', 'openvpn', 'ntpd', 'userlog', 'auth'))) {
705
		$logfile_type = "system";
706
	} elseif (in_array($logtypecheck, array('filter'))) {
707
		$logfile_type = "firewall";
708
	} elseif (in_array($logtypecheck, array('vpn'))) {
709
		$logfile_type = "vpn_login";
710
	} elseif (in_array($logtypecheck, array('poes', 'l2tps'))) {
711
		$logfile_type = "vpn_service";
712
	} elseif (in_array($logtypecheck, array('utx'))) {
713
		$logfile_type = "utx";
714
	} else {
715
		$logfile_type = "unknown";
716
	}
717

    
718
# Common Regular Expression Patterns
719
	$base_patterns = array(
720
		'rfc3164' => array('start_pattern' => "^"),
721
		'rfc5424' => array('start_pattern' => "^<[0-9]{1,3}>[0-9]*\ "),
722
	);
723

    
724
	/* bsd log date */
725
	$base_patterns['rfc3164']['date_pattern'] = "\([a-zA-Z]{3}\ +[0-9]{1,2}\ +[0-9]{2}:[0-9]{2}:[0-9]{2}\)";
726
	/* RFC3339 Extended format date */
727
	$base_patterns['rfc5424']['date_pattern'] = "\((?:[0-9]+)-(?:0[1-9]|1[012])-(?:0[1-9]|[12][0-9]|3[01])[Tt](?:[01][0-9]|2[0-3]):(?:[0-5][0-9]):(?:[0-5][0-9]|60)(?:\.[0-9]+)?(?:(?:[Zz])|(?:[\+|\-](?:[01][0-9]|2[0-3]):[0-5][0-9]))\)";
728

    
729
	/* hostname only, might not be present */
730
	$base_patterns['rfc3164']['host_pattern'] = "\(.*?\)";
731
	/* FQDN, always present */
732
	$base_patterns['rfc5424']['host_pattern'] = "\(\S+?\)";
733

    
734
	/* name[pid]:, pid may not be present */
735
	$base_patterns['rfc3164']['process_pattern'] = "\(.*?\)\(?::\ +\)?";
736
	/* name alone, nothing special */
737
	$base_patterns['rfc5424']['process_pattern'] = "\(\S+?\)\ ";
738

    
739
	/* pid in brackets, may not be present */
740
	$base_patterns['rfc3164']['pid_pattern'] = "\(?:\\\[\([0-9:]*\)\\\]\)?:?";
741
	/* pid (which we want) alone, always present but may be '-', message ID is next, but we ignore it */
742
	$base_patterns['rfc5424']['pid_pattern'] = "\(\S+?\)\ \S+?\ \S+?";
743

    
744
	/* match anything from here on */
745
	$base_patterns['rfc3164']['log_message_pattern'] = "\(.*\)";
746
	$base_patterns['rfc5424']['log_message_pattern'] = "\(.*\)";
747

    
748
	$patterns = array(
749
		'rfc3164' => $base_patterns['rfc3164']['start_pattern'],
750
		'rfc5424' => $base_patterns['rfc5424']['start_pattern'],
751
	);
752

    
753
	# Construct RegEx for specific log file type.
754
	switch ($logfile_type) {
755
		case 'firewall':
756
			$patterns['rfc3164'] = "filterlog\[[0-9]+\]:";
757
			$patterns['rfc5424'] = "filterlog";
758
			break;
759
		case 'system':
760
		case 'vpn_service':
761
			$patterns['rfc3164'] .= $base_patterns['rfc3164']['date_pattern'] . "\ +" .
762
						$base_patterns['rfc3164']['host_pattern'] . "\ +" .
763
						$base_patterns['rfc3164']['process_pattern'] .
764
						$base_patterns['rfc3164']['pid_pattern'] . "\ +" .
765
						$base_patterns['rfc3164']['log_message_pattern'] . "$";
766
			$patterns['rfc5424'] .= $base_patterns['rfc5424']['date_pattern'] . "\ " .
767
						$base_patterns['rfc5424']['host_pattern'] . "\ " .
768
						$base_patterns['rfc5424']['process_pattern'] .
769
						$base_patterns['rfc5424']['pid_pattern'] . "\ " .
770
						$base_patterns['rfc5424']['log_message_pattern'] . "$";
771
			break;
772
		case 'vpn_login':
773
			$action_pattern = "\(.*?\)";
774
			$type_pattern = "\(.*?\)";
775
			$ip_address_pattern = "\(.*?\)";
776
			$user_pattern = "\(.*?\)";
777
			$patterns['rfc3164'] .= $base_patterns['rfc3164']['date_pattern'] . "\ +" .
778
						$base_patterns['rfc3164']['host_pattern'] . "\ +" .
779
						$base_patterns['rfc3164']['process_pattern'] . "\ +" .
780
						"\(.*?\)\,\ *" .
781
						"\(.*?\)\,\ *" .
782
						"\(.*?\)\,\ *" .
783
						"\(.*?\)$";
784
			$patterns['rfc5424'] .= $base_patterns['rfc5424']['date_pattern'] . "\ " .
785
						$base_patterns['rfc5424']['host_pattern'] . "\ " .
786
						$base_patterns['rfc5424']['process_pattern'] .
787
						"\S+?\ \S+?\ \S+?\ " .
788
						"\(\S*?\)\,\ *" .
789
						"\(\S*?\)\,\ *" .
790
						"\(\S*?\)\,\ *" .
791
						"\(\S*?\)$";
792
			break;
793
		case 'unknown':
794
			$patterns['rfc3164'] .= $base_patterns['rfc3164']['date_pattern'] . "*\ +" .
795
						$base_patterns['rfc3164']['log_message_pattern'] . "$";
796
			$patterns['rfc5424'] .= $base_patterns['rfc5424']['date_pattern'] . "*\ " .
797
						$base_patterns['rfc5424']['log_message_pattern'] . "$";
798
			break;
799
		default:
800
			$patterns['rfc3164'] .= "\(.*\)$";
801
			$patterns['rfc5424'] .= "\(.*\)$";
802
	}
803

    
804
	if ($logfile_type != 'utx') {
805
		# Get a bunch of log entries.
806
		exec(system_log_get_cat() . ' ' . sort_related_log_files($logfile, true, true) . " | /usr/bin/tail -r -n {$tail}", $logarr);
807
	} else {
808
		$_gb = exec("/usr/bin/last --libxo=json", $rawdata, $rc);
809
		if ($rc == 0) {
810
			$logarr = json_decode(implode(" ", $rawdata), JSON_OBJECT_AS_ARRAY);
811
			$logarr = $logarr['last-information']['last'];
812
		}
813
	}
814

    
815
	# Remove escapes and fix up the pattern for preg_match.
816
	$patterns['rfc3164'] = '/' . str_replace(array('\(', '\)', '\[', '\]'), array('(', ')', '[', ']'), $patterns['rfc3164']) . '/';
817
	$patterns['rfc5424'] = '/' . str_replace(array('\(', '\)', '\[', '\]'), array('(', ')', '[', ']'), $patterns['rfc5424']) . '/';
818

    
819
	$filterlog = array();
820
	$counter = 0;
821

    
822
	$filterinterface = strtoupper($filterinterface);
823
	foreach ($logarr as $logent) {
824
		if ($counter >= $nentries) {
825
			break;
826
		}
827
		if ($logfile_type != 'utx') {
828
			$pattern = (substr($logent, 0, 1) == '<') ? $patterns['rfc5424'] : $patterns['rfc3164'];
829
		}
830
		switch($logfile_type) {
831
			case 'firewall':
832
				$flent = parse_firewall_log_line($logent);
833
				break;
834
			case 'system':
835
				$flent = parse_system_log_line($logent);
836
				break;
837
			case 'vpn_login':
838
				$flent = parse_vpn_login_log_line($logent);
839
				break;
840
			case 'vpn_service':
841
				$flent = parse_vpn_service_log_line($logent);
842
				break;
843
			case 'utx':
844
				$flent = parse_utx_log_line($logent);
845
				break;
846
			case 'unknown':
847
				$flent = parse_unknown_log_line($logent);
848
				break;
849
			default:
850
				$flent = array();
851
				break;
852
		}
853

    
854
		if (!$filterinterface || ($filterinterface == $flent['interface'])) {
855
			if ((($flent != "") && (!is_array($filtertext)) && (match_filter_line($flent, $filtertext))) ||
856
			    (($flent != "") && (is_array($filtertext)) && (match_filter_field($flent, $filtertext)))) {
857
				$counter++;
858
				$filterlog[] = $flent;
859
			}
860
		}
861
	}
862
	/* Since the lines are in reverse order, flip them around if needed based on the user's preference */
863
	# First get the "General Logging Options" (global) chronological order setting.  Then apply specific log override if set.
864
	$reverse = isset($config['syslog']['reverse']);
865
	$specific_log = basename($logfile, '.log') . '_settings';
866
	if (isset($config['syslog'][$specific_log]['cronorder']) && ($config['syslog'][$specific_log]['cronorder'] == 'forward')) $reverse = false;
867
	if (isset($config['syslog'][$specific_log]['cronorder']) && ($config['syslog'][$specific_log]['cronorder'] == 'reverse')) $reverse = true;
868

    
869
	return ($reverse) ? $filterlog : array_reverse($filterlog);
870
}
871

    
872
function escape_filter_regex($filtertext) {
873
	/* If the caller (user) has not already put a backslash before a slash, to escape it in the regex, */
874
	/* then this will do it. Take out any "\/" already there, then turn all ordinary "/" into "\/".    */
875
	return str_replace('/', '\/', str_replace('\/', '/', $filtertext));
876
}
877

    
878
function match_filter_line($flent, $filtertext = "") {
879
	if (!$filtertext) {
880
		return true;
881
	}
882
	$filtertext = escape_filter_regex(str_replace(' ', '\s+', $filtertext));
883
	return @preg_match("/{$filtertext}/i", implode(" ", array_values($flent)));
884
}
885

    
886
function match_filter_field($flent, $fields) {
887
	foreach ($fields as $key => $field) {
888
		if ($field == "All") {
889
			continue;
890
		}
891
		if ((strpos($field, '!') === 0)) {
892
			$field = substr($field, 1);
893
			if (strtolower($key) == 'act') {
894
				if (in_arrayi($flent[$key], explode(" ", $field))) {
895
					return false;
896
				}
897
			} else {
898
				$field_regex = escape_filter_regex($field);
899
				if (@preg_match("/{$field_regex}/i", $flent[$key])) {
900
					return false;
901
				}
902
			}
903
		} else {
904
			if (strtolower($key) == 'act') {
905
				if (!in_arrayi($flent[$key], explode(" ", $field))) {
906
					return false;
907
				}
908
			} else {
909
				$field_regex = escape_filter_regex($field);
910
				if (!@preg_match("/{$field_regex}/i", $flent[$key])) {
911
					return false;
912
				}
913
			}
914
		}
915
	}
916
	return true;
917
}
918

    
919
// Case Insensitive in_array function
920
function in_arrayi($needle, $haystack) {
921
	return in_array(strtolower($needle), array_map('strtolower', $haystack));
922
}
923

    
924
function parse_vpn_login_log_line($line) {
925
	global $config, $g, $pattern;
926

    
927
	$flent = array();
928
	$log_split = "";
929

    
930
	if (!preg_match($pattern, $line, $log_split))
931
		return "";
932

    
933
	list($all, $flent['time'], $flent['host'], $flent['process'], $flent['action'], $flent['type'], $flent['ip_address'], $flent['user']) = $log_split;
934
	$flent['time'] = str_replace('T', ' ', $flent['time']);
935

    
936
	/* If there is time, action, user, and IP address fields, then the line should be usable/good */
937
	if (!( (trim($flent['time']) == "") && (trim($flent['action']) == "") && (trim($flent['user']) == "") && (trim($flent['ip_address']) == "") )) {
938
		return $flent;
939
	} else {
940
		if ($g['debug']) {
941
			log_error(sprintf(gettext("There was a error parsing log entry: %s. Please report to mailing list or forum."), $line));
942
		}
943
		return "";
944
	}
945
}
946

    
947
function parse_vpn_service_log_line($line) {
948
	global $config, $g, $pattern;
949

    
950
	$flent = array();
951
	$log_split = "";
952

    
953
	if (!preg_match($pattern, $line, $log_split))
954
		return "";
955

    
956
	list($all, $flent['time'], $flent['host'], $flent['type'], $flent['pid'], $flent['message']) = $log_split;
957
	$flent['time'] = str_replace('T', ' ', $flent['time']);
958

    
959
	/* If there is time, type, and message fields, then the line should be usable/good */
960
	if (!( (trim($flent['time']) == "") && (trim($flent['type']) == "") && (trim($flent['message']) == "") )) {
961
		return $flent;
962
	} else {
963
		if ($g['debug']) {
964
			log_error(sprintf(gettext("There was a error parsing log entry: %s. Please report to mailing list or forum."), $line));
965
		}
966
		return "";
967
	}
968
}
969

    
970
function parse_utx_log_line($line) {
971
	$flent['time'] = "{$line['login-time']}";
972
	if ($line['logout-time']) {
973
		$flent['time'] .= " - {$line['logout-time']}";
974
	}
975
	$flent['host'] = $line['from'];
976
	$flent['pid'] = $line['tty'];
977
	$flent['message'] = $line['user'];
978
	if ($line['session-length']) {
979
		$flent['process'] = $line['session-length'];
980
	}
981
	return $flent;
982
}
983

    
984
function parse_unknown_log_line($line) {
985
	global $config, $g, $pattern;
986

    
987
	$flent = array();
988
	$log_split = "";
989

    
990
	if (!preg_match($pattern, $line, $log_split)) {
991
		return "";
992
	}
993

    
994
	list($all, $flent['time'], $flent['message']) = $log_split;
995
	$flent['time'] = str_replace('T', ' ', $flent['time']);
996

    
997
	/* If there is time, and message, fields, then the line should be usable/good */
998
	if (!((trim($flent['time']) == "") && (trim($flent['message']) == ""))) {
999
		return $flent;
1000
	} else {
1001
		if ($g['debug']) {
1002
			log_error(sprintf(gettext("There was a error parsing log entry: %s. Please report to mailing list or forum."), $line));
1003
		}
1004
		return "";
1005
	}
1006
}
1007

    
1008
function parse_system_log_line($line) {
1009
	global $config, $g, $pattern;
1010

    
1011
	$flent = array();
1012
	$log_split = "";
1013

    
1014
	if (!preg_match($pattern, $line, $log_split)) {
1015
		return "";
1016
	}
1017

    
1018
	list($all, $flent['time'], $flent['host'], $flent['process'], $flent['pid'], $flent['message']) = $log_split;
1019
	$flent['time'] = str_replace('T', ' ', $flent['time']);
1020

    
1021
	/* If there is time, process, and message, fields, then the line should be usable/good */
1022
	if (!((trim($flent['time']) == "") && (trim($flent['process']) == "") && (trim($flent['message']) == ""))) {
1023
		return $flent;
1024
	} else {
1025
		if ($g['debug']) {
1026
			log_error(sprintf(gettext("There was a error parsing log entry: %s. Please report to mailing list or forum."), $line));
1027
		}
1028
		return "";
1029
	}
1030
}
1031

    
1032
function parse_firewall_log_line($line) {
1033
	global $config, $g;
1034

    
1035
	$flent = array();
1036
	$log_split = "";
1037

    
1038
	if (substr($line, 0, 1) == '<') {
1039
		/* RFC 5424 */;
1040
		$pattern = "/^<[0-9]{1,3}>[0-9]*\ (\S+?)\ (\S+?)\ filterlog\ \S+?\ \S+?\ \S+?\ (.*)$/U";
1041
	} else {
1042
		/* RFC 3164 */
1043
		$pattern = "/(.*)\s(.*)\sfilterlog\[[0-9]+\]:\s(.*)$/";
1044
	}
1045

    
1046
	if (!preg_match($pattern, $line, $log_split)) {
1047
		return "";
1048
	}
1049

    
1050
	list($all, $flent['time'], $host, $rule) = $log_split;
1051
	$flent['time'] = str_replace('T', ' ', $flent['time']);
1052

    
1053
	$rule_data = explode(",", $rule);
1054
	$field = 0;
1055

    
1056
	$flent['rulenum'] = $rule_data[$field++];
1057
	$flent['subrulenum'] = $rule_data[$field++];
1058
	$flent['anchor'] = $rule_data[$field++];
1059
	$flent['tracker'] = $rule_data[$field++];
1060
	$flent['realint'] = $rule_data[$field++];
1061
	$flent['interface'] = convert_real_interface_to_friendly_descr($flent['realint']);
1062
	$flent['reason'] = $rule_data[$field++];
1063
	$flent['act'] = $rule_data[$field++];
1064
	$flent['direction'] = $rule_data[$field++];
1065
	$flent['version'] = $rule_data[$field++];
1066

    
1067
	if ($flent['version'] == '4' || $flent['version'] == '6') {
1068
		if ($flent['version'] == '4') {
1069
			$flent['tos'] = $rule_data[$field++];
1070
			$flent['ecn'] = $rule_data[$field++];
1071
			$flent['ttl'] = $rule_data[$field++];
1072
			$flent['id'] = $rule_data[$field++];
1073
			$flent['offset'] = $rule_data[$field++];
1074
			$flent['flags'] = $rule_data[$field++];
1075
			$flent['protoid'] = $rule_data[$field++];
1076
			$flent['proto'] = strtoupper($rule_data[$field++]);
1077
		} else {
1078
			$flent['class'] = $rule_data[$field++];
1079
			$flent['flowlabel'] = $rule_data[$field++];
1080
			$flent['hlim'] = $rule_data[$field++];
1081
			$flent['proto'] = $rule_data[$field++];
1082
			$flent['protoid'] = $rule_data[$field++];
1083
		}
1084

    
1085
		$flent['length'] = $rule_data[$field++];
1086
		$flent['srcip'] = $rule_data[$field++];
1087
		$flent['dstip'] = $rule_data[$field++];
1088

    
1089
		if ($flent['protoid'] == '6' || $flent['protoid'] == '17') { // TCP or UDP
1090
			$flent['srcport'] = $rule_data[$field++];
1091
			$flent['dstport'] = $rule_data[$field++];
1092

    
1093
			$flent['src'] = $flent['srcip'] . ':' . $flent['srcport'];
1094
			$flent['dst'] = $flent['dstip'] . ':' . $flent['dstport'];
1095

    
1096
			$flent['datalen'] = $rule_data[$field++];
1097
			if ($flent['protoid'] == '6') { // TCP
1098
				$flent['tcpflags'] = $rule_data[$field++];
1099
				$flent['seq'] = $rule_data[$field++];
1100
				$flent['ack'] = $rule_data[$field++];
1101
				$flent['window'] = $rule_data[$field++];
1102
				$flent['urg'] = $rule_data[$field++];
1103
				$flent['options'] = explode(";", $rule_data[$field++]);
1104
			}
1105
		} else if ($flent['protoid'] == '1' || $flent['protoid'] == '58') {	// ICMP (IPv4 & IPv6)
1106
			$flent['src'] = $flent['srcip'];
1107
			$flent['dst'] = $flent['dstip'];
1108

    
1109
			$flent['icmp_type'] = $rule_data[$field++];
1110

    
1111
			switch ($flent['icmp_type']) {
1112
				case "request":
1113
				case "reply":
1114
					$flent['icmp_id'] = $rule_data[$field++];
1115
					$flent['icmp_seq'] = $rule_data[$field++];
1116
					break;
1117
				case "unreachproto":
1118
					$flent['icmp_dstip'] = $rule_data[$field++];
1119
					$flent['icmp_protoid'] = $rule_data[$field++];
1120
					break;
1121
				case "unreachport":
1122
					$flent['icmp_dstip'] = $rule_data[$field++];
1123
					$flent['icmp_protoid'] = $rule_data[$field++];
1124
					$flent['icmp_port'] = $rule_data[$field++];
1125
					break;
1126
				case "unreach":
1127
				case "timexceed":
1128
				case "paramprob":
1129
				case "redirect":
1130
				case "maskreply":
1131
					$flent['icmp_descr'] = $rule_data[$field++];
1132
					break;
1133
				case "needfrag":
1134
					$flent['icmp_dstip'] = $rule_data[$field++];
1135
					$flent['icmp_mtu'] = $rule_data[$field++];
1136
					break;
1137
				case "tstamp":
1138
					$flent['icmp_id'] = $rule_data[$field++];
1139
					$flent['icmp_seq'] = $rule_data[$field++];
1140
					break;
1141
				case "tstampreply":
1142
					$flent['icmp_id'] = $rule_data[$field++];
1143
					$flent['icmp_seq'] = $rule_data[$field++];
1144
					$flent['icmp_otime'] = $rule_data[$field++];
1145
					$flent['icmp_rtime'] = $rule_data[$field++];
1146
					$flent['icmp_ttime'] = $rule_data[$field++];
1147
					break;
1148
				default :
1149
					$flent['icmp_descr'] = $rule_data[$field++];
1150
					break;
1151
			}
1152

    
1153
		} else if ($flent['protoid'] == '2') { // IGMP
1154
			$flent['src'] = $flent['srcip'];
1155
			$flent['dst'] = $flent['dstip'];
1156
		} else if ($flent['protoid'] == '112') { // CARP
1157
			$flent['type'] = $rule_data[$field++];
1158
			$flent['ttl'] = $rule_data[$field++];
1159
			$flent['vhid'] = $rule_data[$field++];
1160
			$flent['version'] = $rule_data[$field++];
1161
			$flent['advskew'] = $rule_data[$field++];
1162
			$flent['advbase'] = $rule_data[$field++];
1163
			$flent['src'] = $flent['srcip'];
1164
			$flent['dst'] = $flent['dstip'];
1165
		}
1166
	} else {
1167
		if ($g['debug']) {
1168
			log_error(sprintf(gettext("There was a error parsing rule number: %s. Please report to mailing list or forum."), $flent['rulenum']));
1169
		}
1170
		return "";
1171
	}
1172

    
1173
	/* If there is a src, a dst, and a time, then the line should be usable/good */
1174
	if (!((trim($flent['src']) == "") || (trim($flent['dst']) == "") || (trim($flent['time']) == ""))) {
1175
		return $flent;
1176
	} else {
1177
		if ($g['debug']) {
1178
			log_error(sprintf(gettext("There was a error parsing rule: %s. Please report to mailing list or forum."), $line));
1179
		}
1180
		return "";
1181
	}
1182
}
1183

    
1184
function get_port_with_service($port, $proto) {
1185
	if (!$port || !is_port($port)) {
1186
		return '';
1187
	}
1188

    
1189
	$service = getservbyport($port, $proto);
1190
	$portstr = "";
1191
	if ($service) {
1192
		$portstr = sprintf('<span title="' . gettext('Service %1$s/%2$s: %3$s') . '">' . htmlspecialchars($port) . '</span>', $port, $proto, $service);
1193
	} else {
1194
		$portstr = htmlspecialchars($port);
1195
	}
1196
	return ':' . $portstr;
1197
}
1198

    
1199
function find_rule_by_number($rulenum, $trackernum, $type="block") {
1200
	global $g;
1201

    
1202
	/* Passing arbitrary input to grep could be a Very Bad Thing(tm) */
1203
	if (!is_numeric($rulenum) || !is_numeric($trackernum) || !in_array($type, array('pass', 'block', 'match', 'rdr'))) {
1204
		return;
1205
	}
1206

    
1207
	if ($trackernum == "0") {
1208
		$lookup_pattern = "^@{$rulenum}\([0-9]+\)[[:space:]]{$type}[[:space:]].*[[:space:]]log[[:space:]]";
1209
	} else {
1210
		$lookup_pattern = "^@[0-9]+\({$trackernum}\)[[:space:]]{$type}[[:space:]].*[[:space:]]log[[:space:]]";
1211
	}
1212

    
1213
	/* At the moment, miniupnpd is the only thing I know of that
1214
	   generates logging rdr rules */
1215
	unset($buffer);
1216
	if ($type == "rdr") {
1217
		$_gb = exec("/sbin/pfctl -vvPsn -a \"miniupnpd\" | /usr/bin/egrep " . escapeshellarg("^@{$rulenum}"), $buffer);
1218
	} else {
1219
		$_gb = exec("/sbin/pfctl -vvPsr | /usr/bin/egrep " . escapeshellarg($lookup_pattern), $buffer);
1220
	}
1221
	if (is_array($buffer)) {
1222
		return $buffer[0];
1223
	}
1224

    
1225
	return "";
1226
}
1227

    
1228
function buffer_rules_load() {
1229
	global $g, $buffer_rules_rdr, $buffer_rules_normal;
1230
	unset($buffer, $buffer_rules_rdr, $buffer_rules_normal);
1231
	/* Redeclare globals after unset to work around PHP */
1232
	global $buffer_rules_rdr, $buffer_rules_normal;
1233
	$buffer_rules_rdr = array();
1234
	$buffer_rules_normal = array();
1235

    
1236
	$_gb = exec("/sbin/pfctl -vvPsn -a \"miniupnpd\" | /usr/bin/grep '^@'", $buffer);
1237
	if (is_array($buffer)) {
1238
		foreach ($buffer as $line) {
1239
			list($key, $value) = explode (" ", $line, 2);
1240
			$buffer_rules_rdr[$key] = $value;
1241
		}
1242
	}
1243
	unset($buffer, $_gb);
1244
	$_gb = exec("/sbin/pfctl -vvPsr | /usr/bin/egrep '^@[0-9]+\([0-9]+\)[[:space:]].*[[:space:]]log[[:space:]]'", $buffer);
1245

    
1246
	if (is_array($buffer)) {
1247
		foreach ($buffer as $line) {
1248
			list($key, $value) = explode (" ", $line, 2);
1249
			# pfctl rule number output with tracker number: @dd(dddddddddd)
1250
			$matches = array();
1251
			if (preg_match('/\@(?P<rulenum>\d+)\((?<trackernum>\d+)\)/', $key, $matches) == 1) {
1252
				if ($matches['trackernum'] > 0) {
1253
					$key = $matches['trackernum'];
1254
				} else {
1255
					$key = "@{$matches['rulenum']}";
1256
				}
1257
			}
1258
			$buffer_rules_normal[$key] = $value;
1259
		}
1260
	}
1261
	unset($_gb, $buffer);
1262
}
1263

    
1264
function buffer_rules_clear() {
1265
	unset($GLOBALS['buffer_rules_normal']);
1266
	unset($GLOBALS['buffer_rules_rdr']);
1267
}
1268

    
1269
function find_rule_by_number_buffer($rulenum, $trackernum, $type) {
1270
	global $g, $buffer_rules_rdr, $buffer_rules_normal;
1271

    
1272
	if ($trackernum == "0") {
1273
		$lookup_key = "@{$rulenum}";
1274
	} else {
1275
		$lookup_key = $trackernum;
1276
	}
1277

    
1278
	if ($type == "rdr") {
1279
		$ruleString = $buffer_rules_rdr[$lookup_key];
1280
		//TODO: get the correct 'description' part of a RDR log line. currently just first 30 characters..
1281
		$rulename = substr($ruleString, 0, 30);
1282
	} else {
1283
		$ruleString = $buffer_rules_normal[$lookup_key];
1284
		list(,$rulename,) = explode("\"", $ruleString);
1285
		$rulename = str_replace("USER_RULE: ", '<i class="fa fa-user"></i> ', $rulename);
1286
	}
1287
	return "{$rulename} ({$lookup_key})";
1288
}
1289

    
1290
function find_action_image($action) {
1291
	global $g;
1292
	if ((strstr(strtolower($action), "p")) || (strtolower($action) == "rdr")) {
1293
		return "fa-check-circle-o";
1294
	} else if (strstr(strtolower($action), "r")) {
1295
		return "fa-times-circle-o";
1296
	} else {
1297
		return "fa-ban";
1298
	}
1299
}
1300

    
1301
/* AJAX specific handlers */
1302
function handle_ajax() {
1303
	global $config;
1304
	if ($_REQUEST['lastsawtime'] && $_REQUEST['logfile']) {
1305

    
1306
		$lastsawtime = getGETPOSTsettingvalue('lastsawtime', null);
1307
		$logfile = getGETPOSTsettingvalue('logfile', null);
1308
		$nentries = getGETPOSTsettingvalue('nentries', null);
1309
		$type = getGETPOSTsettingvalue('type', null);
1310
		$filter = getGETPOSTsettingvalue('filter', null);
1311
		$interfacefilter = getGETPOSTsettingvalue('interfacefilter', null);
1312

    
1313
		if (!empty(trim($filter)) || is_numeric($filter)) {
1314
			$filter = json_decode($filter, true);	# Filter Fields Array or Filter Text
1315
		}
1316

    
1317
		/* compare lastsawrule's time stamp to filter logs.
1318
		 * afterwards return the newer records so that client
1319
		 * can update AJAX interface screen.
1320
		 */
1321
		$new_rules = "";
1322

    
1323
		$filterlog = conv_log_filter($logfile, $nentries, $nentries + 100, $filter, $interfacefilter);
1324

    
1325
		/* We need this to always be in forward order for the AJAX update to work properly */
1326
		/* Since the lines are in reverse order, flip them around if needed based on the user's preference */
1327
		# First get the "General Logging Options" (global) chronological order setting.  Then apply specific log override if set.
1328
		$reverse = isset($config['syslog']['reverse']);
1329
		$specific_log = basename($logfile, '.log') . '_settings';
1330
		if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $reverse = false;
1331
		if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $reverse = true;
1332

    
1333
		$filterlog = ($reverse) ? array_reverse($filterlog) : $filterlog;
1334

    
1335
		foreach ($filterlog as $log_row) {
1336
			$row_time = strtotime($log_row['time']);
1337
			if ($row_time > $lastsawtime) {
1338
				if ($log_row['proto'] == "TCP") {
1339
					$log_row['proto'] .= ":{$log_row['tcpflags']}";
1340
				}
1341

    
1342
				if ($log_row['act'] == "block") {
1343
					$icon_act = "fa-times text-danger";
1344
				} else {
1345
					$icon_act = "fa-check text-success";
1346
				}
1347

    
1348
				$btn = '<i class="fa ' . $icon_act . ' icon-pointer" title="' . $log_row['act'] . '/' . $log_row['tracker'] . '" onclick="javascript:getURL(\'status_logs_filter.php?getrulenum=' . $log_row['rulenum'] . ',' . $log_row['tracker'] . ',' . $log_row['act'] . '\', outputrule);"></i>';
1349
				$new_rules .= "{$btn}||{$log_row['time']}||{$log_row['interface']}||{$log_row['srcip']}||{$log_row['srcport']}||{$log_row['dstip']}||{$log_row['dstport']}||{$log_row['proto']}||{$log_row['version']}||" . time() . "||\n";
1350
			}
1351
		}
1352
		echo $new_rules;
1353
		exit;
1354
	}
1355
}
1356

    
1357
/* Compatibility stubs for old clog functions until packages catch up.
1358
 * Remove once packages all use the new function names. */
1359
function dump_clog_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
1360
	return dump_log($logfile, $tail, $withorig, $grepfor, $grepinvert, $format = 'notable');
1361
}
1362
function dump_log_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
1363
	return dump_log($logfile, $tail, $withorig, $grepfor, $grepinvert, $format = 'notable');
1364
}
1365
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
1366
	return dump_log($logfile, $tail, $withorig, $grepfor, $grepinvert);
1367
}
1368
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
1369
	return dump_log($logfile, $tail, $withorig, $grepfor, $grepinvert, $format = 'none', $grepreverse);
1370
}
1371
function return_log($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
1372
	return dump_log($logfile, $tail, $withorig, $grepfor, $grepinvert, $format = 'none', $grepreverse);
1373
}
(48-48/61)