| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | DJabberd::Component::External - Interface to external plugins implementing JEP-0114 | ||
| 5 | |||
| 6 | =head1 DESCRIPTION | ||
| 7 | |||
| 8 | This component provides support for connecting external components that support the | ||
| 9 | Jabber Component Protocol specified in JEP-0114. Specify the TCP port that the external component | ||
| 10 | will connect on and the secret it will use to authenticate. These should match the equivalent | ||
| 11 | settings in the component's own configuration. | ||
| 12 | |||
| 13 | You can also specify a ListenAddr option, which specifies the IP address of the interface to | ||
| 14 | listen on. By default, we only listen on the loopback address, 127.0.0.1. Alternatively, | ||
| 15 | you can give a UNIX domain socket (an absolute path beginning with a slash) and leave out | ||
| 16 | the ListenPort setting to await a connection on a UNIX domain socket. Most components do not | ||
| 17 | support UNIX domain sockets, however. | ||
| 18 | |||
| 19 | Please note that this component only implements the "accept" variation of the protocol, | ||
| 20 | where DJabberd opens a listen socket and waits for the component to connect. The "connect" | ||
| 21 | variation, where the component waits for DJabberd to connect to it, is not supported. | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | <Plugin DJabberd::Component::External> | ||
| 26 | ListenPort 23534 | ||
| 27 | Secret somesecret | ||
| 28 | </Plugin> | ||
| 29 | |||
| 30 | =head1 LICENCE | ||
| 31 | |||
| 32 | Copyright 2006 Martin Atkins and Six Apart | ||
| 33 | |||
| 34 | This library is part of the Jabber server DJabberd. It can be modified and distributed | ||
| 35 | under the same terms as DJabberd itself. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | package DJabberd::Component::External; | ||
| 40 | |||
| 41 | use base 'DJabberd::Component'; | ||
| 42 | use strict; | ||
| 43 | use DJabberd::Log; | ||
| 44 | use DJabberd::Util qw(exml); | ||
| 45 | use DJabberd::Connection::ComponentIn; | ||
| 46 | use IO::Socket::UNIX; | ||
| 47 | use IO::Socket::INET; | ||
| 48 | use Socket qw(IPPROTO_TCP TCP_NODELAY SOL_SOCKET SOCK_STREAM); | ||
| 49 | |||
| 50 | our $logger = DJabberd::Log->get_logger(); | ||
| 51 | |||
| 52 | sub set_config_listenport { | ||
| 53 | my ($self, $port) = @_; | ||
| 54 | |||
| 55 | return $self->set_config_listenaddr($port); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub set_config_secret { | ||
| 59 | my ($self, $secret) = @_; | ||
| 60 | |||
| 61 | $self->{secret} = $secret; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub set_config_listenaddr { | ||
| 65 | my ($self, $addr) = @_; | ||
| 66 | |||
| 67 | $self->{listenaddr} = DJabberd::Util::as_bind_addr($addr); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub finalize { | ||
| 71 | my ($self) = @_; | ||
| 72 | |||
| 73 | $logger->logdie("No ListenPort specified for external component") unless $self->{listenaddr}; | ||
| 74 | $self->{listenaddr} = "127.0.0.1:".$self->{listenaddr} if $self->{listenaddr} =~ /^\d+$/; | ||
| 75 | |||
| 76 | $logger->logdie("No Secret specified for external component") unless $self->{secret}; | ||
| 77 | |||
| 78 | $self->SUPER::finalize; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub register { | ||
| 82 | my ($self, $vhost) = @_; | ||
| 83 | |||
| 84 | $self->SUPER::register($vhost); | ||
| 85 | |||
| 86 | $self->_start_listener(); | ||
| 87 | |||
| 88 | } | ||
| 89 | |||
| 90 | sub secret { | ||
| 91 | return $_[0]->{secret}; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub handle_component_disconnect { | ||
| 95 | my ($self, $connection) = @_; | ||
| 96 | |||
| 97 | if ($connection != $self->{connection}) { | ||
| 98 | $logger->warn("Got disconnection for the wrong connection. Something's screwy."); | ||
| 99 | return 0; | ||
| 100 | } | ||
| 101 | |||
| 102 | $logger->info("Component ".$self->domain." disconnected."); | ||
| 103 | |||
| 104 | $self->{connection} = undef; | ||
| 105 | $self->_start_listener(); # Re-open the listen port so the component can re-connect. | ||
| 106 | return 1; | ||
| 107 | } | ||
| 108 | |||
| 109 | # Stanza from the server to the component | ||
| 110 | sub handle_stanza { | ||
| 111 | my ($self, $vhost, $stanza) = @_; | ||
| 112 | |||
| 113 | # If the component is not connected, return Service Unavailable | ||
| 114 | unless ($self->{connection} && $self->{connection}->is_authenticated) { | ||
| 115 | $stanza->make_error_response('503', 'cancel', 'service-unavailable')->deliver($vhost); | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | $self->{connection}->send_stanza($stanza); | ||
| 120 | } | ||
| 121 | |||
| 122 | # Stanza from the component to the server | ||
| 123 | sub handle_component_stanza { | ||
| 124 | my ($self, $stanza) = @_; | ||
| 125 | |||
| 126 | if ($stanza->from_jid && $stanza->from_jid->domain eq $self->domain) { | ||
| 127 | $stanza->deliver($self->vhost); | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | $logger->warn("External component ".$self->domain." used bogus from address. Discarding stanza."); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _start_listener { | ||
| 135 | my ($self) = @_; | ||
| 136 | my $vhost = $self->vhost; | ||
| 137 | |||
| 138 | my $bindaddr = $self->{listenaddr}; | ||
| 139 | |||
| 140 | # FIXME: Maybe shouldn't duplicate all of this code out of DJabberd.pm. | ||
| 141 | |||
| 142 | my $server; | ||
| 143 | my $not_tcp = 0; | ||
| 144 | if ($bindaddr =~ m!^/!) { | ||
| 145 | $not_tcp = 1; | ||
| 146 | $server = IO::Socket::UNIX->new( | ||
| 147 | Type => SOCK_STREAM, | ||
| 148 | Local => $bindaddr, | ||
| 149 | Listen => 10 | ||
| 150 | ); | ||
| 151 | $logger->logdie("Error creating UNIX domain socket $bindaddr: $@") unless $server; | ||
| 152 | $logger->info("Started listener for component ".$self->domain." on UNIX domain socket $bindaddr"); | ||
| 153 | } else { | ||
| 154 | $server = IO::Socket::INET->new( | ||
| 155 | LocalAddr => $bindaddr, | ||
| 156 | Type => SOCK_STREAM, | ||
| 157 | Proto => IPPROTO_TCP, | ||
| 158 | Blocking => 0, | ||
| 159 | Reuse => 1, | ||
| 160 | Listen => 10 | ||
| 161 | ); | ||
| 162 | $logger->logdie("Error creating listen socket for <$bindaddr>: $@") unless $server; | ||
| 163 | $logger->info("Started listener for component ".$self->domain." on TCP socket <$bindaddr>"); | ||
| 164 | } | ||
| 165 | |||
| 166 | # Brad thinks this is necessary under Perl 5.6, and who am I to argue? | ||
| 167 | IO::Handle::blocking($server, 0); | ||
| 168 | |||
| 169 | $self->{listener} = $server; | ||
| 170 | |||
| 171 | my $accept_handler = sub { | ||
| 172 | my $csock = $server->accept; | ||
| 173 | return unless $csock; | ||
| 174 | |||
| 175 | $logger->debug("Accepting connection from component ".$self->domain); | ||
| 176 | |||
| 177 | IO::Handle::blocking($csock, 0); | ||
| 178 | unless ($not_tcp) { | ||
| 179 | setsockopt($csock, IPPROTO_TCP, TCP_NODELAY, pack("l", 1)) or $logger->logdie("Couldn't set TCP_NODELAY"); | ||
| 180 | } | ||
| 181 | |||
| 182 | my $connection = DJabberd::Connection::ComponentIn->new($csock, $vhost->server, $self); | ||
| 183 | $connection->watch_read(1); | ||
| 184 | $self->{connection} = $connection; | ||
| 185 | |||
| 186 | # We only need to support one connection at a time, so | ||
| 187 | # shut down the listen socket now to save resources. | ||
| 188 | $self->_stop_listener($self); | ||
| 189 | }; | ||
| 190 | |||
| 191 | Danga::Socket->AddOtherFds(fileno($server) => $accept_handler); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub _stop_listener { | ||
| 195 | my ($self) = @_; | ||
| 196 | |||
| 197 | return unless $self->{listener}; | ||
| 198 | $logger->info("Shutting down listener for component ".$self->domain); | ||
| 199 | $self->{listener} = undef if $self->{listener}->close(); | ||
| 200 | return $self->{listener} == undef; | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; |