Article Listing 1 Listing 2 aug2004.tar

Listing 2 tinydns-worm-update

#!/usr/bin/perl 
# tinydns-worm-update
#
# Update tinydns with tinydns data on hosts infected with email worms.
#
# Copyright 2004 Philip B Chase
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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 the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

# Locations of some critical files and other config data
my $updates_dir = $ENV{UPDATES_DIR};
my $tinydns_root = $ENV{TINYDNS_ROOT};
my $data_file = $ENV{DATA_FILE};
my $sleeptime = $ENV{SLEEPTIME};

# Commands we will need to run.
my $updates_file = "$updates_dir/data";
my $check_updates = "cd $updates_dir; tinydns-data";
my $copy_updates = "/bin/sort -u < $updates_dir/data \
  >$tinydns_root/$data_file";
my $make_data = "cd $tinydns_root; make";

use strict;
use POSIX;
use vars qw($self $old_time $new_time $updates_dir $tinydns_root);

init();

while (1) {
   $new_time = (-M $updates_file);
   process_updates() if ($old_time != $new_time);
   sleep($sleeptime);
}

sub init {
    ($self = $0) =~ s!.*/!!;    # What's my name?

    # Avoid the initial restart.
    $old_time = (-M $updates_file);
    $new_time = $old_time;

    -e $updates_dir or die "$self: updates directory $updates_dir does \
                            not exist.\n";
    -e $tinydns_root or die "$self: tinydns data directory $tinydns_root \
                             does not exist.\n";
}

sub process_updates {
    my $check_output;
    # set time variable to new time so we do this only once.
    $old_time=$new_time;

    # check new data
    $check_output = `$check_updates`;
    if (!$check_output) {
        system $copy_updates;
        system $make_data;
    } else {
        print STDERR "$self: tinydns data file $updates_dir is bad.  \
                      Ignoring new data.\n";
    }
}