2005-03-04 [colin] 1.0.1cvs22.1
[claws.git] / src / common / smtp.c
index d87486c56d72f207fc7ad06a40d80d3c47dc89fd..23087b5f0ae3cc68b0d00dcb3bfd12e0b8bddb72 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2004 Hiroyuki Yamamoto
  *
  * 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
  *
  * 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
 #endif
 
 #include <glib.h>
 #endif
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <stdio.h>
 #include <string.h>
 
 #include <stdio.h>
 #include <string.h>
 
-#include "intl.h"
 #include "smtp.h"
 #include "md5.h"
 #include "base64.h"
 #include "smtp.h"
 #include "md5.h"
 #include "base64.h"
@@ -40,6 +40,7 @@ static gint smtp_auth(SMTPSession *session);
 static gint smtp_starttls(SMTPSession *session);
 static gint smtp_auth_cram_md5(SMTPSession *session);
 static gint smtp_auth_login(SMTPSession *session);
 static gint smtp_starttls(SMTPSession *session);
 static gint smtp_auth_cram_md5(SMTPSession *session);
 static gint smtp_auth_login(SMTPSession *session);
+static gint smtp_auth_plain(SMTPSession *session);
 
 static gint smtp_ehlo(SMTPSession *session);
 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
 
 static gint smtp_ehlo(SMTPSession *session);
 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
@@ -61,12 +62,10 @@ Session *smtp_session_new(void)
        SMTPSession *session;
 
        session = g_new0(SMTPSession, 1);
        SMTPSession *session;
 
        session = g_new0(SMTPSession, 1);
+
+       session_init(SESSION(session));
+
        SESSION(session)->type             = SESSION_SMTP;
        SESSION(session)->type             = SESSION_SMTP;
-       SESSION(session)->server           = NULL;
-       SESSION(session)->port             = 0;
-       SESSION(session)->sock             = NULL;
-       SESSION(session)->state            = SESSION_READY;
-       SESSION(session)->data             = NULL;
 
        SESSION(session)->recv_msg         = smtp_session_recv_msg;
 
 
        SESSION(session)->recv_msg         = smtp_session_recv_msg;
 
@@ -92,9 +91,12 @@ Session *smtp_session_new(void)
        session->send_data                 = NULL;
        session->send_data_len             = 0;
 
        session->send_data                 = NULL;
        session->send_data_len             = 0;
 
+       session->max_message_size          = -1;
+
        session->avail_auth_type           = 0;
        session->forced_auth_type          = 0;
        session->auth_type                 = 0;
        session->avail_auth_type           = 0;
        session->forced_auth_type          = 0;
        session->auth_type                 = 0;
+       session->esmtp_flags               = 0;
 
        session->error_val                 = SM_OK;
        session->error_msg                 = NULL;
 
        session->error_val                 = SM_OK;
        session->error_msg                 = NULL;
@@ -119,18 +121,29 @@ static void smtp_session_destroy(Session *session)
 static gint smtp_from(SMTPSession *session)
 {
        gchar buf[MSGBUFSIZE];
 static gint smtp_from(SMTPSession *session)
 {
        gchar buf[MSGBUFSIZE];
+       gchar *mail_size = NULL;
 
        g_return_val_if_fail(session->from != NULL, SM_ERROR);
 
        session->state = SMTP_FROM;
 
        g_return_val_if_fail(session->from != NULL, SM_ERROR);
 
        session->state = SMTP_FROM;
+       
+       if (session->is_esmtp && (session->esmtp_flags & ESMTP_SIZE)!=0)
+               mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
+       else
+               mail_size = g_strdup("");
+               
 
        if (strchr(session->from, '<'))
 
        if (strchr(session->from, '<'))
-               g_snprintf(buf, sizeof(buf), "MAIL FROM: %s", session->from);
+               g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
+                          mail_size);
        else
        else
-               g_snprintf(buf, sizeof(buf), "MAIL FROM: <%s>", session->from);
+               g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
+                          mail_size);
+
+       g_free(mail_size);
 
        session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
 
        session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
-       log_print("SMTP> %s\n", buf);
+       log_print("%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
 
        return SM_OK;
 }
 
        return SM_OK;
 }
@@ -150,6 +163,10 @@ static gint smtp_auth(SMTPSession *session)
                 (session->forced_auth_type == 0 &&
                  (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
                smtp_auth_login(session);
                 (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))
+               smtp_auth_plain(session);
        else {
                log_warning(_("SMTP AUTH not available\n"));
                return SM_AUTHFAIL;
        else {
                log_warning(_("SMTP AUTH not available\n"));
                return SM_AUTHFAIL;
@@ -269,8 +286,10 @@ static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
                const gchar *p = msg;
                p += 3;
                if (*p == '-' || *p == ' ') p++;
                const gchar *p = msg;
                p += 3;
                if (*p == '-' || *p == ' ') p++;
-               if (g_strncasecmp(p, "AUTH", 4) == 0) {
+               if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
                        p += 5;
                        p += 5;
+                       if (strcasestr(p, "PLAIN"))
+                               session->avail_auth_type |= SMTPAUTH_PLAIN;
                        if (strcasestr(p, "LOGIN"))
                                session->avail_auth_type |= SMTPAUTH_LOGIN;
                        if (strcasestr(p, "CRAM-MD5"))
                        if (strcasestr(p, "LOGIN"))
                                session->avail_auth_type |= SMTPAUTH_LOGIN;
                        if (strcasestr(p, "CRAM-MD5"))
@@ -278,6 +297,11 @@ static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
                        if (strcasestr(p, "DIGEST-MD5"))
                                session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
                }
                        if (strcasestr(p, "DIGEST-MD5"))
                                session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
                }
+               if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
+                       p += 5;
+                       session->max_message_size = atoi(p);
+                       session->esmtp_flags |= ESMTP_SIZE;
+               }
                return SM_OK;
        } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
            (msg[3] == ' ' || msg[3] == '\0'))
                return SM_OK;
        } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
            (msg[3] == ' ' || msg[3] == '\0'))
