33 |
+service |
+service |
34 |
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 |
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 |
35 |
--- 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.old2388/root/etc/e-smith/templates/var/service/tinydns/root/data/65dhcpARecords 1970-01-01 01:00:00.000000000 +0100 |
36 |
+++ e-smith-tinydns-2.4.0.bz2388/root/etc/e-smith/templates/var/service/tinydns/root/data/65dhcpARecords 2015-01-28 00:28:30.792458726 +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 |
37 |
@@ -0,0 +1,128 @@ |
@@ -0,0 +1,136 @@ |
38 |
+{ |
+{ |
39 |
+ use strict; |
+ use strict; |
40 |
+ use warnings; |
+ use warnings; |
41 |
+ use esmith::Logger; |
+ use esmith::Logger; |
42 |
+ use esmith::ConfigDB; |
+ use esmith::ConfigDB; |
43 |
+ use esmith::HostsDB; |
+ use esmith::HostsDB; |
|
+ # we need perl-Time-TAI64 for timestamp conversion |
|
44 |
+ use Time::TAI64 qw /unixtai64/; |
+ use Time::TAI64 qw /unixtai64/; |
|
+ # we need perl-Date-Manip to convert to a unix timestamp |
|
45 |
+ use Date::Manip qw /UnixDate/; |
+ use Date::Manip qw /UnixDate/; |
46 |
+ use File::Copy; |
+ use File::Copy; |
47 |
+ |
+ |
48 |
+ my $DB = esmith::ConfigDB->open_ro or die ("Unable to open configuration database"); |
+ my $DB = esmith::ConfigDB->open_ro or die ("Unable to open configuration database"); |
49 |
+ my $hosts = esmith::HostsDB->open_ro or die ("Unable to open Host database"); |
+ my $hosts = esmith::HostsDB->open_ro or die ("Unable to open Host database"); |
50 |
+ |
+ |
|
+ #test if the service is enabled return if not |
|
51 |
+ my $dhcpdns = $DB->get_prop('dhcp-dns','status') || 'disabled'; |
+ my $dhcpdns = $DB->get_prop('dhcp-dns','status') || 'disabled'; |
52 |
+ return '' if $dhcpdns ne 'enabled'; |
+ return '' if $dhcpdns ne 'enabled'; |
53 |
+ |
+ |
55 |
+ my %allocated_name = (); |
+ my %allocated_name = (); |
56 |
+ my %allocated_ip_dhcplease = (); |
+ my %allocated_ip_dhcplease = (); |
57 |
+ |
+ |
58 |
+ #start a hash of all hosts |
+ #start a hash of all hosts in DB |
59 |
+ my @hosts = $hosts->keys; |
+ my @hosts = $hosts->keys; |
60 |
+ my %hostsdb; |
+ my %hostsdb; |
61 |
+ @hostsdb{@hosts} = (); |
+ @hostsdb{@hosts} = (); |
62 |
+ |
+ |
63 |
+ # copy of dhcpd.leases file to /tmp because working directly on the original |
+ # copy dhcpd.leases to /tmp |
|
+ # is not a good idea |
|
64 |
+ copy("/var/lib/dhcpd/dhcpd.leases","/tmp/tmpdhcpd.leases") |
+ copy("/var/lib/dhcpd/dhcpd.leases","/tmp/tmpdhcpd.leases") |
65 |
+ or die ("Error dhcp-dns Service : Unable to copy the /var/lib/dhcpd/dhcpd.leases"); |
+ or die ("Error dhcp-dns Service : Unable to copy the /var/lib/dhcpd/dhcpd.leases"); |
66 |
+ |
+ |
67 |
+ #we want to write in log |
+ #we want to write in log |
68 |
+ sub log2messages |
+ sub log2messages |
69 |
+ { |
+ { |
70 |
+ my $message = shift; |
+ my $message = shift; |
71 |
+ tie *FH, 'esmith::Logger'; |
+ tie *FH, 'esmith::Logger'; |
72 |
+ print FH "$message"; |
+ print FH "$message"; |
73 |
+ close FH; |
+ close FH; |
74 |
+ } |
+ } |
75 |
+ |
+ |
76 |
+ |
+ #Text::DHCPparse forked because it doesn't allow to retrieve the end of lease |
77 |
+ #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 |
78 |
+ #Only the start is found by the leaseparse of DHCPparse.pm s/starts/ends |
+ sub leaseparse |
79 |
+ sub leaseparse { |
+ { |
80 |
+ my $logfile = shift; |
+ my $logfile = shift; |
81 |
+ my ( %list, $ip ); |
+ my ( %list, $ip ); |
82 |
+ open FILE, $logfile or die; |
+ open FILE, $logfile or die; |
83 |
+ |
+ |
84 |
+ while (<FILE>) { |
+ while (<FILE>) |
85 |
+ next if /^#|^$/; |
+ { |
86 |
+ if (/^lease (\d+\.\d+\.\d+\.\d+)/) { |
+ next if /^#|^$/; |
87 |
+ $ip = $1; |
+ if (/^lease (\d+\.\d+\.\d+\.\d+)/) |
88 |
+ $list{$ip} = sprintf("%-17s", $ip); |
+ { |
89 |
+ } |
+ $ip = $1; |
90 |
+ /^\s*hardware ethernet (.*);/ && ($list{$ip} .= sprintf("%-19s", $1)); |
+ $list{$ip} = sprintf("%-17s", $ip); |
91 |
+ /^\s*ends \d (.*);/ && ($list{$ip} .= sprintf("%-21s", $1)); |
+ } |
92 |
+ /^\s*(abandoned).*/ && ($list{$ip} .= sprintf("%-19s", $1)); |
+ /^\s*hardware ethernet (.*);/ && ($list{$ip} .= sprintf("%-19s", $1)); |
93 |
+ /^\s*client-hostname "(.*)";/ && ($list{$ip} .= sprintf("%-17s", $1)); |
+ /^\s*ends \d (.*);/ && ($list{$ip} .= sprintf("%-21s", $1)); |
94 |
+ } |
+ /^\s*(abandoned).*/ && ($list{$ip} .= sprintf("%-19s", $1)); |
95 |
+ |
+ /^\s*client-hostname "(.*)";/ && ($list{$ip} .= sprintf("%-17s", $1)); |
96 |
+ close FILE; |
+ } |
97 |
+ |
+ |
98 |
+ # make all entries 74 characters long to format properly |
+ close FILE; |
99 |
+ foreach (keys %list) { |
+ |
100 |
+ #$list{$_} = sprintf("%-74s", $list{$_}) if (length$list{$_} < 76); |
+ # make all entries 74 characters long to format properly |
101 |
+ $list{$_} = sprintf("%-74.74s", $list{$_}); |
+ foreach (keys %list) |
102 |
+ } |
+ { |
103 |
|
+ $list{$_} = sprintf("%-74s", $list{$_}) if (length$list{$_} < 76); |
104 |
|
+ } |
105 |
+ |
+ |
106 |
+ return \%list; |
+ return \%list; |
107 |
+} |
+ } |
108 |
|
+ |
109 |
|
+ #hostname validator routine |
110 |
|
+ sub namevalidator |
111 |
|
+ { |
112 |
|
+ my $local_domain = $DB->get_value('DomainName') or die ("Unable retrieve the DomainName property"); |
113 |
|
+ my $server_name = $DB->get_value('SystemName') or die ("Unable retrieve the SystemName property"); |
114 |
|
+ my $validator = shift; |
115 |
|
+ |
116 |
|
+ if ($validator eq $server_name) { |
117 |
|
+ log2messages("The hostname of this server ($server_name) is already in use with a different IP address in /var/lib/dhcpd/dhcpd.leases"); |
118 |
|
+ return 1; |
119 |
|
+ } |
120 |
|
+ elsif ($validator !~ /^[a-zA-Z0-9][a-zA-Z0-9-]*$/) { |
121 |
|
+ log2messages("The hostname of the dhcp client ($validator) contains illegal characters in /var/lib/dhcpd/dhcpd.leases"); |
122 |
|
+ return 1; |
123 |
|
+ } |
124 |
|
+ elsif (exists $hostsdb{lc "$validator\.$local_domain"}) { |
125 |
|
+ log2messages("The hostname of the dhcp client ($validator) is already used in the hosts database"); |
126 |
|
+ return 1; |
127 |
|
+ } |
128 |
|
+ elsif (exists $allocated_name{$validator}) { |
129 |
|
+ log2messages("The hostname ($validator) has already been assigned an IP address in /var/lib/dhcpd/dhcpd.leases"); |
130 |
|
+ return 1; |
131 |
|
+ } |
132 |
|
+ else { |
133 |
|
+ return 0; |
134 |
|
+ } |
135 |
|
+ } |
136 |
+ |
+ |
137 |
+ # now we parse the leases |
+ # now we parse the leases |
138 |
+ my $return = leaseparse('/tmp/tmpdhcpd.leases'); |
+ my $return = leaseparse('/tmp/tmpdhcpd.leases'); |
139 |
+ my ($ip,$time,$mac,$name); |
+ my ($ip,$time,$mac,$name); |
140 |
+ |
+ |
141 |
+ # variable with local domain value (default is mycompamy.local) and retrieve the server name |
+ # retrieve the local domain name |
142 |
+ my $localdomain = $DB->get_value('DomainName') or die ("Unable retrieve the DomainName property"); |
+ my $localdomain = $DB->get_value('DomainName') or die ("Unable retrieve the DomainName property"); |
|
+ my $servername = $DB->get_value('SystemName') or die ("Unable retrieve the SystemName property"); |
|
143 |
+ |
+ |
144 |
+ $OUT .= "# A records for dhcp hosts in $localdomain\n"; |
+ $OUT .= "# A records for dhcp hosts in $localdomain\n"; |
145 |
+ |
+ |
146 |
+ foreach (keys %$return) { |
+ foreach (keys %$return) |
147 |
|
+ { |
148 |
+ ($ip, $time, $mac, $name) = unpack("A17 A21 A19 A30", $return->{$_}); |
+ ($ip, $time, $mac, $name) = unpack("A17 A21 A19 A30", $return->{$_}); |
149 |
+ |
+ |
150 |
+ # when the dhcp lease is over $name is empty .. we want only non empty one |
+ # we skip allocated ips & empty names |
151 |
+ if ( $name ne "" ) |
+ unless ((exists $allocated_ip_dhcplease{$ip}) || ( $name eq "" )) |
152 |
+ { |
+ { |
153 |
+ # we skip also ips & names already allocated |
+ # Convert lease end time to the format expected as |
154 |
+ unless (exists $allocated_ip_dhcplease{$ip} || exists $allocated_name{$name}) |
+ # see: http://cr.yp.to/djbdns/tinydns-data.html |
155 |
+ { |
+ my $ts = UnixDate($time, "%s"); |
156 |
+ # Convert lease end time to the format expected as |
+ my $endtai = unixtai64($ts); |
157 |
+ # see: http://cr.yp.to/djbdns/tinydns-data.html |
+ $endtai =~ s/@//; |
158 |
+ my $ts = UnixDate($time, "%s"); |
+ |
159 |
+ my $endtai = unixtai64($ts); |
+ # Determine TTL |
160 |
+ $endtai =~ s/@//; |
+ my $ttl = ''; |
161 |
+ |
+ $ttl = 0 unless ($ts <= time); |
|
+ # Determine TTL |
|
|
+ my $ttl = ''; |
|
|
+ $ttl = 0 unless ($ts <= time); |
|
|
+ |
|
|
+ # verify if dhcp client name is not used in host DB |
|
|
+ if (! exists $hostsdb{"$name\.$localdomain"}) |
|
|
+ { |
|
|
+ $OUT .= "=$name.$localdomain:$ip:$ttl:$endtai\n" unless (($name eq $servername) || ($name !~ /^[a-zA-Z0-9][a-zA-Z0-9-]*$/)); |
|
|
+ log2messages("The hostname of this server ($servername) is already in use with a different IP address in /var/lib/dhcpd/dhcpd.leases") |
|
|
+ if ($name eq $servername); |
|
|
+ log2messages("The hostname of the dhcp client ($name) has illegal characters in /var/lib/dhcpd/dhcpd.leases") |
|
|
+ if ($name !~ /^[a-zA-Z0-9][a-zA-Z0-9-]*$/); |
|
|
+ } |
|
|
+ else |
|
|
+ { |
|
|
+ log2messages("The hostname of the dhcp client ($name) is already used in the hosts database"); |
|
|
+ } |
|
|
+ } |
|
162 |
+ |
+ |
163 |
+ $allocated_ip_dhcplease{$ip} = 1; |
+ # display if the hostname is valid |
164 |
+ $allocated_name{$name} ++; |
+ $OUT .= "=$name.$localdomain:$ip:$ttl:$endtai\n" if (namevalidator("$name") ne '1'); |
|
+ |
|
|
+ log2messages("The hostname \"$name\" has already been assigned an IP address in /var/lib/dhcpd/dhcpd.leases") |
|
|
+ if ($allocated_name{$name} >1); |
|
165 |
+ } |
+ } |
166 |
|
+ |
167 |
|
+ $allocated_ip_dhcplease{$ip} = 1; |
168 |
|
+ $allocated_name{$name} ++; |
169 |
+ } |
+ } |
170 |
+ # remove the temp file |
+ # remove the temp file |
171 |
+ unlink "/tmp/tmpdhcpd.leases" |
+ unlink "/tmp/tmpdhcpd.leases" |