Fix printf formats for size_t and goffset arguments.
[claws.git] / src / etpan / imap-thread.c
index 5f79cdb00d789d9767b372f460d5f68ba321f2b1..be5678cb8a2c185f5e3c0c47b38da1ca4e2213fb 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2005-2012 DINH Viet Hoa and the Claws Mail team
+ * Copyright (C) 2005-2016 DINH Viet Hoa and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,7 +14,6 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -44,6 +43,8 @@
 #include "etpan-ssl.h"
 #include "utils.h"
 #include "mainwindow.h"
+#include "proxy.h"
+#include "file-utils.h"
 #include "ssl.h"
 #include "ssl_certificate.h"
 #include "socket.h"
@@ -59,6 +60,79 @@ static chash * session_hash = NULL;
 static guint thread_manager_signal = 0;
 static GIOChannel * io_channel = NULL;
 
+static int do_mailimap_socket_connect(mailimap * imap, const char * server,
+                              gushort port, ProxyInfo * proxy_info)
+{
+       SockInfo * sock;
+       mailstream * stream;
+
+       if (!proxy_info)
+               return mailimap_socket_connect(imap, server, port);
+
+       if (port == 0)
+               port = 143;
+
+       sock = sock_connect(proxy_info->proxy_host, proxy_info->proxy_port);
+
+       if (sock == NULL)
+               return MAILIMAP_ERROR_CONNECTION_REFUSED;
+
+       if (proxy_connect(sock, server, port, proxy_info) < 0) {
+               sock_close(sock);
+               return MAILIMAP_ERROR_CONNECTION_REFUSED;
+       }
+
+       stream = mailstream_socket_open_timeout(sock->sock,
+                       imap->imap_timeout);
+       if (stream == NULL) {
+               sock_close(sock);
+               return MAILIMAP_ERROR_MEMORY;
+       }
+
+       return mailimap_connect(imap, stream);
+}
+
+static int do_mailimap_ssl_connect_with_callback(mailimap * imap, const char * server,
+       gushort port,
+       void (* callback)(struct mailstream_ssl_context * ssl_context, void * data),
+       void * data,
+       ProxyInfo *proxy_info)
+{
+       SockInfo * sock;
+       mailstream * stream;
+
+       if (!proxy_info)
+               return mailimap_ssl_connect_with_callback(imap, server,
+                               port, callback, data);
+
+       if (port == 0)
+               port = 993;
+
+       sock = sock_connect(proxy_info->proxy_host, proxy_info->proxy_port);
+
+       if (sock == NULL) {
+               debug_print("Can not connect to proxy %s:%d\n",
+                               proxy_info->proxy_host, proxy_info->proxy_port);
+               return MAILIMAP_ERROR_CONNECTION_REFUSED;
+       }
+
+       if (proxy_connect(sock, server, port, proxy_info) < 0) {
+               debug_print("Can not make proxy connection via %s:%d\n",
+                               proxy_info->proxy_host, proxy_info->proxy_port);
+               sock_close(sock);
+               return MAILIMAP_ERROR_CONNECTION_REFUSED;
+       }
+
+       stream = mailstream_ssl_open_with_callback_timeout(sock->sock,
+                       imap->imap_timeout, callback, data);
+       if (stream == NULL) {
+               sock_close(sock);
+               return MAILIMAP_ERROR_SSL;
+       }
+
+       return mailimap_connect(imap, stream);
+}
+
 static gboolean thread_manager_event(GIOChannel * source,
     GIOCondition condition,
     gpointer data)
