Changeset 375 for trunk


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
Location:
trunk
Files:
6 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    }
  • trunk/dns-1.0-1.2.sql

    r372 r375  
    44CREATE TABLE locations (
    55    location character varying (4) PRIMARY KEY,
     6    loc_id serial UNIQUE,
    67    group_id integer NOT NULL DEFAULT 1,
    78    iplist text NOT NULL DEFAULT '',
    8     description text NOT NULL DEFAULT ''
     9    description character varying(40) NOT NULL DEFAULT '',
     10    comments text NOT NULL DEFAULT ''
    911);
    1012
  • trunk/dns.cgi

    r374 r375  
    13681368
    13691369  if ($webvar{locact} eq 'new') {
    1370 
     1370    # uuhhmm....
    13711371  } elsif ($webvar{locact} eq 'add') {
    13721372    changepage(page => "loclist", errmsg => "You are not permitted to add locations/views", id => $webvar{parentid})
    13731373        unless ($permissions{admin} || $permissions{location_create});
    13741374
    1375 ##work
    1376     my ($code,$msg) = addLocation($dbh, $webvar{parentid}, $webvar{locname}, $webvar{iplist});
     1375    my ($code,$msg) = addLoc($dbh, $curgroup, $webvar{locname}, $webvar{comments}, $webvar{iplist});
    13771376
    13781377    if ($code eq 'OK' || $code eq 'WARN') {
     
    13881387      $page->param(todo         => "Add location/view");
    13891388      $page->param(locact       => "add");
    1390       $page->param(parentid     => $webvar{parentid});
    13911389      $page->param(id           => $webvar{id});
    1392       fill_recdata();   # populate the form... er, mostly.
     1390      $page->param(locname      => $webvar{locname});
     1391      $page->param(comments     => $webvar{comments});
     1392      $page->param(iplist       => $webvar{iplist});
    13931393    }
    13941394
     
    13961396    changepage(page => "loclist", errmsg => "You are not permitted to edit locations/views", id => $webvar{parentid})
    13971397        unless ($permissions{admin} || $permissions{location_edit});
     1398
     1399    my $loc = getLoc($dbh, $webvar{loc});
     1400    $page->param(wastrying      => "adding");
     1401    $page->param(todo           => "Edit location/view");
     1402    $page->param(locact         => "update");
     1403    $page->param(id             => $webvar{loc});
     1404    $page->param(locname        => $loc->{description});
     1405    $page->param(comments       => $loc->{comments});
     1406    $page->param(iplist         => $loc->{iplist});
     1407
    13981408  } elsif ($webvar{locact} eq 'update') {
    13991409    changepage(page => "loclist", errmsg => "You are not permitted to edit locations/views", id => $webvar{parentid})
    14001410        unless ($permissions{admin} || $permissions{location_edit});
     1411##work
    14011412  } else {
    14021413    changepage(page => "loclist", errmsg => "You are not permitted to add locations/views", id => $webvar{parentid})
     
    14051416    $page->param(todo => "Add location/view");
    14061417    $page->param(locact => "add");
    1407     $page->param(parentid => $webvar{parentid});
    1408 
    14091418    $page->param(locname => ($webvar{locname} ? $webvar{locname} : ''));
    14101419    $page->param(iplist => ($webvar{iplist} ? $webvar{iplist} : ''));
  • trunk/dns.sql

    r370 r375  
    1919CREATE TABLE locations (
    2020    location character varying (4) PRIMARY KEY,
     21    loc_id serial UNIQUE,
    2122    group_id integer NOT NULL DEFAULT 1,
    2223    iplist text NOT NULL DEFAULT '',
    23     description text NOT NULL DEFAULT ''
     24    description character varying(40) NOT NULL DEFAULT '',
     25    comments text NOT NULL DEFAULT ''
    2426);
    2527
  • trunk/templates/location.tmpl

    r374 r375  
    1414<input type="hidden" name="page" value="location" />
    1515<input type="hidden" name="sid" value="<TMPL_VAR NAME=sid>" />
    16 <input type="hidden" name="parentid" value="<TMPL_VAR NAME=parentid>" />
    17 <input type="hidden" name="id" value="<TMPL_VAR NAME=id>" />
     16<TMPL_IF id><input type="hidden" name="id" value="<TMPL_VAR NAME=id>" /></TMPL_IF>
    1817<input type="hidden" name="locact" value="<TMPL_VAR NAME=locact>" />
    1918
     
    2625        <tr class="datalinelight">
    2726                <td>Location name/description</td>
    28                 <td><input type="text" name="locname" value="<TMPL_VAR ESCAPE=HTML NAME=locname>" size="30" /></td>
     27                <td><input type="text" name="locname" value="<TMPL_VAR ESCAPE=HTML NAME=locname>" size="30" maxlength="40" /></td>
    2928        </tr>
    3029        <tr class="datalinelight">
    3130                <td>IP list</td>
    3231                <td><input type="text" name="iplist" value="<TMPL_VAR ESCAPE=HTML NAME=iplist>" size="30" /></td>
     32        </tr>
     33        <tr class="datalinelight">
     34                <td>Comments</td>
     35                <td><textarea name="comments" cols="50" rows="5"><TMPL_VAR ESCAPE=HTML NAME=comments></textarea></td>
    3336        </tr>
    3437        <tr class="datalinelight">
  • trunk/tiny-import.pl

    r373 r375  
    525525      if (my ($iplist) = $dbh->selectrow_array("SELECT iplist FROM locations WHERE location = ?", undef, ($loc))) {
    526526        if ($cnet) {
    527           $iplist .= ",$cnet";
     527          $iplist .= ", $cnet";
    528528          $dbh->do("UPDATE locations SET iplist = ? WHERE location = ?", undef, ($iplist, $loc));
    529529        } else {
Note: See TracChangeset for help on using the changeset viewer.