"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "mkdnstab-0.6b/mkdnstab" of archive mkdnstab-0.6b.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 #!/usr/bin/perl
    2 # mkdnstab v 0.6b (C) Miles Lott 10/10/1999 - milos@insync.net
    3 # Make a DNS table for class C or a subset thereof
    4 # I know its not perfect, but it will save you the hassle of typing
    5 # a bunch of addresses manually.
    6 #
    7 # Added ability to handle up to 3 MX entries 05/26/1999
    8 
    9 # Original release version 09/09/1998 v 0.2
   10 # Added ability to handle up to 3 MX entries 05/26/1999 v 0.3
   11 # Fixed serious reverse table generation bug 07/19/1999 v 0.4
   12 # Add ability to build simple bind8 config 08/19/1999 v 0.5
   13 # Added GUI functionality via TkPerl 08/20/1999 v 0.6
   14 #
   15 # Mods to aid reverse mapping problems 10/03/1999 v 0.6a
   16 # Mods to table output to add origin and ttl fields 10/10/1999 v 0.6b
   17 # Cleanup of reverse domain string construction
   18 #
   19 # many thanks to boren, Flav, and deanf on #linpeople
   20 # thanks to the folks on #perl, who helped me get over unnecessary
   21 # complexity in the rewrite of portions of the original code ;)
   22 
   23 use Tk;
   24 use Env qw(HOME);
   25 $version="0.6b";
   26 $dnstabfile="";
   27 $spl="16";
   28 
   29 $wkdir  = qx('pwd'); chomp($wkdir);
   30 if (!$dirspec) { $dirspec=$wkdir;}
   31 
   32 # Main work after opening file or entering data manually
   33 
   34 sub mkdnstab {
   35     &getfields; &cleandata;
   36     &check_val; if ($rtrn) { return; }
   37     @foo;@bar;
   38     $_=$tbldef[23];
   39     while ( /(\d+)\.\.(\d+)/g ) { push @foo, ( $1..$2 ) }
   40     while ( /\,(\d+)/g ) { push @foo, ( $1, ) }
   41     @bar=@foo;
   42     chdir $dirspec;
   43     if ($tbldef[0]) {
   44 	open (FWD,">$tbldef[24]");
   45 
   46 # with headers?
   47 
   48 	if ($tbldef[2]) {
   49 	    print FWD "\$ORIGIN $tbldef[3]\.\n\$TTL $tbldef[22]\n\n";
   50 	    print FWD "\@\tIN\tSOA\t$tbldef[5]\. $tbldef[16]\.$tbldef[5]\.$tbldef[3]\.\t(\n";
   51 	    print FWD "\t\t\t\t$tbldef[18] \;\tSerial\n\t\t\t\t$tbldef[19]\t\;\tRefresh\n";
   52 	    print FWD "\t\t\t\t$tbldef[20]\t\;\tRetry\n\t\t\t\t$tbldef[21]\t\;\tExpire\n";
   53 	    print FWD "\t\t\t\t$tbldef[22]\ )\t\;\tMinimum\n\n";
   54 	    print FWD "\@\t\tIN\tA\t$tbldef[4]\.$tbldef[6]\n";
   55 	    print FWD "\@\t\tIN\tNS\t$tbldef[5]\n";
   56 	    print FWD "\@\t\tIN\tMX\t$tbldef[9]\t$tbldef[7]\n" if ($tbldef[7]);
   57 	    print FWD "\@\t\tIN\tMX\t$tbldef[12]\t$tbldef[10]\n" if ($tbldef[10]);
   58 	    print FWD "\@\t\tIN\tMX\t$tbldef[15]\t$tbldef[13]\n" if ($tbldef[13]);
   59 	    print FWD "\n";
   60 	}
   61 
   62 # forward table
   63 
   64     while (<@foo>) {
   65 	if ($tbldef[6]) {
   66 	    if (/\b$tbldef[6]\b/) {
   67 		print FWD "$tbldef[5]\t\tIN\tA\t$tbldef[4].$tbldef[6]\n";next
   68 	    }
   69 	}
   70 	if ($tbldef[8]) {
   71 	    if (/\b$tbldef[8]\b/) {
   72 		print FWD "$tbldef[7]\t\tIN\tA\t$tbldef[4].$tbldef[8]\n";next
   73 	    }
   74 	}
   75 	if ($tbldef[11]){
   76 	    if (/\b$tbldef[11]\b/) {
   77 		print FWD "$tbldef[10]\t\tIN\tA\t$tbldef[4].$tbldef[11]\n";next
   78 	    }
   79 	}
   80 	if ($tbldef[14]) {
   81 	    if (/\b$tbldef[14]\b/) {
   82 		print FWD "$tbldef[13]\t\tIN\tA\t$tbldef[4].$tbldef[14]\n";next
   83 	    }
   84 	}
   85 	    if (/[0-9][0-9][0-9]/) {
   86 		print FWD "$tbldef[17]$_\t\tIN\tA\t$tbldef[4].$_\n";next }
   87 	    if (/[0-9][0-9]/) {
   88 		print FWD "$tbldef[17]$_\t\tIN\tA\t$tbldef[4].$_\n";next }
   89 	    if (/[0-9]/) {
   90 	        print FWD "$tbldef[17]\60$_\t\tIN\tA\t$tbldef[4].$_\n";next }
   91 	    shift;
   92 	}
   93 	close FWD;
   94     }
   95 
   96 # now the reverse table
   97 
   98     if ($tbldef[1]) {
   99     $_=$tbldef[4]; while ( /(\d+)\.(\d+)\.(\d+)/g ) {
  100         $revdom="$3.$2.$1.in-addr.arpa"; }
  101 
  102 	open (REV,">$tbldef[25]");
  103 
  104 	if ($tbldef[2]) {
  105 	    print REV "\$ORIGIN $revdom\.\n\$TTL $tbldef[22]\n\n";
  106 	    print REV "\@\tIN\tSOA\t$tbldef[5]\. $tbldef[16]\.$tbldef[5]\.$tbldef[3]\.\t(\n";
  107 	    print REV "\t\t\t\t$tbldef[18] \;\tSerial\n\t\t\t\t$tbldef[19]\t\;\tRefresh\n";
  108 	    print REV "\t\t\t\t$tbldef[20]\t\;\tRetry\n\t\t\t\t$tbldef[21]\t\;\tExpire\n";
  109 	    print REV "\t\t\t\t$tbldef[22]\ )\t\;\tMinimum\n\n";
  110 	    print REV "\@\t\tIN\tPTR\t$tbldef[3]\.\n";
  111 	    print REV "\@\t\tIN\tNS\t$tbldef[5]\.\n";
  112 	}
  113 
  114 	while (<@bar>) {
  115 	    if (/\b$tbldef[6]\b/) {
  116 		print REV "$tbldef[6]\t\tIN\tPTR\t$tbldef[5]\.$tbldef[3]\.\n";next }
  117 	    if ($tbldef[8]) { if (/\b$tbldef[8]\b/) {
  118 		print REV "$tbldef[8]\t\tIN\tPTR\t$tbldef[7]\.$tbldef[3]\.\n";next }}
  119 	    if ($tbldef[11]) { if (/\b$tbldef[11]\b/) {
  120 		print REV "$tbldef[11]\t\tIN\tPTR\t$tbldef[10]\.$tbldef[3]\.\n";next }}
  121 	    if ($tbldef[14]) { if (/\b$tbldef[14]\b/) {
  122 		print REV "$tbldef[14]\t\tIN\tPTR\t$tbldef[13]\.$tbldef[3]\.\n";next }}
  123 	    if (/[0-9][0-9][0-9]/) {
  124 		print REV "$_\t\tIN\tPTR\t$tbldef[17]$_\.$tbldef[3]\.\n";next }
  125 	    if (/[0-9][0-9]/) {
  126 		print REV "$_\t\tIN\tPTR\t$tbldef[17]$_\.$tbldef[3]\.\n";next }
  127 	    if (/[0-9]/) {
  128 		print REV "$_\t\tIN\tPTR\t$tbldef[17]\60$_\.$tbldef[3]\.\n";next }
  129 	    shift;
  130 	}
  131 	close REV;
  132     }
  133 
  134 # now the named.conf and local domain files
  135 
  136     if ($tbldef[26]) {
  137 	if ($tbldef[31]) {$domtype="master";} else {$domtype="slave";}
  138 	open (CONF,">$tbldef[27]");
  139 	$timeinfo=scalar(localtime(time));
  140 	print CONF "\/\/ generated by $0 $timeinfo\n";
  141 	print CONF "options \{\n\t\/\/ restrict access via specific ip rather than all addrs\n";
  142 	print CONF "\t\/\/ for this machine\n\tlisten-on \{ $tbldef[4].$tbldef[6]\; 127.0.0.1\; \}\;\n";
  143 	print CONF "\t\/\/ location of the zone files\n\tdirectory \"\/var\/named\"\;\n";
  144 	print CONF "\tforwarders \{";
  145 	if ($tbldef[29]) { print CONF "\n\t\t$tbldef[29]\;"; }
  146 	if ($tbldef[30]) { print CONF "\n\t\t$tbldef[30]\;"; }
  147 	print CONF "\n\t\}\;\n";
  148 	print CONF "\t\/\/ make bind8 query on port 53\n\t\/\/ \(may need only if firewalling prohibits other ports\)\n\t";
  149 	print CONF "query-source address \* port 53\;\n\}\;\n\n";
  150 	print CONF "\/\/\n\/\/ a caching only nameserver config\n\/\/\n";
  151 	print CONF "zone \".\" \{\n\ttype hint\;\n\tfile \"named.ca\"\;\n\}\;\n\n";
  152 	print CONF "\/\/\n\/\/ configured zones\n\/\/\n";
  153 	print CONF "zone \"$tbldef[3]\" \{\n\ttype $domtype\;\n\tfile \"$tbldef[24]\"\;\n";
  154 	if (!$tbldef[31]) { print CONF "\tmasters \{\n\t\t$tbldef[32]\;\n\t\}\;\n"; }
  155 	print CONF "\}\;\n\n";
  156 	print CONF "zone \"$revdom\" \{\n\ttype $domtype\;\n\tfile \"$tbldef[25]\"\;\n";
  157 	if (!$tbldef[31]) { print CONF "\tmasters \{\n\t\t$tbldef[32]\;\n\t\}\;\n"; }
  158 	print CONF "\}\;\n\n";
  159 	print CONF "zone \"0.0.127.in-addr.arpa\" \{\n\ttype master\;\n\tfile \"$tbldef[28]\"\;\n\}\;";
  160 	close CONF;
  161 
  162 	open (LOC,">$tbldef[28]");
  163 	if ($tbldef[2]) {
  164 	    print LOC "\$TTL 86400\n\n";
  165 	    print LOC "\@\tIN\tSOA\tlocalhost\. $tbldef[16]\.localhost\.\t(\n";
  166 	    print LOC "\t\t\t\t$tbldef[18] \;\tSerial\n\t\t\t\t$tbldef[19]\t\;\tRefresh\n";
  167 	    print LOC "\t\t\t\t$tbldef[20]\t\;\tRetry\n\t\t\t\t$tbldef[21]\t\;\tExpire\n";
  168 	    print LOC "\t\t\t\t$tbldef[22]\ )\t\;\tMinimum\n\n";
  169 	}
  170 	print LOC "\t\tIN\tNS\tlocalhost\.\n";
  171 	print LOC "1\t\tIN\tPTR\tlocalhost\.\n";
  172 	close LOC;
  173     }
  174 }
  175 
  176 ### GUI stuff:
  177 
  178 $mw = MainWindow->new;
  179 $mw->title("mkdnstab $version");
  180 
  181 $mfile = $mw->Frame(
  182 	-relief=>'ridge',
  183 	-borderwidth=>2)
  184 	->pack(
  185 	-fill=>'x',
  186 	-anchor=>'w',
  187 	-side=>'bottom');
  188 
  189 $mfile->Label(-text=>"",
  190 	-textvariable => \$tabfile)
  191 	->pack(
  192 	-anchor=>'w',
  193 	-side=>'bottom');
  194 
  195 $mfile->Label(-text=>"Current File:")
  196 	->pack(
  197 	-anchor=>'w',
  198 	-side=>'bottom');
  199 
  200 $mfield = $mw->Frame(
  201 	-relief=>'ridge',
  202 	-borderwidth=>2)
  203 	->pack(
  204 	-fill=>'x',
  205 	-anchor=>'ne',
  206 	-side=>'left');
  207 
  208 $mfield2 = $mw->Frame(
  209 	-relief=>'ridge',
  210 	-borderwidth=>2)
  211 	->pack(
  212 	-fill=>'x',
  213 	-anchor=>'nw',
  214 	-side=>'right');
  215 
  216 @names = ('Build Forward Zone File?:','Build Reverse Zone File?:',
  217 	'Create Standard File Headers?:','Domain:', 'Subnet:',
  218 	'DNS Server Hostname:', 'DNS Server IP Address:',
  219 	'Primary Mailserver Hostname:','Primary Mailserver IP Address:',
  220 	'Primary Mailserver Preference Level:',
  221 	'Secondary Mailserver Hostname:','Secondary Mailserver IP Address:',
  222 	'Secondary Mailserver Preference Level:',
  223 	'Tertiary Mailserver Hostname:','Tertiary Mailserver IP Address:',
  224 	'Tertiary Mailserver Preference Level:',
  225 	'DNS Administrator Username:', 'Default Host Prefix:',
  226 	'Serial Number:', 'Refresh Time:', 'Retry Time:', 'Expire Time:',
  227 	'Minimum Time To Live:', 'IP Range:',
  228 	'Fwd Zone File Name:','Rev Zone File Name:',
  229 	'Build Conf File?:','Conf File Name:','Local Zone File Name:',
  230 	'Forwarder 1:','Forwarder 2:','Is server MASTER?:','MASTER server ip:');
  231 
  232 for $i (0..$spl) {
  233 	($f,$l,$e) = ("mfield_${i}", "mfield_${i}_l", "mfield_${i}_e");
  234 	${$f} = $mfield->Frame();
  235 	${$f}->pack(-side => 'top', -pady => '2', -anchor => 'e');
  236 
  237 	${$l} = ${$f}->Label(-text => $names[$i], -anchor => 'e');
  238 	${$e} = ${$f}->Entry(-width => '30', -relief => 'sunken');
  239 	${$e}->pack(-side => 'right');
  240 	${$l}->pack(-side => 'right');
  241 	$ref_to_name{$e} = ${$e};
  242 }
  243 
  244 for $i ($spl+1..$#names) {
  245 	($f,$l,$e) = ("mfield2_${i}", "mfield2_${i}_l", "mfield2_${i}_e");
  246 	${$f} = $mfield2->Frame();
  247 	${$f}->pack(-side => 'top', -pady => '2', -anchor => 'e');
  248 
  249 	${$l} = ${$f}->Label(-text => $names[$i], -anchor => 'e');
  250 	${$e} = ${$f}->Entry(-width => '30', -relief => 'sunken');
  251 	${$e}->pack(-side => 'right');
  252 	${$l}->pack(-side => 'right');
  253 	$ref_to_name{$e} = ${$e};
  254 }
  255 
  256 $mbar = $mw->Frame(
  257 	-relief=>'ridge',
  258 	-borderwidth=>2)
  259 	->pack(
  260 	-before=> $mfield,
  261 	-fill=>'x',
  262 	-anchor=>'e',
  263 	-side=>'top');
  264 
  265 $mbar->Menubutton(-text => "File", -menuitems => [
  266 	[ 'command' => "Open zone def", -command => \&open_def],
  267 	[ 'command' => "New zone def", -command => \&new_def],
  268 	[ 'command' => "Save zone def", -command => \&save_def],
  269 	[ 'command' => "Save zone def as", -command => \&saveas_def],
  270 	[ 'command' => "Create zone files", -command => \&mkdnstab],
  271 	[ 'command' => "Exit", -command =>sub{exit}]])
  272 	->pack(
  273 	-side => 'left',
  274 	-anchor => 'w');
  275 
  276 $mbar->Menubutton(-text => "Help", -menuitems => [
  277 	[ 'command' => "Contents", -command => \&help_me],
  278 	[ 'command' => "About mkdnstab...", -command => \&about_box]])
  279 	->pack(
  280 	-side =>'left',
  281 	-anchor => 'w');
  282 
  283 ### End main GUI
  284 
  285 sub about_box {
  286     $mw->Dialog(-text =>"mkdnstab,\nthe DNS Zone File Generator,\nis released under the GPL\nCopyright 1999 by Miles Lott\nmilos\@insync.net\nversion $version",
  287 	-title =>'About mkdnstab',
  288 	-default_button=>'OK',
  289 	-buttons =>[qw/OK/])->Show;
  290 }
  291 
  292 sub help_me {
  293     $mw->Dialog(-text =>"fwd, rev, and headers should be 1 if you want to generate\nforward or reverse tables with headers, or 0 if not.\n\nAny or all of the Mailserver\nspecs may be empty, i.e. if you have only one mailserver.\n\nIP Range should be of the format:\n\t'1..20'\nor\t'1,2'\nor\t'1..4,30..254'\netc...",
  294 	-title =>'mkdnstab Help',
  295 	-default_button=>'OK',
  296 	-buttons =>[qw/OK/])->Show;
  297 }
  298 
  299 sub new_def {
  300     if ($tbldef[0] ne "") {
  301 	&confirm('Discard any changes to ', 'discard file',$dnstabfile);
  302 	chomp($answer); $_ = $answer; if (/No/g)  { goto NONEW; }
  303     }
  304     &clearfields;
  305     &defaults;
  306     $dnstabfile = "tmp.dnstab"; $tabfile = $dnstabfile;
  307     &getfields;
  308     NONEW:
  309 }
  310 
  311 sub open_def {
  312     $FSref = $mw->MainWindow->FileSelect(
  313 	-directory => $dirspec,
  314 	-filter => "\*\.dnstab",);
  315     $dnstabfile = $FSref->Show;
  316     open (FILE,"$dnstabfile");
  317     while (<FILE>) { @tbldef = split(/;/,$_); }
  318     close FILE;
  319     $tabfile=$dnstabfile;
  320     &cleandata;
  321     &clearfields;
  322     &fillfields;
  323     $dirspec= $FSref->directory;
  324 }
  325 
  326 sub saveas_def {
  327     &getfields;
  328     if (!$tbldef[0]) {
  329 	&errwin('Please open or create a new file first.  Or, save the current file.',
  330 		'non-fatal');
  331 	goto NOSAVEAS;
  332     }
  333 
  334     $FSref = $mw->MainWindow->FileSelect(
  335 	-directory => $dirspec,
  336 	-filter => "\*\.dnstab",);
  337     $dnstabfile = $FSref->Show;
  338 
  339     if (stat($dnstabfile)) {
  340 	&confirm('Are you sure you want to overwrite', 'save file',$dnstabfile);
  341 	chomp($answer); $_ = $answer; if (/No/g)  { goto NOSAVEAS; }
  342     }
  343     &getfields;
  344     if (substr($dnstabfile,-6,6) ne "dnstab") { $dnstabfile="$dnstabfile.dnstab"; }
  345     open (FILE,">$dnstabfile");
  346     for $i (0..$#tbldef) {
  347 	print FILE "$tbldef[$i]";
  348 	if ($i<$#tbldef) { print FILE "\;"; }
  349     }
  350     close FILE;
  351     $tabfile=$dnstabfile;
  352     NOSAVEAS:
  353 }
  354 
  355 sub save_def {
  356     &getfields;
  357     if (!$tbldef[0]) {
  358 	&errwin('Please open or create a new file first.', 'non-fatal');
  359 	goto NOSAVE;
  360     }
  361     $dnstabfile = $tabfile;
  362     if (stat($dnstabfile)) {
  363 	&confirm('Are you sure you want to overwrite', 'save file',$dnstabfile);
  364 	chomp($answer); $_ = $answer; if (/No/g)  { goto NOSAVE; }
  365     }
  366     &getfields;
  367     open (FILE,">$dnstabfile");
  368     for $i (0..$#tbldef) {
  369 	print FILE "$tbldef[$i]";
  370 	if ($i<$#tbldef) { print FILE "\;"; }
  371     }
  372     close FILE;
  373     NOSAVE:
  374 }
  375 
  376 sub errwin() {
  377 	$mw->Dialog(-text =>"$_[0]",
  378 	-title => "$0 $version $_[1] error",
  379         -default_button=>'OK',
  380         -buttons =>[qw/OK/])->Show;
  381 }
  382 
  383 sub confirm() {
  384 	$answer = $mw->Dialog(-text =>"$_[0] $_[2] ?",
  385 	-title => "Confirm request to $_[1]",
  386 	-bitmap => 'question',
  387 	-default_button=>'No',
  388 	-buttons =>[qw/Yes No/])->Show;
  389 }
  390 
  391 sub debugprint {
  392 	for $i (1..$#tbldef+1) { print "$i\:\t$tbldef[$i-1]\n"; }
  393 }
  394 
  395 sub cleandata {
  396     for $i (0..$#tbldef) { chomp $tbldef[$i]; }
  397 }
  398 
  399 sub fillfields {
  400     for $i (0..$spl) {
  401 	($f,$l,$e) = ("mfield_${i}", "mfield_${i}_l", "mfield_${i}_e");
  402 	${$e}->insert('0', $tbldef[$i]);
  403     }
  404    for $i ($spl+1..$#tbldef) {
  405 	($f,$l,$e) = ("mfield2_${i}", "mfield2_${i}_l", "mfield2_${i}_e");
  406 	${$e}->insert('0', $tbldef[$i]);
  407     }
  408 }
  409 
  410 sub getfields {
  411     for $i (0..$spl) {
  412 	($f,$l,$e) = ("mfield_${i}", "mfield_${i}_l", "mfield_${i}_e");
  413 	$tbldef[$i] = ${$e}->get();
  414     }
  415     for $i ($spl+1..$#names) {
  416 	($f,$l,$e) = ("mfield2_${i}", "mfield2_${i}_l", "mfield2_${i}_e");
  417 	$tbldef[$i] = ${$e}->get();
  418     }
  419 }
  420 
  421 sub clearfields {
  422     for $i (0..$spl) {
  423 	($f,$l,$e) = ("mfield_${i}", "mfield_${i}_l", "mfield_${i}_e");
  424 	${$e}->delete('0', 'end');
  425     }
  426     for $i ($spl+1..$#tbldef) {
  427 	($f,$l,$e) = ("mfield2_${i}", "mfield2_${i}_l", "mfield2_${i}_e");
  428 	${$e}->delete('0', 'end');
  429     }
  430 } 
  431 
  432 sub defaults {
  433     for $i (0..2) {
  434 	($f,$l,$e) = ("mfield_${i}", "mfield_${i}_l", "mfield_${i}_e");
  435 	${$e}->insert('0', '1');
  436     }
  437 }
  438 
  439 sub check_val {
  440     if (!$tbldef[3]) {
  441 	&errwin('A zone file that owns no domain is pretty useless.','No domain');
  442 	$rtrn="9";
  443     }
  444     if (!$tbldef[4]) {
  445 	&errwin('A zone file that cannot address a subnet will not function.','No subnet');
  446 	$rtrn="9";
  447     }
  448     if (!$tbldef[24]) {
  449 	&errwin('An IP Range is required to build the table properly.','No range given');
  450 	$rtrn="9";
  451     }
  452 }
  453 
  454 MainLoop;