2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * Copyright © 2007 Pierre Habouzit
23 #include <sys/epoll.h>
24 #include <sys/socket.h>
26 # include <linux/poll.h>
28 # define EPOLLRDHUP POLLRDHUP
35 #include "mutt_ssl.li"
40 DO_ARRAY_TYPE(job_t, job);
42 static int epollfd = -1;
43 static job_array jobs;
44 static pthread_mutex_t el_mx;
45 static pthread_cond_t el_cond;
46 static pthread_t el_thread;
48 static int el_job_setemode(job_t *w, el_mode emode)
50 static int const evtmode_to_epoll[] = {
51 [EL_NEW] = EPOLLRDHUP,
52 [EL_READING] = EPOLLIN,
53 [EL_WRITING] = EPOLLOUT,
54 [EL_RDWR] = EPOLLIN | EPOLLOUT,
55 [EL_IDLE] = EPOLLRDHUP,
58 assert (w->mode == emode || emode == EL_WRITING || emode == EL_READING);
60 if (emode != w->emode) {
61 struct epoll_event event = {
63 .events = evtmode_to_epoll[emode],
65 int action = w->emode == EL_NEW ? EPOLL_CTL_ADD : EPOLL_CTL_MOD;
66 if (epoll_ctl(epollfd, action, w->fd, &event) < 0) {
67 return el_job_release(w, true);
74 int el_job_setmode(job_t *w, el_mode mode)
76 if (w->mode == w->emode) {
78 return el_job_setemode(w, mode);
85 void job_wipe(job_t *w)
88 gnutls_certificate_free_credentials(w->xcred);
90 gnutls_deinit(w->session);
93 static void job_arrau_dtor(job_t **j)
96 IGNORE(el_job_release(*j, EL_KILLED));
99 DO_ARRAY_FUNCS(job_t, job, job_arrau_dtor);
101 static void job_array_remove(job_array *arr, job_t *j)
103 for (int i = 0; i < arr->len; i++) {
104 if (arr->arr[i] == j) {
105 job_array_take(arr, i);
111 job_t *el_job_start(const machine_t *m, void *cfg)
113 job_t *w = job_new();
115 job_array_append(&jobs, w);
116 return m->setup(w, cfg) < 0 ? NULL : w;
119 int el_job_release(job_t *w, el_status reason)
122 pthread_cond_signal(&el_cond);
125 w->state = EL_LLP_FINI;
126 if (w->m && w->m->finalize) {
127 w->m->finalize(w, reason);
131 gnutls_bye(w->session, GNUTLS_SHUT_RDWR);
134 job_array_remove(&jobs, w);
139 static int el_job_tlsing(job_t *w, int starttls)
141 int err = gnutls_handshake(w->session);
142 if (err < 0 && !gnutls_error_is_fatal(err)) {
143 int wr = gnutls_record_get_direction(w->session);
144 return el_job_setemode(w, wr ? EL_WRITING : EL_READING);
147 return el_job_release(w, EL_RDHUP);
150 if (!tls_check_certificate (conn))
154 /* set Security Strength Factor (SSF) for SASL */
155 /* NB: gnutls_cipher_get_key_size() returns key length in bytes */
156 w->ssf = gnutls_cipher_get_key_size(gnutls_cipher_get(w->session)) * 8;
157 w->state = EL_LLP_READY;
159 return el_job_setemode(w, w->mode);
160 return w->m->on_event(w, EL_EVT_RUNNING);
163 static int el_job_starttlsing(job_t *w)
165 return el_job_tlsing(w, true);
168 static int el_job_connecting_ssl(job_t *w)
170 return el_job_tlsing(w, false);
173 static int el_job_connecting(job_t *w)
176 socklen_t len = sizeof(err);
178 if (getsockopt(w->fd, SOL_SOCKET, SO_ERROR, (void *)&err, &len) || err)
179 return el_job_release(w, EL_ERROR);
182 w->llp = &el_job_connecting_ssl;
185 w->state = EL_LLP_READY;
186 return w->m->on_event(w, EL_EVT_RUNNING);
189 static int tls_negotiate(job_t *w)
191 static int protocol_priority[] = { GNUTLS_TLS1, GNUTLS_SSL3, 0 };
193 if (gnutls_certificate_allocate_credentials(&w->xcred) < 0)
196 /* ignore errors, maybe file doesn't exist yet */
197 gnutls_certificate_set_x509_trust_file(w->xcred, mod_ssl.cert_file,
198 GNUTLS_X509_FMT_PEM);
200 if (mod_ssl.ca_certificates_file) {
201 gnutls_certificate_set_x509_trust_file(w->xcred,
202 mod_ssl.ca_certificates_file, GNUTLS_X509_FMT_PEM);
204 gnutls_init(&w->session, GNUTLS_CLIENT);
207 gnutls_transport_set_ptr(w->session, (gnutls_transport_ptr)(intptr_t)w->fd);
209 /* disable TLS/SSL protocols as needed */
210 if (!mod_ssl.use_sslv3) {
211 protocol_priority[1] = 0;
214 /* We use default priorities (see gnutls documentation),
215 except for protocol version */
216 gnutls_set_default_priority(w->session);
217 gnutls_protocol_set_priority(w->session, protocol_priority);
218 gnutls_credentials_set(w->session, GNUTLS_CRD_CERTIFICATE, w->xcred);
222 int el_job_connect(job_t *w, struct sockaddr *addr, socklen_t len,
223 int type, int proto, int ssl)
225 int res, sock = socket(addr->sa_family, type, proto);
230 res = fcntl(sock, F_GETFL);
233 if (fcntl(sock, F_SETFL, res | O_NONBLOCK) < 0)
235 if (fcntl(sock, F_SETFD, FD_CLOEXEC) < 0)
237 if (connect(sock, addr, len) < 0)
241 if (ssl && tls_negotiate(w) < 0)
244 w->llp = &el_job_connecting;
245 return el_job_setmode(w, EL_WRITING);
249 return el_job_release(w, EL_ERROR);
252 int el_job_connect2(job_t *w, const ACCOUNT *act)
256 struct addrinfo *res;
257 struct addrinfo hints = {
258 .ai_family = AF_UNSPEC,
259 .ai_socktype = SOCK_STREAM,
263 if (idna_to_ascii_lz(act->host, &host, 1) != IDNA_SUCCESS) {
264 mutt_error(_("Bad IDN \"%s\"."), act->host);
270 mutt_message(_("Looking up %s..."), act->host);
271 rc = getaddrinfo(host, NULL, &hints, &res);
277 mutt_error(_("Could not find the host \"%s\""), act->host);
281 mutt_message(_("Connecting to %s..."), act->host);
282 rc = el_job_connect(w, res->ai_addr, res->ai_addrlen, res->ai_socktype,
283 res->ai_protocol, act->has_ssl);
286 mutt_error(_("Could not connect to %s (%m)."), act->host);
293 int el_job_starttls(job_t *w)
295 if (tls_negotiate(w) < 0)
296 return el_job_release(w, EL_RDHUP);
297 w->state = EL_LLP_INIT;
298 w->llp = &el_job_starttlsing;
302 ssize_t el_job_read(job_t *w, buffer_t *buf)
306 buffer_ensure(buf, BUFSIZ);
309 nr = gnutls_record_recv(w->session, buf->data + buf->len, BUFSIZ);
310 if (nr < 0 && !gnutls_error_is_fatal(nr)) {
311 int wr = gnutls_record_get_direction(w->session);
312 return el_job_setemode(w, wr ? EL_WRITING : EL_READING);
314 EL_JOB_CHECK(el_job_setemode(w, w->mode));
316 nr = read(w->fd, buf->data + buf->len, BUFSIZ);
317 if (nr < 0 && (errno == EINTR || errno == EAGAIN))
321 return el_job_release(w, EL_RDHUP);
322 buffer_extend(buf, nr);
326 ssize_t el_job_write(job_t *w, buffer_t *buf)
334 nr = gnutls_record_send(w->session, buf->data, buf->len);
335 if (nr < 0 && !gnutls_error_is_fatal(nr)) {
336 int wr = gnutls_record_get_direction(w->session);
337 return el_job_setemode(w, wr ? EL_WRITING : EL_READING);
339 EL_JOB_CHECK(el_job_setemode(w, w->mode));
341 nr = write(w->fd, buf->data, buf->len);
342 if (nr < 0 && (errno == EINTR || errno == EAGAIN))
346 return el_job_release(w, EL_RDHUP);
347 buffer_splice(buf, 0, nr, NULL, 0);
353 pthread_mutex_lock(&el_mx);
358 pthread_mutex_unlock(&el_mx);
361 int el_dispatch(int timeout)
363 struct epoll_event events[FD_SETSIZE];
364 int count = epoll_wait(epollfd, events, countof(events), timeout);
367 if (errno == EAGAIN || errno == EINTR)
369 mutt_error("epoll_wait");
370 mutt_exit(EXIT_FAILURE);
374 while (--count >= 0) {
375 job_t *w = events[count].data.ptr;
376 int event = events[count].events;
380 pthread_cond_signal(&el_cond);
383 gettimeofday(&w->mru, NULL);
390 if (event & EPOLLRDHUP) {
391 IGNORE(el_job_release(w, EL_RDHUP));
392 } else if (w->mode != w->emode) {
393 IGNORE(w->m->on_event(w, EL_EVT_INOUT ^ w->emode));
397 if (event & EPOLLOUT)
399 IGNORE(w->m->on_event(w, evt));
404 IGNORE(el_job_release(w, EL_ERROR));
413 void el_wait(volatile job_t *w)
416 pthread_cond_wait(&el_cond, &el_mx);
419 static void *el_loop(void *data)
421 time_t sec = time(NULL);
427 pthread_testcancel();
429 gettimeofday(&now, NULL);
430 if (sec >= now.tv_sec)
436 for (int i = jobs.len - 1; i >= 0; --i) {
437 job_t *w = jobs.arr[i];
438 if (timercmp(&now, &w->mru, >)) {
440 pthread_cond_signal(&el_cond);
443 IGNORE(w->m->on_event(w, EL_EVT_WAKEUP));
450 void el_initialize(void)
452 pthread_mutexattr_t attr;
454 pthread_mutexattr_init(&attr);
455 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
456 pthread_mutex_init(&el_mx, &attr);
457 pthread_mutexattr_destroy(&attr);
459 gnutls_global_init();
460 epollfd = epoll_create(1024);
462 mutt_error("epoll_create");
463 mutt_exit(EXIT_FAILURE);
465 job_array_init(&jobs);
466 pthread_create(&el_thread, NULL, &el_loop, NULL);
469 void el_shutdown(void)
471 pthread_cancel(el_thread);
472 pthread_join(el_thread, NULL);
473 job_array_wipe(&jobs);
475 gnutls_global_deinit();
476 pthread_mutex_destroy(&el_mx);