1 | # dns/trunk/DNSDB.pm
|
---|
2 | # Abstraction functions for DNS administration
|
---|
3 | ###
|
---|
4 | # SVN revision info
|
---|
5 | # $Date: 2009-08-31 20:16:27 +0000 (Mon, 31 Aug 2009) $
|
---|
6 | # SVN revision $Rev: 4 $
|
---|
7 | # Last update by $Author: kdeugau $
|
---|
8 | ###
|
---|
9 | # Copyright (C) 2008 - Kris Deugau <kdeugau@deepnet.cx>
|
---|
10 |
|
---|
11 | package DNSDB;
|
---|
12 |
|
---|
13 | use strict;
|
---|
14 | use warnings;
|
---|
15 | use Exporter;
|
---|
16 | use DBI;
|
---|
17 | #use Net::SMTP;
|
---|
18 | #use NetAddr::IP qw( Compact );
|
---|
19 | #use POSIX;
|
---|
20 | use 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 &getSOA &getRecLine &getDomRecs
|
---|
26 | &addRec &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 &getSOA &getRecLine &getDomRecs
|
---|
33 | &addRec &delRec &domStatus
|
---|
34 | %typemap %reverse_typemap
|
---|
35 | )]
|
---|
36 | );
|
---|
37 |
|
---|
38 | our $group = 1;
|
---|
39 | our $errstr = '';
|
---|
40 |
|
---|
41 | # Halfway sane defaults for SOA, TTL, etc.
|
---|
42 | our %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
|
---|
55 | our %typemap;
|
---|
56 | our %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.
|
---|
68 | sub connectDB {
|
---|
69 | $errstr = '';
|
---|
70 | my ($dbname,$user,$pass) = @_;
|
---|
71 | my $dbh;
|
---|
72 | my $DSN = "DBI:Pg:dbname=$dbname";
|
---|
73 |
|
---|
74 | my $host = shift;
|
---|
75 | $DSN .= ";host=$host" if $host;
|
---|
76 |
|
---|
77 | # Note that we want to autocommit by default, and we will turn it off locally as necessary.
|
---|
78 | # We may not want to print gobbledygook errors; YMMV. Have to ponder that further.
|
---|
79 | $dbh = DBI->connect($DSN, $user, $pass, {
|
---|
80 | AutoCommit => 1,
|
---|
81 | PrintError => 0
|
---|
82 | })
|
---|
83 | or return (undef, $DBI::errstr) if(!$dbh);
|
---|
84 |
|
---|
85 | # Return here if we can't select. Note that this indicates a
|
---|
86 | # problem executing the select.
|
---|
87 | my $sth = $dbh->prepare("select group_id from groups limit 1");
|
---|
88 | $sth->execute();
|
---|
89 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
90 |
|
---|
91 | # See if the select returned anything (or null data). This should
|
---|
92 | # succeed if the select executed, but...
|
---|
93 | $sth->fetchrow();
|
---|
94 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
95 |
|
---|
96 | $sth->finish;
|
---|
97 |
|
---|
98 | # If we get here, we should be OK.
|
---|
99 | return ($dbh,"DB connection OK");
|
---|
100 | } # end connectDB
|
---|
101 |
|
---|
102 |
|
---|
103 | ## DNSDB::finish()
|
---|
104 | # Cleans up after database handles and so on.
|
---|
105 | # Requires a database handle
|
---|
106 | sub finish {
|
---|
107 | my $dbh = $_[0];
|
---|
108 | $dbh->disconnect;
|
---|
109 | } # end finish
|
---|
110 |
|
---|
111 |
|
---|
112 | ## DNSDB::initGlobals()
|
---|
113 | # Initialize global variables
|
---|
114 | # NB: this does NOT include web-specific session variables!
|
---|
115 | # Requires a database handle
|
---|
116 | sub initGlobals {
|
---|
117 | my $dbh = shift;
|
---|
118 |
|
---|
119 | # load system-wide site defaults and things from config file
|
---|
120 | open SYSDEFAULTS, "</etc/dnsdb.conf";
|
---|
121 | ##fixme - error check!
|
---|
122 | while (<SYSDEFAULTS>) {
|
---|
123 | next if /^\s*#/;
|
---|
124 | $def{contact} = $1 if /contact ?= ?([a-z0-9_.-]+)/i;
|
---|
125 | $def{prins} = $1 if /prins ?= ?([a-z0-9_.-]+)/i;
|
---|
126 | $def{soattl} = $1 if /soattl ?= ?([a-z0-9_.-]+)/i;
|
---|
127 | $def{refresh} = $1 if /refresh ?= ?([a-z0-9_.-]+)/i;
|
---|
128 | $def{retry} = $1 if /retry ?= ?([a-z0-9_.-]+)/i;
|
---|
129 | $def{expire} = $1 if /expire ?= ?([a-z0-9_.-]+)/i;
|
---|
130 | $def{minttl} = $1 if /minttl ?= ?([a-z0-9_.-]+)/i;
|
---|
131 | $def{ttl} = $1 if /ttl ?= ?([a-z0-9_.-]+)/i;
|
---|
132 | ##fixme? load DB user/pass from config file?
|
---|
133 | }
|
---|
134 | # load from database
|
---|
135 | my $sth = $dbh->prepare("select val,name from rectypes");
|
---|
136 | $sth->execute;
|
---|
137 | while (my ($recval,$recname) = $sth->fetchrow_array()) {
|
---|
138 | $typemap{$recval} = $recname;
|
---|
139 | $reverse_typemap{$recname} = $recval;
|
---|
140 | }
|
---|
141 | } # end initGlobals
|
---|
142 |
|
---|
143 |
|
---|
144 | ##
|
---|
145 | ## Processing subs
|
---|
146 | ##
|
---|
147 |
|
---|
148 | ## DNSDB::addDomain()
|
---|
149 | # Add a domain
|
---|
150 | # Takes a database handle, domain name, numeric group, and boolean(ish) state (active/inactive)
|
---|
151 | # Returns a status code and message
|
---|
152 | sub addDomain {
|
---|
153 | $errstr = '';
|
---|
154 | my $dbh = shift;
|
---|
155 | return ('FAIL',"Need database handle") if !$dbh;
|
---|
156 | my $domain = shift;
|
---|
157 | return ('FAIL',"Need domain") if !defined($domain);
|
---|
158 | my $group = shift;
|
---|
159 | return ('FAIL',"Need group") if !defined($group);
|
---|
160 | my $state = shift;
|
---|
161 | return ('FAIL',"Need domain status") if !defined($state);
|
---|
162 |
|
---|
163 | my $dom_id;
|
---|
164 |
|
---|
165 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
166 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
167 | local $dbh->{AutoCommit} = 0;
|
---|
168 | local $dbh->{RaiseError} = 1;
|
---|
169 |
|
---|
170 | # Wrap all the SQL in a transaction
|
---|
171 | eval {
|
---|
172 | # insert the domain...
|
---|
173 | my $sth = $dbh->prepare("insert into domains (domain,group_id,status) values (?,?,?)");
|
---|
174 | $sth->execute($domain,$group,$state);
|
---|
175 |
|
---|
176 | # get the ID...
|
---|
177 | $sth = $dbh->prepare("select domain_id from domains where domain='$domain'");
|
---|
178 | $sth->execute;
|
---|
179 | ($dom_id) = $sth->fetchrow_array();
|
---|
180 |
|
---|
181 | # ... and now we construct the standard records from the default set. NB: group should be variable.
|
---|
182 | $sth = $dbh->prepare("select host,type,val,distance,weight,port,ttl from default_records where group_id=$group");
|
---|
183 | my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,weight,port,ttl)".
|
---|
184 | " values ($dom_id,?,?,?,?,?,?,?)");
|
---|
185 | $sth->execute;
|
---|
186 | while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) {
|
---|
187 | $host =~ s/DOMAIN/$domain/g;
|
---|
188 | $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl);
|
---|
189 | }
|
---|
190 |
|
---|
191 | # once we get here, we should have suceeded.
|
---|
192 | $dbh->commit;
|
---|
193 | }; # end eval
|
---|
194 |
|
---|
195 | if ($@) {
|
---|
196 | my $msg = $@;
|
---|
197 | eval { $dbh->rollback; };
|
---|
198 | return ('FAIL',$msg);
|
---|
199 | } else {
|
---|
200 | return ('OK',$dom_id);
|
---|
201 | }
|
---|
202 | } # end addDomain
|
---|
203 |
|
---|
204 |
|
---|
205 | ## DNSDB::delDomain()
|
---|
206 | # Delete a domain.
|
---|
207 | # for now, just delete the records, then the domain.
|
---|
208 | # later we may want to archive it in some way instead (status code 2, for example?)
|
---|
209 | sub delDomain {
|
---|
210 | my $dbh = shift;
|
---|
211 | my $domain = shift;
|
---|
212 |
|
---|
213 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
214 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
215 | local $dbh->{AutoCommit} = 0;
|
---|
216 | local $dbh->{RaiseError} = 1;
|
---|
217 |
|
---|
218 | # Wrap all the SQL in a transaction
|
---|
219 | eval {
|
---|
220 | # brute force.
|
---|
221 | my $sth = $dbh->prepare("select domain_id from domains where domain=?");
|
---|
222 | $sth->execute($domain);
|
---|
223 | die "Domain not found, can't delete\n" if $sth->rows < 1;
|
---|
224 | my ($id) = $sth->fetchrow_array;
|
---|
225 |
|
---|
226 | $sth = $dbh->prepare("delete from records where domain_id=$id");
|
---|
227 | $sth->execute;
|
---|
228 | $sth = $dbh->prepare("delete from domains where domain=?");
|
---|
229 | $sth->execute($domain);
|
---|
230 |
|
---|
231 | # once we get here, we should have suceeded.
|
---|
232 | $dbh->commit;
|
---|
233 | }; # end eval
|
---|
234 |
|
---|
235 | if ($@) {
|
---|
236 | my $msg = $@;
|
---|
237 | eval { $dbh->rollback; };
|
---|
238 | return ('FAIL',$msg);
|
---|
239 | } else {
|
---|
240 | return ('OK','OK');
|
---|
241 | }
|
---|
242 |
|
---|
243 | } # end delDomain()
|
---|
244 |
|
---|
245 |
|
---|
246 | ## DNSDB::domainName()
|
---|
247 | # Return the domain name based on a domain ID
|
---|
248 | # Takes a database handle and the domain ID
|
---|
249 | # Returns the domain name or undef on failure
|
---|
250 | sub domainName {
|
---|
251 | $errstr = '';
|
---|
252 | my $dbh = shift;
|
---|
253 | my $domid = shift;
|
---|
254 | my $sth = $dbh->prepare("select domain from domains where domain_id=?");
|
---|
255 | $sth->execute($domid);
|
---|
256 | my ($domname) = $sth->fetchrow_array();
|
---|
257 | $errstr = $DBI::errstr if !$domname;
|
---|
258 | return $domname if $domname;
|
---|
259 | } # end domainName
|
---|
260 |
|
---|
261 |
|
---|
262 | ## DNSDB::editRecord()
|
---|
263 | # Change an existing record
|
---|
264 | # Takes a database handle, default/live flag, record ID, and new data and updates the data fields for it
|
---|
265 | sub editRecord {
|
---|
266 | $errstr = '';
|
---|
267 | my $dbh = shift;
|
---|
268 | my $defflag = shift;
|
---|
269 | my $recid = shift;
|
---|
270 | my $host = shift;
|
---|
271 | my $address = shift;
|
---|
272 | my $distance = shift;
|
---|
273 | my $weight = shift;
|
---|
274 | my $port = shift;
|
---|
275 | my $ttl = shift;
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 | ## DNSDB::getSOA()
|
---|
280 | # Return all suitable fields from an SOA record in separate elements of a hash
|
---|
281 | # Takes a database handle, default/live flag, and group (default) or domain (live) ID
|
---|
282 | sub getSOA {
|
---|
283 | $errstr = '';
|
---|
284 | my $dbh = shift;
|
---|
285 | my $def = shift;
|
---|
286 | my $id = shift;
|
---|
287 | my %ret;
|
---|
288 |
|
---|
289 | my $sql = "select record_id,host,val,ttl from";
|
---|
290 | if ($def eq 'def' or $def eq 'y') {
|
---|
291 | $sql .= " default_records where group_id=$id and type=$reverse_typemap{SOA}";
|
---|
292 | } else {
|
---|
293 | # we're editing a live SOA record; find based on domain
|
---|
294 | $sql .= " records where domain_id=$id and type=$reverse_typemap{SOA}";
|
---|
295 | }
|
---|
296 | #print "getSOA DEBUG: $sql<br>\n";
|
---|
297 | my $sth = $dbh->prepare($sql);
|
---|
298 | $sth->execute;
|
---|
299 |
|
---|
300 | my ($recid,$host,$val,$ttl) = $sth->fetchrow_array();
|
---|
301 | my ($prins,$contact) = split /:/, $host;
|
---|
302 | my ($refresh,$retry,$expire,$minttl) = split /:/, $val;
|
---|
303 |
|
---|
304 | $ret{recid} = $recid;
|
---|
305 | $ret{ttl} = $ttl;
|
---|
306 | $ret{prins} = $prins;
|
---|
307 | $ret{contact} = $contact;
|
---|
308 | $ret{refresh} = $refresh;
|
---|
309 | $ret{retry} = $retry;
|
---|
310 | $ret{expire} = $expire;
|
---|
311 | $ret{minttl} = $minttl;
|
---|
312 |
|
---|
313 | return %ret;
|
---|
314 | } # end getSOA()
|
---|
315 |
|
---|
316 |
|
---|
317 | ## DNSDB::getRecLine()
|
---|
318 | # Return all data fields for a zone record in separate elements of a hash
|
---|
319 | # Takes a database handle, default/live flag, and record ID
|
---|
320 | sub getRecLine {
|
---|
321 | $errstr = '';
|
---|
322 | my $dbh = shift;
|
---|
323 | my $def = shift;
|
---|
324 | my $id = shift;
|
---|
325 |
|
---|
326 | my $sql = "select record_id,host,type,val,distance,weight,port,ttl from ".
|
---|
327 | (($def eq 'def' or $def eq 'y') ? 'default_' : '').
|
---|
328 | "records where record_id=$id";
|
---|
329 | print "MDEBUG: $sql<br>\n";
|
---|
330 | my $sth = $dbh->prepare($sql);
|
---|
331 | $sth->execute;
|
---|
332 |
|
---|
333 | my ($recid,$host,$rtype,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array();
|
---|
334 |
|
---|
335 | if ($sth->err) {
|
---|
336 | $errstr = $DBI::errstr;
|
---|
337 | return undef;
|
---|
338 | }
|
---|
339 | my %ret;
|
---|
340 | $ret{recid} = $recid;
|
---|
341 | $ret{host} = $host;
|
---|
342 | $ret{type} = $rtype;
|
---|
343 | $ret{val} = $val;
|
---|
344 | $ret{distance}= $distance;
|
---|
345 | $ret{weight} = $weight;
|
---|
346 | $ret{port} = $port;
|
---|
347 | $ret{ttl} = $ttl;
|
---|
348 |
|
---|
349 | return %ret;
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | ##fixme: should use above (getRecLine()) to get lines for below?
|
---|
354 | ## DNSDB::getDomRecs()
|
---|
355 | # Return records for a domain
|
---|
356 | # Takes a database handle, default/live flag, group/domain ID, start,
|
---|
357 | # number of records, sort field, and sort order
|
---|
358 | # Returns a reference to an array of hashes
|
---|
359 | sub getDomRecs {
|
---|
360 | $errstr = '';
|
---|
361 | my $dbh = shift;
|
---|
362 | my $type = shift;
|
---|
363 | my $id = shift;
|
---|
364 | my $nrecs = shift || 'all';
|
---|
365 | my $nstart = shift || 0;
|
---|
366 |
|
---|
367 | ## for order, need to map input to column names
|
---|
368 | my $order = shift || 'host';
|
---|
369 |
|
---|
370 | my $sql = "select record_id,host,type,val,distance,weight,port,ttl from";
|
---|
371 | if ($type eq 'def' or $type eq 'y') {
|
---|
372 | $sql .= " default_records where group_id=$id";
|
---|
373 | } else {
|
---|
374 | $sql .= " records where domain_id=$id";
|
---|
375 | }
|
---|
376 | $sql .= " and not type=$reverse_typemap{SOA} order by $order";
|
---|
377 | $sql .= " limit $nrecs offset $nstart" if $nstart ne 'all';
|
---|
378 |
|
---|
379 | my $sth = $dbh->prepare($sql);
|
---|
380 | $sth->execute;
|
---|
381 |
|
---|
382 | my @retbase;
|
---|
383 | while (my $ref = $sth->fetchrow_hashref()) {
|
---|
384 | push @retbase, $ref;
|
---|
385 | }
|
---|
386 |
|
---|
387 | my $ret = \@retbase;
|
---|
388 | return $ret;
|
---|
389 | } # end getDomRecs()
|
---|
390 |
|
---|
391 |
|
---|
392 | ## DNSDB::addRec()
|
---|
393 | # Add a new record to a domain or a group's default records
|
---|
394 | # Takes a database handle, default/live flag, group/domain ID,
|
---|
395 | # host, type, value, and TTL
|
---|
396 | # Some types require additional detail: "distance" for MX and SRV,
|
---|
397 | # and weight/port for SRV
|
---|
398 | # Returns a status code and detail message in case of error
|
---|
399 | sub addRec {
|
---|
400 | $errstr = '';
|
---|
401 | my $dbh = shift;
|
---|
402 | my $defrec = shift;
|
---|
403 | my $id = shift;
|
---|
404 |
|
---|
405 | my $host = shift;
|
---|
406 | my $rectype = shift;
|
---|
407 | my $val = shift;
|
---|
408 | my $ttl = shift;
|
---|
409 |
|
---|
410 | my $fields = ($defrec eq 'y' ? 'group_id' : 'domain_id').",host,type,val,ttl";
|
---|
411 | my $vallist = "$id,'$host',$rectype,'$val',$ttl";
|
---|
412 |
|
---|
413 | my $dist;
|
---|
414 | if ($rectype == $reverse_typemap{MX} or $rectype == $reverse_typemap{SRV}) {
|
---|
415 | $dist = shift;
|
---|
416 | return ('FAIL',"Need distance for $typemap{$rectype} record") if !defined($dist);
|
---|
417 | $fields .= ",distance";
|
---|
418 | $vallist .= ",$dist";
|
---|
419 | }
|
---|
420 | my $weight;
|
---|
421 | my $port;
|
---|
422 | if ($rectype == $reverse_typemap{SRV}) {
|
---|
423 | $weight = shift;
|
---|
424 | $port = shift;
|
---|
425 | return ('FAIL',"Need weight and port for SRV record") if !defined($weight) or !defined($port);
|
---|
426 | $fields .= ",weight,port";
|
---|
427 | $vallist .= ",$weight,$port";
|
---|
428 | }
|
---|
429 |
|
---|
430 | my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallist)";
|
---|
431 | # something is bugging me about this...
|
---|
432 | print "DEBUG: $sql<br>\n";
|
---|
433 | my $sth = $dbh->prepare($sql);
|
---|
434 | $sth->execute;
|
---|
435 |
|
---|
436 | return ('FAIL',$sth->errstr) if $sth->err;
|
---|
437 |
|
---|
438 | return ('OK','OK');
|
---|
439 | } # end addRec()
|
---|
440 |
|
---|
441 |
|
---|
442 | ## DNSDB::delRec()
|
---|
443 | # Delete a record.
|
---|
444 | sub delRec {
|
---|
445 | $errstr = '';
|
---|
446 | my $dbh = shift;
|
---|
447 | my $defrec = shift;
|
---|
448 | my $id = shift;
|
---|
449 |
|
---|
450 | my $sth = $dbh->prepare("delete from ".($defrec eq 'y' ? 'default_' : '')."records where record_id=?");
|
---|
451 | $sth->execute($id);
|
---|
452 |
|
---|
453 | return ('FAIL',$sth->errstr) if $sth->err;
|
---|
454 |
|
---|
455 | return ('OK','OK');
|
---|
456 | } # end delRec()
|
---|
457 |
|
---|
458 |
|
---|
459 | ## DNSDB::domStatus()
|
---|
460 | # Sets and/or returns a domain's status
|
---|
461 | # Takes a database handle, domain ID and optionally a status argument
|
---|
462 | # Returns undef on errors.
|
---|
463 | sub domStatus {
|
---|
464 | my $dbh = shift;
|
---|
465 | my $id = shift;
|
---|
466 | my $newstatus = shift;
|
---|
467 |
|
---|
468 | return undef if $id !~ /^\d+$/;
|
---|
469 |
|
---|
470 | my $sth;
|
---|
471 |
|
---|
472 | # ooo, fun! let's see what we were passed for status
|
---|
473 | if ($newstatus) {
|
---|
474 | $sth = $dbh->prepare("update domains set status=? where domain_id=?");
|
---|
475 | # ass-u-me caller knows what's going on in full
|
---|
476 | if ($newstatus =~ /^[01]$/) { # only two valid for now.
|
---|
477 | $sth->execute($newstatus,$id);
|
---|
478 | } elsif ($newstatus =~ /^domo(?:n|ff)$/) {
|
---|
479 | $sth->execute(($newstatus eq 'domon' ? 1 : 0),$id);
|
---|
480 | }
|
---|
481 | }
|
---|
482 |
|
---|
483 | $sth = $dbh->prepare("select status from domains where domain_id=?");
|
---|
484 | $sth->execute($id);
|
---|
485 | my ($status) = $sth->fetchrow_array;
|
---|
486 | return $status;
|
---|
487 | } # end domStatus()
|
---|
488 |
|
---|
489 |
|
---|
490 | # shut Perl up
|
---|
491 | 1;
|
---|