preliminary web config editor
[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 qw(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
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   $smtpmachine = $conf->config('smtpmachine');
30
31   @mxmachines    = $conf->config('mxmachines');
32   @nsmachines    = $conf->config('nsmachines');
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 machines are defined in the I<nsmachines> configuration file, NS
120 records are added to the domain_record table (see L<FS::domain_record>).
121
122 If any machines are defined in the I<mxmachines> configuration file, MX
123 records are added to the domain_record table (see L<FS::domain_record>).
124
125 If a machine is defined in the I<shellmachine> configuration value, the
126 I<qmailmachines> configuration file exists, and the I<catchall> field points
127 to an an account with a home directory (see L<FS::svc_acct>), the command:
128
129   [ -e $dir/.qmail-$qdomain-defualt ] || {
130     touch $dir/.qmail-$qdomain-default;
131     chown $uid:$gid $dir/.qmail-$qdomain-default;
132   }
133
134 is executed on shellmachine via ssh (see L<dot-qmail/"EXTENSION ADDRESSES">).
135 This behaviour can be supressed by setting $FS::svc_domain::nossh_hack true.
136
137 a machine is defined
138 in the 
139
140 =cut
141
142 sub insert {
143   my $self = shift;
144   my $error;
145
146   local $SIG{HUP} = 'IGNORE';
147   local $SIG{INT} = 'IGNORE';
148   local $SIG{QUIT} = 'IGNORE';
149   local $SIG{TERM} = 'IGNORE';
150   local $SIG{TSTP} = 'IGNORE';
151   local $SIG{PIPE} = 'IGNORE';
152
153   my $oldAutoCommit = $FS::UID::AutoCommit;
154   local $FS::UID::AutoCommit = 0;
155   my $dbh = dbh;
156
157   $error = $self->check;
158   return $error if $error;
159
160   return "Domain in use (here)"
161     if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
162
163   my $whois = $self->whois;
164   if ( $self->action eq "N" && ! $whois_hack && $whois ) {
165     $dbh->rollback if $oldAutoCommit;
166     return "Domain in use (see whois)";
167   }
168   if ( $self->action eq "M" && ! $whois ) {
169     $dbh->rollback if $oldAutoCommit;
170     return "Domain not found (see whois)";
171   }
172
173   $error = $self->SUPER::insert;
174   if ( $error ) {
175     $dbh->rollback if $oldAutoCommit;
176     return $error;
177   }
178
179   $self->submit_internic unless $whois_hack;
180
181   if ( $soamachine ) {
182     my $soa = new FS::domain_record {
183       'svcnum'  => $self->svcnum,
184       'reczone' => '@',
185       'recaf'   => 'IN',
186       'rectype' => 'SOA',
187       'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
188                    "$soarefresh $soaretry $soaexpire $soadefaultttl )"
189     };
190     $error = $soa->insert;
191     if ( $error ) {
192       $dbh->rollback if $oldAutoCommit;
193       return "couldn't insert SOA record for new domain: $error";
194     }
195
196     foreach my $nsmachine ( @nsmachines ) {
197       my $ns = new FS::domain_record {
198         'svcnum'  => $self->svcnum,
199         'reczone' => '@',
200         'recaf'   => 'IN',
201         'rectype' => 'NS',
202         'recdata' => $nsmachine,
203       };
204       my $error = $ns->insert;
205       if ( $error ) {
206         $dbh->rollback if $oldAutoCommit;
207         return "couldn't insert NS record for new domain: $error";
208       }
209     }
210
211     foreach my $mxmachine ( @mxmachines ) {
212       my $mx = new FS::domain_record {
213         'svcnum'  => $self->svcnum,
214         'reczone' => '@',
215         'recaf'   => 'IN',
216         'rectype' => 'MX',
217         'recdata' => $mxmachine,
218       };
219       my $error = $mx->insert;
220       if ( $error ) {
221         $dbh->rollback if $oldAutoCommit;
222         return "couldn't insert MX record for new domain: $error";
223       }
224     }
225
226   }
227
228   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
229
230   if ( $qshellmachine && $self->catchall && ! $nossh_hack ) {
231     my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } )
232       or warn "WARNING: inserted unknown catchall: ". $self->catchall;
233     if ( $svc_acct && $svc_acct->dir ) {
234       my $qdomain = $self->domain;
235       $qdomain =~ s/\./:/g; #see manpage for 'dot-qmail': EXTENSION ADDRESSES
236       my ( $uid, $gid, $dir ) = (
237         $svc_acct->uid,
238         $svc_acct->gid,
239         $svc_acct->dir,
240       );
241       ssh("root\@$qshellmachine", "[ -e $dir/.qmail-$qdomain-default ] || { touch $dir/.qmail-$qdomain-default; chown $uid:$gid $dir/.qmail-$qdomain-default; }");
242     }
243   }
244
245   ''; #no error
246 }
247
248 =item delete
249
250 Deletes this domain from the database.  If there is an error, returns the
251 error, otherwise returns false.
252
253 The corresponding FS::cust_svc record will be deleted as well.
254
255 =cut
256
257 sub delete {
258   my $self = shift;
259
260   return "Can't delete a domain which has accounts!"
261     if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
262
263   return "Can't delete a domain with (svc_acct_sm) mail aliases!"
264     if defined( $FS::Record::dbdef->table('svc_acct_sm') )
265        && qsearch('svc_acct_sm', { 'domsvc' => $self->svcnum } );
266
267   return "Can't delete a domain with (domain_record) zone entries!"
268     if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
269
270   $self->SUPER::delete;
271 }
272
273 =item replace OLD_RECORD
274
275 Replaces OLD_RECORD with this one in the database.  If there is an error,
276 returns the error, otherwise returns false.
277
278 =cut
279
280 sub replace {
281   my ( $new, $old ) = ( shift, shift );
282   my $error;
283
284   return "Can't change domain - reorder."
285     if $old->getfield('domain') ne $new->getfield('domain'); 
286
287   $new->SUPER::replace($old);
288
289 }
290
291 =item suspend
292
293 Just returns false (no error) for now.
294
295 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
296
297 =item unsuspend
298
299 Just returns false (no error) for now.
300
301 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
302
303 =item cancel
304
305 Just returns false (no error) for now.
306
307 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
308
309 =item check
310
311 Checks all fields to make sure this is a valid domain.  If there is an error,
312 returns the error, otherwise returns false.  Called by the insert and replace
313 methods.
314
315 Sets any fixed values; see L<FS::part_svc>.
316
317 =cut
318
319 sub check {
320   my $self = shift;
321
322   my $x = $self->setfixed;
323   return $x unless ref($x);
324   #my $part_svc = $x;
325
326   my $error = $self->ut_numbern('svcnum')
327               || $self->ut_numbern('catchall')
328   ;
329   return $error if $error;
330
331   #hmm
332   my $pkgnum;
333   if ( $self->svcnum ) {
334     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
335     $pkgnum = $cust_svc->pkgnum;
336   } else {
337     $pkgnum = $self->pkgnum;
338   }
339
340   my($recref) = $self->hashref;
341
342   unless ( $whois_hack ) {
343     unless ( $self->email ) { #find out an email address
344       my @svc_acct;
345       foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
346         my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
347         push @svc_acct, $svc_acct if $svc_acct;
348       }
349
350       if ( scalar(@svc_acct) == 0 ) {
351         return "Must order an account in package ". $pkgnum. " first";
352       } elsif ( scalar(@svc_acct) > 1 ) {
353         return "More than one account in package ". $pkgnum. ": specify admin contact email";
354       } else {
355         $self->email($svc_acct[0]->email );
356       }
357     }
358   }
359
360   #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
361   if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
362     $recref->{domain} = "$1.$2";
363   # hmmmmmmmm.
364   } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
365     $recref->{domain} = $1;
366   } else {
367     return "Illegal domain ". $recref->{domain}.
368            " (or unknown registry - try \$whois_hack)";
369   }
370
371   $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
372   $recref->{action} = $1;
373
374   my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
375   return "Unknown catchall" unless $svc_acct || ! $recref->{catchall};
376
377   $self->ut_textn('purpose');
378
379 }
380
381 =item whois
382
383 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
384 undef if the domain is not found in whois.
385
386 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
387
388 =cut
389
390 sub whois {
391   $whois_hack or new Net::Whois::Domain $_[0]->domain;
392 }
393
394 =item _whois
395
396 Depriciated.
397
398 =cut
399
400 sub _whois {
401   die "_whois depriciated";
402 }
403
404 =item submit_internic
405
406 Submits a registration email for this domain.
407
408 =cut
409
410 sub submit_internic {
411   #my $self = shift;
412   carp "submit_internic depreciated";
413 }
414
415 =back
416
417 =head1 VERSION
418
419 $Id: svc_domain.pm,v 1.22 2001-10-24 15:29:30 ivan Exp $
420
421 =head1 BUGS
422
423 All BIND/DNS fields should be included (and exported).
424
425 Delete doesn't send a registration template.
426
427 All registries should be supported.
428
429 Should change action to a real field.
430
431 The $recref stuff in sub check should be cleaned up.
432
433 =head1 SEE ALSO
434
435 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
436 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
437 L<dot-qmail>, schema.html from the base documentation, config.html from the
438 base documentation.
439
440 =cut
441
442 1;
443
444