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