#!/bin/sh

# PROVIDE: apache24
# REQUIRE: LOGIN cleanvar sshd
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable apache24:
# apache24_enable (bool):      Set to "NO" by default.
#                             Set it to "YES" to enable apache24
# apache24_profiles (str):     Set to "" by default.
#                              Define your profiles here.
# apache24limits_enable (bool):Set to "NO" by default.
#                             Set it to yes to run `limits $limits_args`
#                             just before apache starts.
# apache24_flags (str):        Set to "" by default.
#                             Extra flags passed to start command.
# apache24limits_args (str):   Default to "-e -C daemon"
#                             Arguments of pre-start limits run.
# apache24_http_accept_enable (bool): Set to "NO" by default.
#                             Set to yes to check for accf_http kernel
#                             module on start up and load if not loaded.
# apache24_fib (str):	      Set an altered default network view for apache
# apache24_configcheck_disable (bool): Set to "YES" to disable sanity check on startup
# apache24_aslr_disable (bool): Set to "YES" to disable ASLR (Address Space Layout
#                             Randomization): workaround for PR#268318

. /etc/rc.subr

name="apache24"
rcvar=apache24_enable

start_precmd="apache24_prestart"
restart_precmd="apache24_checkconfig"
reload_precmd="apache24_checkconfig"
reload_cmd="apache24_graceful"
graceful_cmd="apache24_graceful"
gracefulstop_cmd="apache24_gracefulstop"
configtest_cmd="apache24_checkconfig"
command="%%PREFIX%%/sbin/httpd"
_pidprefix="/var/run/httpd"
pidfile="${_pidprefix}.pid"
required_files=%%PREFIX%%/etc/apache24/httpd.conf
envvars="%%PREFIX%%/sbin/envvars"

[ -z "$apache24_enable" ]       && apache24_enable="NO"
[ -z "$apache24limits_enable" ] && apache24limits_enable="NO"
[ -z "$apache24limits_args" ]   && apache24limits_args="-e -C daemon"
[ -z "$apache24_http_accept_enable" ] && apache24_http_accept_enable="NO"
[ -z "$apache24_configcheck_disable" ] && apache24_configcheck_disable="NO"
[ -z "$apache24_aslr_disable" ] && apache24_aslr_disable="NO"

apache24_accf()
{
  if checkyesno apache24_http_accept_enable; then
    /sbin/kldstat -qm accf_http || /sbin/kldload accf_http || return ${?}
    /sbin/kldstat -qm accf_data || /sbin/kldload accf_data || return ${?}
  else
    apache24_flags="${apache24_flags} -DNOHTTPACCEPT"
  fi
}

load_rc_config $name

if [ -n "$2" ]; then
	profile="$2"
	if [ "x${apache24_profiles}" != "x" ]; then
		pidfile="${_pidprefix}.${profile}.pid"
		eval apache24_configfile="\${apache24_${profile}_configfile:-}"
		if [ "x${apache24_configfile}" = "x" ]; then
			echo "You must define a configuration file (apache24_${profile}_configfile)"
			exit 1
		fi
		required_files="${apache24_configfile}"
		eval apache24_enable="\${apache24_${profile}_enable:-${apache24_enable}}"
		eval apache24_flags="\${apache24_${profile}_flags:-${apache24_flags}}"
		eval apache24_http_accept_enable="\${apache24_${profile}_http_accept_enable:-${apache24_http_accept_enable}}"
		eval apache24limits_enable="\${apache24limits_${profile}_enable:-${apache24limits_enable}}"
		eval apache24limits_args="\${apache24limits_${profile}_args:-${apache24limits_args}}"
		eval apache24_fib="\${apache24_${profile}_fib:-${apache24_fib}}"
		eval apache24_configcheck_disable="\${apache24_${profile}_configcheck_disable:-${apache24_configcheck_disable}}"
		eval apache24_aslr_disable="\${apache24_${profile}_aslr_disable:-${apache24_aslr_disable}}"
		eval command="\${apache24_${profile}_command:-${command}}"
		eval pidfile="\${apache24_${profile}_pidfile:-${pidfile}}"
		eval apache24_envvars="\${apache24_${profile}_envvars:-${envvars}}"
		apache24_flags="-f ${apache24_configfile} -c \"PidFile ${pidfile}\" ${apache24_flags}"
	else
		echo "$0: extra argument ignored"
	fi
