#!/usr/bin/sh
NGINX_CFG_DIR="/etc/nginx/conf.d"
UPSTREAM_CFG="s3_upstream.conf"
UPSTREAM="ostor_s3"
OSTOR_S3_CFG="ostor-s3.conf"
DOMAIN=""
HTTP_PORT="8080"
OPEN_SSL="/usr/bin/openssl"
SSL_PORT="443"
PORT=$HTTP_PORT
REPLACE=""
CFG_SVC=""
MAX_KEEPALIVE=32
KEEPALIVE=""
KEEPALIVE_OPTS="fastcgi_keep_conn on;"
VECTORS=""
VECTORS_API="s3vectors"
VECTORS_PREFIX="s3vectors"
VECTORS_NAME=""
# When set, build_cfg emits these listen directives verbatim instead of
# deriving one from $PORT. Used by enable-vectors, which must reproduce an
# existing block's listeners exactly - a dual-stack block has two of them and
# keeping only one silently drops a whole address family.
LISTEN_LINES=""
# Set when the source block carries no OSTOR_CFG_NAME, so the added block
# does not gain a parameter its neighbour does not have.
NO_CFG_NAME=""
# s3vectors bodies are JSON batches of vectors, not objects: a far smaller
# limit than the S3 one, and it must not exceed what the gateway validates
# against the AWS per-request limits.
VECTORS_MAX_BODY="64m"
MAX_BODY="5g"
# With the s3vectors block a listen address carries two server blocks, and
# nginx then hashes their names. Its default bucket, one cache line (64),
# holds names of at most 46 characters; 512 holds any DNS name.
BUCKET_DIRECTIVE="server_names_hash_bucket_size"
BUCKET_SIZE=512
# What to do about it in the file being written: append, raise, drop or keep.
BUCKET_ACTION="keep"
# RFC 1035 2.3.4: a label is at most 63 octets, a name at most 255 on the
# wire, which is 253 characters in the text form without the trailing dot.
DNS_NAME_MAX=253
DNS_LABEL_MAX=63
NGINX="/usr/sbin/nginx"
# The directory the system nginx loads; nginx -t says something about the
# files written only when they are written there.
NGINX_SYSTEM_CFG_DIR="/etc/nginx/conf.d"
NO_TEST=""
# Temporary files to remove if the script stops early; ones already moved into
# place no longer exist under these names.
CLEANUP=""
ARGS="${@}"

function print_usage {
        exec 1>&2
	echo "Usage:"
	echo -e \
	     " ostor-configure-nginx COMMAND [-D,--domain DOMAIN_NAME] [-n,--name HOST_NAME ]\n" \
	     "                              [-r,--replace] [-C,--cfg-dir DIR] [-K,--keepalive]\n" \
	     "                              [-V,--vectors] [-p PORT] [--no-test] [-h,--help]"
	echo "Commands:"
	echo "  create                    Create new configuration."
	echo "  update                    Update existing upstream configuration. It should be called"
	echo "                            when configuration of S3 gateways was changed."
	echo "  enable-vectors            Append the s3vectors server block to an existing"
	echo "                            configuration, leaving the S3 one untouched. Also sets"
	echo "                            server_names_hash_bucket_size $BUCKET_SIZE, which two server"
	echo "                            blocks on one address need for long names - unless"
	echo "                            another file sets it; below $BUCKET_SIZE there, it warns,"
	echo "                            and refuses if nginx -t cannot check the result."
	echo "                            On a configuration that has the block, adds only that."
	echo "  disable-vectors           Remove the s3vectors server block, leaving the S3 one"
	echo "                            untouched. Required before downgrading the package."
	echo "                            server_names_hash_bucket_size stays."
        echo "Options:"
	echo -e "  -n,--name   HOST_NAME     The hostname of configuration service. It should be specified\n"\
	     "                           only if this host does not present in configuration."
	echo "  -D,--domain DOMAIN_NAME   The domain name of your object storage."
	echo "  -p,--port   PORT          The listening port, default is 8080."
	echo "  -r,--replace              Replace existing configuration on create instead of fail."
	echo "  -K,--keepalive            Enable keepalive FCGI connections, disabled by default."
	echo -e "  -V,--vectors              Also serve the s3vectors API on ${VECTORS_PREFIX}.DOMAIN_NAME,\n"\
	     "                           with server_names_hash_bucket_size as enable-vectors sets\n"\
	     "                           it. Both names must be valid DNS names. Disabled by default."
	echo -e "  --vectors-name NAME       Serve the s3vectors API on NAME instead of\n"\
	     "                           ${VECTORS_PREFIX}.DOMAIN_NAME. NAME is the FULL hostname,\n"\
	     "                           not a prefix. Useful when an existing wildcard\n"\
	     "                           certificate covers a sibling name but not a nested one.\n"\
	     "                           Implies -V,--vectors."
	echo "  -C,--cfg-dir DIR          Directory where NGINX's configuration files are located."
	echo -e "  --no-test                 Do not check the result with nginx -t. Without it, create,\n"\
	     "                           enable-vectors and disable-vectors check it and restore\n"\
	     "                           the previous configuration if nginx rejects it - when DIR\n"\
	     "                           is the one the system nginx loads, and nginx accepted the\n"\
	     "                           configuration before the change."
	echo "  -S,--ssl                  Turn on SSL."
	echo "  --ssl-cert FILE           Path to file with SSL certificate."
	echo "  --ssl-key  FILE           Path to file with SSL key."
	echo "  -h,--help                 Print this help."
	echo "Exit status:"
	echo "  0    done, or nothing to do"
	echo "  1-3  invalid arguments, names, certificate or existing configuration, or"
	echo "       the cluster configuration or a temporary file could not be read or"
	echo "       written; nothing was changed"
	echo "  4    cannot install the files; what was there before is back"
	echo "  5    nginx -t rejects the result; what was there before is back"
	echo "  6    as 4 or 5, but what was there before could not all be put back;"
	echo "       the messages say where the saved copies are"
	echo "  130  stopped by INT, QUIT, HUP or TERM before installing anything;"
	echo "       nothing was changed. Once installing the files has begun, these and"
	echo "       PIPE are ignored until the command finishes with one of the above."
        echo " "
        exit 1
}

