/usr/share/perl5/ClamTk/Quarantine.pm is in clamtk 5.20-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | # ClamTk, copyright (C) 2004-2015 Dave M
#
# This file is part of ClamTk (http://code.google.com/p/clamtk/).
#
# ClamTk is free software; you can redistribute it and/or modify it
# under the terms of either:
#
# a) the GNU General Public License as published by the Free Software
# Foundation; either version 1, or (at your option) any later version, or
#
# b) the "Artistic License".
package ClamTk::Quarantine;
use Glib 'TRUE', 'FALSE';
# use strict;
# use warnings;
$| = 1;
use POSIX 'locale_h';
use File::Basename 'basename', 'dirname';
use File::Copy 'move';
use Digest::SHA 'sha256_hex';
use Locale::gettext;
use Encode 'decode';
use constant ROW => 0;
use constant FILE => 1;
my $liststore;
binmode( STDIN, ':utf8' );
binmode( STDOUT, ':utf8' );
sub show_window {
my $box = Gtk2::VBox->new( FALSE, 5 );
my $sw = Gtk2::ScrolledWindow->new( undef, undef );
$box->pack_start( $sw, TRUE, TRUE, 5 );
$sw->set_policy( 'never', 'automatic' );
#<<<
$liststore = Gtk2::ListStore->new(
'Glib::Int',
'Glib::String',
);
#>>>
my $view = Gtk2::TreeView->new_with_model( $liststore );
$view->set_rules_hint( TRUE );
my $column = Gtk2::TreeViewColumn->new_with_attributes(
_( 'Number' ),
Gtk2::CellRendererText->new,
text => ROW,
);
$column->set_sort_column_id( 0 );
$view->append_column( $column );
$column = Gtk2::TreeViewColumn->new_with_attributes(
_( 'File' ),
Gtk2::CellRendererText->new,
text => FILE,
);
$column->set_sort_column_id( 1 );
$view->append_column( $column );
$sw->add( $view );
my $viewbar = Gtk2::Toolbar->new;
$box->pack_start( $viewbar, FALSE, FALSE, 5 );
$viewbar->set_style( 'both-horiz' );
my $button = Gtk2::ToolButton->new_from_stock( 'gtk-undelete' );
$button->set_label( _( 'Restore' ) );
$viewbar->insert( $button, -1 );
$button->set_is_important( TRUE );
$button->signal_connect( clicked => \&restore, $view );
my $v_sep = Gtk2::SeparatorToolItem->new;
$v_sep->set_draw( FALSE );
$v_sep->set_expand( TRUE );
$viewbar->insert( $v_sep, -1 );
$button = Gtk2::ToolButton->new_from_stock( 'gtk-delete' );
$viewbar->insert( $button, -1 );
$button->set_is_important( TRUE );
$button->signal_connect( clicked => \&delete, $view );
push_viruses();
$box->show_all;
return $box;
}
sub delete {
my ( $toolbutton, $treeview ) = @_;
my $selected = $treeview->get_selection;
my ( $model, $iter ) = $selected->get_selected;
return unless $iter;
my $row = $model->get_value( $iter, ROW );
my $viruses = get_and_sort_viruses();
my $file = $viruses->{ $row }->{ basename };
my $fullname = $viruses->{ $row }->{ fullname };
# Get confirmation about deletion
#<<<
my $question = sprintf(
_( 'Really delete this file (%s) ?' ),
$file,
);
my $message
= Gtk2::MessageDialog->new(
undef,
[ qw| modal destroy-with-parent | ],
'question',
'ok-cancel',
$question,
);
#>>>
if ( 'ok' eq $message->run ) {
$message->destroy;
unlink( $fullname ) or do {
warn "unable to delete >$fullname<: $!\n";
return FALSE;
};
$model->clear;
push_viruses();
return TRUE;
} else {
$message->destroy;
return FALSE;
}
}
sub restore {
my ( $toolbutton, $treeview ) = @_;
my $selected = $treeview->get_selection;
my ( $model, $iter ) = $selected->get_selected;
return unless $iter;
my $row = $model->get_value( $iter, ROW );
my $viruses = get_and_sort_viruses();
my $fullname = $viruses->{ $row }->{ fullname };
my $basename = $viruses->{ $row }->{ basename };
# Current location of quarantined file
my $current_path = ClamTk::App->get_path( 'viruses' );
$current_path .= '/';
$current_path .= $basename;
# By default, the final destination will be $HOME
my $final_destination = ClamTk::App->get_path( 'directory' );
$final_destination .= '/';
$final_destination .= $basename;
my $current_hash = get_hash( $current_path );
# See if we have a record of this file
my ( $hopeful_path, $hopeful_mode ) = query_hash( $current_hash );
if ( $hopeful_path ) {
$final_destination = decode( 'utf8', $hopeful_path );
}
# Save-as dialog
my $dialog = Gtk2::FileChooserDialog->new(
_( 'Save file as...' ),
undef,
'save',
'gtk-cancel' => 'cancel',
'gtk-ok' => 'ok',
);
$dialog->set_current_name( $final_destination );
$dialog->set_do_overwrite_confirmation( TRUE );
if ( 'ok' eq $dialog->run ) {
$dialog->destroy;
# If we obtained permissions, apply them;
# or, 644 by default. (?)
# Unless someone has a better idea for default perms.
$hopeful_mode ||= 644;
chmod oct( $hopeful_mode ), $current_path;
move( $current_path, $final_destination ) or do {
warn "Unable to move >$current_path< to ",
">", $final_destination, "<\n";
return FALSE;
};
if ( remove_hash( undef, $current_hash ) ) {
$liststore->clear;
push_viruses();
return TRUE;
} else {
warn "error removing file >$fullname< from restore file: $!\n";
$liststore->clear;
push_viruses();
return FALSE;
}
} else {
$dialog->destroy;
return FALSE;
}
}
sub get_and_sort_viruses {
# Where we hold the quarantined stuff
my $path = ClamTk::App->get_path( 'viruses' );
my $hash;
my $i = 1;
for my $virus ( glob "$path/*" ) {
$hash->{ $i }->{ fullname } = decode( 'utf8', $virus );
$hash->{ $i }->{ basename } = decode( 'utf8', basename( $virus ) );
$hash->{ $i }->{ number } = $i;
$i++;
}
return $hash;
}
sub push_viruses {
my $hash = shift;
# If we weren't given a hash of info,
# go and get it:
if ( !$hash ) {
$hash = get_and_sort_viruses();
}
#<<<
for my $key ( sort { $a <=> $b } keys %$hash ) {
my $iter = $liststore->append;
$liststore->set(
$iter,
0, $hash->{ $key }->{ number },
1, $hash->{ $key }->{ basename },
);
}
#<<<
}
sub query_hash {
# See if queried file exists in 'restore' file;
# this might have to change to allow for querying
# by hash or fullpath
my $filehash = shift;
my $restore_path = ClamTk::App->get_path( 'restore' );
#open( my $F, '<:encoding(UTF-8)', $restore_path ) or do {
open( my $F, '<', $restore_path ) or do {
warn "Can't open restore file for reading: $!\n";
return FALSE;
};
my $current_list;
while ( <$F> ) {
chomp;
my ( $qhash, $qpath, $qmode ) = split /:/;
$current_list->{ $qhash }->{ hash } = $qhash;
$current_list->{ $qhash }->{ path } = $qpath;
$current_list->{ $qhash }->{ mode } = $qmode;
# See if it already exists in the restore file
if ( $current_list->{ $qhash }->{ hash } eq $filehash ) {
close( $F );
return (
$current_list->{ $qhash }->{ path },
$current_list->{ $qhash }->{ mode }
);
}
}
close( $F );
return FALSE;
}
sub add_hash {
# Add quarantined file to 'restore' file
my ( $pkg_name, $file, $permissions ) = @_;
my $hash = get_hash( $file );
my $restore_path = ClamTk::App->get_path( 'restore' );
open( my $F, '<:encoding(UTF-8)', $restore_path ) or do {
# open( my $F, '<', $restore_path ) or do {
warn "Can't open restore file for reading: $!\n";
return FALSE;
};
my $current_list;
while ( <$F> ) {
chomp;
next if ( /^\s*$/ );
my ( $qhash, $qpath, $qmode ) = split /:/;
$current_list->{ $qhash }->{ hash } = $qhash;
$current_list->{ $qhash }->{ path } = $qpath;
$current_list->{ $qhash }->{ mode } = $qmode;
# See if it already exists in the restore file
if ( $current_list->{ $qhash }->{ hash } eq $hash
|| $current_list->{ $qhash }->{ path } eq $file )
{
return FALSE;
}
}
close( $F );
# Rewrite restore file with new addition
open( $F, '>:encoding(UTF-8)', $restore_path ) or do {
# open( $F, '>', $restore_path ) or do {
warn "Can't open restore file for reading: $!\n";
return FALSE;
};
for my $key ( keys %$current_list ) {
print $F join( ':',
$current_list->{ $key }->{ hash },
$current_list->{ $key }->{ path },
$current_list->{ $key }->{ mode },
);
print $F "\n";
}
print $F $hash, ':', $file, ':', $permissions, "\n";
close( $F );
return TRUE;
}
sub remove_hash {
# Remove quarantined file from 'restore' file
my ( $pkg_name, $hash ) = @_;
my $restore_path = ClamTk::App->get_path( 'restore' );
#open( my $F, '<:encoding(UTF-8)', $restore_path ) or do {
open( my $F, '<', $restore_path ) or do {
warn "Can't open restore file for reading: $!\n";
return FALSE;
};
my $current_list;
while ( <$F> ) {
chomp;
my ( $qhash, $qpath, $qmode ) = split /:/;
$current_list->{ $qhash }->{ hash } = $qhash;
$current_list->{ $qhash }->{ path } = $qpath;
$current_list->{ $qhash }->{ mode } = $qmode;
}
close( $F );
warn "no files listed in restore file!\n"
and return FALSE
unless scalar keys %$current_list;
# Rewrite restore file
#open( $F, '>:encoding(UTF-8)', $restore_path ) or do {
open( $F, '>', $restore_path ) or do {
warn "Can't open restore file for reading: $!\n";
return FALSE;
};
# Next over the one we're removing
for my $key ( keys %$current_list ) {
next if ( $current_list->{ $key }->{ hash } eq $hash );
print $F join( ':',
$current_list->{ $key }->{ hash },
$current_list->{ $key }->{ path },
$current_list->{ $key }->{ mode },
);
print $F "\n";
}
close( $F );
return TRUE;
}
sub get_hash {
my $file = shift;
my $slurp = do {
local $/ = undef;
open( my $f, '<', $file ) or do {
warn "unable to open >$file< for hashing: $!\n";
return;
};
binmode( $f );
<$f>;
};
return sha256_hex( $slurp );
}
1;
|