Project

General

Profile

Download (50.7 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-2025 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
require_once('util.inc');
30

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

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

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

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

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

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

    
82
	foreach (config_get_path('installedpackages/package', []) as $key => $package) {
83
		if (!empty(config_get_path("installedpackages/package/{$key}/logging", [])) &&
84
		    !empty(config_get_path("installedpackages/package/{$key}/logging/logfilename"))) {
85
			$all_syslog_files[] = config_get_path("installedpackages/package/{$key}/logging/logfilename");
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 $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 $g;
186
	if (config_path_enabled('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_get_path('syslog', []);
194

    
195
	if (is_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
	$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');
217
	$syslogconf = "# Automatically generated, do not edit!\n";
218

    
219
	foreach (config_get_path('installedpackages/package', []) as $key => $package) {
220
		$pkg_log_facility = config_get_path("installedpackages/package/{$key}/logging/facilityname");
221
		$pkg_log_filename = basename(config_get_path("installedpackages/package/{$key}/logging/logfilename"));
222

    
223
		if (!empty($pkg_log_facility) && !empty($pkg_log_filename)) {
224
			array_push($separatelogfacilities, $pkg_log_facility);
225
			$fn = "{$g['varlog_path']}/{$pkg_log_filename}";
226
			if (!is_file($fn)) {
227
				mwexec('/usr/bin/touch ' . escapeshellarg($fn));
228
			}
229
			$pkgsyslogconf = "# Automatically generated for package {$package['name']}. Do not edit.\n";
230
			$pkgsyslogconf .= "!{$pkg_log_facility}\n*.*\t\t\t\t\t\t {$fn}\n";
231
			@file_put_contents("{$g['varetc_path']}/syslog.d/" . basename($pkg_log_filename) . ".conf", $pkgsyslogconf);
232
		}
233
	}
234

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

    
243
	$syslogconf .= "!radvd\n";
244
	$radvdloglevel = ((config_path_enabled('system', 'radvddebug')) ? '*' : 'err');
245
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
246
		$syslogconf .= "*.{$radvdloglevel}								{$g['varlog_path']}/routing.log\n";
247
	}
248
	if (config_path_enabled('syslog', 'routing')) {
249
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "!radvd.{$radvdloglevel}");
250
	}
251

    
252
	$syslogconf .= "!routed,zebra,ospfd,ospf6d,bgpd,watchfrr,miniupnpd,igmpproxy\n";
253
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
254
		$syslogconf .= "*.*									{$g['varlog_path']}/routing.log\n";
255
	}
256
	if (config_path_enabled('syslog', 'routing')) {
257
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
258
	}
259

    
260
	$syslogconf .= "!ntp,ntpd,ntpdate\n";
261
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
262
		$syslogconf .= "*.*									{$g['varlog_path']}/ntpd.log\n";
263
	}
264
	if (config_path_enabled('syslog', 'ntpd')) {
265
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
266
	}
267

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

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

    
284
	$syslogconf .= "!l2tps\n";
285
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
286
		$syslogconf .= "*.*									{$g['varlog_path']}/l2tps.log\n";
287
	}
288
	if (config_path_enabled('syslog', 'vpn')) {
289
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
290
	}
291

    
292
	$syslogconf .= "!charon,ipsec_starter\n";
293
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
294
		$syslogconf .= "*.*									{$g['varlog_path']}/ipsec.log\n";
295
	}
296
	if (config_path_enabled('syslog', 'vpn')) {
297
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
298
	}
299

    
300
	$syslogconf .= "!openvpn\n";
301
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
302
		$syslogconf .= "*.*									{$g['varlog_path']}/openvpn.log\n";
303
	}
304
	if (config_path_enabled('syslog', 'vpn')) {
305
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
306
	}
307

    
308
	$syslogconf .= "!dpinger\n";
309
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
310
		$syslogconf .= "*.*									{$g['varlog_path']}/gateways.log\n";
311
	}
312
	if (config_path_enabled('syslog', 'dpinger')) {
313
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
314
	}
315

    
316
	$syslogconf .= "!dnsmasq,named,filterdns,unbound\n";
317
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
318
		$syslogconf .= "*.*									{$g['varlog_path']}/resolver.log\n";
319
	}
320
	if (config_path_enabled('syslog', 'resolver')) {
321
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
322
	}
323

    
324
	$syslogconf .= "!dhcpd,dhcrelay,dhclient,dhcp6c,dhcpleases,dhcpleases6,kea-dhcp4,kea-dhcp6\n";
325
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
326
		$syslogconf .= "*.*									{$g['varlog_path']}/dhcpd.log\n";
327
	}
328
	if (config_path_enabled('syslog', 'dhcp')) {
329
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
330
	}
331

    
332
	$syslogconf .= "!hostapd\n";
333
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
334
		$syslogconf .= "*.* 								{$g['varlog_path']}/wireless.log\n";
335
	}
336
	if (config_path_enabled('syslog', 'hostapd')) {
337
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
338
	}
339

    
340
	$syslogconf .= "!filterlog\n";
341
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
342
		$syslogconf .= "*.* 								{$g['varlog_path']}/filter.log\n";
343
	}
344
	if (config_path_enabled('syslog', 'filter')) {
345
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
346
	}
347

    
348
	$syslogconf .= "!logportalauth\n";
349
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
350
		$syslogconf .= "*.* 								{$g['varlog_path']}/portalauth.log\n";
351
	}
352
	if (config_path_enabled('syslog', 'portalauth')) {
353
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
354
	}
355

    
356
	$facilitylist = implode(',', array_unique($separatelogfacilities));
357
	$syslogconf .= "!-{$facilitylist}\n";
358
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
359
		$syslogconf .= <<<EOD
360
local3.*							{$g['varlog_path']}/vpn.log
361
local5.*							{$g['varlog_path']}/nginx.log
362
*.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
363
*.emerg								*
364

    
365
EOD;
366
	}
367

    
368
	$syslogconf .= <<<EOD
369
!*
370
:msg, startswith, "if_pppoe: "
371
*.*								{$g['varlog_path']}/ppp.log
372
EOD;
373

    
374
	if (config_path_enabled('syslog', 'vpn')) {
375
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "local3.*");
376
	}
377
	if (config_path_enabled('syslog', 'system')) {
378
		$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");
379
	}
380
	if (config_path_enabled('syslog', 'logall')) {
381
		// Make everything mean everything, including facilities excluded above.
382
		$syslogconf .= "!*\n";
383
		$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
384
	}
385

    
386
	/* write syslog config to pfSense.conf */
387
	if (!@file_put_contents("{$g['varetc_path']}/syslog.d/pfSense.conf", $syslogconf)) {
388
		printf(gettext("Error: cannot open syslog config file in system_syslogd_start().%s"), "\n");
389
		unset($syslogconf);
390
		return 1;
391
	}
392
	unset($syslogconf);
393

    
394
	$sourceip = config_get_path('syslog/sourceip');
