2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
27 #include <glib/gi18n.h>
41 static void smtp_session_destroy(Session *session);
43 static gint smtp_auth(SMTPSession *session);
45 static gint smtp_starttls(SMTPSession *session);
47 static gint smtp_auth_cram_md5(SMTPSession *session);
48 static gint smtp_auth_login(SMTPSession *session);
49 static gint smtp_auth_plain(SMTPSession *session);
51 static gint smtp_ehlo(SMTPSession *session);
52 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
54 static gint smtp_helo(SMTPSession *session);
55 static gint smtp_rcpt(SMTPSession *session);
56 static gint smtp_data(SMTPSession *session);
57 static gint smtp_send_data(SMTPSession *session);
58 static gint smtp_make_ready(SMTPSession *session);
59 static gint smtp_eom(SMTPSession *session);
61 static gint smtp_session_recv_msg(Session *session, const gchar *msg);
62 static gint smtp_session_send_data_finished(Session *session, guint len);
65 Session *smtp_session_new(void *prefs_account)
69 session = g_new0(SMTPSession, 1);
71 session_init(SESSION(session), prefs_account, TRUE);
73 SESSION(session)->type = SESSION_SMTP;
75 SESSION(session)->recv_msg = smtp_session_recv_msg;
77 SESSION(session)->recv_data_finished = NULL;
78 SESSION(session)->send_data_finished = smtp_session_send_data_finished;
80 SESSION(session)->destroy = smtp_session_destroy;
82 session->state = SMTP_READY;
85 session->tls_init_done = FALSE;
88 session->hostname = NULL;
93 session->to_list = NULL;
94 session->cur_to = NULL;
96 session->send_data = NULL;
97 session->send_data_len = 0;
99 session->max_message_size = -1;
101 session->avail_auth_type = 0;
102 session->forced_auth_type = 0;
103 session->auth_type = 0;
104 session->esmtp_flags = 0;
106 session->error_val = SM_OK;
107 session->error_msg = NULL;
109 return SESSION(session);
112 static void smtp_session_destroy(Session *session)
114 SMTPSession *smtp_session = SMTP_SESSION(session);
116 g_free(smtp_session->hostname);
117 g_free(smtp_session->user);
118 g_free(smtp_session->pass);
119 g_free(smtp_session->from);
121 g_free(smtp_session->send_data);
123 g_free(smtp_session->error_msg);
126 gint smtp_from(SMTPSession *session)
128 gchar buf[MESSAGEBUFSIZE];
129 gchar *mail_size = NULL;
131 cm_return_val_if_fail(session->from != NULL, SM_ERROR);
133 session->state = SMTP_FROM;
135 if (session->is_esmtp && (session->esmtp_flags & ESMTP_SIZE)!=0)
136 mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
138 mail_size = g_strdup("");
141 if (strchr(session->from, '<'))
142 g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
145 g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
150 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
152 log_print(LOG_PROTOCOL, "%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
157 static gint smtp_auth(SMTPSession *session)
160 cm_return_val_if_fail(session->user != NULL, SM_ERROR);
162 session->state = SMTP_AUTH;
164 if ((session->forced_auth_type == SMTPAUTH_CRAM_MD5
165 || session->forced_auth_type == 0)
167 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0)
168 smtp_auth_cram_md5(session);
169 else if ((session->forced_auth_type == SMTPAUTH_LOGIN
170 || session->forced_auth_type == 0)
172 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0)
173 smtp_auth_login(session);
174 else if ((session->forced_auth_type == SMTPAUTH_PLAIN
175 || session->forced_auth_type == 0)
177 (session->avail_auth_type & SMTPAUTH_PLAIN) != 0)
178 smtp_auth_plain(session);
179 else if (session->forced_auth_type == 0) {
180 log_warning(LOG_PROTOCOL, _("No SMTP AUTH method available\n"));
183 log_warning(LOG_PROTOCOL, _("Selected SMTP AUTH method not available\n"));
190 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
192 gchar buf[MESSAGEBUFSIZE];
194 switch (session->auth_type) {
196 session->state = SMTP_AUTH_LOGIN_USER;
198 if (!strncmp(msg, "334 ", 4)) {
199 base64_encode(buf, session->user, strlen(session->user));
201 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
204 log_print(LOG_PROTOCOL, "ESMTP> [USERID]\n");
206 /* Server rejects AUTH */
207 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
210 log_print(LOG_PROTOCOL, "ESMTP> *\n");
213 case SMTPAUTH_CRAM_MD5:
214 session->state = SMTP_AUTH_CRAM_MD5;
216 if (!strncmp(msg, "334 ", 4)) {
221 guchar hexdigest[33];
223 challenge = g_malloc(strlen(msg + 4) + 1);
224 challengelen = base64_decode(challenge, msg + 4, -1);
225 challenge[challengelen] = '\0';
226 log_print(LOG_PROTOCOL, "ESMTP< [Decoded: %s]\n", challenge);
228 g_snprintf(buf, sizeof(buf), "%s", session->pass);
229 md5_hex_hmac(hexdigest, challenge, challengelen,
230 buf, strlen(session->pass));
233 response = g_strdup_printf
234 ("%s %s", session->user, hexdigest);
235 log_print(LOG_PROTOCOL, "ESMTP> [Encoded: %s]\n", response);
237 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
238 base64_encode(response64, response, strlen(response));
241 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
244 log_print(LOG_PROTOCOL, "ESMTP> %s\n", response64);
247 /* Server rejects AUTH */
248 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
251 log_print(LOG_PROTOCOL, "ESMTP> *\n");
254 case SMTPAUTH_DIGEST_MD5:
256 /* stop smtp_auth when no correct authtype */
257 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*") < 0)
259 log_print(LOG_PROTOCOL, "ESMTP> *\n");
266 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
268 gchar buf[MESSAGEBUFSIZE];
270 session->state = SMTP_AUTH_LOGIN_PASS;
272 if (!strncmp(msg, "334 ", 4))
273 base64_encode(buf, session->pass, strlen(session->pass));
275 /* Server rejects AUTH */
276 g_snprintf(buf, sizeof(buf), "*");
278 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
281 log_print(LOG_PROTOCOL, "ESMTP> [PASSWORD]\n");
286 static gint smtp_ehlo(SMTPSession *session)
288 gchar buf[MESSAGEBUFSIZE];
290 session->state = SMTP_EHLO;
292 session->avail_auth_type = 0;
294 g_snprintf(buf, sizeof(buf), "EHLO %s",
295 session->hostname ? session->hostname : get_domain_name());
296 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
298 log_print(LOG_PROTOCOL, "ESMTP> %s\n", buf);
303 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
305 if (strncmp(msg, "250", 3) == 0) {
306 const gchar *p = msg;
308 if (*p == '-' || *p == ' ') p++;
309 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
311 if (strcasestr(p, "PLAIN"))
312 session->avail_auth_type |= SMTPAUTH_PLAIN;
313 if (strcasestr(p, "LOGIN"))
314 session->avail_auth_type |= SMTPAUTH_LOGIN;
315 if (strcasestr(p, "CRAM-MD5"))
316 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
317 if (strcasestr(p, "DIGEST-MD5"))
318 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
320 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
322 session->max_message_size = atoi(p);
323 session->esmtp_flags |= ESMTP_SIZE;
325 if (g_ascii_strncasecmp(p, "STARTTLS", 8) == 0) {
327 session->avail_auth_type |= SMTPAUTH_TLS_AVAILABLE;
330 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
331 (msg[3] == ' ' || msg[3] == '\0'))
333 else if (msg[0] == '5' && msg[1] == '0' &&
334 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
341 static gint smtp_starttls(SMTPSession *session)
343 session->state = SMTP_STARTTLS;
345 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS") < 0)
347 log_print(LOG_PROTOCOL, "ESMTP> STARTTLS\n");
353 static gint smtp_auth_cram_md5(SMTPSession *session)
355 session->state = SMTP_AUTH;
356 session->auth_type = SMTPAUTH_CRAM_MD5;
358 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5") < 0)
360 log_print(LOG_PROTOCOL, "ESMTP> AUTH CRAM-MD5\n");
365 static gint smtp_auth_plain(SMTPSession *session)
367 gchar buf[MESSAGEBUFSIZE];
373 int b64len = (1 + strlen(session->user) + 1 + strlen(session->pass) + 1);
374 gchar *b64buf = g_malloc(b64len);
376 /* use the char *ptr to walk the base64 string with embedded \0 */
380 session->state = SMTP_AUTH_PLAIN;
381 session->auth_type = SMTPAUTH_PLAIN;
383 memset(buf, 0, sizeof buf);
386 * have to construct the string bit by bit. sprintf can't do it in one.
387 * first field is null, so string is \0<user>\0<password>
392 g_snprintf (a, b64len - 1, "%s", session->user);
394 b64cnt = strlen(session->user)+1;
397 g_snprintf (a, b64len - b64cnt - 1, "%s", session->pass);
398 b64cnt += strlen(session->pass) + 1;
401 * reuse the char *ptr to offset into the textbuf to meld
402 * the plaintext ESMTP message and the base64 string value
404 strcpy(buf, "AUTH PLAIN ");
405 a = buf + strlen(buf);
406 base64_encode(a, b64buf, b64cnt);
408 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
411 log_print(LOG_PROTOCOL, "ESMTP> [AUTH PLAIN]\n");
418 static gint smtp_auth_login(SMTPSession *session)
420 session->state = SMTP_AUTH;
421 session->auth_type = SMTPAUTH_LOGIN;
423 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN") < 0)
425 log_print(LOG_PROTOCOL, "ESMTP> AUTH LOGIN\n");
430 static gint smtp_helo(SMTPSession *session)
432 gchar buf[MESSAGEBUFSIZE];
434 session->state = SMTP_HELO;
436 g_snprintf(buf, sizeof(buf), "HELO %s",
437 session->hostname ? session->hostname : get_domain_name());
438 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
440 log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
445 static gint smtp_rcpt(SMTPSession *session)
447 gchar buf[MESSAGEBUFSIZE];
450 cm_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
452 session->state = SMTP_RCPT;
454 to = (gchar *)session->cur_to->data;
457 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
459 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
460 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
462 log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
464 session->cur_to = session->cur_to->next;
469 static gint smtp_data(SMTPSession *session)
471 session->state = SMTP_DATA;
473 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA") < 0)
475 log_print(LOG_PROTOCOL, "SMTP> DATA\n");
480 static gint smtp_send_data(SMTPSession *session)
482 session->state = SMTP_SEND_DATA;
484 session_send_data(SESSION(session), session->send_data,
485 session->send_data_len);
490 static gint smtp_make_ready(SMTPSession *session)
492 session->state = SMTP_MAIL_SENT_OK;
497 gint smtp_quit(SMTPSession *session)
499 session->state = SMTP_QUIT;
501 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
502 log_print(LOG_PROTOCOL, "SMTP> QUIT\n");
507 static gint smtp_eom(SMTPSession *session)
509 session->state = SMTP_EOM;
511 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".") < 0)
513 log_print(LOG_PROTOCOL, "SMTP> . (EOM)\n");
518 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
520 SMTPSession *smtp_session = SMTP_SESSION(session);
521 gboolean cont = FALSE;
524 if (strlen(msg) < 4) {
525 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
529 switch (smtp_session->state) {
533 case SMTP_AUTH_PLAIN:
534 case SMTP_AUTH_LOGIN_USER:
535 case SMTP_AUTH_LOGIN_PASS:
536 case SMTP_AUTH_CRAM_MD5:
537 log_print(LOG_PROTOCOL, "ESMTP< %s\n", msg);
540 log_print(LOG_PROTOCOL, "SMTP< %s\n", msg);
544 /* ignore all multiline responses except for EHLO */
545 if (msg[3] == '-' && smtp_session->state != SMTP_EHLO)
546 return session_recv_msg(session);
548 if (msg[0] == '5' && msg[1] == '0' &&
549 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
550 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
551 smtp_session->state = SMTP_ERROR;
552 smtp_session->error_val = SM_ERROR;
553 g_free(smtp_session->error_msg);
554 smtp_session->error_msg = g_strdup(msg);
558 if (!strncmp(msg, "535", 3)) {
559 log_warning(LOG_PROTOCOL, _("error occurred on authentication\n"));
560 smtp_session->state = SMTP_ERROR;
561 smtp_session->error_val = SM_AUTHFAIL;
562 g_free(smtp_session->error_msg);
563 smtp_session->error_msg = g_strdup(msg);
567 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
568 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
569 smtp_session->state = SMTP_ERROR;
570 smtp_session->error_val = SM_ERROR;
571 g_free(smtp_session->error_msg);
572 smtp_session->error_msg = g_strdup(msg);
578 else if (msg[3] != ' ' && msg[3] != '\0') {
579 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
580 smtp_session->state = SMTP_ERROR;
581 smtp_session->error_val = SM_UNRECOVERABLE;
585 switch (smtp_session->state) {
587 if (strstr(msg, "ESMTP"))
588 smtp_session->is_esmtp = TRUE;
590 if (smtp_session->user || session->ssl_type != SSL_NONE ||
591 smtp_session->is_esmtp)
593 if (smtp_session->user || smtp_session->is_esmtp)
595 ret = smtp_ehlo(smtp_session);
597 ret = smtp_helo(smtp_session);
600 ret = smtp_from(smtp_session);
603 ret = smtp_ehlo_recv(smtp_session, msg);
606 if (smtp_session->max_message_size > 0
607 && smtp_session->max_message_size <
608 smtp_session->send_data_len) {
609 log_warning(LOG_PROTOCOL, _("Message is too big "
610 "(Maximum size is %s)\n"),
612 (goffset)(smtp_session->max_message_size)));
613 smtp_session->state = SMTP_ERROR;
614 smtp_session->error_val = SM_ERROR;
618 if (session->ssl_type == SSL_STARTTLS &&
619 smtp_session->tls_init_done == FALSE) {
620 ret = smtp_starttls(smtp_session);
624 if (smtp_session->user) {
625 if (smtp_auth(smtp_session) != SM_OK) {
627 if (session->ssl_type == SSL_NONE
628 && smtp_session->tls_init_done == FALSE
629 && (smtp_session->avail_auth_type & SMTPAUTH_TLS_AVAILABLE))
630 ret = smtp_starttls(smtp_session);
633 ret = smtp_from(smtp_session);
636 ret = smtp_from(smtp_session);
640 if (session_start_tls(session) < 0) {
641 log_warning(LOG_PROTOCOL, _("couldn't start TLS session\n"));
642 smtp_session->state = SMTP_ERROR;
643 smtp_session->error_val = SM_ERROR;
646 smtp_session->tls_init_done = TRUE;
647 ret = smtp_ehlo(smtp_session);
651 ret = smtp_auth_recv(smtp_session, msg);
653 case SMTP_AUTH_LOGIN_USER:
654 ret = smtp_auth_login_user_recv(smtp_session, msg);
656 case SMTP_AUTH_PLAIN:
657 case SMTP_AUTH_LOGIN_PASS:
658 case SMTP_AUTH_CRAM_MD5:
659 ret = smtp_from(smtp_session);
662 if (smtp_session->cur_to)
663 ret = smtp_rcpt(smtp_session);
666 if (smtp_session->cur_to)
667 ret = smtp_rcpt(smtp_session);
669 ret = smtp_data(smtp_session);
672 ret = smtp_send_data(smtp_session);
675 smtp_make_ready(smtp_session);
678 session_disconnect(session);
682 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
683 smtp_session->error_val = SM_ERROR;
687 if (cont && ret == SM_OK)
688 return session_recv_msg(session);
691 smtp_session->error_val = SM_ERROR;
693 return ret == SM_OK ? 0 : -1;
696 static gint smtp_session_send_data_finished(Session *session, guint len)
698 return smtp_eom(SMTP_SESSION(session));