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
|
# ClamTk, copyright (C) 2004-2010 Dave M
#
# This file is part of 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::Prefs;
use strict;
#use warnings; # disabled upon release
$|++;
use ClamTk::App;
use encoding 'utf8';
use File::Path;
use Locale::gettext;
use POSIX qw/locale_h/;
textdomain('clamtk');
setlocale( LC_MESSAGES, '' );
bind_textdomain_codeset( 'clamtk', 'UTF-8' );
sub structure {
my $paths = ClamTk::App->get_path('all');
# Ensure default paths/files exist.
# If they do, ensure they have the proper permissions
if ( !-d $paths->{viruses} ) {
# This is typically /home/user/.clamtk/viruses,
# used for the quarantine directory
eval { mkpath( $paths->{viruses}, 0, oct(755) ); };
return 0 if ($@);
} else {
# Ensure the permissions are correct
chmod oct(755), $paths->{viruses};
}
if ( !-d $paths->{history} ) {
# This is typically /home/user/.clamtk/history,
# which holds records of scans
eval { mkpath( $paths->{history}, 0, oct(755) ); };
return 0 if ($@);
} else {
# Ensure the permissions are correct
chmod oct(755), $paths->{history};
}
if ( !-d $paths->{db} ) {
# This is typically /home/user/.clamtk/db
eval { mkpath( $paths->{db}, 0, oct(755) ); };
warn $@ if $@;
return 0 if ($@);
} else {
# Ensure the permissions are correct
chmod oct(755), $paths->{db};
}
if ( !-e $paths->{prefs} ) {
# This is /home/user/.clamtk/prefs,
# a custom INI-style file
open( my $F, ">:encoding(UTF-8)", $paths->{prefs} )
or do {
warn "Unable to create preferences! $!\n";
return 0;
};
close($F);
eval { custom_prefs() };
return 0 if ($@);
}
if ( !-e $paths->{restore} ) {
# This is /home/user/.clamtk/restore, which holds
# information for putting back false positives
open( my $F, ">:encoding(UTF-8)", $paths->{restore} )
or do {
warn "Unable to create restore file! $!\n";
return 0;
};
close($F);
}
return 1;
}
sub custom_prefs {
# ensure prefs have normalized variables, especially for 3.11 -> 4.00 users
my %pkg;
# Get the user's current prefs
my $paths = ClamTk::App->get_path('prefs');
open( my $F, "<:encoding(UTF-8)", $paths )
or do {
warn "Unable to read preferences! $!\n";
return 0;
};
while (<$F>) {
my ( $k, $v ) = split(/=/);
chomp($v);
$pkg{$k} = $v;
}
close($F);
# If the preferences aren't already set,
# use 'shared' by default. This makes it work out of the box.
if ( !exists $pkg{Update} ) {
$pkg{Update} = 'shared';
} elsif ( $pkg{Update} !~ /shared|single/ ) {
# If it's set to 'shared' or 'single', leave it alone.
# Otherwise, look for system signatures.
$pkg{Update} = 'shared';
}
# The proxy is off by default
if ( !exists $pkg{HTTPProxy} ) {
$pkg{HTTPProxy} = 0;
}
# The whitelist is off by default
if ( !exists $pkg{Whitelist} ) {
$pkg{Whitelist} = '';
}
for my $n (qw/LastScan LastInfection/) {
if ( !exists $pkg{$n} ) {
$pkg{$n} = gettext('Never');
}
}
for my $o (
qw/SaveToLog ScanHidden SizeLimit
Thorough Recursive/
)
{
if ( !exists $pkg{$o} ) {
$pkg{$o} = 0; # off by default
}
}
for my $p (qw/OpenDNS AVCheck GUICheck TruncateLog/) {
if ( !exists $pkg{$p} ) {
$pkg{$p} = 1; # on by default
}
}
write_all(%pkg);
return;
}
sub get_all_prefs {
# Sometimes it's useful to have all
# the preferences rather than just one.
my %pkg;
my $paths = ClamTk::App->get_path('prefs');
open( my $F, "<:encoding(UTF-8)", $paths )
or do {
warn "Unable to read preferences! $!\n";
return 0;
};
while (<$F>) {
my ( $k, $v ) = split(/=/);
chomp($v);
$pkg{$k} = $v;
}
close($F);
return %pkg if %pkg;
}
sub write_all {
my %loc = @_;
my $paths = ClamTk::App->get_path('prefs');
open( my $F, ">:encoding(UTF-8)", $paths )
or do {
warn "Unable to write preferences! $!\n";
return 0;
};
while ( my ( $k, $v ) = each %loc ) {
print $F "$k=$v\n";
}
close($F);
return 1;
}
sub set_preference {
my ( undef, $wk, $wv ) = @_; # undef = package name
my $paths = ClamTk::App->get_path('prefs');
open( my $F, "<:encoding(UTF-8)", $paths )
or do {
warn "Unable to read preferences! $!\n";
return 0;
};
my %pkg;
while (<$F>) {
my ( $k, $v ) = split(/=/);
chomp($v);
$pkg{$k} = $v;
}
close($F);
open( $F, ">:encoding(UTF-8)", $paths )
or return -1;
while ( my ( $k, $v ) = each %pkg ) {
print $F "$k=$v\n" unless ( $k eq $wk );
}
print $F "$wk=$wv\n";
close($F)
or warn "Couldn't close $paths: $!\n";
return 1;
}
sub get_preference {
my ( undef, $wanted ) = @_; # undef = package name
my $paths = ClamTk::App->get_path('prefs');
my %pkg;
open( my $F, "<:encoding(UTF-8)", $paths )
or do {
warn "Unable to read preferences! $!\n";
return 0;
};
while (<$F>) {
my ( $k, $v ) = split(/=/);
chomp($v);
$pkg{$k} = $v;
}
close($F);
return unless %pkg;
return $pkg{$wanted} || '';
}
sub set_proxy {
my ( undef, $ip, $port ) = @_; # undef = package name
# If the user doesn't set a port, we'll just jot down port 80.
$port = $port || '80';
my $path = ClamTk::App->get_path('db');
# I'm going to clobber this every time.
# Doesn't need to be utf-8 friendly. I think.
open( my $FH, '>', "$path/local.conf" )
or return -1;
print $FH <<"EOF";
HTTPProxyServer $ip
HTTPProxyPort $port
DatabaseMirror db.local.clamav.net
DatabaseMirror database.clamav.net
EOF
close($FH)
or warn "Couldn't close $path/local.conf: $!\n";
return 1;
}
sub restore {
shift; # throw one away
my $wanted = shift; # the md5sum of the file we're after
my $job = shift; # either exists, add, or remove
my $path = shift; # full path of file
my $perm = shift; # permissions in octal (eg., 0644)
my %p;
my $restore_path = ClamTk::App->get_path('restore');
open( my $F, "<:encoding(UTF-8)", $restore_path ) or do {
warn "Can't open restore file for reading: $!\n";
return -1;
};
while (<$F>) {
chomp;
my ( $m, $paths, $perms ) = split /:/;
if ( exists $p{$m} ) {
#warn "File $m already exists?\n";
return -1;
}
$p{$m} = { path => $paths, perm => $perms };
}
close($F)
or warn "Couldn't close $restore_path: $!\n";
if ( $job eq 'exists' ) {
for my $e ( keys %p ) {
if ( $e eq $wanted ) {
return ( $p{$e}->{path}, $p{$e}->{perm} );
}
}
return -1;
}
if ( $job eq 'add' ) {
if ( exists( $p{$wanted} ) ) {
#warn "File $wanted already exists?\n";
return -1;
}
open( $F, ">:encoding(UTF-8)", $restore_path ) or do {
warn "Can't open restore file for writing: $!\n";
return -1;
};
if ( scalar( keys %p ) ) {
for my $e ( keys %p ) {
print $F $e, ":", $p{$e}->{path}, ":", $p{$e}->{perm}, "\n";
}
}
print $F "$wanted:$path:$perm\n";
close($F);
}
if ( $job eq 'remove' ) {
open( $F, ">:encoding(UTF-8)", $restore_path ) or do {
warn "Can't open restore file for writing: $!\n";
return -1;
};
for my $e ( keys %p ) {
next if ( $e eq $wanted );
print $F $e, ":", $p{$e}->{path}, ":", $p{$e}->{perm}, "\n";
}
close($F)
or warn "Couldn't close $restore_path: $!\n";
}
return;
}
sub restore_file_fix {
# This subroutine might only be in 4.25; for some reason the line
# 'next if ($e eq $wanted)' got dropped from 'sub restore' above.
# So, we need to remove all the lines from the restore file that
# don't have coresponding viruses in the quarantine directory.
my $restore = ClamTk::App->get_path('restore');
my $quarantine = ClamTk::App->get_path('viruses');
return unless ( -e $restore && -e $quarantine );
use Digest::MD5 qw/md5_hex/;
# First, grab everything quarantined.
my @viruses = glob "$quarantine/*";
# Now, grab their md5sums
my %p;
for my $f (@viruses) {
my $ctx = Digest::MD5->new;
open( my $G, '<', $f ) or next;
$ctx->addfile(*$G);
my $md5 = $ctx->hexdigest;
close($G)
or warn "Couldn't close $f: $!\n";
$p{$md5} = $f;
}
# Now grab the md5sums the restore file says we have
my %q;
open( my $F, "<:encoding(UTF-8)", $restore )
or return;
while (<$F>) {
chomp;
my ( $m, undef, undef ) = split /:/;
$q{$m} = 1;
}
close($F)
or warn "Couldn't close $restore: $!\n";
# Go through the keys (md5s) of the restore file;
# remove the records of those that don't exist.
for my $k ( keys %q ) {
unless ( exists $p{$k} ) {
ClamTk::Prefs->restore( $k, 'remove' );
}
}
return;
}
1;
|