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.
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);
44 static gint smtp_ehlo(SMTPSession *session);
45 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
47 static gint smtp_helo(SMTPSession *session);
48 static gint smtp_rcpt(SMTPSession *session);
49 static gint smtp_data(SMTPSession *session);
50 static gint smtp_send_data(SMTPSession *session);
51 /* static gint smtp_rset(SMTPSession *session); */
52 static gint smtp_quit(SMTPSession *session);
53 static gint smtp_eom(SMTPSession *session);
55 static gint smtp_session_recv_msg(Session *session, const gchar *msg);
56 static gint smtp_session_send_data_finished(Session *session, guint len);
59 Session *smtp_session_new(void)
63 session = g_new0(SMTPSession, 1);
65 session_init(SESSION(session));
67 SESSION(session)->type = SESSION_SMTP;
69 SESSION(session)->recv_msg = smtp_session_recv_msg;
71 SESSION(session)->recv_data_finished = NULL;
72 SESSION(session)->send_data_finished = smtp_session_send_data_finished;
74 SESSION(session)->destroy = smtp_session_destroy;
76 session->state = SMTP_READY;
79 session->tls_init_done = FALSE;
82 session->hostname = NULL;
87 session->to_list = NULL;
88 session->cur_to = NULL;
90 session->send_data = NULL;
91 session->send_data_len = 0;
93 session->max_message_size = -1;
95 session->avail_auth_type = 0;
96 session->forced_auth_type = 0;
97 session->auth_type = 0;
99 session->error_val = SM_OK;
100 session->error_msg = NULL;
102 return SESSION(session);
105 static void smtp_session_destroy(Session *session)
107 SMTPSession *smtp_session = SMTP_SESSION(session);
109 g_free(smtp_session->hostname);
110 g_free(smtp_session->user);
111 g_free(smtp_session->pass);
112 g_free(smtp_session->from);
114 g_free(smtp_session->send_data);
116 g_free(smtp_session->error_msg);
119 static gint smtp_from(SMTPSession *session)
121 gchar buf[MSGBUFSIZE];
122 gchar *mail_size = NULL;
124 g_return_val_if_fail(session->from != NULL, SM_ERROR);
126 session->state = SMTP_FROM;
128 if (session->is_esmtp)
129 mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
131 mail_size = g_strdup("");
134 if (strchr(session->from, '<'))
135 g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
138 g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
143 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
144 log_print("%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
149 static gint smtp_auth(SMTPSession *session)
152 g_return_val_if_fail(session->user != NULL, SM_ERROR);
154 session->state = SMTP_AUTH;
156 if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
157 (session->forced_auth_type == 0 &&
158 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
159 smtp_auth_cram_md5(session);
160 else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
161 (session->forced_auth_type == 0 &&
162 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
163 smtp_auth_login(session);
165 log_warning(_("SMTP AUTH not available\n"));
172 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
174 gchar buf[MSGBUFSIZE];
176 switch (session->auth_type) {
178 session->state = SMTP_AUTH_LOGIN_USER;
180 if (!strncmp(msg, "334 ", 4)) {
181 base64_encode(buf, session->user, strlen(session->user));
183 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
185 log_print("ESMTP> [USERID]\n");
187 /* Server rejects AUTH */
188 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
190 log_print("ESMTP> *\n");
193 case SMTPAUTH_CRAM_MD5:
194 session->state = SMTP_AUTH_CRAM_MD5;
196 if (!strncmp(msg, "334 ", 4)) {
201 guchar hexdigest[33];
203 challenge = g_malloc(strlen(msg + 4) + 1);
204 challengelen = base64_decode(challenge, msg + 4, -1);
205 challenge[challengelen] = '\0';
206 log_print("ESMTP< [Decoded: %s]\n", challenge);
208 g_snprintf(buf, sizeof(buf), "%s", session->pass);
209 md5_hex_hmac(hexdigest, challenge, challengelen,
210 buf, strlen(session->pass));
213 response = g_strdup_printf
214 ("%s %s", session->user, hexdigest);
215 log_print("ESMTP> [Encoded: %s]\n", response);
217 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
218 base64_encode(response64, response, strlen(response));
221 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
223 log_print("ESMTP> %s\n", response64);
226 /* Server rejects AUTH */
227 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
229 log_print("ESMTP> *\n");
232 case SMTPAUTH_DIGEST_MD5:
234 /* stop smtp_auth when no correct authtype */
235 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*");
236 log_print("ESMTP> *\n");
243 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
245 gchar buf[MSGBUFSIZE];
247 session->state = SMTP_AUTH_LOGIN_PASS;
249 if (!strncmp(msg, "334 ", 4))
250 base64_encode(buf, session->pass, strlen(session->pass));
252 /* Server rejects AUTH */
253 g_snprintf(buf, sizeof(buf), "*");
255 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
256 log_print("ESMTP> [PASSWORD]\n");
261 static gint smtp_ehlo(SMTPSession *session)
263 gchar buf[MSGBUFSIZE];
265 session->state = SMTP_EHLO;
267 session->avail_auth_type = 0;
269 g_snprintf(buf, sizeof(buf), "EHLO %s",
270 session->hostname ? session->hostname : get_domain_name());
271 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
272 log_print("ESMTP> %s\n", buf);
277 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
279 if (strncmp(msg, "250", 3) == 0) {
280 const gchar *p = msg;
282 if (*p == '-' || *p == ' ') p++;
283 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
285 if (strcasestr(p, "LOGIN"))
286 session->avail_auth_type |= SMTPAUTH_LOGIN;
287 if (strcasestr(p, "CRAM-MD5"))
288 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
289 if (strcasestr(p, "DIGEST-MD5"))
290 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
292 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
294 session->max_message_size = atoi(p);
297 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
298 (msg[3] == ' ' || msg[3] == '\0'))
300 else if (msg[0] == '5' && msg[1] == '0' &&
301 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
307 static gint smtp_starttls(SMTPSession *session)
309 session->state = SMTP_STARTTLS;
311 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS");
312 log_print("ESMTP> STARTTLS\n");
317 static gint smtp_auth_cram_md5(SMTPSession *session)
319 session->state = SMTP_AUTH;
320 session->auth_type = SMTPAUTH_CRAM_MD5;
322 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5");
323 log_print("ESMTP> AUTH CRAM-MD5\n");
328 static gint smtp_auth_login(SMTPSession *session)
330 session->state = SMTP_AUTH;
331 session->auth_type = SMTPAUTH_LOGIN;
333 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN");
334 log_print("ESMTP> AUTH LOGIN\n");
339 static gint smtp_helo(SMTPSession *session)
341 gchar buf[MSGBUFSIZE];
343 session->state = SMTP_HELO;
345 g_snprintf(buf, sizeof(buf), "HELO %s",
346 session->hostname ? session->hostname : get_domain_name());
347 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
348 log_print("SMTP> %s\n", buf);
353 static gint smtp_rcpt(SMTPSession *session)
355 gchar buf[MSGBUFSIZE];
358 g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
360 session->state = SMTP_RCPT;
362 to = (gchar *)session->cur_to->data;
365 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
367 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
368 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
369 log_print("SMTP> %s\n", buf);
371 session->cur_to = session->cur_to->next;
376 static gint smtp_data(SMTPSession *session)
378 session->state = SMTP_DATA;
380 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA");
381 log_print("SMTP> DATA\n");
386 static gint smtp_send_data(SMTPSession *session)
388 session->state = SMTP_SEND_DATA;
390 session_send_data(SESSION(session), session->send_data,
391 session->send_data_len);
397 static gint smtp_rset(SMTPSession *session)
399 session->state = SMTP_RSET;
401 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "RSET");
402 log_print("SMTP> RSET\n");
408 static gint smtp_quit(SMTPSession *session)
410 session->state = SMTP_QUIT;
412 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
413 log_print("SMTP> QUIT\n");
418 static gint smtp_eom(SMTPSession *session)
420 session->state = SMTP_EOM;
422 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".");
423 log_print("SMTP> . (EOM)\n");
428 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
430 SMTPSession *smtp_session = SMTP_SESSION(session);
431 gboolean cont = FALSE;
433 if (strlen(msg) < 4) {
434 log_warning(_("bad SMTP response\n"));
438 switch (smtp_session->state) {
442 case SMTP_AUTH_LOGIN_USER:
443 case SMTP_AUTH_LOGIN_PASS:
444 case SMTP_AUTH_CRAM_MD5:
445 log_print("ESMTP< %s\n", msg);
448 log_print("SMTP< %s\n", msg);
452 if (msg[0] == '5' && msg[1] == '0' &&
453 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
454 log_warning(_("error occurred on SMTP session\n"));
455 smtp_session->state = SMTP_ERROR;
456 smtp_session->error_val = SM_ERROR;
457 g_free(smtp_session->error_msg);
458 smtp_session->error_msg = g_strdup(msg);
462 if (!strncmp(msg, "535", 3)) {
463 log_warning(_("error occurred on authentication\n"));
464 smtp_session->state = SMTP_ERROR;
465 smtp_session->error_val = SM_AUTHFAIL;
466 g_free(smtp_session->error_msg);
467 smtp_session->error_msg = g_strdup(msg);
471 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
472 log_warning(_("error occurred on SMTP session\n"));
473 smtp_session->state = SMTP_ERROR;
474 smtp_session->error_val = SM_ERROR;
475 g_free(smtp_session->error_msg);
476 smtp_session->error_msg = g_strdup(msg);
482 else if (msg[3] != ' ' && msg[3] != '\0') {
483 log_warning(_("bad SMTP response\n"));
484 smtp_session->state = SMTP_ERROR;
485 smtp_session->error_val = SM_UNRECOVERABLE;
489 /* ignore all multiline responses except for EHLO */
490 if (cont && smtp_session->state != SMTP_EHLO)
491 return session_recv_msg(session);
493 switch (smtp_session->state) {
495 if (strstr(msg, "ESMTP"))
496 smtp_session->is_esmtp = TRUE;
499 if (smtp_session->user || session->ssl_type != SSL_NONE ||
500 smtp_session->is_esmtp)
502 if (smtp_session->user || smtp_session->is_esmtp)
504 smtp_ehlo(smtp_session);
506 smtp_helo(smtp_session);
509 smtp_from(smtp_session);
512 smtp_ehlo_recv(smtp_session, msg);
515 if (smtp_session->max_message_size > 0
516 && smtp_session->max_message_size <
517 smtp_session->send_data_len) {
518 log_warning(_("Message is too big "
519 "(Maximum size is %s)\n"),
521 (off_t)(smtp_session->max_message_size)));
522 smtp_session->state = SMTP_ERROR;
523 smtp_session->error_val = SM_ERROR;
527 if (session->ssl_type == SSL_STARTTLS &&
528 smtp_session->tls_init_done == FALSE) {
529 smtp_starttls(smtp_session);
533 if (smtp_session->user) {
534 if (smtp_auth(smtp_session) != SM_OK)
535 smtp_from(smtp_session);
537 smtp_from(smtp_session);
541 if (session_start_tls(session) < 0) {
542 log_warning(_("can't start TLS session\n"));
543 smtp_session->state = SMTP_ERROR;
544 smtp_session->error_val = SM_ERROR;
547 smtp_session->tls_init_done = TRUE;
548 smtp_ehlo(smtp_session);
552 smtp_auth_recv(smtp_session, msg);
554 case SMTP_AUTH_LOGIN_USER:
555 smtp_auth_login_user_recv(smtp_session, msg);
557 case SMTP_AUTH_LOGIN_PASS:
558 case SMTP_AUTH_CRAM_MD5:
559 smtp_from(smtp_session);
562 if (smtp_session->cur_to)
563 smtp_rcpt(smtp_session);
566 if (smtp_session->cur_to)
567 smtp_rcpt(smtp_session);
569 smtp_data(smtp_session);
572 smtp_send_data(smtp_session);
575 smtp_quit(smtp_session);
578 session_disconnect(session);
582 log_warning(_("error occurred on SMTP session\n"));
583 smtp_session->error_val = SM_ERROR;
588 return session_recv_msg(session);
593 static gint smtp_session_send_data_finished(Session *session, guint len)
595 smtp_eom(SMTP_SESSION(session));