File Coverage

blib/lib/Net/DNS/Create/Bind.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             # Copyright (c) 2009-2013 David Caldwell, All Rights Reserved.
2              
3             package Net::DNS::Create::Bind;
4 1     1   6 use Net::DNS::Create qw(internal full_host local_host email interval);
  1         2  
  1         11  
5 1     1   15 use feature ':5.10';
  1         2  
  1         132  
6 1     1   5 use strict;
  1         1  
  1         29  
7 1     1   4 use warnings;
  1         2  
  1         34  
8              
9 1     1   952 use POSIX qw(strftime);
  1         7759  
  1         14  
10 1     1   1894 use File::Slurp qw(write_file);
  0            
  0            
11              
12             our %config = (conf_prefix=>'', default_ttl=>'1h', dest_dir=>'.');
13             sub import {
14             my $package = shift;
15             my %c = @_;
16             $config{$_} = $c{$_} for keys %c;
17             }
18              
19             sub txt(@) {
20             return "\"$_[0]\"" if scalar @_ == 1;
21             '('.join("\n" . " " x 41, map { "\"$_\"" } @_).')';
22             }
23              
24             our @zone;
25             sub domain {
26             my ($package, $domain, $entries) = @_;
27              
28             my $conf = '$TTL '.interval($config{default_ttl})."\n".
29             join '', map { ;
30             my $rr = lc $_->type;
31             my $ttl = $_->ttl != interval($config{default_ttl}) ? $_->ttl : "";
32             my $prefix = sprintf "%-30s %7s in %-5s", local_host($_->name, $domain), $ttl, $rr;
33              
34             $rr eq 'mx' ? "$prefix ".$_->preference." ".local_host($_->exchange, $domain)."\n" :
35             $rr eq 'ns' ? "$prefix ".local_host($_->nsdname, $domain)."\n" :
36             $rr eq 'txt' ? "$prefix ".txt($_->char_str_list)."\n" :
37             $rr eq 'srv' ? "$prefix ".join(' ', $_->priority, $_->weight, $_->port, local_host($_->target, $domain))."\n" :
38             $rr eq 'rp' ? "$prefix ".local_host(email($_->mbox), $domain)." ".local_host($_->txtdname, $domain)."\n" :
39             $rr eq 'soa' ? "$prefix ".join(' ', local_host($_->mname, $domain),
40             local_host(email($_->rname), $domain),
41             '(',
42             $_->serial || strftime('%g%m%d%H%M', localtime),
43             $_->refresh,
44             $_->retry,
45             $_->expire,
46             $_->minimum,
47             ')')."\n" :
48             $rr eq 'a' ? "$prefix ".$_->address."\n" :
49             $rr eq 'cname' ? "$prefix ".local_host($_->cname, $domain)."\n" :
50             die __PACKAGE__." doesn't handle $rr record types";
51             } @$entries;
52              
53             my $conf_name = "$config{dest_dir}/$config{conf_prefix}$domain.zone";
54             $conf_name =~ s/\.\././g;
55             push @zone, { conf => $conf_name, domain => $domain };
56             write_file($conf_name, $conf);
57             }
58              
59             sub master {
60             my ($package, $filename, $prefix, @extra) = @_;
61             $prefix //= '';
62             my $master_file_name = "$config{dest_dir}/$config{conf_prefix}$filename";
63             write_file($master_file_name,
64             @extra,
65             map { <
66             zone "$_->{domain}" {
67             type master;
68             file "$prefix$_->{conf}";
69             };
70              
71             EOZ
72             } @zone);
73             system("named-checkconf", "-z", $master_file_name);
74             }
75              
76             sub domain_list($@) {
77             print "$config{conf_prefix}$_[0].zone\n";
78             }
79              
80             sub master_list($$) {
81             print "$config{conf_prefix}$_[0]\n"
82             }
83              
84             1;
85             __END__