Fix bug #2875, "SMTP session disconnects before recieving multi-line
[claws.git] / src / common / smtp.c
index 60915a326209a4abb1f8a45ffebc6215ab45bcd0..e540d1fc057d6924b6582ba1cf065248bcbd6ded 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
+ * Copyright (C) 1999-2012 Hiroyuki Yamamoto 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
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include <glib.h>
@@ -40,7 +41,7 @@
 static void smtp_session_destroy(Session *session);
 
 static gint smtp_auth(SMTPSession *session);
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
 static gint smtp_starttls(SMTPSession *session);
 #endif
 static gint smtp_auth_cram_md5(SMTPSession *session);
@@ -61,13 +62,13 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg);
 static gint smtp_session_send_data_finished(Session *session, guint len);
 
 
-Session *smtp_session_new(void)
+Session *smtp_session_new(void *prefs_account)
 {
        SMTPSession *session;
 
        session = g_new0(SMTPSession, 1);
 
-       session_init(SESSION(session));
+       session_init(SESSION(session), prefs_account, TRUE);
 
        SESSION(session)->type             = SESSION_SMTP;
 
@@ -80,7 +81,7 @@ Session *smtp_session_new(void)
 
        session->state                     = SMTP_READY;
 
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
        session->tls_init_done             = FALSE;
 #endif
 
@@ -124,10 +125,10 @@ static void smtp_session_destroy(Session *session)
 
 gint smtp_from(SMTPSession *session)
 {
-       gchar buf[MSGBUFSIZE];
+       gchar buf[MESSAGEBUFSIZE];
        gchar *mail_size = NULL;
 
-       g_return_val_if_fail(session->from != NULL, SM_ERROR);
+       cm_return_val_if_fail(session->from != NULL, SM_ERROR);
 
        session->state = SMTP_FROM;
        
@@ -148,7 +149,7 @@ gint smtp_from(SMTPSession *session)
 
        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
                return SM_ERROR;
-       log_print("%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
+       log_print(LOG_PROTOCOL, "%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
 
        return SM_OK;
 }
@@ -156,24 +157,30 @@ gint smtp_from(SMTPSession *session)
 static gint smtp_auth(SMTPSession *session)
 {
 
-       g_return_val_if_fail(session->user != NULL, SM_ERROR);
+       cm_return_val_if_fail(session->user != NULL, SM_ERROR);
 
        session->state = SMTP_AUTH;
 
-       if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
-           (session->forced_auth_type == 0 &&
-            (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
+       if ((session->forced_auth_type == SMTPAUTH_CRAM_MD5
+            || session->forced_auth_type == 0)
+            &&
+            (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0)
                smtp_auth_cram_md5(session);
-       else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
-                (session->forced_auth_type == 0 &&
-                 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
+       else if ((session->forced_auth_type == SMTPAUTH_LOGIN
+                 || session->forced_auth_type == 0)
+                 &&
+                 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0)
                smtp_auth_login(session);
-       else if (session->forced_auth_type == SMTPAUTH_PLAIN ||
-                (session->forced_auth_type == 0 &&
-                 (session->avail_auth_type & SMTPAUTH_PLAIN) != 0))
+       else if ((session->forced_auth_type == SMTPAUTH_PLAIN
+                 || session->forced_auth_type == 0)
+                 &&
+                 (session->avail_auth_type & SMTPAUTH_PLAIN) != 0)
                smtp_auth_plain(session);
-       else {
-               log_warning(_("SMTP AUTH not available\n"));
+       else if (session->forced_auth_type == 0) {
+               log_warning(LOG_PROTOCOL, _("No SMTP AUTH method available\n"));
+               return SM_AUTHFAIL;
+       } else {
+               log_warning(LOG_PROTOCOL, _("Selected SMTP AUTH method not available\n"));
                return SM_AUTHFAIL;
        }
 
@@ -182,7 +189,7 @@ static gint smtp_auth(SMTPSession *session)
 
 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
 {
-       gchar buf[MSGBUFSIZE];
+       gchar buf[MESSAGEBUFSIZE];
 
        switch (session->auth_type) {
        case SMTPAUTH_LOGIN:
@@ -194,13 +201,13 @@ static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
                        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
                                         buf) < 0)
                                return SM_ERROR;
-                       log_print("ESMTP> [USERID]\n");
+                       log_print(LOG_PROTOCOL, "ESMTP> [USERID]\n");
                } else {
                        /* Server rejects AUTH */
                        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
                                         "*") < 0)
                                return SM_ERROR;
-                       log_print("ESMTP> *\n");
+                       log_print(LOG_PROTOCOL, "ESMTP> *\n");
                }
                break;
        case SMTPAUTH_CRAM_MD5:
@@ -216,7 +223,7 @@ static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
                        challenge = g_malloc(strlen(msg + 4) + 1);
                        challengelen = base64_decode(challenge, msg + 4, -1);
                        challenge[challengelen] = '\0';
-                       log_print("ESMTP< [Decoded: %s]\n", challenge);
+                       log_print(LOG_PROTOCOL, "ESMTP< [Decoded: %s]\n", challenge);
 
                        g_snprintf(buf, sizeof(buf), "%s", session->pass);
                        md5_hex_hmac(hexdigest, challenge, challengelen,
@@ -225,7 +232,7 @@ static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
 
                        response = g_strdup_printf
                                ("%s %s", session->user, hexdigest);
-                       log_print("ESMTP> [Encoded: %s]\n", response);
+                       log_print(LOG_PROTOCOL, "ESMTP> [Encoded: %s]\n", response);
 
                        response64 = g_malloc((strlen(response) + 3) * 2 + 1);
                        base64_encode(response64, response, strlen(response));
@@ -234,14 +241,14 @@ static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
                        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
                                         response64) < 0)
                                return SM_ERROR;
-                       log_print("ESMTP> %s\n", response64);
+                       log_print(LOG_PROTOCOL, "ESMTP> %s\n", response64);
                        g_free(response64);
                } else {
                        /* Server rejects AUTH */
                        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
                                         "*") < 0)
                                return SM_ERROR;
-                       log_print("ESMTP> *\n");
+                       log_print(LOG_PROTOCOL, "ESMTP> *\n");
                }
                break;
        case SMTPAUTH_DIGEST_MD5:
@@ -249,7 +256,7 @@ static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
                /* stop smtp_auth when no correct authtype */
                if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*") < 0)
                        return SM_ERROR;
-               log_print("ESMTP> *\n");
+               log_print(LOG_PROTOCOL, "ESMTP> *\n");
                break;
        }
 
@@ -258,7 +265,7 @@ static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
 
 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
 {
-       gchar buf[MSGBUFSIZE];
+       gchar buf[MESSAGEBUFSIZE];
 
        session->state = SMTP_AUTH_LOGIN_PASS;
 
@@ -271,14 +278,14 @@ static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
                return SM_ERROR;
 
-       log_print("ESMTP> [PASSWORD]\n");
+       log_print(LOG_PROTOCOL, "ESMTP> [PASSWORD]\n");
 
        return SM_OK;
 }
 
 static gint smtp_ehlo(SMTPSession *session)
 {
-       gchar buf[MSGBUFSIZE];
+       gchar buf[MESSAGEBUFSIZE];
 
        session->state = SMTP_EHLO;
 
@@ -288,7 +295,7 @@ static gint smtp_ehlo(SMTPSession *session)
                   session->hostname ? session->hostname : get_domain_name());
        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
                return SM_ERROR;
-       log_print("ESMTP> %s\n", buf);
+       log_print(LOG_PROTOCOL, "ESMTP> %s\n", buf);
 
        return SM_OK;
 }
