[2] | 1 | # dns/trunk/DNSDB.pm
|
---|
| 2 | # Abstraction functions for DNS administration
|
---|
| 3 | ###
|
---|
| 4 | # SVN revision info
|
---|
| 5 | # $Date: 2010-01-15 22:48:50 +0000 (Fri, 15 Jan 2010) $
|
---|
| 6 | # SVN revision $Rev: 61 $
|
---|
| 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;
|
---|
[33] | 17 | use Net::DNS;
|
---|
[2] | 18 | #use Net::SMTP;
|
---|
| 19 | #use NetAddr::IP qw( Compact );
|
---|
| 20 | #use POSIX;
|
---|
| 21 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
|
---|
| 22 |
|
---|
| 23 | $VERSION = 0.1;
|
---|
| 24 | @ISA = qw(Exporter);
|
---|
| 25 | @EXPORT_OK = qw(
|
---|
[22] | 26 | &initGlobals &connectDB &finish
|
---|
| 27 | &addDomain &delDomain &domainName
|
---|
| 28 | &addGroup &delGroup &getChildren &groupName
|
---|
[51] | 29 | &addUser &delUser &userFullName &userStatus
|
---|
[22] | 30 | &getSOA &getRecLine &getDomRecs
|
---|
| 31 | &addRec &updateRec &delRec
|
---|
[34] | 32 | &domStatus &importAXFR
|
---|
[2] | 33 | %typemap %reverse_typemap
|
---|
| 34 | );
|
---|
| 35 |
|
---|
| 36 | @EXPORT = (); # Export nothing by default.
|
---|
| 37 | %EXPORT_TAGS = ( ALL => [qw(
|
---|
[22] | 38 | &initGlobals &connectDB &finish
|
---|
| 39 | &addDomain &delDomain &domainName
|
---|
| 40 | &addGroup &delGroup &getChildren &groupName
|
---|
[51] | 41 | &addUser &delUser &userFullName &userStatus
|
---|
[22] | 42 | &getSOA &getRecLine &getDomRecs
|
---|
| 43 | &addRec &updateRec &delRec
|
---|
[34] | 44 | &domStatus &importAXFR
|
---|
[2] | 45 | %typemap %reverse_typemap
|
---|
| 46 | )]
|
---|
| 47 | );
|
---|
| 48 |
|
---|
| 49 | our $group = 1;
|
---|
| 50 | our $errstr = '';
|
---|
| 51 |
|
---|
| 52 | # Halfway sane defaults for SOA, TTL, etc.
|
---|
| 53 | our %def = qw (
|
---|
| 54 | contact hostmaster.DOMAIN
|
---|
| 55 | prins ns1.myserver.com
|
---|
| 56 | soattl 86400
|
---|
| 57 | refresh 10800
|
---|
| 58 | retry 3600
|
---|
| 59 | expire 604800
|
---|
| 60 | minttl 10800
|
---|
| 61 | ttl 10800
|
---|
| 62 | );
|
---|
| 63 |
|
---|
| 64 | # DNS record type map and reverse map.
|
---|
| 65 | # loaded from the database, from http://www.iana.org/assignments/dns-parameters
|
---|
| 66 | our %typemap;
|
---|
| 67 | our %reverse_typemap;
|
---|
| 68 |
|
---|
[55] | 69 |
|
---|
[2] | 70 | ##
|
---|
| 71 | ## Initialization and cleanup subs
|
---|
| 72 | ##
|
---|
| 73 |
|
---|
[55] | 74 |
|
---|
[2] | 75 | ## DNSDB::connectDB()
|
---|
| 76 | # Creates connection to DNS database.
|
---|
| 77 | # Requires the database name, username, and password.
|
---|
| 78 | # Returns a handle to the db.
|
---|
| 79 | # Set up for a PostgreSQL db; could be any transactional DBMS with the
|
---|
| 80 | # right changes.
|
---|
| 81 | sub connectDB {
|
---|
| 82 | $errstr = '';
|
---|
[15] | 83 | my $dbname = shift;
|
---|
| 84 | my $user = shift;
|
---|
| 85 | my $pass = shift;
|
---|
[2] | 86 | my $dbh;
|
---|
| 87 | my $DSN = "DBI:Pg:dbname=$dbname";
|
---|
| 88 |
|
---|
| 89 | my $host = shift;
|
---|
| 90 | $DSN .= ";host=$host" if $host;
|
---|
| 91 |
|
---|
| 92 | # Note that we want to autocommit by default, and we will turn it off locally as necessary.
|
---|
| 93 | # We may not want to print gobbledygook errors; YMMV. Have to ponder that further.
|
---|
| 94 | $dbh = DBI->connect($DSN, $user, $pass, {
|
---|
| 95 | AutoCommit => 1,
|
---|
| 96 | PrintError => 0
|
---|
| 97 | })
|
---|
| 98 | or return (undef, $DBI::errstr) if(!$dbh);
|
---|
| 99 |
|
---|
| 100 | # Return here if we can't select. Note that this indicates a
|
---|
| 101 | # problem executing the select.
|
---|
| 102 | my $sth = $dbh->prepare("select group_id from groups limit 1");
|
---|
| 103 | $sth->execute();
|
---|
| 104 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 105 |
|
---|
| 106 | # See if the select returned anything (or null data). This should
|
---|
| 107 | # succeed if the select executed, but...
|
---|
| 108 | $sth->fetchrow();
|
---|
| 109 | return (undef,$DBI::errstr) if ($sth->err);
|
---|
| 110 |
|
---|
| 111 | $sth->finish;
|
---|
| 112 |
|
---|
| 113 | # If we get here, we should be OK.
|
---|
| 114 | return ($dbh,"DB connection OK");
|
---|
| 115 | } # end connectDB
|
---|
| 116 |
|
---|
| 117 |
|
---|
| 118 | ## DNSDB::finish()
|
---|
| 119 | # Cleans up after database handles and so on.
|
---|
| 120 | # Requires a database handle
|
---|
| 121 | sub finish {
|
---|
| 122 | my $dbh = $_[0];
|
---|
| 123 | $dbh->disconnect;
|
---|
| 124 | } # end finish
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | ## DNSDB::initGlobals()
|
---|
| 128 | # Initialize global variables
|
---|
| 129 | # NB: this does NOT include web-specific session variables!
|
---|
| 130 | # Requires a database handle
|
---|
| 131 | sub initGlobals {
|
---|
| 132 | my $dbh = shift;
|
---|
| 133 |
|
---|
| 134 | # load system-wide site defaults and things from config file
|
---|
[29] | 135 | if (open SYSDEFAULTS, "</etc/dnsdb.conf") {
|
---|
[2] | 136 | ##fixme - error check!
|
---|
[29] | 137 | while (<SYSDEFAULTS>) {
|
---|
| 138 | next if /^\s*#/;
|
---|
| 139 | $def{contact} = $1 if /contact ?= ?([a-z0-9_.-]+)/i;
|
---|
| 140 | $def{prins} = $1 if /prins ?= ?([a-z0-9_.-]+)/i;
|
---|
| 141 | $def{soattl} = $1 if /soattl ?= ?([a-z0-9_.-]+)/i;
|
---|
| 142 | $def{refresh} = $1 if /refresh ?= ?([a-z0-9_.-]+)/i;
|
---|
| 143 | $def{retry} = $1 if /retry ?= ?([a-z0-9_.-]+)/i;
|
---|
| 144 | $def{expire} = $1 if /expire ?= ?([a-z0-9_.-]+)/i;
|
---|
| 145 | $def{minttl} = $1 if /minttl ?= ?([a-z0-9_.-]+)/i;
|
---|
| 146 | $def{ttl} = $1 if /ttl ?= ?([a-z0-9_.-]+)/i;
|
---|
[2] | 147 | ##fixme? load DB user/pass from config file?
|
---|
[29] | 148 | }
|
---|
[2] | 149 | }
|
---|
| 150 | # load from database
|
---|
| 151 | my $sth = $dbh->prepare("select val,name from rectypes");
|
---|
| 152 | $sth->execute;
|
---|
| 153 | while (my ($recval,$recname) = $sth->fetchrow_array()) {
|
---|
| 154 | $typemap{$recval} = $recname;
|
---|
| 155 | $reverse_typemap{$recname} = $recval;
|
---|
| 156 | }
|
---|
| 157 | } # end initGlobals
|
---|
| 158 |
|
---|
| 159 |
|
---|
[55] | 160 | ## DNSDB::_log()
|
---|
| 161 | # Log an action
|
---|
| 162 | # Internal sub
|
---|
| 163 | # Takes a database handle, <foo>, <bar>
|
---|
| 164 | sub _log {
|
---|
| 165 | } # end _log
|
---|
| 166 |
|
---|
| 167 |
|
---|
[2] | 168 | ##
|
---|
| 169 | ## Processing subs
|
---|
| 170 | ##
|
---|
| 171 |
|
---|
| 172 | ## DNSDB::addDomain()
|
---|
| 173 | # Add a domain
|
---|
| 174 | # Takes a database handle, domain name, numeric group, and boolean(ish) state (active/inactive)
|
---|
| 175 | # Returns a status code and message
|
---|
| 176 | sub addDomain {
|
---|
| 177 | $errstr = '';
|
---|
| 178 | my $dbh = shift;
|
---|
| 179 | return ('FAIL',"Need database handle") if !$dbh;
|
---|
| 180 | my $domain = shift;
|
---|
| 181 | return ('FAIL',"Need domain") if !defined($domain);
|
---|
| 182 | my $group = shift;
|
---|
| 183 | return ('FAIL',"Need group") if !defined($group);
|
---|
| 184 | my $state = shift;
|
---|
| 185 | return ('FAIL',"Need domain status") if !defined($state);
|
---|
| 186 |
|
---|
[38] | 187 | my $sth = $dbh->prepare("SELECT domain_id FROM domains WHERE domain=?");
|
---|
[3] | 188 | my $dom_id;
|
---|
| 189 |
|
---|
[38] | 190 | # quick check to start to see if we've already got one
|
---|
| 191 | $sth->execute($domain);
|
---|
| 192 | ($dom_id) = $sth->fetchrow_array;
|
---|
| 193 |
|
---|
| 194 | return ('FAIL', "Domain already exists") if $dom_id;
|
---|
| 195 |
|
---|
[2] | 196 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 197 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 198 | local $dbh->{AutoCommit} = 0;
|
---|
| 199 | local $dbh->{RaiseError} = 1;
|
---|
| 200 |
|
---|
| 201 | # Wrap all the SQL in a transaction
|
---|
| 202 | eval {
|
---|
| 203 | # insert the domain...
|
---|
| 204 | my $sth = $dbh->prepare("insert into domains (domain,group_id,status) values (?,?,?)");
|
---|
| 205 | $sth->execute($domain,$group,$state);
|
---|
| 206 |
|
---|
| 207 | # get the ID...
|
---|
| 208 | $sth = $dbh->prepare("select domain_id from domains where domain='$domain'");
|
---|
| 209 | $sth->execute;
|
---|
[3] | 210 | ($dom_id) = $sth->fetchrow_array();
|
---|
[2] | 211 |
|
---|
| 212 | # ... and now we construct the standard records from the default set. NB: group should be variable.
|
---|
[3] | 213 | $sth = $dbh->prepare("select host,type,val,distance,weight,port,ttl from default_records where group_id=$group");
|
---|
| 214 | my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,weight,port,ttl)".
|
---|
| 215 | " values ($dom_id,?,?,?,?,?,?,?)");
|
---|
[2] | 216 | $sth->execute;
|
---|
[3] | 217 | while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) {
|
---|
[2] | 218 | $host =~ s/DOMAIN/$domain/g;
|
---|
[37] | 219 | $val =~ s/DOMAIN/$domain/g;
|
---|
[3] | 220 | $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl);
|
---|
[2] | 221 | }
|
---|
| 222 |
|
---|
| 223 | # once we get here, we should have suceeded.
|
---|
| 224 | $dbh->commit;
|
---|
| 225 | }; # end eval
|
---|
| 226 |
|
---|
| 227 | if ($@) {
|
---|
| 228 | my $msg = $@;
|
---|
| 229 | eval { $dbh->rollback; };
|
---|
| 230 | return ('FAIL',$msg);
|
---|
| 231 | } else {
|
---|
[3] | 232 | return ('OK',$dom_id);
|
---|
[2] | 233 | }
|
---|
| 234 | } # end addDomain
|
---|
| 235 |
|
---|
| 236 |
|
---|
[3] | 237 | ## DNSDB::delDomain()
|
---|
| 238 | # Delete a domain.
|
---|
| 239 | # for now, just delete the records, then the domain.
|
---|
| 240 | # later we may want to archive it in some way instead (status code 2, for example?)
|
---|
| 241 | sub delDomain {
|
---|
| 242 | my $dbh = shift;
|
---|
[5] | 243 | my $domid = shift;
|
---|
[3] | 244 |
|
---|
[61] | 245 | return "FAIL", "whee!";
|
---|
[3] | 246 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 247 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 248 | local $dbh->{AutoCommit} = 0;
|
---|
| 249 | local $dbh->{RaiseError} = 1;
|
---|
| 250 |
|
---|
[23] | 251 | my $failmsg = '';
|
---|
| 252 |
|
---|
[3] | 253 | # Wrap all the SQL in a transaction
|
---|
| 254 | eval {
|
---|
[5] | 255 | my $sth = $dbh->prepare("delete from records where domain_id=?");
|
---|
[23] | 256 | $failmsg = "Failure removing domain records";
|
---|
[5] | 257 | $sth->execute($domid);
|
---|
| 258 | $sth = $dbh->prepare("delete from domains where domain_id=?");
|
---|
[23] | 259 | $failmsg = "Failure removing domain";
|
---|
[5] | 260 | $sth->execute($domid);
|
---|
[3] | 261 |
|
---|
| 262 | # once we get here, we should have suceeded.
|
---|
[23] | 263 | $dbh->commit;
|
---|
[3] | 264 | }; # end eval
|
---|
| 265 |
|
---|
| 266 | if ($@) {
|
---|
| 267 | my $msg = $@;
|
---|
| 268 | eval { $dbh->rollback; };
|
---|
[23] | 269 | return ('FAIL',"$failmsg: $msg");
|
---|
[3] | 270 | } else {
|
---|
| 271 | return ('OK','OK');
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | } # end delDomain()
|
---|
| 275 |
|
---|
| 276 |
|
---|
[2] | 277 | ## DNSDB::domainName()
|
---|
| 278 | # Return the domain name based on a domain ID
|
---|
| 279 | # Takes a database handle and the domain ID
|
---|
| 280 | # Returns the domain name or undef on failure
|
---|
| 281 | sub domainName {
|
---|
| 282 | $errstr = '';
|
---|
| 283 | my $dbh = shift;
|
---|
| 284 | my $domid = shift;
|
---|
| 285 | my $sth = $dbh->prepare("select domain from domains where domain_id=?");
|
---|
| 286 | $sth->execute($domid);
|
---|
| 287 | my ($domname) = $sth->fetchrow_array();
|
---|
| 288 | $errstr = $DBI::errstr if !$domname;
|
---|
| 289 | return $domname if $domname;
|
---|
| 290 | } # end domainName
|
---|
| 291 |
|
---|
| 292 |
|
---|
[18] | 293 | ## DNSDB::addGroup()
|
---|
| 294 | # Add a group
|
---|
| 295 | # Takes a database handle, group name, parent group, and template-vs-cloneme flag
|
---|
| 296 | # Returns a status code and message
|
---|
| 297 | sub addGroup {
|
---|
| 298 | $errstr = '';
|
---|
| 299 | my $dbh = shift;
|
---|
[20] | 300 | my $groupname = shift;
|
---|
| 301 | my $pargroup = shift;
|
---|
[18] | 302 |
|
---|
| 303 | # 0 indicates "template", hardcoded.
|
---|
| 304 | # Any other value clones that group's default records, if it exists.
|
---|
| 305 | my $torc = shift || 0;
|
---|
| 306 |
|
---|
| 307 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 308 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 309 | local $dbh->{AutoCommit} = 0;
|
---|
| 310 | local $dbh->{RaiseError} = 1;
|
---|
| 311 |
|
---|
[38] | 312 | my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE group_name=?");
|
---|
| 313 | my $group_id;
|
---|
| 314 |
|
---|
| 315 | # quick check to start to see if we've already got one
|
---|
| 316 | $sth->execute($groupname);
|
---|
| 317 | ($group_id) = $sth->fetchrow_array;
|
---|
| 318 |
|
---|
| 319 | return ('FAIL', "Group already exists") if $group_id;
|
---|
| 320 |
|
---|
[18] | 321 | # Wrap all the SQL in a transaction
|
---|
| 322 | eval {
|
---|
[38] | 323 | $sth = $dbh->prepare("INSERT INTO groups (parent_group_id,group_name) VALUES (?,?)");
|
---|
[20] | 324 | $sth->execute($pargroup,$groupname);
|
---|
[18] | 325 |
|
---|
| 326 | $sth = $dbh->prepare("SELECT group_id FROM groups WHERE group_name=?");
|
---|
[20] | 327 | $sth->execute($groupname);
|
---|
| 328 | my ($groupid) = $sth->fetchrow_array();
|
---|
[18] | 329 |
|
---|
| 330 | $sth = $dbh->prepare("INSERT INTO default_records (group_id,host,type,val,distance,weight,port,ttl) ".
|
---|
[20] | 331 | "VALUES ($groupid,?,?,?,?,?,?,?)");
|
---|
[18] | 332 | if ($torc) {
|
---|
| 333 | my $sth2 = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM default_records WHERE group_id=?");
|
---|
| 334 | while (my @clonedata = $sth2->fetchrow_array) {
|
---|
| 335 | $sth->execute(@clonedata);
|
---|
| 336 | }
|
---|
| 337 | } else {
|
---|
| 338 | # reasonable basic defaults for SOA, MX, NS, and minimal hosting
|
---|
| 339 | # could load from a config file, but somewhere along the line we need hardcoded bits.
|
---|
| 340 | $sth->execute('ns1.example.com:hostmaster.example.com', 6, '10800:3600:604800:10800', 0, 0, 0, 86400);
|
---|
| 341 | $sth->execute('DOMAIN', 1, '192.168.4.2', 0, 0, 0, 7200);
|
---|
| 342 | $sth->execute('DOMAIN', 15, 'mx.example.com', 10, 0, 0, 7200);
|
---|
| 343 | $sth->execute('DOMAIN', 2, 'ns1.example.com', 0, 0, 0, 7200);
|
---|
| 344 | $sth->execute('DOMAIN', 2, 'ns2.example.com', 0, 0, 0, 7200);
|
---|
| 345 | $sth->execute('www.DOMAIN', 5, 'DOMAIN', 0, 0, 0, 7200);
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | # once we get here, we should have suceeded.
|
---|
| 349 | $dbh->commit;
|
---|
| 350 | }; # end eval
|
---|
| 351 |
|
---|
| 352 | if ($@) {
|
---|
| 353 | my $msg = $@;
|
---|
| 354 | eval { $dbh->rollback; };
|
---|
| 355 | return ('FAIL',$msg);
|
---|
| 356 | } else {
|
---|
| 357 | return ('OK','OK');
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | } # end addGroup()
|
---|
| 361 |
|
---|
| 362 |
|
---|
[22] | 363 | ## DNSDB::delGroup()
|
---|
| 364 | # Delete a group.
|
---|
| 365 | # Takes a group ID
|
---|
| 366 | # Returns a status code and message
|
---|
| 367 | sub delGroup {
|
---|
| 368 | my $dbh = shift;
|
---|
| 369 | my $groupid = shift;
|
---|
| 370 |
|
---|
| 371 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 372 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 373 | local $dbh->{AutoCommit} = 0;
|
---|
| 374 | local $dbh->{RaiseError} = 1;
|
---|
| 375 |
|
---|
| 376 | ##fixme: locate "knowable" error conditions and deal with them before the eval
|
---|
[23] | 377 | # ... or inside, whatever.
|
---|
[22] | 378 | # -> domains still exist in group
|
---|
| 379 | # -> ...
|
---|
[23] | 380 | my $failmsg = '';
|
---|
[22] | 381 |
|
---|
| 382 | # Wrap all the SQL in a transaction
|
---|
| 383 | eval {
|
---|
[23] | 384 | my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
|
---|
[22] | 385 | $sth->execute($groupid);
|
---|
[23] | 386 | my ($domcnt) = $sth->fetchrow_array;
|
---|
| 387 | $failmsg = "Can't remove group ".groupName($dbh,$groupid);
|
---|
| 388 | die "$domcnt domains still in group\n" if $domcnt;
|
---|
| 389 |
|
---|
| 390 | $sth = $dbh->prepare("delete from default_records where group_id=?");
|
---|
| 391 | $failmsg = "Failed to delete default records for ".groupName($dbh,$groupid);
|
---|
| 392 | $sth->execute($groupid);
|
---|
[22] | 393 | $sth = $dbh->prepare("delete from groups where group_id=?");
|
---|
[23] | 394 | $failmsg = "Failed to remove group ".groupName($dbh,$groupid);
|
---|
[22] | 395 | $sth->execute($groupid);
|
---|
| 396 |
|
---|
| 397 | # once we get here, we should have suceeded.
|
---|
| 398 | $dbh->commit;
|
---|
| 399 | }; # end eval
|
---|
| 400 |
|
---|
| 401 | if ($@) {
|
---|
| 402 | my $msg = $@;
|
---|
| 403 | eval { $dbh->rollback; };
|
---|
[23] | 404 | return ('FAIL',"$failmsg: $msg");
|
---|
[22] | 405 | } else {
|
---|
| 406 | return ('OK','OK');
|
---|
| 407 | }
|
---|
| 408 | } # end delGroup()
|
---|
| 409 |
|
---|
| 410 |
|
---|
[19] | 411 | ## DNSDB::getChildren()
|
---|
| 412 | # Get a list of all groups whose parent^n is group <n>
|
---|
[24] | 413 | # Takes a database handle, group ID, reference to an array to put the group IDs in,
|
---|
| 414 | # and an optional flag to return only immediate children or all children-of-children
|
---|
| 415 | # default to returning all children
|
---|
[19] | 416 | # Calls itself
|
---|
| 417 | sub getChildren {
|
---|
| 418 | $errstr = '';
|
---|
| 419 | my $dbh = shift;
|
---|
[20] | 420 | my $rootgroup = shift;
|
---|
| 421 | my $groupdest = shift;
|
---|
[24] | 422 | my $immed = shift || 'all';
|
---|
[19] | 423 |
|
---|
| 424 | # special break for default group; otherwise we get stuck.
|
---|
[20] | 425 | if ($rootgroup == 1) {
|
---|
[19] | 426 | # by definition, group 1 is the Root Of All Groups
|
---|
[24] | 427 | my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE NOT (group_id=1)".
|
---|
| 428 | ($immed ne 'all' ? " AND parent_group_id=1" : ''));
|
---|
[19] | 429 | $sth->execute;
|
---|
| 430 | while (my @this = $sth->fetchrow_array) {
|
---|
[20] | 431 | push @$groupdest, @this;
|
---|
[19] | 432 | }
|
---|
| 433 | } else {
|
---|
| 434 | my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE parent_group_id=?");
|
---|
[20] | 435 | $sth->execute($rootgroup);
|
---|
[19] | 436 | return if $sth->rows == 0;
|
---|
[20] | 437 | my @grouplist;
|
---|
| 438 | while (my ($group) = $sth->fetchrow_array) {
|
---|
| 439 | push @$groupdest, $group;
|
---|
[24] | 440 | getChildren($dbh,$group,$groupdest) if $immed eq 'all';
|
---|
[19] | 441 | }
|
---|
| 442 | }
|
---|
| 443 | } # end getChildren()
|
---|
| 444 |
|
---|
| 445 |
|
---|
[20] | 446 | ## DNSDB::groupName()
|
---|
[17] | 447 | # Return the group name based on a group ID
|
---|
| 448 | # Takes a database handle and the group ID
|
---|
| 449 | # Returns the group name or undef on failure
|
---|
[20] | 450 | sub groupName {
|
---|
[13] | 451 | $errstr = '';
|
---|
| 452 | my $dbh = shift;
|
---|
[20] | 453 | my $groupid = shift;
|
---|
| 454 | my $sth = $dbh->prepare("SELECT group_name FROM groups WHERE group_id=?");
|
---|
| 455 | $sth->execute($groupid);
|
---|
| 456 | my ($groupname) = $sth->fetchrow_array();
|
---|
| 457 | $errstr = $DBI::errstr if !$groupname;
|
---|
| 458 | return $groupname if $groupname;
|
---|
| 459 | } # end groupName
|
---|
[13] | 460 |
|
---|
| 461 |
|
---|
[24] | 462 | ## DNSDB::addUser()
|
---|
| 463 | #
|
---|
| 464 | sub addUser {
|
---|
| 465 | $errstr = '';
|
---|
| 466 | my $dbh = shift;
|
---|
| 467 | return ('FAIL',"Need database handle") if !$dbh;
|
---|
| 468 | my $username = shift;
|
---|
| 469 | return ('FAIL',"Missing username") if !defined($username);
|
---|
| 470 | my $group = shift;
|
---|
| 471 | return ('FAIL',"Missing group") if !defined($group);
|
---|
| 472 | my $pass = shift;
|
---|
| 473 | return ('FAIL',"Missing password") if !defined($pass);
|
---|
| 474 | my $state = shift;
|
---|
| 475 | return ('FAIL',"Need account status") if !defined($state);
|
---|
[25] | 476 |
|
---|
| 477 | my $type = shift || 'u'; # create limited users by default - fwiw, not sure yet how this will interact with ACLs
|
---|
| 478 |
|
---|
| 479 | my $fname = shift || $username;
|
---|
[24] | 480 | my $lname = shift || '';
|
---|
[25] | 481 | my $phone = shift || ''; # not going format-check
|
---|
[24] | 482 |
|
---|
[38] | 483 | my $sth = $dbh->prepare("SELECT user_id FROM users WHERE username=?");
|
---|
[24] | 484 | my $user_id;
|
---|
| 485 |
|
---|
[38] | 486 | # quick check to start to see if we've already got one
|
---|
| 487 | $sth->execute($username);
|
---|
| 488 | ($user_id) = $sth->fetchrow_array;
|
---|
| 489 |
|
---|
| 490 | return ('FAIL', "User already exists") if $user_id;
|
---|
| 491 |
|
---|
[24] | 492 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 493 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 494 | local $dbh->{AutoCommit} = 0;
|
---|
| 495 | local $dbh->{RaiseError} = 1;
|
---|
| 496 |
|
---|
| 497 | # Wrap all the SQL in a transaction
|
---|
| 498 | eval {
|
---|
| 499 | # insert the user...
|
---|
[25] | 500 | my $sth = $dbh->prepare("INSERT INTO users (group_id,username,password,firstname,lastname,phone,type,status) ".
|
---|
| 501 | "VALUES (?,?,?,?,?,?,?,?)");
|
---|
[55] | 502 | $sth->execute($group,$username,unix_md5_crypt($pass),$fname,$lname,$phone,$type,$state);
|
---|
[24] | 503 |
|
---|
| 504 | # get the ID...
|
---|
| 505 | $sth = $dbh->prepare("select user_id from users where username=?");
|
---|
| 506 | $sth->execute($username);
|
---|
| 507 | ($user_id) = $sth->fetchrow_array();
|
---|
| 508 |
|
---|
[25] | 509 | ##fixme: add another table to hold name/email for log table?
|
---|
| 510 |
|
---|
[24] | 511 | # once we get here, we should have suceeded.
|
---|
| 512 | $dbh->commit;
|
---|
| 513 | }; # end eval
|
---|
| 514 |
|
---|
| 515 | if ($@) {
|
---|
| 516 | my $msg = $@;
|
---|
| 517 | eval { $dbh->rollback; };
|
---|
| 518 | return ('FAIL',$msg);
|
---|
| 519 | } else {
|
---|
| 520 | return ('OK',$user_id);
|
---|
| 521 | }
|
---|
| 522 | } # end addUser
|
---|
| 523 |
|
---|
| 524 |
|
---|
[55] | 525 | ## DNSDB::checkUser()
|
---|
| 526 | # Check user/pass combo on login
|
---|
| 527 | sub checkUser {
|
---|
| 528 | my $dbh = shift;
|
---|
| 529 | my $user = shift;
|
---|
[56] | 530 | my $inpass = shift;
|
---|
[55] | 531 |
|
---|
| 532 | my $sth = $dbh->prepare("SELECT user_id,group_id,password,firstname,lastname FROM users WHERE username=?");
|
---|
| 533 | $sth->execute($user);
|
---|
| 534 | my ($uid,$gid,$pass,$fname,$lname) = $sth->fetchrow_array;
|
---|
| 535 | my $loginfailed = 1 if !defined($uid);
|
---|
| 536 |
|
---|
| 537 | if ($pass =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
|
---|
[56] | 538 | $loginfailed = 1 if $pass ne unix_md5_crypt($inpass,$1);
|
---|
[55] | 539 | } else {
|
---|
[56] | 540 | $loginfailed = 1 if $pass ne $inpass;
|
---|
[55] | 541 | }
|
---|
| 542 |
|
---|
| 543 | # nnnngggg
|
---|
| 544 | return ($uid, $gid);
|
---|
| 545 | } # end checkUser
|
---|
| 546 |
|
---|
| 547 |
|
---|
[24] | 548 | ## DNSDB::delUser()
|
---|
| 549 | #
|
---|
| 550 | sub delUser {
|
---|
[25] | 551 | my $dbh = shift;
|
---|
| 552 | return ('FAIL',"Need database handle") if !$dbh;
|
---|
| 553 | my $userid = shift;
|
---|
| 554 | return ('FAIL',"Missing userid") if !defined($userid);
|
---|
| 555 |
|
---|
| 556 | my $sth = $dbh->prepare("delete from users where user_id=?");
|
---|
| 557 | $sth->execute($userid);
|
---|
| 558 |
|
---|
| 559 | return ('FAIL',"Couldn't remove user: ".$sth->errstr) if $sth->err;
|
---|
| 560 |
|
---|
| 561 | return ('OK','OK');
|
---|
| 562 |
|
---|
[24] | 563 | } # end delUser
|
---|
| 564 |
|
---|
| 565 |
|
---|
[25] | 566 | ## DNSDB::userFullName()
|
---|
| 567 | # Return a pretty string!
|
---|
| 568 | # Takes a user_id and optional printf-ish string to indicate which pieces where:
|
---|
| 569 | # %u for the username
|
---|
| 570 | # %f for the first name
|
---|
| 571 | # %l for the last name
|
---|
| 572 | # All other text in the passed string will be left as-is.
|
---|
| 573 | ##fixme: need a "smart" option too, so that missing/null/blank first/last names don't give funky output
|
---|
| 574 | sub userFullName {
|
---|
| 575 | $errstr = '';
|
---|
| 576 | my $dbh = shift;
|
---|
| 577 | my $userid = shift;
|
---|
| 578 | my $fullformat = shift || '%f %l (%u)';
|
---|
| 579 | my $sth = $dbh->prepare("select username,firstname,lastname from users where user_id=?");
|
---|
| 580 | $sth->execute($userid);
|
---|
| 581 | my ($uname,$fname,$lname) = $sth->fetchrow_array();
|
---|
| 582 | $errstr = $DBI::errstr if !$uname;
|
---|
| 583 |
|
---|
| 584 | $fullformat =~ s/\%u/$uname/g;
|
---|
| 585 | $fullformat =~ s/\%f/$fname/g;
|
---|
| 586 | $fullformat =~ s/\%l/$lname/g;
|
---|
| 587 |
|
---|
| 588 | return $fullformat;
|
---|
| 589 | } # end userFullName
|
---|
| 590 |
|
---|
| 591 |
|
---|
[51] | 592 | ## DNSDB::userStatus()
|
---|
| 593 | # Sets and/or returns a user's status
|
---|
| 594 | # Takes a database handle, user ID and optionally a status argument
|
---|
| 595 | # Returns undef on errors.
|
---|
| 596 | sub userStatus {
|
---|
| 597 | my $dbh = shift;
|
---|
| 598 | my $id = shift;
|
---|
| 599 | my $newstatus = shift;
|
---|
| 600 |
|
---|
| 601 | return undef if $id !~ /^\d+$/;
|
---|
| 602 |
|
---|
| 603 | my $sth;
|
---|
| 604 |
|
---|
| 605 | # ooo, fun! let's see what we were passed for status
|
---|
| 606 | if ($newstatus) {
|
---|
| 607 | $sth = $dbh->prepare("update users set status=? where user_id=?");
|
---|
| 608 | # ass-u-me caller knows what's going on in full
|
---|
| 609 | if ($newstatus =~ /^[01]$/) { # only two valid for now.
|
---|
| 610 | $sth->execute($newstatus,$id);
|
---|
| 611 | } elsif ($newstatus =~ /^usero(?:n|ff)$/) {
|
---|
| 612 | $sth->execute(($newstatus eq 'useron' ? 1 : 0),$id);
|
---|
| 613 | }
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | $sth = $dbh->prepare("select status from users where user_id=?");
|
---|
| 617 | $sth->execute($id);
|
---|
| 618 | my ($status) = $sth->fetchrow_array;
|
---|
| 619 | return $status;
|
---|
| 620 | } # end userStatus()
|
---|
| 621 |
|
---|
| 622 |
|
---|
[2] | 623 | ## DNSDB::editRecord()
|
---|
| 624 | # Change an existing record
|
---|
| 625 | # Takes a database handle, default/live flag, record ID, and new data and updates the data fields for it
|
---|
| 626 | sub editRecord {
|
---|
| 627 | $errstr = '';
|
---|
| 628 | my $dbh = shift;
|
---|
| 629 | my $defflag = shift;
|
---|
| 630 | my $recid = shift;
|
---|
| 631 | my $host = shift;
|
---|
| 632 | my $address = shift;
|
---|
| 633 | my $distance = shift;
|
---|
| 634 | my $weight = shift;
|
---|
| 635 | my $port = shift;
|
---|
| 636 | my $ttl = shift;
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 |
|
---|
| 640 | ## DNSDB::getSOA()
|
---|
| 641 | # Return all suitable fields from an SOA record in separate elements of a hash
|
---|
| 642 | # Takes a database handle, default/live flag, and group (default) or domain (live) ID
|
---|
| 643 | sub getSOA {
|
---|
| 644 | $errstr = '';
|
---|
| 645 | my $dbh = shift;
|
---|
| 646 | my $def = shift;
|
---|
| 647 | my $id = shift;
|
---|
| 648 | my %ret;
|
---|
| 649 |
|
---|
| 650 | my $sql = "select record_id,host,val,ttl from";
|
---|
| 651 | if ($def eq 'def' or $def eq 'y') {
|
---|
| 652 | $sql .= " default_records where group_id=$id and type=$reverse_typemap{SOA}";
|
---|
| 653 | } else {
|
---|
| 654 | # we're editing a live SOA record; find based on domain
|
---|
| 655 | $sql .= " records where domain_id=$id and type=$reverse_typemap{SOA}";
|
---|
| 656 | }
|
---|
| 657 | my $sth = $dbh->prepare($sql);
|
---|
| 658 | $sth->execute;
|
---|
| 659 |
|
---|
| 660 | my ($recid,$host,$val,$ttl) = $sth->fetchrow_array();
|
---|
| 661 | my ($prins,$contact) = split /:/, $host;
|
---|
| 662 | my ($refresh,$retry,$expire,$minttl) = split /:/, $val;
|
---|
| 663 |
|
---|
| 664 | $ret{recid} = $recid;
|
---|
| 665 | $ret{ttl} = $ttl;
|
---|
| 666 | $ret{prins} = $prins;
|
---|
| 667 | $ret{contact} = $contact;
|
---|
| 668 | $ret{refresh} = $refresh;
|
---|
| 669 | $ret{retry} = $retry;
|
---|
| 670 | $ret{expire} = $expire;
|
---|
| 671 | $ret{minttl} = $minttl;
|
---|
| 672 |
|
---|
| 673 | return %ret;
|
---|
| 674 | } # end getSOA()
|
---|
| 675 |
|
---|
| 676 |
|
---|
| 677 | ## DNSDB::getRecLine()
|
---|
| 678 | # Return all data fields for a zone record in separate elements of a hash
|
---|
| 679 | # Takes a database handle, default/live flag, and record ID
|
---|
| 680 | sub getRecLine {
|
---|
| 681 | $errstr = '';
|
---|
| 682 | my $dbh = shift;
|
---|
| 683 | my $def = shift;
|
---|
| 684 | my $id = shift;
|
---|
| 685 |
|
---|
| 686 | my $sql = "select record_id,host,type,val,distance,weight,port,ttl from ".
|
---|
| 687 | (($def eq 'def' or $def eq 'y') ? 'default_' : '').
|
---|
| 688 | "records where record_id=$id";
|
---|
| 689 | print "MDEBUG: $sql<br>\n";
|
---|
| 690 | my $sth = $dbh->prepare($sql);
|
---|
| 691 | $sth->execute;
|
---|
| 692 |
|
---|
| 693 | my ($recid,$host,$rtype,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array();
|
---|
| 694 |
|
---|
| 695 | if ($sth->err) {
|
---|
| 696 | $errstr = $DBI::errstr;
|
---|
| 697 | return undef;
|
---|
| 698 | }
|
---|
[3] | 699 | my %ret;
|
---|
[2] | 700 | $ret{recid} = $recid;
|
---|
| 701 | $ret{host} = $host;
|
---|
| 702 | $ret{type} = $rtype;
|
---|
| 703 | $ret{val} = $val;
|
---|
| 704 | $ret{distance}= $distance;
|
---|
| 705 | $ret{weight} = $weight;
|
---|
| 706 | $ret{port} = $port;
|
---|
| 707 | $ret{ttl} = $ttl;
|
---|
| 708 |
|
---|
| 709 | return %ret;
|
---|
| 710 | }
|
---|
| 711 |
|
---|
| 712 |
|
---|
| 713 | ##fixme: should use above (getRecLine()) to get lines for below?
|
---|
| 714 | ## DNSDB::getDomRecs()
|
---|
| 715 | # Return records for a domain
|
---|
| 716 | # Takes a database handle, default/live flag, group/domain ID, start,
|
---|
| 717 | # number of records, sort field, and sort order
|
---|
| 718 | # Returns a reference to an array of hashes
|
---|
| 719 | sub getDomRecs {
|
---|
| 720 | $errstr = '';
|
---|
| 721 | my $dbh = shift;
|
---|
| 722 | my $type = shift;
|
---|
| 723 | my $id = shift;
|
---|
[4] | 724 | my $nrecs = shift || 'all';
|
---|
| 725 | my $nstart = shift || 0;
|
---|
[2] | 726 |
|
---|
[4] | 727 | ## for order, need to map input to column names
|
---|
| 728 | my $order = shift || 'host';
|
---|
| 729 |
|
---|
[2] | 730 | my $sql = "select record_id,host,type,val,distance,weight,port,ttl from";
|
---|
| 731 | if ($type eq 'def' or $type eq 'y') {
|
---|
| 732 | $sql .= " default_records where group_id=$id";
|
---|
| 733 | } else {
|
---|
| 734 | $sql .= " records where domain_id=$id";
|
---|
| 735 | }
|
---|
[4] | 736 | $sql .= " and not type=$reverse_typemap{SOA} order by $order";
|
---|
[15] | 737 | ##fixme: need to set nstart properly (offset is not internally multiplied with limit)
|
---|
[17] | 738 | $sql .= " limit $nrecs offset ".($nstart*$nrecs) if $nstart ne 'all';
|
---|
[4] | 739 |
|
---|
[2] | 740 | my $sth = $dbh->prepare($sql);
|
---|
| 741 | $sth->execute;
|
---|
| 742 |
|
---|
| 743 | my @retbase;
|
---|
| 744 | while (my $ref = $sth->fetchrow_hashref()) {
|
---|
| 745 | push @retbase, $ref;
|
---|
| 746 | }
|
---|
| 747 |
|
---|
| 748 | my $ret = \@retbase;
|
---|
| 749 | return $ret;
|
---|
| 750 | } # end getDomRecs()
|
---|
| 751 |
|
---|
| 752 |
|
---|
[3] | 753 | ## DNSDB::addRec()
|
---|
[2] | 754 | # Add a new record to a domain or a group's default records
|
---|
| 755 | # Takes a database handle, default/live flag, group/domain ID,
|
---|
| 756 | # host, type, value, and TTL
|
---|
| 757 | # Some types require additional detail: "distance" for MX and SRV,
|
---|
| 758 | # and weight/port for SRV
|
---|
| 759 | # Returns a status code and detail message in case of error
|
---|
| 760 | sub addRec {
|
---|
| 761 | $errstr = '';
|
---|
| 762 | my $dbh = shift;
|
---|
| 763 | my $defrec = shift;
|
---|
| 764 | my $id = shift;
|
---|
| 765 |
|
---|
| 766 | my $host = shift;
|
---|
| 767 | my $rectype = shift;
|
---|
| 768 | my $val = shift;
|
---|
| 769 | my $ttl = shift;
|
---|
| 770 |
|
---|
| 771 | my $fields = ($defrec eq 'y' ? 'group_id' : 'domain_id').",host,type,val,ttl";
|
---|
[24] | 772 | my $vallen = "?,?,?,?,?";
|
---|
| 773 | my @vallist = ($id,$host,$rectype,$val,$ttl);
|
---|
[2] | 774 |
|
---|
| 775 | my $dist;
|
---|
| 776 | if ($rectype == $reverse_typemap{MX} or $rectype == $reverse_typemap{SRV}) {
|
---|
| 777 | $dist = shift;
|
---|
| 778 | return ('FAIL',"Need distance for $typemap{$rectype} record") if !defined($dist);
|
---|
| 779 | $fields .= ",distance";
|
---|
[24] | 780 | $vallen .= ",?";
|
---|
| 781 | push @vallist, $dist;
|
---|
[2] | 782 | }
|
---|
| 783 | my $weight;
|
---|
| 784 | my $port;
|
---|
| 785 | if ($rectype == $reverse_typemap{SRV}) {
|
---|
[24] | 786 | # check for _service._protocol. NB: RFC2782 does not say "MUST"... nor "SHOULD"...
|
---|
| 787 | # it just says (paraphrased) "... is prepended with _ to prevent DNS collisions"
|
---|
| 788 | return ('FAIL',"SRV records must begin with _service._protocol")
|
---|
| 789 | if $host !~ /^_[A-Za-z]+\._[A-Za-z]+\.[a-z0-9-]+/;
|
---|
[2] | 790 | $weight = shift;
|
---|
| 791 | $port = shift;
|
---|
| 792 | return ('FAIL',"Need weight and port for SRV record") if !defined($weight) or !defined($port);
|
---|
| 793 | $fields .= ",weight,port";
|
---|
[24] | 794 | $vallen .= ",?,?";
|
---|
| 795 | push @vallist, ($weight,$port);
|
---|
[2] | 796 | }
|
---|
| 797 |
|
---|
[24] | 798 | my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallen)";
|
---|
| 799 | ##fixme: use array for values, replace "vallist" with series of ?,?,? etc
|
---|
[2] | 800 | # something is bugging me about this...
|
---|
[15] | 801 | #warn "DEBUG: $sql";
|
---|
[2] | 802 | my $sth = $dbh->prepare($sql);
|
---|
[24] | 803 | $sth->execute(@vallist);
|
---|
[2] | 804 |
|
---|
| 805 | return ('FAIL',$sth->errstr) if $sth->err;
|
---|
| 806 |
|
---|
| 807 | return ('OK','OK');
|
---|
| 808 | } # end addRec()
|
---|
| 809 |
|
---|
| 810 |
|
---|
[16] | 811 | ## DNSDB::updateRec()
|
---|
| 812 | # Update a record
|
---|
| 813 | sub updateRec {
|
---|
| 814 | $errstr = '';
|
---|
[17] | 815 |
|
---|
[16] | 816 | my $dbh = shift;
|
---|
| 817 | my $defrec = shift;
|
---|
| 818 | my $id = shift;
|
---|
| 819 |
|
---|
| 820 | # all records have these
|
---|
| 821 | my $host = shift;
|
---|
| 822 | my $type = shift;
|
---|
| 823 | my $val = shift;
|
---|
| 824 | my $ttl = shift;
|
---|
| 825 |
|
---|
| 826 | return('FAIL',"Missing standard argument(s)") if !defined($ttl);
|
---|
| 827 |
|
---|
| 828 | # only MX and SRV will use these
|
---|
| 829 | my $dist = 0;
|
---|
| 830 | my $weight = 0;
|
---|
| 831 | my $port = 0;
|
---|
| 832 |
|
---|
| 833 | if ($type == $reverse_typemap{MX} || $type == $reverse_typemap{SRV}) {
|
---|
[17] | 834 | $dist = shift;
|
---|
| 835 | return ('FAIL',"MX or SRV requires distance") if !defined($dist);
|
---|
[16] | 836 | if ($type == $reverse_typemap{SRV}) {
|
---|
[17] | 837 | $weight = shift;
|
---|
| 838 | return ('FAIL',"SRV requires weight") if !defined($weight);
|
---|
| 839 | $port = shift;
|
---|
| 840 | return ('FAIL',"SRV requires port") if !defined($port);
|
---|
[16] | 841 | }
|
---|
| 842 | }
|
---|
| 843 |
|
---|
| 844 | my $sth = $dbh->prepare("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records ".
|
---|
| 845 | "SET host=?,type=?,val=?,ttl=?,distance=?,weight=?,port=? ".
|
---|
| 846 | "WHERE record_id=?");
|
---|
| 847 | $sth->execute($host,$type,$val,$ttl,$dist,$weight,$port,$id);
|
---|
| 848 |
|
---|
[17] | 849 | return ('FAIL',$sth->errstr."<br>\n$errstr<br>\n") if $sth->err;
|
---|
[16] | 850 |
|
---|
| 851 | return ('OK','OK');
|
---|
| 852 | } # end updateRec()
|
---|
| 853 |
|
---|
| 854 |
|
---|
[3] | 855 | ## DNSDB::delRec()
|
---|
| 856 | # Delete a record.
|
---|
| 857 | sub delRec {
|
---|
| 858 | $errstr = '';
|
---|
| 859 | my $dbh = shift;
|
---|
| 860 | my $defrec = shift;
|
---|
| 861 | my $id = shift;
|
---|
| 862 |
|
---|
| 863 | my $sth = $dbh->prepare("delete from ".($defrec eq 'y' ? 'default_' : '')."records where record_id=?");
|
---|
| 864 | $sth->execute($id);
|
---|
| 865 |
|
---|
[23] | 866 | return ('FAIL',"Couldn't remove record: ".$sth->errstr) if $sth->err;
|
---|
[3] | 867 |
|
---|
| 868 | return ('OK','OK');
|
---|
| 869 | } # end delRec()
|
---|
| 870 |
|
---|
| 871 |
|
---|
| 872 | ## DNSDB::domStatus()
|
---|
| 873 | # Sets and/or returns a domain's status
|
---|
| 874 | # Takes a database handle, domain ID and optionally a status argument
|
---|
| 875 | # Returns undef on errors.
|
---|
| 876 | sub domStatus {
|
---|
| 877 | my $dbh = shift;
|
---|
| 878 | my $id = shift;
|
---|
| 879 | my $newstatus = shift;
|
---|
| 880 |
|
---|
| 881 | return undef if $id !~ /^\d+$/;
|
---|
| 882 |
|
---|
| 883 | my $sth;
|
---|
| 884 |
|
---|
| 885 | # ooo, fun! let's see what we were passed for status
|
---|
| 886 | if ($newstatus) {
|
---|
| 887 | $sth = $dbh->prepare("update domains set status=? where domain_id=?");
|
---|
| 888 | # ass-u-me caller knows what's going on in full
|
---|
| 889 | if ($newstatus =~ /^[01]$/) { # only two valid for now.
|
---|
| 890 | $sth->execute($newstatus,$id);
|
---|
| 891 | } elsif ($newstatus =~ /^domo(?:n|ff)$/) {
|
---|
| 892 | $sth->execute(($newstatus eq 'domon' ? 1 : 0),$id);
|
---|
| 893 | }
|
---|
| 894 | }
|
---|
| 895 |
|
---|
| 896 | $sth = $dbh->prepare("select status from domains where domain_id=?");
|
---|
| 897 | $sth->execute($id);
|
---|
| 898 | my ($status) = $sth->fetchrow_array;
|
---|
| 899 | return $status;
|
---|
| 900 | } # end domStatus()
|
---|
| 901 |
|
---|
| 902 |
|
---|
[33] | 903 | ## DNSDB::importAXFR
|
---|
| 904 | # Import a domain via AXFR
|
---|
[37] | 905 | # Takes AXFR host, domain to transfer, group to put the domain in,
|
---|
| 906 | # and optionally:
|
---|
| 907 | # - active/inactive state flag (defaults to active)
|
---|
| 908 | # - overwrite-SOA flag (defaults to off)
|
---|
| 909 | # - overwrite-NS flag (defaults to off, doesn't affect subdomain NS records)
|
---|
| 910 | # Returns a status code (OK, WARN, or FAIL) and message - message should be blank
|
---|
| 911 | # if status is OK, but WARN includes conditions that are not fatal but should
|
---|
| 912 | # really be reported.
|
---|
[33] | 913 | sub importAXFR {
|
---|
| 914 | my $dbh = shift;
|
---|
[35] | 915 | my $ifrom_in = shift;
|
---|
[33] | 916 | my $domain = shift;
|
---|
| 917 | my $group = shift;
|
---|
| 918 | my $status = shift || 1;
|
---|
| 919 | my $rwsoa = shift || 0;
|
---|
| 920 | my $rwns = shift || 0;
|
---|
[37] | 921 |
|
---|
[33] | 922 | ##fixme: add mode to delete&replace, merge+overwrite, merge new?
|
---|
| 923 |
|
---|
[37] | 924 | my $nrecs = 0;
|
---|
| 925 | my $soaflag = 0;
|
---|
| 926 | my $nsflag = 0;
|
---|
| 927 | my $warnmsg = '';
|
---|
| 928 | my $ifrom;
|
---|
[33] | 929 |
|
---|
[35] | 930 | # choke on possible bad setting in ifrom
|
---|
[37] | 931 | # IPv4 and v6, and valid hostnames!
|
---|
[35] | 932 | ($ifrom) = ($ifrom_in =~ /^([0-9a-f\:.]+|[0-9a-z_.-]+)$/i);
|
---|
| 933 | return ('FAIL', "Bad AXFR source host $ifrom")
|
---|
| 934 | unless ($ifrom) = ($ifrom_in =~ /^([0-9a-f\:.]+|[0-9a-z_.-]+)$/i);
|
---|
| 935 |
|
---|
[33] | 936 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
| 937 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
| 938 | local $dbh->{AutoCommit} = 0;
|
---|
| 939 | local $dbh->{RaiseError} = 1;
|
---|
| 940 |
|
---|
[37] | 941 | my $sth = $dbh->prepare("SELECT domain_id FROM domains WHERE domain=?");
|
---|
[34] | 942 | my $dom_id;
|
---|
| 943 |
|
---|
[35] | 944 | # quick check to start to see if we've already got one
|
---|
[37] | 945 | $sth->execute($domain);
|
---|
| 946 | ($dom_id) = $sth->fetchrow_array;
|
---|
[35] | 947 |
|
---|
| 948 | return ('FAIL', "Domain already exists") if $dom_id;
|
---|
| 949 |
|
---|
[33] | 950 | eval {
|
---|
| 951 | # can't do this, can't nest transactions. sigh.
|
---|
[35] | 952 | #my ($dcode, $dmsg) = addDomain(dbh, domain, group, status);
|
---|
[33] | 953 |
|
---|
| 954 | ##fixme: serial
|
---|
[37] | 955 | my $sth = $dbh->prepare("INSERT INTO domains (domain,group_id,status) VALUES (?,?,?)");
|
---|
| 956 | $sth->execute($domain,$group,$status);
|
---|
[33] | 957 |
|
---|
[35] | 958 | ## bizarre DBI<->Net::DNS interaction bug:
|
---|
| 959 | ## sometimes a zone will cause an immediate commit-and-exit (sort of) of the while()
|
---|
[37] | 960 | ## fixed, apparently I was doing *something* odd, but not certain what it was that
|
---|
| 961 | ## caused a commit instead of barfing
|
---|
[35] | 962 |
|
---|
[33] | 963 | # get domain id so we can do the records
|
---|
[37] | 964 | $sth = $dbh->prepare("SELECT domain_id FROM domains WHERE domain=?");
|
---|
| 965 | $sth->execute($domain);
|
---|
| 966 | ($dom_id) = $sth->fetchrow_array();
|
---|
[33] | 967 |
|
---|
[34] | 968 | my $res = Net::DNS::Resolver->new;
|
---|
[35] | 969 | $res->nameservers($ifrom);
|
---|
| 970 | $res->axfr_start($domain)
|
---|
| 971 | or die "Couldn't begin AXFR\n";
|
---|
[34] | 972 |
|
---|
[35] | 973 | while (my $rr = $res->axfr_next()) {
|
---|
[33] | 974 | my $type = $rr->type;
|
---|
[35] | 975 |
|
---|
[34] | 976 | my $sql = "INSERT INTO records (domain_id,host,type,ttl,val";
|
---|
[33] | 977 | my $vallen = "?,?,?,?,?";
|
---|
| 978 |
|
---|
[37] | 979 | $soaflag = 1 if $type eq 'SOA';
|
---|
| 980 | $nsflag = 1 if $type eq 'NS';
|
---|
[35] | 981 |
|
---|
| 982 | my @vallist = ($dom_id, $rr->name, $reverse_typemap{$type}, $rr->ttl);
|
---|
[34] | 983 |
|
---|
| 984 | # "Primary" types:
|
---|
| 985 | # A, NS, CNAME, SOA, PTR(warn in forward), MX, TXT, AAAA, SRV, A6(ob), SPF
|
---|
| 986 | # maybe KEY
|
---|
| 987 |
|
---|
[35] | 988 | # nasty big ugly case-like thing here, since we have to do *some* different
|
---|
| 989 | # processing depending on the record. le sigh.
|
---|
| 990 |
|
---|
[34] | 991 | if ($type eq 'A') {
|
---|
| 992 | push @vallist, $rr->address;
|
---|
| 993 | } elsif ($type eq 'NS') {
|
---|
[37] | 994 | # hmm. should we warn here if subdomain NS'es are left alone?
|
---|
| 995 | next if ($rwns && ($rr->name eq $domain));
|
---|
[34] | 996 | push @vallist, $rr->nsdname;
|
---|
[35] | 997 | $nsflag = 1;
|
---|
[34] | 998 | } elsif ($type eq 'CNAME') {
|
---|
| 999 | push @vallist, $rr->cname;
|
---|
| 1000 | } elsif ($type eq 'SOA') {
|
---|
[37] | 1001 | next if $rwsoa;
|
---|
[34] | 1002 | $vallist[1] = $rr->mname.":".$rr->rname;
|
---|
| 1003 | push @vallist, ($rr->refresh.":".$rr->retry.":".$rr->expire.":".$rr->minimum);
|
---|
[35] | 1004 | $soaflag = 1;
|
---|
[34] | 1005 | } elsif ($type eq 'PTR') {
|
---|
| 1006 | # hmm. PTR records should not be in forward zones.
|
---|
| 1007 | } elsif ($type eq 'MX') {
|
---|
[33] | 1008 | $sql .= ",distance";
|
---|
| 1009 | $vallen .= ",?";
|
---|
[34] | 1010 | push @vallist, $rr->exchange;
|
---|
| 1011 | push @vallist, $rr->preference;
|
---|
| 1012 | } elsif ($type eq 'TXT') {
|
---|
| 1013 | ##fixme: Net::DNS docs say this should be deprecated for rdatastr() or char_str_list(),
|
---|
| 1014 | ## but don't really seem enthusiastic about it.
|
---|
| 1015 | push @vallist, $rr->txtdata;
|
---|
| 1016 | } elsif ($type eq 'SPF') {
|
---|
| 1017 | ##fixme: and the same caveat here, since it is apparently a clone of ::TXT
|
---|
| 1018 | push @vallist, $rr->txtdata;
|
---|
| 1019 | } elsif ($type eq 'AAAA') {
|
---|
| 1020 | push @vallist, $rr->address;
|
---|
| 1021 | } elsif ($type eq 'SRV') {
|
---|
| 1022 | $sql .= ",distance,weight,port" if $type eq 'SRV';
|
---|
| 1023 | $vallen .= ",?,?,?" if $type eq 'SRV';
|
---|
[37] | 1024 | push @vallist, $rr->target;
|
---|
[34] | 1025 | push @vallist, $rr->priority;
|
---|
| 1026 | push @vallist, $rr->weight;
|
---|
| 1027 | push @vallist, $rr->port;
|
---|
| 1028 | } elsif ($type eq 'KEY') {
|
---|
[35] | 1029 | # we don't actually know what to do with these...
|
---|
[34] | 1030 | push @vallist, ($rr->flags.":".$rr->protocol.":".$rr->algorithm.":".$rr->key.":".$rr->keytag.":".$rr->privatekeyname);
|
---|
[35] | 1031 | } else {
|
---|
[37] | 1032 | push @vallist, $rr->rdatastr;
|
---|
[35] | 1033 | # Finding a different record type is not fatal.... just problematic.
|
---|
[37] | 1034 | # We may not be able to export it correctly.
|
---|
[35] | 1035 | $warnmsg .= "Unusual record ".$rr->name." ($type) found\n";
|
---|
[33] | 1036 | }
|
---|
| 1037 |
|
---|
[34] | 1038 | # BIND supports:
|
---|
| 1039 | # A CNAME HINFO MB(ex) MD(ob) MF(ob) MG(ex) MINFO(ex) MR(ex) MX NS NULL
|
---|
| 1040 | # PTR SOA TXT WKS AFSDB(ex) ISDN(ex) RP(ex) RT(ex) X25(ex) PX
|
---|
| 1041 | # ... if one can ever find the right magic to format them correctly
|
---|
| 1042 |
|
---|
| 1043 | # Net::DNS supports:
|
---|
| 1044 | # RRSIG SIG NSAP NS NIMLOC NAPTR MX MR MINFO MG MB LOC ISDN IPSECKEY HINFO
|
---|
| 1045 | # EID DNAME CNAME CERT APL AFSDB AAAA A DS NXT NSEC3PARAM NSEC3 NSEC KEY
|
---|
| 1046 | # DNSKEY DLV X25 TXT TSIG TKEY SSHFP SRV SPF SOA RT RP PX PTR NULL APL::AplItem
|
---|
| 1047 |
|
---|
[37] | 1048 | $sth = $dbh->prepare($sql.") VALUES (".$vallen.")") or die "problem preparing record insert SQL\n";
|
---|
| 1049 | $sth->execute(@vallist) or die "failed to insert ".$rr->string.": ".$sth->errstr."\n";
|
---|
[34] | 1050 |
|
---|
[37] | 1051 | $nrecs++;
|
---|
[34] | 1052 |
|
---|
[37] | 1053 | } # while axfr_next
|
---|
| 1054 |
|
---|
| 1055 | # Overwrite SOA record
|
---|
| 1056 | if ($rwsoa) {
|
---|
| 1057 | $soaflag = 1;
|
---|
| 1058 | my $sthgetsoa = $dbh->prepare("SELECT host,val,ttl FROM default_records WHERE group_id=? AND type=?");
|
---|
| 1059 | my $sthputsoa = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,ttl) VALUES (?,?,?,?,?)");
|
---|
| 1060 | $sthgetsoa->execute($group,$reverse_typemap{SOA});
|
---|
| 1061 | while (my ($host,$val,$ttl) = $sthgetsoa->fetchrow_array()) {
|
---|
| 1062 | $host =~ s/DOMAIN/$domain/g;
|
---|
| 1063 | $val =~ s/DOMAIN/$domain/g;
|
---|
| 1064 | $sthputsoa->execute($dom_id,$host,$reverse_typemap{SOA},$val,$ttl);
|
---|
[34] | 1065 | }
|
---|
[37] | 1066 | }
|
---|
[34] | 1067 |
|
---|
[37] | 1068 | # Overwrite NS records
|
---|
| 1069 | if ($rwns) {
|
---|
| 1070 | $nsflag = 1;
|
---|
| 1071 | my $sthgetns = $dbh->prepare("SELECT host,val,ttl FROM default_records WHERE group_id=? AND type=?");
|
---|
| 1072 | my $sthputns = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,ttl) VALUES (?,?,?,?,?)");
|
---|
| 1073 | $sthgetns->execute($group,$reverse_typemap{NS});
|
---|
| 1074 | while (my ($host,$val,$ttl) = $sthgetns->fetchrow_array()) {
|
---|
| 1075 | $host =~ s/DOMAIN/$domain/g;
|
---|
| 1076 | $val =~ s/DOMAIN/$domain/g;
|
---|
| 1077 | $sthputns->execute($dom_id,$host,$reverse_typemap{NS},$val,$ttl);
|
---|
| 1078 | }
|
---|
| 1079 | }
|
---|
[34] | 1080 |
|
---|
[35] | 1081 | die "No records found; either $ifrom is not authoritative or doesn't allow transfers\n" if !$nrecs;
|
---|
| 1082 | die "Bad zone: No SOA record!\n" if !$soaflag;
|
---|
| 1083 | die "Bad zone: No NS records!\n" if !$nsflag;
|
---|
| 1084 |
|
---|
[37] | 1085 | $dbh->commit;
|
---|
[35] | 1086 |
|
---|
[33] | 1087 | };
|
---|
| 1088 |
|
---|
| 1089 | if ($@) {
|
---|
| 1090 | my $msg = $@;
|
---|
| 1091 | eval { $dbh->rollback; };
|
---|
[34] | 1092 | return ('FAIL',$msg." $warnmsg");
|
---|
[33] | 1093 | } else {
|
---|
[35] | 1094 | return ('WARN', $warnmsg) if $warnmsg;
|
---|
[33] | 1095 | return ('OK',"ook");
|
---|
| 1096 | }
|
---|
| 1097 |
|
---|
[37] | 1098 | # it should be impossible to get here.
|
---|
[34] | 1099 | return ('WARN',"OOOK!");
|
---|
[33] | 1100 | } # end importAXFR()
|
---|
| 1101 |
|
---|
| 1102 |
|
---|
[2] | 1103 | # shut Perl up
|
---|
| 1104 | 1;
|
---|