#------------------------------------------------------------------------------
# /check/base.ext - extended check of base parameters              3.0.1
#
# Last Update:  $Id: base.ext 10842 2006-01-18 10:33:02Z babel $
#------------------------------------------------------------------------------

provides fli4l version 3.0.1

crypt (PASSWORD)

if (start_imond && !opt_isdn && !opt_pppoe && !opt_pppoe_circ && !opt_pptp && !opt_modem && !opt_fritzdsl && !opt_umts)
then
    error "if you don't use ISDN or PPPOE/PPTP/FRITZDSL, you must set START_IMOND='no'"
fi

foreach i in net_drv_%
do
    if (copy_pending("%i"))
    then
        error "No network driver found for %i='$i', check config/base.txt"
    fi
    if (i =~ "(RE:TR_DRV)")
    then
        set tr_used
    fi
done

if (ip_net_n == 0 && (!defined (ignore_ip_net_warning) || ! ignore_ip_net_warning))
then
    warning "You should configure at least one network device. If you actually don't have network devices or use dhcp or similiar to configure them, disable this warning by setting IGNORE_IP_NET_WARNING='yes' in your config file"
fi

foreach i in ip_net_n
do
    if (ip_net_%[i] == "" && ip_net_%_mac[i] == "")
    then
        error "IP_NET_$i must not be empty if IP_NET_${i}_MAC is not set!"
    fi
done

foreach i in ip_net_n
do
    foreach j in ip_net_n
    do
        set net1=ip_net_%[i]
        set net2=ip_net_%[j]
        if (i != j)
        then
            set if1=ip_net_%_dev[i]
            set if2=ip_net_%_dev[j]

            if( if1 == if2 )
            then
                error "You can't use the same interface more than once.  IP_NET_$i='$net1' and IP_NET_$j='$net2' use the same interface."
            else
                if ("$net1" != "" && "$net2" != "")
                then
                    if (subnet ("$net1", "$net2"))
                    then
                    warning "Possibly invalid IP_NET configuration, IP_NET_$i='$net1' and IP_NET_$j='$net2' overlap each other."
                fi
                fi
            fi
        fi
    done
done

if (tr_used)
then
    set tr_dev_req = "no"
    foreach i in ip_net_%_dev
    do
	if (i =~ "tr[0-9]+")
	then
	    set tr_dev_req = "yes"
	else
	    if (tr_dev_req == "yes")
	    then
		error "please specify tokenring devices at the end of IP_NET-configuration"
	    fi
	fi
    done
    if (tr_dev_req == "no")
    then
	warning "If you want to configure your tokenring devices via IP_NET_* you have to specify IP_NET_x_DEV='tr?', otherwise it will assume you are using ethernet device only."
    fi

fi

if (ip_route_n != 0)
then
    set defaultroute="no"
    foreach i in ip_route_n
    do
        if (ip_route_%[i] =~ "^0\.0\.0\.0/0[[:space:]]+.*")
        then
            if(defaultroute == "yes")
            then
                error "You can't have more than one default route in your IP_ROUTE configuration. You must remove the defaultroute in IP_ROUTE_${i}."
            fi
            set defaultroute="yes"
        fi
    done
fi

# we check later if the masq modul is used
set masq_ftp="no"
foreach i in masq_module_%
do
    if (copy_pending("%i") && i!="udp_dloose")
    then
	error "No masq module found for %i='$i', check config/base.txt"
    fi
    if (i == "ftp")
    then
      set masq_ftp="yes"
    fi
done

if (opt_dns != "no")
then
    foreach i in host_%_name
    do
	if (i == hostname)
	then
	    set found
	fi
        if (i == "HOSTNAME")
        then
            set found
        fi
    done
    if (!found)
    then
	error "DNS Configuration: Missing host entry (HOST_x_NAME) for fli4l router '$hostname' or Alias-Name (HOSTNAME) for Name set in var HOSTNAME"
    fi
fi

if(domain_name =~ ".+\.local$")
then
    warning "Your DOMAIN is '$domain_name' - on some OS there are problems with this DOMAIN-name"
fi

if (opt_pnp)
then
    stat ("opt/etc/isapnp.conf", res)
    if (res_res == "OK")
    then
	if (res_size == 74)
	then
	    warning "unconfigured isapnp.conf found, please create a valid opt/etc/isapnp.conf (see documentation to find out how)"
	fi
    else
	error "$res_res: while trying to check for opt/etc/isapnp.conf"
    fi
