Removed SessionMsgType enum and its use, since it is useless.
[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), 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), tmp) < 0) {
201                                 g_free(tmp);
202                                 return SM_ERROR;
203                         }
204                         g_free(tmp);
205                         log_print(LOG_PROTOCOL, "ESMTP> [USERID]\n");
206                 } else {
207                         /* Server rejects AUTH */
208                         if (session_send_msg(SESSION(session), "*") < 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                         gsize challengelen;
221                         guchar hexdigest[33];
222
223                         challenge = g_base64_decode_zero(msg + 4, &challengelen);
224                         log_print(LOG_PROTOCOL, "ESMTP< [Decoded: %s]\n", challenge);
225
226                         g_snprintf(buf, sizeof(buf), "%s", session->pass);
227                         md5_hex_hmac(hexdigest, challenge, challengelen,
228                                      buf, strlen(session->pass));
229                         g_free(challenge);
230
231                         response = g_strdup_printf
232                                 ("%s %s", session->user, hexdigest);
233                         log_print(LOG_PROTOCOL, "ESMTP> [Encoded: %s]\n", response);
234
235                         response64 = g_base64_encode(response, strlen(response));
236                         g_free(response);
237
238                         if (session_send_msg(SESSION(session), response64) < 0) {
239                                 g_free(response64);
240                                 return SM_ERROR;
241                         }
242                         log_print(LOG_PROTOCOL, "ESMTP> %s\n", response64);
243                         g_free(response64);
244                 } else {
245                         /* Server rejects AUTH */
246                         if (session_send_msg(SESSION(session), "*") < 0)
247                                 return SM_ERROR;
248                         log_print(LOG_PROTOCOL, "ESMTP> *\n");
249                 }
250                 break;
251         case SMTPAUTH_DIGEST_MD5:
252         default:
253                 /* stop smtp_auth when no correct authtype */
254                 if (session_send_msg(SESSION(session), "*") < 0)
255                         return SM_ERROR;
256                 log_print(LOG_PROTOCOL, "ESMTP> *\n");
257                 break;
258         }
259
260         return SM_OK;
261 }
262
263 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
264 {
265         gchar *tmp;
266
267         session->state = SMTP_AUTH_LOGIN_PASS;
268
269         if (!strncmp(msg, "334 ", 4)) {
270                 tmp = g_base64_encode(session->pass, strlen(session->pass));
271         } else {
272                 /* Server rejects AUTH */
273                 tmp = g_strdup("*");
274         }
275
276         if (session_send_msg(SESSION(session), tmp) < 0) {
277                 g_free(tmp);
278                 return SM_ERROR;
279         }
280         g_free(tmp);
281
282         log_print(LOG_PROTOCOL, "ESMTP> [PASSWORD]\n");
283
284         return SM_OK;
285 }
286
287 static gint smtp_ehlo(SMTPSession *session)
288 {
289         gchar buf[MESSAGEBUFSIZE];
290
291         session->state = SMTP_EHLO;
292
293         session->avail_auth_type = 0;
294
295         g_snprintf(buf, sizeof(buf), "EHLO %s",
296                    session->hostname ? session->hostname : get_domain_name());
297         if (session_send_msg(SESSION(session), buf) < 0)
298                 return SM_ERROR;
299         log_print(LOG_PROTOCOL, "ESMTP> %s\n", buf);
300
301         return SM_OK;
302 }
303
304 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
305 {
306         if (strncmp(msg, "250", 3) == 0) {
307                 const gchar *p = msg;
308                 p += 3;
309                 if (*p == '-' || *p == ' ') p++;
310                 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
311                         p += 5;
312                         if (strcasestr(p, "PLAIN"))
313                                 session->avail_auth_type |= SMTPAUTH_PLAIN;
314                         if (strcasestr(p, "LOGIN"))
315                                 session->avail_auth_type |= SMTPAUTH_LOGIN;
316                         if (strcasestr(p, "CRAM-MD5"))
317                                 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
318                         if (strcasestr(p, "DIGEST-MD5"))
319                                 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
320                 }
321                 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
322                         p += 5;
323                         session->max_message_size = atoi(p);
324                         session->esmtp_flags |= ESMTP_SIZE;
325                 }
326                 if (g_ascii_strncasecmp(p, "STARTTLS", 8) == 0) {
327                         p += 9;
328                         session->avail_auth_type |= SMTPAUTH_TLS_AVAILABLE;
329                 }
330                 return SM_OK;
331         } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
332             (msg[3] == ' ' || msg[3] == '\0'))
333                 return SM_OK;
334         else if (msg[0] == '5' && msg[1] == '0' &&
335                  (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
336                 return SM_ERROR;
337
338         return SM_ERROR;
339 }
340
341 #ifdef USE_GNUTLS
342 static gint smtp_starttls(SMTPSession *session)
343 {
344         session->state = SMTP_STARTTLS;
345
346         if (session_send_msg(SESSION(session), "STARTTLS") < 0)
347                 return SM_ERROR;
348         log_print(LOG_PROTOCOL, "ESMTP> STARTTLS\n");
349
350         return SM_OK;
351 }
352 #endif
353
354 static gint smtp_auth_cram_md5(SMTPSession *session)
355 {
356         session->state = SMTP_AUTH;
357         session->auth_type = SMTPAUTH_CRAM_MD5;
358
359         if (session_send_msg(SESSION(session), "AUTH CRAM-MD5") < 0)
360                 return SM_ERROR;
361         log_print(LOG_PROTOCOL, "ESMTP> AUTH CRAM-MD5\n");
362
363         return SM_OK;
364 }
365
366 static gint smtp_auth_plain(SMTPSession *session)
367 {
368         gchar buf[MESSAGEBUFSIZE], *b64buf, *out;
369         gint len;
370
371         session->state = SMTP_AUTH_PLAIN;
372         session->auth_type = SMTPAUTH_PLAIN;
373
374         memset(buf, 0, sizeof buf);
375
376         /* "\0user\0password" */
377         len = sprintf(buf, "%c%s%c%s", '\0', session->user, '\0', session->pass);
378         b64buf = g_base64_encode(buf, len);
379         out = g_strconcat("AUTH PLAIN ", b64buf, NULL);
380         g_free(b64buf);
381
382         if (session_send_msg(SESSION(session), out) < 0) {
383                 g_free(out);
384                 return SM_ERROR;
385         }
386
387         g_free(out);
388
389         log_print(LOG_PROTOCOL, "ESMTP> [AUTH PLAIN]\n");
390
391         return SM_OK;
392 }
393
394 static gint smtp_auth_login(SMTPSession *session)
395 {
396         session->state = SMTP_AUTH;
397         session->auth_type = SMTPAUTH_LOGIN;
398
399         if (session_send_msg(SESSION(session), "AUTH LOGIN") < 0)
400                 return SM_ERROR;
401         log_print(LOG_PROTOCOL, "ESMTP> AUTH LOGIN\n");
402
403         return SM_OK;
404 }
405
406 static gint smtp_helo(SMTPSession *session)
407 {
408         gchar buf[MESSAGEBUFSIZE];
409
410         session->state = SMTP_HELO;
411
412         g_snprintf(buf, sizeof(buf), "HELO %s",
413                    session->hostname ? session->hostname : get_domain_name());
414         if (session_send_msg(SESSION(session), buf) < 0)
415                 return SM_ERROR;
416         log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
417
418         return SM_OK;
419 }
420
421 static gint smtp_rcpt(SMTPSession *session)
422 {
423         gchar buf[MESSAGEBUFSIZE];
424         gchar *to;
425
426         cm_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
427
428         session->state = SMTP_RCPT;
429
430         to = (gchar *)session->cur_to->data;
431
432         if (strchr(to, '<'))
433                 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
434         else
435                 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
436         if (session_send_msg(SESSION(session), buf) < 0)
437                 return SM_ERROR;
438         log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
439
440         session->cur_to = session->cur_to->next;
441
442         return SM_OK;
443 }
444
445 static gint smtp_data(SMTPSession *session)
446 {
447         session->state = SMTP_DATA;
448
449         if (session_send_msg(SESSION(session), "DATA") < 0)
450                 return SM_ERROR;
451         log_print(LOG_PROTOCOL, "SMTP> DATA\n");
452
453         return SM_OK;
454 }
455
456 static gint smtp_send_data(SMTPSession *session)
457 {
458         session->state = SMTP_SEND_DATA;
459
460         session_send_data(SESSION(session), session->send_data,
461                           session->send_data_len);
462
463         return SM_OK;
464 }
465
466 static gint smtp_make_ready(SMTPSession *session)
467 {
468         session->state = SMTP_MAIL_SENT_OK;
469
470         return SM_OK;
471 }
472
473 gint smtp_quit(SMTPSession *session)
474 {
475         session->state = SMTP_QUIT;
476
477         session_send_msg(SESSION(session), "QUIT");
478         log_print(LOG_PROTOCOL, "SMTP> QUIT\n");
479
480         return SM_OK;
481 }
482
483 static gint smtp_eom(SMTPSession *session)
484 {
485         session->state = SMTP_EOM;
486
487         if (session_send_msg(SESSION(session), ".") < 0)
488                 return SM_ERROR;
489         log_print(LOG_PROTOCOL, "SMTP> . (EOM)\n");
490
491         return SM_OK;
492 }
493
494 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
495 {
496         SMTPSession *smtp_session = SMTP_SESSION(session);
497         gboolean cont = FALSE;
498         gint ret = 0;
499         
500         if (strlen(msg) < 4) {
501                 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
502                 return -1;
503         }
504
505         switch (smtp_session->state) {
506         case SMTP_EHLO:
507         case SMTP_STARTTLS:
508         case SMTP_AUTH:
509         case SMTP_AUTH_PLAIN:
510         case SMTP_AUTH_LOGIN_USER:
511         case SMTP_AUTH_LOGIN_PASS:
512         case SMTP_AUTH_CRAM_MD5:
513                 log_print(LOG_PROTOCOL, "ESMTP< %s\n", msg);
514                 break;
515         default:
516                 log_print(LOG_PROTOCOL, "SMTP< %s\n", msg);
517                 break;
518         }
519
520         /* ignore all multiline responses except for EHLO */
521         if (msg[3] == '-' && smtp_session->state != SMTP_EHLO)
522                 return session_recv_msg(session);
523
524         if (msg[0] == '5' && msg[1] == '0' &&
525             (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
526                 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
527                 smtp_session->state = SMTP_ERROR;
528                 smtp_session->error_val = SM_ERROR;
529                 g_free(smtp_session->error_msg);
530                 smtp_session->error_msg = g_strdup(msg);
531                 return -1;
532         }
533
534         if (!strncmp(msg, "535", 3)) {
535                 log_warning(LOG_PROTOCOL, _("error occurred on authentication\n"));
536                 smtp_session->state = SMTP_ERROR;
537                 smtp_session->error_val = SM_AUTHFAIL;
538                 g_free(smtp_session->error_msg);
539                 smtp_session->error_msg = g_strdup(msg);
540                 return -1;
541         }
542
543         if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
544                 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
545                 smtp_session->state = SMTP_ERROR;
546                 smtp_session->error_val = SM_ERROR;
547                 g_free(smtp_session->error_msg);
548                 smtp_session->error_msg = g_strdup(msg);
549                 return -1;
550         }
551
552         if (msg[3] == '-')
553                 cont = TRUE;
554         else if (msg[3] != ' ' && msg[3] != '\0') {
555                 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
556                 smtp_session->state = SMTP_ERROR;
557                 smtp_session->error_val = SM_UNRECOVERABLE;
558                 return -1;
559         }
560
561         switch (smtp_session->state) {
562         case SMTP_READY:
563                 if (strstr(msg, "ESMTP"))
564                         smtp_session->is_esmtp = TRUE;
565 #ifdef USE_GNUTLS
566                 if (smtp_session->user || session->ssl_type != SSL_NONE ||
567                     smtp_session->is_esmtp)
568 #else
569                 if (smtp_session->user || smtp_session->is_esmtp)
570 #endif
571                         ret = smtp_ehlo(smtp_session);
572                 else
573                         ret = smtp_helo(smtp_session);
574                 break;
575         case SMTP_HELO:
576                 ret = smtp_from(smtp_session);
577                 break;
578         case SMTP_EHLO:
579                 ret = smtp_ehlo_recv(smtp_session, msg);
580                 if (cont == TRUE)
581                         break;
582                 if (smtp_session->max_message_size > 0
583                 && smtp_session->max_message_size < 
584                    smtp_session->send_data_len) {
585                         log_warning(LOG_PROTOCOL, _("Message is too big "
586                               "(Maximum size is %s)\n"),
587                               to_human_readable(
588                                (goffset)(smtp_session->max_message_size)));
589                         smtp_session->state = SMTP_ERROR;
590                         smtp_session->error_val = SM_ERROR;
591                         return -1;
592                 }
593 #ifdef USE_GNUTLS
594                 if (session->ssl_type == SSL_STARTTLS &&
595                     smtp_session->tls_init_done == FALSE) {
596                         ret = smtp_starttls(smtp_session);
597                         break;
598                 }
599 #endif
600                 if (smtp_session->user) {
601                         if (smtp_auth(smtp_session) != SM_OK) {
602 #ifdef USE_GNUTLS
603                                 if (session->ssl_type == SSL_NONE
604                                 &&  smtp_session->tls_init_done == FALSE
605                                 &&  (smtp_session->avail_auth_type & SMTPAUTH_TLS_AVAILABLE))
606                                         ret = smtp_starttls(smtp_session);
607                                 else
608 #endif
609                                         ret = smtp_from(smtp_session);
610                         }
611                 } else
612                         ret = smtp_from(smtp_session);
613                 break;
614         case SMTP_STARTTLS:
615 #ifdef USE_GNUTLS
616                 if (session_start_tls(session) < 0) {
617                         log_warning(LOG_PROTOCOL, _("couldn't start TLS session\n"));
618                         smtp_session->state = SMTP_ERROR;
619                         smtp_session->error_val = SM_ERROR;
620                         return -1;
621                 }
622                 smtp_session->tls_init_done = TRUE;
623                 ret = smtp_ehlo(smtp_session);
624 #endif
625                 break;
626         case SMTP_AUTH:
627                 ret = smtp_auth_recv(smtp_session, msg);
628                 break;
629         case SMTP_AUTH_LOGIN_USER:
630                 ret = smtp_auth_login_user_recv(smtp_session, msg);
631                 break;
632         case SMTP_AUTH_PLAIN:
633         case SMTP_AUTH_LOGIN_PASS:
634         case SMTP_AUTH_CRAM_MD5:
635                 ret = smtp_from(smtp_session);
636                 break;
637         case SMTP_FROM:
638                 if (smtp_session->cur_to)
639                         ret = smtp_rcpt(smtp_session);
640                 break;
641         case SMTP_RCPT:
642                 if (smtp_session->cur_to)
643                         ret = smtp_rcpt(smtp_session);
644                 else
645                         ret = smtp_data(smtp_session);
646                 break;
647         case SMTP_DATA:
648                 ret = smtp_send_data(smtp_session);
649                 break;
650         case SMTP_EOM:
651                 smtp_make_ready(smtp_session);
652                 break;
653         case SMTP_QUIT:
654                 session_disconnect(session);
655                 break;
656         case SMTP_ERROR:
657         default:
658                 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
659                 smtp_session->error_val = SM_ERROR;
660                 return -1;
661         }
662
663         if (cont && ret == SM_OK)
664                 return session_recv_msg(session);
665
666         if (ret != SM_OK)
667                 smtp_session->error_val = SM_ERROR;
668
669         return ret == SM_OK ? 0 : -1;
670 }
671
672 static gint smtp_session_send_data_finished(Session *session, guint len)
673 {
674         return smtp_eom(SMTP_SESSION(session));
675 }