395
	if (config_path_enabled('syslog') &&
396
	    !empty($sourceip)) {
397
		if (config_get_path('syslog/ipproto') == "ipv6") {
398
			$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
399
			if (!is_ipaddr($ifaddr)) {
400
				$ifaddr = get_interface_ip($sourceip);
401
			}
402
		} else {
403
			$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
404
			if (!is_ipaddr($ifaddr)) {
405
				$ifaddr = get_interface_ipv6($sourceip);
406
			}
407
		}
408
		$sourceip = is_ipaddr($ifaddr) ? "-b {$ifaddr}" : '';
409
	} else {
410
		$sourceip = '';
411
	}
412
	$syslogd_extra = "-f {$g['etc_path']}/syslog.conf {$sourceip}";
413

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

    
416
	foreach (config_get_path('installedpackages/package', []) as $key => $package) {
417
		$pkg_log_socket = config_get_path("installedpackages/package/{$key}/logging/logsocket");
418
		if (!empty($pkg_log_socket) &&
419
		    !in_array($pkg_log_socket, $log_sockets)) {
420
			$log_sockets[] = $package['logging']['logsocket'];
421
		}
422
	}
423

    
424
	$syslogd_format = empty(config_get_path('syslog/format')) ? '' : '-O ' . escapeshellarg(config_get_path('syslog/format'));
425

    
426
	$syslogd_sockets = "";
427
	foreach ($log_sockets as $log_socket) {
428
		// Ensure that the log directory exists
429
		$logpath = dirname($log_socket);
430
		safe_mkdir($logpath);
431
		$syslogd_sockets .= " -l {$log_socket}";
432
	}
433

    
434
	/* Setup log rotation */
435
	system_log_rotation_setup();
436

    
437
	/* If HUP was requested, but syslogd is not running, restart it instead. */
438
	if ($sighup && !isvalidpid("{$g['varrun_path']}/syslog.pid")) {
439
		$sighup = false;
440
	}
441

    
442
	if (!$sighup) {
443
		if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
444
			sigkillbypid("{$g['varrun_path']}/syslog.pid", "TERM");
445
			usleep(100000); // syslogd often doesn't respond to a TERM quickly enough for the starting of syslogd below to be successful
446
		}
447

    
448
		if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
449
			// if it still hasn't responded to the TERM, KILL it.
450
			sigkillbypid("{$g['varrun_path']}/syslog.pid", "KILL");
451
			usleep(100000);
452
		}
453

    
454
		$retval = mwexec_bg("/usr/sbin/syslogd {$syslogd_format} -s -c -c {$syslogd_sockets} -P {$g['varrun_path']}/syslog.pid {$syslogd_extra}");
455
	} else {
456
		$retval = sigkillbypid("{$g['varrun_path']}/syslog.pid", "HUP");
457
	}
458

    
459
	if (!config_path_enabled('syslog', 'disablelocallogging')) {
460
		system_sshguard_start();
461
	}
462

    
463
	if (is_platform_booting()) {
464
		echo gettext("done.") . "\n";
465
	}
466

    
467
	return $retval;