else
	eval apache24_envvars=${envvars}
	if [ "x${apache24_profiles}" != "x" -a "x$1" != "x" ]; then
		for profile in ${apache24_profiles}; do
			eval _enable="\${apache24_${profile}_enable}"
			case "x${_enable:-${apache24_enable}}" in
			x|x[Nn][Oo]|x[Nn][Oo][Nn][Ee])
				continue
				;;
			x[Yy][Ee][Ss])
				;;
			*)
				if test -z "$_enable"; then
					_var=apache24_enable
				else
					_var=apache24_"${profile}"_enable
				fi
				echo "Bad value" \
				    "'${_enable:-${apache24_enable}}'" \
				    "for ${_var}. " \
				    "Profile ${profile} skipped."
				continue
				;;
			esac
			echo "===> apache24 profile: ${profile}"
			%%PREFIX%%/etc/rc.d/apache24 $1 ${profile}
			retcode="$?"
			if [ "0${retcode}" -ne 0 ]; then
				failed="${profile} (${retcode}) ${failed:-}"
			else
				success="${profile} ${success:-}"
			fi
		done
		exit 0
	fi
fi

if [ "${1}" != "stop" ] ; then \
	apache24_accf
fi

if checkyesno apache24_configcheck_disable
then
        unset restart_precmd
        unset reload_precmd
fi

apache24_requirepidfile()
{
	if [ ! "0`check_pidfile ${pidfile} ${command}`" -gt 1 ]; then
		echo "${name} not running? (check $pidfile)."
		exit 1
	fi
}

apache24_checkconfig()
{
	if test -f ${apache24_envvars}
	then
		. ${apache24_envvars}
	fi

	echo "Performing sanity check on apache24 configuration:"
	eval ${command} ${apache24_flags} -t
}

apache24_graceful() {
	apache24_requirepidfile

	if ! checkyesno apache24_configcheck_disable
	then
		apache24_checkconfig
	fi

	echo "Performing a graceful restart"
	eval ${command} ${apache24_flags} -k graceful
}

apache24_gracefulstop() {
	apache24_requirepidfile

	if ! checkyesno apache24_configcheck_disable
	then
		apache24_checkconfig
	fi

	echo "Performing a graceful stop"
	eval ${command} ${apache24_flags} -k graceful-stop
}

apache24_precmd()
{
	if ! checkyesno apache24_configcheck_disable
	then
		apache24_checkconfig
	fi

	if checkyesno apache24limits_enable
	then
		eval `/usr/bin/limits ${apache24limits_args}` 2>/dev/null
	else
		return 0
        fi

}

apache24_checkalsr () {
	if checkyesno apache24_aslr_disable
	then
		command="/usr/bin/proccontrol -m aslr -s disable ${command}"
	fi
}

apache24_checkfib () {
	if command -v check_namevarlist > /dev/null 2>&1; then
		check_namevarlist fib && return 0
	fi

	$SYSCTL net.fibs >/dev/null 2>&1 || return 0

	apache24_fib=${apache24_fib:-"NONE"}
	if [ "x$apache24_fib" != "xNONE" ]
	then
		command="/usr/sbin/setfib -F ${apache24_fib} ${command}"
	else
		return 0
	fi
}

apache24_prestart() {
	apache24_checkalsr
	apache24_checkfib
	apache24_precmd
}

extra_commands="reload graceful gracefulstop configtest"
run_rc_command "$1"
