Changeset 375 for trunk/DNSDB.pm
- Timestamp:
- 08/08/12 18:04:47 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/DNSDB.pm
r372 r375 47 47 &addUser &updateUser &delUser &userFullName &userStatus &getUserData 48 48 &getUserCount &getUserList &getUserDropdown 49 &addLoc &updateLoc &delLoc 49 &addLoc &updateLoc &delLoc &getLoc 50 50 &getLocCount &getLocList &getLocDropdown 51 51 &getSOA &updateSOA &getRecLine &getDomRecs &getRecCount … … 74 74 &addUser &updateUser &delUser &userFullName &userStatus &getUserData 75 75 &getUserCount &getUserList &getUserDropdown 76 &addLoc &updateLoc &delLoc 76 &addLoc &updateLoc &delLoc &getLoc 77 77 &getLocCount &getLocList &getLocDropdown 78 78 &getSOA &updateSOA &getRecLine &getDomRecs &getRecCount … … 2895 2895 $sth->execute($uid); 2896 2896 return $sth->fetchrow_hashref(); 2897 2898 2897 } # end getUserData() 2899 2898 2900 2899 2901 2900 ## 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 2905 sub 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 2903 2976 2904 2977 ## DNSDB::updateLoc() … … 2907 2980 ## DNSDB::delLoc() 2908 2981 sub delLoc {} 2982 2983 2984 ## DNSDB::getLoc() 2985 sub 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() 2909 2993 2910 2994 … … 4296 4380 my $lochash = $dbh->selectall_hashref("SELECT location,iplist FROM locations", 'location'); 4297 4381 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; 4299 4384 print $datafile "%$location:$ipprefix\n"; 4300 4385 }
Note:
See TracChangeset
for help on using the changeset viewer.