source: trunk/DNSDB.pm@ 20

Last change on this file since 20 was 20, checked in by Kris Deugau, 15 years ago

/trunk

checkpoint, group mangling almost complete
also normalized *most* group* refs to use "group*" vs "grp*"

  • Property svn:keywords set to Date Rev Author Id
File size: 17.7 KB
Line 
1# dns/trunk/DNSDB.pm
2# Abstraction functions for DNS administration
3###
4# SVN revision info
5# $Date: 2009-10-15 17:23:51 +0000 (Thu, 15 Oct 2009) $
6# SVN revision $Rev: 20 $
7# Last update by $Author: kdeugau $
8###
9# Copyright (C) 2008 - Kris Deugau <kdeugau@deepnet.cx>
10
11package DNSDB;
12
13use strict;
14use warnings;
15use Exporter;
16use DBI;
17#use Net::SMTP;
18#use NetAddr::IP qw( Compact );
19#use POSIX;
20use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
21
22$VERSION = 0.1;
23@ISA = qw(Exporter);
24@EXPORT_OK = qw(
25 &initGlobals &connectDB &finish &addDomain &delDomain &domainName &addGroup &getChildren &groupName
26 &getSOA &getRecLine &getDomRecs &addRec &updateRec &delRec &domStatus
27 %typemap %reverse_typemap
28 );
29
30@EXPORT = (); # Export nothing by default.
31%EXPORT_TAGS = ( ALL => [qw(
32 &initGlobals &connectDB &finish &addDomain &delDomain &domainName &addGroup &getChildren
33 &groupName &getSOA &getRecLine &getDomRecs &addRec &updateRec &delRec &domStatus
34 %typemap %reverse_typemap
35 )]
36 );
37
38our $group = 1;
39our $errstr = '';
40
41# Halfway sane defaults for SOA, TTL, etc.
42our %def = qw (
43 contact hostmaster.DOMAIN
44 prins ns1.myserver.com
45 soattl 86400
46 refresh 10800
47 retry 3600
48 expire 604800
49 minttl 10800
50 ttl 10800
51);
52
53# DNS record type map and reverse map.
54# loaded from the database, from http://www.iana.org/assignments/dns-parameters
55our %typemap;
56our %reverse_typemap;
57
58##
59## Initialization and cleanup subs
60##
61
62## DNSDB::connectDB()
63# Creates connection to DNS database.
64# Requires the database name, username, and password.
65# Returns a handle to the db.
66# Set up for a PostgreSQL db; could be any transactional DBMS with the
67# right changes.
68sub connectDB {
69 $errstr = '';
70 my $dbname = shift;
71 my $user = shift;
72 my $pass = shift;
73 my $dbh;
74 my $DSN = "DBI:Pg:dbname=$dbname";
75
76 my $host = shift;
77 $DSN .= ";host=$host" if $host;
78
79# Note that we want to autocommit by default, and we will turn it off locally as necessary.
80# We may not want to print gobbledygook errors; YMMV. Have to ponder that further.
81 $dbh = DBI->connect($DSN, $user, $pass, {
82 AutoCommit => 1,
83 PrintError => 0
84 })
85 or return (undef, $DBI::errstr) if(!$dbh);
86
87# Return here if we can't select. Note that this indicates a
88# problem executing the select.
89 my $sth = $dbh->prepare("select group_id from groups limit 1");
90 $sth->execute();
91 return (undef,$DBI::errstr) if ($sth->err);
92
93# See if the select returned anything (or null data). This should
94# succeed if the select executed, but...
95 $sth->fetchrow();
96 return (undef,$DBI::errstr) if ($sth->err);
97
98 $sth->finish;
99
100# If we get here, we should be OK.
101 return ($dbh,"DB connection OK");
102} # end connectDB
103
104
105## DNSDB::finish()
106# Cleans up after database handles and so on.
107# Requires a database handle
108sub finish {
109 my $dbh = $_[0];
110 $dbh->disconnect;
111} # end finish
112
113
114## DNSDB::initGlobals()
115# Initialize global variables
116# NB: this does NOT include web-specific session variables!
117# Requires a database handle
118sub initGlobals {
119 my $dbh = shift;
120
121# load system-wide site defaults and things from config file
122 open SYSDEFAULTS, "</etc/dnsdb.conf";
123##fixme - error check!
124 while (<SYSDEFAULTS>) {
125 next if /^\s*#/;
126 $def{contact} = $1 if /contact ?= ?([a-z0-9_.-]+)/i;
127 $def{prins} = $1 if /prins ?= ?([a-z0-9_.-]+)/i;
128 $def{soattl} = $1 if /soattl ?= ?([a-z0-9_.-]+)/i;
129 $def{refresh} = $1 if /refresh ?= ?([a-z0-9_.-]+)/i;
130 $def{retry} = $1 if /retry ?= ?([a-z0-9_.-]+)/i;
131 $def{expire} = $1 if /expire ?= ?([a-z0-9_.-]+)/i;
132 $def{minttl} = $1 if /minttl ?= ?([a-z0-9_.-]+)/i;
133 $def{ttl} = $1 if /ttl ?= ?([a-z0-9_.-]+)/i;
134##fixme? load DB user/pass from config file?
135 }
136# load from database
137 my $sth = $dbh->prepare("select val,name from rectypes");
138 $sth->execute;
139 while (my ($recval,$recname) = $sth->fetchrow_array()) {
140 $typemap{$recval} = $recname;
141 $reverse_typemap{$recname} = $recval;
142 }
143} # end initGlobals
144
145
146##
147## Processing subs
148##
149
150## DNSDB::addDomain()
151# Add a domain
152# Takes a database handle, domain name, numeric group, and boolean(ish) state (active/inactive)
153# Returns a status code and message
154sub addDomain {
155 $errstr = '';
156 my $dbh = shift;
157 return ('FAIL',"Need database handle") if !$dbh;
158 my $domain = shift;
159 return ('FAIL',"Need domain") if !defined($domain);
160 my $group = shift;
161 return ('FAIL',"Need group") if !defined($group);
162 my $state = shift;
163 return ('FAIL',"Need domain status") if !defined($state);
164
165 my $dom_id;
166
167 # Allow transactions, and raise an exception on errors so we can catch it later.
168 # Use local to make sure these get "reset" properly on exiting this block
169 local $dbh->{AutoCommit} = 0;
170 local $dbh->{RaiseError} = 1;
171
172 # Wrap all the SQL in a transaction
173 eval {
174 # insert the domain...
175 my $sth = $dbh->prepare("insert into domains (domain,group_id,status) values (?,?,?)");
176 $sth->execute($domain,$group,$state);
177
178 # get the ID...
179 $sth = $dbh->prepare("select domain_id from domains where domain='$domain'");
180 $sth->execute;
181 ($dom_id) = $sth->fetchrow_array();
182
183 # ... and now we construct the standard records from the default set. NB: group should be variable.
184 $sth = $dbh->prepare("select host,type,val,distance,weight,port,ttl from default_records where group_id=$group");
185 my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,weight,port,ttl)".
186 " values ($dom_id,?,?,?,?,?,?,?)");
187 $sth->execute;
188 while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) {
189 $host =~ s/DOMAIN/$domain/g;
190 $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl);
191 }
192
193 # once we get here, we should have suceeded.
194 $dbh->commit;
195 }; # end eval
196
197 if ($@) {
198 my $msg = $@;
199 eval { $dbh->rollback; };
200 return ('FAIL',$msg);
201 } else {
202 return ('OK',$dom_id);
203 }
204} # end addDomain
205
206
207## DNSDB::delDomain()
208# Delete a domain.
209# for now, just delete the records, then the domain.
210# later we may want to archive it in some way instead (status code 2, for example?)
211sub delDomain {
212 my $dbh = shift;
213 my $domid = shift;
214
215 # Allow transactions, and raise an exception on errors so we can catch it later.
216 # Use local to make sure these get "reset" properly on exiting this block
217 local $dbh->{AutoCommit} = 0;
218 local $dbh->{RaiseError} = 1;
219
220 # Wrap all the SQL in a transaction
221 eval {
222 my $sth = $dbh->prepare("delete from records where domain_id=?");
223 $sth->execute($domid);
224 $sth = $dbh->prepare("delete from domains where domain_id=?");
225 $sth->execute($domid);
226
227 # once we get here, we should have suceeded.
228 $dbh->commit;
229 }; # end eval
230
231 if ($@) {
232 my $msg = $@;
233 eval { $dbh->rollback; };
234 return ('FAIL',$msg);
235 } else {
236 return ('OK','OK');
237 }
238
239} # end delDomain()
240
241
242## DNSDB::domainName()
243# Return the domain name based on a domain ID
244# Takes a database handle and the domain ID
245# Returns the domain name or undef on failure
246sub domainName {
247 $errstr = '';
248 my $dbh = shift;
249 my $domid = shift;
250 my $sth = $dbh->prepare("select domain from domains where domain_id=?");
251 $sth->execute($domid);
252 my ($domname) = $sth->fetchrow_array();
253 $errstr = $DBI::errstr if !$domname;
254 return $domname if $domname;
255} # end domainName
256
257
258## DNSDB::addGroup()
259# Add a group
260# Takes a database handle, group name, parent group, and template-vs-cloneme flag
261# Returns a status code and message
262sub addGroup {
263 $errstr = '';
264 my $dbh = shift;
265 my $groupname = shift;
266 my $pargroup = shift;
267
268 # 0 indicates "template", hardcoded.
269 # Any other value clones that group's default records, if it exists.
270 my $torc = shift || 0;
271
272 # Allow transactions, and raise an exception on errors so we can catch it later.
273 # Use local to make sure these get "reset" properly on exiting this block
274 local $dbh->{AutoCommit} = 0;
275 local $dbh->{RaiseError} = 1;
276
277 # Wrap all the SQL in a transaction
278 eval {
279 my $sth = $dbh->prepare("INSERT INTO groups (parent_group_id,group_name) VALUES (?,?)");
280 $sth->execute($pargroup,$groupname);
281
282 $sth = $dbh->prepare("SELECT group_id FROM groups WHERE group_name=?");
283 $sth->execute($groupname);
284 my ($groupid) = $sth->fetchrow_array();
285
286 $sth = $dbh->prepare("INSERT INTO default_records (group_id,host,type,val,distance,weight,port,ttl) ".
287 "VALUES ($groupid,?,?,?,?,?,?,?)");
288 if ($torc) {
289 my $sth2 = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM default_records WHERE group_id=?");
290 while (my @clonedata = $sth2->fetchrow_array) {
291 $sth->execute(@clonedata);
292 }
293 } else {
294 # reasonable basic defaults for SOA, MX, NS, and minimal hosting
295 # could load from a config file, but somewhere along the line we need hardcoded bits.
296 $sth->execute('ns1.example.com:hostmaster.example.com', 6, '10800:3600:604800:10800', 0, 0, 0, 86400);
297 $sth->execute('DOMAIN', 1, '192.168.4.2', 0, 0, 0, 7200);
298 $sth->execute('DOMAIN', 15, 'mx.example.com', 10, 0, 0, 7200);
299 $sth->execute('DOMAIN', 2, 'ns1.example.com', 0, 0, 0, 7200);
300 $sth->execute('DOMAIN', 2, 'ns2.example.com', 0, 0, 0, 7200);
301 $sth->execute('www.DOMAIN', 5, 'DOMAIN', 0, 0, 0, 7200);
302 }
303
304die "epic FAIL.. hahahah, just testing!\n";
305 # once we get here, we should have suceeded.
306 $dbh->commit;
307 }; # end eval
308
309 if ($@) {
310 my $msg = $@;
311 eval { $dbh->rollback; };
312 return ('FAIL',$msg);
313 } else {
314 return ('OK','OK');
315 }
316
317} # end addGroup()
318
319
320## DNSDB::getChildren()
321# Get a list of all groups whose parent^n is group <n>
322# Takes a database handle, group ID, and reference to an array to put the group IDs in
323# Calls itself
324sub getChildren {
325 $errstr = '';
326 my $dbh = shift;
327 my $rootgroup = shift;
328 my $groupdest = shift;
329
330 # special break for default group; otherwise we get stuck.
331 if ($rootgroup == 1) {
332 # by definition, group 1 is the Root Of All Groups
333 my $sth = $dbh->prepare("SELECT group_id FROM groups");
334 $sth->execute;
335 my @grouplist;
336 while (my @this = $sth->fetchrow_array) {
337 push @$groupdest, @this;
338 }
339 } else {
340 my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE parent_group_id=?");
341 $sth->execute($rootgroup);
342 return if $sth->rows == 0;
343 my @grouplist;
344 while (my ($group) = $sth->fetchrow_array) {
345 push @$groupdest, $group;
346 getChildren($dbh,$group,$groupdest);
347 }
348 }
349} # end getChildren()
350
351
352## DNSDB::groupName()
353# Return the group name based on a group ID
354# Takes a database handle and the group ID
355# Returns the group name or undef on failure
356sub groupName {
357 $errstr = '';
358 my $dbh = shift;
359 my $groupid = shift;
360 my $sth = $dbh->prepare("SELECT group_name FROM groups WHERE group_id=?");
361 $sth->execute($groupid);
362 my ($groupname) = $sth->fetchrow_array();
363 $errstr = $DBI::errstr if !$groupname;
364 return $groupname if $groupname;
365} # end groupName
366
367
368## DNSDB::editRecord()
369# Change an existing record
370# Takes a database handle, default/live flag, record ID, and new data and updates the data fields for it
371sub editRecord {
372 $errstr = '';
373 my $dbh = shift;
374 my $defflag = shift;
375 my $recid = shift;
376 my $host = shift;
377 my $address = shift;
378 my $distance = shift;
379 my $weight = shift;
380 my $port = shift;
381 my $ttl = shift;
382}
383
384
385## DNSDB::getSOA()
386# Return all suitable fields from an SOA record in separate elements of a hash
387# Takes a database handle, default/live flag, and group (default) or domain (live) ID
388sub getSOA {
389 $errstr = '';
390 my $dbh = shift;
391 my $def = shift;
392 my $id = shift;
393 my %ret;
394
395 my $sql = "select record_id,host,val,ttl from";
396 if ($def eq 'def' or $def eq 'y') {
397 $sql .= " default_records where group_id=$id and type=$reverse_typemap{SOA}";
398 } else {
399 # we're editing a live SOA record; find based on domain
400 $sql .= " records where domain_id=$id and type=$reverse_typemap{SOA}";
401 }
402#print "getSOA DEBUG: $sql<br>\n";
403 my $sth = $dbh->prepare($sql);
404 $sth->execute;
405
406 my ($recid,$host,$val,$ttl) = $sth->fetchrow_array();
407 my ($prins,$contact) = split /:/, $host;
408 my ($refresh,$retry,$expire,$minttl) = split /:/, $val;
409
410 $ret{recid} = $recid;
411 $ret{ttl} = $ttl;
412 $ret{prins} = $prins;
413 $ret{contact} = $contact;
414 $ret{refresh} = $refresh;
415 $ret{retry} = $retry;
416 $ret{expire} = $expire;
417 $ret{minttl} = $minttl;
418
419 return %ret;
420} # end getSOA()
421
422
423## DNSDB::getRecLine()
424# Return all data fields for a zone record in separate elements of a hash
425# Takes a database handle, default/live flag, and record ID
426sub getRecLine {
427 $errstr = '';
428 my $dbh = shift;
429 my $def = shift;
430 my $id = shift;
431
432 my $sql = "select record_id,host,type,val,distance,weight,port,ttl from ".
433 (($def eq 'def' or $def eq 'y') ? 'default_' : '').
434 "records where record_id=$id";
435print "MDEBUG: $sql<br>\n";
436 my $sth = $dbh->prepare($sql);
437 $sth->execute;
438
439 my ($recid,$host,$rtype,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array();
440
441 if ($sth->err) {
442 $errstr = $DBI::errstr;
443 return undef;
444 }
445 my %ret;
446 $ret{recid} = $recid;
447 $ret{host} = $host;
448 $ret{type} = $rtype;
449 $ret{val} = $val;
450 $ret{distance}= $distance;
451 $ret{weight} = $weight;
452 $ret{port} = $port;
453 $ret{ttl} = $ttl;
454
455 return %ret;
456}
457
458
459##fixme: should use above (getRecLine()) to get lines for below?
460## DNSDB::getDomRecs()
461# Return records for a domain
462# Takes a database handle, default/live flag, group/domain ID, start,
463# number of records, sort field, and sort order
464# Returns a reference to an array of hashes
465sub getDomRecs {
466 $errstr = '';
467 my $dbh = shift;
468 my $type = shift;
469 my $id = shift;
470 my $nrecs = shift || 'all';
471 my $nstart = shift || 0;
472
473## for order, need to map input to column names
474 my $order = shift || 'host';
475
476 my $sql = "select record_id,host,type,val,distance,weight,port,ttl from";
477 if ($type eq 'def' or $type eq 'y') {
478 $sql .= " default_records where group_id=$id";
479 } else {
480 $sql .= " records where domain_id=$id";
481 }
482 $sql .= " and not type=$reverse_typemap{SOA} order by $order";
483##fixme: need to set nstart properly (offset is not internally multiplied with limit)
484 $sql .= " limit $nrecs offset ".($nstart*$nrecs) if $nstart ne 'all';
485
486 my $sth = $dbh->prepare($sql);
487 $sth->execute;
488
489 my @retbase;
490 while (my $ref = $sth->fetchrow_hashref()) {
491 push @retbase, $ref;
492 }
493
494 my $ret = \@retbase;
495 return $ret;
496} # end getDomRecs()
497
498
499## DNSDB::addRec()
500# Add a new record to a domain or a group's default records
501# Takes a database handle, default/live flag, group/domain ID,
502# host, type, value, and TTL
503# Some types require additional detail: "distance" for MX and SRV,
504# and weight/port for SRV
505# Returns a status code and detail message in case of error
506sub addRec {
507 $errstr = '';
508 my $dbh = shift;
509 my $defrec = shift;
510 my $id = shift;
511
512 my $host = shift;
513 my $rectype = shift;
514 my $val = shift;
515 my $ttl = shift;
516
517 my $fields = ($defrec eq 'y' ? 'group_id' : 'domain_id').",host,type,val,ttl";
518 my $vallist = "$id,'$host',$rectype,'$val',$ttl";
519
520 my $dist;
521 if ($rectype == $reverse_typemap{MX} or $rectype == $reverse_typemap{SRV}) {
522 $dist = shift;
523 return ('FAIL',"Need distance for $typemap{$rectype} record") if !defined($dist);
524 $fields .= ",distance";
525 $vallist .= ",$dist";
526 }
527 my $weight;
528 my $port;
529 if ($rectype == $reverse_typemap{SRV}) {
530 $weight = shift;
531 $port = shift;
532 return ('FAIL',"Need weight and port for SRV record") if !defined($weight) or !defined($port);
533 $fields .= ",weight,port";
534 $vallist .= ",$weight,$port";
535 }
536
537 my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallist)";
538# something is bugging me about this...
539#warn "DEBUG: $sql";
540 my $sth = $dbh->prepare($sql);
541 $sth->execute;
542
543 return ('FAIL',$sth->errstr) if $sth->err;
544
545 return ('OK','OK');
546} # end addRec()
547
548
549## DNSDB::updateRec()
550# Update a record
551sub updateRec {
552 $errstr = '';
553
554 my $dbh = shift;
555 my $defrec = shift;
556 my $id = shift;
557
558# all records have these
559 my $host = shift;
560 my $type = shift;
561 my $val = shift;
562 my $ttl = shift;
563
564 return('FAIL',"Missing standard argument(s)") if !defined($ttl);
565
566# only MX and SRV will use these
567 my $dist = 0;
568 my $weight = 0;
569 my $port = 0;
570
571 if ($type == $reverse_typemap{MX} || $type == $reverse_typemap{SRV}) {
572 $dist = shift;
573 return ('FAIL',"MX or SRV requires distance") if !defined($dist);
574 if ($type == $reverse_typemap{SRV}) {
575 $weight = shift;
576 return ('FAIL',"SRV requires weight") if !defined($weight);
577 $port = shift;
578 return ('FAIL',"SRV requires port") if !defined($port);
579 }
580 }
581
582 my $sth = $dbh->prepare("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records ".
583 "SET host=?,type=?,val=?,ttl=?,distance=?,weight=?,port=? ".
584 "WHERE record_id=?");
585 $sth->execute($host,$type,$val,$ttl,$dist,$weight,$port,$id);
586
587 return ('FAIL',$sth->errstr."<br>\n$errstr<br>\n") if $sth->err;
588
589 return ('OK','OK');
590} # end updateRec()
591
592
593## DNSDB::delRec()
594# Delete a record.
595sub delRec {
596 $errstr = '';
597 my $dbh = shift;
598 my $defrec = shift;
599 my $id = shift;
600
601 my $sth = $dbh->prepare("delete from ".($defrec eq 'y' ? 'default_' : '')."records where record_id=?");
602 $sth->execute($id);
603
604 return ('FAIL',$sth->errstr) if $sth->err;
605
606 return ('OK','OK');
607} # end delRec()
608
609
610## DNSDB::domStatus()
611# Sets and/or returns a domain's status
612# Takes a database handle, domain ID and optionally a status argument
613# Returns undef on errors.
614sub domStatus {
615 my $dbh = shift;
616 my $id = shift;
617 my $newstatus = shift;
618
619 return undef if $id !~ /^\d+$/;
620
621 my $sth;
622
623# ooo, fun! let's see what we were passed for status
624 if ($newstatus) {
625 $sth = $dbh->prepare("update domains set status=? where domain_id=?");
626 # ass-u-me caller knows what's going on in full
627 if ($newstatus =~ /^[01]$/) { # only two valid for now.
628 $sth->execute($newstatus,$id);
629 } elsif ($newstatus =~ /^domo(?:n|ff)$/) {
630 $sth->execute(($newstatus eq 'domon' ? 1 : 0),$id);
631 }
632 }
633
634 $sth = $dbh->prepare("select status from domains where domain_id=?");
635 $sth->execute($id);
636 my ($status) = $sth->fetchrow_array;
637 return $status;
638} # end domStatus()
639
640
641# shut Perl up
6421;
Note: See TracBrowser for help on using the repository browser.