2006-05-16 [wwp] 2.2.0cvs20
[claws.git] / src / etpan / imap-thread.c
index 9b7a122df2067620d1b96a6c6e6d0f22fe5e82a8..465d14aec3ef2827bf74d38f151a0178649d5cd5 100644 (file)
@@ -8,7 +8,7 @@
 #include <imap.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#if (defined (__NetBSD__) || defined (__FreeBSD__))
+#if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__CYGWIN__))
 #include <sys/socket.h>
 #endif
 #include <fcntl.h>
@@ -19,6 +19,7 @@
 #include <log.h>
 #include "etpan-thread-manager.h"
 #include "utils.h"
+#include "ssl_certificate.h"
 
 #define DISABLE_LOG_DURING_LOGIN
 
@@ -39,26 +40,69 @@ static gboolean thread_manager_event(GIOChannel * source,
        return TRUE;
 }
 
-void imap_logger(int direction, const char * str, size_t size) 
+void imap_logger_cmd(int direction, const char * str, size_t size) 
 {
-       gchar buf[512];
+       gchar *buf;
+       gchar **lines;
+       int i = 0;
+
+       buf = malloc(size+1);
+       memset(buf, 0, size+1);
+       strncpy(buf, str, size);
+       buf[size] = '\0';
 
-       memset(buf, 0, 512);
-       strncpy(buf, str, size > 510 ? 510:size);
-       buf[511] = '\0';
-       if (size < 511)
-               buf[size] = '\0';
        if (!strncmp(buf, "<<<<<<<", 7) 
-       ||  !strncmp(buf, ">>>>>>>", 7) 
-       ||  buf[0] == '\r' ||  buf[0] == '\n')
+       ||  !strncmp(buf, ">>>>>>>", 7)) {
+               free(buf);
                return;
+       }
+       while (strstr(buf, "\r"))
+               *strstr(buf, "\r") = ' ';
+       while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
+               buf[strlen(buf)-1] = '\0';
+
+       lines = g_strsplit(buf, "\n", -1);
+
+       while (lines[i] && *lines[i]) {
+               log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+               i++;
+       }
+       g_strfreev(lines);
+       free(buf);
+}
+
+void imap_logger_fetch(int direction, const char * str, size_t size) 
+{
+       gchar *buf;
+       gchar **lines;
+       int i = 0;
 
+       buf = malloc(size+1);
+       memset(buf, 0, size+1);
+       strncpy(buf, str, size);
+       buf[size] = '\0';
+       if (!strncmp(buf, "<<<<<<<", 7) 
+       ||  !strncmp(buf, ">>>>>>>", 7)) {
+               free(buf);
+               return;
+       }
        while (strstr(buf, "\r"))
                *strstr(buf, "\r") = ' ';
-       while (strstr(buf, "\n"))
-               *strstr(buf, "\n") = ' ';
+       while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
+               buf[strlen(buf)-1] = '\0';
+
+       lines = g_strsplit(buf, "\n", -1);
 
-       log_print("IMAP4%c %s\n", direction?'>':'<', buf);
+       if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
+               while (lines[i] && *lines[i]) {
+                       log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       i++;
+               }
+       } else {
+               log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+       }
+       g_strfreev(lines);
+       free(buf);
 }
 
 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
@@ -71,7 +115,7 @@ void imap_main_init(void)
        mailstream_network_delay.tv_usec = 0;
        
        mailstream_debug = 1;
-       mailstream_logger = imap_logger;
+       mailstream_logger = imap_logger_cmd;
 
        imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
        session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
@@ -284,6 +328,30 @@ int imap_threaded_connect(Folder * folder, const char * server, int port)
        return result.error;
 }
 
+static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
+{
+#ifdef USE_OPENSSL
+       struct connect_param *param = (struct connect_param *)data;
+       X509 *cert = NULL;
+
+       if (certificate == NULL || len < 0) {
+               g_warning("no cert presented.\n");
+               return 0;
+       }
+       cert = d2i_X509(NULL, (unsigned char **) &certificate, len);
+       if (cert == NULL) {
+               g_warning("can't get cert\n");
+               return 0;
+       } else if (ssl_certificate_check(cert, 
+               (gchar *)param->server, param->port) == TRUE) {
+               return 0;
+       } else {
+               return -1;
+       }
+#else
+       return 0;
+#endif
+}
 
 static void connect_ssl_run(struct etpan_thread_op * op)
 {
@@ -296,7 +364,6 @@ static void connect_ssl_run(struct etpan_thread_op * op)
        
        r = mailimap_ssl_connect(param->imap,
                                 param->server, param->port);
-       
        result->error = r;
 }
 
@@ -307,7 +374,9 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        chashdatum key;
        chashdatum value;
        mailimap * imap;
-       
+       unsigned char *certificate = NULL;
+       int cert_len;
+
        imap = mailimap_new(0, NULL);
        
        key.data = &folder;
@@ -322,6 +391,13 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        
        threaded_run(folder, &param, &result, connect_ssl_run);
        
+       if (result.error >= 0) {
+               cert_len = mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
+               if (etpan_certificate_check(certificate, cert_len, &param) < 0)
+                       return -1;
+               if (certificate) 
+                       free(certificate); 
+       }
        debug_print("connect %d\n", result.error);
        
        return result.error;
