#
# Copyright (c) 2002, 2005, Oracle. All rights reserved.  
#
#   NAME
#     webconfig
#
#   DESCRIPTION
#     (re-)configure the web server key and certificate
#
#   MODIFIED   (MM/DD/YY) 
#   tdziedzi   05/18/05 - Product name change 
#   tdziedzi   05/18/04 - Executables name change 
#   tdziedzi   03/05/04 - Update vendor/product name, copyright info 
#
# ******************************************************************************
#  Old changes (pre ade migration):
#     09/30/02 jrw  torn from the clutches of webcheck.
#
#  Usage:
#     install/webconfig


#  Say hello:

    echo ""
    echo "Welcome to Oracle Secure Backup's Web server configuration tool."
    echo ""


#  Local initialization:

    onintr done_intr
    setenv OBTRACE 0
    set myargs = ($argv)


#  Arg processing:

    while ($#argv > 0)
        if ("$argv[1]" == "-y") then
            setenv OBTRACE 1
            if (-f trenter) then
                trenter installob $myargs
            else if (-f install/trenter) then
                install/trenter install/installob $myargs
                endif
        else
            echo "Unknown command line option, $argv[1]."
            goto failed
            endif
        shift
        end


#  Run initinstall:

    if (! $?installobinit) then
        if ($?r_testing && -f /ccs/rd/qr/install/initinstall.sh) then
            source /ccs/rd/qr/install/initinstall.sh
            set st = $status
        else if (-f initinstall.sh) then
            source initinstall.sh
            set st = $status
        else if (-f install/initinstall.sh) then
            source install/initinstall.sh
            set st = $status
        else
            echo " "
            echo "Error: no initinstall.sh program exists in either '.' or './install'.  Be"
            echo "       sure to run the Oracle Secure Backup Easy Install program with your current"
            echo "       directory set to the Oracle Secure Backup directory for this host."
            set st = 1
            endif
        if ($st) goto failed
        endif

    source $idir/mymachinfo.sh


#  User must be root:

    whoami |& grep root >&/dev/null
    if ($status) then
        id |& grep "uid=0" >&/dev/null
        if ($status) then
            echo "Error: you must be logged in as 'root' to run this tool."
            goto failed
            endif
        endif


    #  Without an apache directory, we're useless:
    #
    if (! -d apache) then
        echo "Error: no apache sub-directory; can't continue."
        goto failed
        endif


    #  Check the current configuration state:

    if (-z apache/conf/ssl.key/server.key) rm -f apache/conf/ssl.key/server.key >&/dev/null
    if (-z apache/conf/ssl.crt/server.crt) rm -f apache/conf/ssl.crt/server.crt >&/dev/null

    unset guiconfigreqd
    if (! -f apache/conf/ssl.key/server.key || ! -f apache/conf/ssl.crt/server.crt ) set guiconfigreqd


    if (! $?guiconfigreqd) then
        echo    "Oracle Secure Backup's Web server is already configured.  Would you like to change"
        echo -n "the PEM pass phrase or certificate contents [no]? "
        set ayenay_dft = no
        source $idir/ayenay.sh
        if (! $status) then
            echo "" ; echo "Nothing to do; good-bye."
            goto done
            endif
        endif


    rm -f apache/conf/ssl.key/server.key >&/dev/null
    rm -f apache/conf/ssl.crt/server.crt >&/dev/null


    #  Create a random number file for obopenssl to use.
    #
    .bin.$my_mach_arch/hostinfo -z >/tmp/.ob.rand


    while (1)
        if (-z apache/conf/ssl.key/server.key) rm apache/conf/ssl.key/server.key
        if (! -f apache/conf/ssl.key/server.key ) then
            echo ""
            echo "Oracle Secure Backup will use your responses to the following questions to generate"
            echo "a certificate for secure communications between your browser and its Web"
            echo 'server.  (Remember the "PEM pass phrase" you choose; '"you'll"' need it (a) to'
            echo 'set the "daemons/webpass" policy, and (b) each time you manually start the'
            echo 'Web server.)'
            echo ""

            .etc.$my_mach_arch/obopenssl genrsa -des3 -rand /tmp/.ob.rand                   \
                                     -out apache/conf/ssl.key/server.key 1024

            # verify that the server.key file is not 0 size.
            # if it is then remove it.
            if (-z apache/conf/ssl.key/server.key) then
                 echo ""
                 echo "Error: PEM pass phrase mismatch.  Please try again."
                 echo ""
                 rm -rf apache/conf/ssl.key/server.key
                 endif
        else
            break
            endif
        end


    while (1)
        if (! -f apache/conf/ssl.crt/server.crt ) then

            .etc.$my_mach_arch/obopenssl req -new -days 3650 -config apache/conf/openssl.cnf \
                                         -key apache/conf/ssl.key/server.key -x509          \
                                         -out apache/conf/ssl.crt/server.crt
            if (! -f apache/conf/ssl.crt/server.crt ) then
                echo ""
                echo "Error: PEM pass phrase mismatch. Please try again."
                echo ""
                endif
        else
            break
            endif
        end

    set webserver_only
    set report_sd
    unset cmd blk
    source $idir/stopd.sh

    echo ""

    if ($daemons_sd == 0) then
        echo    "Would you like to start the Web server now using the new"
        echo -n "configuration [yes]? "
    else
        echo    "Would you like to restart the Web server now using the new configuration?"
        echo    "Doing so will interrupt any on-going Oracle Secure Backup browser sessions."
        echo -n "Restart the Web server now [yes]? "
        endif

    set ayenay_dft = yes
    source $idir/ayenay.sh
    if ($status == 0) then
        echo ""
        echo "When you're ready to use the new configuration on ${my_mach_name}:"
        echo ' 1. Stop any running "obhttpd" processes.'
        echo ""
        echo " 2. Restart the Web server:"
        echo "        etc/obhttpd -D SSL -d $cwd/apache"
        echo ""
        echo " 3. Use obtool or the Web server to reset your daemons/webpass policy:"
        echo "        bin/obtool setp daemon/webpass [<PEM-pass-phrase>]"
        echo ""
        goto done
        endif

    if ($daemons_sd > 0) then
        echo ""
        echo -n "Stopping $daemons_sd obhttpd processes... "
        unset report_sd
        source $idir/stopd.sh
        echo ""
        endif

    echo "" ; echo "Starting obhttpd; enter the PEM pass phrase when prompted."
    .etc.$my_mach_arch/obhttpd -DSSL -d $cwd/apache

    echo ""
    echo "Use obtool or the Web server to reset your daemons/webpass policy:"
    echo "    bin/obtool setp daemon/webpass [<PEM-pass-phrase>]"

done:
    set st = 0

done_int:
    rm -f /tmp/.ob.rand >&/dev/null
    if ($OBTRACE) $idir/trexit installob $st
    exit ($st)

done_intr:
    set st = 9
    goto done_int

failed:
    set st = 1
    goto done_int
