1 /* mutt - text oriented MIME mail user agent
2 * Copyright (C) 2002 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 2005-2009 Brendan Cully <brendan@kublai.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 /* This file contains code for direct SMTP delivery of email messages. */
27 #include "mutt_curses.h"
28 #include "mutt_socket.h"
30 # include "mutt_ssl.h"
33 #include "mutt_sasl.h"
35 #include <sasl/sasl.h>
36 #include <sasl/saslutil.h>
40 #include <netinet/in.h>
44 #define smtp_success(x) ((x)/100 == 2)
45 #define smtp_ready 334
46 #define smtp_continue 354
48 #define smtp_err_read -2
49 #define smtp_err_write -3
50 #define smtp_err_code -4
53 #define SMTPS_PORT 465
55 #define SMTP_AUTH_SUCCESS 0
56 #define SMTP_AUTH_UNAVAIL 1
57 #define SMTP_AUTH_FAIL -1
69 static int smtp_auth (CONNECTION* conn);
70 static int smtp_auth_oauth (CONNECTION* conn);
72 static int smtp_auth_sasl (CONNECTION* conn, const char* mechanisms);
75 static int smtp_fill_account (ACCOUNT* account);
76 static int smtp_open (CONNECTION* conn);
79 static char* AuthMechs = NULL;
80 static unsigned char Capabilities[(CAPMAX + 7)/ 8];
82 static int smtp_code (char *buf, size_t len, int *n)
92 if (mutt_atoi (code, n) < 0)
97 /* Reads a command response from the SMTP server.
99 * 0 on success (2xx code) or continue (354 code)
100 * -1 write error, or any other response code
103 smtp_get_resp (CONNECTION * conn)
110 n = mutt_socket_readln (buf, sizeof (buf), conn);
113 /* read error, or no response code */
114 return smtp_err_read;
117 if (!ascii_strncasecmp ("8BITMIME", buf + 4, 8))
118 mutt_bit_set (Capabilities, EIGHTBITMIME);
119 else if (!ascii_strncasecmp ("AUTH ", buf + 4, 5))
121 mutt_bit_set (Capabilities, AUTH);
123 AuthMechs = safe_strdup (buf + 9);
125 else if (!ascii_strncasecmp ("DSN", buf + 4, 3))
126 mutt_bit_set (Capabilities, DSN);
127 else if (!ascii_strncasecmp ("STARTTLS", buf + 4, 8))
128 mutt_bit_set (Capabilities, STARTTLS);
129 else if (!ascii_strncasecmp ("SMTPUTF8", buf + 4, 8))
130 mutt_bit_set (Capabilities, SMTPUTF8);
132 if (smtp_code (buf, n, &n) < 0)
133 return smtp_err_code;
135 } while (buf[3] == '-');
137 if (smtp_success (n) || n == smtp_continue)
140 mutt_error (_("SMTP session failed: %s"), buf);
145 smtp_rcpt_to (CONNECTION * conn, const ADDRESS * a)
152 /* weed out group mailboxes, since those are for display only */
153 if (!a->mailbox || a->group)
158 if (mutt_bit_isset (Capabilities, DSN) && DsnNotify)
159 snprintf (buf, sizeof (buf), "RCPT TO:<%s> NOTIFY=%s\r\n",
160 a->mailbox, DsnNotify);
162 snprintf (buf, sizeof (buf), "RCPT TO:<%s>\r\n", a->mailbox);
163 if (mutt_socket_write (conn, buf) == -1)
164 return smtp_err_write;
165 if ((r = smtp_get_resp (conn)))
174 smtp_data (CONNECTION * conn, const char *msgfile)
183 fp = fopen (msgfile, "r");
186 mutt_error (_("SMTP session failed: unable to open %s"), msgfile);
191 mutt_progress_init (&progress, _("Sending message..."), MUTT_PROGRESS_SIZE,
194 snprintf (buf, sizeof (buf), "DATA\r\n");
195 if (mutt_socket_write (conn, buf) == -1)
198 return smtp_err_write;
200 if ((r = smtp_get_resp (conn)))
206 while (fgets (buf, sizeof (buf) - 1, fp))
208 buflen = mutt_strlen (buf);
209 term = buflen && buf[buflen-1] == '\n';
210 if (term && (buflen == 1 || buf[buflen - 2] != '\r'))
211 snprintf (buf + buflen - 1, sizeof (buf) - buflen + 1, "\r\n");
214 if (mutt_socket_write_d (conn, ".", -1, MUTT_SOCK_LOG_FULL) == -1)
217 return smtp_err_write;
220 if (mutt_socket_write_d (conn, buf, -1, MUTT_SOCK_LOG_FULL) == -1)
223 return smtp_err_write;
225 mutt_progress_update (&progress, ftell (fp), -1);
227 if (!term && buflen &&
228 mutt_socket_write_d (conn, "\r\n", -1, MUTT_SOCK_LOG_FULL) == -1)
231 return smtp_err_write;
235 /* terminate the message body */
236 if (mutt_socket_write (conn, ".\r\n") == -1)
237 return smtp_err_write;
239 if ((r = smtp_get_resp (conn)))
246 /* Returns 1 if a contains at least one 8-bit character, 0 if none do.
248 static int address_uses_unicode(const char *a)
255 if ((unsigned char) *a & (1<<7))
264 /* Returns 1 if any address in a contains at least one 8-bit
265 * character, 0 if none do.
267 static int addresses_use_unicode(const ADDRESS* a)
271 if (a->mailbox && !a->group && address_uses_unicode(a->mailbox))
280 mutt_smtp_send (const ADDRESS* from, const ADDRESS* to, const ADDRESS* cc,
281 const ADDRESS* bcc, const char *msgfile, int eightbit)
289 /* it might be better to synthesize an envelope from from user and host
290 * but this condition is most likely arrived at accidentally */
292 envfrom = EnvFrom->mailbox;
294 envfrom = from->mailbox;
297 mutt_error (_("No from address given"));
301 if (smtp_fill_account (&account) < 0)
304 if (!(conn = mutt_conn_find (NULL, &account)))
311 /* send our greeting */
312 if (( ret = smtp_open (conn)))
316 /* send the sender's address */
317 ret = snprintf (buf, sizeof (buf), "MAIL FROM:<%s>", envfrom);
318 if (eightbit && mutt_bit_isset (Capabilities, EIGHTBITMIME))
320 safe_strncat (buf, sizeof (buf), " BODY=8BITMIME", 15);
323 if (DsnReturn && mutt_bit_isset (Capabilities, DSN))
324 ret += snprintf (buf + ret, sizeof (buf) - ret, " RET=%s", DsnReturn);
325 if (mutt_bit_isset (Capabilities, SMTPUTF8) &&
326 (address_uses_unicode(envfrom) ||
327 addresses_use_unicode(to) ||
328 addresses_use_unicode(cc) ||
329 addresses_use_unicode(bcc)))
330 ret += snprintf (buf + ret, sizeof (buf) - ret, " SMTPUTF8");
331 safe_strncat (buf, sizeof (buf), "\r\n", 3);
332 if (mutt_socket_write (conn, buf) == -1)
334 ret = smtp_err_write;
337 if ((ret = smtp_get_resp (conn)))
340 /* send the recipient list */
341 if ((ret = smtp_rcpt_to (conn, to)) || (ret = smtp_rcpt_to (conn, cc))
342 || (ret = smtp_rcpt_to (conn, bcc)))
345 /* send the message data */
346 if ((ret = smtp_data (conn, msgfile)))
349 mutt_socket_write (conn, "QUIT\r\n");
356 mutt_socket_close (conn);
358 if (ret == smtp_err_read)
359 mutt_error (_("SMTP session failed: read error"));
360 else if (ret == smtp_err_write)
361 mutt_error (_("SMTP session failed: write error"));
362 else if (ret == smtp_err_code)
363 mutt_error (_("Invalid server response"));
368 static int smtp_fill_account (ACCOUNT* account)
370 static unsigned short SmtpPort = 0;
372 struct servent* service;
378 account->type = MUTT_ACCT_TYPE_SMTP;
380 urlstr = safe_strdup (SmtpUrl);
381 url_parse_ciss (&url, urlstr);
382 if ((url.scheme != U_SMTP && url.scheme != U_SMTPS)
383 || mutt_account_fromurl (account, &url) < 0)
386 mutt_error (_("Invalid SMTP URL: %s"), SmtpUrl);
392 if (url.scheme == U_SMTPS)
393 account->flags |= MUTT_ACCT_SSL;
397 if (account->flags & MUTT_ACCT_SSL)
398 account->port = SMTPS_PORT;
403 service = getservbyname ("smtp", "tcp");
405 SmtpPort = ntohs (service->s_port);
407 SmtpPort = SMTP_PORT;
408 dprint (3, (debugfile, "Using default SMTP port %d\n", SmtpPort));
410 account->port = SmtpPort;
417 static int smtp_helo (CONNECTION* conn)
419 char buf[LONG_STRING];
422 memset (Capabilities, 0, sizeof (Capabilities));
426 /* if TLS or AUTH are requested, use EHLO */
427 if (conn->account.flags & MUTT_ACCT_USER)
430 if (option (OPTSSLFORCETLS) || quadoption (OPT_SSLSTARTTLS) != MUTT_NO)
435 if (!(fqdn = mutt_fqdn (0)))
436 fqdn = NONULL (Hostname);
438 snprintf (buf, sizeof (buf), "%s %s\r\n", Esmtp ? "EHLO" : "HELO", fqdn);
439 /* XXX there should probably be a wrapper in mutt_socket.c that
440 * repeatedly calls conn->write until all data is sent. This
441 * currently doesn't check for a short write.
443 if (mutt_socket_write (conn, buf) == -1)
444 return smtp_err_write;
445 return smtp_get_resp (conn);
448 static int smtp_open (CONNECTION* conn)
452 if (mutt_socket_open (conn))
455 /* get greeting string */
456 if ((rc = smtp_get_resp (conn)))
459 if ((rc = smtp_helo (conn)))
465 else if (option (OPTSSLFORCETLS))
467 else if (mutt_bit_isset (Capabilities, STARTTLS) &&
468 (rc = query_quadoption (OPT_SSLSTARTTLS,
469 _("Secure connection with TLS?"))) == -1)
474 if (mutt_socket_write (conn, "STARTTLS\r\n") < 0)
475 return smtp_err_write;
476 if ((rc = smtp_get_resp (conn)))
479 if (mutt_ssl_starttls (conn))
481 mutt_error (_("Could not negotiate TLS connection"));
486 /* re-EHLO to get authentication mechanisms */
487 if ((rc = smtp_helo (conn)))
492 if (conn->account.flags & MUTT_ACCT_USER)
494 if (!mutt_bit_isset (Capabilities, AUTH))
496 mutt_error (_("SMTP server does not support authentication"));
501 return smtp_auth (conn);
507 static int smtp_auth (CONNECTION* conn)
509 int r = SMTP_AUTH_UNAVAIL;
511 if (SmtpAuthenticators)
513 char* methods = safe_strdup (SmtpAuthenticators);
517 for (method = methods; method; method = delim)
519 delim = strchr (method, ':');
525 dprint (2, (debugfile, "smtp_authenticate: Trying method %s\n", method));
527 if (!strcmp (method, "oauthbearer"))
529 r = smtp_auth_oauth (conn);
534 r = smtp_auth_sasl (conn, method);
536 mutt_error (_("SMTP authentication method %s requires SASL"), method);
541 if (r == SMTP_AUTH_FAIL && delim)
543 mutt_error (_("%s authentication failed, trying next method"), method);
546 else if (r != SMTP_AUTH_UNAVAIL)
555 r = smtp_auth_sasl (conn, AuthMechs);
557 mutt_error (_("SMTP authentication requires SASL"));
559 r = SMTP_AUTH_UNAVAIL;
563 if (r != SMTP_AUTH_SUCCESS)
564 mutt_account_unsetpass (&conn->account);
566 if (r == SMTP_AUTH_FAIL)
568 mutt_error (_("SASL authentication failed"));
571 else if (r == SMTP_AUTH_UNAVAIL)
573 mutt_error (_("No authenticators available"));
577 return r == SMTP_AUTH_SUCCESS ? 0 : -1;
581 static int smtp_auth_sasl (CONNECTION* conn, const char* mechlist)
583 sasl_conn_t* saslconn;
584 sasl_interact_t* interaction = NULL;
586 const char* data = NULL;
592 if (mutt_sasl_client_new (conn, &saslconn) < 0)
593 return SMTP_AUTH_FAIL;
597 rc = sasl_client_start (saslconn, mechlist, &interaction, &data, &len, &mech);
598 if (rc == SASL_INTERACT)
599 mutt_sasl_interact (interaction);
601 while (rc == SASL_INTERACT);
603 if (rc != SASL_OK && rc != SASL_CONTINUE)
605 dprint (2, (debugfile, "smtp_auth_sasl: %s unavailable\n", mech));
606 sasl_dispose (&saslconn);
607 return SMTP_AUTH_UNAVAIL;
610 if (!option(OPTNOCURSES))
611 mutt_message (_("Authenticating (%s)..."), mech);
613 bufsize = ((len * 2) > LONG_STRING) ? (len * 2) : LONG_STRING;
614 buf = safe_malloc (bufsize);
616 snprintf (buf, bufsize, "AUTH %s", mech);
619 safe_strcat (buf, bufsize, " ");
620 if (sasl_encode64 (data, len, buf + mutt_strlen (buf),
621 bufsize - mutt_strlen (buf), &len) != SASL_OK)
623 dprint (1, (debugfile, "smtp_auth_sasl: error base64-encoding client response.\n"));
627 safe_strcat (buf, bufsize, "\r\n");
631 if (mutt_socket_write (conn, buf) < 0)
633 if ((rc = mutt_socket_readln (buf, bufsize, conn)) < 0)
635 if (smtp_code (buf, rc, &rc) < 0)
638 if (rc != smtp_ready)
641 if (sasl_decode64 (buf+4, strlen (buf+4), buf, bufsize - 1, &len) != SASL_OK)
643 dprint (1, (debugfile, "smtp_auth_sasl: error base64-decoding server response.\n"));
649 saslrc = sasl_client_step (saslconn, buf, len, &interaction, &data, &len);
650 if (saslrc == SASL_INTERACT)
651 mutt_sasl_interact (interaction);
653 while (saslrc == SASL_INTERACT);
657 if ((len * 2) > bufsize)
660 safe_realloc (&buf, bufsize);
662 if (sasl_encode64 (data, len, buf, bufsize, &len) != SASL_OK)
664 dprint (1, (debugfile, "smtp_auth_sasl: error base64-encoding client response.\n"));
668 strfcpy (buf + len, "\r\n", bufsize - len);
669 } while (rc == smtp_ready && saslrc != SASL_FAIL);
671 if (smtp_success (rc))
673 mutt_sasl_setup_conn (conn, saslconn);
675 return SMTP_AUTH_SUCCESS;
679 sasl_dispose (&saslconn);
681 return SMTP_AUTH_FAIL;
683 #endif /* USE_SASL */
686 /* smtp_auth_oauth: AUTH=OAUTHBEARER support. See RFC 7628 */
687 static int smtp_auth_oauth (CONNECTION* conn)
690 char* oauthbearer = NULL;
694 mutt_message _("Authenticating (OAUTHBEARER)...");
696 /* We get the access token from the smtp_oauth_refresh_command */
697 oauthbearer = mutt_account_getoauthbearer (&conn->account);
698 if (oauthbearer == NULL)
699 return SMTP_AUTH_FAIL;
701 ilen = strlen (oauthbearer) + 30;
702 ibuf = safe_malloc (ilen);
704 snprintf (ibuf, ilen, "AUTH OAUTHBEARER %s\r\n", oauthbearer);
706 rc = mutt_socket_write (conn, ibuf);
711 return SMTP_AUTH_FAIL;
712 if (smtp_get_resp (conn) != 0)
713 return SMTP_AUTH_FAIL;
715 return SMTP_AUTH_SUCCESS;