Skip to content
Snippets Groups Projects
Verified Commit 3a28c684 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

Archivist: Allow arbitrary data in several columns

User inputs (ssh commands, names, passwords) may contain invalid UTF-8
data, therefore store them in BYTEA columns. The main database already
uses BYTEA. Make sure the data are fed to the DB in the correct format,
with a trick with bind_param.
parent 1e0c1415
Branches
1 merge request!11Archivist: Allow arbitrary data in several columns
#!/usr/bin/perl
use common::sense;
use DBI;
use DBD::Pg qw(:pg_types); # Import the DBD::Pg::PG_BYTEA constant (and other similar ones)
use Config::IniFiles;
use List::Util qw(sum);
use Date::Format;
......@@ -473,9 +474,18 @@ if (fork == 0) {
my $get_commands = $source->prepare('SELECT ssh_commands.id, start_time, end_time, login, password, remote, ts, success, command FROM ssh_commands JOIN ssh_sessions ON ssh_commands.session_id = ssh_sessions.id WHERE NOT archived');
my $mark_command = $source->prepare('UPDATE ssh_commands SET archived = TRUE WHERE id = ?');
my $store_command = $destination->prepare('INSERT INTO ssh_commands (session, timestamp, success, command) VALUES (?, ?, ?, ?)');
# Make sure the params are considered the correct type.
# bind_param does two things here:
# * Sets the value of the parameter to NULL (which we'll override by calling execute with a new value).
# * Sets the data type for the column (which stays across the future calls to bind_param or execute).
$store_command->bind_param(4, undef, { pg_type => DBD::Pg::PG_BYTEA });
my $get_session = $destination->prepare('SELECT id, end_time FROM ssh_sessions WHERE start_time = ? AND login = ? AND password = ?');
$get_session->bind_param(2, undef, { pg_type => DBD::Pg::PG_BYTEA });
$get_session->bind_param(3, undef, { pg_type => DBD::Pg::PG_BYTEA });
my $update_session = $destination->prepare('UPDATE ssh_sessions SET end_time = ? WHERE id = ?');
my $store_session = $destination->prepare('INSERT INTO ssh_sessions (start_time, end_time, login, password, remote) VALUES (?, ?, ?, ?, ?) RETURNING id');
$store_session->bind_param(3, undef, { pg_type => DBD::Pg::PG_BYTEA });
$store_session->bind_param(4, undef, { pg_type => DBD::Pg::PG_BYTEA });
$get_commands->execute;
my $count_commands = 0;
my $count_sessions = 0;
......@@ -521,6 +531,12 @@ if (fork == 0) {
my $get_passwords = $source->prepare("SELECT timestamp, server, remote, name, password, remote_port FROM fake_logs WHERE name IS NOT NULL AND password IS NOT NULL AND event = 'login' AND timestamp >= ?");
$get_passwords->execute($max_date);
my $put_password = $destination->prepare("INSERT INTO fake_passwords (timestamp, server, remote, name, password, remote_port) VALUES (?, ?, ?, ?, ?, ?)");
# Make sure the params are considered the correct type.
# bind_param does two things here:
# * Sets the value of the parameter to NULL (which we'll override by calling execute with a new value).
# * Sets the data type for the column (which stays across the future calls to bind_param or execute).
$put_password->bind_param(4, undef, { pg_type => DBD::Pg::PG_BYTEA });
$put_password->bind_param(5, undef, { pg_type => DBD::Pg::PG_BYTEA });
my $passwords = -1;
$put_password->execute_for_fetch(sub {
$passwords ++;
......
......@@ -342,8 +342,8 @@ CREATE TABLE fake_passwords (
server fake_server NOT NULL,
remote INET NOT NULL,
remote_port INT,
name TEXT NOT NULL,
password TEXT NOT NULL
name BYTEA NOT NULL,
password BYTEA NOT NULL
);
CREATE TABLE fake_server_activity (
date DATE NOT NULL,
......@@ -361,8 +361,8 @@ CREATE TABLE ssh_sessions (
id INT NOT NULL PRIMARY KEY,
start_time TIMESTAMP WITH TIME ZONE NOT NULL,
end_time TIMESTAMP WITH TIME ZONE,
login TEXT NOT NULL,
password TEXT NOT NULL,
login BYTEA NOT NULL,
password BYTEA NOT NULL,
remote INET,
UNIQUE(start_time, login, password)
);
......@@ -373,7 +373,7 @@ CREATE TABLE ssh_commands (
session INT NOT NULL,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL,
success BOOL NOT NULL,
command TEXT NOT NULL,
command BYTEA NOT NULL,
FOREIGN KEY (session) REFERENCES ssh_sessions(id) ON DELETE CASCADE
);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment