zone-mk.pl
branchhs12
changeset 39 8b46e7c48995
parent 30 5ac92c1ffdf9
child 40 512e12c40389
equal deleted inserted replaced
38:d50f6874b7ab 39:8b46e7c48995
       
     1 #!/usr/bin/perl -w
       
     2 
       
     3 use strict;
       
     4 use FindBin;
       
     5 
       
     6 if ( @ARGV < 2 ) {
       
     7     print "usage: zone-mk kundennummer domain ... \n";
       
     8     exit 1;
       
     9 }
       
    10 
       
    11 # oeffnet Konfigurations- und Templatefiles - relativ oder absolut
       
    12 my @configs = ( "$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf" );
       
    13 my @templc = (
       
    14     "$FindBin::Bin/templates/named.config",
       
    15     "/etc/dnstools/templates/named.config"
       
    16 );
       
    17 my @templz = (
       
    18     "$FindBin::Bin/templates/named.zone",
       
    19     "/etc/dnstools/templates/named.zone"
       
    20 );
       
    21 my %config;
       
    22 
       
    23 for ( grep {-f} @configs ) {
       
    24     open( CONFIG, $_ ) or die "Can't open $_: $!\n";
       
    25 }
       
    26 unless ( seek( CONFIG, 0, 0 ) ) {
       
    27     die "Can't open config (searched: @configs)\n";
       
    28 }
       
    29 
       
    30 for ( grep {-f} @templc ) {
       
    31     open( TEMPCONF, $_ ) or die "Can't open $_: $!\n";
       
    32 }
       
    33 unless ( seek( TEMPCONF, 0, 0 ) ) {
       
    34     die "Can't open template (searched: @templc)\n";
       
    35 }
       
    36 
       
    37 for ( grep {-f} @templz ) {
       
    38     open( TEMPZONE, $_ ) or die "Can't open $_: $!\n";
       
    39 }
       
    40 unless ( seek( TEMPZONE, 0, 0 ) ) {
       
    41     die "Can't open template (searched: @templz)\n";
       
    42 }
       
    43 
       
    44 while (<CONFIG>) {
       
    45     chomp;
       
    46     s/#.*//;
       
    47     s/\t//g;
       
    48     s/\s//g;
       
    49     next unless length;
       
    50     my ( $cname, $ccont ) = split( /\s*=\s*/, $_, 2 );
       
    51     $config{$cname} = $ccont;
       
    52 }
       
    53 close(CONFIG);
       
    54 
       
    55 my $primary       = $config{primary};
       
    56 my $secondary     = $config{secondary};
       
    57 my $zone_conf_dir = $config{zone_conf_dir};
       
    58 my $master_dir    = $config{master_dir};
       
    59 my $customer      = shift @ARGV;
       
    60 chomp( my $primary_ip   = `dig +short $primary` );
       
    61 chomp( my $secondary_ip = `dig +short $secondary` );
       
    62 chomp( my $this_host    = `hostname -f` );
       
    63 chomp( my $this_ip      = `hostname -i` );
       
    64 chomp( my $this_domain  = `hostname -d` );
       
    65 chomp( my $time         = `date +%Y%m%d00` );
       
    66 chomp( my $start        = `date -I` );
       
    67 my $hostmaster = "hostmaster.$this_domain";
       
    68 
       
    69 unless ( -d $master_dir and -r $master_dir ) {
       
    70     die "$master_dir: $!\n";
       
    71 }
       
    72 
       
    73 unless ( -d $zone_conf_dir and -r $zone_conf_dir ) {
       
    74     die "$master_dir: $!\n";
       
    75 }
       
    76 
       
    77 # legt fuer jede domain in @ARGV ein verzeichnis in $master_dir an.
       
    78 # schreibt aus den angegebenen templates die dateien $zonefile und $config
       
    79 # in die entsprechenden verzeichnisse.
       
    80 for (@ARGV) {
       
    81 
       
    82     chomp( my $domain = `idn --quiet "$_"` );
       
    83     my $zonefile   = "$master_dir/$domain/$domain";
       
    84     my $config     = "$zone_conf_dir/$domain";
       
    85     my $utf8domain = "$_";
       
    86 
       
    87     unless ( -d "$master_dir/$domain" ) {
       
    88         `mkdir $master_dir/$domain`;
       
    89     }
       
    90 
       
    91     if ( -f $zonefile ) {
       
    92         $zonefile =~ s#/.*/##;
       
    93         print "$zonefile exists. Skipping $domain\n";
       
    94         next;
       
    95     }
       
    96     if ( -f $config ) {
       
    97         $config =~ s#/.*/##;
       
    98         print "$config exists. Skipping $domain\n";
       
    99         next;
       
   100     }
       
   101 
       
   102     print "$domain ($_) for $customer \n";
       
   103 
       
   104     my @tempzone = <TEMPZONE>;
       
   105     for (@tempzone) {
       
   106         s#<start>#$start#;
       
   107         s#<domain>#$domain#;
       
   108         s#<time>#$time#;
       
   109         s#<primary>#$primary#;
       
   110         s#<secondary>#$secondary#;
       
   111         s#<hostmaster>#$hostmaster#;
       
   112         s#<customer>#$customer#;
       
   113         s#<utf8domain>#$utf8domain#;
       
   114     }
       
   115 
       
   116     open( ZONEOUT, ">$zonefile" );
       
   117     print ZONEOUT @tempzone;
       
   118     close(ZONEOUT);
       
   119 
       
   120     my @tempconf = <TEMPCONF>;
       
   121     for (@tempconf) {
       
   122         s#<domain>#$domain#;
       
   123         s#<start>#$start#;
       
   124         s#<customer>#$customer#;
       
   125         s#<utf8domain>#$utf8domain#;
       
   126         s#<file>#$master_dir/$domain/$domain#;
       
   127         s#<primary_ip>#$primary_ip#;
       
   128         s#<secondary_ip>#$secondary_ip#;
       
   129     }
       
   130 
       
   131     open( CONFOUT, ">$config" );
       
   132     print CONFOUT @tempconf;
       
   133     close(CONFOUT);
       
   134 }