source: trunk/DNSDB.pm@ 69

Last change on this file since 69 was 67, checked in by Kris Deugau, 14 years ago

/trunk

checkpoint
Add user ACL handling nearing function
Add user/edit user pages merged; they're next to identical anyway
Group list images tweaked with proper "alpha channel", CSS fiddled again

  • final arrangement will probably be a couple of small triangles; pointing right for an expandable group, down for an expanded one

Option to add user to a different group removed

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