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

Contents 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.8 - (show annotations) (download)
Wed Jan 28 23:41:54 2015 UTC (9 years, 3 months ago) by stephdl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +87 -79 lines
same patch but now there is a name validator routine

1 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 @@ -26,3 +26,19 @@
5 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 + safe_symlink("sigusr1", "root/etc/e-smith/events/$event/services2adjust/dnscache");
14 + 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 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 @@ -0,0 +1 @@
28 +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 @@ -0,0 +1 @@
33 +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
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-29 00:35:54.988996507 +0100
37 @@ -0,0 +1,136 @@
38 +{
39 + use strict;
40 + use warnings;
41 + use esmith::Logger;
42 + use esmith::ConfigDB;
43 + use esmith::HostsDB;
44 + use Time::TAI64 qw /unixtai64/;
45 + use Date::Manip qw /UnixDate/;
46 + use File::Copy;
47 +
48 + 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");
50 +
51 + my $dhcpdns = $DB->get_prop('dhcp-dns','status') || 'disabled';
52 + return '' if $dhcpdns ne 'enabled';
53 +
54 + #we start a hash for name & ip
55 + my %allocated_name = ();
56 + my %allocated_ip_dhcplease = ();
57 +
58 + #start a hash of all hosts in DB
59 + my @hosts = $hosts->keys;
60 + my %hostsdb;
61 + @hostsdb{@hosts} = ();
62 +
63 + # copy dhcpd.leases to /tmp
64 + 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");
66 +
67 + #we want to write in log
68 + sub log2messages
69 + {
70 + my $message = shift;
71 + tie *FH, 'esmith::Logger';
72 + print FH "$message";
73 + close FH;
74 + }
75 +
76 + #Text::DHCPparse forked because it doesn't allow to retrieve the end of lease
77 + #Only the start is found by the leaseparse of DHCPparse.pm s/starts/ends
78 + sub leaseparse
79 + {
80 + my $logfile = shift;
81 + my ( %list, $ip );
82 + open FILE, $logfile or die;
83 +
84 + while (<FILE>)
85 + {
86 + next if /^#|^$/;
87 + if (/^lease (\d+\.\d+\.\d+\.\d+)/)
88 + {
89 + $ip = $1;
90 + $list{$ip} = sprintf("%-17s", $ip);
91 + }
92 + /^\s*hardware ethernet (.*);/ && ($list{$ip} .= sprintf("%-19s", $1));
93 + /^\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 + }
97 +
98 + close FILE;
99 +
100 + # make all entries 74 characters long to format properly
101 + foreach (keys %list)
102 + {
103 + $list{$_} = sprintf("%-74s", $list{$_}) if (length$list{$_} < 76);
104 + }
105 +
106 + 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
138 + my $return = leaseparse('/tmp/tmpdhcpd.leases');
139 + my ($ip,$time,$mac,$name);
140 +
141 + # retrieve the local domain name
142 + my $localdomain = $DB->get_value('DomainName') or die ("Unable retrieve the DomainName property");
143 +
144 + $OUT .= "# A records for dhcp hosts in $localdomain\n";
145 +
146 + foreach (keys %$return)
147 + {
148 + ($ip, $time, $mac, $name) = unpack("A17 A21 A19 A30", $return->{$_});
149 +
150 + # we skip allocated ips & empty names
151 + unless ((exists $allocated_ip_dhcplease{$ip}) || ( $name eq "" ))
152 + {
153 + # Convert lease end time to the format expected as
154 + # see: http://cr.yp.to/djbdns/tinydns-data.html
155 + my $ts = UnixDate($time, "%s");
156 + my $endtai = unixtai64($ts);
157 + $endtai =~ s/@//;
158 +
159 + # Determine TTL
160 + my $ttl = '';
161 + $ttl = 0 unless ($ts <= time);
162 +
163 + # display if the hostname is valid
164 + $OUT .= "=$name.$localdomain:$ip:$ttl:$endtai\n" if (namevalidator("$name") ne '1');
165 + }
166 +
167 + $allocated_ip_dhcplease{$ip} = 1;
168 + $allocated_name{$name} ++;
169 + }
170 + # remove the temp file
171 + unlink "/tmp/tmpdhcpd.leases"
172 + or log2messages("Error dhcp-dns Service : Unable to remove the /tmp/tmpdhcpd.leases");
173 +}
174 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
175 --- e-smith-tinydns-2.4.0.old2388/root/var/service/dhcp-dns/dhcp-dns 1970-01-01 01:00:00.000000000 +0100
176 +++ e-smith-tinydns-2.4.0.bz2388/root/var/service/dhcp-dns/dhcp-dns 2015-01-28 00:24:48.209462754 +0100
177 @@ -0,0 +1,49 @@
178 +#!/usr/bin/perl
179 +use strict;
180 +use warnings;
181 +use esmith::ConfigDB;
182 +
183 +my $DB = esmith::ConfigDB->open_ro or die ("Unable to open configuration database");
184 +my $dhcpdns = $DB->get_prop('dhcp-dns','status') || 'disabled';
185 +
186 +my $lease_file = "/var/lib/dhcpd/dhcpd.leases";
187 +
188 +# last modified time
189 +my $modtime = 0;
190 +
191 +# seconds to wait
192 +my $update_freq = 30;
193 +
194 +#we want to write in log
195 +sub log2messages
196 + {
197 + my $message = shift;
198 + tie *FH, 'esmith::Logger';
199 + print FH "$message";
200 + close FH;
201 + }
202 +
203 +###########################################################################
204 +# Main Loop
205 +while ($dhcpdns eq 'enabled') {
206 +
207 + # check the file's last updated time, if it's been changed, update
208 + # the DNS and save the modified time. This will ALWAYS run once - on
209 + # startup, since $modtime starts at zero.
210 +
211 + my @stats = stat ($lease_file);
212 + if ($stats[9] > $modtime) {
213 +
214 + $modtime = $stats[9];
215 + system ("/usr/bin/sv 1 /service/tinydns") ==0
216 + or log2messages('Error service dhcp-dns : Unable to restart /service/tinydns');
217 +
218 + system ("/usr/bin/sv 1 /service/dnscache") ==0
219 + or log2messages('Error service dhcp-dns : Unable to do restart /service/dnscache');
220 + }
221 +
222 + # wait till next check time
223 + sleep $update_freq;
224 +
225 +} # end main
226 +###########################################################################
227 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
228 --- e-smith-tinydns-2.4.0.old2388/root/var/service/dhcp-dns/run 1970-01-01 01:00:00.000000000 +0100
229 +++ e-smith-tinydns-2.4.0.bz2388/root/var/service/dhcp-dns/run 2015-01-27 20:23:29.079882477 +0100
230 @@ -0,0 +1,4 @@
231 +#!/bin/sh
232 +
233 +exec 2>&1
234 +exec /usr/bin/perl dhcp-dns

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