->setfixed untaints svcnum
[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
293   my $x = $self->setfixed;
294   return $x unless ref($x);
295   my $part_svc = $x;
296
297   my $error = $self->ut_numbern('catchall');
298   return $error if $error;
299
300   #hmm
301   my $pkgnum;
302   if ( $self->svcnum ) {
303     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
304     $pkgnum = $cust_svc->pkgnum;
305   } else {
306     $pkgnum = $self->pkgnum;
307   }
308
309   my($recref) = $self->hashref;
310
311   unless ( $whois_hack ) {
312     unless ( $self->email ) { #find out an email address
313       my @svc_acct;
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;
317       }
318
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";
323       } else {
324         $self->email($svc_acct[0]->username. '@'. $mydomain);
325       }
326     }
327   }
328
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";
332   # hmmmmmmmm.
333   } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
334     $recref->{domain} = $1;
335   } else {
336     return "Illegal domain ". $recref->{domain}.
337            " (or unknown registry - try \$whois_hack)";
338   }
339
340   $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
341   $recref->{action} = $1;
342
343   my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
344   return "Unknown catchall" unless $svc_acct || ! $recref->{catchall};
345
346   $self->ut_textn('purpose');
347
348 }
349
350 =item whois
351
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.
354
355 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
356
357 =cut
358
359 sub whois {
360   $whois_hack or new Net::Whois::Domain $_[0]->domain;
361 }
362
363 =item _whois
364
365 Depriciated.
366
367 =cut
368
369 sub _whois {
370   die "_whois depriciated";
371 }
372
373 =item submit_internic
374
375 Submits a registration email for this domain.
376
377 =cut
378
379 sub submit_internic {
380   my $self = shift;
381
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;
386
387   my %subs = (
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')
394                       ,
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,
407   );
408
409   #yuck
410   my @xtemplate = @template;
411   my @body;
412   my $line;
413   OLOOP: while ( defined( $line = shift @xtemplate ) ) {
414
415     if ( $line =~ /^###LOOP###$/ ) {
416       my(@buffer);
417       LOADBUF: while ( defined( $line = shift @xtemplate ) ) {
418         last LOADBUF if ( $line =~ /^###ENDLOOP###$/ );
419         push @buffer, $line;
420       }
421       my %lubs = (
422         'address'      => $cust_main->address2 
423                             ? [ $cust_main->address1, $cust_main->address2 ]
424                             : [ $cust_main->address1 ]
425                           ,
426         'secondary'    => [ @nameservers ],
427         'secondary_ip' => [ @nameserver_ips ],
428       );
429       LOOP: while (1) {
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;
436             redo SUBLOOP;
437           } else {
438             push @body, $line;
439           }
440         } #SUBLOOP
441       } #LOOP
442
443     }
444
445     if ( $line =~ /###(\w+)###/ ) {
446       #$line =~ s/###(\w+)###/$subs{$1}/eg;
447       $line =~ s/###(\w+)###/$subs{$1}/e;
448       redo OLOOP;
449     } else {
450       push @body, $line;
451     }
452
453   } #OLOOP
454
455   my $subject;
456   if ( $self->action eq "M" ) {
457     $subject = "MODIFY DOMAIN ". $self->domain;
458   } elsif ( $self->action eq "N" ) { 
459     $subject = "NEW DOMAIN ". $self->domain;
460   } else {
461     croak "submit_internic called with action ". $self->action;
462   }
463
464   $ENV{SMTPHOSTS} = $smtpmachine;
465   $ENV{MAILADDRESS} = $from;
466   my $header = Mail::Header->new( [
467     "From: $from",
468     "To: $to",
469     "Sender: $from",
470     "Reply-To: $from",
471     "Date: ". time2str("%a, %d %b %Y %X %z", time),
472     "Subject: $subject",
473   ] );
474
475   my($msg)=Mail::Internet->new(
476     'Header' => $header,
477     'Body' => \@body,
478   );
479
480   $msg->smtpsend or die "Can't send registration email"; #die? warn?
481
482 }
483
484 =back
485
486 =head1 VERSION
487
488 $Id: svc_domain.pm,v 1.15 2001-08-19 08:18:01 ivan Exp $
489
490 =head1 BUGS
491
492 All BIND/DNS fields should be included (and exported).
493
494 Delete doesn't send a registration template.
495
496 All registries should be supported.
497
498 Should change action to a real field.
499
500 The $recref stuff in sub check should be cleaned up.
501
502 =head1 SEE ALSO
503
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
507 base documentation.
508
509 =cut
510
511 1;
512
513