DJabberd.pm

filter: Statementstmt 36.7% 143 / 389 Branchbran 2.8% 4 / 140 Conditioncond 14.2% 2 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 56.4% 44 / 78 Podpod 0.0% 0 / 34 totaltotal 29.1% 193 / 663 CC 82 SCAR 49.6
CC
82
Cov
27%
_load_config_ref
756.0
run
72.0
_start_server
72.0
CRAP 143.3
↑ Summary DJabberd/Connection.pm »
1 #!/usr/bin/perl
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Danga::Socket 1.51;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶use DJabberd::VHost;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use DJabberd::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use DJabberd::HookDocs;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use DJabberd::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use DJabberd::Connection::ServerIn;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use DJabberd::Connection::ClientIn;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use DJabberd::Connection::ClusterIn;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use DJabberd::Connection::OldSSLClientIn;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use DJabberd::Connection::Admin;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 17 ▶use DJabberd::Stanza::StartTLS;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 17 ▶use DJabberd::Stanza::SASL;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use DJabberd::Stanza::StreamFeatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 17 ▶use DJabberd::Stanza::DialbackVerify;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 17 ▶use DJabberd::Stanza::DialbackResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 17 ▶use DJabberd::JID;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 17 ▶use DJabberd::IQ;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 17 ▶use DJabberd::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 17 ▶use DJabberd::Presence;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 17 ▶use DJabberd::StreamVersion;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 17 ▶use DJabberd::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 17 ▶use DJabberd::Delivery::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 17 ▶use DJabberd::Delivery::S2S;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 17 ▶use DJabberd::PresenceChecker::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 17 ▶use DJabberd::Stats;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 package DJabberd;
40 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 17 ▶use Socket qw(IPPROTO_TCP TCP_NODELAY SOL_SOCKET SOCK_STREAM);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 17 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 17 ▶use DJabberd::Util qw(tsub as_bool as_num as_abs_path as_bind_addr);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 17 our $VERSION = '0.85_01';
46  
47 17 our $logger = DJabberd::Log->get_logger();
48 17 our $hook_logger = DJabberd::Log->get_logger("DJabberd::Hook");
49  
50 17 our %server;
51  
52 0 ▶$SIG{USR2} = sub { Carp::cluck("USR2") };
Subroutine __ANON__: not called CC=1 SCAR=1.2
53  
54 sub new {
55 38 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=4 SCAR=14.2
Pod undocumented
56  
57 my $self = {
58 'daemonize' => delete $opts{daemonize},
59 's2s_port' => delete $opts{s2s_port},
60 'c2s_port' => delete($opts{c2s_port}) || 5222, # {=clientportnumber}
61 'old_ssl' => delete $opts{old_ssl},
62 38 ▶ 'vhosts' => {},
Condition50%1/2
delete $opts{'c2s_port'} || 5222
l!l
038
MC/DC0%0/1
delete $opts{'c2s_port'} || 5222
delete $opts{'c2s_port'}
Truth table50%1/2
A
A= delete $opts{'c2s_port'}
Aresult
11
00
63 'fake_peers' => {}, # for s2s testing. $hostname => "ip:port"
64 'share_parsers' => 1,
65 'monitor_host' => {},
66 };
67  
68 # if they set s2s_port to explicitly 0, it's disabled for all vhosts
69 # but not setting it means 5269 still listens, if vhosts are configured
70 # for s2s.
71 # {=serverportnumber}
72 38 ▶ $self->{s2s_port} = 5269 unless defined $self->{s2s_port};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"s2s_port"} 38 0 38
73  
74 38 ▶ croak("Unknown server parameters: " . join(", ", keys %opts)) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 38 38
75  
76 38 bless $self, $class;
77 38 $server{$self} = $self;
78 38 Scalar::Util::weaken($server{$self});
79  
80 38 return $self;
81 }
82  
83 sub DESTROY {
84 38 ▶ delete $server{$_[0]};
Subroutine DESTROY: called CC=1 SCAR=0.0
85 }
86  
87 # class method
88 sub foreach_vhost {
89 0 ▶ my (undef, $cb) = @_;
Subroutine foreach_vhost: not called CC=3 SCAR=24.8
Pod undocumented
90 0 foreach my $server (values %DJabberd::server) {
91 0 foreach my $vhost (values %{$server->{vhosts}}) {
92 0 $cb->($vhost);
93 }
94 }
95 }
96  
97 0 ▶sub share_parsers { $_[0]{share_parsers} };
Subroutine share_parsers: not called CC=1 SCAR=6.9
Pod undocumented
98  
99 sub set_config_shareparsers {
100 0 ▶ my ($self, $val) = @_;
Subroutine set_config_shareparsers: not called CC=1 SCAR=6.9
Pod undocumented
101 0 $self->{share_parsers} = as_bool($val);
102 }
103  
104 sub set_config_declaremonitor {
105 0 ▶ my ($self, $val) = @_;
Subroutine set_config_declaremonitor: not called CC=1 SCAR=6.9
Pod undocumented
106 0 $self->{monitor_host}{$val} = 1;
107 }
108  
109 # mimicing Apache's SSLCertificateKeyFile config
110 sub set_config_sslcertificatekeyfile {
111 0 ▶ my ($self, $val) = @_;
Subroutine set_config_sslcertificatekeyfile: not called CC=1 SCAR=6.9
Pod undocumented
112 0 $self->{ssl_private_key_file} = as_abs_path($val);
113 }
114  
115 # mimicing Apache's SSLCertificateFile
116 sub set_config_sslcertificatefile {
117 0 ▶ my ($self, $val) = @_;
Subroutine set_config_sslcertificatefile: not called CC=1 SCAR=6.9
Pod undocumented
118 0 $self->{ssl_cert_file} = as_abs_path($val);
119 }
120  
121 0 ▶sub ssl_private_key_file { return $_[0]{ssl_private_key_file} }
Subroutine ssl_private_key_file: not called CC=1 SCAR=6.9
Pod undocumented
122 0 ▶sub ssl_cert_file { return $_[0]{ssl_cert_file} }
Subroutine ssl_cert_file: not called CC=1 SCAR=6.9
Pod undocumented
123  
124 sub set_config_oldssl {
125 0 ▶ my ($self, $val) = @_;
Subroutine set_config_oldssl: not called CC=1 SCAR=6.9
Pod undocumented
126 0 $self->{old_ssl} = as_bool($val);
127 }
128  
129 sub set_config_unixdomainsocket {
130 0 ▶ my ($self, $val) = @_;
Subroutine set_config_unixdomainsocket: not called CC=1 SCAR=6.9
Pod undocumented
131 0 $self->{unixdomainsocket} = $val;
132 }
133  
134 sub set_config_clientport {
135 37 ▶ my ($self, $val) = @_;
Subroutine set_config_clientport: called CC=1 SCAR=0.0
Pod undocumented
136 37 $self->{c2s_port} = as_bind_addr($val);
137 }
138  
139 sub set_config_serverport {
140 37 ▶ my ($self, $val) = @_;
Subroutine set_config_serverport: called CC=1 SCAR=0.0
Pod undocumented
141 37 $self->{s2s_port} = as_bind_addr($val);
142 }
143  
144 sub set_config_adminport {
145 1 ▶ my ($self, $val) = @_;
Subroutine set_config_adminport: called CC=1 SCAR=0.0
Pod undocumented
146 1 $self->{admin_port} = as_bind_addr($val);
147 }
148  
149 sub set_config_intradomainlisten {
150 0 ▶ my ($self, $val) = @_;
Subroutine set_config_intradomainlisten: not called CC=1 SCAR=6.9
Pod undocumented
151 0 $self->{cluster_listen} = $val;
152 }
153  
154 sub set_config_pidfile {
155 0 ▶ my ($self, $val) = @_;
Subroutine set_config_pidfile: not called CC=1 SCAR=6.9
Pod undocumented
156 0 $self->{pid_file} = $val;
157 }
158  
159 17 our %fake_peers;
160 sub set_fake_s2s_peer {
161 23 ▶ my ($self, $host, $ipendpt) = @_;
Subroutine set_fake_s2s_peer: called CC=1 SCAR=0.0
Pod undocumented
162 23 $fake_peers{$host} = $ipendpt;
163 }
164  
165 sub fake_s2s_peer {
166 0 ▶ my ($self, $host) = @_;
Subroutine fake_s2s_peer: not called CC=1 SCAR=6.9
Pod undocumented
167 0 return $fake_peers{$host};
168 }
169  
170 sub set_config_casesensitive {
171 0 ▶ my ($self, $val) = @_;
Subroutine set_config_casesensitive: not called CC=1 SCAR=6.9
Pod undocumented
172 0 $DJabberd::JID::CASE_SENSITIVE = as_bool($val);
173 }
174  
175 sub add_vhost {
176 38 ▶ my ($self, $vhost) = @_;
Subroutine add_vhost: called CC=5 SCAR=19.7
Pod undocumented
177 38 my $sname = lc $vhost->name;
178 38 ▶ if (my $existing = $self->{vhosts}{$sname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $existing = $self->{"vhosts"}{$sname}) 0 38 38
179 0 ▶ croak("Can't set vhost with name '$sname'. Already exists in this server.")
Branch: 1 branch
BranchTrueFalseTotal
if $existing != $vhost 0 0 0
180 if $existing != $vhost;
181 }
182 38 ▶ if ($vhost->server && $vhost->server != $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vhost->server and $vhost->server != $self) 0 38 38
Condition33%1/3
$vhost->server and $vhost->server != $self
!ll&&!rl&&r
3800
MC/DC0%0/2
$vhost->server and $vhost->server != $self
$vhost->server $vhost->server != $self
Truth table33%1/3
A and B
A= $vhost->serverB= $vhost->server != $self
ABresult
0X0
100
111
183 0 croak("Vhost already has a server.");
184 }
185  
186 38 $vhost->setup_default_plugins;
187  
188 38 $self->{vhosts}{$sname} = $vhost;
189 38 $vhost->set_server($self);
190 }
191  
192 # works as Server method or class method.
193 sub lookup_vhost {
194 0 ▶ my ($self, $hostname) = @_;
Subroutine lookup_vhost: not called CC=6 SCAR=37.4
Pod undocumented
195  
196 # look at all server objects in process
197 0 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
198 0 foreach my $server (values %DJabberd::server) {
199 0 my $vh = $server->lookup_vhost($hostname);
200 0 ▶ return $vh if $vh;
Branch: 1 branch
BranchTrueFalseTotal
if $vh 0 0 0
201 }
202 0 return 0;
203 }
204  
205 # method on server object
206 0 foreach my $vhost (values %{$self->{vhosts}}) {
207 0 ▶ return $vhost
Branch: 1 branch
BranchTrueFalseTotal
if $vhost->handles_domain($hostname) 0 0 0
208 if ($vhost->handles_domain($hostname));
209 }
210 0 return 0;
211 }
212  
213 # return the version of the spec we implement
214 sub spec_version {
215 0 ▶ my $self = shift;
Subroutine spec_version: not called CC=2 SCAR=17.9
Pod undocumented
216 0 ▶ return $self->{_spec_version} ||= DJabberd::StreamVersion->new("1.0");
Condition0%0/3
$self->{"_spec_version"} ||= "DJabberd::StreamVersion"->new("1.0")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_spec_version"} ||= "DJabberd::StreamVersion"->new("1.0")
$self->{"_spec_version"} "DJabberd::StreamVersion"->new("1.0")
Truth table0%0/3
A ||= B
A= $self->{"_spec_version"}B= "DJabberd::StreamVersion"->new("1.0")
ABresult
1X1
011
000
217 }
218  
219  
220 17 my %obj_source; # refaddr -> file/linenumber
221 my %obj_living; # file/linenumber -> ct
222 17 ▶use Scalar::Util qw(refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
223 17 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
224 sub dump_obj_stats {
225 0 ▶ print Dumper(\%obj_living);
Subroutine dump_obj_stats: not called CC=2 SCAR=17.9
Pod undocumented
226 0 my %class_ct;
227 0 foreach (values %obj_source) {
228 0 $class_ct{ref($_->[1])}++;
229 }
230 0 print Dumper(\%class_ct);
231 }
232  
233  
234 sub track_new_obj {
235 0 ▶ return unless $ENV{TRACKOBJ};
Subroutine track_new_obj: not called CC=6 SCAR=37.4
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"TRACKOBJ"} 0 0 0
236  
237 0 my ($class, $obj) = @_;
238 0 my $i = 0;
239 0 my $fileline;
240 0 ▶ while (!$fileline) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fileline) 0 0 0
241 0 $i++;
242 0 my ($pkg, $filename, $line, $subname) = caller($i);
243 0 ▶ next if $subname eq "new";
Branch: 1 branch
BranchTrueFalseTotal
if $subname eq "new" 0 0 0
244 0 $fileline = "$filename/$line";
245 }
246 0 my $addr = refaddr($obj);
247 0 ▶ warn "New object $obj -- $fileline\n" if $ENV{TRACKOBJ};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACKOBJ"} 0 0 0
248 0 $obj_source{$addr} = [$fileline, $obj];
249 0 weaken($obj_source{$addr}[1]);
250  
251 0 $obj_living{$fileline}++;
252 0 ▶ dump_obj_stats() if $ENV{TRACKOBJ};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACKOBJ"} 0 0 0
253 }
254  
255 sub track_destroyed_obj {
256 0 ▶ return unless $ENV{TRACKOBJ};
Subroutine track_destroyed_obj: not called CC=5 SCAR=34.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"TRACKOBJ"} 0 0 0
257  
258 0 my ($class, $obj) = @_;
259 0 my $addr = refaddr($obj);
260 0 ▶ my $fileline = $obj_source{$addr}->[0] or die "Where did $obj come from?";
Branch: 1 branch
BranchTrueFalseTotal
my $fileline = $obj_source{$addr}[0] or die "Where did $obj come from?" 0 0 0
261 0 delete $obj_source{$addr};
262 0 ▶ warn "Destroyed object $obj -- $fileline\n" if $ENV{TRACKOBJ};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACKOBJ"} 0 0 0
263 0 $obj_living{$fileline}--;
264 0 ▶ dump_obj_stats() if $ENV{TRACKOBJ};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACKOBJ"} 0 0 0
265 }
266  
267 sub debug {
268 0 ▶ my $self = shift;
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
269 0 ▶ return unless $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"debug"} 0 0 0
270 0 printf STDERR @_;
271 }
272  
273 sub run {
274 0 ▶ my $self = shift;
Subroutine run: not called CC=8 SCAR=42.8
Pod undocumented
275 0 ▶ daemonize() if $self->{daemonize};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"daemonize"} 0 0 0
276 0 local $SIG{'PIPE'} = "IGNORE"; # handled manually
277 0 ▶ if ($self->{pid_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid_file"}) 0 0 0
278 0 $logger->debug("Logging PID to file $self->{pid_file}");
279 0 ▶ open(PIDFILE,'>',$self->{pid_file}) or $logger->logdie("Can't open pidfile $self->{pid_file} for writing");
Branch: 1 branch
BranchTrueFalseTotal
open PIDFILE, ">", $self->{"pid_file"} or $DJabberd::logger->logdie(qq[Can't open pidfile $self->{"pid_file"} for writing]) 0 0 0
280 0 print PIDFILE "$$\n";
281 0 close(PIDFILE);
282 }
283 0 $self->start_c2s_server();
284  
285 # {=s2soptional}
286 0 ▶ $self->start_s2s_server() if $self->{s2s_port};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"s2s_port"} 0 0 0
287  
288 0 ▶ $self->start_cluster_server() if $self->{cluster_listen};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cluster_listen"} 0 0 0
289  
290 0 ▶ $self->_start_server($self->{admin_port}, "DJabberd::Connection::Admin") if $self->{admin_port};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"admin_port"} 0 0 0
291  
292 0 DJabberd::Connection::Admin->on_startup;
293 0 Danga::Socket->EventLoop();
294 0 ▶ unlink($self->{pid_file}) if (-f $self->{pid_file});
Branch: 1 branch
BranchTrueFalseTotal
if -f $self->{"pid_file"} 0 0 0
295 }
296  
297 sub _start_server {
298 0 ▶ my ($self, $localaddr, $class) = @_;
Subroutine _start_server: not called CC=8 SCAR=42.8
299  
300 # establish SERVER socket, bind and listen.
301 0 my $server;
302 0 my $not_tcp = 0;
303 0 ▶ if ($localaddr =~ m!^/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($localaddr =~ m[^/]) { } 0 0 0
304 0 $not_tcp = 1;
305 0 ▶ $server = IO::Socket::UNIX->new(Type => SOCK_STREAM,
Branch: 1 branch
BranchTrueFalseTotal
$server = "IO::Socket::UNIX"->new("Type", 1, "Local", $localaddr, "Listen", 10) or $DJabberd::logger->logdie("Error creating unix domain socket: $@\n") 0 0 0
306 Local => $localaddr,
307 Listen => 10)
308 or $logger->logdie("Error creating unix domain socket: $@\n");
309 } else {
310 # assume it's a port if there's no colon
311 0 ▶ unless ($localaddr =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($localaddr =~ /:/) 0 0 0
312 0 $localaddr = "0.0.0.0:$localaddr";
313 }
314  
315 0 $logger->debug("Opening TCP listen socket on $localaddr");
316  
317 0 ▶ $server = IO::Socket::INET->new(LocalAddr => $localaddr,
Branch: 1 branch
BranchTrueFalseTotal
$server = "IO::Socket::INET"->new("LocalAddr", $localaddr, "Type", 1, "Proto", 6, "Reuse", 1, "Listen", 10) or $DJabberd::logger->logdie("Error creating socket: $@\n") 0 0 0
318 Type => SOCK_STREAM,
319 Proto => IPPROTO_TCP,
320 Reuse => 1,
321 Listen => 10 )
322 or $logger->logdie("Error creating socket: $@\n");
323  
324 0 my $success = $server->blocking(0);
325  
326 0 ▶ unless (defined($success)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $success) 0 0 0
327 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
328 # On Windows, we have to do this a bit differently
329 0 my $do = 1;
330 0 ▶ ioctl($server, 0x8004667E, \$do) or $logger->warn("Failed to make socket non-blocking: $!");
Branch: 1 branch
BranchTrueFalseTotal
ioctl $server, 2147772030, \$do or $DJabberd::logger->warn("Failed to make socket non-blocking: $!") 0 0 0
331 }
332 else {
333 0 $logger->warn("Failed to make socket non-blocking: $!")
334 }
335 }
336 }
337  
338 # Not sure if I'm crazy or not, but I can't see in strace where/how
339 # Perl 5.6 sets blocking to 0 without this. In Perl 5.8, IO::Socket::INET
340 # obviously sets it from watching strace.
341 0 IO::Handle::blocking($server, 0);
342  
343 my $accept_handler = sub {
344 0 ▶ local *__ANON__ = " Accept handler in ". __FILE__ ." on line ". __LINE__;
Subroutine __ANON__: not called CC=5 SCAR=34.0
345  
346 0 my $csock = $server->accept;
347 0 ▶ return unless $csock;
Branch: 1 branch
BranchTrueFalseTotal
unless $csock 0 0 0
348  
349 0 $self->debug("Listen child making a DJabberd::Connection for %d.\n", fileno($csock));
350  
351 0 IO::Handle::blocking($csock, 0);
352 0 ▶ unless ($not_tcp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($not_tcp) 0 0 0
353 0 ▶ setsockopt($csock, IPPROTO_TCP, TCP_NODELAY, pack("l", 1)) or die;
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $csock, 6, 1, "\1\0\0\0" or die 0 0 0
354 }
355  
356 0 ▶ if (my $client = eval { $class->new($csock, $self) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $client = eval { do { $class->new($csock, $self) } }) { } 0 0 0
357 0 $DJabberd::Stats::counter{connect}++;
358 0 $client->watch_read(1);
359 0 return;
360 } else {
361 0 $logger->error("Error creating new $class: $@");
362 }
363 0 };
364  
365 0 Danga::Socket->AddOtherFds(fileno($server) => $accept_handler);
366 }
367  
368 sub start_c2s_server {
369 0 ▶ my $self = shift;
Subroutine start_c2s_server: not called CC=3 SCAR=24.8
Pod undocumented
370 $self->_start_server($self->{c2s_port},
371 0 "DJabberd::Connection::ClientIn");
372  
373 0 ▶ if ($self->{old_ssl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"old_ssl"}) 0 0 0
374 0 $self->_start_server(5223, "DJabberd::Connection::OldSSLClientIn");
375 }
376  
377 0 ▶ if ($self->{unixdomainsocket}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"unixdomainsocket"}) 0 0 0
378 0 $self->_start_server($self->{unixdomainsocket}, "DJabberd::Connection::ClientIn");
379 }
380 }
381  
382 sub start_s2s_server {
383 0 ▶ my $self = shift;
Subroutine start_s2s_server: not called CC=1 SCAR=6.9
Pod undocumented
384 $self->_start_server($self->{s2s_port},
385 0 "DJabberd::Connection::ServerIn");
386 }
387  
388 sub start_cluster_server {
389 0 ▶ my $self = shift;
Subroutine start_cluster_server: not called CC=1 SCAR=6.9
Pod undocumented
390 $self->_start_server($self->{cluster_listen},
391 0 "DJabberd::Connection::ClusterIn");
392 }
393  
394 sub start_simple_server {
395 0 ▶ my ($self, $port) = @_;
Subroutine start_simple_server: not called CC=2 SCAR=17.9
Pod undocumented
396 0 ▶ eval "use DJabberd::Connection::SimpleIn; 1"
Branch: 1 branch
BranchTrueFalseTotal
eval "use DJabberd::Connection::SimpleIn; 1" or die "Failed to load DJabberd::Connection::SimpleIn: $@\n" 0 0 0
397 or die "Failed to load DJabberd::Connection::SimpleIn: $@\n";
398 0 $self->_start_server($port, "DJabberd::Connection::SimpleIn");
399 }
400  
401 sub is_monitor_host {
402 0 ▶ my ($self, $host) = @_;
Subroutine is_monitor_host: not called CC=1 SCAR=6.9
Pod undocumented
403 0 return $self->{monitor_host}{$host};
404 }
405  
406 sub load_config {
407 0 ▶ my ($self, $arg) = @_;
Subroutine load_config: not called CC=3 SCAR=24.8
Pod undocumented
408 0 ▶ if (ref $arg eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'SCALAR') { } 0 0 0
409 0 $self->_load_config_ref($arg);
410 } else {
411 0 ▶ open (my $fh, $arg) or die "Couldn't open config file '$arg': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $arg or die "Couldn't open config file '${arg}': $!\n" 0 0 0
412 0 my $slurp = do { local $/; <$fh>; };
413 0 $self->_load_config_ref(\$slurp);
414 }
415 }
416  
417 sub _load_config_ref {
418 0 ▶ my ($self, $configref) = @_;
Subroutine _load_config_ref: not called CC=27 SCAR=66.3
419 0 my $linenum = 0;
420 0 my $vhost; # current vhost in scope
421 my $plugin; # current plugin in scope
422 0 my @vhost_stack = ();
423  
424 my $expand_var = sub {
425 0 ▶ my ($type, $key) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
426 0 $type = uc $type;
427  
428 0 ▶ if ($type eq "ENV") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ENV") 0 0 0
429 # expands ${ENV:KEY} on a line into $ENV{'KEY'} or dies if not defined
430 0 my $val = $ENV{$key};
431 0 ▶ die "Undefined environment variable '$key'\n" unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
432 0 return $val;
433 }
434 0 die "Unknown variable type '$type'\n";
435 0 };
436  
437 0 foreach my $line (split(/\n/, $$configref)) {
438 0 $linenum++;
439  
440 0 $line =~ s/^\s+//;
441 0 ▶ next if $line =~ /^\#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\#/ 0 0 0
442 0 $line =~ s/\s+$//;
443 0 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 0 0
444  
445 0 eval {
446 # expand environment variables
447 0 $line =~ s/\$\{(\w+):(\w+)\}/$expand_var->($1, $2)/eg;
448  
449 0 ▶ if ($line =~ /^(\w+)\s+(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\w+)\s+(.+)/) 0 0 0
450 0 my $pkey = $1;
451 0 my $key = lc $1;
452 0 my $val = $2;
453 0 ▶ my $inv = $plugin || $vhost || $self;
Condition0%0/6
$plugin || $vhost || $self
l!l&&r!l&&!r
000
$plugin or $vhost
l!l&&r!l&&!r
000
MC/DC0%0/3
$plugin || $vhost || $self
$plugin $vhost $self
Truth table0%0/4
A or B || C
A= $pluginB= $vhostC= $self
ABCresult
1XX1
01X1
0011
0000
454 0 my $meth = "set_config_$key";
455 0 ▶ if ($inv->can($meth)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inv->can($meth)) 0 0 0
456 0 $inv->$meth($val);
457 0 next;
458 }
459 0 $meth = "set_config__option";
460 0 ▶ if ($inv->can($meth)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inv->can($meth)) 0 0 0
461 0 $inv->$meth($key, $val);
462 0 next;
463 }
464  
465 0 die "Unknown option '$pkey'\n";
466 }
467 0 ▶ if ($line =~ /<VHost\s+(\S+?)>/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /<VHost\s+(\S+?)>/i) 0 0 0
468 0 ▶ die "Can't configure a vhost in a vhost\n" if $vhost;
Branch: 1 branch
BranchTrueFalseTotal
if $vhost 0 0 0
469 0 $vhost = DJabberd::VHost->new(server_name => $1);
470 0 $vhost->set_server($self);
471 0 next;
472 }
473 0 ▶ if ($line =~ m!</VHost>!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[</VHost>]i) 0 0 0
474 0 ▶ die "Can't end a not-open vhost\n" unless $vhost;
Branch: 1 branch
BranchTrueFalseTotal
unless $vhost 0 0 0
475 0 ▶ die "Can't end a vhost with an open plugin\n" if $plugin;
Branch: 1 branch
BranchTrueFalseTotal
if $plugin 0 0 0
476 0 ▶ die "Can't end a vhost with an open subdomain\n" if @vhost_stack;
Branch: 1 branch
BranchTrueFalseTotal
if @vhost_stack 0 0 0
477 0 $self->add_vhost($vhost);
478 0 $vhost = undef;
479 0 next;
480 }
481 0 ▶ if ($line =~ /<Subdomain\s+(\S+?)>/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /<Subdomain\s+(\S+?)>/i) 0 0 0
482 0 ▶ die "Subdomain blocks can only inside VHost\n" unless $vhost;
Branch: 1 branch
BranchTrueFalseTotal
unless $vhost 0 0 0
483 0 my $subdomain_name = $1.".".$vhost->server_name;
484  
485 0 my $old_vhost = $vhost;
486 0 push @vhost_stack, $old_vhost;
487  
488 0 $vhost = DJabberd::VHost->new(server_name => $subdomain_name);
489 0 $vhost->set_server($self);
490  
491 # Automatically add the LocalVHosts delivery plugin so that these
492 # VHosts can talk to one another without S2S.
493 0 my $loaded = eval "use DJabberd::Delivery::LocalVHosts; 1;";
494 0 ▶ die "Failed to load LocalVHosts delivery plugin for subdomain" unless $loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 0 0 0
495  
496 0 my $ld1 = DJabberd::Delivery::LocalVHosts->new(allowvhost => $subdomain_name);
497 0 my $ld2 = DJabberd::Delivery::LocalVHosts->new(allowvhost => $old_vhost->server_name);
498 0 $old_vhost->add_plugin($ld1);
499 0 $vhost->add_plugin($ld2);
500  
501 0 next;
502 }
503 0 ▶ if ($line =~ m!</Subdomain>!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[</Subdomain>]i) 0 0 0
504 0 ▶ die "Extraneous subdomain end\n" unless @vhost_stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @vhost_stack 0 0 0
505 0 $self->add_vhost($vhost);
506 0 $vhost = pop @vhost_stack;
507 0 next;
508 }
509 my $close_plugin = sub {
510 0 ▶ die "Can't end a not-open plugin\n" unless $plugin;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin 0 0 0
511 0 $plugin->finalize;
512 0 $vhost->add_plugin($plugin);
513 0 $plugin = undef;
514 0 };
515  
516 0 ▶ if ($line =~ /<Plugin\s+([\w:]+?)(\s*\/)?>/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[<Plugin\s+([\w:]+?)(\s*/)?>]i) 0 0 0
517 0 my $class = $1;
518 0 my $immediate_close = $2;
519 0 ▶ die "Can't configure a plugin outside of a vhost config vhost\n" unless $vhost;
Branch: 1 branch
BranchTrueFalseTotal
unless $vhost 0 0 0
520 0 ▶ die "Can't configure a plugin inside of a plugin\n" if $plugin;
Branch: 1 branch
BranchTrueFalseTotal
if $plugin 0 0 0
521  
522 0 my $loaded = eval "use $class; 1;";
523 0 ▶ die "Failed to load plugin $class: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
524 0 $plugin = $class->new;
525 0 ▶ $close_plugin->() if $immediate_close;
Branch: 1 branch
BranchTrueFalseTotal
if $immediate_close 0 0 0
526 0 next;
527 }
528 0 ▶ if ($line =~ m!</Plugin>!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[</Plugin>]i) 0 0 0
529 0 $close_plugin->();
530 0 next;
531 }
532  
533 0 die "Syntax error: '$line'\n";
534 };
535 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
536 0 die "Configuration error on line $linenum: $@\n";
537 }
538 }
539 }
540  
541 sub daemonize {
542 0 ▶ my($pid, $sess_id, $i);
Subroutine daemonize: not called CC=4 SCAR=30.0
Pod undocumented
543  
544 ## Fork and exit parent
545 0 ▶ if ($pid = fork) { exit 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($pid = fork) 0 0 0
546  
547 ## Detach ourselves from the terminal
548 0 ▶ Carp::croak("Cannot detach from controlling terminal")
Branch: 1 branch
BranchTrueFalseTotal
unless $sess_id = POSIX::setsid() 0 0 0
549 unless $sess_id = POSIX::setsid();
550  
551 ## Prevent possibility of acquiring a controling terminal
552 0 $SIG{'HUP'} = 'IGNORE';
553 0 ▶ if ($pid = fork) { exit 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($pid = fork) 0 0 0
554  
555 ## Change working directory
556 0 chdir "/";
557  
558 ## Clear file creation mask
559 0 umask 0;
560  
561 ## Close open file descriptors
562 0 close(STDIN);
563 0 close(STDOUT);
564 0 close(STDERR);
565  
566 ## Reopen stderr, stdout, stdin to /dev/null
567 0 open(STDIN, "+>/dev/null");
568 0 open(STDOUT, "+>&STDIN");
569 0 open(STDERR, "+>&STDIN");
570 }
571  
572 # Local Variables:
573 # mode: perl
574 # c-basic-indent: 4
575 # indent-tabs-mode: nil
576 # End:
577  
578 17 1;
579  
580 __END__
581  
582 =head1 NAME
583  
584 DJabberd - scalable, extensible Jabber/XMPP server.
585  
586 =head1 SYNOPSIS
587  
588 $ djabberd --conf=config-file.conf --daemonize
589  
590 =head1 DESCRIPTION
591  
592 DJabberd was the answer to LiveJournal's Jabber (XMPP)
593 server needs. We needed:
594  
595 =over 4
596  
597 =item * good performance for tons of connected users
598  
599 =item * ability to scale to multiple nodes
600  
601 =item * ability to hook into LiveJournal at all places, not just auth
602  
603 =back
604  
605 Basically we wanted the swiss army knife of Jabber servers (think
606 qpsmtpd or mod_perl), but none existed in any language. While some
607 popular Jabber servers let us do pluggable auth, none let us get our
608 hands into roster storage, vcards, avatars, presence, etc.
609  
610 So we made DJabberd. It's a Jabber server where almost everything
611 defers to hooks to be implemented by plugins. It does the core spec
612 itself (including SSL, StartTLS, server-to-server, etc), but it
613 doesn't come with any way to do authentication or storage or rosters,
614 etc. You'll need to go pick up a plugin to do those.
615  
616 You should be able to plop DJabberd into your existing systems /
617 userbase with minimal pain. Just find a plugin that's close (if a
618 perfect match doesn't already exist!) and tweak.
619  
620 DJabberd is event-based so we can have really low per-connection
621 memory requirements, smaller than is possible with a threaded jabber
622 server. Because of this, all plugins can operate asynchronously,
623 taking as long as they want to finish their work, or to decline to the
624 next handler. (in the meantime, while plugins wait on a response from
625 whatever they're talking to, the DJabberd event loop continues at full
626 speed) However, that's more work, so some plugins may choose to
627 operate synchronously, but they do so with the understanding that
628 those plugins will cause the whole server to get bogged down. If
629 you're running a Jabber server for 5 users, you may not care that the
630 SQLite authentication backend pauses your server for milliseconds at a
631 time, but on a site with hundreds of thousands of connections, that
632 wouldn't be acceptable. Watch out for those plugins.
633  
634 =head1 HACKING
635  
636 Read HookDocs.pm, read the major base classes (Authen, RosterStorage,
637 PresenceChecker), join the mailing list, ask questions ... we're here
638 to help. If you want more hooks, let us know! If we forgot one, it
639 doesn't hurt us to add more.
640  
641 =head1 COPYRIGHT
642  
643 This module is Copyright (c) 2006 Six Apart, Ltd.
644 All rights reserved.
645  
646 You may distribute under the terms of either the GNU General Public
647 License or the Artistic License, as specified in the Perl README file.
648  
649 =head1 WARRANTY
650  
651 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
652  
653 =head1 WEBSITE
654  
655 Visit:
656  
657 http://danga.com/djabberd/
658  
659 =head1 AUTHORS
660  
661 Brad Fitzpatrick <brad@danga.com>
662  
663 Artur Bergman <sky@crucially.net>
664  
665 Jonathan Steinert <jsteinert@sixapart.com>
666  
↑ Summary DJabberd/Connection.pm »