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
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 qsearch('svc_acct_sm', { 'domsvc' => $self->svcnum } );
284 return "Can't delete a domain with (domain_record) zone entries!"
285 if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
287 $self->SUPER::delete;
290 =item replace OLD_RECORD
292 Replaces OLD_RECORD with this one in the database. If there is an error,
293 returns the error, otherwise returns false.
298 my ( $new, $old ) = ( shift, shift );
301 return "Can't change domain - reorder."
302 if $old->getfield('domain') ne $new->getfield('domain');
304 $new->SUPER::replace($old);
310 Just returns false (no error) for now.
312 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
316 Just returns false (no error) for now.
318 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
322 Just returns false (no error) for now.
324 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
328 Checks all fields to make sure this is a valid domain. If there is an error,
329 returns the error, otherwise returns false. Called by the insert and replace
332 Sets any fixed values; see L<FS::part_svc>.
339 my $x = $self->setfixed;
340 return $x unless ref($x);
343 my $error = $self->ut_numbern('catchall');
344 return $error if $error;
348 if ( $self->svcnum ) {
349 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
350 $pkgnum = $cust_svc->pkgnum;
352 $pkgnum = $self->pkgnum;
355 my($recref) = $self->hashref;
357 unless ( $whois_hack ) {
358 unless ( $self->email ) { #find out an email address
360 foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
361 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
362 push @svc_acct, $svc_acct if $svc_acct;
365 if ( scalar(@svc_acct) == 0 ) {
366 return "Must order an account in package ". $pkgnum. " first";
367 } elsif ( scalar(@svc_acct) > 1 ) {
368 return "More than one account in package ". $pkgnum. ": specify admin contact email";
370 $self->email($svc_acct[0]->email );
375 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
376 if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
377 $recref->{domain} = "$1.$2";
379 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
380 $recref->{domain} = $1;
382 return "Illegal domain ". $recref->{domain}.
383 " (or unknown registry - try \$whois_hack)";
386 $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
387 $recref->{action} = $1;
389 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
390 return "Unknown catchall" unless $svc_acct || ! $recref->{catchall};
392 $self->ut_textn('purpose');
398 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
399 undef if the domain is not found in whois.
401 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
406 $whois_hack or new Net::Whois::Domain $_[0]->domain;
416 die "_whois depriciated";
419 =item submit_internic
421 Submits a registration email for this domain.
425 sub submit_internic {
428 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
429 return unless $cust_pkg;
430 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $cust_pkg->custnum } );
431 return unless $cust_main;
434 'action' => $self->action,
435 'purpose' => $self->purpose,
436 'domain' => $self->domain,
437 'company' => $cust_main->company
438 || $cust_main->getfield('first'). ' '.
439 $cust_main->getfield('last')
441 'city' => $cust_main->city,
442 'state' => $cust_main->state,
443 'zip' => $cust_main->zip,
444 'country' => $cust_main->country,
445 'last' => $cust_main->getfield('last'),
446 'first' => $cust_main->getfield('first'),
447 'daytime' => $cust_main->daytime,
448 'fax' => $cust_main->fax,
449 'email' => $self->email,
450 'tech_contact' => $tech_contact,
451 'primary' => shift @nameservers,
452 'primary_ip' => shift @nameserver_ips,
456 my @xtemplate = @template;
459 OLOOP: while ( defined( $line = shift @xtemplate ) ) {
461 if ( $line =~ /^###LOOP###$/ ) {
463 LOADBUF: while ( defined( $line = shift @xtemplate ) ) {
464 last LOADBUF if ( $line =~ /^###ENDLOOP###$/ );
468 'address' => $cust_main->address2
469 ? [ $cust_main->address1, $cust_main->address2 ]
470 : [ $cust_main->address1 ]
472 'secondary' => [ @nameservers ],
473 'secondary_ip' => [ @nameserver_ips ],
476 my @xbuffer = @buffer;
477 SUBLOOP: while ( defined( $line = shift @xbuffer ) ) {
478 if ( $line =~ /###(\w+)###/ ) {
479 #last LOOP unless my($lub)=shift@{$lubs{$1}};
480 next OLOOP unless my $lub = shift @{$lubs{$1}};
481 $line =~ s/###(\w+)###/$lub/e;
491 if ( $line =~ /###(\w+)###/ ) {
492 #$line =~ s/###(\w+)###/$subs{$1}/eg;
493 $line =~ s/###(\w+)###/$subs{$1}/e;
502 if ( $self->action eq "M" ) {
503 $subject = "MODIFY DOMAIN ". $self->domain;
504 } elsif ( $self->action eq "N" ) {
505 $subject = "NEW DOMAIN ". $self->domain;
507 croak "submit_internic called with action ". $self->action;
510 $ENV{SMTPHOSTS} = $smtpmachine;
511 $ENV{MAILADDRESS} = $from;
512 my $header = Mail::Header->new( [
517 "Date: ". time2str("%a, %d %b %Y %X %z", time),
521 my($msg)=Mail::Internet->new(
526 $msg->smtpsend or die "Can't send registration email"; #die? warn?
534 $Id: svc_domain.pm,v 1.16 2001-08-20 09:41:52 ivan Exp $
538 All BIND/DNS fields should be included (and exported).
540 Delete doesn't send a registration template.
542 All registries should be supported.
544 Should change action to a real field.
546 The $recref stuff in sub check should be cleaned up.
550 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
551 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
552 L<dot-qmail>, schema.html from the base documentation, config.html from the