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_auth(SMTPSession *session);
38 static gint smtp_starttls(SMTPSession *session);
39 static gint smtp_auth_cram_md5(SMTPSession *session);
40 static gint smtp_auth_login(SMTPSession *session);
41 static gint smtp_auth_plain(SMTPSession *session);
43 static gint smtp_ehlo(SMTPSession *session);
44 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
46 static gint smtp_helo(SMTPSession *session);
47 static gint smtp_rcpt(SMTPSession *session);
48 static gint smtp_data(SMTPSession *session);
49 static gint smtp_send_data(SMTPSession *session);
50 static gint smtp_make_ready(SMTPSession *session);
51 static gint smtp_eom(SMTPSession *session);
53 static gint smtp_session_recv_msg(Session *session, const gchar *msg);
54 static gint smtp_session_send_data_finished(Session *session, guint len);
57 Session *smtp_session_new(void)
61 session = g_new0(SMTPSession, 1);
63 session_init(SESSION(session));
65 SESSION(session)->type = SESSION_SMTP;
67 SESSION(session)->recv_msg = smtp_session_recv_msg;
69 SESSION(session)->recv_data_finished = NULL;
70 SESSION(session)->send_data_finished = smtp_session_send_data_finished;
72 SESSION(session)->destroy = smtp_session_destroy;
74 session->state = SMTP_READY;
77 session->tls_init_done = FALSE;
80 session->hostname = NULL;
85 session->to_list = NULL;
86 session->cur_to = NULL;
88 session->send_data = NULL;
89 session->send_data_len = 0;
91 session->max_message_size = -1;
93 session->avail_auth_type = 0;
94 session->forced_auth_type = 0;
95 session->auth_type = 0;
96 session->esmtp_flags = 0;
98 session->error_val = SM_OK;
99 session->error_msg = NULL;
101 return SESSION(session);
104 static void smtp_session_destroy(Session *session)
106 SMTPSession *smtp_session = SMTP_SESSION(session);
108 g_free(smtp_session->hostname);
109 g_free(smtp_session->user);
110 g_free(smtp_session->pass);
111 g_free(smtp_session->from);
113 g_free(smtp_session->send_data);
115 g_free(smtp_session->error_msg);
118 gint smtp_from(SMTPSession *session)
120 gchar buf[MSGBUFSIZE];
121 gchar *mail_size = NULL;
123 g_return_val_if_fail(session->from != NULL, SM_ERROR);
125 session->state = SMTP_FROM;
127 if (session->is_esmtp && (session->esmtp_flags & ESMTP_SIZE)!=0)
128 mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
130 mail_size = g_strdup("");
133 if (strchr(session->from, '<'))
134 g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
137 g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
142 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
143 log_print("%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
148 static gint smtp_auth(SMTPSession *session)
151 g_return_val_if_fail(session->user != NULL, SM_ERROR);
153 session->state = SMTP_AUTH;
155 if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
156 (session->forced_auth_type == 0 &&
157 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
158 smtp_auth_cram_md5(session);
159 else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
160 (session->forced_auth_type == 0 &&
161 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
162 smtp_auth_login(session);
163 else if (session->forced_auth_type == SMTPAUTH_PLAIN ||
164 (session->forced_auth_type == 0 &&
165 (session->avail_auth_type & SMTPAUTH_PLAIN) != 0))
166 smtp_auth_plain(session);
168 log_warning(_("SMTP AUTH not available\n"));
175 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
177 gchar buf[MSGBUFSIZE];
179 switch (session->auth_type) {
181 session->state = SMTP_AUTH_LOGIN_USER;
183 if (!strncmp(msg, "334 ", 4)) {
184 base64_encode(buf, session->user, strlen(session->user));
186 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
188 log_print("ESMTP> [USERID]\n");
190 /* Server rejects AUTH */
191 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
193 log_print("ESMTP> *\n");
196 case SMTPAUTH_CRAM_MD5:
197 session->state = SMTP_AUTH_CRAM_MD5;
199 if (!strncmp(msg, "334 ", 4)) {
204 guchar hexdigest[33];
206 challenge = g_malloc(strlen(msg + 4) + 1);
207 challengelen = base64_decode(challenge, msg + 4, -1);
208 challenge[challengelen] = '\0';
209 log_print("ESMTP< [Decoded: %s]\n", challenge);
211 g_snprintf(buf, sizeof(buf), "%s", session->pass);
212 md5_hex_hmac(hexdigest, challenge, challengelen,
213 buf, strlen(session->pass));
216 response = g_strdup_printf
217 ("%s %s", session->user, hexdigest);
218 log_print("ESMTP> [Encoded: %s]\n", response);
220 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
221 base64_encode(response64, response, strlen(response));
224 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
226 log_print("ESMTP> %s\n", response64);
229 /* Server rejects AUTH */
230 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
232 log_print("ESMTP> *\n");
235 case SMTPAUTH_DIGEST_MD5:
237 /* stop smtp_auth when no correct authtype */
238 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*");
239 log_print("ESMTP> *\n");
246 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
248 gchar buf[MSGBUFSIZE];
250 session->state = SMTP_AUTH_LOGIN_PASS;
252 if (!strncmp(msg, "334 ", 4))
253 base64_encode(buf, session->pass, strlen(session->pass));
255 /* Server rejects AUTH */
256 g_snprintf(buf, sizeof(buf), "*");
258 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
259 log_print("ESMTP> [PASSWORD]\n");
264 static gint smtp_ehlo(SMTPSession *session)
266 gchar buf[MSGBUFSIZE];
268 session->state = SMTP_EHLO;
270 session->avail_auth_type = 0;
272 g_snprintf(buf, sizeof(buf), "EHLO %s",
273 session->hostname ? session->hostname : get_domain_name());
274 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
275 log_print("ESMTP> %s\n", buf);
280 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
282 if (strncmp(msg, "250", 3) == 0) {
283 const gchar *p = msg;
285 if (*p == '-' || *p == ' ') p++;
286 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
288 if (strcasestr(p, "PLAIN"))
289 session->avail_auth_type |= SMTPAUTH_PLAIN;
290 if (strcasestr(p, "LOGIN"))
291 session->avail_auth_type |= SMTPAUTH_LOGIN;
292 if (strcasestr(p, "CRAM-MD5"))
293 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
294 if (strcasestr(p, "DIGEST-MD5"))
295 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
297 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
299 session->max_message_size = atoi(p);
300 session->esmtp_flags |= ESMTP_SIZE;
303 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
304 (msg[3] == ' ' || msg[3] == '\0'))
306 else if (msg[0] == '5' && msg[1] == '0' &&
307 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
313 static gint smtp_starttls(SMTPSession *session)
315 session->state = SMTP_STARTTLS;
317 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS");
318 log_print("ESMTP> STARTTLS\n");
323 static gint smtp_auth_cram_md5(SMTPSession *session)
325 session->state = SMTP_AUTH;
326 session->auth_type = SMTPAUTH_CRAM_MD5;
328 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5");
329 log_print("ESMTP> AUTH CRAM-MD5\n");
334 static gint smtp_auth_plain(SMTPSession *session)
336 gchar buf[MSGBUFSIZE];
342 int b64len = (1 + strlen(session->user) + 1 + strlen(session->pass) + 1);
343 gchar *b64buf = g_malloc(b64len);
345 /* use the char *ptr to walk the base64 string with embedded \0 */
349 session->state = SMTP_AUTH_PLAIN;
350 session->auth_type = SMTPAUTH_PLAIN;
352 memset(buf, 0, sizeof buf);
355 * have to construct the string bit by bit. sprintf can't do it in one.
356 * first field is null, so string is \0<user>\0<password>
361 g_snprintf (a, b64len - 1, "%s", session->user);
363 b64cnt = strlen(session->user)+1;
366 g_snprintf (a, b64len - b64cnt - 1, "%s", session->pass);
367 b64cnt += strlen(session->pass) + 1;
370 * reuse the char *ptr to offset into the textbuf to meld
371 * the plaintext ESMTP message and the base64 string value
373 strcpy(buf, "AUTH PLAIN ");
374 a = buf + strlen(buf);
375 base64_encode(a, b64buf, b64cnt);
377 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
379 log_print("ESMTP> [AUTH PLAIN]\n");
386 static gint smtp_auth_login(SMTPSession *session)
388 session->state = SMTP_AUTH;
389 session->auth_type = SMTPAUTH_LOGIN;
391 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN");
392 log_print("ESMTP> AUTH LOGIN\n");
397 static gint smtp_helo(SMTPSession *session)
399 gchar buf[MSGBUFSIZE];
401 session->state = SMTP_HELO;
403 g_snprintf(buf, sizeof(buf), "HELO %s",
404 session->hostname ? session->hostname : get_domain_name());
405 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
406 log_print("SMTP> %s\n", buf);
411 static gint smtp_rcpt(SMTPSession *session)
413 gchar buf[MSGBUFSIZE];
416 g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
418 session->state = SMTP_RCPT;
420 to = (gchar *)session->cur_to->data;
423 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
425 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
426 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
427 log_print("SMTP> %s\n", buf);
429 session->cur_to = session->cur_to->next;
434 static gint smtp_data(SMTPSession *session)
436 session->state = SMTP_DATA;
438 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA");
439 log_print("SMTP> DATA\n");
444 static gint smtp_send_data(SMTPSession *session)
446 session->state = SMTP_SEND_DATA;
448 session_send_data(SESSION(session), session->send_data,
449 session->send_data_len);
455 static gint smtp_rset(SMTPSession *session)
457 session->state = SMTP_RSET;
459 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "RSET");
460 log_print("SMTP> RSET\n");
466 static gint smtp_make_ready(SMTPSession *session)
468 session->state = SMTP_MAIL_SENT_OK;
473 gint smtp_quit(SMTPSession *session)
475 session->state = SMTP_QUIT;
477 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
478 log_print("SMTP> QUIT\n");
483 static gint smtp_eom(SMTPSession *session)
485 session->state = SMTP_EOM;
487 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".");
488 log_print("SMTP> . (EOM)\n");
493 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
495 SMTPSession *smtp_session = SMTP_SESSION(session);
496 gboolean cont = FALSE;
498 if (strlen(msg) < 4) {
499 log_warning(_("bad SMTP response\n"));
503 switch (smtp_session->state) {
507 case SMTP_AUTH_PLAIN:
508 case SMTP_AUTH_LOGIN_USER:
509 case SMTP_AUTH_LOGIN_PASS:
510 case SMTP_AUTH_CRAM_MD5:
511 log_print("ESMTP< %s\n", msg);
514 log_print("SMTP< %s\n", msg);
518 if (msg[0] == '5' && msg[1] == '0' &&
519 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
520 log_warning(_("error occurred on SMTP session\n"));
521 smtp_session->state = SMTP_ERROR;
522 smtp_session->error_val = SM_ERROR;
523 g_free(smtp_session->error_msg);
524 smtp_session->error_msg = g_strdup(msg);
528 if (!strncmp(msg, "535", 3)) {
529 log_warning(_("error occurred on authentication\n"));
530 smtp_session->state = SMTP_ERROR;
531 smtp_session->error_val = SM_AUTHFAIL;
532 g_free(smtp_session->error_msg);
533 smtp_session->error_msg = g_strdup(msg);
537 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
538 log_warning(_("error occurred on SMTP session\n"));
539 smtp_session->state = SMTP_ERROR;
540 smtp_session->error_val = SM_ERROR;
541 g_free(smtp_session->error_msg);
542 smtp_session->error_msg = g_strdup(msg);
548 else if (msg[3] != ' ' && msg[3] != '\0') {
549 log_warning(_("bad SMTP response\n"));
550 smtp_session->state = SMTP_ERROR;
551 smtp_session->error_val = SM_UNRECOVERABLE;
555 /* ignore all multiline responses except for EHLO */
556 if (cont && smtp_session->state != SMTP_EHLO)
557 return session_recv_msg(session);
559 switch (smtp_session->state) {
561 if (strstr(msg, "ESMTP"))
562 smtp_session->is_esmtp = TRUE;
565 if (smtp_session->user || session->ssl_type != SSL_NONE ||
566 smtp_session->is_esmtp)
568 if (smtp_session->user || smtp_session->is_esmtp)
570 smtp_ehlo(smtp_session);
572 smtp_helo(smtp_session);
575 smtp_from(smtp_session);
578 smtp_ehlo_recv(smtp_session, msg);
581 if (smtp_session->max_message_size > 0
582 && smtp_session->max_message_size <
583 smtp_session->send_data_len) {
584 log_warning(_("Message is too big "
585 "(Maximum size is %s)\n"),
587 (off_t)(smtp_session->max_message_size)));
588 smtp_session->state = SMTP_ERROR;
589 smtp_session->error_val = SM_ERROR;
593 if (session->ssl_type == SSL_STARTTLS &&
594 smtp_session->tls_init_done == FALSE) {
595 smtp_starttls(smtp_session);
599 if (smtp_session->user) {
600 if (smtp_auth(smtp_session) != SM_OK)
601 smtp_from(smtp_session);
603 smtp_from(smtp_session);
607 if (session_start_tls(session) < 0) {
608 log_warning(_("can't start TLS session\n"));
609 smtp_session->state = SMTP_ERROR;
610 smtp_session->error_val = SM_ERROR;
613 smtp_session->tls_init_done = TRUE;
614 smtp_ehlo(smtp_session);
618 smtp_auth_recv(smtp_session, msg);
620 case SMTP_AUTH_LOGIN_USER:
621 smtp_auth_login_user_recv(smtp_session, msg);
623 case SMTP_AUTH_PLAIN:
624 case SMTP_AUTH_LOGIN_PASS:
625 case SMTP_AUTH_CRAM_MD5:
626 smtp_from(smtp_session);
629 if (smtp_session->cur_to)
630 smtp_rcpt(smtp_session);
633 if (smtp_session->cur_to)
634 smtp_rcpt(smtp_session);
636 smtp_data(smtp_session);
639 smtp_send_data(smtp_session);
642 smtp_make_ready(smtp_session);
645 session_disconnect(session);
649 log_warning(_("error occurred on SMTP session\n"));
650 smtp_session->error_val = SM_ERROR;
655 return session_recv_msg(session);
660 static gint smtp_session_send_data_finished(Session *session, guint len)
662 smtp_eom(SMTP_SESSION(session));