9 use File::Copy qw(copy);
11 my %conf = %main::conf;
12 my $chans = $conf{chans};
13 my $staff = $conf{staff};
15 my $hash = $conf{hash};
16 my $hostname = $conf{hostname};
17 my $verbose = $conf{verbose};
20 my $ip6subnet = $conf{ip6subnet};
21 my $zonedir = $conf{zonedir};
22 my $hostnameif = $conf{hostnameif};
23 if (host($hostname) =~ /(\d+\.){3,}\d+/) {
26 main::cbind("msg", "-", "setrdns", \&msetrdns);
27 main::cbind("msg", "-", "delrdns", \&mdelrdns);
28 main::cbind("msg", "-", "setdns", \&msetdns);
29 main::cbind("msg", "-", "deldns", \&mdeldns);
30 main::cbind("msg", "-", "host", \&mhost);
31 main::cbind("msg", "-", "nextdns", \&mnextdns);
32 main::cbind("msg", "-", "readip6s", \&mreadip6s);
35 unveil("$zonedir", "rwc") or die "Unable to unveil $!";
36 unveil("/usr/bin/doas", "rx") or die "Unable to unveil $!";
37 unveil("/usr/bin/host", "rx") or die "Unable to unveil $!";
38 unveil("$hostnameif", "rwc") or die "Unable to unveil $!";
41 # !setrdns 2001:bd8:: username.example.com
43 my ($bot, $nick, $host, $hand, $text) = @_;
44 if (! (main::isstaff($bot, $nick))) { return; }
45 if ($text =~ /^([0-9A-Fa-f:\.]{3,})\s+([-0-9A-Za-z\.]+)$/) {
46 my ($ip, $hostname) = ($1, $2);
47 if (setrdns($ip, $ip6subnet, $hostname)) {
48 main::putserv($bot, "PRIVMSG $nick :$hostname set to $ip");
50 main::putserv($bot, "PRIVMSG $nick :ERROR: failed to set rDNS");
57 my ($bot, $nick, $host, $hand, $text) = @_;
58 if (! (main::isstaff($bot, $nick))) { return; }
59 if ($text =~ /^([0-9A-Fa-f:\.]{3,})$/) {
61 if (delrdns($ip, $ip6subnet)) {
62 main::putserv($bot, "PRIVMSG $nick :$ip rDNS deleted");
64 main::putserv($bot, "PRIVMSG $nick :ERROR: failed to set rDNS");
68 # !setdns username 1.2.3.4
70 my ($bot, $nick, $host, $hand, $text) = @_;
71 if (! (main::isstaff($bot, $nick))) { return; }
72 if ($text =~ /^([-0-9A-Za-z\.]+)\s+([0-9A-Fa-f:\.]+)/) {
73 my ($name, $value) = ($1, $2);
74 if ($value =~ /:/ and setdns($name, $hostname, "AAAA", $value)) {
75 main::putserv($bot, "PRIVMSG $nick :$name.$hostname AAAA set to $value");
76 } elsif (setdns($name, $hostname, "A", $value)) {
77 main::putserv($bot, "PRIVMSG $nick :$name.$hostname A set to $value");
79 main::putserv($bot, "PRIVMSG $nick :ERROR: failed to set DNS");
86 my ($bot, $nick, $host, $hand, $text) = @_;
87 if (! (main::isstaff($bot, $nick))) { return; }
88 if ($text =~ /^([-0-9A-Za-z\.]+)$/) {
90 if (setdns($name, $hostname)) {
91 main::putserv($bot, "PRIVMSG $nick :$text deleted");
93 main::putserv($bot, "PRIVMSG $nick :ERROR: failed to delete DNS records");
100 my ($bot, $nick, $host, $hand, $text) = @_;
101 if (! (main::isstaff($bot, $nick))) { return; }
102 if ($text =~ /^([-0-9A-Za-z:\.]{3,})/) {
103 my ($hostname) = ($1);
104 main::putserv($bot, "PRIVMSG $nick :".host($hostname));
110 my ($bot, $nick, $host, $hand, $text) = @_;
111 if (! (main::isstaff($bot, $nick))) { return; }
112 if ($text =~ /^([-0-9a-zA-Z]+)/) {
113 main::putserv($bot, "PRIVMSG $nick :$text set to ".nextdns($text));
119 my ($bot, $nick, $host, $hand, $text) = @_;
120 if (! (main::isstaff($bot, $nick))) { return; }
121 foreach my $line (readip6s($hostnameif)) {
126 # Return list of ipv6 addresses from filename
129 my @lines = main::readarray($filename);
131 foreach my $line (@lines) {
132 if ($line =~ /^\s*inet6\s+(alias\s+)?([0-9a-f:]{4,})\s+[0-9]+\s*$/i) {
134 } elsif ($line =~ /^\s*([0-9a-f:]{4,})\s*$/i) {
141 # set rdns of $ip6 to $hostname given $subnet
142 # return true on success; false on failure
144 my ($ip6, $subnet, $hostname) = @_;
145 my $digits = ip6full($ip6);
147 my $reversed = reverse($digits);
148 my $origin = substr($reversed, 32-$subnet/4);
149 $origin = join('.', split(//, $origin)).".ip6.arpa";
150 my $name = substr($reversed, 0, 32-$subnet/4);
151 $name = join('.', split(//, $name));
152 # delete old PTR records, then set new one
153 return setdns($name, $origin) && setdns($name, $origin, "PTR", $hostname.".");
155 # delete rdns of $ip6 given $subnet
156 # return true on success; false on failure
158 my ($ip6, $subnet) = @_;
159 return setrdns($ip6, $subnet);
162 # given $origin. create $name RR of $type and set to $value if provided;
163 # if $value is missing, delete $domain
164 # returns true upon success, false upon failure
166 my ($name, $origin, $type, $value) = @_;
167 my $filename = "$zonedir/$origin";
168 my @lines = main::readarray($filename);
169 foreach my $line (@lines) {
170 # increment the zone's serial number
171 if ($line =~ /(\d{8})(\d{2})((\s+\d+){4}\s*\))/) {
172 my $date = main::date();
174 if ($date <= $1) { $serial = $2+1; }
175 $line = $`.$date.sprintf("%02d",$serial).$3.$';
178 if (!defined($value)) { # delete records
179 @lines = grep !/\b$name\s*3600\s*IN/, @lines;
181 push(@lines, "$name 3600 IN $type $value");
183 # trailing newline necessary
184 main::writefile("$filename.bak", join("\n", @lines)."\n");
185 copy "$filename.bak", $filename;
186 if (system("doas -u _nsd nsd-control reload")) {
193 # given hostname, return IP addresses; or given IP address, return hostname
197 my @lines = split /\n/m, `host $name`;
198 if ($name =~ /^[0-9\.]+$/ or $name =~ /:/) { # IP address
199 foreach my $line (@lines) {
200 if ($line =~ /([\d\.]+).(in-addr|ip6).arpa domain name pointer (.*)/) {
205 foreach my $line (@lines) {
206 if ($line =~ /$name has (IPv6 )?address ([0-9a-fA-F\.:]+)/) {
211 return join(' ', @matches);
214 # Return an ipv6 address with all zeroes filled in
217 my $left = substr($ip6, 0, index($ip6, "::"));
218 my $leftcolons = ($left =~ tr/://);
220 my @quartets = split(':', $ip6);
221 my $length = scalar(@quartets);
222 for (my $n = 1; $n <= 8 - $length; $n++) {
223 splice(@quartets, $leftcolons+1, 0, "0000");
225 my @newquartets = map(sprintf('%04s', $_), @quartets);
226 my $full = join(':',@newquartets);
229 # Returns the network part of the first IPv6 address (indicated by subnet)
230 # with the host part of the second IPv6 address
232 my ($ip6net, $subnet, $ip6host) = @_;
233 my $netdigits = ip6full($ip6net);
234 $netdigits =~ tr/://d;
235 my $hostdigits = ip6full($ip6host);
236 $hostdigits =~ tr/://d;
237 my $digits = substr($netdigits,0,($subnet/4)).substr($hostdigits,($subnet/4));
239 for (my $n = 0; $n < 32; $n++) {
240 if ($n > 0 && $n % 4 == 0) {
243 $ip6 .= substr($digits,$n,1);
248 return join ':', map { sprintf '%04x', rand 0x10000 } (1 .. 8);
251 # create A and AAAA records for subdomain, set the rDNS,
252 # and return the new ipv6 address
254 my ($subdomain) = @_;
256 my @allip6s = readip6s($hostnameif);
257 while (grep(/$newip6/, @allip6s)) {
258 $newip6 = ip6mask($ip6, $ip6subnet,randip6());
260 main::appendfile($hostnameif, "inet6 alias $newip6 48\n");
261 `doas ifconfig vio0 inet6 $newip6/48`;
262 if (setdns($subdomain, $hostname, "A", $ip4) && setdns($subdomain, $hostname, "AAAA", $newip6) && setrdns($newip6, $ip6subnet, "$subdomain.$hostname")) {
268 1; # MUST BE LAST STATEMENT IN FILE