Restore
Restore
Restore file to captured state
Every captured change carries its SHA-256-addressed content in BLOB_STORE. Restoring writes that content back to the original path — current file gets backed up to <path>.drift_restore_backup_<epoch> before overwrite.
What will change
Preview -- nothing has been written yet.
| Target file | /var/www/vhosts/3dshawn.com/site1/_agent_scan.pl |
| Site | DriftSense self-monitor on local |
| Kind | local |
| Captured at | 2026-07-11 23:03:27 |
| Captured SHA | e833d76d304c6db47c44560e638ca05c3f3b88c121df6079b48324d01b082e95 |
Current state on disk
Live check just now. If SHAs match, the restore is a no-op.
| File present | yes |
| Size | 16577 bytes |
| Current SHA | bf5b44533145 |
| Same as captured? | no -- restore will change it |
What restore will change — live diff
Left column shows current on-disk content; right shows what restore will write. +5 additions, -52 deletions, 402 unchanged context lines.
| 1 | 1 | #!/usr/bin/perl |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # DriftSense -- SSH agent scanner (Stealth Flavor 2) |
| 4 | 4 | # |
| 5 | 5 | # Cron entry point (NOT a CGI). For each active SERVERS row with |
| 6 | 6 | # kind='ssh_agent', SSHes into the monitored host using the configured |
| 7 | 7 | # restricted key and walks the paths in that server's FILE_MONITOR_SETTINGS. |
| 8 | 8 | # |
| 9 | 9 | # Monitored host runs zero DriftSense code -- only a single command- |
| 10 | 10 | # restricted authorized_keys entry. The wrapper on the remote side |
| 11 | 11 | # only allows 3 commands: |
| 12 | 12 | # |
| 13 | 13 | # drift-find <path> -> path\tsize\tmtime\n per file |
| 14 | 14 | # drift-hash <files> -> sha256sum output |
| 15 | 15 | # drift-read <file> -> raw bytes |
| 16 | 16 | # |
| 17 | 17 | # We drive it via SSH_ORIGINAL_COMMAND. If the wrapper isn't installed, |
| 18 | 18 | # we fall back to plain `find`, `sha256sum`, `cat` on the assumption |
| 19 | 19 | # that the SSH user has a normal shell (test-mode). |
| 20 | 20 | # |
| 21 | 21 | # Config: /etc/drift_sense/drift_sense.conf (via MODS::Config). |
| 22 | 22 | # Log: /var/log/drift_sense/agent_scan.log (via cron redirect). |
| 23 | 23 | #====================================================================== |
| 24 | 24 | use strict; |
| 25 | 25 | use warnings; |
| 26 | 26 | use lib '/var/www/vhosts/3dshawn.com/site1'; |
| 27 | 27 | use POSIX (); |
| 28 | 28 | use Digest::SHA qw(sha256_hex); |
| 29 | 29 | use Compress::Zlib qw(compress); |
| 30 | 30 | use IPC::Open3; |
| 31 | 31 | use Symbol 'gensym'; |
| 32 | 32 | use MODS::Config; |
| 33 | 33 | use MODS::DBConnect; |
| 34 | use MODS::Crypto; | |
| 35 | use Fcntl (); | |
| 36 | 34 | |
| 37 | 35 | $| = 1; |
| 38 | 36 | my $ts = POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime); |
| 39 | 37 | |
| 40 | 38 | my $cfg = MODS::Config->new; |
| 41 | 39 | my $db = MODS::DBConnect->new; |
| 42 | 40 | my $dbh = $db->db_connect or die "[$ts] cannot connect to DriftSense storage DB\n"; |
| 43 | 41 | |
| 44 | 42 | my $default_max_size = int($cfg->settings('max_file_size_bytes') || 10_485_760); |
| 45 | 43 | my $DEFAULT_TIMEOUT = 30; |
| 46 | ||
| 47 | # ---- Global scan settings (SCAN_GLOBALS singleton) ----------------- | |
| 48 | my $g_row = $db->db_readwrite($dbh, q~ | |
| 49 | SELECT global_ignore_list, global_file_type_filter, global_max_file_size_bytes | |
| 50 | FROM SCAN_GLOBALS WHERE id = 1 LIMIT 1 | |
| 51 | ~, __FILE__, __LINE__); | |
| 52 | my $global_ignore = ($g_row && $g_row->{global_ignore_list}) // ''; | |
| 53 | my $global_ftypes = ($g_row && $g_row->{global_file_type_filter}) // ''; | |
| 54 | my $global_max_size = ($g_row && $g_row->{global_max_file_size_bytes}) || 0; | |
| 55 | $default_max_size = $global_max_size if $global_max_size; | |
| 56 | 44 | |
| 57 | 45 | # ---- Fetch ssh_agent servers ---------------------------------------- |
| 58 | 46 | my @servers = $db->db_readwrite_multiple($dbh, q~ |
| 59 | 47 | SELECT server_id, server_name, ssh_host, ssh_user, ssh_port, |
| 60 | ssh_key_path, ssh_options, ssh_key_blob, ssh_key_enc | |
| 48 | ssh_key_path, ssh_options | |
| 61 | 49 | FROM SERVERS |
| 62 | 50 | WHERE kind = 'ssh_agent' |
| 63 | 51 | AND status = 'active' |
| 64 | 52 | ~, __FILE__, __LINE__); |
| 65 | 53 | |
| 66 | 54 | unless (@servers) { |
| 67 | 55 | print "[$ts] no active ssh_agent servers, exiting\n"; |
| 68 | 56 | $db->db_disconnect($dbh); |
| 69 | 57 | exit 0; |
| 70 | 58 | } |
| 71 | 59 | |
| 72 | 60 | my $total_added = 0; |
| 73 | 61 | my $total_modified = 0; |
| 74 | 62 | my $total_deleted = 0; |
| 75 | 63 | |
| 76 | 64 | foreach my $server (@servers) { |
| 77 | 65 | my $sid = $server->{server_id}; |
| 78 | 66 | my $host = $server->{ssh_host} || next; |
| 79 | 67 | my $user = $server->{ssh_user} || 'root'; |
| 80 | 68 | my $port = $server->{ssh_port} || 22; |
| 69 | my $key = $server->{ssh_key_path} || ''; | |
| 81 | 70 | my $opts = $server->{ssh_options} || ''; |
| 82 | ||
| 83 | # Resolve key: prefer encrypted blob (decrypt to a mode-600 temp | |
| 84 | # file for the SSH call), else fall back to on-disk ssh_key_path. | |
| 85 | my $key_path = $server->{ssh_key_path} || ''; | |
| 86 | my $tmp_key; | |
| 87 | if ($server->{ssh_key_blob}) { | |
| 88 | my $pem = eval { MODS::Crypto::decrypt($server->{ssh_key_blob}) }; | |
| 89 | if ($pem) { | |
| 90 | $tmp_key = _materialize_key($pem); | |
| 91 | $key_path = $tmp_key if $tmp_key; | |
| 92 | } else { | |
| 93 | print "[$ts] server $server->{server_name}: key decrypt failed ($@)\n"; | |
| 94 | } | |
| 95 | } | |
| 96 | 71 | |
| 97 | my @ssh_base = _ssh_argv($user, $host, $port, $key_path, $opts); | |
| 72 | my @ssh_base = _ssh_argv($user, $host, $port, $key, $opts); | |
| 98 | 73 | |
| 99 | 74 | # Fetch this server's active file monitors |
| 100 | 75 | my @scans = $db->db_readwrite_multiple($dbh, qq~ |
| 101 | 76 | SELECT file_monitor_list_id, scan_path, ignore_list, file_type_filter, |
| 102 | 77 | max_file_size_bytes, scan_name |
| 103 | 78 | FROM FILE_MONITOR_SETTINGS |
| 104 | 79 | WHERE status = 1 |
| 105 | 80 | AND server_id = $sid |
| 106 | 81 | ~, __FILE__, __LINE__); |
| 107 | 82 | |
| 108 | 83 | unless (@scans) { |
| 109 | 84 | print "[$ts] server '$server->{server_name}' has no active monitors\n"; |
| 110 | 85 | next; |
| 111 | 86 | } |
| 112 | 87 | |
| 113 | 88 | my $server_err; |
| 114 | 89 | |
| 115 | 90 | foreach my $scan (@scans) { |
| 116 | 91 | my $path = $scan->{scan_path} or next; |
| 117 | 92 | my $max_size = $scan->{max_file_size_bytes} || $default_max_size; |
| 118 | 93 | |
| 119 | # UNION global + per-monitor ignore lists | |
| 120 | my $combined_ignore = join(',', grep { length } ($global_ignore, ($scan->{ignore_list} // ''))); | |
| 121 | my %ignore_pats = _build_ignore_patterns($combined_ignore); | |
| 122 | # Per-monitor filter wins if set; else global | |
| 123 | my $eff_ftypes = length($scan->{file_type_filter} // '') | |
| 124 | ? $scan->{file_type_filter} | |
| 125 | : $global_ftypes; | |
| 126 | my %type_filter = _build_type_filter($eff_ftypes); | |
| 94 | my %ignore_pats = _build_ignore_patterns($scan->{ignore_list}); | |
| 95 | my %type_filter = _build_type_filter($scan->{file_type_filter}); | |
| 127 | 96 | |
| 128 | 97 | # ---- Remote listing ----------------------------------------- |
| 129 | 98 | my $remote_cmd = qq{drift-find $path}; |
| 130 | 99 | my ($rc, $out, $err) = _ssh_run(\@ssh_base, $remote_cmd); |
| 131 | 100 | if ($rc != 0) { |
| 132 | 101 | # Fallback: no wrapper -> plain shell |
| 133 | 102 | my $shell = qq{find } . _sh_quote($path) . qq{ -type f -not -path '*/.*' -printf '%p\\t%s\\t%T@\\n' 2>/dev/null}; |
| 134 | 103 | ($rc, $out, $err) = _ssh_run(\@ssh_base, $shell); |
| 135 | 104 | } |
| 136 | 105 | if ($rc != 0) { |
| 137 | 106 | $server_err = "list failed: $err"; |
| 138 | 107 | print "[$ts] server '$server->{server_name}' scan '$scan->{scan_name}': $server_err\n"; |
| 139 | 108 | next; |
| 140 | 109 | } |
| 141 | 110 | |
| 142 | 111 | # Parse listing: path\tsize\tmtime_epoch |
| 143 | 112 | my %remote; |
| 144 | 113 | foreach my $line (split /\n/, $out) { |
| 145 | 114 | chomp $line; |
| 146 | 115 | next unless length $line; |
| 147 | 116 | my ($p, $sz, $mt) = split /\t/, $line, 3; |
| 148 | 117 | next unless defined $p && defined $sz && defined $mt; |
| 149 | 118 | $sz = int($sz); |
| 150 | 119 | $mt = int($mt); # printf gives float mtime, truncate |
| 151 | 120 | |
| 152 | 121 | # Ignore-glob filter (local side) |
| 153 | 122 | my $skip = 0; |
| 154 | 123 | for my $pat (keys %ignore_pats) { |
| 155 | 124 | my $re = $ignore_pats{$pat}; |
| 156 | 125 | if ($p =~ $re) { $skip = 1; last; } |
| 157 | 126 | } |
| 158 | 127 | next if $skip; |
| 159 | 128 | |
| 160 | 129 | # File-type filter (local side) |
| 161 | 130 | if (%type_filter) { |
| 162 | 131 | my $ext_ok = 0; |
| 163 | 132 | for my $e (keys %type_filter) { |
| 164 | 133 | if (lc(substr($p, -length($e))) eq $e) { $ext_ok = 1; last; } |
| 165 | 134 | } |
| 166 | 135 | next unless $ext_ok; |
| 167 | 136 | } |
| 168 | 137 | |
| 169 | 138 | next if $sz > $max_size; |
| 170 | 139 | $remote{$p} = { size => $sz, mtime => $mt }; |
| 171 | 140 | } |
| 172 | 141 | |
| 173 | 142 | # ---- Previous state ---------------------------------------- |
| 174 | 143 | my $prev_sth = $dbh->prepare(qq{ |
| 175 | 144 | SELECT fc.file_name, fc.blob_sha, fc.status, |
| 176 | 145 | UNIX_TIMESTAMP(fc.date_time) AS mt_epoch |
| 177 | 146 | FROM FILE_CHANGES fc |
| 178 | 147 | WHERE fc.file_monitor_list_id = $scan->{file_monitor_list_id} |
| 179 | 148 | AND fc.server_id = $sid |
| 180 | 149 | AND fc.file_changes_id IN ( |
| 181 | 150 | SELECT MAX(inner_fc.file_changes_id) |
| 182 | 151 | FROM FILE_CHANGES inner_fc |
| 183 | 152 | WHERE inner_fc.file_monitor_list_id = $scan->{file_monitor_list_id} |
| 184 | 153 | AND inner_fc.server_id = $sid |
| 185 | 154 | GROUP BY inner_fc.file_name |
| 186 | 155 | ) |
| 187 | 156 | }); |
| 188 | 157 | my %prev; |
| 189 | 158 | if ($prev_sth && $prev_sth->execute) { |
| 190 | 159 | while (my $r = $prev_sth->fetchrow_hashref) { |
| 191 | 160 | $prev{$r->{file_name}} = $r; |
| 192 | 161 | } |
| 193 | 162 | $prev_sth->finish; |
| 194 | 163 | } |
| 195 | 164 | |
| 196 | 165 | # ---- Decide who needs a fresh hash -------------------------- |
| 197 | 166 | my @needs_hash; |
| 198 | 167 | foreach my $p (keys %remote) { |
| 199 | 168 | my $prev_row = $prev{$p}; |
| 200 | 169 | if (!$prev_row || ($prev_row->{status} // '') eq 'deleted') { |
| 201 | 170 | push @needs_hash, $p; |
| 202 | 171 | next; |
| 203 | 172 | } |
| 204 | 173 | if (($prev_row->{mt_epoch} // 0) != $remote{$p}{mtime}) { |
| 205 | 174 | push @needs_hash, $p; |
| 206 | 175 | } |
| 207 | 176 | } |
| 208 | 177 | |
| 209 | 178 | # ---- Batch hash over SSH ----------------------------------- |
| 210 | 179 | # `drift-hash <files>` returns sha256sum output: "<sha> <path>" |
| 211 | 180 | my %fresh_sha; |
| 212 | 181 | if (@needs_hash) { |
| 213 | 182 | my $q_list = join(' ', map { _sh_quote($_) } @needs_hash); |
| 214 | 183 | my $cmd = "drift-hash $q_list"; |
| 215 | 184 | my ($h_rc, $h_out, $h_err) = _ssh_run(\@ssh_base, $cmd); |
| 216 | 185 | if ($h_rc != 0) { |
| 217 | 186 | # Fallback: plain sha256sum |
| 218 | 187 | $cmd = "sha256sum $q_list 2>/dev/null"; |
| 219 | 188 | ($h_rc, $h_out, $h_err) = _ssh_run(\@ssh_base, $cmd); |
| 220 | 189 | } |
| 221 | 190 | if ($h_rc == 0) { |
| 222 | 191 | foreach my $line (split /\n/, $h_out) { |
| 223 | 192 | chomp $line; |
| 224 | 193 | if ($line =~ /^([0-9a-f]{64})\s+(.+)$/) { |
| 225 | 194 | $fresh_sha{$2} = $1; |
| 226 | 195 | } |
| 227 | 196 | } |
| 228 | 197 | } |
| 229 | 198 | } |
| 230 | 199 | |
| 231 | 200 | # ---- Process each remote file ------------------------------ |
| 232 | 201 | foreach my $p (keys %remote) { |
| 233 | 202 | my $sha = $fresh_sha{$p}; |
| 234 | 203 | my $prev_row = $prev{$p}; |
| 235 | 204 | |
| 236 | 205 | # If we didn't rehash this file, previous SHA still valid |
| 237 | 206 | if (!defined $sha && $prev_row && ($prev_row->{status} // '') ne 'deleted') { |
| 238 | 207 | # Nothing changed since last tick -- move on |
| 239 | 208 | next; |
| 240 | 209 | } |
| 241 | 210 | |
| 242 | 211 | # Content-addressable dedup: if we already have this SHA in |
| 243 | 212 | # BLOB_STORE, don't re-fetch content over SSH. |
| 244 | 213 | my $have = $db->db_readwrite($dbh, |
| 245 | 214 | "SELECT blob_sha FROM BLOB_STORE WHERE blob_sha = " . $dbh->quote($sha) . " LIMIT 1", |
| 246 | 215 | __FILE__, __LINE__); |
| 247 | 216 | |
| 248 | 217 | my $status; |
| 249 | 218 | if (!$prev_row) { |
| 250 | 219 | $status = 'added'; |
| 251 | 220 | } else { |
| 252 | 221 | $status = 'modified'; |
| 253 | 222 | if ($prev_row->{blob_sha} && $prev_row->{blob_sha} eq $sha) { |
| 254 | 223 | # Same bytes, just an mtime touch -- flag ts-only |
| 255 | 224 | $status = 'modified'; |
| 256 | 225 | } |
| 257 | 226 | } |
| 258 | 227 | |
| 259 | 228 | # Fetch + store blob only if we don't already have it |
| 260 | 229 | if (!($have && $have->{blob_sha})) { |
| 261 | 230 | my $cmd = 'drift-read ' . _sh_quote($p); |
| 262 | 231 | my ($c_rc, $c_out, $c_err) = _ssh_run(\@ssh_base, $cmd); |
| 263 | 232 | if ($c_rc != 0) { |
| 264 | 233 | $cmd = 'cat ' . _sh_quote($p); |
| 265 | 234 | ($c_rc, $c_out, $c_err) = _ssh_run(\@ssh_base, $cmd); |
| 266 | 235 | } |
| 267 | 236 | if ($c_rc != 0) { |
| 268 | 237 | print "[$ts] fetch $p on $server->{server_name}: $c_err\n"; |
| 269 | 238 | next; |
| 270 | 239 | } |
| 271 | 240 | # Verify the fetched content matches the reported hash |
| 272 | 241 | my $verify_sha = sha256_hex($c_out); |
| 273 | 242 | if ($verify_sha ne $sha) { |
| 274 | 243 | print "[$ts] $p: hash mismatch (list=$sha vs read=$verify_sha), using read\n"; |
| 275 | 244 | $sha = $verify_sha; |
| 276 | 245 | } |
| 277 | 246 | _store_blob($db, $dbh, $sha, $c_out); |
| 278 | 247 | } |
| 279 | 248 | |
| 280 | 249 | # Determine timestamp-only via prev SHA compare |
| 281 | 250 | my $is_ts_only = 0; |
| 282 | 251 | if ($prev_row && $prev_row->{blob_sha} && $prev_row->{blob_sha} eq $sha) { |
| 283 | 252 | $is_ts_only = 1; |
| 284 | 253 | } |
| 285 | 254 | |
| 286 | 255 | # Insert FILE_CHANGES row keyed to this server |
| 287 | 256 | my $q_file = $dbh->quote($p); |
| 288 | 257 | my $q_sha = $dbh->quote($sha); |
| 289 | 258 | my $q_stat = $dbh->quote($status); |
| 290 | 259 | my $mt = $remote{$p}{mtime}; |
| 291 | 260 | $db->db_readwrite($dbh, qq{ |
| 292 | 261 | INSERT INTO FILE_CHANGES |
| 293 | 262 | (file_monitor_list_id, server_id, container_target_id, |
| 294 | 263 | file_name, blob_sha, is_ts_only, date_time, status) |
| 295 | 264 | VALUES |
| 296 | 265 | ($scan->{file_monitor_list_id}, $sid, NULL, |
| 297 | 266 | $q_file, $q_sha, $is_ts_only, |
| 298 | 267 | FROM_UNIXTIME($mt), $q_stat) |
| 299 | 268 | }, __FILE__, __LINE__); |
| 300 | 269 | |
| 301 | 270 | if ($is_ts_only) { } |
| 302 | 271 | elsif ($status eq 'added') { $total_added++; } |
| 303 | 272 | else { $total_modified++; } |
| 304 | 273 | } |
| 305 | 274 | |
| 306 | 275 | # ---- Delete detection -------------------------------------- |
| 307 | 276 | foreach my $p (keys %prev) { |
| 308 | 277 | next if $remote{$p}; |
| 309 | 278 | next if ($prev{$p}{status} // '') eq 'deleted'; |
| 310 | 279 | my $q_file = $dbh->quote($p); |
| 311 | 280 | $db->db_readwrite($dbh, qq{ |
| 312 | 281 | INSERT INTO FILE_CHANGES |
| 313 | 282 | (file_monitor_list_id, server_id, file_name, status, date_time) |
| 314 | 283 | VALUES |
| 315 | 284 | ($scan->{file_monitor_list_id}, $sid, $q_file, 'deleted', NOW()) |
| 316 | 285 | }, __FILE__, __LINE__); |
| 317 | 286 | $total_deleted++; |
| 318 | 287 | } |
| 319 | 288 | |
| 320 | 289 | $db->db_readwrite($dbh, qq{ |
| 321 | 290 | UPDATE FILE_MONITOR_SETTINGS |
| 322 | 291 | SET last_scanned = NOW() |
| 323 | 292 | WHERE file_monitor_list_id = $scan->{file_monitor_list_id} |
| 324 | 293 | }, __FILE__, __LINE__); |
| 325 | 294 | } |
| 326 | 295 | |
| 327 | 296 | # Server-level bookkeeping |
| 328 | 297 | my $q_err = $dbh->quote($server_err || ''); |
| 329 | 298 | $db->db_readwrite($dbh, qq{ |
| 330 | 299 | UPDATE SERVERS |
| 331 | 300 | SET last_agent_scan_at = NOW(), |
| 332 | 301 | last_agent_error = $q_err, |
| 333 | 302 | last_heartbeat_at = NOW() |
| 334 | 303 | WHERE server_id = $sid |
| 335 | 304 | }, __FILE__, __LINE__); |
| 336 | ||
| 337 | # Clean up decrypted-key temp file | |
| 338 | unlink $tmp_key if $tmp_key && $tmp_key =~ m!^/tmp/\.ds_agent_key_!; | |
| 339 | 305 | } |
| 340 | 306 | |
| 341 | 307 | $db->db_disconnect($dbh); |
| 342 | 308 | print "[$ts] agent scan complete: +$total_added added, ~$total_modified modified, -$total_deleted deleted\n" |
| 343 | 309 | if ($total_added + $total_modified + $total_deleted) > 0; |
| 344 | 310 | exit 0; |
| 345 | 311 | |
| 346 | 312 | #--------------------------------------------------------------------- |
| 347 | 313 | sub _ssh_argv { |
| 348 | 314 | my ($user, $host, $port, $key, $opts) = @_; |
| 349 | 315 | my @a = ('/usr/bin/ssh', |
| 350 | 316 | '-o', 'BatchMode=yes', |
| 351 | 317 | '-o', 'StrictHostKeyChecking=no', |
| 352 | 318 | '-o', 'UserKnownHostsFile=/dev/null', |
| 353 | 319 | '-o', 'LogLevel=ERROR', |
| 354 | 320 | '-o', 'ConnectTimeout=8', |
| 355 | 321 | '-p', $port); |
| 356 | 322 | if ($key && -r $key) { |
| 357 | 323 | push @a, '-i', $key; |
| 358 | 324 | } |
| 359 | 325 | if ($opts) { |
| 360 | 326 | # Space-separated -o key=value pairs |
| 361 | 327 | foreach my $o (split /\s+/, $opts) { |
| 362 | 328 | push @a, '-o', $o if length $o; |
| 363 | 329 | } |
| 364 | 330 | } |
| 365 | 331 | push @a, "$user\@$host"; |
| 366 | 332 | return @a; |
| 367 | 333 | } |
| 368 | 334 | |
| 369 | 335 | sub _ssh_run { |
| 370 | 336 | my ($base, $cmd) = @_; |
| 371 | 337 | my @argv = (@$base, $cmd); |
| 372 | 338 | my ($wtr, $rdr, $err_fh); |
| 373 | 339 | $err_fh = gensym; |
| 374 | 340 | my $pid = eval { open3($wtr, $rdr, $err_fh, @argv) }; |
| 375 | 341 | return (-1, '', $@) if $@ || !$pid; |
| 376 | 342 | close $wtr; |
| 377 | 343 | binmode $rdr; |
| 378 | 344 | my $out = do { local $/; <$rdr> // '' }; |
| 379 | 345 | my $err = do { local $/; <$err_fh> // '' }; |
| 380 | 346 | close $rdr; close $err_fh; |
| 381 | 347 | waitpid $pid, 0; |
| 382 | 348 | my $rc = $? >> 8; |
| 383 | 349 | return ($rc, $out, $err); |
| 384 | } | |
| 385 | ||
| 386 | sub _materialize_key { | |
| 387 | my ($pem) = @_; | |
| 388 | return undef unless defined $pem && length $pem; | |
| 389 | my $t = POSIX::strftime('%s', localtime); | |
| 390 | my $path = "/tmp/.ds_agent_key_${t}_$$_" . int(rand(1_000_000)); | |
| 391 | sysopen(my $fh, $path, | |
| 392 | Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_EXCL(), | |
| 393 | 0600) or return undef; | |
| 394 | print $fh $pem; | |
| 395 | close $fh; | |
| 396 | return $path; | |
| 397 | 350 | } |
| 398 | 351 | |
| 399 | 352 | sub _sh_quote { |
| 400 | 353 | my $s = shift; $s //= ''; |
| 401 | 354 | # Single-quote and escape internal single quotes |
| 402 | 355 | $s =~ s/'/'\\''/g; |
| 403 | 356 | return "'$s'"; |
| 404 | 357 | } |
| 405 | 358 | |
| 406 | 359 | sub _build_ignore_patterns { |
| 407 | 360 | my $list = shift; |
| 408 | 361 | my %out; |
| 409 | 362 | return %out unless $list; |
| 410 | 363 | for my $ig (split /[,\n]+/, $list) { |
| 411 | 364 | $ig =~ s/^\s+|\s+$//g; |
| 412 | 365 | next unless length $ig; |
| 413 | 366 | my $re = quotemeta $ig; |
| 414 | 367 | $re =~ s/\\\*/.*/g; |
| 415 | 368 | $re =~ s/\\\?/./g; |
| 416 | 369 | $out{$ig} = qr/$re/; |
| 417 | 370 | } |
| 418 | 371 | return %out; |
| 419 | 372 | } |
| 420 | 373 | |
| 421 | 374 | sub _build_type_filter { |
| 422 | 375 | my $list = shift; |
| 423 | 376 | my %out; |
| 424 | 377 | return %out unless $list && length $list; |
| 425 | 378 | for my $ext (split /,/, $list) { |
| 426 | 379 | $ext =~ s/^\s+|\s+$//g; |
| 427 | 380 | next unless length $ext; |
| 428 | 381 | $ext = ".$ext" unless $ext =~ /^\./; |
| 429 | 382 | $out{lc $ext} = 1; |
| 430 | 383 | } |
| 431 | 384 | return %out; |
| 432 | 385 | } |
| 433 | 386 | |
| 434 | 387 | sub _store_blob { |
| 435 | 388 | my ($db, $dbh, $sha, $content) = @_; |
| 436 | 389 | my $existing = $db->db_readwrite($dbh, |
| 437 | 390 | "SELECT blob_sha FROM BLOB_STORE WHERE blob_sha = " . $dbh->quote($sha) . " LIMIT 1", |
| 438 | 391 | __FILE__, __LINE__); |
| 439 | 392 | if ($existing && $existing->{blob_sha}) { |
| 440 | 393 | $db->db_readwrite($dbh, |
| 441 | 394 | "UPDATE BLOB_STORE SET ref_count = ref_count + 1 WHERE blob_sha = " . $dbh->quote($sha), |
| 442 | 395 | __FILE__, __LINE__); |
| 443 | 396 | return; |
| 444 | 397 | } |
| 445 | 398 | my $gz = compress($content) // ''; |
| 446 | 399 | my $sth = $dbh->prepare(qq{ |
| 447 | 400 | INSERT INTO BLOB_STORE (blob_sha, content_gz, size_uncompressed, size_compressed, ref_count, first_seen_at) |
| 448 | 401 | VALUES (?, ?, ?, ?, 1, NOW()) |
| 449 | 402 | }); |
| 450 | 403 | if ($sth) { |
| 451 | 404 | $sth->execute($sha, $gz, length($content), length($gz)); |
| 452 | 405 | $sth->finish; |
| 453 | 406 | } |
| 454 | 407 | } |
Confirm restore
Backs current content to <target>.drift_restore_backup_<epoch>, writes the captured content, verifies SHA post-write.