#!/sbin/openrc-run
# Copyright 1999-2025 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

description="kea dhcp services"

dhcp4_command="/usr/sbin/kea-dhcp4"
dhcp6_command="/usr/sbin/kea-dhcp6"
ddns_command="/usr/sbin/kea-dhcp-ddns"
ctrl_agent_command="/usr/sbin/kea-ctrl-agent"
dhcp4_config="${DHCP4_CONFIG:-/etc/kea/kea-dhcp4.conf}"
dhcp6_config="${DHCP6_CONFIG:-/etc/kea/kea-dhcp6.conf}"
ddns_config="${DDNS_CONFIG:-/etc/kea/kea-dhcp-ddns.conf}"
ctrl_agent_config="${CTRL_AGENT_CONFIG:-/etc/kea/kea-ctrl-agent.conf}"
dhcp4_pidfile="/run/kea/$(basename ${dhcp4_config%.conf}).kea-dhcp4.pid"
dhcp6_pidfile="/run/kea/$(basename ${dhcp6_config%.conf}).kea-dhcp6.pid"
ddns_pidfile="/run/kea/$(basename ${ddns_config%.conf}).kea-dhcp-ddns.pid"
ctrl_agent_pidfile="/run/kea/$(basename ${ctrl_agent_config%.conf}).kea-ctrl-agent.pid"
kea_user="${KEA_USER:-dhcp}"
kea_group="${KEA_GROUP:-dhcp}"

cap_list="^cap_net_bind_service"
cap4_list="${cap_list},^cap_net_raw"

depend() {
	use net
}

start_pre() {
	if ${DHCP4:-false} ; then
		if [ ! -f "${dhcp4_config}" ] ; then
			eerror "Please create a ${dhcp4_config} config file."
			return 1
		fi
		if [ $(stat -c "%U:%G" ${dhcp4_config}) != "root:${kea_group}" ] ; then
			eerror "${dhcp4_config} config file is not owned by root:${kea_group}"
			eerror "you should reset the ownership:"
			eerror "chown root:${kea_group} ${dhcp4_config}"
			return 1
		fi
		if ! ${dhcp4_command} -t ${dhcp4_config} 1>/dev/null 2>/dev/null ; then
			eerror "Error in config file ${dhcp4_config}"
			return 1
		fi
	fi
	if ${DHCP6:-false} ; then
		if [ ! -f "${dhcp6_config}" ] ; then
			eerror "Please create a ${dhcp6_file} config file."
			return 1
		fi
		if [ $(stat -c "%U:%G" ${dhcp6_config}) != "root:${kea_group}" ] ; then
			eerror "${dhcp6_config} config file is not owned by root:${kea_group}"
			eerror "you should reset the ownership:"
			eerror "chown root:${kea_group} ${dhcp6_config}"
			return 1
		fi
		if ! ${dhcp6_command} -t ${dhcp6_config} 1>/dev/null 2>/dev/null ; then
			eerror "Error in config file ${dhcp6_config}"
			return 1
		fi
	fi
	if ${DDNS:-false} ; then
		if [ ! -f "${ddns_config}" ] ; then
			eerror "Please create a ${ddns_config} config file."
			return 1
		fi
		if [ $(stat -c "%U:%G" ${ddns_config}) != "root:${kea_group}" ] ; then
			eerror "${ddns_config} config file is not owned by root:${kea_group}"
			eerror "you should reset the ownership:"
			eerror "chown root:${kea_group} ${ddns_config}"
			return 1
		fi
		if ! ${ddns_command} -t ${ddns_config} 1>/dev/null 2>/dev/null ; then
			eerror "Error in config file ${ddns_config}"
			return 1
		fi
	fi
	if ${CTRL_AGENT:-false} ; then
		if [ ! -f "${ctrl_agent_config}" ] ; then
			eerror "Please create a ${ctrl_agent_config} config file."
			return 1
		fi
		if [ $(stat -c "%U:%G" ${ctrl_agent_config}) != "root:${kea_group}" ] ; then
			eerror "${ctrl_agent_config} config file is not owned by root:${kea_group}"
			eerror "you should reset the ownership:"
			eerror "chown root:${kea_group} ${ctrl_agent_config}"
			return 1
		fi
		if ! ${ctrl_agent_command} -t ${ctrl_agent_config} 1>/dev/null 2>/dev/null ; then
			eerror "Error in config file ${ctrl_agent_config}"
			return 1
		fi
	fi
}

start() {
	einfo "Starting kea dhcp services"
	atleastone=
	if ${DHCP4:-false} ; then
		start-stop-daemon -b --capabilities ${cap4_list} -u ${kea_user} -g ${kea_group} -p ${dhcp4_pidfile} \
			-x ${dhcp4_command} -- -c ${dhcp4_config} \
			|| return 1
		atleastone=1
	fi
	if ${DHCP6:-false} ; then
		start-stop-daemon -b --capabilities ${cap_list} -u ${kea_user} -g ${kea_group} -p ${dhcp6_pidfile} \
			-x ${dhcp6_command} -- -c ${dhcp6_config} \
			|| return 1
		atleastone=1
	fi
	if ${DDNS:-false} ; then
		start-stop-daemon -b --capabilities ${cap_list} -u ${kea_user} -g ${kea_group} -p ${ddns_pidfile} \
			-x ${ddns_command} -- -c ${ddns_config} \
			|| return 1
		atleastone=1
	fi
	if ${CTRL_AGENT:-false} ; then
		start-stop-daemon -b -u ${kea_user} -g ${kea_group} -p ${ctrl_agent_pidfile} \
			-x ${ctrl_agent_command} -- -c ${ctrl_agent_config} \
			|| return 1
		atleastone=1
	fi
	if [ -z ${atleastone} ] ; then
		eerror "No service has been launched!"
		return 1
	fi
}

stop() {
	einfo "Stopping kea dhcp services"
	if ${DHCP4:-false} ; then
		start-stop-daemon --stop -p ${dhcp4_pidfile} \
			|| return 1
	fi
	if ${DHCP6:-false} ; then
		start-stop-daemon --stop -p ${dhcp6_pidfile} \
			|| return 1
	fi
	if ${DDNS:-false} ; then
		start-stop-daemon --stop -p ${ddns_pidfile} \
			|| return 1
	fi
	if ${CTRL_AGENT:-false} ; then
		start-stop-daemon --stop -p ${ctrl_agent_pidfile} \
			|| return 1
	fi
}