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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
#!/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 Vyatta::Config;
use Vyatta::Misc;
use Getopt::Long;
use strict;
use warnings;
my $dnsforwarding_init = '/etc/init.d/dnsmasq';
my $dnsforwarding_conf = '/etc/dnsmasq.conf';
sub dnsforwarding_restart {
system("$dnsforwarding_init restart >&/dev/null");
}
sub dnsforwarding_stop {
system("$dnsforwarding_init stop >&/dev/null");
}
sub dnsforwarding_get_constants {
my $output;
my $date = `date`;
chomp $date;
$output = "#\n# autogenerated by vyatta-dns-forwarding.pl on $date\n#\n";
$output .= "log-facility=/var/log/dnsmasq.log\n";
$output .= "no-poll\n";
system("rm -f /var/log/dnsmasq.log; touch /var/log/dnsmasq.log");
return $output;
}
sub dnsforwarding_get_values {
my $outside_cli = shift;
my $output = '';
my $config = new Vyatta::Config;
my $use_dnsmasq_conf = 0;
my (@listen_interfaces, $cache_size, @use_nameservers, $use_system_nameservers, @use_dhcp_nameservers);
$config->setLevel("service dns forwarding");
if ($outside_cli == 1){
$config->{_active_dir_base} = "/opt/vyatta/config/active/";
@listen_interfaces = $config->returnOrigValues("listen-on");
$cache_size = $config->returnOrigValue("cache-size");
@use_nameservers = $config->returnOrigValues("name-server");
$use_system_nameservers = $config->existsOrig("system");
@use_dhcp_nameservers = $config->returnOrigValues("dhcp");
} else {
@listen_interfaces = $config->returnValues("listen-on");
$cache_size = $config->returnValue("cache-size");
@use_nameservers = $config->returnValues("name-server");
$use_system_nameservers = $config->exists("system");
@use_dhcp_nameservers = $config->returnValues("dhcp");
}
if (@listen_interfaces != 0) {
foreach my $interface (@listen_interfaces) {
$output .= "interface=$interface\n";
}
}
if (defined $cache_size) {
$output .= "cache-size=$cache_size\n";
}
if (@use_nameservers != 0){
$use_dnsmasq_conf = 1;
foreach my $cli_nameserver (@use_nameservers) {
$output .= "server=$cli_nameserver\t# statically configured\n";
}
}
if (defined($use_system_nameservers)) {
$use_dnsmasq_conf = 1;
my $sys_config = new Vyatta::Config;
$sys_config->setLevel("system");
my @system_nameservers;
if ($outside_cli == 1){
$sys_config->{_active_dir_base} = "/opt/vyatta/config/active/";
@system_nameservers = $sys_config->returnOrigValues("name-server");
} else {
@system_nameservers = $sys_config->returnValues("name-server");
}
if (@system_nameservers > 0) {
foreach my $system_nameserver (@system_nameservers) {
$output .= "server=$system_nameserver\t# system\n";
}
}
}
if (@use_dhcp_nameservers != 0) {
$use_dnsmasq_conf = 1;
foreach my $interface (@use_dhcp_nameservers) {
my $dhcp_nameserver_count=`grep nameserver /etc/resolv.conf.dhclient-new-$interface 2>/dev/null | wc -l`;
if ($dhcp_nameserver_count > 0) {
my @dhcp_nameservers = `grep nameserver /etc/resolv.conf.dhclient-new-$interface`;
for my $each_nameserver (@dhcp_nameservers) {
my @nameserver = split(/ /, $each_nameserver, 2);
my $ns = $nameserver[1];
chomp $ns;
$output .= "server=$ns\t# dhcp $interface\n";
}
}
}
}
if ($use_dnsmasq_conf == 1) {
$output .= "resolv-file=/etc/dnsmasq.conf\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 2>/dev/null | wc -l`;
return $cmd;
}
sub check_system_nameserver {
my $config = new Vyatta::Config;
$config->setLevel("system");
my @system_nameservers = $config->returnValues("name-server");
return(@system_nameservers);
}
sub check_dhcp_any_interface {
my $config = new Vyatta::Config;
$config->setLevel("interfaces ethernet");
my @eths = $config->listNodes(".");
foreach my $eth (@eths) {
$config->setLevel("interfaces ethernet $eth");
my @addrs = $config->returnOrigValues("address");
foreach my $addr (@addrs) {
if (defined $addr && $addr eq "dhcp") {
return 1;
}
}
my @vifs = $config->listNodes("vif");
foreach my $vif (@vifs) {
$config->setLevel("interfaces ethernet $eth vif $vif");
my @addrs = $config->returnOrigValues("address");
foreach my $addr (@addrs) {
if (defined $addr && $addr eq "dhcp") {
return 1;
}
}
}
}
$config->setLevel("interfaces bridge");
my @bridges = $config->listNodes(".");
foreach my $bridge (@bridges) {
$config->setLevel("interfaces bridge $bridge");
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 (!Vyatta::Misc::is_dhcp_enabled($interface)) {
print "DNS forwarding error: $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 "DNS forwarding warning: DHCP lease for $interface has been released by user\n";
}
return 1;
}
#
# main
#
my ($update_dnsforwarding, $stop_dnsforwarding, $system_nameserver, $dhcp_interface, $outside_cli);
GetOptions("update-dnsforwarding!" => \$update_dnsforwarding,
"stop-dnsforwarding!" => \$stop_dnsforwarding,
"system-nameserver!" => \$system_nameserver,
"outside-cli!" => \$outside_cli,
"dhcp-interface=s" => \$dhcp_interface);
if (defined $system_nameserver) {
my $system_nameserver_exists = check_system_nameserver();
if ($system_nameserver_exists < 1){
print "DNS forwarding warning: No name-servers set under 'system name-server'\n";
}
}
if (defined $dhcp_interface) {
if (!check_dhcp_interface($dhcp_interface)){
exit 1;
}
}
if (defined $update_dnsforwarding) {
my $config;
my $vyatta_config = new Vyatta::Config;
$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();
my $dhcp_enabled_any_interface = check_dhcp_any_interface();
if ($nameserver_exists < 1 && $dhcp_enabled_any_interface == 0){
print "DNS forwarding warning: No name-servers to forward DNS queries\n";
}
}
my $called_from_outside_cli = 0;
if (defined $outside_cli){
$called_from_outside_cli = 1;
}
$config = dnsforwarding_get_constants();
$config .= dnsforwarding_get_values($called_from_outside_cli);
dnsforwarding_write_file($config);
dnsforwarding_restart();
}
if (defined $stop_dnsforwarding) {
dnsforwarding_stop();
}
exit 0;
# end of file
|