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.
25 #include <glib/gi18n.h>
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);
43 static gint smtp_auth_plain(SMTPSession *session);
45 static gint smtp_ehlo(SMTPSession *session);
46 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
48 static gint smtp_helo(SMTPSession *session);
49 static gint smtp_rcpt(SMTPSession *session);
50 static gint smtp_data(SMTPSession *session);
51 static gint smtp_send_data(SMTPSession *session);
52 /* static gint smtp_rset(SMTPSession *session); */
53 static gint smtp_quit(SMTPSession *session);
54 static gint smtp_eom(SMTPSession *session);
56 static gint smtp_session_recv_msg(Session *session, const gchar *msg);
57 static gint smtp_session_send_data_finished(Session *session, guint len);
60 Session *smtp_session_new(void)
64 session = g_new0(SMTPSession, 1);
66 session_init(SESSION(session));
68 SESSION(session)->type = SESSION_SMTP;
70 SESSION(session)->recv_msg = smtp_session_recv_msg;
72 SESSION(session)->recv_data_finished = NULL;
73 SESSION(session)->send_data_finished = smtp_session_send_data_finished;
75 SESSION(session)->destroy = smtp_session_destroy;
77 session->state = SMTP_READY;
80 session->tls_init_done = FALSE;
83 session->hostname = NULL;
88 session->to_list = NULL;
89 session->cur_to = NULL;
91 session->send_data = NULL;
92 session->send_data_len = 0;
94 session->max_message_size = -1;
96 session->avail_auth_type = 0;
97 session->forced_auth_type = 0;
98 session->auth_type = 0;
99 session->esmtp_flags = 0;
101 session->error_val = SM_OK;
102 session->error_msg = NULL;
104 return SESSION(session);
107 static void smtp_session_destroy(Session *session)
109 SMTPSession *smtp_session = SMTP_SESSION(session);
111 g_free(smtp_session->hostname);
112 g_free(smtp_session->user);
113 g_free(smtp_session->pass);
114 g_free(smtp_session->from);
116 g_free(smtp_session->send_data);
118 g_free(smtp_session->error_msg);
121 static gint smtp_from(SMTPSession *session)
123 gchar buf[MSGBUFSIZE];
124 gchar *mail_size = NULL;
126 g_return_val_if_fail(session->from != NULL, SM_ERROR);
128 session->state = SMTP_FROM;
130 if (session->is_esmtp && (session->esmtp_flags & ESMTP_SIZE)!=0)
131 mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
133 mail_size = g_strdup("");
136 if (strchr(session->from, '<'))
137 g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
140 g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
145 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
146 log_print("%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
151 static gint smtp_auth(SMTPSession *session)
154 g_return_val_if_fail(session->user != NULL, SM_ERROR);
156 session->state = SMTP_AUTH;
158 if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
159 (session->forced_auth_type == 0 &&
160 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
161 smtp_auth_cram_md5(session);
162 else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
163 (session->forced_auth_type == 0 &&
164 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
165 smtp_auth_login(session);
166 else if (session->forced_auth_type == SMTPAUTH_PLAIN ||
167 (session->forced_auth_type == 0 &&
168 (session->avail_auth_type & SMTPAUTH_PLAIN) != 0))
169 smtp_auth_plain(session);
171 log_warning(_("SMTP AUTH not available\n"));
178 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
180 gchar buf[MSGBUFSIZE];
182 switch (session->auth_type) {
184 session->state = SMTP_AUTH_LOGIN_USER;
186 if (!strncmp(msg, "334 ", 4)) {
187 base64_encode(buf, session->user, strlen(session->user));
189 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
191 log_print("ESMTP> [USERID]\n");
193 /* Server rejects AUTH */
194 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
196 log_print("ESMTP> *\n");
199 case SMTPAUTH_CRAM_MD5:
200 session->state = SMTP_AUTH_CRAM_MD5;
202 if (!strncmp(msg, "334 ", 4)) {
207 guchar hexdigest[33];
209 challenge = g_malloc(strlen(msg + 4) + 1);
210 challengelen = base64_decode(challenge, msg + 4, -1);
211 challenge[challengelen] = '\0';
212 log_print("ESMTP< [Decoded: %s]\n", challenge);
214 g_snprintf(buf, sizeof(buf), "%s", session->pass);
215 md5_hex_hmac(hexdigest, challenge, challengelen,
216 buf, strlen(session->pass));
219 response = g_strdup_printf
220 ("%s %s", session->user, hexdigest);
221 log_print("ESMTP> [Encoded: %s]\n", response);
223 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
224 base64_encode(response64, response, strlen(response));
227 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
229 log_print("ESMTP> %s\n", response64);
232 /* Server rejects AUTH */
233 session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
235 log_print("ESMTP> *\n");
238 case SMTPAUTH_DIGEST_MD5:
240 /* stop smtp_auth when no correct authtype */
241 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*");
242 log_print("ESMTP> *\n");
249 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
251 gchar buf[MSGBUFSIZE];
253 session->state = SMTP_AUTH_LOGIN_PASS;
255 if (!strncmp(msg, "334 ", 4))
256 base64_encode(buf, session->pass, strlen(session->pass));
258 /* Server rejects AUTH */
259 g_snprintf(buf, sizeof(buf), "*");
261 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
262 log_print("ESMTP> [PASSWORD]\n");
267 static gint smtp_ehlo(SMTPSession *session)
269 gchar buf[MSGBUFSIZE];
271 session->state = SMTP_EHLO;
273 session->avail_auth_type = 0;
275 g_snprintf(buf, sizeof(buf), "EHLO %s",
276 session->hostname ? session->hostname : get_domain_name());
277 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
278 log_print("ESMTP> %s\n", buf);
283 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
285 if (strncmp(msg, "250", 3) == 0) {
286 const gchar *p = msg;
288 if (*p == '-' || *p == ' ') p++;
289 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
291 if (strcasestr(p, "PLAIN"))
292 session->avail_auth_type |= SMTPAUTH_PLAIN;
293 if (strcasestr(p, "LOGIN"))
294 session->avail_auth_type |= SMTPAUTH_LOGIN;
295 if (strcasestr(p, "CRAM-MD5"))
296 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
297 if (strcasestr(p, "DIGEST-MD5"))
298 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
300 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
302 session->max_message_size = atoi(p);
303 session->esmtp_flags |= ESMTP_SIZE;
306 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
307 (msg[3] == ' ' || msg[3] == '\0'))
309 else if (msg[0] == '5' && msg[1] == '0' &&
310 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
316 static gint smtp_starttls(SMTPSession *session)
318 session->state = SMTP_STARTTLS;
320 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS");
321 log_print("ESMTP> STARTTLS\n");
326 static gint smtp_auth_cram_md5(SMTPSession *session)
328 session->state = SMTP_AUTH;
329 session->auth_type = SMTPAUTH_CRAM_MD5;
331 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5");
332 log_print("ESMTP> AUTH CRAM-MD5\n");
337 static gint smtp_auth_plain(SMTPSession *session)
339 gchar buf[MSGBUFSIZE];
345 int b64len = (1 + strlen(session->user) + 1 + strlen(session->pass) + 1);
346 gchar *b64buf = g_malloc(b64len);
348 /* use the char *ptr to walk the base64 string with embedded \0 */
352 session->state = SMTP_AUTH_PLAIN;
353 session->auth_type = SMTPAUTH_PLAIN;
355 memset(buf, 0, sizeof buf);
358 * have to construct the string bit by bit. sprintf can't do it in one.
359 * first field is null, so string is \0<user>\0<password>
364 g_snprintf (a, b64len - 1, "%s", session->user);
366 b64cnt = strlen(session->user)+1;
369 g_snprintf (a, b64len - b64cnt - 1, "%s", session->pass);
370 b64cnt += strlen(session->pass) + 1;
373 * reuse the char *ptr to offset into the textbuf to meld
374 * the plaintext ESMTP message and the base64 string value
376 strcpy(buf, "AUTH PLAIN ");
377 a = buf + strlen(buf);
378 base64_encode(a, b64buf, b64cnt);
380 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
382 log_print("ESMTP> [AUTH PLAIN]\n");
389 static gint smtp_auth_login(SMTPSession *session)
391 session->state = SMTP_AUTH;
392 session->auth_type = SMTPAUTH_LOGIN;
394 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN");
395 log_print("ESMTP> AUTH LOGIN\n");
400 static gint smtp_helo(SMTPSession *session)
402 gchar buf[MSGBUFSIZE];
404 session->state = SMTP_HELO;
406 g_snprintf(buf, sizeof(buf), "HELO %s",
407 session->hostname ? session->hostname : get_domain_name());
408 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
409 log_print("SMTP> %s\n", buf);
414 static gint smtp_rcpt(SMTPSession *session)
416 gchar buf[MSGBUFSIZE];
419 g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
421 session->state = SMTP_RCPT;
423 to = (gchar *)session->cur_to->data;
426 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
428 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
429 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
430 log_print("SMTP> %s\n", buf);
432 session->cur_to = session->cur_to->next;
437 static gint smtp_data(SMTPSession *session)
439 session->state = SMTP_DATA;
441 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA");
442 log_print("SMTP> DATA\n");
447 static gint smtp_send_data(SMTPSession *session)
449 session->state = SMTP_SEND_DATA;
451 session_send_data(SESSION(session), session->send_data,
452 session->send_data_len);
458 static gint smtp_rset(SMTPSession *session)
460 session->state = SMTP_RSET;
462 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "RSET");
463 log_print("SMTP> RSET\n");
469 static gint smtp_quit(SMTPSession *session)
471 session->state = SMTP_QUIT;
473 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
474 log_print("SMTP> QUIT\n");
479 static gint smtp_eom(SMTPSession *session)
481 session->state = SMTP_EOM;
483 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".");
484 log_print("SMTP> . (EOM)\n");
489 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
491 SMTPSession *smtp_session = SMTP_SESSION(session);
492 gboolean cont = FALSE;
494 if (strlen(msg) < 4) {
495 log_warning(_("bad SMTP response\n"));
499 switch (smtp_session->state) {
503 case SMTP_AUTH_PLAIN:
504 case SMTP_AUTH_LOGIN_USER:
505 case SMTP_AUTH_LOGIN_PASS:
506 case SMTP_AUTH_CRAM_MD5:
507 log_print("ESMTP< %s\n", msg);
510 log_print("SMTP< %s\n", msg);
514 if (msg[0] == '5' && msg[1] == '0' &&
515 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
516 log_warning(_("error occurred on SMTP session\n"));
517 smtp_session->state = SMTP_ERROR;
518 smtp_session->error_val = SM_ERROR;
519 g_free(smtp_session->error_msg);
520 smtp_session->error_msg = g_strdup(msg);
524 if (!strncmp(msg, "535", 3)) {
525 log_warning(_("error occurred on authentication\n"));
526 smtp_session->state = SMTP_ERROR;
527 smtp_session->error_val = SM_AUTHFAIL;
528 g_free(smtp_session->error_msg);
529 smtp_session->error_msg = g_strdup(msg);
533 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
534 log_warning(_("error occurred on SMTP session\n"));
535 smtp_session->state = SMTP_ERROR;
536 smtp_session->error_val = SM_ERROR;
537 g_free(smtp_session->error_msg);
538 smtp_session->error_msg = g_strdup(msg);
544 else if (msg[3] != ' ' && msg[3] != '\0') {
545 log_warning(_("bad SMTP response\n"));
546 smtp_session->state = SMTP_ERROR;
547 smtp_session->error_val = SM_UNRECOVERABLE;
551 /* ignore all multiline responses except for EHLO */
552 if (cont && smtp_session->state != SMTP_EHLO)
553 return session_recv_msg(session);
555 switch (smtp_session->state) {
557 if (strstr(msg, "ESMTP"))
558 smtp_session->is_esmtp = TRUE;
561 if (smtp_session->user || session->ssl_type != SSL_NONE ||
562 smtp_session->is_esmtp)
564 if (smtp_session->user || smtp_session->is_esmtp)
566 smtp_ehlo(smtp_session);
568 smtp_helo(smtp_session);
571 smtp_from(smtp_session);
574 smtp_ehlo_recv(smtp_session, msg);
577 if (smtp_session->max_message_size > 0
578 && smtp_session->max_message_size <
579 smtp_session->send_data_len) {
580 log_warning(_("Message is too big "
581 "(Maximum size is %s)\n"),
583 (off_t)(smtp_session->max_message_size)));
584 smtp_session->state = SMTP_ERROR;
585 smtp_session->error_val = SM_ERROR;
589 if (session->ssl_type == SSL_STARTTLS &&
590 smtp_session->tls_init_done == FALSE) {
591 smtp_starttls(smtp_session);
595 if (smtp_session->user) {
596 if (smtp_auth(smtp_session) != SM_OK)
597 smtp_from(smtp_session);
599 smtp_from(smtp_session);
603 if (session_start_tls(session) < 0) {
604 log_warning(_("can't start TLS session\n"));
605 smtp_session->state = SMTP_ERROR;
606 smtp_session->error_val = SM_ERROR;
609 smtp_session->tls_init_done = TRUE;
610 smtp_ehlo(smtp_session);
614 smtp_auth_recv(smtp_session, msg);
616 case SMTP_AUTH_LOGIN_USER:
617 smtp_auth_login_user_recv(smtp_session, msg);
619 case SMTP_AUTH_PLAIN:
620 case SMTP_AUTH_LOGIN_PASS:
621 case SMTP_AUTH_CRAM_MD5:
622 smtp_from(smtp_session);
625 if (smtp_session->cur_to)
626 smtp_rcpt(smtp_session);
629 if (smtp_session->cur_to)
630 smtp_rcpt(smtp_session);
632 smtp_data(smtp_session);
635 smtp_send_data(smtp_session);
638 smtp_quit(smtp_session);
641 session_disconnect(session);
645 log_warning(_("error occurred on SMTP session\n"));
646 smtp_session->error_val = SM_ERROR;
651 return session_recv_msg(session);
656 static gint smtp_session_send_data_finished(Session *session, guint len)
658 smtp_eom(SMTP_SESSION(session));