#VERSION,2.07
###############################################################################
#  Copyright (C) 2007 Chris Sullo
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; version 2
#  of the License only.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to
#  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
###############################################################################
# PURPOSE:
# CSV Reporting
###############################################################################
sub nikto_report_csv_init {
    my $id = { name              => "report_csv",
               full_name         => "CSV reports",
               author            => "Tautology",
               description       => "Produces a CSV report.",
               report_head       => \&csv_open,
               report_host_start => \&csv_host_start,
               report_item       => \&csv_item,
               report_format     => 'csv',
               copyright         => "2008 Chris Sullo"
               };
    return $id;
}

###############################################################################
# open output file
sub csv_open {
    my ($file) = @_;
    print STDERR "+ ERROR: Output file not specified.\n" if $file eq '';

    # Open file and produce header
    open(OUT, ">>$file") || die print STDERR "+ ERROR: Unable to open '$file' for write: $@\n";

    # Write header
    print OUT "\"$VARIABLES{'name'} - v$VARIABLES{'version'}/$VARIABLES{'core_version'}\"\n";
    return OUT;
}

###############################################################################
# start output
sub csv_host_start {
    my ($handle, $mark) = @_;
    $mark->{'banner'} =~ s/"/\\"/g;
    my $hostname = $mark->{'vhost'} ? $mark->{'vhost'} : $mark->{'hostname'};
    print $handle "\"$hostname\","
      . "\"$mark->{'ip'}\","
      . "\"$mark->{'port'}\"," . "\"\"," . "\"\"," . "\"\","
      . "\"$mark->{'banner'}\"\n";
    return;
}

###############################################################################
# print an item
sub csv_item {
    my ($handle, $mark, $item) = @_;
    foreach my $uri (split(' ', $item->{'uri'})) {
        my $line = '';
    	my $hostname = $item->{'mark'}->{'vhost'} ? $item->{'mark'}->{'vhost'} : $item->{'mark'}->{'hostname'};
        $line .= "\"$hostname\",";
        $line .= "\"$item->{'mark'}->{'ip'}\",";
        $line .= "\"$item->{'mark'}->{'port'}\",";

        $line .= "\"";
        if ($item->{'osvdb'} ne '') { $line .= "OSVDB-" . $item->{'osvdb'}; }
        $line .= "\",";

        $line .= "\"";
        if ($item->{'method'} ne '') { $line .= $item->{'method'}; }
        $line .= "\",";

        $line .= "\"";
        if (($uri ne '') && ($mark->{'root'} ne '') && ($uri !~ /^$mark->{'root'}/))
			{ $line .= $mark->{'root'} . $uri; }
		else { $line .= $uri; }
        $line .= "\",";
	
		my $msg = $item->{'message'};
		$uri=quotemeta($uri);
		my $root = quotemeta($mark->{'root'});
		$msg =~ s/^$uri:\s//;
		$msg =~ s/^$root$uri:\s//;
        $msg =~ s/"/\\"/g;
        $line .= "\"$msg\"";
        print $handle "$line\n";
    }
}

1;
