use Net::SSH::ssh_cmd for all job queueing rather than local duplicated ssh subs
[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 {
245       'svcnum' => $self->svcnum,
246       'job'    => 'Net::SSH::ssh_cmd',
247     };
248     $error = $queue->insert("root\@$qshellmachine", "[ -e $dir/.qmail-$qdomain-default ] || { touch $dir/.qmail-$qdomain-default; chown $uid:$gid $dir/.qmail-$qdomain-default; }" );
249
250     }
251   }
252
253   ''; #no error
254 }
255
256 =item delete
257
258 Deletes this domain from the database.  If there is an error, returns the
259 error, otherwise returns false.
260
261 The corresponding FS::cust_svc record will be deleted as well.
262
263 =cut
264
265 sub delete {
266   my $self = shift;
267
268   return "Can't delete a domain which has accounts!"
269     if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
270
271   return "Can't delete a domain with (svc_acct_sm) mail aliases!"
272     if defined( $FS::Record::dbdef->table('svc_acct_sm') )
273        && qsearch('svc_acct_sm', { 'domsvc' => $self->svcnum } );
274
275   return "Can't delete a domain with (domain_record) zone entries!"
276     if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
277
278   $self->SUPER::delete;
279 }
280
281 =item replace OLD_RECORD
282
283 Replaces OLD_RECORD with this one in the database.  If there is an error,
284 returns the error, otherwise returns false.
285
286 =cut
287
288 sub replace {
289   my ( $new, $old ) = ( shift, shift );
290   my $error;
291
292   return "Can't change domain - reorder."
293     if $old->getfield('domain') ne $new->getfield('domain'); 
294
295   $new->SUPER::replace($old);
296
297 }
298
299 =item suspend
300
301 Just returns false (no error) for now.
302
303 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
304
305 =item unsuspend
306
307 Just returns false (no error) for now.
308
309 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
310
311 =item cancel
312
313 Just returns false (no error) for now.
314
315 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
316
317 =item check
318
319 Checks all fields to make sure this is a valid domain.  If there is an error,
320 returns the error, otherwise returns false.  Called by the insert and replace
321 methods.
322
323 Sets any fixed values; see L<FS::part_svc>.
324
325 =cut
326
327 sub check {
328   my $self = shift;
329
330   my $x = $self->setfixed;
331   return $x unless ref($x);
332   #my $part_svc = $x;
333
334   my $error = $self->ut_numbern('svcnum')
335               || $self->ut_numbern('catchall')
336   ;
337   return $error if $error;
338
339   #hmm
340   my $pkgnum;
341   if ( $self->svcnum ) {
342     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
343     $pkgnum = $cust_svc->pkgnum;
344   } else {
345     $pkgnum = $self->pkgnum;
346   }
347
348   my($recref) = $self->hashref;
349
350   unless ( $whois_hack ) {
351     unless ( $self->email ) { #find out an email address
352       my @svc_acct;
353       foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
354         my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
355         push @svc_acct, $svc_acct if $svc_acct;
356       }
357
358       if ( scalar(@svc_acct) == 0 ) {
359         return "Must order an account in package ". $pkgnum. " first";
360       } elsif ( scalar(@svc_acct) > 1 ) {
361         return "More than one account in package ". $pkgnum. ": specify admin contact email";
362       } else {
363         $self->email($svc_acct[0]->email );
364       }
365     }
366   }
367
368   #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
369   if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
370     $recref->{domain} = "$1.$2";
371   # hmmmmmmmm.
372   } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
373     $recref->{domain} = $1;
374   } else {
375     return "Illegal domain ". $recref->{domain}.
376            " (or unknown registry - try \$whois_hack)";
377   }
378
379   $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
380   $recref->{action} = $1;
381
382   my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
383   return "Unknown catchall" unless $svc_acct || ! $recref->{catchall};
384
385   $self->ut_textn('purpose');
386
387 }
388
389 =item whois
390
391 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
392 undef if the domain is not found in whois.
393
394 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
395
396 =cut
397
398 sub whois {
399   $whois_hack or new Net::Whois::Domain $_[0]->domain;
400 }
401
402 =item _whois
403
404 Depriciated.
405
406 =cut
407
408 sub _whois {
409   die "_whois depriciated";
410 }
411
412 =item submit_internic
413
414 Submits a registration email for this domain.
415
416 =cut
417
418 sub submit_internic {
419   #my $self = shift;
420   carp "submit_internic depreciated";
421 }
422
423 =back
424
425 =head1 VERSION
426
427 $Id: svc_domain.pm,v 1.24 2002-02-20 01:03:09 ivan Exp $
428
429 =head1 BUGS
430
431 All BIND/DNS fields should be included (and exported).
432
433 Delete doesn't send a registration template.
434
435 All registries should be supported.
436
437 Should change action to a real field.
438
439 The $recref stuff in sub check should be cleaned up.
440
441 =head1 SEE ALSO
442
443 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
444 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
445 L<dot-qmail>, schema.html from the base documentation, config.html from the
446 base documentation.
447
448 =cut
449
450 1;
451
452