1 package FS::svc_domain;
4 use vars qw( @ISA $whois_hack $conf $smtpmachine
5 @mxmachines @nsmachines $soadefaultttl $soaemail $soaexpire $soamachine
6 $soarefresh $soaretry $qshellmachine $nossh_hack %arecords %cnamerecords
14 use FS::Record qw(fields qsearch qsearchs dbh);
21 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 %arecords = map { split /\t/ } $conf->config('arecords');
33 %cnamerecords = map { split /\t/ } $conf->config('cnamerecords');
34 @mxmachines = $conf->config('mxmachines');
35 @nsmachines = $conf->config('nsmachines');
36 $soadefaultttl = $conf->config('soadefaultttl');
37 $soaemail = $conf->config('soaemail');
38 $soaexpire = $conf->config('soaexpire');
39 $soamachine = $conf->config('soamachine');
40 $soarefresh = $conf->config('soarefresh');
41 $soaretry = $conf->config('soaretry');
43 $qshellmachine = $conf->exists('qmailmachines')
44 ? $conf->config('shellmachine')
50 FS::svc_domain - Object methods for svc_domain records
56 $record = new FS::svc_domain \%hash;
57 $record = new FS::svc_domain { 'column' => 'value' };
59 $error = $record->insert;
61 $error = $new_record->replace($old_record);
63 $error = $record->delete;
65 $error = $record->check;
67 $error = $record->suspend;
69 $error = $record->unsuspend;
71 $error = $record->cancel;
75 An FS::svc_domain object represents a domain. FS::svc_domain inherits from
76 FS::svc_Common. The following fields are currently supported:
80 =item svcnum - primary key (assigned automatically for new accounts)
84 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
94 Creates a new domain. To add the domain to the database, see L<"insert">.
98 sub table { 'svc_domain'; }
102 Adds this domain to the database. If there is an error, returns the error,
103 otherwise returns false.
105 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be
106 defined. An FS::cust_svc record will be created and inserted.
108 The additional field I<action> should be set to I<N> for new domains or I<M>
111 A registration or transfer email will be submitted unless
112 $FS::svc_domain::whois_hack is true.
114 The additional field I<email> can be used to manually set the admin contact
115 email address on this email. Otherwise, the svc_acct records for this package
116 (see L<FS::cust_pkg>) are searched. If there is exactly one svc_acct record
117 in the same package, it is automatically used. Otherwise an error is returned.
119 If any I<soamachine> configuration file exists, an SOA record is added to
120 the domain_record table (see <FS::domain_record>).
122 If any machines are defined in the I<nsmachines> configuration file, NS
123 records are added to the domain_record table (see L<FS::domain_record>).
125 If any machines are defined in the I<mxmachines> configuration file, MX
126 records are added to the domain_record table (see L<FS::domain_record>).
128 If the I<arecords> configuration file exits, A records are added to the
129 domain_record table. The I<cnamerecords> file does the same thing for
132 If a machine is defined in the I<shellmachine> configuration value, the
133 I<qmailmachines> configuration file exists, and the I<catchall> field points
134 to an an account with a home directory (see L<FS::svc_acct>), the command:
136 [ -e $dir/.qmail-$qdomain-defualt ] || {
137 touch $dir/.qmail-$qdomain-default;
138 chown $uid:$gid $dir/.qmail-$qdomain-default;
141 is executed on shellmachine via ssh (see L<dot-qmail/"EXTENSION ADDRESSES">).
142 This behaviour can be supressed by setting $FS::svc_domain::nossh_hack true.
153 local $SIG{HUP} = 'IGNORE';
154 local $SIG{INT} = 'IGNORE';
155 local $SIG{QUIT} = 'IGNORE';
156 local $SIG{TERM} = 'IGNORE';
157 local $SIG{TSTP} = 'IGNORE';
158 local $SIG{PIPE} = 'IGNORE';
160 my $oldAutoCommit = $FS::UID::AutoCommit;
161 local $FS::UID::AutoCommit = 0;
164 $error = $self->check;
165 return $error if $error;
167 return "Domain in use (here)"
168 if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
170 my $whois = $self->whois;
171 if ( $self->action eq "N" && ! $whois_hack && $whois ) {
172 $dbh->rollback if $oldAutoCommit;
173 return "Domain in use (see whois)";
175 if ( $self->action eq "M" && ! $whois ) {
176 $dbh->rollback if $oldAutoCommit;
177 return "Domain not found (see whois)";
180 $error = $self->SUPER::insert;
182 $dbh->rollback if $oldAutoCommit;
186 $self->submit_internic unless $whois_hack;
189 my $soa = new FS::domain_record {
190 'svcnum' => $self->svcnum,
194 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
195 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
197 $error = $soa->insert;
199 $dbh->rollback if $oldAutoCommit;
200 return "couldn't insert SOA record for new domain: $error";
203 foreach my $nsmachine ( @nsmachines ) {
204 my $ns = new FS::domain_record {
205 'svcnum' => $self->svcnum,
209 'recdata' => $nsmachine,
211 my $error = $ns->insert;
213 $dbh->rollback if $oldAutoCommit;
214 return "couldn't insert NS record for new domain: $error";
218 foreach my $mxmachine ( @mxmachines ) {
219 my $mx = new FS::domain_record {
220 'svcnum' => $self->svcnum,
224 'recdata' => $mxmachine,
226 my $error = $mx->insert;
228 $dbh->rollback if $oldAutoCommit;
229 return "couldn't insert MX record for new domain: $error";
233 foreach my $arecord ( keys %arecords ) {
234 my $arec = new FS::domain_record {
235 'svcnum' => $self->svcnum,
236 'reczone' => $arecord,
239 'recdata' => $arecords{$arecord},
241 my $error = $arec->insert;
243 $dbh->rollback if $oldAutoCommit;
244 return "WARNING: couldn't insert A record for new domain: $error";
248 foreach my $cnamerecord ( keys %cnamerecords ) {
249 my $cnamerec = new FS::domain_record {
250 'svcnum' => $self->svcnum,
251 'reczone' => $cnamerecord,
253 'rectype' => 'CNAME',
254 'recdata' => $cnamerecords{$cnamerecord},
256 my $error = $cnamerec->insert;
258 $dbh->rollback if $oldAutoCommit;
259 return "WARNING: couldn't insert CNAME record for new domain: $error";
265 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
267 if ( $qshellmachine && $self->catchall && ! $nossh_hack ) {
269 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } )
270 or warn "WARNING: inserted unknown catchall: ". $self->catchall;
271 if ( $svc_acct && $svc_acct->dir ) {
272 my $qdomain = $self->domain;
273 $qdomain =~ s/\./:/g; #see manpage for 'dot-qmail': EXTENSION ADDRESSES
274 my ( $uid, $gid, $dir ) = (
280 my $queue = new FS::queue {
281 'svcnum' => $self->svcnum,
282 'job' => 'Net::SSH::ssh_cmd',
284 $error = $queue->insert("root\@$qshellmachine", "[ -e $dir/.qmail-$qdomain-default ] || { touch $dir/.qmail-$qdomain-default; chown $uid:$gid $dir/.qmail-$qdomain-default; }" );
294 Deletes this domain from the database. If there is an error, returns the
295 error, otherwise returns false.
297 The corresponding FS::cust_svc record will be deleted as well.
304 return "Can't delete a domain which has accounts!"
305 if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
307 return "Can't delete a domain with (svc_acct_sm) mail aliases!"
308 if defined( $FS::Record::dbdef->table('svc_acct_sm') )
309 && qsearch('svc_acct_sm', { 'domsvc' => $self->svcnum } );
311 return "Can't delete a domain with (domain_record) zone entries!"
312 if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
314 $self->SUPER::delete;
317 =item replace OLD_RECORD
319 Replaces OLD_RECORD with this one in the database. If there is an error,
320 returns the error, otherwise returns false.
325 my ( $new, $old ) = ( shift, shift );
328 return "Can't change domain - reorder."
329 if $old->getfield('domain') ne $new->getfield('domain');
331 $new->SUPER::replace($old);
337 Just returns false (no error) for now.
339 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
343 Just returns false (no error) for now.
345 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
349 Just returns false (no error) for now.
351 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
355 Checks all fields to make sure this is a valid domain. If there is an error,
356 returns the error, otherwise returns false. Called by the insert and replace
359 Sets any fixed values; see L<FS::part_svc>.
366 my $x = $self->setfixed;
367 return $x unless ref($x);
370 my $error = $self->ut_numbern('svcnum')
371 || $self->ut_numbern('catchall')
373 return $error if $error;
377 if ( $self->svcnum ) {
378 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
379 $pkgnum = $cust_svc->pkgnum;
381 $pkgnum = $self->pkgnum;
384 my($recref) = $self->hashref;
386 unless ( $whois_hack ) {
387 unless ( $self->email ) { #find out an email address
389 foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
390 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
391 push @svc_acct, $svc_acct if $svc_acct;
394 if ( scalar(@svc_acct) == 0 ) {
395 return "Must order an account in package ". $pkgnum. " first";
396 } elsif ( scalar(@svc_acct) > 1 ) {
397 return "More than one account in package ". $pkgnum. ": specify admin contact email";
399 $self->email($svc_acct[0]->email );
404 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
405 if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
406 $recref->{domain} = "$1.$2";
408 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
409 $recref->{domain} = $1;
411 return "Illegal domain ". $recref->{domain}.
412 " (or unknown registry - try \$whois_hack)";
415 $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
416 $recref->{action} = $1;
418 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
419 return "Unknown catchall" unless $svc_acct || ! $recref->{catchall};
421 $self->ut_textn('purpose');
427 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
428 undef if the domain is not found in whois.
430 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
435 $whois_hack or new Net::Whois::Domain $_[0]->domain;
445 die "_whois depriciated";
448 =item submit_internic
450 Submits a registration email for this domain.
454 sub submit_internic {
456 carp "submit_internic depreciated";
463 $Id: svc_domain.pm,v 1.25 2002-03-09 10:19:38 khoff Exp $
467 All BIND/DNS fields should be included (and exported).
469 Delete doesn't send a registration template.
471 All registries should be supported.
473 Should change action to a real field.
475 The $recref stuff in sub check should be cleaned up.
479 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
480 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
481 L<dot-qmail>, schema.html from the base documentation, config.html from the