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; };
78 print NAMED_CONF <<END;
81 file "$zonepath/db.$domain";
86 open (DB_MASTER,">$prefix/db.$domain")
87 or die "can't open $prefix/db.$domain: $!";
89 if ($bind_rel eq 'BIND9') {
90 print DB_MASTER "\$TTL $minttl\n\$ORIGIN $domain.\n";
94 qsearch('domain_record', { 'svcnum' => $svc_domain->svcnum } );
95 foreach my $domain_record (
96 sort { $b->rectype cmp $a->rectype } @domain_records
98 #if ( $domain_record->rectype eq 'SOA' ) {
99 # print DB_MASTER join("\t", $domain_record-> reczone
101 print DB_MASTER join("\t",
102 map { $domain_record->getfield($_) }
103 qw( reczone recaf rectype recdata )
117 dest => "root\@$machine:$zonepath/",
118 exclude => [qw( *.import named.conf.HEADER named.conf )],
119 } ) or die "rsync to $machine failed: ". join(" / ", $rsync->err);
123 src => "$prefix/named.conf",
124 dest => "root\@$machine:". $export->option('named_conf'),
125 } ) or die "rsync to $machine failed: ". join(" / ", $rsync->err);
128 ssh("root\@$machine", "$ndc_cmd reload");
134 foreach my $sexport ( @sexports ) { #false laziness with above
136 my $machine = $sexport->machine;
137 my $prefix = "$spooldir/$machine";
139 my $bind_rel = $sexport->option('bind_release');
140 my $ndc_cmd = ($bind_rel eq 'BIND9') ? 'rndc' : 'ndc';
142 #prevent old domain files from piling up
143 #rmtree "$prefix" or die "can't rmtree $prefix.db: $!";
145 mkdir $prefix, 0700 unless -d $prefix;
147 open(NAMED_CONF,">$prefix/named.conf")
148 or die "can't open $prefix/named.conf: $!";
150 if ( -e "$prefix/named.conf.HEADER" ) {
151 open(CONF_HEADER,"<$prefix/named.conf.HEADER")
152 or die "can't open $prefix/named.conf.HEADER: $!";
153 while (<CONF_HEADER>) { print NAMED_CONF $_; }
157 my $masters = $sexport->option('master');
159 #false laziness with freeside-sqlradius-reset
161 map { qsearchs('svc_domain', { 'svcnum' => $_->svcnum } ) }
162 map { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
163 grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
164 $sexport->export_svc;
166 foreach my $svc_domain ( @svc_domain ) {
167 my $domain = $svc_domain->domain;
168 print NAMED_CONF <<END;
172 masters { $masters; };
180 src => "$prefix/named.conf",
181 dest => "root\@$machine:". $sexport->option('named_conf'),
182 } ) or die "rsync to $machine failed: ". join(" / ", $rsync->err);
185 ssh("root\@$machine", "$ndc_cmd reload");
193 die "Usage:\n bind.export user\n";