#!/bin/sh
#
# $NetBSD: rbldns.sh,v 1.9 2019/01/08 18:29:30 schmonz Exp $
#
# djbdns-run-20250402 script to control rbldns (local RBL service).
#

# PROVIDE: rbldns named
# REQUIRE: NETWORKING mountcritremote syslogd
# BEFORE:  DAEMON

name="rbldns"

# User-settable rc.conf variables and their default values:
: ${rbldns_postenv:=""}
: ${rbldns_ip:="127.0.0.3"}
: ${rbldns_base:="rbl"}
: ${rbldns_datalimit:="250000"}
: ${rbldns_autocdb:="YES"}
: ${rbldns_log:="YES"}
: ${rbldns_logcmd:="logger -t nbdjbdns/rbl -p daemon.info"}
: ${rbldns_nologcmd:="/usr/pkg/bin/multilog -*"}

rbldns_data="/usr/pkg/etc/${name}/data"

if [ -f /etc/rc.subr ]; then
	. /etc/rc.subr
fi

rcvar=${name}
required_files="${rbldns_data}"
command="/usr/pkg/bin/${name}"
procname=nb${name}
start_precmd="rbldns_precmd"
extra_commands="cdb reload"
cdb_cmd="rbldns_cdb"
reload_cmd=${cdb_cmd}

rbldns_precmd() {
	if [ -f /etc/rc.subr ] && ! checkyesno rbldns_log; then
		rbldns_logcmd=${rbldns_nologcmd}
	fi
	if [ -f /etc/rc.subr ] && checkyesno rbldns_autocdb; then
		rbldns_needcdb && rbldns_cdb
	fi
	command="/usr/pkg/bin/pgrphack /usr/bin/env - ${rbldns_postenv} \
ROOT=/usr/pkg/etc/${name} \
IP=${rbldns_ip} \
BASE=${rbldns_base} \
/usr/pkg/bin/envuidgid rbldns \
/usr/pkg/bin/softlimit -d ${rbldns_datalimit} \
/usr/pkg/bin/argv0 /usr/pkg/bin/${name} ${procname} \
2>&1 | \
/usr/pkg/bin/pgrphack /usr/pkg/bin/setuidgid dnslog ${rbldns_logcmd}"
	command_args="&"
	rc_flags=""
}

rbldns_needcdb() {
	_src=${rbldns_data}
	_dst=${rbldns_data}.cdb
	[ -f "${_src}" -a "${_src}" -nt "${_dst}" ] || [ ! -f "${_dst}" ]
}

rbldns_cdb() {
	echo "Reloading ${rbldns_data}."
	cd /usr/pkg/etc/${name} && /usr/pkg/bin/rbldns-data
	/bin/chmod 644 ${rbldns_data}.cdb
}

if [ -f /etc/rc.subr ]; then
	load_rc_config $name
	run_rc_command "$1"
else
	echo -n " ${name}"
	rbldns_precmd
	eval ${command} ${rbldns_flags} ${command_args}
fi
