diff options
author | Mohit Mehta <mohit.mehta@vyatta.com> | 2008-08-06 22:15:10 +0000 |
---|---|---|
committer | Mohit Mehta <mohit.mehta@vyatta.com> | 2008-08-06 22:15:10 +0000 |
commit | dc4d53c9eb42a6f9f27f0de762de85ce46b6f788 (patch) | |
tree | cf9a60ff7c6b31b239ad0b54ce164eb1fab65e04 | |
parent | 0f78fcfe3c21e4d07b402aaae75bb494fc4e4133 (diff) | |
download | vyatta-cfg-quagga-dc4d53c9eb42a6f9f27f0de762de85ce46b6f788.tar.gz vyatta-cfg-quagga-dc4d53c9eb42a6f9f27f0de762de85ce46b6f788.zip |
code to specify DNS forwarding nameservers (system, dhcp, explicitly specified) using CLI
-rw-r--r-- | scripts/dns-forwarding/vyatta-dns-forwarding.pl | 128 | ||||
-rw-r--r-- | templates/service/dns/forwarding/dhcp/node.def | 9 | ||||
-rw-r--r-- | templates/service/dns/forwarding/name-server/node.def | 3 | ||||
-rw-r--r-- | templates/service/dns/forwarding/node.def | 4 | ||||
-rw-r--r-- | templates/service/dns/forwarding/system/node.def | 2 |
5 files changed, 118 insertions, 28 deletions
diff --git a/scripts/dns-forwarding/vyatta-dns-forwarding.pl b/scripts/dns-forwarding/vyatta-dns-forwarding.pl index 2cfe75e9..60f61e16 100644 --- a/scripts/dns-forwarding/vyatta-dns-forwarding.pl +++ b/scripts/dns-forwarding/vyatta-dns-forwarding.pl @@ -34,9 +34,6 @@ use warnings; my $dnsforwarding_init = '/etc/init.d/dnsmasq'; my $dnsforwarding_conf = '/etc/dnsmasq.conf'; -sub dnsforwarding_init { - -} sub dnsforwarding_restart { system("$dnsforwarding_init restart >&/dev/null"); @@ -92,40 +89,121 @@ sub check_nameserver { return $cmd; } +sub check_system_nameserver { + + my $num_all_nameservers = `grep nameserver /etc/resolv.conf|wc -l`; + my $num_dhcp_nameservers = `grep nameserver /etc/resolv.conf| grep vyatta_update_resolv|wc -l`; + return ($num_all_nameservers - $num_dhcp_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 $init_dnsforwarding; -my $update_dnsforwarding; -my $stop_dnsforwarding; -my $nameserver; - -GetOptions("init-dnsforwarding!" => \$init_dnsforwarding, - "update-dnsforwarding!" => \$update_dnsforwarding, - "stop-dnsforwarding!" => \$stop_dnsforwarding, - "nameserver!" => \$nameserver); - -if (defined $nameserver) { - my $nameserver_exists = check_nameserver(); - if ($nameserver_exists < 1){ - exit 1; - } else { - exit 0; + +my ($update_dnsforwarding, $stop_dnsforwarding, $system_nameserver, $dhcp_interface, $dhcp_interface_nameserver); + +GetOptions("update-dnsforwarding!" => \$update_dnsforwarding, + "stop-dnsforwarding!" => \$stop_dnsforwarding, + "system-nameserver!" => \$system_nameserver, + "dhcp-interface-nameserver=s" => \$dhcp_interface_nameserver, + "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 $init_dnsforwarding) { - dnsforwarding_init(); +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"; + } + } - $config = dnsforwarding_get_constants(); - $config .= dnsforwarding_get_values(); - dnsforwarding_write_file($config); - dnsforwarding_restart(); + $config = dnsforwarding_get_constants(); + $config .= dnsforwarding_get_values(); + dnsforwarding_write_file($config); + dnsforwarding_restart(); } if (defined $stop_dnsforwarding) { diff --git a/templates/service/dns/forwarding/dhcp/node.def b/templates/service/dns/forwarding/dhcp/node.def new file mode 100644 index 00000000..24598129 --- /dev/null +++ b/templates/service/dns/forwarding/dhcp/node.def @@ -0,0 +1,9 @@ +multi: +type: txt +help: Set to forward DNS queries to nameservers received from DHCP server for specified interface +syntax:expression: exec "/opt/vyatta/sbin/vyatta-dns-forwarding.pl --dhcp-interface $VAR(@)" +commit:expression: exec "/opt/vyatta/sbin/vyatta-dns-forwarding.pl --dhcp-interface-nameserver $VAR(@)" +allowed: + local -a array ; + array=( /var/lib/dhcp3/eth* ) ; + echo -n ${array[@]##*/} diff --git a/templates/service/dns/forwarding/name-server/node.def b/templates/service/dns/forwarding/name-server/node.def new file mode 100644 index 00000000..ad67c6de --- /dev/null +++ b/templates/service/dns/forwarding/name-server/node.def @@ -0,0 +1,3 @@ +multi: +type: ipv4 +help: Set DNS server to forward queries diff --git a/templates/service/dns/forwarding/node.def b/templates/service/dns/forwarding/node.def index f84e4f02..46dbd199 100644 --- a/templates/service/dns/forwarding/node.def +++ b/templates/service/dns/forwarding/node.def @@ -1,6 +1,4 @@ -help: Configure Domain Name Server (DNS) forwarding -syntax:expression: exec "/opt/vyatta/sbin/vyatta-dns-forwarding.pl --nameserver"; "No nameserver exists to forward DNS queries" -create:expression: "/opt/vyatta/sbin/vyatta-dns-forwarding.pl --init-dnsforwarding" +help: Configure DNS forwarding delete:expression: "touch /tmp/dnsmasq.$PPID" end:expression: "if [ -f \"/tmp/dnsmasq.$PPID\" ]; then \ sudo /opt/vyatta/sbin/vyatta-dns-forwarding.pl --stop-dnsforwarding \ diff --git a/templates/service/dns/forwarding/system/node.def b/templates/service/dns/forwarding/system/node.def new file mode 100644 index 00000000..e92a7ad6 --- /dev/null +++ b/templates/service/dns/forwarding/system/node.def @@ -0,0 +1,2 @@ +help: Set DNS forwarding to system nameservers +commit:expression: exec "/opt/vyatta/sbin/vyatta-dns-forwarding.pl --system-nameserver" |