134 |
|
|
135 |
e = SSL_connect(*ssl); |
e = SSL_connect(*ssl); |
136 |
|
|
137 |
if (e < 0) { |
if (e <= 0) { |
138 |
|
SSL_get_error(*ssl, e); |
139 |
error("imapfilter: initiating SSL connection; %s", |
error("imapfilter: initiating SSL connection; %s", |
140 |
ERR_error_string(e, NULL)); |
ERR_error_string(ERR_get_error(), NULL)); |
141 |
return ERROR_SSL; |
return ERROR_SSL; |
142 |
} |
} |
143 |
/* Get server's certificate. */ |
/* Get server's certificate. */ |
202 |
*/ |
*/ |
203 |
int socket_read(int *sock, char *buf) |
int socket_read(int *sock, char *buf) |
204 |
{ |
{ |
205 |
int f, r, s; |
int f, e, s; |
206 |
fd_set fds; |
fd_set fds; |
207 |
struct timeval tv; |
struct timeval tv; |
208 |
struct timeval *tvp = NULL; |
struct timeval *tvp = NULL; |
210 |
SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux); |
SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux); |
211 |
#endif |
#endif |
212 |
|
|
213 |
r = 0; |
e = 0; |
214 |
s = 1; |
s = 1; |
215 |
|
|
216 |
memset(buf, 0, RESPONSE_BUF); |
memset(buf, 0, RESPONSE_BUF); |
228 |
|
|
229 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
230 |
if (*ssl) { |
if (*ssl) { |
231 |
if (SSL_pending(*ssl) |
|
232 |
|| ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0 |
for (;;) { |
233 |
&& FD_ISSET(*sock, &fds))) |
if (SSL_pending(*ssl) |
234 |
r = SSL_read(*ssl, buf, RESPONSE_BUF - 1); |
|| ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0 |
235 |
|
&& FD_ISSET(*sock, &fds))) |
236 |
|
e = SSL_read(*ssl, buf, RESPONSE_BUF - 1); |
237 |
|
|
238 |
|
if (e > 0) |
239 |
|
break; |
240 |
|
|
241 |
|
switch (SSL_get_error(*ssl, e)) { |
242 |
|
case SSL_ERROR_WANT_READ: |
243 |
|
case SSL_ERROR_WANT_WRITE: |
244 |
|
continue; |
245 |
|
case SSL_ERROR_SYSCALL: |
246 |
|
case SSL_ERROR_SSL: |
247 |
|
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
248 |
|
ERR_error_string(ERR_get_error(), NULL)); |
249 |
|
default: |
250 |
|
fatal(ERROR_NETWORK, |
251 |
|
"imapfilter: undefined ssl error while reading data\n"); |
252 |
|
} |
253 |
|
} |
254 |
} else |
} else |
255 |
|
#else |
256 |
|
if ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0 |
257 |
|
&& FD_ISSET(*sock, &fds)) |
258 |
|
e = read(*sock, buf, RESPONSE_BUF - 1); |
259 |
|
|
260 |
|
if (e == -1) |
261 |
|
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
262 |
|
strerror(errno)); |
263 |
#endif |
#endif |
264 |
if ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0 |
|
|
&& FD_ISSET(*sock, &fds)) |
|
|
r = read(*sock, buf, RESPONSE_BUF - 1); |
|
265 |
|
|
266 |
fcntl(*sock, F_SETFL, f); |
fcntl(*sock, F_SETFL, f); |
267 |
|
|
268 |
if (s == -1) |
if (s == -1) |
269 |
fatal(ERROR_NETWORK, "imapfilter: waiting input from socket; %s\n", |
fatal(ERROR_NETWORK, "imapfilter: waiting to read from socket; %s\n", |
270 |
strerror(errno)); |
strerror(errno)); |
271 |
else if (!s) |
else if (!s) |
272 |
fatal(ERROR_NETWORK, |
fatal(ERROR_NETWORK, |
273 |
"imapfilter: timeout period expired while waiting data\n"); |
"imapfilter: timeout period expired while waiting to read " |
274 |
|
"data\n"); |
|
#ifdef SSL_TLS |
|
|
if (*ssl) { |
|
|
if (r < 0) |
|
|
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
|
|
ERR_error_string(r, NULL)); |
|
|
} else |
|
|
#endif |
|
|
if (r == -1) |
|
|
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
|
|
strerror(errno)); |
|
275 |
|
|
276 |
return 0; |
return 0; |
277 |
} |
} |
282 |
*/ |
*/ |
283 |
int socket_write(int *sock, char *data) |
int socket_write(int *sock, char *data) |
284 |
{ |
{ |
285 |
|
int f, e, s; |
286 |
|
fd_set fds; |
287 |
|
struct timeval tv; |
288 |
|
struct timeval *tvp = NULL; |
289 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
|
int e; |
|
290 |
SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux); |
SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux); |
291 |
|
#endif |
292 |
|
|
293 |
|
e = 0; |
294 |
|
s = 1; |
295 |
|
|
296 |
|
if (timeout >= 0) { |
297 |
|
tv.tv_sec = timeout; |
298 |
|
tv.tv_usec = 0; |
299 |
|
tvp = &tv; |
300 |
|
} |
301 |
|
f = fcntl(*sock, F_GETFL, 0); |
302 |
|
fcntl(*sock, F_SETFL, f | O_NONBLOCK); |
303 |
|
|
304 |
|
FD_ZERO(&fds); |
305 |
|
FD_SET(*sock, &fds); |
306 |
|
|
307 |
|
#ifdef SSL_TLS |
308 |
if (*ssl) { |
if (*ssl) { |
309 |
e = SSL_write(*ssl, data, strlen(data)); |
for (;;) { |
310 |
if (e <= 0) |
if ((s = select(*sock + 1, NULL, &fds, NULL, tvp) > 0 |
311 |
fatal(ERROR_NETWORK, |
&& FD_ISSET(*sock, &fds))) |
312 |
"imapfilter: sending data; %s", |
e = SSL_write(*ssl, data, strlen(data)); |
313 |
ERR_error_string(e, NULL)); |
|
314 |
|
if (e > 0) |
315 |
|
break; |
316 |
|
|
317 |
|
switch (SSL_get_error(*ssl, e)) { |
318 |
|
case SSL_ERROR_WANT_READ: |
319 |
|
case SSL_ERROR_WANT_WRITE: |
320 |
|
continue; |
321 |
|
case SSL_ERROR_SYSCALL: |
322 |
|
case SSL_ERROR_SSL: |
323 |
|
fatal(ERROR_NETWORK, "imapfilter: writing data; %s", |
324 |
|
ERR_error_string(ERR_get_error(), NULL)); |
325 |
|
default: |
326 |
|
fatal(ERROR_NETWORK, |
327 |
|
"imapfilter: undefined ssl error while writing data\n"); |
328 |
|
} |
329 |
|
} |
330 |
} else |
} else |
331 |
|
#else |
332 |
|
if ((s = select(*sock + 1, NULL, &fds, NULL, tvp)) > 0 |
333 |
|
&& FD_ISSET(*sock, &fds)) |
334 |
|
e = write(*sock, data, strlen(data)); |
335 |
|
|
336 |
|
if (e == -1) |
337 |
|
fatal(ERROR_NETWORK, "imapfilter: writing data; %s", |
338 |
|
strerror(errno)); |
339 |
#endif |
#endif |
340 |
if (write(*sock, data, strlen(data)) == -1) |
|
341 |
fatal(ERROR_NETWORK, "imapfilter: sending data; %s", |
fcntl(*sock, F_SETFL, f); |
342 |
|
|
343 |
|
if (s == -1) |
344 |
|
fatal(ERROR_NETWORK, "imapfilter: waiting to write to socket; %s\n", |
345 |
strerror(errno)); |
strerror(errno)); |
346 |
|
else if (!s) |
347 |
|
fatal(ERROR_NETWORK, |
348 |
|
"imapfilter: timeout period expired while waiting to write " |
349 |
|
"data\n"); |
350 |
|
|
351 |
return 0; |
return 0; |
352 |
} |
} |