function parse_args {

	CMD="${1}"
	[ -z "$CMD" ] && print_usage

	if [ "x$CMD" != "xcreate" -a "x$CMD" != "xupdate" \
	     -a "x$CMD" != "xenable-vectors" -a "x$CMD" != "xdisable-vectors" ]; then
		echo "Unknown command: ${1}" 1>&2
		exit 1
	fi
	shift
        while [ "${#}" -gt 0 ]; do
                case "${1}" in
			"-S"|"--ssl")
				USE_SSL="1"
				shift
				;;
			"--ssl-cert")
				[ -z "${2}" ] && print_usage
				SSL_CERT_FILE="${2}"
				shift
				shift
				;;
			"--ssl-key")
				[ -z "${2}" ] && print_usage
				SSL_KEY_FILE="${2}"
				shift
				shift
				;;
			"-K"|"--keepalive")
				KEEPALIVE="1"
				shift
				;;
			"--no-test")
				NO_TEST="1"
				shift
				;;
			"-V"|"--vectors")
				VECTORS="1"
				shift
				;;
			"--vectors-name")
				[ -z "${2}" ] && print_usage
				VECTORS_NAME="${2}"
				VECTORS="1"
				shift
				shift
				;;
			"-n"|"--name")
				[ -z "${2}" ] && print_usage
				CFG_SVC="${2}"
				shift
				shift
				;;
			"-r"|"--replace")
				REPLACE="${1}"
				shift
				;;
			"-C"|"--cfg-dir")
				[ -z "${2}" ] && print_usage
				NGINX_CFG_DIR="${2}"
				shift
				shift
				;;	
                        "-D"|"--domain")
				[ -z "${2}" ] && print_usage
				DOMAIN="${2}"
                                shift
				shift
                                ;;
			"-p"|"--port")
				[ -z "${2}" ] && print_usage
                                PORT="${2}"
				shift
				shift
				printf '%d' "$PORT" >/dev/null 2>&1
				if [ $? -ne 0 ] ; then
					echo "Argument of -p,--port must be a number" 1>&2
					exit 1
				fi
                                ;;
                        "-h"|"--help")
                                print_usage
                                ;;
                        *)
                                echo "Unknown option '${1}'." 1>&2
                                print_usage
                                ;;
                esac
        done

	# The vectors options only shape a server block, and update/disable-vectors
	# write no server block at all. Accepting them there silently did nothing
	# and still reported success, which reads as "the block was added".
	if [ -n "$VECTORS" ] \
	   && [ "x$CMD" != "xcreate" -a "x$CMD" != "xenable-vectors" ]; then
		echo "-V,--vectors and --vectors-name apply to 'create' and 'enable-vectors' only." 1>&2
		echo "To add the s3vectors block to an existing configuration run:" 1>&2
		echo "  ostor-configure-nginx enable-vectors [--vectors-name NAME]" 1>&2
		exit 1
	fi

	# Likewise: update rewrites only the upstream and checks nothing, so
	# accepting --no-test there would claim a check was skipped.
	if [ -n "$NO_TEST" ] && [ "x$CMD" = "xupdate" ]; then
		echo "--no-test applies to 'create', 'enable-vectors' and 'disable-vectors' only." 1>&2
		exit 1
	fi

	if [ "x$CMD" = "xcreate" ]; then
		if [ -z "$DOMAIN" ] ; then
			echo "Option -D,--domain should be specified." 1>&2
			exit 1
		fi

		if [ -n "$USE_SSL" ]; then
			if [ -z "$SSL_CERT_FILE" ]; then
				echo "Path to SSL certificate file (--ssl-cert) should be specified." 1>&2
				exit 1
			elif [ ! -f "$SSL_CERT_FILE" ]; then
				echo "Can't find file $SSL_CERT_FILE" 1>&2
				exit 1
			fi

			if [ -z "$SSL_KEY_FILE" ]; then
				echo "Path to SSL key file (--ssl-key) should be specified." 1>&2
				exit 1
			elif [ ! -f "$SSL_KEY_FILE" ]; then
				echo "Can't find file $SSL_KEY_FILE" 1>&2
				exit 1
			fi

			[ "x$HTTP_PORT" = "x$PORT" ] && PORT=$SSL_PORT
		fi
	fi

	if [ -z "$CFG_SVC" ]; then
		if [ ! -f "/var/lib/ostor/name" ]; then
			echo "Option -n,--name should be specified." 1>&2
			exit 1
		else
			CFG_SVC=$(cat /var/lib/ostor/name 2>/dev/null | head -n 1)
			if [ -z "$CFG_SVC" ] ; then
				echo "Can't read name of configuration service." 1>&2
				exit 1
			fi	
		fi
	fi
}

