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 return SESSION(session);
102 static void smtp_session_destroy(Session *session)
104 SMTPSession *smtp_session = SMTP_SESSION(session);
106 g_free(smtp_session->hostname);
107 g_free(smtp_session->user);
108 g_free(smtp_session->pass);
109 g_free(smtp_session->from);
111 g_free(smtp_session->send_data);
114 static gint smtp_from(SMTPSession *session)
116 gchar buf[MSGBUFSIZE];
118 g_return_val_if_fail(session->from != NULL, SM_ERROR);
120 session->state = SMTP_FROM;
122 if (strchr(session->from, '<'))
123 g_snprintf(buf, sizeof(buf), "MAIL FROM: %s", session->from);
125 g_snprintf(buf, sizeof(buf), "MAIL FROM: <%s>", session->from);
127 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
128 log_print("SMTP> %s\n", buf);
133 static gint smtp_auth(SMTPSession *session)
136 g_return_val_if_fail(session->user != NULL, SM_ERROR);
138 session->state = SMTP_AUTH;
140 if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
141 (session->forced_auth_type == 0 &&
142 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
143 smtp_auth_cram_md5(session);
144 else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
145 (session->forced_auth_type == 0 &&
146 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
147 smtp_auth_login(session);
149 log_warning(_("SMTP AUTH not available\n"));
156 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
158 gchar buf[MSGBUFSIZE];
160 switch (session->auth_type) {
162 session->state = SMTP_AUTH_LOGIN_USER;
164 if (!strncmp(msg, "334 ", 4)) {
165 base64_encode(buf, session->user, strlen(session->user));
167 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
169 log_print("ESMTP> [USERID]\n");
171 /* Server rejects AUTH */
172 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
174 log_print("ESMTP> *\n");
177 case SMTPAUTH_CRAM_MD5:
178 session->state = SMTP_AUTH_CRAM_MD5;
180 if (!strncmp(msg, "334 ", 4)) {
185 guchar hexdigest[33];
187 challenge = g_malloc(strlen(msg + 4) + 1);
188 challengelen = base64_decode(challenge, msg + 4, -1);
189 challenge[challengelen] = '\0';
190 log_print("ESMTP< [Decoded: %s]\n", challenge);
192 g_snprintf(buf, sizeof(buf), "%s", session->pass);
193 md5_hex_hmac(hexdigest, challenge, challengelen,
194 buf, strlen(session->pass));
197 response = g_strdup_printf
198 ("%s %s", session->user, hexdigest);
199 log_print("ESMTP> [Encoded: %s]\n", response);
201 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
202 base64_encode(response64, response, strlen(response));
205 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
207 log_print("ESMTP> %s\n", response64);
210 /* Server rejects AUTH */
211 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
213 log_print("ESMTP> *\n");
216 case SMTPAUTH_DIGEST_MD5:
218 /* stop smtp_auth when no correct authtype */
219 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*");
220 log_print("ESMTP> *\n");
227 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
229 gchar buf[MSGBUFSIZE];
231 session->state = SMTP_AUTH_LOGIN_PASS;
233 if (!strncmp(msg, "334 ", 4))
234 base64_encode(buf, session->pass, strlen(session->pass));
236 /* Server rejects AUTH */
237 g_snprintf(buf, sizeof(buf), "*");
239 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
240 log_print("ESMTP> [PASSWORD]\n");
245 static gint smtp_ehlo(SMTPSession *session)
247 gchar buf[MSGBUFSIZE];
249 session->state = SMTP_EHLO;
251 session->avail_auth_type = 0;
253 g_snprintf(buf, sizeof(buf), "EHLO %s",
254 session->hostname ? session->hostname : get_domain_name());
255 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
256 log_print("ESMTP> %s\n", buf);
261 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
263 if (strncmp(msg, "250", 3) == 0) {
264 const gchar *p = msg;
266 if (*p == '-' || *p == ' ') p++;
267 if (g_strncasecmp(p, "AUTH", 4) == 0) {
269 if (strcasestr(p, "LOGIN"))
270 session->avail_auth_type |= SMTPAUTH_LOGIN;
271 if (strcasestr(p, "CRAM-MD5"))
272 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
273 if (strcasestr(p, "DIGEST-MD5"))
274 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
277 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
278 (msg[3] == ' ' || msg[3] == '\0'))
280 else if (msg[0] == '5' && msg[1] == '0' &&
281 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
287 static gint smtp_starttls(SMTPSession *session)
289 session->state = SMTP_STARTTLS;
291 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS");
292 log_print("ESMTP> STARTTLS\n");
297 static gint smtp_auth_cram_md5(SMTPSession *session)
299 session->state = SMTP_AUTH;
300 session->auth_type = SMTPAUTH_CRAM_MD5;
302 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5");
303 log_print("ESMTP> AUTH CRAM-MD5\n");
308 static gint smtp_auth_login(SMTPSession *session)
310 session->state = SMTP_AUTH;
311 session->auth_type = SMTPAUTH_LOGIN;
313 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN");
314 log_print("ESMTP> AUTH LOGIN\n");
319 static gint smtp_helo(SMTPSession *session)
321 gchar buf[MSGBUFSIZE];
323 session->state = SMTP_HELO;
325 g_snprintf(buf, sizeof(buf), "HELO %s",
326 session->hostname ? session->hostname : get_domain_name());
327 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
328 log_print("SMTP> %s\n", buf);
333 static gint smtp_rcpt(SMTPSession *session)
335 gchar buf[MSGBUFSIZE];
338 g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
340 session->state = SMTP_RCPT;
342 to = (gchar *)session->cur_to->data;
345 g_snprintf(buf, sizeof(buf), "RCPT TO: %s", to);
347 g_snprintf(buf, sizeof(buf), "RCPT TO: <%s>", to);
348 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
349 log_print("SMTP> %s\n", buf);
351 session->cur_to = session->cur_to->next;
356 static gint smtp_data(SMTPSession *session)
358 session->state = SMTP_DATA;
360 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA");
361 log_print("SMTP> DATA\n");
366 static gint smtp_send_data(SMTPSession *session)
368 session->state = SMTP_SEND_DATA;
370 session_send_data(SESSION(session), session->send_data,
371 session->send_data_len);
376 static gint smtp_rset(SMTPSession *session)
378 session->state = SMTP_RSET;
380 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "RSET");
381 log_print("SMTP> RSET\n");
386 static gint smtp_quit(SMTPSession *session)
388 session->state = SMTP_QUIT;
390 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
391 log_print("SMTP> QUIT\n");
396 static gint smtp_eom(SMTPSession *session)
398 session->state = SMTP_EOM;
400 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".");
401 log_print("SMTP> . (EOM)\n");
406 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
408 SMTPSession *smtp_session = SMTP_SESSION(session);
409 gboolean cont = FALSE;
414 log_print("SMTP< %s\n", msg);
416 if (msg[0] == '5' && msg[1] == '0' &&
417 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
418 smtp_session->state = SMTP_ERROR;
422 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
423 smtp_session->state = SMTP_ERROR;
429 else if (msg[3] != ' ' && msg[3] != '\0') {
430 smtp_session->state = SMTP_ERROR;
434 /* ignore all multiline responses except for EHLO */
435 if (cont && smtp_session->state != SMTP_EHLO)
438 switch (smtp_session->state) {
442 if (smtp_session->user || session->ssl_type != SSL_NONE)
444 if (smtp_session->user)
446 smtp_ehlo(smtp_session);
448 smtp_helo(smtp_session);
451 smtp_from(smtp_session);
454 smtp_ehlo_recv(smtp_session, msg);
458 if (session->ssl_type == SSL_STARTTLS &&
459 smtp_session->tls_init_done == FALSE) {
460 smtp_starttls(smtp_session);
464 if (smtp_session->user) {
465 if (smtp_auth(smtp_session) != SM_OK)
466 smtp_from(smtp_session);
468 smtp_from(smtp_session);
472 if (session_start_tls(session) < 0)
474 smtp_session->tls_init_done = TRUE;
475 smtp_ehlo(smtp_session);
479 smtp_auth_recv(smtp_session, msg);
481 case SMTP_AUTH_LOGIN_USER:
482 smtp_auth_login_user_recv(smtp_session, msg);
484 case SMTP_AUTH_LOGIN_PASS:
485 case SMTP_AUTH_CRAM_MD5:
486 smtp_from(smtp_session);
489 if (smtp_session->cur_to)
490 smtp_rcpt(smtp_session);
493 if (smtp_session->cur_to)
494 smtp_rcpt(smtp_session);
496 smtp_data(smtp_session);
499 smtp_send_data(smtp_session);
502 smtp_quit(smtp_session);
505 session_disconnect(session);
518 static gint smtp_session_send_data_finished(Session *session, guint len)
520 smtp_eom(SMTP_SESSION(session));