Changeset 24 for trunk/DNSDB.pm
- Timestamp:
- 10/30/09 17:52:55 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/DNSDB.pm
r23 r24 26 26 &addDomain &delDomain &domainName 27 27 &addGroup &delGroup &getChildren &groupName 28 &addUser &delUser 28 29 &getSOA &getRecLine &getDomRecs 29 30 &addRec &updateRec &delRec … … 37 38 &addDomain &delDomain &domainName 38 39 &addGroup &delGroup &getChildren &groupName 40 &addUser &delUser 39 41 &getSOA &getRecLine &getDomRecs 40 42 &addRec &updateRec &delRec … … 379 381 ## DNSDB::getChildren() 380 382 # Get a list of all groups whose parent^n is group <n> 381 # Takes a database handle, group ID, and reference to an array to put the group IDs in 383 # Takes a database handle, group ID, reference to an array to put the group IDs in, 384 # and an optional flag to return only immediate children or all children-of-children 385 # default to returning all children 382 386 # Calls itself 383 387 sub getChildren { … … 386 390 my $rootgroup = shift; 387 391 my $groupdest = shift; 392 my $immed = shift || 'all'; 388 393 389 394 # special break for default group; otherwise we get stuck. 390 395 if ($rootgroup == 1) { 391 396 # by definition, group 1 is the Root Of All Groups 392 my $sth = $dbh->prepare("SELECT group_id FROM groups"); 397 my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE NOT (group_id=1)". 398 ($immed ne 'all' ? " AND parent_group_id=1" : '')); 393 399 $sth->execute; 394 my @grouplist;395 400 while (my @this = $sth->fetchrow_array) { 396 401 push @$groupdest, @this; … … 403 408 while (my ($group) = $sth->fetchrow_array) { 404 409 push @$groupdest, $group; 405 getChildren($dbh,$group,$groupdest) ;410 getChildren($dbh,$group,$groupdest) if $immed eq 'all'; 406 411 } 407 412 } … … 423 428 return $groupname if $groupname; 424 429 } # end groupName 430 431 432 ## DNSDB::addUser() 433 # 434 sub addUser { 435 $errstr = ''; 436 my $dbh = shift; 437 return ('FAIL',"Need database handle") if !$dbh; 438 my $username = shift; 439 return ('FAIL',"Missing username") if !defined($username); 440 my $group = shift; 441 return ('FAIL',"Missing group") if !defined($group); 442 my $pass = shift; 443 return ('FAIL',"Missing password") if !defined($pass); 444 445 my $state = shift; 446 return ('FAIL',"Need account status") if !defined($state); 447 my $fname = shift || ''; 448 my $lname = shift || ''; 449 450 my $user_id; 451 452 # Allow transactions, and raise an exception on errors so we can catch it later. 453 # Use local to make sure these get "reset" properly on exiting this block 454 local $dbh->{AutoCommit} = 0; 455 local $dbh->{RaiseError} = 1; 456 457 # Wrap all the SQL in a transaction 458 eval { 459 # insert the user... 460 my $sth = $dbh->prepare("INSERT INTO users (email,group_id,password,status) VALUES (?,?,?,?)"); 461 $sth->execute($username,$group,$pass,$state); 462 die "user fail\n"; 463 464 # get the ID... 465 $sth = $dbh->prepare("select user_id from users where username=?"); 466 $sth->execute($username); 467 ($user_id) = $sth->fetchrow_array(); 468 469 # once we get here, we should have suceeded. 470 $dbh->commit; 471 }; # end eval 472 473 if ($@) { 474 my $msg = $@; 475 eval { $dbh->rollback; }; 476 return ('FAIL',$msg); 477 } else { 478 return ('OK',$user_id); 479 } 480 } # end addUser 481 482 483 ## DNSDB::delUser() 484 # 485 sub delUser { 486 } # end delUser 425 487 426 488 … … 574 636 575 637 my $fields = ($defrec eq 'y' ? 'group_id' : 'domain_id').",host,type,val,ttl"; 576 my $vallist = "$id,'$host',$rectype,'$val',$ttl"; 638 my $vallen = "?,?,?,?,?"; 639 my @vallist = ($id,$host,$rectype,$val,$ttl); 577 640 578 641 my $dist; … … 581 644 return ('FAIL',"Need distance for $typemap{$rectype} record") if !defined($dist); 582 645 $fields .= ",distance"; 583 $vallist .= ",$dist"; 646 $vallen .= ",?"; 647 push @vallist, $dist; 584 648 } 585 649 my $weight; 586 650 my $port; 587 651 if ($rectype == $reverse_typemap{SRV}) { 652 # check for _service._protocol. NB: RFC2782 does not say "MUST"... nor "SHOULD"... 653 # it just says (paraphrased) "... is prepended with _ to prevent DNS collisions" 654 return ('FAIL',"SRV records must begin with _service._protocol") 655 if $host !~ /^_[A-Za-z]+\._[A-Za-z]+\.[a-z0-9-]+/; 588 656 $weight = shift; 589 657 $port = shift; 590 658 return ('FAIL',"Need weight and port for SRV record") if !defined($weight) or !defined($port); 591 659 $fields .= ",weight,port"; 592 $vallist .= ",$weight,$port"; 593 } 594 595 my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallist)"; 660 $vallen .= ",?,?"; 661 push @vallist, ($weight,$port); 662 } 663 664 my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallen)"; 665 ##fixme: use array for values, replace "vallist" with series of ?,?,? etc 596 666 # something is bugging me about this... 597 667 #warn "DEBUG: $sql"; 598 668 my $sth = $dbh->prepare($sql); 599 $sth->execute ;669 $sth->execute(@vallist); 600 670 601 671 return ('FAIL',$sth->errstr) if $sth->err;
Note:
See TracChangeset
for help on using the changeset viewer.