| 1 | package Net::DNS::Utility; | ||
| 2 | |||
| 3 | use 5.006001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Debug; | ||
| 7 | use Net::IPv6Address; | ||
| 8 | |||
| 9 | require Exporter; | ||
| 10 | |||
| 11 | our @ISA = qw(Exporter); | ||
| 12 | |||
| 13 | # Items to export into callers namespace by default. Note: do not export | ||
| 14 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 15 | # Do not simply export all your public functions/methods/constants. | ||
| 16 | |||
| 17 | # This allows declaration use Net::DNS::Utility ':all'; | ||
| 18 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 19 | # will save memory. | ||
| 20 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 21 | |||
| 22 | ) ] ); | ||
| 23 | |||
| 24 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 25 | |||
| 26 | our @EXPORT = qw( | ||
| 27 | |||
| 28 | ); | ||
| 29 | |||
| 30 | our $VERSION = '0.02'; | ||
| 31 | |||
| 32 | # Preloaded methods go here. | ||
| 33 | |||
| 34 | my $package = __PACKAGE__; | ||
| 35 | my $logger = new Debug(); | ||
| 36 | |||
| 37 | my $prefixlen = undef; | ||
| 38 | my $length = undef; | ||
| 39 | my $type = "ip6.arpa"; | ||
| 40 | my $maxsections = 8; | ||
| 41 | my $debug = 0; | ||
| 42 | my $dnsserver = "bind"; | ||
| 43 | my $ttl = 3600; | ||
| 44 | my $serialno = 0; | ||
| 45 | my $refresh = "1H"; | ||
| 46 | my $retry = "15M"; | ||
| 47 | my $expiry = "1D"; | ||
| 48 | my $minimum = "12H"; | ||
| 49 | my $nameserver = "ns1.example.com."; | ||
| 50 | my $emailaddr = "root.example.com"; | ||
| 51 | my $allowtransfer = "any"; | ||
| 52 | my $allowupdate = "none"; | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my $class = shift; | ||
| 56 | my $self = {}; | ||
| 57 | my $addressLength = shift; | ||
| 58 | |||
| 59 | # $logger->initialize(); | ||
| 60 | |||
| 61 | $self->{'ADDRESSLENGTH'} = $addressLength if defined $addressLength; | ||
| 62 | |||
| 63 | bless $self, $class; | ||
| 64 | return $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | # addressLength() - use this function to set the address/length attibute of this object. This would be used to override the address/length attribute that is set when | ||
| 68 | # constructed or if no address/length was supplied when the object was constructed. Address/length passed in or if no argument specified currently | ||
| 69 | # set address/length attribute is returned. | ||
| 70 | sub addressLength() { | ||
| 71 | my $self = shift; | ||
| 72 | my $addressLength = shift || $self->{'ADDRESSLENGTH'}; | ||
| 73 | |||
| 74 | $self->{'ADDRESSLENGTH'} = $addressLength if defined $addressLength; | ||
| 75 | |||
| 76 | return $self->{'ADDRESSLENGTH'}; | ||
| 77 | } | ||
| 78 | |||
| 79 | # loadDebug() - this routine accepts a Debug.pm object to facilitate initialization of the debugging | ||
| 80 | sub loadDebug() { | ||
| 81 | my $self = shift; | ||
| 82 | my $debug = shift; | ||
| 83 | |||
| 84 | $logger = bless $debug, "Debug"; | ||
| 85 | } | ||
| 86 | |||
| 87 | # createIp6ReverseZone() - accepts two strings one prefix and one prefix length. A string representation of the valid reverse zone is returned as a string. | ||
| 88 | # The prefix must be fully uncompressed to ensure creation of a proper IPv6 reverse zone. | ||
| 89 | sub createIp6ReverseZone() { | ||
| 90 | my $self = shift; | ||
| 91 | my $prefix = shift; | ||
| 92 | my $prefixlen = shift; | ||
| 93 | my $revprefix = undef; | ||
| 94 | my $revprefixlen = undef; | ||
| 95 | my @revprefixarray = undef; | ||
| 96 | my $revzonename = undef; | ||
| 97 | |||
| 98 | $prefix =~ s/://g; | ||
| 99 | |||
| 100 | $logger->message("Processing prefix $prefix"); | ||
| 101 | |||
| 102 | $revprefix = reverse($prefix); | ||
| 103 | $logger->message("Prefix reversed $revprefix"); | ||
| 104 | |||
| 105 | $revprefixlen = length($revprefix); | ||
| 106 | @revprefixarray = split(//, $revprefix); | ||
| 107 | |||
| 108 | foreach my $x (@revprefixarray) { | ||
| 109 | # $logger->message("$x"); | ||
| 110 | $revzonename .= $x; | ||
| 111 | $revzonename .= "."; | ||
| 112 | } | ||
| 113 | |||
| 114 | $revzonename .= "ip6.arpa"; | ||
| 115 | $logger->message("Reverse zone $revzonename"); | ||
| 116 | |||
| 117 | if($dnsserver eq "bind") { | ||
| 118 | # $logger->message("$revzonename, $prefix, $prefixlen"); | ||
| 119 | # $logger->message("$revzonename, $prefix, $prefixlen"); | ||
| 120 | } else { | ||
| 121 | $logger->message("Unsupported implementation of DNS"); | ||
| 122 | } | ||
| 123 | |||
| 124 | return $revzonename; | ||
| 125 | } | ||
| 126 | |||
| 127 | # createBindNamedConf() - used to create the stanza required for the provided IPv6 reverze zone. | ||
| 128 | sub createBindNamedConf() { | ||
| 129 | my $self = shift; | ||
| 130 | my $ip6arpa = shift; | ||
| 131 | my $strippedprefix = shift; | ||
| 132 | my $prefixlen = shift; | ||
| 133 | |||
| 134 | print STDOUT "########################################################\n"; | ||
| 135 | print STDOUT "\; copy the following lines and paste into named.conf\n"; | ||
| 136 | print STDOUT "zone \"$ip6arpa\" IN \{ \n"; | ||
| 137 | print STDOUT "\ttype master\;\n"; | ||
| 138 | print STDOUT "\tallow-transfer \{ $allowtransfer\; \}\;\n"; | ||
| 139 | print STDOUT "\tallow-update \{ $allowupdate\; \}\;\n"; | ||
| 140 | print STDOUT "\tfile \"db.$strippedprefix-$prefixlen\"\;\n"; | ||
| 141 | print STDOUT "\}\;\n"; | ||
| 142 | print STDOUT "########################################################\n"; | ||
| 143 | } | ||
| 144 | |||
| 145 | # createBindNamedDb() - used to create an exmaple ISC BIND DB file for the supplied IPv6 reverse zone. | ||
| 146 | sub createBindNamedDb() { | ||
| 147 | my $self = shift; | ||
| 148 | my $ip6arpa = shift; | ||
| 149 | my $strippedprefix = shift; | ||
| 150 | my $prefixlen = shift; | ||
| 151 | my $dbfile = "db.$strippedprefix-$prefixlen"; | ||
| 152 | |||
| 153 | $logger->message("[create_nameddb]:dbfile=$dbfile"); | ||
| 154 | |||
| 155 | open(NDB, ">$dbfile") || &log("[create_nameddb]:error opening $dbfile:$1"); | ||
| 156 | print NDB "\; named zone file for $dbfile\n"; | ||
| 157 | print NDB "\$TTL $ttl\n"; | ||
| 158 | print NDB "\$ORIGIN $ip6arpa.\n"; | ||
| 159 | print NDB "\@ IN SOA $nameserver $emailaddr \( \n"; | ||
| 160 | print NDB "\t\t$serialno \; serial number\n"; | ||
| 161 | print NDB "\t\t$refresh \; refresh\n"; | ||
| 162 | print NDB "\t\t$retry \; retry\n"; | ||
| 163 | print NDB "\t\t$expiry \; expiry\n"; | ||
| 164 | print NDB "\t\t$minimum \) \; minimum\n"; | ||
| 165 | print NDB "\tIN NS\t$nameserver\n"; | ||
| 166 | print NDB "\$ORIGIN $ip6arpa.\n"; | ||
| 167 | print NDB "\; insert PTR records here, for example\n"; | ||
| 168 | print NDB "\; e.f.a.c.f.e.e.b.d.a.e.d.d.c.b.a IN PTR foo.example.com.\n"; | ||
| 169 | close(NDB); | ||
| 170 | } | ||
| 171 | |||
| 172 | # createPtrData() - used to generate the interface portion of a PTR record based on prefix and prefix length that have been provided. | ||
| 173 | # A string is returned. | ||
| 174 | sub createPtrData() { | ||
| 175 | # returns two values reversed portion of IPv6 address that is required based on prefix length and FQDN | ||
| 176 | my $self = shift; | ||
| 177 | my $interface = shift; | ||
| 178 | my $s_rInterface = undef; | ||
| 179 | my $s_rInterfaceFormatted = undef; | ||
| 180 | my $i_interfaceLen = undef; | ||
| 181 | my @a_interface = undef; | ||
| 182 | |||
| 183 | $s_rInterface = reverse($interface); | ||
| 184 | $logger->message("Interface reversed $s_rInterface"); | ||
| 185 | |||
| 186 | $i_interfaceLen = length($s_rInterface); | ||
| 187 | @a_interface = split(//, $s_rInterface); | ||
| 188 | |||
| 189 | foreach my $x (@a_interface) { | ||
| 190 | $s_rInterfaceFormatted .= $x; | ||
| 191 | $s_rInterfaceFormatted .= "."; | ||
| 192 | } | ||
| 193 | |||
| 194 | return $s_rInterfaceFormatted; | ||
| 195 | } | ||
| 196 | |||
| 197 | # parse() - | ||
| 198 | sub parse() { | ||
| 199 | my $self = shift; | ||
| 200 | my $prefixlen = shift; | ||
| 201 | |||
| 202 | $logger->message("Processing $prefixlen"); | ||
| 203 | |||
| 204 | (my $p, my $l) = split(/\//,$prefixlen); | ||
| 205 | |||
| 206 | $logger->message("Returning prefix=$p, length=$l"); | ||
| 207 | |||
| 208 | return $p, $l; | ||
| 209 | } | ||
| 210 | |||
| 211 | 1; | ||
| 212 | __END__ |