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);
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->avail_auth_type = 0;
94 session->forced_auth_type = 0;
95 session->auth_type = 0;
97 session->error_val = SM_OK;
98 session->error_msg = NULL;
100 return SESSION(session);
103 static void smtp_session_destroy(Session *session)
105 SMTPSession *smtp_session = SMTP_SESSION(session);
107 g_free(smtp_session->hostname);
108 g_free(smtp_session->user);
109 g_free(smtp_session->pass);
110 g_free(smtp_session->from);
112 g_free(smtp_session->send_data);
114 g_free(smtp_session->error_msg);
117 static gint smtp_from(SMTPSession *session)
119 gchar buf[MSGBUFSIZE];
121 g_return_val_if_fail(session->from != NULL, SM_ERROR);
123 session->state = SMTP_FROM;
125 if (strchr(session->from, '<'))
126 g_snprintf(buf, sizeof(buf), "MAIL FROM: %s", session->from);
128 g_snprintf(buf, sizeof(buf), "MAIL FROM: <%s>", session->from);
130 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
131 log_print("SMTP> %s\n", buf);
136 static gint smtp_auth(SMTPSession *session)
139 g_return_val_if_fail(session->user != NULL, SM_ERROR);
141 session->state = SMTP_AUTH;
143 if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
144 (session->forced_auth_type == 0 &&
145 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
146 smtp_auth_cram_md5(session);
147 else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
148 (session->forced_auth_type == 0 &&
149 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
150 smtp_auth_login(session);
152 log_warning(_("SMTP AUTH not available\n"));
159 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
161 gchar buf[MSGBUFSIZE];
163 switch (session->auth_type) {
165 session->state = SMTP_AUTH_LOGIN_USER;
167 if (!strncmp(msg, "334 ", 4)) {
168 base64_encode(buf, session->user, strlen(session->user));
170 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
172 log_print("ESMTP> [USERID]\n");
174 /* Server rejects AUTH */
175 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
177 log_print("ESMTP> *\n");
180 case SMTPAUTH_CRAM_MD5:
181 session->state = SMTP_AUTH_CRAM_MD5;
183 if (!strncmp(msg, "334 ", 4)) {
188 guchar hexdigest[33];
190 challenge = g_malloc(strlen(msg + 4) + 1);
191 challengelen = base64_decode(challenge, msg + 4, -1);
192 challenge[challengelen] = '\0';
193 log_print("ESMTP< [Decoded: %s]\n", challenge);
195 g_snprintf(buf, sizeof(buf), "%s", session->pass);
196 md5_hex_hmac(hexdigest, challenge, challengelen,
197 buf, strlen(session->pass));
200 response = g_strdup_printf
201 ("%s %s", session->user, hexdigest);
202 log_print("ESMTP> [Encoded: %s]\n", response);
204 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
205 base64_encode(response64, response, strlen(response));
208 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
210 log_print("ESMTP> %s\n", response64);
213 /* Server rejects AUTH */
214 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
216 log_print("ESMTP> *\n");
219 case SMTPAUTH_DIGEST_MD5:
221 /* stop smtp_auth when no correct authtype */
222 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*");
223 log_print("ESMTP> *\n");
230 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
232 gchar buf[MSGBUFSIZE];
234 session->state = SMTP_AUTH_LOGIN_PASS;
236 if (!strncmp(msg, "334 ", 4))
237 base64_encode(buf, session->pass, strlen(session->pass));
239 /* Server rejects AUTH */
240 g_snprintf(buf, sizeof(buf), "*");
242 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
243 log_print("ESMTP> [PASSWORD]\n");
248 static gint smtp_ehlo(SMTPSession *session)
250 gchar buf[MSGBUFSIZE];
252 session->state = SMTP_EHLO;
254 session->avail_auth_type = 0;
256 g_snprintf(buf, sizeof(buf), "EHLO %s",
257 session->hostname ? session->hostname : get_domain_name());
258 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
259 log_print("ESMTP> %s\n", buf);
264 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
266 if (strncmp(msg, "250", 3) == 0) {
267 const gchar *p = msg;
269 if (*p == '-' || *p == ' ') p++;
270 if (g_strncasecmp(p, "AUTH", 4) == 0) {
272 if (strcasestr(p, "LOGIN"))
273 session->avail_auth_type |= SMTPAUTH_LOGIN;
274 if (strcasestr(p, "CRAM-MD5"))
275 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
276 if (strcasestr(p, "DIGEST-MD5"))
277 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
280 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
281 (msg[3] == ' ' || msg[3] == '\0'))
283 else if (msg[0] == '5' && msg[1] == '0' &&
284 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
290 static gint smtp_starttls(SMTPSession *session)
292 session->state = SMTP_STARTTLS;
294 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS");
295 log_print("ESMTP> STARTTLS\n");
300 static gint smtp_auth_cram_md5(SMTPSession *session)
302 session->state = SMTP_AUTH;
303 session->auth_type = SMTPAUTH_CRAM_MD5;
305 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5");
306 log_print("ESMTP> AUTH CRAM-MD5\n");
311 static gint smtp_auth_login(SMTPSession *session)
313 session->state = SMTP_AUTH;
314 session->auth_type = SMTPAUTH_LOGIN;
316 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN");
317 log_print("ESMTP> AUTH LOGIN\n");
322 static gint smtp_helo(SMTPSession *session)
324 gchar buf[MSGBUFSIZE];
326 session->state = SMTP_HELO;
328 g_snprintf(buf, sizeof(buf), "HELO %s",
329 session->hostname ? session->hostname : get_domain_name());
330 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
331 log_print("SMTP> %s\n", buf);
336 static gint smtp_rcpt(SMTPSession *session)
338 gchar buf[MSGBUFSIZE];
341 g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
343 session->state = SMTP_RCPT;
345 to = (gchar *)session->cur_to->data;
348 g_snprintf(buf, sizeof(buf), "RCPT TO: %s", to);
350 g_snprintf(buf, sizeof(buf), "RCPT TO: <%s>", to);
351 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
352 log_print("SMTP> %s\n", buf);
354 session->cur_to = session->cur_to->next;
359 static gint smtp_data(SMTPSession *session)
361 session->state = SMTP_DATA;
363 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA");
364 log_print("SMTP> DATA\n");
369 static gint smtp_send_data(SMTPSession *session)
371 session->state = SMTP_SEND_DATA;
373 session_send_data(SESSION(session), session->send_data,
374 session->send_data_len);
380 static gint smtp_rset(SMTPSession *session)
382 session->state = SMTP_RSET;
384 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "RSET");
385 log_print("SMTP> RSET\n");
391 static gint smtp_quit(SMTPSession *session)
393 session->state = SMTP_QUIT;
395 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
396 log_print("SMTP> QUIT\n");
401 static gint smtp_eom(SMTPSession *session)
403 session->state = SMTP_EOM;
405 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".");
406 log_print("SMTP> . (EOM)\n");
411 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
413 SMTPSession *smtp_session = SMTP_SESSION(session);
414 gboolean cont = FALSE;
416 if (strlen(msg) < 4) {
417 log_warning(_("bad SMTP response\n"));
421 switch (smtp_session->state) {
425 case SMTP_AUTH_LOGIN_USER:
426 case SMTP_AUTH_LOGIN_PASS:
427 case SMTP_AUTH_CRAM_MD5:
428 log_print("ESMTP< %s\n", msg);
431 log_print("SMTP< %s\n", msg);
435 if (msg[0] == '5' && msg[1] == '0' &&
436 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
437 log_warning(_("error occurred on SMTP session\n"));
438 smtp_session->state = SMTP_ERROR;
439 smtp_session->error_val = SM_ERROR;
440 g_free(smtp_session->error_msg);
441 smtp_session->error_msg = g_strdup(msg);
445 if (!strncmp(msg, "535", 3)) {
446 log_warning(_("error occurred on authentication\n"));
447 smtp_session->state = SMTP_ERROR;
448 smtp_session->error_val = SM_AUTHFAIL;
449 g_free(smtp_session->error_msg);
450 smtp_session->error_msg = g_strdup(msg);
454 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
455 log_warning(_("error occurred on SMTP session\n"));
456 smtp_session->state = SMTP_ERROR;
457 smtp_session->error_val = SM_ERROR;
458 g_free(smtp_session->error_msg);
459 smtp_session->error_msg = g_strdup(msg);
465 else if (msg[3] != ' ' && msg[3] != '\0') {
466 log_warning(_("bad SMTP response\n"));
467 smtp_session->state = SMTP_ERROR;
468 smtp_session->error_val = SM_UNRECOVERABLE;
472 /* ignore all multiline responses except for EHLO */
473 if (cont && smtp_session->state != SMTP_EHLO)
474 return session_recv_msg(session);
476 switch (smtp_session->state) {
480 if (smtp_session->user || session->ssl_type != SSL_NONE)
482 if (smtp_session->user)
484 smtp_ehlo(smtp_session);
486 smtp_helo(smtp_session);
489 smtp_from(smtp_session);
492 smtp_ehlo_recv(smtp_session, msg);
496 if (session->ssl_type == SSL_STARTTLS &&
497 smtp_session->tls_init_done == FALSE) {
498 smtp_starttls(smtp_session);
502 if (smtp_session->user) {
503 if (smtp_auth(smtp_session) != SM_OK)
504 smtp_from(smtp_session);
506 smtp_from(smtp_session);
510 if (session_start_tls(session) < 0) {
511 log_warning(_("can't start TLS session\n"));
512 smtp_session->state = SMTP_ERROR;
513 smtp_session->error_val = SM_ERROR;
516 smtp_session->tls_init_done = TRUE;
517 smtp_ehlo(smtp_session);
521 smtp_auth_recv(smtp_session, msg);
523 case SMTP_AUTH_LOGIN_USER:
524 smtp_auth_login_user_recv(smtp_session, msg);
526 case SMTP_AUTH_LOGIN_PASS:
527 case SMTP_AUTH_CRAM_MD5:
528 smtp_from(smtp_session);
531 if (smtp_session->cur_to)
532 smtp_rcpt(smtp_session);
535 if (smtp_session->cur_to)
536 smtp_rcpt(smtp_session);
538 smtp_data(smtp_session);
541 smtp_send_data(smtp_session);
544 smtp_quit(smtp_session);
547 session_disconnect(session);
551 log_warning(_("error occurred on SMTP session\n"));
552 smtp_session->error_val = SM_ERROR;
557 return session_recv_msg(session);
562 static gint smtp_session_send_data_finished(Session *session, guint len)
564 smtp_eom(SMTP_SESSION(session));