diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/dns-forwarding/vyatta-dns-forwarding.pl | 289 | ||||
-rwxr-xr-x | scripts/rl-system.init | 2 | ||||
-rwxr-xr-x | scripts/system/vyatta_update_resolv.pl | 29 |
3 files changed, 310 insertions, 10 deletions
diff --git a/scripts/dns-forwarding/vyatta-dns-forwarding.pl b/scripts/dns-forwarding/vyatta-dns-forwarding.pl new file mode 100644 index 00000000..73a4bbe4 --- /dev/null +++ b/scripts/dns-forwarding/vyatta-dns-forwarding.pl @@ -0,0 +1,289 @@ +#!/usr/bin/perl +# +# Module: vyatta-dns-forwarding.pl +# +# **** License **** +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# 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. +# +# This code was originally developed by Vyatta, Inc. +# Portions created by Vyatta are Copyright (C) 2008 Vyatta, Inc. +# All Rights Reserved. +# +# Author: Mohit Mehta +# Date: August 2008 +# Description: Script to glue Vyatta CLI to dnsmasq daemon +# +# **** End License **** +# + +use lib "/opt/vyatta/share/perl5/"; +use VyattaConfig; +use VyattaMisc; +use Getopt::Long; + +use strict; +use warnings; + +my $dnsforwarding_init = '/etc/init.d/dnsmasq'; +my $dnsforwarding_conf = '/etc/dnsmasq.conf'; + + +sub dnsforwarding_restart { + system("$dnsforwarding_init restart >&/dev/null"); + print "Setting up DNS forwarding.\n"; +} + +sub dnsforwarding_stop { + system("$dnsforwarding_init stop >&/dev/null"); + print "Stopping DNS forwarding.\n"; +} + +sub dnsforwarding_get_constants { + my $output; + + my $date = `date`; + chomp $date; + $output = "#\n# autogenerated by vyatta-dns-forwarding.pl on $date\n#\n"; + $output .= "log-facility=/var/log/dnsmasq.log\n"; + system("rm -f /var/log/dnsmasq.log; touch /var/log/dnsmasq.log"); + return $output; +} + +sub dnsforwarding_get_values { + + my $dhclient_script = shift; + + my $output = ''; + my $config = new VyattaConfig; + my $use_dnsmasq_conf = 0; + my (@ignore_interfaces, $cache_size, @use_nameservers, $use_system_nameservers, @use_dhcp_nameservers); + + $config->setLevel("service dns forwarding"); + + if ($dhclient_script == 1){ + $config->{_active_dir_base} = "/opt/vyatta/config/active/"; + @ignore_interfaces = $config->returnOrigValues("ignore-interface"); + $cache_size = $config->returnOrigValue("cache-size"); + @use_nameservers = $config->returnOrigValues("name-server"); + $use_system_nameservers = $config->existsOrig("system"); + @use_dhcp_nameservers = $config->returnOrigValues("dhcp"); + + } else { + @ignore_interfaces = $config->returnValues("ignore-interface"); + $cache_size = $config->returnValue("cache-size"); + @use_nameservers = $config->returnValues("name-server"); + $use_system_nameservers = $config->exists("system"); + @use_dhcp_nameservers = $config->returnValues("dhcp"); + } + + if (@ignore_interfaces != 0) { + foreach my $interface (@ignore_interfaces) { + $output .= "except-interface=$interface\n"; + } + } + + if (defined $cache_size) { + $output .= "cache-size=$cache_size\n"; + } + + if (@use_nameservers != 0){ + $use_dnsmasq_conf = 1; + foreach my $cli_nameserver (@use_nameservers) { + $output .= "server=$cli_nameserver\t# statically configured\n"; + } + } + + if (defined($use_system_nameservers)) { + $use_dnsmasq_conf = 1; + my $sys_config = new VyattaConfig; + $sys_config->setLevel("system"); + my @system_nameservers; + if ($dhclient_script == 1){ + $sys_config->{_active_dir_base} = "/opt/vyatta/config/active/"; + @system_nameservers = $sys_config->returnOrigValues("name-server"); + } else { + @system_nameservers = $sys_config->returnValues("name-server"); + } + if (@system_nameservers > 0) { + foreach my $system_nameserver (@system_nameservers) { + $output .= "server=$system_nameserver\t# system\n"; + } + } + } + + if (@use_dhcp_nameservers != 0) { + $use_dnsmasq_conf = 1; + foreach my $interface (@use_dhcp_nameservers) { + my $dhcp_nameserver_count=`grep nameserver /etc/resolv.conf.dhclient-new-$interface | wc -l`; + if ($dhcp_nameserver_count > 0) { + my @dhcp_nameservers = `grep nameserver /etc/resolv.conf.dhclient-new-$interface`; + for my $each_nameserver (@dhcp_nameservers) { + my @nameserver = split(/ /, $each_nameserver, 2); + my $ns = $nameserver[1]; + chomp $ns; + $output .= "server=$ns\t# dhcp $interface\n"; + } + } + } + } + + if ($use_dnsmasq_conf == 1) { + $output .= "resolv-file=/etc/dnsmasq.conf\n"; + } + + return $output; +} + +sub dnsforwarding_write_file { + my ($config) = @_; + + open(my $fh, '>', $dnsforwarding_conf) || die "Couldn't open $dnsforwarding_conf - $!"; + print $fh $config; + close $fh; +} + +sub check_nameserver { + + my $cmd = `grep nameserver /etc/resolv.conf|wc -l`; + return $cmd; +} + +sub check_system_nameserver { + + my $config = new VyattaConfig; + $config->setLevel("system"); + my @system_nameservers = $config->returnValues("name-server"); + return(@system_nameservers); + +} + +sub check_dhcp_nameserver { + + my $intf = shift; + my $cmd = `grep nameserver /etc/resolv.conf.dhclient-new-$intf|wc -l`; + return $cmd; +} + +sub is_dhcp_enabled { + my $intf = shift; + + my $config = new VyattaConfig; + + if ($intf =~ m/^eth/) { + if ($intf =~ m/(\w+)\.(\d+)/) { + $config->setLevel("interfaces ethernet $1 vif $2"); + } else { + $config->setLevel("interfaces ethernet $intf"); + } + } elsif ($intf =~ m/^br/) { + $config->setLevel("interfaces bridge $intf"); + } else { + # + # currently we only support dhcp on ethernet + # and bridge interfaces. + # + return 0; + } + my @addrs = $config->returnOrigValues("address"); + foreach my $addr (@addrs) { + if (defined $addr && $addr eq "dhcp") { + return 1; + } + } + return 0; +} + +sub check_dhcp_interface { + + my $interface = shift; + + if (!is_dhcp_enabled($interface)) { + print "$interface is not using DHCP to get an IP address\n"; + return 0; + } + + if (-e "/var/run/vyatta/dhclient/dhclient_release_$interface") { + # dhcp released for the interface + print "DHCP lease for $interface has been released.\n"; + print "Renew lease for $interface before setting this parameter.\n"; + return 0; + } + + return 1; +} + + +# +# main +# + +my ($update_dnsforwarding, $stop_dnsforwarding, $system_nameserver, $dhcp_interface, $dhcp_interface_nameserver, $dhclient_script); + +GetOptions("update-dnsforwarding!" => \$update_dnsforwarding, + "stop-dnsforwarding!" => \$stop_dnsforwarding, + "system-nameserver!" => \$system_nameserver, + "dhcp-interface-nameserver=s" => \$dhcp_interface_nameserver, + "dhclient-script!" => \$dhclient_script, + "dhcp-interface=s" => \$dhcp_interface); + +if (defined $system_nameserver) { + my $system_nameserver_exists = check_system_nameserver(); + if ($system_nameserver_exists < 1){ + print "Warning: No DNS servers set in system to forward queries.\n"; + } +} + +if (defined $dhcp_interface_nameserver) { + my $dhcp_interface_nameserver_exists = check_dhcp_nameserver($dhcp_interface_nameserver); + if ($dhcp_interface_nameserver_exists < 1){ + print "Warning: No DNS servers received from DHCP server for $dhcp_interface_nameserver.\n"; + } +} + +if (defined $dhcp_interface) { + if (!check_dhcp_interface($dhcp_interface)){ + exit 1; + } +} + +if (defined $update_dnsforwarding) { + my $config; + my $vyatta_config = new VyattaConfig; + + $vyatta_config->setLevel("service dns forwarding"); + my $use_system_nameservers = $vyatta_config->exists("system"); + my @use_dhcp_nameservers = $vyatta_config->returnValues("dhcp"); + my @use_nameservers = $vyatta_config->returnValues("name-server"); + + if (!(defined $use_system_nameservers) && (@use_dhcp_nameservers == 0) && (@use_nameservers == 0)) { + my $nameserver_exists = check_nameserver(); + if ($nameserver_exists < 1){ + print "Warning: No DNS servers ('system set' or 'dhcp received') to forward queries.\n"; + } + } + + my $called_from_dhclient_script = 0; + if (defined $dhclient_script){ + $called_from_dhclient_script = 1; + } + $config = dnsforwarding_get_constants(); + $config .= dnsforwarding_get_values($called_from_dhclient_script); + dnsforwarding_write_file($config); + dnsforwarding_restart(); +} + +if (defined $stop_dnsforwarding) { + dnsforwarding_stop(); +} + +exit 0; + +# end of file + diff --git a/scripts/rl-system.init b/scripts/rl-system.init index 23d67d12..0f6957ad 100755 --- a/scripts/rl-system.init +++ b/scripts/rl-system.init @@ -121,7 +121,7 @@ clear_or_override_config_files () udev_rescan () { rm -f /tmp/vyatta_net_name_* - udevtrigger --subsystem-match=net && udevsettle + udevadm trigger --subsystem-match=net && udevsettle for ff in /tmp/vyatta_net_name_* ; do f=${ff##*/} cmd_name_hwid=${f/vyatta_net_name_/} diff --git a/scripts/system/vyatta_update_resolv.pl b/scripts/system/vyatta_update_resolv.pl index b35cae4a..8f0e9ead 100755 --- a/scripts/system/vyatta_update_resolv.pl +++ b/scripts/system/vyatta_update_resolv.pl @@ -74,14 +74,14 @@ foreach my $domain (@domains) { # add domain names received from dhcp client to domain search in /etc/resolv.conf if domain-name not set in CLI if (!defined($domain_name)) { - my @dhcp_interfaces_resolv_files = `ls /etc/ | grep dhclient.new`; + my @dhcp_interfaces_resolv_files = `ls /etc/ | grep resolv.conf.dhclient-new`; if ($#dhcp_interfaces_resolv_files >= 0) { for my $each_file (@dhcp_interfaces_resolv_files) { chomp $each_file; - my $find_search = `grep search /etc/$each_file 2> /dev/null | wc -l`; + my $find_search = `grep "^search" /etc/$each_file 2> /dev/null | wc -l`; if ($find_search == 1) { - my $search_string = `grep search /etc/$each_file`; - my @dhcp_domains = split(/ /, $search_string, 2); + my $search_string = `grep "^search" /etc/$each_file`; + my @dhcp_domains = split(/\s+/, $search_string, 2); my $dhcp_domain = $dhcp_domains[1]; chomp $dhcp_domain; $doms .= ' ' . $dhcp_domain; @@ -107,7 +107,7 @@ if ($dhclient_script == 1) { # code below to add new name-servers received from dhcp client - my @dhcp_interfaces_resolv_files = `ls /etc/ | grep dhclient.new`; + my @dhcp_interfaces_resolv_files = `ls /etc/ | grep resolv.conf.dhclient-new`; if ($#dhcp_interfaces_resolv_files >= 0) { my $ns_count = 0; for my $each_file (@dhcp_interfaces_resolv_files) { @@ -121,8 +121,19 @@ if ($dhclient_script == 1) { chomp $ns; $current_dhcp_nameservers[$ns_count] = $ns; $ns_count++; - my $search_ns_in_resolvconf = `grep $ns /etc/resolv.conf 2> /dev/null | wc -l`; - if ($search_ns_in_resolvconf == 0) { + my @search_ns_in_resolvconf = `grep $ns /etc/resolv.conf`; + my $ns_in_resolvconf = 0; + if (@search_ns_in_resolvconf > 0) { + foreach my $ns_resolvconf (@search_ns_in_resolvconf) { + my @resolv_ns = split(/\s+/, $ns_resolvconf); + my $final_ns = $resolv_ns[1]; + chomp $final_ns; + if ($final_ns eq $ns) { + $ns_in_resolvconf = 1; + } + } + } + if ($ns_in_resolvconf == 0) { open (APPEND, ">>/etc/resolv.conf") or die "$! error trying to overwrite"; print APPEND "nameserver\t$ns\t\t#nameserver written by $0\n"; close (APPEND); @@ -145,7 +156,7 @@ if ($dhclient_script == 1) { } if ($#current_dhcp_nameservers < 0) { for my $dhcpnameserver (@dhcp_nameservers_in_resolvconf) { - my $cmd = "sed -i '/$dhcpnameserver/d' /etc/resolv.conf"; + my $cmd = "sed -i '/$dhcpnameserver\t/d' /etc/resolv.conf"; system($cmd); $restart_ntp = 1; } @@ -158,7 +169,7 @@ if ($dhclient_script == 1) { } } if ($found == 0) { - my $cmd = "sed -i '/$dhcpnameserver/d' /etc/resolv.conf"; + my $cmd = "sed -i '/$dhcpnameserver\t/d' /etc/resolv.conf"; system($cmd); $restart_ntp = 1; } |