#!/bin/bash
#
# Copyright (c) 2008 SUSE LINUX Products GmbH, Nuernberg, Germany.
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed 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 General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: Michael Calmer <mc@suse.de>
#         Marius Tomaschewski <mt@suse.de>
#
##

unset POSIXLY_CORRECT ; set +o posix # we are using non-posix bash features

# The environment variable ROOT indicates the root of the system to be
# managed by SuSEconfig when that root is not '/'
r="$ROOT"

. "$r/etc/sysconfig/network/scripts/functions.netconfig"

PROGNAME="${0##*/}"
if test "$UID" != "0" -a "$USER" != root -a -z "$ROOT" ; then
    warn "You must be root to start $0." >&2
    exit 1
fi

SYSFSDIR="/sys/class/net"
STATEDIR="/run/netconfig"
debug "$PROGNAME module called"

. "$r/etc/sysconfig/network/config"

unset DNS_SEARCHLIST ${!DNS_SEARCHLIST_*}
unset DNS_SERVERS ${!DNS_SERVERS_*}

DESTLINK="/etc/resolv.conf"
DESTFILE="$STATEDIR/resolv.conf"

# *********************
# FUNCTIONS 
# *********************

function dump_resolv_conf()
{
    local SEARCHLIST=()
    local NAMESERVER=()

    cat << EOT
### $DESTLINK is a symlink to $DESTFILE
### autogenerated by netconfig!
#
# Before you change this file manually, consider to define the
# static DNS configuration using the following variables in the
# /etc/sysconfig/network/config file:
#     NETCONFIG_DNS_STATIC_SEARCHLIST
#     NETCONFIG_DNS_STATIC_SERVERS
#     NETCONFIG_DNS_FORWARDER
# or disable DNS configuration updates via netconfig by setting:
#     NETCONFIG_DNS_POLICY=''
#
# See also the netconfig(8) manual page and other documentation.
#
### Call "netconfig update -f" to force adjusting of ${DESTLINK}.
EOT

    if [ "X$NETCONFIG_DNS_RESOLVER_OPTIONS" != "X" ] ; then
        echo "options $NETCONFIG_DNS_RESOLVER_OPTIONS"
    fi

    if [ "X$NETCONFIG_DNS_RESOLVER_SORTLIST" != "X" ] ; then
        echo "sortlist $NETCONFIG_DNS_RESOLVER_SORTLIST"
    fi

    for nd in $1; do
        # strip trailing dot added by dhcp 4.x dhclient(6)
        [ "x$nd" != "x." ] && nd="${nd%.}"
        test "x$nd" = x && continue

        # skip duplicates
        for od in ${SEARCHLIST[@]} ; do
            [ "x$nd" == "x$od" ] && continue 2
        done

        SEARCHLIST=(${SEARCHLIST[@]} ${nd})
    done

    if [ ${#SEARCHLIST[@]} -gt 0 ]; then
        {
            echo "search ${SEARCHLIST[@]}"
        }
    fi

    for ns in $2; do
        # skip duplicates
        for os in ${NAMESERVER[@]} ; do
            [ "x$ns" == "x$os" ] && continue 2
        done

        # resolv.conf supports up to 3 nameserver
        [ ${#NAMESERVER[@]} -lt 3 ] || break

        NAMESERVER=(${NAMESERVER[@]} ${ns})
    done
    if [ ${#NAMESERVER[@]} -gt 0 ]; then
        {
            for ns in ${NAMESERVER[@]}; do
                echo "nameserver $ns"
            done
        }
    fi
}

function write_resolv_conf()
{
    local TMP_FILE=""
    local changed=0
    local updated=0

    #
    # empty search list is a valid value
    #
    #test -z "$1" && return 1
    #
    # empty nameserver is a valid value too
    # (empty at least in the forwarder mode)
    #
    #test -z "$2" && return 1

    debug "write_resolv_conf: '$1' '$2'"

    TMP_FILE=`netconfig_mktemp "$ROOT$DESTFILE" 0644 0755` || return 3
    if ! dump_resolv_conf "$@" >> "$TMP_FILE"   ; then
        rm -f -- "$TMP_FILE" ; return 3
    fi
    if cmp -s -- "$TMP_FILE" "$ROOT$DESTFILE"   ; then
        rm -f -- "$TMP_FILE" # unchanged
    elif mv -f -- "$TMP_FILE" "$ROOT$DESTFILE"  ; then
        changed=1
    else
        rm -f -- "$TMP_FILE" ; return 3
    fi

    debug "dns settings written to $DESTFILE"

    netconfig_check_and_link "$DESTLINK" "$DESTFILE" "$ROOT"
    updated=$?

    test $changed -ne 0 -a $updated -eq 1 || return $updated
    return $?
}

function get_dns_settings()
{
    local cfg=$1 ; shift
    test "x$cfg" = x && return 1

    local SERVICE DNSSEARCH DNSDOMAIN DNSSERVERS
    local var idx DNS_SEARCHLIST DNS_SERVERS

    debug "exec get_dns_settings: $cfg"

    get_variable "SERVICE" "$cfg"
    idx=`get_ranking_idx "$SERVICE" "$@"`
    debug "     get_dns_settings: service '$SERVICE' => rank '$idx'"

    var="DNS_SEARCHLIST_$idx"
    DNS_SEARCHLIST=(${!var})
    get_variable "DNSSEARCH" "$cfg"
    if [ "x$DNSSEARCH" != "x" ]; then
        DNS_SEARCHLIST=(${DNS_SEARCHLIST[@]} $DNSSEARCH)
    else
        get_variable "DNSDOMAIN" "$cfg"
        if [ "x$DNSDOMAIN" != "x" ]; then
            DNS_SEARCHLIST=(${DNS_SEARCHLIST[@]} $DNSDOMAIN)
        else
            local H=$HOSTNAME
            local HOSTNAME
            get_variable "HOSTNAME" "$cfg"
            HOSTNAME="${HOSTNAME%.}"
            if [ "x$HOSTNAME" != "x" -a "x${HOSTNAME#*.}" != "x" \
                 -a "${HOSTNAME#*.}" != "${HOSTNAME}" ] ; then
                DNS_SEARCHLIST=(${DNS_SEARCHLIST[@]} "${HOSTNAME#*.}")
            fi
            HOSTNAME=$H
        fi
    fi
    unset DNSSEARCH DNSDOMAIN
    eval "${var}='${DNS_SEARCHLIST[@]}'"
    debug "     get_dns_settings: ${var}='${!var}'"

    var="DNS_SERVERS_$idx"
    DNS_SERVERS=(${!var})
    get_variable "DNSSERVERS" "$cfg"
    if [ "x$DNSSERVERS" != "x" ]; then
        DNS_SERVERS=(${DNS_SERVERS[@]} $DNSSERVERS)
    fi
    unset DNSSERVERS
    eval "${var}='${DNS_SERVERS[@]}'"
    debug "     get_dns_settings: ${var}='${!var}'"

    debug "exit get_dns_settings: $cfg"
    return 0
}

function manage_interfaceconfig()
{
    local cfg dir="$1" ; shift
    test "x$dir" != x -a -d "$dir" || return 1

    debug "exec manage_interfaceconfig: $dir"
    for cfg in `ls -X -r "$dir/" 2>/dev/null`; do
        get_dns_settings "$dir/$cfg" "$@"
    done
    debug "exit manage_interfaceconfig: $dir"
    return 0
}

# *********************
# EXECUTION STARTS HERE
# *********************

# just for the case we need the original value...
_NETCONFIG_DNS_RANKING="$NETCONFIG_DNS_RANKING"
case "$_NETCONFIG_DNS_RANKING" in
  auto) _NETCONFIG_DNS_RANKING="$NETCONFIG_DNS_RANKING_DEFAULT" ;;
  none) _NETCONFIG_DNS_RANKING=""                               ;;
esac

# just for the case we need the original value...
_NETCONFIG_DNS_POLICY=`netconfig_policy "$NETCONFIG_DNS_POLICY" dns`
if [ "x$_NETCONFIG_DNS_POLICY" = "x" ]; then
    #
    # empty policy means do not touch anything. 
    # successful exit.
    #
    exit 0;
fi

sf=0
_g=1
# disable filename glob expansion if needed
shopt -o -q noglob || _g=0
[ $_g ] && shopt -o -s noglob
for POL in $_NETCONFIG_DNS_POLICY; do
    shopt -o -u noglob
    case "$POL" in
    (NetworkManager)
        debug "Use NetworkManager policy merged settings"
        cfg="$ROOT$STATEDIR/NetworkManager.netconfig"
        if [ -r "$cfg" ] ; then
            get_dns_settings "$cfg" "$_NETCONFIG_DNS_RANKING"
        fi
        break
    ;;
    (STATIC)
        debug "Keep Static"
        DNS_SEARCHLIST_1="$DNS_SEARCHLIST_1 $NETCONFIG_DNS_STATIC_SEARCHLIST"
        DNS_SERVERS_1="$DNS_SERVERS_1 $NETCONFIG_DNS_STATIC_SERVERS"
    ;;
    (STATIC_FALLBACK)
        debug "Static Fallback"
        sf=1
    ;;
    (*)
        debug "Other: $POL"
        for IFDIR in $ROOT$STATEDIR/$POL; do
            test -d "$IFDIR" -a \
                 -d "$SYSFSDIR/${IFDIR##*/}" || continue
            # proceed every interface we find with this match
            manage_interfaceconfig  "$IFDIR" "$_NETCONFIG_DNS_RANKING"
        done
    ;;
    esac
done
[ $_g ] && shopt -o -u noglob

if [ $sf -eq 1 -a -z "$DNS_SEARCHLIST_0" \
               -a -z "$DNS_SEARCHLIST_1" ] ; then
    DNS_SEARCHLIST_2="$DNS_SEARCHLIST_2 $NETCONFIG_DNS_STATIC_SEARCHLIST"
fi
if [ $sf -eq 1 -a -z "$DNS_SERVERS_0" \
               -a -z "$DNS_SERVERS_1" ] ; then
    DNS_SERVERS_2="$DNS_SERVERS_2 $NETCONFIG_DNS_STATIC_SERVERS"
fi

# check if a nameserver module is used.
# If yes, don't write nameserver. glibc is using
# the name server on the local machine then ...
if [ "$NETCONFIG_DNS_FORWARDER" != "resolver" -a \
     "$NETCONFIG_DNS_FORWARDER" != "" ]; then
    # write explicitly specified loopback addresses (when any);
    # everything else will go into the forwarder specific config.
    lns=()
    rns_0=""
    rns_1=""
    rns_2=""
    for idx in 0 1 2 ; do
        var="DNS_SERVERS_$idx"
        val=(${!var})
        rns=()
        for ns in ${val[*]} ; do
            case $ns in
            127.*|::1) lns+=("$ns") ;;
            *)         rns+=("$ns") ;;
            esac
        done
        unset $var
        eval "rns_$idx='${rns[*]}'"
    done
    # fallback requires a localhost address, set when needed
    if  test "$NETCONFIG_DNS_FORWARDER_FALLBACK" = yes ;
    then
        test ${#lns[@]} -le 0 && lns=(127.0.0.1)
    fi
    # apply localhost addresses + fallback name servers
    if  test ${#lns[@]} -gt 0 ; then
       # local addresses have top prioriry in forwarder modes
       DNS_SERVERS_0="${lns[*]} $rns_0"
       DNS_SERVERS_1="$rns_1"
       DNS_SERVERS_2="$rns_2"
    fi
fi

write_resolv_conf "$DNS_SEARCHLIST_0 $DNS_SEARCHLIST_1 $DNS_SEARCHLIST_2" \
                  "$DNS_SERVERS_0 $DNS_SERVERS_1 $DNS_SERVERS_2"
RET=$?

if [ $RET -eq 1 ]; then
    # nothing changed; we are finished
    exit 0
elif [ $RET -eq 2 ]; then
    # user modified the config.
    echo "ATTENTION: $DESTLINK is not a link to $DESTFILE"
    echo "call \"netconfig update -f\" to adjust $DESTLINK"
    exit 20
fi

# here we should restart services if needed
# => not applicable to reload anything

exit 0;

# vim: set ts=8 sts=4 sw=4 ai et:
