File-OSS-Scan

 view release on metacpan or  search on metacpan

lib/File/OSS/Scan.pm  view on Meta::CPAN

package File::OSS::Scan;

use strict;
use warnings FATAL => 'all';

use Fatal qw( open close );
use Carp;
use English qw( -no_match_vars );
use Data::Dumper; # for debug
use Cwd;
use File::Copy;
use File::Basename;

use File::OSS::Scan::Constant qw(:all);
use File::OSS::Scan::Ruleset;
use File::OSS::Scan::Matches;
use File::OSS::Scan::Cache;

our $VERSION = '0.04';

our @ISA = qw(Exporter);
our @EXPORT_OK = qw(scan_init scan_execute scan_result clear_cache);

our %EXPORT_TAGS = (
            all => \@EXPORT_OK,
            scan => [ @EXPORT_OK[0..2] ],
        );

my $curr_dir;
my $recur_depth;
my $scan_base = '';
my $anchor_file;

our $cmd_strings;
our $cmd_jar;
our $cmd_tar;
our $cmd_gunzip;
our $cmd_unzip;
our $cmd_uncompress;

my $ruleset = undef;
my $setting = undef;
my $result  = undef;

# list all valid options with their default values
my %valid_options = (
    ruleset_config  => undef,
    verbose         => VERBOSE_NORMAL,
    cache           => CACHE_NONE,
    strings         => '/bin/strings',
    jar             => '/bin/jar',
    tar             => '/bin/tar',
    gunzip          => '/bin/gunzip',
    unzip           => '/bin/unzip',
    uncompress      => '/bin/uncompress',
    working_dir     => getcwd() . "\/\.working",
    inflate         => UNI_FALSE,
);

my $options = undef;
my $user = getlogin() || ( getpwuid $< )[0];

sub scan_init {
    my %params = ( scalar(@_) != 1 ) ? @_ : ( 'ruleset_config' => $_[0] );

    # convert hash keys to lower case
    %params = map { lc $_ => $params{$_} } keys %params;

    # clear previously set options
    undef $options;

    # set options
    foreach my $opt ( keys %valid_options ) {
        $options->{$opt} = defined $params{$opt} ?
        $params{$opt} : $valid_options{$opt};
    }

    croak "invalid option verbose: $options->{'verbose'}"
    if ( ( $options->{'verbose'} !~ /^\d$/ ) ||
         ( $options->{'verbose'} < VERBOSE_SILIENT ) ||
         ( $options->{'verbose'} > VERBOSE_CHATTY ) );

    croak "invalid option cache: $options->{'cache'}"
    if ( ( $options->{'cache'} !~ /^\d$/ ) ||
         ( $options->{'cache'} < CACHE_NONE ) ||
         ( $options->{'cache'} > CACHE_REFRESH ) );

    croak "working directory $options->{'working_dir'} doesn't exist or not writable"
    if ( ! ( ( -d $options->{'working_dir'} and
                -w $options->{'working_dir'} ) ||
                    mkdir( $options->{'working_dir'}, 0755 ) ) );

    # empty the working directory,
    # should be very very cautious with the param working_dir ...
    system("rm -rf $options->{'working_dir'}/*");

    # make sure the tools are available
    foreach ( qw/strings jar tar gunzip unzip uncompress/ ) {
        no strict 'refs';

        my $cmd_var = __PACKAGE__ . '::cmd_' . $_;
        $$cmd_var = $options->{$_};

        if ( ! -x $$cmd_var ) {
            carp "unable to execute the $_ binary $$cmd_var";
            undef $$cmd_var;
        }
    }

    my $config_file = $options->{'ruleset_config'};

    # clear previously set rulesets
    undef $ruleset;

    # clear previous settings
    undef $setting;

    # initiate an Ruleset object with the rules
    # fetched from the config file.
    File::OSS::Scan::Ruleset->init($config_file);



( run in 1.200 second using v1.01-cache-2.11-cpan-39bf76dae61 )