1 package FS::svc_domain;
4 use vars qw( @ISA $whois_hack $conf $smtpmachine
5 @defaultrecords $soadefaultttl $soaemail $soaexpire $soamachine
6 $soarefresh $soaretry $qshellmachine $nossh_hack
9 use Mail::Internet 1.44;
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 @defaultrecords = $conf->config('defaultrecords');
33 $soadefaultttl = $conf->config('soadefaultttl');
34 $soaemail = $conf->config('soaemail');
35 $soaexpire = $conf->config('soaexpire');
36 $soamachine = $conf->config('soamachine');
37 $soarefresh = $conf->config('soarefresh');
38 $soaretry = $conf->config('soaretry');
40 $qshellmachine = $conf->exists('qmailmachines')
41 ? $conf->config('shellmachine')
47 FS::svc_domain - Object methods for svc_domain records
53 $record = new FS::svc_domain \%hash;
54 $record = new FS::svc_domain { 'column' => 'value' };
56 $error = $record->insert;
58 $error = $new_record->replace($old_record);
60 $error = $record->delete;
62 $error = $record->check;
64 $error = $record->suspend;
66 $error = $record->unsuspend;
68 $error = $record->cancel;
72 An FS::svc_domain object represents a domain. FS::svc_domain inherits from
73 FS::svc_Common. The following fields are currently supported:
77 =item svcnum - primary key (assigned automatically for new accounts)
81 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
91 Creates a new domain. To add the domain to the database, see L<"insert">.
95 sub table { 'svc_domain'; }
99 Adds this domain to the database. If there is an error, returns the error,
100 otherwise returns false.
102 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be
103 defined. An FS::cust_svc record will be created and inserted.
105 The additional field I<action> should be set to I<N> for new domains or I<M>
108 A registration or transfer email will be submitted unless
109 $FS::svc_domain::whois_hack is true.
111 The additional field I<email> can be used to manually set the admin contact
112 email address on this email. Otherwise, the svc_acct records for this package
113 (see L<FS::cust_pkg>) are searched. If there is exactly one svc_acct record
114 in the same package, it is automatically used. Otherwise an error is returned.
116 If any I<soamachine> configuration file exists, an SOA record is added to
117 the domain_record table (see <FS::domain_record>).
119 If any records are defined in the I<defaultrecords> configuration file,
120 appropriate records are added to the domain_record table (see
121 L<FS::domain_record>).
123 If a machine is defined in the I<shellmachine> configuration value, the
124 I<qmailmachines> configuration file exists, and the I<catchall> field points
125 to an an account with a home directory (see L<FS::svc_acct>), the command:
127 [ -e $dir/.qmail-$qdomain-defualt ] || {
128 touch $dir/.qmail-$qdomain-default;
129 chown $uid:$gid $dir/.qmail-$qdomain-default;
132 is executed on shellmachine via ssh (see L<dot-qmail/"EXTENSION ADDRESSES">).
133 This behaviour can be supressed by setting $FS::svc_domain::nossh_hack true.
144 local $SIG{HUP} = 'IGNORE';
145 local $SIG{INT} = 'IGNORE';
146 local $SIG{QUIT} = 'IGNORE';
147 local $SIG{TERM} = 'IGNORE';
148 local $SIG{TSTP} = 'IGNORE';
149 local $SIG{PIPE} = 'IGNORE';
151 my $oldAutoCommit = $FS::UID::AutoCommit;
152 local $FS::UID::AutoCommit = 0;
155 $error = $self->check;
156 return $error if $error;
158 return "Domain in use (here)"
159 if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
161 my $whois = $self->whois;
162 if ( $self->action eq "N" && ! $whois_hack && $whois ) {
163 $dbh->rollback if $oldAutoCommit;
164 return "Domain in use (see whois)";
166 if ( $self->action eq "M" && ! $whois ) {
167 $dbh->rollback if $oldAutoCommit;
168 return "Domain not found (see whois)";
171 $error = $self->SUPER::insert;
173 $dbh->rollback if $oldAutoCommit;
177 $self->submit_internic unless $whois_hack;
180 my $soa = new FS::domain_record {
181 'svcnum' => $self->svcnum,
185 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
186 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
188 $error = $soa->insert;
190 $dbh->rollback if $oldAutoCommit;
191 return "couldn't insert SOA record for new domain: $error";
194 foreach my $record ( @defaultrecords ) {
195 my($zone,$af,$type,$data) = split(/\s+/,$record,4);
196 my $domain_record = new FS::domain_record {
197 'svcnum' => $self->svcnum,
203 my $error = $domain_record->insert;
205 $dbh->rollback if $oldAutoCommit;
206 return "couldn't insert record for new domain: $error";
212 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
214 if ( $qshellmachine && $self->catchall && ! $nossh_hack ) {
216 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } )
217 or warn "WARNING: inserted unknown catchall: ". $self->catchall;
218 if ( $svc_acct && $svc_acct->dir ) {
219 my $qdomain = $self->domain;
220 $qdomain =~ s/\./:/g; #see manpage for 'dot-qmail': EXTENSION ADDRESSES
221 my ( $uid, $gid, $dir ) = (
227 my $queue = new FS::queue {
228 'svcnum' => $self->svcnum,
229 'job' => 'Net::SSH::ssh_cmd',
231 $error = $queue->insert("root\@$qshellmachine", "[ -e $dir/.qmail-$qdomain-default ] || { touch $dir/.qmail-$qdomain-default; chown $uid:$gid $dir/.qmail-$qdomain-default; }" );
241 Deletes this domain from the database. If there is an error, returns the
242 error, otherwise returns false.
244 The corresponding FS::cust_svc record will be deleted as well.
251 return "Can't delete a domain which has accounts!"
252 if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
254 return "Can't delete a domain with (svc_acct_sm) mail aliases!"
255 if defined( $FS::Record::dbdef->table('svc_acct_sm') )
256 && qsearch('svc_acct_sm', { 'domsvc' => $self->svcnum } );
258 #return "Can't delete a domain with (domain_record) zone entries!"
259 # if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
261 local $SIG{HUP} = 'IGNORE';
262 local $SIG{INT} = 'IGNORE';
263 local $SIG{QUIT} = 'IGNORE';
264 local $SIG{TERM} = 'IGNORE';
265 local $SIG{TSTP} = 'IGNORE';
266 local $SIG{PIPE} = 'IGNORE';
268 my $oldAutoCommit = $FS::UID::AutoCommit;
269 local $FS::UID::AutoCommit = 0;
272 foreach my $domain_record ( reverse $self->domain_record ) {
273 my $error = $domain_record->delete;
275 $dbh->rollback if $oldAutoCommit;
280 my $error = $self->SUPER::delete;
282 $dbh->rollback if $oldAutoCommit;
286 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
289 =item replace OLD_RECORD
291 Replaces OLD_RECORD with this one in the database. If there is an error,
292 returns the error, otherwise returns false.
297 my ( $new, $old ) = ( shift, shift );
299 return "Can't change domain - reorder."
300 if $old->getfield('domain') ne $new->getfield('domain');
302 my $error = $new->SUPER::replace($old);
303 return $error if $error;
308 Just returns false (no error) for now.
310 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
314 Just returns false (no error) for now.
316 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
320 Just returns false (no error) for now.
322 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
326 Checks all fields to make sure this is a valid domain. If there is an error,
327 returns the error, otherwise returns false. Called by the insert and replace
330 Sets any fixed values; see L<FS::part_svc>.
337 my $x = $self->setfixed;
338 return $x unless ref($x);
341 my $error = $self->ut_numbern('svcnum')
342 || $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');
411 sort { $order{$a->rectype} <=> $order{$b->rectype} }
412 qsearch('domain_record', { svcnum => $self->svcnum } );
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 All BIND/DNS fields should be included (and exported).
456 Delete doesn't send a registration template.
458 All registries should be supported.
460 Should change action to a real field.
462 The $recref stuff in sub check should be cleaned up.
466 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
467 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
468 L<dot-qmail>, schema.html from the base documentation, config.html from the