File: App.pm

package info (click to toggle)
clamtk 4.27-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,796 kB
  • ctags: 73
  • sloc: perl: 3,554; makefile: 11
file content (258 lines) | stat: -rw-r--r-- 7,955 bytes parent folder | download
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
# 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::App;

use strict;
#use warnings;    # disabled upon release
$|++;

use Net::DNS;

use encoding 'utf8';

sub get_TK_version {
    # Stick with %.2f format - 4.50 vice 4.5
    return '4.27';
}

sub get_path {
    my ( undef, $wanted ) = @_;
    my $path;

    # These are directories and files necessary for
    # preferences, storing AV signatures and more

    # First, determine home directory
    $path->{directory} = $ENV{HOME} || ( ( getpwuid $< )[-2] );

    # Default personal clamtk directory
    $path->{clamtk} = $path->{directory} . '/.clamtk';

    # For storing quarantined files
    $path->{viruses} = $path->{clamtk} . '/viruses';

    # Store history logs here
    $path->{history} = $path->{clamtk} . '/history';

    # Plain text file for preferences
    $path->{prefs} = $path->{clamtk} . '/prefs';

    # Plain text file for restoring quarantined files
    $path->{restore} = $path->{clamtk} . '/restore';

    # The db directory stores virus defs/freshclam-related stuff
    $path->{db} = $path->{clamtk} . '/db';

    # Default variables
    $path->{whitelist_dir} =
        join( ';', $path->{viruses}, '/sys', '/dev', '/proc;' );

    # Most times freshclam is under /usr/bin
    $path->{freshclam} =
          ( -e '/usr/bin/freshclam' )       ? '/usr/bin/freshclam'
        : ( -e '/usr/local/bin/freshclam' ) ? '/usr/local/bin/freshclam'
        : ( -e '/opt/local/bin/freshclam' ) ? '/opt/local/bin/freshclam'
        :                                     '';

    # Most times clamscan is under /usr/bin
    # We'll use clampath as the actual path
    # and clamscan as clampath + scan options
    $path->{clampath} =
          ( -e '/usr/bin/clamscan' )       ? '/usr/bin/clamscan'
        : ( -e '/usr/local/bin/clamscan' ) ? '/usr/local/bin/clamscan'
        : ( -e '/opt/local/bin/clamscan' ) ? '/opt/local/bin/clamscan'
        :                                    '';

    $path->{clamscan} = $path->{clampath};

    # The default ClamAV options:
    # leave out the summary and warn on encrypted
    $path->{clamscan} .= ' --no-summary --block-encrypted ';

    return ( $wanted eq 'all' ) ? $path : $path->{$wanted};
}

sub def_paths {
    # Returns (path_of_daily.c?d, path_of_main.c?d)
    # There are 3 formats:
    # 1. The .cld files
    # 2. The .cvd files
    # 3. The {daily,main}.info directories
    # As of 4.23, we're no longer looking foro the .info dirs.
    # The .cvd is the compressed database, while .cld is a
    # previous .cvd/.cld with incremental updates.

    my ( $DAILY_PATH, $MAIN_PATH );

    # These are the typical directories where the sigs are found:
    my @dirs = qw#
        /var/lib/clamav
        /var/clamav
        /opt/local/share/clamav
        /usr/share/clamav
        /usr/local/share/clamav
        /var/db/clamav
        #;

    my $user_set = 0;
    if ( ClamTk::Prefs->get_preference('Update') eq 'single' ) {
        $user_set = 1;
        my $paths = ClamTk::App->get_path('db');
        unshift( @dirs, $paths );
    }

    # We'll search for the daily file then main;
    # and check for .cld before .cvd
    for my $dir_list (@dirs) {
        if ( -e "$dir_list/daily.cld" ) {
            $DAILY_PATH = "$dir_list/daily.cld";
        } elsif ( -e "$dir_list/daily.cvd" ) {
            $DAILY_PATH = "$dir_list/daily.cvd";
        }
        if ( -e "$dir_list/main.cld" ) {
            $MAIN_PATH = "$dir_list/main.cld";
        } elsif ( -e "$dir_list/main.cvd" ) {
            $MAIN_PATH = "$dir_list/main.cvd";
        }
        last if ( $DAILY_PATH && $MAIN_PATH );

        # the user may have set single - may need to update db
        last if ($user_set);
    }

    return ( $DAILY_PATH, $MAIN_PATH );
}