468
}
469

    
470
function system_sshguard_start() {
471
	$sshguard_whitelist = array();
472
	if (!empty(config_get_path('system/sshguard_whitelist'))) {
473
		$sshguard_whitelist = explode(' ',
474
		    config_get_path('system/sshguard_whitelist'));
475
	}
476

    
477
	$sshguard_config = array();
478
	$sshguard_config[] = 'FILES="' . g_get('varlog_path') . '/auth.log"' . "\n";
479
	$sshguard_config[] = 'BACKEND="/usr/local/libexec/sshg-fw-pf"' . "\n";
480
	if (!empty(config_get_path('system/sshguard_threshold'))) {
481
		$sshguard_config[] = 'THRESHOLD=' .
482
		    config_get_path('system/sshguard_threshold') . "\n";
483
	}
484
	if (!empty(config_get_path('system/sshguard_blocktime'))) {
485
		$sshguard_config[] = 'BLOCK_TIME=' .
486
		    config_get_path('system/sshguard_blocktime') . "\n";
487
	}
488
	if (!empty(config_get_path('system/sshguard_detection_time'))) {
489
		$sshguard_config[] = 'DETECTION_TIME=' .
490
		    config_get_path('system/sshguard_detection_time') . "\n";
491
	}
492
	if (!empty($sshguard_whitelist)) {
493
		@file_put_contents("/usr/local/etc/sshguard.whitelist",
494
		    implode(PHP_EOL, $sshguard_whitelist));
495
		$sshguard_config[] =
496
		    'WHITELIST_FILE=/usr/local/etc/sshguard.whitelist' . "\n";
497
	} else {
498
		unlink_if_exists("/usr/local/etc/sshguard.whitelist");
499
	}
500
	file_put_contents("/usr/local/etc/sshguard.conf", $sshguard_config);
501

    
502
	mwexec_bg('/usr/sbin/daemon -R 60 -P /var/run/daemon_sshguard.pid -t sshguard /usr/local/sbin/sshguard -i /var/run/sshguard.pid');
503
}
504

    
505
function system_sshguard_stop() {
506
	mwexec_bg('pkill -f "' . str_replace('/', '\\/', g_get('varlog_path')) . '\\/auth\.log|[[:<:]](sshguard|sshg-parser|sshg-fw-pf|sshg-blocker)[[:>:]]"');
507
}
508

    
509
function system_log_get_compression() {
510
	global $g, $system_log_compression_types;
511
	$compressiontype = config_get_path('syslog/logcompressiontype');
512
	if (empty($compressiontype) ||
513
	    !array_key_exists($compressiontype, $system_log_compression_types)) {
514
		if (file_exists("{$g['conf_path']}/syslog_default_uncompressed")) {
515
			/* Disable log compression by default when flag file is present. */
516
			$compressiontype = 'none';
517
		} else {
518
			/* Default is bzip2 */
519
			$compressiontype = 'bzip2';
520
		}
521
	}
522
	return $compressiontype;
523
}
524

    
525
function system_log_get_cat() {
526
	global $system_log_compression_types;
527
	return $system_log_compression_types[system_log_get_compression()]['cat'];
528
}
529

    
530
/* Setup newsyslog log rotation */
531
function system_log_rotation_setup() {
532
	global $g, $system_log_files, $system_log_compression_types;
533
	$syslogcfg = config_get_path('syslog', []);
534

    
535
	$mainnewsyslogconf = <<<EOD
536
# Automatically generated, do not edit!
537
# Place configuration files in {$g['varetc_path']}/newsyslog.conf.d
538
<include> {$g['varetc_path']}/newsyslog.conf.d/*
539
# /* Manually added files with non-conflicting names will not be automatically removed */
540

    
541
EOD;
542

    
543
	file_put_contents("{$g['etc_path']}/newsyslog.conf", $mainnewsyslogconf);
544
	safe_mkdir("{$g['varetc_path']}/newsyslog.conf.d");
545

    
546
	$log_size = config_get_path('syslog/logfilesize', g_get('default_log_size'));
547
	$log_size = ($log_size < (2**32)/2) ? $log_size : g_get('default_log_size');
548
	$log_size = (int)$log_size/1024;
549

    
550
	$rotatecount = config_get_path('syslog/rotatecount', 7);
551
	$rotatecount = is_numericint($rotatecount) ? $rotatecount : 7;
552

    
553
	$compression_flag = $system_log_compression_types[system_log_get_compression()]['flag'];
554

    
555
	$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');
556
	$newsyslogconf = <<<EOD
557
/var/log/userlog		root:wheel	600	3	{$log_size}	*	B
558
/var/log/utx.log		root:wheel	644	3	{$log_size}	*	B
559

    
560
EOD;
561

    
562
	foreach (config_get_path('installedpackages/package', []) as $key => $package) {
563
		if (!empty(config_get_path("installedpackages/package/{$key}/logging", [])) &&
564
		    !empty(config_get_path("installedpackages/package/{$key}/logging/logfilename"))) {
565
			$pkg_log_filename = config_get_path("installedpackages/package/{$key}/logging/logfilename");
566
			$pkg_log_owner = config_get_path("installedpackages/package/{$key}/logging/logowner", 'root:wheel');
567
			$pkg_log_mode = config_get_path("installedpackages/package/{$key}/logging/logmode", '600');
568
			$pkg_rotate_count = (int) config_get_path("installedpackages/package/{$key}/logging/rotatecount", $rotatecount);
569
			$pkg_log_size = (int) config_get_path("installedpackages/package/{$key}/logging/logfilesize", 0);
570
			$pkg_log_size = ($pkg_log_size > 0) ? $pkg_log_size / 1024 : $log_size;
571
			$pkg_rotate_time = config_get_path("installedpackages/package/{$key}/logging/rotatetime", '*');
572
			$pkg_extra_flags = config_get_path("installedpackages/package/{$key}/logging/rotateflags", '');
573
			$pkg_pidcmd = config_get_path("installedpackages/package/{$key}/logging/pidcmd", '');
574
			$pkg_signal = config_get_path("installedpackages/package/{$key}/logging/signal", '');
575

    
576
			$pkgnewsyslogconf = "# Automatically generated for package {$package['name']}. Do not edit.\n";
577
			$pkgnewsyslogconf .= system_log_rotation_make_line("{$g['varlog_path']}/{$pkg_log_filename}",
578
										$pkg_log_owner,
579
										$pkg_log_mode,
580
										$pkg_rotate_count,
581
										$pkg_log_size,
582
										$pkg_rotate_time,
583
										"{$compression_flag}C{$pkg_extra_flags}",
584
										$pkg_pidcmd,
585
										$pkg_signal);
586
			@file_put_contents("{$g['varetc_path']}/newsyslog.conf.d/" . basename($pkg_log_filename) . ".conf", $pkgnewsyslogconf);
587
		}
588
	}
589

    
590
	foreach($system_log_files as $logfile) {
591
		$local_log_size = (int) config_get_path("syslog/" . basename($logfile, '.log') . "_settings/logfilesize", 0);
592
		$local_log_size = ($local_log_size > 0) ? $local_log_size / 1024 : $log_size;
593
		$local_rotate_count = (int) config_get_path("syslog/" . basename($logfile, '.log') . "_settings/rotatecount", $rotatecount);
594

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

    
598
	if (!@file_put_contents("{$g['varetc_path']}/newsyslog.conf.d/pfSense.conf", $newsyslogconf)) {
599
		printf(gettext("Error: cannot open syslog config file in system_log_rotation_setup().%s"), "\n");
600
	}
601
	unset($newsyslogconf);
602
}
603

    
604
function system_log_rotation_make_line($filename, $owner = 'root:wheel', $mode='644', $count=7, $size=1000, $time='*', $flags='C', $pidcmd = '', $signal = '') {
605
	/* TODO: Fix default size, flags, etc. */
606
	$nslline = $filename;
607
	$nslline .= "		{$owner}";
608
	$nslline .= "	{$mode}";
609
	$nslline .= "	{$count}";
610
	$nslline .= "	{$size}";
611
	$nslline .= "	{$time}";
612
	$nslline .= "	{$flags}";
613
	if (!empty($pidcmd)) {
614
		$nslline .= "	{$pidcmd}";
615
	}
616
	if (!empty($signal)) {
617
		$nslline .= "	{$signal}";
618
	}
619
	$nslline .= "\n";
620
	return $nslline;
621
}
622

    
623
function dump_log($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $format = 'table', $grepreverse = false) {
624
	global $g;
625
	$sor = (config_path_enabled('syslog', 'reverse') || $grepreverse) ? "-r" : "";
626
	$specific_log = basename($logfile, '.log') . '_settings';
627
	$cronorder = config_get_path("syslog/{$specific_log}/cronorder");
628
	if (($cronorder == 'forward') && !$grepreverse) {
629
		$sor = "";
630
	}
631
	if (($cronorder == 'reverse') ||  $grepreverse) {
632
		$sor = "-r";
633
	}
634
	$logarr = array();
635
	$grepline = "  ";
636
	if (is_array($grepfor)) {
637
		$invert = '';
638
		if ((strpos($grepfor[0], '!') === 0)) {
639
			$grepfor[0] = substr($grepfor[0], 1);
640
			$invert = '-v';
641
		}
642
		/* Ensure the user-supplied filter is sane */
643
		$filtertext = cleanup_regex_pattern(implode("|", $grepfor));
644
		if (!empty($filtertext)) {
645
			$grepline .= " | /usr/bin/egrep {$invert} -- " . escapeshellarg($filtertext);
646
		}
647
	}
648
	if (is_array($grepinvert)) {
649
		/* Ensure the user-supplied filter is sane */
650
		$filtertext = cleanup_regex_pattern(implode("|", $grepinvert));
651
		if (!empty($filtertext)) {
652
			$grepline .= " | /usr/bin/egrep -v -- " . escapeshellarg($filtertext);
653
		}
654
	}
655
	if (is_dir($logfile)) {
656
		$logarr = array(sprintf(gettext("File %s is a directory."), $logfile));
657
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
658
		$logarr = array(gettext("Log file started."));
659
	} else {
660
		exec(system_log_get_cat() . ' ' . sort_related_log_files($logfile, true, true) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
661
	}
662

    
663
	if ($format == 'none') {
664
		return($logarr);
665
	}
666

    
667
	$rows = 0;
668
	foreach ($logarr as $logent) {
669
		$rows++;
670
		$entry_date_time = "";
671

    
672
		/* Determine log entry content */
673
		$splitlogent = preg_split("/\s+/", $logent, 10);
674
		if (strpos($splitlogent[0], '>') === false) {
675
			/* RFC 3164 Format */
676
			$syslogformat = 'rfc3164';
677
		} else {
678
			/* RFC 5424 Format */
679
			$syslogformat = 'rfc5424';
680
		}
681

    
682
		if ($format == 'raw') {
683
			$entry_text = $logent;
684
		} elseif ($withorig) {
685
			if ($syslogformat == 'rfc3164') {
686
				$entry_date_time = htmlspecialchars(join(" ", array_slice($splitlogent, 0, 3)));
687
				$entry_text = ($splitlogent[3] == config_get_path('system/hostname')) ? "" : $splitlogent[3] . " ";
688
				$entry_text .= implode(' ', array_slice($splitlogent, 4));
689
			} else {
690
				$entry_date_time = htmlspecialchars($splitlogent[1]);
691
				$entry_text = ($splitlogent[2] == config_get_path('system/hostname') . '.' . config_get_path('system/domain')) ? '' : $splitlogent[2] . ' ';
692
				$entry_text .= implode(' ', array_slice($splitlogent, 3));
693
			}
694
		} else {
695
			$entry_text = implode(' ', array_slice($splitlogent, ($syslogformat == 'rfc3164') ? 5 : 9));
696
		}
697
		$entry_text = htmlspecialchars($entry_text);
698

    
699
		/* Output content in desired format. */
700
		switch ($format) {
701
			case 'notable':
702
				echo implode(' ', array($entry_date_time, $entry_text)) . "\n";
703
				break;
704
			case 'raw':
705
				$span = 'colspan="2"';
706
			case 'table':
707
			default:
708
				echo "<tr>\n";
709
				if (!empty($entry_date_time)) {
710
					echo "	<td class=\"text-nowrap\">{$entry_date_time}</td>\n";
711
				}
712
				echo "	<td {$span} style=\"word-wrap:break-word; word-break:break-all; white-space:normal\">{$entry_text}</td>\n";
713
				echo "</tr>\n";
714
				break;
715
		}
716
	}
717

    
718
	return($rows);
719
}
720

    
721
/* format filter logs */
722
function conv_log_filter($logfile, $nentries, $tail = 500, $filtertext = "", $filterinterface = null) {
723
	global $g, $pattern;
724

    
725
	/* Make sure this is a number before using it in a system call */
726
	if (!(is_numeric($tail))) {
727
		return;
728
	}
729

    
730
	/* Safety belt to ensure we get enough lines for filtering without overloading the parsing code */
731
	if ($filtertext) {
732
		$tail = 10000;
733
	}
734

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

    
738
	$logtypecheck = preg_replace('/\.log$/', '', basename($logfile));
739
	if (in_array($logtypecheck, array('system', 'gateways', 'routing', 'resolver', 'wireless', 'nginx', 'dmesg.boot', 'portalauth', 'dhcpd', 'ipsec', 'ppp', 'openvpn', 'ntpd', 'userlog', 'auth'))) {
740
		$logfile_type = "system";
741
	} elseif (in_array($logtypecheck, array('filter'))) {
742
		$logfile_type = "firewall";
743
	} elseif (in_array($logtypecheck, array('vpn'))) {
744
		$logfile_type = "vpn_login";
745
	} elseif (in_array($logtypecheck, array('poes', 'l2tps'))) {
746
		$logfile_type = "vpn_service";
747
	} elseif (in_array($logtypecheck, array('utx'))) {
748
		$logfile_type = "utx";
749
	} else {
750
		$logfile_type = "unknown";
751
	}
752

    
753
# Common Regular Expression Patterns
754
	$base_patterns = array(
755
		'rfc3164' => array('start_pattern' => "^"),
756
		'rfc5424' => array('start_pattern' => "^<[0-9]{1,3}>[0-9]*\ "),
757
	);
758

    
759
	/* bsd log date */
760
	$base_patterns['rfc3164']['date_pattern'] = "\([a-zA-Z]{3}\ +[0-9]{1,2}\ +[0-9]{2}:[0-9]{2}:[0-9]{2}\)";
761
	/* RFC3339 Extended format date */
762
	$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]))\)";