# The hostname the s3vectors block answers to. Defaults to a child of the S3
# domain; --vectors-name overrides it, which is what an installation whose
# wildcard certificate sits one level higher needs (*.example.com covers
# s3vectors.example.com but not s3vectors.s3.example.com).
vectors_name() {
	if [ -n "$VECTORS_NAME" ]; then
		echo "$VECTORS_NAME"
	else
		echo "$VECTORS_PREFIX.$DOMAIN"
	fi
}

# Labels of 1 to DNS_LABEL_MAX characters, DNS_NAME_MAX in all. An empty
# label - "a..b", a leading or a trailing dot - never matches a Host.
dns_name_fits() {
	local name="$1" rest label
	[ -n "$name" ] && [ "${#name}" -le $DNS_NAME_MAX ] || return 1
	rest="$name."
	while [ -n "$rest" ]; do
		label="${rest%%.*}"
		rest="${rest#*.}"
		[ -n "$label" ] && [ "${#label}" -le $DNS_LABEL_MAX ] || return 1
	done
}

test_vectors_name() {
	[ -z "$VECTORS" ] && return
	if [ "x$(vectors_name)" = "x$DOMAIN" ]; then
		echo "The s3vectors hostname must differ from the S3 one ($DOMAIN)." 1>&2
		exit 1
	fi

	# Both names go into the hash, so both have to fit its bucket. The
	# bucket is sized for DNS names; alone, the S3 block builds no hash and
	# takes a name of any length, which is why this is checked here only.
	local name
	for name in "$DOMAIN" "$(vectors_name)"; do
		dns_name_fits "$name" && continue
		echo "$name is not a valid DNS name: it has ${#name} characters, a name" \
		     "may have at most $DNS_NAME_MAX, in labels of 1 to $DNS_LABEL_MAX." 1>&2
		exit 1
	done
}

# Does $SSL_CERT_FILE cover the hostname $1? subjectAltName is what every
# current client actually checks; CN is kept as a fallback for certificates
# issued before SAN became mandatory. A wildcard SAN covers a single label,
# which is exactly the shape of s3vectors.<domain>.
cert_has_name() {
	local name="$1"
	local wild="*.${name#*.}"

	$OPEN_SSL x509 -noout -ext subjectAltName -in $SSL_CERT_FILE 2>/dev/null \
		| tr ',' '\n' | sed -e 's/^[[:space:]]*DNS://' -e 's/[[:space:]]*$//' \
		| grep -Fxq -e "$name" -e "$wild" && return 0

	# openssl renders the subject as "CN=x" or "CN = x" depending on version.
	$OPEN_SSL x509 -noout -subject -in $SSL_CERT_FILE 2>/dev/null \
		| tr -d '[:space:]' | grep -Fq "CN=$name" && return 0

	return 1
}

test_ssl_cert() {
	[ ! -x $OPEN_SSL -o -z "$USE_SSL" ] && return

	subj=$($OPEN_SSL x509 -noout -subject -in $SSL_CERT_FILE)
	if [ $? -ne 0 ]; then
		echo "Can't fetch subject from certificate $SSL_CERT_FILE" 1>&2
		exit 1
	fi
	if ! cert_has_name "$DOMAIN"; then
		echo "Certificate $SSL_CERT_FILE does not cover $DOMAIN" 1>&2
		echo "Actual value: $subj"
		exit 1
	fi

	# The s3vectors block is served under its own hostname, so a certificate
	# that only covers the S3 domain would break the vectors endpoint alone -
	# with the S3 one still working, which makes it easy to miss.
	if [ -n "$VECTORS" ] && ! cert_has_name "$(vectors_name)"; then
		echo "Certificate $SSL_CERT_FILE does not cover $(vectors_name)" 1>&2
		echo "Add it to subjectAltName, or drop -V,--vectors." 1>&2
		exit 1
	fi
}

get_addrs() {
	local host_id="$1"
	local args=""
	[ -n "$CFG_SVC" ] && args="-n $CFG_SVC"
	/usr/bin/ostor-ctl get-config $args -S | grep "S3GW .*$host_id" 2>/dev/null | sed -e "s#.*address=\(.*\)#\1#" | cut -d '&' -f1
}

build_upstream() {
	local host_id="$1"
	local name="$2"
	local addrs="$3"

	echo "# upstream configuration for $CFG_SVC"
	echo "upstream $name {"
	echo "$addrs" | while read a ;do
		echo "	server $a;"
	done

	[ -n "$KEEPALIVE" ] && echo "	keepalive $MAX_KEEPALIVE;"
	echo "}"
	return 0
}