@@ -87,7 +161,7 @@ static void imap_logger_cmd(int direction, const char * str, size_t size)
        int i = 0;
 
        if (size > 8192) {
-               log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [CMD data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
                return;
        }
        buf = malloc(size+1);
@@ -108,7 +182,7 @@ static void imap_logger_cmd(int direction, const char * str, size_t size)
        lines = g_strsplit(buf, "\n", -1);
 
        while (lines[i] && *lines[i]) {
-               log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
+               log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
                i++;
        }
        g_strfreev(lines);
@@ -122,7 +196,7 @@ static void imap_logger_fetch(int direction, const char * str, size_t size)
        int i = 0;
 
        if (size > 128 && !direction) {
-               log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [FETCH data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
                return;
        }
        
@@ -144,11 +218,11 @@ static void imap_logger_fetch(int direction, const char * str, size_t size)
 
        if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
                while (lines[i] && *lines[i]) {
-                       log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
                        i++;
                }
        } else {
-               log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
        }
        g_strfreev(lines);
        free(buf);
@@ -161,7 +235,7 @@ static void imap_logger_uid(int direction, const char * str, size_t size)
        int i = 0;
 
        if (size > 8192) {
-               log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [UID data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
                return;
        }
        buf = malloc(size+1);
@@ -183,11 +257,11 @@ static void imap_logger_uid(int direction, const char * str, size_t size)
        while (lines[i] && *lines[i]) {
                int llen = strlen(lines[i]);
                if (llen < 64)
-                       log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
                else {
                        gchar tmp[64];
                        strncpy2(tmp, lines[i], 63);
-                       log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
+                       log_print(LOG_PROTOCOL, "IMAP%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
                                  llen-64);
                }
                i++;
@@ -203,10 +277,10 @@ static void imap_logger_append(int direction, const char * str, size_t size)
        int i = 0;
 
        if (size > 8192) {
-               log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [APPEND data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
                return;
        } else if (direction == 0 && size > 64) {
-               log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [APPEND data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
                return;
        } 
        buf = malloc(size+1);
@@ -227,11 +301,11 @@ static void imap_logger_append(int direction, const char * str, size_t size)
 
        if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
                while (lines[i] && *lines[i]) {
-                       log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
                        i++;
                }
        } else {
-               log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
        }
        g_strfreev(lines);
        free(buf);
@@ -333,7 +407,7 @@ void imap_done(Folder * folder)
        
        chash_delete(imap_hash, &key, NULL);
        
-       debug_print("remove thread");
+       debug_print("remove thread\n");
 }
 
 static struct etpan_thread * get_thread(Folder * folder)
@@ -389,7 +463,7 @@ static void generic_cb(int cancelled, void * result, void * callback_data)
        debug_print("generic_cb\n");
        if (op->imap && op->imap->imap_response_info &&
            op->imap->imap_response_info->rsp_alert) {
-               log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n", 
+               log_error(LOG_PROTOCOL, "IMAP< Alert: %s\n",
                        op->imap->imap_response_info->rsp_alert);
                g_timeout_add(10, cb_show_error, NULL);
        } 
@@ -447,6 +521,7 @@ struct connect_param {
        PrefsAccount *account;
        const char * server;
        int port;
+       ProxyInfo * proxy_info;
 };
 
 struct connect_result {
@@ -520,14 +595,14 @@ static void connect_run(struct etpan_thread_op * op)
        
        CHECK_IMAP();
 
-       r = mailimap_socket_connect(param->imap,
-                                   param->server, param->port);
+       r = do_mailimap_socket_connect(param->imap,
+                                   param->server, param->port, param->proxy_info);
        
        result->error = r;
 }
 
 
-int imap_threaded_connect(Folder * folder, const char * server, int port)
+int imap_threaded_connect(Folder * folder, const char * server, int port, ProxyInfo *proxy_info)
 {
        struct connect_param param;
        struct connect_result result;
@@ -553,6 +628,7 @@ int imap_threaded_connect(Folder * folder, const char * server, int port)
        param.imap = imap;
        param.server = server;
        param.port = port;
+       param.proxy_info = proxy_info;
 
        refresh_resolvers();
        threaded_run(folder, &param, &result, connect_run);
@@ -573,13 +649,14 @@ static void connect_ssl_run(struct etpan_thread_op * op)
        
        CHECK_IMAP();
 
-       r = mailimap_ssl_connect_with_callback(param->imap,
+       r = do_mailimap_ssl_connect_with_callback(param->imap,
                                                param->server, param->port,
-                                               etpan_connect_ssl_context_cb, param->account);
+                                               etpan_connect_ssl_context_cb, param->account,
+                                               param->proxy_info);
        result->error = r;
 }
 
-int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
+int imap_threaded_connect_ssl(Folder * folder, const char * server, int port, ProxyInfo *proxy_info)
 {
        struct connect_param param;
        struct connect_result result;
@@ -607,6 +684,7 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        param.server = server;
        param.port = port;
        param.account = folder->account;
+       param.proxy_info = proxy_info;
 
        if (folder->account)
                accept_if_valid = folder->account->ssl_certs_auto_accept;
@@ -894,7 +972,7 @@ static void login_run(struct etpan_thread_op * op)
        old_debug = mailstream_debug;
        mailstream_debug = 0;
 #endif
-       if (!strcmp(param->type, "LOGIN"))
+       if (!strcmp(param->type, "plaintext"))
                r = mailimap_login(param->imap,
                           param->login, param->password);
        else if (!strcmp(param->type, "GSSAPI"))
@@ -902,7 +980,16 @@ static void login_run(struct etpan_thread_op * op)
                        param->type, param->server, NULL, NULL,
                        param->login, param->login,
                        param->password, NULL);
-       else 
+       else if (!strcmp(param->type, "SCRAM-SHA-1"))
+               /* 7th argument has to be NULL here, to stop libetpan sending the
+                * a= attribute in its initial SCRAM-SHA-1 message to server. At least
+                * Dovecot 2.2 doesn't seem to like that, and will not authenticate
+                * successfully. */
+               r = mailimap_authenticate(param->imap,
+                       param->type, NULL, NULL, NULL,
+                       NULL, param->login,
+                       param->password, NULL);
+       else
                r = mailimap_authenticate(param->imap,
                        param->type, NULL, NULL, NULL,
                        param->login, param->login,
@@ -926,6 +1013,9 @@ int imap_threaded_login(Folder * folder,
        
        debug_print("imap login - begin\n");
        
+       if (!folder)
+               return MAILIMAP_ERROR_INVAL;
+
        param.imap = get_imap(folder);
        param.login = login;
        param.password = password;
@@ -1110,7 +1200,7 @@ static void starttls_run(struct etpan_thread_op * op)
        r = mailimap_starttls(param->imap);
        
        result->error = r;
-       debug_print("imap starttls run - end %i\n", r);
+       debug_print("imap STARTTLS run - end %i\n", r);
        
        if (r == 0) {
                mailimap *imap = param->imap;
@@ -1121,14 +1211,14 @@ static void starttls_run(struct etpan_thread_op * op)
                plain_low = mailstream_get_low(imap->imap_stream);
                fd = mailstream_low_get_fd(plain_low);
                if (fd == -1) {
-                       debug_print("imap starttls run - can't get fd\n");
+                       debug_print("imap STARTTLS run - can't get fd\n");
                        result->error = MAILIMAP_ERROR_STREAM;
                        return;
                }
 
                tls_low = mailstream_low_tls_open_with_callback(fd, etpan_connect_ssl_context_cb, param->account);
                if (tls_low == NULL) {
-                       debug_print("imap starttls run - can't tls_open\n");
+                       debug_print("imap STARTTLS run - can't tls_open\n");
                        result->error = MAILIMAP_ERROR_STREAM;
                        return;
                }
@@ -1143,7 +1233,7 @@ int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
        struct starttls_result result;
        gboolean accept_if_valid = FALSE;
 
-       debug_print("imap starttls - begin\n");
+       debug_print("imap STARTTLS - begin\n");
 
        param.imap = get_imap(folder);
        param.server = host;
@@ -1156,7 +1246,7 @@ int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
        if (threaded_run(folder, &param, &result, starttls_run))
                return MAILIMAP_ERROR_INVAL;
 
-       debug_print("imap starttls - end\n");
+       debug_print("imap STARTTLS - end\n");
 
        if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
                if (etpan_certificate_check(param.imap->imap_stream, host, port,
@@ -1985,7 +2075,7 @@ static void fetch_uid_run(struct etpan_thread_op * op)
 
        fetch_result = NULL;
        mailstream_logger = imap_logger_noop;
-       log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
+       log_print(LOG_PROTOCOL, "IMAP- [fetching UIDs...]\n");
 
        r = imap_get_messages_list(param->imap, param->first_index,
                                   &fetch_result);
@@ -2242,7 +2332,7 @@ int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
        param.first_index = first_index;
        
        mailstream_logger = imap_logger_noop;
-       log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
+       log_print(LOG_PROTOCOL, "IMAP- [fetching flags...]\n");
 
        threaded_run(folder, &param, &result, fetch_uid_flags_run);
 
@@ -2536,27 +2626,27 @@ static void fetch_content_run(struct etpan_thread_op * op)
                        goto free;
                }
                
-               f = fdopen(fd, "wb");
+               f = claws_fdopen(fd, "wb");
                if (f == NULL) {
                        result->error = MAILIMAP_ERROR_FETCH;
                        goto close;
                }
                
-               r = fwrite(content, 1, content_size, f);
+               r = claws_fwrite(content, 1, content_size, f);
                if (r < content_size) {
                        result->error = MAILIMAP_ERROR_FETCH;
-                       goto fclose;
+                       goto do_fclose;
                }
                
-               r = fclose(f);
+               r = claws_safe_fclose(f);
                if (r == EOF) {
                        result->error = MAILIMAP_ERROR_FETCH;
                        goto unlink;
                }
                goto free;
                
-       fclose:
-               fclose(f);
+       do_fclose:
+               claws_fclose(f);
                goto unlink;
        close:
                close(fd);
@@ -2858,7 +2948,12 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
                r = imap_add_envelope_fetch_att(fetch_type);
        else
                r = imap_add_header_fetch_att(fetch_type);
-       
+
+       if (r != MAILIMAP_NO_ERROR) {
+               debug_print("add fetch attr: %d\n", r);
+               return r;
+       }
+
        mailstream_logger = imap_logger_fetch;
        
        r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);