source: trunk/DNSDB.pm@ 324

Last change on this file since 324 was 324, checked in by Kris Deugau, 13 years ago

/trunk

Add consistency to log entry and record list sorts by adding
secondary sort on log_id or record_id respectively

  • Property svn:keywords set to Date Rev Author Id
File size: 140.8 KB
Line 
1# dns/trunk/DNSDB.pm
2# Abstraction functions for DNS administration
3##
4# $Id: DNSDB.pm 324 2012-05-02 17:23:13Z kdeugau $
5# Copyright 2008-2012 Kris Deugau <kdeugau@deepnet.cx>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19##
20
21package DNSDB;
22
23use strict;
24use warnings;
25use Exporter;
26use DBI;
27use Net::DNS;
28use Crypt::PasswdMD5;
29use Net::SMTP;
30use NetAddr::IP qw(:lower);
31use POSIX;
32use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
33
34$VERSION = 1.1; ##VERSION##
35@ISA = qw(Exporter);
36@EXPORT_OK = qw(
37 &initGlobals &login &initActionLog
38 &initPermissions &getPermissions &changePermissions &comparePermissions
39 &changeGroup
40 &loadConfig &connectDB &finish
41 &addDomain &delZone &domainName &revName &domainID &revID &addRDNS
42 &getZoneCount &getZoneList
43 &addGroup &delGroup &getChildren &groupName
44 &getGroupCount &getGroupList
45 &addUser &updateUser &delUser &userFullName &userStatus &getUserData
46 &getSOA &updateSOA &getRecLine &getDomRecs &getRecCount
47 &addRec &updateRec &delRec
48 &getLogCount &getLogEntries
49 &getTypelist
50 &parentID
51 &isParent
52 &zoneStatus &importAXFR
53 &export
54 &mailNotify
55 %typemap %reverse_typemap %config
56 %permissions @permtypes $permlist
57 );
58
59@EXPORT = (); # Export nothing by default.
60%EXPORT_TAGS = ( ALL => [qw(
61 &initGlobals &login &initActionLog
62 &initPermissions &getPermissions &changePermissions &comparePermissions
63 &changeGroup
64 &loadConfig &connectDB &finish
65 &addDomain &delZone &domainName &revName &domainID &revID &addRDNS
66 &getZoneCount &getZoneList
67 &addGroup &delGroup &getChildren &groupName
68 &getGroupCount &getGroupList
69 &addUser &updateUser &delUser &userFullName &userStatus &getUserData
70 &getSOA &updateSOA &getRecLine &getDomRecs &getRecCount
71 &addRec &updateRec &delRec
72 &getLogCount &getLogEntries
73 &getTypelist
74 &parentID
75 &isParent
76 &zoneStatus &importAXFR
77 &export
78 &mailNotify
79 %typemap %reverse_typemap %config
80 %permissions @permtypes $permlist
81 )]
82 );
83
84our $group = 1;
85our $errstr = '';
86our $resultstr = '';
87
88# Halfway sane defaults for SOA, TTL, etc.
89# serial defaults to 0 for convenience.
90# value will be either YYYYMMDDNN for BIND/etc, or auto-internal for tinydns
91our %def = qw (
92 contact hostmaster.DOMAIN
93 prins ns1.myserver.com
94 serial 0
95 soattl 86400
96 refresh 10800
97 retry 3600
98 expire 604800
99 minttl 10800
100 ttl 10800
101);
102
103# Arguably defined wholly in the db, but little reason to change without supporting code changes
104our @permtypes = qw (
105 group_edit group_create group_delete
106 user_edit user_create user_delete
107 domain_edit domain_create domain_delete
108 record_edit record_create record_delete
109 self_edit admin
110);
111our $permlist = join(',',@permtypes);
112
113# DNS record type map and reverse map.
114# loaded from the database, from http://www.iana.org/assignments/dns-parameters
115our %typemap;
116our %reverse_typemap;
117
118our %permissions;
119
120# Prepopulate a basic config. Note some of these *will* cause errors if left unset.
121# note: add appropriate stanzas in loadConfig to parse these
122our %config = (
123 # Database connection info
124 dbname => 'dnsdb',
125 dbuser => 'dnsdb',
126 dbpass => 'secret',
127 dbhost => '',
128
129 # Email notice settings
130 mailhost => 'smtp.example.com',
131 mailnotify => 'dnsdb@example.com', # to
132 mailsender => 'dnsdb@example.com', # from
133 mailname => 'DNS Administration',
134 orgname => 'Example Corp',
135 domain => 'example.com',
136
137 # Template directory
138 templatedir => 'templates/',
139# fmeh. this is a real web path, not a logical internal one. hm..
140# cssdir => 'templates/',
141 sessiondir => 'session/',
142
143 # Session params
144 timeout => '3600', # 1 hour default
145
146 # Other miscellanea
147 log_failures => 1, # log all evarthing by default
148 perpage => 15,
149 );
150
151## (Semi)private variables
152
153# Hash of functions for validating record types. Filled in initGlobals() since
154# it relies on visibility flags from the rectypes table in the DB
155my %validators;
156
157# Username, full name, ID - mainly for logging
158my %userdata;
159
160# Entity-relationship reference hashes.
161my %par_tbl = (
162 group => 'groups',
163 user => 'users',
164 defrec => 'default_records',
165 defrevrec => 'default_rev_records',
166 domain => 'domains',
167 revzone => 'revzones',
168 record => 'records'
169 );
170my %id_col = (
171 group => 'group_id',
172 user => 'user_id',
173 defrec => 'record_id',
174 defrevrec => 'record_id',
175 domain => 'domain_id',
176 revzone => 'rdns_id',
177 record => 'record_id'
178 );
179my %par_col = (
180 group => 'parent_group_id',
181 user => 'group_id',
182 defrec => 'group_id',
183 defrevrec => 'group_id',
184 domain => 'group_id',
185 revzone => 'group_id',
186 record => 'domain_id'
187 );
188my %par_type = (
189 group => 'group',
190 user => 'group',
191 defrec => 'group',
192 defrevrec => 'group',
193 domain => 'group',
194 revzone => 'group',
195 record => 'domain'
196 );
197
198##
199## utility functions
200##
201
202## DNSDB::_rectable()
203# Takes default+rdns flags, returns appropriate table name
204sub _rectable {
205 my $def = shift;
206 my $rev = shift;
207
208 return 'records' if $def ne 'y';
209 return 'default_records' if $rev ne 'y';
210 return 'default_rev_records';
211} # end _rectable()
212
213## DNSDB::_recparent()
214# Takes default+rdns flags, returns appropriate parent-id column name
215sub _recparent {
216 my $def = shift;
217 my $rev = shift;
218
219 return 'group_id' if $def eq 'y';
220 return 'rdns_id' if $rev eq 'y';
221 return 'domain_id';
222} # end _recparent()
223
224## DNSDB::_ipparent()
225# Check an IP to be added in a reverse zone to see if it's really in the requested parent.
226# Takes a database handle, default and reverse flags, IP (fragment) to check, parent zone ID,
227# and a reference to a NetAddr::IP object (also used to pass back a fully-reconstructed IP for
228# database insertion)
229sub _ipparent {
230 my $dbh = shift;
231 my $defrec = shift;
232 my $revrec = shift;
233 my $val = shift;
234 my $id = shift;
235 my $addr = shift;
236
237 return if $revrec ne 'y'; # this sub not useful in forward zones
238
239 $$addr = NetAddr::IP->new($$val); #necessary?
240
241 # subsub to split, reverse, and overlay an IP fragment on a netblock
242 sub __rev_overlay {
243 my $splitme = shift; # ':' or '.', m'lud?
244 my $parnet = shift;
245 my $val = shift;
246 my $addr = shift;
247
248 my $joinme = $splitme;
249 $splitme = '\.' if $splitme eq '.';
250 my @working = reverse(split($splitme, $parnet->addr));
251 my @parts = reverse(split($splitme, $$val));
252 for (my $i = 0; $i <= $#parts; $i++) {
253 $working[$i] = $parts[$i];
254 }
255 my $checkme = NetAddr::IP->new(join($joinme, reverse(@working))) or return 0;
256 return 0 unless $checkme->within($parnet);
257 $$addr = $checkme; # force "correct" IP to be recorded.
258 return 1;
259 }
260
261 my ($parstr) = $dbh->selectrow_array("SELECT revnet FROM revzones WHERE rdns_id = ?", undef, ($id));
262 my $parnet = NetAddr::IP->new($parstr);
263
264 # Fail early on v6-in-v4 or v4-in-v6. We're not accepting these ATM.
265 return 0 if $parnet->addr =~ /\./ && $$val =~ /:/;
266 return 0 if $parnet->addr =~ /:/ && $$val =~ /\./;
267
268 if ($$addr && $$val =~ /^[\da-fA-F][\da-fA-F:]+[\da-fA-F]$/) {
269 # the only case where NetAddr::IP's acceptance of legitimate IPs is "correct" is for a proper IPv6 address.
270 # the rest we have to restructure before fiddling. *sigh*
271 return 1 if $$addr->within($parnet);
272 } else {
273 # We don't have a complete IP in $$val (yet)
274 if ($parnet->addr =~ /:/) {
275 $$val =~ s/^:+//; # gotta strip'em all...
276 return __rev_overlay(':', $parnet, $val, $addr);
277 }
278 if ($parnet->addr =~ /\./) {
279 $$val =~ s/^\.+//;
280 return __rev_overlay('.', $parnet, $val, $addr);
281 }
282 # should be impossible to get here...
283 }
284 # ... and here.
285 # can't do nuttin' in forward zones
286} # end _ipparent()
287
288## DNSDB::_hostparent()
289# A little different than _ipparent above; this tries to *find* the parent zone of a hostname
290# Takes a database handle and hostname.
291# Returns the domain ID of the parent domain if one was found.
292sub _hostparent {
293 my $dbh = shift;
294 my $hname = shift;
295
296 my @hostbits = split /\./, $hname;
297 my $sth = $dbh->prepare("SELECT count(*),domain_id FROM domains WHERE domain = ? GROUP BY domain_id");
298 foreach (@hostbits) {
299 $sth->execute($hname);
300 my ($found, $parid) = $sth->fetchrow_array;
301 if ($found) {
302 return $parid;
303 }
304 $hname =~ s/^$_\.//;
305 }
306} # end _hostparent()
307
308## DNSDB::_log()
309# Log an action
310# Takes a database handle and log entry hash containing at least:
311# group_id, log entry
312# and optionally one or more of:
313# domain_id, rdns_id
314# The %userdata hash provides the user ID, username, and fullname
315sub _log {
316 my $dbh = shift;
317
318 my %args = @_;
319
320 $args{rdns_id} = 0 if !$args{rdns_id};
321 $args{domain_id} = 0 if !$args{domain_id};
322
323##fixme: farm out the actual logging to different subs for file, syslog, internal, etc based on config
324# if ($config{log_channel} eq 'sql') {
325 $dbh->do("INSERT INTO log (domain_id,rdns_id,group_id,entry,user_id,email,name) VALUES (?,?,?,?,?,?,?)",
326 undef,
327 ($args{domain_id}, $args{rdns_id}, $args{group_id}, $args{entry},
328 $userdata{userid}, $userdata{username}, $userdata{fullname}) );
329# } elsif ($config{log_channel} eq 'file') {
330# } elsif ($config{log_channel} eq 'syslog') {
331# }
332} # end _log
333
334
335##
336## Record validation subs.
337##
338
339## All of these subs take substantially the same arguments:
340# a database handle
341# a hash containing at least the following keys:
342# - defrec (default/live flag)
343# - revrec (forward/reverse flag)
344# - id (parent entity ID)
345# - host (hostname)
346# - rectype
347# - val (IP, hostname [CNAME/MX/SRV] or text)
348# - addr (NetAddr::IP object from val. May be undef.)
349# MX and SRV record validation also expect distance, and SRV records expect weight and port as well.
350# host, rectype, and addr should be references as these may be modified in validation
351
352# A record
353sub _validate_1 {
354 my $dbh = shift;
355
356 my %args = @_;
357
358 return ('FAIL', 'Reverse zones cannot contain A records') if $args{revrec} eq 'y';
359
360 # Coerce all hostnames to end in ".DOMAIN" for group/default records,
361 # or the intended parent domain for live records.
362 my $pname = ($args{defrec} eq 'y' ? 'DOMAIN' : domainName($dbh,$args{id}));
363 ${$args{host}} =~ s/\.*$/\.$pname/ if ${$args{host}} !~ /$pname$/;
364
365 # Check IP is well-formed, and that it's a v4 address
366 # Fail on "compact" IPv4 variants, because they are not consistent and predictable.
367 return ('FAIL',"$typemap{${$args{rectype}}} record must be a valid IPv4 address")
368 unless ${$args{val}} =~ /^\d+\.\d+\.\d+\.\d+$/;
369 return ('FAIL',"$typemap{${$args{rectype}}} record must be a valid IPv4 address")
370 unless $args{addr} && !$args{addr}->{isv6};
371 # coerce IP/value to normalized form for storage
372 ${$args{val}} = $args{addr}->addr;
373
374 return ('OK','OK');
375} # done A record
376
377# NS record
378sub _validate_2 {
379 my $dbh = shift;
380
381 my %args = @_;
382
383 # Coerce the hostname to "DOMAIN" for forward default records, "ZONE" for reverse default records,
384 # or the intended parent zone for live records.
385##fixme: allow for delegating <subdomain>.DOMAIN?
386 if ($args{revrec} eq 'y') {
387 my $pname = ($args{defrec} eq 'y' ? 'ZONE' : revName($dbh,$args{id}));
388 ${$args{host}} = $pname if ${$args{host}} ne $pname;
389 } else {
390 my $pname = ($args{defrec} eq 'y' ? 'DOMAIN' : domainName($dbh,$args{id}));
391 ${$args{host}} = $pname if ${$args{host}} ne $pname;
392 }
393
394# Let this lie for now. Needs more magic.
395# # Check IP is well-formed, and that it's a v4 address
396# return ('FAIL',"A record must be a valid IPv4 address")
397# unless $addr && !$addr->{isv6};
398# # coerce IP/value to normalized form for storage
399# $$val = $addr->addr;
400
401 return ('OK','OK');
402} # done NS record
403
404# CNAME record
405sub _validate_5 {
406 my $dbh = shift;
407
408 my %args = @_;
409
410# Not really true, but these are only useful for delegating smaller-than-/24 IP blocks.
411# This is fundamentally a messy operation and should really just be taken care of by the
412# export process, not manual maintenance of the necessary records.
413 return ('FAIL', 'Reverse zones cannot contain CNAME records') if $args{revrec} eq 'y';
414
415 # Coerce all hostnames to end in ".DOMAIN" for group/default records,
416 # or the intended parent domain for live records.
417 my $pname = ($args{defrec} eq 'y' ? 'DOMAIN' : domainName($dbh,$args{id}));
418 ${$args{host}} =~ s/\.*$/\.$pname/ if ${$args{host}} !~ /$pname$/;
419
420 return ('OK','OK');
421} # done CNAME record
422
423# SOA record
424sub _validate_6 {
425 # Smart monkeys won't stick their fingers in here; we have
426 # separate dedicated routines to deal with SOA records.
427 return ('OK','OK');
428} # done SOA record
429
430# PTR record
431sub _validate_12 {
432 my $dbh = shift;
433
434 my %args = @_;
435
436 if ($args{revrec} eq 'y') {
437 if ($args{defrec} eq 'n') {
438 return ('FAIL', "IP or IP fragment ${$args{val}} is not within ".revName($dbh, $args{id}))
439 unless _ipparent($dbh, $args{defrec}, $args{revrec}, $args{val}, $args{id}, \$args{addr});
440 ${$args{val}} = $args{addr}->addr;
441 } else {
442 if (${$args{val}} =~ /\./) {
443 # looks like a v4 or fragment
444 if (${$args{val}} =~ /^\d+\.\d+\.\d+\.\d+$/) {
445 # woo! a complete IP! validate it and normalize, or fail.
446 $args{addr} = NetAddr::IP->new(${$args{val}})
447 or return ('FAIL', "IP/value looks like IPv4 but isn't valid");
448 ${$args{val}} = $args{addr}->addr;
449 } else {
450 ${$args{val}} =~ s/^\.*/ZONE./ unless ${$args{val}} =~ /^ZONE/;
451 }
452 } elsif (${$args{val}} =~ /[a-f:]/) {
453 # looks like a v6 or fragment
454 ${$args{val}} =~ s/^:*/ZONE::/ if !$args{addr} && ${$args{val}} !~ /^ZONE/;
455 if ($args{addr}) {
456 if ($args{addr}->addr =~ /^0/) {
457 ${$args{val}} =~ s/^:*/ZONE::/ unless ${$args{val}} =~ /^ZONE/;
458 } else {
459 ${$args{val}} = $args{addr}->addr;
460 }
461 }
462 } else {
463 # bare number (probably). These could be v4 or v6, so we'll
464 # expand on these on creation of a reverse zone.
465 ${$args{val}} = "ZONE,${$args{val}}" unless ${$args{val}} =~ /^ZONE/;
466 }
467 ${$args{host}} =~ s/\.*$/\.$config{domain}/ if ${$args{host}} !~ /(?:$config{domain}|ADMINDOMAIN)$/;
468 }
469
470# Multiple PTR records do NOT generally do what most people believe they do,
471# and tend to fail in the most awkward way possible. Check and warn.
472# We use $val instead of $addr->addr since we may be in a defrec, and may have eg "ZONE::42" or "ZONE.12"
473
474 my @checkvals = (${$args{val}});
475 if (${$args{val}} =~ /,/) {
476 # push . and :: variants into checkvals if val has ,
477 my $tmp;
478 ($tmp = ${$args{val}}) =~ s/,/./;
479 push @checkvals, $tmp;
480 ($tmp = ${$args{val}}) =~ s/,/::/;
481 push @checkvals, $tmp;
482 }
483 my $pcsth = $dbh->prepare("SELECT count(*) FROM "._rectable($args{defrec},$args{revrec})." WHERE val = ?");
484 foreach my $checkme (@checkvals) {
485 if ($args{update}) {
486 # Record update. There should usually be an existing PTR (the record being updated)
487 my @ptrs = @{ $dbh->selectcol_arrayref("SELECT record_id FROM "._rectable($args{defrec},$args{revrec}).
488 " WHERE val = ?", undef, ($checkme)) };
489 return ('WARN', "PTR record for $checkme already exists; adding another will probably not do what you want")
490 if @ptrs && (!grep /^$args{update}$/, @ptrs);
491 } else {
492 # New record. Always warn if a PTR exists
493 my ($ptrcount) = $dbh->selectrow_array("SELECT count(*) FROM "._rectable($args{defrec},$args{revrec}).
494 " WHERE val = ?", undef, ($checkme));
495 return ('WARN', "PTR record for $checkme already exists; adding another will probably not do what you want")
496 if $ptrcount;
497 }
498 }
499
500 } else {
501 # Not absolutely true but only useful if you hack things up for sub-/24 v4 reverse delegations
502 # Simpler to just create the reverse zone and grant access for the customer to edit it, and create direct
503 # PTR records on export
504 return ('FAIL',"Forward zones cannot contain PTR records");
505 }
506
507 return ('OK','OK');
508} # done PTR record
509
510# MX record
511sub _validate_15 {
512 my $dbh = shift;
513
514 my %args = @_;
515
516# Not absolutely true but WTF use is an MX record for a reverse zone?
517 return ('FAIL', 'Reverse zones cannot contain MX records') if $args{revrec} eq 'y';
518
519 return ('FAIL', "Distance is required for MX records") unless defined(${$args{dist}});
520 ${$args{dist}} =~ s/\s*//g;
521 return ('FAIL',"Distance is required, and must be numeric") unless ${$args{dist}} =~ /^\d+$/;
522
523 ${$args{fields}} = "distance,";
524 push @{$args{vallist}}, ${$args{dist}};
525
526 # Coerce all hostnames to end in ".DOMAIN" for group/default records,
527 # or the intended parent domain for live records.
528 my $pname = ($args{defrec} eq 'y' ? 'DOMAIN' : domainName($dbh,$args{id}));
529 ${$args{host}} =~ s/\.*$/\.$pname/ if ${$args{host}} !~ /$pname$/;
530
531# hmm.. this might work. except possibly for something pointing to "deadbeef.ca". <g>
532# if ($type == $reverse_typemap{NS} || $type == $reverse_typemap{MX} || $type == $reverse_typemap{SRV}) {
533# if ($val =~ /^\s*[\da-f:.]+\s*$/) {
534# return ('FAIL',"$val is not a valid IP address") if !$addr;
535# }
536# }
537
538 return ('OK','OK');
539} # done MX record
540
541# TXT record
542sub _validate_16 {
543 # Could arguably put a WARN return here on very long (>512) records
544 return ('OK','OK');
545} # done TXT record
546
547# RP record
548sub _validate_17 {
549 # Probably have to validate these some day
550 return ('OK','OK');
551} # done RP record
552
553# AAAA record
554sub _validate_28 {
555 my $dbh = shift;
556
557 my %args = @_;
558
559 return ('FAIL', 'Reverse zones cannot contain AAAA records') if $args{revrec} eq 'y';
560
561 # Coerce all hostnames to end in ".DOMAIN" for group/default records,
562 # or the intended parent domain for live records.
563 my $pname = ($args{defrec} eq 'y' ? 'DOMAIN' : domainName($dbh,$args{id}));
564 ${$args{host}} =~ s/\.*$/\.$pname/ if ${$args{host}} !~ /$pname$/;
565
566 # Check IP is well-formed, and that it's a v6 address
567 return ('FAIL',"$typemap{${$args{rectype}}} record must be a valid IPv6 address")
568 unless $args{addr} && $args{addr}->{isv6};
569 # coerce IP/value to normalized form for storage
570 ${$args{val}} = $args{addr}->addr;
571
572 return ('OK','OK');
573} # done AAAA record
574
575# SRV record
576sub _validate_33 {
577 my $dbh = shift;
578
579 my %args = @_;
580
581# Not absolutely true but WTF use is an SRV record for a reverse zone?
582 return ('FAIL', 'Reverse zones cannot contain SRV records') if $args{revrec} eq 'y';
583
584 return ('FAIL', "Distance is required for SRV records") unless defined(${$args{dist}});
585 ${$args{dist}} =~ s/\s*//g;
586 return ('FAIL',"Distance is required, and must be numeric") unless ${$args{dist}} =~ /^\d+$/;
587
588 return ('FAIL',"SRV records must begin with _service._protocol [${$args{host}}]")
589 unless ${$args{host}} =~ /^_[A-Za-z]+\._[A-Za-z]+\.[a-zA-Z0-9-]+/;
590 return ('FAIL',"Port and weight are required for SRV records")
591 unless defined(${$args{weight}}) && defined(${$args{port}});
592 ${$args{weight}} =~ s/\s*//g;
593 ${$args{port}} =~ s/\s*//g;
594
595 return ('FAIL',"Port and weight are required, and must be numeric")
596 unless ${$args{weight}} =~ /^\d+$/ && ${$args{port}} =~ /^\d+$/;
597
598 ${$args{fields}} = "distance,weight,port,";
599 push @{$args{vallist}}, (${$args{dist}}, ${$args{weight}}, ${$args{port}});
600
601 # Coerce all hostnames to end in ".DOMAIN" for group/default records,
602 # or the intended parent domain for live records.
603 my $pname = ($args{defrec} eq 'y' ? 'DOMAIN' : domainName($dbh,$args{id}));
604 ${$args{host}} =~ s/\.*$/\.$pname/ if ${$args{host}} !~ /$pname$/;
605
606 return ('OK','OK');
607} # done SRV record
608
609# Now the custom types
610
611# A+PTR record. With a very little bit of magic we can also use this sub to validate AAAA+PTR. Whee!
612sub _validate_65280 {
613 my $dbh = shift;
614
615 my %args = @_;
616
617 my $code = 'OK';
618 my $msg = 'OK';
619
620 if ($args{defrec} eq 'n') {
621 # live record; revrec determines whether we validate the PTR or A component first.
622
623 if ($args{revrec} eq 'y') {
624 ($code,$msg) = _validate_12($dbh, %args);
625 return ($code,$msg) if $code eq 'FAIL';
626
627 # Check if the reqested domain exists. If not, coerce the type down to PTR and warn.
628 if (!(${$args{domid}} = _hostparent($dbh, ${$args{host}}))) {
629 my $addmsg = "Record ".($args{update} ? 'updated' : 'added').
630 " as PTR instead of $typemap{${$args{rectype}}}; domain not found for ${$args{host}}";
631 $msg .= "\n$addmsg" if $code eq 'WARN';
632 $msg = $addmsg if $code eq 'OK';
633 ${$args{rectype}} = $reverse_typemap{PTR};
634 return ('WARN', $msg);
635 }
636
637 # Add domain ID to field list and values
638 ${$args{fields}} .= "domain_id,";
639 push @{$args{vallist}}, ${$args{domid}};
640
641 } else {
642 ($code,$msg) = _validate_1($dbh, %args) if ${$args{rectype}} == 65280;
643 ($code,$msg) = _validate_28($dbh, %args) if ${$args{rectype}} == 65281;
644 return ($code,$msg) if $code eq 'FAIL';
645
646 # Check if the requested reverse zone exists - note, an IP fragment won't
647 # work here since we don't *know* which parent to put it in.
648 # ${$args{val}} has been validated as a valid IP by now, in one of the above calls.
649 my ($revid) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?".
650 " ORDER BY masklen(revnet) DESC", undef, (${$args{val}}));
651 if (!$revid) {
652 $msg = "Record ".($args{update} ? 'updated' : 'added')." as ".(${$args{rectype}} == 65280 ? 'A' : 'AAAA').
653 " instead of $typemap{${$args{rectype}}}; reverse zone not found for ${$args{val}}";
654 ${$args{rectype}} = (${$args{rectype}} == 65280 ? $reverse_typemap{A} : $reverse_typemap{AAAA});
655 return ('WARN', $msg);
656 }
657
658 # Check for duplicate PTRs. Note we don't have to play games with $code and $msg, because
659 # by definition there can't be duplicate PTRs if the reverse zone isn't managed here.
660 if ($args{update}) {
661 # Record update. There should usually be an existing PTR (the record being updated)
662 my @ptrs = @{ $dbh->selectcol_arrayref("SELECT record_id FROM "._rectable($args{defrec},$args{revrec}).
663 " WHERE val = ?", undef, (${$args{val}})) };
664 if (@ptrs && (!grep /^$args{update}$/, @ptrs)) {
665 $msg = "PTR record for ${$args{val}} already exists; adding another will probably not do what you want";
666 $code = 'WARN';
667 }
668 } else {
669 # New record. Always warn if a PTR exists
670 my ($ptrcount) = $dbh->selectrow_array("SELECT count(*) FROM "._rectable($args{defrec},$args{revrec}).
671 " WHERE val = ?", undef, (${$args{val}}));
672 $msg = "PTR record for ${$args{val}} already exists; adding another will probably not do what you want"
673 if $ptrcount;
674 $code = 'WARN' if $ptrcount;
675 }
676
677# my ($ptrcount) = $dbh->selectrow_array("SELECT count(*) FROM "._rectable($args{defrec},$args{revrec}).
678# " WHERE val = ?", undef, ${$args{val}});
679# if ($ptrcount) {
680# my $curid = $dbh->selectrow_array("SELECT record_id FROM "._rectable($args{defrec},$args{revrec}).
681# " WHERE val = ?
682# $msg = "PTR record for ${$args{val}} already exists; adding another will probably not do what you want";
683# $code = 'WARN';
684# }
685
686 ${$args{fields}} .= "rdns_id,";
687 push @{$args{vallist}}, $revid;
688 }
689
690 } else { # defrec eq 'y'
691 if ($args{revrec} eq 'y') {
692 ($code,$msg) = _validate_12($dbh, %args);
693 return ($code,$msg) if $code eq 'FAIL';
694 if (${$args{rectype}} == 65280) {
695 return ('FAIL',"A+PTR record must be a valid IPv4 address or fragment")
696 if ${$args{val}} =~ /:/;
697 ${$args{val}} =~ s/^ZONE,/ZONE./; # Clean up after uncertain IP-fragment-type from _validate_12
698 } elsif (${$args{rectype}} == 65281) {
699 return ('FAIL',"AAAA+PTR record must be a valid IPv6 address or fragment")
700 if ${$args{val}} =~ /\./;
701 ${$args{val}} =~ s/^ZONE,/ZONE::/; # Clean up after uncertain IP-fragment-type from _validate_12
702 }
703 } else {
704 # This is easy. I also can't see a real use-case for A/AAAA+PTR in *all* forward
705 # domains, since you wouldn't be able to substitute both domain and reverse zone
706 # sanely, and you'd end up with guaranteed over-replicated PTR records that would
707 # confuse the hell out of pretty much anything that uses them.
708##fixme: make this a config flag?
709 return ('FAIL', "$typemap{${$args{rectype}}} records not allowed in default domains");
710 }
711 }
712
713 return ($code, $msg);
714} # done A+PTR record
715
716# AAAA+PTR record
717# A+PTR above has been magicked to handle AAAA+PTR as well.
718sub _validate_65281 {
719 return _validate_65280(@_);
720} # done AAAA+PTR record
721
722# PTR template record
723sub _validate_65282 {
724 return ('OK','OK');
725} # done PTR template record
726
727# A+PTR template record
728sub _validate_65283 {
729 return ('OK','OK');
730} # done AAAA+PTR template record
731
732# AAAA+PTR template record
733sub _validate_65284 {
734 return ('OK','OK');
735} # done AAAA+PTR template record
736
737
738##
739## Record data substitution subs
740##
741
742# Replace ZONE in hostname, or create (most of) the actual proper zone name
743sub _ZONE {
744 my $zone = shift;
745 my $string = shift;
746 my $fr = shift || 'f'; # flag for forward/reverse order? nb: ignored for IP
747 my $sep = shift || '-'; # Separator character - unlikely we'll ever need more than . or -
748
749 my $prefix;
750
751 $string =~ s/,/./ if !$zone->{isv6};
752 $string =~ s/,/::/ if $zone->{isv6};
753
754 # Subbing ZONE in the host. We need to properly ID the netblock range
755 # The subbed text should have "network IP with trailing zeros stripped" for
756 # blocks lined up on octet (for v4) or hex-quad (for v6) boundaries
757 # For blocks that do NOT line up on these boundaries, we take the most
758 # significant octet or 16-bit chunk of the "broadcast" IP and append it
759 # after a double-dash
760 # ie:
761 # 8.0.0.0/6 -> 8.0.0.0 -> 11.255.255.255; sub should be 8--11
762 # 10.0.0.0/12 -> 10.0.0.0 -> 10.0.0.0 -> 10.15.255.255; sub should be 10-0--15
763 # 192.168.4.0/22 -> 192.168.4.0 -> 192.168.7.255; sub should be 192-168-4--7
764 # 192.168.0.8/29 -> 192.168.0.8 -> 192.168.0.15; sub should be 192-168-0-8--15
765 # Similar for v6
766
767 if (!$zone->{isv6}) { # IPv4
768
769 $prefix = $zone->network->addr; # Just In Case someone managed to slip in
770 # a funky subnet that had host bits set.
771 my $bc = $zone->broadcast->addr;
772
773 if ($zone->masklen > 24) {
774 $bc =~ s/^\d+\.\d+\.\d+\.//;
775 } elsif ($zone->masklen > 16) {
776 $prefix =~ s/\.0$//;
777 $bc =~ s/^\d+\.\d+\.//;
778 } elsif ($zone->masklen > 8) {
779 $bc =~ s/^\d+\.//;
780 $prefix =~ s/\.0\.0$//;
781 } else {
782 $prefix =~ s/\.0\.0\.0$//;
783 }
784 if ($zone->masklen % 8) {
785 $bc =~ s/(\.255)+$//;
786 $prefix .= "--$bc"; #"--".zone->masklen; # use range or mask length?
787 }
788 if ($fr eq 'f') {
789 $prefix =~ s/\.+/$sep/g;
790 } else {
791 $prefix = join($sep, reverse(split(/\./, $prefix)));
792 }
793
794 } else { # IPv6
795
796 if ($fr eq 'f') {
797
798 $prefix = $zone->network->addr; # Just In Case someone managed to slip in
799 # a funky subnet that had host bits set.
800 my $bc = $zone->broadcast->addr;
801 if (($zone->masklen % 16) != 0) {
802 # Strip trailing :0 off $prefix, and :ffff off the broadcast IP
803 for (my $i=0; $i<(7-int($zone->masklen / 16)); $i++) {
804 $prefix =~ s/:0$//;
805 $bc =~ s/:ffff$//;
806 }
807 # Strip the leading 16-bit chunks off the front of the broadcast IP
808 $bc =~ s/^([a-f0-9]+:)+//;
809 # Append the remaining 16-bit chunk to the prefix after "--"
810 $prefix .= "--$bc";
811 } else {
812 # Strip off :0 from the end until we reach the netblock length.
813 for (my $i=0; $i<(8-$zone->masklen / 16); $i++) {
814 $prefix =~ s/:0$//;
815 }
816 }
817 # Actually deal with the separator
818 $prefix =~ s/:/$sep/g;
819
820 } else { # $fr eq 'f'
821
822 $prefix = $zone->network->full; # Just In Case someone managed to slip in
823 # a funky subnet that had host bits set.
824 my $bc = $zone->broadcast->full;
825 $prefix =~ s/://g; # clean these out since they're not spaced right for this case
826 $bc =~ s/://g;
827 # Strip trailing 0 off $prefix, and f off the broadcast IP, to match the mask length
828 for (my $i=0; $i<(31-int($zone->masklen / 4)); $i++) {
829 $prefix =~ s/0$//;
830 $bc =~ s/f$//;
831 }
832 # Split and reverse the order of the nibbles in the network/broadcast IPs
833 $prefix =~ s/0$// if $zone->masklen % 4 == 0; # trim another 0 for nibble-aligned blocks first
834 my @nbits = reverse split //, $prefix;
835 my @bbits = reverse split //, $bc;
836 # Handle the sub-nibble case. Eww. I feel dirty supporting this...
837 $nbits[0] = "$nbits[0]-$bbits[0]" if ($zone->masklen % 4) != 0;
838 # Glue it back together
839 $prefix = join($sep, @nbits);
840
841 } # $fr ne 'f'
842
843 } # $zone->{isv6}
844
845 # Do the substitution, finally
846 $string =~ s/ZONE/$prefix/;
847 $string =~ s/--/-/ if $sep ne '-'; # - as separator needs extra help for sub-octet v4 netblocks
848 return $string;
849} # done _ZONE()
850
851# Not quite a substitution sub, but placed here as it's basically the inverse of above;
852# given the .arpa zone name, return the CIDR netblock the zone is for.
853# Supports v4 non-octet/non-classful netblocks as per the method outlined in the Grasshopper Book (2nd Ed p217-218)
854# Does NOT support non-quad v6 netblocks via the same scheme; it shouldn't ever be necessary.
855# Takes a nominal .arpa zone name, returns a success code and NetAddr::IP, or a fail code and message
856sub _zone2cidr {
857 my $zone = shift;
858
859 my $cidr;
860 my $tmpcidr;
861 my $warnmsg = '';
862
863 if ($zone =~ /\.in-addr\.arpa\.?$/) {
864 # v4 revzone, formal zone name type
865 my $tmpzone = $zone;
866 $tmpzone =~ s/\.in-addr\.arpa\.?//;
867 return ('FAIL', "Non-numerics in apparent IPv4 reverse zone name") if $tmpzone !~ /^(?:\d+-)?[\d\.]+$/;
868
869 # Snag the octet pieces
870 my @octs = split /\./, $tmpzone;
871
872 # Map result of a range manipulation to a mask length change. Cheaper than finding the 2-root of $octets[0]+1.
873 # Note we will not support /31 blocks, mostly due to issues telling "24-31" -> .24/29 apart from
874 # "24-31" -> .24/31", with a litte bit of "/31 is icky".
875 my %maskmap = ( 3 => 2, 7 => 3, 15 => 4, 31 => 5, 63 => 6, 127 => 7,
876 30 => 2, 29 => 3, 28 => 4, 27 => 5, 26 => 6, 25 => 7
877 );
878
879 # Handle "range" blocks, eg, 80-83.168.192.in-addr.arpa (192.168.80.0/22)
880 # Need to take the size of the range to offset the basic octet-based mask length,
881 # and make sure the first number in the range gets used as the network address for the block
882 # Alternate form: The second number is actually the real netmask, not the end of the range.
883 my $masklen = 0;
884 if ($octs[0] =~ /^((\d+)-(\d+))$/) { # take the range...
885 if (24 < $3 && $3 < 31) {
886 # we have a real netmask
887 $masklen = -$maskmap{$3};
888 } else {
889 # we have a range. NB: only real CIDR ranges are supported
890 $masklen -= $maskmap{-(eval $1)}; # find the mask base...
891 }
892 $octs[0] = $2; # set the base octet of the range...
893 }
894 @octs = reverse @octs; # We can reverse the octet pieces now that we've extracted and munged any ranges
895
896# arguably we should only allow sub-octet range/mask in-addr.arpa
897# specifications in the least significant octet, but the code is
898# simpler if we deal with sub-octet delegations at any level.
899
900 # Now we find the "true" mask with the aid of the "base" calculated above
901 if ($#octs == 0) {
902 $masklen += 8;
903 $tmpcidr = "$octs[0].0.0.0/$masklen"; # really hope we don't see one of these very often.
904 } elsif ($#octs == 1) {
905 $masklen += 16;
906 $tmpcidr = "$octs[0].$octs[1].0.0/$masklen";
907 } elsif ($#octs == 2) {
908 $masklen += 24;
909 $tmpcidr = "$octs[0].$octs[1].$octs[2].0/$masklen";
910 } else {
911 $masklen += 32;
912 $tmpcidr = "$octs[0].$octs[1].$octs[2].$octs[3]/$masklen";
913 }
914
915 } elsif ($zone =~ /\.ip6\.arpa$/) {
916 # v6 revzone, formal zone name type
917 my $tmpzone = $zone;
918 $tmpzone =~ s/\.ip6\.arpa\.?//;
919##fixme: if-n-when we decide we can support sub-nibble v6 zone names, we'll need to change this segment
920 return ('FAIL', "Non-hexadecimals in apparent IPv6 reverse zone name") if $tmpzone !~ /^[a-fA-F\d\.]+$/;
921 my @quads = reverse(split(/\./, $tmpzone));
922 $warnmsg .= "Apparent sub-/64 IPv6 reverse zone\n" if $#quads > 15;
923 my $nc;
924 foreach (@quads) {
925 $tmpcidr .= $_;
926 $tmpcidr .= ":" if ++$nc % 4 == 0;
927 }
928 my $nq = 1 if $nc % 4 != 0;
929 my $mask = $nc * 4; # need to do this here because we probably increment it below
930 while ($nc++ % 4 != 0) {
931 $tmpcidr .= "0";
932 }
933 $tmpcidr .= ($nq ? '::' : ':')."/$mask";
934 }
935
936 # Just to be sure, use NetAddr::IP to validate. Saves a lot of nasty regex watching for valid octet values.
937 return ('FAIL', "Invalid zone $zone (apparent netblock $tmpcidr)")
938 unless $cidr = NetAddr::IP->new($tmpcidr);
939
940 if ($warnmsg) {
941 $errstr = $warnmsg;
942 return ('WARN', $cidr);
943 }
944 return ('OK', $cidr);
945} # done _zone2cidr()
946
947
948##
949## Initialization and cleanup subs
950##
951
952
953## DNSDB::loadConfig()
954# Load the minimum required initial state (DB connect info) from a config file
955# Load misc other bits while we're at it.
956# Takes an optional basename and config path to look for
957# Populates the %config and %def hashes
958sub loadConfig {
959 my $basename = shift || ''; # this will work OK
960##fixme $basename isn't doing what I think I thought I was trying to do.
961
962 my $deferr = ''; # place to put error from default config file in case we can't find either one
963
964 my $configroot = "/etc/dnsdb"; ##CFG_LEAF##
965 $configroot = '' if $basename =~ m|^/|;
966 $basename .= ".conf" if $basename !~ /\.conf$/;
967 my $defconfig = "$configroot/dnsdb.conf";
968 my $siteconfig = "$configroot/$basename";
969
970 # System defaults
971 __cfgload("$defconfig") or $deferr = $errstr;
972
973 # Per-site-ish settings.
974 if ($basename ne '.conf') {
975 unless (__cfgload("$siteconfig")) {
976 $errstr = ($deferr ? "Error opening default config file $defconfig: $deferr\n" : '').
977 "Error opening site config file $siteconfig";
978 return;
979 }
980 }
981
982 # Munge log_failures.
983 if ($config{log_failures} ne '1' && $config{log_failures} ne '0') {
984 # true/false, on/off, yes/no all valid.
985 if ($config{log_failures} =~ /^(?:true|false|on|off|yes|no)$/) {
986 if ($config{log_failures} =~ /(?:true|on|yes)/) {
987 $config{log_failures} = 1;
988 } else {
989 $config{log_failures} = 0;
990 }
991 } else {
992 $errstr = "Bad log_failures setting $config{log_failures}";
993 $config{log_failures} = 1;
994 # Bad setting shouldn't be fatal.
995 # return 2;
996 }
997 }
998
999 # All good, clear the error and go home.
1000 $errstr = '';
1001 return 1;
1002} # end loadConfig()
1003
1004
1005## DNSDB::__cfgload()
1006# Private sub to parse a config file and load it into %config
1007# Takes a file handle on an open config file
1008sub __cfgload {
1009 $errstr = '';
1010 my $cfgfile = shift;
1011
1012 if (open CFG, "<$cfgfile") {
1013 while (<CFG>) {
1014 chomp;
1015 s/^\s*//;
1016 next if /^#/;
1017 next if /^$/;
1018# hmm. more complex bits in this file might require [heading] headers, maybe?
1019# $mode = $1 if /^\[(a-z)+]/;
1020 # DB connect info
1021 $config{dbname} = $1 if /^dbname\s*=\s*([a-z0-9_.-]+)/i;
1022 $config{dbuser} = $1 if /^dbuser\s*=\s*([a-z0-9_.-]+)/i;
1023 $config{dbpass} = $1 if /^dbpass\s*=\s*([a-z0-9_.-]+)/i;
1024 $config{dbhost} = $1 if /^dbhost\s*=\s*([a-z0-9_.-]+)/i;
1025 # SOA defaults
1026 $def{contact} = $1 if /^contact\s*=\s*([a-z0-9_.-]+)/i;
1027 $def{prins} = $1 if /^prins\s*=\s*([a-z0-9_.-]+)/i;
1028 $def{soattl} = $1 if /^soattl\s*=\s*(\d+)/i;
1029 $def{refresh} = $1 if /^refresh\s*=\s*(\d+)/i;
1030 $def{retry} = $1 if /^retry\s*=\s*(\d+)/i;
1031 $def{expire} = $1 if /^expire\s*=\s*(\d+)/i;
1032 $def{minttl} = $1 if /^minttl\s*=\s*(\d+)/i;
1033 $def{ttl} = $1 if /^ttl\s*=\s*(\d+)/i;
1034 # Mail settings
1035 $config{mailhost} = $1 if /^mailhost\s*=\s*([a-z0-9_.-]+)/i;
1036 $config{mailnotify} = $1 if /^mailnotify\s*=\s*([a-z0-9_.\@-]+)/i;
1037 $config{mailsender} = $1 if /^mailsender\s*=\s*([a-z0-9_.\@-]+)/i;
1038 $config{mailname} = $1 if /^mailname\s*=\s*([a-z0-9\s_.-]+)/i;
1039 $config{orgname} = $1 if /^orgname\s*=\s*([a-z0-9\s_.,'-]+)/i;
1040 $config{domain} = $1 if /^domain\s*=\s*([a-z0-9_.-]+)/i;
1041 # session - note this is fed directly to CGI::Session
1042 $config{timeout} = $1 if /^[tT][iI][mM][eE][oO][uU][tT]\s*=\s*(\d+[smhdwMy]?)/;
1043 $config{sessiondir} = $1 if m{^sessiondir\s*=\s*([a-z0-9/_.-]+)}i;
1044 # misc
1045 $config{log_failures} = $1 if /^log_failures\s*=\s*([a-z01]+)/i;
1046 $config{perpage} = $1 if /^perpage\s*=\s*(\d+)/i;
1047 }
1048 close CFG;
1049 } else {
1050 $errstr = $!;
1051 return;
1052 }
1053 return 1;
1054} # end __cfgload()
1055
1056
1057## DNSDB::connectDB()
1058# Creates connection to DNS database.
1059# Requires the database name, username, and password.
1060# Returns a handle to the db.
1061# Set up for a PostgreSQL db; could be any transactional DBMS with the
1062# right changes.
1063sub connectDB {
1064 $errstr = '';
1065 my $dbname = shift;
1066 my $user = shift;
1067 my $pass = shift;
1068 my $dbh;
1069 my $DSN = "DBI:Pg:dbname=$dbname";
1070
1071 my $host = shift;
1072 $DSN .= ";host=$host" if $host;
1073
1074# Note that we want to autocommit by default, and we will turn it off locally as necessary.
1075# We may not want to print gobbledygook errors; YMMV. Have to ponder that further.
1076 $dbh = DBI->connect($DSN, $user, $pass, {
1077 AutoCommit => 1,
1078 PrintError => 0
1079 })
1080 or return (undef, $DBI::errstr) if(!$dbh);
1081
1082##fixme: initialize the DB if we can't find the table (since, by definition, there's
1083# nothing there if we can't select from it...)
1084 my $tblsth = $dbh->prepare("SELECT count(*) FROM pg_catalog.pg_class WHERE relkind='r' AND relname=?");
1085 my ($tblcount) = $dbh->selectrow_array($tblsth, undef, ('misc'));
1086 return (undef,$DBI::errstr) if $dbh->err;
1087
1088#if ($tblcount == 0) {
1089# # create tables one at a time, checking for each.
1090# return (undef, "check table misc missing");
1091#}
1092
1093
1094# Return here if we can't select.
1095# This should retrieve the dbversion key.
1096 my $sth = $dbh->prepare("SELECT key,value FROM misc WHERE misc_id=1");
1097 $sth->execute();
1098 return (undef,$DBI::errstr) if ($sth->err);
1099
1100##fixme: do stuff to the DB on version mismatch
1101# x.y series should upgrade on $DNSDB::VERSION > misc(key=>version)
1102# DB should be downward-compatible; column defaults should give sane (if possibly
1103# useless-and-needs-help) values in columns an older software stack doesn't know about.
1104
1105# See if the select returned anything (or null data). This should
1106# succeed if the select executed, but...
1107 $sth->fetchrow();
1108 return (undef,$DBI::errstr) if ($sth->err);
1109
1110 $sth->finish;
1111
1112# If we get here, we should be OK.
1113 return ($dbh,"DB connection OK");
1114} # end connectDB
1115
1116
1117## DNSDB::finish()
1118# Cleans up after database handles and so on.
1119# Requires a database handle
1120sub finish {
1121 my $dbh = $_[0];
1122 $dbh->disconnect;
1123} # end finish
1124
1125
1126## DNSDB::initGlobals()
1127# Initialize global variables
1128# NB: this does NOT include web-specific session variables!
1129# Requires a database handle
1130sub initGlobals {
1131 my $dbh = shift;
1132
1133# load record types from database
1134 my $sth = $dbh->prepare("SELECT val,name,stdflag FROM rectypes");
1135 $sth->execute;
1136 while (my ($recval,$recname,$stdflag) = $sth->fetchrow_array()) {
1137 $typemap{$recval} = $recname;
1138 $reverse_typemap{$recname} = $recval;
1139 # now we fill the record validation function hash
1140 if ($stdflag < 5) {
1141 my $fn = "_validate_$recval";
1142 $validators{$recval} = \&$fn;
1143 } else {
1144 my $fn = "sub { return ('FAIL','Type $recval ($recname) not supported'); }";
1145 $validators{$recval} = eval $fn;
1146 }
1147 }
1148} # end initGlobals
1149
1150
1151## DNSDB::login()
1152# Takes a database handle, username and password
1153# Returns a userdata hash (UID, GID, username, fullname parts) if username exists,
1154# password matches the one on file, and account is not disabled
1155# Returns undef otherwise
1156sub login {
1157 my $dbh = shift;
1158 my $user = shift;
1159 my $pass = shift;
1160
1161 my $userinfo = $dbh->selectrow_hashref("SELECT user_id,group_id,password,firstname,lastname,status".
1162 " FROM users WHERE username=?",
1163 undef, ($user) );
1164 return if !$userinfo;
1165 return if !$userinfo->{status};
1166
1167 if ($userinfo->{password} =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
1168 # native passwords (crypt-md5)
1169 return if $userinfo->{password} ne unix_md5_crypt($pass,$1);
1170 } elsif ($userinfo->{password} =~ /^[0-9a-f]{32}$/) {
1171 # VegaDNS import (hex-coded MD5)
1172 return if $userinfo->{password} ne md5_hex($pass);
1173 } else {
1174 # plaintext (convenient now and then)
1175 return if $userinfo->{password} ne $pass;
1176 }
1177
1178 return $userinfo;
1179} # end login()
1180
1181
1182## DNSDB::initActionLog()
1183# Set up action logging. Takes a database handle and user ID
1184# Sets some internal globals and Does The Right Thing to set up a logging channel.
1185# This sets up _log() to spew out log entries to the defined channel without worrying
1186# about having to open a file or a syslog channel
1187##fixme Need to call _initActionLog_blah() for various logging channels, configured
1188# via dnsdb.conf, in $config{log_channel} or something
1189# See https://secure.deepnet.cx/trac/dnsadmin/ticket/21
1190sub initActionLog {
1191 my $dbh = shift;
1192 my $uid = shift;
1193
1194 return if !$uid;
1195
1196 # snag user info for logging. there's got to be a way to not have to pass this back
1197 # and forth from a caller, but web usage means no persistence we can rely on from
1198 # the server side.
1199 my ($username,$fullname) = $dbh->selectrow_array("SELECT username, firstname || ' ' || lastname".
1200 " FROM users WHERE user_id=?", undef, ($uid));
1201##fixme: errors are unpossible!
1202
1203 $userdata{username} = $username;
1204 $userdata{userid} = $uid;
1205 $userdata{fullname} = $fullname;
1206
1207 # convert to real check once we have other logging channels
1208 # if ($config{log_channel} eq 'sql') {
1209 # Open Log, Sez Me!
1210 # }
1211
1212} # end initActionLog
1213
1214
1215## DNSDB::initPermissions()
1216# Set up permissions global
1217# Takes database handle and UID
1218sub initPermissions {
1219 my $dbh = shift;
1220 my $uid = shift;
1221
1222# %permissions = $(getPermissions($dbh,'user',$uid));
1223 getPermissions($dbh, 'user', $uid, \%permissions);
1224
1225} # end initPermissions()
1226
1227
1228## DNSDB::getPermissions()
1229# Get permissions from DB
1230# Requires DB handle, group or user flag, ID, and hashref.
1231sub getPermissions {
1232 my $dbh = shift;
1233 my $type = shift;
1234 my $id = shift;
1235 my $hash = shift;
1236
1237 my $sql = qq(
1238 SELECT
1239 p.admin,p.self_edit,
1240 p.group_create,p.group_edit,p.group_delete,
1241 p.user_create,p.user_edit,p.user_delete,
1242 p.domain_create,p.domain_edit,p.domain_delete,
1243 p.record_create,p.record_edit,p.record_delete
1244 FROM permissions p
1245 );
1246 if ($type eq 'group') {
1247 $sql .= qq(
1248 JOIN groups g ON g.permission_id=p.permission_id
1249 WHERE g.group_id=?
1250 );
1251 } else {
1252 $sql .= qq(
1253 JOIN users u ON u.permission_id=p.permission_id
1254 WHERE u.user_id=?
1255 );
1256 }
1257
1258 my $sth = $dbh->prepare($sql);
1259
1260 $sth->execute($id) or die "argh: ".$sth->errstr;
1261
1262# my $permref = $sth->fetchrow_hashref;
1263# return $permref;
1264# $hash = $permref;
1265# Eww. Need to learn how to forcibly drop a hashref onto an existing hash.
1266 ($hash->{admin},$hash->{self_edit},
1267 $hash->{group_create},$hash->{group_edit},$hash->{group_delete},
1268 $hash->{user_create},$hash->{user_edit},$hash->{user_delete},
1269 $hash->{domain_create},$hash->{domain_edit},$hash->{domain_delete},
1270 $hash->{record_create},$hash->{record_edit},$hash->{record_delete})
1271 = $sth->fetchrow_array;
1272
1273} # end getPermissions()
1274
1275
1276## DNSDB::changePermissions()
1277# Update an ACL entry
1278# Takes a db handle, type, owner-id, and hashref for the changed permissions.
1279sub changePermissions {
1280 my $dbh = shift;
1281 my $type = shift;
1282 my $id = shift;
1283 my $newperms = shift;
1284 my $inherit = shift || 0;
1285
1286 my $resultmsg = '';
1287
1288 # see if we're switching from inherited to custom. for bonus points,
1289 # snag the permid and parent permid anyway, since we'll need the permid
1290 # to set/alter custom perms, and both if we're switching from custom to
1291 # inherited.
1292 my $sth = $dbh->prepare("SELECT (u.permission_id=g.permission_id) AS was_inherited,u.permission_id,g.permission_id,".
1293 ($type eq 'user' ? 'u.group_id,u.username' : 'u.parent_group_id,u.group_name').
1294 " FROM ".($type eq 'user' ? 'users' : 'groups')." u ".
1295 " JOIN groups g ON u.".($type eq 'user' ? '' : 'parent_')."group_id=g.group_id ".
1296 " WHERE u.".($type eq 'user' ? 'user' : 'group')."_id=?");
1297 $sth->execute($id);
1298
1299 my ($wasinherited,$permid,$parpermid,$parid,$name) = $sth->fetchrow_array;
1300
1301# hack phtoui
1302# group id 1 is "special" in that it's it's own parent (err... possibly.)
1303# may make its parent id 0 which doesn't exist, and as a bonus is Perl-false.
1304 $wasinherited = 0 if ($type eq 'group' && $id == 1);
1305
1306 local $dbh->{AutoCommit} = 0;
1307 local $dbh->{RaiseError} = 1;
1308
1309 # Wrap all the SQL in a transaction
1310 eval {
1311 if ($inherit) {
1312
1313 $dbh->do("UPDATE ".($type eq 'user' ? 'users' : 'groups')." SET inherit_perm='t',permission_id=? ".
1314 "WHERE ".($type eq 'user' ? 'user' : 'group')."_id=?", undef, ($parpermid, $id) );
1315 $dbh->do("DELETE FROM permissions WHERE permission_id=?", undef, ($permid) );
1316
1317 } else {
1318
1319 if ($wasinherited) { # munge new permission entry in if we're switching from inherited perms
1320##fixme: need to add semirecursive bit to properly munge inherited permission ID on subgroups and users
1321# ... if'n'when we have groups with fully inherited permissions.
1322 # SQL is coo
1323 $dbh->do("INSERT INTO permissions ($permlist,".($type eq 'user' ? 'user' : 'group')."_id) ".
1324 "SELECT $permlist,? FROM permissions WHERE permission_id=?", undef, ($id,$permid) );
1325 ($permid) = $dbh->selectrow_array("SELECT permission_id FROM permissions ".
1326 "WHERE ".($type eq 'user' ? 'user' : 'group')."_id=?", undef, ($id) );
1327 $dbh->do("UPDATE ".($type eq 'user' ? 'users' : 'groups')." SET inherit_perm='f',permission_id=? ".
1328 "WHERE ".($type eq 'user' ? 'user' : 'group')."_id=?", undef, ($permid, $id) );
1329 }
1330
1331 # and now set the permissions we were passed
1332 foreach (@permtypes) {
1333 if (defined ($newperms->{$_})) {
1334 $dbh->do("UPDATE permissions SET $_=? WHERE permission_id=?", undef, ($newperms->{$_},$permid) );
1335 }
1336 }
1337
1338 } # (inherited->)? custom
1339
1340 if ($type eq 'user') {
1341 $resultmsg = "Updated permissions for user $name";
1342 } else {
1343 $resultmsg = "Updated default permissions for group $name";
1344 }
1345 _log($dbh, (group_id => ($type eq 'user' ? $parid : $id), entry => $resultmsg));
1346 $dbh->commit;
1347 }; # end eval
1348 if ($@) {
1349 my $msg = $@;
1350 eval { $dbh->rollback; };
1351 return ('FAIL',"Error changing permissions: $msg");
1352 }
1353
1354 return ('OK',$resultmsg);
1355} # end changePermissions()
1356
1357
1358## DNSDB::comparePermissions()
1359# Compare two permission hashes
1360# Returns '>', '<', '=', '!'
1361sub comparePermissions {
1362 my $p1 = shift;
1363 my $p2 = shift;
1364
1365 my $retval = '='; # assume equality until proven otherwise
1366
1367 no warnings "uninitialized";
1368
1369 foreach (@permtypes) {
1370 next if $p1->{$_} == $p2->{$_}; # equal is good
1371 if ($p1->{$_} && !$p2->{$_}) {
1372 if ($retval eq '<') { # if we've already found an unequal pair where
1373 $retval = '!'; # $p2 has more access, and we now find a pair
1374 last; # where $p1 has more access, the overall access
1375 } # is neither greater or lesser, it's unequal.
1376 $retval = '>';
1377 }
1378 if (!$p1->{$_} && $p2->{$_}) {
1379 if ($retval eq '>') { # if we've already found an unequal pair where
1380 $retval = '!'; # $p1 has more access, and we now find a pair
1381 last; # where $p2 has more access, the overall access
1382 } # is neither greater or lesser, it's unequal.
1383 $retval = '<';
1384 }
1385 }
1386 return $retval;
1387} # end comparePermissions()
1388
1389
1390## DNSDB::changeGroup()
1391# Change group ID of an entity
1392# Takes a database handle, entity type, entity ID, and new group ID
1393sub changeGroup {
1394 my $dbh = shift;
1395 my $type = shift;
1396 my $id = shift;
1397 my $newgrp = shift;
1398
1399##fixme: fail on not enough args
1400 #return ('FAIL', "Missing
1401
1402 return ('FAIL', "Can't change the group of a $type")
1403 unless grep /^$type$/, ('domain','revzone','user','group'); # could be extended for defrecs?
1404
1405 # Collect some names for logging and messages
1406 my $entname;
1407 if ($type eq 'domain') {
1408 $entname = domainName($dbh, $id);
1409 } elsif ($type eq 'revzone') {
1410 $entname = revName($dbh, $id);
1411 } elsif ($type eq 'user') {
1412 $entname = userFullName($dbh, $id, '%u');
1413 } elsif ($type eq 'group') {
1414 $entname = groupName($dbh, $id);
1415 }
1416
1417 my ($oldgid) = $dbh->selectrow_array("SELECT group_id FROM $par_tbl{$type} WHERE $id_col{$type}=?",
1418 undef, ($id));
1419 my $oldgname = groupName($dbh, $oldgid);
1420 my $newgname = groupName($dbh, $newgrp);
1421
1422 return ('FAIL', "Can't move things into a group that doesn't exist") if !$newgname;
1423
1424 return ('WARN', "Nothing to do, new group is the same as the old group") if $oldgid == $newgrp;
1425
1426 # Allow transactions, and raise an exception on errors so we can catch it later.
1427 # Use local to make sure these get "reset" properly on exiting this block
1428 local $dbh->{AutoCommit} = 0;
1429 local $dbh->{RaiseError} = 1;
1430
1431 eval {
1432 $dbh->do("UPDATE $par_tbl{$type} SET group_id=? WHERE $id_col{$type}=?", undef, ($newgrp, $id));
1433 # Log the change in both the old and new groups
1434 _log($dbh, (group_id => $oldgid, entry => "Moved $type $entname from $oldgname to $newgname"));
1435 _log($dbh, (group_id => $newgrp, entry => "Moved $type $entname from $oldgname to $newgname"));
1436 $dbh->commit;
1437 };
1438 if ($@) {
1439 my $msg = $@;
1440 eval { $dbh->rollback; };
1441 if ($config{log_failures}) {
1442 _log($dbh, (group_id => $oldgid, entry => "Error moving $type $entname to $newgname: $msg"));
1443 $dbh->commit; # since we enabled transactions earlier
1444 }
1445 return ('FAIL',"Error moving $type $entname to $newgname: $msg");
1446 }
1447
1448 return ('OK',"Moved $type $entname from $oldgname to $newgname");
1449} # end changeGroup()
1450
1451
1452##
1453## Processing subs
1454##
1455
1456## DNSDB::addDomain()
1457# Add a domain
1458# Takes a database handle, domain name, numeric group, boolean(ish) state (active/inactive),
1459# and user info hash (for logging).
1460# Returns a status code and message
1461sub addDomain {
1462 $errstr = '';
1463 my $dbh = shift;
1464 return ('FAIL',"Need database handle") if !$dbh;
1465 my $domain = shift;
1466 return ('FAIL',"Domain must not be blank") if !$domain;
1467 my $group = shift;
1468 return ('FAIL',"Need group") if !defined($group);
1469 my $state = shift;
1470 return ('FAIL',"Need domain status") if !defined($state);
1471
1472 $state = 1 if $state =~ /^active$/;
1473 $state = 1 if $state =~ /^on$/;
1474 $state = 0 if $state =~ /^inactive$/;
1475 $state = 0 if $state =~ /^off$/;
1476
1477 return ('FAIL',"Invalid domain status") if $state !~ /^\d+$/;
1478
1479 return ('FAIL', "Invalid characters in domain") if $domain !~ /^[a-zA-Z0-9_.-]+$/;
1480
1481 my $sth = $dbh->prepare("SELECT domain_id FROM domains WHERE domain=?");
1482 my $dom_id;
1483
1484# quick check to start to see if we've already got one
1485 $sth->execute($domain);
1486 ($dom_id) = $sth->fetchrow_array;
1487
1488 return ('FAIL', "Domain already exists") if $dom_id;
1489
1490 # Allow transactions, and raise an exception on errors so we can catch it later.
1491 # Use local to make sure these get "reset" properly on exiting this block
1492 local $dbh->{AutoCommit} = 0;
1493 local $dbh->{RaiseError} = 1;
1494
1495 # Wrap all the SQL in a transaction
1496 eval {
1497 # insert the domain...
1498 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,?,?)", undef, ($domain, $group, $state));
1499
1500 # get the ID...
1501 ($dom_id) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE domain=?", undef, ($domain));
1502
1503 _log($dbh, (domain_id => $dom_id, group_id => $group,
1504 entry => "Added ".($state ? 'active' : 'inactive')." domain $domain"));
1505
1506 # ... and now we construct the standard records from the default set. NB: group should be variable.
1507 my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM default_records WHERE group_id=?");
1508 my $sth_in = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,distance,weight,port,ttl)".
1509 " VALUES ($dom_id,?,?,?,?,?,?,?)");
1510 $sth->execute($group);
1511 while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) {
1512 $host =~ s/DOMAIN/$domain/g;
1513 $val =~ s/DOMAIN/$domain/g;
1514 $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl);
1515 if ($typemap{$type} eq 'SOA') {
1516 my @tmp1 = split /:/, $host;
1517 my @tmp2 = split /:/, $val;
1518 _log($dbh, (domain_id => $dom_id, group_id => $group,
1519 entry => "[new $domain] Added SOA record [contact $tmp1[0]] [master $tmp1[1]] ".
1520 "[refresh $tmp2[0]] [retry $tmp2[1]] [expire $tmp2[2]] [minttl $tmp2[3]], TTL $ttl"));
1521 } else {
1522 my $logentry = "[new $domain] Added record '$host $typemap{$type}";
1523 $logentry .= " [distance $dist]" if $typemap{$type} eq 'MX';
1524 $logentry .= " [priority $dist] [weight $weight] [port $port]" if $typemap{$type} eq 'SRV';
1525 _log($dbh, (domain_id => $dom_id, group_id => $group,
1526 entry => $logentry." $val', TTL $ttl"));
1527 }
1528 }
1529
1530 # once we get here, we should have suceeded.
1531 $dbh->commit;
1532 }; # end eval
1533
1534 if ($@) {
1535 my $msg = $@;
1536 eval { $dbh->rollback; };
1537 _log($dbh, (group_id => $group, entry => "Failed adding domain $domain ($msg)"))
1538 if $config{log_failures};
1539 $dbh->commit; # since we enabled transactions earlier
1540 return ('FAIL',$msg);
1541 } else {
1542 return ('OK',$dom_id);
1543 }
1544} # end addDomain
1545
1546
1547## DNSDB::delZone()
1548# Delete a forward or reverse zone.
1549# Takes a database handle, zone ID, and forward/reverse flag.
1550# for now, just delete the records, then the domain.
1551# later we may want to archive it in some way instead (status code 2, for example?)
1552sub delZone {
1553 my $dbh = shift;
1554 my $zoneid = shift;
1555 my $revrec = shift;
1556
1557 # Allow transactions, and raise an exception on errors so we can catch it later.
1558 # Use local to make sure these get "reset" properly on exiting this block
1559 local $dbh->{AutoCommit} = 0;
1560 local $dbh->{RaiseError} = 1;
1561
1562 my $msg = '';
1563 my $failmsg = '';
1564 my $zone = ($revrec eq 'n' ? domainName($dbh, $zoneid) : revName($dbh, $zoneid));
1565
1566 # Set this up here since we may use if if $config{log_failures} is enabled
1567 my %loghash;
1568 $loghash{domain_id} = $zoneid if $revrec eq 'n';
1569 $loghash{rdns_id} = $zoneid if $revrec eq 'y';
1570 $loghash{group_id} = parentID($dbh,
1571 (id => $zoneid, type => ($revrec eq 'n' ? 'domain' : 'revzone'), revrec => $revrec) );
1572
1573 # Wrap all the SQL in a transaction
1574 eval {
1575 # Disentangle custom record types before removing the
1576 # ones that are only in the zone to be deleted
1577 if ($revrec eq 'n') {
1578 my $sth = $dbh->prepare("UPDATE records SET type=?,domain_id=0 WHERE domain_id=? AND type=?");
1579 $failmsg = "Failure converting multizone types to single-zone";
1580 $sth->execute($reverse_typemap{PTR}, $zoneid, 65280);
1581 $sth->execute($reverse_typemap{PTR}, $zoneid, 65281);
1582 $sth->execute(65282, $zoneid, 65283);
1583 $sth->execute(65282, $zoneid, 65284);
1584 $failmsg = "Failure removing domain records";
1585 $dbh->do("DELETE FROM records WHERE domain_id=?", undef, ($zoneid));
1586 $failmsg = "Failure removing domain";
1587 $dbh->do("DELETE FROM domains WHERE domain_id=?", undef, ($zoneid));
1588 } else {
1589 my $sth = $dbh->prepare("UPDATE records SET type=?,rdns_id=0 WHERE rdns_id=? AND type=?");
1590 $failmsg = "Failure converting multizone types to single-zone";
1591 $sth->execute($reverse_typemap{A}, $zoneid, 65280);
1592 $sth->execute($reverse_typemap{AAAA}, $zoneid, 65281);
1593# We don't have an "A template" or "AAAA template" type, although it might be useful for symmetry.
1594# $sth->execute(65285?, $zoneid, 65283);
1595# $sth->execute(65285?, $zoneid, 65284);
1596 $failmsg = "Failure removing reverse records";
1597 $dbh->do("DELETE FROM records WHERE rdns_id=?", undef, ($zoneid));
1598 $failmsg = "Failure removing reverse zone";
1599 $dbh->do("DELETE FROM revzones WHERE rdns_id=?", undef, ($zoneid));
1600 }
1601
1602 $msg = "Deleted ".($revrec eq 'n' ? 'domain' : 'reverse zone')." $zone";
1603 $loghash{entry} = $msg;
1604 _log($dbh, %loghash);
1605
1606 # once we get here, we should have suceeded.
1607 $dbh->commit;
1608 }; # end eval
1609
1610 if ($@) {
1611 $msg = $@;
1612 eval { $dbh->rollback; };
1613 $loghash{entry} = "Error deleting $zone: $msg ($failmsg)";
1614 if ($config{log_failures}) {
1615 _log($dbh, %loghash);
1616 $dbh->commit; # since we enabled transactions earlier
1617 }
1618 return ('FAIL', $loghash{entry});
1619 } else {
1620 return ('OK', $msg);
1621 }
1622
1623} # end delZone()
1624
1625
1626## DNSDB::domainName()
1627# Return the domain name based on a domain ID
1628# Takes a database handle and the domain ID
1629# Returns the domain name or undef on failure
1630sub domainName {
1631 $errstr = '';
1632 my $dbh = shift;
1633 my $domid = shift;
1634 my ($domname) = $dbh->selectrow_array("SELECT domain FROM domains WHERE domain_id=?", undef, ($domid) );
1635 $errstr = $DBI::errstr if !$domname;
1636 return $domname if $domname;
1637} # end domainName()
1638
1639
1640## DNSDB::revName()
1641# Return the reverse zone name based on an rDNS ID
1642# Takes a database handle and the rDNS ID
1643# Returns the reverse zone name or undef on failure
1644sub revName {
1645 $errstr = '';
1646 my $dbh = shift;
1647 my $revid = shift;
1648 my ($revname) = $dbh->selectrow_array("SELECT revnet FROM revzones WHERE rdns_id=?", undef, ($revid) );
1649 $errstr = $DBI::errstr if !$revname;
1650 return $revname if $revname;
1651} # end revName()
1652
1653
1654## DNSDB::domainID()
1655# Takes a database handle and domain name
1656# Returns the domain ID number
1657sub domainID {
1658 $errstr = '';
1659 my $dbh = shift;
1660 my $domain = shift;
1661 my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE domain=?", undef, ($domain) );
1662 $errstr = $DBI::errstr if !$domid;
1663 return $domid if $domid;
1664} # end domainID()
1665
1666
1667## DNSDB::revID()
1668# Takes a database handle and reverse zone name
1669# Returns the rDNS ID number
1670sub revID {
1671 $errstr = '';
1672 my $dbh = shift;
1673 my $revzone = shift;
1674 my ($revid) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet=?", undef, ($revzone) );
1675 $errstr = $DBI::errstr if !$revid;
1676 return $revid if $revid;
1677} # end revID()
1678
1679
1680## DNSDB::addRDNS
1681# Adds a reverse DNS zone
1682# Takes a database handle, CIDR block, reverse DNS pattern, numeric group,
1683# and boolean(ish) state (active/inactive)
1684# Returns a status code and message
1685sub addRDNS {
1686 my $dbh = shift;
1687 my $zone = NetAddr::IP->new(shift);
1688 return ('FAIL',"Zone name must be a valid CIDR netblock") unless ($zone && $zone->addr !~ /^0/);
1689 my $revpatt = shift; # construct a custom (A/AAAA+)? PTR template record
1690 my $group = shift;
1691 my $state = shift;
1692
1693 $state = 1 if $state =~ /^active$/;
1694 $state = 1 if $state =~ /^on$/;
1695 $state = 0 if $state =~ /^inactive$/;
1696 $state = 0 if $state =~ /^off$/;
1697
1698 return ('FAIL',"Invalid zone status") if $state !~ /^\d+$/;
1699
1700# quick check to start to see if we've already got one
1701 my ($rdns_id) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet=?", undef, ("$zone"));
1702
1703 return ('FAIL', "Zone already exists") if $rdns_id;
1704
1705 # Allow transactions, and raise an exception on errors so we can catch it later.
1706 # Use local to make sure these get "reset" properly on exiting this block
1707 local $dbh->{AutoCommit} = 0;
1708 local $dbh->{RaiseError} = 1;
1709
1710 my $warnstr = '';
1711 my $defttl = 3600; # 1 hour should be reasonable. And unless things have gone horribly
1712 # wrong, we should have a value to override this anyway.
1713
1714 # Wrap all the SQL in a transaction
1715 eval {
1716 # insert the domain...
1717 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,?,?)", undef, ($zone, $group, $state));
1718
1719 # get the ID...
1720 ($rdns_id) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
1721
1722 _log($dbh, (rdns_id => $rdns_id, group_id => $group,
1723 entry => "Added ".($state ? 'active' : 'inactive')." reverse zone $zone"));
1724
1725 # ... and now we construct the standard records from the default set. NB: group should be variable.
1726 my $sth = $dbh->prepare("SELECT host,type,val,ttl FROM default_rev_records WHERE group_id=?");
1727 my $sth_in = $dbh->prepare("INSERT INTO records (rdns_id,domain_id,host,type,val,ttl)".
1728 " VALUES ($rdns_id,?,?,?,?,?)");
1729 $sth->execute($group);
1730 while (my ($host,$type,$val,$ttl) = $sth->fetchrow_array()) {
1731 # Silently skip v4/v6 mismatches. This is not an error, this is expected.
1732 if ($zone->{isv6}) {
1733 next if ($type == 65280 || $type == 65283);
1734 } else {
1735 next if ($type == 65281 || $type == 65284);
1736 }
1737
1738 $host =~ s/ADMINDOMAIN/$config{domain}/g;
1739
1740 # Check to make sure the IP stubs will fit in the zone. Under most usage failures here should be rare.
1741 # On failure, tack a note on to a warning string and continue without adding this record.
1742 # While we're at it, we substitute $zone for ZONE in the value.
1743 if ($val eq 'ZONE') {
1744 next if $revpatt; # If we've got a pattern, we skip the default record version.
1745##fixme? do we care if we have multiple whole-zone templates?
1746 $val = $zone->network;
1747 } elsif ($val =~ /ZONE/) {
1748 my $tmpval = $val;
1749 $tmpval =~ s/ZONE//;
1750 # Bend the rules and allow single-trailing-number PTR or PTR template records to be inserted
1751 # as either v4 or v6. May make this an off-by-default config flag
1752 # Note that the origin records that may trigger this **SHOULD** already have ZONE,\d
1753 if ($type == 12 || $type == 65282) {
1754 $tmpval =~ s/[,.]/::/ if ($tmpval =~ /^[,.]\d+$/ && $zone->{isv6});
1755 $tmpval =~ s/[,:]+/./ if ($tmpval =~ /^(?:,|::)\d+$/ && !$zone->{isv6});
1756 }
1757 my $addr;
1758 if (_ipparent($dbh, 'n', 'y', \$tmpval, $rdns_id, \$addr)) {
1759 $val = $addr->addr;
1760 } else {
1761 $warnstr .= "\nDefault record '$val $typemap{$type} $host' doesn't fit in $zone, skipping";
1762 next;
1763 }
1764 }
1765
1766 # Substitute $zone for ZONE in the hostname.
1767 $host = _ZONE($zone, $host);
1768
1769 # Fill in the forward domain ID if we can find it, otherwise:
1770 # Coerce type down to PTR or PTR template if we can't
1771 my $domid = 0;
1772 if ($type >= 65280) {
1773 if (!($domid = _hostparent($dbh, $host))) {
1774 $warnstr .= "\nRecord added as PTR instead of $typemap{$type}; domain not found for $host";
1775 $type = $reverse_typemap{PTR};
1776 $domid = 0; # just to be explicit.
1777 }
1778 }
1779
1780 $sth_in->execute($domid,$host,$type,$val,$ttl);
1781
1782 if ($typemap{$type} eq 'SOA') {
1783 my @tmp1 = split /:/, $host;
1784 my @tmp2 = split /:/, $val;
1785 _log($dbh, (rdns_id => $rdns_id, group_id => $group,
1786 entry => "[new $zone] Added SOA record [contact $tmp1[0]] [master $tmp1[1]] ".
1787 "[refresh $tmp2[0]] [retry $tmp2[1]] [expire $tmp2[2]] [minttl $tmp2[3]], TTL $ttl"));
1788 $defttl = $tmp2[3];
1789 } else {
1790 my $logentry = "[new $zone] Added record '$host $typemap{$type}";
1791 _log($dbh, (rdns_id => $rdns_id, domain_id => $domid, group_id => $group,
1792 entry => $logentry." $val', TTL $ttl"));
1793 }
1794 }
1795
1796 # Generate record based on provided pattern.
1797 if ($revpatt) {
1798 my $host;
1799 my $type = ($zone->{isv6} ? 65284 : 65283);
1800 my $val = $zone->network;
1801
1802 # Substitute $zone for ZONE in the hostname.
1803 $host = _ZONE($zone, $revpatt);
1804
1805 my $domid = 0;
1806 if (!($domid = _hostparent($dbh, $host))) {
1807 $warnstr .= "\nDefault pattern added as PTR template instead of $typemap{$type}; domain not found for $host";
1808 $type = 65282;
1809 $domid = 0; # just to be explicit.
1810 }
1811
1812 $sth_in->execute($domid,$host,$type,$val,$defttl);
1813 my $logentry = "[new $zone] Added record '$host $typemap{$type}";
1814 _log($dbh, (rdns_id => $rdns_id, domain_id => $domid, group_id => $group,
1815 entry => $logentry." $val', TTL $defttl from pattern"));
1816 }
1817
1818 # If there are warnings (presumably about default records skipped for cause) log them
1819 _log($dbh, (rdns_id => $rdns_id, group_id => $group, entry => "Warning(s) adding $zone:$warnstr"))
1820 if $warnstr;
1821
1822 # once we get here, we should have suceeded.
1823 $dbh->commit;
1824 }; # end eval
1825
1826 if ($@) {
1827 my $msg = $@;
1828 eval { $dbh->rollback; };
1829 _log($dbh, (group_id => $group, entry => "Failed adding reverse zone $zone ($msg)"))
1830 if $config{log_failures};
1831 $dbh->commit; # since we enabled transactions earlier
1832 return ('FAIL',$msg);
1833 } else {
1834 my $retcode = 'OK';
1835 if ($warnstr) {
1836 $resultstr = $warnstr;
1837 $retcode = 'WARN';
1838 }
1839 return ($retcode, $rdns_id);
1840 }
1841
1842} # end addRDNS()
1843
1844
1845## DNSDB::getZoneCount
1846# Get count of zones in group or groups
1847# Takes a database handle and hash containing:
1848# - the "current" group
1849# - an array of "acceptable" groups
1850# - a flag for forward/reverse zones
1851# - Optionally accept a "starts with" and/or "contains" filter argument
1852# Returns an integer count of the resulting zone list.
1853sub getZoneCount {
1854 my $dbh = shift;
1855
1856 my %args = @_;
1857
1858 my @filterargs;
1859 $args{startwith} = undef if $args{startwith} && $args{startwith} !~ /^(?:[a-z]|0-9)$/;
1860 push @filterargs, "^$args{startwith}" if $args{startwith};
1861 $args{filter} =~ s/\./\[\.\]/g if $args{filter}; # only match literal dots, usually in reverse zones
1862 push @filterargs, $args{filter} if $args{filter};
1863
1864 my $sql;
1865 # Not as compact, and fix-me-twice if the common bits get wrong, but much easier to read
1866 if ($args{revrec} eq 'n') {
1867 $sql = "SELECT count(*) FROM domains".
1868 " WHERE group_id IN ($args{curgroup}".($args{childlist} ? ",$args{childlist}" : '').")".
1869 ($args{startwith} ? " AND domain ~* ?" : '').
1870 ($args{filter} ? " AND domain ~* ?" : '');
1871 } else {
1872 $sql = "SELECT count(*) FROM revzones".
1873 " WHERE group_id IN ($args{curgroup}".($args{childlist} ? ",$args{childlist}" : '').")".
1874 ($args{startwith} ? " AND CAST(revnet AS VARCHAR) ~* ?" : '').
1875 ($args{filter} ? " AND CAST(revnet AS VARCHAR) ~* ?" : '');
1876 }
1877 my ($count) = $dbh->selectrow_array($sql, undef, @filterargs);
1878 return $count;
1879} # end getZoneCount()
1880
1881
1882## DNSDB::getZoneList()
1883# Get a list of zones in the specified group(s)
1884# Takes the same arguments as getZoneCount() above
1885# Returns a reference to an array of hashrefs suitable for feeding to HTML::Template
1886sub getZoneList {
1887 my $dbh = shift;
1888
1889 my %args = @_;
1890
1891 my @zonelist;
1892
1893 $args{sortorder} = 'ASC' if !grep /^$args{sortorder}$/, ('ASC','DESC');
1894 $args{offset} = 0 if !$args{offset} || $args{offset} !~ /^(?:all|\d+)$/;
1895
1896 my @filterargs;
1897 $args{startwith} = undef if $args{startwith} && $args{startwith} !~ /^(?:[a-z]|0-9)$/;
1898 push @filterargs, "^$args{startwith}" if $args{startwith};
1899 $args{filter} =~ s/\./\[\.\]/g if $args{filter}; # only match literal dots, usually in reverse zones
1900 push @filterargs, $args{filter} if $args{filter};
1901
1902 my $sql;
1903 # Not as compact, and fix-me-twice if the common bits get wrong, but much easier to read
1904 if ($args{revrec} eq 'n') {
1905 $args{sortby} = 'domain' if !grep /^$args{sortby}$/, ('domain','group','status');
1906 $sql = "SELECT domain_id,domain,status,groups.group_name AS group FROM domains".
1907 " INNER JOIN groups ON domains.group_id=groups.group_id".
1908 " WHERE domains.group_id IN ($args{curgroup}".($args{childlist} ? ",$args{childlist}" : '').")".
1909 ($args{startwith} ? " AND domain ~* ?" : '').
1910 ($args{filter} ? " AND domain ~* ?" : '');
1911 } else {
1912##fixme: arguably startwith here is irrelevant. depends on the UI though.
1913 $args{sortby} = 'revnet' if !grep /^$args{sortby}$/, ('revnet','group','status');
1914 $sql = "SELECT rdns_id,revnet,status,groups.group_name AS group FROM revzones".
1915 " INNER JOIN groups ON revzones.group_id=groups.group_id".
1916 " WHERE revzones.group_id IN ($args{curgroup}".($args{childlist} ? ",$args{childlist}" : '').")".
1917 ($args{startwith} ? " AND CAST(revnet AS VARCHAR) ~* ?" : '').
1918 ($args{filter} ? " AND CAST(revnet AS VARCHAR) ~* ?" : '');
1919 }
1920 # A common tail.
1921 $sql .= " ORDER BY ".($args{sortby} eq 'group' ? 'groups.group_name' : $args{sortby})." $args{sortorder} ".
1922 ($args{offset} eq 'all' ? '' : " LIMIT $config{perpage}".
1923 " OFFSET ".$args{offset}*$config{perpage});
1924 my $sth = $dbh->prepare($sql);
1925 $sth->execute(@filterargs);
1926 my $rownum = 0;
1927
1928 while (my @data = $sth->fetchrow_array) {
1929 my %row;
1930 $row{domainid} = $data[0];
1931 $row{domain} = $data[1];
1932 $row{status} = $data[2];
1933 $row{group} = $data[3];
1934 push @zonelist, \%row;
1935 }
1936
1937 return \@zonelist;
1938} # end getZoneList()
1939
1940
1941## DNSDB::addGroup()
1942# Add a group
1943# Takes a database handle, group name, parent group, hashref for permissions,
1944# and optional template-vs-cloneme flag for the default records
1945# Returns a status code and message
1946sub addGroup {
1947 $errstr = '';
1948 my $dbh = shift;
1949 my $groupname = shift;
1950 my $pargroup = shift;
1951 my $permissions = shift;
1952
1953 # 0 indicates "custom", hardcoded.
1954 # Any other value clones that group's default records, if it exists.
1955 my $inherit = shift || 0;
1956##fixme: need a flag to indicate clone records or <?> ?
1957
1958 # Allow transactions, and raise an exception on errors so we can catch it later.
1959 # Use local to make sure these get "reset" properly on exiting this block
1960 local $dbh->{AutoCommit} = 0;
1961 local $dbh->{RaiseError} = 1;
1962
1963 my ($group_id) = $dbh->selectrow_array("SELECT group_id FROM groups WHERE group_name=?", undef, ($groupname));
1964
1965 return ('FAIL', "Group already exists") if $group_id;
1966
1967 # Wrap all the SQL in a transaction
1968 eval {
1969 $dbh->do("INSERT INTO groups (parent_group_id,group_name) VALUES (?,?)", undef, ($pargroup, $groupname) );
1970
1971 my ($groupid) = $dbh->selectrow_array("SELECT currval('groups_group_id_seq')");
1972
1973 # We work through the whole set of permissions instead of specifying them so
1974 # that when we add a new permission, we don't have to change the code anywhere
1975 # that doesn't explicitly deal with that specific permission.
1976 my @permvals;
1977 foreach (@permtypes) {
1978 if (!defined ($permissions->{$_})) {
1979 push @permvals, 0;
1980 } else {
1981 push @permvals, $permissions->{$_};
1982 }
1983 }
1984 $dbh->do("INSERT INTO permissions (group_id,$permlist) values (?".',?'x($#permtypes+1).")",
1985 undef, ($groupid, @permvals) );
1986 my ($permid) = $dbh->selectrow_array("SELECT currval('permissions_permission_id_seq')");
1987 $dbh->do("UPDATE groups SET permission_id=$permid WHERE group_id=$groupid");
1988
1989 # Default records
1990 my $sthf = $dbh->prepare("INSERT INTO default_records (group_id,host,type,val,distance,weight,port,ttl) ".
1991 "VALUES ($groupid,?,?,?,?,?,?,?)");
1992 my $sthr = $dbh->prepare("INSERT INTO default_rev_records (group_id,host,type,val,ttl) ".
1993 "VALUES ($groupid,?,?,?,?)");
1994 if ($inherit) {
1995 # Duplicate records from parent. Actually relying on inherited records feels
1996 # very fragile, and it would be problematic to roll over at a later time.
1997 my $sth2 = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM default_records WHERE group_id=?");
1998 $sth2->execute($pargroup);
1999 while (my @clonedata = $sth2->fetchrow_array) {
2000 $sthf->execute(@clonedata);
2001 }
2002 # And now the reverse records
2003 $sth2 = $dbh->prepare("SELECT host,type,val,ttl FROM default_rev_records WHERE group_id=?");
2004 $sth2->execute($pargroup);
2005 while (my @clonedata = $sth2->fetchrow_array) {
2006 $sthr->execute(@clonedata);
2007 }
2008 } else {
2009##fixme: Hardcoding is Bad, mmmmkaaaay?
2010 # reasonable basic defaults for SOA, MX, NS, and minimal hosting
2011 # could load from a config file, but somewhere along the line we need hardcoded bits.
2012 $sthf->execute('ns1.example.com:hostmaster.example.com', 6, '10800:3600:604800:10800', 0, 0, 0, 86400);
2013 $sthf->execute('DOMAIN', 1, '192.168.4.2', 0, 0, 0, 7200);
2014 $sthf->execute('DOMAIN', 15, 'mx.example.com', 10, 0, 0, 7200);
2015 $sthf->execute('DOMAIN', 2, 'ns1.example.com', 0, 0, 0, 7200);
2016 $sthf->execute('DOMAIN', 2, 'ns2.example.com', 0, 0, 0, 7200);
2017 $sthf->execute('www.DOMAIN', 5, 'DOMAIN', 0, 0, 0, 7200);
2018 # reasonable basic defaults for generic reverse zone. Same as initial SQL tabledef.
2019 $sthr->execute('hostmaster.ADMINDOMAIN:ns1.ADMINDOMAIN', 6, '10800:3600:604800:10800', 86400);
2020 $sthr->execute('unused-%r.ADMINDOMAIN', 65283, 'ZONE', 3600);
2021 }
2022
2023 _log($dbh, (group_id => $pargroup, entry => "Added group $groupname") );
2024
2025 # once we get here, we should have suceeded.
2026 $dbh->commit;
2027 }; # end eval
2028
2029 if ($@) {
2030 my $msg = $@;
2031 eval { $dbh->rollback; };
2032 if ($config{log_failures}) {
2033 _log($dbh, (group_id => $pargroup, entry => "Failed to add group $groupname: $msg") );
2034 $dbh->commit;
2035 }
2036 return ('FAIL',$msg);
2037 }
2038
2039 return ('OK','OK');
2040} # end addGroup()
2041
2042
2043## DNSDB::delGroup()
2044# Delete a group.
2045# Takes a group ID
2046# Returns a status code and message
2047sub delGroup {
2048 my $dbh = shift;
2049 my $groupid = shift;
2050
2051 # Allow transactions, and raise an exception on errors so we can catch it later.
2052 # Use local to make sure these get "reset" properly on exiting this block
2053 local $dbh->{AutoCommit} = 0;
2054 local $dbh->{RaiseError} = 1;
2055
2056##fixme: locate "knowable" error conditions and deal with them before the eval
2057# ... or inside, whatever.
2058# -> domains still exist in group
2059# -> ...
2060 my $failmsg = '';
2061 my $resultmsg = '';
2062
2063 # collect some pieces for logging and error messages
2064 my $groupname = groupName($dbh,$groupid);
2065 my $parid = parentID($dbh, (id => $groupid, type => 'group'));
2066
2067 # Wrap all the SQL in a transaction
2068 eval {
2069 # Check for Things in the group
2070 $failmsg = "Can't remove group $groupname";
2071 my ($grpcnt) = $dbh->selectrow_array("SELECT count(*) FROM groups WHERE parent_group_id=?", undef, ($groupid));
2072 die "$grpcnt groups still in group\n" if $grpcnt;
2073 my ($domcnt) = $dbh->selectrow_array("SELECT count(*) FROM domains WHERE group_id=?", undef, ($groupid));
2074 die "$domcnt domains still in group\n" if $domcnt;
2075 my ($usercnt) = $dbh->selectrow_array("SELECT count(*) FROM users WHERE group_id=?", undef, ($groupid));
2076 die "$usercnt users still in group\n" if $usercnt;
2077
2078 $failmsg = "Failed to delete default records for $groupname";
2079 $dbh->do("DELETE from default_records WHERE group_id=?", undef, ($groupid));
2080 $failmsg = "Failed to delete default reverse records for $groupname";
2081 $dbh->do("DELETE from default_rev_records WHERE group_id=?", undef, ($groupid));
2082 $failmsg = "Failed to remove group $groupname";
2083 $dbh->do("DELETE from groups WHERE group_id=?", undef, ($groupid));
2084
2085 _log($dbh, (group_id => $parid, entry => "Deleted group $groupname"));
2086 $resultmsg = "Deleted group $groupname";
2087
2088 # once we get here, we should have suceeded.
2089 $dbh->commit;
2090 }; # end eval
2091
2092 if ($@) {
2093 my $msg = $@;
2094 eval { $dbh->rollback; };
2095 if ($config{log_failures}) {
2096 _log($dbh, (group_id => $parid, entry => "$failmsg: $msg"));
2097 $dbh->commit; # since we enabled transactions earlier
2098 }
2099 return ('FAIL',"$failmsg: $msg");
2100 }
2101
2102 return ('OK',$resultmsg);
2103} # end delGroup()
2104
2105
2106## DNSDB::getChildren()
2107# Get a list of all groups whose parent^n is group <n>
2108# Takes a database handle, group ID, reference to an array to put the group IDs in,
2109# and an optional flag to return only immediate children or all children-of-children
2110# default to returning all children
2111# Calls itself
2112sub getChildren {
2113 $errstr = '';
2114 my $dbh = shift;
2115 my $rootgroup = shift;
2116 my $groupdest = shift;
2117 my $immed = shift || 'all';
2118
2119 # special break for default group; otherwise we get stuck.
2120 if ($rootgroup == 1) {
2121 # by definition, group 1 is the Root Of All Groups
2122 my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE NOT (group_id=1)".
2123 ($immed ne 'all' ? " AND parent_group_id=1" : ''));
2124 $sth->execute;
2125 while (my @this = $sth->fetchrow_array) {
2126 push @$groupdest, @this;
2127 }
2128 } else {
2129 my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE parent_group_id=?");
2130 $sth->execute($rootgroup);
2131 return if $sth->rows == 0;
2132 my @grouplist;
2133 while (my ($group) = $sth->fetchrow_array) {
2134 push @$groupdest, $group;
2135 getChildren($dbh,$group,$groupdest) if $immed eq 'all';
2136 }
2137 }
2138} # end getChildren()
2139
2140
2141## DNSDB::groupName()
2142# Return the group name based on a group ID
2143# Takes a database handle and the group ID
2144# Returns the group name or undef on failure
2145sub groupName {
2146 $errstr = '';
2147 my $dbh = shift;
2148 my $groupid = shift;
2149 my $sth = $dbh->prepare("SELECT group_name FROM groups WHERE group_id=?");
2150 $sth->execute($groupid);
2151 my ($groupname) = $sth->fetchrow_array();
2152 $errstr = $DBI::errstr if !$groupname;
2153 return $groupname if $groupname;
2154} # end groupName
2155
2156
2157## DNSDB::getGroupCount()
2158# Get count of subgroups in group or groups
2159# Takes a database handle and hash containing:
2160# - the "current" group
2161# - an array of "acceptable" groups
2162# - Optionally accept a "starts with" and/or "contains" filter argument
2163# Returns an integer count of the resulting group list.
2164sub getGroupCount {
2165 my $dbh = shift;
2166
2167 my %args = @_;
2168
2169 my @filterargs;
2170
2171 $args{startwith} = undef if $args{startwith} && $args{startwith} !~ /^(?:[a-z]|0-9)$/;
2172 push @filterargs, "^$args{startwith}" if $args{startwith};
2173 push @filterargs, $args{filter} if $args{filter};
2174
2175 my $sql = "SELECT count(*) FROM groups ".
2176 "WHERE parent_group_id IN ($args{curgroup}".($args{childlist} ? ",$args{childlist}" : '').")".
2177 ($args{startwith} ? " AND group_name ~* ?" : '').
2178 ($args{filter} ? " AND group_name ~* ?" : '');
2179 my ($count) = $dbh->selectrow_array($sql, undef, (@filterargs) );
2180 $errstr = $dbh->errstr if !$count;
2181 return $count;
2182} # end getGroupCount
2183
2184
2185## DNSDB::getGroupList()
2186# Get a list of sub^n-groups in the specified group(s)
2187# Takes the same arguments as getGroupCount() above
2188# Returns an arrayref containing hashrefs suitable for feeding straight to HTML::Template
2189sub getGroupList {
2190 my $dbh = shift;
2191
2192 my %args = @_;
2193
2194 my @filterargs;
2195
2196 $args{startwith} = undef if $args{startwith} && $args{startwith} !~ /^(?:[a-z]|0-9)$/;
2197 push @filterargs, "^$args{startwith}" if $args{startwith};
2198 push @filterargs, $args{filter} if $args{filter};
2199
2200 # protection against bad or missing arguments
2201 $args{sortorder} = 'ASC' if !$args{sortorder};
2202 $args{offset} = 0 if !$args{offset};
2203
2204 # munge sortby for columns in database
2205 $args{sortby} = 'g.group_name' if $args{sortby} eq 'group';
2206 $args{sortby} = 'g2.group_name' if $args{sortby} eq 'parent';
2207
2208 my $sql = q(SELECT g.group_id AS groupid, g.group_name AS groupname, g2.group_name AS pgroup,
2209 count(distinct(u.username)) AS nusers, count(distinct(d.domain)) AS ndomains,
2210 count(distinct(r.revnet)) AS nrevzones
2211 FROM groups g
2212 INNER JOIN groups g2 ON g2.group_id=g.parent_group_id
2213 LEFT OUTER JOIN users u ON u.group_id=g.group_id
2214 LEFT OUTER JOIN domains d ON d.group_id=g.group_id
2215 LEFT OUTER JOIN revzones r ON r.group_id=g.group_id
2216 ).
2217 "WHERE g.parent_group_id IN ($args{curgroup}".($args{childlist} ? ",$args{childlist}" : '').")".
2218 ($args{startwith} ? " AND g.group_name ~* ?" : '').
2219 ($args{filter} ? " AND g.group_name ~* ?" : '').
2220 " GROUP BY g.group_id, g.group_name, g2.group_name ".
2221 " ORDER BY $args{sortby} $args{sortorder} ".
2222 ($args{offset} eq 'all' ? '' : " LIMIT $config{perpage} OFFSET ".$args{offset}*$config{perpage});
2223 my $glist = $dbh->selectall_arrayref($sql, { Slice => {} }, (@filterargs) );
2224 $errstr = $dbh->errstr if !$glist;
2225 return $glist;
2226} # end getGroupList
2227
2228
2229## DNSDB::groupID()
2230# Return the group ID based on the group name
2231# Takes a database handle and the group name
2232# Returns the group ID or undef on failure
2233sub groupID {
2234 $errstr = '';
2235 my $dbh = shift;
2236 my $group = shift;
2237 my ($grpid) = $dbh->selectrow_array("SELECT group_id FROM groups WHERE group=?", undef, ($group) );
2238 $errstr = $DBI::errstr if !$grpid;
2239 return $grpid if $grpid;
2240} # end groupID()
2241
2242
2243## DNSDB::addUser()
2244# Add a user.
2245# Takes a DB handle, username, group ID, password, state (active/inactive).
2246# Optionally accepts:
2247# user type (user/admin) - defaults to user
2248# permissions string - defaults to inherit from group
2249# three valid forms:
2250# i - Inherit permissions
2251# c:<user_id> - Clone permissions from <user_id>
2252# C:<permission list> - Set these specific permissions
2253# first name - defaults to username
2254# last name - defaults to blank
2255# phone - defaults to blank (could put other data within column def)
2256# Returns (OK,<uid>) on success, (FAIL,<message>) on failure
2257sub addUser {
2258 $errstr = '';
2259 my $dbh = shift;
2260 my $username = shift;
2261 my $group = shift;
2262 my $pass = shift;
2263 my $state = shift;
2264
2265 return ('FAIL', "Missing one or more required entries") if !defined($state);
2266 return ('FAIL', "Username must not be blank") if !$username;
2267
2268 my $type = shift || 'u'; # create limited users by default - fwiw, not sure yet how this will interact with ACLs
2269
2270 my $permstring = shift || 'i'; # default is to inhert permissions from group
2271
2272 my $fname = shift || $username;
2273 my $lname = shift || '';
2274 my $phone = shift || ''; # not going format-check
2275
2276 my $sth = $dbh->prepare("SELECT user_id FROM users WHERE username=?");
2277 my $user_id;
2278
2279# quick check to start to see if we've already got one
2280 $sth->execute($username);
2281 ($user_id) = $sth->fetchrow_array;
2282
2283 return ('FAIL', "User already exists") if $user_id;
2284
2285 # Allow transactions, and raise an exception on errors so we can catch it later.
2286 # Use local to make sure these get "reset" properly on exiting this block
2287 local $dbh->{AutoCommit} = 0;
2288 local $dbh->{RaiseError} = 1;
2289
2290 # Wrap all the SQL in a transaction
2291 eval {
2292 # insert the user... note we set inherited perms by default since
2293 # it's simple and cleans up some other bits of state
2294 my $sth = $dbh->prepare("INSERT INTO users ".
2295 "(group_id,username,password,firstname,lastname,phone,type,status,permission_id,inherit_perm) ".
2296 "VALUES (?,?,?,?,?,?,?,?,(SELECT permission_id FROM permissions WHERE group_id=?),'t')");
2297 $sth->execute($group,$username,unix_md5_crypt($pass),$fname,$lname,$phone,$type,$state,$group);
2298
2299 # get the ID...
2300 ($user_id) = $dbh->selectrow_array("SELECT currval('users_user_id_seq')");
2301
2302# Permissions! Gotta set'em all!
2303 die "Invalid permission string $permstring"
2304 if $permstring !~ /^(?:
2305 i # inherit
2306 |c:\d+ # clone
2307 # custom. no, the leading , is not a typo
2308 |C:(?:,(?:group|user|domain|record|self)_(?:edit|create|delete))*
2309 )$/x;
2310# bleh. I'd call another function to do my dirty work, but we're in the middle of a transaction already.
2311 if ($permstring ne 'i') {
2312 # for cloned or custom permissions, we have to create a new permissions entry.
2313 my $clonesrc = $group;
2314 if ($permstring =~ /^c:(\d+)/) { $clonesrc = $1; }
2315 $dbh->do("INSERT INTO permissions ($permlist,user_id) ".
2316 "SELECT $permlist,? FROM permissions WHERE permission_id=".
2317 "(SELECT permission_id FROM permissions WHERE ".($permstring =~ /^c:/ ? 'user' : 'group')."_id=?)",
2318 undef, ($user_id,$clonesrc) );
2319 $dbh->do("UPDATE users SET permission_id=".
2320 "(SELECT permission_id FROM permissions WHERE user_id=?) ".
2321 "WHERE user_id=?", undef, ($user_id, $user_id) );
2322 }
2323 if ($permstring =~ /^C:/) {
2324 # finally for custom permissions, we set the passed-in permissions (and unset
2325 # any that might have been brought in by the clone operation above)
2326 my ($permid) = $dbh->selectrow_array("SELECT permission_id FROM permissions WHERE user_id=?",
2327 undef, ($user_id) );
2328 foreach (@permtypes) {
2329 if ($permstring =~ /,$_/) {
2330 $dbh->do("UPDATE permissions SET $_='t' WHERE permission_id=?", undef, ($permid) );
2331 } else {
2332 $dbh->do("UPDATE permissions SET $_='f' WHERE permission_id=?", undef, ($permid) );
2333 }
2334 }
2335 }
2336
2337 $dbh->do("UPDATE users SET inherit_perm='n' WHERE user_id=?", undef, ($user_id) );
2338
2339##fixme: add another table to hold name/email for log table?
2340
2341 _log($dbh, (group_id => $group, entry => "Added user $username ($fname $lname)"));
2342 # once we get here, we should have suceeded.
2343 $dbh->commit;
2344 }; # end eval
2345
2346 if ($@) {
2347 my $msg = $@;
2348 eval { $dbh->rollback; };
2349 if ($config{log_failures}) {
2350 _log($dbh, (group_id => $group, entry => "Error adding user $username: $msg"));
2351 $dbh->commit; # since we enabled transactions earlier
2352 }
2353 return ('FAIL',"Error adding user $username: $msg");
2354 }
2355
2356 return ('OK',"User $username ($fname $lname) added");
2357} # end addUser
2358
2359
2360## DNSDB::checkUser()
2361# Check user/pass combo on login
2362sub checkUser {
2363 my $dbh = shift;
2364 my $user = shift;
2365 my $inpass = shift;
2366
2367 my $sth = $dbh->prepare("SELECT user_id,group_id,password,firstname,lastname FROM users WHERE username=?");
2368 $sth->execute($user);
2369 my ($uid,$gid,$pass,$fname,$lname) = $sth->fetchrow_array;
2370 my $loginfailed = 1 if !defined($uid);
2371
2372 if ($pass =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
2373 $loginfailed = 1 if $pass ne unix_md5_crypt($inpass,$1);
2374 } else {
2375 $loginfailed = 1 if $pass ne $inpass;
2376 }
2377
2378 # nnnngggg
2379 return ($uid, $gid);
2380} # end checkUser
2381
2382
2383## DNSDB:: updateUser()
2384# Update general data about user
2385sub updateUser {
2386 my $dbh = shift;
2387
2388##fixme: tweak calling convention so that we can update any given bit of data
2389 my $uid = shift;
2390 my $username = shift;
2391 my $group = shift;
2392 my $pass = shift;
2393 my $state = shift;
2394 my $type = shift || 'u';
2395 my $fname = shift || $username;
2396 my $lname = shift || '';
2397 my $phone = shift || ''; # not going format-check
2398
2399 my $resultmsg = '';
2400
2401 # Allow transactions, and raise an exception on errors so we can catch it later.
2402 # Use local to make sure these get "reset" properly on exiting this block
2403 local $dbh->{AutoCommit} = 0;
2404 local $dbh->{RaiseError} = 1;
2405
2406 my $sth;
2407
2408 # Password can be left blank; if so we assume there's one on file.
2409 # Actual blank passwords are bad, mm'kay?
2410 if (!$pass) {
2411 ($pass) = $dbh->selectrow_array("SELECT password FROM users WHERE user_id=?", undef, ($uid));
2412 } else {
2413 $pass = unix_md5_crypt($pass);
2414 }
2415
2416 eval {
2417 $dbh->do("UPDATE users SET username=?, password=?, firstname=?, lastname=?, phone=?, type=?, status=?".
2418 " WHERE user_id=?", undef, ($username, $pass, $fname, $lname, $phone, $type, $state, $uid));
2419 $resultmsg = "Updated user info for $username ($fname $lname)";
2420 _log($dbh, group_id => $group, entry => $resultmsg);
2421 $dbh->commit;
2422 };
2423 if ($@) {
2424 my $msg = $@;
2425 eval { $dbh->rollback; };
2426 if ($config{log_failures}) {
2427 _log($dbh, (group_id => $group, entry => "Error updating user $username: $msg"));
2428 $dbh->commit; # since we enabled transactions earlier
2429 }
2430 return ('FAIL',"Error updating user $username: $msg");
2431 }
2432
2433 return ('OK',$resultmsg);
2434} # end updateUser()
2435
2436
2437## DNSDB::delUser()
2438# Delete a user.
2439# Takes a database handle and user ID
2440# Returns a success/failure code and matching message
2441sub delUser {
2442 my $dbh = shift;
2443 my $userid = shift;
2444
2445 return ('FAIL',"Bad userid") if !defined($userid);
2446
2447 my $userdata = getUserData($dbh, $userid);
2448
2449 # Allow transactions, and raise an exception on errors so we can catch it later.
2450 # Use local to make sure these get "reset" properly on exiting this block
2451 local $dbh->{AutoCommit} = 0;
2452 local $dbh->{RaiseError} = 1;
2453
2454 eval {
2455 $dbh->do("DELETE FROM users WHERE user_id=?", undef, ($userid));
2456 _log($dbh, (group_id => $userdata->{group_id},
2457 entry => "Deleted user ID $userid/".$userdata->{username}.
2458 " (".$userdata->{firstname}." ".$userdata->{lastname}.")") );
2459 $dbh->commit;
2460 };
2461 if ($@) {
2462 my $msg = $@;
2463 eval { $dbh->rollback; };
2464 if ($config{log_failures}) {
2465 _log($dbh, (group_id => $userdata->{group_id}, entry => "Error deleting user ID ".
2466 "$userid/".$userdata->{username}.": $msg") );
2467 $dbh->commit;
2468 }
2469 return ('FAIL',"Error deleting user $userid/".$userdata->{username}.": $msg");
2470 }
2471
2472 return ('OK',"Deleted user ".$userdata->{username}." (".$userdata->{firstname}." ".$userdata->{lastname}.")");
2473} # end delUser
2474
2475
2476## DNSDB::userFullName()
2477# Return a pretty string!
2478# Takes a user_id and optional printf-ish string to indicate which pieces where:
2479# %u for the username
2480# %f for the first name
2481# %l for the last name
2482# All other text in the passed string will be left as-is.
2483##fixme: need a "smart" option too, so that missing/null/blank first/last names don't give funky output
2484sub userFullName {
2485 $errstr = '';
2486 my $dbh = shift;
2487 my $userid = shift;
2488 my $fullformat = shift || '%f %l (%u)';
2489 my $sth = $dbh->prepare("select username,firstname,lastname from users where user_id=?");
2490 $sth->execute($userid);
2491 my ($uname,$fname,$lname) = $sth->fetchrow_array();
2492 $errstr = $DBI::errstr if !$uname;
2493
2494 $fullformat =~ s/\%u/$uname/g;
2495 $fullformat =~ s/\%f/$fname/g;
2496 $fullformat =~ s/\%l/$lname/g;
2497
2498 return $fullformat;
2499} # end userFullName
2500
2501
2502## DNSDB::userStatus()
2503# Sets and/or returns a user's status
2504# Takes a database handle, user ID and optionally a status argument
2505# Returns undef on errors.
2506sub userStatus {
2507 my $dbh = shift;
2508 my $id = shift;
2509 my $newstatus = shift || 'mu';
2510
2511 return undef if $id !~ /^\d+$/;
2512
2513 my $userdata = getUserData($dbh, $id);
2514
2515 # Allow transactions, and raise an exception on errors so we can catch it later.
2516 # Use local to make sure these get "reset" properly on exiting this block
2517 local $dbh->{AutoCommit} = 0;
2518 local $dbh->{RaiseError} = 1;
2519
2520 if ($newstatus ne 'mu') {
2521 # ooo, fun! let's see what we were passed for status
2522 eval {
2523 $newstatus = 0 if $newstatus eq 'useroff';
2524 $newstatus = 1 if $newstatus eq 'useron';
2525 $dbh->do("UPDATE users SET status=? WHERE user_id=?", undef, ($newstatus, $id));
2526
2527 $resultstr = ($newstatus ? 'Enabled' : 'Disabled')." user ".$userdata->{username}.
2528 " (".$userdata->{firstname}." ".$userdata->{lastname}.")";
2529
2530 my %loghash;
2531 $loghash{group_id} = parentID($dbh, (id => $id, type => 'user'));
2532 $loghash{entry} = $resultstr;
2533 _log($dbh, %loghash);
2534
2535 $dbh->commit;
2536 };
2537 if ($@) {
2538 my $msg = $@;
2539 eval { $dbh->rollback; };
2540 $resultstr = '';
2541 $errstr = $msg;
2542##fixme: failure logging?
2543 return;
2544 }
2545 }
2546
2547 my ($status) = $dbh->selectrow_array("SELECT status FROM users WHERE user_id=?", undef, ($id));
2548 return $status;
2549} # end userStatus()
2550
2551
2552## DNSDB::getUserData()
2553# Get misc user data for display
2554sub getUserData {
2555 my $dbh = shift;
2556 my $uid = shift;
2557
2558 my $sth = $dbh->prepare("SELECT group_id,username,firstname,lastname,phone,type,status,inherit_perm ".
2559 "FROM users WHERE user_id=?");
2560 $sth->execute($uid);
2561 return $sth->fetchrow_hashref();
2562
2563} # end getUserData()
2564
2565
2566## DNSDB::getSOA()
2567# Return all suitable fields from an SOA record in separate elements of a hash
2568# Takes a database handle, default/live flag, domain/reverse flag, and parent ID
2569sub getSOA {
2570 $errstr = '';
2571 my $dbh = shift;
2572 my $def = shift;
2573 my $rev = shift;
2574 my $id = shift;
2575
2576 # (ab)use distance and weight columns to store SOA data? can't for default_rev_records...
2577 # - should really attach serial to the zone parent somewhere
2578
2579 my $sql = "SELECT record_id,host,val,ttl from "._rectable($def,$rev).
2580 " WHERE "._recparent($def,$rev)." = ? AND type=$reverse_typemap{SOA}";
2581 my $ret = $dbh->selectrow_hashref($sql, undef, ($id) );
2582 return if !$ret;
2583##fixme: stick a flag somewhere if the record doesn't exist. by the API, this is an impossible case, but...
2584
2585 ($ret->{contact},$ret->{prins}) = split /:/, $ret->{host};
2586 delete $ret->{host};
2587 ($ret->{refresh},$ret->{retry},$ret->{expire},$ret->{minttl}) = split /:/, $ret->{val};
2588 delete $ret->{val};
2589
2590 return $ret;
2591} # end getSOA()
2592
2593
2594## DNSDB::updateSOA()
2595# Update the specified SOA record
2596# Takes a database handle, default/live flag, forward/reverse flag, and SOA data hash
2597# Returns a two-element list with a result code and message
2598sub updateSOA {
2599 my $dbh = shift;
2600 my $defrec = shift;
2601 my $revrec = shift;
2602
2603 my %soa = @_;
2604
2605 my $oldsoa = getSOA($dbh, $defrec, $revrec, $soa{id});
2606
2607 my $msg;
2608 my %logdata;
2609 if ($defrec eq 'n') {
2610 $logdata{domain_id} = $soa{id} if $revrec eq 'n';
2611 $logdata{rdns_id} = $soa{id} if $revrec eq 'y';
2612 $logdata{group_id} = parentID($dbh, (id => $soa{id}, revrec => $revrec,
2613 type => ($revrec eq 'n' ? 'domain' : 'revzone') ) );
2614 } else {
2615 $logdata{group_id} = $soa{id};
2616 }
2617 my $parname = ($defrec eq 'y' ? groupName($dbh, $soa{id}) :
2618 ($revrec eq 'n' ? domainName($dbh, $soa{id}) : revName($dbh, $soa{id})) );
2619
2620 # Allow transactions, and raise an exception on errors so we can catch it later.
2621 # Use local to make sure these get "reset" properly on exiting this block
2622 local $dbh->{AutoCommit} = 0;
2623 local $dbh->{RaiseError} = 1;
2624
2625 eval {
2626 my $sql = "UPDATE "._rectable($defrec, $revrec)." SET host=?, val=?, ttl=? WHERE record_id=? AND type=6";
2627 $dbh->do($sql, undef, ("$soa{contact}:$soa{prins}", "$soa{refresh}:$soa{retry}:$soa{expire}:$soa{minttl}",
2628 $soa{ttl}, $oldsoa->{record_id}) );
2629 $msg = "Updated ".($defrec eq 'y' ? ($revrec eq 'y' ? 'default reverse ' : 'default ') : '').
2630 "SOA for $parname: ".
2631 "(ns $oldsoa->{prins}, contact $oldsoa->{contact}, refresh $oldsoa->{refresh},".
2632 " retry $oldsoa->{retry}, expire $oldsoa->{expire}, minTTL $oldsoa->{minttl}, TTL $oldsoa->{ttl}) to ".
2633 "(ns $soa{prins}, contact $soa{contact}, refresh $soa{refresh},".
2634 " retry $soa{retry}, expire $soa{expire}, minTTL $soa{minttl}, TTL $soa{ttl})";
2635
2636 $logdata{entry} = $msg;
2637 _log($dbh, %logdata);
2638
2639 $dbh->commit;
2640 };
2641 if ($@) {
2642 $msg = $@;
2643 eval { $dbh->rollback; };
2644 $logdata{entry} = "Error updating ".($defrec eq 'y' ? ($revrec eq 'y' ? 'default reverse zone ' : 'default ') : '').
2645 "SOA record for $parname: $msg";
2646 if ($config{log_failures}) {
2647 _log($dbh, %logdata);
2648 $dbh->commit;
2649 }
2650 return ('FAIL', $logdata{entry});
2651 } else {
2652 return ('OK', $msg);
2653 }
2654} # end updateSOA()
2655
2656
2657## DNSDB::getRecLine()
2658# Return all data fields for a zone record in separate elements of a hash
2659# Takes a database handle, default/live flag, forward/reverse flag, and record ID
2660sub getRecLine {
2661 $errstr = '';
2662 my $dbh = shift;
2663 my $defrec = shift;
2664 my $revrec = shift;
2665 my $id = shift;
2666
2667 my $sql = "SELECT record_id,host,type,val,ttl".($revrec eq 'n' ? ',distance,weight,port' : '').
2668 (($defrec eq 'y') ? ',group_id FROM ' : ',domain_id,rdns_id FROM ').
2669 _rectable($defrec,$revrec)." WHERE record_id=?";
2670 my $ret = $dbh->selectrow_hashref($sql, undef, ($id) );
2671
2672 if ($dbh->err) {
2673 $errstr = $DBI::errstr;
2674 return undef;
2675 }
2676
2677 if (!$ret) {
2678 $errstr = "No such record";
2679 return undef;
2680 }
2681
2682 # explicitly set a parent id
2683 if ($defrec eq 'y') {
2684 $ret->{parid} = $ret->{group_id};
2685 } else {
2686 $ret->{parid} = (($revrec eq 'n') ? $ret->{domain_id} : $ret->{rdns_id});
2687 # and a secondary if we have a custom type that lives in both a forward and reverse zone
2688 $ret->{secid} = (($revrec eq 'y') ? $ret->{domain_id} : $ret->{rdns_id}) if $ret->{type} > 65279;
2689 }
2690
2691 return $ret;
2692}
2693
2694
2695##fixme: should use above (getRecLine()) to get lines for below?
2696## DNSDB::getDomRecs()
2697# Return records for a domain
2698# Takes a database handle, default/live flag, group/domain ID, start,
2699# number of records, sort field, and sort order
2700# Returns a reference to an array of hashes
2701sub getDomRecs {
2702 $errstr = '';
2703 my $dbh = shift;
2704 my $def = shift;
2705 my $rev = shift;
2706 my $id = shift;
2707 my $nrecs = shift || 'all';
2708 my $nstart = shift || 0;
2709
2710## for order, need to map input to column names
2711 my $order = shift || 'host';
2712 my $direction = shift || 'ASC';
2713
2714 my $filter = shift || '';
2715
2716 my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.ttl";
2717 $sql .= ",r.distance,r.weight,r.port" if $rev eq 'n';
2718 $sql .= " FROM "._rectable($def,$rev)." r ";
2719 $sql .= "INNER JOIN rectypes t ON r.type=t.val "; # for sorting by type alphabetically
2720 $sql .= "WHERE "._recparent($def,$rev)." = ?";
2721 $sql .= " AND NOT r.type=$reverse_typemap{SOA}";
2722 $sql .= " AND host ~* ?" if $filter;
2723 # use alphaorder column for "correct" ordering of sort-by-type instead of DNS RR type number
2724 $sql .= " ORDER BY ".($order eq 'type' ? 't.alphaorder' : "r.$order")." $direction";
2725 # ensure consistent ordering by sorting on record_id too
2726 $sql .= ", record_id $direction";
2727
2728 my @bindvars = ($id);
2729 push @bindvars, $filter if $filter;
2730
2731 # just to be ultraparanoid about SQL injection vectors
2732 if ($nstart ne 'all') {
2733 $sql .= " LIMIT ? OFFSET ?";
2734 push @bindvars, $nrecs;
2735 push @bindvars, ($nstart*$nrecs);
2736 }
2737 my $sth = $dbh->prepare($sql) or warn $dbh->errstr;
2738 $sth->execute(@bindvars) or warn "$sql: ".$sth->errstr;
2739
2740 my @retbase;
2741 while (my $ref = $sth->fetchrow_hashref()) {
2742 push @retbase, $ref;
2743 }
2744
2745 my $ret = \@retbase;
2746 return $ret;
2747} # end getDomRecs()
2748
2749
2750## DNSDB::getRecCount()
2751# Return count of non-SOA records in zone (or default records in a group)
2752# Takes a database handle, default/live flag, reverse/forward flag, group/domain ID,
2753# and optional filtering modifier
2754# Returns the count
2755sub getRecCount {
2756 my $dbh = shift;
2757 my $defrec = shift;
2758 my $revrec = shift;
2759 my $id = shift;
2760 my $filter = shift || '';
2761
2762 # keep the nasties down, since we can't ?-sub this bit. :/
2763 # note this is chars allowed in DNS hostnames
2764 $filter =~ s/[^a-zA-Z0-9_.:-]//g;
2765
2766 my @bindvars = ($id);
2767 push @bindvars, $filter if $filter;
2768 my $sql = "SELECT count(*) FROM ".
2769 _rectable($defrec,$revrec).
2770 " WHERE "._recparent($defrec,$revrec)."=? ".
2771 "AND NOT type=$reverse_typemap{SOA}".
2772 ($filter ? " AND host ~* ?" : '');
2773 my ($count) = $dbh->selectrow_array($sql, undef, (@bindvars) );
2774
2775 return $count;
2776
2777} # end getRecCount()
2778
2779
2780## DNSDB::addRec()
2781# Add a new record to a domain or a group's default records
2782# Takes a database handle, default/live flag, group/domain ID,
2783# host, type, value, and TTL
2784# Some types require additional detail: "distance" for MX and SRV,
2785# and weight/port for SRV
2786# Returns a status code and detail message in case of error
2787##fixme: pass a hash with the record data, not a series of separate values
2788sub addRec {
2789 $errstr = '';
2790 my $dbh = shift;
2791 my $defrec = shift;
2792 my $revrec = shift;
2793 my $id = shift; # parent (group_id for defrecs, rdns_id for reverse records,
2794 # domain_id for domain records)
2795
2796 my $host = shift;
2797 my $rectype = shift; # reference so we can coerce it if "+"-types can't find both zones
2798 my $val = shift;
2799 my $ttl = shift;
2800
2801 # prep for validation
2802 my $addr = NetAddr::IP->new($$val);
2803 $$host =~ s/\.+$//; # FQDNs ending in . are an internal detail, and really shouldn't be exposed in the UI.
2804
2805 my $domid = 0;
2806 my $revid = 0;
2807
2808 my $retcode = 'OK'; # assume everything will go OK
2809 my $retmsg = '';
2810
2811 # do simple validation first
2812 return ('FAIL', "TTL must be numeric") unless $ttl =~ /^\d+$/;
2813
2814 # Quick check on hostname parts. Note the regex is more forgiving than the error message;
2815 # domain names technically are case-insensitive, and we use printf-like % codes for a couple
2816 # of types. Other things may also be added to validate default records of several flavours.
2817 return ('FAIL', "Hostnames may not contain anything other than (0-9 a-z . _)")
2818 if $defrec eq 'n' && $$host !~ /^[0-9a-z_%.-]+$/i;
2819
2820 # Collect these even if we're only doing a simple A record so we can call *any* validation sub
2821 my $dist = shift;
2822 my $weight = shift;
2823 my $port = shift;
2824
2825 my $fields;
2826 my @vallist;
2827
2828 # Call the validation sub for the type requested.
2829 ($retcode,$retmsg) = $validators{$$rectype}($dbh, (defrec => $defrec, revrec => $revrec, id => $id,
2830 host => $host, rectype => $rectype, val => $val, addr => $addr,
2831 dist => \$dist, port => \$port, weight => \$weight,
2832 fields => \$fields, vallist => \@vallist) );
2833
2834 return ($retcode,$retmsg) if $retcode eq 'FAIL';
2835
2836 # Set up database fields and bind parameters
2837 $fields .= "host,type,val,ttl,"._recparent($defrec,$revrec);
2838 push @vallist, ($$host,$$rectype,$$val,$ttl,$id);
2839 my $vallen = '?'.(',?'x$#vallist);
2840
2841 # Put together the success log entry. We have to use this horrible kludge
2842 # because domain_id and rdns_id may or may not be present, and if they are,
2843 # they're not at a guaranteed consistent index in the array. wheee!
2844 my %logdata;
2845 my @ftmp = split /,/, $fields;
2846 for (my $i=0; $i <= $#vallist; $i++) {
2847 $logdata{domain_id} = $vallist[$i] if $ftmp[$i] eq 'domain_id';
2848 $logdata{rdns_id} = $vallist[$i] if $ftmp[$i] eq 'rdns_id';
2849 }
2850 $logdata{group_id} = $id if $defrec eq 'y';
2851 $logdata{group_id} = parentID($dbh,
2852 (id => $id, type => ($revrec eq 'n' ? 'domain' : 'revzone'), revrec => $revrec) )
2853 if $defrec eq 'n';
2854 $logdata{entry} = "Added ".($defrec eq 'y' ? 'default record' : 'record')." '$$host $typemap{$$rectype} $$val";
2855 $logdata{entry} .= " [distance $dist]" if $typemap{$$rectype} eq 'MX';
2856 $logdata{entry} .= " [priority $dist] [weight $weight] [port $port]"
2857 if $typemap{$$rectype} eq 'SRV';
2858 $logdata{entry} .= "', TTL $ttl";
2859
2860 # Allow transactions, and raise an exception on errors so we can catch it later.
2861 # Use local to make sure these get "reset" properly on exiting this block
2862 local $dbh->{AutoCommit} = 0;
2863 local $dbh->{RaiseError} = 1;
2864
2865 eval {
2866 $dbh->do("INSERT INTO "._rectable($defrec, $revrec)." ($fields) VALUES ($vallen)",
2867 undef, @vallist);
2868 _log($dbh, %logdata);
2869 $dbh->commit;
2870 };
2871 if ($@) {
2872 my $msg = $@;
2873 eval { $dbh->rollback; };
2874 if ($config{log_failures}) {
2875 $logdata{entry} = "Failed adding ".($defrec eq 'y' ? 'default ' : '').
2876 "record '$$host $typemap{$$rectype} $$val', TTL $ttl ($msg)";
2877 _log($dbh, %logdata);
2878 $dbh->commit;
2879 }
2880 return ('FAIL',$msg);
2881 }
2882
2883 $resultstr = $logdata{entry};
2884 return ($retcode, $retmsg);
2885
2886} # end addRec()
2887
2888
2889## DNSDB::updateRec()
2890# Update a record
2891# Takes a database handle, default and reverse flags, record ID, immediate parent ID, and new record data.
2892# Returns a status code and message
2893sub updateRec {
2894 $errstr = '';
2895
2896 my $dbh = shift;
2897 my $defrec = shift;
2898 my $revrec = shift;
2899 my $id = shift;
2900 my $parid = shift; # immediate parent entity that we're descending from to update the record
2901
2902 # all records have these
2903 my $host = shift;
2904 my $hostbk = $$host; # Keep a backup copy of the original, so we can WARN if the update mangles the domain
2905 my $rectype = shift;
2906 my $val = shift;
2907 my $ttl = shift;
2908
2909 # prep for validation
2910 my $addr = NetAddr::IP->new($$val);
2911 $$host =~ s/\.+$//; # FQDNs ending in . are an internal detail, and really shouldn't be exposed in the UI.
2912
2913 my $domid = 0;
2914 my $revid = 0;
2915
2916 my $retcode = 'OK'; # assume everything will go OK
2917 my $retmsg = '';
2918
2919 # do simple validation first
2920 return ('FAIL', "TTL must be numeric") unless $ttl =~ /^\d+$/;
2921
2922 # Quick check on hostname parts. Note the regex is more forgiving than the error message;
2923 # domain names technically are case-insensitive, and we use printf-like % codes for a couple
2924 # of types. Other things may also be added to validate default records of several flavours.
2925 return ('FAIL', "Hostnames may not contain anything other than (0-9 a-z - . _)")
2926 if $defrec eq 'n' && $$host !~ /^[0-9a-z_%.-]+$/i;
2927
2928 # only MX and SRV will use these
2929 my $dist = shift || 0;
2930 my $weight = shift || 0;
2931 my $port = shift || 0;
2932
2933 my $fields;
2934 my @vallist;
2935
2936 # get old record data so we have the right parent ID
2937 # and for logging (eventually)
2938 my $oldrec = getRecLine($dbh, $defrec, $revrec, $id);
2939
2940 # Call the validation sub for the type requested.
2941 # Note the ID to pass here is the *parent*, not the record
2942 ($retcode,$retmsg) = $validators{$$rectype}($dbh, (defrec => $defrec, revrec => $revrec,
2943 id => ($defrec eq 'y' ? $oldrec->{group_id} : ($revrec eq 'n' ? $oldrec->{domain_id} : $oldrec->{rdns_id})),
2944 host => $host, rectype => $rectype, val => $val, addr => $addr,
2945 dist => \$dist, port => \$port, weight => \$weight,
2946 fields => \$fields, vallist => \@vallist,
2947 update => $id) );
2948
2949 return ($retcode,$retmsg) if $retcode eq 'FAIL';
2950
2951 # Set up database fields and bind parameters. Note only the optional fields
2952 # (distance, weight, port, secondary parent ID) are added in the validation call above
2953 $fields .= "host,type,val,ttl,"._recparent($defrec,$revrec);
2954 push @vallist, ($$host,$$rectype,$$val,$ttl,
2955 ($defrec eq 'y' ? $oldrec->{group_id} : ($revrec eq 'n' ? $oldrec->{domain_id} : $oldrec->{rdns_id})) );
2956
2957 # hack hack PTHUI
2958 # need to forcibly make sure we disassociate a record with a parent it's no longer related to.
2959 # eg, PTR records may not have a domain parent, or A/AAAA records may not have a revzone parent.
2960 # mainly needed for crossover types that got coerced down to "standard" types
2961 if ($defrec eq 'n') {
2962 if ($$rectype == $reverse_typemap{PTR}) {
2963 $fields .= ",domain_id";
2964 push @vallist, 0;
2965 }
2966 if ($$rectype == $reverse_typemap{A} || $$rectype == $reverse_typemap{AAAA}) {
2967 $fields .= ",rdns_id";
2968 push @vallist, 0;
2969 }
2970 }
2971
2972 # Fiddle the field list into something suitable for updates
2973 $fields =~ s/,/=?,/g;
2974 $fields .= "=?";
2975
2976 # Put together the success log entry. Horrible kludge from addRec() copied as-is since
2977 # we don't know whether the passed arguments or retrieved values for domain_id and rdns_id
2978 # will be maintained (due to "not-in-zone" validation changes)
2979 my %logdata;
2980 my @ftmp = split /,/, $fields;
2981 for (my $i=0; $i <= $#vallist; $i++) {
2982 $logdata{domain_id} = $vallist[$i] if $ftmp[$i] eq 'domain_id';
2983 $logdata{rdns_id} = $vallist[$i] if $ftmp[$i] eq 'rdns_id';
2984 }
2985 $logdata{group_id} = $parid if $defrec eq 'y';
2986 $logdata{group_id} = parentID($dbh,
2987 (id => $parid, type => ($revrec eq 'n' ? 'domain' : 'revzone'), revrec => $revrec) )
2988 if $defrec eq 'n';
2989 $logdata{entry} = "Updated ".($defrec eq 'y' ? 'default record' : 'record')." from\n".
2990 "'$oldrec->{host} $typemap{$oldrec->{type}} $oldrec->{val}";
2991 $logdata{entry} .= " [distance $oldrec->{distance}]" if $typemap{$oldrec->{type}} eq 'MX';
2992 $logdata{entry} .= " [priority $oldrec->{distance}] [weight $oldrec->{weight}] [port $oldrec->{port}]"
2993 if $typemap{$oldrec->{type}} eq 'SRV';
2994 $logdata{entry} .= "', TTL $oldrec->{ttl}\nto\n'$$host $typemap{$$rectype} $$val";
2995 $logdata{entry} .= " [distance $dist]" if $typemap{$$rectype} eq 'MX';
2996 $logdata{entry} .= " [priority $dist] [weight $weight] [port $port]" if $typemap{$$rectype} eq 'SRV';
2997 $logdata{entry} .= "', TTL $ttl";
2998
2999 local $dbh->{AutoCommit} = 0;
3000 local $dbh->{RaiseError} = 1;
3001
3002 eval {
3003 $dbh->do("UPDATE "._rectable($defrec,$revrec)." SET $fields WHERE record_id=?", undef, (@vallist, $id) );
3004 _log($dbh, %logdata);
3005 $dbh->commit;
3006 };
3007 if ($@) {
3008 my $msg = $@;
3009 eval { $dbh->rollback; };
3010 if ($config{log_failures}) {
3011 $logdata{entry} = "Failed updating ".($defrec eq 'y' ? 'default ' : '').
3012 "record '$oldrec->{host} $typemap{$oldrec->{type}} $oldrec->{val}', TTL $oldrec->{ttl} ($msg)";
3013 _log($dbh, %logdata);
3014 $dbh->commit;
3015 }
3016 return ('FAIL', $msg);
3017 }
3018
3019 $resultstr = $logdata{entry};
3020 return ($retcode, $retmsg);
3021} # end updateRec()
3022
3023
3024## DNSDB::delRec()
3025# Delete a record.
3026sub delRec {
3027 $errstr = '';
3028 my $dbh = shift;
3029 my $defrec = shift;
3030 my $revrec = shift;
3031 my $id = shift;
3032
3033 my $oldrec = getRecLine($dbh, $defrec, $revrec, $id);
3034
3035 # Allow transactions, and raise an exception on errors so we can catch it later.
3036 # Use local to make sure these get "reset" properly on exiting this block
3037 local $dbh->{AutoCommit} = 0;
3038 local $dbh->{RaiseError} = 1;
3039
3040 # Put together the log entry
3041 my %logdata;
3042 $logdata{domain_id} = $oldrec->{domain_id};
3043 $logdata{rdns_id} = $oldrec->{rdns_id};
3044 $logdata{group_id} = $oldrec->{group_id} if $defrec eq 'y';
3045 $logdata{group_id} = parentID($dbh,
3046 (id => $oldrec->{domain_id}, type => ($revrec eq 'n' ? 'domain' : 'revzone'), revrec => $revrec) )
3047 if $defrec eq 'n';
3048 $logdata{entry} = "Deleted ".($defrec eq 'y' ? 'default record ' : 'record ').
3049 "'$oldrec->{host} $typemap{$oldrec->{type}} $oldrec->{val}";
3050 $logdata{entry} .= " [distance $oldrec->{distance}]" if $typemap{$oldrec->{type}} eq 'MX';
3051 $logdata{entry} .= " [priority $oldrec->{distance}] [weight $oldrec->{weight}] [port $oldrec->{port}]"
3052 if $typemap{$oldrec->{type}} eq 'SRV';
3053 $logdata{entry} .= "', TTL $oldrec->{ttl}\n";
3054
3055 eval {
3056 my $sth = $dbh->do("DELETE FROM "._rectable($defrec,$revrec)." WHERE record_id=?", undef, ($id));
3057 _log($dbh, %logdata);
3058 $dbh->commit;
3059 };
3060 if ($@) {
3061 my $msg = $@;
3062 eval { $dbh->rollback; };
3063 if ($config{log_failures}) {
3064 $logdata{entry} = "Error deleting ".($defrec eq 'y' ? 'default record' : 'record').
3065 " '$oldrec->{host} $typemap{$oldrec->{type}} $oldrec->{val}', TTL $oldrec->{ttl} ($msg)";
3066 _log($dbh, %logdata);
3067 $dbh->commit;
3068 }
3069 return ('FAIL', $msg);
3070 }
3071
3072 return ('OK',$logdata{entry});
3073} # end delRec()
3074
3075
3076## DNSDB::getLogCount()
3077# Get a count of log entries
3078# Takes a database handle and a hash containing at least:
3079# - Entity ID and entity type as the primary log "slice"
3080sub getLogCount {
3081 my $dbh = shift;
3082
3083 my %args = @_;
3084
3085 my @filterargs;
3086##fixme: which fields do we want to filter on?
3087# push @filterargs,
3088
3089 $errstr = 'Missing primary parent ID and/or type';
3090 # fail early if we don't have a "prime" ID to look for log entries for
3091 return if !$args{id};
3092
3093 # or if the prime id type is missing or invalid
3094 return if !$args{logtype};
3095 $args{logtype} = 'revzone' if $args{logtype} eq 'rdns'; # hack pthui
3096 $args{logtype} = 'domain' if $args{logtype} eq 'dom'; # hack pthui
3097 return if !grep /^$args{logtype}$/, ('group', 'domain', 'revzone', 'user');
3098
3099 $args{logtype} = 'revzone' if $args{logtype} eq 'rdns'; # hack pthui
3100
3101 my $sql = "SELECT count(*) FROM log ".
3102 "WHERE $id_col{$args{logtype}}=?".
3103 ($args{filter} ? " AND entry ~* ?" : '');
3104 my ($count) = $dbh->selectrow_array($sql, undef, ($args{id}, @filterargs) );
3105 $errstr = $dbh->errstr if !$count;
3106 return $count;
3107} # end getLogCount()
3108
3109
3110## DNSDB::getLogEntries()
3111# Get a list of log entries
3112# Takes arguments as with getLogCount() above, plus optional:
3113# - sort field
3114# - sort order
3115# - offset for pagination
3116sub getLogEntries {
3117 my $dbh = shift;
3118
3119 my %args = @_;
3120
3121 my @filterargs;
3122
3123 # fail early if we don't have a "prime" ID to look for log entries for
3124 return if !$args{id};
3125
3126 # or if the prime id type is missing or invalid
3127 return if !$args{logtype};
3128 $args{logtype} = 'revzone' if $args{logtype} eq 'rdns'; # hack pthui
3129 $args{logtype} = 'domain' if $args{logtype} eq 'dom'; # hack pthui
3130 return if !grep /^$args{logtype}$/, ('group', 'domain', 'revzone', 'user');
3131
3132 # Sorting defaults
3133 $args{sortby} = 'stamp' if !$args{sortby};
3134 $args{sortorder} = 'DESC' if !$args{sortorder};
3135 $args{offset} = 0 if !$args{offset};
3136
3137 my %sortmap = (fname => 'name', username => 'email', entry => 'entry', stamp => 'stamp');
3138 $args{sortby} = $sortmap{$args{sortby}};
3139
3140 my $sql = "SELECT user_id AS userid, email AS useremail, name AS userfname, entry AS logentry, ".
3141 "date_trunc('second',stamp) AS logtime ".
3142 "FROM log ".
3143 "WHERE $id_col{$args{logtype}}=?".
3144 ($args{filter} ? " AND entry ~* ?" : '').
3145 " ORDER BY $args{sortby} $args{sortorder}, log_id $args{sortorder}".
3146 ($args{offset} eq 'all' ? '' : " LIMIT $config{perpage} OFFSET ".$args{offset}*$config{perpage});
3147 my $loglist = $dbh->selectall_arrayref($sql, { Slice => {} }, ($args{id}, @filterargs) );
3148 $errstr = $dbh->errstr if !$loglist;
3149 return $loglist;
3150} # end getLogEntries()
3151
3152
3153## DNSDB::getTypelist()
3154# Get a list of record types for various UI dropdowns
3155# Takes database handle, forward/reverse/lookup flag, and optional "tag as selected" indicator (defaults to A)
3156# Returns an arrayref to list of hashrefs perfect for HTML::Template
3157sub getTypelist {
3158 my $dbh = shift;
3159 my $recgroup = shift;
3160 my $type = shift || $reverse_typemap{A};
3161
3162 # also accepting $webvar{revrec}!
3163 $recgroup = 'f' if $recgroup eq 'n';
3164 $recgroup = 'r' if $recgroup eq 'y';
3165
3166 my $sql = "SELECT val,name FROM rectypes WHERE ";
3167 if ($recgroup eq 'r') {
3168 # reverse zone types
3169 $sql .= "stdflag=2 OR stdflag=3";
3170 } elsif ($recgroup eq 'l') {
3171 # DNS lookup types. Note we avoid our custom types >= 65280, since those are entirely internal.
3172 $sql .= "(stdflag=1 OR stdflag=2 OR stdflag=3) AND val < 65280";
3173 } else {
3174 # default; forward zone types. technically $type eq 'f' but not worth the error message.
3175 $sql .= "stdflag=1 OR stdflag=2";
3176 }
3177 $sql .= " ORDER BY listorder";
3178
3179 my $sth = $dbh->prepare($sql);
3180 $sth->execute;
3181 my @typelist;
3182 while (my ($rval,$rname) = $sth->fetchrow_array()) {
3183 my %row = ( recval => $rval, recname => $rname );
3184 $row{tselect} = 1 if $rval == $type;
3185 push @typelist, \%row;
3186 }
3187
3188 # Add SOA on lookups since it's not listed in other dropdowns.
3189 if ($recgroup eq 'l') {
3190 my %row = ( recval => $reverse_typemap{SOA}, recname => 'SOA' );
3191 $row{tselect} = 1 if $reverse_typemap{SOA} == $type;
3192 push @typelist, \%row;
3193 }
3194
3195 return \@typelist;
3196} # end getTypelist()
3197
3198
3199## DNSDB::parentID()
3200# Get ID of entity that is nearest parent to requested id
3201# Takes a database handle and a hash of entity ID, entity type, optional parent type flag
3202# (domain/reverse zone or group), and optional default/live and forward/reverse flags
3203# Returns the ID or undef on failure
3204sub parentID {
3205 my $dbh = shift;
3206
3207 my %args = @_;
3208
3209 # clean up the parent-type. Set it to group if not set; coerce revzone to domain for simpler logic
3210 $args{partype} = 'group' if !$args{partype};
3211 $args{partype} = 'domain' if $args{partype} eq 'revzone';
3212
3213 # clean up defrec and revrec. default to live record, forward zone
3214 $args{defrec} = 'n' if !$args{defrec};
3215 $args{revrec} = 'n' if !$args{revrec};
3216
3217 if ($par_type{$args{partype}} eq 'domain') {
3218 # only live records can have a domain/zone parent
3219 return unless ($args{type} eq 'record' && $args{defrec} eq 'n');
3220 my $result = $dbh->selectrow_hashref("SELECT ".($args{revrec} eq 'n' ? 'domain_id' : 'rdns_id').
3221 " FROM records WHERE record_id = ?",
3222 undef, ($args{id}) ) or return;
3223 return $result;
3224 } else {
3225 # snag some arguments that will either fall through or be overwritten to save some code duplication
3226 my $tmpid = $args{id};
3227 my $type = $args{type};
3228 if ($type eq 'record' && $args{defrec} eq 'n') {
3229 # Live records go through the records table first.
3230 ($tmpid) = $dbh->selectrow_array("SELECT ".($args{revrec} eq 'n' ? 'domain_id' : 'rdns_id').
3231 " FROM records WHERE record_id = ?",
3232 undef, ($args{id}) ) or return;
3233 $type = ($args{revrec} eq 'n' ? 'domain' : 'revzone');
3234 }
3235 my ($result) = $dbh->selectrow_array("SELECT $par_col{$type} FROM $par_tbl{$type} WHERE $id_col{$type} = ?",
3236 undef, ($tmpid) );
3237 return $result;
3238 }
3239# should be impossible to get here with even remotely sane arguments
3240 return;
3241} # end parentID()
3242
3243
3244## DNSDB::isParent()
3245# Returns true if $id1 is a parent of $id2, false otherwise
3246sub isParent {
3247 my $dbh = shift;
3248 my $id1 = shift;
3249 my $type1 = shift;
3250 my $id2 = shift;
3251 my $type2 = shift;
3252##todo: immediate, secondary, full (default)
3253
3254 # Return false on invalid types
3255 return 0 if !grep /^$type1$/, ('record','defrec','defrevrec','user','domain','revzone','group');
3256 return 0 if !grep /^$type2$/, ('record','defrec','defrevrec','user','domain','revzone','group');
3257
3258 # Return false on impossible relations
3259 return 0 if $type1 eq 'record'; # nothing may be a child of a record
3260 return 0 if $type1 eq 'defrec'; # nothing may be a child of a record
3261 return 0 if $type1 eq 'defrevrec'; # nothing may be a child of a record
3262 return 0 if $type1 eq 'user'; # nothing may be child of a user
3263 return 0 if $type1 eq 'domain' && $type2 ne 'record'; # domain may not be a parent of anything other than a record
3264 return 0 if $type1 eq 'revzone' && $type2 ne 'record';# reverse zone may not be a parent of anything other than a record
3265
3266 # ennnhhhh.... if we're passed an id of 0, it will never be found. usual
3267 # case would be the UI creating a new <thing>, and so we don't have an ID for
3268 # <thing> to look up yet. in that case the UI should check the parent as well.
3269 return 0 if $id1 == 0; # nothing can have a parent id of 0
3270 return 1 if $id2 == 0; # anything could have a child id of 0 (or "unknown")
3271
3272 # group 1 is the ultimate root parent
3273 return 1 if $type1 eq 'group' && $id1 == 1;
3274
3275 # groups are always (a) parent of themselves
3276 return 1 if $type1 eq 'group' && $type2 eq 'group' && $id1 == $id2;
3277
3278 my $id = $id2;
3279 my $type = $type2;
3280 my $foundparent = 0;
3281
3282 # Records are the only entity with two possible parents. We need to split the parent checks on
3283 # domain/rdns.
3284 if ($type eq 'record') {
3285 my ($dom,$rdns) = $dbh->selectrow_array("SELECT domain_id,rdns_id FROM records WHERE record_id=?",
3286 undef, ($id));
3287 # check immediate parent against request
3288 return 1 if $type1 eq 'domain' && $id1 == $dom;
3289 return 1 if $type1 eq 'revzone' && $id1 == $rdns;
3290 # if request is group, check *both* parents. Only check if the parent is nonzero though.
3291 return 1 if $dom && isParent($dbh, $id1, $type1, $dom, 'domain');
3292 return 1 if $rdns && isParent($dbh, $id1, $type1, $rdns, 'revzone');
3293 # exit here since we've executed the loop below by proxy in the above recursive calls.
3294 return 0;
3295 }
3296
3297# almost the same loop as getParents() above
3298 my $limiter = 0;
3299 while (1) {
3300 my $sql = "SELECT $par_col{$type} FROM $par_tbl{$type} WHERE $id_col{$type} = ?";
3301 my $result = $dbh->selectrow_hashref($sql,
3302 undef, ($id) );
3303 if (!$result) {
3304 $limiter++;
3305##fixme: how often will this happen on a live site? fail at max limiter <n>?
3306 warn "no results looking for $sql with id $id (depth $limiter)\n";
3307 last;
3308 }
3309 if ($result && $result->{$par_col{$type}} == $id1) {
3310 $foundparent = 1;
3311 last;
3312 } else {
3313##fixme: do we care about trying to return a "no such record/domain/user/group" error?
3314# should be impossible to create an inconsistent DB just with API calls.
3315 warn $dbh->errstr." $sql, $id" if $dbh->errstr;
3316 }
3317 # group 1 is its own parent. need this here more to break strange loops than for detecting a parent
3318 last if $result->{$par_col{$type}} == 1;
3319 $id = $result->{$par_col{$type}};
3320 $type = $par_type{$type};
3321 }
3322
3323 return $foundparent;
3324} # end isParent()
3325
3326
3327## DNSDB::zoneStatus()
3328# Returns and optionally sets a zone's status
3329# Takes a database handle, domain/revzone ID, forward/reverse flag, and optionally a status argument
3330# Returns status, or undef on errors.
3331sub zoneStatus {
3332 my $dbh = shift;
3333 my $id = shift;
3334 my $revrec = shift;
3335 my $newstatus = shift || 'mu';
3336
3337 return undef if $id !~ /^\d+$/;
3338
3339 # Allow transactions, and raise an exception on errors so we can catch it later.
3340 # Use local to make sure these get "reset" properly on exiting this block
3341 local $dbh->{AutoCommit} = 0;
3342 local $dbh->{RaiseError} = 1;
3343
3344 if ($newstatus ne 'mu') {
3345 # ooo, fun! let's see what we were passed for status
3346 eval {
3347 $newstatus = 0 if $newstatus eq 'domoff';
3348 $newstatus = 1 if $newstatus eq 'domon';
3349 $dbh->do("UPDATE ".($revrec eq 'n' ? 'domains' : 'revzones')." SET status=? WHERE ".
3350 ($revrec eq 'n' ? 'domain_id' : 'rdns_id')."=?", undef, ($newstatus,$id) );
3351
3352##fixme switch to more consise "Enabled <domain"/"Disabled <domain>" as with users?
3353 $resultstr = "Changed ".($revrec eq 'n' ? domainName($dbh, $id) : revName($dbh, $id)).
3354 " state to ".($newstatus ? 'active' : 'inactive');
3355
3356 my %loghash;
3357 $loghash{domain_id} = $id if $revrec eq 'n';
3358 $loghash{rdns_id} = $id if $revrec eq 'y';
3359 $loghash{group_id} = parentID($dbh,
3360 (id => $id, type => ($revrec eq 'n' ? 'domain' : 'revzone'), revrec => $revrec) );
3361 $loghash{entry} = $resultstr;
3362 _log($dbh, %loghash);
3363
3364 $dbh->commit;
3365 };
3366 if ($@) {
3367 my $msg = $@;
3368 eval { $dbh->rollback; };
3369 $resultstr = '';
3370 $errstr = $msg;
3371 return;
3372 }
3373 }
3374
3375 my ($status) = $dbh->selectrow_array("SELECT status FROM ".
3376 ($revrec eq 'n' ? "domains WHERE domain_id=?" : "revzones WHERE rdns_id=?"),
3377 undef, ($id) );
3378 return $status;
3379} # end zoneStatus()
3380
3381
3382## DNSDB::importAXFR
3383# Import a domain via AXFR
3384# Takes AXFR host, domain to transfer, group to put the domain in,
3385# and optionally:
3386# - active/inactive state flag (defaults to active)
3387# - overwrite-SOA flag (defaults to off)
3388# - overwrite-NS flag (defaults to off, doesn't affect subdomain NS records)
3389# Returns a status code (OK, WARN, or FAIL) and message - message should be blank
3390# if status is OK, but WARN includes conditions that are not fatal but should
3391# really be reported.
3392sub importAXFR {
3393 my $dbh = shift;
3394 my $ifrom_in = shift;
3395 my $zone = shift;
3396 my $group = shift;
3397 my $status = shift || 1;
3398 my $rwsoa = shift || 0;
3399 my $rwns = shift || 0;
3400 my $merge = shift || 0; # do we attempt to merge A/AAAA and PTR records whenever possible?
3401 # do we overload this with the fixme below?
3402##fixme: add mode to delete&replace, merge+overwrite, merge new?
3403
3404 my $nrecs = 0;
3405 my $soaflag = 0;
3406 my $nsflag = 0;
3407 my $warnmsg = '';
3408 my $ifrom;
3409
3410 my $rev = 'n';
3411 my $code = 'OK';
3412 my $msg = 'foobar?';
3413
3414 # choke on possible bad setting in ifrom
3415 # IPv4 and v6, and valid hostnames!
3416 ($ifrom) = ($ifrom_in =~ /^([0-9a-f\:.]+|[0-9a-z_.-]+)$/i);
3417 return ('FAIL', "Bad AXFR source host $ifrom")
3418 unless ($ifrom) = ($ifrom_in =~ /^([0-9a-f\:.]+|[0-9a-z_.-]+)$/i);
3419
3420 my $errmsg;
3421
3422 my $zone_id;
3423 my $domain_id = 0;
3424 my $rdns_id = 0;
3425 my $cidr;
3426
3427# magic happens! detect if we're importing a domain or a reverse zone
3428# while we're at it, figure out what the CIDR netblock is (if we got a .arpa)
3429# or what the formal .arpa zone is (if we got a CIDR netblock)
3430# Handles sub-octet v4 zones in the format specified in the Cricket Book, 2nd Ed, p217-218
3431
3432 if ($zone =~ m{(?:\.arpa\.?|/\d+)$}) {
3433 # we seem to have a reverse zone
3434 $rev = 'y';
3435
3436 if ($zone =~ /\.arpa\.?$/) {
3437 # we have a formal reverse zone. call _zone2cidr and get the CIDR block.
3438 ($code,$msg) = _zone2cidr($zone);
3439 return ($code, $msg) if $code eq 'FAIL';
3440 $cidr = $msg;
3441 } elsif ($zone =~ m|^[\d.]+/\d+$|) {
3442 # v4 revzone, CIDR netblock
3443 $cidr = NetAddr::IP->new($zone) or return ('FAIL',"$zone is not a valid CIDR block");
3444 $zone = _ZONE($cidr, 'ZONE.in-addr.arpa', 'r', '.');
3445 } elsif ($zone =~ m|^[a-fA-F\d:]+/\d+$|) {
3446 # v6 revzone, CIDR netblock
3447 $cidr = NetAddr::IP->new($zone) or return ('FAIL',"$zone is not a valid CIDR block");
3448 return ('FAIL', "$zone is not a nibble-aligned block") if $cidr->masklen % 4 != 0;
3449 $zone = _ZONE($cidr, 'ZONE.ip6.arpa', 'r', '.');
3450 } else {
3451 # there is. no. else!
3452 return ('FAIL', "Unknown zone name format");
3453 }
3454
3455 # quick check to start to see if we've already got one
3456
3457 ($zone_id) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet=?",
3458 undef, ("$cidr"));
3459 $rdns_id = $zone_id;
3460 } else {
3461 # default to domain
3462 ($zone_id) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE domain=?",
3463 undef, ($zone));
3464 $domain_id = $zone_id;
3465 }
3466
3467 return ('FAIL', ($rev eq 'n' ? 'Domain' : 'Reverse zone')." already exists") if $zone_id;
3468
3469 # little local utility sub to swap $val and $host for revzone records.
3470 sub _revswap {
3471 my $rechost = shift;
3472 my $recdata = shift;
3473
3474 if ($rechost =~ /\.in-addr\.arpa\.?$/) {
3475 $rechost =~ s/\.in-addr\.arpa\.?$//;
3476 $rechost = join '.', reverse split /\./, $rechost;
3477 } else {
3478 $rechost =~ s/\.ip6\.arpa\.?$//;
3479 my @nibs = reverse split /\./, $rechost;
3480 $rechost = '';
3481 my $nc;
3482 foreach (@nibs) {
3483 $rechost.= $_;
3484 $rechost .= ":" if ++$nc % 4 == 0 && $nc < 32;
3485 }
3486 $rechost .= ":" if $nc < 32 && $rechost !~ /\*$/; # close netblock records?
3487##fixme: there's a case that ends up with a partial entry here:
3488# ip:add:re:ss::
3489# can't reproduce after letting it sit overnight after discovery. :(
3490#print "$rechost\n";
3491 # canonicalize with NetAddr::IP
3492 $rechost = NetAddr::IP->new($rechost)->addr unless $rechost =~ /\*$/;
3493 }
3494 return ($recdata,$rechost)
3495 }
3496
3497
3498 # Allow transactions, and raise an exception on errors so we can catch it later.
3499 # Use local to make sure these get "reset" properly on exiting this block
3500 local $dbh->{AutoCommit} = 0;
3501 local $dbh->{RaiseError} = 1;
3502
3503 my $sth;
3504 eval {
3505
3506 if ($rev eq 'n') {
3507##fixme: serial
3508 $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,?,?)", undef, ($zone,$group,$status) );
3509 # get domain id so we can do the records
3510 ($zone_id) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
3511 $domain_id = $zone_id;
3512 _log($dbh, (group_id => $group, domain_id => $domain_id,
3513 entry => "[Added ".($status ? 'active' : 'inactive')." domain $zone via AXFR]") );
3514 } else {
3515##fixme: serial
3516 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,?,?)", undef, ($cidr,$group,$status) );
3517 # get revzone id so we can do the records
3518 ($zone_id) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
3519 $rdns_id = $zone_id;
3520 _log($dbh, (group_id => $group, rdns_id => $rdns_id,
3521 entry => "[Added ".($status ? 'active' : 'inactive')." reverse zone $cidr via AXFR]") );
3522 }
3523
3524## bizarre DBI<->Net::DNS interaction bug:
3525## sometimes a zone will cause an immediate commit-and-exit (sort of) of the while()
3526## fixed, apparently I was doing *something* odd, but not certain what it was that
3527## caused a commit instead of barfing
3528
3529 my $res = Net::DNS::Resolver->new;
3530 $res->nameservers($ifrom);
3531 $res->axfr_start($zone)
3532 or die "Couldn't begin AXFR\n";
3533
3534 $sth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl)".
3535 " VALUES (?,?,?,?,?,?,?,?,?)");
3536
3537 # Stash info about sub-octet v4 revzones here so we don't have
3538 # to store the CNAMEs used to delegate a suboctet zone
3539 # $suboct{zone}{ns}[] -> array of nameservers
3540 # $suboct{zone}{cname}[] -> array of extant CNAMEs (Just In Case someone did something bizarre)
3541## commented pending actual use of this data. for now, we'll just
3542## auto-(re)create the CNAMEs in revzones on export
3543# my %suboct;
3544
3545 while (my $rr = $res->axfr_next()) {
3546
3547 my $val;
3548 my $distance = 0;
3549 my $weight = 0;
3550 my $port = 0;
3551 my $logfrag = '';
3552
3553 my $type = $rr->type;
3554 my $host = $rr->name;
3555 my $ttl = $rr->ttl;
3556
3557 $soaflag = 1 if $type eq 'SOA';
3558 $nsflag = 1 if $type eq 'NS';
3559
3560# "Primary" types:
3561# A, NS, CNAME, SOA, PTR(warn in forward), MX, TXT, AAAA, SRV, A6(ob), SPF
3562# maybe KEY
3563
3564# BIND supports:
3565# [standard]
3566# A AAAA CNAME MX NS PTR SOA TXT
3567# [variously experimental, obsolete, or obscure]
3568# HINFO MB(ex) MD(ob) MF(ob) MG(ex) MINFO(ex) MR(ex) NULL WKS AFSDB(ex) ISDN(ex) RP(ex) RT(ex) X25(ex) PX
3569# ... if one can ever find the right magic to format them correctly
3570
3571# Net::DNS supports:
3572# RRSIG SIG NSAP NS NIMLOC NAPTR MX MR MINFO MG MB LOC ISDN IPSECKEY HINFO
3573# EID DNAME CNAME CERT APL AFSDB AAAA A DS NXT NSEC3PARAM NSEC3 NSEC KEY
3574# DNSKEY DLV X25 TXT TSIG TKEY SSHFP SRV SPF SOA RT RP PX PTR NULL APL::AplItem
3575
3576# nasty big ugly case-like thing here, since we have to do *some* different
3577# processing depending on the record. le sigh.
3578
3579##fixme: what record types other than TXT can/will have >255-byte payloads?
3580
3581 if ($type eq 'A') {
3582 $val = $rr->address;
3583 } elsif ($type eq 'NS') {
3584# hmm. should we warn here if subdomain NS'es are left alone?
3585 next if ($rwns && ($rr->name eq $zone));
3586 if ($rev eq 'y') {
3587 # revzones have records more or less reversed from forward zones.
3588 my ($tmpcode,$tmpmsg) = _zone2cidr($host);
3589 die "Error converting NS record: $tmpmsg" if $tmpcode eq 'FAIL'; # hmm. may not make sense...
3590 $val = "$tmpmsg";
3591 $host = $rr->nsdname;
3592 $logfrag = "Added record '$val $type $host', TTL $ttl";
3593# Tag and preserve. For now this is commented for a no-op, but we have Ideas for
3594# another custom storage type ("DELEGATE") that will use these subzone-delegation records
3595#if ($val ne "$cidr") {
3596# push @{$suboct{$val}{ns}}, $host;
3597#}
3598 } else {
3599 $val = $rr->nsdname;
3600 }
3601 $nsflag = 1;
3602 } elsif ($type eq 'CNAME') {
3603 if ($rev eq 'y') {
3604 # hmm. do we even want to bother with storing these at this level? Sub-octet delegation
3605 # by CNAME is essentially a record-publication hack, and we want to just represent the
3606 # "true" logical intentions as far down the stack as we can from the UI.
3607 ($host,$val) = _revswap($host,$rr->cname);
3608 $logfrag = "Added record '$val $type $host', TTL $ttl";
3609# Tag and preserve in case we want to commit them as-is later, but mostly we don't care.
3610# Commented pending actually doing something with possibly new type DELEGATE
3611#my $tmprev = $host;
3612#$tmprev =~ s/^\d+\.//;
3613#($code,$tmprev) = _zone2cidr($tmprev);
3614#push @{$suboct{"$tmprev"}{cname}}, $val;
3615 # Silently skip CNAMEs in revzones.
3616 next;
3617 } else {
3618 $val = $rr->cname;
3619 }
3620 } elsif ($type eq 'SOA') {
3621 next if $rwsoa;
3622 $host = $rr->rname.":".$rr->mname;
3623 $val = $rr->refresh.":".$rr->retry.":".$rr->expire.":".$rr->minimum;
3624 $soaflag = 1;
3625 } elsif ($type eq 'PTR') {
3626 ($host,$val) = _revswap($host,$rr->ptrdname);
3627 $logfrag = "Added record '$val $type $host', TTL $ttl";
3628 # hmm. PTR records should not be in forward zones.
3629 } elsif ($type eq 'MX') {
3630 $val = $rr->exchange;
3631 $distance = $rr->preference;
3632 } elsif ($type eq 'TXT') {
3633##fixme: Net::DNS docs say this should be deprecated for rdatastr() or char_str_list(),
3634## but don't really seem enthusiastic about it.
3635#print "should use rdatastr:\n\t".$rr->rdatastr."\n or char_str_list:\n\t".join(' ',$rr->char_str_list())."\n";
3636# rdatastr returns a BIND-targetted logical string, including opening and closing quotes
3637# char_str_list returns a list of the individual string fragments in the record
3638# txtdata returns the more useful all-in-one form (since we want to push such protocol
3639# details as far down the stack as we can)
3640# NB: this may turn out to be more troublesome if we ever have need of >512-byte TXT records.
3641 if ($rev eq 'y') {
3642 ($host,$val) = _revswap($host,$rr->txtdata);
3643 $logfrag = "Added record '$val $type $host', TTL $ttl";
3644 } else {
3645 $val = $rr->txtdata;
3646 }
3647 } elsif ($type eq 'SPF') {
3648##fixme: and the same caveat here, since it is apparently a clone of ::TXT
3649 $val = $rr->txtdata;
3650 } elsif ($type eq 'AAAA') {
3651 $val = $rr->address;
3652 } elsif ($type eq 'SRV') {
3653 $val = $rr->target;
3654 $distance = $rr->priority;
3655 $weight = $rr->weight;
3656 $port = $rr->port;
3657 } elsif ($type eq 'KEY') {
3658 # we don't actually know what to do with these...
3659 $val = $rr->flags.":".$rr->protocol.":".$rr->algorithm.":".$rr->key.":".$rr->keytag.":".$rr->privatekeyname;
3660 } else {
3661 $val = $rr->rdatastr;
3662 # Finding a different record type is not fatal.... just problematic.
3663 # We may not be able to export it correctly.
3664 $warnmsg .= "Unusual record ".$rr->name." ($type) found\n";
3665 }
3666
3667 my $logentry = "[AXFR ".($rev eq 'n' ? $zone : $cidr)."] ";
3668
3669 if ($merge) {
3670 if ($rev eq 'n') {
3671 # importing a domain; we have A and AAAA records that could be merged with matching PTR records
3672 my $etype;
3673 my ($erdns,$erid,$ettl) = $dbh->selectrow_array("SELECT rdns_id,record_id,ttl FROM records ".
3674 "WHERE host=? AND val=? AND type=12",
3675 undef, ($host, $val) );
3676 if ($erid) {
3677 if ($type eq 'A') { # PTR -> A+PTR
3678 $etype = 65280;
3679 $logentry .= "Merged A record with existing PTR record '$host A+PTR $val', TTL $ettl";
3680 }
3681 if ($type eq 'AAAA') { # PTR -> AAAA+PTR
3682 $etype = 65281;
3683 $logentry .= "Merged AAAA record with existing PTR record '$host AAAA+PTR $val', TTL $ettl";
3684 }
3685 $ettl = ($ettl < $ttl ? $ettl : $ttl); # use lower TTL
3686 $dbh->do("UPDATE records SET domain_id=?,ttl=?,type=? WHERE record_id=?", undef,
3687 ($domain_id, $ettl, $etype, $erid));
3688 $nrecs++;
3689 _log($dbh, (group_id => $group, domain_id => $domain_id, rdns_id => $erdns, entry => $logentry) );
3690 next; # while axfr_next
3691 }
3692 } # $rev eq 'n'
3693 else {
3694 # importing a revzone, we have PTR records that could be merged with matching A/AAAA records
3695 my ($domid,$erid,$ettl,$etype) = $dbh->selectrow_array("SELECT domain_id,record_id,ttl,type FROM records ".
3696 "WHERE host=? AND val=? AND (type=1 OR type=28)",
3697 undef, ($host, $val) );
3698 if ($erid) {
3699 if ($etype == 1) { # A -> A+PTR
3700 $etype = 65280;
3701 $logentry .= "Merged PTR record with existing matching A record '$host A+PTR $val', TTL $ettl";
3702 }
3703 if ($etype == 28) { # AAAA -> AAAA+PTR
3704 $etype = 65281;
3705 $logentry .= "Merged PTR record with existing matching AAAA record '$host AAAA+PTR $val', TTL $ettl";
3706 }
3707 $ettl = ($ettl < $ttl ? $ettl : $ttl); # use lower TTL
3708 $dbh->do("UPDATE records SET rdns_id=?,ttl=?,type=? WHERE record_id=?", undef,
3709 ($rdns_id, $ettl, $etype, $erid));
3710 $nrecs++;
3711 _log($dbh, (group_id => $group, domain_id => $domid, rdns_id => $rdns_id, entry => $logentry) );
3712 next; # while axfr_next
3713 }
3714 } # $rev eq 'y'
3715 } # if $merge
3716
3717 # Insert the new record
3718 $sth->execute($domain_id, $rdns_id, $host, $reverse_typemap{$type}, $val,
3719 $distance, $weight, $port, $ttl);
3720
3721 $nrecs++;
3722
3723 if ($type eq 'SOA') {
3724 # also !$rwsoa, but if that's set, it should be impossible to get here.
3725 my @tmp1 = split /:/, $host;
3726 my @tmp2 = split /:/, $val;
3727 $logentry .= "Added SOA record [contact $tmp1[0]] [master $tmp1[1]] ".
3728 "[refresh $tmp2[0]] [retry $tmp2[1]] [expire $tmp2[2]] [minttl $tmp2[3]], TTL $ttl";
3729 } elsif ($logfrag) {
3730 # special case for log entries we need to meddle with a little.
3731 $logentry .= $logfrag;
3732 } else {
3733 $logentry .= "Added record '$host $type";
3734 $logentry .= " [distance $distance]" if $type eq 'MX';
3735 $logentry .= " [priority $distance] [weight $weight] [port $port]" if $type eq 'SRV';
3736 $logentry .= " $val', TTL $ttl";
3737 }
3738 _log($dbh, (group_id => $group, domain_id => $domain_id, rdns_id => $rdns_id, entry => $logentry) );
3739
3740 } # while axfr_next
3741
3742# Detect and handle delegated subzones
3743# Placeholder for when we decide what to actually do with this, see previous comments in NS and CNAME handling.
3744#foreach (keys %suboct) {
3745# print "found ".($suboct{$_}{ns} ? @{$suboct{$_}{ns}} : '0')." NS records and ".
3746# ($suboct{$_}{cname} ? @{$suboct{$_}{cname}} : '0')." CNAMEs for $_\n";
3747#}
3748
3749 # Overwrite SOA record
3750 if ($rwsoa) {
3751 $soaflag = 1;
3752 my $sthgetsoa = $dbh->prepare("SELECT host,val,ttl FROM default_records WHERE group_id=? AND type=?");
3753 my $sthputsoa = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,ttl) VALUES (?,?,?,?,?)");
3754 $sthgetsoa->execute($group,$reverse_typemap{SOA});
3755 while (my ($host,$val,$ttl) = $sthgetsoa->fetchrow_array()) {
3756 $host =~ s/DOMAIN/$zone/g;
3757 $val =~ s/DOMAIN/$zone/g;
3758 $sthputsoa->execute($zone_id,$host,$reverse_typemap{SOA},$val,$ttl);
3759 }
3760 }
3761
3762 # Overwrite NS records
3763 if ($rwns) {
3764 $nsflag = 1;
3765 my $sthgetns = $dbh->prepare("SELECT host,val,ttl FROM default_records WHERE group_id=? AND type=?");
3766 my $sthputns = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,ttl) VALUES (?,?,?,?,?)");
3767 $sthgetns->execute($group,$reverse_typemap{NS});
3768 while (my ($host,$val,$ttl) = $sthgetns->fetchrow_array()) {
3769 $host =~ s/DOMAIN/$zone/g;
3770 $val =~ s/DOMAIN/$zone/g;
3771 $sthputns->execute($zone_id,$host,$reverse_typemap{NS},$val,$ttl);
3772 }
3773 }
3774
3775 die "No records found; either $ifrom is not authoritative or doesn't allow transfers\n" if !$nrecs;
3776 die "Bad zone: No SOA record!\n" if !$soaflag;
3777 die "Bad zone: No NS records!\n" if !$nsflag;
3778
3779 $dbh->commit;
3780
3781 };
3782
3783 if ($@) {
3784 my $msg = $@;
3785 eval { $dbh->rollback; };
3786 return ('FAIL',$msg." $warnmsg");
3787 } else {
3788 return ('WARN', $warnmsg) if $warnmsg;
3789 return ('OK',"Imported OK");
3790 }
3791
3792 # it should be impossible to get here.
3793 return ('WARN',"OOOK!");
3794} # end importAXFR()
3795
3796
3797## DNSDB::importBIND()
3798sub importBIND {
3799} # end importBIND()
3800
3801
3802## DNSDB::import_tinydns()
3803sub import_tinydns {
3804} # end import_tinydns()
3805
3806
3807## DNSDB::export()
3808# Export the DNS database, or a part of it
3809# Takes database handle, export type, optional arguments depending on type
3810# Writes zone data to targets as appropriate for type
3811sub export {
3812 my $dbh = shift;
3813 my $target = shift;
3814
3815 if ($target eq 'tiny') {
3816 __export_tiny($dbh,@_);
3817 }
3818# elsif ($target eq 'foo') {
3819# __export_foo($dbh,@_);
3820#}
3821# etc
3822
3823} # end export()
3824
3825
3826## DNSDB::__export_tiny
3827# Internal sub to implement tinyDNS (compatible) export
3828# Takes database handle, filehandle to write export to, optional argument(s)
3829# to determine which data gets exported
3830sub __export_tiny {
3831 my $dbh = shift;
3832 my $datafile = shift;
3833
3834##fixme: slurp up further options to specify particular zone(s) to export
3835
3836 ## Convert a bare number into an octal-coded pair of octets.
3837 # Take optional arg to indicate a decimal or hex input. Defaults to hex.
3838 sub octalize {
3839 my $tmp = shift;
3840 my $srctype = shift || 'h'; # default assumes hex string
3841 $tmp = sprintf "%0.4x", hex($tmp) if $srctype eq 'h'; # 0-pad hex to 4 digits
3842 $tmp = sprintf "%0.4x", $tmp if $srctype eq 'd'; # 0-pad decimal to 4 hex digits
3843 my @o = ($tmp =~ /^(..)(..)$/); # split into octets
3844 return sprintf "\\%0.3o\\%0.3o", hex($o[0]), hex($o[1]);;
3845 }
3846
3847##fixme: fail if $datafile isn't an open, writable file
3848
3849 # easy case - export all evarything
3850 # not-so-easy case - export item(s) specified
3851 # todo: figure out what kind of list we use to export items
3852
3853 my $domsth = $dbh->prepare("SELECT domain_id,domain,status FROM domains WHERE status=1");
3854 my $recsth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl ".
3855 "FROM records WHERE domain_id=?");
3856 $domsth->execute();
3857 while (my ($domid,$dom,$domstat) = $domsth->fetchrow_array) {
3858 $recsth->execute($domid);
3859 while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $recsth->fetchrow_array) {
3860##fixme: need to store location in the db, and retrieve it here.
3861# temporarily hardcoded to empty so we can include it further down.
3862my $loc = '';
3863
3864##fixme: record validity timestamp. tinydns supports fiddling with timestamps.
3865# note $ttl must be set to 0 if we want to use tinydns's auto-expiring timestamps.
3866# timestamps are TAI64
3867# ~~ 2^62 + time()
3868my $stamp = '';
3869
3870# raw packet in unknown format: first byte indicates length
3871# of remaining data, allows up to 255 raw bytes
3872
3873##fixme? append . to all host/val hostnames
3874 if ($typemap{$type} eq 'SOA') {
3875
3876 # host contains pri-ns:responsible
3877 # val is abused to contain refresh:retry:expire:minttl
3878##fixme: "manual" serial vs tinydns-autoserial
3879 # let's be explicit about abusing $host and $val
3880 my ($email, $primary) = (split /:/, $host)[0,1];
3881 my ($refresh, $retry, $expire, $min_ttl) = (split /:/, $val)[0,1,2,3];
3882 print $datafile "Z$dom:$primary:$email"."::$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n";
3883
3884 } elsif ($typemap{$type} eq 'A') {
3885
3886 print $datafile "+$host:$val:$ttl:$stamp:$loc\n";
3887
3888 } elsif ($typemap{$type} eq 'NS') {
3889
3890 print $datafile "\&$host"."::$val:$ttl:$stamp:$loc\n";
3891
3892 } elsif ($typemap{$type} eq 'AAAA') {
3893
3894 print $datafile ":$host:28:";
3895 my $altgrp = 0;
3896 my @altconv;
3897 # Split in to up to 8 groups of hex digits (allows for IPv6 :: 0-collapsing)
3898 foreach (split /:/, $val) {
3899 if (/^$/) {
3900 # flag blank entry; this is a series of 0's of (currently) unknown length
3901 $altconv[$altgrp++] = 's';
3902 } else {
3903 # call sub to convert 1-4 hex digits to 2 string-rep octal bytes
3904 $altconv[$altgrp++] = octalize($_)
3905 }
3906 }
3907 foreach my $octet (@altconv) {
3908 # if not 's', output
3909 print $datafile $octet unless $octet =~ /^s$/;
3910 # if 's', output (9-array length)x literal '\000\000'
3911 print $datafile '\000\000'x(9-$altgrp) if $octet =~ /^s$/;
3912 }
3913 print $datafile ":$ttl:$stamp:$loc\n";
3914
3915 } elsif ($typemap{$type} eq 'MX') {
3916
3917 print $datafile "\@$host"."::$val:$dist:$ttl:$stamp:$loc\n";
3918
3919 } elsif ($typemap{$type} eq 'TXT') {
3920
3921##fixme: split v-e-r-y long TXT strings? will need to do so for BIND export, at least
3922 $val =~ s/:/\\072/g; # may need to replace other symbols
3923 print $datafile "'$host:$val:$ttl:$stamp:$loc\n";
3924
3925# by-hand TXT
3926#:deepnet.cx:16:2v\075spf1\040a\040a\072bacon.deepnet.cx\040a\072home.deepnet.cx\040-all:3600
3927#@ IN TXT "v=spf1 a a:bacon.deepnet.cx a:home.deepnet.cx -all"
3928#'deepnet.cx:v=spf1 a a\072bacon.deepnet.cx a\072home.deepnet.cx -all:3600
3929
3930#txttest IN TXT "v=foo bar:bob kn;ob' \" !@#$%^&*()-=_+[]{}<>?"
3931#:txttest.deepnet.cx:16:\054v\075foo\040bar\072bob\040kn\073ob\047\040\042\040\041\100\043\044\045\136\046\052\050\051-\075\137\053\133\135\173\175\074\076\077:3600
3932
3933# very long TXT record as brought in by axfr-get
3934# note tinydns does not support >512-byte RR data, need axfr-dns (for TCP support) for that
3935# also note, tinydns does not seem to support <512, >256-byte RRdata from axfr-get either. :/
3936#:longtxt.deepnet.cx:16:
3937#\170this is a very long txt record. it is really long. long. very long. really very long. this is a very long txt record.
3938#\263 it is really long. long. very long. really very long. this is a very long txt record. it is really long. long. very long. really very long. this is a very long txt record.
3939#\351 it is really long. long. very long. really very long.this is a very long txt record. it is really long. long. very long. really very long. this is a very long txt record. it is really long. long. very long. really very long.
3940#:3600
3941
3942 } elsif ($typemap{$type} eq 'CNAME') {
3943
3944 print $datafile "C$host:$val:$ttl:$stamp:$loc\n";
3945
3946 } elsif ($typemap{$type} eq 'SRV') {
3947
3948 # data is two-byte values for priority, weight, port, in that order,
3949 # followed by length/string data
3950
3951 print $datafile ":$host:33:".octalize($dist,'d').octalize($weight,'d').octalize($port,'d');
3952
3953 $val .= '.' if $val !~ /\.$/;
3954 foreach (split /\./, $val) {
3955 printf $datafile "\\%0.3o%s", length($_), $_;
3956 }
3957 print $datafile "\\000:$ttl:$stamp:$loc\n";
3958
3959 } elsif ($typemap{$type} eq 'RP') {
3960
3961 # RP consists of two mostly free-form strings.
3962 # The first is supposed to be an email address with @ replaced by . (as with the SOA contact)
3963 # The second is the "hostname" of a TXT record with more info.
3964 print $datafile ":$host:17:";
3965 my ($who,$what) = split /\s/, $val;
3966 foreach (split /\./, $who) {
3967 printf $datafile "\\%0.3o%s", length($_), $_;
3968 }
3969 print $datafile '\000';
3970 foreach (split /\./, $what) {
3971 printf $datafile "\\%0.3o%s", length($_), $_;
3972 }
3973 print $datafile "\\000:$ttl:$stamp:$loc\n";
3974
3975 } elsif ($typemap{$type} eq 'PTR') {
3976
3977 # must handle both IPv4 and IPv6
3978##work
3979 # data should already be in suitable reverse order.
3980 print $datafile "^$host:$val:$ttl:$stamp:$loc\n";
3981
3982 } else {
3983 # raw record. we don't know what's in here, so we ASS-U-ME the user has
3984 # put it in correctly, since either the user is messing directly with the
3985 # database, or the record was imported via AXFR
3986 # <split by char>
3987 # convert anything not a-zA-Z0-9.- to octal coding
3988
3989##fixme: add flag to export "unknown" record types - note we'll probably end up
3990# mangling them since they were written to the DB from Net::DNS::RR::<type>->rdatastr.
3991 #print $datafile ":$host:$type:$val:$ttl:$stamp:$loc\n";
3992
3993 } # record type if-else
3994
3995 } # while ($recsth)
3996 } # while ($domsth)
3997} # end __export_tiny()
3998
3999
4000## DNSDB::mailNotify()
4001# Sends notification mail to recipients regarding a DNSDB operation
4002sub mailNotify {
4003 my $dbh = shift;
4004 my ($subj,$message) = @_;
4005
4006 return if $config{mailhost} eq 'smtp.example.com'; # do nothing if still using default SMTP host.
4007
4008 my $mailer = Net::SMTP->new($config{mailhost}, Hello => "dnsadmin.$config{domain}");
4009
4010 my $mailsender = ($config{mailsender} ? $config{mailsender} : $config{mailnotify});
4011
4012 $mailer->mail($mailsender);
4013 $mailer->to($config{mailnotify});
4014 $mailer->data("From: \"$config{mailname}\" <$mailsender>\n",
4015 "To: <$config{mailnotify}>\n",
4016 "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
4017 "Subject: $subj\n",
4018 "X-Mailer: DNSAdmin Notify v".sprintf("%.1d",$DNSDB::VERSION)."\n",
4019 "Organization: $config{orgname}\n",
4020 "\n$message\n");
4021 $mailer->quit;
4022}
4023
4024# shut Perl up
40251;
Note: See TracBrowser for help on using the repository browser.