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