#!/usr/bin/env bash set -e usage() { echo "usage: $0 [--force] [--exists-ok] EXTERNAL_HOST" >&2 exit 1 } args="$(getopt -o '' --long help,force,exists-ok -- "$@")" eval "set -- $args" while true; do case "$1" in --help) usage;; --force) FORCE=1; shift;; --exists-ok) EXISTS_OK=1; shift;; --) shift; break;; *) usage;; esac done EXTERNAL_HOST="$1" if [ -z "$EXTERNAL_HOST" ]; then usage fi if [ "$EUID" -ne 0 ]; then echo "error: this script must be run as root" >&2 exit 1 fi set -x is_redhat=false if [ -e /etc/redhat-release ]; then is_redhat=true SSLDIR=/etc/pki/tls else SSLDIR=/etc/ssl fi KEYFILE=$SSLDIR/private/zulip.key CERTFILE=$SSLDIR/certs/zulip.combined-chain.crt if [ -n "$EXISTS_OK" ] && [ -e "$KEYFILE" ] && [ -e "$CERTFILE" ]; then exit 0 fi if [ -z "$FORCE" ] && { [ -e "$KEYFILE" ] || [ -e "$CERTFILE" ]; }; then echo "$0: certificate and/or key already exists; use --force to overwrite." >&2 exit 1 fi rm -f "$KEYFILE" "$CERTFILE" if [[ "$EXTERNAL_HOST" =~ ^(([0-9]+\.){3}[0-9]+)(:[0-9]+)?$ ]]; then subjectAltName="IP:${BASH_REMATCH[1]}" # IPv4 address elif [[ "$EXTERNAL_HOST" =~ ^\[([^][]*)\](:[0-9]+)?$ ]]; then subjectAltName="IP:${BASH_REMATCH[1]}" # IPv6 address elif [[ "$EXTERNAL_HOST" =~ ^([^:]+)(:[0-9]+)?$ ]]; then subjectAltName="DNS:${BASH_REMATCH[1]}" else echo "$0: invalid host $EXTERNAL_HOST" >&2 exit 1 fi config="$(mktemp)" || exit 1 trap 'rm -f "$config"' EXIT cat >"$config" <