transactions part deux
[freeside.git] / FS / FS / svc_domain.pm
1 package FS::svc_domain;
2
3 use strict;
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
7   $soarefresh $soaretry
8 );
9 use Carp;
10 use Mail::Internet;
11 use Mail::Header;
12 use Date::Format;
13 use Net::Whois 1.0;
14 use FS::Record qw(fields qsearch qsearchs);
15 use FS::Conf;
16 use FS::svc_Common;
17 use FS::cust_svc;
18 use FS::svc_acct;
19 use FS::cust_pkg;
20 use FS::cust_main;
21 use FS::domain_record;
22
23 @ISA = qw( FS::svc_Common );
24
25 #ask FS::UID to run this stuff for us later
26 $FS::UID::callback{'FS::domain'} = sub { 
27   $conf = new FS::Conf;
28
29   $mydomain = $conf->config('domain');
30   $smtpmachine = $conf->config('smtpmachine');
31
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");
37   @nameservers=map {
38     /^\s*\d+\.\d+\.\d+\.\d+\s+([^\s]+)\s*$/
39       or die "Illegal line in $internic/nameservers";
40     $1;
41   } @ns;
42   @nameserver_ips=map {
43     /^\s*(\d+\.\d+\.\d+\.\d+)\s+([^\s]+)\s*$/
44       or die "Illegal line in $internic/nameservers!";
45     $1;
46   } @ns;
47   @template = map { $_. "\n" } $conf->config("$internic/template");
48
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');
57
58 };
59
60 =head1 NAME
61
62 FS::svc_domain - Object methods for svc_domain records
63
64 =head1 SYNOPSIS
65
66   use FS::svc_domain;
67
68   $record = new FS::svc_domain \%hash;
69   $record = new FS::svc_domain { 'column' => 'value' };
70
71   $error = $record->insert;
72
73   $error = $new_record->replace($old_record);
74
75   $error = $record->delete;
76
77   $error = $record->check;
78
79   $error = $record->suspend;
80
81   $error = $record->unsuspend;
82
83   $error = $record->cancel;
84
85 =head1 DESCRIPTION
86
87 An FS::svc_domain object represents a domain.  FS::svc_domain inherits from
88 FS::svc_Common.  The following fields are currently supported:
89
90 =over 4
91
92 =item svcnum - primary key (assigned automatically for new accounts)
93
94 =item domain
95
96 =back
97
98 =head1 METHODS
99
100 =over 4
101
102 =item new HASHREF
103
104 Creates a new domain.  To add the domain to the database, see L<"insert">.
105
106 =cut
107
108 sub table { 'svc_domain'; }
109
110 =item insert
111
112 Adds this domain to the database.  If there is an error, returns the error,
113 otherwise returns false.
114
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.
117
118 The additional field I<action> should be set to I<N> for new domains or I<M>
119 for transfers.
120
121 A registration or transfer email will be submitted unless
122 $FS::svc_domain::whois_hack is true.
123
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.
128
129 If any I<soamachine> configuration file exists, an SOA record is added to
130 the domain_record table (see <FS::domain_record>).
131
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>).
134
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>).
137
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.
141
142 =cut
143
144 sub insert {
145   my $self = shift;
146   my $error;
147
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';
154
155   my $oldAutoCommit = $FS::UID::AutoCommit;
156   local $FS::UID::AutoCommit = 0;
157   my $dbh = dbh;
158
159   $error = $self->check;
160   return $error if $error;
161
162   return "Domain in use (here)"
163     if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
164
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)";
169   }
170   if ( $self->action eq "M" && ! $whois ) {
171     $dbh->rollback if $oldAutoCommit;
172     return "Domain not found (see whois)";
173   }
174
175   $error = $self->SUPER::insert;
176   if ( $error ) {
177     $dbh->rollback if $oldAutoCommit;
178     return $error;
179   }
180
181   $self->submit_internic unless $whois_hack;
182
183   if ( $soamachine ) {
184     my $soa = new FS::domain_record {
185       'svcnum'  => $self->svcnum,
186       'reczone' => '@',
187       'recaf'   => 'IN',
188       'rectype' => 'SOA',
189       'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%e", time). "00 ".
190                    "$soarefresh $soaretry $soaexpire $soadefaultttl )"
191     };
192     $error = $soa->insert;
193     if ( $error ) {
194       $dbh->rollback if $oldAutoCommit;
195       return "couldn't insert SOA record for new domain: $error";
196     }
197
198     foreach my $nsmachine ( @nsmachines ) {
199       my $ns = new FS::domain_record {
200         'svcnum'  => $self->svcnum,
201         'reczone' => '@',
202         'recaf'   => 'IN',
203         'rectype' => 'NS',
204         'recdata' => $nsmachine,
205       };
206       my $error = $ns->insert;
207       if ( $error ) {
208         $dbh->rollback if $oldAutoCommit;
209         return "couldn't insert NS record for new domain: $error";
210       }
211     }
212
213     foreach my $mxmachine ( @mxmachines ) {
214       my $mx = new FS::domain_record {
215         'svcnum'  => $self->svcnum,
216         'reczone' => '@',
217         'recaf'   => 'IN',
218         'rectype' => 'mx',
219         'recdata' => $mxmachine,
220       };
221       my $error = $mx->insert;
222       if ( $error ) {
223         $dbh->rollback if $oldAutoCommit;
224         return "couldn't insert MX record for new domain: $error";
225       }
226     }
227
228   }
229
230   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
231
232   ''; #no error
233 }
234
235 =item delete
236
237 Deletes this domain from the database.  If there is an error, returns the
238 error, otherwise returns false.
239
240 The corresponding FS::cust_svc record will be deleted as well.
241
242 =item replace OLD_RECORD
243
244 Replaces OLD_RECORD with this one in the database.  If there is an error,
245 returns the error, otherwise returns false.
246
247 =cut
248
249 sub replace {
250   my ( $new, $old ) = ( shift, shift );
251   my $error;
252
253   return "Can't change domain - reorder."
254     if $old->getfield('domain') ne $new->getfield('domain'); 
255
256   $new->SUPER::replace($old);
257
258 }
259
260 =item suspend
261
262 Just returns false (no error) for now.
263
264 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
265
266 =item unsuspend
267
268 Just returns false (no error) for now.
269
270 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
271
272 =item cancel
273
274 Just returns false (no error) for now.
275
276 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
277
278 =item check
279
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
282 methods.
283
284 Sets any fixed values; see L<FS::part_svc>.
285
286 =cut
287
288 sub check {
289   my $self = shift;
290   my $error;
291
292   my $x = $self->setfixed;
293   return $x unless ref($x);
294   my $part_svc = $x;
295
296   #hmm
297   my $pkgnum;
298   if ( $self->svcnum ) {
299     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
300     $pkgnum = $cust_svc->pkgnum;
301   } else {
302     $pkgnum = $self->pkgnum;
303   }
304
305   my($recref) = $self->hashref;
306
307   unless ( $whois_hack ) {
308     unless ( $self->email ) { #find out an email address
309       my @svc_acct;
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;
313       }
314
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";
319       } else {
320         $self->email($svc_acct[0]->username. '@'. $mydomain);
321       }
322     }
323   }
324
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";
328   # hmmmmmmmm.
329   } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
330     $recref->{domain} = $1;
331   } else {
332     return "Illegal domain ". $recref->{domain}.
333            " (or unknown registry - try \$whois_hack)";
334   }
335
336   $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
337   $recref->{action} = $1;
338
339   $self->ut_textn('purpose');
340
341 }
342
343 =item whois
344
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.
347
348 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
349
350 =cut
351
352 sub whois {
353   $whois_hack or new Net::Whois::Domain $_[0]->domain;
354 }
355
356 =item _whois
357
358 Depriciated.
359
360 =cut
361
362 sub _whois {
363   die "_whois depriciated";
364 }
365
366 =item submit_internic
367
368 Submits a registration email for this domain.
369
370 =cut
371
372 sub submit_internic {
373   my $self = shift;
374
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;
379
380   my %subs = (
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')
387                       ,
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,
400   );
401
402   #yuck
403   my @xtemplate = @template;
404   my @body;
405   my $line;
406   OLOOP: while ( defined( $line = shift @xtemplate ) ) {
407
408     if ( $line =~ /^###LOOP###$/ ) {
409       my(@buffer);
410       LOADBUF: while ( defined( $line = shift @xtemplate ) ) {
411         last LOADBUF if ( $line =~ /^###ENDLOOP###$/ );
412         push @buffer, $line;
413       }
414       my %lubs = (
415         'address'      => $cust_main->address2 
416                             ? [ $cust_main->address1, $cust_main->address2 ]
417                             : [ $cust_main->address1 ]
418                           ,
419         'secondary'    => [ @nameservers ],
420         'secondary_ip' => [ @nameserver_ips ],
421       );
422       LOOP: while (1) {
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;
429             redo SUBLOOP;
430           } else {
431             push @body, $line;
432           }
433         } #SUBLOOP
434       } #LOOP
435
436     }
437
438     if ( $line =~ /###(\w+)###/ ) {
439       #$line =~ s/###(\w+)###/$subs{$1}/eg;
440       $line =~ s/###(\w+)###/$subs{$1}/e;
441       redo OLOOP;
442     } else {
443       push @body, $line;
444     }
445
446   } #OLOOP
447
448   my $subject;
449   if ( $self->action eq "M" ) {
450     $subject = "MODIFY DOMAIN ". $self->domain;
451   } elsif ( $self->action eq "N" ) { 
452     $subject = "NEW DOMAIN ". $self->domain;
453   } else {
454     croak "submit_internic called with action ". $self->action;
455   }
456
457   $ENV{SMTPHOSTS} = $smtpmachine;
458   $ENV{MAILADDRESS} = $from;
459   my $header = Mail::Header->new( [
460     "From: $from",
461     "To: $to",
462     "Sender: $from",
463     "Reply-To: $from",
464     "Date: ". time2str("%a, %d %b %Y %X %z", time),
465     "Subject: $subject",
466   ] );
467
468   my($msg)=Mail::Internet->new(
469     'Header' => $header,
470     'Body' => \@body,
471   );
472
473   $msg->smtpsend or die "Can't send registration email"; #die? warn?
474
475 }
476
477 =back
478
479 =head1 VERSION
480
481 $Id: svc_domain.pm,v 1.8 2001-04-15 13:35:12 ivan Exp $
482
483 =head1 BUGS
484
485 All BIND/DNS fields should be included (and exported).
486
487 Delete doesn't send a registration template.
488
489 All registries should be supported.
490
491 Should change action to a real field.
492
493 The $recref stuff in sub check should be cleaned up.
494
495 =head1 SEE ALSO
496
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<FS::SSH>, L<Net::Whois>, L<ssh>,
499 L<dot-qmail>, schema.html from the base documentation, config.html from the
500 base documentation.
501
502 =cut
503
504 1;
505
506