build_cfg() {
	local server="$1"
	local port="$2"
	local upstream="$3"
	local api="$4"
	local max_body="$5"
	[ -z "$max_body" ] && max_body="$MAX_BODY"
	local listen_opts=""
	# "ssl on;" was removed in nginx 1.25; the listen parameter is the only
	# form that works everywhere, and it is also the one the endpoint parser
	# in go/account_control/lifecycle looks for when deciding http vs https.
	[ -n "$USE_SSL" ] && listen_opts=" ssl"
	echo "# configuration for object storage '$CFG_SVC'"
	if [ -n "$LISTEN_LINES" ]; then
	echo "server {"
	echo "$LISTEN_LINES"
	else
	echo -e \
	"server {\n" \
	"	listen $port$listen_opts;"
	fi
	if [ -n "$USE_SSL" ]; then
	echo -e \
	"	ssl_protocols  TLSv1.2 TLSv1.3;\n" \
	"	ssl_certificate $SSL_CERT_FILE;\n" \
	"	ssl_certificate_key $SSL_KEY_FILE;\n" \
	"	ssl_ciphers  HIGH:!aNULL:!MD5;\n" \
	"	ssl_prefer_server_ciphers   on;"
	fi	
	echo -e \
	"	server_name $server;\n" \
	"	client_max_body_size $max_body;\n" \
	"	location / {\n" \
	"		fastcgi_pass_header Connection-close;\n" \
	"		fastcgi_pass $upstream;\n" \
	"		fastcgi_no_cache 1;\n"	\
	"		include fastcgi_params;\n" \
	"		fastcgi_buffering off;\n" \
	"		fastcgi_request_buffering off;\n" \
	"		fastcgi_max_temp_file_size 0; "
	# Mirror the source block: a configuration that names no object storage
	# (the UI agent writes none) must not gain the parameter in one block
	# only, which would configure the two differently for no reason.
	# The leading space is what "echo -e" used to insert when this line was
	# the last argument of the chain above; keep it so the generated file is
	# unchanged byte for byte.
	[ -z "$NO_CFG_NAME" ] && echo \
	" 		fastcgi_param OSTOR_CFG_NAME $CFG_SVC;"
	[ -n "$api" ] && echo \
	"		fastcgi_param OSTOR_API $api;"
	[ -n "$USE_SSL" ] && echo \
	"		fastcgi_param SSL on;"
	[ -n "$KEEPALIVE" ] && 	echo \
	"		$KEEPALIVE_OPTS"
	echo -e \
	"	}\n" \
	"}\n"
}

build_vectors_cfg() {
	build_cfg "$(vectors_name)" "$PORT" "$UPSTREAM" \
		  "$VECTORS_API" "$VECTORS_MAX_BODY"
}

build_cfg_all() {
	build_cfg "$DOMAIN" "$PORT" "$UPSTREAM"
	[ -n "$VECTORS" ] && build_vectors_cfg
	return 0
}

# A size as nginx reads it: bytes, or with a k or m suffix. 0 for anything
# else, which then reads as too small.
size_bytes() {
	local n="${1%[kKmM]}"
	case "$n" in
	''|*[!0-9]*) echo 0; return ;;
	esac
	case "$1" in
	*[kK]) echo $((n * 1024)) ;;
	*[mM]) echo $((n * 1048576)) ;;
	*) echo "$n" ;;
	esac
}

# The bucket size $1 sets, if it sets one. Read in tokens and statements the
# way nginx reads them, so a directive sharing its line with others - "http {
# ...; ...;" - or split over several lines is found too: a "#" starts a
# comment only where a token would start and outside quotes, and ";", "{" and
# "}" outside quotes end a statement. A "#" inside a token or a quoted string -
# a log_format, say - is part of it.
bucket_in() {
	awk -v d="$BUCKET_DIRECTIVE" '
	function word() { if (tok != "") { w[++n] = tok; tok = "" } }
	function statement() {
		word()
		if (n >= 2 && w[1] == d) { print w[2]; exit }
		n = 0
	}
	{
		for (i = 1; i <= length($0); i++) {
			c = substr($0, i, 1)
			if (q != "") {
				if (c == "\\") { tok = tok c substr($0, ++i, 1); continue }
				if (c == q) q = ""; else tok = tok c
				continue
			}
			if (c == " " || c == "\t" || c == "\r") { word(); continue }
			if (tok == "" && c == "#") break
			if (tok == "" && (c == "\"" || c == "\047")) { q = c; continue }
			if (c == ";" || c == "{" || c == "}") { statement(); continue }
			tok = tok c
		}
		if (q == "") word()
	}' "$1"
}

