/usr/share/perl5/POE/Component/Jabber/XMPP.pm is in libpoe-component-jabber-perl 3.00-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package POE::Component::Jabber::XMPP;
use warnings;
use strict;
use 5.010;
use POE qw/ Wheel::ReadWrite /;
use POE::Component::SSLify qw/ Client_SSLify /;
use POE::Component::Jabber::Events;
use POE::Filter::XML;
use POE::Filter::XML::Node;
use POE::Filter::XML::NS qw/ :JABBER :IQ /;
use Digest::MD5 qw/ md5_hex /;
use MIME::Base64;
use Authen::SASL;
use base('POE::Component::Jabber::Protocol');
our $VERSION = '3.00';
sub get_version()
{
return '1.0';
}
sub get_xmlns()
{
return +NS_JABBER_CLIENT;
}
sub get_states()
{
return
[
'set_auth',
'init_input_handler',
'build_tls_wheel',
'challenge_response',
'binding',
'session_establish',
];
}
sub get_input_event()
{
return 'init_input_handler';
}
sub set_auth()
{
my ($kernel, $heap, $self, $mech) = @_[KERNEL, HEAP, OBJECT, ARG0];
my $config = $heap->config();
$self->{'challenge'} = Authen::SASL->new
(
mechanism => $mech,
callback =>
{
user => $config->{'username'},
pass => $config->{'password'},
}
);
my $node = POE::Filter::XML::Node->new('auth', ['xmlns', +NS_XMPP_SASL, 'mechanism', $mech]);
if ($mech eq 'PLAIN')
{
my $auth_str = '';
$auth_str .= "\0";
$auth_str .= $config->{'username'};
$auth_str .= "\0";
$auth_str .= $config->{'password'};
$node->appendText(encode_base64($auth_str));
}
$kernel->yield('output_handler', $node, 1);
return;
}
sub challenge_response()
{
my ($kernel, $heap, $self, $node) = @_[KERNEL, HEAP, OBJECT, ARG0];
my $config = $heap->config();
if ($config->{'debug'}) {
$heap->debug_message("Server sent a challenge. Decoded Challenge:\n".
decode_base64($node->textContent()));
}
my $sasl = $self->{'challenge'};
my $conn = $sasl->client_new('xmpp', $config->{'hostname'});
$conn->client_start();
my $step = $conn->client_step(decode_base64($node->textContent()));
$step ||= '';
if ($config->{'debug'}) {
$heap->debug_message("Decoded Response:\n$step");
}
$step =~ s/\s+//go;
$step = encode_base64($step);
$step =~ s/\s+//go;
my $response = POE::Filter::XML::Node->new('response', ['xmlns', +NS_XMPP_SASL]);
$response->appendText($step);
$kernel->yield('output_handler', $response, 1);
return;
}
sub init_input_handler()
{
my ($kernel, $heap, $self, $node) = @_[KERNEL, HEAP, OBJECT, ARG0];
my $attrs = $node->getAttributes();
my $config = $heap->config();
my $name = $node->nodeName();
if ($config->{'debug'})
{
$heap->debug_message("Recd: ".$node->toString());
}
if(exists($attrs->{'id'}))
{
my $pending = $heap->pending();
if(defined($pending->{$attrs->{'id'}}))
{
my $array = delete $pending->{$attrs->{'id'}};
$kernel->post($array->[0], $array->[1], $node);
return;
}
}
given($name)
{
when ('stream:stream')
{
$self->{'sid'} = $attrs->{'id'};
}
when ('challenge')
{
$kernel->yield('challenge_response', $node);
}
when ('failure')
{
$heap->debug_message('SASL Negotiation Failed');
$kernel->yield('shutdown');
$kernel->post($heap->events(), +PCJ_AUTHFAIL);
}
when ('stream:features')
{
given(my $clist = $node->getChildrenHash())
{
when ('starttls')
{
my $starttls = POE::Filter::XML::Node->new('starttls', ['xmlns', +NS_XMPP_TLS]);
$kernel->yield('output_handler', $starttls, 1);
$kernel->post($heap->events(), +PCJ_SSLNEGOTIATE);
}
when('mechanisms')
{
$self->{'MECHANISMS'} = 1;
foreach($clist->{'mechanisms'}->[0]->getChildrenByTagName('*'))
{
when($_->textContent() eq 'DIGEST-MD5' or $_->textContent() eq 'PLAIN')
{
$kernel->yield('set_auth', $_->textContent());
$kernel->post($heap->events(), +PCJ_AUTHNEGOTIATE);
return;
}
}
$heap->debug_message('Unknown mechanism: '.$node->toString());
$kernel->yield('shutdown');
$kernel->post($heap->events(), +PCJ_AUTHFAIL);
}
when('bind')
{
my $iq = POE::Filter::XML::Node->new('iq', ['type', +IQ_SET]);
$iq->appendChild('bind', ['xmlns', +NS_XMPP_BIND])
->appendChild('resource')
->appendText($config->{'resource'});
$self->{'STARTSESSION'} = 1 if exists($clist->{'session'});
$kernel->yield('return_to_sender', 'binding', $iq);
$kernel->post($heap->events(), +PCJ_BINDNEGOTIATE);
}
default
{
# If we get here, it means the server has decided TLS isn't
# necessary, or that it is a non-compliant server and has skipped
# SASL negotition. Check for MECHANISMS flag. If it is present then
# we are finished with connection initialization.
#
# See http://www.xmpp.org/rfcs/rfc3920.html for more info
if($self->{'MECHANISMS'})
{
$heap->relinquish_states();
$kernel->post(
$heap->events(),
+PCJ_READY);
} else {
$heap->debug_message('Non-compliant server implementation! '.
'SASL negotiation not initiated.');
$kernel->yield('shutdown');
$kernel->post($heap->events(), +PCJ_AUTHFAIL);
}
}
}
}
when ('proceed')
{
$kernel->yield('build_tls_wheel');
}
when('success')
{
$kernel->yield('initiate_stream');
$kernel->post($heap->events(), +PCJ_AUTHSUCCESS);
}
}
return;
}
sub binding()
{
my ($kernel, $heap, $self, $node) = @_[KERNEL, HEAP, OBJECT, ARG0];
my $attr = $node->getAttribute('type');
my $config = $heap->config();
given($attr)
{
when(+IQ_RESULT)
{
if($self->{'STARTSESSION'})
{
my $iq = POE::Filter::XML::Node->new('iq', ['type', +IQ_SET]);
$iq->appendChild('session', ['xmlns', +NS_XMPP_SESSION]);
$kernel->yield('return_to_sender', 'session_establish', $iq);
$kernel->post($heap->events(), +PCJ_BINDSUCCESS);
$kernel->post(
$heap->events(),
+PCJ_SESSIONNEGOTIATE);
} else {
$heap->relinquish_states();
$kernel->post($heap->events(), +PCJ_BINDSUCCESS);
$kernel->post($heap->events(), +PCJ_READY);
}
$heap->jid($node->getSingleChildByTagName('bind')->getSingleChildByTagName('jid')->textContent());
}
when(+IQ_ERROR)
{
my $error = $node->getSingleChildByTagName('error');
my $type = $error->getAttribute('type');
given($type)
{
when('modify')
{
my $iq = POE::Filter::XML::Node->new('iq', ['type', +IQ_SET]);
$iq->appendChild('bind', ['xmlns', +NS_XMPP_BIND])
->appendChild('resource')
->appendText(md5_hex(time().rand().$$.rand().$^T.rand()));
$kernel->yield('return_to_sender', 'binding', $iq);
}
when('cancel')
{
my $clist = $error->getChildrenHash();
if(exists($clist->{'conflict'}))
{
my $iq = POE::Filter::XML::Node->new('iq', ['type', +IQ_SET]);
$iq->appendChild('bind', ['xmlns', +NS_XMPP_BIND])
->appendChild('resource')
->appendText(md5_hex(time().rand().$$.rand().$^T.rand()));
$kernel->yield('return_to_sender', 'binding', $iq);
} else {
$heap->debug_message('Unable to BIND, yet binding required: '.
$node->toString());
$kernel->yield('shutdown');
$kernel->post($heap->events(), +PCJ_BINDFAIL);
}
}
}
}
}
return;
}
sub session_establish()
{
my ($kernel, $heap, $node) = @_[KERNEL, HEAP, ARG0];
my $attr = $node->getAttribute('type');
my $config = $heap->config();
given($attr)
{
when(+IQ_RESULT)
{
$heap->relinquish_states();
$kernel->post($heap->events(), +PCJ_SESSIONSUCCESS);
$kernel->post($heap->events(), +PCJ_READY);
}
when(+IQ_ERROR)
{
$heap->debug_message('Unable to intiate SESSION, yet session required');
$heap->debug_message($node->toString());
$kernel->yield('shutdown');
$kernel->post($heap->events(), +PCJ_SESSIONFAIL);
}
}
return;
}
sub build_tls_wheel()
{
my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
$heap->wheel(undef);
eval
{
$heap->sock(Client_SSLify($heap->sock()));
};
if($@)
{
if($self->{'SSLTRIES'} > 3)
{
$heap->debug_message('Unable to negotiate SSL: '. $@);
$self->{'SSLTRIES'} = 0;
$kernel->post($heap->events(), +PCJ_SSLFAIL, $@);
} else {
$self->{'SSLTRIES'}++;
$kernel->yield('build_tls_wheel');
}
} else {
$heap->wheel(POE::Wheel::ReadWrite->new
(
'Handle' => $heap->sock(),
'Filter' => POE::Filter::XML->new(),
'InputEvent' => 'input_handler',
'ErrorEvent' => 'server_error',
'FlushedEvent' => 'flushed',
));
$kernel->yield('initiate_stream');
$kernel->post($heap->events(), +PCJ_SSLSUCCESS);
}
return;
}
1;
__END__
=pod
=head1 NAME
POE::Component::Jabber::XMPP - connect using the XMPP Jabber protocol
=head1 SYNOPSIS
This is a Protocol implementation for the specifics in the XMPP protocol during
connection initialization.
=head1 DESCRIPTION
PCJ::XMPP provides all the mechanisms to negotiate TLS, SASL, resource binding,
and session negotiation that PCJ needs to successfully establish an XMPP
connection. In essence, it implements XMPP Core and a smidgeon of XMPP IM.
=head1 METHODS
Please see PCJ::Protocol for what methods this class supports.
=head1 EVENTS
Listed are the exported events that make their way into the PCJ session:
=over 2
=item set_auth
This handles the initial SASL authentication portion of the XMPP connection.
=item init_input_handler
This is our entry point. This is what PCJ uses to deliver events to us.
=item build_tls_wheel
If TLS is required by the server, this is where that negotiation process
happens.
=item challenge_response
This handles the subsequent SASL authentication steps.
=item binding
This handles the resource binding
=item session_establish
This handles session binding.
=back
=head1 NOTES AND BUGS
Currently, only DIGEST-MD5 and PLAIN SASL mechanisms are supported. Server
implementations are free to include more strigent mechanisms, but these are the
bare minimum required. (And PLAIN isn't /really/ allowed by the spec, but it is
included because it was a requested feature)
The underlying backend has changed this release to now use a new Node
implementation based on XML::LibXML::Element. Please see POE::Filter::XML::Node
documentation for the relevant API changes.
=head1 AUTHOR
Copyright (c) 2003-2009 Nicholas Perez. Distributed under the GPL.
=cut
|