fi

stat ("check/crlf-test.txt", res)
if (res_res == "OK")
then
    if (res_size != 11)
    then
        error "If you used WinZip to uncompress the Fli4l archives switch of Smart-Tar-CRLF-Conversion and do it again."
    fi
else
    error "$res_res: while trying to check size of check/crlf-test.txt"
fi

stat ("opt/etc/issue", res)
if (res_res != "OK")
then
    error "$res_res: Maybe you used WinACE and didn't read the warning on the download page?"
fi

if (opt_klogd && !opt_syslogd)
then
    error "if you want to use klogd, you have to enable syslogd too (set OPT_SYSLOGD='yes')"
fi

if (portfw_n != 0)
then
    foreach i in portfw_n
    do
        if (portfw_%_target[i] =~ ".*-.*" && portfw_%_new_target[i] =~ ".*:.*")
        then
            error "you can't forward a portrange to one port (PORTFW_$i)"
        fi
	if (portfw_%_protocol[i] == "gre" && ! portfw_%_target[i] =~ ".*none")
	then
	    error "you can't specify a destination port if you wan't to forward the gre protocol"
	fi
        foreach j in portfw_n
	do
	    if (i<j && portfw_%_target[i]==portfw_%_target[j] && portfw_%_protocol[i]==portfw_%_protocol[j])
	    then
	        error "PORTFW_${i}_TARGET and PORTFW_${j}_TARGET conflict: you can't forward the same port to multiple hosts"
	    fi
	done
    done
fi

if ( orig_fw_config || ! new_fw_config )
then
    warning "The original packet filter configuration is deprecated, please use the new one (by deleting ORIG_FW_CONFIG and setting NEW_FW_CONFIG='yes')."
fi

if ( (orig_fw_config && new_fw_config) || (!orig_fw_config && !new_fw_config))
then
    error "choose either orig_fw_config or new_fw_config"
fi

if (defined (MASQ_NETWORK) || defined(ROUTE_NETWORK) || defined(TRUSTED_NETS))
then
   warning "Please consider removing all config variables of the original packetfilter section (except ORIG_FW_CONFIG) from your config file to avoid any confusion."
fi

if (defined (orig_fw_config) && orig_fw_config )
then
   warning "Please consider moving to the new packet filter configuration. During boot up the router will translate the original to the new configuration; copy the result to your config file and remove the original packet filter configuration."
    warning "If you would like to keep the original configuration please consider removing all config variables of the new packetfilter section (except NEW_FW_CONFIG) from your config file to avoid any confusion."
fi


if (boot_type =~ "integrated|attached" && mount_boot != "no")
then
    error "you must set MOUNT_BOOT='no' if you want to use BOOT_TYPE integrated or attached'"
fi

if (boot_type =~ "hd|cd|cdemul|ls120" && !opt_hddrv)
then
    error "you must install and configure opt_hddrv (hd package) to be able to boot from hard disk or cdrom"
fi

foreach rule in prerouting_list_% postrouting_list_% input_list_% dmz_orange_router_%
do
    if (rule =~ "if:.*[[:space:]]BIDIRECTIONAL")
    then
	error "you can't use an interface specification with bidirectional in %rule='$rule'"  
    fi
done

set tmpl_ftp="no"

