/[smeserver]/rpms/e-smith-tinydns/sme9/e-smith-tinydns-2.4.0_add_hostname_following_dhcpdleases_hostname.patch
ViewVC logotype

Annotation of /rpms/e-smith-tinydns/sme9/e-smith-tinydns-2.4.0_add_hostname_following_dhcpdleases_hostname.patch

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (hide annotations) (download)
Tue Jan 27 23:35:57 2015 UTC (9 years, 4 months ago) by stephdl
Branch: MAIN
Changes since 1.6: +55 -30 lines
now we filter bad dns name for dhcp client, dhcp-dns is disabled by default and we look after its status for doing the job

1 stephdl 1.7 diff -Nur e-smith-tinydns-2.4.0.old2388/createlinks e-smith-tinydns-2.4.0.bz2388/createlinks
2     --- e-smith-tinydns-2.4.0.old2388/createlinks 2015-01-27 19:31:32.953849658 +0100
3     +++ e-smith-tinydns-2.4.0.bz2388/createlinks 2015-01-27 20:23:29.080882560 +0100
4 stephdl 1.5 @@ -26,3 +26,19 @@
5 stephdl 1.1 service_link_enhanced("tinydns", "K65", "6");
6     service_link_enhanced("tinydns", "K65", "0");
7     service_link_enhanced("tinydns", "K65", "1");
8     +
9     +#link to services2adjust and templates2expand for dhcp-dns event
10     + for my $event (qw( dhcp-dns ))
11     + {
12     + # services to launch on event
13 stephdl 1.5 + safe_symlink("sigusr1", "root/etc/e-smith/events/$event/services2adjust/dnscache");
14 stephdl 1.1 + safe_symlink("sigusr1", "root/etc/e-smith/events/$event/services2adjust/tinydns");
15     + }
16     +
17     +#creation of dhcp-dns service
18     + safe_symlink("daemontools", "root/etc/rc.d/init.d/dhcp-dns");
19     + service_link_enhanced("dhcp-dns", "S85", "7");
20     + service_link_enhanced("dhcp-dns", "K65", "6");
21     + service_link_enhanced("dhcp-dns", "K65", "0");
22     + service_link_enhanced("dhcp-dns", "K65", "1");
23     + safe_symlink("/var/service/dhcp-dns/", "root/service/dhcp-dns");
24 stephdl 1.7 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
25     --- 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
26     +++ 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
27 stephdl 1.1 @@ -0,0 +1 @@
28 stephdl 1.7 +disabled
29     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
30     --- 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
31     +++ 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
32 stephdl 1.1 @@ -0,0 +1 @@
33     +service
34 stephdl 1.7 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
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
37     @@ -0,0 +1,128 @@
38 stephdl 1.1 +{
39 stephdl 1.5 + use strict;
40     + use warnings;
41     + use esmith::Logger;
42     + use esmith::ConfigDB;
43 stephdl 1.7 + use esmith::HostsDB;
44 stephdl 1.5 + # we need perl-Time-TAI64 for timestamp conversion
45     + use Time::TAI64 qw /unixtai64/;
46 stephdl 1.1 + # we need perl-Date-Manip to convert to a unix timestamp
47     + use Date::Manip qw /UnixDate/;
48 stephdl 1.5 + use File::Copy;
49     +
50 stephdl 1.7 + my $DB = esmith::ConfigDB->open_ro or die ("Unable to open configuration database");
51     + my $hosts = esmith::HostsDB->open_ro or die ("Unable to open Host database");
52 stephdl 1.5 +
53 stephdl 1.7 + #test if the service is enabled return if not
54     + my $dhcpdns = $DB->get_prop('dhcp-dns','status') || 'disabled';
55     + return '' if $dhcpdns ne 'enabled';
56     +
57     + #we start a hash for name & ip
58 stephdl 1.5 + my %allocated_name = ();
59     + my %allocated_ip_dhcplease = ();
60 stephdl 1.1 +
61 stephdl 1.7 + #start a hash of all hosts
62     + my @hosts = $hosts->keys;
63     + my %hostsdb;
64     + @hostsdb{@hosts} = ();
65     +
66 stephdl 1.1 + # copy of dhcpd.leases file to /tmp because working directly on the original
67     + # is not a good idea
68 stephdl 1.5 + copy("/var/lib/dhcpd/dhcpd.leases","/tmp/tmpdhcpd.leases")
69     + or die ("Error dhcp-dns Service : Unable to copy the /var/lib/dhcpd/dhcpd.leases");
70     +
71 stephdl 1.7 + #we want to write in log
72 stephdl 1.5 + sub log2messages
73     + {
74     + my $message = shift;
75     + tie *FH, 'esmith::Logger';
76     + print FH "$message";
77     + close FH;
78     + }
79     +
80 stephdl 1.3 +
81     + #Text::DHCPparse forked because it doesn't allow to retrieve the end of lease
82     + #Only the start is found by the leaseparse of DHCPparse.pm s/starts/ends
83     + sub leaseparse {
84     + my $logfile = shift;
85     + my ( %list, $ip );
86     + open FILE, $logfile or die;
87     +
88     + while (<FILE>) {
89     + next if /^#|^$/;
90     + if (/^lease (\d+\.\d+\.\d+\.\d+)/) {
91     + $ip = $1;
92     + $list{$ip} = sprintf("%-17s", $ip);
93     + }
94     + /^\s*hardware ethernet (.*);/ && ($list{$ip} .= sprintf("%-19s", $1));
95     + /^\s*ends \d (.*);/ && ($list{$ip} .= sprintf("%-21s", $1));
96     + /^\s*(abandoned).*/ && ($list{$ip} .= sprintf("%-19s", $1));
97     + /^\s*client-hostname "(.*)";/ && ($list{$ip} .= sprintf("%-17s", $1));
98     + }
99     +
100     + close FILE;
101     +
102     + # make all entries 74 characters long to format properly
103     + foreach (keys %list) {
104     + #$list{$_} = sprintf("%-74s", $list{$_}) if (length$list{$_} < 76);
105     + $list{$_} = sprintf("%-74.74s", $list{$_});
106     + }
107     +
108     + return \%list;
109     +}
110     +
111 stephdl 1.1 + # now we parse the leases
112 stephdl 1.5 + my $return = leaseparse('/tmp/tmpdhcpd.leases');
113     + my ($ip,$time,$mac,$name);
114     +
115     + # variable with local domain value (default is mycompamy.local) and retrieve the server name
116     + my $localdomain = $DB->get_value('DomainName') or die ("Unable retrieve the DomainName property");
117     + my $servername = $DB->get_value('SystemName') or die ("Unable retrieve the SystemName property");
118 stephdl 1.1 +
119     + $OUT .= "# A records for dhcp hosts in $localdomain\n";
120     +
121     + foreach (keys %$return) {
122     + ($ip, $time, $mac, $name) = unpack("A17 A21 A19 A30", $return->{$_});
123 stephdl 1.5 +
124     + # when the dhcp lease is over $name is empty .. we want only non empty one
125     + if ( $name ne "" )
126     + {
127     + # we skip also ips & names already allocated
128     + unless (exists $allocated_ip_dhcplease{$ip} || exists $allocated_name{$name})
129 stephdl 1.1 + {
130     + # Convert lease end time to the format expected as
131     + # see: http://cr.yp.to/djbdns/tinydns-data.html
132     + my $ts = UnixDate($time, "%s");
133     + my $endtai = unixtai64($ts);
134     + $endtai =~ s/@//;
135     +
136     + # Determine TTL
137     + my $ttl = '';
138     + $ttl = 0 unless ($ts <= time);
139 stephdl 1.7 +
140     + # verify if dhcp client name is not used in host DB
141     + if (! exists $hostsdb{"$name\.$localdomain"})
142     + {
143     + $OUT .= "=$name.$localdomain:$ip:$ttl:$endtai\n" unless (($name eq $servername) || ($name !~ /^[a-zA-Z0-9][a-zA-Z0-9-]*$/));
144     + log2messages("The hostname of this server ($servername) is already in use with a different IP address in /var/lib/dhcpd/dhcpd.leases")
145     + if ($name eq $servername);
146     + log2messages("The hostname of the dhcp client ($name) has illegal characters in /var/lib/dhcpd/dhcpd.leases")
147     + if ($name !~ /^[a-zA-Z0-9][a-zA-Z0-9-]*$/);
148     + }
149     + else
150     + {
151     + log2messages("The hostname of the dhcp client ($name) is already used in the hosts database");
152     + }
153 stephdl 1.1 + }
154 stephdl 1.5 +
155     + $allocated_ip_dhcplease{$ip} = 1;
156     + $allocated_name{$name} ++;
157     +
158 stephdl 1.6 + log2messages("The hostname \"$name\" has already been assigned an IP address in /var/lib/dhcpd/dhcpd.leases")
159 stephdl 1.5 + if ($allocated_name{$name} >1);
160 stephdl 1.1 + }
161     + }
162     + # remove the temp file
163 stephdl 1.5 + unlink "/tmp/tmpdhcpd.leases"
164     + or log2messages("Error dhcp-dns Service : Unable to remove the /tmp/tmpdhcpd.leases");
165 stephdl 1.1 +}
166 stephdl 1.7 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
167     --- e-smith-tinydns-2.4.0.old2388/root/var/service/dhcp-dns/dhcp-dns 1970-01-01 01:00:00.000000000 +0100
168     +++ e-smith-tinydns-2.4.0.bz2388/root/var/service/dhcp-dns/dhcp-dns 2015-01-28 00:24:48.209462754 +0100
169     @@ -0,0 +1,49 @@
170 stephdl 1.1 +#!/usr/bin/perl
171 stephdl 1.5 +use strict;
172     +use warnings;
173 stephdl 1.7 +use esmith::ConfigDB;
174     +
175     +my $DB = esmith::ConfigDB->open_ro or die ("Unable to open configuration database");
176     +my $dhcpdns = $DB->get_prop('dhcp-dns','status') || 'disabled';
177 stephdl 1.1 +
178     +my $lease_file = "/var/lib/dhcpd/dhcpd.leases";
179     +
180     +# last modified time
181     +my $modtime = 0;
182     +
183     +# seconds to wait
184 stephdl 1.3 +my $update_freq = 30;
185 stephdl 1.1 +
186 stephdl 1.7 +#we want to write in log
187 stephdl 1.5 +sub log2messages
188     + {
189     + my $message = shift;
190     + tie *FH, 'esmith::Logger';
191     + print FH "$message";
192     + close FH;
193     + }
194     +
195 stephdl 1.1 +###########################################################################
196     +# Main Loop
197 stephdl 1.7 +while ($dhcpdns eq 'enabled') {
198 stephdl 1.1 +
199     + # check the file's last updated time, if it's been changed, update
200     + # the DNS and save the modified time. This will ALWAYS run once - on
201     + # startup, since $modtime starts at zero.
202     +
203     + my @stats = stat ($lease_file);
204     + if ($stats[9] > $modtime) {
205     +
206     + $modtime = $stats[9];
207 stephdl 1.3 + system ("/usr/bin/sv 1 /service/tinydns") ==0
208 stephdl 1.5 + or log2messages('Error service dhcp-dns : Unable to restart /service/tinydns');
209 stephdl 1.3 +
210     + system ("/usr/bin/sv 1 /service/dnscache") ==0
211 stephdl 1.5 + or log2messages('Error service dhcp-dns : Unable to do restart /service/dnscache');
212 stephdl 1.1 + }
213     +
214     + # wait till next check time
215     + sleep $update_freq;
216     +
217     +} # end main
218     +###########################################################################
219 stephdl 1.7 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
220     --- e-smith-tinydns-2.4.0.old2388/root/var/service/dhcp-dns/run 1970-01-01 01:00:00.000000000 +0100
221     +++ e-smith-tinydns-2.4.0.bz2388/root/var/service/dhcp-dns/run 2015-01-27 20:23:29.079882477 +0100
222 stephdl 1.1 @@ -0,0 +1,4 @@
223     +#!/bin/sh
224     +
225     +exec 2>&1
226     +exec /usr/bin/perl dhcp-dns

admin@koozali.org
ViewVC Help
Powered by ViewVC 1.2.1 RSS 2.0 feed