NTLMManager.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 173 CC 14 SCAR 42.4
CC
14
Cov
0%
server_step
182.0
invoke_ntlm_auth
6.0
new
2.0
CRAP 69.1
1 package DJabberd::SASL::NTLMManager;
2  
3 use strict;
4 use warnings;
5 use MIME::Base64 qw/encode_base64 decode_base64/;
6 use POSIX ":sys_wait_h";
7 use Carp;
8 our $logger = DJabberd::Log->get_logger();
9  
10 sub new {
11 my $class = shift;
12 my $plugin = shift;
13 return bless { __plugin => $plugin }, $class;
14 }
15  
16 sub server_start {
17 my $self = shift;
18 my $init = shift;
19 my $cb = shift;
20 $self->{state} = "start";
21 $logger->info("server start");
22 &$cb("1");
23 }
24  
25 sub need_step {
26 my $self = shift;
27 $logger->info("need step");
28 }
29  
30 sub server_step {
31  
32 my $self = shift;
33 my $response = shift;
34 my $cb = shift;
35 my ( $code, $reply, $data );
36 my $plugin = $self->{__plugin};
37 my $in = $plugin->{in};
38 my $out = $plugin->{out};
39 my $s = $plugin->{s};
40  
41 $logger->info( "server step: " . $self->{state} );
42 if ( waitpid( $plugin->{pid}, WNOHANG ) > 0 ) {
43 $logger->info("ntlm_auth die");
44 &invoke_ntlm_auth($plugin);
45 &$cb;
46 return;
47 }
48  
49 if ( $self->{state} eq "start" ) {
50 $self->{state} = "challenge";
51 $response = encode_base64( $response, '' );
52 print $out "YR $response\n";
53 if ( $s->can_read(2) ) {
54 unless ( sysread $in, $data, 16 ) {
55 &$cb(undef);
56 return;
57 }
58 $reply = $data;
59 while ( $s->can_read(0) ) {
60 last unless ( sysread $in, $data, 16 );
61 $reply .= $data;
62 }
63 }
64 else {
65 $logger->warn("no reply after 2 sec");
66 &rip_child($plugin);
67 &$cb(undef);
68 return;
69 }
70 ( $code, $reply ) = split( /\s+/, $reply );
71 if ( $code ne "TT" ) {
72 $self->{error} = "internal ntlmhelper error";
73 $logger->warn("receive: $code $reply");
74 $logger->warn( $self->{error} );
75 $reply = undef;
76 }
77 else {
78 $reply = decode_base64($reply);
79 }
80 &$cb($reply);
81 }
82 elsif ( $self->{state} eq "challenge" ) {
83 $response = encode_base64( $response, '' );
84 print $out "KK $response\n";
85 if ( $s->can_read(2) ) {
86 unless ( sysread $in, $data, 16 ) {
87 &$cb;
88 return;
89 }
90 $reply = $data;
91 while ( $s->can_read(0) ) {
92 last unless ( sysread $in, $data, 16 );
93 $reply .= $data;
94 }
95 }
96 else {
97 $logger->warn("no reply after 2 sec");
98 &rip_child($plugin);
99 &$cb;
100 return;
101 }
102 ( $code, $reply ) = split( /\s+/, $reply );
103 if ( $code eq "AF" ) {
104 $plugin->{user} = lc $reply;
105 $self->{success} = 1;
106 }
107 elsif ( $code eq "NA" ) {
108 $self->{error} = "no match, " . $reply;
109 $logger->warn( $self->{error} );
110 }
111 else {
112 $self->{error} = "internal ntlmhelper error";
113 $logger->warn( $self->{error} );
114 }
115 &$cb;
116 }
117 }
118  
119 sub rip_child {
120 my $plugin = shift;
121  
122 #close $plugin->{in};
123 #close $plugin->{out};
124 kill 15, $plugin->{pid};
125 }
126  
127 sub invoke_ntlm_auth {
128 use IPC::Open3;
129 use IO::Select;
130  
131 my $plugin = shift;
132 $plugin->{err} = 1;
133 $plugin->{pid} =
134 open3( $plugin->{out}, $plugin->{in}, $plugin->{err}, $plugin->{helper},
135 @{ $plugin->{params} } );
136 $plugin->{s} = IO::Select->new();
137 $plugin->{s}->add( $plugin->{in} );
138  
139 croak "Can't open bidirectional pipe to NTLM helper"
140 unless ( $plugin->{pid} );
141 }
142  
143 sub mechanism { }
144 sub server_new { $_[0] }
145 sub is_success { $_[0]->{success} }
146 sub error { $_[0]->{error} }
147 sub answer { $_[0]->{__plugin}{user} }
148 sub property { }
149  
150 sub authenticated_jid { $_[0]->{jid} }
151 sub set_authenticated_jid { $_[0]->{jid} = $_[1] }
152  
153 sub is_mechanism_supported { "NTLM" }
154  
155 1;