4 # -p: part number for domains
6 # -n: named.conf file (or an include file with zones you want to import),
7 # for example root@ns.isp.com:/var/named/named.conf
10 # -d: dry-run, debug: don't insert any records, just dump debugging output
11 # -s: import slave zones as master. useful if you need to recreate your
12 # primary nameserver from a secondary
13 # -c dir: override patch for downloading zone files (for example, when
14 # downloading zone files from chrooted bind)
16 # need to manually put header in
17 # /usr/local/etc/freeside/export.<datasrc./bind/<machine>/named.conf.HEADER
18 # (or, nowadays, better just to include the file freeside exports)
22 use vars qw($domain_svcpart);
26 #use BIND::Conf_Parser;
27 #use DNS::ZoneParse 0.81;
29 use Net::SCP qw(scp iscp);
31 use FS::UID qw(adminsuidsetup datasrc);
32 use FS::Record qw(qsearch); #qsearchs);
35 use FS::domain_record;
39 use vars qw($opt_p $opt_n $opt_s $opt_c $opt_d);
42 my $user = shift or die &usage;
45 $FS::svc_Common::noexport_hack = 1;
46 $FS::domain_record::noserial_hack = 1;
48 use vars qw($spooldir);
49 $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/bind";
50 mkdir $spooldir unless -d $spooldir;
52 $domain_svcpart = $opt_p;
54 my $named_conf = $opt_n;
56 use vars qw($named_machine $prefix);
57 $named_machine = (split(/:/, $named_conf))[0];
58 my $pnamed_machine = $named_machine;
59 $pnamed_machine =~ s/^[\w\-]+\@//;
60 $prefix = "$spooldir/$pnamed_machine";
61 mkdir $prefix unless -d $prefix;
63 #iscp("$named_conf","$prefix/named.conf.import");
64 scp("$named_conf","$prefix/named.conf.import");
68 $FS::svc_domain::whois_hack=1;
71 $p->parse_file("$prefix/named.conf.import");
73 print "\nBIND import completed.\n";
78 die "Usage:\n\n bind.import -p partnum -n \"user\@machine:/path/to/named.conf\" [ -s ] [ -c chroot_dir ] [ -f ] user\n";
85 use BIND::Conf_Parser;
86 use vars qw(@ISA $named_dir);
87 @ISA = qw(BIND::Conf_Parser);
89 $named_dir = 'COULD_NOT_FIND_NAMED_DIRECTORY_TRY_SETTING_-C_OPTION';
91 my($self, $option, $argument) = @_;
92 return unless $option eq "directory";
93 $named_dir = $argument;
94 #warn "found named dir: $named_dir\n";
98 my($self, $name, $class, $type, $options) = @_;
99 return unless $class eq 'in';
100 return if grep { $name eq $_ } (qw(
101 . localhost 127.in-addr.arpa 0.in-addr.arpa 255.in-addr.arpa
102 0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa
103 0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.int
106 use FS::Record qw(qsearchs);
110 qsearchs('svc_domain', { 'domain' => $name } )
111 || new FS::svc_domain( {
112 svcpart => $main::domain_svcpart,
116 unless ( $domain->svcnum ) {
117 my $error = $domain->insert;
118 die $error if $error;
121 if ( $type eq 'slave' && !$main::opt_s ) {
123 if ( $main::opt_d ) {
126 print "$name: ". Dumper($options);
130 foreach my $master ( @{ $options->{masters} } ) {
131 my $domain_record = new FS::domain_record( {
132 'svcnum' => $domain->svcnum,
135 'rectype' => '_mstr',
136 'recdata' => $master,
138 my $error = $domain_record->insert;
139 die $error if $error;
144 } elsif ( $type eq 'master' || ( $type eq 'slave' && $main::opt_s ) ) {
146 my $file = $options->{file};
149 my $basefile = basename($file);
150 my $sourcefile = $file;
151 if ( $main::opt_c ) {
152 $sourcefile = "$main::opt_c/$sourcefile" if $main::opt_c;
154 $sourcefile = "$named_dir/$sourcefile" unless $file =~ /^\//;
157 use Net::SCP qw(iscp scp);
158 #iscp("$main::named_machine:$sourcefile",
159 # "$main::prefix/$basefile.import");
160 scp("$main::named_machine:$sourcefile",
161 "$main::prefix/$basefile.import");
163 use DNS::ZoneParse 0.84;
164 my $zone = DNS::ZoneParse->new("$main::prefix/$basefile.import");
166 my $dump = $zone->dump;
168 if ( $main::opt_d ) {
171 print "$name: ". Dumper($dump);
175 foreach my $rectype ( keys %$dump ) {
176 if ( $rectype =~ /^SOA$/i ) {
177 my $rec = $dump->{$rectype};
178 $rec->{email} =~ s/\@/\./;
179 my $domain_record = new FS::domain_record( {
180 'svcnum' => $domain->svcnum,
181 'reczone' => $rec->{origin},
183 'rectype' => $rectype,
185 $rec->{primary}. ' '. $rec->{email}. ' ( '.
186 join(' ', map $rec->{$_},
187 qw( serial refresh retry expire minimumTTL ) ).
190 my $error = $domain_record->insert;
191 die $error if $error;
193 #die $dump->{$rectype};
196 if ( $rectype =~ /^MX$/i ) {
197 $datasub = sub { $_[0]->{priority}. ' '. $_[0]->{host}; };
198 } elsif ( $rectype =~ /^TXT$/i ) {
199 $datasub = sub { $_[0]->{text}; };
201 $datasub = sub { $_[0]->{host}; };
204 foreach my $rec ( @{ $dump->{$rectype} } ) {
205 my $domain_record = new FS::domain_record( {
206 'svcnum' => $domain->svcnum,
207 'reczone' => $rec->{name},
208 'recaf' => $rec->{class} || 'IN',
209 'rectype' => $rectype,
210 'recdata' => &{$datasub}($rec),
212 my $error = $domain_record->insert;
214 warn "$error inserting ".
215 $rec->{name}. ' . '. $domain->domain. "\n";
217 #system('cat',"$main::prefix/$basefile.import");
227 # die "unrecognized type $type\n";