#!/bin/bash
#
# Univention Server
#  network script: modify resolv.conf
#
# Like what you see? Join us!
# https://www.univention.com/about-us/careers/vacancies/
#
# Copyright 2021-2023 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

# Bug #37689: this script is "sourced", not forked; Do not "exit" the process!
test -n "${interface:-}" || return 0
[ "$(ucr get "interfaces/${interface}/type")" = dhcp ] || return 0
[ -x /usr/share/univention-server/univention-fix-ucr-dns ] || return 0

make_resolv_conf () {
	local ns
	eval "$(ucr shell '^nameserver[123]$')"

	# shellcheck source=/dev/null
	if ( . /usr/share/univention-lib/ucr.sh ; is_ucr_true "nameserver/external" ); then
		return 0  # do not touch /etc/resolv.conf at all
	fi

	[ "${old_domain_name_servers:-}" != "${new_domain_name_servers:-}" ] ||
		[ "${old_dhcp6_name_servers:-}" != "${new_dhcp6_name_servers:-}" ] ||
		return 0

	# IPv4
	# shellcheck disable=SC2086
	set -- ${new_domain_name_servers:-}  # IFS
	# IPv6
	for ns in ${new_dhcp6_name_servers:-}  # IFS
	do
		set -- ${1:+"$@"} "$(link_local "$ns")"
	done

	for ns in "$@"
	do
		shift
		set -- "$@" --dnsserver "$ns"
	done

	if [ -z "$*" ] ; then
		# no nameservers have been specified - do not touch existing config
		return 0
	fi

	# On a joined UCS system with local DNS server, the DHCP nameserver are automatically
	# converted to nameserver[123] resp. dns/forwarder[123] and the local system is also
	# added to nameserver[123] if not already present (Bug #44462).
	# shellcheck source=/dev/null
	if	[ -e /var/univention-join/joined ] &&
		( . /usr/share/univention-lib/base.sh ; is_domain_controller )
	then
		if [ -n "$new_ip_address" ]; then
			/usr/share/univention-server/univention-fix-ucr-dns --no-ucr --force-self --own-ip "$new_ip_address" "$@"
		else
			/usr/share/univention-server/univention-fix-ucr-dns --no-ucr --force-self "$@"
		fi
	else
		# on unjoined systems or non-domaincontrollers do not add own IP address
		/usr/share/univention-server/univention-fix-ucr-dns --no-ucr --no-self "$@"
	fi
}
ns_in_list () {
	case "$2" in
	"$1 "*|*" $1 "*|*" $1") return 0 ;;
	*) return 1 ;;
	esac
}
link_local () {  # append "%interface" to link-local-address
	case "$1" in
	[fF][eE]80:*) echo "${1}%${interface}" ;;
	*) echo "$1" ;;
	esac
}
