Changeset 582 for branches


Ignore:
Timestamp:
01/02/14 13:26:38 (10 years ago)
Author:
Kris Deugau
Message:

/branches/stable

Merge forward bugfixes and option additions from /trunk r570 through r581

Location:
branches/stable
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/stable

  • branches/stable/DNSDB.pm

    r568 r582  
    213213                force_refresh   => 1,
    214214                lowercase       => 0,   # mangle as little as possible by default
     215                showrec_arpa_ns => 0,   # show formal .arpa zone name instead of logical CIDR on reverse NS records
    215216        );
    216217
     
    242243
    243244  # Several settings are booleans.  Handle multiple possible ways of setting them.
    244   for my $boolopt ('log_failures', 'force_refresh', 'lowercase', 'usecache') {
     245  for my $boolopt ('log_failures', 'force_refresh', 'lowercase', 'usecache', 'showrec_arpa_ns') {
    245246    if ($self->{$boolopt} ne '1' && $self->{$boolopt} ne '0') {
    246247      # true/false, on/off, yes/no all valid.
     
    11361137    my $tmpzone = $zone;
    11371138    $tmpzone =~ s/\.in-addr\.arpa\.?//;
    1138     return ('FAIL', "Non-numerics in apparent IPv4 reverse zone name") if $tmpzone !~ /^(?:\d+-)?[\d\.]+$/;
     1139    return ('FAIL', "Non-numerics in apparent IPv4 reverse zone name [$tmpzone]") if $tmpzone !~ m{^(?:\d+[/-])?[\d\.]+$};
    11391140
    11401141    # Snag the octet pieces
     
    11531154    # Alternate form:  The second number is actually the real netmask, not the end of the range.
    11541155    my $masklen = 0;
    1155     if ($octs[0] =~ /^((\d+)-(\d+))$/) {        # take the range...
     1156    if ($octs[0] =~ m{^((\d+)[/-](\d+))$}) {    # take the range...
    11561157      if (24 < $3 && $3 < 31) {
    11571158        # we have a real netmask
     
    13421343      $cfg->{force_refresh}     = $1 if /^force_refresh\s*=\s*([a-z01]+)/i;
    13431344      $cfg->{lowercase}         = $1 if /^lowercase\s*=\s*([a-z01]+)/i;
     1345      $cfg->{showrec_arpa_ns}   = $1 if /^showrec_arpa_ns\s*=\s*([a-z01]+)/i;
    13441346# not supported in dns.cgi yet
    13451347#      $cfg->{templatedir}      = $1 if m{^templatedir\s*=\s*([a-z0-9/_.-]+)}i;
     
    23072309  if ($args{revrec} eq 'n') {
    23082310    $args{sortby} = 'domain' if !$args{sortby} || !grep /^$args{sortby}$/, ('domain','group','status');
    2309     $sql = "SELECT domain_id,domain,status,groups.group_name AS group FROM domains".
     2311    $sql = "SELECT domain_id AS zoneid,domain AS zone,status,groups.group_name AS group FROM domains".
    23102312        " INNER JOIN groups ON domains.group_id=groups.group_id".
    23112313        " WHERE domains.group_id IN ($args{curgroup}".($args{childlist} ? ",$args{childlist}" : '').")".
     
    23152317##fixme:  arguably startwith here is irrelevant.  depends on the UI though.
    23162318    $args{sortby} = 'revnet' if !$args{sortby} || !grep /^$args{sortby}$/, ('revnet','group','status');
    2317     $sql = "SELECT rdns_id,revnet,status,groups.group_name AS group FROM revzones".
     2319    $sql = "SELECT rdns_id AS zoneid,revnet AS zone,status,groups.group_name AS group FROM revzones".
    23182320        " INNER JOIN groups ON revzones.group_id=groups.group_id".
    23192321        " WHERE revzones.group_id IN ($args{curgroup}".($args{childlist} ? ",$args{childlist}" : '').")".
     
    23252327        ($args{offset} eq 'all' ? '' : " LIMIT $self->{perpage}".
    23262328        " OFFSET ".$args{offset}*$self->{perpage});
    2327   my $sth = $dbh->prepare($sql);
    2328   $sth->execute(@filterargs);
    2329   my $rownum = 0;
    2330 
    2331   while (my @data = $sth->fetchrow_array) {
    2332     my %row;
    2333     $row{domain_id} = $data[0];
    2334     $row{domain} = $data[1];
    2335     $row{status} = $data[2];
    2336     $row{group} = $data[3];
    2337     push @zonelist, \%row;
    2338   }
    2339 
    2340   return \@zonelist;
     2329
     2330  my $ret = $dbh->selectall_arrayref($sql, { Slice => {} }, @filterargs);
     2331  return $ret;
    23412332} # end getZoneList()
    23422333
     
    47124703      } elsif ($type eq 'NS') {
    47134704# hmm.  should we warn here if subdomain NS'es are left alone?
    4714         next if ($args{rwns} && ($rr->name eq $zone));
    47154705        if ($rev eq 'y') {
    47164706          # revzones have records more or less reversed from forward zones.
    47174707          my ($tmpcode,$tmpmsg) = _zone2cidr($host);
    47184708          die "Error converting NS record: $tmpmsg\n" if $tmpcode eq 'FAIL';    # hmm.  may not make sense...
     4709          next if ($args{rwns} && ($tmpmsg eq "$cidr"));
    47194710          $val = "$tmpmsg";
    47204711          $host = $rr->nsdname;
     
    47264717#}
    47274718        } else {
     4719          next if ($args{rwns} && ($rr->name eq $zone));
    47284720          $val = $rr->nsdname;
    47294721        }
     
    48794871    if ($args{rwsoa}) {
    48804872      $soaflag = 1;
    4881       my $sthgetsoa = $dbh->prepare("SELECT host,val,ttl FROM default_records WHERE group_id=? AND type=?");
    4882       my $sthputsoa = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,ttl) VALUES (?,?,?,?,?)");
     4873      my $sthgetsoa = $dbh->prepare("SELECT host,val,ttl FROM "._rectable('y', $rev)." WHERE group_id=? AND type=?");
     4874      my $sthputsoa = $dbh->prepare("INSERT INTO records (".
     4875        ($rev eq 'n' ? 'domain_id' : 'rdns_id').",host,type,val,ttl) VALUES (?,?,?,?,?)");
    48834876      $sthgetsoa->execute($group,$reverse_typemap{SOA});
    48844877      while (my ($host,$val,$ttl) = $sthgetsoa->fetchrow_array()) {
    4885         $host =~ s/DOMAIN/$zone/g;
    4886         $val =~ s/DOMAIN/$zone/g;
     4878        if ($rev eq 'n') {
     4879          $host =~ s/DOMAIN/$zone/g;
     4880          $val =~ s/DOMAIN/$zone/g;     # arguably useless
     4881        } else {
     4882          $host =~ s/ADMINDOMAIN/$self->{domain}/g;
     4883        }
    48874884        $sthputsoa->execute($zone_id,$host,$reverse_typemap{SOA},$val,$ttl);
    48884885      }
    48894886    }
    48904887
    4891     # Overwrite NS records
     4888    # Add standard NS records.  The old one(s) should have been skipped by this point.
    48924889    if ($args{rwns}) {
    48934890      $nsflag = 1;
    4894       my $sthgetns = $dbh->prepare("SELECT host,val,ttl FROM default_records WHERE group_id=? AND type=?");
    4895       my $sthputns = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,ttl) VALUES (?,?,?,?,?)");
     4891      my $sthgetns = $dbh->prepare("SELECT host,val,ttl FROM "._rectable('y',$rev)." WHERE group_id=? AND type=?");
     4892      my $sthputns = $dbh->prepare("INSERT INTO records (".
     4893        ($rev eq 'n' ? 'domain_id' : 'rdns_id').",host,type,val,ttl) VALUES (?,?,?,?,?)");
    48964894      $sthgetns->execute($group,$reverse_typemap{NS});
    48974895      while (my ($host,$val,$ttl) = $sthgetns->fetchrow_array()) {
    4898         $host =~ s/DOMAIN/$zone/g;
    4899         $val =~ s/DOMAIN/$zone/g;
     4896        if ($rev eq 'n') {
     4897          $host =~ s/DOMAIN/$zone/g;
     4898          $val =~ s/DOMAIN/$zone/g;     #hmm.
     4899        } else {
     4900          $host =~ s/ADMINDOMAIN/$self->{domain}/g;     #hmm.
     4901          $val =~ s/ZONE/$cidr/g;
     4902        }
    49004903        $sthputns->execute($zone_id,$host,$reverse_typemap{NS},$val,$ttl);
    49014904      }
     
    50535056        "FROM records WHERE rdns_id=? AND type=6");
    50545057  my $recsth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location,extract(epoch from stamp),expires,stampactive ".
    5055         "FROM records WHERE rdns_id=? AND not type=6 ".
     5058        "FROM records WHERE rdns_id=? AND NOT type=6 ".
    50565059        "ORDER BY masklen(CAST(val AS inet)) DESC, CAST(val AS inet)");
    50575060  my $revsth = $dbh->prepare("SELECT rdns_id,revnet,status,changed FROM revzones WHERE status=1 ".
     
    50895092        $soasth->execute($revid);
    50905093        my (@zsoa) = $soasth->fetchrow_array();
    5091         _printrec_tiny($zonefilehandle,'y',\%recflags,$revzone,
     5094        _printrec_tiny($zonefilehandle, $zsoa[7], 'y',\%recflags,$revzone,
    50925095          $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
    50935096
    50945097        $recsth->execute($revid);
    5095         while (my ($host,$type,$val,$dist,$weight,$port,$ttl,$recid,$loc,$stamp,$expires,$stampactive) = $recsth->fetchrow_array) {
     5098        while (my ($host, $type, $val, $dist, $weight, $port, $ttl, $recid, $loc, $stamp, $expires, $stampactive)
     5099                = $recsth->fetchrow_array) {
    50965100          next if $recflags{$recid};
    50975101
     
    51065110#         }
    51075111
    5108           _printrec_tiny($zonefilehandle, 'y', \%recflags, $revzone,
     5112          _printrec_tiny($zonefilehandle, $recid, 'y', \%recflags, $revzone,
    51095113            $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    51105114
     
    51485152  } # while ($revsth)
    51495153
     5154  $soasth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ".
     5155        "FROM records WHERE domain_id=? AND type=6");
     5156  $recsth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location,extract(epoch from stamp),expires,stampactive ".
     5157        "FROM records WHERE domain_id=? AND NOT type=6");       # Just exclude all types relating to rDNS
     5158#       "FROM records WHERE domain_id=? AND type < 65280");     # Just exclude all types relating to rDNS
    51505159  my $domsth = $dbh->prepare("SELECT domain_id,domain,status,changed FROM domains WHERE status=1 ORDER BY domain_id");
    5151   $recsth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location,extract(epoch from stamp),expires,stampactive ".
    5152         "FROM records WHERE domain_id=?");      # Just exclude all types relating to rDNS
    5153 #       "FROM records WHERE domain_id=? AND type < 65280");     # Just exclude all types relating to rDNS
    51545160  $zonesth = $dbh->prepare("UPDATE domains SET changed='n' WHERE domain_id=?");
    51555161  $domsth->execute();
     
    51775183        }
    51785184
     5185        # need to fetch this separately so the SOA comes first in the flatfile....
     5186        # Just In Case we need/want to reimport from the flatfile later on.
     5187        $soasth->execute($domid);
     5188        my (@zsoa) = $soasth->fetchrow_array();
     5189        _printrec_tiny($zonefilehandle, $zsoa[7], 'n',\%recflags,$dom,
     5190          $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
     5191
    51795192        $recsth->execute($domid);
    51805193        while (my ($host,$type,$val,$dist,$weight,$port,$ttl,$recid,$loc,$stamp,$expires,$stampactive) = $recsth->fetchrow_array) {
     
    51905203          }
    51915204
    5192           _printrec_tiny($zonefilehandle, 'n', \%recflags,
     5205          _printrec_tiny($zonefilehandle, $recid, 'n', \%recflags,
    51935206                $dom, $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
    51945207
     
    52395252# Utility sub for __export_tiny above
    52405253sub _printrec_tiny {
    5241   my ($datafile,$revrec,$recflags,$zone,$host,$type,$val,$dist,$weight,$port,$ttl,$loc,$stamp,$expires,$stampactive) = @_;
     5254  my ($datafile, $recid, $revrec, $recflags, $zone, $host, $type, $val, $dist, $weight, $port, $ttl,
     5255        $loc, $stamp, $expires, $stampactive) = @_;
    52425256
    52435257  $loc = '' if !$loc;   # de-nullify - just in case
  • branches/stable/dns.cgi

    r562 r582  
    275275      # and reconstruct the URI argument list.
    276276      my %target = (page => "domlist");
    277       if ($webvar{target} && $webvar{target} =~ /\?/) {
     277      if ($webvar{target} && $webvar{target} =~ /\?/ && $webvar{target} !~ /page=login/) {
    278278        my $tmp = (split /\?/, $webvar{target})[1];
    279279        $tmp =~ s/^\&//;
     
    20042004
    20052005  foreach my $rec (@$foo2) {
     2006    # NS records.  Need to do this first before we convert the type-value to the text representation
     2007    if ($rev eq 'y' && $dnsdb->{showrec_arpa_ns} && $rec->{type} == $reverse_typemap{NS}) {
     2008      my $tmp = new NetAddr::IP $rec->{val};
     2009      $rec->{val} = DNSDB::_ZONE($tmp, 'ZONE', 'r', '.').($tmp->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
     2010    }
    20062011    $rec->{type} = $typemap{$rec->{type}};
    20072012    $rec->{fwdzone} = $rev eq 'n';
  • branches/stable/dnsdb.conf

    r562 r582  
    3838#lowercase = 0
    3939
     40# Show formal .arpa zone name instead of usual CIDR for reverse zone NS records?
     41#showrec_arpa_ns = 0
     42
    4043## General RPC options
    4144# may already be obsolete.  how do we want to run RPC requests?
  • branches/stable/export.pl

    r548 r582  
    2929my $dnsdb = new DNSDB;
    3030
     31#open TINYDATA, ">small/tinydata";
    3132open TINYDATA, ">tinydata";
    3233
    33 $dnsdb->export('tiny', *TINYDATA);
     34$dnsdb->export('tiny', *TINYDATA) or die "fatal: ".$dnsdb->errstr."\n";
  • branches/stable/templates/bulkdomain.tmpl

    r548 r582  
    3838<table>
    3939<tr>
    40 <TMPL_LOOP NAME=domtable><td><input type="checkbox" name="dom_<TMPL_VAR NAME=domain_id>" value="<TMPL_VAR NAME=domain_id>" /> <TMPL_VAR NAME=domain></td>
     40<TMPL_LOOP NAME=domtable><td><input type="checkbox" name="dom_<TMPL_VAR NAME=zoneid>" value="<TMPL_VAR NAME=zoneid>" /> <TMPL_VAR NAME=zone></td>
    4141<TMPL_IF newrow></tr>
    4242<tr>
  • branches/stable/templates/domlist.tmpl

    r548 r582  
    3939<TMPL_LOOP name=domtable>
    4040<tr class="row<TMPL_IF __odd__>0<TMPL_ELSE>1</TMPL_IF>">
    41         <td align="left"><a href="<TMPL_VAR NAME=script_self>&amp;page=reclist&amp;id=<TMPL_VAR NAME=domain_id>&amp;defrec=n<TMPL_UNLESS domlist>&amp;revrec=y</TMPL_UNLESS>"><TMPL_VAR NAME=domain></a></td>
     41        <td align="left"><a href="<TMPL_VAR NAME=script_self>&amp;page=reclist&amp;id=<TMPL_VAR NAME=zoneid>&amp;defrec=n<TMPL_UNLESS domlist>&amp;revrec=y</TMPL_UNLESS>"><TMPL_VAR NAME=zone></a></td>
    4242        <td><TMPL_IF status>Active<TMPL_ELSE>Inactive</TMPL_IF></td>
    4343        <td><TMPL_VAR name=group></td>
    44 <TMPL_IF domain_edit>   <td align="center"><a href="<TMPL_VAR NAME=script_self>&amp;page=<TMPL_VAR NAME=curpage><TMPL_IF NAME=offset>&amp;offset=<TMPL_VAR NAME=offset></TMPL_IF>&amp;id=<TMPL_VAR NAME=domainid>&amp;zonestatus=<TMPL_IF status>domoff<TMPL_ELSE>domon</TMPL_IF>"><TMPL_IF status>deactivate<TMPL_ELSE>activate</TMPL_IF></a></td></TMPL_IF>
    45 <TMPL_IF domain_delete> <td align="center"><a href="<TMPL_VAR NAME=script_self>&amp;page=<TMPL_IF domlist>deldom<TMPL_ELSE>delrevzone</TMPL_IF>&amp;id=<TMPL_VAR NAME=domain_id>"><img src="images/trash2.png" alt="[ Delete ]" /></a></td></TMPL_IF>
     44<TMPL_IF domain_edit>   <td align="center"><a href="<TMPL_VAR NAME=script_self>&amp;page=<TMPL_VAR NAME=curpage><TMPL_IF NAME=offset>&amp;offset=<TMPL_VAR NAME=offset></TMPL_IF>&amp;id=<TMPL_VAR NAME=zoneid>&amp;zonestatus=<TMPL_IF status>domoff<TMPL_ELSE>domon</TMPL_IF>"><TMPL_IF status>deactivate<TMPL_ELSE>activate</TMPL_IF></a></td></TMPL_IF>
     45<TMPL_IF domain_delete> <td align="center"><a href="<TMPL_VAR NAME=script_self>&amp;page=<TMPL_IF domlist>deldom<TMPL_ELSE>delrevzone</TMPL_IF>&amp;id=<TMPL_VAR NAME=zoneid>"><img src="images/trash2.png" alt="[ Delete ]" /></a></td></TMPL_IF>
    4646</tr>
    4747</TMPL_LOOP>
  • branches/stable/textrecs.cgi

    r550 r582  
    7575
    7676my $reclist = $dnsdb->getRecList(defrec => $webvar{defrec}, revrec => $webvar{revrec}, id => $webvar{id},
    77         sortby => ($webvar{revrec} eq 'n' ? 'type,host' : 'type,val'), sortorder => 'ASC');
     77        sortby => ($webvar{revrec} eq 'n' ? 'type,host' : 'type,val'), sortorder => 'ASC', offset => 'all');
    7878foreach my $rec (@$reclist) {
    7979  $rec->{type} = $typemap{$rec->{type}};
     
    8383  $rec->{val} = "$rec->{distance}  $rec->{weight}  $rec->{port}  $rec->{val}" if $rec->{type} eq 'SRV';
    8484  if ($webvar{revrec} eq 'y') {
     85    if ($dnsdb->{showrec_arpa_ns} && $rec->{type} eq 'NS') {
     86      my $tmp = new NetAddr::IP $rec->{val};
     87      $rec->{val} = DNSDB::_ZONE($tmp, 'ZONE', 'r', '.').($tmp->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
     88    }
    8589    printf "%-16s\t%d\t%s\t%s\n", $rec->{val}, $rec->{ttl}, $rec->{type}, $rec->{host};
    8690  } else {
  • branches/stable/tiny-import.pl

    r548 r582  
    3939        conv    => 0,
    4040        trial   => 0,
    41         legacy  => 0,
     41        legacy  => 0,
     42        merge   => 0,
     43        group   => 1,
    4244        );
     45my $gnum = '';
    4346# Handle some command-line arguments
    4447while ($ARGV[0] =~ /^-/) {
    4548  my $arg = shift @ARGV;
    46   usage() if $arg !~ /^-[rclt]+$/;
     49  usage() if $arg !~ /^-(?:[rclmt]+|g\d*)$/;
    4750  # -r  rewrite imported files to comment imported records
    4851  # -c  coerce/downconvert A+PTR = records to PTR
    4952  # -l  swallow A+PTR as-is
     53  # -m  merge PTR and A/AAAA as possible
    5054  # -t  trial mode;  don't commit to DB or actually rewrite flatfile (disables -r)
     55  # -g  import to specified group (name or ID) instead of group 1
    5156  $arg =~ s/^-//;
    52   my @tmp = split //, $arg;
    53   foreach (@tmp) {
    54     $importcfg{rw} = 1 if $_ eq 'r';
    55     $importcfg{conv} = 1 if $_ eq 'c';
    56     $importcfg{legacy} = 1 if $_ eq 'l';
    57     $importcfg{trial} = 1 if $_ eq 't';
    58   }
     57# for Reasons (none clear), $arg is undefined yet defined, but only when number characters are involved.  Ebbeh?
     58no warnings qw(uninitialized);
     59  if ($arg =~ /^g/) {
     60    if ($arg eq 'g') {
     61      $importcfg{group} = shift @ARGV;
     62    } else {
     63      $arg =~ s/^g//;
     64      $importcfg{group} = $arg;
     65    }
     66  } else {
     67    my @tmp = split //, $arg;
     68    foreach (@tmp) {
     69      $importcfg{rw} = 1 if $_ eq 'r';
     70      $importcfg{conv} = 1 if $_ eq 'c';
     71      $importcfg{legacy} = 1 if $_ eq 'l';
     72      $importcfg{merge} = 1 if $_ eq 'm';
     73      $importcfg{trial} = 1 if $_ eq 't';
     74    }
     75  }
     76  use warnings qw(uninitialized);
    5977}
    6078$importcfg{rw} = 0 if $importcfg{trial};
    6179
     80# allow group names
     81if ($importcfg{group} =~ /^\d+$/) {
     82  $importcfg{groupname} = $dnsdb->groupName($importcfg{group});
     83} else {
     84  $importcfg{groupname} = $importcfg{group};
     85  $importcfg{group} = $dnsdb->groupID($importcfg{groupname});
     86}
     87
     88die usage() if $importcfg{group} !~ /^\d+$/;
     89
    6290sub usage {
    63   die q(usage:  tiny-import.pl [-r] [-c] datafile1 datafile2 ... datafileN ...
     91  die q(usage:  tiny-import.pl [-rclt] [-gnn] [-g name] datafile1 datafile2 ... datafileN ...
    6492        -r  Rewrite all specified data files with a warning header indicating the
    6593            records are now managed by web, and commenting out all imported records.
     
    72100        -l  (for "legacy")  Force import of A+PTR records as-is.  Mutually exclusive
    73101            with -c.  -l takes precedence as -c is lossy.
     102        -m  Merge PTR and A or AAAA records to A+PTR or AAAA+PTR records where possible
     103        -gnnn or -g nnn or -g name
     104            Import new zones into this group (group name or ID accepted) instead of
     105            the root/default group 1
    74106        -t  Trial run mode;  spits out records that would be left unimported.
    75107            Disables -r if set.
     
    83115my $code;
    84116my $dbh = $dnsdb->{dbh};
     117
     118# collect some things for logging
     119($dnsdb->{logusername}, undef, undef, undef, undef, undef, $dnsdb->{logfullname}) = getpwuid($<);
     120$dnsdb->{loguserid} = 0;        # not worth setting up a pseudouser the way the RPC system does
     121$dnsdb->{logusername} = $dnsdb->{logusername}."/tiny-import.pl";
     122$dnsdb->{logfullname} = $dnsdb->{logusername} if !$dnsdb->{logfullname};
    85123
    86124$dbh->{AutoCommit} = 0;
     
    129167  our $recsth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl,location,stamp,expires,stampactive) ".
    130168        " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
     169
     170  # for A/AAAA records
     171  our $revcheck = $dbh->prepare("SELECT rdns_id,record_id,ttl FROM records WHERE host=? AND val=? AND type=12");
     172  our $mergefwd = $dbh->prepare("UPDATE records SET type=?,domain_id=?,ttl=? WHERE record_id=?");
     173  # for PTR records
     174  our $fwdcheck = $dbh->prepare("SELECT domain_id,record_id,ttl FROM records WHERE host=? AND val=? AND (type=1 OR type=28)");
     175  our $mergerev = $dbh->prepare("UPDATE records SET type=?,rdns_id=?,ttl=? WHERE record_id=?");
    131176
    132177  my %deleg;
     
    451496        ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ("$msg"));
    452497      }
     498
    453499      if ($rparent) {
    454         ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y');
    455         $recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
     500##fixme:  really want to pull this DB call inside an if $importcfg{merge},
     501# but then we need to duplicate the insert for the case where the matching
     502# reverse doesn't exist.
     503        $host =~ s/\.$//g;   # pure sytactic sugar, we don't store this trailing dot.
     504        $fwdcheck->execute($host, $msg->addr);
     505        my ($domid, $recid, $rttl) = $fwdcheck->fetchrow_array;
     506        if ($importcfg{merge} && $domid) {
     507          $ttl = ($rttl < $ttl ? $rttl : $ttl);        # Take the shorter TTL
     508          $mergerev->execute(($msg->{isv6} ? 65281 : 65280), $rparent, $ttl, $recid);
     509          $dnsdb->_log(rdns_id => $rparent, domain_id => $domid, group_id => $importcfg{group},
     510            entry => "[ import ] PTR ".$msg->addr." -> $host merged with matching ".
     511                  ($msg->{isv6} ? 'AAAA' : 'A')." record");
     512        } else {
     513          ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $rparent, 'y');
     514          $recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
     515        }
    456516      } else {
    457517        push @deferred, $rec unless $nodefer;
     
    476536      my $domid = $dnsdb->_hostparent($host);
    477537      if ($domid) {
    478         ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n');
    479         $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
     538##fixme:  really want to pull this DB call inside an if $importcfg{merge},
     539# but then we need to duplicate the insert for the case where the matching
     540# reverse doesn't exist.
     541        $revcheck->execute($host, $ip);
     542        my ($revid, $recid, $rttl) = $revcheck->fetchrow_array;
     543        if ($importcfg{merge} && $revid) {
     544          $ttl = ($rttl < $ttl ? $rttl : $ttl); # Take the shorter TTL
     545          $mergefwd->execute(65280, $domid, $ttl, $recid);
     546          $dnsdb->_log(rdns_id => $revid, domain_id => $domid, group_id => $importcfg{group},
     547            entry => "[ import ] ".($msg->{isv6} ? 'AAAA' : 'A')." record $host -> $ip".
     548                  " merged with matching PTR record");
     549        } else {
     550          ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $domid, 'n');
     551          $recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
     552        }
    480553      } else {
    481554        push @deferred, $rec unless $nodefer;
     
    514587      if ($zone =~ /\.arpa$/) {
    515588        ($code,$msg) = DNSDB::_zone2cidr($zone);
    516         $dbh->do("INSERT INTO revzones (revnet,group_id,status,default_location) VALUES (?,1,1,?)",
    517                 undef, ($msg, $loc));
     589        $dbh->do("INSERT INTO revzones (revnet,group_id,status,default_location) VALUES (?,?,1,?)",
     590                undef, ($msg, $importcfg{group}, $loc));
    518591        my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
    519592        my $newttl;
     
    523596                $loc, $stamp, $expires, $stampactive);
    524597      } else {
    525         $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,1,1,?)",
    526                 undef, ($zone, $loc));
     598        $dbh->do("INSERT INTO domains (domain,group_id,status,default_location) VALUES (?,?,1,?)",
     599                undef, ($zone, $importcfg{group}, $loc));
    527600        my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
    528601        my $newttl;
     
    748821        my $fparent = $dnsdb->_hostparent($fqdn);
    749822
    750         if ($fparent) {
    751           ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $fparent, 'n');
    752           $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
    753         } else {
    754           push @deferred, $rec unless $nodefer;
    755           $impok = 0;
    756         }
     823##fixme:  really want to pull this DB call inside an if $importcfg{merge},
     824# but then we need to duplicate the insert for the case where the matching
     825# reverse doesn't exist.
     826        $revcheck->execute($fqdn, $val);
     827        my ($revid, $recid, $rttl) = $revcheck->fetchrow_array;
     828
     829        # If we have a revzone and merging is enabled, update the existing
     830        # record with a reverse ID, set the type to one of the internal
     831        # pseudotypes, and set the TTL to the lower of the two.
     832        if ($importcfg{merge} && $revid) {
     833          $ttl = ($rttl < $ttl ? $rttl : $ttl); # Take the shorter TTL
     834          $mergefwd->execute(65281, $fparent, $ttl, $recid);
     835          $dnsdb->_log(rdns_id => $revid, domain_id => $fparent, group_id => $importcfg{group},
     836            entry => "[ import ] ".($msg->{isv6} ? 'AAAA' : 'A')." record $fqdn -> $val".
     837                  " merged with matching PTR record");
     838        } else {
     839          if ($fparent) {
     840            ($ttl, $stampactive, $expires, $stamp) = calcstamp($stamp, $ttl, $fparent, 'n');
     841            $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl, $loc, $stamp, $expires, $stampactive);
     842          } else {
     843            push @deferred, $rec unless $nodefer;
     844            $impok = 0;
     845          }
     846        }
    757847
    758848      } elsif ($type == 16) {
Note: See TracChangeset for help on using the changeset viewer.