#!/usr/bin/perl -w -I/usr/sausalito/perl -I.
# $Id: regen_httpd_figlet
#
# this handler is responsible for maintaining the per-Workgroup configuration
# files for Apache.
#
# Depends on:
#   System.hostname
#   System.domainname
#
# MPBug fixed.

# Debugging switch:
$DEBUG = "0";
if ($DEBUG)
{
        use Sys::Syslog qw( :DEFAULT setlogsock);
}

my $confdir = '/etc/httpd/conf';
my $SSLconfdir = '/etc/httpd/conf.d/';

use Sauce::Config;
use FileHandle;
use File::Copy;
use CCE;

my $cce = new CCE;
$cce->connectfd();

&debug_msg("Running regen_httpd_figlet\n");

my ($oid) = $cce->find("System");
my ($ok, $obj) = $cce->get($oid);
my ($status, $web) = $cce->get($oid, "Web");

my $hostname = $obj->{hostname};
my $domain = $obj->{domainname};
$hostname =~ s/\.${domain}$//;
my $fqdn = $hostname . '.' . $domain;
my $httpPort = $web->{'httpPort'};
my $sslPort = $web->{'sslPort'};
my $HSTS = $web->{'HSTS'};

my $minSpare = $web->{minSpare};
my $maxSpare = $web->{maxSpare};
my $maxClients = $web->{maxClients};
my $hostLookups = $web->{hostnameLookups};
if($hostLookups) {
  $hostLookups = 'on';
} else {
  $hostLookups = 'off';
}

umask(0077);
my $stage = "$confdir/httpd.conf~";
open(HTTPD, "$confdir/httpd.conf");
unlink($stage);
sysopen(STAGE, $stage, 1|O_CREAT|O_EXCL, 0600) || die;
&debug_msg("Working on $stage\n");
while(<HTTPD>) {
  s/^ServerAdmin\s.+$/ServerAdmin admin\@$fqdn/;
  s/^ServerName\s.+$/ServerName $fqdn/;
  s/^#ServerName\s.+$/ServerName $fqdn/;
  s/^MinSpareServers\s.+$/MinSpareServers $minSpare/;
  s/^MaxSpareServers\s.+$/MaxSpareServers $maxSpare/;
  s/^MaxClients\s.+$/MaxClients $maxClients/;
  s/^HostnameLookups\s.+$/HostnameLookups $hostLookups/;
  s/^Listen\s.+$/Listen $httpPort/;
  
  print STAGE;
}
close(STAGE);
close(HTTPD);

chmod(0644, $stage);
if(-s $stage) {
  move($stage,"$confdir/httpd.conf");
  chmod(0644, "$confdir/httpd.conf"); # paranoia

  #
  #-- Start: Handle ssl_bx.conf:
  #

  umask(0077);
  my $SSLstage = "$SSLconfdir/ssl_bx.conf~";
  open(SSLCONF, "$SSLconfdir/ssl_bx.conf");
  unlink($SSLstage);
  sysopen(SSLSTAGE, $SSLstage, 1|O_CREAT|O_EXCL, 0600) || die;
  &debug_msg("Working on $SSLstage\n");
  while(<SSLCONF>) {
    s/^NameVirtualHost\s.+$/NameVirtualHost *:$sslPort/;
    s/^Listen\s.+$/Listen $sslPort/;

    print SSLSTAGE;
  }
  close(SSLSTAGE);
  close(SSLCONF);

  chmod(0644, $SSLstage);
  if(-s $SSLstage) {
    move($SSLstage,"$SSLconfdir/ssl_bx.conf");
    chmod(0644, "$SSLconfdir/ssl_bx.conf"); # paranoia
    $cce->bye("SUCCESS");
  }
  else {
    $cce->bye("FAILURE");
  }

  #
  #-- Fin: Handle ssl_bx.conf:
  #

  $cce->bye("SUCCESS");
} else {
  $cce->bye("FAILURE");
}

exit(0);

sub debug_msg {
    if ($DEBUG) {
        my $msg = shift;
        $user = $ENV{'USER'};
        setlogsock('unix');
        openlog($0,'','user');
        syslog('info', "$ARGV[0]: $msg");
        closelog;
    }
}

# 
# Copyright (c) 2008-2019 Michael Stauber, SOLARSPEED.NET
# Copyright (c) 2008-2019 Team BlueOnyx, BLUEONYX.IT
# Copyright (c) 2003 Sun Microsystems, Inc. 
# All Rights Reserved.
# 
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright 
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
# 
# 3. Neither the name of the copyright holder nor the names of its 
#   contributors may be used to endorse or promote products derived 
#   from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.
# 
# You acknowledge that this software is not designed or intended for 
# use in the design, construction, operation or maintenance of any 
# nuclear facility.
# 