@@ -614,6 +690,8 @@ int imap_threaded_status(Folder * folder, const char * mb,
        
        * data_status = result.data_status;
        
+       mailimap_status_att_list_free(status_att_list);
+       
        return result.error;
 }
 
@@ -667,20 +745,16 @@ int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
 }
 
 
-struct starttls_param {
-       mailimap * imap;
-};
-
 struct starttls_result {
        int error;
 };
 
 static void starttls_run(struct etpan_thread_op * op)
 {
-       struct starttls_param * param;
+       struct connect_param * param;
        struct starttls_result * result;
        int r;
-       
+
        param = op->param;
        r = mailimap_starttls(param->imap);
        
@@ -701,9 +775,10 @@ static void starttls_run(struct etpan_thread_op * op)
                        result->error = MAILIMAP_ERROR_STREAM;
                        return;
                }
-               tls_low = mailstream_low_ssl_open(fd);
+
+               tls_low = mailstream_low_tls_open(fd);
                if (tls_low == NULL) {
-                       debug_print("imap starttls run - can't ssl_open\n");
+                       debug_print("imap starttls run - can't tls_open\n");
                        result->error = MAILIMAP_ERROR_STREAM;
                        return;
                }
@@ -712,19 +787,30 @@ static void starttls_run(struct etpan_thread_op * op)
        }
 }
 
-int imap_threaded_starttls(Folder * folder)
+int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
 {
-       struct starttls_param param;
+       struct connect_param param;
        struct starttls_result result;
+       int cert_len;
+       unsigned char *certificate;
        
        debug_print("imap starttls - begin\n");
        
        param.imap = get_imap(folder);
+       param.server = host;
+       param.port = port;
        
        threaded_run(folder, &param, &result, starttls_run);
        
        debug_print("imap starttls - end\n");
        
+       if (result.error == 0) {
+               cert_len = mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
+               if (etpan_certificate_check(certificate, cert_len, &param) < 0)
+                       result.error = MAILIMAP_ERROR_STREAM;
+               if (certificate) 
+                       free(certificate); 
+       }       
        return result.error;
 }
 
@@ -1348,9 +1434,13 @@ static int imap_fetch(mailimap * imap,
                goto free_fetch_att;
        }
 
+       mailstream_logger = imap_logger_fetch;
+       
        r = mailimap_uid_fetch(imap, set,
                               fetch_type, &fetch_result);
   
+       mailstream_logger = imap_logger_cmd;
+       
        mailimap_fetch_type_free(fetch_type);
        mailimap_set_free(set);
   
@@ -1820,7 +1910,7 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
        int res;
        clist * fetch_result;
        int r;
-       carray * env_list;
+       carray * env_list = NULL;
        chashdatum key;
        chashdatum value;
        
@@ -1854,11 +1944,13 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
                break;
        default:
                mailimap_fetch_type_free(fetch_type);
+               debug_print("uid_fetch: %d\n", r);
                return r;
        }
        
        if (clist_begin(fetch_result) == NULL) {
                res = MAILIMAP_ERROR_FETCH;
+               debug_print("clist_begin = NULL\n");
                goto err;
        }
        
@@ -1868,6 +1960,7 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
        if (r != MAILIMAP_NO_ERROR) {
                mailimap_fetch_type_free(fetch_type);
                res = MAILIMAP_ERROR_MEMORY;
+               debug_print("fetch_result_to_envelop_list: %d\n", res);
                goto err;
        }
        
@@ -1980,6 +2073,7 @@ struct append_param {
 
 struct append_result {
        int error;
+       int uid;
 };
 
 static void append_run(struct etpan_thread_op * op)
@@ -1990,7 +2084,7 @@ static void append_run(struct etpan_thread_op * op)
        char * data;
        size_t size;
        struct stat stat_buf;
-       int fd;
+       int fd, uid = 0;
        
        param = op->param;
        result = op->result;
@@ -2017,19 +2111,20 @@ static void append_run(struct etpan_thread_op * op)
        
        r = mailimap_append(param->imap, param->mailbox,
                            param->flag_list, NULL,
-                           data, size);
+                           data, size/*, &uid */);
        
        munmap(data, size);
        close(fd);
        
        result->error = r;
-       
-       debug_print("imap append run - end %i\n", r);
+       result->uid = uid;
+       debug_print("imap append run - end %i uid %d\n", r, uid);
 }
 
 int imap_threaded_append(Folder * folder, const char * mailbox,
                         const char * filename,
-                        struct mailimap_flag_list * flag_list)
+                        struct mailimap_flag_list * flag_list,
+                        int *uid)
 {
        struct append_param param;
        struct append_result result;
@@ -2049,7 +2144,9 @@ int imap_threaded_append(Folder * folder, const char * mailbox,
                return result.error;
        
        debug_print("imap append - end\n");
-       
+       if (uid != NULL)
+               *uid = result.uid;
+
        return result.error;
 }
 
@@ -2322,12 +2419,13 @@ int socket_connect_cmd(mailimap * imap, const char * command,
        }
        
        r = mailimap_connect(imap, s);
-       if (r != MAILIMAP_NO_ERROR) {
+       if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
+       &&  r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
                mailstream_close(s);
                return r;
        }
        
-       return MAILIMAP_NO_ERROR;
+       return r;
 }
 
 /* connect cmd */