set templates=""
foreach rule in prerouting_list_% postrouting_list_% input_list_% forward_list_% user_list_% dmz_orange_red_% dmz_orange_router_%
do
    if (rule =~ "tmpl:([^[:space:]]+)")
    then
	set tmpl=match_%[1]
	if (! templates =~ ".*$tmpl.*")
	then
	    set templates="$templates $tmpl"

	    if(tmpl == "ftp")
	    then
		set tmpl_ftp="yes"
	    fi
	    
	    stat("$config_dir/etc/fwrules.tmpl/$tmpl", tmplfile)
	    if(tmplfile_res == "OK")
	    then
		add_to_opt "etc/fwrules.tmpl/$tmpl" "flags=utxt"
	    else
		stat("opt/etc/fwrules.tmpl/$tmpl", tmplfile)
		if(tmplfile_res == "OK")
		then
		    add_to_opt "etc/fwrules.tmpl/$tmpl" "flags=utxt"
		else
		    fgrep("opt/etc/fwrules.tmpl/templates", "^$tmpl[[:space:]]+")
		    if (fgrep_match_n == 0)
		    then
			error "Can't find template '$tmpl', check your packet filter configuration!"
		    fi
		fi
	    fi
	fi
    fi

    # check if each host that is used via @host syntax is
    # defined in the fli4l host list
    if (rule =~ "@([^[:space:]:]+)")
    then
      set host_is_defined="no"
      set host_wanted=match_%[1]
      foreach m in HOSTS_N
      do
        if ("$host_wanted" == HOST_%_NAME[m])
        then
          set host_is_defined
        fi
        set host_aliases=HOST_%_ALIAS_N[m]
        foreach n in host_aliases
        do
          set host=HOST_%_ALIAS_%[m][n]
          if ("$host_wanted" == "$host")
          then
            set host_is_defined
          fi
        done      
      done
      if(!host_is_defined)
      then
        error "The host $host_wanted referenced in the paket filtr configuration is not defined in the HOST section!"
      fi
   fi

done

# check if tmpl:ftp is used
if(tmpl_ftp == "yes" && masq_ftp == "no")
then
  warning "You're using a tmpl:ftp without the ftp masq module. You have to open the data port 20 (tcp) to use active ftp."
fi

if (OPT_DNSDHCP)
then
  if (OPT_DHCP)
  then
    error "DNSMASQ with internal DHCPD: sorry, only one dhcpd daemon on fli4l is allowed. Please disable opt_dhcp or opt_dnsdhcp."
  fi

  if (! OPT_DNS)
  then
    error "DNSMASQ with internal DHCP: sorry, you must activate the DNS-Server in base.txt. Set OPT_DNS to yes"
  fi

  foreach n in IP_NET_N
  do
    set checknet=IP_NET_%[n]
    set dhcp_start=DNSDHCP_RANGE_%_START[n]
    set dhcp_end=DNSDHCP_RANGE_%_END[n]

    if ("$dhcp_start" != "") && ("$dhcp_end" != "")
    then
      if(!subnet("$dhcp_start", "$checknet"))
      then
        error "DNSMASQ with DHCPD: DNSDHCP_RANGE_${n}_START is not a valid IP of the network defined in IP_NET_${n}"
      fi
      if(!subnet("$dhcp_end", "$checknet"))
      then
        error "DNSMASQ with DHCP: DNSDHCP_RANGE_${n}_END is not a valid IP of the network defined in IP_NET_${n}"
      fi

      split (dhcp_start, tmp_s_%, '.', numeric)
      split (dhcp_end, tmp_e_%, '.', numeric)

      set n_start=((tmp_s_%[1]*16777216)+(tmp_s_%[2]*65536)+(tmp_s_%[3]*256)+(tmp_s_%[4]))
      set n_end=((tmp_e_%[1]*16777216)+(tmp_e_%[2]*65536)+(tmp_e_%[3]*256)+(tmp_e_%[4]))

      if (n_start >= n_end)
      then
        error "DNSMASQ with DHCP: DNSDHCP_RANGE_${n}_START and DNSDHCP_RANGE_${n}_END defines not a valid range"
      fi

      foreach m in HOSTS_N
      do
        set host_mac=HOST_%_MAC[m]
        if ("$host_mac" != "")
        then
          set host_ip=HOST_%_IP[m]
	  if (host_ip =~ "(RE:IPADDR)")
	  then
	    split (host_ip, tmp_h_%, '.', numeric)
	    set n_host=((tmp_h_%[1]*16777216)+(tmp_h_%[2]*65536)+(tmp_h_%[3]*256)+(tmp_h_%[4]))
	    if ((n_host <= n_end) && (n_host >= n_start))
	    then
		error "DNSMASQ with DHCP: IP of HOST_${m} is in DHCP_RANGE_${n}"
	    fi
	  fi
        fi
      done
    fi
  done

  foreach n in DNSDHCP_EXTRA_RANGE_N
  do
    set dhcp_e_start=DNSDHCP_EXTRA_RANGE_%_START[n]
    set dhcp_e_end=DNSDHCP_EXTRA_RANGE_%_END[n]
    set dhcp_e_gw=DNSDHCP_EXTRA_RANGE_%_GATEWAY[n]
    set dhcp_e_mask=DNSDHCP_EXTRA_RANGE_%_NETMASK[n]
    if ("$dhcp_e_start" != "") && ("$dhcp_e_end" != "")
    then
      if(!samenet("$dhcp_e_start/$dhcp_e_mask", "$dhcp_e_end/$dhcp_e_mask"))
      then
        error "DNSMASQ with DHCP: DNSDHCP_EXTRA_RANGE_${n}_START, DNSDHCP_EXTRA_RANGE_${n}_END and DNSDHCP_EXTRA_RANGE_${n}_NETMASK defines not a valid network"
      fi
      if(!subnet("$dhcp_e_gw", "$dhcp_e_end/$dhcp_e_mask"))
      then
        error "DNSMASQ with DHCP: DNSDHCP_EXTRA_RANGE_${n}_GATEWAY are not in the Network defined in DNSDHCP_EXTRA_RANGE_${n}_*"
      fi

      split (dhcp_e_start, tmp_s_%, '.', numeric)
      split (dhcp_e_end, tmp_e_%, '.', numeric)

      set n_start=((tmp_s_%[1]*16777216)+(tmp_s_%[2]*65536)+(tmp_s_%[3]*256)+(tmp_s_%[4]))
      set n_end=((tmp_e_%[1]*16777216)+(tmp_e_%[2]*65536)+(tmp_e_%[3]*256)+(tmp_e_%[4]))

      if (n_start >= n_end)
      then
        error "DNSMASQ with DHCP: DNSDHCP_EXTRA_RANGE_${n}_START and DNSDHCP_EXTRA_RANGE_${n}_END defines not a valid range"
      fi

      foreach m in HOSTS_N
      do
        set host_mac=HOST_%_MAC[m]
        if ("$host_mac" != "")
        then
          set host_ip=HOST_%_IP[m]
          split (host_ip, tmp_h_%, '.', numeric)
          set n_host=((tmp_h_%[1]*16777216)+(tmp_h_%[2]*65536)+(tmp_h_%[3]*256)+(tmp_h_%[4]))
          if ((n_host <= n_end) && (n_host >= n_start))
          then
            error "DNSMASQ with DHCP: IP of HOST_${m} is in DHCP_EXTRA_RANGE_${n}"
          fi
        fi
      done
    fi
  done
