Changeset 375 for trunk/DNSDB.pm


Ignore:
Timestamp:
08/08/12 18:04:47 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Checkpoint adding locations/views. See #10.

  • Tweak new locations table again with an internal serial for sorting and finding the "highest" previous location
  • Complete "Add location" stub
  • Fill in initial display page for "Edit location"
  • Tweak location add/edit/update template a little, add new comments field to match new field in table
  • Tweak location import so we don't end up with great long space-free fields similar to what can't be avoided with TXT (usually SPF/DomainKeys) records
  • Fill in addLoc() stub
  • Add getLoc()
  • Tweak location export to strip commas and whitespace properly
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r372 r375  
    4747        &addUser &updateUser &delUser &userFullName &userStatus &getUserData
    4848        &getUserCount &getUserList &getUserDropdown
    49         &addLoc &updateLoc &delLoc
     49        &addLoc &updateLoc &delLoc &getLoc
    5050        &getLocCount &getLocList &getLocDropdown
    5151        &getSOA &updateSOA &getRecLine &getDomRecs &getRecCount
     
    7474                &addUser &updateUser &delUser &userFullName &userStatus &getUserData
    7575                &getUserCount &getUserList &getUserDropdown
    76                 &addLoc &updateLoc &delLoc
     76                &addLoc &updateLoc &delLoc &getLoc
    7777                &getLocCount &getLocList &getLocDropdown
    7878                &getSOA &updateSOA &getRecLine &getDomRecs &getRecCount
     
    28952895  $sth->execute($uid);
    28962896  return $sth->fetchrow_hashref();
    2897 
    28982897} # end getUserData()
    28992898
    29002899
    29012900## DNSDB::addLoc()
    2902 sub addLoc {}
     2901# Add a new location.
     2902# Takes a database handle, group ID, short and long description, and a comma-separated
     2903# list of IP addresses.
     2904# Returns ('OK',<location>) on success, ('FAIL',<failmsg>) on failure
     2905sub addLoc {
     2906  my $dbh = shift;
     2907  my $grp = shift;
     2908  my $shdesc = shift;
     2909  my $comments = shift;
     2910  my $iplist = shift;
     2911
     2912  # $shdesc gets set to the generated location ID if possible, but these can be de-undefined here.
     2913  $comments = '' if !$comments;
     2914  $iplist = '' if !$iplist;
     2915
     2916  my $loc;
     2917
     2918  # Generate a location ID.  This is, by spec, a two-character widget.  We'll use [a-z][a-z]
     2919  # for now;  676 locations should satisfy all but the largest of the huge networks.
     2920  # Not sure whether these are case-sensitive, or what other rules might apply - in any case
     2921  # the absolute maximum is 16K (256*256) since it's parsed by tinydns as a two-character field.
     2922
     2923# add just after "my $origloc = $loc;":
     2924#    # These expand the possible space from 26^2 to 52^2 [* note in testing only 2052 were achieved],
     2925#    # and wrap it around.
     2926#    # Yes, they skip a couple of possibles.  No, I don't care.
     2927#    $loc = 'aA' if $loc eq 'zz';
     2928#    $loc = 'Aa' if $loc eq 'zZ';
     2929#    $loc = 'ZA' if $loc eq 'Zz';
     2930#    $loc = 'aa' if $loc eq 'ZZ';
     2931
     2932  # Allow transactions, and raise an exception on errors so we can catch it later.
     2933  # Use local to make sure these get "reset" properly on exiting this block
     2934  local $dbh->{AutoCommit} = 0;
     2935  local $dbh->{RaiseError} = 1;
     2936
     2937##fixme:  There is probably a far better way to do this.  Sequential increments
     2938# are marginally less stupid that pure random generation though, and the existence
     2939# check makes sure we don't stomp on an imported one.
     2940
     2941  eval {
     2942    # Get the "last" location.  Note this is the only use for loc_id, because selecting on location Does Funky Things
     2943    ($loc) = $dbh->selectrow_array("SELECT location FROM locations ORDER BY loc_id DESC LIMIT 1");
     2944    ($loc) = ($loc =~ /^(..)/);
     2945    my $origloc = $loc;
     2946    # Make a change...
     2947    $loc++;
     2948    # ... and keep changing if it exists
     2949    while ($dbh->selectrow_array("SELECT count(*) FROM locations WHERE location LIKE ?", undef, ($loc.'%'))) {
     2950      $loc++;
     2951      ($loc) = ($loc =~ /^(..)/);
     2952      die "too many locations in use, can't add another one\n" if $loc eq $origloc;
     2953##fixme: really need to handle this case faster somehow
     2954#if $loc eq $origloc die "<thwap> bad admin:  all locations used, your network is too fragmented";
     2955    }
     2956    # And now we should have a unique location.  tinydns fundamentally limits the
     2957    # number of these but there's no doc on what characters are valid.
     2958    $shdesc = $loc if !$shdesc;
     2959    $dbh->do("INSERT INTO locations (location, group_id, iplist, description, comments) VALUES (?,?,?,?,?)",
     2960        undef, ($loc, $grp, $iplist, $shdesc, $comments) );
     2961  };
     2962  if ($@) {
     2963    my $msg = $@;
     2964    eval { $dbh->rollback; };
     2965    if ($config{log_failures}) {
     2966      $shdesc = $loc if !$shdesc;
     2967      _log($dbh, (entry => "Failed adding location ($shdesc, '$iplist'): $msg"));
     2968      $dbh->commit;
     2969    }
     2970    return ('FAIL',$msg);
     2971  }
     2972
     2973  return ('OK',$loc);
     2974}
     2975
    29032976
    29042977## DNSDB::updateLoc()
     
    29072980## DNSDB::delLoc()
    29082981sub delLoc {}
     2982
     2983
     2984## DNSDB::getLoc()
     2985sub getLoc {
     2986  my $dbh = shift;
     2987  my $loc = shift;
     2988
     2989  my $sth = $dbh->prepare("SELECT group_id,iplist,description,comments FROM locations WHERE location=?");
     2990  $sth->execute($loc);
     2991  return $sth->fetchrow_hashref();
     2992} # end getLoc()
    29092993
    29102994
     
    42964380  my $lochash = $dbh->selectall_hashref("SELECT location,iplist FROM locations", 'location');
    42974381  foreach my $location (keys %$lochash) {
    4298     foreach my $ipprefix (split /,/, $lochash->{$location}{iplist}) {
     4382    foreach my $ipprefix (split /[,\s]+/, $lochash->{$location}{iplist}) {
     4383      $ipprefix =~ s/\s+//g;
    42994384      print $datafile "%$location:$ipprefix\n";
    43004385    }
Note: See TracChangeset for help on using the changeset viewer.