763

    
764
	/* hostname only, might not be present */
765
	$base_patterns['rfc3164']['host_pattern'] = "\(.*?\)";
766
	/* FQDN, always present */
767
	$base_patterns['rfc5424']['host_pattern'] = "\(\S+?\)";
768

    
769
	/* name[pid]:, pid may not be present */
770
	$base_patterns['rfc3164']['process_pattern'] = "\(.*?\)\(?::\ +\)?";
771
	/* name alone, nothing special */
772
	$base_patterns['rfc5424']['process_pattern'] = "\(\S+?\)\ ";
773

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

    
779
	/* match anything from here on */
780
	$base_patterns['rfc3164']['log_message_pattern'] = "\(.*\)";
781
	$base_patterns['rfc5424']['log_message_pattern'] = "\(.*\)";
782

    
783
	$patterns = array(
784
		'rfc3164' => $base_patterns['rfc3164']['start_pattern'],
785
		'rfc5424' => $base_patterns['rfc5424']['start_pattern'],
786
	);
787

    
788
	# Construct RegEx for specific log file type.
789
	switch ($logfile_type) {
790
		case 'firewall':
791
			$patterns['rfc3164'] = "filterlog\[[0-9]+\]:";
792
			$patterns['rfc5424'] = "filterlog";
793
			break;
794
		case 'system':
795
		case 'vpn_service':
796
			$patterns['rfc3164'] .= $base_patterns['rfc3164']['date_pattern'] . "\ +" .
797
						$base_patterns['rfc3164']['host_pattern'] . "\ +" .
798
						$base_patterns['rfc3164']['process_pattern'] .
799
						$base_patterns['rfc3164']['pid_pattern'] . "\ +" .
800
						$base_patterns['rfc3164']['log_message_pattern'] . "$";
801
			$patterns['rfc5424'] .= $base_patterns['rfc5424']['date_pattern'] . "\ " .
802
						$base_patterns['rfc5424']['host_pattern'] . "\ " .
803
						$base_patterns['rfc5424']['process_pattern'] .
804
						$base_patterns['rfc5424']['pid_pattern'] . "\ " .
805
						$base_patterns['rfc5424']['log_message_pattern'] . "$";
806
			break;
807
		case 'vpn_login':
808
			$action_pattern = "\(.*?\)";
809
			$type_pattern = "\(.*?\)";
810
			$ip_address_pattern = "\(.*?\)";
811
			$user_pattern = "\(.*?\)";
812
			$patterns['rfc3164'] .= $base_patterns['rfc3164']['date_pattern'] . "\ +" .
813
						$base_patterns['rfc3164']['host_pattern'] . "\ +" .
814
						$base_patterns['rfc3164']['process_pattern'] . "\ +" .
815
						"\(.*?\)\,\ *" .
816
						"\(.*?\)\,\ *" .
817
						"\(.*?\)\,\ *" .
818
						"\(.*?\)$";
819
			$patterns['rfc5424'] .= $base_patterns['rfc5424']['date_pattern'] . "\ " .
820
						$base_patterns['rfc5424']['host_pattern'] . "\ " .
821
						$base_patterns['rfc5424']['process_pattern'] .
822
						"\S+?\ \S+?\ \S+?\ " .
823
						"\(\S*?\)\,\ *" .
824
						"\(\S*?\)\,\ *" .
825
						"\(\S*?\)\,\ *" .
826
						"\(\S*?\)$";
827
			break;
828
		case 'unknown':
829
			$patterns['rfc3164'] .= $base_patterns['rfc3164']['date_pattern'] . "*\ +" .
830
						$base_patterns['rfc3164']['log_message_pattern'] . "$";
831
			$patterns['rfc5424'] .= $base_patterns['rfc5424']['date_pattern'] . "*\ " .
832
						$base_patterns['rfc5424']['log_message_pattern'] . "$";
833
			break;
834
		default:
835
			$patterns['rfc3164'] .= "\(.*\)$";
836
			$patterns['rfc5424'] .= "\(.*\)$";
837
	}
