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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <glib/gi18n.h>
35 static void smtp_session_destroy(Session *session);
37 static gint smtp_from(SMTPSession *session);
39 static gint smtp_auth(SMTPSession *session);
40 static gint smtp_starttls(SMTPSession *session);
41 static gint smtp_auth_cram_md5(SMTPSession *session);
42 static gint smtp_auth_login(SMTPSession *session);
43 static gint smtp_auth_plain(SMTPSession *session);
45 static gint smtp_ehlo(SMTPSession *session);
46 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
48 static gint smtp_helo(SMTPSession *session);
49 static gint smtp_rcpt(SMTPSession *session);
50 static gint smtp_data(SMTPSession *session);
51 static gint smtp_send_data(SMTPSession *session);
52 /* static gint smtp_rset(SMTPSession *session); */
53 static gint smtp_quit(SMTPSession *session);
54 static gint smtp_eom(SMTPSession *session);
56 static gint smtp_session_recv_msg(Session *session, const gchar *msg);
57 static gint smtp_session_send_data_finished(Session *session, guint len);
60 Session *smtp_session_new(void)
64 session = g_new0(SMTPSession, 1);
66 session_init(SESSION(session));
68 SESSION(session)->type = SESSION_SMTP;
70 SESSION(session)->recv_msg = smtp_session_recv_msg;
72 SESSION(session)->recv_data_finished = NULL;
73 SESSION(session)->send_data_finished = smtp_session_send_data_finished;
75 SESSION(session)->destroy = smtp_session_destroy;
77 session->state = SMTP_READY;
80 session->tls_init_done = FALSE;
83 session->hostname = NULL;
88 session->to_list = NULL;
89 session->cur_to = NULL;
91 session->send_data = NULL;
92 session->send_data_len = 0;
94 session->max_message_size = -1;
96 session->avail_auth_type = 0;
97 session->forced_auth_type = 0;
98 session->auth_type = 0;
100 session->error_val = SM_OK;
101 session->error_msg = NULL;
103 return SESSION(session);
106 static void smtp_session_destroy(Session *session)
108 SMTPSession *smtp_session = SMTP_SESSION(session);
110 g_free(smtp_session->hostname);
111 g_free(smtp_session->user);
112 g_free(smtp_session->pass);
113 g_free(smtp_session->from);
115 g_free(smtp_session->send_data);
117 g_free(smtp_session->error_msg);
120 static gint smtp_from(SMTPSession *session)
122 gchar buf[MSGBUFSIZE];
123 gchar *mail_size = NULL;
125 g_return_val_if_fail(session->from != NULL, SM_ERROR);
127 session->state = SMTP_FROM;
129 if (session->is_esmtp)
130 mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
132 mail_size = g_strdup("");
135 if (strchr(session->from, '<'))
136 g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
139 g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
144 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
145 log_print("%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
150 static gint smtp_auth(SMTPSession *session)
153 g_return_val_if_fail(session->user != NULL, SM_ERROR);
155 session->state = SMTP_AUTH;
157 if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
158 (session->forced_auth_type == 0 &&
159 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
160 smtp_auth_cram_md5(session);
161 else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
162 (session->forced_auth_type == 0 &&
163 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
164 smtp_auth_login(session);
165 else if (session->forced_auth_type == SMTPAUTH_PLAIN ||
166 (session->forced_auth_type == 0 &&
167 (session->avail_auth_type & SMTPAUTH_PLAIN) != 0))
168 smtp_auth_plain(session);
170 log_warning(_("SMTP AUTH not available\n"));
177 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
179 gchar buf[MSGBUFSIZE];
181 switch (session->auth_type) {
183 session->state = SMTP_AUTH_LOGIN_USER;
185 if (!strncmp(msg, "334 ", 4)) {
186 base64_encode(buf, session->user, strlen(session->user));
188 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
190 log_print("ESMTP> [USERID]\n");
192 /* Server rejects AUTH */
193 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
195 log_print("ESMTP> *\n");
198 case SMTPAUTH_CRAM_MD5:
199 session->state = SMTP_AUTH_CRAM_MD5;
201 if (!strncmp(msg, "334 ", 4)) {
206 guchar hexdigest[33];
208 challenge = g_malloc(strlen(msg + 4) + 1);
209 challengelen = base64_decode(challenge, msg + 4, -1);
210 challenge[challengelen] = '\0';
211 log_print("ESMTP< [Decoded: %s]\n", challenge);
213 g_snprintf(buf, sizeof(buf), "%s", session->pass);
214 md5_hex_hmac(hexdigest, challenge, challengelen,
215 buf, strlen(session->pass));
218 response = g_strdup_printf
219 ("%s %s", session->user, hexdigest);
220 log_print("ESMTP> [Encoded: %s]\n", response);
222 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
223 base64_encode(response64, response, strlen(response));
226 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
228 log_print("ESMTP> %s\n", response64);
231 /* Server rejects AUTH */
232 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
234 log_print("ESMTP> *\n");
237 case SMTPAUTH_DIGEST_MD5:
239 /* stop smtp_auth when no correct authtype */
240 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*");
241 log_print("ESMTP> *\n");
248 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
250 gchar buf[MSGBUFSIZE];
252 session->state = SMTP_AUTH_LOGIN_PASS;
254 if (!strncmp(msg, "334 ", 4))
255 base64_encode(buf, session->pass, strlen(session->pass));
257 /* Server rejects AUTH */
258 g_snprintf(buf, sizeof(buf), "*");
260 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
261 log_print("ESMTP> [PASSWORD]\n");
266 static gint smtp_ehlo(SMTPSession *session)
268 gchar buf[MSGBUFSIZE];
270 session->state = SMTP_EHLO;
272 session->avail_auth_type = 0;
274 g_snprintf(buf, sizeof(buf), "EHLO %s",
275 session->hostname ? session->hostname : get_domain_name());
276 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
277 log_print("ESMTP> %s\n", buf);
282 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
284 if (strncmp(msg, "250", 3) == 0) {
285 const gchar *p = msg;
287 if (*p == '-' || *p == ' ') p++;
288 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
290 if (strcasestr(p, "PLAIN"))
291 session->avail_auth_type |= SMTPAUTH_PLAIN;
292 if (strcasestr(p, "LOGIN"))
293 session->avail_auth_type |= SMTPAUTH_LOGIN;
294 if (strcasestr(p, "CRAM-MD5"))
295 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
296 if (strcasestr(p, "DIGEST-MD5"))
297 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
299 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
301 session->max_message_size = atoi(p);
304 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
305 (msg[3] == ' ' || msg[3] == '\0'))
307 else if (msg[0] == '5' && msg[1] == '0' &&
308 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
314 static gint smtp_starttls(SMTPSession *session)
316 session->state = SMTP_STARTTLS;
318 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS");
319 log_print("ESMTP> STARTTLS\n");
324 static gint smtp_auth_cram_md5(SMTPSession *session)
326 session->state = SMTP_AUTH;
327 session->auth_type = SMTPAUTH_CRAM_MD5;
329 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5");
330 log_print("ESMTP> AUTH CRAM-MD5\n");
335 static gint smtp_auth_plain(SMTPSession *session)
337 gchar buf[MSGBUFSIZE];
343 int b64len = (1 + strlen(session->user) + 1 + strlen(session->pass) + 1);
344 gchar *b64buf = g_malloc(b64len);
346 /* use the char *ptr to walk the base64 string with embedded \0 */
350 session->state = SMTP_AUTH_PLAIN;
351 session->auth_type = SMTPAUTH_PLAIN;
353 memset(buf, 0, sizeof buf);
356 * have to construct the string bit by bit. sprintf can't do it in one.
357 * first field is null, so string is \0<user>\0<password>
362 g_snprintf (a, b64len - 1, "%s", session->user);
364 b64cnt = strlen(session->user)+1;
367 g_snprintf (a, b64len - b64cnt - 1, "%s", session->pass);
368 b64cnt += strlen(session->pass) + 1;
371 * reuse the char *ptr to offset into the textbuf to meld
372 * the plaintext ESMTP message and the base64 string value
374 strcpy(buf, "AUTH PLAIN ");
375 a = buf + strlen(buf);
376 base64_encode(a, b64buf, b64cnt);
378 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
380 log_print("ESMTP> [AUTH PLAIN]\n");
387 static gint smtp_auth_login(SMTPSession *session)
389 session->state = SMTP_AUTH;
390 session->auth_type = SMTPAUTH_LOGIN;
392 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN");
393 log_print("ESMTP> AUTH LOGIN\n");
398 static gint smtp_helo(SMTPSession *session)
400 gchar buf[MSGBUFSIZE];
402 session->state = SMTP_HELO;
404 g_snprintf(buf, sizeof(buf), "HELO %s",
405 session->hostname ? session->hostname : get_domain_name());
406 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
407 log_print("SMTP> %s\n", buf);
412 static gint smtp_rcpt(SMTPSession *session)
414 gchar buf[MSGBUFSIZE];
417 g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
419 session->state = SMTP_RCPT;
421 to = (gchar *)session->cur_to->data;
424 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
426 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
427 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
428 log_print("SMTP> %s\n", buf);
430 session->cur_to = session->cur_to->next;
435 static gint smtp_data(SMTPSession *session)
437 session->state = SMTP_DATA;
439 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA");
440 log_print("SMTP> DATA\n");
445 static gint smtp_send_data(SMTPSession *session)
447 session->state = SMTP_SEND_DATA;
449 session_send_data(SESSION(session), session->send_data,
450 session->send_data_len);
456 static gint smtp_rset(SMTPSession *session)
458 session->state = SMTP_RSET;
460 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "RSET");
461 log_print("SMTP> RSET\n");
467 static gint smtp_quit(SMTPSession *session)
469 session->state = SMTP_QUIT;
471 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
472 log_print("SMTP> QUIT\n");
477 static gint smtp_eom(SMTPSession *session)
479 session->state = SMTP_EOM;
481 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".");
482 log_print("SMTP> . (EOM)\n");
487 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
489 SMTPSession *smtp_session = SMTP_SESSION(session);
490 gboolean cont = FALSE;
492 if (strlen(msg) < 4) {
493 log_warning(_("bad SMTP response\n"));
497 switch (smtp_session->state) {
501 case SMTP_AUTH_PLAIN:
502 case SMTP_AUTH_LOGIN_USER:
503 case SMTP_AUTH_LOGIN_PASS:
504 case SMTP_AUTH_CRAM_MD5:
505 log_print("ESMTP< %s\n", msg);
508 log_print("SMTP< %s\n", msg);
512 if (msg[0] == '5' && msg[1] == '0' &&
513 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
514 log_warning(_("error occurred on SMTP session\n"));
515 smtp_session->state = SMTP_ERROR;
516 smtp_session->error_val = SM_ERROR;
517 g_free(smtp_session->error_msg);
518 smtp_session->error_msg = g_strdup(msg);
522 if (!strncmp(msg, "535", 3)) {
523 log_warning(_("error occurred on authentication\n"));
524 smtp_session->state = SMTP_ERROR;
525 smtp_session->error_val = SM_AUTHFAIL;
526 g_free(smtp_session->error_msg);
527 smtp_session->error_msg = g_strdup(msg);
531 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
532 log_warning(_("error occurred on SMTP session\n"));
533 smtp_session->state = SMTP_ERROR;
534 smtp_session->error_val = SM_ERROR;
535 g_free(smtp_session->error_msg);
536 smtp_session->error_msg = g_strdup(msg);
542 else if (msg[3] != ' ' && msg[3] != '\0') {
543 log_warning(_("bad SMTP response\n"));
544 smtp_session->state = SMTP_ERROR;
545 smtp_session->error_val = SM_UNRECOVERABLE;
549 /* ignore all multiline responses except for EHLO */
550 if (cont && smtp_session->state != SMTP_EHLO)
551 return session_recv_msg(session);
553 switch (smtp_session->state) {
555 if (strstr(msg, "ESMTP"))
556 smtp_session->is_esmtp = TRUE;
559 if (smtp_session->user || session->ssl_type != SSL_NONE ||
560 smtp_session->is_esmtp)
562 if (smtp_session->user || smtp_session->is_esmtp)
564 smtp_ehlo(smtp_session);
566 smtp_helo(smtp_session);
569 smtp_from(smtp_session);
572 smtp_ehlo_recv(smtp_session, msg);
575 if (smtp_session->max_message_size > 0
576 && smtp_session->max_message_size <
577 smtp_session->send_data_len) {
578 log_warning(_("Message is too big "
579 "(Maximum size is %s)\n"),
581 (off_t)(smtp_session->max_message_size)));
582 smtp_session->state = SMTP_ERROR;
583 smtp_session->error_val = SM_ERROR;
587 if (session->ssl_type == SSL_STARTTLS &&
588 smtp_session->tls_init_done == FALSE) {
589 smtp_starttls(smtp_session);
593 if (smtp_session->user) {
594 if (smtp_auth(smtp_session) != SM_OK)
595 smtp_from(smtp_session);
597 smtp_from(smtp_session);
601 if (session_start_tls(session) < 0) {
602 log_warning(_("can't start TLS session\n"));
603 smtp_session->state = SMTP_ERROR;
604 smtp_session->error_val = SM_ERROR;
607 smtp_session->tls_init_done = TRUE;
608 smtp_ehlo(smtp_session);
612 smtp_auth_recv(smtp_session, msg);
614 case SMTP_AUTH_LOGIN_USER:
615 smtp_auth_login_user_recv(smtp_session, msg);
617 case SMTP_AUTH_PLAIN:
618 case SMTP_AUTH_LOGIN_PASS:
619 case SMTP_AUTH_CRAM_MD5:
620 smtp_from(smtp_session);
623 if (smtp_session->cur_to)
624 smtp_rcpt(smtp_session);
627 if (smtp_session->cur_to)
628 smtp_rcpt(smtp_session);
630 smtp_data(smtp_session);
633 smtp_send_data(smtp_session);
636 smtp_quit(smtp_session);
639 session_disconnect(session);
643 log_warning(_("error occurred on SMTP session\n"));
644 smtp_session->error_val = SM_ERROR;
649 return session_recv_msg(session);
654 static gint smtp_session_send_data_finished(Session *session, guint len)
656 smtp_eom(SMTP_SESSION(session));