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
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 @mxmachines = $conf->config('mxmachines');
33 @nsmachines = $conf->config('nsmachines');
34 $soadefaultttl = $conf->config('soadefaultttl');
35 $soaemail = $conf->config('soaemail');
36 $soaexpire = $conf->config('soaexpire');
37 $soamachine = $conf->config('soamachine');
38 $soarefresh = $conf->config('soarefresh');
39 $soaretry = $conf->config('soaretry');
41 $qshellmachine = $conf->exists('qmailmachines')
42 ? $conf->config('shellmachine')
48 FS::svc_domain - Object methods for svc_domain records
54 $record = new FS::svc_domain \%hash;
55 $record = new FS::svc_domain { 'column' => 'value' };
57 $error = $record->insert;
59 $error = $new_record->replace($old_record);
61 $error = $record->delete;
63 $error = $record->check;
65 $error = $record->suspend;
67 $error = $record->unsuspend;
69 $error = $record->cancel;
73 An FS::svc_domain object represents a domain. FS::svc_domain inherits from
74 FS::svc_Common. The following fields are currently supported:
78 =item svcnum - primary key (assigned automatically for new accounts)
82 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
92 Creates a new domain. To add the domain to the database, see L<"insert">.
96 sub table { 'svc_domain'; }
100 Adds this domain to the database. If there is an error, returns the error,
101 otherwise returns false.
103 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be
104 defined. An FS::cust_svc record will be created and inserted.
106 The additional field I<action> should be set to I<N> for new domains or I<M>
109 A registration or transfer email will be submitted unless
110 $FS::svc_domain::whois_hack is true.
112 The additional field I<email> can be used to manually set the admin contact
113 email address on this email. Otherwise, the svc_acct records for this package
114 (see L<FS::cust_pkg>) are searched. If there is exactly one svc_acct record
115 in the same package, it is automatically used. Otherwise an error is returned.
117 If any I<soamachine> configuration file exists, an SOA record is added to
118 the domain_record table (see <FS::domain_record>).
120 If any machines are defined in the I<nsmachines> configuration file, NS
121 records are added to the domain_record table (see L<FS::domain_record>).
123 If any machines are defined in the I<mxmachines> configuration file, MX
124 records are added to the domain_record table (see L<FS::domain_record>).
126 If a machine is defined in the I<shellmachine> configuration value, the
127 I<qmailmachines> configuration file exists, and the I<catchall> field points
128 to an an account with a home directory (see L<FS::svc_acct>), the command:
130 [ -e $dir/.qmail-$qdomain-defualt ] || {
131 touch $dir/.qmail-$qdomain-default;
132 chown $uid:$gid $dir/.qmail-$qdomain-default;
135 is executed on shellmachine via ssh (see L<dot-qmail/"EXTENSION ADDRESSES">).
136 This behaviour can be supressed by setting $FS::svc_domain::nossh_hack true.
147 local $SIG{HUP} = 'IGNORE';
148 local $SIG{INT} = 'IGNORE';
149 local $SIG{QUIT} = 'IGNORE';
150 local $SIG{TERM} = 'IGNORE';
151 local $SIG{TSTP} = 'IGNORE';
152 local $SIG{PIPE} = 'IGNORE';
154 my $oldAutoCommit = $FS::UID::AutoCommit;
155 local $FS::UID::AutoCommit = 0;
158 $error = $self->check;
159 return $error if $error;
161 return "Domain in use (here)"
162 if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
164 my $whois = $self->whois;
165 if ( $self->action eq "N" && ! $whois_hack && $whois ) {
166 $dbh->rollback if $oldAutoCommit;
167 return "Domain in use (see whois)";
169 if ( $self->action eq "M" && ! $whois ) {
170 $dbh->rollback if $oldAutoCommit;
171 return "Domain not found (see whois)";
174 $error = $self->SUPER::insert;
176 $dbh->rollback if $oldAutoCommit;
180 $self->submit_internic unless $whois_hack;
183 my $soa = new FS::domain_record {
184 'svcnum' => $self->svcnum,
188 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
189 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
191 $error = $soa->insert;
193 $dbh->rollback if $oldAutoCommit;
194 return "couldn't insert SOA record for new domain: $error";
197 foreach my $nsmachine ( @nsmachines ) {
198 my $ns = new FS::domain_record {
199 'svcnum' => $self->svcnum,
203 'recdata' => $nsmachine,
205 my $error = $ns->insert;
207 $dbh->rollback if $oldAutoCommit;
208 return "couldn't insert NS record for new domain: $error";
212 foreach my $mxmachine ( @mxmachines ) {
213 my $mx = new FS::domain_record {
214 'svcnum' => $self->svcnum,
218 'recdata' => $mxmachine,
220 my $error = $mx->insert;
222 $dbh->rollback if $oldAutoCommit;
223 return "couldn't insert MX record for new domain: $error";
229 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
231 if ( $qshellmachine && $self->catchall && ! $nossh_hack ) {
233 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } )
234 or warn "WARNING: inserted unknown catchall: ". $self->catchall;
235 if ( $svc_acct && $svc_acct->dir ) {
236 my $qdomain = $self->domain;
237 $qdomain =~ s/\./:/g; #see manpage for 'dot-qmail': EXTENSION ADDRESSES
238 my ( $uid, $gid, $dir ) = (
244 my $queue = new FS::queue { 'job' => 'FS::svc_domain::ssh' };
245 $error = $queue->insert("root\@$qshellmachine", "[ -e $dir/.qmail-$qdomain-default ] || { touch $dir/.qmail-$qdomain-default; chown $uid:$gid $dir/.qmail-$qdomain-default; }" );
257 #false laziness with FS::svc_acct::ssh
259 my ( $host, @cmd_and_args ) = @_;
262 my $reader = IO::File->new();
263 my $writer = IO::File->new();
264 my $error = IO::File->new();
266 &Net::SSH::sshopen3( $host, $reader, $writer, $error, @cmd_and_args) or die $!;
269 my $output_stream = <$writer>;
270 my $error_stream = <$error>;
271 if ( length $error_stream ) {
272 #warn "[FS::svc_acct::ssh] STDERR $error_stream";
273 die "[FS::svc_domain::ssh] STDERR $error_stream";
275 if ( length $output_stream ) {
276 warn "[FS::svc_domain::ssh] STDOUT $output_stream";
279 # &Net::SSH::ssh(@args,">>/usr/local/etc/freeside/sshoutput 2>&1");
285 Deletes this domain from the database. If there is an error, returns the
286 error, otherwise returns false.
288 The corresponding FS::cust_svc record will be deleted as well.
295 return "Can't delete a domain which has accounts!"
296 if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
298 return "Can't delete a domain with (svc_acct_sm) mail aliases!"
299 if defined( $FS::Record::dbdef->table('svc_acct_sm') )
300 && qsearch('svc_acct_sm', { 'domsvc' => $self->svcnum } );
302 return "Can't delete a domain with (domain_record) zone entries!"
303 if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
305 $self->SUPER::delete;
308 =item replace OLD_RECORD
310 Replaces OLD_RECORD with this one in the database. If there is an error,
311 returns the error, otherwise returns false.
316 my ( $new, $old ) = ( shift, shift );
319 return "Can't change domain - reorder."
320 if $old->getfield('domain') ne $new->getfield('domain');
322 $new->SUPER::replace($old);
328 Just returns false (no error) for now.
330 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
334 Just returns false (no error) for now.
336 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
340 Just returns false (no error) for now.
342 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
346 Checks all fields to make sure this is a valid domain. If there is an error,
347 returns the error, otherwise returns false. Called by the insert and replace
350 Sets any fixed values; see L<FS::part_svc>.
357 my $x = $self->setfixed;
358 return $x unless ref($x);
361 my $error = $self->ut_numbern('svcnum')
362 || $self->ut_numbern('catchall')
364 return $error if $error;
368 if ( $self->svcnum ) {
369 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
370 $pkgnum = $cust_svc->pkgnum;
372 $pkgnum = $self->pkgnum;
375 my($recref) = $self->hashref;
377 unless ( $whois_hack ) {
378 unless ( $self->email ) { #find out an email address
380 foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
381 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
382 push @svc_acct, $svc_acct if $svc_acct;
385 if ( scalar(@svc_acct) == 0 ) {
386 return "Must order an account in package ". $pkgnum. " first";
387 } elsif ( scalar(@svc_acct) > 1 ) {
388 return "More than one account in package ". $pkgnum. ": specify admin contact email";
390 $self->email($svc_acct[0]->email );
395 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
396 if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
397 $recref->{domain} = "$1.$2";
399 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
400 $recref->{domain} = $1;
402 return "Illegal domain ". $recref->{domain}.
403 " (or unknown registry - try \$whois_hack)";
406 $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
407 $recref->{action} = $1;
409 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
410 return "Unknown catchall" unless $svc_acct || ! $recref->{catchall};
412 $self->ut_textn('purpose');
418 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
419 undef if the domain is not found in whois.
421 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
426 $whois_hack or new Net::Whois::Domain $_[0]->domain;
436 die "_whois depriciated";
439 =item submit_internic
441 Submits a registration email for this domain.
445 sub submit_internic {
447 carp "submit_internic depreciated";
454 $Id: svc_domain.pm,v 1.23 2002-02-09 18:09:30 ivan Exp $
458 All BIND/DNS fields should be included (and exported).
460 Delete doesn't send a registration template.
462 All registries should be supported.
464 Should change action to a real field.
466 The $recref stuff in sub check should be cleaned up.
470 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
471 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
472 L<dot-qmail>, schema.html from the base documentation, config.html from the