15 |
#include <openssl/ssl.h> |
#include <openssl/ssl.h> |
16 |
#include <openssl/err.h> |
#include <openssl/err.h> |
17 |
#include <openssl/x509.h> |
#include <openssl/x509.h> |
18 |
|
#endif |
19 |
|
|
20 |
|
|
21 |
static SSL *ssl; |
extern long timeout; |
|
#endif |
|
22 |
|
|
23 |
static int sock; |
static int sock; |
24 |
|
|
25 |
|
#ifdef SSL_TLS |
26 |
|
static SSL *ssl; |
27 |
|
#endif |
28 |
|
|
29 |
|
|
30 |
/* |
/* |
31 |
* Connect to mail server. |
* Connect to mail server. |
33 |
#ifndef SSL_TLS |
#ifndef SSL_TLS |
34 |
int init_connection(char *serv, unsigned short int port) |
int init_connection(char *serv, unsigned short int port) |
35 |
#else |
#else |
36 |
int init_connection(char *serv, unsigned short int port, unsigned int protocol) |
int init_connection(char *serv, unsigned short int port, |
37 |
|
unsigned int protocol) |
38 |
#endif |
#endif |
39 |
{ |
{ |
40 |
struct sockaddr_in sa; |
struct sockaddr_in sa; |
48 |
error("imapfilter: create socket; %s\n", strerror(errno)); |
error("imapfilter: create socket; %s\n", strerror(errno)); |
49 |
return ERROR_NETWORK; |
return ERROR_NETWORK; |
50 |
} |
} |
|
|
|
51 |
if (!(he = gethostbyname(serv))) { |
if (!(he = gethostbyname(serv))) { |
52 |
error("imapfilter: get network host entry of %s; %s\n", serv, |
error("imapfilter: get network host entry of %s; %s\n", serv, |
53 |
strerror(errno)); |
strerror(errno)); |
61 |
xstrncpy(serv, he->h_name, SERVER_LEN - 1); |
xstrncpy(serv, he->h_name, SERVER_LEN - 1); |
62 |
|
|
63 |
if (connect(sock, (struct sockaddr *) & sa, sizeof(struct sockaddr))) { |
if (connect(sock, (struct sockaddr *) & sa, sizeof(struct sockaddr))) { |
64 |
error("imapfilter: initiating connection to %s; %s\n", serv, strerror(errno)); |
error("imapfilter: initiating connection to %s; %s\n", serv, |
65 |
|
strerror(errno)); |
66 |
close_connection(); |
close_connection(); |
67 |
return ERROR_NETWORK; |
return ERROR_NETWORK; |
68 |
} |
} |
124 |
|
|
125 |
e = SSL_connect(ssl); |
e = SSL_connect(ssl); |
126 |
|
|
127 |
if (e <= 0) { |
if (e < 0) { |
128 |
error("imapfilter: initiating SSL connection; %s", |
error("imapfilter: initiating SSL connection; %s", |
129 |
ERR_error_string(e, NULL)); |
ERR_error_string(e, NULL)); |
130 |
return ERROR_SSL; |
return ERROR_SSL; |
188 |
int flags, r, s; |
int flags, r, s; |
189 |
fd_set fds; |
fd_set fds; |
190 |
struct timeval tv; |
struct timeval tv; |
191 |
|
struct timeval *tvp = NULL; |
192 |
|
|
193 |
r = 0; |
r = 0; |
194 |
s = 1; |
s = 1; |
195 |
|
|
196 |
memset(buf, 0, RESPONSE_BUF); |
memset(buf, 0, RESPONSE_BUF); |
197 |
|
|
198 |
tv.tv_sec = 60; |
if (timeout >= 0) { |
199 |
tv.tv_usec = 0; |
tv.tv_sec = timeout; |
200 |
|
tv.tv_usec = 0; |
201 |
|
tvp = &tv; |
202 |
|
} |
203 |
flags = fcntl(sock, F_GETFL, 0); |
flags = fcntl(sock, F_GETFL, 0); |
204 |
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
205 |
|
|
209 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
210 |
if (ssl) { |
if (ssl) { |
211 |
if (SSL_pending(ssl) |
if (SSL_pending(ssl) |
212 |
|| ((s = select(sock + 1, &fds, NULL, NULL, &tv)) > 0 |
|| ((s = select(sock + 1, &fds, NULL, NULL, tvp)) > 0 |
213 |
&& FD_ISSET(sock, &fds))) |
&& FD_ISSET(sock, &fds))) |
214 |
r = SSL_read(ssl, buf, RESPONSE_BUF - 1); |
r = SSL_read(ssl, buf, RESPONSE_BUF - 1); |
215 |
} else |
} else |
216 |
#endif |
#endif |
217 |
if ((s = select(sock + 1, &fds, NULL, NULL, &tv)) > 0 |
if ((s = select(sock + 1, &fds, NULL, NULL, tvp)) > 0 |
218 |
&& FD_ISSET(sock, &fds)) |
&& FD_ISSET(sock, &fds)) |
219 |
r = read(sock, buf, RESPONSE_BUF - 1); |
r = read(sock, buf, RESPONSE_BUF - 1); |
220 |
|
|