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/>.
26 #include <glib/gi18n.h>
40 static void smtp_session_destroy(Session *session);
42 static gint smtp_auth(SMTPSession *session);
44 static gint smtp_starttls(SMTPSession *session);
46 static gint smtp_auth_cram_md5(SMTPSession *session);
47 static gint smtp_auth_login(SMTPSession *session);
48 static gint smtp_auth_plain(SMTPSession *session);
50 static gint smtp_ehlo(SMTPSession *session);
51 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
53 static gint smtp_helo(SMTPSession *session);
54 static gint smtp_rcpt(SMTPSession *session);
55 static gint smtp_data(SMTPSession *session);
56 static gint smtp_send_data(SMTPSession *session);
57 static gint smtp_make_ready(SMTPSession *session);
58 static gint smtp_eom(SMTPSession *session);
60 static gint smtp_session_recv_msg(Session *session, const gchar *msg);
61 static gint smtp_session_send_data_finished(Session *session, guint len);
64 Session *smtp_session_new(void *prefs_account)
68 session = g_new0(SMTPSession, 1);
70 session_init(SESSION(session), prefs_account, TRUE);
72 SESSION(session)->type = SESSION_SMTP;
74 SESSION(session)->recv_msg = smtp_session_recv_msg;
76 SESSION(session)->recv_data_finished = NULL;
77 SESSION(session)->send_data_finished = smtp_session_send_data_finished;
79 SESSION(session)->destroy = smtp_session_destroy;
81 session->state = SMTP_READY;
84 session->tls_init_done = FALSE;
87 session->hostname = NULL;
92 session->to_list = NULL;
93 session->cur_to = NULL;
95 session->send_data = NULL;
96 session->send_data_len = 0;
98 session->max_message_size = -1;
100 session->avail_auth_type = 0;
101 session->forced_auth_type = 0;
102 session->auth_type = 0;
103 session->esmtp_flags = 0;
105 session->error_val = SM_OK;
106 session->error_msg = NULL;
108 return SESSION(session);
111 static void smtp_session_destroy(Session *session)
113 SMTPSession *smtp_session = SMTP_SESSION(session);
115 g_free(smtp_session->hostname);
116 g_free(smtp_session->user);
117 g_free(smtp_session->pass);
118 g_free(smtp_session->from);
120 g_free(smtp_session->send_data);
122 g_free(smtp_session->error_msg);
125 gint smtp_from(SMTPSession *session)
127 gchar buf[MSGBUFSIZE];
128 gchar *mail_size = NULL;
130 cm_return_val_if_fail(session->from != NULL, SM_ERROR);
132 session->state = SMTP_FROM;
134 if (session->is_esmtp && (session->esmtp_flags & ESMTP_SIZE)!=0)
135 mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
137 mail_size = g_strdup("");
140 if (strchr(session->from, '<'))
141 g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
144 g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
149 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
151 log_print(LOG_PROTOCOL, "%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
156 static gint smtp_auth(SMTPSession *session)
159 cm_return_val_if_fail(session->user != NULL, SM_ERROR);
161 session->state = SMTP_AUTH;
163 if ((session->forced_auth_type == SMTPAUTH_CRAM_MD5
164 || session->forced_auth_type == 0)
166 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0)
167 smtp_auth_cram_md5(session);
168 else if ((session->forced_auth_type == SMTPAUTH_LOGIN
169 || session->forced_auth_type == 0)
171 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0)
172 smtp_auth_login(session);
173 else if ((session->forced_auth_type == SMTPAUTH_PLAIN
174 || session->forced_auth_type == 0)
176 (session->avail_auth_type & SMTPAUTH_PLAIN) != 0)
177 smtp_auth_plain(session);
178 else if (session->forced_auth_type == 0) {
179 log_warning(LOG_PROTOCOL, _("No SMTP AUTH method available\n"));
182 log_warning(LOG_PROTOCOL, _("Selected SMTP AUTH method not available\n"));
189 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
191 gchar buf[MSGBUFSIZE];
193 switch (session->auth_type) {
195 session->state = SMTP_AUTH_LOGIN_USER;
197 if (!strncmp(msg, "334 ", 4)) {
198 base64_encode(buf, session->user, strlen(session->user));
200 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
203 log_print(LOG_PROTOCOL, "ESMTP> [USERID]\n");
205 /* Server rejects AUTH */
206 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
209 log_print(LOG_PROTOCOL, "ESMTP> *\n");
212 case SMTPAUTH_CRAM_MD5:
213 session->state = SMTP_AUTH_CRAM_MD5;
215 if (!strncmp(msg, "334 ", 4)) {
220 guchar hexdigest[33];
222 challenge = g_malloc(strlen(msg + 4) + 1);
223 challengelen = base64_decode(challenge, msg + 4, -1);
224 challenge[challengelen] = '\0';
225 log_print(LOG_PROTOCOL, "ESMTP< [Decoded: %s]\n", challenge);
227 g_snprintf(buf, sizeof(buf), "%s", session->pass);
228 md5_hex_hmac(hexdigest, challenge, challengelen,
229 buf, strlen(session->pass));
232 response = g_strdup_printf
233 ("%s %s", session->user, hexdigest);
234 log_print(LOG_PROTOCOL, "ESMTP> [Encoded: %s]\n", response);
236 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
237 base64_encode(response64, response, strlen(response));
240 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
243 log_print(LOG_PROTOCOL, "ESMTP> %s\n", response64);
246 /* Server rejects AUTH */
247 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
250 log_print(LOG_PROTOCOL, "ESMTP> *\n");
253 case SMTPAUTH_DIGEST_MD5:
255 /* stop smtp_auth when no correct authtype */
256 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*") < 0)
258 log_print(LOG_PROTOCOL, "ESMTP> *\n");
265 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
267 gchar buf[MSGBUFSIZE];
269 session->state = SMTP_AUTH_LOGIN_PASS;
271 if (!strncmp(msg, "334 ", 4))
272 base64_encode(buf, session->pass, strlen(session->pass));
274 /* Server rejects AUTH */
275 g_snprintf(buf, sizeof(buf), "*");
277 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
280 log_print(LOG_PROTOCOL, "ESMTP> [PASSWORD]\n");
285 static gint smtp_ehlo(SMTPSession *session)
287 gchar buf[MSGBUFSIZE];
289 session->state = SMTP_EHLO;
291 session->avail_auth_type = 0;
293 g_snprintf(buf, sizeof(buf), "EHLO %s",
294 session->hostname ? session->hostname : get_domain_name());
295 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
297 log_print(LOG_PROTOCOL, "ESMTP> %s\n", buf);
302 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
304 if (strncmp(msg, "250", 3) == 0) {
305 const gchar *p = msg;
307 if (*p == '-' || *p == ' ') p++;
308 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
310 if (strcasestr(p, "PLAIN"))
311 session->avail_auth_type |= SMTPAUTH_PLAIN;
312 if (strcasestr(p, "LOGIN"))
313 session->avail_auth_type |= SMTPAUTH_LOGIN;
314 if (strcasestr(p, "CRAM-MD5"))
315 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
316 if (strcasestr(p, "DIGEST-MD5"))
317 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
319 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
321 session->max_message_size = atoi(p);
322 session->esmtp_flags |= ESMTP_SIZE;
324 if (g_ascii_strncasecmp(p, "STARTTLS", 8) == 0) {
326 session->avail_auth_type |= SMTPAUTH_TLS_AVAILABLE;
329 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
330 (msg[3] == ' ' || msg[3] == '\0'))
332 else if (msg[0] == '5' && msg[1] == '0' &&
333 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
340 static gint smtp_starttls(SMTPSession *session)
342 session->state = SMTP_STARTTLS;
344 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS") < 0)
346 log_print(LOG_PROTOCOL, "ESMTP> STARTTLS\n");
352 static gint smtp_auth_cram_md5(SMTPSession *session)
354 session->state = SMTP_AUTH;
355 session->auth_type = SMTPAUTH_CRAM_MD5;
357 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5") < 0)
359 log_print(LOG_PROTOCOL, "ESMTP> AUTH CRAM-MD5\n");
364 static gint smtp_auth_plain(SMTPSession *session)
366 gchar buf[MSGBUFSIZE];
372 int b64len = (1 + strlen(session->user) + 1 + strlen(session->pass) + 1);
373 gchar *b64buf = g_malloc(b64len);
375 /* use the char *ptr to walk the base64 string with embedded \0 */
379 session->state = SMTP_AUTH_PLAIN;
380 session->auth_type = SMTPAUTH_PLAIN;
382 memset(buf, 0, sizeof buf);
385 * have to construct the string bit by bit. sprintf can't do it in one.
386 * first field is null, so string is \0<user>\0<password>
391 g_snprintf (a, b64len - 1, "%s", session->user);
393 b64cnt = strlen(session->user)+1;
396 g_snprintf (a, b64len - b64cnt - 1, "%s", session->pass);
397 b64cnt += strlen(session->pass) + 1;
400 * reuse the char *ptr to offset into the textbuf to meld
401 * the plaintext ESMTP message and the base64 string value
403 strcpy(buf, "AUTH PLAIN ");
404 a = buf + strlen(buf);
405 base64_encode(a, b64buf, b64cnt);
407 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
410 log_print(LOG_PROTOCOL, "ESMTP> [AUTH PLAIN]\n");
417 static gint smtp_auth_login(SMTPSession *session)
419 session->state = SMTP_AUTH;
420 session->auth_type = SMTPAUTH_LOGIN;
422 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN") < 0)
424 log_print(LOG_PROTOCOL, "ESMTP> AUTH LOGIN\n");
429 static gint smtp_helo(SMTPSession *session)
431 gchar buf[MSGBUFSIZE];
433 session->state = SMTP_HELO;
435 g_snprintf(buf, sizeof(buf), "HELO %s",
436 session->hostname ? session->hostname : get_domain_name());
437 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
439 log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
444 static gint smtp_rcpt(SMTPSession *session)
446 gchar buf[MSGBUFSIZE];
449 cm_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
451 session->state = SMTP_RCPT;
453 to = (gchar *)session->cur_to->data;
456 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
458 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
459 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
461 log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
463 session->cur_to = session->cur_to->next;
468 static gint smtp_data(SMTPSession *session)
470 session->state = SMTP_DATA;
472 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA") < 0)
474 log_print(LOG_PROTOCOL, "SMTP> DATA\n");
479 static gint smtp_send_data(SMTPSession *session)
481 session->state = SMTP_SEND_DATA;
483 session_send_data(SESSION(session), session->send_data,
484 session->send_data_len);
489 static gint smtp_make_ready(SMTPSession *session)
491 session->state = SMTP_MAIL_SENT_OK;
496 gint smtp_quit(SMTPSession *session)
498 session->state = SMTP_QUIT;
500 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
501 log_print(LOG_PROTOCOL, "SMTP> QUIT\n");
506 static gint smtp_eom(SMTPSession *session)
508 session->state = SMTP_EOM;
510 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".") < 0)
512 log_print(LOG_PROTOCOL, "SMTP> . (EOM)\n");
517 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
519 SMTPSession *smtp_session = SMTP_SESSION(session);
520 gboolean cont = FALSE;
523 if (strlen(msg) < 4) {
524 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
528 switch (smtp_session->state) {
532 case SMTP_AUTH_PLAIN:
533 case SMTP_AUTH_LOGIN_USER:
534 case SMTP_AUTH_LOGIN_PASS:
535 case SMTP_AUTH_CRAM_MD5:
536 log_print(LOG_PROTOCOL, "ESMTP< %s\n", msg);
539 log_print(LOG_PROTOCOL, "SMTP< %s\n", msg);
543 if (msg[0] == '5' && msg[1] == '0' &&
544 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
545 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
546 smtp_session->state = SMTP_ERROR;
547 smtp_session->error_val = SM_ERROR;
548 g_free(smtp_session->error_msg);
549 smtp_session->error_msg = g_strdup(msg);
553 if (!strncmp(msg, "535", 3)) {
554 log_warning(LOG_PROTOCOL, _("error occurred on authentication\n"));
555 smtp_session->state = SMTP_ERROR;
556 smtp_session->error_val = SM_AUTHFAIL;
557 g_free(smtp_session->error_msg);
558 smtp_session->error_msg = g_strdup(msg);
562 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
563 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
564 smtp_session->state = SMTP_ERROR;
565 smtp_session->error_val = SM_ERROR;
566 g_free(smtp_session->error_msg);
567 smtp_session->error_msg = g_strdup(msg);
573 else if (msg[3] != ' ' && msg[3] != '\0') {
574 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
575 smtp_session->state = SMTP_ERROR;
576 smtp_session->error_val = SM_UNRECOVERABLE;
580 /* ignore all multiline responses except for EHLO */
581 if (cont && smtp_session->state != SMTP_EHLO)
582 return session_recv_msg(session);
584 switch (smtp_session->state) {
586 if (strstr(msg, "ESMTP"))
587 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));