wagon/back/lib/dashboard/ssl

55 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# FILE: dashboard/ssl
# DESCRIPTION: Get a user's SSL certs and keys
# USAGE: ssl remote_ip querystring
# QUERYSTRING: ?host=$hostname&type=cert
source /etc/wagon/config
ip="${1}"; qs="$(<<<"${2}" tr '&' '\n' | sed 's/?//')"
# Parse querystring
hostname="$(<<<"${qs}" grep -oP 'host=(.*)' | sed 's/^host=//' | xargs)"
type="$(<<<"${qs}" grep -oP 'type=(.*)' | sed 's/^type=//' | xargs)"
# Make sure type is 'cert' or 'key'
if [ "${type}" != 'cert' ] && [ "${type}" != 'key' ]; then
printf 'Invalid type: %s\n' "${type}" | tee >(cat 1>&2) | /usr/lib/wagon/http_res 400; exit
fi
if ! file="${hostname:?}/${type:?}.pem"; then
printf 'ERROR! Hostname "%s" or type "%s" missing!\n' "${hostname}" "${type}" >&2
printf 'Hostname or type missing!\n' | /usr/lib/wagon/http_res 400; exit
else
printf 'User %s requested SSL file %s\n' "${ip}" "${file}" >&2
fi
# Get username
if ! domain="$(/usr/lib/wagon/ns_lookup_rdns "${ip}")"; then
printf 'ERROR! Failed to lookup domain from user IP %s\n' "${ip}" | tee >(cat 1>&2) | /usr/lib/wagon/http_res 500
exit 7
fi; if ! username="$(<<<"${domain}" cut -d'.' -f2)"; then
printf 'ERROR! Failed to parse username from domain "%s"\n' "${domain}" >&2
/usr/lib/wagon/http_res 500; exit
else
printf 'User %s is "%s"\n' "${ip}" "${username}" >&2
fi
if ! path="${SSL_CONFIG_DIR:?}/${username:?}/${file}"; then
printf 'ERROR! Username "%s" or SSL_CONFIG_DIR "%s" missing!\n' "${username}" "${SSL_CONFIG_DIR}" >&2
/usr/lib/wagon/http_res 500; exit
fi
# Check that the file exists
if ! [ -f "${path}" ]; then
printf 'ERROR! File missing: "%s"\n' "${path}" >&2
/usr/lib/wagon/http_res 500; exit
fi
# Try to return it to the user
if <"${path}" /usr/lib/wagon/http_res 200; then
printf 'Sent SSL file "%s" to %s\n' "${path}" "${username}" >&2
else
printf 'ERROR! Failed to return file: "%s"\n' "${path}" >&2
/usr/lib/wagon/http_res 500; exit
fi