Fix error creation and warning
[claws.git] / src / common / smtp.c
blobc8f22ec9a2e0ba2305b0716d941c13670c32ed63
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 the Claws Mail team and 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 3 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, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
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>
35 #include "smtp.h"
36 #include "md5.h"
37 #include "utils.h"
38 #include "log.h"
40 static void smtp_session_destroy(Session *session);
42 static gint smtp_auth(SMTPSession *session);
43 #ifdef USE_GNUTLS
44 static gint smtp_starttls(SMTPSession *session);
45 #endif
46 #ifdef USE_OAUTH2
47 static gint smtp_auth_oauth2(SMTPSession *session);
48 #endif
49 static gint smtp_auth_cram_md5(SMTPSession *session);
50 static gint smtp_auth_login(SMTPSession *session);
51 static gint smtp_auth_plain(SMTPSession *session);
53 static gint smtp_ehlo(SMTPSession *session);
54 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
56 static gint smtp_helo(SMTPSession *session);
57 static gint smtp_rcpt(SMTPSession *session);
58 static gint smtp_data(SMTPSession *session);
59 static gint smtp_send_data(SMTPSession *session);
60 static gint smtp_make_ready(SMTPSession *session);
61 static gint smtp_eom(SMTPSession *session);
63 static gint smtp_session_recv_msg(Session *session, const gchar *msg);
64 static gint smtp_session_send_data_finished(Session *session, guint len);
67 Session *smtp_session_new(void *prefs_account)
69 SMTPSession *session;
71 session = g_new0(SMTPSession, 1);
73 session_init(SESSION(session), prefs_account, TRUE);
75 SESSION(session)->type = SESSION_SMTP;
77 SESSION(session)->recv_msg = smtp_session_recv_msg;
79 SESSION(session)->recv_data_finished = NULL;
80 SESSION(session)->send_data_finished = smtp_session_send_data_finished;
82 SESSION(session)->destroy = smtp_session_destroy;
84 session->state = SMTP_READY;
86 #ifdef USE_GNUTLS
87 session->tls_init_done = FALSE;
88 #endif
90 session->hostname = NULL;
91 session->user = NULL;
92 session->pass = NULL;
94 session->from = NULL;
95 session->to_list = NULL;
96 session->cur_to = NULL;
98 session->send_data = NULL;
99 session->send_data_len = 0;
101 session->max_message_size = -1;
103 session->avail_auth_type = 0;
104 session->forced_auth_type = 0;
105 session->auth_type = 0;
106 session->esmtp_flags = 0;
108 session->error_val = SM_OK;
109 session->error_msg = NULL;
111 return SESSION(session);
114 static void smtp_session_destroy(Session *session)
116 SMTPSession *smtp_session = SMTP_SESSION(session);
118 g_free(smtp_session->hostname);
119 g_free(smtp_session->user);
120 g_free(smtp_session->pass);
121 g_free(smtp_session->from);
123 g_free(smtp_session->send_data);
125 g_free(smtp_session->error_msg);
128 gint smtp_from(SMTPSession *session)
130 gchar buf[MESSAGEBUFSIZE];
131 gchar *mail_size = NULL;
133 cm_return_val_if_fail(session->from != NULL, SM_ERROR);
135 session->state = SMTP_FROM;
137 if (session->is_esmtp && (session->esmtp_flags & ESMTP_SIZE)!=0)
138 mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
139 else
140 mail_size = g_strdup("");
143 if (strchr(session->from, '<'))
144 g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
145 mail_size);
146 else
147 g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
148 mail_size);
150 g_free(mail_size);
152 if (session_send_msg(SESSION(session), buf) < 0)
153 return SM_ERROR;
154 log_print(LOG_PROTOCOL, "%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
156 return SM_OK;
159 static gint smtp_auth(SMTPSession *session)
162 cm_return_val_if_fail(session->user != NULL, SM_ERROR);
164 session->state = SMTP_AUTH;
166 if ((session->forced_auth_type == SMTPAUTH_CRAM_MD5
167 || session->forced_auth_type == 0)
169 (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0)
170 smtp_auth_cram_md5(session);
171 else if ((session->forced_auth_type == SMTPAUTH_LOGIN
172 || session->forced_auth_type == 0)
174 (session->avail_auth_type & SMTPAUTH_LOGIN) != 0)
175 smtp_auth_login(session);
176 else if ((session->forced_auth_type == SMTPAUTH_PLAIN
177 || session->forced_auth_type == 0)
179 (session->avail_auth_type & SMTPAUTH_PLAIN) != 0)
180 smtp_auth_plain(session);
181 #ifdef USE_OAUTH2
182 else if ((session->forced_auth_type == SMTPAUTH_OAUTH2
183 || session->forced_auth_type == 0)
185 (session->avail_auth_type & SMTPAUTH_OAUTH2) != 0)
186 smtp_auth_oauth2(session);
187 #endif
188 else if (session->forced_auth_type == 0) {
189 log_warning(LOG_PROTOCOL, _("No SMTP AUTH method available\n"));
190 return SM_AUTHFAIL;
191 } else {
192 log_warning(LOG_PROTOCOL, _("Selected SMTP AUTH method not available\n"));
193 return SM_AUTHFAIL;
196 return SM_OK;
199 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
201 gchar buf[MESSAGEBUFSIZE], *tmp;
203 switch (session->auth_type) {
204 case SMTPAUTH_LOGIN:
205 session->state = SMTP_AUTH_LOGIN_USER;
207 if (!strncmp(msg, "334 ", 4)) {
208 tmp = g_base64_encode(session->user, strlen(session->user));
210 if (session_send_msg(SESSION(session), tmp) < 0) {
211 g_free(tmp);
212 return SM_ERROR;
214 g_free(tmp);
215 log_print(LOG_PROTOCOL, "ESMTP> [USERID]\n");
216 } else {
217 /* Server rejects AUTH */
218 if (session_send_msg(SESSION(session), "*") < 0)
219 return SM_ERROR;
220 log_print(LOG_PROTOCOL, "ESMTP> *\n");
222 break;
223 case SMTPAUTH_CRAM_MD5:
224 session->state = SMTP_AUTH_CRAM_MD5;
226 if (!strncmp(msg, "334 ", 4)) {
227 gchar *response;
228 gchar *response64;
229 gchar *challenge;
230 gsize challengelen;
231 guchar hexdigest[33];
233 challenge = g_base64_decode_zero(msg + 4, &challengelen);
234 log_print(LOG_PROTOCOL, "ESMTP< [Decoded: %s]\n", challenge);
236 g_snprintf(buf, sizeof(buf), "%s", session->pass);
237 md5_hex_hmac(hexdigest, challenge, challengelen,
238 buf, strlen(session->pass));
239 g_free(challenge);
241 response = g_strdup_printf
242 ("%s %s", session->user, hexdigest);
243 log_print(LOG_PROTOCOL, "ESMTP> [Encoded: %s]\n", response);
245 response64 = g_base64_encode(response, strlen(response));
246 g_free(response);
248 if (session_send_msg(SESSION(session), response64) < 0) {
249 g_free(response64);
250 return SM_ERROR;
252 log_print(LOG_PROTOCOL, "ESMTP> %s\n", response64);
253 g_free(response64);
254 } else {
255 /* Server rejects AUTH */
256 if (session_send_msg(SESSION(session), "*") < 0)
257 return SM_ERROR;
258 log_print(LOG_PROTOCOL, "ESMTP> *\n");
260 break;
261 case SMTPAUTH_DIGEST_MD5:
262 default:
263 /* stop smtp_auth when no correct authtype */
264 if (session_send_msg(SESSION(session), "*") < 0)
265 return SM_ERROR;
266 log_print(LOG_PROTOCOL, "ESMTP> *\n");
267 break;
270 return SM_OK;
273 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
275 gchar *tmp;
277 session->state = SMTP_AUTH_LOGIN_PASS;
279 if (!strncmp(msg, "334 ", 4)) {
280 tmp = g_base64_encode(session->pass, strlen(session->pass));
281 } else {
282 /* Server rejects AUTH */
283 tmp = g_strdup("*");
286 if (session_send_msg(SESSION(session), tmp) < 0) {
287 g_free(tmp);
288 return SM_ERROR;
290 g_free(tmp);
292 log_print(LOG_PROTOCOL, "ESMTP> [PASSWORD]\n");
294 return SM_OK;
297 static gint smtp_ehlo(SMTPSession *session)
299 gchar buf[MESSAGEBUFSIZE];
301 session->state = SMTP_EHLO;
303 session->avail_auth_type = 0;
305 g_snprintf(buf, sizeof(buf), "EHLO %s",
306 session->hostname ? session->hostname : get_domain_name());
307 if (session_send_msg(SESSION(session), buf) < 0)
308 return SM_ERROR;
309 log_print(LOG_PROTOCOL, "ESMTP> %s\n", buf);
311 return SM_OK;
314 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
316 if (strncmp(msg, "250", 3) == 0) {
317 const gchar *p = msg;
318 p += 3;
319 if (*p == '-' || *p == ' ') p++;
320 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
321 p += 5;
322 if (strcasestr(p, "PLAIN"))
323 session->avail_auth_type |= SMTPAUTH_PLAIN;
324 if (strcasestr(p, "LOGIN"))
325 session->avail_auth_type |= SMTPAUTH_LOGIN;
326 if (strcasestr(p, "CRAM-MD5"))
327 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
328 if (strcasestr(p, "DIGEST-MD5"))
329 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
330 #ifdef USE_GNUTLS
331 if (strcasestr(p, "XOAUTH2"))
332 session->avail_auth_type |= SMTPAUTH_OAUTH2;
333 #endif
335 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
336 p += 5;
337 session->max_message_size = atoi(p);
338 session->esmtp_flags |= ESMTP_SIZE;
340 if (g_ascii_strncasecmp(p, "STARTTLS", 8) == 0) {
341 p += 9;
342 session->avail_auth_type |= SMTPAUTH_TLS_AVAILABLE;
344 return SM_OK;
345 } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
346 (msg[3] == ' ' || msg[3] == '\0'))
347 return SM_OK;
348 else if (msg[0] == '5' && msg[1] == '0' &&
349 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
350 return SM_ERROR;
352 return SM_ERROR;
355 #ifdef USE_GNUTLS
356 static gint smtp_starttls(SMTPSession *session)
358 session->state = SMTP_STARTTLS;
360 if (session_send_msg(SESSION(session), "STARTTLS") < 0)
361 return SM_ERROR;
362 log_print(LOG_PROTOCOL, "ESMTP> STARTTLS\n");
364 return SM_OK;
366 #endif
368 static gint smtp_auth_cram_md5(SMTPSession *session)
370 session->state = SMTP_AUTH;
371 session->auth_type = SMTPAUTH_CRAM_MD5;
373 if (session_send_msg(SESSION(session), "AUTH CRAM-MD5") < 0)
374 return SM_ERROR;
375 log_print(LOG_PROTOCOL, "ESMTP> AUTH CRAM-MD5\n");
377 return SM_OK;
380 static gint smtp_auth_plain(SMTPSession *session)
382 gchar buf[MESSAGEBUFSIZE], *b64buf, *out;
383 gint len;
385 session->state = SMTP_AUTH_PLAIN;
386 session->auth_type = SMTPAUTH_PLAIN;
388 memset(buf, 0, sizeof buf);
390 /* "\0user\0password" */
391 len = sprintf(buf, "%c%s%c%s", '\0', session->user, '\0', session->pass);
392 b64buf = g_base64_encode(buf, len);
393 out = g_strconcat("AUTH PLAIN ", b64buf, NULL);
394 g_free(b64buf);
396 if (session_send_msg(SESSION(session), out) < 0) {
397 g_free(out);
398 return SM_ERROR;
401 g_free(out);
403 log_print(LOG_PROTOCOL, "ESMTP> [AUTH PLAIN]\n");
405 return SM_OK;
408 #ifdef USE_OAUTH2
409 static gint smtp_auth_oauth2(SMTPSession *session)
411 gchar buf[MESSAGEBUFSIZE], *b64buf, *out;
412 gint len;
414 session->state = SMTP_AUTH_OAUTH2;
415 session->auth_type = SMTPAUTH_OAUTH2;
417 memset(buf, 0, sizeof buf);
419 /* "user=" {User} "^Aauth=Bearer " {Access Token} "^A^A"*/
420 /* session->pass contains the OAUTH2 Access Token*/
421 len = sprintf(buf, "user=%s\1auth=Bearer %s\1\1", session->user, session->pass);
422 b64buf = g_base64_encode(buf, len);
423 out = g_strconcat("AUTH XOAUTH2 ", b64buf, NULL);
424 g_free(b64buf);
426 if (session_send_msg(SESSION(session), out) < 0) {
427 g_free(out);
428 return SM_ERROR;
431 g_free(out);
433 log_print(LOG_PROTOCOL, "ESMTP> [AUTH XOAUTH2]\n");
435 return SM_OK;
437 #endif
439 static gint smtp_auth_login(SMTPSession *session)
441 session->state = SMTP_AUTH;
442 session->auth_type = SMTPAUTH_LOGIN;
444 if (session_send_msg(SESSION(session), "AUTH LOGIN") < 0)
445 return SM_ERROR;
446 log_print(LOG_PROTOCOL, "ESMTP> AUTH LOGIN\n");
448 return SM_OK;
451 static gint smtp_helo(SMTPSession *session)
453 gchar buf[MESSAGEBUFSIZE];
455 session->state = SMTP_HELO;
457 g_snprintf(buf, sizeof(buf), "HELO %s",
458 session->hostname ? session->hostname : get_domain_name());
459 if (session_send_msg(SESSION(session), buf) < 0)
460 return SM_ERROR;
461 log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
463 return SM_OK;
466 static gint smtp_rcpt(SMTPSession *session)
468 gchar buf[MESSAGEBUFSIZE];
469 gchar *to;
471 cm_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
473 session->state = SMTP_RCPT;
475 to = (gchar *)session->cur_to->data;
477 if (strchr(to, '<'))
478 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
479 else
480 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
481 if (session_send_msg(SESSION(session), buf) < 0)
482 return SM_ERROR;
483 log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
485 session->cur_to = session->cur_to->next;
487 return SM_OK;
490 static gint smtp_data(SMTPSession *session)
492 session->state = SMTP_DATA;
494 if (session_send_msg(SESSION(session), "DATA") < 0)
495 return SM_ERROR;
496 log_print(LOG_PROTOCOL, "SMTP> DATA\n");
498 return SM_OK;
501 static gint smtp_send_data(SMTPSession *session)
503 session->state = SMTP_SEND_DATA;
505 session_send_data(SESSION(session), session->send_data,
506 session->send_data_len);
508 return SM_OK;
511 static gint smtp_make_ready(SMTPSession *session)
513 session->state = SMTP_MAIL_SENT_OK;
515 return SM_OK;
518 gint smtp_quit(SMTPSession *session)
520 session->state = SMTP_QUIT;
522 session_send_msg(SESSION(session), "QUIT");
523 log_print(LOG_PROTOCOL, "SMTP> QUIT\n");
525 return SM_OK;
528 static gint smtp_eom(SMTPSession *session)
530 session->state = SMTP_EOM;
532 if (session_send_msg(SESSION(session), ".") < 0)
533 return SM_ERROR;
534 log_print(LOG_PROTOCOL, "SMTP> . (EOM)\n");
536 return SM_OK;
539 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
541 SMTPSession *smtp_session = SMTP_SESSION(session);
542 gboolean cont = FALSE;
543 gint ret = 0;
545 if (strlen(msg) < 4) {
546 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
547 return -1;
550 switch (smtp_session->state) {
551 case SMTP_EHLO:
552 case SMTP_STARTTLS:
553 case SMTP_AUTH:
554 case SMTP_AUTH_PLAIN:
555 case SMTP_AUTH_LOGIN_USER:
556 case SMTP_AUTH_LOGIN_PASS:
557 #ifdef USE_GNUTLS
558 case SMTP_AUTH_OAUTH2:
559 #endif
560 case SMTP_AUTH_CRAM_MD5:
561 log_print(LOG_PROTOCOL, "ESMTP< %s\n", msg);
562 break;
563 default:
564 log_print(LOG_PROTOCOL, "SMTP< %s\n", msg);
565 break;
568 /* ignore all multiline responses except for EHLO */
569 if (msg[3] == '-' && smtp_session->state != SMTP_EHLO)
570 return session_recv_msg(session);
572 if (msg[0] == '5' && msg[1] == '0' &&
573 (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
574 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
575 smtp_session->state = SMTP_ERROR;
576 smtp_session->error_val = SM_ERROR;
577 g_free(smtp_session->error_msg);
578 smtp_session->error_msg = g_strdup(msg);
579 return -1;
582 if (!strncmp(msg, "535", 3)) {
583 log_warning(LOG_PROTOCOL, _("error occurred on authentication\n"));
584 smtp_session->state = SMTP_ERROR;
585 smtp_session->error_val = SM_AUTHFAIL;
586 g_free(smtp_session->error_msg);
587 smtp_session->error_msg = g_strdup(msg);
588 return -1;
591 if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
592 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
593 smtp_session->state = SMTP_ERROR;
594 smtp_session->error_val = SM_ERROR;
595 g_free(smtp_session->error_msg);
596 smtp_session->error_msg = g_strdup(msg);
597 return -1;
600 if (msg[3] == '-')
601 cont = TRUE;
602 else if (msg[3] != ' ' && msg[3] != '\0') {
603 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
604 smtp_session->state = SMTP_ERROR;
605 smtp_session->error_val = SM_UNRECOVERABLE;
606 return -1;
609 switch (smtp_session->state) {
610 case SMTP_READY:
611 if (strstr(msg, "ESMTP"))
612 smtp_session->is_esmtp = TRUE;
613 #ifdef USE_GNUTLS
614 if (smtp_session->user || session->ssl_type != SSL_NONE ||
615 smtp_session->is_esmtp)
616 #else
617 if (smtp_session->user || smtp_session->is_esmtp)
618 #endif
619 ret = smtp_ehlo(smtp_session);
620 else
621 ret = smtp_helo(smtp_session);
622 break;
623 case SMTP_HELO:
624 ret = smtp_from(smtp_session);
625 break;
626 case SMTP_EHLO:
627 ret = smtp_ehlo_recv(smtp_session, msg);
628 if (cont == TRUE)
629 break;
630 if (smtp_session->max_message_size > 0
631 && smtp_session->max_message_size <
632 smtp_session->send_data_len) {
633 log_warning(LOG_PROTOCOL, _("Message is too big "
634 "(Maximum size is %s)\n"),
635 to_human_readable(
636 (goffset)(smtp_session->max_message_size)));
637 smtp_session->state = SMTP_ERROR;
638 smtp_session->error_val = SM_ERROR;
639 return -1;
641 #ifdef USE_GNUTLS
642 if (session->ssl_type == SSL_STARTTLS &&
643 smtp_session->tls_init_done == FALSE) {
644 ret = smtp_starttls(smtp_session);
645 break;
647 #endif
648 if (smtp_session->user) {
649 if (smtp_auth(smtp_session) != SM_OK) {
650 #ifdef USE_GNUTLS
651 if (session->ssl_type == SSL_NONE
652 && smtp_session->tls_init_done == FALSE
653 && (smtp_session->avail_auth_type & SMTPAUTH_TLS_AVAILABLE))
654 ret = smtp_starttls(smtp_session);
655 else
656 #endif
657 ret = smtp_from(smtp_session);
659 } else
660 ret = smtp_from(smtp_session);
661 break;
662 case SMTP_STARTTLS:
663 #ifdef USE_GNUTLS
664 if (session_start_tls(session) < 0) {
665 log_warning(LOG_PROTOCOL, _("couldn't start STARTTLS session\n"));
666 smtp_session->state = SMTP_ERROR;
667 smtp_session->error_val = SM_ERROR;
668 return -1;
670 smtp_session->tls_init_done = TRUE;
671 ret = smtp_ehlo(smtp_session);
672 #endif
673 break;
674 case SMTP_AUTH:
675 ret = smtp_auth_recv(smtp_session, msg);
676 break;
677 case SMTP_AUTH_LOGIN_USER:
678 ret = smtp_auth_login_user_recv(smtp_session, msg);
679 break;
680 case SMTP_AUTH_PLAIN:
681 case SMTP_AUTH_LOGIN_PASS:
682 #ifdef USE_GNUTLS
683 case SMTP_AUTH_OAUTH2:
684 #endif
685 case SMTP_AUTH_CRAM_MD5:
686 ret = smtp_from(smtp_session);
687 break;
688 case SMTP_FROM:
689 if (smtp_session->cur_to)
690 ret = smtp_rcpt(smtp_session);
691 break;
692 case SMTP_RCPT:
693 if (smtp_session->cur_to)
694 ret = smtp_rcpt(smtp_session);
695 else
696 ret = smtp_data(smtp_session);
697 break;
698 case SMTP_DATA:
699 ret = smtp_send_data(smtp_session);
700 break;
701 case SMTP_EOM:
702 smtp_make_ready(smtp_session);
703 break;
704 case SMTP_QUIT:
705 session_disconnect(session);
706 break;
707 case SMTP_ERROR:
708 default:
709 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
710 smtp_session->error_val = SM_ERROR;
711 return -1;
714 if (cont && ret == SM_OK)
715 return session_recv_msg(session);
717 if (ret != SM_OK)
718 smtp_session->error_val = SM_ERROR;
720 return ret == SM_OK ? 0 : -1;
723 static gint smtp_session_send_data_finished(Session *session, guint len)
725 return smtp_eom(SMTP_SESSION(session));