1e16ba87f42546e11a019f248ddc0d4960ad1878
[freeside.git] / FS / FS / svc_domain.pm
1 package FS::svc_domain;
2
3 use strict;
4 use vars qw( @ISA $whois_hack $conf $smtpmachine
5   @mxmachines @nsmachines $soadefaultttl $soaemail $soaexpire $soamachine
6   $soarefresh $soaretry $qshellmachine $nossh_hack %arecords %cnamerecords
7 );
8 use Carp;
9 use Mail::Internet;
10 use Mail::Header;
11 use Date::Format;
12 use Net::Whois 1.0;
13 use Net::SSH;
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 use FS::queue;
23
24 @ISA = qw( FS::svc_Common );
25
26 #ask FS::UID to run this stuff for us later
27 $FS::UID::callback{'FS::domain'} = sub { 
28   $conf = new FS::Conf;
29
30   $smtpmachine = $conf->config('smtpmachine');
31
32   %arecords      = map { split /\t/ } $conf->config('arecords');
33   %cnamerecords  = map { split /\t/ } $conf->config('cnamerecords');
34   @mxmachines    = $conf->config('mxmachines');
35   @nsmachines    = $conf->config('nsmachines');
36   $soadefaultttl = $conf->config('soadefaultttl');
37   $soaemail      = $conf->config('soaemail');
38   $soaexpire     = $conf->config('soaexpire');
39   $soamachine    = $conf->config('soamachine');
40   $soarefresh    = $conf->config('soarefresh');
41   $soaretry      = $conf->config('soaretry');
42
43   $qshellmachine = $conf->exists('qmailmachines')
44                    ? $conf->config('shellmachine')
45                    : '';
46 };
47
48 =head1 NAME
49
50 FS::svc_domain - Object methods for svc_domain records
51
52 =head1 SYNOPSIS
53
54   use FS::svc_domain;
55
56   $record = new FS::svc_domain \%hash;
57   $record = new FS::svc_domain { 'column' => 'value' };
58
59   $error = $record->insert;
60
61   $error = $new_record->replace($old_record);
62
63   $error = $record->delete;
64
65   $error = $record->check;
66
67   $error = $record->suspend;
68
69   $error = $record->unsuspend;
70
71   $error = $record->cancel;
72
73 =head1 DESCRIPTION
74
75 An FS::svc_domain object represents a domain.  FS::svc_domain inherits from
76 FS::svc_Common.  The following fields are currently supported:
77
78 =over 4
79
80 =item svcnum - primary key (assigned automatically for new accounts)
81
82 =item domain
83
84 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
85
86 =back
87
88 =head1 METHODS
89
90 =over 4
91
92 =item new HASHREF
93
94 Creates a new domain.  To add the domain to the database, see L<"insert">.
95
96 =cut
97
98 sub table { 'svc_domain'; }
99
100 =item insert
101
102 Adds this domain to the database.  If there is an error, returns the error,
103 otherwise returns false.
104
105 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be 
106 defined.  An FS::cust_svc record will be created and inserted.
107
108 The additional field I<action> should be set to I<N> for new domains or I<M>
109 for transfers.
110
111 A registration or transfer email will be submitted unless
112 $FS::svc_domain::whois_hack is true.
113
114 The additional field I<email> can be used to manually set the admin contact
115 email address on this email.  Otherwise, the svc_acct records for this package 
116 (see L<FS::cust_pkg>) are searched.  If there is exactly one svc_acct record
117 in the same package, it is automatically used.  Otherwise an error is returned.
118
119 If any I<soamachine> configuration file exists, an SOA record is added to
120 the domain_record table (see <FS::domain_record>).
121
122 If any machines are defined in the I<nsmachines> configuration file, NS
123 records are added to the domain_record table (see L<FS::domain_record>).
124
125 If any machines are defined in the I<mxmachines> configuration file, MX
126 records are added to the domain_record table (see L<FS::domain_record>).
127
128 If the I<arecords> configuration file exits, A records are added to the
129 domain_record table.  The I<cnamerecords> file does the same thing for
130 CNAME records.
131
132 If a machine is defined in the I<shellmachine> configuration value, the
133 I<qmailmachines> configuration file exists, and the I<catchall> field points
134 to an an account with a home directory (see L<FS::svc_acct>), the command:
135
136   [ -e $dir/.qmail-$qdomain-defualt ] || {
137     touch $dir/.qmail-$qdomain-default;
138     chown $uid:$gid $dir/.qmail-$qdomain-default;
139   }
140
141 is executed on shellmachine via ssh (see L<dot-qmail/"EXTENSION ADDRESSES">).
142 This behaviour can be supressed by setting $FS::svc_domain::nossh_hack true.
143
144 a machine is defined
145 in the 
146
147 =cut
148
149 sub insert {
150   my $self = shift;
151   my $error;
152
153   local $SIG{HUP} = 'IGNORE';
154   local $SIG{INT} = 'IGNORE';
155   local $SIG{QUIT} = 'IGNORE';
156   local $SIG{TERM} = 'IGNORE';
157   local $SIG{TSTP} = 'IGNORE';
158   local $SIG{PIPE} = 'IGNORE';
159
160   my $oldAutoCommit = $FS::UID::AutoCommit;
161   local $FS::UID::AutoCommit = 0;
162   my $dbh = dbh;
163
164   $error = $self->check;
165   return $error if $error;
166
167   return "Domain in use (here)"
168     if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
169
170   my $whois = $self->whois;
171   if ( $self->action eq "N" && ! $whois_hack && $whois ) {
172     $dbh->rollback if $oldAutoCommit;
173     return "Domain in use (see whois)";
174   }
175   if ( $self->action eq "M" && ! $whois ) {
176     $dbh->rollback if $oldAutoCommit;
177     return "Domain not found (see whois)";
178   }
179
180   $error = $self->SUPER::insert;
181   if ( $error ) {
182     $dbh->rollback if $oldAutoCommit;
183     return $error;
184   }
185
186   $self->submit_internic unless $whois_hack;
187
188   if ( $soamachine ) {
189     my $soa = new FS::domain_record {
190       'svcnum'  => $self->svcnum,
191       'reczone' => '@',
192       'recaf'   => 'IN',
193       'rectype' => 'SOA',
194       'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
195                    "$soarefresh $soaretry $soaexpire $soadefaultttl )"
196     };
197     $error = $soa->insert;
198     if ( $error ) {
199       $dbh->rollback if $oldAutoCommit;
200       return "couldn't insert SOA record for new domain: $error";
201     }
202
203     foreach my $nsmachine ( @nsmachines ) {
204       my $ns = new FS::domain_record {
205         'svcnum'  => $self->svcnum,
206         'reczone' => '@',
207         'recaf'   => 'IN',
208         'rectype' => 'NS',
209         'recdata' => $nsmachine,
210       };
211       my $error = $ns->insert;
212       if ( $error ) {
213         $dbh->rollback if $oldAutoCommit;
214         return "couldn't insert NS record for new domain: $error";
215       }
216     }
217
218     foreach my $mxmachine ( @mxmachines ) {
219       my $mx = new FS::domain_record {
220         'svcnum'  => $self->svcnum,
221         'reczone' => '@',
222         'recaf'   => 'IN',
223         'rectype' => 'MX',
224         'recdata' => $mxmachine,
225       };
226       my $error = $mx->insert;
227       if ( $error ) {
228         $dbh->rollback if $oldAutoCommit;
229         return "couldn't insert MX record for new domain: $error";
230       }
231     }
232
233     foreach my $arecord ( keys %arecords ) {
234       my $arec = new FS::domain_record {
235         'svcnum'  => $self->svcnum,
236         'reczone' => $arecord,
237         'recaf'   => 'IN',
238         'rectype' => 'A',
239         'recdata' => $arecords{$arecord},
240       };
241       my $error = $arec->insert;
242       if ( $error ) {
243         $dbh->rollback if $oldAutoCommit;
244         return "WARNING: couldn't insert A record for new domain: $error";
245       }
246     }
247
248     foreach my $cnamerecord ( keys %cnamerecords ) {
249       my $cnamerec = new FS::domain_record {
250         'svcnum'  => $self->svcnum,
251         'reczone' => $cnamerecord,
252         'recaf'   => 'IN',
253         'rectype' => 'CNAME',
254         'recdata' => $cnamerecords{$cnamerecord},
255       };
256       my $error = $cnamerec->insert;
257       if ( $error ) {
258         $dbh->rollback if $oldAutoCommit;
259         return "WARNING: couldn't insert CNAME record for new domain: $error";
260       }
261     }
262
263   }
264
265   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
266
267   if ( $qshellmachine && $self->catchall && ! $nossh_hack ) {
268
269     my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } )
270       or warn "WARNING: inserted unknown catchall: ". $self->catchall;
271     if ( $svc_acct && $svc_acct->dir ) {
272       my $qdomain = $self->domain;
273       $qdomain =~ s/\./:/g; #see manpage for 'dot-qmail': EXTENSION ADDRESSES
274       my ( $uid, $gid, $dir ) = (
275         $svc_acct->uid,
276         $svc_acct->gid,
277         $svc_acct->dir,
278       );
279   
280     my $queue = new FS::queue {
281       'svcnum' => $self->svcnum,
282       'job'    => 'Net::SSH::ssh_cmd',
283     };
284     $error = $queue->insert("root\@$qshellmachine", "[ -e $dir/.qmail-$qdomain-default ] || { touch $dir/.qmail-$qdomain-default; chown $uid:$gid $dir/.qmail-$qdomain-default; }" );
285
286     }
287   }
288
289   ''; #no error
290 }
291
292 =item delete
293
294 Deletes this domain from the database.  If there is an error, returns the
295 error, otherwise returns false.
296
297 The corresponding FS::cust_svc record will be deleted as well.
298
299 =cut
300
301 sub delete {
302   my $self = shift;
303
304   return "Can't delete a domain which has accounts!"
305     if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
306
307   return "Can't delete a domain with (svc_acct_sm) mail aliases!"
308     if defined( $FS::Record::dbdef->table('svc_acct_sm') )
309        && qsearch('svc_acct_sm', { 'domsvc' => $self->svcnum } );
310
311   return "Can't delete a domain with (domain_record) zone entries!"
312     if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
313
314   $self->SUPER::delete;
315 }
316
317 =item replace OLD_RECORD
318
319 Replaces OLD_RECORD with this one in the database.  If there is an error,
320 returns the error, otherwise returns false.
321
322 =cut
323
324 sub replace {
325   my ( $new, $old ) = ( shift, shift );
326   my $error;
327
328   return "Can't change domain - reorder."
329     if $old->getfield('domain') ne $new->getfield('domain'); 
330
331   $new->SUPER::replace($old);
332
333 }
334
335 =item suspend
336
337 Just returns false (no error) for now.
338
339 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
340
341 =item unsuspend
342
343 Just returns false (no error) for now.
344
345 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
346
347 =item cancel
348
349 Just returns false (no error) for now.
350
351 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
352
353 =item check
354
355 Checks all fields to make sure this is a valid domain.  If there is an error,
356 returns the error, otherwise returns false.  Called by the insert and replace
357 methods.
358
359 Sets any fixed values; see L<FS::part_svc>.
360
361 =cut
362
363 sub check {
364   my $self = shift;
365
366   my $x = $self->setfixed;
367   return $x unless ref($x);
368   #my $part_svc = $x;
369
370   my $error = $self->ut_numbern('svcnum')
371               || $self->ut_numbern('catchall')
372   ;
373   return $error if $error;
374
375   #hmm
376   my $pkgnum;
377   if ( $self->svcnum ) {
378     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
379     $pkgnum = $cust_svc->pkgnum;
380   } else {
381     $pkgnum = $self->pkgnum;
382   }
383
384   my($recref) = $self->hashref;
385
386   unless ( $whois_hack ) {
387     unless ( $self->email ) { #find out an email address
388       my @svc_acct;
389       foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
390         my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
391         push @svc_acct, $svc_acct if $svc_acct;
392       }
393
394       if ( scalar(@svc_acct) == 0 ) {
395         return "Must order an account in package ". $pkgnum. " first";
396       } elsif ( scalar(@svc_acct) > 1 ) {
397         return "More than one account in package ". $pkgnum. ": specify admin contact email";
398       } else {
399         $self->email($svc_acct[0]->email );
400       }
401     }
402   }
403
404   #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
405   if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
406     $recref->{domain} = "$1.$2";
407   # hmmmmmmmm.
408   } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
409     $recref->{domain} = $1;
410   } else {
411     return "Illegal domain ". $recref->{domain}.
412            " (or unknown registry - try \$whois_hack)";
413   }
414
415   $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
416   $recref->{action} = $1;
417
418   my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
419   return "Unknown catchall" unless $svc_acct || ! $recref->{catchall};
420
421   $self->ut_textn('purpose');
422
423 }
424
425 =item whois
426
427 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
428 undef if the domain is not found in whois.
429
430 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
431
432 =cut
433
434 sub whois {
435   $whois_hack or new Net::Whois::Domain $_[0]->domain;
436 }
437
438 =item _whois
439
440 Depriciated.
441
442 =cut
443
444 sub _whois {
445   die "_whois depriciated";
446 }
447
448 =item submit_internic
449
450 Submits a registration email for this domain.
451
452 =cut
453
454 sub submit_internic {
455   #my $self = shift;
456   carp "submit_internic depreciated";
457 }
458
459 =back
460
461 =head1 VERSION
462
463 $Id: svc_domain.pm,v 1.25 2002-03-09 10:19:38 khoff Exp $
464
465 =head1 BUGS
466
467 All BIND/DNS fields should be included (and exported).
468
469 Delete doesn't send a registration template.
470
471 All registries should be supported.
472
473 Should change action to a real field.
474
475 The $recref stuff in sub check should be cleaned up.
476
477 =head1 SEE ALSO
478
479 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
480 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
481 L<dot-qmail>, schema.html from the base documentation, config.html from the
482 base documentation.
483
484 =cut
485
486 1;
487
488