-
Notifications
You must be signed in to change notification settings - Fork 10
/
openfpc
executable file
·1123 lines (1005 loc) · 39 KB
/
openfpc
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
# The master control program for starting and stopping OpenFPC instances.
#########################################################################################
# Copyright (C) 2012 - 2014 Leon Ward
# openfpc - Part of the OpenFPC - (Full Packet Capture) project
#
# Contact: [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.
#########################################################################################
use strict;
use warnings;
use Getopt::Long;
use Switch;
use Data::Dumper;
use File::Path qw(mkpath make_path remove_tree);
no if $] >= 5.017011, warnings => 'experimental::smartmatch';
my $openfpcver = 0.9;
my $conf_dir = "/etc/openfpc/";
my ( $quiet, $usertype, $verbose, $help );
my $init = 0; # init script friendly output (i.e not much!)
my $type = 0;
my $thing = 0;
my %configs = (); # Hash of all configs in $CONF_DIR;
my @nodes = (); # List if all nodes configured
my $action = 0;
my @daemonsNode = (
"openfpc-daemonlogger", # Daemons we want to start in OpenFPC Node mode
"openfpc-queued",
"openfpc-cxtracker",
"openfpc-cx2db"
);
my @daemonsNodeNoSession = (
"openfpc-daemonlogger", # Daemons we want to start in OpenFPC Node mode
"openfpc-queued"
); # with ENABLE_SESSION=0
my @daemonsProxy = ("openfpc-queued"); # Daemons we want to start in OpenFPC proxy mode
my $debug = 0;
my %defaults = (
PIDPATH => "/var/run/",
DAEMONLOGGER_CMD => "/usr/bin/daemonlogger",
CXTRACKER_CMD => "/usr/bin/cxtracker",
OPENFPC_QUEUED_CMD => "/usr/bin/openfpc-queued",
SESSION_DIR => "/var/tmp/openfpc/session",
SAVEDIR => "/var/tmp/openfpc/extracted",
BUFFER_DIR => "/var/tmp/openfpc/pcap",
CX2DB_CMD => "/usr/bin/openfpc-cx2db",
BPF_FILE => 0,
DROP_USER => "openfpc",
);
############################################
# End of globals
############################################
=head2 checkconfig
Check all config files to look for things I know will break the system and catch common mistakes
=cut
sub checkconfig {
my $c = shift;
my %o = (
success => 0,
error => "None",
);
my @ports = ();
my @errs = ();
foreach my $file ( keys %$c ) {
my $bad = 0;
if ( $c->{$file}{NODENAME} =~ /[-\?\\\/\.;]/ ) {
my $err = "Invalid characters found in NODENAME \"$c->{$file}{NODENAME}\"";
push( @errs, $err );
$bad = 1;
}
if ( $c->{$file}{'OFPC_ENABLED'} =~ /[yY1]/ ) {
if ( grep ( /$c->{$file}{'OFPC_PORT'}/, @ports ) ) {
#if ( $c->{$file}{'OFPC_PORT'} ~~ @ports) {
my $err = "OFPC Port $c->{$file}{'OFPC_PORT'} already in use by another node";
push( @errs, $err );
$bad = 1;
}
push( @ports, $c->{$file}{'OFPC_PORT'} );
}
if ($bad) {
foreach (@errs) {
print "[!] Error: $_ in file $file\n";
}
$c->{$file}{OFPC_ENABLED} = 0;
print " Disabling node $c->{$file}{'NODENAME'} in instance file $file.\n";
}
}
return ($c);
}
sub readconfig {
my $conf_dir = shift;
my %configs;
# Read in a hash of all configs on the system
opendir( my $dh, $conf_dir ) || die("Unable to open config dir $conf_dir\n");
while ( my $file = readdir $dh ) {
my $goodfile = 0;
if ( $file =~ /.*\.conf$/ ) {
# Check if this is an OpenFPC config file, or some other junk.
open FILE, '<', "$conf_dir/$file" or die "Unable to open config file $conf_dir/$file $!";
while (<FILE>) {
chomp $_;
$goodfile = 1 if ( $_ =~ /^OFPC_ENABLED=/ );
}
close(FILE);
# Open file to read config if it is a valid OpenFPC config file
if ($goodfile) {
# Apply defaults to this instance file in case it's missing some of the required configuration lines.
foreach ( keys %defaults ) {
$configs{$file}{$_} = $defaults{$_};
}
open FILE, '<', "$conf_dir/$file" or die "Unable to open config file $conf_dir/$file $!";
while ( my $line = <FILE> ) {
chomp $line;
if ( $line =~ m/^[a-zA-Z]/ ) {
( my $key, my @value ) = split /=/, $line;
unless ( $key eq "USER" ) {
$configs{$file}{$key} = join '=', @value;
}
}
}
close(FILE);
# Perform some sanity checks on the instance config, if something strange is found, disable it and warn the user.
push( @nodes, $configs{$file}{NODENAME} ) if ( $configs{$file}{OFPC_ENABLED} =~ /[Yy]/ );
}
}
else {
print "DEBUG: Not reading possible config file $file becasue it doesn't end in .conf\n" if $debug;
}
}
# Check config is valid
my $c = checkconfig( \%configs );
return ($c);
closedir($dh);
}
=head2 getType
Unless a user has specified if we are taking action on a daemon, or a configuration, this function will try to autodetect.
The only real risk of failing to detect is if someone stupidly names a configuration the same name as a daemon
e.g. NODENAME="openfpc-daemonlogger"
Expects string
Returns a hash ...
( type => "config",
instance => "instance" ,
filename => "/etc/openfpc/config.filename",
)
=cut
sub getType {
my $thing = shift;
my %result = (
type => 0,
instance => 0,
filename => 0,
);
my @daemons = ( "openfpc-queued", "openfpc-daemonlogger", "openfpc-cxtracker", "openfpc-cx2db" );
# Check if thing is one of our known daemons
foreach (@daemonsNode) {
if ( $_ eq $thing ) {
$result{'type'} = "daemon";
$result{'filename'} = "/etc/init.d/" . $thing;
return ( \%result );
}
}
# Check if thing is one of our config files
if ( exists $configs{$thing} ) {
$result{'type'} = "instance";
$result{'instance'} = "$thing";
$result{'filename'} = $thing;
}
# Check if thing is a node-name in any of the config files.
foreach ( keys(%configs) ) {
if ( defined $configs{$_} ) {
my $filename = $_;
if ( $thing eq $configs{$_}{'NODENAME'} ) {
$result{'type'} = "instance";
$result{'filename'} = "$filename";
return ( \%result );
}
}
}
# We don't know what it is.
return ( \%result );
}
=head2 getInstanceByDaemon
Get a list of instances that this daemon needs to be started for.
returns an array of instances (config filenames)
=cut
sub getInstanceByDaemon {
my $daemon = shift;
my @instances = ();
foreach my $conf ( keys(%configs) ) {
if ( defined $configs{$conf}{'OFPC_ENABLED'} ) {
if ( $configs{$conf}{'OFPC_ENABLED'} =~ /(y|yes|1)/i ) {
if ( $configs{$conf}{'PROXY'} == 1 ) {
# Enabled Proxy
if ( grep $_ eq $daemon, @daemonsProxy ) {
push( @instances, $conf );
}
}
elsif ( $configs{$conf}{'PROXY'} == 0 ) {
if ( $configs{$conf}{'ENABLE_SESSION'} == 1 ) {
if ( grep $_ eq $daemon, @daemonsNode ) {
push( @instances, $conf );
}
}
else {
if ( grep $_ eq $daemon, @daemonsNodeNoSession ) {
push( @instances, $conf );
}
}
}
}
}
}
return (@instances);
}
=head2 getDaemonsByInstance
Return a list of daemons we need to start for a config files
=cut
sub getDaemonsByInstance {
my $instance = shift;
my @daemons = ();
if ( $configs{$instance}{'OFPC_ENABLED'} =~ /(y|yes|1)/i ) {
if ( $configs{$instance}{'PROXY'} == 1 ) {
return (@daemonsProxy);
}
elsif ( $configs{$instance}{'PROXY'} == 0 ) {
if ( $configs{$instance}{'ENABLE_SESSION'} == 1 ) {
return (@daemonsNode);
}
else {
return (@daemonsNodeNoSession);
}
}
else {
die("Unknown Proxy config for instance: $instance\n");
}
}
else {
return (@daemons);
}
}
=head2 getPidFromFile
Open a pidfile, and return the pid
=cut
sub getPidFromFile($) {
my $filename = shift;
my $pid = 0;
if ( -f $filename ) {
open PIDFILE, '<', "$filename" or return (0);
while (<PIDFILE>) {
$pid = $_;
chomp $pid;
}
}
else {
print "getPidFromFile: Pidfile \"$filename\" not found \n" if $debug;
}
print "getPidFromFile PID is $pid\n" if $debug;
return ($pid);
}
sub isPidRunning($) {
my $pid = shift;
if ( system("ps $pid > /dev/null") ) {
return (0);
}
else {
return (1);
}
}
=head2 findcmd
Seach for installed file.
We need this because /usr/bin/ is really for system provided bins.
/usr/local/ makes more sense when installing from src.
I also want to allow user specification in a .conf
-Leon
Expects a daemon and an instance.
Returns the path to the daemon
e.g. findcmd("openfpc-daemonlogger","openfpc-example.conf");
=cut
sub findcmd {
my $daemon = shift;
my $instance = shift;
my @daemoncmds = ();
switch ($daemon) {
case "openfpc-queued" {
@daemoncmds = ( "$defaults{'OPENFPC_QUEUED_CMD'}", "/usr/local/bin/openfpc-queued", "/usr/bin/openfpc-queued", "./openpfc-queued" );
unshift( @daemoncmds, $configs{$instance}{'OPENFPC_QUEUED_CMD'} ) if defined $configs{$instance}{'OPENFPC_QUEUED_CMD'};
}
case "openfpc-daemonlogger" {
@daemoncmds = ( "$defaults{'DAEMONLOGGER_CMD'}", "/usr/local/bin/daemonlogger", "/usr/bin/daemonlogger", "./daemonlogger" );
unshift( @daemoncmds, $configs{$instance}{'DAEMONLOGGER_CMD'} ) if defined $configs{$instance}{'DAEMONLOGGER_CMD'};
}
case "openfpc-cxtracker" {
@daemoncmds = ( "$defaults{'CXTRACKER_CMD'}", "/usr/local/bin/cxtracker", "/usr/bin/cxtracker", "./cxtracker" );
unshift( @daemoncmds, $configs{$instance}{'CXTRACKER_CMD'} ) if defined $configs{$instance}{'CXTRACKER_CMD'};
}
case "openfpc-cx2db" {
@daemoncmds = ( "$defaults{'CX2DB_CMD'}", "/usr/local/bin/openfpc-cx2db", "/usr/bin/openfpc-cx2db", "./openfpc-cx2db" );
unshift( @daemoncmds, $configs{$instance}{'CX2DB_CMD'} ) if defined $configs{$instance}{'CX2DB_CMD'};
}
else {
print "Unknown daemon $_\n";
return (0);
}
}
foreach (@daemoncmds) {
if ( -x $_ ) {
print " - $daemon is $_ \n" if $verbose;
return ($_);
}
}
print " V Unable to find $daemon for $instance\n" if $verbose;
foreach (@daemoncmds) {
print " - Tried : $_ \n" if $verbose;
}
return (0);
}
=head2 initpidpath
1) Make sure the pidpath exists
2) Make sure the pidpath is writable by DROP_USER
If all is good, Returns the name of the pidpath, if not, 0.
=cut
sub initpidfile {
my $i = shift;
my $pidpath = "/var/run";
my %p = (
pidpath => 0,
queued => 0,
cxtracker => 0,
cx2db => 0,
daemonlogger => 0,
error => 0,
user => 0,
group => 0,
);
$pidpath = $configs{$i}{'PIDPATH'} if defined $configs{$i}{'PIDPATH'};
my $drop_user = $configs{$i}{'DROP_USER'} if defined $configs{$i}{'DROP_USER'};
die("No node name defined in config") unless defined( $configs{$i}{'NODENAME'} );
$pidpath = $pidpath . "/openfpc-" . $configs{$i}{'NODENAME'}; # append nodename to pidpath so DROP_USER can access it.
unless ( -d $pidpath ) {
unless (
make_path $pidpath,
{
owner => $drop_user,
group => $drop_user
}
)
{
die("Failed to make path $pidpath");
}
}
$p{'pidpath'} = $pidpath;
$p{'queued'} = $pidpath . "/" . "openfpc-queued.pid";
$p{'cx2db'} = $pidpath . "/" . "openfpc-cx2db.pid";
$p{'cxtracker'} = "openfpc-cxtracker.pid";
$p{'daemonlogger'} = "openfpc-daemonlogger.pid"; # Doesn't want full path
$p{'user'} = $drop_user;
$p{'group'} = $drop_user;
return ( \%p );
}
=head2 openfpcqueued
Start/Stop the OpenFPC Queue Daemon
- Leon 2010
=cut
sub openfpcqueued {
my $action = shift;
my $instance = shift;
my $pidpath = $defaults{'PIDPATH'};
my $daemoncmd = findcmd( "openfpc-queued", $instance );
my $daemonargs = "-c $conf_dir/$instance --daemon";
my $p = initpidfile($instance);
my $pidfile = $p->{'queued'};
# Get a PID, and check if it's running
my $pid = getPidFromFile($pidfile);
my $running = isPidRunning($pid) if ($pid);
switch ($action) {
case "debug" {
print "[*] Putting $configs{$instance}{'NODENAME'} into DEBUG mode\n";
openfpcqueued( 'stop', $instance );
print " - Waiting for daemon to stop..";
my $count = 1;
while ( $count < 20 ) {
$running = isPidRunning($pid) if ($pid);
unless ($running) {
print "Putting openfpc-queued into debug mode:\n";
exec("export OFPCDEBUG=1 && $daemoncmd -c $conf_dir/$instance");
}
else {
print ".";
sleep 1;
}
$count++;
}
print "Error: Daemon $configs{$instance}{'NODENAME'} still running\n";
}
case "start" {
printf '%-70s', "Starting OpenFPC Queue Daemon ($configs{$instance}{'NODENAME'})... ";
if ($running) {
printf '%10s', "Running\n";
}
else {
if ( -x $daemoncmd ) {
my $result = system("$daemoncmd $daemonargs");
if ($result) {
printf '%10s', "Failed\n";
print " - Check syslog for details\n" unless $quiet;
}
else {
printf '%10s', "Done\n";
return (1);
}
}
else {
printf '%10s', "Failed\n";
print " - Cant find openfpc-queued. Check your conifg and OpenFPC install $daemoncmd!\n";
}
}
}
case "stop" {
printf '%-70s', "Stopping OpenFPC Queue Daemon ($configs{$instance}{'NODENAME'})... ";
if ($running) {
my $result = system("kill $pid");
if ($result) {
printf '%10s', "Failed\n";
}
else {
printf '%10s', "Done\n";
return (1);
}
}
else {
printf '%10s', "Not running\n";
}
}
case "restart" {
openfpcqueued( "stop", $instance );
openfpcqueued( "start", $instance );
}
case "status" {
printf '%-70s', "OpenFPC Queue Daemon ($configs{$instance}{'NODENAME'}):";
if ($running) {
printf '%10s', "Running\n";
}
else {
printf '%10s', "Stopped\n";
}
}
}
return (0);
}
=head2 dameonlogger
Start/Stop the OpenFPC Queue Daemon
- Leon 2010
=cut
sub openfpcdaemonlogger {
my $action = shift;
my $instance = shift;
my $daemonargs = "-d ";
my $daemoncmd = findcmd( "openfpc-daemonlogger", $instance );
my $p = initpidfile($instance);
my $pidfile = $p->{'daemonlogger'};
# Get a PID, and check if it's running
my $pid = getPidFromFile("$p->{'pidpath'}/$pidfile");
my $running = isPidRunning($pid) if ($pid);
# Check we have a BPF file before rtying to use it
if ( $configs{$instance}{'BPF_FILE'} ) {
print "Using BPF\n" if $verbose;
if ( -f $configs{$instance}{'BPF_FILE'} ) {
$daemonargs .= " -f $configs{$instance}{'BPF_FILE'} ";
print "DEBUG: Found BPF file $configs{$instance}{'BPF_FILE'}" if $verbose;
}
else {
print "WARNING: BPF file specified but does not exist! Starting without a BPF\n";
}
}
$daemonargs .= "-i $configs{$instance}{'INTERFACE'} " . "-l $configs{$instance}{'BUFFER_PATH'} " . "-M $configs{$instance}{'PCAP_SPACE'} " . "-s $configs{$instance}{'FILE_SIZE'} " . "-p $pidfile " . "-P $p->{'pidpath'} " . "-n openfpc-$configs{$instance}{'NODENAME'}.pcap " . "-u $configs{$instance}{'DROP_USER'} " . "-g $configs{$instance}{'DROP_USER'} " . " 2>&1 " . "| logger -t OFPC-DL-$configs{$instance}{'NODENAME'}";
#" >> /tmp/openfpc-dl-out 2>&1 ";
# Daemonlogger is verbose at startup, and we need to store the output to find
# any problems if found. Throwing it to syslog.
switch ($action) {
case "start" {
print "DEBUG: Command used to start daemonlogger is\n " . "$daemoncmd $daemonargs\n" if $verbose;
printf '%-70s', "Starting Daemonlogger ($configs{$instance}{'NODENAME'})...";
if ($running) {
printf '%10s', "Running\n";
}
else {
# Check if interface is available
if ( system("ifconfig $configs{$instance}{'INTERFACE'} up > /dev/null 2>&1") ) {
print " - WARNING unable to bring $configs{$instance}{'INTERFACE'} up\n" if $verbose;
}
# Check if BUFFER_PATH exists and is writable
if ( !-d $configs{$instance}{'BUFFER_PATH'} ) {
unless (
make_path $configs{$instance}{'BUFFER_PATH'},
{
owner => $configs{$instance}{'DROP_USER'},
group => $configs{$instance}{'DROP_USER'}
}
)
{
printf '%10s', "Failed\n";
print " - Unable to create BUFFER_PATH $configs{$instance}{'BUFFER_PATH'}\n" if $verbose;
return (0);
}
}
else {
# Check path is writable as user
unless (
make_path "$configs{$instance}{'BUFFER_PATH'}/test",
{
owner => $configs{$instance}{'DROP_USER'},
group => $configs{$instance}{'DROP_USER'}
}
)
{
print "Error: BUFFER_PATH $configs{$instance}{'BUFFER_PATH'} is not writable by user $configs{$instance}{'DROP_USER'}";
}
else {
remove_tree "$configs{$instance}{'BUFFER_PATH'}/test";
}
}
# Check if SAVE_PATH exists and is writable
if ( !-d $configs{$instance}{'SAVEDIR'} ) {
unless (
make_path $configs{$instance}{'SAVEDIR'},
{
owner => $configs{$instance}{'DROP_USER'},
group => $configs{$instance}{'DROP_USER'}
}
)
{
printf '%10s', "Failed\n";
print " - Unable to create SAVEDIR $configs{$instance}{'SAVEDIR'}\n" if $verbose;
return (0);
}
}
else {
# Check path is writable as user
unless (
make_path "$configs{$instance}{'SAVEDIR'}/test",
{
owner => $configs{$instance}{'DROP_USER'},
group => $configs{$instance}{'DROP_USER'}
}
)
{
print "Error: SAVEDIR $configs{$instance}{'SAVEDIR'} is not writable by user $configs{$instance}{'DROP_USER'}";
}
else {
remove_tree "$configs{$instance}{'SAVEDIR'}/test";
}
}
my $cf = "$configs{$instance}{'BUFFER_PATH'}/openfpc-$configs{$instance}{'NODENAME'}.pcap.0";
print "\nDEBUG: Touching a canary file to check if DL needs to delete something at startup:$cf\n" if $verbose;
`touch $cf`;
if ( -x $daemoncmd ) {
my $result = system("$daemoncmd $daemonargs");
if ($result) {
printf '%10s', "Failed\n";
}
else {
printf '%10s', "Done\n";
if ( -e $cf ) {
print "DEBUG: canary file still here (good)" if $verbose;
unlink $cf;
}
else {
print "WARNING: Buffer path is already over capacity, and a new instance is starting to log!\nBad things will happen unless you clean up older large buffer files.\n";
}
return (1);
}
}
else {
printf '%10s', "Failed\n";
printf " - Dameonlogger not found on this system.\n" if $verbose;
return (0);
}
}
}
case "stop" {
printf '%-70s', "Stopping Daemonlogger... ";
if ($running) {
my $result = system("kill $pid");
if ($result) {
printf '%10s', "Failed\n";
}
else {
printf '%10s', "Done\n";
return (1);
}
}
else {
printf '%10s', "Not running\n";
}
}
case "status" {
printf '%-70s', "Daemonlogger ($configs{$instance}{'NODENAME'}) :";
if ($running) {
printf '%10s', "Running\n";
}
else {
printf '%10s', "Stopped\n";
}
}
case "restart" {
openfpcdaemonlogger( "stop", $instance );
sleep(1);
openfpcdaemonlogger( "start", $instance );
}
}
return (0);
}
sub openfpccxtracker {
my $action = shift;
my $instance = shift;
my $pidpath = $defaults{'PIDPATH'};
my $daemoncmd = findcmd( "openfpc-cxtracker", $instance );
my $sessiondir = $defaults{'SESSION_DIR'};
die("DROP_USER Not defined") unless ( defined $configs{$instance}{'DROP_USER'} );
$sessiondir = $configs{$instance}{'SESSION_DIR'} if defined $configs{$instance}{'SESSION_DIR'};
my $p = initpidfile($instance);
my $pidfile = $p->{'cxtracker'};
# Get a PID, and check if it's running
my $pid = getPidFromFile("$p->{'pidpath'}/$pidfile");
my $running = isPidRunning($pid) if ($pid);
my $daemonargs = "-i $configs{$instance}{'INTERFACE'} " . "-d $sessiondir " . "-p $pidfile " . "-P $p->{'pidpath'} " . "-u $configs{$instance}{'DROP_USER'} " . "-g $configs{$instance}{'DROP_USER'} " . "-D > /tmp/cxtracker.start.log 2>&1";
switch ($action) {
case "start" {
printf '%-70s', "Starting OpenFPC cxtracker ($configs{$instance}{'NODENAME'})... ";
if ($running) {
printf '%10s', "Running\n";
}
else {
# Check if interface is available
if ( system("ifconfig $configs{$instance}{'INTERFACE'} up > /dev/null 2>&1") ) {
print " - WARNING unable to bring $configs{$instance}{'INTERFACE'} up\n" if $verbose;
}
# Check if SESSION_PATH exists and is writable by DROP_USER
if ( !-d $configs{$instance}{'SESSION_DIR'} ) {
my $fp = $configs{$instance}{'SESSION_DIR'} . "/failed";
unless (
make_path $fp,
{
owner => $configs{$instance}{'DROP_USER'},
group => $configs{$instance}{'DROP_USER'}
}
)
{
printf '%10s', "Failed\n";
print " - Unable to create $configs{$instance}{'SESSION_DIR'}\n" if $verbose;
return (1);
}
}
if ( -x $daemoncmd ) {
print "DEBUG: Daemon start command is\n $daemoncmd $daemonargs\n" if $verbose;
my $result = system("$daemoncmd $daemonargs");
if ($result) {
printf '%10s', "Failed\n";
}
else {
printf '%10s', "Done\n";
return (1);
}
}
else {
printf '%10s', "Failed\n";
print "[!] cxtracker not found on this system. Can't start it\n";
}
}
}
case "stop" {
printf '%-70s', "Stopping OpenFPC cxtracker ($configs{$instance}{'NODENAME'})... ";
if ($running) {
my $result = system("kill $pid");
if ($result) {
printf '%10s', "Failed\n";
}
else {
printf '%10s', "Done\n";
return (1);
}
}
else {
printf '%10s', "Not running\n";
}
}
case "status" {
printf '%-70s', "OpenFPC Connection Tracker ($configs{$instance}{'NODENAME'}) :";
if ($running) {
printf '%10s', "Running\n";
}
else {
printf '%10s', "Stopped\n";
}
}
case "restart" {
openfpccxtracker( "stop", $instance );
openfpccxtracker( "start", $instance );
}
}
return (0);
}
=head2 openfpccx2db
Start/Stop cx2db (uploads sessions from disk to DB)
=cut
sub openfpccx2db {
my $action = shift;
my $instance = shift;
my $pidpath = $defaults{'PIDPATH'};
my $daemoncmd = findcmd( "openfpc-cx2db", $instance );
my $p = initpidfile($instance);
my $pidfile = $p->{'cx2db'};
# Get a PID, and check if it's running
my $pid = getPidFromFile("$pidfile");
my $running = isPidRunning($pid) if ($pid);
my $daemonargs = "--daemon --config $conf_dir/$instance ";
switch ($action) {
case "start" {
printf '%-70s', "Starting OpenFPC Connection Uploader ($configs{$instance}{'NODENAME'}) ... ";
if ($running) {
printf '%10s', "Running\n";
}
else {
# Check if BUFFER_PATH exists and is writable
if ( !-d $configs{$instance}{'SESSION_DIR'} ) {
printf '%10s', "Failed. \n";
print " - Log path $configs{$instance}{'SESSION_DIR'} doesn't exist\n" if $verbose;
return (0);
}
print "daemoncmd is $daemoncmd $daemonargs\n" if $debug;
if ( -x $daemoncmd ) {
my $result = system("$daemoncmd $daemonargs");
if ($result) {
printf '%10s', "Failed\n";
print "[!] ERROR: Problem starting cx2db process. Check syslog for details\n" if $verbose;
}
else {
printf '%10s', "Done\n";
return (1);
}
}
else {
printf '%10s', "Failed\n";
print " - openfpc-cx2db command not found on this system \n" if $verbose;
}
}
}
case "stop" {
printf '%-70s', "Stopping OpenFPC Connection Uploader ($configs{$instance}{'NODENAME'})... ";
if ($running) {
my $result = system("kill $pid");
if ($result) {
printf '%10s', "Failed\n";
}
else {
printf '%10s', "Done\n";
return (1);
}
}
else {
printf '%10s', "Not running\n";
}
}
case "status" {
printf '%-70s', "OpenFPC Connection Uploader ($configs{$instance}{'NODENAME'}) :";
if ($running) {
printf '%10s', "Running\n";
}
else {
printf '%10s', "Stopped\n";
}
}
case "restart" {
openfpccx2db( "stop", $instance );
openfpccx2db( "start", $instance );
}
}
return (0);
}
sub showhelp {
print "
openfpc : An OpenFPC control tool.
This tool start/stops all OpenFPC components on a host. It does all the
hard work so you don't have to. Multiple instances of OpenFPC can exist
on a host, this tool caters for that need allowing a user to start/stop
either an instance, or all copies of a specific daemon.
e.g
To start all daemons required to operate an OpenFPC node with the name
\"Default_Node\"
openfpc --action start -t Default_Node
or
openfpc --action start -t /etc/openfpc/openfpc-default.conf
To start all the instances of the openfpc-queued daemon
openfpc --action start -t openfpc-queued
To get the status of all daemons that are configured to run on this host
openfpc --action status
###############################################################################
openfpc <arguements>
--action or -a start/stop/status
--quiet or -q Quiet output for init scripts
--thing or -t Take action on this \"thing\"
Note: --thing can be one of...
- A Daemon name e.g.
openfpc-daemonlogger
openfpc-queued
openfpc-cx2db
openfpc-cxtracker
- Config file e.g.
openfpc-default.conf
- Node name (defined in any config file in conf_dir) e.g.
MyOpenFPCNode
CORP.UK.DMZ.WWW
Part of the OpenFPC project http://www.openfpc.org
";
exit 0;
}
GetOptions(
'a|action=s' => \$action, # Action to take
'v|verbose' => \$verbose, # Verbose
't|thing=s' => \$thing, # The thing we want to take action on
'q|quiet' => \$quiet,
'help' => \$help,
'debug' => \$debug,
);
&showhelp if ($help);
unless ( $action =~ /^(status|start|stop|debug|restart)$/ ) {
print "Invalid --action specified. See --help for usage\n";
exit(1);
}
# Check if we are root
unless ( $> == 0 || $< == 0 ) { die "You need root privileges to run this tool.\n" }
$verbose = 1 if ($debug);
# Make sure we have the minimum input to do something of use.
# XXXX
my $c = readconfig($conf_dir);
%configs = %$c;
#my $g = checkconfig(\%configs);
#if ($g->{'success'}) {
# print "Config looks good\n";
#} else {
# print "Problem with config $g->{'error'}\n";
#}
# Check we don't have any dupe nodes active.
while ( my $node = shift(@nodes) ) {
if ( grep { $_ eq $node } @nodes ) {
die("ERROR: Duplicate node found enabled - $node. A Node name MUST be unique!\n");
}
}
# Check if we need to act in the context of a daemon type, or an instance.
# Get the type of the thing the user wants to start/stop
if ($thing) {
my $ofpc = getType($thing);
#print "Filename is $ofpc->{'filename'} \nType is $ofpc->{'type'} \n" unless $quiet;
if ( $ofpc->{'type'} eq "daemon" ) {
my @instances = getInstanceByDaemon($thing);
#foreach (@instances) {
# print "- $_\n";
#}
my $num = @instances;
unless ($num) {
print "WARNING: daemon $thing is not enabled in any instance\n" if $verbose;
}
switch ($thing) {
case "openfpc-queued" {
foreach (@instances) {
openfpcqueued( $action, $_ );
}
}
case "openfpc-daemonlogger" {
foreach (@instances) {
openfpcdaemonlogger( $action, $_ );
}