DJabberd/IQ.pm

filter: Statementstmt 6.4% 21 / 326 Branchbran 0.0% 0 / 120 Conditioncond 0.0% 0 / 56 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 10.0% 6 / 60 Podpod 0.0% 0 / 25 totaltotal 4.3% 27 / 623 CC 87 SCAR 34.8
CC
87
Cov
4%
process_iq_setroster
110.0
process_iq_setregister
90.0
process_iq_getauth
90.0
CRAP 32.6
« DJabberd/Connection/Admin.pm ↑ Summary DJabberd/Stanza/SASL.pm »
1 package DJabberd::IQ;
2 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use base qw(DJabberd::Stanza);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use DJabberd::Util qw(exml);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use DJabberd::Roster;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 ▶use DJabberd::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 our $logger = DJabberd::Log->get_logger();
10  
11 0 ▶sub on_recv_from_client {
Subroutine on_recv_from_client: not called CC=3 SCAR=24.8
Pod undocumented
12 0 my ($self, $conn) = @_;
13  
14 0 my $to = $self->to_jid;
15 0 ▶ if (! $to || $conn->vhost->uses_jid($to)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $to or $conn->vhost->uses_jid($to)) 0 0 0
Condition0%0/3
not $to or $conn->vhost->uses_jid($to)
l!l&&r!l&&!r
000
MC/DC0%0/2
not $to or $conn->vhost->uses_jid($to)
not $to $conn->vhost->uses_jid($to)
Truth table0%0/3
not(A) or B
A= not $toB= $conn->vhost->uses_jid($to)
ABresult
1X1
011
000
16 0 $self->process($conn);
17 0 return;
18 }
19  
20 0 $self->deliver;
21 }
22  
23 20 my $iq_handler = {
24 'get-{jabber:iq:roster}query' => \&process_iq_getroster,
25 'set-{jabber:iq:roster}query' => \&process_iq_setroster,
26 'get-{jabber:iq:auth}query' => \&process_iq_getauth,
27 'set-{jabber:iq:auth}query' => \&process_iq_setauth,
28 'set-{urn:ietf:params:xml:ns:xmpp-session}session' => \&process_iq_session,
29 'set-{urn:ietf:params:xml:ns:xmpp-bind}bind' => \&process_iq_bind,
30 'get-{http://jabber.org/protocol/disco#info}query' => \&process_iq_disco_info_query,
31 'get-{http://jabber.org/protocol/disco#items}query' => \&process_iq_disco_items_query,
32 'get-{jabber:iq:register}query' => \&process_iq_getregister,
33 'set-{jabber:iq:register}query' => \&process_iq_setregister,
34 'set-{djabberd:test}query' => \&process_iq_set_djabberd_test,
35 };
36  
37 # DO NOT OVERRIDE THIS
38 0 ▶sub process {
Subroutine process: not called CC=3 SCAR=24.8
Pod undocumented
39 0 my DJabberd::IQ $self = shift;
40 0 my $conn = shift;
41  
42 # FIXME: handle 'result'/'error' IQs from when we send IQs
43 # out, like in roster pushes
44  
45 # Trillian Jabber 3.1 is stupid and sends a lot of IQs (but non-important ones)
46 # without ids. If we respond to them (also without ids, or with id='', rather),
47 # then Trillian crashes. So let's just ignore them.
48 0 ▶ return unless defined($self->id) && length($self->id);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->id and length $self->id 0 0 0
Condition0%0/3
defined $self->id and length $self->id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->id and length $self->id
defined $self->id length $self->id
Truth table0%0/3
A and B
A= defined $self->idB= length $self->id
ABresult
0X0
100
111
49  
50 $conn->vhost->run_hook_chain(phase => "c2s-iq",
51 args => [ $self ],
52 0 ▶ fallback => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
53 0 my $sig = $self->signature;
54 0 my $meth = $iq_handler->{$sig};
55 0 ▶ unless ($meth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meth) 0 0 0
56 0 $self->send_error(
57 qq{<error type='cancel'>}.
58 qq{<feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>}.
59 qq{<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='en'>}.
60 qq{This feature is not implemented yet in DJabberd.}.
61 qq{</text>}.
62 qq{</error>}
63 );
64 0 $logger->warn("Unknown IQ packet: $sig");
65 0 return;
66 }
67  
68 0 $DJabberd::Stats::counter{"InIQ:$sig"}++;
69 0 $meth->($conn, $self);
70 0 });
71 }
72  
73 0 ▶sub signature {
Subroutine signature: not called CC=2 SCAR=17.9
Pod undocumented
74 0 my $iq = shift;
75 0 my $fc = $iq->first_element;
76 # FIXME: should signature ever get called on a bogus IQ packet?
77 0 ▶ return $iq->type . "-" . ($fc ? $fc->element : "(BOGUS)");
Branch: 1 branch
BranchTrueFalseTotal
$fc ? : 0 0 0
78 }
79  
80 0 ▶sub send_result {
Subroutine send_result: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my DJabberd::IQ $self = shift;
82 0 $self->send_reply("result");
83 }
84  
85 0 ▶sub send_error {
Subroutine send_error: not called CC=2 SCAR=17.9
Pod undocumented
86 0 my DJabberd::IQ $self = shift;
87 0 ▶ my $raw = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
88 0 $self->send_reply("error", $self->innards_as_xml . "\n" . $raw);
89 }
90  
91 # caller must send well-formed XML (but we do the wrapping element)
92 0 ▶sub send_result_raw {
Subroutine send_result_raw: not called CC=1 SCAR=6.9
Pod undocumented
93 0 my DJabberd::IQ $self = shift;
94 0 my $raw = shift;
95 0 return $self->send_reply("result", $raw);
96 }
97  
98 0 ▶sub send_reply {
Subroutine send_reply: not called CC=5 SCAR=34.0
Pod undocumented
99 0 my DJabberd::IQ $self = shift;
100 0 my ($type, $raw) = @_;
101  
102 my $conn = $self->{connection}
103 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $conn = $self->{"connection"} or return 0 0 0
104  
105 0 ▶ $raw ||= "";
Condition0%0/2
$raw ||= ""
l!l
00
MC/DC0%0/1
$raw ||= ""
$raw
Truth table0%0/2
A
A= $raw
Aresult
11
00
106 0 my $id = $self->id;
107 0 my $bj = $conn->bound_jid;
108 0 my $from_jid = $self->to;
109 0 ▶ my $to = $bj ? (" to='" . $bj->as_string_exml . "'") : "";
Branch: 1 branch
BranchTrueFalseTotal
$bj ? : 0 0 0
110 0 ▶ my $from = $from_jid ? (" from='" . $from_jid . "'") : "";
Branch: 1 branch
BranchTrueFalseTotal
$from_jid ? : 0 0 0
111 0 my $xml = qq{<iq$to$from type='$type' id='$id'>$raw</iq>};
112 0 $conn->xmllog->info($xml);
113 0 $conn->write(\$xml);
114 }
115  
116 0 ▶sub process_iq_disco_info_query {
Subroutine process_iq_disco_info_query: not called CC=4 SCAR=30.0
Pod undocumented
117 0 my ($conn, $iq) = @_;
118  
119 # Trillian, again, is fucking stupid and crashes on just
120 # about anything its homemade XML parser doesn't like.
121 # so ignore it when it asks for this, just never giving
122 # it a reply.
123 0 ▶ if ($conn->vhost->quirksmode && $iq->id =~ /^trill_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn->vhost->quirksmode and $iq->id =~ /^trill_/) 0 0 0
Condition0%0/3
$conn->vhost->quirksmode and $iq->id =~ /^trill_/
!ll&&!rl&&r
000
MC/DC0%0/2
$conn->vhost->quirksmode and $iq->id =~ /^trill_/
$conn->vhost->quirksmode $iq->id =~ /^trill_/
Truth table0%0/3
A and B
A= $conn->vhost->quirksmodeB= $iq->id =~ /^trill_/
ABresult
0X0
100
111
124 0 return;
125 }
126  
127 # TODO: these can be sent back to another server I believe -- sky
128  
129 # TODO: Here we need to figure out what identities we have and
130 # capabilities we have
131 0 my $xml;
132 0 $xml = qq{<query xmlns='http://jabber.org/protocol/disco#info'>};
133 0 $xml .= qq{<identity category='server' type='im' name='djabberd'/>};
134  
135 0 foreach my $cap ('http://jabber.org/protocol/disco#info',
136 $conn->vhost->features)
137 {
138 0 $xml .= "<feature var='$cap'/>";
139 }
140 0 $xml .= qq{</query>};
141  
142 0 $iq->send_reply('result', $xml);
143 }
144  
145 0 ▶sub process_iq_disco_items_query {
Subroutine process_iq_disco_items_query: not called CC=2 SCAR=17.9
Pod undocumented
146 0 my ($conn, $iq) = @_;
147  
148 0 my $vhost = $conn->vhost;
149  
150 0 ▶ my $items = $vhost ? $vhost->child_services : {};
Branch: 1 branch
BranchTrueFalseTotal
$vhost ? : 0 0 0
151  
152 my $xml = qq{<query xmlns='http://jabber.org/protocol/disco#items'>}.
153 0 join('', map({ "<item jid='".exml($_)."' name='".exml($items->{$_})."' />" } keys %$items)).
154 qq{</query>};
155  
156 0 $iq->send_reply('result', $xml);
157 }
158  
159 0 ▶sub process_iq_getroster {
Subroutine process_iq_getroster: not called CC=2 SCAR=17.9
Pod undocumented
160 0 my ($conn, $iq) = @_;
161  
162 0 ▶ my $send_roster = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
163 0 my $roster = shift;
164 0 $logger->info("Sending roster to conn $conn->{id}");
165 0 $iq->send_result_raw($roster->as_xml);
166  
167 # JIDs who want to subscribe to us, since we were offline
168 0 foreach my $jid (map { $_->jid }
169 0 grep { $_->subscription->pending_in }
170 $roster->items) {
171 0 my $subpkt = DJabberd::Presence->make_subscribe(to => $conn->bound_jid,
172 from => $jid);
173 # already in roster as pendin, we've already processed it,
174 # so just deliver it (or queue it) so user can reply with
175 # subscribed/unsubscribed:
176 0 $conn->note_pend_in_subscription($subpkt);
177 }
178 0 };
179  
180 # need to be authenticated to request a roster.
181 0 my $bj = $conn->bound_jid;
182 0 ▶ unless ($bj) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bj) 0 0 0
183 0 $iq->send_error(
184 qq{<error type='auth'>}.
185 qq{<not-authorized xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>}.
186 qq{<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='en'>}.
187 qq{You need to be authenticated before requesting a roster.}.
188 qq{</text>}.
189 qq{</error>}
190 );
191 0 return;
192 }
193  
194 # {=getting-roster-on-login}
195 0 $conn->set_requested_roster(1);
196  
197 $conn->vhost->get_roster($bj,
198 on_success => $send_roster,
199 0 ▶ on_fail => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
200 0 $send_roster->(DJabberd::Roster->new);
201 0 });
202  
203 0 return 1;
204 }
205  
206 0 ▶sub process_iq_setroster {
Subroutine process_iq_setroster: not called CC=10 SCAR=47.0
Pod undocumented
207 0 my ($conn, $iq) = @_;
208  
209 0 my $item = $iq->query->first_element;
210 0 ▶ unless ($item && $item->element eq "{jabber:iq:roster}item") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item and $item->element eq "{jabber:iq:roster}item") 0 0 0
Condition0%0/3
$item and $item->element eq "{jabber:iq:roster}item"
!ll&&!rl&&r
000
MC/DC0%0/2
$item and $item->element eq "{jabber:iq:roster}item"
$item $item->element eq "{jabber:iq:roster}item"
Truth table0%0/3
A and B
A= $itemB= $item->element eq "{jabber:iq:roster}item"
ABresult
0X0
100
111
211 0 $iq->send_error( # TODO make this error proper
212 qq{<error type='error-type'>}.
213 qq{<not-authorized xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>}.
214 qq{<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='langcode'>}.
215 qq{You need to be authenticated before requesting a roster.}.
216 qq{</text>}.
217 qq{</error>}
218 );
219 0 return;
220 }
221  
222 # {=xmpp-ip-7.6-must-ignore-subscription-values}
223 0 ▶ my $subattr = $item->attr('{}subscription') || "";
Condition0%0/2
$item->attr('{}subscription') || ''
l!l
00
MC/DC0%0/1
$item->attr('{}subscription') || ''
$item->attr('{}subscription')
Truth table0%0/2
A
A= $item->attr('{}subscription')
Aresult
11
00
224 0 ▶ my $removing = $subattr eq "remove" ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$subattr eq 'remove' ? : 0 0 0
225  
226 0 ▶ my $jid = $item->attr("{}jid")
Branch: 1 branch
BranchTrueFalseTotal
my $jid = $item->attr("{}jid") or return $iq->send_error("<error type='error-type'><not-authorized xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/><text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='langcode'>You need to be authenticated before requesting a roster.</text></error>") 0 0 0
227 or return $iq->send_error( # TODO Yeah, this one too
228 qq{<error type='error-type'>}.
229 qq{<not-authorized xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>}.
230 qq{<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='langcode'>}.
231 qq{You need to be authenticated before requesting a roster.}.
232 qq{</text>}.
233 qq{</error>}
234 );
235  
236 0 my $name = $item->attr("{}name");
237  
238 # find list of group names to add/update. can ignore
239 # if we're just removing.
240 0 my @groups; # scalars of names
241 0 ▶ unless ($removing) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($removing) 0 0 0
242 0 foreach my $ele ($item->children_elements) {
243 0 ▶ next unless $ele->element eq "{jabber:iq:roster}group";
Branch: 1 branch
BranchTrueFalseTotal
unless $ele->element eq "{jabber:iq:roster}group" 0 0 0
244 0 push @groups, $ele->first_child;
245 }
246 }
247  
248 0 my $ritem = DJabberd::RosterItem->new(jid => $jid,
249 name => $name,
250 remove => $removing,
251 groups => \@groups,
252 );
253  
254 # TODO if ($removing), send unsubscribe/unsubscribed presence
255 # stanzas. See RFC3921 8.6
256  
257 # {=add-item-to-roster}
258 0 ▶ my $phase = $removing ? "RosterRemoveItem" : "RosterAddUpdateItem";
Branch: 1 branch
BranchTrueFalseTotal
$removing ? : 0 0 0
259 $conn->vhost->run_hook_chain(phase => $phase,
260 args => [ $conn->bound_jid, $ritem ],
261 methods => {
262 0 ▶ done => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
263 0 my ($self, $ritem_final) = @_;
264  
265 # the RosterRemoveItem isn't required to return the final item
266 0 ▶ $ritem_final = $ritem if $removing;
Branch: 1 branch
BranchTrueFalseTotal
if $removing 0 0 0
267  
268 0 $iq->send_result;
269 0 $conn->vhost->roster_push($conn->bound_jid, $ritem_final);
270  
271 # TODO: section 8.6: must send a
272 # bunch of presence
273 # unsubscribe/unsubscribed messages
274 },
275 0 ▶ error => sub { # TODO What sort of error stat is being hit here?
Subroutine __ANON__: not called CC=1 SCAR=6.9
276 0 $iq->send_error;
277 },
278 },
279 0 ▶ fallback => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
280 0 ▶ if ($removing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($removing) { } 0 0 0
281 # NOTE: we used to send an error here, but clients get
282 # out of sync and we need to let them think a delete
283 # happened even if it didn't.
284 0 $iq->send_result;
285 } else { # TODO ACK, This one as well
286 0 $iq->send_error;
287 }
288 0 });
289  
290 0 return 1;
291 }
292  
293 0 ▶sub process_iq_getregister {
Subroutine process_iq_getregister: not called CC=3 SCAR=24.8
Pod undocumented
294 0 my ($conn, $iq) = @_;
295  
296 # If the entity is not already registered and the host supports
297 # In-Band Registration, the host MUST inform the entity of the
298 # required registration fields. If the host does not support
299 # In-Band Registration, it MUST return a <service-unavailable/>
300 # error. If the host is redirecting registration requests to some
301 # other medium (e.g., a website), it MAY return an <instructions/>
302 # element only, as shown in the Redirection section of this
303 # document.
304 0 my $vhost = $conn->vhost;
305 0 ▶ unless ($vhost->allow_inband_registration) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vhost->allow_inband_registration) 0 0 0
306 # MUST return a <service-unavailable/>
307 0 $iq->send_error(
308 qq{<error type='cancel' code='503'>}.
309 qq{<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>}.
310 qq{<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='en'>}.
311 qq{In-Band registration is not supported by this server's configuration.}.
312 qq{</text>}.
313 qq{</error>}
314 );
315 0 return;
316 }
317  
318 # if authenticated, give them existing login info:
319 0 ▶ if (my $jid = $conn->bound_jid) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $jid = $conn->bound_jid) 0 0 0
320  
321 0 my $password = 0 ? "<password></password>" : ""; # TODO
322 0 my $username = $jid->node;
323 0 $iq->send_result_raw(qq{<query xmlns='jabber:iq:register'>
324 <registered/>
325 <username>$username</username>
326 $password
327 </query>});
328 0 return;
329 }
330  
331 # not authenticated, ask for their required fields
332 # NOTE: we send_result_raw here, which just writes, so they don't
333 # need to be an available resource (since they're not even authed
334 # yet) for this to work. that's like most things in IQ anyway.
335 0 $iq->send_result_raw(qq{<query xmlns='jabber:iq:register'>
336 <instructions>
337 Choose a username and password for use with this service.
338 </instructions>
339 <username/>
340 <password/>
341 </query>});
342 }
343  
344 0 ▶sub process_iq_setregister {
Subroutine process_iq_setregister: not called CC=9 SCAR=45.0
Pod undocumented
345 0 my ($conn, $iq) = @_;
346  
347 0 my $vhost = $conn->vhost;
348 0 ▶ unless ($vhost->allow_inband_registration) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vhost->allow_inband_registration) 0 0 0
349 # MUST return a <service-unavailable/>
350 0 $iq->send_error(
351 qq{<error type='cancel'>}.
352 qq{<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>}.
353 qq{<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='en'>}.
354 qq{In-Band registration is not supported by this server\'s configuration.}.
355 qq{</text>}.
356 qq{</error>}
357 );
358 0 return;
359 }
360  
361 0 my $bjid = $conn->bound_jid;
362  
363 # remove (cancel) support
364 0 my $item = $iq->query->first_element;
365 0 ▶ if ($item && $item->element eq "{jabber:iq:register}remove") {
Branch: 1 branch
BranchTrueFalseTotal
if ($item and $item->element eq "{jabber:iq:register}remove") 0 0 0
Condition0%0/3
$item and $item->element eq "{jabber:iq:register}remove"
!ll&&!rl&&r
000
MC/DC0%0/2
$item and $item->element eq "{jabber:iq:register}remove"
$item $item->element eq "{jabber:iq:register}remove"
Truth table0%0/3
A and B
A= $itemB= $item->element eq "{jabber:iq:register}remove"
ABresult
0X0
100
111
366 0 ▶ if ($bjid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bjid) { } 0 0 0
367 0 ▶ my $rosterwipe = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
368 $vhost->run_hook_chain(phase => "RosterWipe",
369 args => [ $bjid ],
370 methods => {
371 0 ▶ done => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
372 0 $iq->send_result;
373 0 $conn->stream_error("not-authorized");
374 },
375 0 });
376 0 };
377  
378 $vhost->run_hook_chain(phase => "UnregisterJID",
379 args => [ username => $bjid->node, conn => $conn ],
380 methods => {
381 0 ▶ deleted => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
382 0 $rosterwipe->();
383 },
384 0 ▶ notfound => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
385 0 warn "notfound.\n";
386 0 return $iq->send_error;
387 },
388 0 ▶ error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
389 0 return $iq->send_error;
390 },
391 0 });
392  
393 0 $iq->send_result;
394 } else {
395 0 $iq->send_error; # TODO: <forbidden/>
396 }
397 0 return;
398 }
399  
400 0 ▶ my $query = $iq->query
Branch: 1 branch
BranchTrueFalseTotal
my $query = $iq->query or die 0 0 0
401 or die;
402 0 my @children = $query->children;
403 0 ▶ my $get = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
404 0 my $lname = shift;
405 0 foreach my $c (@children) {
406 0 ▶ next unless ref $c && $c->element eq "{jabber:iq:register}$lname";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c and $c->element eq "{jabber:iq:register}$lname" 0 0 0
Condition0%0/3
ref $c and $c->element eq "{jabber:iq:register}$lname"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $c and $c->element eq "{jabber:iq:register}$lname"
ref $c $c->element eq "{jabber:iq:register}$lname"
Truth table0%0/3
A and B
A= ref $cB= $c->element eq "{jabber:iq:register}$lname"
ABresult
0X0
100
111
407 0 my $text = $c->first_child;
408 0 ▶ return undef if ref $text;
Branch: 1 branch
BranchTrueFalseTotal
if ref $text 0 0 0
409 0 return $text;
410 }
411 0 return undef;
412 0 };
413  
414 0 my $username = $get->("username");
415 0 my $password = $get->("password");
416 0 ▶ return $iq->send_error unless $username =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $username =~ /^\w+$/ 0 0 0
417 0 ▶ return $iq->send_error if $bjid && $bjid->node ne $username;
Branch: 1 branch
BranchTrueFalseTotal
if $bjid and $bjid->node ne $username 0 0 0
Condition0%0/3
$bjid and $bjid->node ne $username
!ll&&!rl&&r
000
MC/DC0%0/2
$bjid and $bjid->node ne $username
$bjid $bjid->node ne $username
Truth table0%0/3
A and B
A= $bjidB= $bjid->node ne $username
ABresult
0X0
100
111
418  
419 # create the account
420 $vhost->run_hook_chain(phase => "RegisterJID",
421 args => [ username => $username, conn => $conn, password => $password ],
422 methods => {
423 0 ▶ saved => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
424 0 return $iq->send_result;
425 },
426 0 ▶ conflict => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
427 0 my $epass = exml($password);
428 0 return $iq->send_error(qq{
429 <query xmlns='jabber:iq:register'>
430 <username>$username</username>
431 <password>$epass</password>
432 </query>
433 <error code='409' type='cancel'>
434 <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
435 </error>
436 });
437 },
438 0 ▶ error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
439 0 return $iq->send_error;
440 },
441 0 });
442  
443 }
444  
445  
446 0 ▶sub process_iq_getauth {
Subroutine process_iq_getauth: not called CC=9 SCAR=45.0
Pod undocumented
447 0 my ($conn, $iq) = @_;
448 # <iq type='get' id='gaimf46fbc1e'><query xmlns='jabber:iq:auth'><username>brad</username></query></iq>
449  
450 # force SSL by not letting them login
451 0 ▶ if ($conn->vhost->requires_ssl && ! $conn->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn->vhost->requires_ssl and not $conn->ssl) 0 0 0
Condition0%0/3
$conn->vhost->requires_ssl and not $conn->ssl
!ll&&!rl&&r
000
MC/DC0%0/2
$conn->vhost->requires_ssl and not $conn->ssl
$conn->vhost->requires_ssl not $conn->ssl
Truth table0%0/3
A and not(B)
A= $conn->vhost->requires_sslB= not $conn->ssl
ABresult
0X0
100
111
452 0 $conn->stream_error("policy-violation", "Local policy requires use of SSL before authentication.");
453 0 return;
454 }
455  
456 0 my $username = "";
457 0 my $child = $iq->query->first_element;
458 0 ▶ if ($child && $child->element eq "{jabber:iq:auth}username") {
Branch: 1 branch
BranchTrueFalseTotal
if ($child and $child->element eq "{jabber:iq:auth}username") 0 0 0
Condition0%0/3
$child and $child->element eq "{jabber:iq:auth}username"
!ll&&!rl&&r
000
MC/DC0%0/2
$child and $child->element eq "{jabber:iq:auth}username"
$child $child->element eq "{jabber:iq:auth}username"
Truth table0%0/3
A and B
A= $childB= $child->element eq "{jabber:iq:auth}username"
ABresult
0X0
100
111
459 0 $username = $child->first_child;
460 0 ▶ die "Element in username field?" if ref $username;
Branch: 1 branch
BranchTrueFalseTotal
if ref $username 0 0 0
461 }
462  
463 # FIXME: use nodeprep or whatever, not \w+
464 0 ▶ $username = '' unless $username =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $username =~ /^\w+$/ 0 0 0
465  
466 0 ▶ my $type = ($conn->vhost->are_hooks("GetPassword") ||
Branch: 1 branch
BranchTrueFalseTotal
$conn->vhost->are_hooks('GetPassword') || $conn->vhost->are_hooks('CheckDigest') ? : 0 0 0
Condition0%0/3
$conn->vhost->are_hooks("GetPassword") or $conn->vhost->are_hooks("CheckDigest")
l!l&&r!l&&!r
000
MC/DC0%0/2
$conn->vhost->are_hooks("GetPassword") or $conn->vhost->are_hooks("CheckDigest")
$conn->vhost->are_hooks("GetPassword") $conn->vhost->are_hooks("CheckDigest")
Truth table0%0/3
A or B
A= $conn->vhost->are_hooks("GetPassword")B= $conn->vhost->are_hooks("CheckDigest")
ABresult
1X1
011
000
467 $conn->vhost->are_hooks("CheckDigest")) ? "<digest/>" : "<password/>";
468  
469 0 $iq->send_result_raw("<query xmlns='jabber:iq:auth'><username>$username</username>$type<resource/></query>");
470 0 return 1;
471 }
472  
473 0 ▶sub process_iq_setauth {
Subroutine process_iq_setauth: not called CC=7 SCAR=40.3
Pod undocumented
474 0 my ($conn, $iq) = @_;
475 # <iq type='set' id='gaimbb822399'><query xmlns='jabber:iq:auth'><username>brad</username><resource>work</resource><digest>ab2459dc7506d56247e2dc684f6e3b0a5951a808</digest></query></iq>
476 0 my $id = $iq->id;
477  
478 0 ▶ my $query = $iq->query
Branch: 1 branch
BranchTrueFalseTotal
my $query = $iq->query or die 0 0 0
479 or die;
480 0 my @children = $query->children;
481  
482 0 ▶ my $get = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
483 0 my $lname = shift;
484 0 foreach my $c (@children) {
485 0 ▶ next unless ref $c && $c->element eq "{jabber:iq:auth}$lname";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c and $c->element eq "{jabber:iq:auth}$lname" 0 0 0
Condition0%0/3
ref $c and $c->element eq "{jabber:iq:auth}$lname"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $c and $c->element eq "{jabber:iq:auth}$lname"
ref $c $c->element eq "{jabber:iq:auth}$lname"
Truth table0%0/3
A and B
A= ref $cB= $c->element eq "{jabber:iq:auth}$lname"
ABresult
0X0
100
111
486 0 my $text = $c->first_child;
487 0 ▶ return undef if ref $text;
Branch: 1 branch
BranchTrueFalseTotal
if ref $text 0 0 0
488 0 return $text;
489 }
490 0 return undef;
491 0 };
492  
493 0 my $username = $get->("username");
494 0 my $resource = $get->("resource");
495 0 my $password = $get->("password");
496 0 my $digest = $get->("digest");
497  
498 # "Both the username and the resource are REQUIRED for client
499 # authentication" Section 3.1 of XEP 0078
500 0 ▶ return unless $username && $username =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $username and $username =~ /^\w+$/ 0 0 0
Condition0%0/3
$username and $username =~ /^\w+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$username and $username =~ /^\w+$/
$username $username =~ /^\w+$/
Truth table0%0/3
A and B
A= $usernameB= $username =~ /^\w+$/
ABresult
0X0
100
111
501 0 ▶ return unless $resource;
Branch: 1 branch
BranchTrueFalseTotal
unless $resource 0 0 0
502  
503 0 my $vhost = $conn->vhost;
504  
505 0 ▶ my $reject = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
506 0 $DJabberd::Stats::counter{'auth_failure'}++;
507 0 $iq->send_reply("error", qq{<error code='401' type='auth'><not-authorized xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>});
508 0 return 1;
509 0 };
510  
511  
512 0 ▶ my $accept = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
513 0 my $cb = shift;
514 0 my $authjid = shift;
515  
516 # create default JID
517 0 ▶ unless (defined $authjid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $authjid) 0 0 0
518 0 my $sname = $vhost->name;
519 0 $authjid = "$username\@$sname";
520 }
521  
522 # register
523 0 my $jid = DJabberd::JID->new("$authjid");
524  
525 0 ▶ unless ($jid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($jid) 0 0 0
526 0 $reject->();
527 0 return;
528 }
529  
530 my $regcb = DJabberd::Callback->new({
531 0 ▶ registered => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
532 0 (undef, my $fulljid) = @_;
533 0 $conn->set_bound_jid($fulljid);
534 0 $DJabberd::Stats::counter{'auth_success'}++;
535 0 $iq->send_result;
536 },
537 0 ▶ error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
538 0 $iq->send_error;
539 },
540 0 ▶ _post_fire => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
541 0 $conn = undef;
542 0 $iq = undef;
543 },
544 0 });
545  
546 0 $vhost->register_jid($jid, $resource, $conn, $regcb);
547 0 };
548  
549  
550 # XXX FIXME
551 # If the client ignores your wishes get a digest or password
552 # We should throw an error indicating so
553 # Currently we will just return authentication denied -- artur
554  
555 0 ▶ if ($vhost->are_hooks("GetPassword")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($vhost->are_hooks('GetPassword')) { } 0 0 0
elsif ($vhost->are_hooks('CheckDigest')) { } 0 0 0
556 $vhost->run_hook_chain(phase => "GetPassword",
557 args => [ username => $username, conn => $conn ],
558 methods => {
559 0 ▶ set => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
560 0 my (undef, $good_password) = @_;
561 0 ▶ if ($password && $password eq $good_password) {
Branch: 2 branches
BranchTrueFalseTotal
if ($password and $password eq $good_password) { } 0 0 0
elsif ($digest) { } 0 0 0
Condition0%0/3
$password and $password eq $good_password
!ll&&!rl&&r
000
MC/DC0%0/2
$password and $password eq $good_password
$password $password eq $good_password
Truth table0%0/3
A and B
A= $passwordB= $password eq $good_password
ABresult
0X0
100
111
562 0 $accept->();
563 } elsif ($digest) {
564 0 my $good_dig = lc(Digest::SHA1::sha1_hex($conn->{stream_id} . $good_password));
565 0 ▶ if ($good_dig eq $digest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($good_dig eq $digest) { } 0 0 0
566 0 $accept->();
567 } else {
568 0 $reject->();
569 }
570 } else {
571 0 $reject->();
572 }
573 },
574 },
575 0 fallback => $reject);
576 } elsif ($vhost->are_hooks("CheckDigest")) {
577 0 $vhost->run_hook_chain(phase => "CheckDigest",
578 args => [ username => $username, conn => $conn, digest => $digest, resource => $resource ],
579 methods => {
580 accept => $accept,
581 reject => $reject,
582 });
583 } else {
584 0 $vhost->run_hook_chain(phase => "CheckCleartext",
585 args => [ username => $username, conn => $conn, password => $password ],
586 methods => {
587 accept => $accept,
588 reject => $reject,
589 });
590 }
591  
592 0 return 1; # signal that we've handled it
593 }
594  
595 ## sessions have been deprecated, see appendix E of:
596 ## http://xmpp.org/internet-drafts/draft-saintandre-rfc3921bis-07.html
597 ## BUT, we have to advertise session support since, libpurple REQUIRES it
598 ## (sigh)
599 0 ▶sub process_iq_session {
Subroutine process_iq_session: not called CC=1 SCAR=6.9
Pod undocumented
600 0 my ($conn, $iq) = @_;
601  
602 0 my $from = $iq->from;
603 0 my $id = $iq->id;
604  
605 0 my $xml = qq{<iq from='$from' type='result' id='$id'/>};
606 0 $conn->xmllog->info($xml);
607 0 $conn->write(\$xml);
608 }
609  
610 0 ▶sub process_iq_bind {
Subroutine process_iq_bind: not called CC=6 SCAR=37.4
Pod undocumented
611 0 my ($conn, $iq) = @_;
612  
613 # <iq type='set' id='purple88621b5d'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>yann</resource></bind></iq>
614 0 my $id = $iq->id;
615  
616 0 ▶ my $query = $iq->bind
Branch: 1 branch
BranchTrueFalseTotal
my $query = $iq->bind or die 0 0 0
617 or die;
618  
619 0 my $bindns = 'urn:ietf:params:xml:ns:xmpp-bind';
620 0 my @children = $query->children;
621  
622 0 ▶ my $get = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
623 0 my $lname = shift;
624 0 foreach my $c (@children) {
625 0 ▶ next unless ref $c && $c->element eq "{$bindns}$lname";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c and $c->element eq "{$bindns}$lname" 0 0 0
Condition0%0/3
ref $c and $c->element eq "{$bindns}$lname"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $c and $c->element eq "{$bindns}$lname"
ref $c $c->element eq "{$bindns}$lname"
Truth table0%0/3
A and B
A= ref $cB= $c->element eq "{$bindns}$lname"
ABresult
0X0
100
111
626 0 my $text = $c->first_child;
627 0 ▶ return undef if ref $text;
Branch: 1 branch
BranchTrueFalseTotal
if ref $text 0 0 0
628 0 return $text;
629 }
630 0 return undef;
631 0 };
632  
633 0 ▶ my $resource = $get->("resource") || DJabberd::JID->rand_resource;
Condition0%0/3
&$get('resource') || 'DJabberd::JID'->rand_resource
l!l&&r!l&&!r
000
MC/DC0%0/2
&$get('resource') || 'DJabberd::JID'->rand_resource
&$get('resource') 'DJabberd::JID'->rand_resource
Truth table0%0/3
A || B
A= &$get('resource')B= 'DJabberd::JID'->rand_resource
ABresult
1X1
011
000
634  
635 0 my $vhost = $conn->vhost;
636  
637 0 ▶ my $reject = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
638 0 my $xml = <<EOX;
639 <iq id='$id' type='error'>
640 <error type='modify'>
641 <bad-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
642 </error>
643 </iq>
644 EOX
645 0 $conn->xmllog->info($xml);
646 0 $conn->write(\$xml);
647 0 return 1;
648 0 };
649  
650 ## rfc3920 ยง8.4.2.2
651 0 ▶ my $cancel = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
652 0 ▶ my $reason = shift || "no reason";
Condition0%0/2
shift() || 'no reason'
l!l
00
MC/DC0%0/1
shift() || 'no reason'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
653 0 my $xml = <<EOX;
654 <iq id='$id' type='error'>
655 <error type='cancel'>
656 <not-allowed
657 xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
658 </error>
659 </iq>
660 EOX
661 0 $conn->log->error("Reject bind request: $reason");
662 0 $conn->xmllog->info($xml);
663 0 $conn->write(\$xml);
664 0 return 1;
665 0 };
666  
667 0 ▶ my $sasl = $conn->sasl
Branch: 1 branch
BranchTrueFalseTotal
my $sasl = $conn->sasl or return &$cancel("no sasl") 0 0 0
668 or return $cancel->("no sasl");
669  
670 0 ▶ my $authjid = $conn->sasl->authenticated_jid
Branch: 1 branch
BranchTrueFalseTotal
my $authjid = $conn->sasl->authenticated_jid or return &$cancel("no authenticated_jid") 0 0 0
671 or return $cancel->("no authenticated_jid");
672  
673 # register
674 0 my $jid = DJabberd::JID->new($authjid);
675  
676 0 ▶ unless ($jid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($jid) 0 0 0
677 0 $reject->();
678 0 return;
679 }
680  
681 my $regcb = DJabberd::Callback->new({
682 0 ▶ registered => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
683 0 (undef, my $fulljid) = @_;
684 0 $conn->set_bound_jid($fulljid);
685 0 $DJabberd::Stats::counter{'auth_success'}++;
686 0 my $xml = <<EOX;
687 <iq id='$id' type='result'>
688 <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
689 <jid>$fulljid</jid>
690 </bind>
691 </iq>
692 EOX
693 0 $conn->xmllog->info($xml);
694 0 $conn->write(\$xml);
695 },
696 0 ▶ error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
697 0 $reject->();
698 },
699 0 ▶ _post_fire => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
700 0 $conn = undef;
701 0 $iq = undef;
702 },
703 0 });
704  
705 0 $vhost->register_jid($jid, $resource, $conn, $regcb);
706 0 return 1;
707 }
708  
709 0 ▶sub process_iq_set_djabberd_test {
Subroutine process_iq_set_djabberd_test: not called CC=4 SCAR=30.0
Pod undocumented
710 0 my ($conn, $iq) = @_;
711 # <iq type='set' id='foo'><query xmlns='djabberd:test'>some command</query></iq>
712 0 my $id = $iq->id;
713  
714 0 ▶ unless ($ENV{DJABBERD_TEST_COMMANDS}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"DJABBERD_TEST_COMMANDS"}) 0 0 0
715 0 $iq->send_error;
716 0 return;
717 }
718  
719 0 ▶ my $query = $iq->query
Branch: 1 branch
BranchTrueFalseTotal
my $query = $iq->query or die 0 0 0
720 or die;
721 0 my $command = $query->first_child;
722  
723 0 ▶ if ($command eq "write error") {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "write error") 0 0 0
724 0 ▶ $conn->set_writer_func(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
725 0 my ($bref, $to_write, $offset) = @_;
726 0 $conn->close;
727 0 return 0;
728 0 });
729 0 $iq->send_result_raw("<wont_get_to_you_anyway/>");
730 0 return;
731 }
732  
733 0 $iq->send_result_raw("<unknown-command/>");
734 }
735  
736 0 ▶sub id {
Subroutine id: not called CC=1 SCAR=6.9
Pod undocumented
737 0 return $_[0]->attr("{}id");
738 }
739  
740 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
741 0 return $_[0]->attr("{}type");
742 }
743  
744 0 ▶sub from {
Subroutine from: not called CC=1 SCAR=6.9
Pod undocumented
745 0 return $_[0]->attr("{}from");
746 }
747  
748 0 ▶sub query {
Subroutine query: not called CC=4 SCAR=30.0
Pod undocumented
749 0 my $self = shift;
750 0 ▶ my $child = $self->first_element
Branch: 1 branch
BranchTrueFalseTotal
my $child = $self->first_element or return 0 0 0
751 or return;
752 0 ▶ my $ele = $child->element
Branch: 1 branch
BranchTrueFalseTotal
my $ele = $child->element or return 0 0 0
753 or return;
754 0 ▶ return undef unless $child->element =~ /\}query$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $child->element =~ /\}query$/ 0 0 0
755 0 return $child;
756 }
757  
758 0 ▶sub bind {
Subroutine bind: not called CC=4 SCAR=30.0
Pod undocumented
759 0 my $self = shift;
760 0 ▶ my $child = $self->first_element
Branch: 1 branch
BranchTrueFalseTotal
my $child = $self->first_element or return 0 0 0
761 or return;
762 0 ▶ my $ele = $child->element
Branch: 1 branch
BranchTrueFalseTotal
my $ele = $child->element or return 0 0 0
763 or return;
764 0 ▶ return unless $child->element =~ /\}bind$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $child->element =~ /\}bind$/ 0 0 0
765 0 return $child;
766 }
767  
768 0 ▶sub deliver_when_unavailable {
Subroutine deliver_when_unavailable: not called CC=2 SCAR=17.9
Pod undocumented
769 0 my $self = shift;
770 0 ▶ return $self->type eq "result" ||
Condition0%0/3
$self->type eq 'result' || $self->type eq 'error'
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->type eq 'result' || $self->type eq 'error'
$self->type eq 'result' $self->type eq 'error'
Truth table0%0/3
A || B
A= $self->type eq 'result'B= $self->type eq 'error'
ABresult
1X1
011
000
771 $self->type eq "error";
772 }
773  
774 0 ▶sub make_response {
Subroutine make_response: not called CC=1 SCAR=6.9
Pod undocumented
775 0 my ($self) = @_;
776  
777 0 my $response = $self->SUPER::make_response();
778 0 $response->attrs->{"{}type"} = "result";
779 0 return $response;
780 }
781  
782 20 1;
« DJabberd/Connection/Admin.pm ↑ Summary DJabberd/Stanza/SASL.pm »