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