File Coverage

blib/lib/DNS/Oterica/RecordMaker/TinyDNS.pm
Criterion Covered Total %
statement 6 107 5.6
branch 0 30 0.0
condition 0 58 0.0
subroutine 2 22 9.0
pod 6 13 46.1
total 14 230 6.0


line stmt bran cond sub pod time code
1 1     1   3 use strict;
  1         1  
  1         23  
2 1     1   3 use warnings;
  1         1  
  1         1360  
3             package DNS::Oterica::RecordMaker::TinyDNS;
4             # ABSTRACT: a tinydns recordmaker for DNSO.
5             $DNS::Oterica::RecordMaker::TinyDNS::VERSION = '0.303';
6             #pod =head1 DESCRIPTION
7             #pod
8             #pod This role provides logic for generating lines for the F<tinydns-data> program
9             #pod to consume.
10             #pod
11             #pod =cut
12              
13 0     0     sub _default_ttl { 1800 }
14              
15             sub _serial_number {
16 0   0 0     return($ENV{DNS_OTERICA_SN} || $^T)
17             }
18              
19             #pod =method comment
20             #pod
21             #pod my $line = $rec->comment("Hello, world!");
22             #pod
23             #pod This returns a line that is a one-line commment.
24             #pod
25             #pod =cut
26              
27             sub comment {
28 0     0 1   my ($self, $comment) = @_;
29              
30 0           return "# $comment\n";
31             }
32              
33             #pod =method location
34             #pod
35             #pod This returns a location line.
36             #pod
37             #pod =cut
38              
39             sub location {
40 0     0 1   my ($self, $location) = @_;
41              
42 0 0         return if $location->code eq '';
43              
44 0 0         Carp::confess("location codes must be two-character")
45             unless length $location->code == 2;
46              
47 0           my @prefixes = $location->_class_prefixes;
48 0           map { sprintf "%%%s:%s\n", $location->code, $_ } @prefixes;
  0            
49             }
50              
51             sub __ip_locode_pairs {
52 0     0     my ($self, $rec) = @_;
53              
54 0 0         Carp::confess('no node provided') unless $rec->{node};
55              
56             return
57 0           map {; [ $_->[0] => $_->[1]->code ] }
58 0           $rec->{node}->interfaces;
59             }
60              
61             sub _generic {
62 0     0     my ($self, $op, $rec) = @_;
63              
64 0           my @lines;
65 0           for my $if ($self->__ip_locode_pairs($rec)) {
66             push @lines, sprintf "%s%s:%s:%s:%s:%s\n",
67             $op,
68             $rec->{name},
69             $if->[0],
70 0   0       $rec->{ttl} || $self->_default_ttl,
71             $self->_serial_number,
72             $if->[1],
73             ;
74             }
75              
76 0           return @lines;
77             }
78              
79             #pod =method a_and_ptr
80             #pod
81             #pod Generate an C<=> line, the bread and butter A and PTR record pair for a
82             #pod hostname and IP.
83             #pod
84             #pod =cut
85              
86             # =fqdn:ip:ttl:timestamp:lo
87             sub a_and_ptr {
88 0     0 1   my ($self, $rec) = @_;
89              
90             return (
91 0           $self->_generic(q{+}, $rec),
92             $self->ptr($rec),
93             );
94             }
95              
96             #pod =method ptr
97             #pod
98             #pod Generate an C<^> line, for the reverse DNS of an IP address.
99             #pod
100             #pod =cut
101              
102             # ^fqdn:ip:ttl:timestamp:lo
103             # can't use __generic here because it wants to look at interfaces, and we want
104             # the reverse of that
105             sub ptr {
106 0     0 1   my ($self, $rec) = @_;
107              
108 0           my @lines;
109 0           for my $if ($self->__ip_locode_pairs($rec)) {
110 0           my $ip = $if->[0];
111 0           my @bytes = reverse split /\./, $ip;
112              
113 0 0 0       splice @bytes, 1, 1, '0-24', $bytes[1]
      0        
114             if $bytes[1] eq 237 && $bytes[2] eq 72 && $bytes[3] eq 208;
115              
116 0           my $extended_arpa = join '.', @bytes, 'in-addr', 'arpa';
117             push @lines, sprintf "^%s:%s:%s:%s:%s\n",
118             $extended_arpa,
119             $rec->{name},
120 0 0 0       $rec->{ttl} || $self->_default_ttl,
121             $self->_serial_number,
122             $if->[1] eq 'FB' ? '' : $if->[1];
123             }
124              
125 0           return @lines;
126             }
127              
128             # TODO find out why we generate Z and & records for our IPs and refactor this
129             # to not duplicate effort with &ptr and the like. problem is that &a calls &ptr
130             # so having the code there means it gets called for every time we generate a +
131             # record, totally not what we want. What we want is for this to be called once
132             # for every IP address, not every hostname.
133             sub soa_and_ns_for_ip {
134 0     0 0   my ($self, $rec) = @_;
135              
136 0           my @lines;
137 0           my $node = $rec->{node};
138 0           my $ns_f = $node->hub->ns_family;
139 0           my %ns = $node->hub->node_family($ns_f)->ns_nodes;
140 0           my $ns_1 = (keys %ns)[0];
141 0           my $addr = $node->hub->soa_rname;
142 0           my $ip = $rec->{ip};
143 0           my @bytes = reverse split /\./, $ip;
144 0           my $arpa = join '.', @bytes, 'in-addr', 'arpa';
145              
146 0           push @lines, sprintf "Z%s:%s:%s::::::%s:%s:%s\n",
147             $arpa,
148             $ns_1,
149             $addr,
150             $self->_default_ttl,
151             $self->_serial_number,
152             '',
153             ;
154              
155 0           for my $ns (keys %ns) {
156 0           push @lines, $self->domain({
157             domain => $arpa,
158             ip => $ip,
159             ns => $ns,
160             });
161             }
162 0           return @lines;
163             }
164              
165             # +fqdn:ip:ttl:timestamp:lo
166             sub a {
167 0     0 0   my ($self, $rec) = @_;
168 0           my @lines = $self->_generic(q{+}, $rec);
169              
170 0           return @lines;
171             }
172              
173             # @fqdn:ip:x:dist:ttl:timestamp:lo
174             sub mx {
175 0     0 0   my ($self, $rec) = @_;
176              
177 0           my @lines;
178              
179             my $mx_name = defined $rec->{mx} ? $rec->{mx}
180             : $rec->{node} ? $rec->{node}->fqdn
181 0 0         : Carp::confess('neither mx nor node given as mx for mx record');
    0          
182              
183 0           for my $if ($self->__ip_locode_pairs($rec)) {
184             push @lines, sprintf "@%s:%s:%s:%s:%s:%s:%s\n",
185             $rec->{name},
186             ($rec->{no_ip} ? '' : $if->[0]),
187             $mx_name,
188             $rec->{dist} || 10,
189 0 0 0       $rec->{ttl} || $self->_default_ttl,
      0        
190             $self->_serial_number,
191             $if->[1],
192             ;
193             }
194              
195 0           return @lines;
196             }
197              
198             # .fqdn:ip:x:ttl:timestamp:lo
199             # This doesn't handle nodes, because I don't want to deal with ip-less records,
200             # which would cause __generic to barf. This is just a hack for now.
201             # -- rjbs, 2008-12-15
202             sub domain {
203 0     0 0   my ($self, $rec) = @_;
204              
205 0           my @lines;
206              
207             push @lines, sprintf "&%s:%s:%s:%s:%s:%s\n",
208             $rec->{domain},
209             $rec->{ip} || '',
210             $rec->{ns},
211 0   0       $rec->{ttl} || $self->_default_ttl,
      0        
212             $self->_serial_number,
213             '',
214             ;
215              
216 0           return @lines;
217             }
218              
219             sub soa_and_ns {
220 0     0 0   my ($self, $rec) = @_;
221              
222 0           my @lines;
223              
224             push @lines, sprintf "Z%s:%s:%s::::::%s:%s:%s\n",
225             $rec->{domain},
226             $rec->{ns} || '',
227             $rec->{node}->hub->soa_rname,
228 0   0       $rec->{ttl} || $self->_default_ttl,
      0        
229             $self->_serial_number,
230             '',
231             ;
232              
233 0           return @lines;
234             }
235              
236              
237             # Cfqdn:p:ttl:timestamp:lo
238             sub cname {
239 0     0 0   my ($self, $rec) = @_;
240              
241 0           my @lines;
242              
243             push @lines, sprintf "C%s:%s:%s:%s:%s\n",
244             $rec->{cname},
245             $rec->{domain} || '',
246 0   0       $rec->{ttl} || $self->_default_ttl,
      0        
247             $self->_serial_number,
248             '',
249             ;
250              
251 0           return @lines;
252             }
253              
254             sub txt {
255 0     0 0   my ($self, $rec) = @_;
256 0           my @lines;
257              
258 0           my $name = $rec->{name};
259 0 0 0       $name = $rec->{node}->fqdn if ! $name && $rec->{node};
260              
261 0 0 0       Carp::confess("no record name or node given for txt record")
262             unless defined $name and length $name;
263              
264             # 'fqdn:s:ttl:timestamp:lo
265             push @lines, sprintf qq{'%s:%s:%s:%s:%s\n},
266             $name,
267             _colon_safe($rec->{text}),
268 0   0       $rec->{ttl} || $self->_default_ttl,
269             $self->_serial_number,
270             '',
271             ;
272              
273 0           return @lines;
274             }
275              
276             sub _colon_safe {
277 0     0     my $str = $_[0];
278 0           $str =~ s/([^A-Za-z0-9=])/sprintf '\\%03o', ord $1/ge;
  0            
279 0           $str;
280             }
281              
282             sub _escaped_octals {
283 0     0     join q{}, map {; sprintf '\\%03o', ord } split //, pack 'n', $_[0];
  0            
284             }
285              
286             sub _hostname_to_labels {
287 0     0     my @labels = split /\./, $_[0];
288 0           my $str = '';
289 0           $str .= sprintf('\\%03o', length) . $_ for @labels;
290 0           $str .= '\000';
291              
292 0           return $str;
293             }
294              
295             #pod =method srv
296             #pod
297             #pod @lines = $rec->srv({
298             #pod # We want to produce _finger._tcp.example.com for port 70
299             #pod domain => 'example.com',
300             #pod service => 'finger',
301             #pod protocol => 'tcp',
302             #pod target => 'f.example.com',
303             #pod port => 70,
304             #pod
305             #pod priority => 10,
306             #pod weight => 20,
307             #pod });
308             #pod
309             #pod This returns lines for SRV records following RFC 2782. It takes the following
310             #pod special arguments:
311             #pod
312             #pod domain - the domain offering service
313             #pod service - the well-known service name (http, imaps, finger)
314             #pod protocol - tcp or udp
315             #pod
316             #pod target - the host providing service
317             #pod port - the port the service listens on
318             #pod
319             #pod priority - numeric priority; lower numbers should be used first
320             #pod weight - weight to break priority ties; higher numbers preferred
321             #pod
322             #pod =cut
323              
324             sub srv {
325 0     0 1   my ($self, $rec) = @_;
326              
327             Carp::confess("srv record with no target! use empty string for null target")
328 0 0         unless defined $rec->{target};
329              
330 0           for my $needed (qw(port service domain)) {
331             Carp::confess("tried to make srv record with no $needed!")
332 0 0         unless defined $rec->{$needed};
333             }
334              
335 0   0       my $priority = $rec->{priority} || 0;
336 0   0       my $weight = $rec->{weight} || 0;
337              
338 0           my @lines;
339             push @lines, sprintf ":_%s._%s.%s:33:%s%s%s%s:%s:%s\n",
340             $rec->{service},
341             $rec->{protocol} || 'tcp',
342             $rec->{domain},
343             _escaped_octals($priority),
344             _escaped_octals($weight),
345             _escaped_octals($rec->{port}),
346             _hostname_to_labels($rec->{target}),
347             $rec->{ttl} || $self->_default_ttl,
348 0   0       $rec->{location} || '';
      0        
      0        
349              
350 0           return @lines;
351             }
352              
353             #pod =method dkim
354             #pod
355             #pod This returns lines for TXT records for DKIM keys. It takes the following
356             #pod arguments:
357             #pod
358             #pod domain - the domain
359             #pod selector - the key selector
360             #pod
361             #pod ttl - record time to live
362             #pod
363             #pod tags - the DKIM record tags, a hashref
364             #pod
365             #pod Any tag given in the hashref will be included. C<p> is required.
366             #pod
367             #pod =cut
368              
369             sub dkim {
370 0     0 1   my ($self, $rec) = @_;
371              
372 0 0         Carp::confess("no domain for DKIM record") unless $rec->{domain};
373 0 0         Carp::confess("no selector for DKIM record") unless $rec->{selector};
374 0 0         Carp::confess("no public key for DKIM record") unless $rec->{tags}{p};
375              
376 0           my $tags = $rec->{tags};
377 0           my $name = "$rec->{selector}._domainkey.$rec->{domain}";
378 0           my $text = join q{; }, map {; "$_=$tags->{$_}" } sort keys %$tags;
  0            
379              
380             # We can't use ->txt because tinydns will split TXT records (generated by ')
381             # up into 127b chunks. DKIM doesn't let you do that. -- rjbs, 2016-10-04
382             return sprintf ":%s:16:\\%03o%s:%s\n",
383             $name,
384             length($text),
385             _colon_safe($text),
386 0   0       $rec->{ttl} || $self->_default_ttl;
387             }
388              
389             1;
390              
391             __END__
392              
393             =pod
394              
395             =encoding UTF-8
396              
397             =head1 NAME
398              
399             DNS::Oterica::RecordMaker::TinyDNS - a tinydns recordmaker for DNSO.
400              
401             =head1 VERSION
402              
403             version 0.303
404              
405             =head1 DESCRIPTION
406              
407             This role provides logic for generating lines for the F<tinydns-data> program
408             to consume.
409              
410             =head1 METHODS
411              
412             =head2 comment
413              
414             my $line = $rec->comment("Hello, world!");
415              
416             This returns a line that is a one-line commment.
417              
418             =head2 location
419              
420             This returns a location line.
421              
422             =head2 a_and_ptr
423              
424             Generate an C<=> line, the bread and butter A and PTR record pair for a
425             hostname and IP.
426              
427             =head2 ptr
428              
429             Generate an C<^> line, for the reverse DNS of an IP address.
430              
431             =head2 srv
432              
433             @lines = $rec->srv({
434             # We want to produce _finger._tcp.example.com for port 70
435             domain => 'example.com',
436             service => 'finger',
437             protocol => 'tcp',
438             target => 'f.example.com',
439             port => 70,
440              
441             priority => 10,
442             weight => 20,
443             });
444              
445             This returns lines for SRV records following RFC 2782. It takes the following
446             special arguments:
447              
448             domain - the domain offering service
449             service - the well-known service name (http, imaps, finger)
450             protocol - tcp or udp
451              
452             target - the host providing service
453             port - the port the service listens on
454              
455             priority - numeric priority; lower numbers should be used first
456             weight - weight to break priority ties; higher numbers preferred
457              
458             =head2 dkim
459              
460             This returns lines for TXT records for DKIM keys. It takes the following
461             arguments:
462              
463             domain - the domain
464             selector - the key selector
465              
466             ttl - record time to live
467              
468             tags - the DKIM record tags, a hashref
469              
470             Any tag given in the hashref will be included. C<p> is required.
471              
472             =head1 AUTHOR
473              
474             Ricardo SIGNES <rjbs@cpan.org>
475              
476             =head1 COPYRIGHT AND LICENSE
477              
478             This software is copyright (c) 2016 by Ricardo SIGNES.
479              
480             This is free software; you can redistribute it and/or modify it under
481             the same terms as the Perl 5 programming language system itself.
482              
483             =cut