838

    
839
	if ($logfile_type != 'utx') {
840
		# Get a bunch of log entries.
841
		exec(system_log_get_cat() . ' ' . sort_related_log_files($logfile, true, true) . " | /usr/bin/tail -r -n {$tail}", $logarr);
842
	} else {
843
		$_gb = exec("/usr/bin/last --libxo=json", $rawdata, $rc);
844
		if ($rc == 0) {
845
			$logarr = json_decode(implode(" ", $rawdata), JSON_OBJECT_AS_ARRAY);
846
			$logarr = $logarr['last-information']['last'];
847
		}
848
	}
849

    
850
	# Remove escapes and fix up the pattern for preg_match.
851
	$patterns['rfc3164'] = '/' . str_replace(array('\(', '\)', '\[', '\]'), array('(', ')', '[', ']'), $patterns['rfc3164']) . '/';
852
	$patterns['rfc5424'] = '/' . str_replace(array('\(', '\)', '\[', '\]'), array('(', ')', '[', ']'), $patterns['rfc5424']) . '/';
853

    
854
	$filterlog = array();
855
	$counter = 0;
856

    
857
	$filterinterface = strtoupper($filterinterface);
858
	foreach ($logarr as $logent) {
859
		if ($counter >= $nentries) {
860
			break;
861
		}
862
		if ($logfile_type != 'utx') {
863
			$pattern = (substr($logent, 0, 1) == '<') ? $patterns['rfc5424'] : $patterns['rfc3164'];
864
		}
865
		switch($logfile_type) {
866
			case 'firewall':
867
				$flent = parse_firewall_log_line($logent);
868
				break;
869
			case 'system':
870
				$flent = parse_system_log_line($logent);
871
				break;
872
			case 'vpn_login':
873
				$flent = parse_vpn_login_log_line($logent);
874
				break;
875
			case 'vpn_service':
876
				$flent = parse_vpn_service_log_line($logent);
877
				break;
878
			case 'utx':
879
				$flent = parse_utx_log_line($logent);
880
				break;
881
			case 'unknown':
882
				$flent = parse_unknown_log_line($logent);
883
				break;
884
			default:
885
				$flent = array();
886
				break;
887
		}
888

    
889
		if (!$filterinterface || ($filterinterface == $flent['interface'])) {
890
			if ((($flent != "") && (!is_array($filtertext)) && (match_filter_line($flent, $filtertext))) ||
891
			    (($flent != "") && (is_array($filtertext)) && (match_filter_field($flent, $filtertext)))) {
892
				$counter++;
893
				$filterlog[] = $flent;
894
			}
895
		}
896
	}
897
	/* Since the lines are in reverse order, flip them around if needed based on the user's preference */
898
	# First get the "General Logging Options" (global) chronological order setting.  Then apply specific log override if set.
899
	$reverse = config_path_enabled('syslog','reverse');
900
	$specific_log = basename($logfile, '.log') . '_settings';
901
	$cronorder = config_get_path("syslog/{$specific_log}/cronorder");
902
	if ($cronorder == 'forward') {
903
		$reverse = false;
904
	}
905
	if ($cronorder == 'reverse') {
906
		$reverse = true;
907
	}
908

    
909
	return ($reverse) ? $filterlog : array_reverse($filterlog);
