-
Notifications
You must be signed in to change notification settings - Fork 6
/
bcinstall
executable file
·359 lines (305 loc) · 10 KB
/
bcinstall
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
#!/bin/bash
# Copyright (C) 2016 Timothe Litt litt _at_ acm _ddot_ org
BCVERSION="2.97"
# Minimum version of iptables
#
IPTV=1.4.7
# Width
W=25
prog="`basename $0`"
export V=
IP6=1
IP4=1
while getopts ":hHFSv" opt; do
case $opt in
v)
V=1
;;
F)
IP4=
;;
S)
IP6=
;;
h|H)
cat >&2 <<EOF
Check for software required to run BlockCountries version $BCVERSION
Usage
$prog [-h] [-v] [-F] [-S]
Options
-h This help
-F Do NOT check for IPv4 dependencies
-S Do NOT check for IPv6 dependencies
-v List installed Perl modules that meet minimun version requirements
Output
Reports the installed version of Perl
Reports Perl modules required by BlockCountries that are not installed
or below minimum required version
Reports iptables/ip6tables version, path consistency and configuration
-path value
Reports if a symlink is required for BlockCountries to find the Perl
interpreter
Reports cpan command used to update/install modules. However,
obtaining modules from your Perl packager/distribution is prefered.
Reports BlockCountries configuration file search list, and if a
file exists in each one.
NOTE:
Some failures abort checking. Re-run until no issues are reported.
exit status
0 if all checks pass
1 if one or more checks fail
EOF
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
echo "Usage: $prog -h for usage"
exit 1
;;
esac
done
echo "$prog version $BCVERSION"
echo ""
if ! which perl > /dev/null 2>&1 ; then
echo "You need to install Perl, either from your distribution or see https://www.perl.org"
echo "After installing Perl, run $prog again"
exit 1
fi
ppath="`which perl`"
export ppath="`dirname $ppath`"
perl -e'printf "Perl version %vd was found in %s\n", $^V, $ENV{ppath}'
function needs {
local m="$1"
local v="$2"
local VW=8
if perl -M"$m $v" -e'exit;' >/dev/null 2>&1 ; then
if [ -n "$v" ] && [ -n "$V" ]; then
perl -M"$m" -e"printf \" %-${W}s v%-${VW}s installed\n\", \"$m\", \$$m::VERSION;exit;"
fi
else
if [ -z "$v" ]; then
printf " %-${W}s v%-${VW}s *OUTDATED Must upgrade to v$m or higher\n" "Perl" "$m"
OK=
NEWPERL="$m"
return 1
fi
if perl -M"$m" -e'exit;' >/dev/null 2>&1 ; then
perl -M$m -e"printf \" %-${W}s v%-${VW}s *OUTDATED Must upgrade to v$v or higher\n\", \"$m\", \$$m::VERSION;exit;"
OK=
CPAN="$CPAN $m"
return 1
fi
printf " %-${W}s %-${VW}s *NOT INSTALLED Must install v$v or higher\n" $m ""
OK=
CPAN="$CPAN $m"
return 1
fi
return 0
}
OK=1
CPAN=
NEWPERL=
echo ""
echo " Checking Perl module status"
echo ""
# Update with BlockCountries needs -modules
needs 5.8.8 # Perl version
needs Carp 1.3301
needs Cwd 3.33
needs Errno 1.0901
needs Fcntl 1.05
needs File::Basename 2.74
needs File::Path 2.08
needs File::Temp 0.2304
needs IO::Compress::Zip 2.069
needs IO::Uncompress::Gunzip 2.064
needs LWP::Simple 6.00
needs Locale::Country 3.37
needs Net::Domain 2.23
needs Net::IP 1.25
needs NetAddr::IP 4.044
needs POSIX 1.09
needs Parse::Syslog 1.10
needs Regexp::IPv6 0.03
needs Socket 2.006
needs Storable 2.30
needs Symbol 1.06
needs Sys::Syslog 0.29
needs Text::ParseWords 3.27
needs Text::Wrap 2005.082401
if [ -n "$V" ]; then
echo
fi
if [ -n "$OK" ]; then
echo "All needed perl modules are installed and meet minimum version requirements"
else
echo ""
if [ -n "$NEWPERL" ]; then
echo "Perl $NEWPERL must be installed from your distribution, or https://www.perl.org"
echo "After upgrading Perl, run $prog again."
exit 1
fi
echo " If Perl was installed as a package, you should get the *modules from your package distribution if possible."
echo " For Linux distributions, you would use apt-get, yum install or equivalent"
echo " If Perl was installed from source, or your package distribution doesn't have these modules, get them from CPAN."
if [ -n"$CPAN" ]; then
if which cpan > /dev/null 2>&1 ; then
echo " If you need to get them from CPAN, the cpan install command is:"
echo ""
echo " cpan install$CPAN"
echo ""
echo " You can also visit https://search.cpan.org to download them manually."
else
echo " The cpan command is not installed. Visit https://search.cpan.org to download them manually."
fi
fi
fi
if [ "`readlink -f $ppath/perl`" != "`readlink -f /usr/bin/perl`" ]; then
echo ""
echo "You must create a symbolic link from /usr/bin/perl to `readlink -f $ppath/perl`"
fi
echo ""
W=17
VW=13
IOK=1
function checkver {
local PGM="$1"
local VER="$2"
local REQ="$3"
local DIR="$4"
[ "$VF"="Unknown" ] && VF="$VER"
if [ "$VER" != "$VF" ]; then
echo "$PGM version $VER doesn't match $VF"
IOK=
return 1;
fi
perl - <<EOF $PGM $VER $REQ $DIR && return 0
use warnings;
use strict;
my @ver = split( /\./, \$ARGV[1] );
\$ver[0] =~ s/^v//;
my @req = split( /\./, \$ARGV[2]) ;
my \$c = 0;
while( @ver && @req ) {
my \$v = shift @ver;
my \$r = shift @req;
\$c = \$v <=> \$r;
next unless( \$c );
last;
}
if( \$c > 0 || \$c == 0 && @ver >= @req ) {
printf " %-${W}s %-${VW}s in %-*s\n", \$ARGV[0], \$ARGV[1], \$ENV{PW}, \$ARGV[3] if( \$ENV{V} );
exit 0;
}
printf " %-${W}s %-${VW}s in %-*s *OUTDATED Must upgrade to v%s or higher\n", \$ARGV[0], \$ARGV[1], \$ENV{PW}, \$ARGV[3], \$ARGV[2];
exit 1;
EOF
IOK=
return 1;
}
path=
patha=
pgma=
VF="Unknown"
export PW=20
ip4tables="iptables iptables-restore iptables-save"
ip6tables="ip6tables ip6tables-restore ip6tables-save"
if [ -z "$IP6" ]; then
ip6tables=
fi
if [ -z "$IP4" ]; then
ip4tables=
fi
[ -n "$IP4$IP6" ] && echo "Checking iptables installation"
for ipt in $ip4tables $ip6tables; do
if ! which $ipt >/dev/null 2>&1 ; then
printf " %-${W}s %-${VW}s %-${PW}s *NOT INSTALLED\n" $ipt "" ""
IOK=
else
path="`which $ipt`"
path="`dirname $path`"
if [ -z "$patha" ]; then
patha="$path"
PW=${#patha}
pgma="$ipt"
fi
if [[ "$ipt" =~ ip.?tables-save ]]; then # ip*tables-save has no --version, but writes in data
checkver `$ipt | perl -e'$_=<>; s/^#\s+Generated\sby\s(\S+)\s+(\S*).*$/$1 $2/; print;'` $IPTV $path
else
if [[ "$ipt" =~ ip.?tables-restore ]]; then # ip*tables-restore doesn't have --version. Can't extract.
[ -n "$V" ] && printf " %-${W}s %-${VW}s in %-${PW}s\n" $ipt "" $path
else
checkver `$ipt --version` $IPTV $path
fi
fi
if [ "$path" != "$patha" ]; then
echo "$ipt must be in the same directory ($patha) as $pgma"
IOK=
fi
fi
done
echo ""
if [ -z "$IOK" ]; then
echo "iptables must be installed or upgraded. Minimum version is $IPTV"
fi
[ -z "$IP4$IP6" ] && echo "*** iptables installation not checked ***" || ( [ -n "$IOK" ] && echo "iptables version $VF installation validated" )
if [ -n "$patha" ] && [ "$patha" != '/sbin' ]; then
echo ""
echo "*** Be sure to include \"-path $patha\" in your configuration file"
fi
[ -z "$IOK" ] && OK=
# Extract config search list from BlockCountries & report
BC="`dirname $0`/BlockCountries"
if [ -f "$BC" ]; then
echo ""
echo "Inspecting configuration of $BC"
perl <<EOF
use warnings;
use strict;
use File::Basename;
`sed -n -e'/my @CFGFILE =/,/};/p' $BC`
my @dirs;
my \$maxd = 0;
\$maxd = ((length dirname \$_) > \$maxd? length dirname \$_ : \$maxd) foreach( 'Directory/x', 'xENVxBlockCountriesCFGx/x', @CFGFILE );
my \$maxf = 0;
\$maxf = ((length basename \$_) > \$maxf? length basename \$_ : \$maxf) foreach( 'x/File', @CFGFILE );
\$maxf = length( \$ENV{BlockCountriesCFG} ) if( defined \$ENV{BlockCountriesCFG} && length( \$ENV{BlockCountriesCFG} ) > \$maxf );
print ("\n Configuration file search list:\n");
printf( " %-*s %-*s Status\n", \$maxd, "Directory", \$maxf, "File" );
printf( " %-*s %-*s %s\n", \$maxd, '\$ENV{BlockCountriesCFG}', \$maxf, (\$ENV{BlockCountriesCFG} || ''),
(\$ENV{BlockCountriesCFG} && -f \$ENV{BlockCountriesCFG})? '[Exists]':
(\$ENV{BlockCountriesCFG} && -d dirname( \$ENV{BlockCountriesCFG} )? '[Directory exists]':'[Not present]' ) );
foreach my \$file (@CFGFILE) {
my \$dir = dirname \$file;
my \$rt = '';
printf(" %-*s ", \$maxd, \$dir );
if( \$dir =~ /^[.]/ ) {
printf( "%-*s [Not checked, runtime value unknown]\n", \$maxf, '' );
next;
}
if( \$dir =~ /^[~]/ ) {
\$dir =~ s,^[~],\$ENV{HOME} || (getpwuid(\$<))[7],e;
\$file = "\$dir/" . basename \$file;
\$rt = ", assuming runtime \$dir"
}
if( -f \$file && ! -x \$file ) {
printf( "%-*s [Exists\$rt]\n", \$maxf, basename( \$file ) );
} elsif( -d \$dir ) {
printf( "%-*s [Directory exists\$rt]\n", \$maxf, '' );
} else {
printf( "%-*s [Not present\$rt]\n", \$maxf, '' );
}
}
print( "\n The first file found will be used at runtime.\n Use BlockCountries config to determine which file is selected.\n" );
exit;
EOF
else
echo "BlockCountries not found in `dirname $0`"
fi
echo ""
if [ -n "$OK" ]; then
echo "All checks completed successfully by $prog v$BCVERSION"
exit 0
fi
echo "*** $prog: BlockCountries requirements v$BCVERSION are **not** satisfied"
exit 1