@@ -310,6 +334,58 @@ static gint smtp_auth_cram_md5(SMTPSession *session)
        return SM_OK;
 }
 
        return SM_OK;
 }
 
+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;
+
+       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);
+
+       session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
+
+       log_print("ESMTP> [AUTH PLAIN]\n");
+
+       g_free(b64buf);
+
+       return SM_OK;
+}
+
 static gint smtp_auth_login(SMTPSession *session)
 {
        session->state = SMTP_AUTH;
 static gint smtp_auth_login(SMTPSession *session)
 {
        session->state = SMTP_AUTH;
@@ -347,9 +423,9 @@ static gint smtp_rcpt(SMTPSession *session)
        to = (gchar *)session->cur_to->data;
 
        if (strchr(to, '<'))
        to = (gchar *)session->cur_to->data;
 
        if (strchr(to, '<'))
-               g_snprintf(buf, sizeof(buf), "RCPT TO: %s", to);
+               g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
        else
        else
-               g_snprintf(buf, sizeof(buf), "RCPT TO: <%s>", to);
+               g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
        session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
        log_print("SMTP> %s\n", buf);
 
        session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
        log_print("SMTP> %s\n", buf);
 
@@ -424,6 +500,7 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
        case SMTP_EHLO:
        case SMTP_STARTTLS:
        case SMTP_AUTH:
        case SMTP_EHLO:
        case SMTP_STARTTLS:
        case SMTP_AUTH:
+       case SMTP_AUTH_PLAIN:
        case SMTP_AUTH_LOGIN_USER:
        case SMTP_AUTH_LOGIN_PASS:
        case SMTP_AUTH_CRAM_MD5:
        case SMTP_AUTH_LOGIN_USER:
        case SMTP_AUTH_LOGIN_PASS:
        case SMTP_AUTH_CRAM_MD5:
@@ -473,15 +550,18 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
 
        /* ignore all multiline responses except for EHLO */
        if (cont && smtp_session->state != SMTP_EHLO)
 
        /* ignore all multiline responses except for EHLO */
        if (cont && smtp_session->state != SMTP_EHLO)
-               return 1;
+               return session_recv_msg(session);
 
        switch (smtp_session->state) {
        case SMTP_READY:
 
        switch (smtp_session->state) {
        case SMTP_READY:
+               if (strstr(msg, "ESMTP"))
+                       smtp_session->is_esmtp = TRUE;
        case SMTP_CONNECTED:
 #if USE_OPENSSL
        case SMTP_CONNECTED:
 #if USE_OPENSSL
-               if (smtp_session->user || session->ssl_type != SSL_NONE)
+               if (smtp_session->user || session->ssl_type != SSL_NONE ||
+                   smtp_session->is_esmtp)
 #else
 #else
-               if (smtp_session->user)
+               if (smtp_session->user || smtp_session->is_esmtp)
 #endif
                        smtp_ehlo(smtp_session);
                else
 #endif
                        smtp_ehlo(smtp_session);
                else
@@ -494,6 +574,17 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
                smtp_ehlo_recv(smtp_session, msg);
                if (cont == TRUE)
                        break;
                smtp_ehlo_recv(smtp_session, msg);
                if (cont == TRUE)
                        break;
+               if (smtp_session->max_message_size > 0
+               && smtp_session->max_message_size < 
+                  smtp_session->send_data_len) {
+                       log_warning(_("Message is too big "
+                             "(Maximum size is %s)\n"),
+                             to_human_readable(
+                              (off_t)(smtp_session->max_message_size)));
+                       smtp_session->state = SMTP_ERROR;
+                       smtp_session->error_val = SM_ERROR;
+                       return -1;
+               }
 #if USE_OPENSSL
                if (session->ssl_type == SSL_STARTTLS &&
                    smtp_session->tls_init_done == FALSE) {
 #if USE_OPENSSL
                if (session->ssl_type == SSL_STARTTLS &&
                    smtp_session->tls_init_done == FALSE) {
@@ -525,6 +616,7 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
        case SMTP_AUTH_LOGIN_USER:
                smtp_auth_login_user_recv(smtp_session, msg);
                break;
        case SMTP_AUTH_LOGIN_USER:
                smtp_auth_login_user_recv(smtp_session, msg);
                break;
+       case SMTP_AUTH_PLAIN:
        case SMTP_AUTH_LOGIN_PASS:
        case SMTP_AUTH_CRAM_MD5:
                smtp_from(smtp_session);
        case SMTP_AUTH_LOGIN_PASS:
        case SMTP_AUTH_CRAM_MD5:
                smtp_from(smtp_session);
@@ -556,7 +648,7 @@ static gint smtp_session_recv_msg(Session *session, const gchar *msg)
        }
 
        if (cont)
        }
 
        if (cont)
-               return 1;
+               return session_recv_msg(session);
 
        return 0;
 }
 
        return 0;
 }