#!/usr/bin/perl # DHIS DNS Perl Engine by Dan Mahoney, please contact dhis@gushi.org with questions/comments/suggestions # Last updated 9/25/2002 #Modified to fit to tinydns 1.05 by Nikademus nikademus@llorien.org # Set this to the location of your dhis.db. # You may need a different one. $database = "/etc/dhis/db/dhis.db"; # Set this to the location of tinydns root on your system # the directory where you can have the data file $tinydnsroot = "/etc/tinydns/root"; # Set this to the IP this module should set for an offline host. $offline = "192.168.255.0"; # Set this for the TTL a host should have. 60 was the DHIS original value. $ttl = "60"; # logging setup. By default DHIS has no way of logging the activity of the child processes # so I added this kludge here. It's heavy on file opens, but it's better to allow you to read # the logfile in realtime (with tail -f) whereas if I didn't open so frequently the output would # be buffered. # set to 1 to turn on logging (only recommended for debugging) $logging = "0"; # specify a log file. The default will do but isn't very secure. $logfile = "/tmp/dhis.log"; # set this to be whatever service you have defined in the DHIS service.db for this module # otherwise this module will ignore the update requests $thisservice = "dns"; ########## End Of Configuration ########## logthis("Starting up"); &cleardb; &dbread; &main; logthis("EOF Received, exiting"); sub dbread { logthis("Reading database \"$database\""); open DATABASE, "$database" or die "Unable to read database $database"; while () { $dbline = $_; # This parse code works on my system, it may not work on yours, it assumes # there are no spaces between your hostid and the beginning of the line. # It also assumes you've followed the directions and put the curly brackets in. &logthis("reading database line \"$dbline\""); if ($dbline =~ /^(\d*)\s\{/) { &logthis("ID \"$1\" located"); $currentid = $1; } if ($dbline =~ /hostname\s*(.*)\n/) { &logthis("setting hostname of host $currentid to \"$1\""); $hostname{$currentid} = "$1"; } } } sub cleardb { foreach $key (keys %hostname) { delete $hostname{$key}; } } sub main { while () { $line = $_; if ($line =~ /reload/) { &logthis("reload command issued, reloading"); &cleardb; &dbread; &logthis("database reload complete"); } elsif ($line =~ /exit/) { &logthis("exit command received, exiting"); close STDIN; exit 0; } # add & update definitions elsif ($line =~ /add\s(\w*)\s(\d*)\s(.*)\n/ || $line =~ /update\s(\w*)\s(\d*)\s(.*)\n/) { &logthis("update received:\"$line\""); if (($hostname{$2}) && ($thisservice eq $1)) { &logthis("that's for $hostname{$2}"); $newdata = "\+$hostname{$2}\:$3\:$ttl\n"; update_zone(); } } # delete definition elsif ($line =~ /delete\s(\w*)\s(\d*)\s(.*)\n/) { &logthis("update received:\"$line\""); &logthis("that's for $hostname{$2}"); if (($hostname{$2}) && ($thisservice eq $1)) { &logthis("thisservice = .$thisservice. and $1 is the arg"); $newdata= "\+$hostname{$2}:$offline:$ttl\n"; update_zone(); } } } } sub logthis ($) { if ($logging eq 1) { $thing = shift; open LOGFILE, ">>$logfile"; print LOGFILE "$thing\n"; close LOGFILE; } } sub update_zone() { open (DATA, "<$tinydnsroot/data") || die print $!; @data=; close (DATA); open (DATA, ">$tinydnsroot/data") || die print $!; for ($i=0;$i<@data;$i++) { if ($data[$i] =~ /\+$hostname{$2}\:([0-9]{1,3}\.){3}[0-9]{1,3}\:$ttl/ ) { print DATA $newdata; print $newdata; } else { print DATA $data[$i]; } } close (DATA); system ("cd $tinydnsroot/ && /usr/local/bin/tinydns-data"); }