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);
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 $error = $self->check;
156 return $error if $error;
158 return "Domain in use (here)"
159 if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
161 my $whois = $self->whois;
162 return "Domain in use (see whois)"
163 if ( $self->action eq "N" && ! $whois_hack && $whois );
164 return "Domain not found (see whois)"
165 if ( $self->action eq "M" && ! $whois );
167 $error = $self->SUPER::insert;
168 return $error if $error;
170 $self->submit_internic unless $whois_hack;
173 my $soa = new FS::domain_record {
174 'svcnum' => $self->svcnum,
178 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%e", time). "00 ".
179 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
181 $error = $soa->insert;
182 warn "WARNING: couldn't insert SOA record for new domain: $error" if $error;
184 foreach my $nsmachine ( @nsmachines ) {
185 my $ns = new FS::domain_record {
186 'svcnum' => $self->svcnum,
190 'recdata' => $nsmachine,
192 my $error = $ns->insert;
193 warn "WARNING: couldn't insert NS record for new domain: $error"
197 foreach my $mxmachine ( @mxmachines ) {
198 my $mx = new FS::domain_record {
199 'svcnum' => $self->svcnum,
203 'recdata' => $mxmachine,
205 my $error = $mx->insert;
206 warn "WARNING: couldn't insert MX record for new domain: $error"
218 Deletes this domain from the database. If there is an error, returns the
219 error, otherwise returns false.
221 The corresponding FS::cust_svc record will be deleted as well.
223 =item replace OLD_RECORD
225 Replaces OLD_RECORD with this one in the database. If there is an error,
226 returns the error, otherwise returns false.
231 my ( $new, $old ) = ( shift, shift );
234 return "Can't change domain - reorder."
235 if $old->getfield('domain') ne $new->getfield('domain');
237 $new->SUPER::replace($old);
243 Just returns false (no error) for now.
245 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
249 Just returns false (no error) for now.
251 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
255 Just returns false (no error) for now.
257 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
261 Checks all fields to make sure this is a valid domain. If there is an error,
262 returns the error, otherwise returns false. Called by the insert and replace
265 Sets any fixed values; see L<FS::part_svc>.
273 my $x = $self->setfixed;
274 return $x unless ref($x);
279 if ( $self->svcnum ) {
280 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
281 $pkgnum = $cust_svc->pkgnum;
283 $pkgnum = $self->pkgnum;
286 my($recref) = $self->hashref;
288 unless ( $whois_hack ) {
289 unless ( $self->email ) { #find out an email address
291 foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
292 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
293 push @svc_acct, $svc_acct if $svc_acct;
296 if ( scalar(@svc_acct) == 0 ) {
297 return "Must order an account in package ". $pkgnum. " first";
298 } elsif ( scalar(@svc_acct) > 1 ) {
299 return "More than one account in package ". $pkgnum. ": specify admin contact email";
301 $self->email($svc_acct[0]->username. '@'. $mydomain);
306 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
307 if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
308 $recref->{domain} = "$1.$2";
310 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
311 $recref->{domain} = $1;
313 return "Illegal domain ". $recref->{domain}.
314 " (or unknown registry - try \$whois_hack)";
317 $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
318 $recref->{action} = $1;
320 $self->ut_textn('purpose');
326 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
327 undef if the domain is not found in whois.
329 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
334 $whois_hack or new Net::Whois::Domain $_[0]->domain;
344 die "_whois depriciated";
347 =item submit_internic
349 Submits a registration email for this domain.
353 sub submit_internic {
356 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
357 return unless $cust_pkg;
358 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $cust_pkg->custnum } );
359 return unless $cust_main;
362 'action' => $self->action,
363 'purpose' => $self->purpose,
364 'domain' => $self->domain,
365 'company' => $cust_main->company
366 || $cust_main->getfield('first'). ' '.
367 $cust_main->getfield('last')
369 'city' => $cust_main->city,
370 'state' => $cust_main->state,
371 'zip' => $cust_main->zip,
372 'country' => $cust_main->country,
373 'last' => $cust_main->getfield('last'),
374 'first' => $cust_main->getfield('first'),
375 'daytime' => $cust_main->daytime,
376 'fax' => $cust_main->fax,
377 'email' => $self->email,
378 'tech_contact' => $tech_contact,
379 'primary' => shift @nameservers,
380 'primary_ip' => shift @nameserver_ips,
384 my @xtemplate = @template;
387 OLOOP: while ( defined( $line = shift @xtemplate ) ) {
389 if ( $line =~ /^###LOOP###$/ ) {
391 LOADBUF: while ( defined( $line = shift @xtemplate ) ) {
392 last LOADBUF if ( $line =~ /^###ENDLOOP###$/ );
396 'address' => $cust_main->address2
397 ? [ $cust_main->address1, $cust_main->address2 ]
398 : [ $cust_main->address1 ]
400 'secondary' => [ @nameservers ],
401 'secondary_ip' => [ @nameserver_ips ],
404 my @xbuffer = @buffer;
405 SUBLOOP: while ( defined( $line = shift @xbuffer ) ) {
406 if ( $line =~ /###(\w+)###/ ) {
407 #last LOOP unless my($lub)=shift@{$lubs{$1}};
408 next OLOOP unless my $lub = shift @{$lubs{$1}};
409 $line =~ s/###(\w+)###/$lub/e;
419 if ( $line =~ /###(\w+)###/ ) {
420 #$line =~ s/###(\w+)###/$subs{$1}/eg;
421 $line =~ s/###(\w+)###/$subs{$1}/e;
430 if ( $self->action eq "M" ) {
431 $subject = "MODIFY DOMAIN ". $self->domain;
432 } elsif ( $self->action eq "N" ) {
433 $subject = "NEW DOMAIN ". $self->domain;
435 croak "submit_internic called with action ". $self->action;
438 $ENV{SMTPHOSTS} = $smtpmachine;
439 $ENV{MAILADDRESS} = $from;
440 my $header = Mail::Header->new( [
445 "Date: ". time2str("%a, %d %b %Y %X %z", time),
449 my($msg)=Mail::Internet->new(
454 $msg->smtpsend or die "Can't send registration email"; #die? warn?
462 $Id: svc_domain.pm,v 1.7 2000-06-29 11:12:20 ivan Exp $
466 All BIND/DNS fields should be included (and exported).
468 Delete doesn't send a registration template.
470 All registries should be supported.
472 Should change action to a real field.
474 The $recref stuff in sub check should be cleaned up.
478 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
479 L<FS::part_svc>, L<FS::cust_pkg>, L<FS::SSH>, L<Net::Whois>, L<ssh>,
480 L<dot-qmail>, schema.html from the base documentation, config.html from the