#!/usr/bin/perl -w # # mkservers # jms1@jms1.net 2002-10-15 # # reads "servers.txt" and produces the files in the "servers" directory that # dnscache needs # # 2003-07-22 jms1 - added GPL notice, allowing for multiple lines of "@" # ############################################################################### # # Copyright (C) 2003 John Simpson. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # or visit http://www.gnu.org/licenses/gpl.txt # ############################################################################### require 5.003 ; use strict ; ############################################################################### # # configuration ######################################## # service directory my $svc_dir = "/service/dnscache" ; ######################################## # delete all existing "servers/*" files # before we write our files? my $do_unlink = 0 ; ############################################################################### # # globals my ( %droot , $ln ) ; ############################################################################### # # die if a given string isn't really an IP address sub checkip($) { my $ip = ( shift || "" ) ; my @d = split ( /\./ , $ip ) ; die "Invalid IP \"$ip\" on line $ln\n" unless ( 3 == $#d ) ; for my $p ( @d ) { die "Invalid IP \"$ip\" on line $ln\n" if ( $p =~ /\D/ ) ; die "Invalid IP \"$ip\" on line $ln\n" unless ( $p >= 0 && $p <= 255 ) ; } } ############################################################################### ############################################################################### ############################################################################### # # CODE STARTS HERE # # read the data into memory open ( I , "<$svc_dir/root/servers.txt" ) or die "Can\'t read $svc_dir/root/servers.txt: $!\n" ; $ln = 0 ; while ( my $line = ) { $ln ++ ; chomp $line ; my @w = split ( /\:/ , $line ) ; my $domain = ( shift @w || "\@" ) ; if ( $domain ne "\@" ) { die "Duplicate domain \"$domain\" at line $ln\n" if ( exists $droot{$domain} ) ; } for my $ip ( @w ) { checkip $ip ; $droot{$domain} .= "$ip\n" ; } } close I ; ############################################################################### # # if we're erasing all old data, do that here... if ( $do_unlink ) { map { unlink } glob "$svc_dir/root/servers/*" ; } ############################################################################### # # write our data for my $domain ( sort keys %droot ) { open ( O , ">$svc_dir/root/servers/$domain" ) or die "Can\'t create $svc_dir/root/servers/$domain: $!\n" ; print O $droot{$domain} ; close O ; chmod ( 0644 , "$svc_dir/root/servers/$domain" ) ; }