-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathinstall.pl
executable file
·2281 lines (2044 loc) · 76.9 KB
/
install.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
#
#########################################################################
#
# File: install.pl
#
# Purpose: install.pl is the installation script for psad. It is safe
# to execute install.pl even if psad has already been installed
# on a system since install.pl will preserve the existing
# config section.
#
# Credits: (see the CREDITS file)
#
# Copyright (C) 1999-2018 Michael Rash ([email protected])
#
# License (GNU Public License):
#
# 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
#
# TODO:
#
#########################################################################
#
use Cwd;
use File::Path;
use File::Copy;
use Sys::Hostname;
use IO::Socket;
use Getopt::Long;
use strict;
#============== config ===============
my $USRSBIN_DIR = '/usr/sbin'; ### consistent with FHS (Filesystem
### Hierarchy Standard)
my $USRBIN_DIR = '/usr/bin'; ### consistent with FHS
my $psad_conf_file = 'psad.conf';
### system binaries ###
my $chkconfigCmd = '/sbin/chkconfig';
my $rcupdateCmd = '/sbin/rc-update'; ### Gentoo
my $updatercdCmd = '/usr/sbin/update-rc.d'; ### Ubuntu
my $makeCmd = '/usr/bin/make';
my $perlCmd = '/usr/bin/perl';
my $wgetCmd = '/usr/bin/wget';
my $runlevelCmd = '/sbin/runlevel';
my $systemctlCmd = '/bin/systemctl';
my $install_root = '/';
my $answers_file = 'install.answers';
#============ end config ============
my %file_vars = (
'signatures' => 'SIGS_FILE',
'auto_dl' => 'AUTO_DL_FILE',
'icmp_types' => 'ICMP_TYPES_FILE',
'icmp6_types' => 'ICMP6_TYPES_FILE',
'posf' => 'POSF_FILE',
'pf.os' => 'P0F_FILE',
'snort_rule_dl' => 'SNORT_RULE_DL_FILE',
'ip_options' => 'IP_OPTS_FILE',
'protocols' => 'PROTOCOLS_FILE'
);
my %exclude_cmds = (
'wget' => '',
'netstat' => '',
'mail' => '',
'sendmail' => '',
'uname' => '',
'df' => '',
'psadwatchd' => '',
'kmsgsd' => '',
'psad' => '',
'whois' => '',
'fwcheck_psad' => ''
);
### map perl modules to versions
my %required_perl_modules = (
'Unix::Syslog' => {
'force-install' => 0,
'mod-dir' => 'Unix-Syslog'
},
'Bit::Vector' => {
'force-install' => 0,
'mod-dir' => 'Bit-Vector'
},
'Storable', => {
'force-install' => 0,
'mod-dir' => 'Storable'
},
'Carp::Clan', => {
'force-install' => 0,
'mod-dir' => 'Carp-Clan'
},
'Date::Calc', => {
'force-install' => 0,
'mod-dir' => 'Date-Calc'
},
'NetAddr::IP' => {
'force-install' => 0,
'mod-dir' => 'NetAddr-IP'
},
'IPTables::Parse' => {
'force-install' => 1,
'mod-dir' => 'IPTables-Parse'
},
'IPTables::ChainMgr' => {
'force-install' => 1,
'mod-dir' => 'IPTables-ChainMgr'
},
);
my @ordered_modules = (qw/
Unix::Syslog
Bit::Vector
Storable
Carp::Clan
Date::Calc
NetAddr::IP
IPTables::Parse
IPTables::ChainMgr
/);
my %config = ();
my %cmds = ();
my @cmd_search_paths = qw(
/bin
/sbin
/usr/bin
/usr/sbin
/usr/local/bin
/usr/local/sbin
);
### IP regex
my $ip_re = qr|(?:[0-2]?\d{1,2}\.){3}[0-2]?\d{1,2}|;
### get the hostname of the system
my $HOSTNAME = hostname();
my $src_dir = getcwd() or die "[*] Could not get current working directory.";
### for user answers
my $ACCEPT_YES_DEFAULT = 1;
my $ACCEPT_NO_DEFAULT = 2;
my $NO_ANS_DEFAULT = 0;
### set the default execution flags and command line args
my $noarchive = 0;
my $uninstall = 0;
my $help = 0;
my $archived_old = 0;
my $use_answers = 0;
my $no_write_answers = 0;
my %answers = ();
my $skip_module_install = 0;
my $cmdline_force_install = 0;
my $force_path_update = 0;
my $force_mod_re = '';
my $exclude_mod_re = '';
my $install_test_dir = 0;
my $no_rm_old_lib_dir = 0;
my $syslog_conf = '';
my $locale = 'C'; ### default LC_ALL env variable
my $no_locale = 0;
my $deps_dir = 'deps';
my $init_dir = '/etc/init.d';
my $systemd_init_dir = '/lib/systemd/system';
my $init_name = 'psad';
my $systemd_init_name = 'psad.service';
my $install_syslog_fifo = 0;
my $runlevel = -1;
my @installation_lines = ();
### make Getopts case sensitive
Getopt::Long::Configure('no_ignore_case');
&usage(1) unless (GetOptions(
'config=s' => \$psad_conf_file, ### specify path to psad.conf
'force-mod-install' => \$cmdline_force_install, ### force install of all modules
'Force-mod-regex=s' => \$force_mod_re, ### force specific mod install with regex
'Exclude-mod-regex=s' => \$exclude_mod_re, ### exclude a particular perl module
'path-update' => \$force_path_update, ### update command paths
'Skip-mod-install' => \$skip_module_install,
'Use-answers' => \$use_answers,
'answers-file=s' => \$answers_file,
'no-write-answers' => \$no_write_answers,
'no-rm-lib-dir' => \$no_rm_old_lib_dir, ### remove any old /usr/lib/psad dir
'no-preserve' => \$noarchive, ### Don't preserve existing configs.
'syslog-conf=s' => \$syslog_conf, ### specify path to syslog config file.
'uninstall' => \$uninstall, ### Uninstall psad.
'init-dir=s' => \$init_dir,
'systemd-init-dir=s' => \$systemd_init_dir,
'init-name=s' => \$init_name,
'install-syslog-fifo' => \$install_syslog_fifo,
'install-root=s' => \$install_root,
'install-test-dir' => \$install_test_dir,
'runlevel=i' => \$runlevel,
'LC_ALL=s' => \$locale,
'no-LC_ALL' => \$no_locale,
'help' => \$help ### Display help.
));
&usage(0) if $help;
### set LC_ALL env variable
$ENV{'LC_ALL'} = $locale unless $no_locale;
### make a copy of the original psad.conf file and restore at the end
copy $psad_conf_file, "${psad_conf_file}.orig" or die "[*] Could not ",
"copy $psad_conf_file -> $psad_conf_file.orig";
if ($install_test_dir) {
$install_root = getcwd() . '/test/psad-install';
$init_dir = $install_root . '/etc/init.d';
}
&import_answers() if $use_answers;
### import paths from default psad.conf
&import_config();
$force_mod_re = qr|$force_mod_re| if $force_mod_re;
$exclude_mod_re = qr|$exclude_mod_re| if $exclude_mod_re;
### see if the deps/ directory exists, and if not then we are installing
### from the -nodeps sources so don't install any perl modules
$skip_module_install = 1 unless -d $deps_dir;
$cmds{'make'} = $makeCmd;
$cmds{'perl'} = $perlCmd;
$cmds{'runlevel'} = $runlevelCmd;
my $distro = &get_distro();
### handle systems with systemd
my $is_systemd = &look_for_process(qr|/systemd|);
if ($is_systemd) {
$init_dir = $systemd_init_dir;
$init_name = $systemd_init_name;
$cmds{'systemctl'} = $systemctlCmd;
die "[*] systemd init directory $init_dir does not exist, ",
"use --systemd-init-dir <path>" unless -d $init_dir;
} else {
if ($distro eq 'redhat' or $distro eq 'fedora') {
### add chkconfig only if we are runing on a redhat distro
$cmds{'chkconfig'} = $chkconfigCmd;
} elsif ($distro eq 'gentoo') {
### add rc-update if we are running on a gentoo distro
$cmds{'rc-update'} = $rcupdateCmd;
} elsif ($distro eq 'ubuntu') {
### add update-rc.d if we are running on an ubuntu distro
$cmds{'update-rc.d'} = $updatercdCmd;
}
unless (-d $init_dir) {
if (-d '/etc/rc.d/init.d') {
$init_dir = '/etc/rc.d/init.d';
} elsif (-d '/etc/rc.d') { ### for Slackware
$init_dir = '/etc/rc.d';
} else {
die "[*] Cannot find the init script directory, use ",
"--init-dir <path>" unless $install_test_dir;
}
}
}
### need to make sure this exists before attempting to
### write anything to the install log.
&full_mkdir($config{'PSAD_DIR'}, 0700);
### make sure the system binaries are where we expect
### them to be.
&check_commands();
### occasionally things from old psad installations need to be
### dealt with separately.
&check_old_psad_installation();
if ($uninstall) {
&uninstall();
} else {
&install();
open F, "> $config{'INSTALL_LOG_FILE'}" or die $!;
print F for @installation_lines;
close F;
}
### restore the original psad.conf file (this is just the local one in the
### sources directory).
if (-e "${psad_conf_file}.orig") {
unlink $psad_conf_file if -e $psad_conf_file;
move "${psad_conf_file}.orig", $psad_conf_file;
}
exit 0;
#================= end main =================
sub install() {
### make sure install.pl is being called from the source directory
unless (-e 'psad') {
die "[*] install.pl can only be executed from the directory\n",
" that contains the psad sources! Exiting.";
}
&logr('[+] ' . localtime() . " Installing psad on hostname: $HOSTNAME\n");
### make sure another psad process is not running
if (&ask_to_stop_psad()) {
&stop_psad();
}
unless (-d $config{'PSAD_RUN_DIR'}) {
&logr("[+] Creating $config{'PSAD_RUN_DIR'}\n");
&full_mkdir($config{'PSAD_RUN_DIR'}, 0700);
}
unless (-d $config{'PSAD_FIFO_DIR'}) {
&logr("[+] Creating $config{'PSAD_FIFO_DIR'}\n");
&full_mkdir($config{'PSAD_FIFO_DIR'}, 0700);
}
### change any existing psad module directory to allow anyone to import
unless ($skip_module_install) {
my $dir_tmp = $config{'PSAD_LIBS_DIR'};
$dir_tmp =~ s|lib/|lib64/|;
for my $dir ($config{'PSAD_LIBS_DIR'}, $dir_tmp) {
if (-d $dir) {
chmod 0755, $dir;
unless ($no_rm_old_lib_dir) {
&logr("[+] Removing $dir/ directory from previous " .
"psad installation.\n");
rmtree $dir;
}
}
}
}
unless (-d $config{'PSAD_CONF_DIR'}) {
&logr("[+] Creating $config{'PSAD_CONF_DIR'}\n");
&full_mkdir($config{'PSAD_CONF_DIR'}, 0700);
}
unless (-d $config{'CONF_ARCHIVE_DIR'}) {
&logr("[+] Creating $config{'CONF_ARCHIVE_DIR'}\n");
&full_mkdir($config{'CONF_ARCHIVE_DIR'}, 0700);
}
if ($install_syslog_fifo) {
unless (-e $config{'PSAD_FIFO_FILE'}) {
&logr("[+] Creating named pipe $config{'PSAD_FIFO_FILE'}\n");
unless (&run_cmd("$cmds{'mknod'} -m 600 " .
"$config{'PSAD_FIFO_FILE'} p") == 0) {
&logr("[*] Could not create the named pipe " .
"\"$config{'PSAD_FIFO_FILE'}\"!\n" .
"[*] psad requires this file to exist! Aborting install.\n");
die;
}
unless (-p $config{'PSAD_FIFO_FILE'}) {
&logr("[*] Could not create the named pipe " .
"\"$config{'PSAD_FIFO_FILE'}\"!\n" .
"[*] psad requires this file to exist! Aborting " .
"install.\n");
die;
}
}
}
unless (-d $config{'PSAD_DIR'}) {
&logr("[+] Creating $config{'PSAD_DIR'}\n");
&full_mkdir($config{'PSAD_DIR'}, 0700);
}
unless (-d $config{'PSAD_TMP_DIR'}) {
&logr("[+] Creating $config{'PSAD_TMP_DIR'}\n");
&full_mkdir($config{'PSAD_TMP_DIR'}, 0700);
}
unless (-d $config{'PSAD_LIBS_DIR'}) {
&logr("[+] Creating $config{'PSAD_LIBS_DIR'}\n");
&full_mkdir($config{'PSAD_LIBS_DIR'}, 0755);
}
unless (-e $config{'FW_DATA_FILE'}) {
&logr("[+] Creating $config{'FW_DATA_FILE'} file\n");
open F, "> $config{'FW_DATA_FILE'}" or die "[*] Could not open ",
"$config{'FW_DATA_FILE'}: $!";
close F;
chmod 0600, "$config{'FW_DATA_FILE'}";
&perms_ownership("$config{'FW_DATA_FILE'}", 0600);
}
unless (-d $USRSBIN_DIR) {
&logr("[+] Creating $USRSBIN_DIR\n");
&full_mkdir($USRSBIN_DIR, 0755);
}
if (-d 'deps' and -d 'deps/whois') {
&logr("[+] Compiling Marco d'Itri's whois client\n");
&run_cmd("$cmds{'make'} -C deps/whois");
if (-e 'deps/whois/whois') {
### if an old whois process is still around ("text file
### busy" error), then it is ok to not be able to copy
### the new whois binary into place; the old one should
### work fine.
&logr("[+] Copying whois binary to $cmds{'whois'}\n");
copy 'deps/whois/whois', $cmds{'whois'};
die "[*] Could not copy deps/whois/whois -> $cmds{'whois'}: $!"
unless -e $cmds{'whois'};
} else {
die "[*] Could not compile whois";
}
}
&perms_ownership($cmds{'whois'}, 0755);
print "\n\n";
### install perl modules
unless ($skip_module_install) {
for my $module (@ordered_modules) {
&install_perl_module($module);
}
}
my $snort_dir = 'deps/snort_rules';
if (-d 'deps' and -d $snort_dir) {
&logr("[+] Installing Snort-2.3.3 signatures in " .
"$config{'SNORT_RULES_DIR'}\n");
unless (-d $config{'SNORT_RULES_DIR'}) {
&full_mkdir($config{'SNORT_RULES_DIR'}, 0700);
}
opendir D, $snort_dir or die "[*] Could not open ",
"the $snort_dir/ directory: $!";
my @files = readdir D;
closedir D;
for my $file (@files) {
next unless $file =~ /\.rules$/ or $file =~ /\.config$/;
&logr("[+] Installing ${snort_dir}/${file}\n");
copy "${snort_dir}/${file}",
"$config{'SNORT_RULES_DIR'}/${file}" or
die "[*] Could not copy ${snort_dir}/${file} -> ",
"$config{'SNORT_RULES_DIR'}/${file}: $!";
&perms_ownership("$config{'SNORT_RULES_DIR'}/${file}", 0600);
}
}
### reputation feed data
my $repu_dir = 'deps/reputation_feeds';
if (-d 'deps' and -d $repu_dir) {
&logr("[+] Installing reputation feed data in " .
"$config{'REPUTATION_FEEDS_DIR'}\n");
unless (-d $config{'REPUTATION_FEEDS_DIR'}) {
&full_mkdir($config{'REPUTATION_FEEDS_DIR'}, 0700);
}
opendir D, $repu_dir or die "[*] Could not open ",
"the $repu_dir directory: $!";
my @files = readdir D;
closedir D;
for my $file (@files) {
next if $file eq '.' or $file eq '..';
&logr("[+] Installing ${repu_dir}/${file}\n");
copy "${repu_dir}/${file}",
"$config{'REPUTATION_FEEDS_DIR'}/${file}" or
die "[*] Could not copy ${repu_dir}/${file} -> ",
"$config{'REPUTATION_FEEDS_DIR'}/${file}: $!";
&perms_ownership("$config{'REPUTATION_FEEDS_DIR'}/${file}", 0600);
}
}
print "\n\n";
&logr("[+] Compiling kmsgsd, and psadwatchd:\n");
### remove any previously compiled kmsgsd
unlink 'kmsgsd' if -e 'kmsgsd';
### remove any previously compiled psadwatchd
unlink 'psadwatchd' if -e 'psadwatchd';
### compile the C psad daemons
&run_cmd($cmds{'make'});
&logr("[-] Could not compile kmsgsd.c.\n") unless (-e 'kmsgsd');
&logr("[-] Could not compile psadwatchd.c.\n") unless (-e 'psadwatchd');
### install fwcheck_psad.pl
print "\n\n";
&logr("[+] Verifying compilation of fwcheck_psad.pl script:\n");
unless (&run_cmd("$cmds{'perl'} -c fwcheck_psad.pl") == 0) {
die "[*] fwcheck_psad.pl does not compile with \"perl -c\". Download ",
"the latest sources from:\n\nhttp://www.cipherdyne.org/"
unless $skip_module_install;
}
### make sure the psad (perl) daemon compiles. The other three
### daemons have all been re-written in C.
&logr("[+] Verifying compilation of psad perl daemon:\n");
unless (&run_cmd("$cmds{'perl'} -c psad") == 0) {
die "[*] psad does not compile with \"perl -c\". Download the",
" latest sources from:\n\nhttp://www.cipherdyne.org/"
unless $skip_module_install;
}
### install nf2csv
&logr("[+] Verifying compilation of nf2csv script:\n");
unless (&run_cmd("$cmds{'perl'} -c nf2csv") == 0) {
die "[*] nf2csv does not compile with \"perl -c\". Download ",
"the latest sources from:\n\nhttp://www.cipherdyne.org/"
unless $skip_module_install;
}
### put the nf2csv script in place
unlink '/usr/sbin/nf2csv' if -e '/usr/sbin/nf2csv'; ### old path
&logr("[+] Copying nf2csv -> ${USRBIN_DIR}/nf2csv\n");
unlink "${USRBIN_DIR}/nf2csv" if -e "${USRBIN_DIR}/nf2csv";
copy 'nf2csv', "${USRBIN_DIR}/nf2csv" or die "[*] Could ",
"not copy nf2csv -> ${USRBIN_DIR}/nf2csv: $!";
&perms_ownership("${USRBIN_DIR}/nf2csv", 0755);
### put the fwcheck_psad.pl script in place
&logr("[+] Copying fwcheck_psad.pl -> $cmds{'fwcheck_psad'}\n");
unlink $cmds{'fwcheck_psad'} if -e $cmds{'fwcheck_psad'};
copy 'fwcheck_psad.pl', $cmds{'fwcheck_psad'} or die "[*] Could ",
"not copy fwcheck_psad.pl -> $cmds{'fwcheck_psad'}: $!";
&perms_ownership($cmds{'fwcheck_psad'}, 0500);
### put the psad daemons in place
&logr("[+] Copying psad -> ${USRSBIN_DIR}/psad\n");
unlink "${USRSBIN_DIR}/psad" if -e "${USRSBIN_DIR}/psad";
copy 'psad', "${USRSBIN_DIR}/psad" or die "[*] Could not copy ",
"psad -> ${USRSBIN_DIR}/psad: $!";
&perms_ownership("${USRSBIN_DIR}/psad", 0500);
&logr("[+] Copying psadwatchd -> ${USRSBIN_DIR}/psadwatchd\n");
unlink "${USRSBIN_DIR}/psadwatchd" if -e "${USRSBIN_DIR}/psadwatchd";
copy 'psadwatchd', "${USRSBIN_DIR}/psadwatchd" or die "[*] Could not ",
"copy psadwatchd -> ${USRSBIN_DIR}/psadwatchd: $!";
&perms_ownership("${USRSBIN_DIR}/psadwatchd", 0500);
&logr("[+] Copying kmsgsd -> ${USRSBIN_DIR}/kmsgsd\n");
unlink "${USRSBIN_DIR}/kmsgsd" if -e "${USRSBIN_DIR}/kmsgsd";
copy 'kmsgsd', "${USRSBIN_DIR}/kmsgsd" or die "[*] Could not copy ",
"kmsgsd -> ${USRSBIN_DIR}/kmsgsd: $!";
&perms_ownership("${USRSBIN_DIR}/kmsgsd", 0500);
unless (-d $config{'PSAD_CONF_DIR'}) {
&logr("[+] Creating $config{'PSAD_CONF_DIR'}\n");
&full_mkdir($config{'PSAD_CONF_DIR'}, 0700);
}
my $syslog_str = '';
if ($install_syslog_fifo) {
### get syslog daemon (e.g. syslog, rsyslog syslog-ng, or metalog)
$syslog_str = &query_syslog();
} else {
print
"[+] psad by default parses iptables log messages from the /var/log/messages\n",
" file, but you can alter this with the IPT_SYSLOG_FILE variable in the\n",
" /etc/psad/psad.conf file.\n";
}
my $preserve_rv = 0;
if (-e "$config{'PSAD_CONF_DIR'}/psad.conf") {
$preserve_rv = &query_preserve_config();
}
### the order of the config files is important (legacy FW_MSG_SEARCH
### vars in psad.conf).
my $prod_file = "$config{'PSAD_CONF_DIR'}/psad.conf";
if (-e $prod_file) {
&archive($prod_file) unless $noarchive;
if ($preserve_rv) {
&preserve_config('psad.conf', $prod_file);
} else {
&logr("[+] Copying psad.conf -> $prod_file\n");
copy 'psad.conf', $prod_file or die "[*] Could not ",
"copy psad.conf -> $prod_file: $!";
}
} else {
&logr("[+] Copying psad.conf -> $prod_file\n");
copy 'psad.conf', $prod_file or die "[*] Could not copy ",
"psad.conf -> $prod_file: $!";
}
if ($force_path_update or not $preserve_rv) {
&update_command_paths($prod_file);
}
&perms_ownership($prod_file, 0600);
### install auto_dl, signatures, icmp_types, posf, and pf.os files
for my $filename (keys %file_vars) {
my $file = $config{$file_vars{$filename}};
if (-e $file) {
&archive($file) unless $noarchive;
### FIXME, need a real config preservation routine for these files.
unless (&query_preserve_sigs_autodl($file)) {
&logr("[+] Copying $filename -> $file\n");
copy $filename, $file or die "[*] Could not ",
"copy $filename -> $file: $!";
&perms_ownership($file, 0600);
}
} else {
&logr("[+] Copying $filename -> $file\n");
copy $filename, $file or die "[*] Could not ",
"copy $filename -> $file: $!";
&perms_ownership($file, 0600);
}
}
### archive and remove legacy config files
for my $filename (qw(kmsgsd.conf psadwatchd.conf alert.conf
fw_search.conf)) {
my $path = "$config{'PSAD_CONF_DIR'}/$filename";
if (-e $path) {
&archive($path);
unlink $path;
}
}
&logr("\n");
unless ($preserve_rv) { ### we want to preserve the existing config
### get email address(es)
my $email_str = &query_email();
if ($email_str) {
&put_var('EMAIL_ADDRESSES', $email_str,
"$config{'PSAD_CONF_DIR'}/psad.conf");
}
### Give the admin the opportunity to set the strings that are
### parsed out of iptables messages. This is useful since the
### admin may have configured the firewall to use a logging prefix
### of "Audit" or something else other than the default string
### "DROP".
my $fw_search_ar = &get_fw_search_strings();
if ($fw_search_ar) {
open F, "< $config{'PSAD_CONF_DIR'}/psad.conf"
or die "[*] Could not open ",
"$config{'PSAD_CONF_DIR'}/psad.conf: $!";
my @lines = <F>;
close F;
open T, "> $config{'PSAD_CONF_DIR'}/psad.conf.tmp"
or die "[*] Could not open ",
"$config{'PSAD_CONF_DIR'}/psad.conf.tmp: $!";
for my $line (@lines) {
if ($line =~ /^\s*FW_MSG_SEARCH\s/) {
for my $fw_str (@$fw_search_ar) {
&logr(qq{[+] Setting FW_MSG_SEARCH to "$fw_str" } .
"in $config{'PSAD_CONF_DIR'}/psad.conf\n");
printf T "%-28s%s;\n", 'FW_MSG_SEARCH', $fw_str;
}
} else {
print T $line unless $line =~ /^\s*FW_MSG_SEARCH\s/;
}
}
close T;
move "$config{'PSAD_CONF_DIR'}/psad.conf.tmp",
"$config{'PSAD_CONF_DIR'}/psad.conf"
or die "[*] Could not move ",
"$config{'PSAD_CONF_DIR'}/psad.conf.tmp -> ",
"$config{'PSAD_CONF_DIR'}/psad.conf: $!";
}
### Give the admin the opportunity to set the HOME_NET variable.
&set_home_net("$config{'PSAD_CONF_DIR'}/psad.conf");
### see if the admin would like to have psad send info to
### DShield
if (&query_dshield()) {
&put_var('ENABLE_DSHIELD_ALERTS', 'Y',
"$config{'PSAD_CONF_DIR'}/psad.conf");
}
### Set the hostname
my $file = "$config{'PSAD_CONF_DIR'}/psad.conf";
&logr("[+] Setting hostname to \"$HOSTNAME\" in $file\n");
&set_hostname($file);
}
if ($install_syslog_fifo) {
&put_var('SYSLOG_DAEMON', $syslog_str,
"$config{'PSAD_CONF_DIR'}/psad.conf");
if ($syslog_str ne 'ulogd') {
my $restarted_syslog = 0;
if ($syslog_str eq 'syslogd') {
if (-e $syslog_conf) {
&append_fifo_syslog($syslog_conf);
if (&run_cmd("$cmds{'pkill'} --signal HUP syslogd 2> /dev/null") == 0) {
&logr("[+] HUP signal sent to syslogd.\n");
$restarted_syslog = 1;
}
}
} elsif ($syslog_str eq 'rsyslogd') {
if (-e $syslog_conf) {
&append_fifo_syslog($syslog_conf);
if (&run_cmd("$cmds{'pkill'} --signal HUP rsyslogd 2> /dev/null") == 0) {
&logr("[+] HUP signal sent to rsyslogd.\n");
$restarted_syslog = 1;
}
}
} elsif ($syslog_str eq 'syslog-ng') {
if (-e $syslog_conf) {
&append_fifo_syslog_ng($syslog_conf);
if (&run_cmd("$cmds{'pkill'} --signal HUP syslog-ng 2> /dev/null") == 0) {
&logr("[+] HUP signal sent to syslog-ng.\n");
$restarted_syslog = 1;
}
}
} elsif ($syslog_str eq 'metalog') {
if (-e $syslog_conf) {
&config_metalog($syslog_conf);
&logr("[-] Metalog support is shaky in psad. " .
"Use at your own risk.\n");
### don't send warning about not restarting metalog daemon
$restarted_syslog = 1;
}
}
unless ($restarted_syslog) {
&logr("[-] Could not restart any syslog daemons.\n");
}
}
}
### download signatures?
&run_cmd("${USRSBIN_DIR}/psad --sig-update") if &query_signatures();
### update reputation feeds?
&run_cmd("${USRSBIN_DIR}/psad --reputation-feeds-update")
if &query_reputation_feeds();
&install_manpage('psad.8');
&install_manpage('psadwatchd.8');
&install_manpage('kmsgsd.8');
&install_manpage('nf2csv.1');
my $init_file = '';
my $installed_init_script = 0;
if ($is_systemd) {
$init_file = 'init-scripts/systemd/psad.service';
} else {
if ($distro eq 'redhat') {
$init_file = 'init-scripts/psad-init.redhat';
} elsif ($distro eq 'fedora') {
$init_file = 'init-scripts/psad-init.fedora';
} elsif ($distro eq 'gentoo') {
$init_file = 'init-scripts/psad-init.gentoo';
} else {
$init_file = 'init-scripts/psad-init.generic';
}
}
if ($init_dir and &is_root()) {
&logr("[+] Copying $init_file -> ${init_dir}/$init_name\n");
copy $init_file, "${init_dir}/$init_name" or die "[*] Could not copy ",
"$init_file -> ${init_dir}/$init_name: $!";
if ($is_systemd) {
&perms_ownership("${init_dir}/$init_name", 0644);
} else {
&perms_ownership("${init_dir}/$init_name", 0744);
}
&enable_psad_at_boot($distro);
$installed_init_script = 1;
}
&logr("\n========================================================\n");
if ($archived_old) {
&logr("[+] Copies of your original configs have been made " .
"in: $config{'CONF_ARCHIVE_DIR'}\n");
}
if ($preserve_rv) {
&logr("\n[+] psad has been installed (with your original config merged).\n");
} else {
&logr("\n[+] psad has been installed.\n");
}
if ($installed_init_script) {
if ($init_dir) {
if ($is_systemd) {
&logr("\n[+] To start psad, run \"$cmds{'systemctl'} start psad\"\n");
} else {
&logr("\n[+] To start psad, run \"${init_dir}/psad start\"\n");
}
} else {
&logr("\n[+] To start psad, run ${USRSBIN_DIR}/psad\"\n");
}
}
return;
}
sub import_config() {
open C, "< $psad_conf_file"
or die "[*] Could not open $psad_conf_file: $!";
while (<C>) {
next if /^\s*#/;
if (/^\s*(\S+)\s+(.*?)\;/) {
my $varname = $1;
my $val = $2;
next if $varname eq 'IP_INFO';
if ($val =~ m|/.+| and $varname =~ /^\s*(\S+)Cmd$/) {
### found a command
$cmds{$1} = $val;
} else {
$config{$varname} = $val;
}
}
}
close C;
### see if the install root is the same as the default in psad.conf and
### update if not
if ($install_root ne '/') {
$install_root = getcwd() . "/$install_root"
unless $install_root =~ m|^/|;
$config{'INSTALL_ROOT'} = $install_root;
$USRSBIN_DIR = $config{'INSTALL_ROOT'} . $USRSBIN_DIR;
$USRBIN_DIR = $config{'INSTALL_ROOT'} . $USRBIN_DIR;
&put_var('INSTALL_ROOT', $install_root, $psad_conf_file);
}
for my $dir ($install_root, $USRSBIN_DIR, $USRBIN_DIR) {
&full_mkdir($dir, 0755) unless -d $dir;
}
if ($install_test_dir) {
&full_mkdir($init_dir, 0755) unless -d $init_dir;
}
### resolve internal vars within variable values
&expand_vars();
&required_vars();
$exclude_cmds{'ifconfig'} = '' if $config{'IFCFGTYPE'} =~ /iproute2/i;
return;
}
sub expand_vars() {
my $has_sub_var = 1;
my $resolve_ctr = 0;
while ($has_sub_var) {
$resolve_ctr++;
$has_sub_var = 0;
if ($resolve_ctr >= 20) {
die "[*] Exceeded maximum variable resolution counter.";
}
for my $hr (\%config, \%cmds) {
for my $var (keys %$hr) {
next if $var eq 'REPUTATION_FEED'
or $var eq 'IP_INFO' or $var eq 'IP_INFO_URL';
my $val = $hr->{$var};
if ($val =~ m|\$(\w+)|) {
my $sub_var = $1;
die "[*] sub-ver $sub_var not allowed within same ",
"variable $var" if $sub_var eq $var;
if (defined $config{$sub_var}) {
if ($sub_var eq 'INSTALL_ROOT' and $config{$sub_var} eq '/') {
$val =~ s|\$$sub_var||;
} else {
$val =~ s|\$$sub_var|$config{$sub_var}|;
}
$hr->{$var} = $val;
} else {
die "[*] sub-var \"$sub_var\" not defined in ",
"config for var: $var."
}
$has_sub_var = 1;
}
}
}
}
return;
}
sub required_vars() {
my @vars = (qw(
INSTALL_LOG_FILE PSAD_DIR PSAD_RUN_DIR PSAD_LIBS_DIR
SIG_UPDATE_URL PSAD_FIFO_DIR PSAD_FIFO_FILE SNORT_RULES_DIR
IP_OPTS_FILE SIGS_FILE AUTO_DL_FILE SNORT_RULE_DL_FILE
POSF_FILE P0F_FILE IP_OPTS_FILE FW_DATA_FILE
));
for my $var (@vars) {
die "[*] Missing required var: $var in $psad_conf_file"
unless defined $config{$var};
}
return;
}
sub uninstall() {
&logr("\n[+] Uninstalling psad from $HOSTNAME: " . localtime() . "\n");
unless (&query_yes_no('[+] This will completely remove psad ' .
"from your system.\n Are you sure (y/n)? ",
$NO_ANS_DEFAULT)) {
&logr("[*] User aborted uninstall by answering \"n\" to the remove " .
"question! Exiting.\n");
exit 0;
}
### after this point, psad will really be uninstalled so stop writing stuff
### to the install.log file. Just print everything to STDOUT
if (-e $config{'PSAD_PID_FILE'}) {
open PID, "$config{'PSAD_PID_FILE'}" or die "[*] Could not open ",
"$config{'PSAD_PID_FILE'}: $!";
my $pid = <PID>;
close PID;
chomp $pid;
if (kill 0, $pid) {
print "[+] Stopping psad daemons!\n";
if (-e "${init_dir}/psad") { ### prefer this for old versions
&run_cmd("${init_dir}/psad stop");
} else {
&run_cmd("${USRSBIN_DIR}/psad --Kill");
}
}
}
if (-e $cmds{'fwcheck_psad'}) {
print "[+] Removing $cmds{'fwcheck_psad'}\n";
unlink $cmds{'fwcheck_psad'};
}
if (-e "${USRBIN_DIR}/nf2csv") {
print "[+] Removing ${USRBIN_DIR}/nf2csv\n";
unlink "${USRBIN_DIR}/nf2csv";
}
if (-e "${USRSBIN_DIR}/psad") {
print "[+] Removing psad daemons: ${USRSBIN_DIR}/",
"(psad, psadwatchd, kmsgsd)\n";
unlink "${USRSBIN_DIR}/psad" or
warn "[*] Could not remove ${USRSBIN_DIR}/psad!!!\n";
unlink "${USRSBIN_DIR}/psadwatchd" or
warn "[*] Could not remove ${USRSBIN_DIR}/psadwatchd!!!\n";
unlink "${USRSBIN_DIR}/kmsgsd" or
warn "[*] Could not remove ${USRSBIN_DIR}/kmsgsd!!!\n";
}
if (-e "${init_dir}/psad") {
print "[+] Removing ${init_dir}/$init_name\n";
unlink "${init_dir}/$init_name";
}
if (-d $config{'PSAD_CONF_DIR'}) {
print "[+] Removing configuration directory: $config{'PSAD_CONF_DIR'}";
rmtree($config{'PSAD_CONF_DIR'}, 1, 0);
}
if (-d $config{'PSAD_DIR'}) {
print "[+] Removing logging directory: $config{'PSAD_DIR'}\n";
rmtree($config{'PSAD_DIR'}, 1, 0);
}
if (-e $config{'PSAD_FIFO_FILE'}) {
print "[+] Removing named pipe: $config{'PSAD_FIFO_FILE'}\n";
unlink $config{'PSAD_FIFO_FILE'};
}
if (-e $cmds{'whois'}) {
print "[+] Removing $cmds{'whois'}\n";
unlink $cmds{'whois'};
}
if (-d $config{'PSAD_FIFO_DIR'}) {
print "[+] Removing $config{'PSAD_FIFO_DIR'}\n";
rmtree $config{'PSAD_FIFO_DIR'};
}
if (-d $config{'PSAD_RUN_DIR'}) {
print "[+] Removing $config{'PSAD_RUN_DIR'}\n";
rmtree $config{'PSAD_RUN_DIR'};
}
my $dir_tmp = $config{'PSAD_LIBS_DIR'};
$dir_tmp =~ s|lib/|lib64/|;
for my $dir ($config{'PSAD_LIBS_DIR'}, $dir_tmp) {
if (-d $dir) {
print "[+] Removing $dir\n";
rmtree $dir;
}
}
print "\n[+] psad has been uninstalled!\n";
return;
}
sub ask_to_stop_psad() {
if (-e $config{'PSAD_PID_FILE'}) {
open P, "< $config{'PSAD_PID_FILE'}" or die "[*] Could not open ",
"$config{'PSAD_PID_FILE'}: $!";
my $pid = <P>;
close P;
chomp $pid;
if (kill 0, $pid) {