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;
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];
123 g_return_val_if_fail(session->from != NULL, SM_ERROR);
125 session->state = SMTP_FROM;
127 if (strchr(session->from, '<'))
128 g_snprintf(buf, sizeof(buf), "MAIL FROM: %s", session->from);
130 g_snprintf(buf, sizeof(buf), "MAIL FROM: <%s>", session->from);
132 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
133 log_print("SMTP> %s\n", buf);
138 static gint smtp_auth(SMTPSession *session)
141 g_return_val_if_fail(session->user != NULL, SM_ERROR);
143 session->state = SMTP_AUTH;
145 if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
146 (session->forced_auth_type == 0 &&
147 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
148 smtp_auth_cram_md5(session);
149 else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
150 (session->forced_auth_type == 0 &&
151 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
152 smtp_auth_login(session);
154 log_warning(_("SMTP AUTH not available\n"));
161 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
163 gchar buf[MSGBUFSIZE];
165 switch (session->auth_type) {
167 session->state = SMTP_AUTH_LOGIN_USER;
169 if (!strncmp(msg, "334 ", 4)) {
170 base64_encode(buf, session->user, strlen(session->user));
172 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
174 log_print("ESMTP> [USERID]\n");
176 /* Server rejects AUTH */
177 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
179 log_print("ESMTP> *\n");
182 case SMTPAUTH_CRAM_MD5:
183 session->state = SMTP_AUTH_CRAM_MD5;
185 if (!strncmp(msg, "334 ", 4)) {
190 guchar hexdigest[33];
192 challenge = g_malloc(strlen(msg + 4) + 1);
193 challengelen = base64_decode(challenge, msg + 4, -1);
194 challenge[challengelen] = '\0';
195 log_print("ESMTP< [Decoded: %s]\n", challenge);
197 g_snprintf(buf, sizeof(buf), "%s", session->pass);
198 md5_hex_hmac(hexdigest, challenge, challengelen,
199 buf, strlen(session->pass));
202 response = g_strdup_printf
203 ("%s %s", session->user, hexdigest);
204 log_print("ESMTP> [Encoded: %s]\n", response);
206 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
207 base64_encode(response64, response, strlen(response));
210 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
212 log_print("ESMTP> %s\n", response64);
215 /* Server rejects AUTH */
216 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
218 log_print("ESMTP> *\n");
221 case SMTPAUTH_DIGEST_MD5:
223 /* stop smtp_auth when no correct authtype */
224 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*");
225 log_print("ESMTP> *\n");
232 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
234 gchar buf[MSGBUFSIZE];
236 session->state = SMTP_AUTH_LOGIN_PASS;
238 if (!strncmp(msg, "334 ", 4))
239 base64_encode(buf, session->pass, strlen(session->pass));
241 /* Server rejects AUTH */
242 g_snprintf(buf, sizeof(buf), "*");
244 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
245 log_print("ESMTP> [PASSWORD]\n");
250 static gint smtp_ehlo(SMTPSession *session)
252 gchar buf[MSGBUFSIZE];
254 session->state = SMTP_EHLO;
256 session->avail_auth_type = 0;
258 g_snprintf(buf, sizeof(buf), "EHLO %s",
259 session->hostname ? session->hostname : get_domain_name());
260 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
261 log_print("ESMTP> %s\n", buf);
266 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
268 if (strncmp(msg, "250", 3) == 0) {
269 const gchar *p = msg;
271 if (*p == '-' || *p == ' ') p++;
272 if (g_strncasecmp(p, "AUTH", 4) == 0) {
274 if (strcasestr(p, "LOGIN"))
275 session->avail_auth_type |= SMTPAUTH_LOGIN;
276 if (strcasestr(p, "CRAM-MD5"))
277 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
278 if (strcasestr(p, "DIGEST-MD5"))
279 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
282 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
283 (msg[3] == ' ' || msg[3] == '\0'))
285 else if (msg[0] == '5' && msg[1] == '0' &&
286 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
292 static gint smtp_starttls(SMTPSession *session)
294 session->state = SMTP_STARTTLS;
296 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS");
297 log_print("ESMTP> STARTTLS\n");
302 static gint smtp_auth_cram_md5(SMTPSession *session)
304 session->state = SMTP_AUTH;
305 session->auth_type = SMTPAUTH_CRAM_MD5;
307 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5");
308 log_print("ESMTP> AUTH CRAM-MD5\n");
313 static gint smtp_auth_login(SMTPSession *session)
315 session->state = SMTP_AUTH;
316 session->auth_type = SMTPAUTH_LOGIN;
318 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN");
319 log_print("ESMTP> AUTH LOGIN\n");
324 static gint smtp_helo(SMTPSession *session)
326 gchar buf[MSGBUFSIZE];
328 session->state = SMTP_HELO;
330 g_snprintf(buf, sizeof(buf), "HELO %s",
331 session->hostname ? session->hostname : get_domain_name());
332 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
333 log_print("SMTP> %s\n", buf);
338 static gint smtp_rcpt(SMTPSession *session)
340 gchar buf[MSGBUFSIZE];
343 g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
345 session->state = SMTP_RCPT;
347 to = (gchar *)session->cur_to->data;
350 g_snprintf(buf, sizeof(buf), "RCPT TO: %s", to);
352 g_snprintf(buf, sizeof(buf), "RCPT TO: <%s>", to);
353 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
354 log_print("SMTP> %s\n", buf);
356 session->cur_to = session->cur_to->next;
361 static gint smtp_data(SMTPSession *session)
363 session->state = SMTP_DATA;
365 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA");
366 log_print("SMTP> DATA\n");
371 static gint smtp_send_data(SMTPSession *session)
373 session->state = SMTP_SEND_DATA;
375 session_send_data(SESSION(session), session->send_data,
376 session->send_data_len);
382 static gint smtp_rset(SMTPSession *session)
384 session->state = SMTP_RSET;
386 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "RSET");
387 log_print("SMTP> RSET\n");
393 static gint smtp_quit(SMTPSession *session)
395 session->state = SMTP_QUIT;
397 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
398 log_print("SMTP> QUIT\n");
403 static gint smtp_eom(SMTPSession *session)
405 session->state = SMTP_EOM;
407 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".");
408 log_print("SMTP> . (EOM)\n");
413 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
415 SMTPSession *smtp_session = SMTP_SESSION(session);
416 gboolean cont = FALSE;
418 if (strlen(msg) < 4) {
419 log_warning(_("bad SMTP response\n"));
423 switch (smtp_session->state) {
427 case SMTP_AUTH_LOGIN_USER:
428 case SMTP_AUTH_LOGIN_PASS:
429 case SMTP_AUTH_CRAM_MD5:
430 log_print("ESMTP< %s\n", msg);
433 log_print("SMTP< %s\n", msg);
437 if (msg[0] == '5' && msg[1] == '0' &&
438 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
439 log_warning(_("error occurred on SMTP session\n"));
440 smtp_session->state = SMTP_ERROR;
441 smtp_session->error_val = SM_ERROR;
442 g_free(smtp_session->error_msg);
443 smtp_session->error_msg = g_strdup(msg);
447 if (!strncmp(msg, "535", 3)) {
448 log_warning(_("error occurred on authentication\n"));
449 smtp_session->state = SMTP_ERROR;
450 smtp_session->error_val = SM_AUTHFAIL;
451 g_free(smtp_session->error_msg);
452 smtp_session->error_msg = g_strdup(msg);
456 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
457 log_warning(_("error occurred on SMTP session\n"));
458 smtp_session->state = SMTP_ERROR;
459 smtp_session->error_val = SM_ERROR;
460 g_free(smtp_session->error_msg);
461 smtp_session->error_msg = g_strdup(msg);
467 else if (msg[3] != ' ' && msg[3] != '\0') {
468 log_warning(_("bad SMTP response\n"));
469 smtp_session->state = SMTP_ERROR;
470 smtp_session->error_val = SM_UNRECOVERABLE;
474 /* ignore all multiline responses except for EHLO */
475 if (cont && smtp_session->state != SMTP_EHLO)
478 switch (smtp_session->state) {
482 if (smtp_session->user || session->ssl_type != SSL_NONE)
484 if (smtp_session->user)
486 smtp_ehlo(smtp_session);
488 smtp_helo(smtp_session);
491 smtp_from(smtp_session);
494 smtp_ehlo_recv(smtp_session, msg);
498 if (session->ssl_type == SSL_STARTTLS &&
499 smtp_session->tls_init_done == FALSE) {
500 smtp_starttls(smtp_session);
504 if (smtp_session->user) {
505 if (smtp_auth(smtp_session) != SM_OK)
506 smtp_from(smtp_session);
508 smtp_from(smtp_session);
512 if (session_start_tls(session) < 0) {
513 log_warning(_("can't start TLS session\n"));
514 smtp_session->state = SMTP_ERROR;
515 smtp_session->error_val = SM_ERROR;
518 smtp_session->tls_init_done = TRUE;
519 smtp_ehlo(smtp_session);
523 smtp_auth_recv(smtp_session, msg);
525 case SMTP_AUTH_LOGIN_USER:
526 smtp_auth_login_user_recv(smtp_session, msg);
528 case SMTP_AUTH_LOGIN_PASS:
529 case SMTP_AUTH_CRAM_MD5:
530 smtp_from(smtp_session);
533 if (smtp_session->cur_to)
534 smtp_rcpt(smtp_session);
537 if (smtp_session->cur_to)
538 smtp_rcpt(smtp_session);
540 smtp_data(smtp_session);
543 smtp_send_data(smtp_session);
546 smtp_quit(smtp_session);
549 session_disconnect(session);
553 log_warning(_("error occurred on SMTP session\n"));
554 smtp_session->error_val = SM_ERROR;
564 static gint smtp_session_send_data_finished(Session *session, guint len)
566 smtp_eom(SMTP_SESSION(session));