1 package FS::svc_domain;
4 use vars qw( @ISA $whois_hack $conf $mydomain $smtpmachine
5 $tech_contact $from $to @nameservers @nameserver_ips @template
6 @mxmachines @nsmachines $soadefaultttl $soaemail $soaexpire $soamachine
14 use FS::Record qw(fields qsearch qsearchs dbh);
21 use FS::domain_record;
23 @ISA = qw( FS::svc_Common );
25 #ask FS::UID to run this stuff for us later
26 $FS::UID::callback{'FS::domain'} = sub {
29 $mydomain = $conf->config('domain');
30 $smtpmachine = $conf->config('smtpmachine');
32 my($internic)="/registries/internic";
33 $tech_contact = $conf->config("$internic/tech_contact");
34 $from = $conf->config("$internic/from");
35 $to = $conf->config("$internic/to");
36 my(@ns) = $conf->config("$internic/nameservers");
38 /^\s*\d+\.\d+\.\d+\.\d+\s+([^\s]+)\s*$/
39 or die "Illegal line in $internic/nameservers";
43 /^\s*(\d+\.\d+\.\d+\.\d+)\s+([^\s]+)\s*$/
44 or die "Illegal line in $internic/nameservers!";
47 @template = map { $_. "\n" } $conf->config("$internic/template");
49 @mxmachines = $conf->config('mxmachines');
50 @nsmachines = $conf->config('nsmachines');
51 $soadefaultttl = $conf->config('soadefaultttl');
52 $soaemail = $conf->config('soaemail');
53 $soaexpire = $conf->config('soaexpire');
54 $soamachine = $conf->config('soamachine');
55 $soarefresh = $conf->config('soarefresh');
56 $soaretry = $conf->config('soaretry');
62 FS::svc_domain - Object methods for svc_domain records
68 $record = new FS::svc_domain \%hash;
69 $record = new FS::svc_domain { 'column' => 'value' };
71 $error = $record->insert;
73 $error = $new_record->replace($old_record);
75 $error = $record->delete;
77 $error = $record->check;
79 $error = $record->suspend;
81 $error = $record->unsuspend;
83 $error = $record->cancel;
87 An FS::svc_domain object represents a domain. FS::svc_domain inherits from
88 FS::svc_Common. The following fields are currently supported:
92 =item svcnum - primary key (assigned automatically for new accounts)
96 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
106 Creates a new domain. To add the domain to the database, see L<"insert">.
110 sub table { 'svc_domain'; }
114 Adds this domain to the database. If there is an error, returns the error,
115 otherwise returns false.
117 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be
118 defined. An FS::cust_svc record will be created and inserted.
120 The additional field I<action> should be set to I<N> for new domains or I<M>
123 A registration or transfer email will be submitted unless
124 $FS::svc_domain::whois_hack is true.
126 The additional field I<email> can be used to manually set the admin contact
127 email address on this email. Otherwise, the svc_acct records for this package
128 (see L<FS::cust_pkg>) are searched. If there is exactly one svc_acct record
129 in the same package, it is automatically used. Otherwise an error is returned.
131 If any I<soamachine> configuration file exists, an SOA record is added to
132 the domain_record table (see <FS::domain_record>).
134 If any machines are defined in the I<nsmachines> configuration file, NS
135 records are added to the domain_record table (see L<FS::domain_record>).
137 If any machines are defined in the I<mxmachines> configuration file, MX
138 records are added to the domain_record table (see L<FS::domain_record>).
140 Any problems adding FS::domain_record records will emit warnings, but will
141 not return errors from this method. If your configuration files are correct
142 you shouln't have any problems.
150 local $SIG{HUP} = 'IGNORE';
151 local $SIG{INT} = 'IGNORE';
152 local $SIG{QUIT} = 'IGNORE';
153 local $SIG{TERM} = 'IGNORE';
154 local $SIG{TSTP} = 'IGNORE';
155 local $SIG{PIPE} = 'IGNORE';
157 my $oldAutoCommit = $FS::UID::AutoCommit;
158 local $FS::UID::AutoCommit = 0;
161 $error = $self->check;
162 return $error if $error;
164 return "Domain in use (here)"
165 if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
167 my $whois = $self->whois;
168 if ( $self->action eq "N" && ! $whois_hack && $whois ) {
169 $dbh->rollback if $oldAutoCommit;
170 return "Domain in use (see whois)";
172 if ( $self->action eq "M" && ! $whois ) {
173 $dbh->rollback if $oldAutoCommit;
174 return "Domain not found (see whois)";
177 $error = $self->SUPER::insert;
179 $dbh->rollback if $oldAutoCommit;
183 $self->submit_internic unless $whois_hack;
186 my $soa = new FS::domain_record {
187 'svcnum' => $self->svcnum,
191 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
192 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
194 $error = $soa->insert;
196 $dbh->rollback if $oldAutoCommit;
197 return "couldn't insert SOA record for new domain: $error";
200 foreach my $nsmachine ( @nsmachines ) {
201 my $ns = new FS::domain_record {
202 'svcnum' => $self->svcnum,
206 'recdata' => $nsmachine,
208 my $error = $ns->insert;
210 $dbh->rollback if $oldAutoCommit;
211 return "couldn't insert NS record for new domain: $error";
215 foreach my $mxmachine ( @mxmachines ) {
216 my $mx = new FS::domain_record {
217 'svcnum' => $self->svcnum,
221 'recdata' => $mxmachine,
223 my $error = $mx->insert;
225 $dbh->rollback if $oldAutoCommit;
226 return "couldn't insert MX record for new domain: $error";
232 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
239 Deletes this domain from the database. If there is an error, returns the
240 error, otherwise returns false.
242 The corresponding FS::cust_svc record will be deleted as well.
244 =item replace OLD_RECORD
246 Replaces OLD_RECORD with this one in the database. If there is an error,
247 returns the error, otherwise returns false.
252 my ( $new, $old ) = ( shift, shift );
255 return "Can't change domain - reorder."
256 if $old->getfield('domain') ne $new->getfield('domain');
258 $new->SUPER::replace($old);
264 Just returns false (no error) for now.
266 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
270 Just returns false (no error) for now.
272 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
276 Just returns false (no error) for now.
278 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
282 Checks all fields to make sure this is a valid domain. If there is an error,
283 returns the error, otherwise returns false. Called by the insert and replace
286 Sets any fixed values; see L<FS::part_svc>.
293 my $x = $self->setfixed;
294 return $x unless ref($x);
297 my $error = $self->ut_numbern('catchall');
298 return $error if $error;
302 if ( $self->svcnum ) {
303 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
304 $pkgnum = $cust_svc->pkgnum;
306 $pkgnum = $self->pkgnum;
309 my($recref) = $self->hashref;
311 unless ( $whois_hack ) {
312 unless ( $self->email ) { #find out an email address
314 foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
315 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
316 push @svc_acct, $svc_acct if $svc_acct;
319 if ( scalar(@svc_acct) == 0 ) {
320 return "Must order an account in package ". $pkgnum. " first";
321 } elsif ( scalar(@svc_acct) > 1 ) {
322 return "More than one account in package ". $pkgnum. ": specify admin contact email";
324 $self->email($svc_acct[0]->username. '@'. $mydomain);
329 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
330 if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
331 $recref->{domain} = "$1.$2";
333 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
334 $recref->{domain} = $1;
336 return "Illegal domain ". $recref->{domain}.
337 " (or unknown registry - try \$whois_hack)";
340 $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
341 $recref->{action} = $1;
343 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
344 return "Unknown catchall" unless $svc_acct || ! $recref->{catchall};
346 $self->ut_textn('purpose');
352 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
353 undef if the domain is not found in whois.
355 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
360 $whois_hack or new Net::Whois::Domain $_[0]->domain;
370 die "_whois depriciated";
373 =item submit_internic
375 Submits a registration email for this domain.
379 sub submit_internic {
382 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
383 return unless $cust_pkg;
384 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $cust_pkg->custnum } );
385 return unless $cust_main;
388 'action' => $self->action,
389 'purpose' => $self->purpose,
390 'domain' => $self->domain,
391 'company' => $cust_main->company
392 || $cust_main->getfield('first'). ' '.
393 $cust_main->getfield('last')
395 'city' => $cust_main->city,
396 'state' => $cust_main->state,
397 'zip' => $cust_main->zip,
398 'country' => $cust_main->country,
399 'last' => $cust_main->getfield('last'),
400 'first' => $cust_main->getfield('first'),
401 'daytime' => $cust_main->daytime,
402 'fax' => $cust_main->fax,
403 'email' => $self->email,
404 'tech_contact' => $tech_contact,
405 'primary' => shift @nameservers,
406 'primary_ip' => shift @nameserver_ips,
410 my @xtemplate = @template;
413 OLOOP: while ( defined( $line = shift @xtemplate ) ) {
415 if ( $line =~ /^###LOOP###$/ ) {
417 LOADBUF: while ( defined( $line = shift @xtemplate ) ) {
418 last LOADBUF if ( $line =~ /^###ENDLOOP###$/ );
422 'address' => $cust_main->address2
423 ? [ $cust_main->address1, $cust_main->address2 ]
424 : [ $cust_main->address1 ]
426 'secondary' => [ @nameservers ],
427 'secondary_ip' => [ @nameserver_ips ],
430 my @xbuffer = @buffer;
431 SUBLOOP: while ( defined( $line = shift @xbuffer ) ) {
432 if ( $line =~ /###(\w+)###/ ) {
433 #last LOOP unless my($lub)=shift@{$lubs{$1}};
434 next OLOOP unless my $lub = shift @{$lubs{$1}};
435 $line =~ s/###(\w+)###/$lub/e;
445 if ( $line =~ /###(\w+)###/ ) {
446 #$line =~ s/###(\w+)###/$subs{$1}/eg;
447 $line =~ s/###(\w+)###/$subs{$1}/e;
456 if ( $self->action eq "M" ) {
457 $subject = "MODIFY DOMAIN ". $self->domain;
458 } elsif ( $self->action eq "N" ) {
459 $subject = "NEW DOMAIN ". $self->domain;
461 croak "submit_internic called with action ". $self->action;
464 $ENV{SMTPHOSTS} = $smtpmachine;
465 $ENV{MAILADDRESS} = $from;
466 my $header = Mail::Header->new( [
471 "Date: ". time2str("%a, %d %b %Y %X %z", time),
475 my($msg)=Mail::Internet->new(
480 $msg->smtpsend or die "Can't send registration email"; #die? warn?
488 $Id: svc_domain.pm,v 1.15 2001-08-19 08:18:01 ivan Exp $
492 All BIND/DNS fields should be included (and exported).
494 Delete doesn't send a registration template.
496 All registries should be supported.
498 Should change action to a real field.
500 The $recref stuff in sub check should be cleaned up.
504 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
505 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
506 L<dot-qmail>, schema.html from the base documentation, config.html from the