1 |
#include <stdio.h> |
2 |
#include <unistd.h> |
3 |
#include <string.h> |
4 |
#include <errno.h> |
5 |
#include <netinet/in.h> |
6 |
#include <netdb.h> |
7 |
#include <sys/socket.h> |
8 |
#include <sys/select.h> |
9 |
#include <fcntl.h> |
10 |
|
11 |
#include "config.h" |
12 |
#include "imapfilter.h" |
13 |
|
14 |
#ifdef SSL_TLS |
15 |
#include <openssl/ssl.h> |
16 |
#include <openssl/err.h> |
17 |
#endif |
18 |
|
19 |
|
20 |
extern unsigned int options; |
21 |
extern conn_t connpri, connaux; |
22 |
|
23 |
long timeout = -1; /* Server non-response timeout in seconds. */ |
24 |
|
25 |
|
26 |
/* |
27 |
* Connect to mail server. |
28 |
*/ |
29 |
int |
30 |
init_connection(conn_t * conn, char *serv, unsigned short int port, |
31 |
unsigned int protocol) |
32 |
{ |
33 |
struct sockaddr_in sa; |
34 |
struct hostent *he; |
35 |
|
36 |
#ifndef SSL_TLS |
37 |
if (protocol != SSL_DISABLED) { |
38 |
error("SSL not supported by this build\n"); |
39 |
return ERROR_SSL; |
40 |
} |
41 |
#endif |
42 |
|
43 |
memset((char *)&sa, 0, sizeof(struct sockaddr_in)); |
44 |
|
45 |
conn->sock = socket(AF_INET, SOCK_STREAM, 0); |
46 |
|
47 |
if (conn->sock < 0) { |
48 |
error("create socket; %s\n", strerror(errno)); |
49 |
return ERROR_NETWORK; |
50 |
} |
51 |
if (!(he = gethostbyname(serv))) { |
52 |
error("get network host entry of %s; %s\n", serv, |
53 |
strerror(errno)); |
54 |
close_connection(conn); |
55 |
return ERROR_NETWORK; |
56 |
} |
57 |
sa.sin_family = AF_INET; |
58 |
sa.sin_port = htons(port); |
59 |
sa.sin_addr = *(struct in_addr *) he->h_addr; |
60 |
|
61 |
if (connect(conn->sock, (struct sockaddr *) & sa, |
62 |
sizeof(struct sockaddr))) { |
63 |
error("initiating connection to %s; %s\n", serv, |
64 |
strerror(errno)); |
65 |
close_connection(conn); |
66 |
return ERROR_NETWORK; |
67 |
} |
68 |
if (conn == &connpri) |
69 |
info("Connected to %s.\n", he->h_name); |
70 |
|
71 |
#ifdef SSL_TLS |
72 |
if (protocol != SSL_DISABLED) |
73 |
if (!init_secure_connection(conn, protocol)) |
74 |
return 0; |
75 |
else |
76 |
return ERROR_SSL; |
77 |
else |
78 |
conn->ssl = NULL; |
79 |
#endif |
80 |
|
81 |
return 0; |
82 |
} |
83 |
|
84 |
|
85 |
#ifdef SSL_TLS |
86 |
/* |
87 |
* Initialize Secure Socket Layer connection. |
88 |
*/ |
89 |
int |
90 |
init_secure_connection(conn_t * conn, unsigned int protocol) |
91 |
{ |
92 |
int e; |
93 |
SSL_CTX *ctx; |
94 |
SSL_METHOD *method; |
95 |
|
96 |
method = NULL; |
97 |
|
98 |
SSL_library_init(); |
99 |
SSL_load_error_strings(); |
100 |
|
101 |
switch (protocol) { |
102 |
case SSL_TLS_V1: |
103 |
method = TLSv1_client_method(); |
104 |
break; |
105 |
case SSL_SSL_V2: |
106 |
case SSL_SSL_V3: |
107 |
method = SSLv23_client_method(); |
108 |
break; |
109 |
} |
110 |
|
111 |
if (!(ctx = SSL_CTX_new(method))) |
112 |
goto fail; |
113 |
|
114 |
if (!(conn->ssl = SSL_new(ctx))) |
115 |
goto fail; |
116 |
|
117 |
SSL_set_fd(conn->ssl, conn->sock); |
118 |
|
119 |
if ((e = SSL_connect(conn->ssl)) <= 0) { |
120 |
SSL_get_error(conn->ssl, e); |
121 |
error("initiating SSL connection; %s\n", |
122 |
ERR_error_string(ERR_get_error(), NULL)); |
123 |
goto fail; |
124 |
} |
125 |
if (options & OPTION_DETAILS_VERBOSE) { |
126 |
SSL_CIPHER *cipher; |
127 |
char *ver; |
128 |
const char *name; |
129 |
int usebits, algbits; |
130 |
|
131 |
cipher = SSL_get_current_cipher(conn->ssl); |
132 |
ver = SSL_CIPHER_get_version(cipher); |
133 |
name = SSL_CIPHER_get_name(cipher); |
134 |
usebits = SSL_CIPHER_get_bits(cipher, &algbits); |
135 |
printf("SSL/TLS handshake completed: %s with cipher %s " |
136 |
"(%d/%d bits).\n", ver, name, usebits, algbits); |
137 |
} |
138 |
if (get_cert(conn)) |
139 |
goto fail; |
140 |
|
141 |
SSL_CTX_free(ctx); |
142 |
|
143 |
return 0; |
144 |
|
145 |
fail: |
146 |
conn->ssl = NULL; |
147 |
SSL_CTX_free(ctx); |
148 |
|
149 |
return ERROR_SSL; |
150 |
} |
151 |
#endif /* SSL_TLS */ |
152 |
|
153 |
|
154 |
/* |
155 |
* Disconnect from mail server. |
156 |
*/ |
157 |
int |
158 |
close_connection(conn_t * conn) |
159 |
{ |
160 |
#ifdef SSL_TLS |
161 |
if (conn->ssl) { |
162 |
SSL_shutdown(conn->ssl); |
163 |
SSL_free(conn->ssl); |
164 |
conn->ssl = NULL; |
165 |
} |
166 |
#endif |
167 |
if (conn->sock >= 0 && close(conn->sock) == -1) { |
168 |
error("closing socket; %s\n", strerror(errno)); |
169 |
return ERROR_NETWORK; |
170 |
} else { |
171 |
conn->sock = -1; |
172 |
return 0; |
173 |
} |
174 |
} |
175 |
|
176 |
|
177 |
/* |
178 |
* Read data from socket. |
179 |
*/ |
180 |
int |
181 |
socket_read(conn_t * conn, char *buf) |
182 |
{ |
183 |
int f, e, s; |
184 |
fd_set fds; |
185 |
struct timeval tv; |
186 |
struct timeval *tvp; |
187 |
|
188 |
e = 0; |
189 |
s = 1; |
190 |
tvp = NULL; |
191 |
|
192 |
memset(buf, 0, RESPONSE_BUF + 1); |
193 |
|
194 |
if (timeout >= 0) { |
195 |
tv.tv_sec = timeout; |
196 |
tv.tv_usec = 0; |
197 |
tvp = &tv; |
198 |
} |
199 |
f = fcntl(conn->sock, F_GETFL, 0); |
200 |
fcntl(conn->sock, F_SETFL, f | O_NONBLOCK); |
201 |
|
202 |
FD_ZERO(&fds); |
203 |
FD_SET(conn->sock, &fds); |
204 |
|
205 |
#ifdef SSL_TLS |
206 |
if (conn->ssl) { |
207 |
while (SSL_pending(conn->ssl) > 0 || |
208 |
((s = select(conn->sock + 1, &fds, NULL, NULL, tvp)) > 0 && |
209 |
FD_ISSET(conn->sock, &fds))) { |
210 |
e = SSL_read(conn->ssl, buf, RESPONSE_BUF); |
211 |
|
212 |
if (e > 0) |
213 |
break; |
214 |
|
215 |
switch (SSL_get_error(conn->ssl, e)) { |
216 |
case SSL_ERROR_WANT_READ: |
217 |
case SSL_ERROR_WANT_WRITE: |
218 |
continue; |
219 |
case SSL_ERROR_ZERO_RETURN: |
220 |
return ERROR_NETWORK; |
221 |
case SSL_ERROR_SYSCALL: |
222 |
case SSL_ERROR_SSL: |
223 |
fatal(ERROR_NETWORK, "reading data; %s\n", |
224 |
ERR_error_string(ERR_get_error(), NULL)); |
225 |
default: |
226 |
fatal(ERROR_NETWORK, |
227 |
"undefined ssl error while reading data\n"); |
228 |
} |
229 |
} |
230 |
} else |
231 |
#endif |
232 |
{ |
233 |
if ((s = select(conn->sock + 1, &fds, NULL, NULL, tvp)) > 0 && |
234 |
FD_ISSET(conn->sock, &fds)) |
235 |
e = read(conn->sock, buf, RESPONSE_BUF); |
236 |
|
237 |
if (e == -1) |
238 |
fatal(ERROR_NETWORK, "reading data; %s", |
239 |
strerror(errno)); |
240 |
else if (e == 0) |
241 |
return ERROR_NETWORK; |
242 |
} |
243 |
|
244 |
fcntl(conn->sock, F_SETFL, f); |
245 |
|
246 |
if (s == -1) |
247 |
fatal(ERROR_NETWORK, "waiting to read from socket; %s\n", |
248 |
strerror(errno)); |
249 |
else if (s == 0) |
250 |
fatal(ERROR_NETWORK, |
251 |
"timeout period expired while waiting to read " |
252 |
"data\n"); |
253 |
|
254 |
return 0; |
255 |
} |
256 |
|
257 |
|
258 |
/* |
259 |
* Write data to socket. |
260 |
*/ |
261 |
int |
262 |
socket_write(conn_t * conn, char *data) |
263 |
{ |
264 |
int f, e, s; |
265 |
fd_set fds; |
266 |
struct timeval tv; |
267 |
struct timeval *tvp = NULL; |
268 |
|
269 |
e = 0; |
270 |
s = 1; |
271 |
|
272 |
if (timeout >= 0) { |
273 |
tv.tv_sec = timeout; |
274 |
tv.tv_usec = 0; |
275 |
tvp = &tv; |
276 |
} |
277 |
f = fcntl(conn->sock, F_GETFL, 0); |
278 |
fcntl(conn->sock, F_SETFL, f | O_NONBLOCK); |
279 |
|
280 |
FD_ZERO(&fds); |
281 |
FD_SET(conn->sock, &fds); |
282 |
|
283 |
#ifdef SSL_TLS |
284 |
if (conn->ssl) { |
285 |
while ((s = select(conn->sock + 1, NULL, &fds, NULL, tvp) > 0 && |
286 |
FD_ISSET(conn->sock, &fds))) { |
287 |
e = SSL_write(conn->ssl, data, strlen(data)); |
288 |
|
289 |
if (e > 0) |
290 |
break; |
291 |
|
292 |
switch (SSL_get_error(conn->ssl, e)) { |
293 |
case SSL_ERROR_WANT_READ: |
294 |
case SSL_ERROR_WANT_WRITE: |
295 |
continue; |
296 |
case SSL_ERROR_ZERO_RETURN: |
297 |
return ERROR_NETWORK; |
298 |
case SSL_ERROR_SYSCALL: |
299 |
case SSL_ERROR_SSL: |
300 |
fatal(ERROR_NETWORK, "writing data; %s\n", |
301 |
ERR_error_string(ERR_get_error(), NULL)); |
302 |
default: |
303 |
fatal(ERROR_NETWORK, |
304 |
"undefined ssl error while writing data\n"); |
305 |
} |
306 |
} |
307 |
} else |
308 |
#endif |
309 |
{ |
310 |
if ((s = select(conn->sock + 1, NULL, &fds, NULL, tvp)) > 0 && |
311 |
FD_ISSET(conn->sock, &fds)) |
312 |
e = write(conn->sock, data, strlen(data)); |
313 |
|
314 |
if (e == -1) |
315 |
fatal(ERROR_NETWORK, "writing data; %s", |
316 |
strerror(errno)); |
317 |
} |
318 |
|
319 |
fcntl(conn->sock, F_SETFL, f); |
320 |
|
321 |
if (s == -1) |
322 |
fatal(ERROR_NETWORK, "waiting to write to socket; %s\n", |
323 |
strerror(errno)); |
324 |
else if (s == 0) |
325 |
fatal(ERROR_NETWORK, |
326 |
"timeout period expired while waiting to write " |
327 |
"data\n"); |
328 |
|
329 |
return 0; |
330 |
} |