-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathopenfpc-queued
executable file
·232 lines (197 loc) · 7.09 KB
/
openfpc-queued
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
#!/usr/bin/perl -I.
#########################################################################################
# Copyright (C) 2010 - 2014 Leon Ward
# openfpc-queued.pl - Part of the OpenFPC - (Full Packet Capture) project
#
# Contact: leon@rm-rf.co.uk
#
# 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 threads;
use threads::shared;
use Thread::Queue;
use IO::Socket;
use Getopt::Long;
use POSIX qw(setsid); # Required for daemon mode
use Data::Dumper;
use File::Temp(qw(tempdir));
use DateTime;
use Privileges::Drop;
use OFPC::Config;
use OFPC::Common;
use OFPC::Parse;
use OFPC::Request;
use OFPC::CXDB;
=head1 NAME
openfpc-queued - Queue and process extract requests from local and remote PCAP storage devices.
Leon Ward - 2010
=head1 VERSION
0.5
=cut
my $help;
my %route : shared = ();
my $configfile = 0;
my $debug = OFPC::Common::wantdebug();
my $ltz = DateTime::TimeZone->new( name => 'local' )->name();
=head 2 showhelp
Show a simple help message.
=cut
sub showhelp {
print <<EOF
* openfpc-queued version $openfpcver *
Part of the OpenFPC project.
Leon Ward 2010
--daemon or -d Daemon mode
--config or -c <file> Config file
--help or -h Show help
--verbose or -v Verbose logging
EOF
}
=head2 pipeHandler
Deal with clients that disappear rather than have perl die.
=cut
sub pipeHandler {
my $sig = shift @_;
print "SIGPIPE -> Bad client went away! $sig \n\n" if ($verbose);
}
########### Start Here ############
$SIG{PIPE} = \&pipeHandler;
$SIG{"TERM"} = sub { closedown("TERM") };
$SIG{"KILL"} = sub { closedown("KILL") };
GetOptions(
'c|conf=s' => \$configfile,
'd|daemon' => \$daemon,
'h|help' => \$help,
'v|verbose' => \$verbose,
'vdebug' => \$vdebug,
);
if ($vdebug) {
$debug = 1;
wlog("VDEBUG Enabled\n");
}
if ($help) {
showhelp();
exit;
}
OFPC::Common::initlog or die("Problem initilizating log subsystem");
OFPC::Common::readconfig("$configfile") or die("Problem processing config file $configfile");
# If PASSWD is set, we have a SHA1 password file, lets read it and use secure passwords.
if ( $config{'PASSWD'} ) {
wlog("DEBUG: SHA1 passwords enabled - Reading $config{'PASSWD'}") if $debug;
OFPC::Common::readpasswd("$config{'PASSWD'}") or die("Problem processing users file $config{'PASSWD'}");
}
wlog("START: *********** OpenFPC $openfpcver **********");
wlog("START: ** http://www.openfpc.org **");
wlog("START: Dropping Privileges to user $config{'DROP_USER'}\n");
my $cuser = `whoami`;
wlog("DEBUG: START: Started by user $cuser") if $debug;
drop_privileges( $config{'DROP_USER'} );
$cuser = `whoami`;
wlog("START: Now running as user $cuser");
# Check if all save directories where we are expected to write files can be accessed as DROP_USER
my @wdirs = ( $config{'SAVEDIR'} );
foreach (@wdirs) {
my $t = "test.openfpc";
wlog "DEBUG: Checking write access to directory $_" if $debug;
if ( -d $_ ) {
if ( open( TESTFILE, "> $_/$t" ) ) {
print TESTFILE "OpenFPC Write Test.";
close TESTFILE;
wlog "DEBUG: Write access to $_ successful" if $debug;
if ( unlink "$_/$t" ) {
wlog "DEBUG: Unlink access successful for $_" if $debug;
}
else {
wlog "ERROR: Unable to unlink files from $_ and user $cuser. Unable to start.";
die "ERROR: Unable to unlink files from $_ and user $cuser. Unable to start.";
}
}
else {
wlog "ERROR: Unable write to directory $_ as user $cuser\n";
die "ERROR: Unable write to directory $_ as user $cuser\n";
}
}
else {
wlog "ERROR: Expected directory $_ does not exist. Unable to start";
die "ERROR:, Expected directory $_ does not exist. Unable to start";
}
}
if ( $config{'PROXY'} ) {
wlog("START: Starting OFPC Node \"$config{'NODENAME'}\" as a Proxy");
OFPC::Common::readroutes;
}
else {
wlog("START: Starting OFPC Node \"$config{'NODENAME'}\" as a Node");
}
# Print verbose config info if debug is enabled
if ($debug) {
wlog("DEBUG: Node Description: $config{'DESCRIPTION'}");
wlog("DEBUG: Enabled : $config{'OFPC_ENABLED'}");
wlog("DEBUG: local savedir : $config{'SAVEDIR'}");
wlog("DEBUG: Buffer Path : $config{'BUFFER_PATH'}");
wlog("DEBUG: mergecap : $config{'MERGECAP'}");
wlog("DEBUG: tcpdump : $config{'TCPDUMP'}");
wlog("DEBUG: Keep files : $config{'KEEPFILES'}");
wlog("DEBUG: Local Timezone for this node is $ltz");
wlog( "DEBUG: Local time at $ltz is " . localtime() );
}
# Start listener
wlog("START: Starting listener on TCP:$config{'OFPC_PORT'}\n");
my $listenSocket = IO::Socket::INET->new(
LocalPort => $config{'OFPC_PORT'},
Proto => 'tcp',
Listen => '10',
Reuse => 1,
);
unless ($listenSocket) {
wlog("ERROR: Problem creating socket on $config{'OFPC_PORT'}");
exit 1;
}
$listenSocket->autoflush(1);
if ($daemon) {
chdir '/' or die "Can't chdir to /: $!";
umask 0;
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open( STDOUT, "> $config{'LOGFILE'}" ) or die "Can't open Log for STDOUT $config{'LOGFILE'}: $!\n";
defined( my $pid = fork ) or die "Can't fork: $!";
if ($pid) {
my $pidfile = $config{'PIDPATH'} . "/openfpc-" . $config{'NODENAME'} . "/openfpc-queued.pid";
open( PID, "> $pidfile" ) or die("Unable write to pid file $pidfile\n");
print PID $pid, "\n";
close(PID);
exit 0;
}
# Redirect STDERR Last to catch any error in the fork() process.
open( STDERR, "> $config{'LOGFILE'}" ) or die "Can't open Log for STDERR $config{'LOGFILE'}: $!\n";
setsid or die "Can't start a new session: $!";
}
# Create a couple of separate threads.
threads->create("OFPC::Common::runq");
threads->create("OFPC::Common::backgroundtasks");
while ( my $sock = $listenSocket->accept ) {
# set client socket to non blocking
my $nonblocking = 1;
ioctl( $sock, 0x8004667e, \\$nonblocking );
$sock->autoflush(1);
my $client_ip = $sock->peerhost;
wlog("COMMS: Accepted new connection from $client_ip");
# start new thread to process the request from this socket
# Suggested fix from Michael Wenthold
# threads->create("OFPC::Common::comms", $sock);
my $t = threads->create( "OFPC::Common::comms", $sock );
$t->detach();
}