2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <glib/gi18n.h>
40 static void smtp_session_destroy(Session *session);
42 static gint smtp_auth(SMTPSession *session);
43 static gint smtp_starttls(SMTPSession *session);
44 static gint smtp_auth_cram_md5(SMTPSession *session);
45 static gint smtp_auth_login(SMTPSession *session);
46 static gint smtp_auth_plain(SMTPSession *session);
48 static gint smtp_ehlo(SMTPSession *session);
49 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
51 static gint smtp_helo(SMTPSession *session);
52 static gint smtp_rcpt(SMTPSession *session);
53 static gint smtp_data(SMTPSession *session);
54 static gint smtp_send_data(SMTPSession *session);
55 static gint smtp_make_ready(SMTPSession *session);
56 static gint smtp_eom(SMTPSession *session);
58 static gint smtp_session_recv_msg(Session *session, const gchar *msg);
59 static gint smtp_session_send_data_finished(Session *session, guint len);
62 Session *smtp_session_new(void)
66 session = g_new0(SMTPSession, 1);
68 session_init(SESSION(session));
70 SESSION(session)->type = SESSION_SMTP;
72 SESSION(session)->recv_msg = smtp_session_recv_msg;
74 SESSION(session)->recv_data_finished = NULL;
75 SESSION(session)->send_data_finished = smtp_session_send_data_finished;
77 SESSION(session)->destroy = smtp_session_destroy;
79 session->state = SMTP_READY;
82 session->tls_init_done = FALSE;
85 session->hostname = NULL;
90 session->to_list = NULL;
91 session->cur_to = NULL;
93 session->send_data = NULL;
94 session->send_data_len = 0;
96 session->max_message_size = -1;
98 session->avail_auth_type = 0;
99 session->forced_auth_type = 0;
100 session->auth_type = 0;
101 session->esmtp_flags = 0;
103 session->error_val = SM_OK;
104 session->error_msg = NULL;
106 return SESSION(session);
109 static void smtp_session_destroy(Session *session)
111 SMTPSession *smtp_session = SMTP_SESSION(session);
113 g_free(smtp_session->hostname);
114 g_free(smtp_session->user);
115 g_free(smtp_session->pass);
116 g_free(smtp_session->from);
118 g_free(smtp_session->send_data);
120 g_free(smtp_session->error_msg);
123 gint smtp_from(SMTPSession *session)
125 gchar buf[MSGBUFSIZE];
126 gchar *mail_size = NULL;
128 g_return_val_if_fail(session->from != NULL, SM_ERROR);
130 session->state = SMTP_FROM;
132 if (session->is_esmtp && (session->esmtp_flags & ESMTP_SIZE)!=0)
133 mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
135 mail_size = g_strdup("");
138 if (strchr(session->from, '<'))
139 g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
142 g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
147 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
149 log_print("%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
154 static gint smtp_auth(SMTPSession *session)
157 g_return_val_if_fail(session->user != NULL, SM_ERROR);
159 session->state = SMTP_AUTH;
161 if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
162 (session->forced_auth_type == 0 &&
163 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
164 smtp_auth_cram_md5(session);
165 else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
166 (session->forced_auth_type == 0 &&
167 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
168 smtp_auth_login(session);
169 else if (session->forced_auth_type == SMTPAUTH_PLAIN ||
170 (session->forced_auth_type == 0 &&
171 (session->avail_auth_type & SMTPAUTH_PLAIN) != 0))
172 smtp_auth_plain(session);
174 log_warning(_("SMTP AUTH not available\n"));
181 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
183 gchar buf[MSGBUFSIZE];
185 switch (session->auth_type) {
187 session->state = SMTP_AUTH_LOGIN_USER;
189 if (!strncmp(msg, "334 ", 4)) {
190 base64_encode(buf, session->user, strlen(session->user));
192 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
195 log_print("ESMTP> [USERID]\n");
197 /* Server rejects AUTH */
198 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
201 log_print("ESMTP> *\n");
204 case SMTPAUTH_CRAM_MD5:
205 session->state = SMTP_AUTH_CRAM_MD5;
207 if (!strncmp(msg, "334 ", 4)) {
212 guchar hexdigest[33];
214 challenge = g_malloc(strlen(msg + 4) + 1);
215 challengelen = base64_decode(challenge, msg + 4, -1);
216 challenge[challengelen] = '\0';
217 log_print("ESMTP< [Decoded: %s]\n", challenge);
219 g_snprintf(buf, sizeof(buf), "%s", session->pass);
220 md5_hex_hmac(hexdigest, challenge, challengelen,
221 buf, strlen(session->pass));
224 response = g_strdup_printf
225 ("%s %s", session->user, hexdigest);
226 log_print("ESMTP> [Encoded: %s]\n", response);
228 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
229 base64_encode(response64, response, strlen(response));
232 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
235 log_print("ESMTP> %s\n", response64);
238 /* Server rejects AUTH */
239 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
242 log_print("ESMTP> *\n");
245 case SMTPAUTH_DIGEST_MD5:
247 /* stop smtp_auth when no correct authtype */
248 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*") < 0)
250 log_print("ESMTP> *\n");
257 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
259 gchar buf[MSGBUFSIZE];
261 session->state = SMTP_AUTH_LOGIN_PASS;
263 if (!strncmp(msg, "334 ", 4))
264 base64_encode(buf, session->pass, strlen(session->pass));
266 /* Server rejects AUTH */
267 g_snprintf(buf, sizeof(buf), "*");
269 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
272 log_print("ESMTP> [PASSWORD]\n");
277 static gint smtp_ehlo(SMTPSession *session)
279 gchar buf[MSGBUFSIZE];
281 session->state = SMTP_EHLO;
283 session->avail_auth_type = 0;
285 g_snprintf(buf, sizeof(buf), "EHLO %s",
286 session->hostname ? session->hostname : get_domain_name());
287 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
289 log_print("ESMTP> %s\n", buf);
294 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
296 if (strncmp(msg, "250", 3) == 0) {
297 const gchar *p = msg;
299 if (*p == '-' || *p == ' ') p++;
300 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
302 if (strcasestr(p, "PLAIN"))
303 session->avail_auth_type |= SMTPAUTH_PLAIN;
304 if (strcasestr(p, "LOGIN"))
305 session->avail_auth_type |= SMTPAUTH_LOGIN;
306 if (strcasestr(p, "CRAM-MD5"))
307 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
308 if (strcasestr(p, "DIGEST-MD5"))
309 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
311 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
313 session->max_message_size = atoi(p);
314 session->esmtp_flags |= ESMTP_SIZE;
316 if (g_ascii_strncasecmp(p, "STARTTLS", 8) == 0) {
318 session->avail_auth_type |= SMTPAUTH_TLS_AVAILABLE;
321 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
322 (msg[3] == ' ' || msg[3] == '\0'))
324 else if (msg[0] == '5' && msg[1] == '0' &&
325 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
331 static gint smtp_starttls(SMTPSession *session)
333 session->state = SMTP_STARTTLS;
335 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS") < 0)
337 log_print("ESMTP> STARTTLS\n");
342 static gint smtp_auth_cram_md5(SMTPSession *session)
344 session->state = SMTP_AUTH;
345 session->auth_type = SMTPAUTH_CRAM_MD5;
347 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5") < 0)
349 log_print("ESMTP> AUTH CRAM-MD5\n");
354 static gint smtp_auth_plain(SMTPSession *session)
356 gchar buf[MSGBUFSIZE];
362 int b64len = (1 + strlen(session->user) + 1 + strlen(session->pass) + 1);
363 gchar *b64buf = g_malloc(b64len);
365 /* use the char *ptr to walk the base64 string with embedded \0 */
369 session->state = SMTP_AUTH_PLAIN;
370 session->auth_type = SMTPAUTH_PLAIN;
372 memset(buf, 0, sizeof buf);
375 * have to construct the string bit by bit. sprintf can't do it in one.
376 * first field is null, so string is \0<user>\0<password>
381 g_snprintf (a, b64len - 1, "%s", session->user);
383 b64cnt = strlen(session->user)+1;
386 g_snprintf (a, b64len - b64cnt - 1, "%s", session->pass);
387 b64cnt += strlen(session->pass) + 1;
390 * reuse the char *ptr to offset into the textbuf to meld
391 * the plaintext ESMTP message and the base64 string value
393 strcpy(buf, "AUTH PLAIN ");
394 a = buf + strlen(buf);
395 base64_encode(a, b64buf, b64cnt);
397 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
400 log_print("ESMTP> [AUTH PLAIN]\n");
407 static gint smtp_auth_login(SMTPSession *session)
409 session->state = SMTP_AUTH;
410 session->auth_type = SMTPAUTH_LOGIN;
412 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN") < 0)
414 log_print("ESMTP> AUTH LOGIN\n");
419 static gint smtp_helo(SMTPSession *session)
421 gchar buf[MSGBUFSIZE];
423 session->state = SMTP_HELO;
425 g_snprintf(buf, sizeof(buf), "HELO %s",
426 session->hostname ? session->hostname : get_domain_name());
427 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
429 log_print("SMTP> %s\n", buf);
434 static gint smtp_rcpt(SMTPSession *session)
436 gchar buf[MSGBUFSIZE];
439 g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
441 session->state = SMTP_RCPT;
443 to = (gchar *)session->cur_to->data;
446 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
448 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
449 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
451 log_print("SMTP> %s\n", buf);
453 session->cur_to = session->cur_to->next;
458 static gint smtp_data(SMTPSession *session)
460 session->state = SMTP_DATA;
462 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA") < 0)
464 log_print("SMTP> DATA\n");
469 static gint smtp_send_data(SMTPSession *session)
471 session->state = SMTP_SEND_DATA;
473 session_send_data(SESSION(session), session->send_data,
474 session->send_data_len);
479 static gint smtp_make_ready(SMTPSession *session)
481 session->state = SMTP_MAIL_SENT_OK;
486 gint smtp_quit(SMTPSession *session)
488 session->state = SMTP_QUIT;
490 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
491 log_print("SMTP> QUIT\n");
496 static gint smtp_eom(SMTPSession *session)
498 session->state = SMTP_EOM;
500 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".") < 0)
502 log_print("SMTP> . (EOM)\n");
507 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
509 SMTPSession *smtp_session = SMTP_SESSION(session);
510 gboolean cont = FALSE;
513 if (strlen(msg) < 4) {
514 log_warning(_("bad SMTP response\n"));
518 switch (smtp_session->state) {
522 case SMTP_AUTH_PLAIN:
523 case SMTP_AUTH_LOGIN_USER:
524 case SMTP_AUTH_LOGIN_PASS:
525 case SMTP_AUTH_CRAM_MD5:
526 log_print("ESMTP< %s\n", msg);
529 log_print("SMTP< %s\n", msg);
533 if (msg[0] == '5' && msg[1] == '0' &&
534 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
535 log_warning(_("error occurred on SMTP session\n"));
536 smtp_session->state = SMTP_ERROR;
537 smtp_session->error_val = SM_ERROR;
538 g_free(smtp_session->error_msg);
539 smtp_session->error_msg = g_strdup(msg);
543 if (!strncmp(msg, "535", 3)) {
544 log_warning(_("error occurred on authentication\n"));
545 smtp_session->state = SMTP_ERROR;
546 smtp_session->error_val = SM_AUTHFAIL;
547 g_free(smtp_session->error_msg);
548 smtp_session->error_msg = g_strdup(msg);
552 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
553 log_warning(_("error occurred on SMTP session\n"));
554 smtp_session->state = SMTP_ERROR;
555 smtp_session->error_val = SM_ERROR;
556 g_free(smtp_session->error_msg);
557 smtp_session->error_msg = g_strdup(msg);
563 else if (msg[3] != ' ' && msg[3] != '\0') {
564 log_warning(_("bad SMTP response\n"));
565 smtp_session->state = SMTP_ERROR;
566 smtp_session->error_val = SM_UNRECOVERABLE;
570 /* ignore all multiline responses except for EHLO */
571 if (cont && smtp_session->state != SMTP_EHLO)
572 return session_recv_msg(session);
574 switch (smtp_session->state) {
576 if (strstr(msg, "ESMTP"))
577 smtp_session->is_esmtp = TRUE;
580 if (smtp_session->user || session->ssl_type != SSL_NONE ||
581 smtp_session->is_esmtp)
583 if (smtp_session->user || smtp_session->is_esmtp)
585 ret = smtp_ehlo(smtp_session);
587 ret = smtp_helo(smtp_session);
590 ret = smtp_from(smtp_session);
593 ret = smtp_ehlo_recv(smtp_session, msg);
596 if (smtp_session->max_message_size > 0
597 && smtp_session->max_message_size <
598 smtp_session->send_data_len) {
599 log_warning(_("Message is too big "
600 "(Maximum size is %s)\n"),
602 (off_t)(smtp_session->max_message_size)));
603 smtp_session->state = SMTP_ERROR;
604 smtp_session->error_val = SM_ERROR;
608 if (session->ssl_type == SSL_STARTTLS &&
609 smtp_session->tls_init_done == FALSE) {
610 ret = smtp_starttls(smtp_session);
614 if (smtp_session->user) {
615 if (smtp_auth(smtp_session) != SM_OK) {
617 if (session->ssl_type == SSL_NONE
618 && smtp_session->tls_init_done == FALSE
619 && (smtp_session->avail_auth_type & SMTPAUTH_TLS_AVAILABLE))
620 ret = smtp_starttls(smtp_session);
623 ret = smtp_from(smtp_session);
626 ret = smtp_from(smtp_session);
630 if (session_start_tls(session) < 0) {
631 log_warning(_("can't start TLS session\n"));
632 smtp_session->state = SMTP_ERROR;
633 smtp_session->error_val = SM_ERROR;
636 smtp_session->tls_init_done = TRUE;
637 ret = smtp_ehlo(smtp_session);
641 ret = smtp_auth_recv(smtp_session, msg);
643 case SMTP_AUTH_LOGIN_USER:
644 ret = smtp_auth_login_user_recv(smtp_session, msg);
646 case SMTP_AUTH_PLAIN:
647 case SMTP_AUTH_LOGIN_PASS:
648 case SMTP_AUTH_CRAM_MD5:
649 ret = smtp_from(smtp_session);
652 if (smtp_session->cur_to)
653 ret = smtp_rcpt(smtp_session);
656 if (smtp_session->cur_to)
657 ret = smtp_rcpt(smtp_session);
659 ret = smtp_data(smtp_session);
662 ret = smtp_send_data(smtp_session);
665 smtp_make_ready(smtp_session);
668 session_disconnect(session);
672 log_warning(_("error occurred on SMTP session\n"));
673 smtp_session->error_val = SM_ERROR;
677 if (cont && ret == SM_OK)
678 return session_recv_msg(session);
681 smtp_session->error_val = SM_ERROR;
683 return ret == SM_OK ? 0 : -1;
686 static gint smtp_session_send_data_finished(Session *session, guint len)
688 return smtp_eom(SMTP_SESSION(session));