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