-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderexpose.pl
executable file
·1285 lines (1167 loc) · 46.9 KB
/
underexpose.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
#
# %{NAME} - Anonymous, private, and caching web proxy installer.
# Copyright (C) 2013-%{YEAR} Brandon Perkins <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
##############################################################################
# Import some semantics into the current package from the named modules
##############################################################################
use strict; # Restrict unsafe constructs
use warnings; # Control optional warnings
use File::Basename; # Parse file paths into directory,
# filename and suffix.
use File::Path; # Create or remove directory trees
use File::ReadBackwards; # Read a file backwards by lines.
use File::Temp; # Return name and handle of a temporary
# file safely
use Getopt::Long; # Getopt::Long - Extended processing
# of command line options
use IO::Select; # OO interface to the select system call
use IPC::Open3; # Open a process for reading, writing,
# and error handling using open3()
use Log::Log4perl; # Log4j implementation for Perl
use Pod::Usage; # Pod::Usage, pod2usage() - print a
# usage message from embedded pod
# documentation
use WWW::Curl::Easy; # WWW::Curl - Perl extension interface
# for libcurl
##############################################################################
# Declare constants
##############################################################################
binmode STDOUT, ":utf8"; # Output UTF-8 using the :utf8 output layer.
# This ensures that the output is completely
# UTF-8, and removes any debug warnings.
$ENV{PATH} = "/usr/sbin:/sbin:/usr/bin:/bin";
$ENV{PAGER} = "more"; # Keep pod2usage output happy
my $name = "%{NAME}"; # Name string
my $version = "%{VERSION}"; # Version number
my $release = "%{RELEASE}"; # Release string
my $torsocksport = "9050"; # Second-generation onion router port
my $privoxylistenport = "8118"; # Privacy Enhancing Proxy port
my $squidhttpport = "3128"; # HTTP web proxy caching server port
my ( $wtr, $rdr, $err, $cmd );
use Symbol 'gensym';
$err = gensym;
##############################################################################
# Specify module configuration options to be enabled
##############################################################################
# Allow single-character options to be bundled. To distinguish bundles from long
# option names, long options must be introduced with '--' and bundles with '-'.
# Do not allow '+' to start options.
Getopt::Long::Configure(qw(bundling no_getopt_compat));
##############################################################################
# Initialize variables
##############################################################################
my $DBG = 1; # Set debug output level:
# 0 -- quiet
# 1 -- normal
# 2 -- verbose
# 3 -- debug
my $dbgtablewidth = 47;
my @ARGVOPTS = @ARGV; # Store original command-line arguments
my %conf; # Active configuration
my %confargs; # Configuration provided by command-line
##############################################################################
# cURL options
##############################################################################
my $curloptproxy = "127.0.0.1";
my $curloptuseragent = "$name/$version";
my $curloptverbose = 0;
##############################################################################
# Parse command line options. This function adheres to the POSIX syntax
# for CLI options, with GNU extensions.
##############################################################################
# Initialize GetOptions variables
my $optcnfdir = "/etc/$name";
my $optdebug;
my $optdocdir = "/usr/share/doc/$name-$version";
my $opthelp;
my $optlogdir = "/var/log/$name";
my $optman;
my $optmandir = "/usr/share/man/man8";
my $optquiet;
my $optsetup;
my $optuninst;
my $optverbose;
my $optversion;
GetOptions(
"c=i" => \$confargs{circuits},
"circuits=i" => \$confargs{circuits},
"C=s" => \$optcnfdir,
"configdir=s" => \$optcnfdir,
"d" => \$optdebug,
"debug" => \$optdebug,
"D=s" => \$optdocdir,
"docdir=s" => \$optdocdir,
"h" => \$opthelp,
"help" => \$opthelp,
"L=s" => \$optlogdir,
"logdir=s" => \$optlogdir,
"m" => \$optman,
"man" => \$optman,
"M=s" => \$optmandir,
"mandir=s" => \$optmandir,
"q" => \$optquiet,
"quiet" => \$optquiet,
"S" => \$optsetup,
"setup" => \$optsetup,
"U" => \$optuninst,
"uninstall" => \$optuninst,
"v" => \$optverbose,
"verbose" => \$optverbose,
"V" => \$optversion,
"version" => \$optversion
) or pod2usage(2);
##############################################################################
# Help function
##############################################################################
pod2usage(1) if $opthelp;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $optman;
##############################################################################
# Version function
##############################################################################
if ($optversion) {
print "$name $version ($release)\n";
exit 0;
}
##############################################################################
# Initialize Logger
##############################################################################
my $logcnf = $optcnfdir . "/log4perl.conf";
unless ( -f "$logcnf" && -r "$logcnf" ) {
die "Cannot read Log4perl configuration file $logcnf: $!\n";
}
unless ( -d "$optlogdir" ) {
unless ( mkpath($optlogdir) ) {
die "Cannot create log directory $optlogdir: $!\n";
}
}
unless ( -d "$optlogdir" && -w "$optlogdir" ) {
die "Cannot write to log directory $optlogdir: $!\n";
}
sub getLogDir() {
return $optlogdir;
}
Log::Log4perl::init($logcnf);
my $logger = Log::Log4perl->get_logger();
$logger->debug( "Log4perl logger started using " . $logcnf );
$logger->debug( "GetOptions: " . join( "|", @ARGVOPTS ) );
my $installer = $optlogdir . "/" . $name . "_install";
my $uninstaller = $optlogdir . "/" . $name . "_uninstall";
my $conffile = $optcnfdir . "/$name.conf";
##############################################################################
# Set output level
##############################################################################
# If multiple outputs are specified, the most verbose will be used.
if ($optquiet) {
$DBG = 0;
}
if ($optverbose) {
$DBG = 2;
$| = 1;
}
if ($optdebug) {
$DBG = 3;
$| = 1;
}
$logger->debug("Debug level set to $DBG");
##############################################################################
# Setup temporary directory
##############################################################################
my $tmpdir = File::Temp->newdir();
my $tmpdirname = $tmpdir->dirname;
##############################################################################
# Determine if SELinux is enabled and enforcing
##############################################################################
$cmd = "selinuxenabled";
$logger->debug("Determining if SELinux is enabled");
&runcmd;
$cmd = "getenforce | grep -i ^enforcing\$ > /dev/null";
$logger->debug("Determining if SELinux is enforcing");
&runcmd;
##############################################################################
# SELinux port types
##############################################################################
my $torpt = "tor_port_t";
my $privoxypt = "http_cache_port_t";
my $squidpt = "squid_port_t";
##############################################################################
# Configuration file locations
##############################################################################
my $torcfg = "/etc/tor/torrc";
my $privoxycfg = "/etc/privoxy/config";
my $squidcfg = "/etc/squid/squid.conf";
##############################################################################
# Log rotate file locations
##############################################################################
my $torlrfile = "/etc/logrotate.d/tor";
my $privoxylrfile = "/etc/logrotate.d/privoxy";
my $squidlrfile = "/etc/logrotate.d/squid";
##############################################################################
# Data directory locations
##############################################################################
my $tordatadir = "/var/lib/tor";
my $privoxydatadir = "";
my $squiddatadir = "/var/spool/squid";
##############################################################################
# Log directory locations
##############################################################################
my $torlogdir = "/var/log/tor";
my $privoxylogdir = "/var/log/privoxy";
my $squidlogdir = "/var/log/squid";
##############################################################################
# Checking for invalid options
##############################################################################
if ( $optsetup && $optuninst ) {
$logger->logcroak("Cannot specify setup and uninstall at the same time!");
}
##############################################################################
# Running Setup
##############################################################################
if ($optsetup) {
$logger->info("Running $name setup...");
$logger->info("Using configuration file $conffile");
&readconf;
&loadargs;
&checkconf;
&writeconf;
$logger->info("Done.");
exit 0;
}
##############################################################################
# Running Uninstaller
##############################################################################
if ($optuninst) {
$logger->warn("Running $name uninstaller...");
$logger->info("Using installation file $installer");
my $bwinst;
unless ( $bwinst = File::ReadBackwards->new($installer) ) {
$logger->logcroak("Cannot open $installer file for reading: $?");
}
if ( -f $uninstaller ) {
my (
$dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
$size, $atime, $mtime, $ctime, $blksize, $blocks
) = stat($uninstaller);
unless ( rename( $uninstaller, "$uninstaller.$mtime" ) ) {
$logger->logcroak(
"Unable to rename $uninstaller as $uninstaller.$mtime");
}
}
unless ( open( UNINST, ">$uninstaller" ) ) {
$logger->logcroak("Cannot open $uninstaller file for writing: $?");
}
while ( defined( my $logline = $bwinst->readline ) ) {
print ".";
$cmd = $logline;
if ( $cmd =~ m/^semanage port / ) {
if ( $cmd =~ m/^semanage port -a / ) {
$cmd =~ s/^semanage port -a /semanage port -d /g;
}
elsif ( $cmd =~ m/^semanage port -d / ) {
$cmd =~ s/^semanage port -d /semanage port -a /g;
}
else {
$logger->logcroak("Unknown semanage port command: $cmd");
}
}
if ( $cmd =~ m/^semanage fcontext / ) {
if ( $cmd =~ m/^semanage fcontext -a / ) {
$cmd =~ s/^semanage fcontext -a /semanage fcontext -d /g;
}
elsif ( $cmd =~ m/^semanage fcontext -d / ) {
$cmd =~ s/^semanage fcontext -d /semanage fcontext -a /g;
}
else {
$logger->logcroak("Unknown semanage fcontext command: $cmd");
}
}
$cmd =~ s /^systemctl enable /systemctl disable /g;
$cmd =~ s /^systemctl start /systemctl stop /g;
$cmd =~ s /^touch /rm /g;
&runcmd;
print UNINST "$cmd";
}
print "\n";
close(UNINST);
$logger->warn("Done.");
exit 0;
}
##############################################################################
# Running Installer
##############################################################################
$logger->info("Running $name installer...");
if ( -f $installer ) {
my (
$dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
$size, $atime, $mtime, $ctime, $blksize, $blocks
) = stat($installer);
unless ( rename( $installer, "$installer.$mtime" ) ) {
$logger->logcroak("Unable to rename $installer as $installer.$mtime");
}
}
unless ( open( INST, ">$installer" ) ) {
$logger->logcroak("Cannot open $installer file for writing: $?");
}
$logger->info("Using configuration file $conffile");
&readconf;
&loadargs;
&checkconf;
&writeconf;
##############################################################################
# cURL browser setup
##############################################################################
my $browser = WWW::Curl::Easy->new;
$browser->setopt( CURLOPT_VERBOSE, $curloptverbose );
my $curlversion = $browser->version(CURLVERSION_NOW);
chomp $curlversion;
my @curlversions = split( /\s/, $curlversion );
my %libversions;
foreach my $curlver (@curlversions) {
my ( $lib, $ver ) = split( /\//, $curlver );
my ( $major, $minor, $patch ) = split( /\./, $ver );
$libversions{$lib} = $ver;
$libversions{ $lib . '-major' } = $major;
$libversions{ $lib . '-minor' } = $minor;
$libversions{ $lib . '-patch' } = $patch;
my $title = "curl, libcurl, & 3rd party library versions";
$title =~ tr/a-z/A-Z/;
my $eqsgns = ( ( $dbgtablewidth - length($title) - 2 ) / 2 );
my $eqcount = 0;
my $eqhr = "";
while ( $eqcount < $eqsgns ) {
$eqhr = $eqhr . "=";
$eqcount++;
}
my $hdr = sprintf( "%s %s %s", $eqhr, $title, $eqhr );
$logger->debug( sprintf( "%${dbgtablewidth}.${dbgtablewidth}s\n", $hdr ) );
my $maxmodlength = 0;
foreach my $name ( keys %libversions ) {
my $modlength = length($name);
if ( $modlength > $maxmodlength ) {
$maxmodlength = $modlength;
}
}
my $modverlength = ( $dbgtablewidth - $maxmodlength - 8 );
foreach my $name ( sort ( keys %libversions ) ) {
my $info = $libversions{$name};
$logger->debug(
sprintf(
"== %${maxmodlength}.${maxmodlength}s: %-${modverlength}.${modverlength}s ==\n",
$name, $info
)
) if defined $info;
}
}
$browser->setopt( CURLOPT_USERAGENT, $curloptuseragent );
my $retcode;
# Get SELinux port status before run
$cmd = "semanage port -E | sort > $tmpdirname/seports.before";
$logger->debug("Getting SELinux port status before run");
&runcmd;
# Get SELinux fcontext status before run
$cmd = "semanage fcontext -E | sort > $tmpdirname/sefcontexts.before";
$logger->debug("Getting SELinux fcontext status before run");
&runcmd;
my $circuit = 0;
while ( $circuit < $conf{circuits} ) {
$circuit++;
$logger->info("Installing circuit $circuit...");
##########################################################################
# Tor Circuit installation
##########################################################################
$logger->info(
"Installing tor circuit $circuit on port $conf{'torport' . $circuit}..."
);
##########################################################################
# Tor SELinux port type modifications
##########################################################################
$logger->debug(
"Setting SELinux type to $torpt on tcp protocol port $conf{'torport' . $circuit}..."
);
$cmd =
"semanage port -a -t $torpt -p tcp $conf{'torport' . $circuit} ; if [ \$? -ne 0 ]; then semanage port -m -t $torpt -p tcp $conf{'torport' . $circuit}; fi";
&runcmd;
##########################################################################
# Tor configuration file generation
##########################################################################
$logger->debug(
"Configuring tor daemon running on port $conf{'torport' . $circuit}..."
);
my $torc = $torcfg . "_" . $conf{ 'torport' . $circuit };
$cmd = "touch $torc";
&runcmd;
print INST "$cmd\n";
unless ( open( CFG, ">$torc" ) ) {
$logger->logcroak("Unable to open $torc for writing: $!");
}
print CFG "SocksPort $conf{'torport' . $circuit} ";
print CFG "# Bind to localhost:$conf{'torport' . $circuit} ";
print CFG "for local connections.\n";
print CFG "AllowInvalidNodes middle,rendezvous\n";
my @loglevels = ( "debug", "info", "notice", "warn", "err" );
foreach my $loglevel (@loglevels) {
my $logf =
"$torlogdir/"
. $loglevel . "_"
. $conf{ 'torport' . $circuit } . ".log";
print CFG "Log $loglevel file $logf\n";
if ( $loglevel =~ m/^notice$/ ) {
print CFG "Log $loglevel syslog\n";
}
}
print CFG "RunAsDaemon 1\n";
my $tordd = $tordatadir . "_" . $conf{ 'torport' . $circuit };
if ( !-d $tordd ) {
unless ( mkdir($tordd) ) {
$logger->logcroak("Unable to create directory $tordd: $!");
}
$cmd = "chmod \$(stat -c %a $tordatadir) $tordd";
&runcmd;
$cmd = "chcon \$(stat -c %C $tordatadir) $tordd";
&runcmd;
$cmd = "chgrp \$(stat -c %G $tordatadir) $tordd";
&runcmd;
$cmd = "chown \$(stat -c %U $tordatadir) $tordd";
&runcmd;
$cmd =
"semanage fcontext -a -e $tordatadir $tordd ; if [ \$? -ne 0 ]; then semanage fcontext -m -e $tordatadir $tordd; fi";
&runcmd;
$cmd = "restorecon -R -v $tordd";
&runcmd;
}
print CFG "DataDirectory $tordd\n";
print CFG "User toranon\n";
print CFG "PidFile $tordd/tor_" . $conf{ 'torport' . $circuit } . ".pid\n";
close(CFG);
$cmd = "chmod \$(stat -c %a $torcfg) $torc";
&runcmd;
$cmd = "chcon \$(stat -c %C $torcfg) $torc";
&runcmd;
$cmd = "chgrp \$(stat -c %G $torcfg) $torc";
&runcmd;
$cmd = "chown \$(stat -c %U $torcfg) $torc";
&runcmd;
$cmd =
"semanage fcontext -a -e $torcfg $torc ; if [ \$? -ne 0 ]; then semanage fcontext -m -e $torcfg $torc; fi";
&runcmd;
$cmd = "restorecon -R -v $torc";
&runcmd;
##########################################################################
# Tor systemd system and service management
##########################################################################
$logger->debug(
"Enabling tor daemon running on port $conf{'torport' . $circuit}...");
$cmd = "systemctl enable tor@" . $conf{ 'torport' . $circuit } . ".service";
&runcmd;
print INST "$cmd\n";
$logger->debug(
"Starting tor daemon running on port $conf{'torport' . $circuit}...");
$cmd = "systemctl start tor@" . $conf{ 'torport' . $circuit } . ".service";
&runcmd;
print INST "$cmd\n";
##########################################################################
# Tor simple tests
##########################################################################
$logger->info(
"Testing tor daemon running on port $conf{'torport' . $circuit}...");
testtor( $conf{ 'torport' . $circuit }, "socks" );
$logger->info(
"Installation of tor circuit $circuit on port $conf{'torport' . $circuit} is complete."
);
##########################################################################
# Privoxy Circuit installation
##########################################################################
$logger->info(
"Installing privoxy circuit $circuit on port $conf{'privoxyport' . $circuit}..."
);
##########################################################################
# Privoxy SELinux port type modifications
##########################################################################
$logger->debug(
"Setting SELinux type to $privoxypt on tcp protocol port $conf{'privoxyport' . $circuit}..."
);
$cmd =
"semanage port -a -t $privoxypt -p tcp $conf{'privoxyport' . $circuit} ; if [ \$? -ne 0 ]; then semanage port -m -t $privoxypt -p tcp $conf{'privoxyport' . $circuit}; fi";
&runcmd;
##########################################################################
# Privoxy configuration file generation
##########################################################################
$logger->debug(
"Configuring privoxy daemon running on port $conf{'privoxyport' . $circuit}..."
);
my $privoxyc = $privoxycfg . "_" . $conf{ 'privoxyport' . $circuit };
$cmd = "touch $privoxyc";
&runcmd;
print INST "$cmd\n";
unless ( open( CFG, ">$privoxyc" ) ) {
$logger->logcroak("Unable to open $privoxyc for writing: $!");
}
print CFG "confdir /etc/privoxy\n";
print CFG "logdir $privoxylogdir\n";
print CFG "actionsfile match-all.action ";
print CFG "# Actions that are applied to ";
print CFG "all sites and maybe overruled later on.\n";
print CFG "actionsfile default.action # Main actions file\n";
print CFG "actionsfile user.action # User customizations\n";
print CFG "filterfile default.filter\n";
print CFG "filterfile user.filter # User customizations\n";
print CFG "logfile logfile_" . $conf{ 'privoxyport' . $circuit } . ".log\n";
print CFG "debug 1 ";
print CFG "# Log the destination for each request ";
print CFG "Privoxy let through. See also debug 1024.\n";
print CFG "debug 1024 # Actions that are applied ";
print CFG "to all sites and maybe overruled later on.\n";
print CFG "debug 4096 # Startup banner and warnings\n";
print CFG "debug 8192 # Non-fatal errors\n";
print CFG "listen-address $curloptproxy:$conf{'privoxyport' . $circuit}\n";
print CFG "toggle 1\n";
print CFG "enable-remote-toggle 0\n";
print CFG "enable-remote-http-toggle 0\n";
print CFG "enable-edit-actions 0\n";
print CFG "enforce-blocks 0\n";
print CFG "buffer-limit 4096\n";
print CFG "enable-proxy-authentication-forwarding 0\n";
my @pps = ( "/", ":443" );
foreach my $pp (@pps) {
print CFG "forward-socks5t $pp ";
print CFG "$curloptproxy:$conf{'torport' . $circuit} .\n";
print CFG "forward 192.168.*.*$pp . ";
print CFG "# Private-Use [RFC1918]\n";
my $privsub = 16;
while ( $privsub < 32 ) {
print CFG "forward 172.$privsub.*.*$pp . ";
print CFG "# Private-Use [RFC1918]\n";
$privsub++;
}
print CFG "forward 10.*.*.*$pp . ";
print CFG "# Private-Use [RFC1918]\n";
print CFG "forward 127.*.*.*$pp . ";
print CFG "# Loopback [RFC1122], section 3.2.1.3\n";
print CFG "forward localhost$pp .\n";
}
print CFG "forwarded-connect-retries 1\n";
print CFG "accept-intercepted-requests 0\n";
print CFG "allow-cgi-request-crunching 0\n";
print CFG "split-large-forms 0\n";
print CFG "keep-alive-timeout 5\n";
print CFG "tolerate-pipelining 1\n";
print CFG "socket-timeout 300\n";
close(CFG);
$cmd = "chmod \$(stat -c %a $privoxycfg) $privoxyc";
&runcmd;
$cmd = "chcon \$(stat -c %C $privoxycfg) $privoxyc";
&runcmd;
$cmd = "chgrp \$(stat -c %G $privoxycfg) $privoxyc";
&runcmd;
$cmd = "chown \$(stat -c %U $privoxycfg) $privoxyc";
&runcmd;
$cmd =
"semanage fcontext -a -e $privoxycfg $privoxyc ; if [ \$? -ne 0 ]; then semanage fcontext -m -e $privoxycfg $privoxyc; fi";
&runcmd;
$cmd = "restorecon -R -v $privoxyc";
&runcmd;
##########################################################################
# Privoxy systemd system and service management
##########################################################################
$logger->debug(
"Enabling privoxy daemon running on port $conf{'privoxyport' . $circuit}..."
);
$cmd =
"systemctl enable privoxy@"
. $conf{ 'privoxyport' . $circuit }
. ".service";
&runcmd;
print INST "$cmd\n";
$logger->debug(
"Starting privoxy daemon running on port $conf{'privoxyport' . $circuit}..."
);
$cmd =
"systemctl start privoxy@"
. $conf{ 'privoxyport' . $circuit }
. ".service";
&runcmd;
print INST "$cmd\n";
##########################################################################
# Privoxy simple tests
##########################################################################
$logger->info(
"Testing privoxy daemon running on port $conf{'privoxyport' . $circuit}..."
);
$browser->setopt( CURLOPT_PROXY, $curloptproxy );
$browser->setopt( CURLOPT_PROXYPORT, $conf{ 'privoxyport' . $circuit } );
$browser->setopt( CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
my $privoxytesturi = "http://config.privoxy.org/";
$browser->setopt( CURLOPT_URL, $privoxytesturi );
my $orgprivoxyconfightml;
$browser->setopt( CURLOPT_WRITEDATA, \$orgprivoxyconfightml );
$retcode = $browser->perform;
$logger->logcroak( "\nCannot get $privoxytesturi -- $retcode "
. $browser->strerror($retcode) . " "
. $browser->errbuf
. "\n" )
unless ( $retcode == 0 );
$logger->logcroak(
"\nDid not receive HTML, got -- ",
$browser->getinfo(CURLINFO_CONTENT_TYPE)
) unless $browser->getinfo(CURLINFO_CONTENT_TYPE) eq 'text/html';
$logger->trace($orgprivoxyconfightml);
if ( $orgprivoxyconfightml =~ m/Privoxy/ ) {
if ( $orgprivoxyconfightml =~ m/127\.0\.0\.1/ ) {
$logger->debug("Privoxy state appears to be up.");
if ( $orgprivoxyconfightml =~
m/port ($conf{ 'privoxyport' . $circuit })/
&& $orgprivoxyconfightml =~ m/ enabled/ )
{
$logger->info("Privoxy state is up.");
testtor( $conf{ 'privoxyport' . $circuit }, "http" );
$logger->info("Tor via Privoxy state is up.");
}
elsif ( $orgprivoxyconfightml =~ m/Privoxy is not being used/ ) {
$logger->logcroak("Privoxy state is down.");
}
else {
$logger->logcroak(
"Cannot determine Privoxy state: $orgprivoxyconfightml");
}
}
else {
$logger->logcroak(
"Cannot determine Privoxy state: $orgprivoxyconfightml");
}
}
else {
$logger->logcroak(
"Cannot determine Privoxy state: $orgprivoxyconfightml");
}
$logger->info(
"Installation of privoxy circuit $circuit on port $conf{'privoxyport' . $circuit} is complete."
);
}
##############################################################################
# Squid installation
##############################################################################
$logger->info("Installing squid on port $conf{'squidport'}...");
##############################################################################
# Squid SELinux port type modifications
##############################################################################
$logger->debug(
"Setting SELinux type to $squidpt on tcp protocol port $conf{'squidport'}..."
);
$cmd =
"semanage port -a -t $squidpt -p tcp $conf{'squidport'} ; if [ \$? -ne 0 ]; then semanage port -m -t $squidpt -p tcp $conf{'squidport'}; fi";
&runcmd;
##############################################################################
# Squid configuration file generation
##############################################################################
$logger->debug(
"Configuring squid daemon running on port $conf{'squidport'}...");
my $squidc = $squidcfg . "_" . $conf{'squidport'};
$cmd = "touch $squidc";
&runcmd;
print INST "$cmd\n";
unless ( open( CFG, ">$squidc" ) ) {
$logger->logcroak("Unable to open $squidc for writing: $!");
}
print CFG "acl localnet src 10.0.0.0/8\t";
print CFG "# RFC1918 possible internal network\n";
print CFG "acl localnet src 172.16.0.0/12\t";
print CFG "# RFC1918 possible internal network\n";
print CFG "acl localnet src 192.168.0.0/16\t";
print CFG "# RFC1918 possible internal network\n";
print CFG "acl SSL_ports port 443\n";
print CFG "acl Safe_ports port 80\t\t# http\n";
print CFG "acl Safe_ports port 21\t\t# ftp\n";
print CFG "acl Safe_ports port 443\t\t# https\n";
print CFG "acl Safe_ports port 70\t\t# gopher\n";
print CFG "acl Safe_ports port 210\t\t# wais\n";
print CFG "acl Safe_ports port 1025-65535\t# unregistered ports\n";
print CFG "acl Safe_ports port 280\t\t# http-mgmt\n";
print CFG "acl Safe_ports port 488\t\t# gss-http\n";
print CFG "acl Safe_ports port 591\t\t# filemaker\n";
print CFG "acl Safe_ports port 777\t\t# multiling http\n";
print CFG "acl CONNECT method CONNECT\n";
print CFG "http_access allow localhost manager\n";
print CFG "http_access deny manager\n";
print CFG "http_access deny !Safe_ports\n";
print CFG "http_access deny CONNECT !SSL_ports\n";
print CFG "http_access deny to_localhost\n";
print CFG "http_access allow localnet\n";
print CFG "http_access allow localhost\n";
print CFG "http_access deny all\n";
print CFG "icp_access deny all\n";
print CFG "htcp_access deny all\n";
print CFG "htcp_clr_access deny all\n";
print CFG "ident_lookup_access deny all\n";
print CFG "http_port $conf{'squidport'}\n";
$circuit = 0;
while ( $circuit < $conf{circuits} ) {
$circuit++;
print CFG "cache_peer $curloptproxy parent ";
print CFG "$conf{'privoxyport' . $circuit} 0 ";
print CFG "no-query round-robin no-digest ";
print CFG "name=localhost-$conf{'privoxyport' . $circuit}\n";
}
print CFG "hierarchy_stoplist cgi-bin \?\n";
my $squiddd = $squiddatadir . "_" . $conf{'squidport'};
if ( !-d $squiddd ) {
unless ( mkdir($squiddd) ) {
$logger->logcroak("Unable to create direcsquidy $squiddd: $!");
}
$cmd = "chmod \$(stat -c %a $squiddatadir) $squiddd";
&runcmd;
$cmd = "chcon \$(stat -c %C $squiddatadir) $squiddd";
&runcmd;
$cmd = "chgrp \$(stat -c %G $squiddatadir) $squiddd";
&runcmd;
$cmd = "chown \$(stat -c %U $squiddatadir) $squiddd";
&runcmd;
$cmd =
"semanage fcontext -a -e $squiddatadir $squiddd ; if [ \$? -ne 0 ]; then semanage fcontext -m -e $squiddatadir $squiddd; fi";
&runcmd;
$cmd = "restorecon -R -v $squiddd";
&runcmd;
}
print CFG "cache_dir ufs $squiddd 100 16 256\n";
print CFG "access_log daemon:$squidlogdir/access_"
. $conf{'squidport'}
. ".log squid\n";
print CFG "cache_store_log daemon:$squidlogdir/store_"
. $conf{'squidport'}
. ".log squid\n";
print CFG "log_mime_hdrs on\n";
print CFG "pid_filename /var/run/squid_" . $conf{'squidport'} . ".pid\n";
print CFG "cache_log $squidlogdir/cache_" . $conf{'squidport'} . ".log\n";
print CFG "coredump_dir $squiddd\n";
print CFG "refresh_pattern ^ftp:\t\t1440\t20%\t10080\n";
print CFG "refresh_pattern ^gopher:\t1440\t0%\t1440\n";
print CFG "refresh_pattern -i (/cgi-bin/|\\?) 0\t0%\t0\n";
print CFG "refresh_pattern .\t\t0\t20%\t4320\n";
print CFG "cache_effective_user squid\n";
print CFG "announce_port 0\n";
print CFG "snmp_port 0\n";
print CFG "snmp_access deny all\n";
print CFG "icp_port 0\n";
print CFG "htcp_port 0\n";
print CFG "log_icp_queries on\n";
print CFG "mcast_miss_port 0\n";
print CFG "always_direct deny all\n";
print CFG "never_direct allow all\n";
print CFG "forwarded_for delete\n";
print CFG "connect_retries 1\n";
print CFG "retry_on_error on\n";
close(CFG);
$cmd = "chmod \$(stat -c %a $squidcfg) $squidc";
&runcmd;
$cmd = "chcon \$(stat -c %C $squidcfg) $squidc";
&runcmd;
$cmd = "chgrp \$(stat -c %G $squidcfg) $squidc";
&runcmd;
$cmd = "chown \$(stat -c %U $squidcfg) $squidc";
&runcmd;
$cmd =
"semanage fcontext -a -e $squidcfg $squidc ; if [ \$? -ne 0 ]; then semanage fcontext -m -e $squidcfg $squidc; fi";
&runcmd;
$cmd = "restorecon -R -v $squidc";
&runcmd;
##############################################################################
# Squid systemd system and service management
##############################################################################
$logger->debug("Enabling squid daemon running on port $conf{'squidport'}...");
$cmd = "systemctl enable squid@" . $conf{'squidport'} . ".service";
&runcmd;
print INST "$cmd\n";
$logger->debug("Starting squid daemon running on port $conf{'squidport'}...");
$cmd = "systemctl start squid@" . $conf{'squidport'} . ".service";
&runcmd;
print INST "$cmd\n";
##############################################################################
# Squid simple tests
##############################################################################
$logger->info("Testing squid daemon running on port $conf{'squidport'}...");
$browser->setopt( CURLOPT_PROXY, "" );
my $squidtesturi =
"http://$curloptproxy:" . $conf{'squidport'} . "/squid-internal-mgr/info";
$browser->setopt( CURLOPT_URL, $squidtesturi );
my $squidinternalmgrhtml;
$browser->setopt( CURLOPT_WRITEDATA, \$squidinternalmgrhtml );
$retcode = $browser->perform;
$logger->logcroak( "\nCannot get $squidtesturi -- $retcode "
. $browser->strerror($retcode) . " "
. $browser->errbuf
. "\n" )
unless ( $retcode == 0 );
$logger->logcroak(
"\nDid not receive text, got -- ",
$browser->getinfo(CURLINFO_CONTENT_TYPE)
) unless $browser->getinfo(CURLINFO_CONTENT_TYPE) eq 'text/plain';
$logger->trace($squidinternalmgrhtml);
if ( $squidinternalmgrhtml =~ m/Squid Object Cache/ ) {
if ( $squidinternalmgrhtml =~ m/Connection information for squid/ ) {
$logger->debug("Squid state appears to be up.");
if ( $squidinternalmgrhtml =~ m/Cache information for squid/
&& $squidinternalmgrhtml =~ m/Memory usage for squid via mallinfo/ )
{
$logger->info("Squid state is up.");
testtor( $conf{'squidport'}, "http" );
$logger->info("Tor via Privoxy via Squid state is up.");
}
elsif ( $squidinternalmgrhtml =~ m/Squid is not being used/ ) {
$logger->logcroak("Squid state is down.");
}
else {
$logger->logcroak(
"Cannot determine Squid state: $squidinternalmgrhtml");
}
}
else {
$logger->logcroak(
"Cannot determine Squid state: $squidinternalmgrhtml");
}
}
else {
$logger->logcroak("Cannot determine Squid state: $squidinternalmgrhtml");
}
$logger->info("Installation of squid on port $conf{'squidport'} is complete.");
##############################################################################
# %{NAMEMIXED} systemd system and service management
##############################################################################
$logger->debug("Enabling %{NAME} target...");
$cmd = "systemctl enable %{NAME}.target";
&runcmd;
print INST "$cmd\n";
# Get SELinux port status after run
$cmd = "semanage port -E | sort > $tmpdirname/seports.after";
$logger->debug("Getting SELinux port status after run");
&runcmd;
# Get SELinux port type subtractions
$cmd =
"comm --nocheck-order -23 $tmpdirname/seports.{before,after} | sort > $tmpdirname/seports.subtracted";
$logger->debug("Getting SELinux port type subtractions");
&runcmd;
unless ( open( SEDIFF, "$tmpdirname/seports.subtracted" ) ) {
$logger->logcroak("Cannot open $tmpdirname/seports.subtracted for reading");
}
while (<SEDIFF>) {
$_ =~ s/^port -a /port -d /g;
print INST "semanage $_";
}
close(SEDIFF);
# Get SELinux port type additions
$cmd =
"comm --nocheck-order -13 $tmpdirname/seports.{before,after} | sort > $tmpdirname/seports.added";
$logger->debug("Getting SELinux port type additions");
&runcmd;
unless ( open( SEDIFF, "$tmpdirname/seports.added" ) ) {
$logger->logcroak("Cannot open $tmpdirname/seports.added for reading");
}
while (<SEDIFF>) {
print INST "semanage $_";
}
close(SEDIFF);
close(INST);
# Get SELinux fcontext status after run
$cmd = "semanage fcontext -E | sort > $tmpdirname/sefcontexts.after";
$logger->debug("Getting SELinux fcontext status after run");
&runcmd;
# Get SELinux fcontext type subtractions
$cmd =
"comm --nocheck-order -23 $tmpdirname/sefcontexts.{before,after} | sort > $tmpdirname/sefcontexts.subtracted";
$logger->debug("Getting SELinux fcontext type subtractions");
&runcmd;
unless ( open( SEDIFF, "$tmpdirname/sefcontexts.subtracted" ) ) {
$logger->logcroak(
"Cannot open $tmpdirname/sefcontexts.subtracted for reading");
}
while (<SEDIFF>) {
$_ =~ s/^fcontext -a /fcontext -d /g;
print INST "semanage $_";
}
close(SEDIFF);
# Get SELinux fcontext type additions
$cmd =
"comm --nocheck-order -13 $tmpdirname/sefcontexts.{before,after} | sort > $tmpdirname/sefcontexts.added";
$logger->debug("Getting SELinux fcontext type additions");
&runcmd;
unless ( open( SEDIFF, "$tmpdirname/sefcontexts.added" ) ) {
$logger->logcroak("Cannot open $tmpdirname/sefcontexts.added for reading");
}