document catchall and untaint everything
[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 dbh);
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 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
97
98 =back
99
100 =head1 METHODS
101
102 =over 4
103
104 =item new HASHREF
105
106 Creates a new domain.  To add the domain to the database, see L<"insert">.
107
108 =cut
109
110 sub table { 'svc_domain'; }
111
112 =item insert
113
114 Adds this domain to the database.  If there is an error, returns the error,
115 otherwise returns false.
116
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.
119
120 The additional field I<action> should be set to I<N> for new domains or I<M>
121 for transfers.
122
123 A registration or transfer email will be submitted unless
124 $FS::svc_domain::whois_hack is true.
125
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.
130
131 If any I<soamachine> configuration file exists, an SOA record is added to
132 the domain_record table (see <FS::domain_record>).
133
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>).
136
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>).
139
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.
143
144 =cut
145
146 sub insert {
147   my $self = shift;
148   my $error;
149
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';
156
157   my $oldAutoCommit = $FS::UID::AutoCommit;
158   local $FS::UID::AutoCommit = 0;
159   my $dbh = dbh;
160
161   $error = $self->check;
162   return $error if $error;
163
164   return "Domain in use (here)"
165     if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
166
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)";
171   }
172   if ( $self->action eq "M" && ! $whois ) {
173     $dbh->rollback if $oldAutoCommit;
174     return "Domain not found (see whois)";
175   }
176
177   $error = $self->SUPER::insert;
178   if ( $error ) {
179     $dbh->rollback if $oldAutoCommit;
180     return $error;
181   }
182
183   $self->submit_internic unless $whois_hack;
184
185   if ( $soamachine ) {
186     my $soa = new FS::domain_record {
187       'svcnum'  => $self->svcnum,
188       'reczone' => '@',
189       'recaf'   => 'IN',
190       'rectype' => 'SOA',
191       'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
192                    "$soarefresh $soaretry $soaexpire $soadefaultttl )"
193     };
194     $error = $soa->insert;
195     if ( $error ) {
196       $dbh->rollback if $oldAutoCommit;
197       return "couldn't insert SOA record for new domain: $error";
198     }
199
200     foreach my $nsmachine ( @nsmachines ) {
201       my $ns = new FS::domain_record {
202         'svcnum'  => $self->svcnum,
203         'reczone' => '@',
204         'recaf'   => 'IN',
205         'rectype' => 'NS',
206         'recdata' => $nsmachine,
207       };
208       my $error = $ns->insert;
209       if ( $error ) {
210         $dbh->rollback if $oldAutoCommit;
211         return "couldn't insert NS record for new domain: $error";
212       }
213     }
214
215     foreach my $mxmachine ( @mxmachines ) {
216       my $mx = new FS::domain_record {
217         'svcnum'  => $self->svcnum,
218         'reczone' => '@',
219         'recaf'   => 'IN',
220         'rectype' => 'MX',
221         'recdata' => $mxmachine,
222       };
223       my $error = $mx->insert;
224       if ( $error ) {
225         $dbh->rollback if $oldAutoCommit;
226         return "couldn't insert MX record for new domain: $error";
227       }
228     }
229
230   }
231
232   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
233
234   ''; #no error
235 }
236
237 =item delete
238
239 Deletes this domain from the database.  If there is an error, returns the
240 error, otherwise returns false.
241
242 The corresponding FS::cust_svc record will be deleted as well.
243
244 =item replace OLD_RECORD
245
246 Replaces OLD_RECORD with this one in the database.  If there is an error,
247 returns the error, otherwise returns false.
248
249 =cut
250
251 sub replace {
252   my ( $new, $old ) = ( shift, shift );
253   my $error;
254
255   return "Can't change domain - reorder."
256     if $old->getfield('domain') ne $new->getfield('domain'); 
257
258   $new->SUPER::replace($old);
259
260 }
261
262 =item suspend
263
264 Just returns false (no error) for now.
265
266 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
267
268 =item unsuspend
269
270 Just returns false (no error) for now.
271
272 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
273
274 =item cancel
275
276 Just returns false (no error) for now.
277
278 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
279
280 =item check
281
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
284 methods.
285
286 Sets any fixed values; see L<FS::part_svc>.
287
288 =cut
289
290 sub check {
291   my $self = shift;
292   my $error =
293     $self->ut_numbern('svcnum')
294     || $self->ut_numbern('catchall')
295   ;
296   return $error if $error;
297
298   my $x = $self->setfixed;
299   return $x unless ref($x);
300   my $part_svc = $x;
301
302   #hmm
303   my $pkgnum;
304   if ( $self->svcnum ) {
305     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
306     $pkgnum = $cust_svc->pkgnum;
307   } else {
308     $pkgnum = $self->pkgnum;
309   }
310
311   my($recref) = $self->hashref;
312
313   unless ( $whois_hack ) {
314     unless ( $self->email ) { #find out an email address
315       my @svc_acct;
316       foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
317         my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
318         push @svc_acct, $svc_acct if $svc_acct;
319       }
320
321       if ( scalar(@svc_acct) == 0 ) {
322         return "Must order an account in package ". $pkgnum. " first";
323       } elsif ( scalar(@svc_acct) > 1 ) {
324         return "More than one account in package ". $pkgnum. ": specify admin contact email";
325       } else {
326         $self->email($svc_acct[0]->username. '@'. $mydomain);
327       }
328     }
329   }
330
331   #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
332   if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
333     $recref->{domain} = "$1.$2";
334   # hmmmmmmmm.
335   } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
336     $recref->{domain} = $1;
337   } else {
338     return "Illegal domain ". $recref->{domain}.
339            " (or unknown registry - try \$whois_hack)";
340   }
341
342   $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
343   $recref->{action} = $1;
344
345   my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
346   return "Unknown catchall" unless $svc_acct || ! $recref->{catchall};
347
348   $self->ut_textn('purpose');
349
350 }
351
352 =item whois
353
354 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
355 undef if the domain is not found in whois.
356
357 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
358
359 =cut
360
361 sub whois {
362   $whois_hack or new Net::Whois::Domain $_[0]->domain;
363 }
364
365 =item _whois
366
367 Depriciated.
368
369 =cut
370
371 sub _whois {
372   die "_whois depriciated";
373 }
374
375 =item submit_internic
376
377 Submits a registration email for this domain.
378
379 =cut
380
381 sub submit_internic {
382   my $self = shift;
383
384   my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
385   return unless $cust_pkg;
386   my $cust_main = qsearchs( 'cust_main', { 'custnum' => $cust_pkg->custnum } );
387   return unless $cust_main;
388
389   my %subs = (
390     'action'       => $self->action,
391     'purpose'      => $self->purpose,
392     'domain'       => $self->domain,
393     'company'      => $cust_main->company 
394                         || $cust_main->getfield('first'). ' '.
395                            $cust_main->getfield('last')
396                       ,
397     'city'         => $cust_main->city,
398     'state'        => $cust_main->state,
399     'zip'          => $cust_main->zip,
400     'country'      => $cust_main->country,
401     'last'         => $cust_main->getfield('last'),
402     'first'        => $cust_main->getfield('first'),
403     'daytime'      => $cust_main->daytime,
404     'fax'          => $cust_main->fax,
405     'email'        => $self->email,
406     'tech_contact' => $tech_contact,
407     'primary'      => shift @nameservers,
408     'primary_ip'   => shift @nameserver_ips,
409   );
410
411   #yuck
412   my @xtemplate = @template;
413   my @body;
414   my $line;
415   OLOOP: while ( defined( $line = shift @xtemplate ) ) {
416
417     if ( $line =~ /^###LOOP###$/ ) {
418       my(@buffer);
419       LOADBUF: while ( defined( $line = shift @xtemplate ) ) {
420         last LOADBUF if ( $line =~ /^###ENDLOOP###$/ );
421         push @buffer, $line;
422       }
423       my %lubs = (
424         'address'      => $cust_main->address2 
425                             ? [ $cust_main->address1, $cust_main->address2 ]
426                             : [ $cust_main->address1 ]
427                           ,
428         'secondary'    => [ @nameservers ],
429         'secondary_ip' => [ @nameserver_ips ],
430       );
431       LOOP: while (1) {
432         my @xbuffer = @buffer;
433         SUBLOOP: while ( defined( $line = shift @xbuffer ) ) {
434           if ( $line =~ /###(\w+)###/ ) {
435             #last LOOP unless my($lub)=shift@{$lubs{$1}};
436             next OLOOP unless my $lub = shift @{$lubs{$1}};
437             $line =~ s/###(\w+)###/$lub/e;
438             redo SUBLOOP;
439           } else {
440             push @body, $line;
441           }
442         } #SUBLOOP
443       } #LOOP
444
445     }
446
447     if ( $line =~ /###(\w+)###/ ) {
448       #$line =~ s/###(\w+)###/$subs{$1}/eg;
449       $line =~ s/###(\w+)###/$subs{$1}/e;
450       redo OLOOP;
451     } else {
452       push @body, $line;
453     }
454
455   } #OLOOP
456
457   my $subject;
458   if ( $self->action eq "M" ) {
459     $subject = "MODIFY DOMAIN ". $self->domain;
460   } elsif ( $self->action eq "N" ) { 
461     $subject = "NEW DOMAIN ". $self->domain;
462   } else {
463     croak "submit_internic called with action ". $self->action;
464   }
465
466   $ENV{SMTPHOSTS} = $smtpmachine;
467   $ENV{MAILADDRESS} = $from;
468   my $header = Mail::Header->new( [
469     "From: $from",
470     "To: $to",
471     "Sender: $from",
472     "Reply-To: $from",
473     "Date: ". time2str("%a, %d %b %Y %X %z", time),
474     "Subject: $subject",
475   ] );
476
477   my($msg)=Mail::Internet->new(
478     'Header' => $header,
479     'Body' => \@body,
480   );
481
482   $msg->smtpsend or die "Can't send registration email"; #die? warn?
483
484 }
485
486 =back
487
488 =head1 VERSION
489
490 $Id: svc_domain.pm,v 1.14 2001-08-17 10:57:23 ivan Exp $
491
492 =head1 BUGS
493
494 All BIND/DNS fields should be included (and exported).
495
496 Delete doesn't send a registration template.
497
498 All registries should be supported.
499
500 Should change action to a real field.
501
502 The $recref stuff in sub check should be cleaned up.
503
504 =head1 SEE ALSO
505
506 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
507 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
508 L<dot-qmail>, schema.html from the base documentation, config.html from the
509 base documentation.
510
511 =cut
512
513 1;
514
515