20 |
|
|
21 |
long timeout = -1; /* Server non-response timeout in seconds. */ |
long timeout = -1; /* Server non-response timeout in seconds. */ |
22 |
|
|
23 |
static int sock; |
int sockpri = -1; /* Main socket used mostly. */ |
24 |
|
int sockaux = -1; /* Auxiliary socked used when another |
25 |
|
connection is needed. */ |
26 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
27 |
static SSL *ssl; |
static SSL *ssl; |
28 |
#endif |
#endif |
31 |
/* |
/* |
32 |
* Connect to mail server. |
* Connect to mail server. |
33 |
*/ |
*/ |
34 |
int init_connection(char *serv, unsigned short int port, |
int init_connection(int *sock, char *serv, unsigned short int port, |
35 |
unsigned int protocol) |
unsigned int protocol) |
36 |
{ |
{ |
37 |
struct sockaddr_in sa; |
struct sockaddr_in sa; |
46 |
|
|
47 |
memset((char *) &sa, 0, sizeof(struct sockaddr_in)); |
memset((char *) &sa, 0, sizeof(struct sockaddr_in)); |
48 |
|
|
49 |
sock = socket(PF_INET, SOCK_STREAM, 0); |
*sock = socket(PF_INET, SOCK_STREAM, 0); |
50 |
|
|
51 |
if (sock < 0) { |
if (*sock < 0) { |
52 |
error("imapfilter: create socket; %s\n", strerror(errno)); |
error("imapfilter: create socket; %s\n", strerror(errno)); |
53 |
return ERROR_NETWORK; |
return ERROR_NETWORK; |
54 |
} |
} |
55 |
if (!(he = gethostbyname(serv))) { |
if (!(he = gethostbyname(serv))) { |
56 |
error("imapfilter: get network host entry of %s; %s\n", serv, |
error("imapfilter: get network host entry of %s; %s\n", serv, |
57 |
strerror(errno)); |
strerror(errno)); |
58 |
close_connection(); |
close_connection(sock); |
59 |
return ERROR_NETWORK; |
return ERROR_NETWORK; |
60 |
} |
} |
61 |
sa.sin_family = AF_INET; |
sa.sin_family = AF_INET; |
64 |
|
|
65 |
xstrncpy(serv, he->h_name, SERVER_LEN - 1); |
xstrncpy(serv, he->h_name, SERVER_LEN - 1); |
66 |
|
|
67 |
if (connect(sock, (struct sockaddr *) & sa, sizeof(struct sockaddr))) { |
if (connect(*sock, (struct sockaddr *) & sa, sizeof(struct sockaddr))) { |
68 |
error("imapfilter: initiating connection to %s; %s\n", serv, |
error("imapfilter: initiating connection to %s; %s\n", serv, |
69 |
strerror(errno)); |
strerror(errno)); |
70 |
close_connection(); |
close_connection(sock); |
71 |
return ERROR_NETWORK; |
return ERROR_NETWORK; |
72 |
} |
} |
73 |
log_info(LOG_SERVER, serv); |
log_info(LOG_SERVER, serv); |
74 |
|
|
75 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
76 |
if (protocol != SSL_DISABLED) |
if (protocol != SSL_DISABLED) |
77 |
if (!ssl_init(protocol)) { |
if (!ssl_init(sock, protocol)) { |
78 |
info("Connected to %s using %s.\n", serv, SSL_get_cipher(ssl)); |
info("Connected to %s using %s.\n", serv, SSL_get_cipher(ssl)); |
79 |
return 0; |
return 0; |
80 |
} else |
} else |
93 |
/* |
/* |
94 |
* Initialize Secure Socket Layer connection. |
* Initialize Secure Socket Layer connection. |
95 |
*/ |
*/ |
96 |
int ssl_init(unsigned int protocol) |
int ssl_init(int *sock, unsigned int protocol) |
97 |
{ |
{ |
98 |
int e; |
int e; |
99 |
unsigned int i, n = 0; |
unsigned int i, n = 0; |
124 |
if (!(ssl = SSL_new(ctx))) |
if (!(ssl = SSL_new(ctx))) |
125 |
return ERROR_SSL; |
return ERROR_SSL; |
126 |
|
|
127 |
SSL_set_fd(ssl, sock); |
SSL_set_fd(ssl, *sock); |
128 |
|
|
129 |
e = SSL_connect(ssl); |
e = SSL_connect(ssl); |
130 |
|
|
167 |
/* |
/* |
168 |
* Disconnect from mail server. |
* Disconnect from mail server. |
169 |
*/ |
*/ |
170 |
int close_connection(void) |
int close_connection(int *sock) |
171 |
{ |
{ |
172 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
173 |
if (ssl) { |
if (ssl) { |
176 |
} |
} |
177 |
#endif |
#endif |
178 |
|
|
179 |
if (sock >= 0 && close(sock)) { |
if (*sock >= 0 && close(*sock)) { |
180 |
error("imapfilter: closing socket; %s\n", strerror(errno)); |
error("imapfilter: closing socket; %s\n", strerror(errno)); |
181 |
return ERROR_NETWORK; |
return ERROR_NETWORK; |
182 |
} else { |
} else { |
183 |
sock = -1; |
*sock = -1; |
184 |
return 0; |
return 0; |
185 |
} |
} |
186 |
} |
} |
189 |
/* |
/* |
190 |
* Read data from socket. |
* Read data from socket. |
191 |
*/ |
*/ |
192 |
int socket_read(char *buf) |
int socket_read(int *sock, char *buf) |
193 |
{ |
{ |
194 |
int flags, r, s; |
int f, r, s; |
195 |
fd_set fds; |
fd_set fds; |
196 |
struct timeval tv; |
struct timeval tv; |
197 |
struct timeval *tvp = NULL; |
struct timeval *tvp = NULL; |
206 |
tv.tv_usec = 0; |
tv.tv_usec = 0; |
207 |
tvp = &tv; |
tvp = &tv; |
208 |
} |
} |
209 |
flags = fcntl(sock, F_GETFL, 0); |
f = fcntl(*sock, F_GETFL, 0); |
210 |
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
fcntl(*sock, F_SETFL, f | O_NONBLOCK); |
211 |
|
|
212 |
FD_ZERO(&fds); |
FD_ZERO(&fds); |
213 |
FD_SET(sock, &fds); |
FD_SET(*sock, &fds); |
214 |
|
|
215 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
216 |
if (ssl) { |
if (ssl) { |
217 |
if (SSL_pending(ssl) |
if (SSL_pending(ssl) |
218 |
|| ((s = select(sock + 1, &fds, NULL, NULL, tvp)) > 0 |
|| ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0 |
219 |
&& FD_ISSET(sock, &fds))) |
&& FD_ISSET(*sock, &fds))) |
220 |
r = SSL_read(ssl, buf, RESPONSE_BUF - 1); |
r = SSL_read(ssl, buf, RESPONSE_BUF - 1); |
221 |
} else |
} else |
222 |
#endif |
#endif |
223 |
if ((s = select(sock + 1, &fds, NULL, NULL, tvp)) > 0 |
if ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0 |
224 |
&& FD_ISSET(sock, &fds)) |
&& FD_ISSET(*sock, &fds)) |
225 |
r = read(sock, buf, RESPONSE_BUF - 1); |
r = read(*sock, buf, RESPONSE_BUF - 1); |
226 |
|
|
227 |
fcntl(sock, F_SETFL, flags); |
fcntl(*sock, F_SETFL, f); |
228 |
|
|
229 |
if (s == -1) |
if (s == -1) |
230 |
fatal(ERROR_NETWORK, "imapfilter: waiting input from socket; %s\n", |
fatal(ERROR_NETWORK, "imapfilter: waiting input from socket; %s\n", |
251 |
/* |
/* |
252 |
* Write data to socket. |
* Write data to socket. |
253 |
*/ |
*/ |
254 |
int socket_write(char *data) |
int socket_write(int *sock, char *data) |
255 |
{ |
{ |
256 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
257 |
int e; |
int e; |
264 |
ERR_error_string(e, NULL)); |
ERR_error_string(e, NULL)); |
265 |
} else |
} else |
266 |
#endif |
#endif |
267 |
if (write(sock, data, strlen(data)) == -1) |
if (write(*sock, data, strlen(data)) == -1) |
268 |
fatal(ERROR_NETWORK, "imapfilter: sending data; %s", |
fatal(ERROR_NETWORK, "imapfilter: sending data; %s", |
269 |
strerror(errno)); |
strerror(errno)); |
270 |
|
|