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)
104 Creates a new domain. To add the domain to the database, see L<"insert">.
108 sub table { 'svc_domain'; }
112 Adds this domain to the database. If there is an error, returns the error,
113 otherwise returns false.
115 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be
116 defined. An FS::cust_svc record will be created and inserted.
118 The additional field I<action> should be set to I<N> for new domains or I<M>
121 A registration or transfer email will be submitted unless
122 $FS::svc_domain::whois_hack is true.
124 The additional field I<email> can be used to manually set the admin contact
125 email address on this email. Otherwise, the svc_acct records for this package
126 (see L<FS::cust_pkg>) are searched. If there is exactly one svc_acct record
127 in the same package, it is automatically used. Otherwise an error is returned.
129 If any I<soamachine> configuration file exists, an SOA record is added to
130 the domain_record table (see <FS::domain_record>).
132 If any machines are defined in the I<nsmachines> configuration file, NS
133 records are added to the domain_record table (see L<FS::domain_record>).
135 If any machines are defined in the I<mxmachines> configuration file, MX
136 records are added to the domain_record table (see L<FS::domain_record>).
138 Any problems adding FS::domain_record records will emit warnings, but will
139 not return errors from this method. If your configuration files are correct
140 you shouln't have any problems.
148 local $SIG{HUP} = 'IGNORE';
149 local $SIG{INT} = 'IGNORE';
150 local $SIG{QUIT} = 'IGNORE';
151 local $SIG{TERM} = 'IGNORE';
152 local $SIG{TSTP} = 'IGNORE';
153 local $SIG{PIPE} = 'IGNORE';
155 my $oldAutoCommit = $FS::UID::AutoCommit;
156 local $FS::UID::AutoCommit = 0;
159 $error = $self->check;
160 return $error if $error;
162 return "Domain in use (here)"
163 if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
165 my $whois = $self->whois;
166 if ( $self->action eq "N" && ! $whois_hack && $whois ) {
167 $dbh->rollback if $oldAutoCommit;
168 return "Domain in use (see whois)";
170 if ( $self->action eq "M" && ! $whois ) {
171 $dbh->rollback if $oldAutoCommit;
172 return "Domain not found (see whois)";
175 $error = $self->SUPER::insert;
177 $dbh->rollback if $oldAutoCommit;
181 $self->submit_internic unless $whois_hack;
184 my $soa = new FS::domain_record {
185 'svcnum' => $self->svcnum,
189 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
190 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
192 $error = $soa->insert;
194 $dbh->rollback if $oldAutoCommit;
195 return "couldn't insert SOA record for new domain: $error";
198 foreach my $nsmachine ( @nsmachines ) {
199 my $ns = new FS::domain_record {
200 'svcnum' => $self->svcnum,
204 'recdata' => $nsmachine,
206 my $error = $ns->insert;
208 $dbh->rollback if $oldAutoCommit;
209 return "couldn't insert NS record for new domain: $error";
213 foreach my $mxmachine ( @mxmachines ) {
214 my $mx = new FS::domain_record {
215 'svcnum' => $self->svcnum,
219 'recdata' => $mxmachine,
221 my $error = $mx->insert;
223 $dbh->rollback if $oldAutoCommit;
224 return "couldn't insert MX record for new domain: $error";
230 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
237 Deletes this domain from the database. If there is an error, returns the
238 error, otherwise returns false.
240 The corresponding FS::cust_svc record will be deleted as well.
242 =item replace OLD_RECORD
244 Replaces OLD_RECORD with this one in the database. If there is an error,
245 returns the error, otherwise returns false.
250 my ( $new, $old ) = ( shift, shift );
253 return "Can't change domain - reorder."
254 if $old->getfield('domain') ne $new->getfield('domain');
256 $new->SUPER::replace($old);
262 Just returns false (no error) for now.
264 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
268 Just returns false (no error) for now.
270 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
274 Just returns false (no error) for now.
276 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
280 Checks all fields to make sure this is a valid domain. If there is an error,
281 returns the error, otherwise returns false. Called by the insert and replace
284 Sets any fixed values; see L<FS::part_svc>.
292 my $x = $self->setfixed;
293 return $x unless ref($x);
298 if ( $self->svcnum ) {
299 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
300 $pkgnum = $cust_svc->pkgnum;
302 $pkgnum = $self->pkgnum;
305 my($recref) = $self->hashref;
307 unless ( $whois_hack ) {
308 unless ( $self->email ) { #find out an email address
310 foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
311 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
312 push @svc_acct, $svc_acct if $svc_acct;
315 if ( scalar(@svc_acct) == 0 ) {
316 return "Must order an account in package ". $pkgnum. " first";
317 } elsif ( scalar(@svc_acct) > 1 ) {
318 return "More than one account in package ". $pkgnum. ": specify admin contact email";
320 $self->email($svc_acct[0]->username. '@'. $mydomain);
325 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
326 if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
327 $recref->{domain} = "$1.$2";
329 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
330 $recref->{domain} = $1;
332 return "Illegal domain ". $recref->{domain}.
333 " (or unknown registry - try \$whois_hack)";
336 $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
337 $recref->{action} = $1;
339 $self->ut_textn('purpose');
345 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
346 undef if the domain is not found in whois.
348 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
353 $whois_hack or new Net::Whois::Domain $_[0]->domain;
363 die "_whois depriciated";
366 =item submit_internic
368 Submits a registration email for this domain.
372 sub submit_internic {
375 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
376 return unless $cust_pkg;
377 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $cust_pkg->custnum } );
378 return unless $cust_main;
381 'action' => $self->action,
382 'purpose' => $self->purpose,
383 'domain' => $self->domain,
384 'company' => $cust_main->company
385 || $cust_main->getfield('first'). ' '.
386 $cust_main->getfield('last')
388 'city' => $cust_main->city,
389 'state' => $cust_main->state,
390 'zip' => $cust_main->zip,
391 'country' => $cust_main->country,
392 'last' => $cust_main->getfield('last'),
393 'first' => $cust_main->getfield('first'),
394 'daytime' => $cust_main->daytime,
395 'fax' => $cust_main->fax,
396 'email' => $self->email,
397 'tech_contact' => $tech_contact,
398 'primary' => shift @nameservers,
399 'primary_ip' => shift @nameserver_ips,
403 my @xtemplate = @template;
406 OLOOP: while ( defined( $line = shift @xtemplate ) ) {
408 if ( $line =~ /^###LOOP###$/ ) {
410 LOADBUF: while ( defined( $line = shift @xtemplate ) ) {
411 last LOADBUF if ( $line =~ /^###ENDLOOP###$/ );
415 'address' => $cust_main->address2
416 ? [ $cust_main->address1, $cust_main->address2 ]
417 : [ $cust_main->address1 ]
419 'secondary' => [ @nameservers ],
420 'secondary_ip' => [ @nameserver_ips ],
423 my @xbuffer = @buffer;
424 SUBLOOP: while ( defined( $line = shift @xbuffer ) ) {
425 if ( $line =~ /###(\w+)###/ ) {
426 #last LOOP unless my($lub)=shift@{$lubs{$1}};
427 next OLOOP unless my $lub = shift @{$lubs{$1}};
428 $line =~ s/###(\w+)###/$lub/e;
438 if ( $line =~ /###(\w+)###/ ) {
439 #$line =~ s/###(\w+)###/$subs{$1}/eg;
440 $line =~ s/###(\w+)###/$subs{$1}/e;
449 if ( $self->action eq "M" ) {
450 $subject = "MODIFY DOMAIN ". $self->domain;
451 } elsif ( $self->action eq "N" ) {
452 $subject = "NEW DOMAIN ". $self->domain;
454 croak "submit_internic called with action ". $self->action;
457 $ENV{SMTPHOSTS} = $smtpmachine;
458 $ENV{MAILADDRESS} = $from;
459 my $header = Mail::Header->new( [
464 "Date: ". time2str("%a, %d %b %Y %X %z", time),
468 my($msg)=Mail::Internet->new(
473 $msg->smtpsend or die "Can't send registration email"; #die? warn?
481 $Id: svc_domain.pm,v 1.12 2001-06-03 17:22:52 ivan Exp $
485 All BIND/DNS fields should be included (and exported).
487 Delete doesn't send a registration template.
489 All registries should be supported.
491 Should change action to a real field.
493 The $recref stuff in sub check should be cleaned up.
497 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
498 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
499 L<dot-qmail>, schema.html from the base documentation, config.html from the