2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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);
64 SESSION(session)->type = SESSION_SMTP;
65 SESSION(session)->server = NULL;
66 SESSION(session)->port = 0;
67 SESSION(session)->sock = NULL;
68 SESSION(session)->state = SESSION_READY;
69 SESSION(session)->data = NULL;
71 SESSION(session)->recv_msg = smtp_session_recv_msg;
73 SESSION(session)->recv_data_finished = NULL;
74 SESSION(session)->send_data_finished = smtp_session_send_data_finished;
76 SESSION(session)->destroy = smtp_session_destroy;
78 session->state = SMTP_READY;
81 session->tls_init_done = FALSE;
84 session->hostname = NULL;
89 session->to_list = NULL;
90 session->cur_to = NULL;
92 session->send_data = NULL;
93 session->send_data_len = 0;
95 session->avail_auth_type = 0;
96 session->forced_auth_type = 0;
97 session->auth_type = 0;
99 session->error_val = SM_OK;
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);
116 static gint smtp_from(SMTPSession *session)
118 gchar buf[MSGBUFSIZE];
120 g_return_val_if_fail(session->from != NULL, SM_ERROR);
122 session->state = SMTP_FROM;
124 if (strchr(session->from, '<'))
125 g_snprintf(buf, sizeof(buf), "MAIL FROM: %s", session->from);
127 g_snprintf(buf, sizeof(buf), "MAIL FROM: <%s>", session->from);
129 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
130 log_print("SMTP> %s\n", buf);
135 static gint smtp_auth(SMTPSession *session)
138 g_return_val_if_fail(session->user != NULL, SM_ERROR);
140 session->state = SMTP_AUTH;
142 if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
143 (session->forced_auth_type == 0 &&
144 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
145 smtp_auth_cram_md5(session);
146 else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
147 (session->forced_auth_type == 0 &&
148 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
149 smtp_auth_login(session);
151 log_warning(_("SMTP AUTH not available\n"));
158 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
160 gchar buf[MSGBUFSIZE];
162 switch (session->auth_type) {
164 session->state = SMTP_AUTH_LOGIN_USER;
166 if (!strncmp(msg, "334 ", 4)) {
167 base64_encode(buf, session->user, strlen(session->user));
169 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
171 log_print("ESMTP> [USERID]\n");
173 /* Server rejects AUTH */
174 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
176 log_print("ESMTP> *\n");
179 case SMTPAUTH_CRAM_MD5:
180 session->state = SMTP_AUTH_CRAM_MD5;
182 if (!strncmp(msg, "334 ", 4)) {
187 guchar hexdigest[33];
189 challenge = g_malloc(strlen(msg + 4) + 1);
190 challengelen = base64_decode(challenge, msg + 4, -1);
191 challenge[challengelen] = '\0';
192 log_print("ESMTP< [Decoded: %s]\n", challenge);
194 g_snprintf(buf, sizeof(buf), "%s", session->pass);
195 md5_hex_hmac(hexdigest, challenge, challengelen,
196 buf, strlen(session->pass));
199 response = g_strdup_printf
200 ("%s %s", session->user, hexdigest);
201 log_print("ESMTP> [Encoded: %s]\n", response);
203 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
204 base64_encode(response64, response, strlen(response));
207 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
209 log_print("ESMTP> %s\n", response64);
212 /* Server rejects AUTH */
213 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
215 log_print("ESMTP> *\n");
218 case SMTPAUTH_DIGEST_MD5:
220 /* stop smtp_auth when no correct authtype */
221 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*");
222 log_print("ESMTP> *\n");
229 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
231 gchar buf[MSGBUFSIZE];
233 session->state = SMTP_AUTH_LOGIN_PASS;
235 if (!strncmp(msg, "334 ", 4))
236 base64_encode(buf, session->pass, strlen(session->pass));
238 /* Server rejects AUTH */
239 g_snprintf(buf, sizeof(buf), "*");
241 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
242 log_print("ESMTP> [PASSWORD]\n");
247 static gint smtp_ehlo(SMTPSession *session)
249 gchar buf[MSGBUFSIZE];
251 session->state = SMTP_EHLO;
253 session->avail_auth_type = 0;
255 g_snprintf(buf, sizeof(buf), "EHLO %s",
256 session->hostname ? session->hostname : get_domain_name());
257 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
258 log_print("ESMTP> %s\n", buf);
263 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
265 if (strncmp(msg, "250", 3) == 0) {
266 const gchar *p = msg;
268 if (*p == '-' || *p == ' ') p++;
269 if (g_strncasecmp(p, "AUTH", 4) == 0) {
271 if (strcasestr(p, "LOGIN"))
272 session->avail_auth_type |= SMTPAUTH_LOGIN;
273 if (strcasestr(p, "CRAM-MD5"))
274 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
275 if (strcasestr(p, "DIGEST-MD5"))
276 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
279 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
280 (msg[3] == ' ' || msg[3] == '\0'))
282 else if (msg[0] == '5' && msg[1] == '0' &&
283 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
289 static gint smtp_starttls(SMTPSession *session)
291 session->state = SMTP_STARTTLS;
293 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS");
294 log_print("ESMTP> STARTTLS\n");
299 static gint smtp_auth_cram_md5(SMTPSession *session)
301 session->state = SMTP_AUTH;
302 session->auth_type = SMTPAUTH_CRAM_MD5;
304 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5");
305 log_print("ESMTP> AUTH CRAM-MD5\n");
310 static gint smtp_auth_login(SMTPSession *session)
312 session->state = SMTP_AUTH;
313 session->auth_type = SMTPAUTH_LOGIN;
315 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN");
316 log_print("ESMTP> AUTH LOGIN\n");
321 static gint smtp_helo(SMTPSession *session)
323 gchar buf[MSGBUFSIZE];
325 session->state = SMTP_HELO;
327 g_snprintf(buf, sizeof(buf), "HELO %s",
328 session->hostname ? session->hostname : get_domain_name());
329 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
330 log_print("SMTP> %s\n", buf);
335 static gint smtp_rcpt(SMTPSession *session)
337 gchar buf[MSGBUFSIZE];
340 g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
342 session->state = SMTP_RCPT;
344 to = (gchar *)session->cur_to->data;
347 g_snprintf(buf, sizeof(buf), "RCPT TO: %s", to);
349 g_snprintf(buf, sizeof(buf), "RCPT TO: <%s>", to);
350 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
351 log_print("SMTP> %s\n", buf);
353 session->cur_to = session->cur_to->next;
358 static gint smtp_data(SMTPSession *session)
360 session->state = SMTP_DATA;
362 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA");
363 log_print("SMTP> DATA\n");
368 static gint smtp_send_data(SMTPSession *session)
370 session->state = SMTP_SEND_DATA;
372 session_send_data(SESSION(session), session->send_data,
373 session->send_data_len);
378 static gint smtp_rset(SMTPSession *session)
380 session->state = SMTP_RSET;
382 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "RSET");
383 log_print("SMTP> RSET\n");
388 static gint smtp_quit(SMTPSession *session)
390 session->state = SMTP_QUIT;
392 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
393 log_print("SMTP> QUIT\n");
398 static gint smtp_eom(SMTPSession *session)
400 session->state = SMTP_EOM;
402 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".");
403 log_print("SMTP> . (EOM)\n");
408 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
410 SMTPSession *smtp_session = SMTP_SESSION(session);
411 gboolean cont = FALSE;
413 if (strlen(msg) < 4) {
414 log_warning(_("bad SMTP response\n"));
418 switch (smtp_session->state) {
422 case SMTP_AUTH_LOGIN_USER:
423 case SMTP_AUTH_LOGIN_PASS:
424 case SMTP_AUTH_CRAM_MD5:
425 log_print("ESMTP< %s\n", msg);
428 log_print("SMTP< %s\n", msg);
432 if (msg[0] == '5' && msg[1] == '0' &&
433 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
434 log_warning(_("error occurred on SMTP session\n"));
435 smtp_session->state = SMTP_ERROR;
436 smtp_session->error_val = SM_ERROR;
440 if (msg[0] == '5' && msg[1] == '3' && msg[2] == '5') {
441 smtp_session->state = SMTP_AUTH_FAILED;
445 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
446 log_warning(_("error occurred on SMTP session\n"));
447 smtp_session->state = SMTP_ERROR;
448 smtp_session->error_val = SM_ERROR;
454 else if (msg[3] != ' ' && msg[3] != '\0') {
455 log_warning(_("bad SMTP response\n"));
456 smtp_session->state = SMTP_ERROR;
457 smtp_session->error_val = SM_UNRECOVERABLE;
461 /* ignore all multiline responses except for EHLO */
462 if (cont && smtp_session->state != SMTP_EHLO)
465 switch (smtp_session->state) {
469 if (smtp_session->user || session->ssl_type != SSL_NONE)
471 if (smtp_session->user)
473 smtp_ehlo(smtp_session);
475 smtp_helo(smtp_session);
478 smtp_from(smtp_session);
481 smtp_ehlo_recv(smtp_session, msg);
485 if (session->ssl_type == SSL_STARTTLS &&
486 smtp_session->tls_init_done == FALSE) {
487 smtp_starttls(smtp_session);
491 if (smtp_session->user) {
492 if (smtp_auth(smtp_session) != SM_OK)
493 smtp_from(smtp_session);
495 smtp_from(smtp_session);
499 if (session_start_tls(session) < 0) {
500 log_warning(_("can't start TLS session\n"));
501 smtp_session->state = SMTP_ERROR;
502 smtp_session->error_val = SM_ERROR;
505 smtp_session->tls_init_done = TRUE;
506 smtp_ehlo(smtp_session);
510 smtp_auth_recv(smtp_session, msg);
512 case SMTP_AUTH_LOGIN_USER:
513 smtp_auth_login_user_recv(smtp_session, msg);
515 case SMTP_AUTH_LOGIN_PASS:
516 case SMTP_AUTH_CRAM_MD5:
517 smtp_from(smtp_session);
520 if (smtp_session->cur_to)
521 smtp_rcpt(smtp_session);
524 if (smtp_session->cur_to)
525 smtp_rcpt(smtp_session);
527 smtp_data(smtp_session);
530 smtp_send_data(smtp_session);
533 smtp_quit(smtp_session);
536 session_disconnect(session);
539 case SMTP_AUTH_FAILED:
541 log_warning(_("error occurred on SMTP session\n"));
542 smtp_session->error_val = SM_ERROR;
552 static gint smtp_session_send_data_finished(Session *session, guint len)
554 smtp_eom(SMTP_SESSION(session));