From 29446d9ed7410a571ce77ee9a9f601c9653c5219 Mon Sep 17 00:00:00 2001 From: Mohit Mehta Date: Fri, 25 Jul 2008 23:14:12 +0000 Subject: First parts of DNS forwarding stuff --- scripts/dns-forwarding/vyatta-dns-forwarding.pl | 126 ++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 scripts/dns-forwarding/vyatta-dns-forwarding.pl (limited to 'scripts/dns-forwarding') diff --git a/scripts/dns-forwarding/vyatta-dns-forwarding.pl b/scripts/dns-forwarding/vyatta-dns-forwarding.pl new file mode 100644 index 00000000..01111d4a --- /dev/null +++ b/scripts/dns-forwarding/vyatta-dns-forwarding.pl @@ -0,0 +1,126 @@ +#!/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_init { + +} + +sub dnsforwarding_restart { + system("$dnsforwarding_init restart 2&>1 /dev/null"); + print "Setting up DNS forwarding.\n"; +} + +sub dnsforwarding_stop { + system("$dnsforwarding_init stop 2&>1 /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"; + return $output; +} + +sub dnsforwarding_get_values { + my $output = ''; + my $config = new VyattaConfig; + + $config->setLevel("service dns-forwarding"); + + 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; +} + +# +# 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; + } +} + + +if (defined $init_dnsforwarding) { + dnsforwarding_init(); +} + +if (defined $update_dnsforwarding) { + my $config; + + $config = dnsforwarding_get_constants(); + $config .= dnsforwarding_get_values(); + dnsforwarding_write_file($config); + dnsforwarding_restart(); +} + +if (defined $stop_dnsforwarding) { + dnsforwarding_stop(); +} + +exit 0; + +# end of file + -- cgit v1.2.3 From d49c9edf95ecb4a94ad3800c336d081030e764c1 Mon Sep 17 00:00:00 2001 From: Mohit Mehta Date: Tue, 5 Aug 2008 01:53:32 +0000 Subject: - add ability to set cache-size for DNS forwarding from CLI - restart dnsmasq when /etc/hosts is modified from CLI to re-read added or deleted hosts --- scripts/dns-forwarding/vyatta-dns-forwarding.pl | 8 ++++++-- templates/service/dns-forwarding/cache-size/node.def | 4 ++++ templates/system/static-host-mapping/host-name/node.def | 17 +++++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 templates/service/dns-forwarding/cache-size/node.def (limited to 'scripts/dns-forwarding') diff --git a/scripts/dns-forwarding/vyatta-dns-forwarding.pl b/scripts/dns-forwarding/vyatta-dns-forwarding.pl index 01111d4a..4eebf6d7 100644 --- a/scripts/dns-forwarding/vyatta-dns-forwarding.pl +++ b/scripts/dns-forwarding/vyatta-dns-forwarding.pl @@ -39,12 +39,12 @@ sub dnsforwarding_init { } sub dnsforwarding_restart { - system("$dnsforwarding_init restart 2&>1 /dev/null"); + system("$dnsforwarding_init restart >&/dev/null"); print "Setting up DNS forwarding.\n"; } sub dnsforwarding_stop { - system("$dnsforwarding_init stop 2&>1 /dev/null"); + system("$dnsforwarding_init stop >&/dev/null"); print "Stopping DNS forwarding.\n"; } @@ -62,6 +62,10 @@ sub dnsforwarding_get_values { my $config = new VyattaConfig; $config->setLevel("service dns-forwarding"); + my $cache_size = $config->returnValue("cache-size"); + if (defined $cache_size) { + $output .= "cache-size=$cache_size\n"; + } return $output; } diff --git a/templates/service/dns-forwarding/cache-size/node.def b/templates/service/dns-forwarding/cache-size/node.def new file mode 100644 index 00000000..3a73f308 --- /dev/null +++ b/templates/service/dns-forwarding/cache-size/node.def @@ -0,0 +1,4 @@ +type: u32 +default:150 +help: Set DNS forwarding cache size +syntax:expression: ($VAR(@) >=0 && $VAR(@) < 10001) ; "Cache size must be between 0 and 10000" diff --git a/templates/system/static-host-mapping/host-name/node.def b/templates/system/static-host-mapping/host-name/node.def index 189025e3..187b21dd 100644 --- a/templates/system/static-host-mapping/host-name/node.def +++ b/templates/system/static-host-mapping/host-name/node.def @@ -3,9 +3,14 @@ type: txt help: Set to map DNS names to system interfaces syntax:expression: pattern $VAR(@) "^[-a-zA-Z0-9.]+$" ; "invalid host name $VAR(@)" commit:expression: $VAR(./inet) != ""; "IP address for the static mapping must be set" -end:expression: "sudo sh -c \"\ - touch /etc/hosts; \ - sed -i '/ $VAR(@) .*#vyatta entry/d' /etc/hosts; \ - if [ -z \"$VAR(./inet/@)\" ]; then exit 0; fi; \ - declare -a aliases=( $VAR(alias/@@) ); \ - echo \\\"$VAR(inet/@)\t $VAR(@) \\${aliases[*]} \t #vyatta entry\\\" \ >> /etc/hosts\" " +end: sudo sh -c " + touch /etc/hosts + sed -i '/ $VAR(@) .*#vyatta entry/d' /etc/hosts + if [ -z \"$VAR(./inet/@)\" ]; then + if [ -d /opt/vyatta/config/active/service/dns-forwarding ]; then /etc/init.d/dnsmasq restart >&/dev/null; fi + exit 0 + fi + declare -a aliases=( $VAR(alias/@@) ) + echo -e \"$VAR(inet/@)\\t $VAR(@) \${aliases[*]} \\t #vyatta entry\" >> /etc/hosts + if [ -d /opt/vyatta/config/active/service/dns-forwarding ]; then /etc/init.d/dnsmasq restart >&/dev/null; fi" + -- cgit v1.2.3 From 24ea5effd365d09abf7bdfa7e2c1f6601cacda50 Mon Sep 17 00:00:00 2001 From: Mohit Mehta Date: Tue, 5 Aug 2008 07:22:38 +0000 Subject: add command for user to enter interfaces on which not to listen for DNS queries --- scripts/dns-forwarding/vyatta-dns-forwarding.pl | 8 ++++++++ .../service/dns-forwarding/ignore-interface/node.def | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 templates/service/dns-forwarding/ignore-interface/node.def (limited to 'scripts/dns-forwarding') diff --git a/scripts/dns-forwarding/vyatta-dns-forwarding.pl b/scripts/dns-forwarding/vyatta-dns-forwarding.pl index 4eebf6d7..1fb2282a 100644 --- a/scripts/dns-forwarding/vyatta-dns-forwarding.pl +++ b/scripts/dns-forwarding/vyatta-dns-forwarding.pl @@ -62,6 +62,14 @@ sub dnsforwarding_get_values { my $config = new VyattaConfig; $config->setLevel("service dns-forwarding"); + + my @ignore_interfaces = $config->returnValues("ignore-interface"); + if ($#ignore_interfaces >= 0) { + foreach my $interface (@ignore_interfaces) { + $output .= "except-interface=$interface\n"; + } + } + my $cache_size = $config->returnValue("cache-size"); if (defined $cache_size) { $output .= "cache-size=$cache_size\n"; diff --git a/templates/service/dns-forwarding/ignore-interface/node.def b/templates/service/dns-forwarding/ignore-interface/node.def new file mode 100644 index 00000000..5355eecf --- /dev/null +++ b/templates/service/dns-forwarding/ignore-interface/node.def @@ -0,0 +1,19 @@ +multi: +type: txt +help: Set interface to ignore DNS queries on +syntax:expression: exec " + intf_array=($(awk '$1 ~ /:$/ { print $1 }' /proc/net/dev)) + intf_array_len=${#intf_array[*]} + i=0 + while [ $i -lt $intf_array_len ]; do + if [ \"${intf_array[$i]}\" == \"$VAR(@):\" ] ; then + exit 0 + fi + let i++ + done + echo Invalid ethernet interface [$VAR(@)] + exit 1 " + +allowed: local -a array ; + array=($(awk '$1 ~ /:$/ { print $1 }' /proc/net/dev)); + echo -n ${array[@]%:} -- cgit v1.2.3