/
lms-makemessages
executable file
·279 lines (243 loc) · 7.21 KB
/
lms-makemessages
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
#!/usr/bin/perl -Tw
#
# LMS version 1.11-git
#
# Copyright (C) 2001-2012 LMS Developers
#
# Please, see the doc/AUTHORS for more information about authors!
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
# $Id$
use strict;
use DBI;
use Config::IniFiles;
use Getopt::Long;
use vars qw($configfile $quiet $help $version);
sub mask2prefix($)
{
my $mask = shift @_;
my @tmp = split('\.',$mask,4);
my $q = sprintf("%b%b%b%b",$tmp[0],$tmp[1],$tmp[2],$tmp[3]);
$q =~ s/0*$//;
if ($q =~ /0/) {
print " You idiot. error in mask\n";
}
my $len = length($q) ;
return $len;
}
sub prefix2mask
{
my $host_bits = 32-$_[0];
my $net_mask = (~0 << $host_bits) & 0xffffffff;
my @bytes = unpack "CCCC", pack "N", $net_mask;
my $dec_rep = sprintf "%d.%d.%d.%d", @bytes;
return $dec_rep;
}
sub matchip($$$)
{
my ($ip,$net,$mask) = @_;
my $prefix = mask2prefix($mask);
my $bmask = 2**32 - 2**(32-$prefix);
my @net = split('\.',$net,4);
my $bnet = dotquad2u32($net);
if(($bnet & $bmask)!= $bnet) {
print "EEediot net &mask != net\n"; return 1==0
}
my $bip = dotquad2u32($ip);
return (($bip&$bmask) == $bnet);
}
sub u32todotquad($)
{
my $p = shift @_;
return sprintf "%d.%d.%d.%d", ($p>>24)&0xff,($p>>16)&0xff, ($p>>8)&0xff,$p&0xff;
}
sub dotquad2u32($)
{
my $dq = shift||'0.0.0.0';
my @dq = split('\.',$dq,4);
return ((($dq[0] << 8) + $dq[1] << 8) + $dq[2] << 8) + $dq[3];
}
sub isprivate($)
{
my ($ip) = @_;
return matchip($ip,"192.168.0.0","255.255.0.0") || matchip($ip,"10.0.0.0","255.0.0.0") || matchip($ip,"172.16.0.0","255.240.0.0");
}
my $_version = '1.11-cvs';
my %options = (
"--config-file|C=s" => \$configfile,
"--quiet|q" => \$quiet,
"--help|h" => \$help,
"--version|v" => \$version
);
Getopt::Long::config("no_ignore_case");
GetOptions(%options);
if($help)
{
print STDERR <<EOF;
lms-makemessages, version $_version
(C) 2001-2012 LMS Developers
-C, --config-file=/etc/lms/lms.ini alternate config file (default: /etc/lms/lms.ini);
-h, --help print this help and exit;
-v, --version print version info and exit;
-q, --quiet suppress any output, except errors;
EOF
exit 0;
}
if($version)
{
print STDERR <<EOF;
lms-makemessages, version $_version
(C) 2001-2012 LMS Developers
EOF
exit 0;
}
if(!$configfile)
{
$configfile = "/etc/lms/lms.ini";
}
if(!$quiet)
{
print STDOUT "lms-makemessages, version $_version\n";
print STDOUT "(C) 2001-2012 LMS Developers\n";
print STDOUT "Using file $configfile as config.\n";
}
if(! -r $configfile)
{
print STDERR "Fatal error: Unable to read configuration file $configfile, exiting.\n";
exit 1;
}
my $ini = new Config::IniFiles -file => $configfile;
print @Config::IniFiles::errors;
my $networks_list = $ini->val('messages', 'networks') || '';
my $excluded_networks_list = $ini->val('messages', 'excluded_networks') || '';
my $customergroups_list = $ini->val('messages', 'customergroups') || '';
my $cfile = $ini->val('messages', 'config_file') || '/etc/rc.d/rc.messages';
my $cuid = $ini->val('messages', 'config_owneruid') || '0';
my $cgid = $ini->val('messages', 'config_ownergid') || '0';
my $cperm = $ini->val('messages', 'config_permission') || '700';
my $redir = $ini->val('messages', 'redirect_address') || '127.0.0.1:80';
my $ipbin = $ini->val('messages', 'iptables_binary') || '/sbin/iptables';
my $chain = $ini->val('messages', 'chain') || 'MESSAGES';
my $dbtype = $ini->val('database', 'type') || 'mysql';
my $dbhost = $ini->val('database', 'host') || 'localhost';
my $dbuser = $ini->val('database', 'user') || 'root';
my $dbpasswd = $ini->val('database', 'password') || '';
my $dbname = $ini->val('database', 'database') || 'lms';
my $dbase;
if($dbtype =~ /mysql/)
{
$dbase = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 });
$dbase->do("SET NAMES utf8");
}
elsif($dbtype eq "postgres")
{
$dbase = DBI->connect("DBI:Pg:dbname=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 });
}
else
{
print STDERR "Fatal error: unsupported database type: $dbtype, exiting.\n";
exit 1;
}
open(MESGFILE, ">$cfile") or die("Fatal error: Unable to write $cfile, exiting.\n");
print MESGFILE $ipbin." -t nat -F ".$chain."\n\n";
my $allnetworks = "";
my $dbq = $dbase->prepare("SELECT name FROM networks");
$dbq->execute();
while (my $row = $dbq->fetchrow_hashref()) {
$allnetworks = "$allnetworks $row->{'name'}";
}
$dbq->finish();
if(!$networks_list)
{
$networks_list = $allnetworks;
}
my @networks = split ' ',$networks_list;
if($excluded_networks_list)
{
my @excluded_networks = split ' ', $excluded_networks_list;
foreach my $excluded_network (@excluded_networks)
{
@networks = grep(!/^$excluded_network$/, @networks);
}
}
my $inicustomergroups = $customergroups_list;
if(!$customergroups_list)
{
$dbq = $dbase->prepare("SELECT name FROM customergroups");
$dbq->execute();
while (my $row = $dbq->fetchrow_hashref()) {
$customergroups_list = "$customergroups_list $row->{'name'}";
}
$dbq->finish();
}
my @customergroups = split ' ',$customergroups_list;
my %customergrouplist;
#
# Buil nodes list
#
$dbq = $dbase->prepare("SELECT id, name FROM customergroups");
$dbq->execute();
while (my $row = $dbq->fetchrow_hashref())
{
$customergrouplist{$row->{'name'}} = $row->{'id'};
}
$dbq->finish();
$dbq = $dbase->prepare("SELECT ipaddr, name, ownerid FROM nodes WHERE warning=1");
$dbq->execute();
while (my $row = $dbq->fetchrow_hashref())
{
my $customergroupscount = 0;
foreach my $key2 (@customergroups)
{
my $sdbq = $dbase->prepare("SELECT count(customerid) AS total FROM customerassignments WHERE customerid=$row->{'ownerid'} AND customergroupid=".$customergrouplist{$key2});
$sdbq->execute();
my $srow = $sdbq->fetchrow_hashref();
$sdbq->finish();
if ($srow->{'total'} eq 1)
{
$customergroupscount++;
}
}
if($customergroupscount || !$inicustomergroups)
{
my $tmpname = lc($row->{'name'});
my $ipaddr = u32todotquad($row->{'ipaddr'});
my $networkscount = 0;
foreach my $key (@networks)
{
my $sdbq2 = $dbase->prepare("SELECT address, mask FROM networks WHERE name = UPPER('$key')");
$sdbq2->execute();
while (my $srow2 = $sdbq2->fetchrow_hashref())
{
my $net = u32todotquad($srow2->{'address'});
if (matchip($ipaddr, $net, $srow2->{'mask'}))
{
$networkscount++;
}
}
$sdbq2->finish();
}
if ($networkscount)
{
print MESGFILE "# $tmpname\n".$ipbin." -t nat -A ".$chain." -p tcp -s ".$ipaddr." -j DNAT --to ".$redir."\n\n";
}
}
}
$dbq->finish();
$dbase->disconnect();
close(MESGFILE);
chown $cuid, $cgid, $cfile or print "Warning! Unable to set owner of $cfile to $cuid.$cgid.\n";
chmod oct($cperm), $cfile or print "Warning! Unable to set permission $cperm to $cfile.\n";