24 |
int sockaux = -1; /* Auxiliary socked used when another |
int sockaux = -1; /* Auxiliary socked used when another |
25 |
connection is needed. */ |
connection is needed. */ |
26 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
27 |
static SSL *ssl; |
static SSL *sslpri = NULL; |
28 |
|
static SSL *sslaux = NULL; |
29 |
#endif |
#endif |
30 |
|
|
31 |
|
|
37 |
{ |
{ |
38 |
struct sockaddr_in sa; |
struct sockaddr_in sa; |
39 |
struct hostent *he; |
struct hostent *he; |
40 |
|
#ifdef SSL_TLS |
41 |
#ifndef SSL_TLS |
SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux); |
42 |
|
#else |
43 |
if (protocol != SSL_DISABLED) { |
if (protocol != SSL_DISABLED) { |
44 |
error("imapfilter: SSL not supported by this build\n"); |
error("imapfilter: SSL not supported by this build\n"); |
45 |
return ERROR_SSL; |
return ERROR_SSL; |
79 |
if (!ssl_init(sock, protocol)) { |
if (!ssl_init(sock, protocol)) { |
80 |
if (sock == &sockpri) |
if (sock == &sockpri) |
81 |
info("Connected to %s using %s.\n", serv, |
info("Connected to %s using %s.\n", serv, |
82 |
SSL_get_cipher(ssl)); |
SSL_get_cipher(*ssl)); |
83 |
return 0; |
return 0; |
84 |
} else |
} else |
85 |
return ERROR_SSL; |
return ERROR_SSL; |
86 |
else |
else |
87 |
ssl = NULL; |
*ssl = NULL; |
88 |
#endif |
#endif |
89 |
|
|
90 |
if (sock == &sockpri) |
if (sock == &sockpri) |
108 |
char *c; |
char *c; |
109 |
EVP_MD *evp; |
EVP_MD *evp; |
110 |
unsigned char digest[EVP_MAX_MD_SIZE]; |
unsigned char digest[EVP_MAX_MD_SIZE]; |
111 |
|
SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux); |
112 |
|
|
113 |
SSL_library_init(); |
SSL_library_init(); |
114 |
|
|
127 |
if (!(ctx = SSL_CTX_new(method))) |
if (!(ctx = SSL_CTX_new(method))) |
128 |
return ERROR_SSL; |
return ERROR_SSL; |
129 |
|
|
130 |
if (!(ssl = SSL_new(ctx))) |
if (!(*ssl = SSL_new(ctx))) |
131 |
return ERROR_SSL; |
return ERROR_SSL; |
132 |
|
|
133 |
SSL_set_fd(ssl, *sock); |
SSL_set_fd(*ssl, *sock); |
134 |
|
|
135 |
e = SSL_connect(ssl); |
e = SSL_connect(*ssl); |
136 |
|
|
137 |
if (e < 0) { |
if (e < 0) { |
138 |
error("imapfilter: initiating SSL connection; %s", |
error("imapfilter: initiating SSL connection; %s", |
140 |
return ERROR_SSL; |
return ERROR_SSL; |
141 |
} |
} |
142 |
/* Get server's certificate. */ |
/* Get server's certificate. */ |
143 |
if (!(cert = SSL_get_peer_certificate(ssl))) |
if (!(cert = SSL_get_peer_certificate(*ssl))) |
144 |
return ERROR_SSL; |
return ERROR_SSL; |
145 |
|
|
146 |
if (!(c = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0))) |
if (!(c = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0))) |
177 |
int close_connection(int *sock) |
int close_connection(int *sock) |
178 |
{ |
{ |
179 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
180 |
if (ssl) { |
SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux); |
181 |
SSL_shutdown(ssl); |
|
182 |
SSL_free(ssl); |
if (*ssl) { |
183 |
|
SSL_shutdown(*ssl); |
184 |
|
SSL_free(*ssl); |
185 |
|
*ssl = NULL; |
186 |
} |
} |
187 |
#endif |
#endif |
188 |
|
|
205 |
fd_set fds; |
fd_set fds; |
206 |
struct timeval tv; |
struct timeval tv; |
207 |
struct timeval *tvp = NULL; |
struct timeval *tvp = NULL; |
208 |
|
#ifdef SSL_TLS |
209 |
|
SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux); |
210 |
|
#endif |
211 |
|
|
212 |
r = 0; |
r = 0; |
213 |
s = 1; |
s = 1; |
226 |
FD_SET(*sock, &fds); |
FD_SET(*sock, &fds); |
227 |
|
|
228 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
229 |
if (ssl) { |
if (*ssl) { |
230 |
if (SSL_pending(ssl) |
if (SSL_pending(*ssl) |
231 |
|| ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0 |
|| ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0 |
232 |
&& FD_ISSET(*sock, &fds))) |
&& FD_ISSET(*sock, &fds))) |
233 |
r = SSL_read(ssl, buf, RESPONSE_BUF - 1); |
r = SSL_read(*ssl, buf, RESPONSE_BUF - 1); |
234 |
} else |
} else |
235 |
#endif |
#endif |
236 |
if ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0 |
if ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0 |
247 |
"imapfilter: timeout period expired while waiting data\n"); |
"imapfilter: timeout period expired while waiting data\n"); |
248 |
|
|
249 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
250 |
if (ssl) { |
if (*ssl) { |
251 |
if (r < 0) |
if (r < 0) |
252 |
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
253 |
ERR_error_string(r, NULL)); |
ERR_error_string(r, NULL)); |
268 |
{ |
{ |
269 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
270 |
int e; |
int e; |
271 |
|
SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux); |
272 |
|
|
273 |
if (ssl) { |
if (*ssl) { |
274 |
e = SSL_write(ssl, data, strlen(data)); |
e = SSL_write(*ssl, data, strlen(data)); |
275 |
if (e <= 0) |
if (e <= 0) |
276 |
fatal(ERROR_NETWORK, |
fatal(ERROR_NETWORK, |
277 |
"imapfilter: sending data; %s", |
"imapfilter: sending data; %s", |