#!/usr/bin/perl -w # # Utility to generate a zone file, add an entry to mail server configs # and the entries for httpsd.conf and /etc/named.conf. # sub generic_sort_config # ( $config_file, $begin_section_pattern, $end_section_pattern, $additional_section, # @order_tags ); # sub sort_sections_by_tags # ( $a, $b ) # sub get_input # ( $prompt_string, $valid_pattern ); # sub get_new_file # ( $prompt_string ); # sub get_existing_dir # ( $prompt_string ); # sub get_host_name # ( $prompt_string ); # sub courier_control # sub add_http_vhost # sub create_zone_file # sub add_to_named_conf use strict; sub generic_sort_config { my $build_section = ""; my @sections; my $other_lines = ""; my $bracket_count = 0; my $position = 0; my ( $config_file, $begin_section_pattern, $end_section_pattern, $additional_section, @order_tags ) = @_; @GenericSortConfig::order_tags = @order_tags; if( ! open(CONF, $config_file) ) { print "Error opening ${config_file}.\n"; return 1; } my $conf_line = ; while( defined($conf_line) ) { if ( $conf_line =~ m/(.*${begin_section_pattern})/ ) { $bracket_count++; $build_section .= $1; $conf_line = substr $conf_line, length $1, length $conf_line; next; } if ( $conf_line =~ m/(.*${end_section_pattern})/ ) { if( $bracket_count == 1 ) { $build_section .= $1; $conf_line = substr $conf_line, length $1, length $conf_line; while( defined($conf_line) && $conf_line eq "\n" ) { $build_section .= "\n"; $conf_line = ; } splice(@sections, $#sections+1, 0, $build_section); $build_section = ""; } elsif( $bracket_count > 1 ) { $build_section .= $1; $conf_line = substr $conf_line, length $1, length $conf_line; } $bracket_count--; next; } if( $bracket_count > 0 ) { $build_section .= $conf_line; $conf_line = ""; } else { $other_lines .= $conf_line; $conf_line = ""; } if( $conf_line eq "" ) { $conf_line = ; } } close(CONF); splice(@sections, $#sections+1, 0, $additional_section); @sections = sort sort_sections_by_tags @sections; if( ! open(CONF, ">${config_file}") ) { print "\n\nError opening $config_file for writing.\n\n"; return 1; } print CONF $other_lines; print CONF @sections; close(CONF); } sub sort_sections_by_tags # ( $a, $b ) { my $aval = -1; my $bval = -1; foreach $GenericSortConfig::tag (@GenericSortConfig::order_tags) { $aval = $GenericSortConfig::tag->[1] if( $aval == -1 && $a =~ m/$GenericSortConfig::tag->[0]/ ); $bval = $GenericSortConfig::tag->[1] if( $bval == -1 && $b =~ m/$GenericSortConfig::tag->[0]/ ); } return $aval cmp $bval if( $aval != $bval ); return $a cmp $b; } sub get_input # ( $prompt_string, $valid_pattern ) { my ( $prompt, $pattern ) = @_; my ( $response ); print "${prompt} "; $response = ; chomp($response); return "" if($response !~ m/$pattern/); return $response; } sub get_new_file # ( $prompt_string ) { my ( $prompt ) = @_; my ( $response ); print "${prompt} "; $response = ; chomp($response); return "" if( -e $response); return $response; } sub get_existing_dir # ( $prompt_string ) { my ( $prompt ) = @_; my ( $response ); print "${prompt} "; $response = ; chomp($response); return "" if( ! -d $response); return $response; } sub get_host_name # ( $prompt_string ) { my ( $prompt ) = @_; my ( $response, $inetn, $host ); print "${prompt} "; $response = ; chomp($response); $inetn = gethostbyname($response); return "" if ( ! $inetn ); my ($a, $b, $c, $d) = unpack('C4', $inetn); $host = "$a.$b.$c.$d"; return $host; } sub courier_control { my ( $chfile ) = @_; my ( @DOMAINS, @NEW ); open(RCPT, "/etc/courier/${chfile}"); @DOMAINS = ; close(RCPT); @NEW = $main::domain."\n"; splice(@DOMAINS, $#DOMAINS+1, 0, @NEW); @DOMAINS = sort @DOMAINS; if( ! open(RCPT, ">/etc/courier/${chfile}") ) { print STDERR "Error writing to /etc/courier/${chfile}.\n"; return 1; } print RCPT @DOMAINS; close(RCPT); } sub add_http_vhost { return if( $main::http_location eq ""); my $http_vhost = " ServerName ${main::domain} ServerAlias ${main::domain} \*.${main::domain} DocumentRoot \"${main::http_location}/html\" Options Indexes Includes FollowSymLinks AllowOverride None Order allow,deny Allow from all ScriptAlias /cgi-bin/ \"${main::http_location}/cgi-bin/\" AllowOverride None Options ExecCGI Order allow,deny Allow from all "; my @otags = ( ["ServerName", 0] ); generic_sort_config( "/etc/httpd/conf/vhosts", "", $http_vhost, @otags ); } sub create_zone_file { my $zone_file = "/var/named/db.${main::domain}"; if( -e $zone_file) { print "${main::zone_file} already exists. I will continue, but will not change that file!\n"; return 1; } if( ! open(ZONE, ">${main::zone_file}") ) { print "\n\nError writing zone file!!\n\n"; return 1; } if( ! open(TEMPLATE, "/var/named/bin/template") ) { print "\n\nCouldn't read template!!\n\n"; return 1; } while (my $tline =