1 package FS::domain_record;
4 use vars qw( @ISA $noserial_hack );
5 #use FS::Record qw( qsearch qsearchs );
6 use FS::Record qw( qsearchs dbh );
13 FS::domain_record - Object methods for domain_record records
17 use FS::domain_record;
19 $record = new FS::domain_record \%hash;
20 $record = new FS::domain_record { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::domain_record object represents an entry in a DNS zone.
33 FS::domain_record inherits from FS::Record. The following fields are currently
38 =item recnum - primary key
40 =item svcnum - Domain (see L<FS::svc_domain>) of this entry
42 =item reczone - partial (or full) zone for this entry
44 =item recaf - address family for this entry, currently only `IN' is recognized.
46 =item rectype - record type for this entry (A, MX, etc.)
48 =item recdata - data for this entry
58 Creates a new entry. To add the example to the database, see L<"insert">.
60 Note that this stores the hash reference, not a distinct copy of the hash it
61 points to. You can ask the object for a copy with the I<hash> method.
65 sub table { 'domain_record'; }
69 Adds this record to the database. If there is an error, returns the error,
70 otherwise returns false.
77 local $SIG{HUP} = 'IGNORE';
78 local $SIG{INT} = 'IGNORE';
79 local $SIG{QUIT} = 'IGNORE';
80 local $SIG{TERM} = 'IGNORE';
81 local $SIG{TSTP} = 'IGNORE';
82 local $SIG{PIPE} = 'IGNORE';
84 my $oldAutoCommit = $FS::UID::AutoCommit;
85 local $FS::UID::AutoCommit = 0;
88 if ( $self->rectype eq '_mstr' ) { #delete all other records
89 foreach my $domain_record ( reverse $self->svc_domain->domain_record ) {
90 my $error = $domain_record->delete;
92 $dbh->rollback if $oldAutoCommit;
98 my $error = $self->SUPER::insert;
100 $dbh->rollback if $oldAutoCommit;
104 unless ( $self->rectype =~ /^(SOA|_mstr)$/ ) {
105 my $error = $self->increment_serial;
107 $dbh->rollback if $oldAutoCommit;
112 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
120 Delete this record from the database.
127 local $SIG{HUP} = 'IGNORE';
128 local $SIG{INT} = 'IGNORE';
129 local $SIG{QUIT} = 'IGNORE';
130 local $SIG{TERM} = 'IGNORE';
131 local $SIG{TSTP} = 'IGNORE';
132 local $SIG{PIPE} = 'IGNORE';
134 my $oldAutoCommit = $FS::UID::AutoCommit;
135 local $FS::UID::AutoCommit = 0;
138 my $error = $self->SUPER::delete;
140 $dbh->rollback if $oldAutoCommit;
144 unless ( $self->rectype =~ /^(SOA|_mstr)$/ ) {
145 my $error = $self->increment_serial;
147 $dbh->rollback if $oldAutoCommit;
152 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
158 =item replace OLD_RECORD
160 Replaces the OLD_RECORD with this one in the database. If there is an error,
161 returns the error, otherwise returns false.
168 local $SIG{HUP} = 'IGNORE';
169 local $SIG{INT} = 'IGNORE';
170 local $SIG{QUIT} = 'IGNORE';
171 local $SIG{TERM} = 'IGNORE';
172 local $SIG{TSTP} = 'IGNORE';
173 local $SIG{PIPE} = 'IGNORE';
175 my $oldAutoCommit = $FS::UID::AutoCommit;
176 local $FS::UID::AutoCommit = 0;
179 my $error = $self->SUPER::replace(@_);
181 $dbh->rollback if $oldAutoCommit;
185 unless ( $self->rectype eq 'SOA' ) {
186 my $error = $self->increment_serial;
188 $dbh->rollback if $oldAutoCommit;
193 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
201 Checks all fields to make sure this is a valid example. If there is
202 an error, returns the error, otherwise returns false. Called by the insert
207 # the check method should currently be supplied - FS::Record contains some
208 # data checking routines
214 $self->ut_numbern('recnum')
215 || $self->ut_number('svcnum')
217 return $error if $error;
219 return "Unknown svcnum (in svc_domain)"
220 unless qsearchs('svc_domain', { 'svcnum' => $self->svcnum } );
222 $self->reczone =~ /^(@|[a-z0-9\.\-\*]+)$/i
223 or return "Illegal reczone: ". $self->reczone;
226 $self->recaf =~ /^(IN)$/ or return "Illegal recaf: ". $self->recaf;
229 $self->rectype =~ /^(SOA|NS|MX|A|PTR|CNAME|_mstr)$/
230 or return "Illegal rectype (only SOA NS MX A PTR CNAME recognized): ".
234 return "Illegal reczone for ". $self->rectype. ": ". $self->reczone
235 if $self->rectype !~ /^MX$/i && $self->reczone =~ /\*/;
237 if ( $self->rectype eq 'SOA' ) {
238 my $recdata = $self->recdata;
239 $recdata =~ s/\s+/ /g;
240 $recdata =~ /^([a-z0-9\.\-]+ [\w\-\+]+\.[a-z0-9\.\-]+ \( (\d+ ){5}\))$/i
241 or return "Illegal data for SOA record: $recdata";
243 } elsif ( $self->rectype eq 'NS' ) {
244 $self->recdata =~ /^([a-z0-9\.\-]+)$/i
245 or return "Illegal data for NS record: ". $self->recdata;
247 } elsif ( $self->rectype eq 'MX' ) {
248 $self->recdata =~ /^(\d+)\s+([a-z0-9\.\-]+)$/i
249 or return "Illegal data for MX record: ". $self->recdata;
250 $self->recdata("$1 $2");
251 } elsif ( $self->rectype eq 'A' ) {
252 $self->recdata =~ /^((\d{1,3}\.){3}\d{1,3})$/
253 or return "Illegal data for A record: ". $self->recdata;
255 } elsif ( $self->rectype eq 'PTR' ) {
256 $self->recdata =~ /^([a-z0-9\.\-]+)$/i
257 or return "Illegal data for PTR record: ". $self->recdata;
259 } elsif ( $self->rectype eq 'CNAME' ) {
260 $self->recdata =~ /^([a-z0-9\.\-]+)$/i
261 or return "Illegal data for CNAME record: ". $self->recdata;
263 } elsif ( $self->rectype eq '_mstr' ) {
264 $self->recdata =~ /^((\d{1,3}\.){3}\d{1,3})$/
265 or return "Illegal data for _master pseudo-record: ". $self->recdata;
273 =item increment_serial
277 sub increment_serial {
278 return '' if $noserial_hack;
281 my $soa = qsearchs('domain_record', {
282 svcnum => $self->svcnum,
283 reczone => '@', #or full domain ?
286 } ) or return "soa record not found; can't increment serial";
288 my $data = $soa->recdata;
289 $data =~ s/(\(\D*)(\d+)/$1.($2+1)/e; #well, it works.
291 my %hash = $soa->hash;
292 $hash{recdata} = $data;
293 my $new = new FS::domain_record \%hash;
299 Returns the domain (see L<FS::svc_domain>) for this record.
305 qsearchs('svc_domain', { svcnum => $self->svcnum } );
312 $Id: domain_record.pm,v 1.10 2002-06-10 23:02:41 ivan Exp $
316 The data validation doesn't check everything it could. In particular,
317 there is no protection against bad data that passes the regex, duplicate
318 SOA records, forgetting the trailing `.', impossible IP addersses, etc. Of
319 course, it's still better than editing the zone files directly. :)
323 L<FS::Record>, schema.html from the base documentation.