#------------------------------------------------------------------------------
# /etc/rc.d/rc370.dnsmasq - configure dnsmasq
#
# Creation:     21.12.2003  george
# Last Update:  $Id: rc370.dnsmasq 10994 2006-02-15 11:58:37Z jw5 $
#------------------------------------------------------------------------------

if [ "$OPT_DNS" = "yes" ]
then
    begin_script DNS "starting dnsmasq server ..."

    if [ "$OPT_DNSDHCP" = "yes" ]
    then
        echo "DHCP" > /var/run/dnsmasq.dhcp
    fi

    # create etc/resolv.dnsmasq
    # forwarder for dnsmasq - changed by ip-up.dnsmasq if USEPEERDNS
    {
      echo "# forwarders for dnsmasq"
      for j in $DNS_FORWARDERS
      do
        echo "nameserver $j"
      done
    } > /etc/resolv.dnsmasq

    chown dns:dns /etc/resolv.dnsmasq
    chmod 600 /etc/resolv.dnsmasq

    # create etc/dnsmasq.conf
    {
        echo "user=dns"
        echo "group=dns"
        echo "resolv-file=/etc/resolv.dnsmasq"
        echo "no-poll"
        echo "no-negcache"
        # echo "strict-order"

        if [ "$DNS_BOGUS_PRIV" = "yes" ]
        then
          echo "bogus-priv"
        fi

        if [ "$DNS_VERBOSE" = "yes" ]
        then
            echo "log-queries"
        fi

        # Listen-IP's (Interfaces)
        if [ 0"$DNS_LISTENIP_N" -ne 0 ]
        then
            echo "listen-address=127.0.0.1"
            for idx in `seq 1 $DNS_LISTENIP_N`
            do
                eval translate_ip_net '$DNS_LISTENIP_'$idx
                if ! is_error
		then
		    if ifconfig | grep -q "$res "
		    then
			echo "listen-address=$res"
		    else
			eval log_error "LISTENIP_$idx \$DNS_LISTENIP_$idx is not a local IP"
		    fi
		fi
            done
        fi

        echo "domain-suffix=$DOMAIN_NAME"
        # do not forward querys for local-Domain
        echo "local=/$DOMAIN_NAME/"
        echo "domain-needed"
        echo "expand-hosts"

        if [ "$DNS_MX_SERVER" != "" ]
        then
            echo "localmx"
            echo "mx-target=$DNS_MX_SERVER"
        fi

        # forbidden Domains
        idx=1
        while [ "$idx" -le "$DNS_FORBIDDEN_N" ]
        do
            eval dom='$DNS_FORBIDDEN_'$idx
            echo "address=/$dom/127.0.0.1"
            idx=`expr $idx + 1`
        done
	if [ -f /etc/dnsmasq_forbiddendom.conf ]
	then
	    echo "conf-file=/etc/dnsmasq_forbiddendom.conf"
	fi

        # redirect Domains
        idx=1
        while [ "$idx" -le "$DNS_REDIRECT_N" ]
        do
            eval dom='$DNS_REDIRECT_'$idx
            eval domip='$DNS_REDIRECT_'$idx'_IP'
            echo "address=/$dom/$domip"
            idx=`expr $idx + 1`
        done
        
	if [ "$DNS_FILTERWIN2K" = "yes" ]
	then
            echo "filterwin2k"
	fi

        # forwarders for given domains

        echo "# /etc/dnsmasq_special.conf" > /etc/dnsmasq_special.conf
        idx=1
        while [ "$idx" -le "$DNS_SPECIAL_N" ]
        do
            eval ns='$DNS_SPECIAL_'$idx
            ns=`echo $ns|tr ' ' /`
            echo "server=/$ns" >> /etc/dnsmasq_special.conf
            idx=`expr $idx + 1`
        done
        # include file with special-DNS-configs
        # forwarders for given domains - extra file can be updated by
        # vpn-config
        chown dns:dns /etc/dnsmasq_special.conf
        chmod 600 /etc/dnsmasq_special.conf
        echo "conf-file=/etc/dnsmasq_special.conf"

	# IP used to trigger dial-in
	case $DNS_TRIGGER_DIAL_IN in
	     yes)     echo "dial-in-trigger-ip=169.254.0.1,9,/var/run/online" ;;
	     *.*.*.*) echo "dial-in-trigger-ip=$DNS_TRIGGER_DIAL_IN,9,/var/run/online" ;;
	esac

	if [ "$OPT_DNSDHCP" = "yes" ]
	then
	    # create conf for dhcp
	    if [ -f /etc/rc.d/dnsdhcp_helper.sh ]
	    then
	        . /etc/rc.d/dnsdhcp_helper.sh
			if [ -f /etc/dnsmasq_dhcp.conf ]
            then
                echo "conf-file=/etc/dnsmasq_dhcp.conf"
			fi
	    fi
	fi
    } > /etc/dnsmasq.conf

    chown dns:dns /etc/dnsmasq.conf
    chmod 600 /etc/dnsmasq.conf

    dnsmasq || log_error "dnsmasq not started!"

    end_script
fi
