lib/Net/Sieve.pm

filter: Statementstmt 9.7% 41 / 421 Branchbran 0.0% 0 / 220 Conditioncond 0.0% 0 / 69 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 32.3% 11 / 34 Podpod 100% 15 / 15 totaltotal 8.3% 67 / 799 CC 138 SCAR 77.7
CC
138
Cov
6%
new
5256.0
_parse_capabilities
808.0
sget
182.0
CRAP 2368.7
↑ Summary
1 package Net::Sieve;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Net::Sieve - Implementation of managesieve protocol to manage sieve scripts
8  
9 =head1 SYNOPSIS
10  
11 use Net::Sieve;
12  
13 my $SieveServer = Net::Sieve->new (
14 server => 'imap.server.org',
15 user => 'user',
16 password => 'pass' ,
17 );
18  
19 foreach my $script ( $SieveServer->list() ) {
20 print $script->{name}." ".$script->{status}."\n";
21 };
22  
23 my $name_script = 'test';
24  
25 # read
26 print $SieveServer->get($name_script);
27  
28 # write
29 my $test_script='
30 require "fileinto";
31 ## Place all these in the "Test" folder
32 if header :contains "Subject" "[Test]" {
33 fileinto "Test";
34 }
35 ';
36  
37 # other
38 $SieveServer->put($name_script,$new_script);
39 $SieveServer->activate($name_script);
40 $SieveServer->deactivate();
41 $SieveServer->delete($name_script);
42  
43  
44 =head1 DESCRIPTION
45  
46 B<Net::Sieve> is a package for clients for the "MANAGESIEVE" protocol, which is an Internet Draft protocol for manipulation of "Sieve" scripts in a repository. More simply, Net::Sieve lets you control your mail-filtering rule files on a mail server.
47  
48 B<Net::Sieve> supports the use of "TLS" via the "STARTTLS" command. B<Net::Sieve> open the connexion to the sieve server, methods allow to list all scripts, activate or deactivate scripts, read, delete or put scripts.
49  
50 Most of code come from the great Phil Pennock B<sieve-connect> command-line tool L<http://people.spodhuis.org/phil.pennock/software/>.
51  
52 See L<Net::Sieve::Script> to manipulate Sieve scripts content.
53  
54 =cut
55  
56 2 ▶use Authen::SASL 2.11 qw(Perl);
Subroutine BEGIN: called CC=1 SCAR=0.0
57 # 2.11: first version with non-broken DIGEST-MD5
58 # Earlier versions don't allow server verification
59 # NB: code still explicitly checks for a new-enough version, so
60 # if you have an older version of Authen::SASL and know what you're
61 # doing then you can remove this version check here. I advise
62 # against it, though.
63 # Perl: Need a way to ask which mechanism to send
64 2 ▶use Authen::SASL::Perl::EXTERNAL; # We munge inside its private stuff.
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶use IO::Socket::INET6;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶use IO::Socket::SSL 0.97; # SSL_ca_path bogus before 0.97
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
72 2 $VERSION = '0.12';
73 2 @ISA = qw(Exporter);
74 #Give a hoot don't pollute, do not export more than needed by default
75 2 @EXPORT = qw();
76 2 @EXPORT_OK = qw();
77 2 %EXPORT_TAGS = ();
78 }
79  
80  
81 2 my %capa;
82 my %raw_capabilities;
83 2 my %capa_dosplit = map {$_ => 1} qw( SASL SIEVE );
84 # Key is permissably empty keyword, value if defined is closure to call with
85 # capabilities after receiving complete list, for verifying permissability.
86 # First param $sock, second \%capa, third \%raw_capabilities
87 my %capa_permit_empty = (
88 # draft 7 onwards clarify that empty SASL is permitted, but is error
89 # in absense of STARTTLS
90 0 ▶ SASL => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
91 0 ▶ return if exists $_[1]{STARTTLS};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[1]{"STARTTLS"} 0 0 0
92 # We die because there's no way to authenticate.
93 # Spec states that after STARTTLS SASL must be non-empty
94 0 warn "Empty SASL not permitted without STARTTLS\n";
95 },
96 2 SIEVE => undef,
97 );
98 2 my $DEBUGGING = 1;
99  
100 =head1 CONSTRUCTOR
101  
102 =head2 new
103  
104 Usage :
105 my $SieveServer = Net::Sieve->new (
106 server => 'imap.server.org',
107 user => 'user',
108 password => 'pass' );
109 Returns :
110 Net::Sieve object which contain current open socket
111 Argument :
112 server : default localhost
113 port : default 2000
114 user : default logname or $ENV{USERNAME} or $ENV{LOGNAME}
115 password :
116 net_domain :
117 sslkeyfile : default search in /etc/ssl/certs
118 sslcertfile : default search in /etc/ssl/certs
119 autmech : to force a particular authentication mechanism
120 authzid : request authorisation to act as the specified id
121 realm : pass realm information to the authentication mechanism
122 ssl_verif : default 0x01, set 0x00 to don't verify and allow self-signed cerificate
123 notssl_verif: default 0x00, set 0x01 to don't verify and allow self-signed cerificate
124 debug : default 0, set 1 to have transmission logs
125 dumptlsinfo : dump tls information
126  
127 =cut
128  
129 sub new
130 0 ▶{
Subroutine new: not called CC=72 SCAR=85.7
Pod documented
131 0 my ($class, %param) = @_;
132  
133 0 ▶ my $self = bless ({}, ref ($class) || $class);
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
134  
135 0 ▶my $server = $param{server}||'localhost';
Condition0%0/2
$param{'server'} || 'localhost'
l!l
00
MC/DC0%0/1
$param{'server'} || 'localhost'
$param{'server'}
Truth table0%0/2
A
A= $param{'server'}
Aresult
11
00
136 0 ▶my $port = $param{port}||'2000';
Condition0%0/2
$param{'port'} || '2000'
l!l
00
MC/DC0%0/1
$param{'port'} || '2000'
$param{'port'}
Truth table0%0/2
A
A= $param{'port'}
Aresult
11
00
137 0 my $user = $param{user};
138 0 my $password = $param{password};
139 0 ▶my $net_domain = $param{net_domain}||AF_UNSPEC;
Condition0%0/2
$param{'net_domain'} || 0
l!l
00
MC/DC0%0/1
$param{'net_domain'} || 0
$param{'net_domain'}
Truth table0%0/2
A
A= $param{'net_domain'}
Aresult
11
00
140 0 my $sslkeyfile = $param{sslkeyfile};
141 0 my $sslcertfile = $param{sslcertfile};
142 0 my $realm = $param{realm};
143 0 my $authmech = $param{autmech};
144 0 my $authzid = $param{authzid};
145 0 my $ssl_verify = 0x01;
146 0 ▶ $ssl_verify = 0x01 if $param{ssl_verify};
Branch: 1 branch
BranchTrueFalseTotal
if $param{"ssl_verify"} 0 0 0
147 0 ▶ $ssl_verify = 0x00 if $param{ssl_verify} eq '0x00';
Branch: 1 branch
BranchTrueFalseTotal
if $param{"ssl_verify"} eq "0x00" 0 0 0
148 0 ▶ $ssl_verify = 0x00 if $param{notssl_verify};
Branch: 1 branch
BranchTrueFalseTotal
if $param{"notssl_verify"} 0 0 0
149 0 my $dump_tls_information = $param{dumptlsinfo};
150 0 $DEBUGGING = $param{debug};
151  
152  
153  
154 0 my %ssl_options = (
155 SSL_version => 'SSLv23:!SSLv2:!SSLv3',
156 SSL_cipher_list => 'ALL:!aNULL:!NULL:!LOW:!EXP:!ADH:@STRENGTH',
157 SSL_verify_mode => $ssl_verify,
158 SSL_ca_path => '/etc/ssl/certs',
159 );
160  
161 0 my $prioritise_auth_external = 0;
162 0 my ($forbid_clearauth, $forbid_clearchan) = (0, 0);
163  
164 0 ▶unless (defined $server) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $server) 0 0 0
165 0 $server = 'localhost';
166 0 ▶ if (exists $ENV{'IMAP_SERVER'}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"IMAP_SERVER"} and not $ENV{"IMAP_SERVER"} =~ m[^/]) 0 0 0
Condition0%0/3
exists $ENV{"IMAP_SERVER"} and not $ENV{"IMAP_SERVER"} =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ENV{"IMAP_SERVER"} and not $ENV{"IMAP_SERVER"} =~ m[^/]
exists $ENV{"IMAP_SERVER"} not $ENV{"IMAP_SERVER"} =~ m[^/]
Truth table0%0/3
A and not(B)
A= exists $ENV{"IMAP_SERVER"}B= not $ENV{"IMAP_SERVER"} =~ m[^/]
ABresult
0X0
100
111
167 and $ENV{'IMAP_SERVER'} !~ m!^/!) {
168 0 $server = $ENV{'IMAP_SERVER'};
169 # deal with a port number.
170 0 ▶ unless ($server =~ /:.*:/) { # IPv6 address literal
Branch: 1 branch
BranchTrueFalseTotal
unless ($server =~ /:.*:/) 0 0 0
171 0 $server =~ s/:\d+\z//;
172 }
173 }
174 }
175  
176 0 ▶die "Bad server name\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $server =~ /^[A-Za-z0-9_.-]+\z/ 0 0 0
177 unless $server =~ /^[A-Za-z0-9_.-]+\z/;
178 0 ▶die "Bad port specification\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $port =~ /^[A-Za-z0-9_()-]+\z/ 0 0 0
179 unless $port =~ /^[A-Za-z0-9_()-]+\z/;
180  
181 0 ▶unless (defined $user) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user) 0 0 0
182 0 ▶ if ($^O eq "MSWin32") {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
183 # perlvar documents always "MSWin32" on Windows ...
184 # what about 64bit windows?
185 0 ▶ if (exists $ENV{USERNAME} and length $ENV{USERNAME}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{'USERNAME'} and length $ENV{'USERNAME'}) { } 0 0 0
elsif (exists $ENV{'LOGNAME'} and length $ENV{'LOGNAME'}) { } 0 0 0
Condition0%0/3
exists $ENV{"USERNAME"} and length $ENV{"USERNAME"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ENV{"USERNAME"} and length $ENV{"USERNAME"}
exists $ENV{"USERNAME"} length $ENV{"USERNAME"}
Truth table0%0/3
A and B
A= exists $ENV{"USERNAME"}B= length $ENV{"USERNAME"}
ABresult
0X0
100
111
186 0 $user = $ENV{USERNAME};
187 ▶ } elsif (exists $ENV{LOGNAME} and length $ENV{LOGNAME}) {
Condition0%0/3
exists $ENV{"LOGNAME"} and length $ENV{"LOGNAME"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ENV{"LOGNAME"} and length $ENV{"LOGNAME"}
exists $ENV{"LOGNAME"} length $ENV{"LOGNAME"}
Truth table0%0/3
A and B
A= exists $ENV{"LOGNAME"}B= length $ENV{"LOGNAME"}
ABresult
0X0
100
111
188 0 $user = $ENV{LOGNAME};
189 } else {
190 0 die "Unable to figure out a default user, sorry.\n";
191 }
192 } else {
193 0 $user = getpwuid $>;
194 }
195 # this should handle the non-mswin32 case if 64bit _is_ different.
196 0 ▶ die "Unable to figure out a default user, sorry!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user 0 0 0
197 unless defined $user;
198 }
199  
200 0 ▶if ((defined $sslkeyfile and not defined $sslcertfile) or
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sslkeyfile and not defined $sslcertfile or defined $sslcertfile and not defined $sslkeyfile) 0 0 0
Condition0%0/9
defined $sslkeyfile and not defined $sslcertfile or defined $sslcertfile and not defined $sslkeyfile
l!l&&r!l&&!r
000
defined $sslkeyfile and not defined $sslcertfile
!ll&&!rl&&r
000
defined $sslcertfile and not defined $sslkeyfile
!ll&&!rl&&r
000
MC/DC0%0/4
defined $sslkeyfile and not defined $sslcertfile or defined $sslcertfile and not defined $sslkeyfile
defined $sslkeyfile not defined $sslcertfile defined $sslcertfile not defined $sslkeyfile
Truth table0%0/7
A and not(B) or C and not(D)
A= defined $sslkeyfileB= not defined $sslcertfileC= defined $sslcertfileD= not defined $sslkeyfile
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
201 (defined $sslcertfile and not defined $sslkeyfile)) {
202 0 die "Need both a client key and cert for SSL certificate auth.\n";
203 }
204 0 ▶if (defined $sslkeyfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sslkeyfile) 0 0 0
205 0 $ssl_options{SSL_use_cert} = 1;
206 0 $ssl_options{SSL_key_file} = $sslkeyfile;
207 0 $ssl_options{SSL_cert_file} = $sslcertfile;
208 0 $prioritise_auth_external = 1;
209 }
210  
211  
212 0 my $sock = IO::Socket::INET6->new(
213 PeerHost => $server,
214 PeerPort => $port,
215 Proto => 'tcp',
216 Domain => $net_domain,
217 MultiHomed => 1, # try multiple IPs (IPv4 works, v6 doesn't?)
218 );
219 0 ▶unless (defined $sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sock) 0 0 0
220 0 my $extra = '';
221 0 ▶ if ($!{EINVAL} and $net_domain != AF_UNSPEC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{"EINVAL"} and $net_domain != 0) 0 0 0
Condition0%0/3
$!{"EINVAL"} and $net_domain != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$!{"EINVAL"} and $net_domain != 0
$!{"EINVAL"} $net_domain != 0
Truth table0%0/3
A and B
A= $!{"EINVAL"}B= $net_domain != 0
ABresult
0X0
100
111
222 0 $extra = " (Probably no host record for overriden IP version)\n";
223 }
224 0 die qq{Connection to "$server" [port $port] failed: $!\n$extra};
225 }
226  
227 0 $sock->autoflush(1);
228 0 _debug("connection: remote host address is @{[$sock->peerhost()]}");
229  
230 0 $self->{_sock} = $sock;
231  
232 0 $self->_parse_capabilities();
233  
234 0 $self->{_capa} = $raw_capabilities{SIEVE};
235  
236  
237 0 my $tls_bitlength = -1;
238  
239 0 ▶if (exists $capa{STARTTLS}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $capa{'STARTTLS'}) { } 0 0 0
elsif ($forbid_clearchan) { } 0 0 0
240 0 $self->ssend("STARTTLS");
241 0 $self->sget();
242 0 ▶ die "STARTTLS request rejected: $_\n" unless /^OK\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^OK\b/ 0 0 0
243 0 ▶ IO::Socket::SSL->start_SSL($sock, %ssl_options) or do {
Branch: 1 branch
BranchTrueFalseTotal
"IO::Socket::SSL"->start_SSL($sock, %ssl_options) or do { my $e = IO::Socket::SSL::errstr(); die "STARTTLS promotion failed: $e\n" } 0 0 0
244 0 my $e = IO::Socket::SSL::errstr();
245 0 die "STARTTLS promotion failed: $e\n";
246 };
247 0 ▶ if (exists $main::{"Net::"} and exists $main::{"Net::"}{"SSLeay::"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $main::main::{"Net::"} and exists $main::main::{"Net::"}{"SSLeay::"}) 0 0 0
Condition0%0/3
exists $main::main::{"Net::"} and exists $main::main::{"Net::"}{"SSLeay::"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $main::main::{"Net::"} and exists $main::main::{"Net::"}{"SSLeay::"}
exists $main::main::{"Net::"} exists $main::main::{"Net::"}{"SSLeay::"}
Truth table0%0/3
A and B
A= exists $main::main::{"Net::"}B= exists $main::main::{"Net::"}{"SSLeay::"}
ABresult
0X0
100
111
248 0 my $t = Net::SSLeay::get_cipher_bits($sock->_get_ssl_object(), 0);
249 0 ▶ $tls_bitlength = $t if defined $t and $t;
Branch: 1 branch
BranchTrueFalseTotal
if defined $t and $t 0 0 0
Condition0%0/3
defined $t and $t
!ll&&!rl&&r
000
MC/DC0%0/2
defined $t and $t
defined $t $t
Truth table0%0/3
A and B
A= defined $tB= $t
ABresult
0X0
100
111
250 }
251 0 _debug("--- TLS activated here [$tls_bitlength bits]");
252 0 ▶ if ($dump_tls_information) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dump_tls_information) 0 0 0
253 0 print $sock->dump_peer_certificate();
254 0 ▶ if ($DEBUGGING and
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUGGING and exists $main::main::{"Net::"} and exists $main::main::{"Net::"}{"SSLeay::"}) 0 0 0
Condition0%0/6
$DEBUGGING and exists $main::main::{"Net::"} and exists $main::main::{"Net::"}{"SSLeay::"}
!ll&&!rl&&r
000
$DEBUGGING and exists $main::main::{"Net::"}
!ll&&!rl&&r
000
MC/DC0%0/3
$DEBUGGING and exists $main::main::{"Net::"} and exists $main::main::{"Net::"}{"SSLeay::"}
$DEBUGGING exists $main::main::{"Net::"} exists $main::main::{"Net::"}{"SSLeay::"}
Truth table0%0/4
A and B and C
A= $DEBUGGINGB= exists $main::main::{"Net::"}C= exists $main::main::{"Net::"}{"SSLeay::"}
ABCresult
0XX0
10X0
1100
1111
255 exists $main::{"Net::"} and exists $main::{"Net::"}{"SSLeay::"}) {
256 # IO::Socket::SSL depends upon Net::SSLeay
257 # so this should be fairly safe, albeit messing
258 # around behind IO::Socket::SSL's back.
259 0 print STDERR Net::SSLeay::PEM_get_string_X509(
260 $sock->peer_certificate());
261 }
262 }
263 0 $forbid_clearauth = 0;
264 # Cyrus sieve might send CAPABILITY after STARTTLS without being
265 # prompted for it. This breaks the command-response model.
266 # We can't just check to see if there's data to read or not, since
267 # that will break if the next data is delayed (race condition).
268 # There is no protocol-compliant method to determine this, short
269 # of "wait a while, see if anything comes along; if not, send
270 # CAPABILITY ourselves". So, I broke protocol by sending the
271 # non-existent command NOOP, then scan for the resulting NO.
272 # This at least is stably deterministic. However, from draft 10
273 # onwards, NOOP is a registered available extension which returns
274 # OK.
275 # New problem: again, Cyrus timsieved. As of 2.3.13, it drops the
276 # connection for an unknown command instead of returning NO. And
277 # logs "Lost connection to client -- exiting" which is an interesting
278 # way of saying "we dropped the connection". At this point, I give up
279 # on protocol-deterministic checks and fall back to version checking.
280 # Alas, Cyrus 2.2.x is still widely deployed because 2.3.x is the
281 # development series and 2.2.x is officially the stable series.
282 # This means that if they don't support NOOP by 2.3.14, I have to
283 # figure out how to decide what is safe and backtrack which version
284 # precisely was the first to send the capability response correctly.
285 0 my $use_noop = 1;
286 0 ▶ if (exists $capa{"IMPLEMENTATION"} and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $capa{"IMPLEMENTATION"} and $capa{"IMPLEMENTATION"} =~ /^Cyrus timsieved v2\.3\.(\d+)/ and $1 >= 13) 0 0 0
Condition0%0/6
exists $capa{"IMPLEMENTATION"} and $capa{"IMPLEMENTATION"} =~ /^Cyrus timsieved v2\.3\.(\d+)/ and $1 >= 13
!ll&&!rl&&r
000
exists $capa{"IMPLEMENTATION"} and $capa{"IMPLEMENTATION"} =~ /^Cyrus timsieved v2\.3\.(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/3
exists $capa{"IMPLEMENTATION"} and $capa{"IMPLEMENTATION"} =~ /^Cyrus timsieved v2\.3\.(\d+)/ and $1 >= 13
exists $capa{"IMPLEMENTATION"} $capa{"IMPLEMENTATION"} =~ /^Cyrus timsieved v2\.3\.(\d+)/ $1 >= 13
Truth table0%0/4
A and B and C
A= exists $capa{"IMPLEMENTATION"}B= $capa{"IMPLEMENTATION"} =~ /^Cyrus timsieved v2\.3\.(\d+)/C= $1 >= 13
ABCresult
0XX0
10X0
1100
1111
287 $capa{"IMPLEMENTATION"} =~ /^Cyrus timsieved v2\.3\.(\d+)/ and
288 $1 >= 13) {
289 0 _debug("--- Cyrus drops connection with dubious log msg if send NOOP, skip that");
290 0 $use_noop = 0;
291 }
292  
293 0 ▶ if ($use_noop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_noop) { } 0 0 0
294 0 my $noop_tag = "STARTTLS-RESYNC-CAPA";
295 0 $self->ssend(qq{NOOP "$noop_tag"});
296 #if ($capa{IMPLEMENTATION} =~ /dovecot/i) {
297 0 $self->_parse_capabilities(
298 sent_a_noop => $noop_tag,
299 # until_see_no => 0,
300 external_first => $prioritise_auth_external);
301 }
302 else {
303 0 $self->_parse_capabilities(
304 # until_see_no => 1,
305 external_first => $prioritise_auth_external);
306 }
307 0 ▶ unless (scalar keys %capa) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %capa) 0 0 0
308 0 $self->ssend("CAPABILITY");
309 0 $self->_parse_capabilities(
310 external_first => $prioritise_auth_external);
311 }
312 } elsif ($forbid_clearchan) {
313 0 die "TLS not offered, SASL confidentiality not supported in client.\n";
314 }
315  
316 0 my %authen_sasl_params;
317 0 $authen_sasl_params{callback}{user} = $user;
318 0 ▶if (defined $authzid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $authzid) 0 0 0
319 0 $authen_sasl_params{callback}{authname} = $authzid;
320 }
321 0 ▶if (defined $realm) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $realm) 0 0 0
322 # for compatibility, we set it as a callback AND as a property (below)
323 0 $authen_sasl_params{callback}{realm} = $realm;
324 }
325  
326  
327 0 $authen_sasl_params{callback}{pass} = $password;
328  
329  
330 $self->closedie("Do not have an authentication mechanism list\n")
331 0 ▶ unless ref($capa{SASL}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $capa{"SASL"} eq "ARRAY" 0 0 0
332 0 ▶if (defined $authmech) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $authmech) { } 0 0 0
333 0 $authmech = uc $authmech;
334 0 ▶ if (grep {$_ eq $authmech} map {uc $_} @{$capa{SASL}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $authmech;} map({uc $_;} @{$capa{'SASL'};})) { } 0 0 0
335 0 _debug("auth: will try requested SASL mechanism $authmech");
336 } else {
337 0 $self->closedie("Server does not offer SASL mechanism $authmech\n");
338 }
339 0 $authen_sasl_params{mechanism} = $authmech;
340 } else {
341 0 $authen_sasl_params{mechanism} = $raw_capabilities{SASL};
342 }
343  
344 0 my $sasl = Authen::SASL->new(%authen_sasl_params);
345 0 ▶die "SASL object init failed (local problem): $!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sasl 0 0 0
346 unless defined $sasl;
347  
348 0 my $secflags = 'noanonymous';
349 0 ▶$secflags .= ' noplaintext' if $forbid_clearauth;
Branch: 1 branch
BranchTrueFalseTotal
if $forbid_clearauth 0 0 0
350 0 ▶my $authconversation = $sasl->client_new('sieve', $server, $secflags)
Branch: 1 branch
BranchTrueFalseTotal
my $authconversation = $sasl->client_new("sieve", $server, $secflags) or die "SASL conversation init failed (local problem): $!\n" 0 0 0
351 or die "SASL conversation init failed (local problem): $!\n";
352 0 ▶if ($tls_bitlength > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tls_bitlength > 0) 0 0 0
353 0 $authconversation->property(externalssf => $tls_bitlength);
354 }
355 0 ▶if (defined $realm) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $realm) 0 0 0
356 0 $authconversation->property(realm => $realm);
357 }
358 {
359 0 ▶ my $sasl_m = $authconversation->mechanism()
Branch: 1 branch
BranchTrueFalseTotal
my $sasl_m = $authconversation->mechanism or die "Oh why can't I decide which auth mech to send?\n" 0 0 0
360 or die "Oh why can't I decide which auth mech to send?\n";
361 0 ▶ if ($sasl_m eq 'GSSAPI') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sasl_m eq "GSSAPI") 0 0 0
362 0 _debug("-A- GSSAPI sasl_m <temp>");
363 # gross hack, but it was bad of us to assume anything.
364 # It also means that we ignore anything specified by the
365 # user, which is good since it's Kerberos anyway.
366 # (Major Assumption Alert!)
367 0 $authconversation->callback(
368 user => undef,
369 pass => undef,
370 );
371 }
372  
373 0 my $sasl_tosend = $authconversation->client_start();
374 0 ▶ if ($authconversation->code()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authconversation->code) 0 0 0
375 0 my $emsg = $authconversation->error();
376 0 $self->closedie("SASL Error: $emsg\n");
377 }
378  
379 0 ▶ if (defined $sasl_tosend and length $sasl_tosend) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sasl_tosend and length $sasl_tosend) { } 0 0 0
Condition0%0/3
defined $sasl_tosend and length $sasl_tosend
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sasl_tosend and length $sasl_tosend
defined $sasl_tosend length $sasl_tosend
Truth table0%0/3
A and B
A= defined $sasl_tosendB= length $sasl_tosend
ABresult
0X0
100
111
380 0 my $mimedata = encode_base64($sasl_tosend, '');
381 0 my $mlen = length($mimedata);
382 0 $self->ssend ( qq!AUTHENTICATE "$sasl_m" {${mlen}+}! );
383 0 $self->ssend ( $mimedata );
384 } else {
385 0 $self->ssend ( qq{AUTHENTICATE "$sasl_m"} );
386 }
387 0 $self->sget();
388  
389 0 ▶ while ($_ !~ /^(OK|NO)(?:\s.*)?$/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ =~ /^(OK|NO)(?:\s.*)?$/m) 0 0 0
390 0 my $challenge;
391 0 ▶ if (/^"(.*)"\r?\n?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^"(.*)"\r?\n?$/) { } 0 0 0
392 0 $challenge = $1;
393 } else {
394 0 ▶ unless (/^\{(\d+)\+?}\r?$/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\{(\d+)\+?}\r?$/m) 0 0 0
395 0 $self->sfinish ( "*" );
396 0 $self->closedie ("Failure to parse server SASL response.\n");
397 }
398 0 ($challenge = $_) =~ s/^{\d+\+?}\r?\n?//;
399 }
400 0 $challenge = decode_base64($challenge);
401  
402 0 my $response = $authconversation->client_step($challenge);
403 0 ▶ if ($authconversation->code()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authconversation->code) 0 0 0
404 0 my $emsg = $authconversation->error();
405 0 $self->closedie("SASL Error: $emsg\n");
406 }
407 0 ▶ $response = '' unless defined $response; # sigh
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response 0 0 0
408 0 my $senddata = encode_base64($response, '');
409 0 my $sendlen = length $senddata;
410 0 $self->ssend ( "{$sendlen+}" );
411 # okay, we send a blank line here even for 0 length data
412 0 $self->ssend ( $senddata );
413 0 $self->sget();
414 }
415  
416 0 ▶ if (/^NO((?:\s.*)?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^NO((?:\s.*)?)$/) 0 0 0
417 0 $self->closedie_NOmsg("Authentication refused by server");
418 }
419 0 ▶ if (/^OK\s+\(SASL\s+\"([^"]+)\"\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^OK\s+\(SASL\s+\"([^"]+)\"\)$/) 0 0 0
420 # This _should_ be pre_sent with server-verification steps which
421 # in other profiles expect an empty response. But Authen::SASL
422 # doesn't let us confirm that we've finished authentication!
423 # The assumption seems to be that the server only verifies us
424 # so if it says "okay", we don't keep trying.
425 0 my $final_auth = decode_base64($1);
426 0 my $valid = $authconversation->client_step($final_auth);
427 # With Authen::SASL before 2.11 (..::Perl 1.06),
428 # Authen::SASL::Perl::DIGEST-MD5 module will complain at this
429 # final step:
430 # Server did not provide required field(s): algorithm nonce
431 # which is bogus -- it's not required or expected.
432 # Authen::SASL 2.11 fixes this, with ..::Perl 1.06
433 # We explicitly permit silent failure with the security
434 # implications because we require a new enough version of
435 # Authen::SASL at import time above and if someone removes
436 # that check, then on their head be it.
437 0 ▶ if ($authconversation->code()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authconversation->code) 0 0 0
438 0 my $emsg = $authconversation->error();
439 0 ▶ if ($Authen::SASL::Perl::VERSION >= 1.06) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Authen::SASL::Perl::VERSION >= "1.06") 0 0 0
440 0 $self->closedie("SASL Error: $emsg\n");
441 }
442 }
443 0 ▶ if (defined $valid and length $valid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid and length $valid) 0 0 0
Condition0%0/3
defined $valid and length $valid
!ll&&!rl&&r
000
MC/DC0%0/2
defined $valid and length $valid
defined $valid length $valid
Truth table0%0/3
A and B
A= defined $validB= length $valid
ABresult
0X0
100
111
444 0 $self->closedie("Server failed final verification [$valid]");
445 }
446 }
447  
448 }
449  
450 0 return $self;
451 };
452  
453 # destructor
454 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
455 0 my $self = shift;
456  
457 0 ▶ $self->sfinish() if $self->{_sock};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_sock"} 0 0 0
458 }
459  
460 #############
461 # public methods
462  
463 =head1 METHODS
464  
465 =head2 sock
466  
467 Usage : my $sock = $ServerSieve->sock();
468 Return : open socket
469 Argument : nothing
470 Purpose : access to socket
471  
472 =cut
473  
474 sub sock
475 0 ▶{
Subroutine sock: not called CC=1 SCAR=6.9
Pod documented
476 0 my $self = shift;
477 0 return $self->{_sock};
478 }
479  
480 =head2 capabilities
481  
482 Usage : my $script_capa = $ServerSieve->capabilities();
483 Return : string with white space separator
484 Argument : nothing
485 Purpose : retrieve sieve script capabilities
486  
487 =cut
488  
489 sub capabilities
490 0 ▶{
Subroutine capabilities: not called CC=1 SCAR=6.9
Pod documented
491 0 my $self = shift;
492 0 return $self->{_capa};
493 }
494  
495 =head2 list
496  
497 Usage :
498 foreach my $script ( $ServerSieve->list() ) {
499 print $script->{name}." ".$script->{status}."\n";
500 };
501 Return : array of hash with names and status scripts for current user
502 Argument : nothing
503 Purpose : list available scripts on server
504  
505 =cut
506  
507 sub list
508 0 ▶{
Subroutine list: not called CC=4 SCAR=30.0
Pod documented
509 0 my $self = shift;
510 0 my @list_scripts;
511 0 my $sock = $self->{_sock};
512 0 $self->ssend("LISTSCRIPTS");
513 0 $self->sget();
514 0 ▶ while (/^\"/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\"/) 0 0 0
515 0 my $line = $_;
516 0 ▶ my $name = $1 if ($line =~ m/\"(.*?)\"/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\"(.*?)\"/ 0 0 0
517 0 ▶ my $status = ($line =~ m/ACTIVE/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /ACTIVE/ ? : 0 0 0
518 0 my %script = (name => $name, status => $status);
519 0 push @list_scripts,\%script;
520 0 $self->sget();
521 }
522  
523 0 return @list_scripts;
524 }
525  
526 =head2 put
527  
528 Usage : $ServerSieve->put($name,$script);
529 Return : 1 on success, 0 on missing name or script
530 Argument : name, script
531 Purpose : put script on server
532  
533 =cut
534  
535 sub put
536 0 ▶{
Subroutine put: not called CC=4 SCAR=30.0
Pod documented
537 0 my $self = shift;
538 0 my $name = shift;
539 0 my $script = shift;
540  
541 0 my $sock = $self->{_sock};
542  
543 0 my $size = length($script);
544 0 ▶ return 0 if (!$size || !$name);
Branch: 1 branch
BranchTrueFalseTotal
unless $size and $name 0 0 0
Condition0%0/3
$size and $name
!ll&&!rl&&r
000
MC/DC0%0/2
$size and $name
$size $name
Truth table0%0/3
A and B
A= $sizeB= $name
ABresult
0X0
100
111
545  
546 0 $self->ssend('PUTSCRIPT "'.$name.'" {'.$size.'+}');
547 0 $self->ssend('-noeol', $script);
548 0 $self->ssend('');
549 0 $self->sget();
550  
551 0 ▶ unless (/^OK((?:\s.*)?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^OK((?:\s.*)?)$/) 0 0 0
552 0 warn "PUTSCRIPT(".$name.") failed: $_\n";
553 }
554  
555 0 return 1;
556 }
557  
558 =head2 get
559  
560 Usage : my $script = $ServerSieve->get($name);
561 Return : 0 on missing name, string with script on success
562 Argument : name
563 Purpose : put script on server
564  
565 =cut
566  
567 sub get
568 0 ▶{
Subroutine get: not called CC=7 SCAR=40.3
Pod documented
569 0 my $self = shift;
570 0 my $name = shift;
571
572 0 ▶ return 0 if (!$name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
573  
574 0 $self->ssend("GETSCRIPT \"$name\"");
575 0 $self->sget();
576 0 ▶ if (/^NO((?:\s.*)?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^NO((?:\s.*)?)$/) 0 0 0
577 0 die_NOmsg($1, qq{Script "$name" not returned by server});
578 }
579 0 ▶ if (/^OK((?:\s.*)?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^OK((?:\s.*)?)$/) 0 0 0
580 0 warn qq{Empty script "$name"? Not saved.\n};
581 0 return 0;
582 }
583 0 ▶ unless (/^\{(\d+)\+?}\r?$/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\{(\d+)\+?}\r?$/m) 0 0 0
584 0 die "QUIT:Failed to parse server response to GETSCRIPT";
585 }
586 0 my $contentdata = $_;
587 0 $self->sget();
588 0 ▶ while (/^$/) { $self->sget(); } # extra newline but only for GETSCRIPT?
Branch: 1 branch
BranchTrueFalseTotal
if (/^$/) 0 0 0
589 0 ▶ unless (/^OK((?:\s.*)?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^OK((?:\s.*)?)$/) 0 0 0
590 0 die_NOmsg($_, "Script retrieval not successful, not saving");
591 }
592 0 $contentdata =~ s/^\{\d+\+?}\r?\n?//m;
593
594 0 return $contentdata;
595 }
596  
597 =head2 activate
598  
599 Usage : $ServerSieve->activate($name);
600 Return : 0 on pb, 1 on success
601 Argument : name
602 Purpose : set named script active and switch other scripts to unactive
603  
604 =cut
605  
606 0 ▶sub activate {
Subroutine activate: not called CC=2 SCAR=17.9
Pod documented
607 0 my $self = shift;
608 0 my $name = shift;
609  
610 0 $self->ssend("SETACTIVE \"$name\"");
611 0 $self->sget();
612 0 ▶ unless (/^OK((?:\s.*)?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^OK((?:\s.*)?)$/) 0 0 0
613 0 warn "SETACTIVE($name) failed: $_\n";
614 0 return 0;
615 }
616  
617 0 return 1;
618 }
619  
620 =head2 deactivate
621  
622 Usage : $ServerSieve->deactivate();
623 Return : activate response
624 Argument : nothing
625 Purpose : stop sieve processing, deactivate all scripts
626  
627 =cut
628  
629 0 ▶sub deactivate {
Subroutine deactivate: not called CC=1 SCAR=6.9
Pod documented
630 0 my $self = shift;
631
632 0 return $self->activate("");
633 }
634  
635 =head2 delete
636  
637 Usage : $ServerSieve->delete($name);
638 Return : 0 on missing name, 1 on success
639 Argument : name
640 Purpose : delete script on server
641  
642 =cut
643  
644 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
645 0 my $self = shift;
646 0 my $name = shift;
647
648 0 ▶ return 0 if (!$name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
649  
650 0 $self->ssend("DELETESCRIPT \"$name\"");
651 0 $self->sget();
652 0 ▶ unless (/^OK((?:\s.*)?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^OK((?:\s.*)?)$/) 0 0 0
653 0 warn "DELETESCRIPT($name) failed: $_\n";
654 0 return 0;
655 }
656  
657 0 return 1;
658 }
659  
660 ###################
661 # private methods
662 #functions
663  
664 sub _parse_capabilities
665 0 ▶{
Subroutine _parse_capabilities: not called CC=29 SCAR=66.9
666 0 my $self = shift;
667 0 my $sock = $self->{_sock};
668 0 local %_ = @_;
669 0 my $external_first = 0;
670 0 ▶ $external_first = $_{external_first} if exists $_{external_first};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_{"external_first"} 0 0 0
671  
672 0 my @double_checks;
673 0 %raw_capabilities = ();
674 0 %capa = ();
675 0 ▶ while (<$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sock)) 0 0 0
676 0 chomp; s/\s*$//;
677 0 ▶ _received() unless /^OK\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^OK\b/ 0 0 0
678 0 ▶ if (/^OK\b/) {
Branch: 5 branches
BranchTrueFalseTotal
if (/^OK\b/) { } 0 0 0
elsif (/^\"([^"]+)\"\s+\"(.*)\"$/) { } 0 0 0
elsif (/^\"([^"]+)\"$/) { } 0 0 0
elsif (/^NO\b/) { } 0 0 0
elsif (/^BYE\b(.*)/) { } 0 0 0
679 0 $self->sget('-firstline', $_);
680 0 ▶ last unless exists $_{sent_a_noop};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_{"sent_a_noop"} 0 0 0
681 # See large comment below in STARTTLS explaining the
682 # resync problem to understand why this is here.
683 0 my $end_tag = $_{sent_a_noop};
684 0 ▶ unless (defined $end_tag and length $end_tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $end_tag and length $end_tag) 0 0 0
Condition0%0/3
defined $end_tag and length $end_tag
!ll&&!rl&&r
000
MC/DC0%0/2
defined $end_tag and length $end_tag
defined $end_tag length $end_tag
Truth table0%0/3
A and B
A= defined $end_tagB= length $end_tag
ABresult
0X0
100
111
685 # Default tag in absense of client-specified
686 # tag MUST be NOOP (2.11.2. NOOP Command)
687 0 $self->closedie("Internal error: sent_a_noop without tag\n");
688 }
689 # Play crude, just look for the tag anywhere in the
690 # response, honouring only word boundaries. It's our
691 # responsibility to make the tag long enough that this
692 # works without tokenising.
693 0 ▶ if ($_ =~ m/\b\Q${end_tag}\E\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /\b\Q$end_tag\E\b/) 0 0 0
694 0 return;
695 }
696 # Okay, that's the "server understands NOOP" case, for
697 # which the server should have advertised the
698 # capability prior to TLS (and so subject to
699 # tampering); we play fast and loose, so have to cover
700 # the NO case below too.
701 } elsif (/^\"([^"]+)\"\s+\"(.*)\"$/) {
702 0 my ($k, $v) = (uc($1), $2);
703 0 ▶ unless (length $v) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $v) 0 0 0
704 0 ▶ unless (exists $capa_permit_empty{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $capa_permit_empty{$k}) 0 0 0
705 0 warn "Empty \"$k\" capability spec not permitted: $_\n";
706 # Don't keep the advertised capability unless
707 # it has some value which is needed. Eg,
708 # NOTIFY must list a mechanism to be useful.
709 0 next;
710 }
711 0 ▶ if (defined $capa_permit_empty{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $capa_permit_empty{$k}) 0 0 0
712 0 push @double_checks, $capa_permit_empty{$k};
713 }
714 }
715 0 ▶ if (exists $capa{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $capa{$k}) 0 0 0
716 # won't catch if the first instance was ignored for an
717 # impermissably empty value; by this point though we
718 # would already have issued a warning and the server
719 # is so fubar that it's not worth worrying about.
720 0 warn "Protocol violation. Already seen capability \"$k\".\n" .
721 "Ignoring second instance and continuing.\n";
722 0 next;
723 }
724 0 $raw_capabilities{$k} = $v;
725 0 $capa{$k} = $v;
726 0 ▶ if (exists $capa_dosplit{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $capa_dosplit{$k}) 0 0 0
727 0 $capa{$k} = [ split /\s+/, $v ];
728 }
729 } elsif (/^\"([^"]+)\"$/) {
730 0 $raw_capabilities{$1} = '';
731 0 $capa{$1} = 1;
732 } elsif (/^NO\b/) {
733 #return if exists $_{until_see_no};
734 0 ▶ return if exists $_{sent_a_noop};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_{"sent_a_noop"} 0 0 0
735 0 warn "Unhandled server line: $_\n"
736 } elsif (/^BYE\b(.*)/) {
737 #closedie_NOmsg( $1,
738 0 die (
739 "Server said BYE when we expected capabilities.\n");
740 } else {
741 0 warn "Unhandled server line: $_\n"
742 }
743 };
744  
745 die ( "Server does not return SIEVE capability, unable to continue.\n" )
746 0 ▶ unless exists $capa{SIEVE};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $capa{"SIEVE"} 0 0 0
747 warn "Server does not return IMPLEMENTATION capability.\n"
748 0 ▶ unless exists $capa{IMPLEMENTATION};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $capa{"IMPLEMENTATION"} 0 0 0
749  
750 0 foreach my $check_sub (@double_checks) {
751 0 $check_sub->($sock, \%capa, \%raw_capabilities);
752 }
753  
754 0 ▶ if (grep {lc($_) eq 'enotify'} @{$capa{SIEVE}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {lc $_ eq "enotify";} @{$capa{"SIEVE"};}) 0 0 0
755 0 ▶ unless (exists $capa{NOTIFY}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $capa{"NOTIFY"}) 0 0 0
756 0 warn "enotify extension present, NOTIFY capability missing\n" .
757 "This violates MANAGESIEVE specification.\n" .
758 "Continuing anyway.\n";
759 }
760 }
761  
762 0 ▶ if (exists $capa{SASL} and $external_first
Branch: 1 branch
BranchTrueFalseTotal
if (exists $capa{"SASL"} and $external_first and grep {uc $_ eq "EXTERNAL";} @{$capa{"SASL"};}) 0 0 0
Condition0%0/6
exists $capa{"SASL"} and $external_first and grep {uc $_ eq "EXTERNAL";} @{$capa{"SASL"};}
!ll&&!rl&&r
000
exists $capa{"SASL"} and $external_first
!ll&&!rl&&r
000
MC/DC0%0/3
exists $capa{"SASL"} and $external_first and grep {uc $_ eq "EXTERNAL";} @{$capa{"SASL"};}
exists $capa{"SASL"} $external_first grep {uc $_ eq "EXTERNAL";} @{$capa{"SASL"};}
Truth table0%0/4
A and B and C
A= exists $capa{"SASL"}B= $external_firstC= grep {uc $_ eq "EXTERNAL";} @{$capa{"SASL"};}
ABCresult
0XX0
10X0
1100
1111
763 0 and grep {uc($_) eq 'EXTERNAL'} @{$capa{SASL}}) {
764 # We do two things. We shift the EXTERNAL to the head of the
765 # list, suggesting that it's the server's preferred choice.
766 # We then mess around inside the Authen::SASL::Perl::EXTERNAL
767 # private stuff (name starts with an underscore) to bump up
768 # its priority -- for some reason, the method which is not
769 # interactive and says "use information already available"
770 # is less favoured than some others.
771 0 _debug("auth: shifting EXTERNAL to start of mechanism list");
772 0 my @sasl = ('EXTERNAL');
773 0 foreach (@{$capa{SASL}}) {
774 0 ▶ push @sasl, $_ unless uc($_) eq 'EXTERNAL';
Branch: 1 branch
BranchTrueFalseTotal
unless uc $_ eq "EXTERNAL" 0 0 0
775 }
776 0 $capa{SASL} = \@sasl;
777 0 $raw_capabilities{SASL} = join(' ', @sasl);
778 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
779 0 ▶ $Authen::SASL::Perl::EXTERNAL::{_order} = sub { 10 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
780 }
781 }
782  
783 sub _debug
784 0 ▶{
Subroutine _debug: not called CC=2 SCAR=17.9
785 0 ▶ return unless $DEBUGGING;
Branch: 1 branch
BranchTrueFalseTotal
unless $DEBUGGING 0 0 0
786 0 print STDERR "$_[0]\n";
787 }
788  
789 0 ▶sub _diag {
Subroutine _diag: not called CC=1 SCAR=6.9
790 0 my ($prefix, $data) = @_;
791 0 $data =~ s/\r/\\r/g; $data =~ s/\n/\\n/g; $data =~ s/\t/\\t/g;
792 0 $data =~ s/([^[:graph:] ])/sprintf("%%%02X", ord $1)/eg;
793 0 _debug "$prefix $data";
794 }
795 0 ▶sub _sent { my $t = defined $_[0] ? $_[0] : $_; _diag('>>>', $t) }
Subroutine _sent: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
796 0 ▶sub _received { my $t = defined $_[0] ? $_[0] : $_; _diag('<<<', $t) }
Subroutine _received: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
797  
798 #sub _sent { $_[0] = $_ unless defined $_[0]; _debug ">>> $_[0]"; }
799 #sub _received { $_[0] = $_ unless defined $_[0]; _debug "<<< $_[0]"; }
800  
801 # ######################################################################
802 # minor public routines
803  
804 =head1 Minor public methods
805  
806 =head2 ssend
807  
808 Usage : $self->ssend("GETSCRIPT \"$name\"");
809  
810 =cut
811  
812 sub ssend
813 0 ▶{
Subroutine ssend: not called CC=4 SCAR=30.0
Pod documented
814 0 my $self = shift;
815 0 my $sock = $self->{_sock};
816
817 0 my $eol = "\r\n";
818 0 ▶ if (defined $_[0] and $_[0] eq '-noeol') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] eq "-noeol") 0 0 0
Condition0%0/3
defined $_[0] and $_[0] eq "-noeol"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and $_[0] eq "-noeol"
defined $_[0] $_[0] eq "-noeol"
Truth table0%0/3
A and B
A= defined $_[0]B= $_[0] eq "-noeol"
ABresult
0X0
100
111
819 0 shift;
820 0 $eol = '';
821 }
822 0 foreach my $l (@_) {
823 0 $sock->print("$l$eol");
824 # yes, the _debug output can have extra blank lines if supplied -noeol because
825 # they're already pre_sent. Rather than mess around to tidy it up, I'm leaving
826 # it because it's _debug output, not UI or protocol text.
827 0 _sent ( "$l$eol" );
828 }
829 }
830  
831 =head2 sget
832  
833 Usage:
834 $self->sget();
835 unless (/^OK((?:\s.*)?)$/) {
836 warn "SETACTIVE($name) failed: $_\n";
837 return 0;
838 }
839  
840 =cut
841  
842 sub sget
843 0 ▶{
Subroutine sget: not called CC=13 SCAR=52.0
Pod documented
844 0 my $self = shift;
845 0 my $l = undef;
846 0 my $sock = $self->{_sock};
847 0 my $dochomp = 1;
848 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
849 0 my $t = shift;
850 0 ▶ next unless defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 0 0 0
851 0 ▶ if ($t eq '-nochomp') { $dochomp = 0; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "-nochomp") 0 0 0
852 0 ▶ if ($t eq '-firstline') {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "-firstline") 0 0 0
853 0 ▶ die "Missing sget -firstline parameter"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
854 unless defined $_[0];
855 0 $l = $_[0];
856 0 shift;
857 0 next;
858 }
859 0 die "Unknown sget parameter [$t]";
860 }
861 0 ▶ $l = $sock->getline() unless defined $l;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l 0 0 0
862 0 ▶ unless (defined $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $l) 0 0 0
863 0 _debug "... no line read, connection dropped?";
864 0 die "Connection dropped unexpectedly when trying to read.\n";
865 }
866 0 ▶ if ($l =~ /{(\d+)\+?}\s*\n?\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /{(\d+)\+?}\s*\n?\z/) 0 0 0
867 0 _debug("... literal string response, length $1");
868 0 my $len = $1;
869 0 ▶ if ($len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 0) { } 0 0 0
870 0 my $discard = $sock->getline();
871 } else {
872 0 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
873 0 my $extra = $sock->getline();
874 0 $len -= length($extra);
875 0 $l .= $extra;
876 }
877 }
878 0 $dochomp = 0;
879 }
880 0 ▶ if ($dochomp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dochomp) 0 0 0
881 0 chomp $l; $l =~ s/\s*$//;
882 }
883 0 _received($l);
884 0 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 0 0 0
885 0 return $l;
886 } else {
887 0 $_ = $l;
888 }
889 }
890  
891 =head2 sfinish
892  
893 send LOGOUT
894  
895 =cut
896  
897 sub sfinish
898 0 ▶{
Subroutine sfinish: not called CC=3 SCAR=24.8
Pod documented
899 0 my $self = shift;
900 0 my $sock = $self->{_sock};
901 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
902 0 $self->ssend($_[0]);
903 0 $self->sget();
904 }
905 0 $self->ssend("LOGOUT");
906 0 $self->sget();
907 0 ▶ if (/^OK/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^OK/) 0 0 0
908 0 undef $self->{_sock};
909 0 return 1;
910 };
911 }
912  
913 =head2 closedie
914  
915 send LOGOUT and die
916  
917 =cut
918  
919 sub closedie
920 0 ▶{
Subroutine closedie: not called CC=1 SCAR=6.9
Pod documented
921 0 my $self = shift;
922 0 my $sock = $self->{_sock};
923 0 my $e = $!;
924 0 $self->sfinish();
925 0 $! = $e;
926 0 die @_;
927 }
928  
929 =head2 closedie_NOmsg
930  
931 closedie whitout message
932  
933 =cut
934  
935 sub closedie_NOmsg
936 0 ▶{
Subroutine closedie_NOmsg: not called CC=1 SCAR=6.9
Pod documented
937 0 my $self = shift;
938 0 my $sock = $self->{_sock};
939 0 my $suffix = shift;
940 0 $self->sfinish();
941 0 die $suffix;
942 }
943  
944 =head2 die_NOmsg
945  
946 die
947  
948 =cut
949  
950 sub die_NOmsg
951 0 ▶{
Subroutine die_NOmsg: not called CC=2 SCAR=17.9
Pod documented
952 0 my $suffix = shift;
953 0 my $msg = shift;
954 0 ▶ if (length $suffix) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $suffix) { } 0 0 0
955 0 $msg .= ':' . $suffix . "\n";
956 } else {
957 0 $msg .= ".\n";
958 }
959 0 die $msg;
960 }
961  
962  
963 =head1 BUGS
964  
965 I don't try plain text or client certificate authentication.
966  
967 You can debug TLS connexion with openssl :
968  
969 openssl s_client -connect your.server.org:2000 -tls1 -CApath /etc/apache/ssl.crt/somecrt.crt -starttls imap
970  
971 See response in C<Verify return code:>
972  
973 Or with gnutls-cli
974  
975 gnutls-cli -s -p 4190 --crlf --insecure your.server.org
976  
977 Use Ctrl+D after STARTTLS to begin TLS negotiation
978  
979 =head1 SUPPORT
980  
981 Please report any bugs or feature requests to "bug-net-sieve at rt.cpan.org", or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Sieve>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
982  
983 =head1 AUTHOR
984  
985 Yves Agostini <yvesago@cpan.org>
986  
987 =head1 COPYRIGHT
988  
989 Copyright 2008-2017 Yves Agostini - <yvesago@cpan.org>
990  
991 This program is free software; you can redistribute
992 it and/or modify it under the same terms as Perl itself.
993  
994 The full text of the license can be found in the
995 LICENSE file included with this module.
996  
997 B<sieve-connect> source code is under a BSD-style license and re-licensed for Net::Sieve with permission of the author.
998  
999 =head1 SEE ALSO
1000  
1001 L<Net::Sieve::Script>
1002  
1003 =cut
1004  
1005 #################### main pod documentation end ###################
1006  
1007  
1008 2 1;
1009 # The preceding line will help the module return a true value
1010  
↑ Summary