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