diff options
author | Mohit Mehta <mohit.mehta@vyatta.com> | 2008-09-06 01:36:53 +0000 |
---|---|---|
committer | Mohit Mehta <mohit.mehta@vyatta.com> | 2008-09-06 01:36:53 +0000 |
commit | a684eb06761896b0236619b9506d6699a98af2ba (patch) | |
tree | f089e0da09f030819fc977395aff2b8f33fdb057 | |
parent | f9c10f6d4d60fba33876f613dfbbf23f58067186 (diff) | |
download | vyatta-cfg-system-a684eb06761896b0236619b9506d6699a98af2ba.tar.gz vyatta-cfg-system-a684eb06761896b0236619b9506d6699a98af2ba.zip |
first attempt at implementing CLI support for Dynamic DNS
8 files changed, 189 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index ec5a473e..9cd18256 100644 --- a/Makefile.am +++ b/Makefile.am @@ -31,6 +31,7 @@ sbin_SCRIPTS += scripts/telnetd.init sbin_SCRIPTS += scripts/add_bootfile_eth_hwid sbin_SCRIPTS += scripts/mod_bootfile_eth_hwid sbin_SCRIPTS += scripts/dns-forwarding/vyatta-dns-forwarding.pl +sbin_SCRIPTS += scripts/dynamic-dns/vyatta-dynamic-dns.pl noinst_DATA = test_bootfile diff --git a/scripts/dynamic-dns/vyatta-dynamic-dns.pl b/scripts/dynamic-dns/vyatta-dynamic-dns.pl new file mode 100644 index 00000000..5c2e4869 --- /dev/null +++ b/scripts/dynamic-dns/vyatta-dynamic-dns.pl @@ -0,0 +1,125 @@ +#!/usr/bin/perl +# +# Module: vyatta-dynamic-dns.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: September 2008 +# Description: Script to run ddclient per interface as set in Vyatta CLI +# +# **** End License **** +# + +use lib "/opt/vyatta/share/perl5/"; +use VyattaConfig; +use VyattaMisc; +use Getopt::Long; + +use strict; +use warnings; +use Switch; + +# +# main +# + +my ($update_dynamicdns, $stop_dynamicdns, $interface); + +GetOptions("update-dynamicdns!" => \$update_dynamicdns, + "stop-dynamicdns!" => \$stop_dynamicdns, + "interface=s" => \$interface); + +if (defined $update_dynamicdns) { + my $config; + $config = dynamicdns_get_constants(); + $config .= dynamicdns_get_values(); + dynamicdns_write_file($config); + dynamicdns_restart(); +} + +if (defined $stop_dynamicdns) { + dynamicdns_stop(); +} + +exit 0; + +# +# subroutines +# + +sub dynamicdns_restart { + system("kill -9 `cat /var/run/ddclient_$interface.pid 2>/dev/null` >&/dev/null"); + system("/usr/sbin/ddclient -file /etc/ddclient_$interface.conf >&/dev/null"); +} + +sub dynamicdns_stop { + system("kill -9 `cat /var/run/ddclient_$interface.pid 2>/dev/null` >&/dev/null"); +} + +sub dynamicdns_get_constants { + my $output; + + my $date = `date`; + chomp $date; + $output = "#\n# autogenerated by vyatta-dynamic-dns.pl on $date\n#\n"; + $output .= "daemon=1m\n"; + $output .= "syslog=yes\n"; + $output .= "ssl=yes\n"; + $output .= "pid=/var/run/ddclient_$interface.pid\n"; + $output .= "cache=/tmp/ddclient_$interface.cache\n"; + $output .= "use=if, if=$interface\n\n\n"; + return $output; +} + +sub dynamicdns_get_values { + + my $output = ''; + my $config = new VyattaConfig; + $config->setLevel("service dns dynamic interface $interface"); + + my @services = $config->listNodes("service"); + print "Services under $interface: @services\n"; + foreach my $service (@services) { + $config->setLevel("service dns dynamic interface $interface service $service"); + switch ($service) { + case "dslreports" {$service="dslreports1";} + case "dyndns" {$service="dyndns2";} + case "zoneedit" {$service="zoneedit1";} + } + my $login = $config->returnValue("login"); + my $password = $config->returnValue("password"); + my @hostnames = $config->returnValues("host-name"); + foreach my $hostname (@hostnames) { + $output .= "protocol=$service\n"; + $output .= "login=$login\n"; + $output .= "password='$password'\n"; + $output .= "$hostname\n\n"; + } + } + + return $output; +} + +sub dynamicdns_write_file { + my ($config) = @_; + + open(my $fh, '>', "/etc/ddclient_$interface.conf") || die "Couldn't open \"/etc/ddclient_$interface.conf\" - $!"; + print $fh $config; + close $fh; +} + + +# end of file diff --git a/templates/service/dns/dynamic/interface/node.def b/templates/service/dns/dynamic/interface/node.def new file mode 100644 index 00000000..bbf0c310 --- /dev/null +++ b/templates/service/dns/dynamic/interface/node.def @@ -0,0 +1,31 @@ +tag: +help: Set interface to send DDNS updates for +type: txt +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 + temp=${intf_array[$i]%:*} + if [ \"$temp\" == \"$VAR(@)\" ] ; then + exit 0 + fi + let i++ + done + echo Invalid interface [$VAR(@)] + exit 1 " + +allowed: local -a array ; + array=($(awk '$1 ~ /:/ { print $1 }' /proc/net/dev)); + echo -n ${array[@]%:*} + +commit:expression: $VAR(./@/service/@@) != ""; "Atleast one service must be set to send DDNS updates for $VAR(@)" + +delete:expression: "touch /tmp/ddclient_$VAR(@).$PPID" + +end:expression: "if [ -f \"/tmp/ddclient_$VAR(@).$PPID\" ]; then \ + sudo /opt/vyatta/sbin/vyatta-dynamic-dns.pl --stop-dynamicdns --interface $VAR(@) \ + rm /tmp/ddclient_$VAR(@).$PPID; \ + else \ + sudo /opt/vyatta/sbin/vyatta-dynamic-dns.pl --update-dynamicdns --interface $VAR(@); \ + fi; " diff --git a/templates/service/dns/dynamic/interface/node.tag/service/node.def b/templates/service/dns/dynamic/interface/node.tag/service/node.def new file mode 100644 index 00000000..a515351e --- /dev/null +++ b/templates/service/dns/dynamic/interface/node.tag/service/node.def @@ -0,0 +1,23 @@ +tag: +help: Set service being used for Dynamic DNS +type: txt +syntax:expression: exec " + service_array=(dnspark dslreports dyndns easydns namecheap sitelutions zoneedit) + service_array_len=${#service_array[*]} + i=0 + while [ $i -lt $service_array_len ]; do + if [ \"${service_array[$i]}\" == \"$VAR(@)\" ] ; then + exit 0 + fi + let i++ + done + echo Invalid service [$VAR(@)] + exit 1 " + +allowed: local -a array ; + array=(dnspark dslreports dyndns easydns namecheap sitelutions zoneedit); + echo -n ${array[@]} + +commit:expression: $VAR(./@/login) != ""; "Set login for service $VAR(./@) to send DDNS updates for interface $VAR(../@)" +commit:expression: $VAR(./@/password) != ""; "Set password for service $VAR(./@) to send DDNS updates for interface $VAR(../@)" +commit:expression: $VAR(./@/host-name) != ""; "Set atleast one host-name registered with service $VAR(./@) to send DDNS updates for interface $VAR(../@)" diff --git a/templates/service/dns/dynamic/interface/node.tag/service/node.tag/host-name/node.def b/templates/service/dns/dynamic/interface/node.tag/service/node.tag/host-name/node.def new file mode 100644 index 00000000..e104ae8f --- /dev/null +++ b/templates/service/dns/dynamic/interface/node.tag/service/node.tag/host-name/node.def @@ -0,0 +1,3 @@ +multi: +help: Set host-name registered with DDNS service +type: txt diff --git a/templates/service/dns/dynamic/interface/node.tag/service/node.tag/login/node.def b/templates/service/dns/dynamic/interface/node.tag/service/node.tag/login/node.def new file mode 100644 index 00000000..4a6ca920 --- /dev/null +++ b/templates/service/dns/dynamic/interface/node.tag/service/node.tag/login/node.def @@ -0,0 +1,2 @@ +help: Set login for DDNS service +type: txt diff --git a/templates/service/dns/dynamic/interface/node.tag/service/node.tag/password/node.def b/templates/service/dns/dynamic/interface/node.tag/service/node.tag/password/node.def new file mode 100644 index 00000000..9dceae00 --- /dev/null +++ b/templates/service/dns/dynamic/interface/node.tag/service/node.tag/password/node.def @@ -0,0 +1,2 @@ +help: Set password for DDNS service +type: txt diff --git a/templates/service/dns/dynamic/node.def b/templates/service/dns/dynamic/node.def new file mode 100644 index 00000000..d713fc1c --- /dev/null +++ b/templates/service/dns/dynamic/node.def @@ -0,0 +1,2 @@ +help: Configure Dynamic DNS +commit:expression: $VAR(./interface/@@) != ""; "Atleast one interface must be set to send DDNS updates for" |