| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | DJabberd::Connection::ComponentOut - JEP-0114 Client | ||
| 5 | |||
| 6 | =head1 INTRODUCTION | ||
| 7 | |||
| 8 | This class provides the "client" end (the initiating party) of a JEP-0114 Component Protocol | ||
| 9 | connection. | ||
| 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::ComponentOut; | ||
| 21 | use strict; | ||
| 22 | use base 'DJabberd::Connection'; | ||
| 23 | use DJabberd::Log; | ||
| 24 | use Digest::SHA1; | ||
| 25 | use Socket qw(PF_INET IPPROTO_TCP SOCK_STREAM); | ||
| 26 | |||
| 27 | our $logger = DJabberd::Log->get_logger(); | ||
| 28 | |||
| 29 | use fields ( | ||
| 30 | 'state', # Connection state ('disconnected', 'connecting', 'connected') | ||
| 31 | 'authenticated', # Whether we have completed the authentication handshake (boolean) | ||
| 32 | 'secret', # The secret used in the handshake with the server (a string) | ||
| 33 | 'connect_handlers', # array of coderefs to call when this connection is established | ||
| 34 | ); | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my ($class, %opts) = @_; | ||
| 38 | |||
| 39 | my $endpoint = delete($opts{endpoint}) or $logger->logdie("No endpoint specified"); | ||
| 40 | my $vhost = delete($opts{vhost}) or $logger->logdie("No vhost specified"); | ||
| 41 | my $secret = delete($opts{secret}) || ""; # FIXME: The secret can't currently be the number 0 :) | ||
| 42 | $logger->logdie("Invalid options ".join(',',keys %opts)) if %opts; | ||
| 43 | |||
| 44 | $logger->warning("No shared secret specified for component connection in $vhost") unless $secret; | ||
| 45 | |||
| 46 | my $server = $vhost->server; | ||
| 47 | |||
| 48 | $logger->debug("Making a $class connecting to ".$endpoint->addr.":".$endpoint->port); | ||
| 49 | |||
| 50 | my $sock; | ||
| 51 | socket $sock, PF_INET, SOCK_STREAM, IPPROTO_TCP; | ||
| 52 | unless ($sock && defined fileno($sock)) { | ||
| 53 | $logger->logdie("Failed to allocate socket"); | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | IO::Handle::blocking($sock, 0) or $logger->logdie("Failed to make socket non-blocking"); | ||
| 57 | connect $sock, Socket::sockaddr_in($endpoint->port, Socket::inet_aton($endpoint->addr)); | ||
| 58 | |||
| 59 | my $self = $class->SUPER::new($sock, $server); | ||
| 60 | $self->watch_write(1); | ||
| 61 | |||
| 62 | $self->set_vhost($vhost); | ||
| 63 | $self->{secret} = $secret; | ||
| 64 | $self->{authenticated} = 0; | ||
| 65 | $self->{state} = 'connecting'; | ||
| 66 | |||
| 67 | #$self->{cmpnt_handler} = $handler; | ||
| 68 | #Scalar::Util::weaken($self->{cmpnt_handler}); | ||
| 69 | |||
| 70 | $logger->debug("$class initialized"); | ||
| 71 | |||
| 72 | return $self; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub on_connected { | ||
| 76 | my ($self) = @_; | ||
| 77 | my $vhost = $self->{vhost}; | ||
| 78 | |||
| 79 | $logger->debug("Got connection"); | ||
| 80 | |||
| 81 | # JEP-0114 stipulates that we must put our server name | ||
| 82 | # in the "to" attribute. Some other components, notably the | ||
| 83 | # pyXXXt family of transports, seem to use the 'from' attribute | ||
| 84 | # instead, so maybe this will have to change when talking | ||
| 85 | # to some server implementations. We'll see. | ||
| 86 | $self->start_init_stream(to => $vhost->server_name, xmlns => $self->namespace); | ||
| 87 | $self->watch_read(1); | ||
| 88 | $self->_run_callback_list($self->{connect_handlers}); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub event_write { | ||
| 92 | my $self = shift; | ||
| 93 | |||
| 94 | if ($self->{state} eq "connecting") { | ||
| 95 | $self->{state} = "connected"; | ||
| 96 | $self->on_connected; | ||
| 97 | } else { | ||
| 98 | return $self->SUPER::event_write; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | sub namespace { | ||
| 103 | return "jabber:component:accept"; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub on_stream_start { | ||
| 107 | my DJabberd::Connection::ComponentOut $self = shift; | ||
| 108 | my $ss = shift; | ||
| 109 | return $self->close unless $ss->xmlns eq $self->namespace; # FIXME: should be stream error | ||
| 110 | |||
| 111 | # Once we get to this point, we've connected to the server, we've sent our <stream> | ||
| 112 | # open tag and then this callback tells us we've got the server's <stream> open tag. | ||
| 113 | # Now we need to send our handshake based on the id attribute the server has given us. | ||
| 114 | |||
| 115 | $logger->debug("Got stream start for server ".$ss->to); | ||
| 116 | |||
| 117 | $self->{in_stream} = 1; | ||
| 118 | |||
| 119 | my $correct_domain = $self->{vhost}->server_name; | ||
| 120 | |||
| 121 | my $stream_id = $ss->id; | ||
| 122 | my $handshake = lc(Digest::SHA1::sha1_hex($stream_id.$self->{secret})); | ||
| 123 | |||
| 124 | $logger->debug("I'm sending a handshake of $handshake based on a stream id of ".$stream_id." and the configured secret"); | ||
| 125 | |||
| 126 | $self->write('<handshake>'.$handshake.'</handshake>'); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub is_server { 1 } | ||
| 130 | |||
| 131 | sub is_authenticated { | ||
| 132 | return $_[0]->{authenticated}; | ||
| 133 | } | ||
| 134 | |||
| 135 | my %element2class = ( | ||
| 136 | "{jabber:component:accept}iq" => 'DJabberd::IQ', | ||
| 137 | "{jabber:component:accept}message" => 'DJabberd::Message', | ||
| 138 | "{jabber:component:accept}presence" => 'DJabberd::Presence', | ||
| 139 | ); | ||
| 140 | |||
| 141 | sub on_stanza_received { | ||
| 142 | my ($self, $node) = @_; | ||
| 143 | |||
| 144 | if ($self->xmllog->is_info) { | ||
| 145 | $self->log_incoming_data($node); | ||
| 146 | } | ||
| 147 | |||
| 148 | unless ($self->{authenticated}) { | ||
| 149 | my $type = $node->element; | ||
| 150 | |||
| 151 | if ($type eq '{urn:ietf:params:xml:ns:xmpp-tls}starttls') { | ||
| 152 | my $stanza = DJabberd::Stanza::StartTLS->downbless($node, $self); | ||
| 153 | $stanza->process($self); | ||
| 154 | return; | ||
| 155 | } | ||
| 156 | |||
| 157 | unless ($type eq '{jabber:component:accept}handshake') { | ||
| 158 | $logger->warn("Server sent $type stanza before handshake acknowledgement. Discarding."); | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | # Server sends us a handshake element (rather than a stream error) | ||
| 163 | # to indicate success. The contents are irrelevent. | ||
| 164 | |||
| 165 | $logger->debug("Handshake accepted. We are now authenticated."); | ||
| 166 | $self->{authenticated} = 1; | ||
| 167 | |||
| 168 | return; | ||
| 169 | } | ||
| 170 | |||
| 171 | my $class = $element2class{$node->element} or return $self->stream_error("unsupported-stanza-type"); | ||
| 172 | |||
| 173 | # same variable as $node, but down(specific)-classed. | ||
| 174 | my $stanza = $class->downbless($node, $self); | ||
| 175 | |||
| 176 | # at present, there are no filter hooks for component connections | ||
| 177 | # we assume that components trust their server. | ||
| 178 | # Also, we just pretent we're S2S here for hook purposes since we basically are, | ||
| 179 | # and S2S and Component delivery are mutually exclusive anyway. | ||
| 180 | |||
| 181 | $self->vhost->run_hook_chain(phase => "switch_incoming_server", | ||
| 182 | args => [ $stanza ], | ||
| 183 | methods => { | ||
| 184 | process => sub { $stanza->process($self) }, | ||
| 185 | deliver => sub { $stanza->deliver($self) }, | ||
| 186 | }, | ||
| 187 | fallback => sub { | ||
| 188 | $stanza->on_recv_from_server($self); | ||
| 189 | }); | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | sub add_connect_handler { | ||
| 194 | my ($self, $callback) = @_; | ||
| 195 | $self->{connect_handlers} ||= []; | ||
| 196 | push @{$self->{connect_handlers}}, $callback; | ||
| 197 | } | ||
| 198 | |||
| 199 | 1; |