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 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('svcnum')
344 || $self->ut_numbern('catchall')
346 return $error if $error;
350 if ( $self->svcnum ) {
351 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
352 $pkgnum = $cust_svc->pkgnum;
354 $pkgnum = $self->pkgnum;
357 my($recref) = $self->hashref;
359 unless ( $whois_hack ) {
360 unless ( $self->email ) { #find out an email address
362 foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
363 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
364 push @svc_acct, $svc_acct if $svc_acct;
367 if ( scalar(@svc_acct) == 0 ) {
368 return "Must order an account in package ". $pkgnum. " first";
369 } elsif ( scalar(@svc_acct) > 1 ) {
370 return "More than one account in package ". $pkgnum. ": specify admin contact email";
372 $self->email($svc_acct[0]->email );
377 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
378 if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
379 $recref->{domain} = "$1.$2";
381 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
382 $recref->{domain} = $1;
384 return "Illegal domain ". $recref->{domain}.
385 " (or unknown registry - try \$whois_hack)";
388 $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
389 $recref->{action} = $1;
391 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
392 return "Unknown catchall" unless $svc_acct || ! $recref->{catchall};
394 $self->ut_textn('purpose');
400 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
401 undef if the domain is not found in whois.
403 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
408 $whois_hack or new Net::Whois::Domain $_[0]->domain;
418 die "_whois depriciated";
421 =item submit_internic
423 Submits a registration email for this domain.
427 sub submit_internic {
430 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
431 return unless $cust_pkg;
432 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $cust_pkg->custnum } );
433 return unless $cust_main;
436 'action' => $self->action,
437 'purpose' => $self->purpose,
438 'domain' => $self->domain,
439 'company' => $cust_main->company
440 || $cust_main->getfield('first'). ' '.
441 $cust_main->getfield('last')
443 'city' => $cust_main->city,
444 'state' => $cust_main->state,
445 'zip' => $cust_main->zip,
446 'country' => $cust_main->country,
447 'last' => $cust_main->getfield('last'),
448 'first' => $cust_main->getfield('first'),
449 'daytime' => $cust_main->daytime,
450 'fax' => $cust_main->fax,
451 'email' => $self->email,
452 'tech_contact' => $tech_contact,
453 'primary' => shift @nameservers,
454 'primary_ip' => shift @nameserver_ips,
458 my @xtemplate = @template;
461 OLOOP: while ( defined( $line = shift @xtemplate ) ) {
463 if ( $line =~ /^###LOOP###$/ ) {
465 LOADBUF: while ( defined( $line = shift @xtemplate ) ) {
466 last LOADBUF if ( $line =~ /^###ENDLOOP###$/ );
470 'address' => $cust_main->address2
471 ? [ $cust_main->address1, $cust_main->address2 ]
472 : [ $cust_main->address1 ]
474 'secondary' => [ @nameservers ],
475 'secondary_ip' => [ @nameserver_ips ],
478 my @xbuffer = @buffer;
479 SUBLOOP: while ( defined( $line = shift @xbuffer ) ) {
480 if ( $line =~ /###(\w+)###/ ) {
481 #last LOOP unless my($lub)=shift@{$lubs{$1}};
482 next OLOOP unless my $lub = shift @{$lubs{$1}};
483 $line =~ s/###(\w+)###/$lub/e;
493 if ( $line =~ /###(\w+)###/ ) {
494 #$line =~ s/###(\w+)###/$subs{$1}/eg;
495 $line =~ s/###(\w+)###/$subs{$1}/e;
504 if ( $self->action eq "M" ) {
505 $subject = "MODIFY DOMAIN ". $self->domain;
506 } elsif ( $self->action eq "N" ) {
507 $subject = "NEW DOMAIN ". $self->domain;
509 croak "submit_internic called with action ". $self->action;
512 $ENV{SMTPHOSTS} = $smtpmachine;
513 $ENV{MAILADDRESS} = $from;
514 my $header = Mail::Header->new( [
519 "Date: ". time2str("%a, %d %b %Y %X %z", time),
523 my($msg)=Mail::Internet->new(
528 $msg->smtpsend or die "Can't send registration email"; #die? warn?
536 $Id: svc_domain.pm,v 1.20 2001-09-06 20:41:59 ivan Exp $
540 All BIND/DNS fields should be included (and exported).
542 Delete doesn't send a registration template.
544 All registries should be supported.
546 Should change action to a real field.
548 The $recref stuff in sub check should be cleaned up.
552 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
553 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
554 L<dot-qmail>, schema.html from the base documentation, config.html from the