summaryrefslogtreecommitdiff
path: root/scripts/dns-forwarding/vyatta-dns-forwarding.pl
blob: 1fb2282afb8a32f4f59608751e492ea9f3f2fbe6 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/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 >&/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";
    return $output;
}

sub dnsforwarding_get_values {
    my $output = '';
    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";
    }

    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