Blob


1 #!/usr/bin/perl
3 package DNS;
5 use strict;
6 use warnings;
7 use OpenBSD::Pledge;
8 use OpenBSD::Unveil;
9 use File::Copy qw(copy);
11 my %conf = %main::conf;
12 my $chans = $conf{chans};
13 my $staff = $conf{staff};
14 my $key = $conf{key};
15 my $hash = $conf{hash};
16 my $hostname = $conf{hostname};
17 my $verbose = $conf{verbose};
18 my $ip4 = $conf{ip4};
19 my $ip6 = $conf{ip6};
20 my $ip6subnet = $conf{ip6subnet};
21 my $zonedir = $conf{zonedir};
22 my $hostnameif = $conf{hostnameif};
23 if (host($hostname) =~ /(\d+\.){3,}\d+/) {
24 $ip4 = $&;
25 }
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);
34 sub init {
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 $!";
39 }
41 # !setrdns 2001:bd8:: username.example.com
42 sub msetrdns {
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");
49 } else {
50 main::putserv($bot, "PRIVMSG $nick :ERROR: failed to set rDNS");
51 }
52 }
53 }
55 # !delrdns 2001:bd8::
56 sub mdelrdns {
57 my ($bot, $nick, $host, $hand, $text) = @_;
58 if (! (main::isstaff($bot, $nick))) { return; }
59 if ($text =~ /^([0-9A-Fa-f:\.]{3,})$/) {
60 my ($ip) = ($1);
61 if (delrdns($ip, $ip6subnet)) {
62 main::putserv($bot, "PRIVMSG $nick :$ip rDNS deleted");
63 } else {
64 main::putserv($bot, "PRIVMSG $nick :ERROR: failed to set rDNS");
65 }
66 }
67 }
68 # !setdns username 1.2.3.4
69 sub msetdns {
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");
78 } else {
79 main::putserv($bot, "PRIVMSG $nick :ERROR: failed to set DNS");
80 }
81 }
82 }
84 # !deldns username
85 sub mdeldns {
86 my ($bot, $nick, $host, $hand, $text) = @_;
87 if (! (main::isstaff($bot, $nick))) { return; }
88 if ($text =~ /^([-0-9A-Za-z\.]+)$/) {
89 my ($name) = ($1);
90 if (setdns($name, $hostname)) {
91 main::putserv($bot, "PRIVMSG $nick :$text deleted");
92 } else {
93 main::putserv($bot, "PRIVMSG $nick :ERROR: failed to delete DNS records");
94 }
95 }
96 }
98 # !host username
99 sub mhost {
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));
108 # !nextdns username
109 sub mnextdns {
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));
117 # !readip6s
118 sub mreadip6s {
119 my ($bot, $nick, $host, $hand, $text) = @_;
120 if (! (main::isstaff($bot, $nick))) { return; }
121 foreach my $line (readip6s($hostnameif)) {
122 print "$line\n"
126 # Return list of ipv6 addresses from filename
127 sub readip6s {
128 my ($filename) = @_;
129 my @lines = main::readarray($filename);
130 my @ipv6s;
131 foreach my $line (@lines) {
132 if ($line =~ /^\s*inet6\s+(alias\s+)?([0-9a-f:]{4,})\s+[0-9]+\s*$/i) {
133 push(@ipv6s, $2);
134 } elsif ($line =~ /^\s*([0-9a-f:]{4,})\s*$/i) {
135 push(@ipv6s, $1);
138 return @ipv6s;
141 # set rdns of $ip6 to $hostname given $subnet
142 # return true on success; false on failure
143 sub setrdns {
144 my ($ip6, $subnet, $hostname) = @_;
145 my $digits = ip6full($ip6);
146 $digits =~ tr/://d;
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
157 sub delrdns {
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
165 sub setdns {
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();
173 my $serial = 0;
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;
180 } else {
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")) {
187 return 0;
188 } else {
189 return 1;
193 # given hostname, return IP addresses; or given IP address, return hostname
194 sub host {
195 my ($name) = @_;
196 my @matches;
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 (.*)/) {
201 push(@matches, $3);
204 } else { # hostname
205 foreach my $line (@lines) {
206 if ($line =~ /$name has (IPv6 )?address ([0-9a-fA-F\.:]+)/) {
207 push(@matches, $2);
211 return join(' ', @matches);
214 # Return an ipv6 address with all zeroes filled in
215 sub ip6full {
216 my ($ip6) = @_;
217 my $left = substr($ip6, 0, index($ip6, "::"));
218 my $leftcolons = ($left =~ tr/://);
219 $ip6 =~ s{::}{:};
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);
227 return $full;
229 # Returns the network part of the first IPv6 address (indicated by subnet)
230 # with the host part of the second IPv6 address
231 sub ip6mask {
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));
238 my $ip6;
239 for (my $n = 0; $n < 32; $n++) {
240 if ($n > 0 && $n % 4 == 0) {
241 $ip6 .= ":";
243 $ip6 .= substr($digits,$n,1);
245 return $ip6;
247 sub randip6 {
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
253 sub nextdns {
254 my ($subdomain) = @_;
255 my $newip6 = $ip6;
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")) {
263 return "$newip6";
265 return "false";
268 1; # MUST BE LAST STATEMENT IN FILE