1 |
lefcha |
1.1 |
#include <string.h> |
2 |
|
|
#include <errno.h> |
3 |
|
|
#include <unistd.h> |
4 |
|
|
#include <sys/types.h> |
5 |
|
|
#include <sys/socket.h> |
6 |
|
|
#include <netinet/in.h> |
7 |
|
|
#include <netdb.h> |
8 |
|
|
#include <fcntl.h> |
9 |
|
|
|
10 |
|
|
#include "config.h" |
11 |
|
|
#include "imapfilter.h" |
12 |
|
|
|
13 |
|
|
#ifdef SSL_TLS |
14 |
|
|
#include <openssl/ssl.h> |
15 |
|
|
#include <openssl/err.h> |
16 |
lefcha |
1.3 |
#include <openssl/x509.h> |
17 |
lefcha |
1.1 |
|
18 |
|
|
|
19 |
|
|
static SSL *ssl; |
20 |
|
|
#endif |
21 |
|
|
|
22 |
|
|
static int sock; |
23 |
|
|
|
24 |
|
|
|
25 |
|
|
/* |
26 |
|
|
* Connect to mail server. |
27 |
|
|
*/ |
28 |
|
|
#ifndef SSL_TLS |
29 |
|
|
int init_connection(char *serv, unsigned int port) |
30 |
|
|
#else |
31 |
|
|
int init_connection(char *serv, unsigned int port, unsigned int protocol) |
32 |
|
|
#endif |
33 |
|
|
{ |
34 |
|
|
struct sockaddr_in sa; |
35 |
|
|
struct hostent *he; |
36 |
|
|
|
37 |
|
|
memset((char *) &sa, 0, sizeof(struct sockaddr_in)); |
38 |
|
|
|
39 |
|
|
sock = socket(PF_INET, SOCK_STREAM, 0); |
40 |
|
|
|
41 |
|
|
if (sock < 0) { |
42 |
|
|
error("imapfilter: create socket; %s\n", strerror(errno)); |
43 |
|
|
return ERROR_NETWORK; |
44 |
|
|
} |
45 |
|
|
if (!(he = gethostbyname(serv))) { |
46 |
|
|
error("imapfilter: get network host entry; %s\n", strerror(errno)); |
47 |
|
|
close_connection(); |
48 |
|
|
return ERROR_NETWORK; |
49 |
|
|
} |
50 |
|
|
sa.sin_family = AF_INET; |
51 |
|
|
sa.sin_port = htons(port); |
52 |
|
|
sa.sin_addr = *(struct in_addr *) he->h_addr; |
53 |
|
|
|
54 |
|
|
if (connect(sock, (struct sockaddr *) & sa, sizeof(struct sockaddr))) { |
55 |
|
|
error("imapfilter: initiating connection; %s\n", strerror(errno)); |
56 |
|
|
close_connection(); |
57 |
|
|
return ERROR_NETWORK; |
58 |
|
|
} |
59 |
lefcha |
1.5 |
xstrncpy(serv, he->h_name, SERVER_LEN - 1); |
60 |
lefcha |
1.1 |
|
61 |
|
|
log_info(LOG_SERVER, serv); |
62 |
lefcha |
1.2 |
|
63 |
lefcha |
1.1 |
#ifdef SSL_TLS |
64 |
|
|
if (protocol != SSL_DISABLED) { |
65 |
|
|
ssl_init(protocol); |
66 |
|
|
info("Connected to %s using %s.\n", serv, SSL_get_cipher(ssl)); |
67 |
|
|
} else { |
68 |
|
|
ssl = NULL; |
69 |
|
|
#endif |
70 |
|
|
info("Connected to %s.\n", serv); |
71 |
|
|
#ifdef SSL_TLS |
72 |
|
|
} |
73 |
|
|
#endif |
74 |
lefcha |
1.2 |
|
75 |
lefcha |
1.1 |
|
76 |
|
|
return 0; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
|
80 |
|
|
#ifdef SSL_TLS |
81 |
|
|
/* |
82 |
|
|
* Initialize Secure Socket Layer connection. |
83 |
|
|
*/ |
84 |
|
|
int ssl_init(unsigned int protocol) |
85 |
|
|
{ |
86 |
|
|
int e; |
87 |
lefcha |
1.7 |
unsigned int i, n = 0; |
88 |
lefcha |
1.1 |
SSL_CTX *ctx; |
89 |
|
|
SSL_METHOD *method = NULL; |
90 |
lefcha |
1.3 |
X509 *cert; |
91 |
|
|
char *c; |
92 |
|
|
EVP_MD *evp; |
93 |
|
|
unsigned char digest[EVP_MAX_MD_SIZE]; |
94 |
lefcha |
1.2 |
|
95 |
lefcha |
1.1 |
SSL_library_init(); |
96 |
lefcha |
1.2 |
|
97 |
lefcha |
1.1 |
switch (protocol) { |
98 |
|
|
case SSL_SSL_V2: |
99 |
|
|
method = SSLv2_client_method(); |
100 |
|
|
break; |
101 |
|
|
case SSL_SSL_V3: |
102 |
|
|
method = SSLv3_client_method(); |
103 |
|
|
break; |
104 |
|
|
case SSL_TLS_V1: |
105 |
|
|
method = TLSv1_client_method(); |
106 |
|
|
break; |
107 |
|
|
} |
108 |
lefcha |
1.2 |
|
109 |
lefcha |
1.1 |
if (!(ctx = SSL_CTX_new(method))) |
110 |
|
|
return ERROR_SSL; |
111 |
lefcha |
1.2 |
|
112 |
lefcha |
1.1 |
if (!(ssl = SSL_new(ctx))) |
113 |
|
|
return ERROR_SSL; |
114 |
lefcha |
1.2 |
|
115 |
lefcha |
1.1 |
SSL_set_fd(ssl, sock); |
116 |
|
|
|
117 |
|
|
e = SSL_connect(ssl); |
118 |
lefcha |
1.2 |
|
119 |
lefcha |
1.1 |
if (e <= 0) { |
120 |
|
|
error("imapfilter: initiating SSL connection; %s", |
121 |
|
|
ERR_error_string(e, NULL)); |
122 |
|
|
return ERROR_SSL; |
123 |
|
|
} |
124 |
lefcha |
1.3 |
/* Get server's certificate. */ |
125 |
|
|
if (!(cert = SSL_get_peer_certificate(ssl))) |
126 |
|
|
return ERROR_SSL; |
127 |
|
|
|
128 |
|
|
if (!(c = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0))) |
129 |
|
|
return ERROR_SSL; |
130 |
|
|
verbose("Server certificate subject: %s\n", c); |
131 |
|
|
free(c); |
132 |
|
|
|
133 |
|
|
if (!(c = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0))) |
134 |
|
|
return ERROR_SSL; |
135 |
|
|
verbose("Server certificate issuer: %s\n", c); |
136 |
|
|
free(c); |
137 |
|
|
|
138 |
|
|
if (!(evp = EVP_md5())) |
139 |
|
|
return ERROR_SSL; |
140 |
lefcha |
1.7 |
if (!(X509_digest(cert, evp, digest, &n))) |
141 |
lefcha |
1.3 |
return ERROR_SSL; |
142 |
|
|
|
143 |
|
|
verbose("Server key fingerprint: "); |
144 |
lefcha |
1.7 |
for (i = 0; i < n; i++) |
145 |
|
|
verbose(i != n - 1 ? "%02X:" : "%02X\n", digest[i]); |
146 |
lefcha |
1.3 |
|
147 |
|
|
X509_free(cert); |
148 |
lefcha |
1.1 |
SSL_CTX_free(ctx); |
149 |
|
|
|
150 |
|
|
return 0; |
151 |
|
|
} |
152 |
|
|
#endif |
153 |
|
|
|
154 |
|
|
|
155 |
|
|
/* |
156 |
|
|
* Disconnect from mail server. |
157 |
|
|
*/ |
158 |
|
|
int close_connection(void) |
159 |
|
|
{ |
160 |
|
|
#ifdef SSL_TLS |
161 |
|
|
if (ssl) { |
162 |
|
|
SSL_shutdown(ssl); |
163 |
|
|
SSL_free(ssl); |
164 |
|
|
} |
165 |
|
|
#endif |
166 |
|
|
|
167 |
|
|
if (close(sock)) { |
168 |
|
|
error("imapfilter: closing socket; %s\n", strerror(errno)); |
169 |
|
|
return ERROR_NETWORK; |
170 |
|
|
} else |
171 |
|
|
return 0; |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
|
175 |
|
|
/* |
176 |
|
|
* Read data from socket. |
177 |
|
|
*/ |
178 |
|
|
int socket_read(char *buf) |
179 |
|
|
{ |
180 |
lefcha |
1.6 |
int flags, r, s; |
181 |
lefcha |
1.1 |
fd_set fds; |
182 |
|
|
struct timeval tv; |
183 |
lefcha |
1.7 |
|
184 |
|
|
r = s = 0; |
185 |
lefcha |
1.1 |
|
186 |
lefcha |
1.5 |
memset(buf, 0, RESPONSE_BUF); |
187 |
lefcha |
1.1 |
|
188 |
lefcha |
1.6 |
tv.tv_sec = 30; |
189 |
lefcha |
1.1 |
tv.tv_usec = 0; |
190 |
|
|
|
191 |
|
|
flags = fcntl(sock, F_GETFL, 0); |
192 |
|
|
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
193 |
|
|
|
194 |
|
|
FD_ZERO(&fds); |
195 |
|
|
FD_SET(sock, &fds); |
196 |
|
|
|
197 |
|
|
#ifdef SSL_TLS |
198 |
lefcha |
1.6 |
if (ssl) { |
199 |
|
|
if (SSL_pending(ssl) || ((s = select(sock + 1, &fds, NULL, NULL, &tv)) > 0 |
200 |
|
|
&& FD_ISSET(sock, &fds))) |
201 |
|
|
r = SSL_read(ssl, buf, RESPONSE_BUF - 1); |
202 |
|
|
} else |
203 |
lefcha |
1.1 |
#endif |
204 |
lefcha |
1.6 |
if ((s = select(sock + 1, &fds, NULL, NULL, &tv)) > 0 |
205 |
|
|
&& FD_ISSET(sock, &fds)) |
206 |
|
|
r = read(sock, buf, RESPONSE_BUF - 1); |
207 |
|
|
|
208 |
|
|
fcntl(sock, F_SETFL, flags); |
209 |
lefcha |
1.2 |
|
210 |
lefcha |
1.6 |
if (s <= 0) |
211 |
|
|
fatal(ERROR_NETWORK, "imapfilter: waiting input from socket; %s\n", |
212 |
|
|
strerror(errno)); |
213 |
lefcha |
1.1 |
|
214 |
|
|
#ifdef SSL_TLS |
215 |
lefcha |
1.6 |
if (ssl) { |
216 |
|
|
if (r <= 0) |
217 |
|
|
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
218 |
|
|
ERR_error_string(r, NULL)); |
219 |
|
|
} else |
220 |
lefcha |
1.1 |
#endif |
221 |
lefcha |
1.6 |
if (r == -1) |
222 |
|
|
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
223 |
|
|
strerror(errno)); |
224 |
lefcha |
1.1 |
|
225 |
lefcha |
1.6 |
return 0; |
226 |
lefcha |
1.1 |
} |
227 |
|
|
|
228 |
|
|
|
229 |
|
|
/* |
230 |
|
|
* Write data to socket. |
231 |
|
|
*/ |
232 |
|
|
int socket_write(char *data) |
233 |
|
|
{ |
234 |
|
|
#ifdef SSL_TLS |
235 |
|
|
int e; |
236 |
|
|
|
237 |
|
|
if (ssl) { |
238 |
|
|
e = SSL_write(ssl, data, strlen(data)); |
239 |
|
|
if (e <= 0) |
240 |
|
|
fatal(ERROR_NETWORK, |
241 |
|
|
"imapfilter: sending data; %s", |
242 |
|
|
ERR_error_string(e, NULL)); |
243 |
|
|
} else |
244 |
|
|
#endif |
245 |
|
|
if (write(sock, data, strlen(data)) == -1) |
246 |
|
|
fatal(ERROR_NETWORK, "imapfilter: sending data; %s", |
247 |
|
|
strerror(errno)); |
248 |
lefcha |
1.2 |
|
249 |
lefcha |
1.1 |
return 0; |
250 |
|
|
} |