# "FILE VALUE" for the first place other than $1 that sets the bucket size:
# nginx.conf next to the configuration directory, or another file in it. An
# http-level directive comes from one of those on this layout; whatever this
# misses, nginx -t still catches.
bucket_elsewhere() {
	local own="$1" f v
	for f in "$(dirname "$NGINX_CFG_DIR")/nginx.conf" "$NGINX_CFG_DIR"/*.conf; do
		[ -f "$f" ] && [ "x$f" != "x$own" ] || continue
		v=$(bucket_in "$f")
		[ -n "$v" ] && echo "$f $v" && return
	done
}

# Set BUCKET_ACTION for a configuration with the s3vectors block. $1 is the
# file it is made from, empty when it is made from scratch. The directive is
# allowed once in http, so when another file sets it this one must not, and
# it is not this script's to raise there. Below our size it may still be
# enough for the names at hand; nginx -t is what knows, not this script, so
# when nginx -t cannot be asked about a block being added, that is a refusal
# - short of --no-test, which is the operator taking it on. $2 is set when
# the block is being added; for one already there the names are unchanged
# and refusing would only break re-running the command. A value above ours
# is someone's choice and stays.
decide_bucket() {
	local src="$1" adding="$2" other file value
	other=$(bucket_elsewhere "$NGINX_CFG_DIR/$OSTOR_S3_CFG")
	if [ -n "$other" ]; then
		file="${other% *}"
		value="${other##* }"
		if [ "$(size_bytes "$value")" -lt $BUCKET_SIZE ]; then
			echo "Warning: $BUCKET_DIRECTIVE is $value in $file, below the" \
			     "$BUCKET_SIZE the s3vectors block is sized for. Not setting it" \
			     "here as well, it may be set only once." 1>&2
			[ -n "$adding" ] && nginx_baseline
			if [ -n "$adding" ] && [ -z "$NGINX_CHECK" ] && [ -z "$NO_TEST" ]; then
				echo "Refusing: nginx -t cannot tell whether the names fit" \
				     "($NGINX_NO_CHECK). Raise it in $file, or pass --no-test." 1>&2
				exit 1
			fi
		fi
		BUCKET_ACTION="keep"
		[ -n "$src" ] && [ -n "$(bucket_in "$src")" ] && BUCKET_ACTION="drop"
		return
	fi

	value=""
	[ -n "$src" ] && value=$(bucket_in "$src")
	if [ -z "$value" ]; then
		BUCKET_ACTION="append"
	elif [ "$(size_bytes "$value")" -lt $BUCKET_SIZE ]; then
		BUCKET_ACTION="raise"
	else
		BUCKET_ACTION="keep"
	fi
}

bucket_comment() {
	echo "# Needed while a listen address carries more than one server block, as"
	echo "# it does with s3vectors: nginx then hashes their names, and its default"
	echo "# bucket holds names of at most 46 characters. Harmless with one block."
	echo "# Allowed once in http."
}

# Filter a configuration through BUCKET_ACTION. Appended at the end, so always
# after the S3 block, which is what reads this file back looks for first. It
# is left in place when the s3vectors block is removed - harmless with one
# block - and a later enable-vectors appends the block after it. Dropping it
# takes the comment written with it too.
apply_bucket() {
	local stmt="$BUCKET_DIRECTIVE[[:space:]][^;]*;"
	case "$BUCKET_ACTION" in
	append)
		cat
		bucket_comment
		echo "$BUCKET_DIRECTIVE $BUCKET_SIZE;"
		;;
	raise)
		sed "s/\(^\|[;{}[:space:]]\)$stmt/\1$BUCKET_DIRECTIVE $BUCKET_SIZE;/"
		;;
	drop)
		awk -v c="$(bucket_comment)" '
		BEGIN { n = split(c, lines, "\n"); for (i = 1; i <= n; i++) ours[lines[i]] = 1 }
		!($0 in ours)
		' | sed -e "/^[[:space:]]*$stmt[[:space:]]*\$/d" -e "s/\(^\|[;{}[:space:]]\)$stmt/\1/"
		;;
	*)
		cat
		;;
	esac
}

# Why nginx -t can say nothing about the files written, if it cannot.
nginx_untestable() {
	if [ -n "$NO_TEST" ]; then
		echo "--no-test given"
	elif [ ! -x "$NGINX" ]; then
		echo "$NGINX is not installed"
	elif [ "x$(readlink -f "$NGINX_CFG_DIR")" != "x$(readlink -f "$NGINX_SYSTEM_CFG_DIR")" ]; then
		echo "$NGINX_CFG_DIR is not the directory $NGINX loads"
	fi
}

# Ask nginx about the configuration as it is before the change, once, and
# set NGINX_CHECK when it is worth asking again after: only then can nginx -t
# tell anything about the change. Otherwise NGINX_NO_CHECK says why. nginx -t
# checks the whole configuration, not only this script's files, and one it
# already rejects for a reason of its own says nothing about this change;
# refusing on it would also stop this script from removing a block that is
# itself the problem.
nginx_baseline() {
	[ -n "$NGINX_BASELINE_DONE" ] && return
	NGINX_BASELINE_DONE="1"
	NGINX_CHECK=""
	NGINX_NO_CHECK=$(nginx_untestable)
	[ -n "$NGINX_NO_CHECK" ] && return

	local out
	if out=$($NGINX -t 2>&1); then
		NGINX_CHECK="1"
		return
	fi
	NGINX_NO_CHECK="it rejects the configuration already"
	echo "nginx -t rejects the configuration before this change:" 1>&2
	echo "$out" 1>&2
}

# Copy $1 aside to restore if nginx rejects what replaces it. Prints the
# copy's name, nothing when there is no $1 yet. Not *.conf, so nginx never
# loads it.
save_prev() {
	[ -f "$1" ] || return 0
	local prev
	prev=$(mktemp "$1.XXXXXXXXXX") || return 1
	if ! cp -p "$1" "$prev"; then
		rm -f "$prev"
		return 1
	fi
	echo "$prev"
}

# Undo putting a new $1 in place: the saved $2 back, or no file if it was new.
# Says so when that fails, and where the saved copy is.
restore_prev() {
	if [ -n "$2" ]; then
		mv -f "$2" "$1" && return 0
		echo "Unable to restore $1, its previous version is in $2." 1>&2
	else
		rm -f "$1" && return 0
		echo "Unable to remove $1." 1>&2
	fi
	return 1
}

# Create a temporary file next to $1, named in TMP and removed if the script
# stops before it is moved into place. The name is recorded before the file
# exists, so no moment has a file the cleanup does not know of - which
# mktemp cannot give: killed by the Ctrl-C that reaches it too, it may have
# made the file without telling its name. Made with O_EXCL and mode 0600,
# like mktemp's; not *.conf, so nginx never loads it. A name that could not
# be made leaves the list again: a file of that name is someone else's.
make_tmp() {
	local i
	for i in 1 2 3 4 5 6 7 8; do
		TMP="$1.$$.$RANDOM$RANDOM"
		CLEANUP="$CLEANUP $TMP"
		( umask 077; set -C; : > "$TMP" ) 2>/dev/null && return 0
		CLEANUP=${CLEANUP% "$TMP"}
	done
	return 1
}

# raise and drop edit the directive where it stands on one line; one split
# over several lines is beyond them, and the file comes out unchanged. Did
# BUCKET_ACTION take effect in $1?
bucket_applied() {
	local v
	v=$(bucket_in "$1")
	case "$BUCKET_ACTION" in
	raise) [ "$(size_bytes "$v")" -ge $BUCKET_SIZE ] ;;
	drop) [ -z "$v" ] ;;
	*) return 0 ;;
	esac
}

# Write what "$2 $1" prints to a new temporary file next to $1, named in TMP,
# with $1's mode.
write_tmp() {
	if ! make_tmp "$1"; then
		echo "Unable to create temporary file." 1>&2
		exit 1
	fi
	"$2" "$1" | dd of="$TMP" conv=fsync >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "Can't save new configuration in $TMP" 1>&2
		rm -f "$TMP"
		exit 2
	fi
	if ! bucket_applied "$TMP"; then
		echo "Unable to $BUCKET_ACTION $BUCKET_DIRECTIVE where it stands in $1." \
		     "Edit it by hand, or put it on a line of its own." 1>&2
		rm -f "$TMP"
		exit 1
	fi
	chmod --reference="$1" "$TMP" 2>/dev/null
}

# Move finished temporary files over their destinations - $2 over $1, then
# $4 over $3 when given - and have nginx check the result. Returns 0 once
# installed. Otherwise every destination is what it was, or absent if it was
# new, no temporary file is left, and the script exits: 4 when a file could
# not be moved, 5 when nginx rejects the result, 6 when what was there before
# could not all be put back.
#
# Signals are ignored from the first backup on, and the script finishes and
# exits as it would have: stopped half way, it would leave a configuration
# nobody checked, or two files out of step. Ignored rather than held, because
# only an ignored signal is ignored by cp, mv and nginx -t as well; a Ctrl-C
# or Ctrl-\ reaches them too, and an nginx -t killed by QUIT reads as a
# rejection that rolls a good change back. PIPE among them: a message to a
# closed stderr would otherwise stop the script between a move and its
# restore.
put_in_place() {
	local dst="$1" tmp="$2" dst2="$3" tmp2="$4" prev="" prev2="" out moved=""
	nginx_baseline
	[ -z "$NGINX_CHECK" ] && [ -z "$NO_TEST" ] && \
		echo "Not checked with nginx -t: $NGINX_NO_CHECK." 1>&2

	trap '' INT QUIT HUP TERM PIPE
	prev=$(save_prev "$dst") && prev2=$(save_prev "$dst2")
	if [ $? -ne 0 ]; then
		echo "Unable to back up the current configuration." 1>&2
		rm -f "$tmp" "$tmp2" $prev
		exit 4
	fi

	# Only what was moved is put back: a destination the move did not reach
	# is unchanged, and the move that failed on it would likely fail again.
	if ! mv -f "$tmp" "$dst" || { moved="1"; [ -n "$dst2" ] && ! mv -f "$tmp2" "$dst2"; }; then
		echo "Unable to install the new configuration." 1>&2
		rm -f "$tmp" "$tmp2" $prev2
		if [ -z "$moved" ]; then
			rm -f $prev
		elif ! restore_prev "$dst" "$prev"; then
			exit 6
		fi
		echo "Previous configuration restored." 1>&2
		exit 4
	fi

	if [ -n "$NGINX_CHECK" ] && ! out=$($NGINX -t 2>&1); then
		echo "nginx rejects the configuration:" 1>&2
		echo "$out" 1>&2
		local restored="1"
		restore_prev "$dst" "$prev" || restored=""
		if [ -n "$dst2" ]; then
			restore_prev "$dst2" "$prev2" || restored=""
		fi
		[ -z "$restored" ] && exit 6
		echo "Previous configuration restored." 1>&2
		exit 5
	fi
	rm -f $prev $prev2
	return 0
}

do_update() {
	local upstream_file="$NGINX_CFG_DIR/$UPSTREAM_CFG"
	local tmp
	if ! make_tmp "$upstream_file"; then
		echo "Unable to create temporary file." 1>&2
		exit 2
	fi
	tmp="$TMP"

	build_upstream "$HOST_ID" "$UPSTREAM" "$GW_ADDRS" | dd of="$tmp" conv=fsync >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "Can't save upstream configuration in $tmp." 1>&2
		rm -f "$tmp"
		exit 3
	fi

	# as in put_in_place: once the move may have happened, exit 130 would
	# claim nothing changed
	trap '' INT QUIT HUP TERM PIPE
	mv -f "$tmp" "$upstream_file" && return 0
	echo "Unable to update upstream configuration." 1>&2
	rm -f "$tmp"
	exit 4
}

# Append the s3vectors block to a configuration that predates it, without
# rewriting the S3 block: on a live cluster that block routinely carries local
# edits (CORS, body limits, extra locations) that a "create --replace" would
# silently drop.
# Print the first server block of $1. The vectors block is modelled on it, so
# what is copied has to come from that block and not from the file at large:
# an "upstream {}" section carries a server_name-looking line of its own, and
# a file that already has a vectors block would otherwise be read twice.
first_server_block() {
	awk '
	!in_block && /^[[:space:]]*server[[:space:]]*\{/ { in_block = 1; depth = 0 }
	in_block {
		print
		depth += gsub(/\{/, "{") - gsub(/\}/, "}")
		if (depth <= 0)
			exit
	}
	' "$1"
}

# What enable-vectors writes, made from the configuration file $1.
cfg_bucket_only() {
	apply_bucket < "$1"
}

cfg_with_vectors() {
	{ cat "$1" && build_vectors_cfg ; } | apply_bucket
}

do_enable_vectors() {
	local cfg_file="$NGINX_CFG_DIR/$OSTOR_S3_CFG"

	if grep -q "fastcgi_param OSTOR_API $VECTORS_API;" "$cfg_file"; then
		# A block added by an earlier version came without the bucket
		# size, and running this again is how such a file gets it.
		decide_bucket "$cfg_file"
		if [ "x$BUCKET_ACTION" = "xkeep" ]; then
			echo "The s3vectors block is already present in $cfg_file."
			exit 0
		fi

		write_tmp "$cfg_file" cfg_bucket_only
		put_in_place "$cfg_file" "$TMP"
		echo "The s3vectors block is already present in $cfg_file, $BUCKET_DIRECTIVE updated."
		echo "Reload nginx to apply it."
		exit 0
	fi

	# Take the shape of the new block from the existing one so that a
	# hand-edited listener, upstream or TLS setup is carried over rather
	# than reinvented.
	local s3_block
	s3_block=$(first_server_block "$cfg_file")
	if [ -z "$s3_block" ]; then
		echo "No server block found in $cfg_file." 1>&2
		exit 1
	fi

	[ -z "$DOMAIN" ] && DOMAIN=$(echo "$s3_block" | grep -m1 "server_name" | awk '{ print $2 }' | tr -d ';')
	if [ -z "$DOMAIN" ]; then
		echo "Can't read server_name from $cfg_file." 1>&2
		exit 1
	fi

	# Every listen line, not just the first: dropping one turns a dual-stack
	# endpoint into a single-family one.
	LISTEN_LINES=$(echo "$s3_block" | grep -E "^[[:space:]]*listen[[:space:]]")
	if [ -z "$LISTEN_LINES" ]; then
		echo "Can't read listen directives from $cfg_file." 1>&2
		exit 1
	fi

	# The upstream this file actually declares. Assuming the name this
	# script generates makes nginx refuse the whole file with "no port in
	# upstream" whenever the configuration came from somewhere else.
	# The trailing space keeps "fastcgi_pass_header" out of the match.
	UPSTREAM=$(echo "$s3_block" | grep -m1 "fastcgi_pass " | awk '{ print $2 }' | tr -d ';')
	if [ -z "$UPSTREAM" ]; then
		echo "Can't read fastcgi_pass from $cfg_file." 1>&2
		exit 1
	fi

	# A configuration that names no object storage keeps it that way.
	echo "$s3_block" | grep -q "fastcgi_param OSTOR_CFG_NAME " || NO_CFG_NAME="1"

	SSL_CERT_FILE=$(echo "$s3_block" | grep -m1 "ssl_certificate " | awk '{ print $2 }' | tr -d ';')
	SSL_KEY_FILE=$(echo "$s3_block" | grep -m1 "ssl_certificate_key " | awk '{ print $2 }' | tr -d ';')
	[ -n "$SSL_CERT_FILE" ] && USE_SSL="1"
	VECTORS="1"

	test_vectors_name
	test_ssl_cert
	decide_bucket "$cfg_file" adding

	write_tmp "$cfg_file" cfg_with_vectors
	put_in_place "$cfg_file" "$TMP"
}

# Drop the s3vectors block again. Needed before a downgrade: a script that
# predates the block reads OSTOR_CFG_NAME from every server block at once and
# refuses to update the upstream once it sees two of them.
strip_vectors_cfg() {
	awk -v api="$VECTORS_API" '
	function flush_pending() { printf "%s", pending; pending = "" }
	# Comments and blank lines belong to the block they precede, so hold them
	# back until it is known whether that block survives.
	!in_block && /^[[:space:]]*(#|$)/	{ pending = pending $0 "\n"; next }
	# No next here: the opening line falls through to the rule below, so a
	# server block written on a single line terminates on that same line.
	!in_block && /^[[:space:]]*server[[:space:]]*{/ { in_block = 1; block = ""; depth = 0 }
	in_block {
		block = block $0 "\n"
		depth += gsub(/{/, "{") - gsub(/}/, "}")
		if (depth > 0)
			next
		if (block !~ "OSTOR_API[[:space:]]+" api ";") {
			flush_pending(); printf "%s", block
		} else
			pending = ""
		in_block = 0; block = ""
		next
	}
	{ flush_pending(); print }
	# An unterminated block means the file is not shaped as expected: emit it
	# verbatim rather than dropping it, and let the sanity check in
	# do_disable_vectors decide whether the result is installable.
	END { flush_pending(); printf "%s", block }
	' "$1"
}

do_disable_vectors() {
	local cfg_file="$NGINX_CFG_DIR/$OSTOR_S3_CFG"

	if ! grep -q "fastcgi_param OSTOR_API $VECTORS_API;" "$cfg_file"; then
		echo "No s3vectors block in $cfg_file."
		exit 0
	fi

	write_tmp "$cfg_file" strip_vectors_cfg

	# Refuse to install a result that lost the S3 block too. The test is
	# "a server block survived", not "the object-storage name survived":
	# a configuration written elsewhere - the UI agent's - carries no such
	# parameter at all, and keying the guard on it made removal impossible
	# on exactly the files the in-place edit exists for.
	if ! grep -qE "^[[:space:]]*server[[:space:]]*\{" "$TMP"; then
		echo "Refusing to install $TMP: no server block left." 1>&2
		rm -f "$TMP"
		exit 2
	fi

	put_in_place "$cfg_file" "$TMP"
}

do_create() {
	local cfg_file="$NGINX_CFG_DIR/$OSTOR_S3_CFG"
	local upstream_file="$NGINX_CFG_DIR/$UPSTREAM_CFG"
	if [ -f "$cfg_file" ] ; then
		if [ -z "$REPLACE" ]; then
			echo "Configuration file $cfg_file already exist." 1>&2
			exit 1
		fi
	fi
	# What --replace overwrites does not count: decide_bucket looks at the
	# other files only, and the new one is made from scratch.
	BUCKET_ACTION="keep"
	[ -n "$VECTORS" ] && decide_bucket "" adding

	local tmp
	if ! make_tmp "$cfg_file"; then
		echo "Unable to create temporary file." 1>&2
		exit 1
	fi
	tmp="$TMP"
	local tmp1
	if ! make_tmp "$upstream_file"; then
		echo "Unable to create temporary file." 1>&2
		rm -f "$tmp"
		exit 2
	fi
	tmp1="$TMP"

	# The S3 block must stay first: with no explicit default_server, nginx
	# makes the first block on the port the default one, and every request
	# whose Host matches nothing (an IP, for one) lands there.
	build_cfg_all | apply_bucket | dd of="$tmp" conv=fsync >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "Can't save new configuration in $tmp" 1>&2
		rm -f "$tmp" "$tmp1"
		exit 2
	fi

	build_upstream "$HOST_ID" "$UPSTREAM" "$GW_ADDRS" | dd of="$tmp1" conv=fsync >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "Can't save upstream configuration in $tmp1" 1>&2
		rm -f "$tmp" "$tmp1"
		exit 3
	fi

	put_in_place "$upstream_file" "$tmp1" "$cfg_file" "$tmp"
}

parse_args $ARGS
trap 'rm -f $CLEANUP' EXIT
trap 'exit 130' INT QUIT HUP TERM
if [ ! -d "$NGINX_CFG_DIR" ]; then
	echo "Directory $NGINX_CFG_DIR does not exist" 2>&1
	exit 1
fi

HOST_ID=$(cat /etc/vstorage/host_id 2>/dev/null | head -n1)
GW_ADDRS=$(get_addrs "$HOST_ID")
if [ $? -ne 0 ]; then
	echo "Can't get configuration." 1>&2
	exit 2
fi

if [ -z "$GW_ADDRS" ] ; then
	echo "Can't get S3 gateways assigned to host $HOST_ID." 1>&2
	exit 2
fi

RC=0
if [ "x$CMD" = "xcreate" ]; then
	test_vectors_name
	test_ssl_cert
	do_create
	RC=$?
	if [ $RC -eq 0 ] ; then
		echo "Configuration stored in $NGINX_CFG_DIR/$OSTOR_S3_CFG"
		echo "Upstream configuration stored in $NGINX_CFG_DIR/$UPSTREAM_CFG"
	fi
else
	if [ ! -f "$NGINX_CFG_DIR/$OSTOR_S3_CFG" ]; then
		echo "Configuration does not exist at $NGINX_CFG_DIR." 1>&2
		exit 1
	fi

	# read keepalive configuration
	cat "$NGINX_CFG_DIR/$OSTOR_S3_CFG" | grep "fastcgi_keep_conn on" >/dev/null
	[ $? -eq 0 ] && KEEPALIVE="1"


	# test hostname of configuration service	
	# head -n1: every server block in the file carries an OSTOR_CFG_NAME, and
	# they all name the same object storage. Without it a configuration with
	# an s3vectors block yields a multi-line $cfg and the comparison below
	# always fails, which would break every subsequent upstream update.
	cfg=$(cat "$NGINX_CFG_DIR/$OSTOR_S3_CFG" | grep "fastcgi_param OSTOR_CFG_NAME .*;" 2>/dev/null | tr ';' ' ' | awk '{ print $3 }' | head -n1)

	# The check answers "does this file belong to the object storage named
	# on the command line", and it exists because update rewrites that
	# storage's upstream. The vectors commands touch no upstream and only
	# add or remove a block in place, so a configuration written by
	# something else - the UI agent's, which carries no OSTOR_CFG_NAME at
	# all - is still a legitimate target for them.
	if [ "x$CMD" = "xupdate" ] || [ -n "$cfg" ]; then
		if [ -z "$cfg" ] ; then
			echo "Can't read OSTOR_CFG_NAME from configuration." 1>&2
			exit 1
		fi

		if [ "x$CFG_SVC" != "x$cfg" ]; then
			echo "Existing configuration created for object storage '$cfg'." 1>&2
			exit 1
		fi
	fi

	if [ "x$CMD" = "xenable-vectors" ]; then
		do_enable_vectors
		RC=$?
		if [ $RC -eq 0 ] ; then
			echo "The $NGINX_CFG_DIR/$OSTOR_S3_CFG now serves s3vectors on $(vectors_name)"
			echo "Reload nginx to apply it."
		fi
	elif [ "x$CMD" = "xdisable-vectors" ]; then
		do_disable_vectors
		RC=$?
		if [ $RC -eq 0 ] ; then
			echo "The s3vectors block was removed from $NGINX_CFG_DIR/$OSTOR_S3_CFG"
			echo "Reload nginx to apply it."
		fi
	else
		do_update
		RC=$?
		[ $RC -eq 0 ] && echo "The $NGINX_CFG_DIR/$UPSTREAM_CFG successfully updated."
	fi
fi
exit $RC

