#!/opt/casp/tools/bin/linux2/perl5/bin/perl -I/opt/casp/tools/bin/linux2/perl5/lib -I/opt/casp use INSTALL::common; use INSTALL::global; use INSTALL::parameter; use INSTALL::os; my $pParameters = \%INSTALL::parameter::variables; #============================================================================== #------------------------ Default global values #============================================================================== $pParameters->{asphome} = "/opt/casp"; $pParameters->{com_layer} = "chilicom"; INSTALL::common::set_standard_globals; my $pGlobals = \%INSTALL::global::variables; my $pOS = \%INSTALL::os::Details; sub usage { print STDERR ("usage: vhostctl engine= vhost= enable=\n". " [global_enable=] [allow_all=]\n". "\n". "===============================================================================\n". "Note (0): \n". " global_enable == yes, specifies that if no virtual hosts are in the\n". " virtual host allow list, allow_all is set to true. Otherwise, if\n", " this value is either left unspecified or set to no, allow_all will\n", " be set to false.\n". "Note (1): \n". " allow_all == yes, specifies that allow_all will be set to yes, \n". " regardless of any condition (see global_enable for a conditional set).\n". " Because of precedence, the allow_all command option will override\n". " any other allow_all modifying flag (e.g. global_enable). In this way\n". " whatever allow_all is set to is the resultant configuration setting.\n" ); exit(1); } #============================================================================== #------------------------ Validate all the required parameters. #============================================================================== my %PARAM_REQUIREMENTS = ( REQUIRED => [ 'engine', 'vhost', 'enable' ], OPTIONAL => [ 'global_enable', 'allow_all' ] ); $pParameters->{non_interactive} = 'true'; # This tool will _NOT_ run interactively. if ((not @ARGV) || boolean_value($pParameters->{'--help'})) { usage(); } unless(INSTALL::parameter::verify(%PARAM_REQUIREMENTS)) { exit (1); } if ($pParameters->{vhost} !~ /^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*/) { exit(not error("Invalid vhost specified '$pParameters->{vhost}'")); } ########## Confirm parameters. if (not -d $pParameters->{engine}) { exit(not error("Specified engine directory '$pParameters->{engine}' does not exist.")); } my $casp_cnfg = "$pParameters->{engine}/casp.cnfg"; if (not -r $casp_cnfg) { exit(not error("Engine configuration '$pParameters->{engine}/casp.cnfg' does not exist,", "or is not readable.")); } use INSTALL::ini; ########## Read in casp.cnfg. my $pIni = INSTALL::ini::open($casp_cnfg); if (not $pIni) { exit(not error("Unable to open '$casp_cnfg'.")); } my $pSection = INSTALL::ini::insertSection($pIni,"virtual hosts"); if (not $pSection) { exit(not error("Unable to open section 'virtual hosts'.")); } ########## Remove virtual host comments. my $pComments = INSTALL::ini::insertComments($pIni,"virtual hosts"); my $quoted_vhost = quotemeta($pParameters->{vhost}); sub no_match_vhost_comment { my ($comment) = @_; if ($comment !~ /^$quoted_vhost$/) { return($comment); } else { return(undef); } } @{$pComments} = grep { no_match_vhost_comment($_) } @{$pComments}; ########## Enable / disable hosts. $pSection->{allow_all} = "no"; if (boolean_value($pParameters->{enable})) { $pSection->{$pParameters->{vhost}} = ""; } else { delete $pSection->{$pParameters->{vhost}}; push @{$pComments},$pParameters->{vhost}; } ########## If global_enable == true, then set allow_all = yes, if no ########## virtual hosts are in the list. if (boolean_value($pParameters->{global_enable})) { $pSection->{allow_all} = "yes"; # default to yes, reset to no upon finding first vhost. foreach(keys %{$pSection}) { if (($_ ne "allow_all") && ($_ ne "timeout")) { # Detected a vhost. $pSection->{allow_all} = "no"; } } } if ((@ARGV > 1) || ((@ARGV == 1) && (not grep { $ARGV[0] =~ /^$_$/} @{$PARAM_REQUIREMENTS{OPTIONAL}}))) { usage(); } ########## Master-override. if (boolean_value($pParameters->{allow_all})) { $pSection->{allow_all} = "yes"; } exit(not INSTALL::ini::save($pIni,$casp_cnfg));