Fix SMTP LOGIN auth method.
[claws.git] / src / common / smtp.c
index f7e5c39d48378557d1d01cc39fc5bb6c46e08d0d..4c818d8ede0f8044b3460c947f2a2a4a1152a957 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail 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
@@ -19,6 +19,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include <glib.h>
 
 #include "smtp.h"
 #include "md5.h"
-#include "base64.h"
 #include "utils.h"
 #include "log.h"
 
 static void smtp_session_destroy(Session *session);
 
 static gint smtp_auth(SMTPSession *session);
-#if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
+#ifdef USE_GNUTLS
 static gint smtp_starttls(SMTPSession *session);
 #endif
 static gint smtp_auth_cram_md5(SMTPSession *session);
@@ -61,13 +61,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 +80,7 @@ Session *smtp_session_new(void)
 
        session->state                     = SMTP_READY;
 
-#if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
+#ifdef USE_GNUTLS
        session->tls_init_done             = FALSE;
 #endif
 
@@ -124,10 +124,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;
        
@@ -156,24 +156,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(LOG_PROTOCOL, _("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,18 +188,22 @@ static gint smtp_auth(SMTPSession *session)
 
 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
 {
-       gchar buf[MSGBUFSIZE];
+       gchar buf[MESSAGEBUFSIZE], *tmp;
 
        switch (session->auth_type) {
        case SMTPAUTH_LOGIN:
                session->state = SMTP_AUTH_LOGIN_USER;
 
                if (!strncmp(msg, "334 ", 4)) {
-                       base64_encode(buf, session->user, strlen(session->user));
+                       tmp = g_base64_encode(session->user, strlen(session->user));
+                       debug_print("|%s|\n", tmp);
 
                        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
-                                        buf) < 0)
+                                        tmp) < 0) {
+                               g_free(tmp);
                                return SM_ERROR;
+                       }
+                       g_free(tmp);
                        log_print(LOG_PROTOCOL, "ESMTP> [USERID]\n");
                } else {
                        /* Server rejects AUTH */
@@ -209,13 +219,13 @@ static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
                if (!strncmp(msg, "334 ", 4)) {
                        gchar *response;
                        gchar *response64;
-                       gchar *challenge;
-                       gint challengelen;
+                       gchar *challenge, *tmp;
+                       gsize challengelen;
                        guchar hexdigest[33];
 
-                       challenge = g_malloc(strlen(msg + 4) + 1);
-                       challengelen = base64_decode(challenge, msg + 4, -1);
-                       challenge[challengelen] = '\0';
+                       tmp = g_base64_decode(msg + 4, &challengelen);
+                       challenge = g_strndup(tmp, challengelen);
+                       g_free(tmp);
                        log_print(LOG_PROTOCOL, "ESMTP< [Decoded: %s]\n", challenge);
 
                        g_snprintf(buf, sizeof(buf), "%s", session->pass);
@@ -227,13 +237,14 @@ static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
                                ("%s %s", session->user, hexdigest);
                        log_print(LOG_PROTOCOL, "ESMTP> [Encoded: %s]\n", response);
 
-                       response64 = g_malloc((strlen(response) + 3) * 2 + 1);
-                       base64_encode(response64, response, strlen(response));
+                       response64 = g_base64_encode(response, strlen(response));
                        g_free(response);
 
                        if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
-                                        response64) < 0)
+                                        response64) < 0) {
+                               g_free(response64);
                                return SM_ERROR;
+                       }
                        log_print(LOG_PROTOCOL, "ESMTP> %s\n", response64);
                        g_free(response64);
                } else {
@@ -258,18 +269,22 @@ 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 *tmp;
 
        session->state = SMTP_AUTH_LOGIN_PASS;
 
-       if (!strncmp(msg, "334 ", 4))
-               base64_encode(buf, session->pass, strlen(session->pass));
-       else
+       if (!strncmp(msg, "334 ", 4)) {
+               tmp = g_base64_encode(session->pass, strlen(session->pass));
+       } else {
                /* Server rejects AUTH */
-               g_snprintf(buf, sizeof(buf), "*");
+               tmp = g_strdup("*");
+       }
 
-       if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
+       if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, tmp) < 0) {
+               g_free(tmp);
                return SM_ERROR;
+       }
+       g_free(tmp);
 
        log_print(LOG_PROTOCOL, "ESMTP> [PASSWORD]\n");
 
