a4de8de1f8c5ca6f811a9c4091a27cfa54c8de6a
[claws.git] / src / common / smtp.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
4  *
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 3 of the License, or
8  * (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #ifdef ENABLE_NLS
27 #include <glib/gi18n.h>
28 #else
29 #define _(a) (a)
30 #define N_(a) (a)
31 #endif
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "smtp.h"
36 #include "md5.h"
37 #include "base64.h"
38 #include "utils.h"
39 #include "log.h"
40
41 static void smtp_session_destroy(Session *session);
42
43 static gint smtp_auth(SMTPSession *session);
44 #ifdef USE_GNUTLS
45 static gint smtp_starttls(SMTPSession *session);
46 #endif
47 static gint smtp_auth_cram_md5(SMTPSession *session);
48 static gint smtp_auth_login(SMTPSession *session);
49 static gint smtp_auth_plain(SMTPSession *session);
50
51 static gint smtp_ehlo(SMTPSession *session);
52 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
53
54 static gint smtp_helo(SMTPSession *session);
55 static gint smtp_rcpt(SMTPSession *session);
56 static gint smtp_data(SMTPSession *session);
57 static gint smtp_send_data(SMTPSession *session);
58 static gint smtp_make_ready(SMTPSession *session);
59 static gint smtp_eom(SMTPSession *session);
60
61 static gint smtp_session_recv_msg(Session *session, const gchar *msg);
62 static gint smtp_session_send_data_finished(Session *session, guint len);
63
64
65 Session *smtp_session_new(void *prefs_account)
66 {
67         SMTPSession *session;
68
69         session = g_new0(SMTPSession, 1);
70
71         session_init(SESSION(session), prefs_account, TRUE);
72
73         SESSION(session)->type             = SESSION_SMTP;
74
75         SESSION(session)->recv_msg         = smtp_session_recv_msg;
76
77         SESSION(session)->recv_data_finished = NULL;
78         SESSION(session)->send_data_finished = smtp_session_send_data_finished;
79
80         SESSION(session)->destroy          = smtp_session_destroy;
81
82         session->state                     = SMTP_READY;
83
84 #ifdef USE_GNUTLS
85         session->tls_init_done             = FALSE;
86 #endif
87
88         session->hostname                  = NULL;
89         session->user                      = NULL;
90         session->pass                      = NULL;
91
92         session->from                      = NULL;
93         session->to_list                   = NULL;
94         session->cur_to                    = NULL;
95
96         session->send_data                 = NULL;
97         session->send_data_len             = 0;
98
99         session->max_message_size          = -1;
100
101         session->avail_auth_type           = 0;
102         session->forced_auth_type          = 0;
103         session->auth_type                 = 0;
104         session->esmtp_flags               = 0;
105
106         session->error_val                 = SM_OK;
107         session->error_msg                 = NULL;
108
109         return SESSION(session);
110 }
111
112 static void smtp_session_destroy(Session *session)
113 {
114         SMTPSession *smtp_session = SMTP_SESSION(session);
115
116         g_free(smtp_session->hostname);
117         g_free(smtp_session->user);
118         g_free(smtp_session->pass);
119         g_free(smtp_session->from);
120
121         g_free(smtp_session->send_data);
122
123         g_free(smtp_session->error_msg);
124 }
125
126 gint smtp_from(SMTPSession *session)
127 {
128         gchar buf[MESSAGEBUFSIZE];
129         gchar *mail_size = NULL;
130
131         cm_return_val_if_fail(session->from != NULL, SM_ERROR);
132
133         session->state = SMTP_FROM;
134         
135         if (session->is_esmtp && (session->esmtp_flags & ESMTP_SIZE)!=0)
136                 mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
137         else
138                 mail_size = g_strdup("");
139                 
140
141         if (strchr(session->from, '<'))
142                 g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
143                            mail_size);
144         else
145                 g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
146                            mail_size);
147
148         g_free(mail_size);
149
150         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
151                 return SM_ERROR;
152         log_print(LOG_PROTOCOL, "%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
153
154         return SM_OK;
155 }
156
157 static gint smtp_auth(SMTPSession *session)
158 {
159
160         cm_return_val_if_fail(session->user != NULL, SM_ERROR);
161
162         session->state = SMTP_AUTH;
163
164         if ((session->forced_auth_type == SMTPAUTH_CRAM_MD5
165              || session->forced_auth_type == 0)
166             &&
167              (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0)
168                 smtp_auth_cram_md5(session);
169         else if ((session->forced_auth_type == SMTPAUTH_LOGIN
170                   || session->forced_auth_type == 0)
171                  &&
172                   (session->avail_auth_type & SMTPAUTH_LOGIN) != 0)
173                 smtp_auth_login(session);
174         else if ((session->forced_auth_type == SMTPAUTH_PLAIN
175                   || session->forced_auth_type == 0)
176                  &&
177                   (session->avail_auth_type & SMTPAUTH_PLAIN) != 0)
178                 smtp_auth_plain(session);
179         else if (session->forced_auth_type == 0) {
180                 log_warning(LOG_PROTOCOL, _("No SMTP AUTH method available\n"));
181                 return SM_AUTHFAIL;
182         } else {
183                 log_warning(LOG_PROTOCOL, _("Selected SMTP AUTH method not available\n"));
184                 return SM_AUTHFAIL;
185         }
186
187         return SM_OK;
188 }
189
190 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
191 {
192         gchar buf[MESSAGEBUFSIZE];
193
194         switch (session->auth_type) {
195         case SMTPAUTH_LOGIN:
196                 session->state = SMTP_AUTH_LOGIN_USER;
197
198                 if (!strncmp(msg, "334 ", 4)) {
199                         base64_encode(buf, session->user, strlen(session->user));
200
201                         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
202                                          buf) < 0)
203                                 return SM_ERROR;
204                         log_print(LOG_PROTOCOL, "ESMTP> [USERID]\n");
205                 } else {
206                         /* Server rejects AUTH */
207                         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
208                                          "*") < 0)
209                                 return SM_ERROR;
210                         log_print(LOG_PROTOCOL, "ESMTP> *\n");
211                 }
212                 break;
213         case SMTPAUTH_CRAM_MD5:
214                 session->state = SMTP_AUTH_CRAM_MD5;
215
216                 if (!strncmp(msg, "334 ", 4)) {
217                         gchar *response;
218                         gchar *response64;
219                         gchar *challenge;
220                         gint challengelen;
221                         guchar hexdigest[33];
222
223                         challenge = g_malloc(strlen(msg + 4) + 1);
224                         challengelen = base64_decode(challenge, msg + 4, -1);
225                         challenge[challengelen] = '\0';
226                         log_print(LOG_PROTOCOL, "ESMTP< [Decoded: %s]\n", challenge);
227
228                         g_snprintf(buf, sizeof(buf), "%s", session->pass);
229                         md5_hex_hmac(hexdigest, challenge, challengelen,
230                                      buf, strlen(session->pass));
231                         g_free(challenge);
232
233                         response = g_strdup_printf
234                                 ("%s %s", session->user, hexdigest);
235                         log_print(LOG_PROTOCOL, "ESMTP> [Encoded: %s]\n", response);
236
237                         response64 = g_malloc((strlen(response) + 3) * 2 + 1);
238                         base64_encode(response64, response, strlen(response));
239                         g_free(response);
240
241                         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
242                                          response64) < 0)
243                                 return SM_ERROR;
244                         log_print(LOG_PROTOCOL, "ESMTP> %s\n", response64);
245                         g_free(response64);
246                 } else {
247                         /* Server rejects AUTH */
248                         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
249                                          "*") < 0)
250                                 return SM_ERROR;
251                         log_print(LOG_PROTOCOL, "ESMTP> *\n");
252                 }
253                 break;
254         case SMTPAUTH_DIGEST_MD5:
255         default:
256                 /* stop smtp_auth when no correct authtype */
257                 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*") < 0)
258                         return SM_ERROR;
259                 log_print(LOG_PROTOCOL, "ESMTP> *\n");
260                 break;
261         }
262
263         return SM_OK;
264 }
265
266 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
267 {
268         gchar buf[MESSAGEBUFSIZE];
269
270         session->state = SMTP_AUTH_LOGIN_PASS;
271
272         if (!strncmp(msg, "334 ", 4))
273                 base64_encode(buf, session->pass, strlen(session->pass));
274         else
275                 /* Server rejects AUTH */
276                 g_snprintf(buf, sizeof(buf), "*");
277
278         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
279                 return SM_ERROR;
280
281         log_print(LOG_PROTOCOL, "ESMTP> [PASSWORD]\n");
282
283         return SM_OK;
284 }
285
286 static gint smtp_ehlo(SMTPSession *session)
287 {
288         gchar buf[MESSAGEBUFSIZE];
289
290         session->state = SMTP_EHLO;
291
292         session->avail_auth_type = 0;
293
294         g_snprintf(buf, sizeof(buf), "EHLO %s",
295                    session->hostname ? session->hostname : get_domain_name());
296         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
297                 return SM_ERROR;
298         log_print(LOG_PROTOCOL, "ESMTP> %s\n", buf);
299
300         return SM_OK;
301 }
302
303 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
304 {
305         if (strncmp(msg, "250", 3) == 0) {
306                 const gchar *p = msg;
307                 p += 3;
308                 if (*p == '-' || *p == ' ') p++;
309                 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
310                         p += 5;
311                         if (strcasestr(p, "PLAIN"))
312                                 session->avail_auth_type |= SMTPAUTH_PLAIN;
313                         if (strcasestr(p, "LOGIN"))
314                                 session->avail_auth_type |= SMTPAUTH_LOGIN;
315                         if (strcasestr(p, "CRAM-MD5"))
316                                 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
317                         if (strcasestr(p, "DIGEST-MD5"))
318                                 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
319                 }
320                 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
321                         p += 5;
322                         session->max_message_size = atoi(p);
323                         session->esmtp_flags |= ESMTP_SIZE;
324                 }
325                 if (g_ascii_strncasecmp(p, "STARTTLS", 8) == 0) {
326                         p += 9;
327                         session->avail_auth_type |= SMTPAUTH_TLS_AVAILABLE;
328                 }
329                 return SM_OK;
330         } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
331             (msg[3] == ' ' || msg[3] == '\0'))
332                 return SM_OK;
333         else if (msg[0] == '5' && msg[1] == '0' &&
334                  (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
335                 return SM_ERROR;
336
337         return SM_ERROR;
338 }
339
340 #ifdef USE_GNUTLS
341 static gint smtp_starttls(SMTPSession *session)
342 {
343         session->state = SMTP_STARTTLS;
344
345         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS") < 0)
346                 return SM_ERROR;
347         log_print(LOG_PROTOCOL, "ESMTP> STARTTLS\n");
348
349         return SM_OK;
350 }
351 #endif
352
353 static gint smtp_auth_cram_md5(SMTPSession *session)
354 {
355         session->state = SMTP_AUTH;
356         session->auth_type = SMTPAUTH_CRAM_MD5;
357
358         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5") < 0)
359                 return SM_ERROR;
360         log_print(LOG_PROTOCOL, "ESMTP> AUTH CRAM-MD5\n");
361
362         return SM_OK;
363 }
364
365 static gint smtp_auth_plain(SMTPSession *session)
366 {
367         gchar buf[MESSAGEBUFSIZE];
368
369         /* 
370          * +1      +1      +1
371          * \0<user>\0<pass>\0 
372          */
373         int b64len = (1 + strlen(session->user) + 1 + strlen(session->pass) + 1);
374         gchar *b64buf = g_malloc(b64len);
375
376         /* use the char *ptr to walk the base64 string with embedded \0 */
377         char  *a = b64buf;
378         int  b64cnt = 0;
379
380         session->state = SMTP_AUTH_PLAIN;
381         session->auth_type = SMTPAUTH_PLAIN;
382
383         memset(buf, 0, sizeof buf);
384
385         /*
386          * have to construct the string bit by bit. sprintf can't do it in one.
387          * first field is null, so string is \0<user>\0<password>
388          */
389         *a = 0;
390         a++;
391
392         g_snprintf (a, b64len - 1, "%s", session->user);
393
394         b64cnt = strlen(session->user)+1;
395         a += b64cnt;
396
397         g_snprintf (a, b64len - b64cnt - 1, "%s", session->pass);
398         b64cnt += strlen(session->pass) + 1;    
399
400         /*
401          * reuse the char *ptr to offset into the textbuf to meld
402          * the plaintext ESMTP message and the base64 string value
403          */
404         strcpy(buf, "AUTH PLAIN ");
405         a = buf + strlen(buf);
406         base64_encode(a, b64buf, b64cnt);
407
408         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
409                 return SM_ERROR;
410
411         log_print(LOG_PROTOCOL, "ESMTP> [AUTH PLAIN]\n");
412
413         g_free(b64buf);
414
415         return SM_OK;
416 }
417
418 static gint smtp_auth_login(SMTPSession *session)
419 {
420         session->state = SMTP_AUTH;
421         session->auth_type = SMTPAUTH_LOGIN;
422
423         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN") < 0)
424                 return SM_ERROR;
425         log_print(LOG_PROTOCOL, "ESMTP> AUTH LOGIN\n");
426
427         return SM_OK;
428 }
429
430 static gint smtp_helo(SMTPSession *session)
431 {
432         gchar buf[MESSAGEBUFSIZE];
433
434         session->state = SMTP_HELO;
435
436         g_snprintf(buf, sizeof(buf), "HELO %s",
437                    session->hostname ? session->hostname : get_domain_name());
438         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
439                 return SM_ERROR;
440         log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
441
442         return SM_OK;
443 }
444
445 static gint smtp_rcpt(SMTPSession *session)
446 {
447         gchar buf[MESSAGEBUFSIZE];
448         gchar *to;
449
450         cm_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
451
452         session->state = SMTP_RCPT;
453
454         to = (gchar *)session->cur_to->data;
455
456         if (strchr(to, '<'))
457                 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
458         else
459                 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
460         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
461                 return SM_ERROR;
462         log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
463
464         session->cur_to = session->cur_to->next;
465
466         return SM_OK;
467 }
468
469 static gint smtp_data(SMTPSession *session)
470 {
471         session->state = SMTP_DATA;
472
473         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA") < 0)
474                 return SM_ERROR;
475         log_print(LOG_PROTOCOL, "SMTP> DATA\n");
476
477         return SM_OK;
478 }
479
480 static gint smtp_send_data(SMTPSession *session)
481 {
482         session->state = SMTP_SEND_DATA;
483
484         session_send_data(SESSION(session), session->send_data,
485                           session->send_data_len);
486
487         return SM_OK;
488 }
489
490 static gint smtp_make_ready(SMTPSession *session)
491 {
492         session->state = SMTP_MAIL_SENT_OK;
493
494         return SM_OK;
495 }
496
497 gint smtp_quit(SMTPSession *session)
498 {
499         session->state = SMTP_QUIT;
500
501         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
502         log_print(LOG_PROTOCOL, "SMTP> QUIT\n");
503
504         return SM_OK;
505 }
506
507 static gint smtp_eom(SMTPSession *session)
508 {
509         session->state = SMTP_EOM;
510
511         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".") < 0)
512                 return SM_ERROR;
513         log_print(LOG_PROTOCOL, "SMTP> . (EOM)\n");
514
515         return SM_OK;
516 }
517
518 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
519 {
520         SMTPSession *smtp_session = SMTP_SESSION(session);
521         gboolean cont = FALSE;
522         gint ret = 0;
523         
524         if (strlen(msg) < 4) {
525                 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
526                 return -1;
527         }
528
529         switch (smtp_session->state) {
530         case SMTP_EHLO:
531         case SMTP_STARTTLS:
532         case SMTP_AUTH:
533         case SMTP_AUTH_PLAIN:
534         case SMTP_AUTH_LOGIN_USER:
535         case SMTP_AUTH_LOGIN_PASS:
536         case SMTP_AUTH_CRAM_MD5:
537                 log_print(LOG_PROTOCOL, "ESMTP< %s\n", msg);
538                 break;
539         default:
540                 log_print(LOG_PROTOCOL, "SMTP< %s\n", msg);
541                 break;
542         }
543
544         if (msg[0] == '5' && msg[1] == '0' &&
545             (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
546                 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
547                 smtp_session->state = SMTP_ERROR;
548                 smtp_session->error_val = SM_ERROR;
549                 g_free(smtp_session->error_msg);
550                 smtp_session->error_msg = g_strdup(msg);
551                 return -1;
552         }
553
554         if (!strncmp(msg, "535", 3)) {
555                 log_warning(LOG_PROTOCOL, _("error occurred on authentication\n"));
556                 smtp_session->state = SMTP_ERROR;
557                 smtp_session->error_val = SM_AUTHFAIL;
558                 g_free(smtp_session->error_msg);
559                 smtp_session->error_msg = g_strdup(msg);
560                 return -1;
561         }
562
563         if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
564                 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
565                 smtp_session->state = SMTP_ERROR;
566                 smtp_session->error_val = SM_ERROR;
567                 g_free(smtp_session->error_msg);
568                 smtp_session->error_msg = g_strdup(msg);
569                 return -1;
570         }
571
572         if (msg[3] == '-')
573                 cont = TRUE;
574         else if (msg[3] != ' ' && msg[3] != '\0') {
575                 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
576                 smtp_session->state = SMTP_ERROR;
577                 smtp_session->error_val = SM_UNRECOVERABLE;
578                 return -1;
579         }
580
581         /* ignore all multiline responses except for EHLO */
582         if (cont && smtp_session->state != SMTP_EHLO)
583                 return session_recv_msg(session);
584
585         switch (smtp_session->state) {
586         case SMTP_READY:
587                 if (strstr(msg, "ESMTP"))
588                         smtp_session->is_esmtp = TRUE;
589         case SMTP_CONNECTED:
590 #ifdef USE_GNUTLS
591                 if (smtp_session->user || session->ssl_type != SSL_NONE ||
592                     smtp_session->is_esmtp)
593 #else
594                 if (smtp_session->user || smtp_session->is_esmtp)
595 #endif
596                         ret = smtp_ehlo(smtp_session);
597                 else
598                         ret = smtp_helo(smtp_session);
599                 break;
600         case SMTP_HELO:
601                 ret = smtp_from(smtp_session);
602                 break;
603         case SMTP_EHLO:
604                 ret = smtp_ehlo_recv(smtp_session, msg);
605                 if (cont == TRUE)
606                         break;
607                 if (smtp_session->max_message_size > 0
608                 && smtp_session->max_message_size < 
609                    smtp_session->send_data_len) {
610                         log_warning(LOG_PROTOCOL, _("Message is too big "
611                               "(Maximum size is %s)\n"),
612                               to_human_readable(
613                                (goffset)(smtp_session->max_message_size)));
614                         smtp_session->state = SMTP_ERROR;
615                         smtp_session->error_val = SM_ERROR;
616                         return -1;
617                 }
618 #ifdef USE_GNUTLS
619                 if (session->ssl_type == SSL_STARTTLS &&
620                     smtp_session->tls_init_done == FALSE) {
621                         ret = smtp_starttls(smtp_session);
622                         break;
623                 }
624 #endif
625                 if (smtp_session->user) {
626                         if (smtp_auth(smtp_session) != SM_OK) {
627 #ifdef USE_GNUTLS
628                                 if (session->ssl_type == SSL_NONE
629                                 &&  smtp_session->tls_init_done == FALSE
630                                 &&  (smtp_session->avail_auth_type & SMTPAUTH_TLS_AVAILABLE))
631                                         ret = smtp_starttls(smtp_session);
632                                 else
633 #endif
634                                         ret = smtp_from(smtp_session);
635                         }
636                 } else
637                         ret = smtp_from(smtp_session);
638                 break;
639         case SMTP_STARTTLS:
640 #ifdef USE_GNUTLS
641                 if (session_start_tls(session) < 0) {
642                         log_warning(LOG_PROTOCOL, _("couldn't start TLS session\n"));
643                         smtp_session->state = SMTP_ERROR;
644                         smtp_session->error_val = SM_ERROR;
645                         return -1;
646                 }
647                 smtp_session->tls_init_done = TRUE;
648                 ret = smtp_ehlo(smtp_session);
649 #endif
650                 break;
651         case SMTP_AUTH:
652                 ret = smtp_auth_recv(smtp_session, msg);
653                 break;
654         case SMTP_AUTH_LOGIN_USER:
655                 ret = smtp_auth_login_user_recv(smtp_session, msg);
656                 break;
657         case SMTP_AUTH_PLAIN:
658         case SMTP_AUTH_LOGIN_PASS:
659         case SMTP_AUTH_CRAM_MD5:
660                 ret = smtp_from(smtp_session);
661                 break;
662         case SMTP_FROM:
663                 if (smtp_session->cur_to)
664                         ret = smtp_rcpt(smtp_session);
665                 break;
666         case SMTP_RCPT:
667                 if (smtp_session->cur_to)
668                         ret = smtp_rcpt(smtp_session);
669                 else
670                         ret = smtp_data(smtp_session);
671                 break;
672         case SMTP_DATA:
673                 ret = smtp_send_data(smtp_session);
674                 break;
675         case SMTP_EOM:
676                 smtp_make_ready(smtp_session);
677                 break;
678         case SMTP_QUIT:
679                 session_disconnect(session);
680                 break;
681         case SMTP_ERROR:
682         default:
683                 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
684                 smtp_session->error_val = SM_ERROR;
685                 return -1;
686         }
687
688         if (cont && ret == SM_OK)
689                 return session_recv_msg(session);
690
691         if (ret != SM_OK)
692                 smtp_session->error_val = SM_ERROR;
693
694         return ret == SM_OK ? 0 : -1;
695 }
696
697 static gint smtp_session_send_data_finished(Session *session, guint len)
698 {
699         return smtp_eom(SMTP_SESSION(session));
700 }