DJabberd/Connection/ComponentIn.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 108 CC 9 SCAR 27.7
CC
9
Cov
0%
on_stanza_received
42.0
on_stream_start
12.0
close
6.0
CRAP 15.9
1  
2 =head1 NAME
3  
4 DJabberd::Connection::ComponentIn - JEP-0114 Server
5  
6 =head1 INTRODUCTION
7  
8 This class provides the connection and stream handling implementation for the
9 DJabberd component interface DJabberd::Component::External. It is of little interest on its own.
10  
11 =head1 LICENCE
12  
13 Copyright 2006 Martin Atkins and Six Apart
14  
15 This library is part of the Jabber server DJabberd. It can be modified and distributed
16 under the same terms as DJabberd itself.
17  
18 =cut
19  
20 package DJabberd::Connection::ComponentIn;
21 use strict;
22 use base 'DJabberd::Connection';
23 use DJabberd::Log;
24 use Digest::SHA1;
25  
26 our $logger = DJabberd::Log->get_logger();
27  
28 use fields (
29 'authenticated', # boolean - whether component has completed the authentication handshake
30 'cmpnt_handler', # reference to the Component::External object that we belong to
31 );
32  
33 sub new {
34 my ($class, $socket, $server, $handler) = @_;
35  
36 $logger->debug("Making a $class for fd ".fileno($socket));
37
38 my $self = $class->SUPER::new($socket, $server);
39  
40 $self->{authenticated} = 0;
41 $self->{cmpnt_handler} = $handler;
42 Scalar::Util::weaken($self->{cmpnt_handler});
43
44 return $self;
45 }
46  
47 sub close {
48 my $self = shift;
49 return if $self->{closed};
50
51 $self->{cmpnt_handler}->handle_component_disconnect($self);
52  
53 $self->SUPER::close;
54 }
55  
56 sub namespace {
57 return "jabber:component:accept";
58 }
59  
60 sub on_stream_start {
61 my DJabberd::Connection::ComponentIn $self = shift;
62 my $ss = shift;
63 return $self->close unless $ss->xmlns eq $self->namespace; # FIXME: should be stream error
64  
65 $logger->debug("Got stream start for component ".$ss->to);
66  
67 $self->{in_stream} = 1;
68  
69 my $correct_domain = $self->{cmpnt_handler}->domain;
70  
71 # JEP-0114 says the component should put its domain name in the "to" attribute,
72 # but the py*t family of transports seems to put it in "from" instead.
73 my $claimed_domain = $ss->to || $ss->from;
74  
75 # FIXME: This dies if the domain is wrong and it came from $ss->from
76 return $self->close_no_vhost($claimed_domain) unless ($claimed_domain eq $correct_domain);
77  
78 $self->set_vhost($self->{cmpnt_handler}->vhost);
79  
80 $self->start_stream_back($ss,
81 namespace => 'jabber:component:accept',
82 );
83 }
84  
85 sub is_server { 1 }
86  
87 sub is_authenticated {
88 return $_[0]->{authenticated};
89 }
90  
91 my %element2class = (
92 "{jabber:component:accept}iq" => 'DJabberd::IQ',
93 "{jabber:component:accept}message" => 'DJabberd::Message',
94 "{jabber:component:accept}presence" => 'DJabberd::Presence',
95 );
96  
97 sub on_stanza_received {
98 my ($self, $node) = @_;
99  
100 if ($self->xmllog->is_info) {
101 $self->log_incoming_data($node);
102 }
103  
104 unless ($self->{authenticated}) {
105 my $type = $node->element;
106
107 if ($type eq '{urn:ietf:params:xml:ns:xmpp-tls}starttls') {
108 my $stanza = DJabberd::Stanza::StartTLS->downbless($node, $self);
109 $stanza->process($self);
110 return;
111 }
112
113 unless ($type eq '{jabber:component:accept}handshake') {
114 $logger->warn("Component sent $type stanza before handshake. Discarding.");
115 return;
116 }
117
118 my $innards = $node->innards_as_xml();
119 $innards =~ s/^\s+//g;
120 $innards =~ s/\s+$//g;
121
122 # So what should the correct hash be?
123 # FIXME: This probably doesn't handle escaping/encodings properly,
124 # so make sure the stream ID and secret are US-ASCII and don't use XML
125 # reserved characters!
126 my $correct = lc(Digest::SHA1::sha1_hex($self->stream_id.$self->{cmpnt_handler}->secret));
127
128 if ($correct eq $innards) {
129 # DJabberd::Connection is allergic to elements without 'to' attributes, so just send this raw
130 $self->write('<handshake/>');
131 $self->{authenticated} = 1;
132 }
133 else {
134 $logger->warn("Got bad handshake from component. Disconnecting. ".$self->stream_id.$self->{cmpnt_handler}->secret);
135 return $self->stream_error('not-authorized', 'The supplied digest was invalid');
136 }
137
138 return;
139 }
140  
141 my $class = $element2class{$node->element} or return $self->stream_error("unsupported-stanza-type");
142  
143 # same variable as $node, but down(specific)-classed.
144 my $stanza = $class->downbless($node, $self);
145  
146 $self->{cmpnt_handler}->handle_component_stanza($stanza);
147 }
148  
149 1;