1 package FS::svc_domain;
4 use vars qw( @ISA $whois_hack $conf $smtpmachine
5 $tech_contact $from $to @nameservers @nameserver_ips @template
6 @mxmachines @nsmachines $soadefaultttl $soaemail $soaexpire $soamachine
7 $soarefresh $soaretry $qshellmachine $nossh_hack
15 use FS::Record qw(fields qsearch qsearchs dbh);
22 use FS::domain_record;
24 @ISA = qw( FS::svc_Common );
26 #ask FS::UID to run this stuff for us later
27 $FS::UID::callback{'FS::domain'} = sub {
30 $smtpmachine = $conf->config('smtpmachine');
32 my($internic)="/registries/internic";
33 $tech_contact = $conf->config("$internic/tech_contact");
34 $from = $conf->config("$internic/from");
35 $to = $conf->config("$internic/to");
36 my(@ns) = $conf->config("$internic/nameservers");
38 /^\s*\d+\.\d+\.\d+\.\d+\s+([^\s]+)\s*$/
39 or die "Illegal line in $internic/nameservers";
43 /^\s*(\d+\.\d+\.\d+\.\d+)\s+([^\s]+)\s*$/
44 or die "Illegal line in $internic/nameservers!";
47 @template = map { $_. "\n" } $conf->config("$internic/template");
49 @mxmachines = $conf->config('mxmachines');
50 @nsmachines = $conf->config('nsmachines');
51 $soadefaultttl = $conf->config('soadefaultttl');
52 $soaemail = $conf->config('soaemail');
53 $soaexpire = $conf->config('soaexpire');
54 $soamachine = $conf->config('soamachine');
55 $soarefresh = $conf->config('soarefresh');
56 $soaretry = $conf->config('soaretry');
58 $qshellmachine = $conf->exists('qmailmachines')
59 ? $conf->config('shellmachine')
65 FS::svc_domain - Object methods for svc_domain records
71 $record = new FS::svc_domain \%hash;
72 $record = new FS::svc_domain { 'column' => 'value' };
74 $error = $record->insert;
76 $error = $new_record->replace($old_record);
78 $error = $record->delete;
80 $error = $record->check;
82 $error = $record->suspend;
84 $error = $record->unsuspend;
86 $error = $record->cancel;
90 An FS::svc_domain object represents a domain. FS::svc_domain inherits from
91 FS::svc_Common. The following fields are currently supported:
95 =item svcnum - primary key (assigned automatically for new accounts)
99 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
109 Creates a new domain. To add the domain to the database, see L<"insert">.
113 sub table { 'svc_domain'; }
117 Adds this domain to the database. If there is an error, returns the error,
118 otherwise returns false.
120 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be
121 defined. An FS::cust_svc record will be created and inserted.
123 The additional field I<action> should be set to I<N> for new domains or I<M>
126 A registration or transfer email will be submitted unless
127 $FS::svc_domain::whois_hack is true.
129 The additional field I<email> can be used to manually set the admin contact
130 email address on this email. Otherwise, the svc_acct records for this package
131 (see L<FS::cust_pkg>) are searched. If there is exactly one svc_acct record
132 in the same package, it is automatically used. Otherwise an error is returned.
134 If any I<soamachine> configuration file exists, an SOA record is added to
135 the domain_record table (see <FS::domain_record>).
137 If any machines are defined in the I<nsmachines> configuration file, NS
138 records are added to the domain_record table (see L<FS::domain_record>).
140 If any machines are defined in the I<mxmachines> configuration file, MX
141 records are added to the domain_record table (see L<FS::domain_record>).
143 If a machine is defined in the I<shellmachine> configuration value, the
144 I<qmailmachines> configuration file exists, and the I<catchall> field points
145 to an an account with a home directory (see L<FS::svc_acct>), the command:
147 [ -e $dir/.qmail-$qdomain-defualt ] || {
148 touch $dir/.qmail-$qdomain-default;
149 chown $uid:$gid $dir/.qmail-$qdomain-default;
152 is executed on shellmachine via ssh (see L<dot-qmail/"EXTENSION ADDRESSES">).
153 This behaviour can be supressed by setting $FS::svc_domain::nossh_hack true.
164 local $SIG{HUP} = 'IGNORE';
165 local $SIG{INT} = 'IGNORE';
166 local $SIG{QUIT} = 'IGNORE';
167 local $SIG{TERM} = 'IGNORE';
168 local $SIG{TSTP} = 'IGNORE';
169 local $SIG{PIPE} = 'IGNORE';
171 my $oldAutoCommit = $FS::UID::AutoCommit;
172 local $FS::UID::AutoCommit = 0;
175 $error = $self->check;
176 return $error if $error;
178 return "Domain in use (here)"
179 if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
181 my $whois = $self->whois;
182 if ( $self->action eq "N" && ! $whois_hack && $whois ) {
183 $dbh->rollback if $oldAutoCommit;
184 return "Domain in use (see whois)";
186 if ( $self->action eq "M" && ! $whois ) {
187 $dbh->rollback if $oldAutoCommit;
188 return "Domain not found (see whois)";
191 $error = $self->SUPER::insert;
193 $dbh->rollback if $oldAutoCommit;
197 $self->submit_internic unless $whois_hack;
200 my $soa = new FS::domain_record {
201 'svcnum' => $self->svcnum,
205 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
206 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
208 $error = $soa->insert;
210 $dbh->rollback if $oldAutoCommit;
211 return "couldn't insert SOA record for new domain: $error";
214 foreach my $nsmachine ( @nsmachines ) {
215 my $ns = new FS::domain_record {
216 'svcnum' => $self->svcnum,
220 'recdata' => $nsmachine,
222 my $error = $ns->insert;
224 $dbh->rollback if $oldAutoCommit;
225 return "couldn't insert NS record for new domain: $error";
229 foreach my $mxmachine ( @mxmachines ) {
230 my $mx = new FS::domain_record {
231 'svcnum' => $self->svcnum,
235 'recdata' => $mxmachine,
237 my $error = $mx->insert;
239 $dbh->rollback if $oldAutoCommit;
240 return "couldn't insert MX record for new domain: $error";
246 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
248 if ( $qshellmachine && $self->catchall && ! $nossh_hack ) {
249 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } )
250 or warn "WARNING: inserted unknown catchall: ". $self->catchall;
251 if ( $svc_acct && $svc_acct->dir ) {
252 my $qdomain = $self->domain;
253 $qdomain =~ s/\./:/g; #see manpage for 'dot-qmail': EXTENSION ADDRESSES
254 my ( $uid, $gid, $dir ) = (
259 ssh("root\@$qshellmachine", "[ -e $dir/.qmail-$qdomain-default ] || { touch $dir/.qmail-$qdomain-default; chown $uid:$gid $dir/.qmail-$qdomain-default; }");
268 Deletes this domain from the database. If there is an error, returns the
269 error, otherwise returns false.
271 The corresponding FS::cust_svc record will be deleted as well.
278 return "Can't delete a domain which has accounts!"
279 if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
281 return "Can't delete a domain with (svc_acct_sm) mail aliases!"
282 if defined( $FS::Record::dbdef->table('svc_acct_sm') )
283 && qsearch('svc_acct_sm', { 'domsvc' => $self->svcnum } );
285 return "Can't delete a domain with (domain_record) zone entries!"
286 if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
288 $self->SUPER::delete;
291 =item replace OLD_RECORD
293 Replaces OLD_RECORD with this one in the database. If there is an error,
294 returns the error, otherwise returns false.
299 my ( $new, $old ) = ( shift, shift );
302 return "Can't change domain - reorder."
303 if $old->getfield('domain') ne $new->getfield('domain');
305 $new->SUPER::replace($old);
311 Just returns false (no error) for now.
313 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
317 Just returns false (no error) for now.
319 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
323 Just returns false (no error) for now.
325 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
329 Checks all fields to make sure this is a valid domain. If there is an error,
330 returns the error, otherwise returns false. Called by the insert and replace
333 Sets any fixed values; see L<FS::part_svc>.
340 my $x = $self->setfixed;
341 return $x unless ref($x);
344 my $error = $self->ut_numbern('svcnum')
345 || $self->ut_numbern('catchall')
347 return $error if $error;
351 if ( $self->svcnum ) {
352 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
353 $pkgnum = $cust_svc->pkgnum;
355 $pkgnum = $self->pkgnum;
358 my($recref) = $self->hashref;
360 unless ( $whois_hack ) {
361 unless ( $self->email ) { #find out an email address
363 foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
364 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
365 push @svc_acct, $svc_acct if $svc_acct;
368 if ( scalar(@svc_acct) == 0 ) {
369 return "Must order an account in package ". $pkgnum. " first";
370 } elsif ( scalar(@svc_acct) > 1 ) {
371 return "More than one account in package ". $pkgnum. ": specify admin contact email";
373 $self->email($svc_acct[0]->email );
378 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
379 if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
380 $recref->{domain} = "$1.$2";
382 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
383 $recref->{domain} = $1;
385 return "Illegal domain ". $recref->{domain}.
386 " (or unknown registry - try \$whois_hack)";
389 $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
390 $recref->{action} = $1;
392 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
393 return "Unknown catchall" unless $svc_acct || ! $recref->{catchall};
395 $self->ut_textn('purpose');
401 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
402 undef if the domain is not found in whois.
404 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
409 $whois_hack or new Net::Whois::Domain $_[0]->domain;
419 die "_whois depriciated";
422 =item submit_internic
424 Submits a registration email for this domain.
428 sub submit_internic {
431 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
432 return unless $cust_pkg;
433 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $cust_pkg->custnum } );
434 return unless $cust_main;
437 'action' => $self->action,
438 'purpose' => $self->purpose,
439 'domain' => $self->domain,
440 'company' => $cust_main->company
441 || $cust_main->getfield('first'). ' '.
442 $cust_main->getfield('last')
444 'city' => $cust_main->city,
445 'state' => $cust_main->state,
446 'zip' => $cust_main->zip,
447 'country' => $cust_main->country,
448 'last' => $cust_main->getfield('last'),
449 'first' => $cust_main->getfield('first'),
450 'daytime' => $cust_main->daytime,
451 'fax' => $cust_main->fax,
452 'email' => $self->email,
453 'tech_contact' => $tech_contact,
454 'primary' => shift @nameservers,
455 'primary_ip' => shift @nameserver_ips,
459 my @xtemplate = @template;
462 OLOOP: while ( defined( $line = shift @xtemplate ) ) {
464 if ( $line =~ /^###LOOP###$/ ) {
466 LOADBUF: while ( defined( $line = shift @xtemplate ) ) {
467 last LOADBUF if ( $line =~ /^###ENDLOOP###$/ );
471 'address' => $cust_main->address2
472 ? [ $cust_main->address1, $cust_main->address2 ]
473 : [ $cust_main->address1 ]
475 'secondary' => [ @nameservers ],
476 'secondary_ip' => [ @nameserver_ips ],
479 my @xbuffer = @buffer;
480 SUBLOOP: while ( defined( $line = shift @xbuffer ) ) {
481 if ( $line =~ /###(\w+)###/ ) {
482 #last LOOP unless my($lub)=shift@{$lubs{$1}};
483 next OLOOP unless my $lub = shift @{$lubs{$1}};
484 $line =~ s/###(\w+)###/$lub/e;
494 if ( $line =~ /###(\w+)###/ ) {
495 #$line =~ s/###(\w+)###/$subs{$1}/eg;
496 $line =~ s/###(\w+)###/$subs{$1}/e;
505 if ( $self->action eq "M" ) {
506 $subject = "MODIFY DOMAIN ". $self->domain;
507 } elsif ( $self->action eq "N" ) {
508 $subject = "NEW DOMAIN ". $self->domain;
510 croak "submit_internic called with action ". $self->action;
513 $ENV{SMTPHOSTS} = $smtpmachine;
514 $ENV{MAILADDRESS} = $from;
515 my $header = Mail::Header->new( [
520 "Date: ". time2str("%a, %d %b %Y %X %z", time),
524 my($msg)=Mail::Internet->new(
529 $msg->smtpsend or die "Can't send registration email"; #die? warn?
537 $Id: svc_domain.pm,v 1.21 2001-10-22 12:22:03 ivan Exp $
541 All BIND/DNS fields should be included (and exported).
543 Delete doesn't send a registration template.
545 All registries should be supported.
547 Should change action to a real field.
549 The $recref stuff in sub check should be cleaned up.
553 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
554 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
555 L<dot-qmail>, schema.html from the base documentation, config.html from the