@@ -330,14 +337,14 @@ static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
        return SM_ERROR;
 }
 
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
 static gint smtp_starttls(SMTPSession *session)
 {
        session->state = SMTP_STARTTLS;
 
        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS") < 0)
                return SM_ERROR;
-       log_print("ESMTP> STARTTLS\n");
+       log_print(LOG_PROTOCOL, "ESMTP> STARTTLS\n");
 
        return SM_OK;
 }
@@ -350,14 +357,14 @@ static gint smtp_auth_cram_md5(SMTPSession *session)
 
        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5") < 0)
                return SM_ERROR;
-       log_print("ESMTP> AUTH CRAM-MD5\n");
+       log_print(LOG_PROTOCOL, "ESMTP> AUTH CRAM-MD5\n");
 
        return SM_OK;
 }
 
 static gint smtp_auth_plain(SMTPSession *session)
 {
-       gchar buf[MSGBUFSIZE];
+       gchar buf[MESSAGEBUFSIZE];
 
        /* 
         * +1      +1      +1
@@ -401,7 +408,7 @@ static gint smtp_auth_plain(SMTPSession *session)
        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
                return SM_ERROR;
 
-       log_print("ESMTP> [AUTH PLAIN]\n");
+       log_print(LOG_PROTOCOL, "ESMTP> [AUTH PLAIN]\n");
 
        g_free(b64buf);
 
@@ -415,14 +422,14 @@ static gint smtp_auth_login(SMTPSession *session)
 
        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN") < 0)
                return SM_ERROR;
-       log_print("ESMTP> AUTH LOGIN\n");
+       log_print(LOG_PROTOCOL, "ESMTP> AUTH LOGIN\n");
 
        return SM_OK;
 }
 
 static gint smtp_helo(SMTPSession *session)
 {
-       gchar buf[MSGBUFSIZE];
+       gchar buf[MESSAGEBUFSIZE];
 
        session->state = SMTP_HELO;
 
@@ -430,17 +437,17 @@ static gint smtp_helo(SMTPSession *session)
                   session->hostname ? session->hostname : get_domain_name());
        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
                return SM_ERROR;
-       log_print("SMTP> %s\n", buf);
+       log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
 
        return SM_OK;
 }
 
 static gint smtp_rcpt(SMTPSession *session)
 {
-       gchar buf[MSGBUFSIZE];
+       gchar buf[MESSAGEBUFSIZE];
        gchar *to;
 
-       g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
+       cm_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
 
        session->state = SMTP_RCPT;
 
@@ -452,7 +459,7 @@ static gint smtp_rcpt(SMTPSession *session)
                g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
                return SM_ERROR;
-       log_print("SMTP> %s\n", buf);
+       log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
 
        session->cur_to = session->cur_to->next;
 
@@ -465,7 +472,7 @@ static gint smtp_data(SMTPSession *session)
 
        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA") < 0)
                return SM_ERROR;
-       log_print("SMTP> DATA\n");
+       log_print(LOG_PROTOCOL, "SMTP> DATA\n");
 
        return SM_OK;
 }
@@ -492,7 +499,7 @@ gint smtp_quit(SMTPSession *session)
        session->state = SMTP_QUIT;
 
        session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
-       log_print("SMTP> QUIT\n");
+       log_print(LOG_PROTOCOL, "SMTP> QUIT\n");
 
        return SM_OK;
 }
@@ -503,7 +510,7 @@ static gint smtp_eom(SMTPSession *session)
 
        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".") < 0)
                return SM_ERROR;
-       log_print("SMTP> . (EOM)\n");
+       log_print(LOG_PROTOCOL, "SMTP> . (EOM)\n");
 
        return SM_OK;
 }
@@ -515,7 +522,7 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
        gint ret = 0;
        
        if (strlen(msg) < 4) {
-               log_warning(_("bad SMTP response\n"));
+               log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
                return -1;
        }
 
@@ -527,16 +534,20 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
        case SMTP_AUTH_LOGIN_USER:
        case SMTP_AUTH_LOGIN_PASS:
        case SMTP_AUTH_CRAM_MD5:
-               log_print("ESMTP< %s\n", msg);
+               log_print(LOG_PROTOCOL, "ESMTP< %s\n", msg);
                break;
        default:
-               log_print("SMTP< %s\n", msg);
+               log_print(LOG_PROTOCOL, "SMTP< %s\n", msg);
                break;
        }
 
+       /* ignore all multiline responses except for EHLO */
+       if (msg[3] == '-' && smtp_session->state != SMTP_EHLO)
+               return session_recv_msg(session);
+
        if (msg[0] == '5' && msg[1] == '0' &&
            (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
-               log_warning(_("error occurred on SMTP session\n"));
+               log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
                smtp_session->state = SMTP_ERROR;
                smtp_session->error_val = SM_ERROR;
                g_free(smtp_session->error_msg);
@@ -545,7 +556,7 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
        }
 
        if (!strncmp(msg, "535", 3)) {
-               log_warning(_("error occurred on authentication\n"));
+               log_warning(LOG_PROTOCOL, _("error occurred on authentication\n"));
                smtp_session->state = SMTP_ERROR;
                smtp_session->error_val = SM_AUTHFAIL;
                g_free(smtp_session->error_msg);
@@ -554,7 +565,7 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
        }
 
        if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
-               log_warning(_("error occurred on SMTP session\n"));
+               log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
                smtp_session->state = SMTP_ERROR;
                smtp_session->error_val = SM_ERROR;
                g_free(smtp_session->error_msg);
@@ -565,22 +576,18 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
        if (msg[3] == '-')
                cont = TRUE;
        else if (msg[3] != ' ' && msg[3] != '\0') {
-               log_warning(_("bad SMTP response\n"));
+               log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
                smtp_session->state = SMTP_ERROR;
                smtp_session->error_val = SM_UNRECOVERABLE;
                return -1;
        }
 
-       /* ignore all multiline responses except for EHLO */
-       if (cont && smtp_session->state != SMTP_EHLO)
-               return session_recv_msg(session);
-
        switch (smtp_session->state) {
        case SMTP_READY:
                if (strstr(msg, "ESMTP"))
                        smtp_session->is_esmtp = TRUE;
        case SMTP_CONNECTED:
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
                if (smtp_session->user || session->ssl_type != SSL_NONE ||
                    smtp_session->is_esmtp)
 #else
@@ -600,15 +607,15 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
                if (smtp_session->max_message_size > 0
                && smtp_session->max_message_size < 
                   smtp_session->send_data_len) {
-                       log_warning(_("Message is too big "
+                       log_warning(LOG_PROTOCOL, _("Message is too big "
                              "(Maximum size is %s)\n"),
                              to_human_readable(
-                              (off_t)(smtp_session->max_message_size)));
+                              (goffset)(smtp_session->max_message_size)));
                        smtp_session->state = SMTP_ERROR;
                        smtp_session->error_val = SM_ERROR;
                        return -1;
                }
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
                if (session->ssl_type == SSL_STARTTLS &&
                    smtp_session->tls_init_done == FALSE) {
                        ret = smtp_starttls(smtp_session);
@@ -617,7 +624,7 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
 #endif
                if (smtp_session->user) {
                        if (smtp_auth(smtp_session) != SM_OK) {
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
                                if (session->ssl_type == SSL_NONE
                                &&  smtp_session->tls_init_done == FALSE
                                &&  (smtp_session->avail_auth_type & SMTPAUTH_TLS_AVAILABLE))
@@ -630,9 +637,9 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
                        ret = smtp_from(smtp_session);
                break;
        case SMTP_STARTTLS:
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
                if (session_start_tls(session) < 0) {
-                       log_warning(_("couldn't start TLS session\n"));
+                       log_warning(LOG_PROTOCOL, _("couldn't start TLS session\n"));
                        smtp_session->state = SMTP_ERROR;
                        smtp_session->error_val = SM_ERROR;
                        return -1;
@@ -673,7 +680,7 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
                break;
        case SMTP_ERROR:
        default:
-               log_warning(_("error occurred on SMTP session\n"));
+               log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
                smtp_session->error_val = SM_ERROR;
                return -1;
        }