diff options
author | Simon Rettberg | 2021-06-25 16:21:17 +0200 |
---|---|---|
committer | Simon Rettberg | 2021-06-25 16:21:17 +0200 |
commit | 32f0677dbca9e3347b931c1d0105eb37aa57e90d (patch) | |
tree | ddad4562e7ee8439a24e2462d44614692bb71d14 /modules-available/syslog | |
parent | Update .idea (diff) | |
download | slx-admin-32f0677dbca9e3347b931c1d0105eb37aa57e90d.tar.gz slx-admin-32f0677dbca9e3347b931c1d0105eb37aa57e90d.tar.xz slx-admin-32f0677dbca9e3347b931c1d0105eb37aa57e90d.zip |
[eventlog] Add event filtering and notification system
Diffstat (limited to 'modules-available/syslog')
-rw-r--r-- | modules-available/syslog/api.inc.php | 20 | ||||
-rw-r--r-- | modules-available/syslog/inc/clientlog.inc.php | 47 |
2 files changed, 53 insertions, 14 deletions
diff --git a/modules-available/syslog/api.inc.php b/modules-available/syslog/api.inc.php index 3378afe6..cc64b31c 100644 --- a/modules-available/syslog/api.inc.php +++ b/modules-available/syslog/api.inc.php @@ -64,25 +64,17 @@ $longdesc = ''; if (isset($_POST['longdesc'])) $longdesc = $_POST['longdesc']; $longdesc = Request::post('longdesc', '', 'string'); -if ($type[0] !== '.' && $type[0] !== '~') { +if (preg_match('/^[a-z0-9\-]+$/', $type)) { - // Spam from IP - $row = Database::queryFirst('SELECT Count(*) AS cnt FROM clientlog WHERE clientip = :client AND dateline + 1800 > UNIX_TIMESTAMP()', array(':client' => $ip)); + // Spam from IP? + $row = Database::queryFirst('SELECT Count(*) AS cnt FROM clientlog + WHERE clientip = :client AND dateline + 1800 > UNIX_TIMESTAMP()', + [':client' => $ip]); if ($row !== false && $row['cnt'] > 250) { exit(0); } - $ret = Database::exec('INSERT INTO clientlog (dateline, logtypeid, clientip, machineuuid, description, extra) VALUES (UNIX_TIMESTAMP(), :type, :client, :uuid, :description, :longdesc)', array( - 'type' => $type, - 'client' => $ip, - 'description' => $description, - 'longdesc' => $longdesc, - 'uuid' => $uuid, - ), true); - if ($ret === false) { - error_log("Constraint failed for client log from $uuid for $type : $description"); - die("NOPE.\n"); - } + ClientLog::write(['machineuuid' => $uuid, 'clientip' => $ip], $type, $description, $longdesc); } diff --git a/modules-available/syslog/inc/clientlog.inc.php b/modules-available/syslog/inc/clientlog.inc.php new file mode 100644 index 00000000..b38c29fe --- /dev/null +++ b/modules-available/syslog/inc/clientlog.inc.php @@ -0,0 +1,47 @@ +<?php + +class ClientLog +{ + + public static function write(array $client, string $type, string $description, string $longDesc = ''): bool + { + if (!isset($client['machineuuid']) && !isset($client['clientip'])) { + error_log("Bad clientlog write call: " . json_encode($client)); + return false; + } + if (!isset($client['machineuuid'])) { + $res = Database::queryFirst("SELECT machineuuid FROM machine WHERE clientip = :ip + ORDER BY lastseen DESC LIMIT 1", ['ip' => $client['clientip']]); + if ($res === false) { + error_log("Invalid client IP for client log: " . $client['clientip']); + return false; + } + $client['machineuuid'] = $res['machineuuid']; + } + if (!isset($client['clientip'])) { + $res = Database::queryFirst("SELECT clientip FROM machine WHERE machineuuid = :uuid", + ['uuid' => $client['machineuuid']]); + if ($res === false) { + error_log("Invalid machine uuid for client log: " . $client['machineuuid']); + return false; + } + $client['clientip'] = $res['clientip']; + } + $data = [ + 'type' => $type, + 'clientip' => $client['clientip'], + 'description' => $description, + 'extra' => $longDesc, + 'machineuuid' => $client['machineuuid'], + ]; + $res = Database::exec('INSERT INTO clientlog (dateline, logtypeid, clientip, machineuuid, description, extra) + VALUES (UNIX_TIMESTAMP(), :type, :clientip, :machineuuid, :description, :extra)', $data, true); + if ($res === false) { + error_log("Constraint failed for client log from {$client['machineuuid']} for $type : $description"); + return false; + } + EventLog::applyFilterRules($type, $data + $client); + return true; + } + +}
\ No newline at end of file |