--- rpms/e-smith-tinydns/sme9/e-smith-tinydns-2.4.0_add_hostname_following_dhcpdleases_hostname.patch 2014/05/18 15:10:52 1.1 +++ rpms/e-smith-tinydns/sme9/e-smith-tinydns-2.4.0_add_hostname_following_dhcpdleases_hostname.patch 2015/01/28 23:41:54 1.8 @@ -1,7 +1,7 @@ -diff -Nur e-smith-tinydns-2.4.0.old/createlinks e-smith-tinydns-2.4.0/createlinks ---- e-smith-tinydns-2.4.0.old/createlinks 2014-05-18 01:58:17.822368168 +0200 -+++ e-smith-tinydns-2.4.0/createlinks 2014-05-18 02:04:52.426622645 +0200 -@@ -26,3 +26,27 @@ +diff -Nur e-smith-tinydns-2.4.0.old2388/createlinks e-smith-tinydns-2.4.0.bz2388/createlinks +--- e-smith-tinydns-2.4.0.old2388/createlinks 2015-01-27 19:31:32.953849658 +0100 ++++ e-smith-tinydns-2.4.0.bz2388/createlinks 2015-01-27 20:23:29.080882560 +0100 +@@ -26,3 +26,19 @@ service_link_enhanced("tinydns", "K65", "6"); service_link_enhanced("tinydns", "K65", "0"); service_link_enhanced("tinydns", "K65", "1"); @@ -10,16 +10,8 @@ diff -Nur e-smith-tinydns-2.4.0.old/crea + for my $event (qw( dhcp-dns )) + { + # services to launch on event -+ safe_symlink("restart", "root/etc/e-smith/events/$event/services2adjust/dnscache"); ++ safe_symlink("sigusr1", "root/etc/e-smith/events/$event/services2adjust/dnscache"); + safe_symlink("sigusr1", "root/etc/e-smith/events/$event/services2adjust/tinydns"); -+ safe_symlink("restart", "root/etc/e-smith/events/$event/services2adjust/dhcpd"); -+ safe_symlink("restart", "root/etc/e-smith/events/$event/services2adjust/dhcp-dns"); -+ -+ # actions to perform -+ safe_symlink("../actions/dnscache-change", "root/etc/e-smith/events/$event/S20dnscache-change"); -+ -+ # Template to event -+ templates2events("/var/service/tinydns/root/data", $event); + } + +#creation of dhcp-dns service @@ -29,80 +21,167 @@ diff -Nur e-smith-tinydns-2.4.0.old/crea + service_link_enhanced("dhcp-dns", "K65", "0"); + service_link_enhanced("dhcp-dns", "K65", "1"); + safe_symlink("/var/service/dhcp-dns/", "root/service/dhcp-dns"); -diff -Nur e-smith-tinydns-2.4.0.old/root/etc/e-smith/db/configuration/defaults/dhcp-dns/status e-smith-tinydns-2.4.0/root/etc/e-smith/db/configuration/defaults/dhcp-dns/status ---- e-smith-tinydns-2.4.0.old/root/etc/e-smith/db/configuration/defaults/dhcp-dns/status 1970-01-01 01:00:00.000000000 +0100 -+++ e-smith-tinydns-2.4.0/root/etc/e-smith/db/configuration/defaults/dhcp-dns/status 2014-05-18 02:05:57.345788401 +0200 +diff -Nur e-smith-tinydns-2.4.0.old2388/root/etc/e-smith/db/configuration/defaults/dhcp-dns/status e-smith-tinydns-2.4.0.bz2388/root/etc/e-smith/db/configuration/defaults/dhcp-dns/status +--- e-smith-tinydns-2.4.0.old2388/root/etc/e-smith/db/configuration/defaults/dhcp-dns/status 1970-01-01 01:00:00.000000000 +0100 ++++ e-smith-tinydns-2.4.0.bz2388/root/etc/e-smith/db/configuration/defaults/dhcp-dns/status 2015-01-27 20:30:30.481143377 +0100 @@ -0,0 +1 @@ -+enabled -diff -Nur e-smith-tinydns-2.4.0.old/root/etc/e-smith/db/configuration/defaults/dhcp-dns/type e-smith-tinydns-2.4.0/root/etc/e-smith/db/configuration/defaults/dhcp-dns/type ---- e-smith-tinydns-2.4.0.old/root/etc/e-smith/db/configuration/defaults/dhcp-dns/type 1970-01-01 01:00:00.000000000 +0100 -+++ e-smith-tinydns-2.4.0/root/etc/e-smith/db/configuration/defaults/dhcp-dns/type 2014-05-18 02:05:57.304783191 +0200 ++disabled +diff -Nur e-smith-tinydns-2.4.0.old2388/root/etc/e-smith/db/configuration/defaults/dhcp-dns/type e-smith-tinydns-2.4.0.bz2388/root/etc/e-smith/db/configuration/defaults/dhcp-dns/type +--- e-smith-tinydns-2.4.0.old2388/root/etc/e-smith/db/configuration/defaults/dhcp-dns/type 1970-01-01 01:00:00.000000000 +0100 ++++ e-smith-tinydns-2.4.0.bz2388/root/etc/e-smith/db/configuration/defaults/dhcp-dns/type 2015-01-27 20:23:29.075882144 +0100 @@ -0,0 +1 @@ +service -diff -Nur e-smith-tinydns-2.4.0.old/root/etc/e-smith/events/actions/dnscache-change e-smith-tinydns-2.4.0/root/etc/e-smith/events/actions/dnscache-change ---- e-smith-tinydns-2.4.0.old/root/etc/e-smith/events/actions/dnscache-change 1970-01-01 01:00:00.000000000 +0100 -+++ e-smith-tinydns-2.4.0/root/etc/e-smith/events/actions/dnscache-change 2014-05-18 02:05:58.604948433 +0200 -@@ -0,0 +1,3 @@ -+#!/bin/sh -+ -+/usr/bin/sv 1 /service/tinydns -diff -Nur e-smith-tinydns-2.4.0.old/root/etc/e-smith/templates/var/service/tinydns/root/data/65dhcpARecords e-smith-tinydns-2.4.0/root/etc/e-smith/templates/var/service/tinydns/root/data/65dhcpARecords ---- e-smith-tinydns-2.4.0.old/root/etc/e-smith/templates/var/service/tinydns/root/data/65dhcpARecords 1970-01-01 01:00:00.000000000 +0100 -+++ e-smith-tinydns-2.4.0/root/etc/e-smith/templates/var/service/tinydns/root/data/65dhcpARecords 2014-05-18 02:06:05.227790167 +0200 -@@ -0,0 +1,48 @@ +diff -Nur e-smith-tinydns-2.4.0.old2388/root/etc/e-smith/templates/var/service/tinydns/root/data/65dhcpARecords e-smith-tinydns-2.4.0.bz2388/root/etc/e-smith/templates/var/service/tinydns/root/data/65dhcpARecords +--- e-smith-tinydns-2.4.0.old2388/root/etc/e-smith/templates/var/service/tinydns/root/data/65dhcpARecords 1970-01-01 01:00:00.000000000 +0100 ++++ e-smith-tinydns-2.4.0.bz2388/root/etc/e-smith/templates/var/service/tinydns/root/data/65dhcpARecords 2015-01-29 00:35:54.988996507 +0100 +@@ -0,0 +1,136 @@ +{ -+ # we need perl-Text-DHCPparse rpm from Dag's repo -+ use Text::DHCPparse; -+ -+ # we need perl-Date-Manip to convert to a unix timestamp -+ use Date::Manip qw /UnixDate/; -+ -+ # we need perl-Time-TAI64 for timestamp conversion ++ use strict; ++ use warnings; ++ use esmith::Logger; ++ use esmith::ConfigDB; ++ use esmith::HostsDB; + use Time::TAI64 qw /unixtai64/; ++ use Date::Manip qw /UnixDate/; ++ use File::Copy; ++ ++ my $DB = esmith::ConfigDB->open_ro or die ("Unable to open configuration database"); ++ my $hosts = esmith::HostsDB->open_ro or die ("Unable to open Host database"); ++ ++ my $dhcpdns = $DB->get_prop('dhcp-dns','status') || 'disabled'; ++ return '' if $dhcpdns ne 'enabled'; ++ ++ #we start a hash for name & ip ++ my %allocated_name = (); ++ my %allocated_ip_dhcplease = (); ++ ++ #start a hash of all hosts in DB ++ my @hosts = $hosts->keys; ++ my %hostsdb; ++ @hostsdb{@hosts} = (); ++ ++ # copy dhcpd.leases to /tmp ++ copy("/var/lib/dhcpd/dhcpd.leases","/tmp/tmpdhcpd.leases") ++ or die ("Error dhcp-dns Service : Unable to copy the /var/lib/dhcpd/dhcpd.leases"); ++ ++ #we want to write in log ++ sub log2messages ++ { ++ my $message = shift; ++ tie *FH, 'esmith::Logger'; ++ print FH "$message"; ++ close FH; ++ } ++ ++ #Text::DHCPparse forked because it doesn't allow to retrieve the end of lease ++ #Only the start is found by the leaseparse of DHCPparse.pm s/starts/ends ++ sub leaseparse ++ { ++ my $logfile = shift; ++ my ( %list, $ip ); ++ open FILE, $logfile or die; ++ ++ while () ++ { ++ next if /^#|^$/; ++ if (/^lease (\d+\.\d+\.\d+\.\d+)/) ++ { ++ $ip = $1; ++ $list{$ip} = sprintf("%-17s", $ip); ++ } ++ /^\s*hardware ethernet (.*);/ && ($list{$ip} .= sprintf("%-19s", $1)); ++ /^\s*ends \d (.*);/ && ($list{$ip} .= sprintf("%-21s", $1)); ++ /^\s*(abandoned).*/ && ($list{$ip} .= sprintf("%-19s", $1)); ++ /^\s*client-hostname "(.*)";/ && ($list{$ip} .= sprintf("%-17s", $1)); ++ } ++ ++ close FILE; ++ ++ # make all entries 74 characters long to format properly ++ foreach (keys %list) ++ { ++ $list{$_} = sprintf("%-74s", $list{$_}) if (length$list{$_} < 76); ++ } ++ ++ return \%list; ++ } ++ ++ #hostname validator routine ++ sub namevalidator ++ { ++ my $local_domain = $DB->get_value('DomainName') or die ("Unable retrieve the DomainName property"); ++ my $server_name = $DB->get_value('SystemName') or die ("Unable retrieve the SystemName property"); ++ my $validator = shift; ++ ++ if ($validator eq $server_name) { ++ log2messages("The hostname of this server ($server_name) is already in use with a different IP address in /var/lib/dhcpd/dhcpd.leases"); ++ return 1; ++ } ++ elsif ($validator !~ /^[a-zA-Z0-9][a-zA-Z0-9-]*$/) { ++ log2messages("The hostname of the dhcp client ($validator) contains illegal characters in /var/lib/dhcpd/dhcpd.leases"); ++ return 1; ++ } ++ elsif (exists $hostsdb{lc "$validator\.$local_domain"}) { ++ log2messages("The hostname of the dhcp client ($validator) is already used in the hosts database"); ++ return 1; ++ } ++ elsif (exists $allocated_name{$validator}) { ++ log2messages("The hostname ($validator) has already been assigned an IP address in /var/lib/dhcpd/dhcpd.leases"); ++ return 1; ++ } ++ else { ++ return 0; ++ } ++ } + -+ # copy of dhcpd.leases file to /tmp because working directly on the original -+ # is not a good idea -+ system "cat /var/lib/dhcpd/dhcpd.leases > /tmp/dhcpd.leases"; -+ + # now we parse the leases -+ $return = leaseparse('/tmp/dhcpd.leases'); -+ -+ # variable with local domain value (default is mycompamy.local) -+ my $localdomain = get_local_domainname; ++ my $return = leaseparse('/tmp/tmpdhcpd.leases'); ++ my ($ip,$time,$mac,$name); ++ ++ # retrieve the local domain name ++ my $localdomain = $DB->get_value('DomainName') or die ("Unable retrieve the DomainName property"); + + $OUT .= "# A records for dhcp hosts in $localdomain\n"; + -+ foreach (keys %$return) { ++ foreach (keys %$return) ++ { + ($ip, $time, $mac, $name) = unpack("A17 A21 A19 A30", $return->{$_}); + -+ # sometimes $name is empty.. we want only non empty one -+ if ( (defined $name) && (defined $ip)) { -+ # we skip also ips already allocated -+ unless (exists $allocated_ips{$ip}) -+ { -+ # Convert lease end time to the format expected as -+ # see: http://cr.yp.to/djbdns/tinydns-data.html -+ my $ts = UnixDate($time, "%s"); -+ my $endtai = unixtai64($ts); -+ $endtai =~ s/@//; -+ -+ # Determine TTL -+ my $ttl = ''; -+ $ttl = 0 unless ($ts <= time); ++ # we skip allocated ips & empty names ++ unless ((exists $allocated_ip_dhcplease{$ip}) || ( $name eq "" )) ++ { ++ # Convert lease end time to the format expected as ++ # see: http://cr.yp.to/djbdns/tinydns-data.html ++ my $ts = UnixDate($time, "%s"); ++ my $endtai = unixtai64($ts); ++ $endtai =~ s/@//; ++ ++ # Determine TTL ++ my $ttl = ''; ++ $ttl = 0 unless ($ts <= time); + -+ $OUT .= "=$name.$localdomain:$ip:$ttl:$endtai\n"; -+ } -+ $allocated_ips{$ip} = 1; ++ # display if the hostname is valid ++ $OUT .= "=$name.$localdomain:$ip:$ttl:$endtai\n" if (namevalidator("$name") ne '1'); + } ++ ++ $allocated_ip_dhcplease{$ip} = 1; ++ $allocated_name{$name} ++; + } + # remove the temp file -+ system "rm -f /tmp/dhcpd.leases"; ++ unlink "/tmp/tmpdhcpd.leases" ++ or log2messages("Error dhcp-dns Service : Unable to remove the /tmp/tmpdhcpd.leases"); +} -diff -Nur e-smith-tinydns-2.4.0.old/root/var/service/dhcp-dns/dhcp-dns e-smith-tinydns-2.4.0/root/var/service/dhcp-dns/dhcp-dns ---- e-smith-tinydns-2.4.0.old/root/var/service/dhcp-dns/dhcp-dns 1970-01-01 01:00:00.000000000 +0100 -+++ e-smith-tinydns-2.4.0/root/var/service/dhcp-dns/dhcp-dns 2014-05-18 02:06:08.370189551 +0200 -@@ -0,0 +1,33 @@ +diff -Nur e-smith-tinydns-2.4.0.old2388/root/var/service/dhcp-dns/dhcp-dns e-smith-tinydns-2.4.0.bz2388/root/var/service/dhcp-dns/dhcp-dns +--- e-smith-tinydns-2.4.0.old2388/root/var/service/dhcp-dns/dhcp-dns 1970-01-01 01:00:00.000000000 +0100 ++++ e-smith-tinydns-2.4.0.bz2388/root/var/service/dhcp-dns/dhcp-dns 2015-01-28 00:24:48.209462754 +0100 +@@ -0,0 +1,49 @@ +#!/usr/bin/perl ++use strict; ++use warnings; ++use esmith::ConfigDB; ++ ++my $DB = esmith::ConfigDB->open_ro or die ("Unable to open configuration database"); ++my $dhcpdns = $DB->get_prop('dhcp-dns','status') || 'disabled'; + +my $lease_file = "/var/lib/dhcpd/dhcpd.leases"; + @@ -110,11 +189,20 @@ diff -Nur e-smith-tinydns-2.4.0.old/root +my $modtime = 0; + +# seconds to wait -+my $update_freq = 15; ++my $update_freq = 30; ++ ++#we want to write in log ++sub log2messages ++ { ++ my $message = shift; ++ tie *FH, 'esmith::Logger'; ++ print FH "$message"; ++ close FH; ++ } + +########################################################################### +# Main Loop -+while (1) { ++while ($dhcpdns eq 'enabled') { + + # check the file's last updated time, if it's been changed, update + # the DNS and save the modified time. This will ALWAYS run once - on @@ -124,8 +212,11 @@ diff -Nur e-smith-tinydns-2.4.0.old/root + if ($stats[9] > $modtime) { + + $modtime = $stats[9]; ++ system ("/usr/bin/sv 1 /service/tinydns") ==0 ++ or log2messages('Error service dhcp-dns : Unable to restart /service/tinydns'); + -+ system ("/usr/bin/sv 1 /service/tinydns"); ++ system ("/usr/bin/sv 1 /service/dnscache") ==0 ++ or log2messages('Error service dhcp-dns : Unable to do restart /service/dnscache'); + } + + # wait till next check time @@ -133,11 +224,9 @@ diff -Nur e-smith-tinydns-2.4.0.old/root + +} # end main +########################################################################### -+ -+ -diff -Nur e-smith-tinydns-2.4.0.old/root/var/service/dhcp-dns/run e-smith-tinydns-2.4.0/root/var/service/dhcp-dns/run ---- e-smith-tinydns-2.4.0.old/root/var/service/dhcp-dns/run 1970-01-01 01:00:00.000000000 +0100 -+++ e-smith-tinydns-2.4.0/root/var/service/dhcp-dns/run 2014-05-18 02:06:08.429197051 +0200 +diff -Nur e-smith-tinydns-2.4.0.old2388/root/var/service/dhcp-dns/run e-smith-tinydns-2.4.0.bz2388/root/var/service/dhcp-dns/run +--- e-smith-tinydns-2.4.0.old2388/root/var/service/dhcp-dns/run 1970-01-01 01:00:00.000000000 +0100 ++++ e-smith-tinydns-2.4.0.bz2388/root/var/service/dhcp-dns/run 2015-01-27 20:23:29.079882477 +0100 @@ -0,0 +1,4 @@ +#!/bin/sh +