#!/sbin/openrc-run

description="Nginx HTTP and reverse proxy server"
description_checkconfig="Check configuration"
description_reload="Reload configuration without downtime"
description_reopen="Reopen log files"
description_upgrade="Upgrade the binary without downtime"

extra_commands="checkconfig"
extra_started_commands="reload reopen upgrade"

: ${cfgfile:="/etc/nginx/nginx.conf"}
: ${command:="/usr/sbin/nginx"}

command_args="-c $cfgfile $command_args"
rundir="/run/nginx"
pidfile="$rundir/nginx.pid"

required_files="$cfgfile"

depend() {
	need net
	use dns logger netmount
}

start_pre() {
	if [ -z "$command_user" ] || [ "${command_user%%:*}" = root ]; then
		# The master process (root) handles files in these directories by path,
		# following symlinks, so the nginx user must not own them, otherwise it
		# could abuse it to change ownership of arbitrary files (cf.
		# CVE-2016-1247).
		# NOTE: This is a mitigation added to v3.22-v3.25. Since v3.26, the
		#  package ships these directories with these permissions.
		checkpath -d -m 0750 -o root:nginx /var/lib/nginx || return 1
		checkpath -d -m 0710 -o root:nginx /var/lib/nginx/tmp || return 1
		checkpath -d -m 0755 -o root:adm /var/log/nginx || return 1
		checkpath -d -m 0755 -o root:root "$rundir" || return 1
	else
		checkpath -d -o nginx:nginx "$rundir" || return 1
	fi

	# nginx doesn't read resolv.conf for runtime lookups, so generate a
	# resolver for the optional include in nginx.conf. Skipped if the
	# directory isn't owned by root (nginx could plant symlinks there).
	if [ "$(stat -c %u "$rundir")" -eq 0 ]; then
		local servers="$(_get_nameservers)"

		if [ "$servers" ]; then
			echo "resolver$servers;" > "$rundir"/resolver.conf
		else
			ewarn "No usable nameserver in /etc/resolv.conf, not updating $rundir/resolver.conf"
		fi
	fi

	"$command" $command_args -t -q
}

checkconfig() {
	ebegin "Checking $RC_SVCNAME configuration"
	start_pre
	eend $?
}

reload() {
	ebegin "Reloading $RC_SVCNAME configuration"
	start_pre && start-stop-daemon --signal HUP --pidfile "$pidfile"
	eend $?
}

reopen() {
	ebegin "Reopening $RC_SVCNAME log files"
	start-stop-daemon --signal USR1 --pidfile "$pidfile"
	eend $?
}

upgrade() {
	start_pre || return 1

	ebegin "Upgrading $RC_SVCNAME binary"

	einfo "Sending USR2 to old binary"
	start-stop-daemon --signal USR2 --pidfile "$pidfile"

	einfo "Sleeping 3 seconds before pid-files checking"
	sleep 3

	if [ ! -f "$pidfile.oldbin" ]; then
		eerror "File with old pid ($pidfile.oldbin) not found"
		return 1
	fi

	if [ ! -f "$pidfile" ]; then
		eerror "New binary failed to start"
		return 1
	fi

	einfo "Sleeping 3 seconds before WINCH"
	sleep 3 ; start-stop-daemon --signal 28 --pidfile "$pidfile.oldbin"

	einfo "Sending QUIT to old binary"
	start-stop-daemon --signal QUIT --pidfile "$pidfile.oldbin"

	einfo "Upgrade completed"

	eend $? "Upgrade failed"
}

# Print the nameservers from resolv.conf, each prefixed with a space, in the
# format of the resolver directive: IPv6 addresses are in brackets and
# link-local ones are skipped, because nginx can't use them (zone ID).
_get_nameservers() {
	awk '$1 == "nameserver" && $2 !~ /%/ && tolower($2) !~ /^fe80:/ {
		printf " %s", ($2 ~ /:/ ? "[" $2 "]" : $2)
	}' /etc/resolv.conf 2>/dev/null
}
