Diff -- /var/www/vhosts/3dshawn.com/site1/restore.cgi
Diff
/var/www/vhosts/3dshawn.com/site1/restore.cgi
modified on local at 2026-07-12 00:48:53
Added
+20
lines
Removed
-0
lines
Context
442
unchanged
Blobs
from 5edc99e61471
to 2f8b971be093
to 2f8b971be093
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | 1 | #!/usr/bin/perl |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # DriftSense -- /restore.cgi |
| 4 | 4 | # |
| 5 | 5 | # Restore a captured file to its original path. Two flavors: |
| 6 | 6 | # Local (server.kind = local) -> write blob to target path directly |
| 7 | 7 | # Agent (server.kind = ssh_agent) -> pipe blob content over SSH to target |
| 8 | 8 | # |
| 9 | 9 | # Safety: |
| 10 | 10 | # - GET /restore.cgi?id=N -> preview page (dry-run details) |
| 11 | 11 | # - POST /restore.cgi id=N confirm=1 -> perform the restore |
| 12 | 12 | # |
| 13 | 13 | # * Path must be inside a configured FILE_MONITOR_SETTINGS.scan_path |
| 14 | 14 | # for that server (prevents restoring blob to /etc/passwd from a |
| 15 | 15 | # tampered FILE_CHANGES row). |
| 16 | 16 | # * Current file content is backed up to |
| 17 | 17 | # $target.drift_restore_backup_$UNIX_TIMESTAMP before overwrite. |
| 18 | 18 | # * Every attempt (success, failure, dry_run) is logged to RESTORE_LOG. |
| 19 | 19 | #====================================================================== |
| 20 | 20 | use strict; |
| 21 | 21 | use warnings; |
| 22 | 22 | use CGI (); |
| 23 | 23 | use IPC::Open3; |
| 24 | 24 | use Symbol 'gensym'; |
| 25 | 25 | use Fcntl (); |
| 26 | 26 | use POSIX (); |
| 27 | 27 | use Compress::Zlib qw(uncompress); |
| 28 | 28 | use Digest::SHA qw(sha256_hex); |
| 29 | 29 | use MODS::Config; |
| 30 | 30 | use MODS::DBConnect; |
| 31 | 31 | use MODS::Template; |
| 32 | 32 | use MODS::PageWrapper; |
| 33 | 33 | use MODS::Crypto; |
| 34 | 34 | use MODS::Diff; |
| 35 | 35 | |
| 36 | 36 | my $cgi = CGI->new; |
| 37 | 37 | my $db = MODS::DBConnect->new; |
| 38 | 38 | my $tpl = MODS::Template->new; |
| 39 | 39 | $|=1; |
| 40 | 40 | |
| 41 | 41 | my $dbh = $db->db_connect or die "DriftSense: DB connect failed\n"; |
| 42 | 42 | |
| 43 | 43 | my $method = $ENV{REQUEST_METHOD} || 'GET'; |
| 44 | 44 | my $id = int($cgi->param('id') || 0); |
| 45 | 45 | |
| 46 | 46 | # ---- Load the source change row ------------------------------------- |
| 47 | 47 | my $row = _load_change_row($dbh, $id); |
| 48 | 48 | unless ($row && $row->{id}) { |
| 49 | 49 | _render_error("Change #$id not found"); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | # ---- Path safety: target file must be inside a monitored scan_path -- |
| 53 | 53 | my $monitor = _load_monitor($dbh, $row->{file_monitor_list_id}); |
| 54 | 54 | my $path_ok = 0; |
| 55 | 55 | if ($monitor && $monitor->{scan_path}) { |
| 56 | 56 | my $sp = $monitor->{scan_path}; |
| 57 | 57 | $sp =~ s!/+$!!; |
| 58 | 58 | $path_ok = 1 if index($row->{file_name}, "$sp/") == 0 || $row->{file_name} eq $sp; |
| 59 | 59 | } |
| 60 | 60 | unless ($path_ok) { |
| 61 | 61 | _render_error("Target path <code>$row->{file_name}</code> is outside its monitor's configured scan_path; refusing to restore."); |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | # ---- If POST, perform the restore ---------------------------------- |
| 65 | 65 | if ($method eq 'POST' && $cgi->param('confirm')) { |
| 66 | 66 | my $result = _do_restore($dbh, $row, $monitor); |
| 67 | 67 | _log_restore($dbh, $row, $result); |
| 68 | 68 | |
| 69 | 69 | # Render result page |
| 70 | 70 | my $tvars = { |
| 71 | 71 | id => $id, |
| 72 | 72 | target_file => $row->{file_name}, |
| 73 | 73 | server_name => $row->{server_name} // 'local', |
| 74 | 74 | server_kind => $row->{server_kind} // 'local', |
| 75 | 75 | source_sha => substr($row->{blob_sha} // '', 0, 12), |
| 76 | 76 | captured_h => $row->{date_time} // '', |
| 77 | 77 | captured_epoch => int($row->{ts_epoch} || 0), |
| 78 | 78 | result_status => $result->{status}, |
| 79 | 79 | result_ok => $result->{status} eq 'success' ? 1 : 0, |
| 80 | 80 | result_msg => $result->{message} // '', |
| 81 | 81 | backup_path => $result->{backup_path} // '', |
| 82 | 82 | did_restore => 1, |
| 83 | 83 | }; |
| 84 | 84 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; |
| 85 | 85 | my @body = $tpl->template('restore.html', $tvars); |
| 86 | 86 | MODS::PageWrapper->new->wrapper( |
| 87 | 87 | page_title => 'Restore', page_key => '', |
| 88 | 88 | body_html => join('', @body), userinfo => { display_name => 'Operator' }, |
| 89 | 89 | ); |
| 90 | 90 | $db->db_disconnect($dbh); |
| 91 | 91 | exit; |
| 92 | } | |
| 93 | ||
| 94 | # ---- Recent RESTORE_LOG for this file (Wave 5 F4) ------------------ | |
| 95 | my $q_file_esc = $dbh->quote($row->{file_name}); | |
| 96 | my @recent_restores_this = $db->db_readwrite_multiple($dbh, qq~ | |
| 97 | SELECT restore_id, status, LEFT(error_message, 80) AS err_short, | |
| 98 | LEFT(source_blob_sha, 12) AS sha_short, | |
| 99 | restored_by, | |
| 100 | UNIX_TIMESTAMP(restored_at) AS restored_epoch, | |
| 101 | DATE_FORMAT(restored_at, '%b %d %H:%i') AS restored_h | |
| 102 | FROM RESTORE_LOG | |
| 103 | WHERE target_file = $q_file_esc | |
| 104 | ORDER BY restore_id DESC LIMIT 5 | |
| 105 | ~, __FILE__, __LINE__); | |
| 106 | foreach my $rr (@recent_restores_this) { | |
| 107 | $rr->{status_pill} = $rr->{status} eq 'success' ? 'pill-ok' : | |
| 108 | $rr->{status} eq 'failed' ? 'pill-bad' : 'pill-mute'; | |
| 109 | $rr->{restored_epoch} //= 0; | |
| 92 | 110 | } |
| 93 | 111 | |
| 94 | 112 | # ---- GET: preview page --------------------------------------------- |
| 95 | 113 | my ($current_sha, $current_exists, $current_size, $current_content) = _peek_current($row, $monitor); |
| 96 | 114 | |
| 97 | 115 | # Live diff: current on-disk vs captured content |
| 98 | 116 | my @diff_lines; |
| 99 | 117 | my ($diff_add, $diff_del) = (0, 0); |
| 100 | 118 | if ($current_exists && $current_sha && $row->{blob_sha} && $current_sha ne $row->{blob_sha}) { |
| 101 | 119 | my $captured = _fetch_blob($dbh, $row->{blob_sha}) // ''; |
| 102 | 120 | @diff_lines = MODS::Diff::html_escape_lines(MODS::Diff::lines($current_content // '', $captured)); |
| 103 | 121 | for my $L (@diff_lines) { |
| 104 | 122 | $diff_add++ if $L->{op} eq 'add'; |
| 105 | 123 | $diff_del++ if $L->{op} eq 'del'; |
| 106 | 124 | } |
| 107 | 125 | } |
| 108 | 126 | my $tvars = { |
| 109 | 127 | id => $id, |
| 110 | 128 | target_file => $row->{file_name}, |
| 111 | 129 | server_name => $row->{server_name} // 'local', |
| 112 | 130 | server_kind => $row->{server_kind} // 'local', |
| 113 | 131 | scan_name => $monitor->{scan_name} // '', |
| 114 | 132 | source_sha => substr($row->{blob_sha} // '', 0, 12), |
| 115 | 133 | source_sha_full=> $row->{blob_sha} // '', |
| 116 | 134 | captured_h => $row->{date_time} // '', |
| 117 | 135 | captured_epoch => int($row->{ts_epoch} || 0), |
| 118 | 136 | current_sha => substr($current_sha // '', 0, 12), |
| 119 | 137 | current_exists => $current_exists, |
| 120 | 138 | current_size => defined($current_size) ? "$current_size bytes" : 'unknown', |
| 121 | 139 | same_content => ($current_sha && $current_sha eq ($row->{blob_sha} // '')) ? 1 : 0, |
| 122 | 140 | did_restore => 0, |
| 123 | 141 | diff_url => "/diff.cgi?kind=file&id=$id", |
| 124 | 142 | diff_lines => \@diff_lines, |
| 125 | 143 | has_live_diff => scalar(@diff_lines) ? 1 : 0, |
| 126 | 144 | diff_add => $diff_add, |
| 127 | 145 | diff_del => $diff_del, |
| 128 | 146 | diff_ctx => scalar(@diff_lines) - $diff_add - $diff_del, |
| 147 | recent_restores_this => \@recent_restores_this, | |
| 148 | has_recent_restores => scalar(@recent_restores_this) ? 1 : 0, | |
| 129 | 149 | }; |
| 130 | 150 | |
| 131 | 151 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; |
| 132 | 152 | my @body = $tpl->template('restore.html', $tvars); |
| 133 | 153 | MODS::PageWrapper->new->wrapper( |
| 134 | 154 | page_title => 'Restore', page_key => '', |
| 135 | 155 | body_html => join('', @body), userinfo => { display_name => 'Operator' }, |
| 136 | 156 | ); |
| 137 | 157 | $db->db_disconnect($dbh); |
| 138 | 158 | exit; |
| 139 | 159 | |
| 140 | 160 | #====================================================================== |
| 141 | 161 | sub _load_change_row { |
| 142 | 162 | my ($dbh, $id) = @_; |
| 143 | 163 | return undef unless $id > 0; |
| 144 | 164 | return $db->db_readwrite($dbh, qq~ |
| 145 | 165 | SELECT fc.file_changes_id AS id, |
| 146 | 166 | fc.file_monitor_list_id, fc.server_id, |
| 147 | 167 | fc.file_name, fc.blob_sha, fc.status, fc.date_time, |
| 148 | 168 | UNIX_TIMESTAMP(fc.date_time) AS ts_epoch, |
| 149 | 169 | s.server_name, s.kind AS server_kind, |
| 150 | 170 | s.ssh_host, s.ssh_user, s.ssh_port, |
| 151 | 171 | s.ssh_key_path, s.ssh_options, |
| 152 | 172 | s.ssh_key_blob |
| 153 | 173 | FROM FILE_CHANGES fc |
| 154 | 174 | LEFT JOIN SERVERS s ON s.server_id = fc.server_id |
| 155 | 175 | WHERE fc.file_changes_id = $id |
| 156 | 176 | LIMIT 1 |
| 157 | 177 | ~, __FILE__, __LINE__); |
| 158 | 178 | } |
| 159 | 179 | |
| 160 | 180 | sub _load_monitor { |
| 161 | 181 | my ($dbh, $mid) = @_; |
| 162 | 182 | return undef unless $mid; |
| 163 | 183 | return $db->db_readwrite($dbh, qq~ |
| 164 | 184 | SELECT file_monitor_list_id, scan_path, scan_name, server_id |
| 165 | 185 | FROM FILE_MONITOR_SETTINGS |
| 166 | 186 | WHERE file_monitor_list_id = $mid |
| 167 | 187 | LIMIT 1 |
| 168 | 188 | ~, __FILE__, __LINE__); |
| 169 | 189 | } |
| 170 | 190 | |
| 171 | 191 | sub _fetch_blob { |
| 172 | 192 | my ($dbh, $sha) = @_; |
| 173 | 193 | return undef unless $sha; |
| 174 | 194 | my $sth = $dbh->prepare("SELECT content_gz FROM BLOB_STORE WHERE blob_sha = ?"); |
| 175 | 195 | return undef unless $sth && $sth->execute($sha); |
| 176 | 196 | my $r = $sth->fetchrow_arrayref; |
| 177 | 197 | $sth->finish; |
| 178 | 198 | return undef unless $r && $r->[0]; |
| 179 | 199 | return eval { uncompress($r->[0]) }; |
| 180 | 200 | } |
| 181 | 201 | |
| 182 | 202 | #--------------------------------------------------------------------- |
| 183 | 203 | # _peek_current($row, $monitor) -> ($sha, $exists, $size) |
| 184 | 204 | # For local: stat + hash the current on-disk content. |
| 185 | 205 | # For ssh_agent: SSH and sha256sum + wc -c. |
| 186 | 206 | #--------------------------------------------------------------------- |
| 187 | 207 | sub _peek_current { |
| 188 | 208 | my ($row, $monitor) = @_; |
| 189 | 209 | my $path = $row->{file_name}; |
| 190 | 210 | if (($row->{server_kind} // 'local') eq 'local') { |
| 191 | 211 | return (undef, 0, undef, undef) unless -e $path; |
| 192 | 212 | return (undef, 1, -s $path, undef) unless -r $path; |
| 193 | 213 | open(my $fh, '<:raw', $path) or return (undef, 1, -s $path, undef); |
| 194 | 214 | local $/; my $c = <$fh>; close $fh; |
| 195 | 215 | return (sha256_hex($c), 1, length $c, $c); |
| 196 | 216 | } |
| 197 | 217 | # SSH agent |
| 198 | 218 | my ($rc, $out, $err) = _ssh_run($row, "test -f " . _sh_quote($path) . " && sha256sum " . _sh_quote($path) . " && wc -c < " . _sh_quote($path)); |
| 199 | 219 | if ($rc != 0) { |
| 200 | 220 | return (undef, 0, undef, undef); |
| 201 | 221 | } |
| 202 | 222 | my @lines = split /\n/, $out; |
| 203 | 223 | my $sha; |
| 204 | 224 | my $size; |
| 205 | 225 | foreach my $l (@lines) { |
| 206 | 226 | if ($l =~ /^([0-9a-f]{64})\s/) { $sha = $1; } |
| 207 | 227 | elsif ($l =~ /^(\d+)$/) { $size = int($1); } |
| 208 | 228 | } |
| 209 | 229 | # Fetch content over SSH for the live diff -- capped at 512 KB |
| 210 | 230 | my $content; |
| 211 | 231 | if ($sha && $size && $size < 512_000) { |
| 212 | 232 | my ($rc2, $out2, $err2) = _ssh_run($row, "cat " . _sh_quote($path)); |
| 213 | 233 | $content = $out2 if $rc2 == 0; |
| 214 | 234 | } |
| 215 | 235 | return ($sha, 1, $size, $content); |
| 216 | 236 | } |
| 217 | 237 | |
| 218 | 238 | #--------------------------------------------------------------------- |
| 219 | 239 | # _do_restore($dbh, $row, $monitor) -> { status, message, backup_path } |
| 220 | 240 | #--------------------------------------------------------------------- |
| 221 | 241 | sub _do_restore { |
| 222 | 242 | my ($dbh, $row, $monitor) = @_; |
| 223 | 243 | my $blob_sha = $row->{blob_sha}; |
| 224 | 244 | unless ($blob_sha) { |
| 225 | 245 | return { status => 'failed', message => 'source change has no blob_sha' }; |
| 226 | 246 | } |
| 227 | 247 | my $content = _fetch_blob($dbh, $blob_sha); |
| 228 | 248 | unless (defined $content) { |
| 229 | 249 | return { status => 'failed', message => "blob $blob_sha not in BLOB_STORE" }; |
| 230 | 250 | } |
| 231 | 251 | |
| 232 | 252 | my $stamp = time(); |
| 233 | 253 | my $backup = "$row->{file_name}.drift_restore_backup_$stamp"; |
| 234 | 254 | |
| 235 | 255 | if (($row->{server_kind} // 'local') eq 'local') { |
| 236 | 256 | # Local restore -- backup then write |
| 237 | 257 | if (-e $row->{file_name}) { |
| 238 | 258 | unless (rename($row->{file_name}, $backup)) { |
| 239 | 259 | # Fallback: cp to backup, then overwrite in place |
| 240 | 260 | open(my $src, '<:raw', $row->{file_name}) or return { |
| 241 | 261 | status => 'failed', |
| 242 | 262 | message => "cannot read current file: $!" |
| 243 | 263 | }; |
| 244 | 264 | open(my $bak, '>:raw', $backup) or return { |
| 245 | 265 | status => 'failed', |
| 246 | 266 | message => "cannot write backup: $!" |
| 247 | 267 | }; |
| 248 | 268 | local $/; print {$bak} <$src>; |
| 249 | 269 | close $src; close $bak; |
| 250 | 270 | } |
| 251 | 271 | } else { |
| 252 | 272 | $backup = ''; # nothing to back up |
| 253 | 273 | } |
| 254 | 274 | # Write new content |
| 255 | 275 | open(my $out, '>:raw', $row->{file_name}) or return { |
| 256 | 276 | status => 'failed', |
| 257 | 277 | message => "write failed: $!", |
| 258 | 278 | backup_path => $backup, |
| 259 | 279 | }; |
| 260 | 280 | print {$out} $content; |
| 261 | 281 | close $out; |
| 262 | 282 | # Verify SHA post-write |
| 263 | 283 | open(my $v, '<:raw', $row->{file_name}); |
| 264 | 284 | local $/; my $verify = <$v>; close $v; |
| 265 | 285 | my $vsha = sha256_hex($verify); |
| 266 | 286 | if ($vsha ne $blob_sha) { |
| 267 | 287 | return { |
| 268 | 288 | status => 'failed', |
| 269 | 289 | message => "post-write SHA mismatch: $vsha vs $blob_sha", |
| 270 | 290 | backup_path => $backup, |
| 271 | 291 | }; |
| 272 | 292 | } |
| 273 | 293 | return { |
| 274 | 294 | status => 'success', |
| 275 | 295 | message => "restored " . length($content) . " bytes", |
| 276 | 296 | backup_path => $backup, |
| 277 | 297 | }; |
| 278 | 298 | } |
| 279 | 299 | |
| 280 | 300 | # SSH agent restore |
| 281 | 301 | # Step 1: cp current to backup (best-effort) |
| 282 | 302 | my ($rc, $out, $err) = _ssh_run($row, |
| 283 | 303 | "test -f " . _sh_quote($row->{file_name}) . |
| 284 | 304 | " && cp -p " . _sh_quote($row->{file_name}) . " " . _sh_quote($backup) . |
| 285 | 305 | " ; echo done"); |
| 286 | 306 | if ($rc != 0) { |
| 287 | 307 | $backup = ''; # backup failed but we'll still try the restore |
| 288 | 308 | } |
| 289 | 309 | |
| 290 | 310 | # Step 2: write content via SSH stdin |
| 291 | 311 | my $ok = _ssh_write($row, $row->{file_name}, $content); |
| 292 | 312 | unless ($ok) { |
| 293 | 313 | return { |
| 294 | 314 | status => 'failed', |
| 295 | 315 | message => "ssh write failed", |
| 296 | 316 | backup_path => $backup, |
| 297 | 317 | }; |
| 298 | 318 | } |
| 299 | 319 | |
| 300 | 320 | # Step 3: Verify SHA remotely |
| 301 | 321 | ($rc, $out, $err) = _ssh_run($row, "sha256sum " . _sh_quote($row->{file_name})); |
| 302 | 322 | my $vsha = ($out =~ /^([0-9a-f]{64})\s/) ? $1 : ''; |
| 303 | 323 | if ($vsha ne $blob_sha) { |
| 304 | 324 | return { |
| 305 | 325 | status => 'failed', |
| 306 | 326 | message => "post-write remote SHA mismatch: $vsha vs $blob_sha", |
| 307 | 327 | backup_path => $backup, |
| 308 | 328 | }; |
| 309 | 329 | } |
| 310 | 330 | return { |
| 311 | 331 | status => 'success', |
| 312 | 332 | message => "remote restored " . length($content) . " bytes", |
| 313 | 333 | backup_path => $backup, |
| 314 | 334 | }; |
| 315 | 335 | } |
| 316 | 336 | |
| 317 | 337 | #--------------------------------------------------------------------- |
| 318 | 338 | sub _log_restore { |
| 319 | 339 | my ($dbh, $row, $result) = @_; |
| 320 | 340 | my $q_file = $dbh->quote($row->{file_name} // ''); |
| 321 | 341 | my $q_sha = $dbh->quote($row->{blob_sha} // ''); |
| 322 | 342 | my $q_bak = $dbh->quote($result->{backup_path} // ''); |
| 323 | 343 | my $q_stat = $dbh->quote($result->{status} // 'failed'); |
| 324 | 344 | my $q_err = $dbh->quote($result->{status} eq 'success' ? '' : ($result->{message} // '')); |
| 325 | 345 | my $sid = int($row->{server_id} || 0); |
| 326 | 346 | my $cid = int($row->{id} || 0); |
| 327 | 347 | $db->db_readwrite($dbh, qq~ |
| 328 | 348 | INSERT INTO RESTORE_LOG |
| 329 | 349 | (source_change_id, server_id, target_file, source_blob_sha, |
| 330 | 350 | backup_path, status, error_message) |
| 331 | 351 | VALUES |
| 332 | 352 | ($cid, $sid, $q_file, $q_sha, $q_bak, $q_stat, $q_err) |
| 333 | 353 | ~, __FILE__, __LINE__); |
| 334 | 354 | } |
| 335 | 355 | |
| 336 | 356 | #--------------------------------------------------------------------- |
| 337 | 357 | # SSH helpers -- resolve key (encrypted blob or on-disk), spawn ssh |
| 338 | 358 | #--------------------------------------------------------------------- |
| 339 | 359 | sub _ssh_argv { |
| 340 | 360 | my ($row) = @_; |
| 341 | 361 | my $user = $row->{ssh_user} || 'root'; |
| 342 | 362 | my $host = $row->{ssh_host}; |
| 343 | 363 | my $port = $row->{ssh_port} || 22; |
| 344 | 364 | my $key_path = $row->{ssh_key_path} || ''; |
| 345 | 365 | my $tmp_key; |
| 346 | 366 | |
| 347 | 367 | if ($row->{ssh_key_blob}) { |
| 348 | 368 | my $pem = eval { MODS::Crypto::decrypt($row->{ssh_key_blob}) }; |
| 349 | 369 | if ($pem) { |
| 350 | 370 | my $t = time(); |
| 351 | 371 | my $p = "/tmp/.ds_restore_key_${t}_$$_" . int(rand(1_000_000)); |
| 352 | 372 | if (sysopen(my $fh, $p, |
| 353 | 373 | Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_EXCL(), 0600)) { |
| 354 | 374 | print $fh $pem; close $fh; |
| 355 | 375 | $key_path = $p; |
| 356 | 376 | $tmp_key = $p; |
| 357 | 377 | } |
| 358 | 378 | } |
| 359 | 379 | } |
| 360 | 380 | |
| 361 | 381 | my @argv = ('/usr/bin/ssh', |
| 362 | 382 | '-o', 'BatchMode=yes', |
| 363 | 383 | '-o', 'StrictHostKeyChecking=no', |
| 364 | 384 | '-o', 'UserKnownHostsFile=/dev/null', |
| 365 | 385 | '-o', 'LogLevel=ERROR', |
| 366 | 386 | '-o', 'ConnectTimeout=8', |
| 367 | 387 | '-p', $port); |
| 368 | 388 | push @argv, '-i', $key_path if $key_path && -r $key_path; |
| 369 | 389 | if ($row->{ssh_options}) { |
| 370 | 390 | foreach my $o (split /\s+/, $row->{ssh_options}) { |
| 371 | 391 | push @argv, '-o', $o if length $o; |
| 372 | 392 | } |
| 373 | 393 | } |
| 374 | 394 | push @argv, "$user\@$host"; |
| 375 | 395 | return (\@argv, $tmp_key); |
| 376 | 396 | } |
| 377 | 397 | |
| 378 | 398 | sub _ssh_run { |
| 379 | 399 | my ($row, $cmd) = @_; |
| 380 | 400 | my ($base, $tmp_key) = _ssh_argv($row); |
| 381 | 401 | my @full = (@$base, $cmd); |
| 382 | 402 | my ($wtr, $rdr, $err_fh); |
| 383 | 403 | $err_fh = gensym; |
| 384 | 404 | my $pid = eval { open3($wtr, $rdr, $err_fh, @full) }; |
| 385 | 405 | if ($@ || !$pid) { |
| 386 | 406 | unlink $tmp_key if $tmp_key; |
| 387 | 407 | return (-1, '', $@); |
| 388 | 408 | } |
| 389 | 409 | close $wtr; |
| 390 | 410 | my $out = do { local $/; <$rdr> // '' }; |
| 391 | 411 | my $err = do { local $/; <$err_fh> // '' }; |
| 392 | 412 | close $rdr; close $err_fh; |
| 393 | 413 | waitpid $pid, 0; |
| 394 | 414 | my $rc = $? >> 8; |
| 395 | 415 | unlink $tmp_key if $tmp_key; |
| 396 | 416 | return ($rc, $out, $err); |
| 397 | 417 | } |
| 398 | 418 | |
| 399 | 419 | sub _ssh_write { |
| 400 | 420 | my ($row, $path, $content) = @_; |
| 401 | 421 | my ($base, $tmp_key) = _ssh_argv($row); |
| 402 | 422 | # `cat > $path` on the remote side, content on stdin |
| 403 | 423 | my @full = (@$base, "cat > " . _sh_quote($path)); |
| 404 | 424 | my ($wtr, $rdr, $err_fh); |
| 405 | 425 | $err_fh = gensym; |
| 406 | 426 | my $pid = eval { open3($wtr, $rdr, $err_fh, @full) }; |
| 407 | 427 | if ($@ || !$pid) { |
| 408 | 428 | unlink $tmp_key if $tmp_key; |
| 409 | 429 | return 0; |
| 410 | 430 | } |
| 411 | 431 | binmode $wtr; |
| 412 | 432 | print {$wtr} $content; |
| 413 | 433 | close $wtr; |
| 414 | 434 | my $out = do { local $/; <$rdr> // '' }; |
| 415 | 435 | my $err = do { local $/; <$err_fh> // '' }; |
| 416 | 436 | close $rdr; close $err_fh; |
| 417 | 437 | waitpid $pid, 0; |
| 418 | 438 | my $rc = $? >> 8; |
| 419 | 439 | unlink $tmp_key if $tmp_key; |
| 420 | 440 | return $rc == 0 ? 1 : 0; |
| 421 | 441 | } |
| 422 | 442 | |
| 423 | 443 | sub _sh_quote { |
| 424 | 444 | my $s = shift; $s //= ''; |
| 425 | 445 | $s =~ s/'/'\\''/g; |
| 426 | 446 | return "'$s'"; |
| 427 | 447 | } |
| 428 | 448 | |
| 429 | 449 | sub _render_error { |
| 430 | 450 | my ($msg) = @_; |
| 431 | 451 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; |
| 432 | 452 | my @body = $tpl->template('restore.html', { |
| 433 | 453 | error_msg => $msg, |
| 434 | 454 | has_error => 1, |
| 435 | 455 | did_restore => 0, |
| 436 | 456 | }); |
| 437 | 457 | MODS::PageWrapper->new->wrapper( |
| 438 | 458 | page_title => 'Restore', page_key => '', |
| 439 | 459 | body_html => join('', @body), userinfo => { display_name => 'Operator' }, |
| 440 | 460 | ); |
| 441 | 461 | exit; |
| 442 | 462 | } |