-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsbire_master.pl
executable file
·400 lines (359 loc) · 10.3 KB
/
sbire_master.pl
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/perl
####################
#
# sbire_master.pl
#
# NRPE plugins update/manage master script
#
# Usage : sbire_master.pl -H <IP> -P LOCAL|NRPE|SSH -c upload -n <name> -f <file> [ -v 1 ]
# sbire_master.pl -H <IP> -P LOCAL|NRPE|SSH -c download -n <name> -f <file>
# sbire_master.pl -H <IP> -P LOCAL|NRPE|SSH -c run [-d <dir>] -- <cmdline>
# sbire_master.pl -H <IP> -P LOCAL|NRPE|SSH -c config [-n <config_file_name>] [-- <OPTION> <value>]
# sbire_master.pl -H <IP> -P LOCAL|NRPE|SSH -c info -n <name>
# sbire_master.pl -H <IP> -P NRPE -c nrpe [ -n <name> ] [ -- <ATTRIBUTES> ]
#
####################
use strict;
$\=$/;
my $CONFIGFILE;
our ($CHUNK_SIZE, $privkey, $NRPE, $USE_ZLIB_COMPRESSION, $USE_RSA);
{
# Default config file
my $ROOT_DIR = $0;$ROOT_DIR=~s!/[^/]*$!!;
$CONFIGFILE = $^O=~/Win/ ? './sbire_master.conf' : "$ROOT_DIR/etc/sbire_master.conf";
$CONFIGFILE = '/etc/sbire_master.conf' unless -f $CONFIGFILE;
# Read config file as first argument
$_ = $ARGV[0];
if (defined $_ && !/^-/) {
# Lets assume that this first argument is the path to the config file
$CONFIGFILE = $_;
}
unless (-e $CONFIGFILE) {
print "Configuration file $CONFIGFILE does not exist. Creating...";
open CF, ">$CONFIGFILE";
print CF <<_EOF_;
# Sample configuration file
\$CHUNK_SIZE = 832;
\$privkey = '/usr/local/nagios/bin/sbire_key.private';
\$NRPE = '/usr/lib/nagios/iplugins/check_nrpe';
\$USE_RSA = 1;
\$USE_ZLIB_COMPRESSION = 1;
1;
_EOF_
close CF;
}
die ("Could not initialize configuration file") unless (-e $CONFIGFILE);
require $CONFIGFILE;
}
# die ("NRPE could not be found at $NRPE") unless (-x $NRPE);
use MIME::Base64;
use Digest::MD5 qw(md5_hex);
my ($protocol,$command,$dest,$file,$name,$cmdline,$dir,$NRPEnossl,$ssh_path);
my ($help,$verbose);
&getOptions(
"P" => \$protocol,
"p" => \$ssh_path,
"c" => \$command,
"H" => \$dest,
"v" => \$verbose,
"n" => \$name,
"S" => \$NRPEnossl, # -S 1 means that NRPE command must use the -n option (NO SSL)
"f" => \$file,
"d" => \$dir,
"-" => \$cmdline,
"h" => \$help
);
&error("Destination (-H) is mandatory") unless defined $dest;
&usage() if defined $help;
unless ($file) {
$file=$name;
$file=~s/.*(\\|\/)//;
}
# transforme les caracteres interdits pour NRPE en meta-caractere
$name =~s/[^\w ]/'%'.sprintf("%x",ord $&)/ge;
$cmdline=~s/[^\w ]/'%'.sprintf("%x",ord $&)/ge;
$dir =~s/[^\w ]/'%'.sprintf("%x",ord $&)/ge;
unless (defined $command) {
print &call_sbire(' ');
exit(0);
}
if ($command eq 'upload') {
&upload($file,$name,$verbose);
} elsif ($command eq 'info') {
&info($name);
} elsif ($command eq 'nrpe') {
&nrpe($name,$cmdline);
} elsif ($command eq 'download') {
&download($file,$name,$verbose);
} elsif ($command eq 'config') {
&config($name,$cmdline);
} elsif ($command eq 'run') {
&run($cmdline,$dir);
} else {
&error("Command '$command' unknown");
}
sub usage {
print "Usage : sbire_master.pl -H <IP> -P LOCAL|NRPE|SSH -c upload -n <remote_file> [-f <local_file>]";
print " sbire_master.pl -H <IP> -P LOCAL|NRPE|SSH -c download -n <remote_file> [-f <local_file>]";
print " sbire_master.pl -H <IP> -P LOCAL|NRPE|SSH -c run [-d <dir>] -- <cmdline>";
print " sbire_master.pl -H <IP> -P LOCAL|NRPE|SSH -c config [-n <config_file_name>] [-- <OPTION> <value>]";
print " sbire_master.pl -H <IP> -P LOCAL|NRPE|SSH -c info -n <name>";
print " sbire_master.pl -H <IP> -P NRPE -c nrpe [ -n <name> ] [ -- <parameters> ]";
}
sub error {
my ($msg)=@_;
print $msg;
&usage();
exit(1);
}
# -----------------------------------------------
sub run {
my ($cmdline,$dir)=@_;
$dir="-$dir-" if $dir;
print &call_sbire("run $name $dir $cmdline");
}
sub info {
my ($name)=@_;
print &call_sbire("info $name");
}
sub nrpe {
my ($name,$params)=@_;
print get_output(&buildNrpeCmd($name, $params));
}
sub config {
my ($name,$cmdline)=@_;
$name='-' unless $name;
print &call_sbire("config $name $cmdline");
}
sub download {
my ($file,$name,$verbose)=@_;
if (-f $file) {
# The file exists, so we should check if a download is necessary
my $localContent=readFileContent($file);
exitIfRemoteContentIsIdentical($name, $localContent);
}
my $content=&call_sbire("download $name");
undef $\;
if ($file eq 'STDOUT') {
print $content;
} else {
print "Writing file$/" if ($verbose);
open INF, ">$file" or die "\nCan't open $file for writing: $!\n";
binmode INF;
print INF $content;
close INF;
my $len=length($content);
print "Downloaded $len bytes to $file$/";
}
exit(0);
}
sub upload {
my ($file,$name,$verbose)=@_;
# Lecture du fichier
print "Reading file" if ($verbose);
my $content=readFileContent($file);
my $mymd=exitIfRemoteContentIsIdentical($name, $content);
# Demande d'un nouvel ID de session
my $ID = &call_sbire("send newfile");
# Compression
my $zcontent;
if ($USE_ZLIB_COMPRESSION) {
eval("use Compress::Zlib");
{
local $\;
print "Compressing..." if ($verbose);
}
$zcontent=compress($content);
print " Ratio : ".int(100-100*length($zcontent)/length($content))."%" if ($verbose);
} else { $zcontent=$content; }
# Conversion en base 64
print "Converting to base64" if ($verbose);
my $offset=0;
my $content64=encode_base64($zcontent,'');
# Decoupage en chunks et envoi
print "Sending..." if ($verbose);
while ($content64=~/.{1,$CHUNK_SIZE}/g) {
my $chunk64=$&;
my $args="send $ID $chunk64 $offset";
$_ = &call_sbire($args);
/OK (\d+)/ or die ("Unknown sbire response : $_");
$offset = $1;
{ local $\;print "." unless ($verbose>1); }
}
# Fin de l'envoi
print "\nSent." if ($verbose);
print "Size=".length($content64)." bytes" if ($verbose);
print "Session ID=$ID" if ($verbose);
# Calcul de la signature
my $signature;
if ($USE_RSA) {
my ($k,$n)=readKeyFile($privkey);
$signature = rsaCrypt($mymd,$k,$n);
$signature=encode_base64($signature,'');
} else {
$signature="unsigned";
}
# Mise a jour du fichier distant
my $args="update $name $ID $signature";
$_=&call_sbire($args);
print;
}
sub readFileContent() {
my ($file)=@_;
open INF, $file or die "\nCan't open $file: $!\n";
binmode INF;
my $content = do { local $/; <INF> };
close INF;
return $content;
}
# This sub control if the given content is identical to the remote content (with MD5)
# It then returns the MD5 of the local content, or exit the program if contents are identical
sub exitIfRemoteContentIsIdentical() {
my ($name, $content)=@_;
# Verification de la version du fichier
$_ = &call_sbire("info $name",1);
my $mymd=md5_hex($content);
if (/Signature\W+([\w]+)/) {
my $md5=$1;
# Verification de notre propre signature
if ($md5 eq $mymd) {
print "Files are identical. Skip...";
exit(0);
}
}
return $mymd;
}
sub readKeyFile() {
my($file)=@_;
open K,$file or die "\nCan't open private key file $file: $!\n";
local $/;
$_=<K>;
close K;
s/\W//g;
my (undef,$k,$n)=split/0x/;
return ($k,$n);
}
sub rsaCrypt() {
my ($content,$k,$n)=@_;
return "-" unless defined $n;
$\=$/;
local $/;
$/=unpack('H*',$content);
my $temp=&createTempFile();
open DC,">$temp";
print DC "16dio\U${k}SK$/SM$n\EsN0p[lN*1lK[d2%Sa2/d0<X+d*lMLa^*lN%0]dsXx++lMlN/dsM0<j]dsjxp";
close DC;
$_=`dc $temp`;
unlink($temp);
s/\W//g;
$_=pack('H*',/((..)*)$/);
}
sub createTempFile() {
use POSIX;
return tmpnam();
}
sub call_sbire {
my ($args,$ignore_err)=@_;
$args=~s/"/\\\\"/g;
my $cmd=&buildCmd($args);
print "sbire > $cmd" if ($verbose>1);
my $result = get_output($cmd);
# Loop : while the result ends with ___Cont:xxx___, then another request must be done
while ($result=~/___Cont:(\d+)___$/) {
my $remove=$&;
$cmd=&buildCmd("continue $1");
print "sbire > $cmd" if ($verbose>1);
$result=substr($result,0,length($result)-length($remove)-1);
$result .= get_output($cmd);
}
print "(Complete decoded output is ".length($result)." bytes)" if ($verbose);
if ($? && !$ignore_err) {
print "Error on > $cmd" if ($verbose);
# Analyse des erreurs connues (et affichage de solutions appropriees)
# if ($result=~/sh:.*sbire.pl: Permission denied/) {
# print "$result\nCheck sbire.pl execution status on distant server.";
# print "Try: chmod a+x /usr/local/";
# exit($?);
# }
print $result;
exit($?);
}
print $result if ($verbose>2);
return $result;
}
sub get_output {
my ($cmd) = @_;
print "> $cmd" if ($verbose);
my $output=qx!$cmd!;
print "(output ".length($output)." bytes)" if ($verbose);
return $output=~/^b\*64_(.*)_b64$/sm ? decode_base64($1) : $output;
}
sub buildNrpeCmd() {
my ($name,$params)=@_;
my $cmd;
if (uc $protocol eq 'NRPE') {
my $nrpe_arg = $NRPEnossl ? "-n" : "";
my $nrpe_cmd;
if ($dest=~/^(.*):(\d+)$/) {
$nrpe_cmd="$NRPE -H $1 -p $2 $nrpe_arg";
} else {
$nrpe_cmd="$NRPE -H $dest $nrpe_arg";
}
$cmd=$nrpe_cmd;
$cmd .= " -c $name" if $name=~/\w/;
$cmd .= qq! -a "$params"! if $params;
} else {
&error("NRPE command incompatible with protocol '$protocol'.");
}
return $cmd;
}
sub buildCmd() {
my ($args)=@_;
my $cmd;
if (uc $protocol eq 'NRPE') {
my $nrpe_arg = $NRPEnossl ? "-n" : "";
my $nrpe_cmd;
if ($dest=~/^(.*):(\d+)$/) {
$nrpe_cmd="$NRPE -H $1 -p $2 $nrpe_arg";
} else {
$nrpe_cmd="$NRPE -H $dest $nrpe_arg";
}
$cmd="$nrpe_cmd -c sbire -a \" $args\"";
} elsif (uc $protocol eq 'SSH') {
$cmd=qq!ssh $dest "$ssh_path $args"!;
} elsif (uc $protocol eq 'LOCAL') {
$cmd="./sbire.pl /etc/sbire.cfg $args";
} else {
&error("Unkown protocol '$protocol'.");
}
return $cmd;
}
sub getOptions() {
my %varRef=@_;
$_ = join ' ',@ARGV;
if (/^(.*?) --(.*)/) {
if (!defined $varRef{'-'}) {
print "Parametre -- non defini";exit 3;
}
$_=$1;
${$varRef{'-'}} = $2;
}
return if (!defined $_);
my @array = split / *-([a-zA-Z])(?: |$)/,$_;
my %params;
if ((+ @array)%2 == 0) {
(undef,%params) = (@array,'');
} else {
(undef,%params) = @array;
}
foreach (keys %params) {
my $cle = $_;
my $val = $params{$cle};
if (defined $varRef{$cle}) {
${$varRef{$cle}} = $val;
}
else {
print "Parametre -$cle inconnu";exit 3;
}
}
}