sub get_AV_version {
    # simple 'clamscan -V'
    # We have to parse this:
    # ClamAV 0.95.3/11220/Fri Jun 18 22:06:39 2010
    my $paths = ClamTk::App->get_path('clampath');
    my ($version) = qx/$paths -V/;
    $version =~ s/ClamAV\s(.*?)\/.*/$1/;
    chomp($version);
    return $version;
}

sub get_AV_remote {
    # The user may have set the preference to not check this
    # on startup using 'AVCheck'
    return 'undef' if ( !ClamTk::Prefs->get_preference('AVCheck') );

    # Host for version
    my $rmt_host = 'current.cvd.clamav.net';

    # No longer need the alarm to "die" since Net::DNS
    # does it for us.
    # Just to be sure, we'll use OpenDNS servers here
    # and set a UDP timeout.
    my ( $get, $res );

    $res = Net::DNS::Resolver->new( udp_timeout => 3, );
    # We have permission from OpenDNS to use this. :)
    if ( ClamTk::Prefs->get_preference('OpenDNS') ) {
        $res->nameservers(
            qw(208.67.222.222 208.67.220.220
                208.67.222.220 208.67.220.222)
        );
    }

    ($get) = $res->send( $rmt_host, "TXT" );
    if ( !$get ) {
        warn $res->errorstring, "\n";
        return 'undef';
    }
    chomp($get);

  # This is a sample of the line that is returned ($get->string):
  # current.cvd.clamav.net.	439	IN	TXT	"0.95.3:51:10091:1259420373:1:44:12728"
  # We just need the version, held between the " and the :
    if ( $get->string =~ /IN\s+TXT\s+\"(.*?):/ ) {
        return $1;
    } else {
        return 'undef';
    }
}

sub get_num_sigs {
    # Adds the main + daily for total # of signatures
    my ( undef, $daily, $main ) = get_sig_info();
    return ( $daily + $main );
}

sub get_date_sigs {
    # Gets the date, preferably from daily.c?d.
    # Useful for informing user of outdated sigs.
    my ( $date, undef, undef ) = get_sig_info();
    return $date;
}

sub get_sig_info {
    # This sub parses the signature files for the date
    # and number of signatures.
    my ( $daily_path, $main_path ) = def_paths();
    #warn "I'm missing daily.{cld,cvd} and/or main.{cld,cvd}.\n"
    #    unless ( $daily_path && $main_path );

    # $INFO_DATE = Date of signatures
    # $INFO_DAILY = # of 'daily' signatures (not part of main.{cld,cvd})
    # $INFO_MAIN = # of signatures part of 'main' signatures
    my ( $INFO_DATE, $INFO_DAILY, $INFO_MAIN );

    # example of what we need to parse
    # ClamAV-VDB:07 Nov 2009 03-23 -0500:9999:102368:44:X:X:ccordes:1257582210
    # ClamAV-VDB:14 May 2009 10-28 -0400:51:545035:42:
    # We'll store the date of main.{cld,cvd}; if daily.{cld,cvd} does not
    # exist, use that date as the date of signatures instead of returning
    # 'None found'. If daily.{cld,cvd} does exist, use that date instead.
    if ($main_path) {
        my $FILE;
        if ( open( $FILE, '<', $main_path ) ) {
            binmode($FILE);
            while (<$FILE>) {
                if (/ClamAV-VDB:(\S+\s+\S+\s+\S+).*?[\-\+]\d+:\d+:(\d+)/) {
                    $INFO_DATE = $1;
                    $INFO_MAIN = $2;
                    last;
                }
                close($FILE)
                    or warn "Couldn't close $main_path: $!\n";
            }
        } else {
            $INFO_MAIN = 0;
        }
    } else {
        $INFO_MAIN = 0;
    }

    if ($daily_path) {
        my $FILE;
        if ( open( $FILE, '<', $daily_path ) ) {
            binmode($FILE);
            while (<$FILE>) {
                if (/ClamAV-VDB:(\S+\s+\S+\s+\S+).*[\-\+]\d+:\d+:(\d+)/) {
                    $INFO_DATE  = $1;
                    $INFO_DAILY = $2;
                    last;
                }
            }
            close($FILE)
                or warn "Couldn't close $daily_path: $!\n";
        } else {
            $INFO_DATE  = '01 Jan 1970';
            $INFO_DAILY = 0;
        }
    }
    return ( $INFO_DATE, $INFO_DAILY, $INFO_MAIN );
}

1;