910
}
911

    
912
function match_filter_line($flent, $filtertext = "") {
913
	if (!$filtertext) {
914
		return true;
915
	}
916
	$filtertext = escape_filter_regex(str_replace(' ', '\s+', $filtertext));
917
	return @preg_match("/{$filtertext}/i", implode(" ", array_values($flent)));
918
}
919

    
920
function match_filter_field($flent, $fields) {
921
	foreach ($fields as $key => $field) {
922
		if ($field == "All") {
923
			continue;
924
		}
925
		if ((strpos($field, '!') === 0)) {
926
			$field = substr($field, 1);
927
			if (strtolower($key) == 'act') {
928
				if (in_arrayi($flent[$key], explode(" ", $field))) {
929
					return false;
930
				}
931
			} else {
932
				$field_regex = escape_filter_regex($field);
933
				if (@preg_match("/{$field_regex}/i", $flent[$key])) {
934
					return false;
935
				}
936
			}
937
		} else {
938
			if (strtolower($key) == 'act') {
939
				if (!in_arrayi($flent[$key], explode(" ", $field))) {
940
					return false;
941
				}
942
			} else {
943
				$field_regex = escape_filter_regex($field);
944
				if (!@preg_match("/{$field_regex}/i", $flent[$key])) {
945
					return false;
946
				}
947
			}
948
		}
949
	}
950
	return true;
951
}
952

    
953
// Case Insensitive in_array function
954
function in_arrayi($needle, $haystack) {
955
	return in_array(strtolower($needle), array_map('strtolower', $haystack));
956
}
957

    
958
function parse_vpn_login_log_line($line) {
959
	global $g, $pattern;
960

    
961
	$flent = array();
962
	$log_split = "";
963

    
964
	if (!preg_match($pattern, $line, $log_split))
965
		return "";
966

    
967
	list($all, $flent['time'], $flent['host'], $flent['process'], $flent['action'], $flent['type'], $flent['ip_address'], $flent['user']) = $log_split;
968
	$flent['time'] = str_replace('T', ' ', $flent['time']);
969

    
970
	/* If there is time, action, user, and IP address fields, then the line should be usable/good */
971
	if (!( (trim($flent['time']) == "") && (trim($flent['action']) == "") && (trim($flent['user']) == "") && (trim($flent['ip_address']) == "") )) {
972
		return $flent;
973
	} else {
974
		if (g_get('debug')) {
975
			log_error(sprintf(gettext("There was a error parsing log entry: %s. Please report to mailing list or forum."), $line));
976
		}
977
		return "";
978
	}
979
}
980

    
981
function parse_vpn_service_log_line($line) {
982
	global $g, $pattern;
983

    
984
	$flent = array();
985
	$log_split = "";
986

    
987
	if (!preg_match($pattern, $line, $log_split))
988
		return "";
989

    
990
	list($all, $flent['time'], $flent['host'], $flent['type'], $flent['pid'], $flent['message']) = $log_split;
991
	$flent['time'] = str_replace('T', ' ', $flent['time']);
992

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

    
1004
function parse_utx_log_line($line) {
1005
	$flent['time'] = "{$line['login-time']}";
1006
	if ($line['logout-time']) {
1007
		$flent['time'] .= " - {$line['logout-time']}";
1008
	}
1009
	$flent['host'] = $line['from'];
1010
	$flent['pid'] = $line['tty'];
1011
	$flent['message'] = $line['user'];
1012
	if ($line['session-length']) {
1013
		$flent['process'] = $line['session-length'];
1014
	}
1015
	return $flent;
1016
}
1017

    
1018
function parse_unknown_log_line($line) {
1019
	global $g, $pattern;
1020

    
1021
	$flent = array();
1022
	$log_split = "";
1023

    
1024
	if (!preg_match($pattern, $line, $log_split)) {
1025
		return "";
1026
	}
1027

    
1028
	list($all, $flent['time'], $flent['message']) = $log_split;
1029
	$flent['time'] = str_replace('T', ' ', $flent['time']);
1030

    
1031
	/* If there is time, and message, fields, then the line should be usable/good */
1032
	if (!((trim($flent['time']) == "") && (trim($flent['message']) == ""))) {
1033
		return $flent;
1034
	} else {
1035
		if (g_get('debug')) {
1036
			log_error(sprintf(gettext("There was a error parsing log entry: %s. Please report to mailing list or forum."), $line));
1037
		}
1038
		return "";
1039
	}
1040
}
1041

    
1042
function parse_system_log_line($line) {
1043
	global $g, $pattern;
1044

    
1045
	$flent = array();
1046
	$log_split = "";
1047

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

    
1052
	list($all, $flent['time'], $flent['host'], $flent['process'], $flent['pid'], $flent['message']) = $log_split;
1053
	$flent['time'] = str_replace('T', ' ', $flent['time']);
1054

    
1055
	/* If there is time, process, and message, fields, then the line should be usable/good */
1056
	if (!((trim($flent['time']) == "") && (trim($flent['process']) == "") && (trim($flent['message']) == ""))) {
1057
		return $flent;
1058
	} else {
1059
		if (g_get('debug')) {
1060
			log_error(sprintf(gettext("There was a error parsing log entry: %s. Please report to mailing list or forum."), $line));
1061
		}
1062
		return "";
1063
	}
1064
}
1065

    
1066
function parse_firewall_log_line($line) {
1067
	global $g;
1068

    
1069
	$flent = array();
1070
	$log_split = "";
1071

    
1072
	if (substr($line, 0, 1) == '<') {
1073
		/* RFC 5424 */;
1074
		$pattern = "/^<[0-9]{1,3}>[0-9]*\ (\S+?)\ (\S+?)\ filterlog\ \S+?\ \S+?\ \S+?\ (.*)$/U";
1075
	} else {
1076
		/* RFC 3164 */
1077
		$pattern = "/(.*)\s(.*)\sfilterlog\[[0-9]+\]:\s(.*)$/";
1078
	}
1079

    
1080
	if (!preg_match($pattern, $line, $log_split)) {
1081
		return "";
1082
	}
1083

    
1084
	list($all, $flent['time'], $host, $rule) = $log_split;
1085
	$flent['time'] = str_replace('T', ' ', $flent['time']);
1086

    
1087
	$rule_data = array_filter(explode(",", $rule));
1088
	$field = 0;
1089

    
1090
	$flent['rulenum'] = $rule_data[$field++];
1091
	$flent['subrulenum'] = $rule_data[$field++];
1092
	$flent['anchor'] = $rule_data[$field++];
1093
	$flent['tracker'] = $rule_data[$field++];
1094
	$flent['realint'] = $rule_data[$field++];
1095
	$flent['interface'] = convert_real_interface_to_friendly_descr($flent['realint']);
1096
	$flent['reason'] = $rule_data[$field++];
1097
	$flent['act'] = $rule_data[$field++];
1098
	$flent['direction'] = $rule_data[$field++];
1099
	$flent['version'] = $rule_data[$field++];
1100

    
1101
	if ($flent['version'] == '4' || $flent['version'] == '6') {
1102
		if ($flent['version'] == '4') {
1103
			$flent['tos'] = $rule_data[$field++];
1104
			$flent['ecn'] = $rule_data[$field++];
1105
			$flent['ttl'] = $rule_data[$field++];
1106
			$flent['id'] = $rule_data[$field++];
1107
			$flent['offset'] = $rule_data[$field++];
1108
			$flent['flags'] = $rule_data[$field++];
1109
			$flent['protoid'] = $rule_data[$field++];
1110
			$flent['proto'] = strtoupper($rule_data[$field++]);
1111
		} else {
1112
			$flent['class'] = $rule_data[$field++];
1113
			$flent['flowlabel'] = $rule_data[$field++];
1114
			$flent['hlim'] = $rule_data[$field++];
1115
			$flent['proto'] = $rule_data[$field++];
1116
			$flent['protoid'] = $rule_data[$field++];
1117
		}
1118

    
1119
		$flent['length'] = $rule_data[$field++];
1120
		$flent['srcip'] = $rule_data[$field++];
1121
		$flent['dstip'] = $rule_data[$field++];
1122

    
1123
		switch($flent['protoid']) {
1124
			case '6':
1125
			case '17': // TCP or UDP
1126
			case '132': // SCTP
1127
				$flent['srcport'] = $rule_data[$field++];
1128
				$flent['dstport'] = $rule_data[$field++];
1129

    
1130
				$flent['src'] = $flent['srcip'] . ':' . $flent['srcport'];
1131
				$flent['dst'] = $flent['dstip'] . ':' . $flent['dstport'];
1132

    
1133
				$flent['datalen'] = $rule_data[$field++];
1134
				if ($flent['protoid'] == '6') { // TCP
1135
					$flent['tcpflags'] = $rule_data[$field++];
1136
					$flent['seq'] = $rule_data[$field++];
1137
					$flent['ack'] = $rule_data[$field++];
1138
					$flent['window'] = $rule_data[$field++];
1139
					$flent['urg'] = $rule_data[$field++];
1140
					$flent['options'] = explode(";", $rule_data[$field++]);
1141
				}
1142
				break;
1143
			case '1':
1144
			case '58': // ICMP (IPv4 & IPv6)
1145
				$flent['src'] = $flent['srcip'];
1146
				$flent['dst'] = $flent['dstip'];
1147

    
1148
				$flent['icmp_type'] = $rule_data[$field++];
1149

    
1150
				switch ($flent['icmp_type']) {
1151
					case "request":
1152
					case "reply":
1153
						$flent['icmp_id'] = $rule_data[$field++];
1154
						$flent['icmp_seq'] = $rule_data[$field++];
1155
						break;
1156
					case "unreachproto":
1157
						$flent['icmp_dstip'] = $rule_data[$field++];
1158
						$flent['icmp_protoid'] = $rule_data[$field++];
1159
						break;
1160
					case "unreachport":
1161
						$flent['icmp_dstip'] = $rule_data[$field++];
1162
						$flent['icmp_protoid'] = $rule_data[$field++];
1163
						$flent['icmp_port'] = $rule_data[$field++];
1164
						break;
1165
					case "unreach":
1166
					case "timexceed":
1167
					case "paramprob":
1168
					case "redirect":
1169
					case "maskreply":
1170
						$flent['icmp_descr'] = $rule_data[$field++];
1171
						break;
1172
					case "needfrag":
1173
						$flent['icmp_dstip'] = $rule_data[$field++];
1174
						$flent['icmp_mtu'] = $rule_data[$field++];
1175
						break;
1176
					case "tstamp":
1177
						$flent['icmp_id'] = $rule_data[$field++];
1178
						$flent['icmp_seq'] = $rule_data[$field++];
1179
						break;
1180
					case "tstampreply":
1181
						$flent['icmp_id'] = $rule_data[$field++];
1182
						$flent['icmp_seq'] = $rule_data[$field++];
1183
						$flent['icmp_otime'] = $rule_data[$field++];
1184
						$flent['icmp_rtime'] = $rule_data[$field++];
1185
						$flent['icmp_ttime'] = $rule_data[$field++];
1186
						break;
1187
					default :
1188
						$flent['icmp_descr'] = $rule_data[$field++];
1189
						break;
1190
				}
1191
				break;
1192
			case '112': // CARP
1193
				$flent['type'] = $rule_data[$field++];
1194
				$flent['ttl'] = $rule_data[$field++];
1195
				$flent['vhid'] = $rule_data[$field++];
1196
				$flent['version'] = $rule_data[$field++];
1197
				$flent['advskew'] = $rule_data[$field++];
1198
				$flent['advbase'] = $rule_data[$field++];
1199
				$flent['src'] = $flent['srcip'];
1200
				$flent['dst'] = $flent['dstip'];
1201
				break;
1202
			default:
1203
				$flent['src'] = $flent['srcip'];
1204
				$flent['dst'] = $flent['dstip'];
1205
				break;
1206
		}
1207
	} else {
1208
		if (g_get('debug')) {
1209
			log_error(sprintf(gettext("There was a error parsing rule number: %s. Please report to mailing list or forum."), $flent['rulenum']));
1210
		}
1211
		return "";
1212
	}
1213

    
1214
	/* If there is a src, a dst, and a time, then the line should be usable/good */
1215
	if (!((trim($flent['src']) == "") || (trim($flent['dst']) == "") || (trim($flent['time']) == ""))) {
1216
		return $flent;
1217
	} else {
1218
		if (g_get('debug')) {
1219
			log_error(sprintf(gettext("There was a error parsing rule: %s. Please report to mailing list or forum."), $line));
1220
		}
1221
		return "";
1222
	}
1223
}
1224

    
1225
function get_port_with_service($port, $proto) {
1226
	if (!$port || !is_port($port)) {
1227
		return '';
1228
	}
1229

    
1230
	$service = getservbyport($port, $proto);
1231
	$portstr = "";
1232
	if ($service) {
1233
		$portstr = sprintf('<span title="' . gettext('Service %1$s/%2$s: %3$s') . '">' . htmlspecialchars($port) . '</span>', $port, $proto, $service);
1234
	} else {
1235
		$portstr = htmlspecialchars($port);
1236
	}
1237
	return ':' . $portstr;
1238
}
1239

    
1240
function find_rule_by_number($rulenum, $trackernum, $type="block") {
1241
	global $g;
1242

    
1243
	/* Passing arbitrary input to grep could be a Very Bad Thing(tm) */
1244
	if (!is_numeric($rulenum) || !is_numeric($trackernum)) {
1245
		return;
1246
	}
1247

    
1248
	switch ($type) {
1249
		case 'pass':
1250
		case 'block':
1251
		case 'match':
1252
		case 'rdr':
1253
			break;
1254
		case 'unkn(%u)':
1255
			// Work around filterlog issue with 'match' rules.
1256
			$type = 'match';
1257
			break;
1258
		default:
1259
			return;
1260
	}
1261

    
1262
	$command = '/sbin/pfctl -vvPsr';
1263
	if ($type == "rdr") {
1264
		/* miniupnpd generates logging rdr rules */
1265
		$lookup_pattern = "/^@{$rulenum}/m";
1266
		$command = '/sbin/pfctl -vvPsn -a "miniupnpd"';
1267
	} elseif ($trackernum == "0") {
1268
		$lookup_pattern = "/^@{$rulenum}\([0-9]+\)[[:blank:]]{$type}[[:blank:]].*[[:blank:]]log[[:blank:]]/m";
1269
	} else {
1270
		$lookup_pattern = "/^@[0-9]+[[:blank:]]{$type}[[:blank:]].*[[:blank:]]log[[:blank:]].*[[:blank:]]ridentifier[[:blank:]]{$trackernum}$/m";
1271
	}
1272

    
1273
	$output = exec_command($command);
1274
	$result = 'unavailable';
1275
	$matches = [];
1276
	if (preg_match($lookup_pattern, $output, $matches)) {
1277
		$result = $matches[0];
1278
	} elseif ($type == 'block') {
1279
		// Check for IP options; check 'pass' with no log.
1280
		$lookup_pattern = "/^@([0-9]+)[[:blank:]]pass[[:blank:]].*[[:blank:]]ridentifier[[:blank:]]{$trackernum}$/m";
1281
		if (preg_match_all($lookup_pattern, $output, $matches)) {
1282
			// Default to the first match.
1283
			$result = $matches[0][0];
1284
			if (count($matches[0]) > 1) {
1285
				// If the rule is a IPv4+IPv6 rule, there may be multiple matches.
1286
				foreach ($matches[1] as $idx => $value) {
1287
					if ($rulenum == $value) {
1288
						$result = $matches[0][$idx];
1289
						break;
1290
					}
1291
				}
1292
			}
1293
		}
1294
	}
1295

    
1296
	return $result;
1297
}
1298

    
1299
function buffer_rules_load() {
1300
	global $g, $buffer_rules_rdr, $buffer_rules_normal;
1301
	unset($buffer, $buffer_rules_rdr, $buffer_rules_normal);
1302
	/* Redeclare globals after unset to work around PHP */
1303
	global $buffer_rules_rdr, $buffer_rules_normal;
1304
	$buffer_rules_rdr = array();
1305
	$buffer_rules_normal = array();
1306

    
1307
	$_gb = exec("/sbin/pfctl -vvPsn -a \"miniupnpd\" | /usr/bin/grep '^@'", $buffer);
1308
	if (is_array($buffer)) {
1309
		foreach ($buffer as $line) {
1310
			list($key, $value) = explode (" ", $line, 2);
1311
			$buffer_rules_rdr[$key] = $value;
1312
		}
1313
	}
1314

    
1315
	$rules = pfSense_get_pf_rules();
1316
	if (is_array($rules)) {
1317
		foreach ($rules as $rule) {
1318
			$key = sprintf("%d", $rule['tracker']);
1319
			$buffer_rules_normal[$key] = is_array($rule['labels']) ? array_shift($rule['labels']) : $rule['labels'];
1320
		}
1321
	}
1322
	unset($_gb, $buffer);
1323
}
1324

    
1325
function buffer_rules_clear() {
1326
	unset($GLOBALS['buffer_rules_normal']);
1327
	unset($GLOBALS['buffer_rules_rdr']);
1328
}
1329

    
1330
function find_rule_by_number_buffer($rulenum, $trackernum, $type) {
1331
	global $g, $buffer_rules_rdr, $buffer_rules_normal;
1332

    
1333
	if ($trackernum == "0") {
1334
		$lookup_key = "@{$rulenum}";
1335
	} else {
1336
		$lookup_key = $trackernum;
1337
	}
1338

    
1339
	if ($type == "rdr") {
1340
		$ruleString = $buffer_rules_rdr[$lookup_key];
1341
		//TODO: get the correct 'description' part of a RDR log line. currently just first 30 characters..
1342
		$rulename = substr($ruleString, 0, 30);
1343
	} else {
1344
		$ruleString = $buffer_rules_normal[$lookup_key];
1345
		$rulename = str_replace("USER_RULE: ", '<i class="fa-solid fa-user"></i> ', $ruleString);
1346
	}
1347
	return "{$rulename} ({$lookup_key})";
1348
}
1349

    
1350
function find_action_image($action) {
1351
	global $g;
1352
	if ((strstr(strtolower($action), "p")) || (strtolower($action) == "rdr")) {
1353
		return "fa-regular fa-circle-check";
1354
	} else if (strstr(strtolower($action), "r")) {
1355
		return "fa-regular fa-circle-xmark";
1356
	} else {
1357
		return "fa-solid fa-ban";
1358
	}
1359
}
1360

    
1361
/* AJAX specific handlers */
1362
function handle_ajax() {
1363
	if ($_REQUEST['lastsawtime'] && $_REQUEST['logfile']) {
1364

    
1365
		$lastsawtime = getGETPOSTsettingvalue('lastsawtime', null);
1366
		$logfile = getGETPOSTsettingvalue('logfile', null);
1367
		$nentries = getGETPOSTsettingvalue('nentries', null);
1368
		$type = getGETPOSTsettingvalue('type', null);
1369
		$filter = getGETPOSTsettingvalue('filter', null);
1370
		$interfacefilter = getGETPOSTsettingvalue('interfacefilter', null);
1371

    
1372
		if (!empty(trim($filter)) || is_numeric($filter)) {
1373
			$filter = json_decode($filter, true);	# Filter Fields Array or Filter Text
1374
		}
1375

    
1376
		/* compare lastsawrule's time stamp to filter logs.
1377
		 * afterwards return the newer records so that client
1378
		 * can update AJAX interface screen.
1379
		 */
1380
		$new_rules = "";
1381

    
1382
		$filterlog = conv_log_filter($logfile, $nentries, $nentries + 100, $filter, $interfacefilter);
1383

    
1384
		/* We need this to always be in forward order for the AJAX update to work properly */
1385
		/* Since the lines are in reverse order, flip them around if needed based on the user's preference */
1386
		# First get the "General Logging Options" (global) chronological order setting.  Then apply specific log override if set.
1387
		$reverse = config_path_enabled('syslog', 'reverse');
1388
		$specific_log = basename($logfile, '.log') . '_settings';
1389
		$cronorder = config_get_path("syslog/{$specific_log}/cronorder");
1390
		if ($cronorder == 'forward') {
1391
			$reverse = false;
1392
		}
1393
		if ($cronorder == 'reverse') {
1394
			$reverse = true;
1395
		}
1396

    
1397
		$filterlog = ($reverse) ? array_reverse($filterlog) : $filterlog;
1398

    
1399
		foreach ($filterlog as $log_row) {
1400
			$row_time = strtotime($log_row['time']);
1401
			if ($row_time > $lastsawtime) {
1402
				if ($log_row['proto'] == "TCP") {
1403
					$log_row['proto'] .= ":{$log_row['tcpflags']}";
1404
				}
1405

    
1406
				if ($log_row['act'] == "block") {
1407
					$icon_act = "fa-solid fa-times text-danger";
1408
				} else {
1409
					$icon_act = "fa-solid fa-check text-success";
1410
				}
1411

    
1412
				$btn = '<i class="' . $icon_act . ' icon-pointer" title="' . $log_row['act'] . '/' . $log_row['reason'] .'/'. $log_row['tracker'] . '" onclick="javascript:getURL(\'status_logs_filter.php?getrulenum=' . $log_row['rulenum'] . ',' . $log_row['tracker'] . ',' . $log_row['act'] . '\', outputrule);"></i>';
1413
				$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";
1414
			}
1415
		}
1416
		echo $new_rules;
1417
		exit;
1418
	}
1419
}
1420

    
1421
function print_syslog_rule_action($rule) {
1422
	$pf_rule = htmlspecialchars(find_rule_by_number($rule['rulenum'], $rule['tracker'], $rule['act']));
1423
	$margin = $rule['count'] ? '0em' : '0.4em';
1424
	$reason = htmlspecialchars($rule['reason']);
1425
	$tracker = htmlspecialchars($rule['tracker']);
1426

    
1427
	// "match" action is not supported - filterlog writes it as 'unkn(%u)'
1428
	$icon_class = match ($rule['act']) {
1429
		'block' => 'fa-solid fa-times text-danger',
1430
		'reject' => 'fa-regular fa-hand text-warning',
1431
		default => 'fa-solid fa-filter'
1432
	};
1433
	switch ($rule['act']) {
1434
		case 'pass':
1435
		case 'block':
1436
		case 'match':
1437
		case 'rdr':
1438
			$action = $rule['act'];
1439
			break;
1440
		default:
1441
			$action = 'unknown';
1442
			break;
1443
	}
1444

    
1445
	return '<a data-toggle="popover" data-trigger="hover focus" title="' . gettext('Rule details') . '" data-content="' .
1446
		"Action: {$action}<br />Reason: {$reason}<br />Tracker ID: {$tracker}<br />Rule: {$pf_rule}" .
1447
		'" data-html="true"><i style="margin-left:' . $margin . '" class="' . $icon_class . ' icon-pointer"></i></a>';
1448
}
1449

    
1450
/* Compatibility stubs for old clog functions until packages catch up.
1451
 * Remove once packages all use the new function names. */
1452
function dump_clog_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
1453
	return dump_log($logfile, $tail, $withorig, $grepfor, $grepinvert, $format = 'notable');
1454
}
1455
function dump_log_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
1456
	return dump_log($logfile, $tail, $withorig, $grepfor, $grepinvert, $format = 'notable');
1457
}
1458
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
1459
	return dump_log($logfile, $tail, $withorig, $grepfor, $grepinvert);
1460
}
1461
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
1462
	return dump_log($logfile, $tail, $withorig, $grepfor, $grepinvert, $format = 'none', $grepreverse);
1463
}
1464
function return_log($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
1465
	return dump_log($logfile, $tail, $withorig, $grepfor, $grepinvert, $format = 'none', $grepreverse);
1466
}
(49-49/61)