#!/bin/bash

###########################################################################
#
# MODULE:       GUI
# COPYRIGHT:    (C) 2009-2025 by CacheGuard Technologies Ltd (UK)
# COPYRIGHT:    (C) 2026-2026 by CacheGuard Technologies SAS (FR)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

download-tls()
{
    test -n "${1}" || return 1
    test -n "${2}" || return 2
    test -n "${3}" || return 3
    local tls=${1}
    local component=${2}
    local filename=${3}

    local file=$(get-tls-server-component-file ${tls} ${component})
    test -f ${file} || return 11

    local size=$(get-characters-in-file ${file})

    echo "Content-Disposition: attachment; filename=\"${filename}\""
    echo "Content-Length: ${size}"
    echo "Cache-control: private"

    echo-http-header

    cat ${file}
}

set-tls-server-manage()
{
    local tls="${VALUES[0]}"
    local component="${VALUES[1]}"
    local operation="${VALUES[2]}"

    local protocol filename

    case ${operation} in
	load|save)
	    protocol="${VALUES[3]:1}"
	    local server=${VALUES[4]}
	    filename=${VALUES[5]}

	    if test -n "${filename}" ; then
		if test "${component}" == 'csr' -a "${operation}" == 'load' -a -n "${VALUES[6]}" ; then
		    if test -z "${VALUES[7]}" ; then
			log-command "tls" "server ${operation} ${tls} ${component} ${protocol} ${server} ${filename} ${VALUES[6]}"
			execute-command "tls server ${operation} ${tls} ${component} ${protocol} ${server} ${filename} ${VALUES[6]}"
		    else
			log-command "tls" "server ${operation} ${tls} ${component} ${protocol} ${server} ${filename} ${VALUES[6]} ocsp"
			execute-command "tls server ${operation} ${tls} ${component} ${protocol} ${server} ${filename} ${VALUES[6]} ocsp"
		    fi
		else
		    log-command "tls" "server ${operation} ${tls} ${component} ${protocol} ${server} ${filename}"
		    execute-command "tls server ${operation} ${tls} ${component} ${protocol} ${server} ${filename}"
		fi
	    else
		gen-gui-error 6
	    fi
	    ;;

	upload)

	    filename="${VALUES[4]}"

	    if test -z "${filename}" ; then
		gen-gui-error 6
	    else
		local uploaded_filename=${UPLOADED_FILES[0]}

		if test -n "${uploaded_filename}" -a -f ${ADMIN_TMP_DIR}/${uploaded_filename} ; then
		    if test "${component}" == 'csr' -a -n "${VALUES[5]}" ; then

			local days=${VALUES[5]}
			local ocsp=${VALUES[6]}

			if test -z "${ocsp}" ; then
			    log-command "tls" "server inject ${tls} ${component} ${filename} ${days}"
			    execute-command "tls server inject ${tls} ${component} ${uploaded_filename} ${days}"
			else
			    log-command "tls" "server inject ${tls} ${component} ${filename} ${days} ocsp"
			    execute-command "tls server inject ${tls} ${component} ${uploaded_filename} ${days} ocsp"
			fi
		    else
			log-command "tls" "server inject ${tls} ${component} ${filename}"
			execute-command "tls server inject ${tls} ${component} ${uploaded_filename}"
		    fi

		    rm -f ${ADMIN_TMP_DIR}/${uploaded_filename}
		else
		    rm -f ${ADMIN_TMP_DIR}/${uploaded_filename}
		    gen-gui-error 16
		fi
	    fi
	    ;;

	download)
	    protocol="${VALUES[3]:1}"
	    test "${protocol}" == 'web' || exit 0
	    filename=${VALUES[4]}
	    test -n "${filename}" || filename=${tls}.${component}
	    download-tls ${tls} ${component} ${filename}
	    reset-gui-error-log
	    exit 0 # important
	    ;;
	*)
	    return 255
	    ;;
    esac
}

set-tls-server-manage
