-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathDJabberd.pm
877 lines (717 loc) · 25.5 KB
/
DJabberd.pm
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
#!/usr/bin/perl
use strict;
use Carp;
use Danga::Socket 1.51;
use IO::Socket::INET6;
use IO::Socket::UNIX;
use POSIX ();
use DJabberd::VHost;
use DJabberd::Callback;
use Scalar::Util;
use DJabberd::HookDocs;
use DJabberd::Connection;
use DJabberd::Connection::ServerIn;
use DJabberd::Connection::ClientIn;
use DJabberd::Connection::ClusterIn;
use DJabberd::Connection::OldSSLClientIn;
use DJabberd::Connection::Admin;
use DJabberd::Stanza::StartTLS;
use DJabberd::Stanza::SASL;
use DJabberd::Stanza::StreamFeatures;
use DJabberd::Stanza::DialbackVerify;
use DJabberd::Stanza::DialbackResult;
use DJabberd::JID;
use DJabberd::IQ;
use DJabberd::Message;
use DJabberd::Presence;
use DJabberd::StreamVersion;
use DJabberd::Log;
use DJabberd::Delivery::Local;
use DJabberd::Delivery::S2S;
use DJabberd::PresenceChecker::Local;
use DJabberd::Stats;
package DJabberd;
use strict;
use Socket qw(IPPROTO_TCP TCP_NODELAY SOL_SOCKET SOCK_STREAM);
use Carp qw(croak);
use DJabberd::Util qw(tsub as_bool as_num as_abs_path as_bind_addr);
our $VERSION = '0.85';
our $logger = DJabberd::Log->get_logger();
our $hook_logger = DJabberd::Log->get_logger("DJabberd::Hook");
our %server;
$SIG{USR2} = sub { Carp::cluck("USR2") };
sub new {
my ($class, %opts) = @_;
my $s2s_port = delete $opts{s2s_port};
my $c2s_port = delete $opts{c2s_port};
my $ssl_port = delete $opts{ssl_port};
my $self = {
'daemonize' => delete $opts{daemonize},
'old_ssl' => delete $opts{old_ssl},
'vhosts' => {},
'fake_peers' => {}, # for s2s testing. $hostname => "ip:port"
'share_parsers' => 1,
'monitor_host' => {},
};
# if they set s2s_port to explicitly 0, it's disabled for all vhosts
# but not setting it means 5269 still listens, if vhosts are configured
# for s2s.
# {=serverportnumber}
if(defined($s2s_port) && ref($s2s_port) eq 'HASH') { # hashref
$self->{s2s_port} = $s2s_port;
} elsif($s2s_port && !ref($s2s_port)) { # scalar
$self->{s2s_port}->{$s2s_port}++;
} elsif(!defined($s2s_port)) { # default
$self->{s2s_port}->{5269}++;
}
if(defined($c2s_port) && ref($c2s_port) eq 'HASH') { # hashref
$self->{c2s_port} = $c2s_port;
} elsif($c2s_port && !ref($c2s_port)) { # scalar
$self->{c2s_port}->{$c2s_port}++;
} else { # default
$self->{c2s_port}->{5222}++;
}
if(defined($ssl_port) && ref($ssl_port) eq 'HASH') {
$self->{ssl_port} = $ssl_port;
} elsif($ssl_port && !ref($ssl_port)) { # scalar
$self->{ssl_port}->{$ssl_port}++;
}
croak("Unknown server parameters: " . join(", ", keys %opts)) if %opts;
bless $self, $class;
$server{$self} = $self;
Scalar::Util::weaken($server{$self});
return $self;
}
sub DESTROY {
delete $server{$_[0]};
}
# class method
sub foreach_vhost {
my (undef, $cb) = @_;
foreach my $server (values %DJabberd::server) {
foreach my $vhost (values %{$server->{vhosts}}) {
$cb->($vhost);
}
}
}
sub share_parsers { $_[0]{share_parsers} };
sub set_config_shareparsers {
my ($self, $val) = @_;
$self->{share_parsers} = as_bool($val);
}
sub set_config_declaremonitor {
my ($self, $val) = @_;
$self->{monitor_host}{$val} = 1;
}
# mimicing Apache's SSLCertificateKeyFile config
sub set_config_sslcertificatekeyfile {
my ($self, $val) = @_;
$self->{ssl_private_key_file} = as_abs_path($val);
}
# mimicing Apache's SSLCertificateFile
sub set_config_sslcertificatefile {
my ($self, $val) = @_;
$self->{ssl_cert_file} = as_abs_path($val);
}
# mimicing Apache's SSLCertificateChainFile
sub set_config_sslcertificatechainfile {
my ($self, $val) = @_;
$self->{ssl_cert_chain_file} = as_abs_path($val);
}
sub set_config_sslcacertificatefile {
my ($self, $val) = @_;
$self->{ssl_ca_cert_file} = as_abs_path($val);
}
sub set_config_sslcacertificatepath {
my ($self, $val) = @_;
die "Path '$val' isn't absolute" unless $val =~ m!^/!;
die "Path '$val' doesn't exist" unless -d $val;
$self->{ssl_ca_cert_path} = $val;
}
sub set_config_sslciphersuite {
my ($self, $val) = @_;
$self->{ssl_cipher_list} = $val;
}
sub set_config_ssldhparamfile {
my ($self, $val) = @_;
$self->{ssl_dhparam_file} = as_abs_path($val);
}
sub set_config_sslecdhcurve {
my ($self, $val) = @_;
$self->{ssl_ecdh_curve} = $val;
}
sub set_config_sslminprotocol {
my ($self,$val) = @_;
$self->{ssl_min_proto} = $val;
}
sub ssl_private_key_file { return $_[0]{ssl_private_key_file} }
sub ssl_cert_file { return $_[0]{ssl_cert_file} }
sub ssl_cert_chain_file { return $_[0]{ssl_cert_chain_file} }
sub ssl_ca_cert_file { return $_[0]{ssl_ca_cert_file}||'' }
sub ssl_ca_cert_path { return $_[0]{ssl_ca_cert_path}||'' }
sub ssl_cipher_list { return $_[0]{ssl_cipher_list} }
sub ssl_dhparam_file { return $_[0]{ssl_dhparam_file} }
sub ssl_ecdh_curve { return $_[0]{ssl_ecdh_curve} }
sub ssl_min_protocol { return $_[0]{ssl_min_proto} }
sub set_config_oldssl {
my ($self, $val) = @_;
$self->{old_ssl} = as_bool($val);
}
sub add_config_sslport {
my ($self, $val) = @_;
$self->{ssl_port}->{as_bind_addr($val)}++;
}
sub set_config_sslport {
my ($self, $val) = @_;
$self->{ssl_port} = {
as_bind_addr($val) => 1,
};
}
sub add_config_unixdomainsocket {
my ($self, $val) = @_;
$self->{unixdomainsocket}->{$val}++;
}
sub set_config_unixdomainsocket {
my ($self, $val) = @_;
$self->{unixdomainsocket} = {
$val => 1,
};
}
sub set_config_clientport {
my ($self, $val) = @_;
$self->{c2s_port} = {
as_bind_addr($val) => 1,
};
}
sub add_config_clientport {
my ($self, $val) = @_;
# necessary because the default in the constructor
# will be 5222 and subsequent binds would fail
if($self->{c2s_port} && ref($self->{c2s_port}) && exists $self->{c2s_port}->{5222}) {
delete $self->{c2s_port}->{5222};
}
$self->{c2s_port}->{as_bind_addr($val)}++;
}
sub set_config_serverport {
my ($self, $val) = @_;
$self->{s2s_port} = {
as_bind_addr($val) => 1,
};
}
sub add_config_serverport {
my ($self, $val) = @_;
# necessary because the default in the constructor
# will be 5269 and subsequent binds would fail
if($self->{s2s_port} && ref($self->{s2s_port}) && exists $self->{s2s_port}->{5269}) {
delete $self->{s2s_port}->{5269};
}
$self->{s2s_port}->{as_bind_addr($val)}++;
}
sub set_config_adminport {
my ($self, $val) = @_;
$self->{admin_port} = {
as_bind_addr($val) => 1,
};
}
sub add_config_adminport {
my ($self, $val) = @_;
$self->{admin_port}->{as_bind_addr($val)}++;
}
sub set_config_intradomainlisten {
my ($self, $val) = @_;
$self->{cluster_listen} = {
$val => 1,
};
}
sub add_config_intradomainlisten {
my ($self, $val) = @_;
$self->{cluster_listen}->{$val}++;
}
sub set_config_pidfile {
my ($self, $val) = @_;
$self->{pid_file} = $val;
}
our %fake_peers;
sub set_fake_s2s_peer {
my ($self, $host, $ipendpt) = @_;
$fake_peers{$host} = $ipendpt;
}
sub fake_s2s_peer {
my ($self, $host) = @_;
return $fake_peers{$host};
}
sub set_config_casesensitive {
my ($self, $val) = @_;
$DJabberd::JID::CASE_SENSITIVE = as_bool($val);
}
# If anyone wishes to replace OldSSL
sub set_config_clientssltransport {
my ($self, $val) = @_;
eval("use $val");
croak("Can't use $val as ssl transport: $@") if($@);
$self->{cssl_class} = $val;
}
sub set_config_s2sservertransport {
my ($self, $val) = @_;
eval("use $val");
croak("Can't use $val as s2s transport: $@") if($@);
$self->{s2ss_class} = $val;
}
sub set_config_s2sclienttransport {
my ($self, $val) = @_;
eval("use $val");
croak("Can't use $val as s2s transport: $@") if($@);
$self->{s2sc_class} = $val;
}
sub client_ssl_class { return ( $_[0]->{cssl_class} || 'DJabberd::Connection::OldSSLClientIn') }
sub s2s_server_class { return ( $_[0]->{s2ss_class} || 'DJabberd::Connection::ServerIn') }
sub s2s_client_class { return ( $_[0]->{s2sc_class} || 'DJabberd::Connection::ServerOut') }
sub add_vhost {
my ($self, $vhost) = @_;
my $sname = lc $vhost->name;
if (my $existing = $self->{vhosts}{$sname}) {
croak("Can't set vhost with name '$sname'. Already exists in this server.")
if $existing != $vhost;
}
if ($vhost->server && $vhost->server != $self) {
croak("Vhost already has a server.");
}
$vhost->setup_default_plugins;
$self->{vhosts}{$sname} = $vhost;
$vhost->set_server($self);
}
# works as Server method or class method.
sub lookup_vhost {
my ($self, $hostname) = @_;
# look at all server objects in process
unless (ref $self) {
foreach my $server (values %DJabberd::server) {
my $vh = $server->lookup_vhost($hostname);
return $vh if $vh;
}
return 0;
}
# method on server object
foreach my $vhost (values %{$self->{vhosts}}) {
return $vhost
if ($vhost->handles_domain($hostname));
}
return 0;
}
# return the version of the spec we implement
sub spec_version {
my $self = shift;
return $self->{_spec_version} ||= DJabberd::StreamVersion->new("1.0");
}
my %obj_source; # refaddr -> file/linenumber
my %obj_living; # file/linenumber -> ct
use Scalar::Util qw(refaddr weaken);
use Data::Dumper;
sub dump_obj_stats {
print Dumper(\%obj_living);
my %class_ct;
foreach (values %obj_source) {
$class_ct{ref($_->[1])}++;
}
print Dumper(\%class_ct);
}
sub track_new_obj {
return unless $ENV{TRACKOBJ};
my ($class, $obj) = @_;
my $i = 0;
my $fileline;
while (!$fileline) {
$i++;
my ($pkg, $filename, $line, $subname) = caller($i);
next if $subname eq "new";
$fileline = "$filename/$line";
}
my $addr = refaddr($obj);
warn "New object $obj -- $fileline\n" if $ENV{TRACKOBJ};
$obj_source{$addr} = [$fileline, $obj];
weaken($obj_source{$addr}[1]);
$obj_living{$fileline}++;
dump_obj_stats() if $ENV{TRACKOBJ};
}
sub track_destroyed_obj {
return unless $ENV{TRACKOBJ};
my ($class, $obj) = @_;
my $addr = refaddr($obj);
my $fileline = $obj_source{$addr}->[0] or die "Where did $obj come from?";
delete $obj_source{$addr};
warn "Destroyed object $obj -- $fileline\n" if $ENV{TRACKOBJ};
$obj_living{$fileline}--;
dump_obj_stats() if $ENV{TRACKOBJ};
}
sub debug {
my $self = shift;
return unless $self->{debug};
printf STDERR @_;
}
sub run {
my $self = shift;
daemonize() if $self->{daemonize};
local $SIG{'PIPE'} = "IGNORE"; # handled manually
if ($self->{pid_file}) {
$logger->debug("Logging PID to file $self->{pid_file}");
open(PIDFILE,'>',$self->{pid_file}) or $logger->logdie("Can't open pidfile $self->{pid_file} for writing");
print PIDFILE "$$\n";
close(PIDFILE);
}
$self->start_c2s_server();
# {=s2soptional}
$self->start_s2s_server() if $self->{s2s_port};
$self->start_cluster_server() if $self->{cluster_listen};
$self->_start_servers($self->{admin_port}, "DJabberd::Connection::Admin") if $self->{admin_port};
DJabberd::Connection::Admin->on_startup;
Danga::Socket->EventLoop();
unlink($self->{pid_file}) if (-f $self->{pid_file});
}
# this will start up one server for each defined listening socket
sub _start_servers {
my ($self, $localaddrs, $class) = @_;
my @addrs;
if(ref($localaddrs) eq 'HASH') {
@addrs = sort keys %$localaddrs;
} else {
@addrs = ($localaddrs);
}
foreach my $localaddr (@addrs) {
$self->_start_server($localaddr, $class);
}
}
sub _start_server {
my ($self, $localaddr, $class) = @_;
# establish SERVER socket, bind and listen.
my $server;
my $not_tcp = 0;
if ($localaddr =~ m!^/!) {
$not_tcp = 1;
my @la = split(/\s/,$localaddr);
my $sock = ($#la > 0) ? $la[0] : $localaddr;
$server = IO::Socket::UNIX->new(Type => SOCK_STREAM,
Local => $sock,
Listen => 10)
or $logger->logdie("Error creating unix domain socket[$sock]: $@\n");
if($la[1] && $la[1] =~ /^([uUgG])\+(.+)$/) {
my @stat = stat($sock)
or $logger->logdie("Socket $sock doesn't actually exist!?: $@");
if(lc($1) eq 'g') {
my ($gn,undef,$gid,$mm) = getgrnam($2);
my $un = getpwuid($<);
$logger->debug("Need to chgrp $sock to $gn/$gid: $un should be in members($mm) or EUID[$<] == 0");
if($<==0 || grep{$_ eq $un}split('\s+',$mm)) {
chown(-1, $gid, $sock) or $logger->logdie("Error changing group: $@\n");
if(!($stat[2] & 020)) {
$logger->debug("Need to chmod $sock to 0770");
chmod(0770, $sock) or $logger->logdie("Error changing perms: $@\n");
}
} else {
$logger->logdie("Cannot chgrp as $un is not root and is not member of $gn\n");
}
} else {
my ($un,undef,$uid) = getpwnam($2);
chown($uid,$sock) or $logger->logdie("Error changing $sock owner: $@\n");;
}
}
} else {
# assume it's a port if there's no number after the (last) colon
unless ($localaddr =~ /:\d+$/) {
$localaddr = '[::]:'.$localaddr;
}
$logger->debug("Opening TCP listen socket on $localaddr");
$server = IO::Socket::INET6->new(LocalAddr => $localaddr,
Type => SOCK_STREAM,
Proto => IPPROTO_TCP,
Reuse => 1,
Listen => 10 )
or $logger->logdie("Error creating socket for $localaddr: $@\n");
my $success = $server->blocking(0);
unless (defined($success)) {
if ($^O eq 'MSWin32') {
# On Windows, we have to do this a bit differently
my $do = 1;
ioctl($server, 0x8004667E, \$do) or $logger->warn("Failed to make socket non-blocking: $!");
}
else {
$logger->warn("Failed to make socket non-blocking: $!")
}
}
}
# Not sure if I'm crazy or not, but I can't see in strace where/how
# Perl 5.6 sets blocking to 0 without this. In Perl 5.8, IO::Socket::INET
# obviously sets it from watching strace.
IO::Handle::blocking($server, 0);
my $accept_handler = sub {
local *__ANON__ = " Accept handler in ". __FILE__ ." on line ". __LINE__;
my $csock = $server->accept;
return unless $csock;
$self->debug("Listen child making a DJabberd::Connection for %d.\n", fileno($csock));
IO::Handle::blocking($csock, 0);
unless ($not_tcp) {
setsockopt($csock, IPPROTO_TCP, TCP_NODELAY, pack("l", 1)) or die;
}
if (my $client = eval { $class->new($csock, $self) }) {
$DJabberd::Stats::counter{connect}++;
if($not_tcp && $self->{ssl_port}->{$localaddr} && !$client->{ssl}) {
# This is ssl offloading via unix domain socket
$self->debug("Enabling fake SSL on Unix Socket $localaddr");
$client->{ssl} = -1;
}
$client->watch_read(1);
return;
} else {
$logger->error("Error creating new $class: $@");
}
};
Danga::Socket->AddOtherFds(fileno($server) => $accept_handler);
}
sub start_c2s_server {
my $self = shift;
$self->_start_servers($self->{c2s_port},
"DJabberd::Connection::ClientIn");
# usually setting OldSSL 1 in the config file
# the server would magically listen on port 5223
# however this conflicts a litte with the new
# multi socket capabilities so add a new keyword
# SSLPort which works like e.g. ClientPort and
# have a fallback for old configs here
if ($self->{old_ssl} || $self->{ssl_port}) {
if(!$self->{ssl_port}) {
$self->{ssl_port}->{5223}++;
}
$self->_start_servers($self->{ssl_port}, $self->client_ssl_class);
}
if ($self->{unixdomainsocket}) {
$self->_start_servers($self->{unixdomainsocket}, "DJabberd::Connection::ClientIn");
}
}
sub start_s2s_server {
my $self = shift;
$self->_start_servers($self->{s2s_port},
$self->s2s_server_class);
}
sub start_cluster_server {
my $self = shift;
$self->_start_servers($self->{cluster_listen},
"DJabberd::Connection::ClusterIn");
}
sub start_simple_server {
my ($self, $port) = @_;
eval "use DJabberd::Connection::SimpleIn; 1"
or die "Failed to load DJabberd::Connection::SimpleIn: $@\n";
$self->_start_server($port, "DJabberd::Connection::SimpleIn");
}
sub is_monitor_host {
my ($self, $host) = @_;
return $self->{monitor_host}{$host};
}
sub load_config {
my ($self, $arg) = @_;
if (ref $arg eq "SCALAR") {
$self->_load_config_ref($arg);
} else {
open (my $fh, $arg) or die "Couldn't open config file '$arg': $!\n";
my $slurp = do { local $/; <$fh>; };
$self->_load_config_ref(\$slurp);
}
}
sub _load_config_ref {
my ($self, $configref) = @_;
my $linenum = 0;
my $vhost; # current vhost in scope
my $plugin; # current plugin in scope
my @vhost_stack = ();
my $expand_var = sub {
my ($type, $key) = @_;
$type = uc $type;
if ($type eq "ENV") {
# expands ${ENV:KEY} on a line into $ENV{'KEY'} or dies if not defined
my $val = $ENV{$key};
die "Undefined environment variable '$key'\n" unless defined $val;
return $val;
}
die "Unknown variable type '$type'\n";
};
foreach my $line (split(/\n/, $$configref)) {
$linenum++;
$line =~ s/^\s+//;
next if $line =~ /^\#/;
$line =~ s/\s+$//;
next unless $line =~ /\S/;
eval {
# expand environment variables
$line =~ s/\$\{(\w+):(\w+)\}/$expand_var->($1, $2)/eg;
if ($line =~ /^(\w+)\s+(.+)/) {
my $pkey = $1;
my $key = lc $1;
my $val = $2;
my $inv = $plugin || $vhost || $self;
my $meth = "add_config_$key";
if ($inv->can($meth)) {
$inv->$meth($val);
return;
}
$meth = "set_config_$key";
if ($inv->can($meth)) {
$inv->$meth($val);
return;
}
$meth = "set_config__option";
if ($inv->can($meth)) {
$inv->$meth($key, $val);
return;
}
die "Unknown option '$pkey'\n";
}
if ($line =~ /<VHost\s+(\S+?)>/i) {
die "Can't configure a vhost in a vhost\n" if $vhost;
$vhost = DJabberd::VHost->new(server_name => $1);
$vhost->set_server($self);
return;
}
if ($line =~ m!</VHost>!i) {
die "Can't end a not-open vhost\n" unless $vhost;
die "Can't end a vhost with an open plugin\n" if $plugin;
die "Can't end a vhost with an open subdomain\n" if @vhost_stack;
$self->add_vhost($vhost);
$vhost = undef;
return;
}
if ($line =~ /<Subdomain\s+(\S+?)>/i) {
die "Subdomain blocks can only inside VHost\n" unless $vhost;
my $subdomain_name = $1.".".$vhost->server_name;
my $old_vhost = $vhost;
push @vhost_stack, $old_vhost;
$vhost = DJabberd::VHost->new(server_name => $subdomain_name);
$vhost->set_server($self);
# Automatically add the LocalVHosts delivery plugin so that these
# VHosts can talk to one another without S2S.
my $loaded = eval "use DJabberd::Delivery::LocalVHosts; 1;";
die "Failed to load LocalVHosts delivery plugin for subdomain" unless $loaded;
my $ld1 = DJabberd::Delivery::LocalVHosts->new(allowvhost => $subdomain_name);
my $ld2 = DJabberd::Delivery::LocalVHosts->new(allowvhost => $old_vhost->server_name);
$old_vhost->add_plugin($ld1);
$vhost->add_plugin($ld2);
return;
}
if ($line =~ m!</Subdomain>!i) {
die "Extraneous subdomain end\n" unless @vhost_stack;
$self->add_vhost($vhost);
$vhost = pop @vhost_stack;
return;
}
my $close_plugin = sub {
die "Can't end a not-open plugin\n" unless $plugin;
$plugin->finalize;
$vhost->add_plugin($plugin);
$plugin = undef;
};
if ($line =~ /<Plugin\s+([\w:]+?)(\s*\/)?>/i) {
my $class = $1;
my $immediate_close = $2;
die "Can't configure a plugin outside of a vhost config vhost\n" unless $vhost;
die "Can't configure a plugin inside of a plugin\n" if $plugin;
my $loaded = eval "use $class; 1;";
die "Failed to load plugin $class: $@" if $@;
$plugin = $class->new;
$close_plugin->() if $immediate_close;
return;
}
if ($line =~ m!</Plugin>!i) {
$close_plugin->();
return;
}
die "Syntax error: '$line'\n";
};
if ($@) {
die "Configuration error on line $linenum: $@\n";
}
}
}
sub daemonize {
my($pid, $sess_id, $i);
## Fork and exit parent
if ($pid = fork) { exit 0; }
## Detach ourselves from the terminal
Carp::croak("Cannot detach from controlling terminal")
unless $sess_id = POSIX::setsid();
## Prevent possibility of acquiring a controling terminal
$SIG{'HUP'} = 'IGNORE';
if ($pid = fork) { exit 0; }
## Change working directory
chdir "/";
## Clear file creation mask
umask 0;
## Close open file descriptors
close(STDIN);
close(STDOUT);
close(STDERR);
## Reopen stderr, stdout, stdin to /dev/null
open(STDIN, "+>/dev/null");
open(STDOUT, "+>&STDIN");
open(STDERR, "+>&STDIN");
}
# Local Variables:
# mode: perl
# c-basic-indent: 4
# indent-tabs-mode: nil
# End:
1;
__END__
=head1 NAME
DJabberd - scalable, extensible Jabber/XMPP server.
=head1 SYNOPSIS
$ djabberd --conf=config-file.conf --daemonize
=head1 DESCRIPTION
DJabberd was the answer to LiveJournal's Jabber (XMPP)
server needs. We needed:
=over 4
=item * good performance for tons of connected users
=item * ability to scale to multiple nodes
=item * ability to hook into LiveJournal at all places, not just auth
=back
Basically we wanted the swiss army knife of Jabber servers (think
qpsmtpd or mod_perl), but none existed in any language. While some
popular Jabber servers let us do pluggable auth, none let us get our
hands into roster storage, vcards, avatars, presence, etc.
So we made DJabberd. It's a Jabber server where almost everything
defers to hooks to be implemented by plugins. It does the core spec
itself (including SSL, StartTLS, server-to-server, etc), but it
doesn't come with any way to do authentication or storage or rosters,
etc. You'll need to go pick up a plugin to do those.
You should be able to plop DJabberd into your existing systems /
userbase with minimal pain. Just find a plugin that's close (if a
perfect match doesn't already exist!) and tweak.
DJabberd is event-based so we can have really low per-connection
memory requirements, smaller than is possible with a threaded jabber
server. Because of this, all plugins can operate asynchronously,
taking as long as they want to finish their work, or to decline to the
next handler. (in the meantime, while plugins wait on a response from
whatever they're talking to, the DJabberd event loop continues at full
speed) However, that's more work, so some plugins may choose to
operate synchronously, but they do so with the understanding that
those plugins will cause the whole server to get bogged down. If
you're running a Jabber server for 5 users, you may not care that the
SQLite authentication backend pauses your server for milliseconds at a
time, but on a site with hundreds of thousands of connections, that
wouldn't be acceptable. Watch out for those plugins.
=head1 HACKING
Read HookDocs.pm, read the major base classes (Authen, RosterStorage,
PresenceChecker), join the mailing list, ask questions ... we're here
to help. If you want more hooks, let us know! If we forgot one, it
doesn't hurt us to add more.
=head1 COPYRIGHT
This module is Copyright (c) 2006 Six Apart, Ltd.
All rights reserved.
You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the Perl README file.
=head1 WARRANTY
This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
=head1 WEBSITE
Visit:
http://danga.com/djabberd/
=head1 AUTHORS
Brad Fitzpatrick <[email protected]>
Artur Bergman <[email protected]>
Jonathan Steinert <[email protected]>