@@ -278,7 +293,7 @@ static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
 
 static gint smtp_ehlo(SMTPSession *session)
 {
-       gchar buf[MSGBUFSIZE];
+       gchar buf[MESSAGEBUFSIZE];
 
        session->state = SMTP_EHLO;
 
@@ -330,7 +345,7 @@ static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
        return SM_ERROR;
 }
 
-#if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
+#ifdef USE_GNUTLS
 static gint smtp_starttls(SMTPSession *session)
 {
        session->state = SMTP_STARTTLS;
@@ -357,49 +372,26 @@ static gint smtp_auth_cram_md5(SMTPSession *session)
 
 static gint smtp_auth_plain(SMTPSession *session)
 {
-       gchar buf[MSGBUFSIZE];
-
-       /* 
-        * +1      +1      +1
-        * \0<user>\0<pass>\0 
-        */
-       int b64len = (1 + strlen(session->user) + 1 + strlen(session->pass) + 1);
-       gchar *b64buf = g_malloc(b64len);
-
-       /* use the char *ptr to walk the base64 string with embedded \0 */
-       char  *a = b64buf;
-       int  b64cnt = 0;
+       gchar buf[MESSAGEBUFSIZE], *b64buf, *out;
+       gint len;
 
        session->state = SMTP_AUTH_PLAIN;
        session->auth_type = SMTPAUTH_PLAIN;
 
        memset(buf, 0, sizeof buf);
 
-       /*
-        * have to construct the string bit by bit. sprintf can't do it in one.
-        * first field is null, so string is \0<user>\0<password>
-        */
-       *a = 0;
-       a++;
-
-       g_snprintf (a, b64len - 1, "%s", session->user);
-
-       b64cnt = strlen(session->user)+1;
-       a += b64cnt;
-
-       g_snprintf (a, b64len - b64cnt - 1, "%s", session->pass);
-       b64cnt += strlen(session->pass) + 1;    
-
-       /*
-        * reuse the char *ptr to offset into the textbuf to meld
-        * the plaintext ESMTP message and the base64 string value
-        */
-       strcpy(buf, "AUTH PLAIN ");
-       a = buf + strlen(buf);
-       base64_encode(a, b64buf, b64cnt);
+       /* "\0user\0password" */
+       len = sprintf(buf, "%c%s%c%s", '\0', session->user, '\0', session->pass);
+       b64buf = g_base64_encode(buf, len);
+       out = g_strconcat("AUTH PLAIN ", b64buf, NULL);
+       g_free(b64buf);
 
-       if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
+       if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, out) < 0) {
+               g_free(out);
                return SM_ERROR;
+       }
+
+       g_free(out);
 
        log_print(LOG_PROTOCOL, "ESMTP> [AUTH PLAIN]\n");
 
@@ -422,7 +414,7 @@ static gint smtp_auth_login(SMTPSession *session)
 
 static gint smtp_helo(SMTPSession *session)
 {
-       gchar buf[MSGBUFSIZE];
+       gchar buf[MESSAGEBUFSIZE];
 
        session->state = SMTP_HELO;
 
@@ -437,10 +429,10 @@ static gint smtp_helo(SMTPSession *session)
 
 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;
 
@@ -534,6 +526,10 @@ static gint smtp_session_recv_msg(Session *session, const gchar *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(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
@@ -571,16 +567,11 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
                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 (defined(USE_OPENSSL) || defined (USE_GNUTLS))
+#ifdef USE_GNUTLS
                if (smtp_session->user || session->ssl_type != SSL_NONE ||
                    smtp_session->is_esmtp)
 #else
@@ -603,12 +594,12 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
                        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 (defined(USE_OPENSSL) || defined (USE_GNUTLS))
+#ifdef USE_GNUTLS
                if (session->ssl_type == SSL_STARTTLS &&
                    smtp_session->tls_init_done == FALSE) {
                        ret = smtp_starttls(smtp_session);
@@ -617,7 +608,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 (defined(USE_OPENSSL) || defined (USE_GNUTLS))
+#ifdef USE_GNUTLS
                                if (session->ssl_type == SSL_NONE
                                &&  smtp_session->tls_init_done == FALSE
                                &&  (smtp_session->avail_auth_type & SMTPAUTH_TLS_AVAILABLE))
@@ -630,7 +621,7 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
                        ret = smtp_from(smtp_session);
                break;
        case SMTP_STARTTLS:
-#if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
+#ifdef USE_GNUTLS
                if (session_start_tls(session) < 0) {
                        log_warning(LOG_PROTOCOL, _("couldn't start TLS session\n"));
                        smtp_session->state = SMTP_ERROR;