-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbpolicyd
executable file
·783 lines (633 loc) · 21.2 KB
/
cbpolicyd
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
#!/usr/bin/perl
# Cluebringer policy daemon
# Copyright (C) 2009-2015, AllWorldIT
# Copyright (C) 2008, LinuxRulz
# Copyright (C) 2007, Nigel Kukard <[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 lib('/usr/local/lib/cbpolicyd-2.1','/usr/lib/cbpolicyd-2.1',
'/usr/lib64/cbpolicyd-2.1','awitpt');
package cbp;
use base qw(Net::Server::PreFork);
use Config::IniFiles;
use Getopt::Long;
use Sys::Syslog;
use cbp::version;
use cbp::config;
use cbp::logging;
use awitpt::db::dbilayer;
use awitpt::cache;
use cbp::tracking;
use cbp::protocols;
# Override configuration
sub configure {
my $self = shift;
my $server = $self->{'server'};
my $cfg;
my $cmdline;
my $inifile;
# We're being called from somewhere else, maybe a protocol?
return if (@_);
# Set defaults
$cfg->{'config_file'} = "/etc/cbpolicyd/cluebringer.conf";
$cfg->{'cache_file'} = '/var/run/cbpolicyd/cache';
$cfg->{'track_sessions'} = 0;
$server->{'timeout_idle'} = 1015;
$server->{'timeout_busy'} = 115;
$server->{'background'} = "yes";
$server->{'pid_file'} = "/var/run/cbpolicyd/cbpolicyd.pid";
$server->{'log_level'} = 2;
$server->{'log_file'} = "/var/log/cbpolicyd/cbpolicyd.log";
$server->{'proto'} = "tcp";
$server->{'host'} = "*";
$server->{'port'} = 10031;
$server->{'min_servers'} = 4;
$server->{'min_spare_servers'} = 4;
$server->{'max_spare_servers'} = 12;
$server->{'max_servers'} = 25;
$server->{'max_requests'} = 1000;
# Parse command line params
%{$cmdline} = ();
GetOptions(
\%{$cmdline},
"help",
"config:s",
"debug",
"fg",
);
# Check for some args
if ($cmdline->{'help'}) {
$self->displayHelp();
exit 0;
}
if (defined($cmdline->{'config'}) && $cmdline->{'config'} ne "") {
$cfg->{'config_file'} = $cmdline->{'config'};
}
# Check config file exists
if (! -f $cfg->{'config_file'}) {
print(STDERR "ERROR: No configuration file '".$cfg->{'config_file'}."' found!\n");
exit 1;
}
# Use config file, ignore case
tie my %inifile, 'Config::IniFiles', (
-file => $cfg->{'config_file'},
-nocase => 1
) or die "Failed to open config file '".$cfg->{'config_file'}."': ".join("\n",@Config::IniFiles::errors);
# Copy config
my %config = %inifile;
# FIXME: This now generates a warning as Config::Inifiles doesn't implement UNTIE
# untie(%inifile);
# Pull in params for the server
my @server_params = (
'log_level','log_file',
'proto', 'host', 'port',
'cidr_allow', 'cidr_deny',
'pid_file',
'user', 'group',
'timeout_idle', 'timeout_busy',
'background',
'min_servers',
'min_spare_servers',
'max_spare_servers',
'max_servers',
'max_requests',
);
foreach my $param (@server_params) {
$server->{$param} = $config{'server'}{$param} if (defined($config{'server'}{$param}));
}
# Fix up these ...
if (defined($server->{'cidr_allow'})) {
my @lst = split(/[,\s;]+/,$server->{'cidr_allow'});
$server->{'cidr_allow'} = \@lst;
}
if (defined($server->{'cidr_deny'})) {
my @lst = split(/[,\s;]+/,$server->{'cidr_deny'});
$server->{'cidr_deny'} = \@lst;
}
# Split off modules
if (!defined($config{'server'}{'modules'})) {
die "Server configuration error: 'modules' not found";
}
if (!defined($config{'server'}{'protocols'})) {
die "Server configuration error: 'protocols' not found";
}
# Split off modules
if (ref($config{'server'}{'modules'}) eq "ARRAY") {
foreach my $module (@{$config{'server'}{'modules'}}) {
$module =~ s/\s+//g;
# Skip comments
next if ($module =~ /^#/);
$module = "modules/$module";
push(@{$cfg->{'module_list'}},$module);
}
} else {
my @moduleList = split(/\s+/,$config{'server'}{'modules'});
foreach my $module (@moduleList) {
# Skip comments
next if ($module =~ /^#/);
$module = "modules/$module";
push(@{$cfg->{'module_list'}},$module);
}
}
# Split off protocols
if (ref($config{'server'}{'protocols'}) eq "ARRAY") {
foreach my $module (@{$config{'server'}{'protocols'}}) {
$module =~ s/\s+//g;
# Skip comments
next if ($module =~ /^#/);
$module = "protocols/$module";
push(@{$cfg->{'module_list'}},$module);
}
} else {
my @protocolList = split(/\s+/,$config{'server'}{'protocols'});
foreach my $module (@protocolList) {
# Skip comments
next if ($module =~ /^#/);
$module = "protocols/$module";
push(@{$cfg->{'module_list'}},$module);
}
}
# Override
if ($cmdline->{'debug'}) {
$server->{'log_level'} = 4;
$cfg->{'debug'} = 1;
}
# If we set on commandline for foreground, keep in foreground
if ($cmdline->{'fg'} || (defined($config{'server'}{'background'}) && $config{'server'}{'background'} eq "no" )) {
$server->{'background'} = undef;
$server->{'log_file'} = undef;
} else {
$server->{'setsid'} = 1;
}
# Loop with logging detail
if (defined($config{'server'}{'log_detail'})) {
# Lets see what we have to enable
foreach my $detail (split(/[,\s;]/,$config{'server'}{'log_detail'})) {
$cfg->{'logging'}{$detail} = 1;
}
}
# Check log_mail
if (defined($config{'server'}{'log_mail'}) && $config{'server'}{'log_mail'} ne "main") {
# COMPATIBILITY
if ($config{'server'}{'log_mail'} eq "maillog") {
$cfg->{'log_mail'} = 'mail@syslog:native';
} else {
$cfg->{'log_mail'} = $config{'server'}{'log_mail'};
}
}
# Check if the user specified a cache_file in the config
if (defined($config{'server'}{'cache_file'})) {
$cfg->{'cache_file'} = $config{'server'}{'cache_file'};
}
# Save our config and stuff
$self->{'config'} = $cfg;
$self->{'cmdline'} = $cmdline;
$self->{'inifile'} = \%config;
}
# Run straight after ->run
sub post_configure_hook {
my $self = shift;
my $log_mail = $self->{'config'}{'log_mail'};
$self->log(LOG_NOTICE,"[CBPOLICYD] PolicyD v2 / Cluebringer - v".VERSION);
$self->log(LOG_NOTICE,"[CBPOLICYD] Initializing system modules.");
# Init config
cbp::config::Init($self);
# Init caching engine
awitpt::cache::Init($self,{
'cache_file' => $self->{'config'}{'cache_file'},
'cache_file_user' => $self->{'server'}->{'user'},
'cache_file_group' => $self->{'server'}->{'group'}
});
$self->log(LOG_NOTICE,"[CBPOLICYD] System modules initialized.");
$self->log(LOG_NOTICE,"[CBPOLICYD] Module load started...");
# Load modules
foreach my $module (@{$self->{'config'}{'module_list'}}) {
# Split off dir and mod name
$module =~ /^(\w+)\/(\w+)$/;
my ($mod_dir,$mod_name) = ($1,$2);
# Load module
my $res = eval("
use cbp::${mod_dir}::${mod_name};
plugin_register(\$self,\"${mod_name}\",\$cbp::${mod_dir}::${mod_name}::pluginInfo);
");
if ($@ || (defined($res) && $res != 0)) {
$self->log(LOG_WARN,"[CBPOLICYD] Error loading plugin $module ($@)");
}
}
$self->log(LOG_NOTICE,"[CBPOLICYD] Module load done.");
# Report if session tracking is on
if ($self->{'config'}{'track_sessions'}) {
$self->log(LOG_NOTICE,"[CBPOLICYD] Session tracking is ENABLED.");
} else {
$self->log(LOG_NOTICE,"[CBPOLICYD] Session tracking is DISABLED.");
}
# Check if we have some custom logging...
if (defined($log_mail)) {
# More flexible method to configure logging
if ($log_mail =~ /^([^@]+)(?:@([^:]+))?(?:\:(\S+))?$/) {
my $facility = $1;
my $method = defined($2) ? $2 : 'syslog';
my $destination = $3;
# Check method of logging
if ($method eq "syslog") {
$destination = 'native' if (!defined($destination));
$facility = 'mail' if (!defined($facility));
$self->log(LOG_DEBUG,"[CBPOLICYD] Opening syslog, destination = '$destination', facility = '$facility'.");
# We may have some args to pass to setlogsock
my @syslogArgs = split(/,/,$destination);
if (!Sys::Syslog::setlogsock(@syslogArgs)) {
$self->log(LOG_ERR,"[CBPOLICYD] Failed to set log socket: $!");
}
if (!Sys::Syslog::openlog("cbpolicyd",'pid|ndelay',$facility)) {
$self->log(LOG_ERR,"[CBPOLICYD] Failed to open syslog socket: $!");
}
} else {
$self->log(LOG_WARN,"[CBPOLICYD] Value of 'log_mail' not understood. Method '$method' is invalid.");
}
} else {
$self->log(LOG_WARN,"[CBPOLICYD] Value '$log_mail' of 'log_mail' not understood.");
}
}
}
# Register plugin info
sub plugin_register {
my ($self,$module,$info) = @_;
# If no info, return
if (!defined($info)) {
$self->log(LOG_WARN,"[CBPOLICYD] Plugin info not found for module => $module");
return -1;
}
# Set real module name & save
$info->{'Module'} = $module;
push(@{$self->{'modules'}},$info);
# If we should, init the module
if (defined($info->{'init'})) {
$info->{'init'}($self);
}
return 0;
}
# Initialize child
sub child_init_hook
{
my $self = shift;
$self->SUPER::child_init_hook();
$self->log(LOG_DEBUG,"[CBPOLICYD] Starting up caching engine");
awitpt::cache::connect($self);
# This is the database connection timestamp, if we connect, it resets to 0
# if not its used to check if we must kill the child and try a reconnect
$self->{'client'}->{'dbh_status'} = time();
# Init system stuff
$self->{'client'}->{'dbh'} = awitpt::db::dbilayer::Init($self,'cbp');
if (defined($self->{'client'}->{'dbh'})) {
# Check if we succeeded
if (!($self->{'client'}->{'dbh'}->connect())) {
# If we succeeded, record OK
$self->{'client'}->{'dbh_status'} = 0;
} else {
$self->log(LOG_WARN,"[CBPOLICYD] Failed to connect to database: ".$self->{'client'}->{'dbh'}->Error()." ($$)");
}
} else {
$self->log(LOG_WARN,"[CBPOLICYD] Failed to Initialize: ".awitpt::db::dbilayer::internalError()." ($$)");
}
}
# Destroy the child
sub child_finish_hook {
my $self = shift;
my $server = $self->{'server'};
my $log_cache = defined($self->{'config'}{'logging'}{'cache'});
$self->SUPER::child_finish_hook();
$self->log(LOG_DEBUG,"[CBPOLICYD] Caching engine: hits = ".awitpt::cache::getCacheHits().", misses = ".
awitpt::cache::getCacheMisses()) if ($log_cache);
$self->log(LOG_DEBUG,"[CBPOLICYD] Shutting down caching engine ($$)");
awitpt::cache::disconnect($self);
}
# Process requests we get
sub process_request {
my $self = shift;
my $server = $self->{'server'};
my $log = defined($self->{'config'}{'logging'}{'modules'});
# Check for unix/tcp peer and set peer_type
my $sock = $self->{'server'}->{'client'};
if ($sock->NS_proto eq 'UNIX') {
$server->{'peer_type'} = "UNIX";
# Some defaults for debugging, these are undef if UNIX
$server->{'peeraddr'} = "";
$server->{'peerport'} = $sock->NS_unix_path;
$server->{'sockaddr'} = "";
$server->{'sockport'} = "";
} elsif ($sock->NS_proto eq 'TCP') {
$server->{'peer_type'} = "TCP";
} else {
$self->log(LOG_WARN,"[CBPOLICYD] Unknown peer type, expected UNIX / TCP. Rejecting.");
return;
}
# How many times did we pipeline...
my $policyRequests = 0;
#
# Loop till we fill up the buffer
#
# Beginning label, we do pipelining ...
CONN_READ:
# Found module, set to 1 if found, 0 if otherwize
my $found = 0;
# Buffer
my $buf = "";
# Create an FDSET for use in select()
my $fdset = "";
vec($fdset, fileno(STDIN), 1) = 1;
while (1) {
# Loop with modules
foreach my $module ( sort { $b->{'priority'} <=> $a->{'priority'} } @{$self->{'modules'}} ) {
# Skip over if we don't have a check...
next if (!defined($module->{'protocol_check'}));
# Check protocol
my $res = $module->{'protocol_check'}($self,$buf);
if (defined($res) && $res == 1) {
$found = $module;
}
}
# Last if found
last if ($found);
# We need to store this cause we use it below a few times
my $bufLen = length($buf);
# Again ... too large
if ($bufLen > 16*1024) {
$self->log(LOG_WARN,"[CBPOLICYD] Request too large from => Peer: ".$server->{'peeraddr'}.":".$server->{'peerport'}.", Local: ".
$server->{'sockaddr'}.":".$server->{'sockport'});
return;
}
# Setup timeout
my $timeout;
# If buffer length > 0, its a busy connection
if ($bufLen > 0) {
$timeout = $server->{'timeout_busy'};
# Else its idle
} else {
$timeout = $server->{'timeout_idle'};
}
# Check for timeout....
my $n = select($fdset,undef,undef,$timeout);
if (!$n) {
$self->log(LOG_NOTICE,"[CBPOLICYD] Timed out after ".$timeout."s from => Peer: ".$server->{'peeraddr'}.":".
$server->{'peerport'}.", Local: ".$server->{'sockaddr'}.":".$server->{'sockport'});
return;
}
# Read in 8kb
$n = sysread(STDIN,$buf,8192,$bufLen);
if (!$n) {
my $reason = defined($n) ? "Client closed connection" : "sysread[$!]";
$self->log(LOG_DEBUG,"[CBPOLICYD] $reason => Peer: ".$server->{'peeraddr'}.":".$server->{'peerport'}.", Local: ".
$server->{'sockaddr'}.":".$server->{'sockport'});
return;
}
}
# Check if a protocol handler wasn't found...
if (!$found) {
$self->log(LOG_WARN,"[CBPOLICYD] Request not understood => Peer: ".$server->{'peeraddr'}.":".$server->{'peerport'}.", Local: ".
$server->{'sockaddr'}.":".$server->{'sockport'});
return;
}
# Set protocol handler
$server->{'_protocol_handler'} = $found;
# If we have a init function, call it before processing...
$server->{'_protocol_handler'}->{'protocol_init'}($self) if (defined($server->{'_protocol_handler'}->{'protocol_init'}));
# Process buffer
my $request = $server->{'_protocol_handler'}->{'protocol_parse'}($self,$buf);
# Check data is ok...
if ((my $res = $server->{'_protocol_handler'}->{'protocol_validate'}($self,$request))) {
$self->log(LOG_WARN,"[CBPOLICYD] Protocol data validation error, $res");
$self->protocol_response(PROTO_ERROR);
print($self->protocol_getresponse());
return;
}
# Data mangling...
$request->{'sender'} = lc($request->{'sender'});
$request->{'recipient'} = lc($request->{'recipient'}) if (defined($request->{'recipient'}));
$request->{'sasl_username'} = lc($request->{'sasl_username'}) if (defined($request->{'sasl_username'}));
# Internal data
$request->{'_timestamp'} = time();
# If this is a TCP peer type then it has a peer address
if ($server->{'peer_type'} eq "TCP") {
$request->{'_peer_address'} = $server->{'peeraddr'};
}
# Check if we got connected, if not ... bypass
if ($self->{'client'}->{'dbh_status'} > 0) {
my $action;
$self->log(LOG_WARN,"[CBPOLICYD] Client in BYPASS mode due to DB connection failure!");
# Check bypass mode
if (!defined($self->{'inifile'}{'database'}{'bypass_mode'})) {
$self->log(LOG_ERR,"[CBPOLICYD] No bypass_mode specified for failed database connections, defaulting to tempfail");
$self->protocol_response(PROTO_DB_ERROR);
$action = "tempfail";
# Check for "tempfail"
} elsif (lc($self->{'inifile'}{'database'}{'bypass_mode'}) eq "tempfail") {
$self->protocol_response(PROTO_DB_ERROR);
$action = "tempfail";
# And for "bypass"
} elsif (lc($self->{'inifile'}{'database'}{'bypass_mode'}) eq "pass") {
$self->protocol_response(PROTO_PASS);
$action = "pass";
# Lasty for invalid
} else {
$self->log(LOG_ERR,"[CBPOLICYD] bypass_mode is invalid, defaulting to tempfail");
$self->protocol_response(PROTO_DB_ERROR);
$action = "tempfail";
}
$self->maillog("module=Core, action=$action, host=%s, from=%s, to=%s, reason=db_failure_bypass",
$request->{'client_address'} ? $request->{'client_address'} : "unknown",
$request->{'helo_name'} ? $request->{'helo_name'} : "",
$request->{'sender'} ? $request->{'sender'} : "unknown",
$request->{'recipient'} ? $request->{'recipient'} : "unknown");
print($self->protocol_getresponse());
# Check if we need to reconnect or not
my $timeout = $self->{'inifile'}{'database'}{'bypass_timeout'};
if (!defined($timeout)) {
$self->log(LOG_ERR,"[CBPOLICYD] No bypass_timeout specified for failed database connections, defaulting to 120s");
$timeout = 120;
}
# Get time left
my $timepassed = $request->{'_timestamp'} - $self->{'client'}->{'dbh_status'};
# Then check...
if ($timepassed >= $timeout) {
$self->log(LOG_NOTICE,"[CBPOLICYD] Client BYPASS timeout exceeded, reconnecting...");
exit 0;
} else {
$self->log(LOG_NOTICE,"[CBPOLICYD] Client still in BYPASS mode, ".( $timeout - $timepassed )."s left till next reconnect");
return;
}
}
# Setup database handle
awitpt::db::dblayer::setHandle($self->{'client'}->{'dbh'});
# Grab session data
my $sessionData = getSessionDataFromRequest($self,$request);
if (ref $sessionData ne "HASH") {
$self->log(LOG_DEBUG,"[CBPOLICYD:$$] Error getting session data");
$self->protocol_response(PROTO_ERROR);
print($self->protocol_getresponse());
return;
}
# Increment counter
$policyRequests++;
$self->log(LOG_INFO,"[CBPOLICYD] Got request #$policyRequests" . ($policyRequests > 1 ? " (pipelined)" : ""));
# Loop with modules
foreach my $module ( sort { $b->{'priority'} <=> $a->{'priority'} } @{$self->{'modules'}} ) {
# Skip over if we don't have a check...
next if (!defined($module->{'request_process'}));
$self->log(LOG_DEBUG,"[CBPOLICYD] Running module: ".$module->{'name'}) if ($log);
# Run request in eval
my $res;
eval {
$res = $module->{'request_process'}($self,$sessionData);
};
# Check results
if ($@) {
$self->log(LOG_ERR,"[CBPOLICYD] Error running module request_process(): $@");
$res = $self->protocol_response(PROTO_ERROR);
}
# Check responses
if (!defined($res)) {
$res = $self->protocol_response(PROTO_ERROR);
last;
} elsif ($res == CBP_SKIP) {
$self->log(LOG_DEBUG,"[CBPOLICYD] Module '".$module->{'name'}."' returned CBP_SKIP") if ($log);
next;
} elsif ($res == CBP_CONTINUE) {
$self->log(LOG_DEBUG,"[CBPOLICYD] Module '".$module->{'name'}."' returned CBP_CONTINUE") if ($log);
next;
} elsif ($res == CBP_STOP) {
$self->log(LOG_DEBUG,"[CBPOLICYD] Module '".$module->{'name'}."' returned CBP_STOP") if ($log);
last;
} elsif ($res == CBP_ERROR) {
$self->log(LOG_ERR,"[CBPOLICYD] Error returned from module '".$module->{'name'}."'");
last;
}
}
$self->log(LOG_DEBUG,"[CBPOLICYD] Done with modules") if ($log);
# Update session data
my $res = updateSessionData($self,$sessionData);
if ($res) {
$self->log(LOG_ERR,"[CBPOLICYD] Error updating session data");
$self->protocol_response(PROTO_ERROR);
}
# Grab and return response
my $response = $self->protocol_getresponse();
print($response);
# Carry on with pipelining?
goto CONN_READ;
}
# Initialize child
sub server_exit
{
my $self = shift;
my $log_mail = $self->{'config'}{'log_mail'};
$self->log(LOG_DEBUG,"Destroying system modules.");
# Destroy cache
awitpt::cache::Destroy($self);
$self->log(LOG_DEBUG,"System modules destroyed.");
# Check if we using syslog
if (defined($log_mail)) {
$self->log(LOG_DEBUG,"Closing syslog.");
Sys::Syslog::closelog();
$self->log(LOG_DEBUG,"Syslog closed.");
};
# Parent exit
$self->SUPER::server_exit();
}
# Slightly better logging
sub log
{
my ($self,$level,$msg,@args) = @_;
# Check log level and set text
my $logtxt = "UNKNOWN";
if ($level == LOG_DEBUG) {
$logtxt = "DEBUG";
} elsif ($level == LOG_INFO) {
$logtxt = "INFO";
} elsif ($level == LOG_NOTICE) {
$logtxt = "NOTICE";
} elsif ($level == LOG_WARN) {
$logtxt = "WARNING";
} elsif ($level == LOG_ERR) {
$logtxt = "ERROR";
}
# Parse message nicely
if ($msg =~ /^(\[[^\]]+\]) (.*)/s) {
$msg = "$1 $logtxt: $2";
} else {
$msg = "[CORE] $logtxt: $msg";
}
# If we have args, this is more than likely a format string & args
if (@args > 0) {
$msg = sprintf($msg,@args);
}
$self->SUPER::log($level,"[".$self->log_time." - $$] $msg");
}
# Syslog logging
sub maillog
{
my ($self,$msg,@args) = @_;
my $log_mail = $self->{'config'}{'log_mail'};
# Log to syslog
if (defined($log_mail)) {
# If we have args use printf style
if (@args) {
Sys::Syslog::syslog('info',$msg,@args);
} else {
Sys::Syslog::syslog('info','%s',$msg);
}
# Or log to main mechanism
} else {
$self->log(LOG_INFO,sprintf($msg,@args));
}
}
# Protocol response setting...
sub protocol_response
{
my $self = shift;
my $server = $self->{'server'};
# Make sure the response handler exists
if (!defined($server->{'_protocol_handler'}->{'protocol_response'})) {
$self->log(LOG_ERR,"[CBPOLICYD] No protocol response handler available");
return -1;
}
return $server->{'_protocol_handler'}->{'protocol_response'}($self,@_);
}
# Get protocol response
sub protocol_getresponse
{
my $self = shift;
my $server = $self->{'server'};
# Make sure the response handler exists
if (!defined($server->{'_protocol_handler'}->{'protocol_getresponse'})) {
$self->log(LOG_ERR,"[CBPOLICYD] No protocol getresponse handler available");
return -1;
}
return $server->{'_protocol_handler'}->{'protocol_getresponse'}($self);
}
# Display help
sub displayHelp {
print(STDERR "PolicyD (Cluebringer) v".VERSION." - Copyright (c) 2007-2014 AllWorldIT\n");
print(STDERR<<EOF);
Usage: $0 [args]
--config=<file> Configuration file
--debug Put into debug mode
--fg Don't go into background
EOF
}
__PACKAGE__->run;
1;
# vim: ts=4