summaryrefslogtreecommitdiff
path: root/bin/bind.import
blob: 57eca2b6311359cded5d395d67b8637acbd24172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/perl -w
#
# $Id: bind.import,v 1.3 2002-07-15 01:44:23 ivan Exp $

#need to manually put header in /usr/local/etc/freeside/export.<datasrc./bind/<machine>/named.conf.HEADER

use strict;
use vars qw( %d_part_svc );
use Term::Query qw(query);
#use BIND::Conf_Parser;
#use DNS::ZoneParse 0.81;

#use Net::SCP qw(iscp);
use Net::SCP qw(scp);
use FS::UID qw(adminsuidsetup datasrc);
use FS::Record qw(qsearch); #qsearchs);
#use FS::svc_acct_sm;
use FS::svc_domain;
use FS::domain_record;
#use FS::svc_acct;
#use FS::part_svc;

my $user = shift or die &usage;
adminsuidsetup $user;

$FS::svc_Common::noexport_hack = 1;
$FS::domain_record::noserial_hack = 1;

use vars qw($spooldir);
$spooldir = "/usr/local/etc/freeside/export.". datasrc. "/bind";
mkdir $spooldir unless -d $spooldir;

%d_part_svc =
  map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_domain'});

print "\n\n",
      ( join "\n", map "$_: ".$d_part_svc{$_}->svc, sort keys %d_part_svc ),
      "\n\n";
use vars qw($domain_svcpart);
$^W=0; #Term::Query isn't -w-safe
$domain_svcpart =
  query "Enter part number for domains: ", 'irk', [ keys %d_part_svc ];
$^W=1;

print "\n\n", <<END;
Enter the location and name of your primary named.conf file, for example
"ns.isp.com:/var/named/named.conf"
END
my($named_conf)=&getvalue(":");
  
use vars qw($named_machine $prefix);
$named_machine = (split(/:/, $named_conf))[0];
$prefix = "$spooldir/$named_machine";
mkdir $prefix unless -d $prefix;

#iscp("root\@$named_conf","$prefix/named.conf.import");
scp("root\@$named_conf","$prefix/named.conf.import");


sub getvalue {
  my $prompt = shift;
  $^W=0; # Term::Query isn't -w-safe
  my $return = query $prompt, '';
  $^W=1;
  $return;
}

print "\n\n";

##

$FS::svc_domain::whois_hack=1;

my $p = Parser->new;
$p->parse_file("$prefix/named.conf.import");

print "\nBIND import completed.\n";

##

sub usage {
  die "Usage:\n\n  svc_domain.import user\n";
}

########
BEGIN {
  
  package Parser;
  use BIND::Conf_Parser;
  use vars qw(@ISA $named_dir);
  @ISA = qw(BIND::Conf_Parser);
  
  sub handle_option {
    my($self, $option, $argument) = @_;
    return unless $option eq "directory";
    $named_dir = $argument;
  }
  
  sub handle_zone {
    my($self, $name, $class, $type, $options) = @_;
    return unless $class eq 'in';
    return if grep { $name eq $_ }
      ( qw( . localhost 127.in-addr.arpa 0.in-addr.arpa 255.in-addr.arpa ) );

    my $domain = new FS::svc_domain( {
      svcpart => $main::domain_svcpart,
      domain  => $name,
      action  => 'N',
    } );
    my $error = $domain->insert;
    die $error if $error;

    if ( $type eq 'slave' ) {

      #use Data::Dumper;
      #print Dumper($options);
      #exit;

      foreach my $master ( @{ $options->{masters} } ) {
        my $domain_record = new FS::domain_record( {
          'svcnum'  => $domain->svcnum,
          'reczone' => '@',
          'recaf'   => 'IN',
          'rectype' => '_mstr',
          'recdata' => $master,
        } );
        my $error = $domain_record->insert;
        die $error if $error;
      }

    } elsif ( $type eq 'master' ) {

      my $file = $options->{file};
  
      use File::Basename;
      my $basefile = basename($file);
      my $sourcefile = $file;
      $sourcefile = "$named_dir/$sourcefile" unless $file =~ /^\//;
      use Net::SCP qw(iscp scp);
      scp("root\@$main::named_machine:$sourcefile",
          "$main::prefix/$basefile.import");
    
      use DNS::ZoneParse 0.81;
      my $zone = DNS::ZoneParse->new("$main::prefix/$basefile.import");
    
      my $dump = $zone->Dump;
  
      #use Data::Dumper;
      #print "$name: ". Dumper($dump);
      #exit;
    
      foreach my $rectype ( keys %$dump ) {
        if ( $rectype =~ /^SOA$/i ) {
          my $rec = $dump->{$rectype};
          my $domain_record = new FS::domain_record( {
            'svcnum'  => $domain->svcnum,
            'reczone' => $rec->{origin},
            'recaf'   => 'IN',
            'rectype' => $rectype,
            'recdata' =>
              $rec->{primary}. ' '. $rec->{email}. ' ( '.
             join(' ', map $rec->{$_},
                           qw( serial refresh retry expire minimumTTL ) ).
             ' )',
          } );
          my $error = $domain_record->insert;
          die $error if $error;
       } else {
          #die $dump->{$rectype};
          foreach my $rec ( @{ $dump->{$rectype} } ) {
            my $domain_record = new FS::domain_record( {
              'svcnum'  => $domain->svcnum,
              'reczone' => $rec->{name},
              'recaf'   => $rec->{class},
              'rectype' => $rectype,
              'recdata' => ( $rectype =~ /^MX$/i
                               ? $rec->{priority}. ' '. $rec->{host}
                               : $rec->{host}                      ),
            } );
            my $error = $domain_record->insert;
            die $error if $error;
          }
        }
      }

    }
    
  }

}
#########