7 use FS::UID qw(adminsuidsetup datasrc);
8 use FS::Record qw(qsearch qsearchs);
14 my $user = shift or die &usage;
17 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/bind";
18 mkdir $spooldir, 0700 unless -d $spooldir;
20 my @exports = qsearch('part_export', { 'exporttype' => 'bind' } );
21 my @sexports = qsearch('part_export', { 'exporttype' => 'bind_slave' } );
23 my $rsync = File::Rsync->new({
28 foreach my $export ( @exports ) {
30 my $machine = $export->machine;
31 my $prefix = "$spooldir/$machine";
33 my $bind_rel = $export->option('bind_release');
34 my $ndc_cmd = $export->option('reload')
35 || ( ($bind_rel eq 'BIND9') ? 'rndc' : 'ndc' );
36 my $minttl = $export->option('bind9_minttl');
38 #prevent old domain files from piling up
39 #rmtree "$prefix" or die "can't rmtree $prefix.db: $!";
41 mkdir $prefix, 0700 unless -d $prefix;
43 open(NAMED_CONF,">$prefix/named.conf")
44 or die "can't open $prefix/named.conf: $!";
46 if ( -e "$prefix/named.conf.HEADER" ) {
47 open(CONF_HEADER,"<$prefix/named.conf.HEADER")
48 or die "can't open $prefix/named.conf.HEADER: $!";
49 while (<CONF_HEADER>) { print NAMED_CONF $_; }
53 my $zonepath = $export->option('zonepath');
56 my @svc_domain = $export->svc_x;
58 foreach my $svc_domain ( @svc_domain ) {
59 my $domain = $svc_domain->domain;
60 my @masters = qsearch('domain_record', {
61 'svcnum' => $svc_domain->svcnum,
65 my $masters = join('; ', map { $_->recdata } @masters );
67 print NAMED_CONF <<END;
71 masters { $masters; };
75 unlink "$prefix/db.$domain" if -e "$prefix/db.$domain";
79 print NAMED_CONF <<END;
82 file "$zonepath/db.$domain";
87 open (DB_MASTER,">$prefix/db.$domain")
88 or die "can't open $prefix/db.$domain: $!";
90 if ($bind_rel eq 'BIND9') {
91 print DB_MASTER "\$TTL $minttl\n\$ORIGIN $domain.\n";
95 qsearch('domain_record', { 'svcnum' => $svc_domain->svcnum } );
96 foreach my $domain_record (
97 sort { $b->rectype cmp $a->rectype } @domain_records
99 #if ( $domain_record->rectype eq 'SOA' ) {
100 # print DB_MASTER join("\t", $domain_record-> reczone
102 print DB_MASTER join("\t",
103 map { $domain_record->getfield($_) }
104 qw( reczone recaf rectype recdata )
118 dest => "root\@$machine:$zonepath/",
119 exclude => [qw( *.import named.conf.HEADER named.conf )],
120 } ) or die "rsync to $machine failed: ". join(" / ", $rsync->err);
124 src => "$prefix/named.conf",
125 dest => "root\@$machine:". $export->option('named_conf'),
126 } ) or die "rsync to $machine failed: ". join(" / ", $rsync->err);
129 ssh("root\@$machine", "$ndc_cmd reload");
135 foreach my $sexport ( @sexports ) { #false laziness with above
137 my $machine = $sexport->machine;
138 my $prefix = "$spooldir/$machine";
140 my $bind_rel = $sexport->option('bind_release');
141 my $ndc_cmd = ($bind_rel eq 'BIND9') ? 'rndc' : 'ndc';
143 #prevent old domain files from piling up
144 #rmtree "$prefix" or die "can't rmtree $prefix.db: $!";
146 mkdir $prefix, 0700 unless -d $prefix;
148 open(NAMED_CONF,">$prefix/named.conf")
149 or die "can't open $prefix/named.conf: $!";
151 if ( -e "$prefix/named.conf.HEADER" ) {
152 open(CONF_HEADER,"<$prefix/named.conf.HEADER")
153 or die "can't open $prefix/named.conf.HEADER: $!";
154 while (<CONF_HEADER>) { print NAMED_CONF $_; }
158 my $masters = $sexport->option('master');
160 #false laziness with freeside-sqlradius-reset
162 map { qsearchs('svc_domain', { 'svcnum' => $_->svcnum } ) }
163 map { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
164 grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
165 $sexport->export_svc;
167 foreach my $svc_domain ( @svc_domain ) {
168 my $domain = $svc_domain->domain;
169 print NAMED_CONF <<END;
173 masters { $masters; };
181 src => "$prefix/named.conf",
182 dest => "root\@$machine:". $sexport->option('named_conf'),
183 } ) or die "rsync to $machine failed: ". join(" / ", $rsync->err);
186 ssh("root\@$machine", "$ndc_cmd reload");
194 die "Usage:\n bind.export user\n";