#!/usr/bin/perl

# Autor: Sebastian Enger, 14.04.05 
# Zweck: Ceckt nach Security Patches für Debian-Systeme und lädt diese bei bedarf automatisch herrunter
# future: installiere heruntergeladene datein gleich

use LWP::Simple;
use HTML::LinkExtor;
use LWP::UserAgent;
use URI::URL;

%debian_security_sites = (	

       "2005" => 'http://www.debian.org/security/2005/',	
       "2004" => 'http://www.debian.org/security/2004/',
       "2003" => 'http://www.debian.org/security/2003/',
       "2002" => 'http://www.debian.org/security/2002/',
       "2001" => 'http://www.debian.org/security/2001/',
       "2000" => 'http://www.debian.org/security/2000/',
   );
   
$count = "1";
  
system("clear");
print "$0 - check for Security Patches for Debian Linux and download them\n";
print "Enter Year to check for updates [2000 2001 2002 2003 2004 2005]\n";

chomp($year=<STDIN>);

if ($year !~ /\d/) { 
	print "Only Numbers please!\n";
	print "Exiting now!\n";
	exit;
} else {

	# make folder structure to download files to
	# /tmp/security_patches/$year/$number -> download
	mkdir("/tmp/security_patches", 0755);
	mkdir("/tmp/security_patches/$year", 0755);
	
	while ( my ($key, $value) = each(%debian_security_sites) ) {
		
		if ($key == $year) {
			
			$doc = get($value);	# get debian site with advisories in
			@content = split(/\n/, $doc);
			
			foreach $line (@content){
				
				if ($line =~ /<tt>/) {	# if an Security Advisories was found
				
					# extract the important entries
				
					@important = split(/>/,$line);
					@important[1] =~ s/<\/tt//;
					@it = split(/"/,@important[3]); 
					substr(@it[1], 0, 2) = "";
					@important[4] =~ s/<\/a//;
					@important[6] =~ s/ - //;
					@important[6] =~ s/ <br//;
					
					print "DATE: @important[1]\n";
					print "PROG: @important[4]\n";
					print "KIND: @important[6]\n\n";
					print " URL: ($count) $value@it[1]\n";	# I LOVE PERL!
					
					push(@download, "$count $value@it[1]");
					
					$count++;
				}
			}
		}
	}

	print "\nDownload which Security Advisory?Type number or all to download all to /tmp!\n";
	chomp($files = <STDIN>);
	
	if ($files =~ /all/i){
	
		print "DOWNLOADING ALL PATCHES FROM $year to /tmp/security_patches/$year/ !\n";
		sleep 1;
		
		foreach $f_entry (@download){
			@uris = split(/ /,$f_entry); # split into number and uri
			&gather_patchfiles(@uris[0],@uris[1]); # get the files and download them to folder!
		}
	
	} else {
	
		print "DOWNLOADING CHOOSEN FILES TO /tmp/security_patches/$year/ ";
		@files = split(/ /,$files);
	
		foreach $entry (@files){
		
			foreach $number (@download){
		
				if ($entry == $number){
					@imp = split(/ /, $number); # split into number and uri
					&gather_patchfiles(@imp[0],@imp[1]);
				} 
			}
		}
	}# else
	
}


sub gather_patchfiles {

$patch_number 	= @_[0];
$url 		= @_[1];

$ua = new LWP::UserAgent;
$p = HTML::LinkExtor->new(\&collect);

	sub collect {

		my($tag, %attr) = @_;
			if  ($tag =~ m/^a$/i ) {   # Nur <A HREF ...
        			push(@A_HREF, values %attr);
			}
	}# sub collect

	$response = $ua->request(HTTP::Request->new(GET => $url), sub { $p->parse($_[0]) } );

	$base = $response->base;
	@A_HREF = map { $_ = url($_, $base)->abs; } @A_HREF;
	
	foreach $file (@A_HREF){
	
		if (reverse($file) =~ /^bed./){	# only *.deb files contain patches
			@filename = split(/\//, $file);
			print "Downloading @filename[$#filename] to $year/$patch_number\n";
			mkdir("/tmp/security_patches/$year/$patch_number", 0755);
			chdir("/tmp/security_patches/$year/$patch_number");
			getstore("$file","@filename[$#filename]");
		}
	}
	
}# sub gather_patchfiles