aboutsummaryrefslogtreecommitdiff
path: root/mail/p5-IMAP-Sieve/files/patch-Sieve.pm
blob: 47ad9b937b5891e93326c8b983e4668f54f6799a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
--- Sieve.pm.orig	Sun Jun 24 05:42:48 2001
+++ Sieve.pm	Sat Mar 10 15:37:46 2007
@@ -93,7 +93,7 @@
 							 Proto => 'tcp',
 							 Reuse => 1); })
     	{
-		$self->_error("initialize", "could'nt establish a Sieve connection to",$self->{'Server'});				
+		$self->_error("initialize", "couldn't establish a Sieve connection to",$self->{'Server'});				
 		return;
     	}
     } # if SSL
@@ -110,6 +110,7 @@
     if (/\r$/) {
 	chop;
     }
+    my $starttls = 0;
     if (/IMPLEMENTATION/) {
 	$self->{'Implementation'}=$1 if /^"IMPLEMENTATION" +"(.*)"/;
     	#version 2 of cyrus imap/timsieved
@@ -119,6 +120,7 @@
         while (!/^OK/) {
 	   $self->{'Capability'}=$1 if /^"SASL" +"(.*)"/;
 	   $self->{'Sieve'}=$1 if /^"SIEVE" +"(.*)"/;
+	   $starttls=1 if /"STARTTLS"/;
 	   $_ = $self->_read;
 ##	   $_=$self->_read;
 	}
@@ -126,8 +128,57 @@
     else {
 	$self->{'Capability'}=$_;
     }
+    if (!defined($self->{'SSL'}) && $starttls) {
+	my $cwd= cwd;
+	my %ssl_defaults = (
+			  'SSL_use_cert' => 0,
+			  'SSL_verify_mode' => 0x00,
+			  'SSL_key_file' => $cwd."/certs/client-key.pem",
+			  'SSL_cert_file' => $cwd."/certs/client-cert.pem",
+			  'SSL_ca_path' => $cwd."/certs",
+			  'SSL_ca_file' => $cwd."/certs/ca-cert.pem",
+			  'SSL_version' => 'tlsv1'
+			  );
+	my @ssl_options;
+	my $ssl_key;
+	my $key;
+	foreach $ssl_key (keys(%ssl_defaults)) {
+		if (!defined($self->{$ssl_key})) {
+			$self->{$ssl_key} = $ssl_defaults{$ssl_key};
+		}
+	}
+	foreach $ssl_key (keys(%{$self})) {
+		if ($ssl_key =~ /^SSL_/) {
+			push @ssl_options, $ssl_key,$self->{$ssl_key};
+		}
+	}
+        my $SSL_try="use IO::Socket::SSL";
+	eval $SSL_try;
+#	$IO::Socket::SSL::DEBUG = 4;
+	if ($self->{'Socket'}->isa('IO::Socket::SSL')) {
+	    $self->_error("starttls", "TLS already started");
+	    $self->close;
+	    return;
+	}
+	print $fh "STARTTLS\r\n";
+	$_ = $self->_read;
+	if (/OK/) {
+
+	    #IO::Socket::SSL::context_init({@ssl_options});
+	    if (!IO::Socket::SSL::socket_to_SSL($self->{'Socket'}, {@ssl_options}))
+    		{
+			$self->_error("starttls", "couldn't secure connection to",$self->{'Server'});
+	    		$self->close;
+			return;
+    		}
+	} else {
+	    $self->close;
+	    $self->_error("starttls", "Unknown error",$_);
+	    return;
+	}
+    }
     $userpass = "$self->{'Proxy'}\x00".$self->{'Login'}."\x00".$self->{'Password'};
-    $encode=encode_base64($userpass);
+    $encode=encode_base64($userpass, '');
     $len=length($encode);
     print $fh "AUTHENTICATE \"PLAIN\" {$len+}\r\n";
  
@@ -196,7 +247,7 @@
 			}
 		}
 		else {
-			if (($char eq "\n") or ($char eq "\r")) {
+			if ($char eq "\n") {
 				if (length($buffer) ==0) {
 					# remove any cr or nl leftover
 				}
@@ -204,7 +255,7 @@
 					return $buffer;
 				}
 			}
-			else {
+			elsif ($char ne "\r") {
 				$buffer.=$char;
 			}
 		}