summaryrefslogtreecommitdiff
path: root/scripts/dynamic-dns/vyatta-dynamic-dns.pl
blob: 5c2e4869884ca082da06219ff4d8b87d805d51e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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