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