fi

if (keyboard_locale != "" && keyboard_locale != "off")
then
  set keyboard_mapfile="in_kernel"
  if (keyboard_locale == "auto")
  then
    set keyboard_mapfile=locale
    if (locale == "de")
    then
      set keyboard_mapfile = "de-latin1-nodeadkeys"
    fi
    if (locale == "fr")
    then
      set keyboard_mapfile = "fr-latin1"
    fi
    if (locale == "en" || locale == "nl")
    then
      set keyboard_mapfile = "in_kernel"
    fi
  else
    set keyboard_mapfile=keyboard_locale
  fi
  if (keyboard_mapfile != "in_kernel")
  then
    stat("opt/etc/$keyboard_mapfile.map", keymap_file)
    stat("$config_dir/etc/$keyboard_mapfile.map", keymap_conf_file)
    if(keymap_file_res == "OK" || keymap_conf_file_res == "OK")
    then
      add_to_opt "etc/$keyboard_mapfile.map"
    else
      error "There is no keyboard mapfile $keyboard_locale.map available in opt/etc that matches your KEYBOARD_LOCALE='$KEYBOARD_LOCALE' setting!"
    fi
  fi
fi

if (opt_makekbl && opt_menu)
then
    add_to_opt "etc/menu/700-makekbl" "flags=utxt"
fi

foreach i in HOSTS_N
do
    foreach j in HOSTS_N
    do
	set hostip1=host_%_ip[i]
	set hostip2=host_%_ip[j]
	if (i != j)
	then
            if( hostip1 == hostip2 )
            then
                error "You can't use the same IP for more than one Host.  HOST_${i}_IP='$hostip1' and HOST_${j}_IP='$hostip2' use the same IP."
            fi
	fi
    done
done

if (opt_mountfloppy != "no")
then
    set type="$opt_mountfloppy$boot_type"
    if (type =~ "^(first(fd|dual)|seconddual)")
    then
        error "OPT_MOUNTFLOPPY='$opt_mountfloppy' is not available with BOOT_TYPE='$BOOT_TYPE'"
    fi
fi
