#!/bin/sh
#
# $NetBSD: tinydyndns-passwd.sh,v 1.2 2020/10/19 07:15:55 schmonz Exp $
#
# tinydyndns-run-20250425 program to update password for a dynamic-DNS hostname.

TINYDYN_ETC="/usr/pkg/etc/tinydyn"
TINYDYN_DOMAIN=$(/usr/bin/head -1 ${TINYDYN_ETC}/domain)
TINYDYN_PASSWD="${TINYDYN_ETC}/passwd"
TINYDYN_PASSWDTMP="${TINYDYN_ETC}/passwd{new}"
TINYDYN_USER=tinydyn
TINYDYN_UID=$(/usr/bin/id -u ${TINYDYN_USER})
TINYDYN_GID=$(/usr/bin/id -g ${TINYDYN_USER})
TINYDYN_SHELL="/sbin/nologin"

passwd_exists_for() {
	local _hostname
	_hostname=$1
	/usr/bin/grep "^${_hostname}:" ${TINYDYN_PASSWD} >/dev/null 2>/dev/null
}

passwd_hash() {
	local _password
	_password=$1
	echo "$1" | /usr/pkg/bin/tinydyndns-pwhash
}

passwd_add() {
	local _hostname _password
	_hostname=$1
	_password=$2
	_hashed_password=$(passwd_hash "${_password}")
	/bin/cat ${TINYDYN_PASSWD} >${TINYDYN_PASSWDTMP} 2>/dev/null
	echo "${_hostname}:${_hashed_password}:${TINYDYN_UID}:${TINYDYN_GID}::${TINYDYN_ETC}:${TINYDYN_SHELL}" \
		>>${TINYDYN_PASSWDTMP}
}

passwd_update() {
	local _hostname _password
	_hostname=$1
	_password=$2
	_hashed_password=$(passwd_hash "${_password}")
	/usr/bin/sed -e \
		"s|^${_hostname}:.*$|${_hostname}:${_hashed_password}:${TINYDYN_UID}:${TINYDYN_GID}::${TINYDYN_ETC}:${TINYDYN_SHELL}|g" \
		${TINYDYN_PASSWD} >${TINYDYN_PASSWDTMP}
}

dyndns_add() {
	local _hostname
	_hostname=$1
	( cd ${TINYDYN_ETC}; /usr/pkg/bin/tinydyndns-data add ${_hostname}.${TINYDYN_DOMAIN} )
}

main() {
	if [ -z "$1" -o -z "$2" ]; then
		echo "Usage: $0 hostname password" >&2
		return 1
	fi

	dyn_hostname=$1
	dyn_password=$2

	if passwd_exists_for "${dyn_hostname}"; then
		passwd_update "${dyn_hostname}" "${dyn_password}"
	else
		passwd_add "${dyn_hostname}" "${dyn_password}"
	fi
	mv -f -f ${TINYDYN_PASSWDTMP} ${TINYDYN_PASSWD}
	/bin/chmod 600 ${TINYDYN_PASSWD}

	dyndns_add "${dyn_hostname}"
}

main "$@"
exit $?
