Changeset 924 for branches/secondaryzones/DNSDB.pm
- Timestamp:
- 08/14/25 18:06:51 (10 hours ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/secondaryzones/DNSDB.pm
r923 r924 2971 2971 2972 2972 2973 ## DNSDB::addSecondary() 2974 # Add a secondary zone 2975 # Takes the zone name, uumeric group, comma-separated list of primary nameserver(s), 2976 # initial active/inactive state, and location tag 2977 # Returns a status code and message 2978 sub addSecondary { 2979 my $self = shift; 2980 my $dbh = $self->{dbh}; 2981 my $zone = shift; 2982 return ('FAIL', "Zone must not be blank\n") if !$zone; 2983 my $group = shift; 2984 return ('FAIL', "Group must be specified\n") if !defined($group); 2985 my $primary = shift; 2986 return ('FAIL', "Primary server(s) must be specified\n") if !$primary; 2987 my $state = shift; 2988 return ('FAIL', "Zone status must be specified\n") if !defined($state); 2989 my $defloc = shift || ''; 2990 2991 $state = 1 if $state =~ /^active$/; 2992 $state = 1 if $state =~ /^on$/; 2993 $state = 0 if $state =~ /^inactive$/; 2994 $state = 0 if $state =~ /^off$/; 2995 2996 return ('FAIL',"Invalid zone status") if $state !~ /^\d+$/; 2997 2998 ##fixme: need to handle case of being passed a CIDR value for reverse zone secondary 2999 # # Check formal .arpa zones 3000 # if ($zone =~ /\.arpa\.?$/) { 3001 # my ($code,$cidrzone) = _zone2cidr($zone); 3002 # return ('FAIL', "Poorly formed .arpa zone name) if $code eq 'FAIL'; 3003 # } 3004 3005 $zone = lc($zone) if $self->{lowercase}; 3006 return ('FAIL', "Invalid characters in zone name") if $zone !~ /^[a-zA-Z0-9_.-]+$/; 3007 3008 $primary = lc($primary) if $self->{lowercase}; 3009 my @pservers = split /\s*,\s*/, $primary; 3010 foreach (@pservers) { 3011 return ('FAIL', "Invalid characters in primary server") if ! /^[a-zA-Z0-9_.-]+$/; 3012 } 3013 $primary = join(',', @pservers); 3014 $primary =~ s/^\s+//; 3015 $primary =~ s/\s+$//; 3016 3017 my $sth = $dbh->prepare("SELECT secondary_id FROM secondaryzones WHERE lower(zone) = lower(?) AND default_location = ?"); 3018 my $zone_id; 3019 3020 # quick check to start to see if we've already got one 3021 $sth->execute($zone, $defloc); 3022 ($zone_id) = $sth->fetchrow_array; 3023 3024 return ('FAIL', "Zone already exists") if $zone_id; 3025 3026 # Allow transactions, and raise an exception on errors so we can catch it later. 3027 # Use local to make sure these get "reset" properly on exiting this block 3028 local $dbh->{AutoCommit} = 0; 3029 local $dbh->{RaiseError} = 1; 3030 3031 my $warnstr = ''; 3032 my $defttl = 3600; # 1 hour should be reasonable. And unless things have gone horribly 3033 # wrong, we should have a value to override this anyway. 3034 3035 # Wrap all the SQL in a transaction 3036 eval { 3037 ##fixme: add Net::DNS lookup to grab SOA serial when importing records for display as well 3038 ($zone_id) = $dbh->selectrow_array( 3039 "INSERT INTO secondaryzones (zone, primaryserver, group_id, status, default_location, zserial) ". 3040 "VALUES (?,?,?,?,?,?) RETURNING secondary_id", 3041 undef, 3042 ($zone, $primary, $group, $state, $defloc, 1) ); 3043 3044 my $logparent = $self->_log(secondary_id => $zone_id, group_id => $group, 3045 entry => "Added ".($state ? 'active' : 'inactive')." secondary zone $zone with primary server(s) $primary"); 3046 3047 # once we get here, we should have suceeded. 3048 $dbh->commit; 3049 }; # end eval 3050 3051 if ($@) { 3052 my $msg = $@; 3053 eval { $dbh->rollback; }; 3054 $self->_log(group_id => $group, entry => "Failed adding secondary zone $zone ($msg)") 3055 if $self->{log_failures}; 3056 $dbh->commit; # since we enabled transactions earlier 3057 return ('FAIL',$msg); 3058 } else { 3059 my $retcode = 'OK'; 3060 if ($warnstr) { 3061 $resultstr = $warnstr; 3062 $retcode = 'WARN'; 3063 } 3064 return ($retcode, $zone_id); 3065 } 3066 3067 } # end addSecondary() 3068 3069 2973 3070 ## DNSDB::delZone() 2974 3071 # Delete a forward or reverse zone.
Note:
See TracChangeset
for help on using the changeset viewer.