| 1 | package DJabberd::Stanza::SASL; | ||||||||||||||||||||||||||||
| 2 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 20 | ▶ | use base qw(DJabberd::Stanza); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 20 | ▶ | use MIME::Base64 qw/encode_base64 decode_base64/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 0 | ▶ | sub on_recv_from_server { die "unimplemented" } | ||||||||||||||||||||||||||
|
Subroutine
on_recv_from_server: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | ## TODO: | ||||||||||||||||||||||||||||
| 11 | ## check number of auth failures, force deconnection, bad for t time §7.3.5 policy-violation | ||||||||||||||||||||||||||||
| 12 | ## Provide hooks for Authen:: modules to return details about errors: | ||||||||||||||||||||||||||||
| 13 | ## - credentials-expired | ||||||||||||||||||||||||||||
| 14 | ## - account-disabled | ||||||||||||||||||||||||||||
| 15 | ## - invalid-authzid | ||||||||||||||||||||||||||||
| 16 | ## - temporary-auth-failure | ||||||||||||||||||||||||||||
| 17 | ## these hooks should probably additions to parameters taken by GetPassword, CheckClearText | ||||||||||||||||||||||||||||
| 18 | ## right now all these errors results in not-authorized being returned | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub on_recv_from_client { | ||||||||||||||||||||||||||
|
Subroutine
on_recv_from_client: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | return $self->handle_abort(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | if $self->element_name eq 'abort'; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | return $self->handle_response(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | if $self->element_name eq 'response'; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | return $self->handle_auth(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | if $self->element_name eq 'auth'; | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | ## supports §7.3.4, §7.4.1 | ||||||||||||||||||||||||||||
| 34 | ## handles: <abort xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/> | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub handle_abort { | ||||||||||||||||||||||||||
|
Subroutine
handle_abort: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 0 | my ($self, $conn) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | $self->send_failure("aborted" => $conn); | |||||||||||||||||||||||||||
| 39 | 0 | return; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub handle_response { | ||||||||||||||||||||||||||
|
Subroutine
handle_response: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | 0 | my ($conn) = @_; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $sasl = $conn->sasl | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | or return $self->send_failure("malformed-request" => $conn); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if (my $error = $sasl->error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | return $self->send_failure("not-authorized" => $conn); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if (! $sasl->need_step) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | $conn->log->info("sasl negotiation unexpected end"); | |||||||||||||||||||||||||||
| 54 | 0 | return $self->send_failure("malformed-request" => $conn); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my $response = $self->first_child; | |||||||||||||||||||||||||||
| 58 | 0 | $response = $self->decode($response); | |||||||||||||||||||||||||||
| 59 | 0 | $conn->log->info("Got the response $response"); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | $sasl->server_step( | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $response => sub { $self->send_reply($conn->{sasl}, shift() => $conn) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 63 | 0 | ); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub handle_auth { | ||||||||||||||||||||||||||
|
Subroutine
handle_auth: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 0 | my ($self, $conn) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my $fallback = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 70 | 0 | $self->send_failure("invalid-mechanism" => $conn); | |||||||||||||||||||||||||||
| 71 | 0 | }; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | my $vhost = $conn->vhost | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | or die "There is no vhost"; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | my $saslmgr; | |||||||||||||||||||||||||||
| 77 | $vhost->run_hook_chain( phase => "GetSASLManager", | ||||||||||||||||||||||||||||
| 78 | args => [ conn => $conn ], | ||||||||||||||||||||||||||||
| 79 | methods => { | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | get => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | 0 | (undef, $saslmgr) = @_; | |||||||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||||||||
| 83 | }, | ||||||||||||||||||||||||||||
| 84 | 0 | fallback => $fallback, | |||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | die "no SASL" unless $saslmgr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | ## TODO: §7.4.4. encryption-required | ||||||||||||||||||||||||||||
| 89 | 0 | my $mechanism = $self->attr("{}mechanism"); | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | return $self->send_failure("invalid-mechanism" => $conn) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | unless $saslmgr->is_mechanism_supported($mechanism); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | ## we don't support it for now | ||||||||||||||||||||||||||||
| 94 | 0 | my $opts = { no_integrity => 1 }; | |||||||||||||||||||||||||||
| 95 | 0 | $saslmgr->mechanism($mechanism); | |||||||||||||||||||||||||||
| 96 | 0 | my $sasl_conn = $saslmgr->server_new("xmpp", $vhost->server_name, $opts); | |||||||||||||||||||||||||||
| 97 | 0 | $conn->{sasl} = $sasl_conn; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | my $init = $self->first_child; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | if (!$init or $init eq '=') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $init or $init eq "="
MC/DC0%0/2
not $init or $init eq "="
not $init
$init eq "="
Truth table0%0/3
not(A) or B
A= not $initB= $init eq "="
| |||||||||||||||||||||||||||||
| 101 | 0 | $init = ''; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | 0 | $init = $self->decode($init); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | $sasl_conn->server_start( | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | $init => sub { $self->send_reply($conn->{sasl}, shift() => $conn) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 109 | 0 | ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub send_challenge { | ||||||||||||||||||||||||||
|
Subroutine
send_challenge: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | 0 | my ($challenge, $conn) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | $conn->log->debug("Sending Challenge: $challenge"); | |||||||||||||||||||||||||||
| 117 | 0 | my $enc_challenge = $self->encode($challenge); | |||||||||||||||||||||||||||
| 118 | 0 | my $xml = "<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>$enc_challenge</challenge>"; | |||||||||||||||||||||||||||
| 119 | 0 | $conn->xmllog->info($xml); | |||||||||||||||||||||||||||
| 120 | 0 | $conn->write(\$xml); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub send_failure { | ||||||||||||||||||||||||||
|
Subroutine
send_failure: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 125 | 0 | my ($error, $conn) = @_; | |||||||||||||||||||||||||||
| 126 | 0 | $conn->log->debug("Sending error: $error"); | |||||||||||||||||||||||||||
| 127 | 0 | my $xml = <<EOF; | |||||||||||||||||||||||||||
| 128 | <failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><$error/></failure> | ||||||||||||||||||||||||||||
| 129 | EOF | ||||||||||||||||||||||||||||
| 130 | 0 | $conn->xmllog->info($xml); | |||||||||||||||||||||||||||
| 131 | 0 | $conn->write(\$xml); | |||||||||||||||||||||||||||
| 132 | 0 | return; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub ack_success { | ||||||||||||||||||||||||||
|
Subroutine
ack_success: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 137 | 0 | my ($sasl_conn, $challenge, $conn) = @_; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my $username = $sasl_conn->answer('username') || $sasl_conn->answer('user'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$sasl_conn->answer('username') || $sasl_conn->answer('user')
MC/DC0%0/2
$sasl_conn->answer('username') || $sasl_conn->answer('user')
$sasl_conn->answer('username')
$sasl_conn->answer('user')
Truth table0%0/3
A || B
A= $sasl_conn->answer('username')B= $sasl_conn->answer('user')
| |||||||||||||||||||||||||||||
| 140 | 0 | my $sname = $conn->vhost->name; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | unless ($username && $sname) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$username and $sname
MC/DC0%0/2
$username and $sname
$username
$sname
Truth table0%0/3
A and B
A= $usernameB= $sname
| |||||||||||||||||||||||||||||
| 142 | 0 | $conn->log->error("Couldn't bind to a jid, declining."); | |||||||||||||||||||||||||||
| 143 | 0 | $self->send_failure("not-authorized" => $conn); | |||||||||||||||||||||||||||
| 144 | 0 | return; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 0 | my $authenticated_jid = "$username\@$sname"; | |||||||||||||||||||||||||||
| 147 | 0 | $sasl_conn->set_authenticated_jid($authenticated_jid); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | my $xml; | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if (defined $challenge) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | my $enc = $challenge ? $self->encode($challenge) : "="; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | $xml = "<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>$enc</success>"; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | else { | ||||||||||||||||||||||||||||
| 155 | 0 | $xml = "<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>"; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 0 | $conn->xmllog->info($xml); | |||||||||||||||||||||||||||
| 158 | 0 | $conn->write(\$xml); | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | if (($sasl_conn->property('ssf') || 0) > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$sasl_conn->property('ssf') || 0
MC/DC0%0/1
$sasl_conn->property('ssf') || 0
$sasl_conn->property('ssf')
Truth table0%0/2
A
A= $sasl_conn->property('ssf')
| |||||||||||||||||||||||||||||
| 160 | 0 | $conn->log->info("SASL: Securing socket"); | |||||||||||||||||||||||||||
| 161 | 0 | $conn->log->warn("This will probably NOT work"); | |||||||||||||||||||||||||||
| 162 | 0 | $sasl_conn->securesocket($conn); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | else { | ||||||||||||||||||||||||||||
| 165 | 0 | $conn->log->info("SASL: Not securing socket"); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 0 | $conn->restart_stream; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | sub encode { | ||||||||||||||||||||||||||
|
Subroutine
encode: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 172 | 0 | my $str = shift; | |||||||||||||||||||||||||||
| 173 | 0 | return encode_base64($str, ''); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub decode { | ||||||||||||||||||||||||||
|
Subroutine
decode: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 177 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 178 | 0 | my $str = shift; | |||||||||||||||||||||||||||
| 179 | 0 | return decode_base64($str); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub send_reply { | ||||||||||||||||||||||||||
|
Subroutine
send_reply: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 183 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 184 | 0 | my ($sasl_conn, $challenge, $conn) = @_; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if (my $error = $sasl_conn->error) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 187 | 0 | $self->send_failure("not-authorized" => $conn); | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | elsif ($sasl_conn->is_success) { | ||||||||||||||||||||||||||||
| 190 | 0 | $self->ack_success($sasl_conn, $challenge => $conn); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | else { | ||||||||||||||||||||||||||||
| 193 | 0 | $self->send_challenge($challenge => $conn); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | 0 | return; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 20 | 1; | |||||||||||||||||||||||||||