NeoMutt  2019-12-07-60-g0cfa53
Teaching an old dog new tricks
DOXYGEN
smtp.c File Reference

Send email to an SMTP server. More...

#include "config.h"
#include <netdb.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt/mutt.h"
#include "address/lib.h"
#include "email/lib.h"
#include "conn/conn.h"
#include "smtp.h"
#include "globals.h"
#include "mutt_account.h"
#include "mutt_socket.h"
#include "progress.h"
#include "sendlib.h"
#include "config/lib.h"
#include "mutt.h"
#include <sasl/sasl.h>
#include <sasl/saslutil.h>
#include "options.h"
+ Include dependency graph for smtp.c:

Go to the source code of this file.

Macros

#define smtp_success(x)   ((x) / 100 == 2)
 
#define SMTP_READY   334
 
#define SMTP_CONTINUE   354
 
#define SMTP_ERR_READ   -2
 
#define SMTP_ERR_WRITE   -3
 
#define SMTP_ERR_CODE   -4
 
#define SMTP_PORT   25
 
#define SMTPS_PORT   465
 
#define SMTP_AUTH_SUCCESS   0
 
#define SMTP_AUTH_UNAVAIL   1
 
#define SMTP_AUTH_FAIL   -1
 
#define SMTP_CAP_NO_FLAGS   0
 No flags are set. More...
 
#define SMTP_CAP_STARTTLS   (1 << 0)
 Server supports STARTTLS command. More...
 
#define SMTP_CAP_AUTH   (1 << 1)
 Server supports AUTH command. More...
 
#define SMTP_CAP_DSN   (1 << 2)
 Server supports Delivery Status Notification. More...
 
#define SMTP_CAP_EIGHTBITMIME   (1 << 3)
 Server supports 8-bit MIME content. More...
 
#define SMTP_CAP_SMTPUTF8   (1 << 4)
 Server accepts UTF-8 strings. More...
 
#define SMTP_CAP_ALL   ((1 << 5) - 1)
 

Typedefs

typedef uint8_t SmtpCapFlags
 typedef SmtpCapFlags - SMTP server capabilities More...
 

Functions

static bool valid_smtp_code (char *buf, size_t buflen, int *n)
 Is the is a valid SMTP return code? More...
 
static int smtp_get_resp (struct Connection *conn)
 Read a command response from the SMTP server. More...
 
static int smtp_rcpt_to (struct Connection *conn, const struct AddressList *al)
 Set the recipient to an Address. More...
 
static int smtp_data (struct Connection *conn, const char *msgfile)
 Send data to an SMTP server. More...
 
static bool address_uses_unicode (const char *a)
 Do any addresses use Unicode. More...
 
static bool addresses_use_unicode (const struct AddressList *al)
 Do any of a list of addresses use Unicode. More...
 
static int smtp_fill_account (struct ConnAccount *account)
 Create ConnAccount object from SMTP Url. More...
 
static int smtp_helo (struct Connection *conn, bool esmtp)
 Say hello to an SMTP Server. More...
 
static int smtp_auth_sasl (struct Connection *conn, const char *mechlist)
 Authenticate using SASL. More...
 
static int smtp_auth_oauth (struct Connection *conn)
 Authenticate an SMTP connection using OAUTHBEARER. More...
 
static int smtp_auth_plain (struct Connection *conn)
 Authenticate using plain text. More...
 
static int smtp_auth (struct Connection *conn)
 Authenticate to an SMTP server. More...
 
static int smtp_open (struct Connection *conn, bool esmtp)
 Open an SMTP Connection. More...
 
int mutt_smtp_send (const struct AddressList *from, const struct AddressList *to, const struct AddressList *cc, const struct AddressList *bcc, const char *msgfile, bool eightbit)
 Send a message using SMTP. More...
 

Variables

struct SlistC_SmtpAuthenticators
 Config: (smtp) List of allowed authentication methods. More...
 
static char * AuthMechs = NULL
 
static SmtpCapFlags Capabilities
 

Detailed Description

Send email to an SMTP server.

Authors
  • Michael R. Elkins
  • Brendan Cully
  • Pietro Cerutti

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file smtp.c.

Macro Definition Documentation

◆ smtp_success

#define smtp_success (   x)    ((x) / 100 == 2)

Definition at line 65 of file smtp.c.

◆ SMTP_READY

#define SMTP_READY   334

Definition at line 66 of file smtp.c.

◆ SMTP_CONTINUE

#define SMTP_CONTINUE   354

Definition at line 67 of file smtp.c.

◆ SMTP_ERR_READ

#define SMTP_ERR_READ   -2

Definition at line 69 of file smtp.c.

◆ SMTP_ERR_WRITE

#define SMTP_ERR_WRITE   -3

Definition at line 70 of file smtp.c.

◆ SMTP_ERR_CODE

#define SMTP_ERR_CODE   -4

Definition at line 71 of file smtp.c.

◆ SMTP_PORT

#define SMTP_PORT   25

Definition at line 73 of file smtp.c.

◆ SMTPS_PORT

#define SMTPS_PORT   465

Definition at line 74 of file smtp.c.

◆ SMTP_AUTH_SUCCESS

#define SMTP_AUTH_SUCCESS   0

Definition at line 76 of file smtp.c.

◆ SMTP_AUTH_UNAVAIL

#define SMTP_AUTH_UNAVAIL   1

Definition at line 77 of file smtp.c.

◆ SMTP_AUTH_FAIL

#define SMTP_AUTH_FAIL   -1

Definition at line 78 of file smtp.c.

◆ SMTP_CAP_NO_FLAGS

#define SMTP_CAP_NO_FLAGS   0

No flags are set.

Definition at line 85 of file smtp.c.

◆ SMTP_CAP_STARTTLS

#define SMTP_CAP_STARTTLS   (1 << 0)

Server supports STARTTLS command.

Definition at line 86 of file smtp.c.

◆ SMTP_CAP_AUTH

#define SMTP_CAP_AUTH   (1 << 1)

Server supports AUTH command.

Definition at line 87 of file smtp.c.

◆ SMTP_CAP_DSN

#define SMTP_CAP_DSN   (1 << 2)

Server supports Delivery Status Notification.

Definition at line 88 of file smtp.c.

◆ SMTP_CAP_EIGHTBITMIME

#define SMTP_CAP_EIGHTBITMIME   (1 << 3)

Server supports 8-bit MIME content.

Definition at line 89 of file smtp.c.

◆ SMTP_CAP_SMTPUTF8

#define SMTP_CAP_SMTPUTF8   (1 << 4)

Server accepts UTF-8 strings.

Definition at line 90 of file smtp.c.

◆ SMTP_CAP_ALL

#define SMTP_CAP_ALL   ((1 << 5) - 1)

Definition at line 92 of file smtp.c.

Typedef Documentation

◆ SmtpCapFlags

typedef uint8_t SmtpCapFlags

typedef SmtpCapFlags - SMTP server capabilities

Flags, e.g. SMTP_CAP_STARTTLS

Definition at line 84 of file smtp.c.

Function Documentation

◆ valid_smtp_code()

static bool valid_smtp_code ( char *  buf,
size_t  buflen,
int *  n 
)
static

Is the is a valid SMTP return code?

Parameters
[in]bufString to check
[in]buflenLength of string
[out]nNumeric value of code
Return values
trueValid number

Definition at line 105 of file smtp.c.

106 {
107  char code[4];
108 
109  if (buflen < 4)
110  return false;
111  code[0] = buf[0];
112  code[1] = buf[1];
113  code[2] = buf[2];
114  code[3] = '\0';
115  if (mutt_str_atoi(code, n) < 0)
116  return false;
117  return true;
118 }
int mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: string.c:262
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_get_resp()

static int smtp_get_resp ( struct Connection conn)
static

Read a command response from the SMTP server.

Parameters
connSMTP connection
Return values
0Success (2xx code) or continue (354 code)
-1Write error, or any other response code

Definition at line 126 of file smtp.c.

127 {
128  int n;
129  char buf[1024];
130 
131  do
132  {
133  n = mutt_socket_readln(buf, sizeof(buf), conn);
134  if (n < 4)
135  {
136  /* read error, or no response code */
137  return SMTP_ERR_READ;
138  }
139  const char *s = buf + 4; /* Skip the response code and the space/dash */
140  size_t plen;
141 
142  if (mutt_str_startswith(s, "8BITMIME", CASE_IGNORE))
144  else if ((plen = mutt_str_startswith(s, "AUTH ", CASE_IGNORE)))
145  {
147  FREE(&AuthMechs);
148  AuthMechs = mutt_str_strdup(s + plen);
149  }
150  else if (mutt_str_startswith(s, "DSN", CASE_IGNORE))
152  else if (mutt_str_startswith(s, "STARTTLS", CASE_IGNORE))
154  else if (mutt_str_startswith(s, "SMTPUTF8", CASE_IGNORE))
156 
157  if (!valid_smtp_code(buf, n, &n))
158  return SMTP_ERR_CODE;
159 
160  } while (buf[3] == '-');
161 
162  if (smtp_success(n) || (n == SMTP_CONTINUE))
163  return 0;
164 
165  mutt_error(_("SMTP session failed: %s"), buf);
166  return -1;
167 }
#define SMTP_CAP_STARTTLS
Server supports STARTTLS command.
Definition: smtp.c:86
static size_t plen
Length of cached packet.
Definition: pgppacket.c:39
#define SMTP_CAP_SMTPUTF8
Server accepts UTF-8 strings.
Definition: smtp.c:90
#define _(a)
Definition: message.h:28
static SmtpCapFlags Capabilities
Definition: smtp.c:96
Ignore case when comparing strings.
Definition: string2.h:68
#define SMTP_ERR_READ
Definition: smtp.c:69
#define smtp_success(x)
Definition: smtp.c:65
size_t mutt_str_startswith(const char *str, const char *prefix, enum CaseSensitivity cs)
Check whether a string starts with a prefix.
Definition: string.c:168
#define mutt_socket_readln(buf, buflen, conn)
Definition: mutt_socket.h:37
#define SMTP_ERR_CODE
Definition: smtp.c:71
static bool valid_smtp_code(char *buf, size_t buflen, int *n)
Is the is a valid SMTP return code?
Definition: smtp.c:105
char * mutt_str_strdup(const char *str)
Copy a string, safely.
Definition: string.c:380
#define mutt_error(...)
Definition: logging.h:84
#define SMTP_CONTINUE
Definition: smtp.c:67
#define SMTP_CAP_EIGHTBITMIME
Server supports 8-bit MIME content.
Definition: smtp.c:89
#define FREE(x)
Definition: memory.h:40
#define SMTP_CAP_AUTH
Server supports AUTH command.
Definition: smtp.c:87
static char * AuthMechs
Definition: smtp.c:95
#define SMTP_CAP_DSN
Server supports Delivery Status Notification.
Definition: smtp.c:88
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_rcpt_to()

static int smtp_rcpt_to ( struct Connection conn,
const struct AddressList *  al 
)
static

Set the recipient to an Address.

Parameters
connServer Connection
alAddressList to use
Return values
0Success
<0Error, e.g. SMTP_ERR_WRITE

Definition at line 176 of file smtp.c.

177 {
178  if (!al)
179  return 0;
180 
181  struct Address *a = NULL;
182  TAILQ_FOREACH(a, al, entries)
183  {
184  /* weed out group mailboxes, since those are for display only */
185  if (!a->mailbox || a->group)
186  {
187  continue;
188  }
189  char buf[1024];
191  snprintf(buf, sizeof(buf), "RCPT TO:<%s> NOTIFY=%s\r\n", a->mailbox, C_DsnNotify);
192  else
193  snprintf(buf, sizeof(buf), "RCPT TO:<%s>\r\n", a->mailbox);
194  if (mutt_socket_send(conn, buf) == -1)
195  return SMTP_ERR_WRITE;
196  int rc = smtp_get_resp(conn);
197  if (rc != 0)
198  return rc;
199  }
200 
201  return 0;
202 }
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:718
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
An email address.
Definition: address.h:34
char * mailbox
Mailbox and host address.
Definition: address.h:37
static SmtpCapFlags Capabilities
Definition: smtp.c:96
#define SMTP_ERR_WRITE
Definition: smtp.c:70
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:126
bool group
Group mailbox?
Definition: address.h:38
WHERE char * C_DsnNotify
Config: Request notification for message delivery or delay.
Definition: globals.h:107
#define SMTP_CAP_DSN
Server supports Delivery Status Notification.
Definition: smtp.c:88
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_data()

static int smtp_data ( struct Connection conn,
const char *  msgfile 
)
static

Send data to an SMTP server.

Parameters
connSMTP Connection
msgfileFilename containing data
Return values
0Success
<0Error, e.g. SMTP_ERR_WRITE

Definition at line 211 of file smtp.c.

212 {
213  char buf[1024];
214  struct Progress progress;
215  struct stat st;
216  int rc, term = 0;
217  size_t buflen = 0;
218 
219  FILE *fp = fopen(msgfile, "r");
220  if (!fp)
221  {
222  mutt_error(_("SMTP session failed: unable to open %s"), msgfile);
223  return -1;
224  }
225  stat(msgfile, &st);
226  unlink(msgfile);
227  mutt_progress_init(&progress, _("Sending message..."), MUTT_PROGRESS_NET, st.st_size);
228 
229  snprintf(buf, sizeof(buf), "DATA\r\n");
230  if (mutt_socket_send(conn, buf) == -1)
231  {
232  mutt_file_fclose(&fp);
233  return SMTP_ERR_WRITE;
234  }
235  rc = smtp_get_resp(conn);
236  if (rc != 0)
237  {
238  mutt_file_fclose(&fp);
239  return rc;
240  }
241 
242  while (fgets(buf, sizeof(buf) - 1, fp))
243  {
244  buflen = mutt_str_strlen(buf);
245  term = buflen && buf[buflen - 1] == '\n';
246  if (term && ((buflen == 1) || (buf[buflen - 2] != '\r')))
247  snprintf(buf + buflen - 1, sizeof(buf) - buflen + 1, "\r\n");
248  if (buf[0] == '.')
249  {
250  if (mutt_socket_send_d(conn, ".", MUTT_SOCK_LOG_FULL) == -1)
251  {
252  mutt_file_fclose(&fp);
253  return SMTP_ERR_WRITE;
254  }
255  }
256  if (mutt_socket_send_d(conn, buf, MUTT_SOCK_LOG_FULL) == -1)
257  {
258  mutt_file_fclose(&fp);
259  return SMTP_ERR_WRITE;
260  }
261  mutt_progress_update(&progress, ftell(fp), -1);
262  }
263  if (!term && buflen && (mutt_socket_send_d(conn, "\r\n", MUTT_SOCK_LOG_FULL) == -1))
264  {
265  mutt_file_fclose(&fp);
266  return SMTP_ERR_WRITE;
267  }
268  mutt_file_fclose(&fp);
269 
270  /* terminate the message body */
271  if (mutt_socket_send(conn, ".\r\n") == -1)
272  return SMTP_ERR_WRITE;
273 
274  rc = smtp_get_resp(conn);
275  if (rc != 0)
276  return rc;
277 
278  return 0;
279 }
Progress tracks bytes, according to C_NetInc.
Definition: progress.h:43
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
#define _(a)
Definition: message.h:28
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
#define MUTT_SOCK_LOG_FULL
Definition: mutt_socket.h:32
A progress bar.
Definition: progress.h:49
void mutt_progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition: progress.c:212
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
#define mutt_socket_send_d(conn, buf, dbg)
Definition: mutt_socket.h:39
#define SMTP_ERR_WRITE
Definition: smtp.c:70
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:126
void mutt_progress_init(struct Progress *progress, const char *msg, enum ProgressType type, size_t size)
Set up a progress bar.
Definition: progress.c:153
#define mutt_error(...)
Definition: logging.h:84
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ address_uses_unicode()

static bool address_uses_unicode ( const char *  a)
static

Do any addresses use Unicode.

Parameters
aAddress list to check
Return values
trueif any of the string of addresses use 8-bit characters

Definition at line 286 of file smtp.c.

287 {
288  if (!a)
289  return false;
290 
291  while (*a)
292  {
293  if ((unsigned char) *a & (1 << 7))
294  return true;
295  a++;
296  }
297 
298  return false;
299 }
+ Here is the caller graph for this function:

◆ addresses_use_unicode()

static bool addresses_use_unicode ( const struct AddressList *  al)
static

Do any of a list of addresses use Unicode.

Parameters
alAddress list to check
Return values
trueif any use 8-bit characters

Definition at line 306 of file smtp.c.

307 {
308  if (!al)
309  {
310  return false;
311  }
312 
313  struct Address *a = NULL;
314  TAILQ_FOREACH(a, al, entries)
315  {
316  if (a->mailbox && !a->group && address_uses_unicode(a->mailbox))
317  return true;
318  }
319  return false;
320 }
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:718
An email address.
Definition: address.h:34
char * mailbox
Mailbox and host address.
Definition: address.h:37
static bool address_uses_unicode(const char *a)
Do any addresses use Unicode.
Definition: smtp.c:286
bool group
Group mailbox?
Definition: address.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_fill_account()

static int smtp_fill_account ( struct ConnAccount account)
static

Create ConnAccount object from SMTP Url.

Parameters
accountConnAccount to populate
Return values
0Success
-1Error

Definition at line 328 of file smtp.c.

329 {
330  account->flags = 0;
331  account->port = 0;
332  account->type = MUTT_ACCT_TYPE_SMTP;
333 
334  struct Url *url = url_parse(C_SmtpUrl);
335  if (!url || ((url->scheme != U_SMTP) && (url->scheme != U_SMTPS)) ||
336  !url->host || (mutt_account_fromurl(account, url) < 0))
337  {
338  url_free(&url);
339  mutt_error(_("Invalid SMTP URL: %s"), C_SmtpUrl);
340  return -1;
341  }
342 
343  if (url->scheme == U_SMTPS)
344  account->flags |= MUTT_ACCT_SSL;
345 
346  if (account->port == 0)
347  {
348  if (account->flags & MUTT_ACCT_SSL)
349  account->port = SMTPS_PORT;
350  else
351  {
352  static unsigned short SmtpPort = 0;
353  if (SmtpPort == 0)
354  {
355  struct servent *service = getservbyname("smtp", "tcp");
356  if (service)
357  SmtpPort = ntohs(service->s_port);
358  else
359  SmtpPort = SMTP_PORT;
360  mutt_debug(LL_DEBUG3, "Using default SMTP port %d\n", SmtpPort);
361  }
362  account->port = SmtpPort;
363  }
364  }
365 
366  url_free(&url);
367  return 0;
368 }
WHERE char * C_SmtpUrl
Config: (smtp) Url of the SMTP server.
Definition: globals.h:142
A parsed URL proto://user:password@host:port/path?a=1&b=2
Definition: url.h:66
enum UrlScheme scheme
Scheme, e.g. U_SMTPS.
Definition: url.h:68
#define _(a)
Definition: message.h:28
#define SMTP_PORT
Definition: smtp.c:73
unsigned short port
Definition: connaccount.h:37
Url is smtps://.
Definition: url.h:43
unsigned char type
Connection type, e.g. MUTT_ACCT_TYPE_IMAP.
Definition: connaccount.h:38
char * host
Host.
Definition: url.h:71
#define mutt_error(...)
Definition: logging.h:84
void url_free(struct Url **u)
Free the contents of a URL.
Definition: url.c:288
Url is smtp://.
Definition: url.h:42
#define MUTT_ACCT_SSL
Account uses SSL/TLS.
Definition: mutt_account.h:64
Smtp Account.
Definition: mutt_account.h:54
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
#define SMTPS_PORT
Definition: smtp.c:74
int mutt_account_fromurl(struct ConnAccount *account, const struct Url *url)
Fill ConnAccount with information from url.
Definition: mutt_account.c:109
Log at debug level 3.
Definition: logging.h:42
MuttAccountFlags flags
Which fields are initialised, e.g. MUTT_ACCT_USER.
Definition: connaccount.h:39
struct Url * url_parse(const char *src)
Fill in Url.
Definition: url.c:161
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_helo()

static int smtp_helo ( struct Connection conn,
bool  esmtp 
)
static

Say hello to an SMTP Server.

Parameters
connSMTP Connection
esmtpIf true, use ESMTP
Return values
0Success
<0Error, e.g. SMTP_ERR_WRITE

Definition at line 377 of file smtp.c.

378 {
379  Capabilities = 0;
380 
381  if (!esmtp)
382  {
383  /* if TLS or AUTH are requested, use EHLO */
384  if (conn->account.flags & MUTT_ACCT_USER)
385  esmtp = true;
386 #ifdef USE_SSL
387  if (C_SslForceTls || (C_SslStarttls != MUTT_NO))
388  esmtp = true;
389 #endif
390  }
391 
392  const char *fqdn = mutt_fqdn(false);
393  if (!fqdn)
394  fqdn = NONULL(ShortHostname);
395 
396  char buf[1024];
397  snprintf(buf, sizeof(buf), "%s %s\r\n", esmtp ? "EHLO" : "HELO", fqdn);
398  /* XXX there should probably be a wrapper in mutt_socket.c that
399  * repeatedly calls conn->write until all data is sent. This
400  * currently doesn't check for a short write. */
401  if (mutt_socket_send(conn, buf) == -1)
402  return SMTP_ERR_WRITE;
403  return smtp_get_resp(conn);
404 }
#define NONULL(x)
Definition: string2.h:37
struct ConnAccount account
Definition: connection.h:36
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
WHERE bool C_SslForceTls
Config: (ssl) Require TLS encryption for all connections.
Definition: globals.h:234
static SmtpCapFlags Capabilities
Definition: smtp.c:96
#define SMTP_ERR_WRITE
Definition: smtp.c:70
User answered &#39;No&#39;, or assume &#39;No&#39;.
Definition: quad.h:39
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:126
const char * mutt_fqdn(bool may_hide_host)
Get the Fully-Qualified Domain Name.
Definition: sendlib.c:2531
WHERE char * ShortHostname
Short version of the hostname.
Definition: globals.h:50
WHERE unsigned char C_SslStarttls
Config: (ssl) Use STARTTLS on servers advertising the capability.
Definition: globals.h:186
MuttAccountFlags flags
Which fields are initialised, e.g. MUTT_ACCT_USER.
Definition: connaccount.h:39
#define MUTT_ACCT_USER
User field has been set.
Definition: mutt_account.h:61
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_auth_sasl()

static int smtp_auth_sasl ( struct Connection conn,
const char *  mechlist 
)
static

Authenticate using SASL.

Parameters
connSMTP Connection
mechlistList of mechanisms to use
Return values
0Success
<0Error, e.g. SMTP_AUTH_FAIL

Definition at line 414 of file smtp.c.

415 {
416  sasl_conn_t *saslconn = NULL;
417  sasl_interact_t *interaction = NULL;
418  const char *mech = NULL;
419  const char *data = NULL;
420  unsigned int len;
421  char *buf = NULL;
422  size_t bufsize = 0;
423  int rc, saslrc;
424 
425  if (mutt_sasl_client_new(conn, &saslconn) < 0)
426  return SMTP_AUTH_FAIL;
427 
428  do
429  {
430  rc = sasl_client_start(saslconn, mechlist, &interaction, &data, &len, &mech);
431  if (rc == SASL_INTERACT)
432  mutt_sasl_interact(interaction);
433  } while (rc == SASL_INTERACT);
434 
435  if ((rc != SASL_OK) && (rc != SASL_CONTINUE))
436  {
437  mutt_debug(LL_DEBUG2, "%s unavailable\n", mech);
438  sasl_dispose(&saslconn);
439  return SMTP_AUTH_UNAVAIL;
440  }
441 
442  if (!OptNoCurses)
443  mutt_message(_("Authenticating (%s)..."), mech);
444 
445  bufsize = MAX((len * 2), 1024);
446  buf = mutt_mem_malloc(bufsize);
447 
448  snprintf(buf, bufsize, "AUTH %s", mech);
449  if (len)
450  {
451  mutt_str_strcat(buf, bufsize, " ");
452  if (sasl_encode64(data, len, buf + mutt_str_strlen(buf),
453  bufsize - mutt_str_strlen(buf), &len) != SASL_OK)
454  {
455  mutt_debug(LL_DEBUG1, "#1 error base64-encoding client response\n");
456  goto fail;
457  }
458  }
459  mutt_str_strcat(buf, bufsize, "\r\n");
460 
461  do
462  {
463  if (mutt_socket_send(conn, buf) < 0)
464  goto fail;
465  rc = mutt_socket_readln_d(buf, bufsize, conn, MUTT_SOCK_LOG_FULL);
466  if (rc < 0)
467  goto fail;
468  if (!valid_smtp_code(buf, rc, &rc))
469  goto fail;
470 
471  if (rc != SMTP_READY)
472  break;
473 
474  if (sasl_decode64(buf + 4, strlen(buf + 4), buf, bufsize - 1, &len) != SASL_OK)
475  {
476  mutt_debug(LL_DEBUG1, "error base64-decoding server response\n");
477  goto fail;
478  }
479 
480  do
481  {
482  saslrc = sasl_client_step(saslconn, buf, len, &interaction, &data, &len);
483  if (saslrc == SASL_INTERACT)
484  mutt_sasl_interact(interaction);
485  } while (saslrc == SASL_INTERACT);
486 
487  if (len)
488  {
489  if ((len * 2) > bufsize)
490  {
491  bufsize = len * 2;
492  mutt_mem_realloc(&buf, bufsize);
493  }
494  if (sasl_encode64(data, len, buf, bufsize, &len) != SASL_OK)
495  {
496  mutt_debug(LL_DEBUG1, "#2 error base64-encoding client response\n");
497  goto fail;
498  }
499  }
500  mutt_str_strfcpy(buf + len, "\r\n", bufsize - len);
501  } while (rc == SMTP_READY && saslrc != SASL_FAIL);
502 
503  if (smtp_success(rc))
504  {
505  mutt_sasl_setup_conn(conn, saslconn);
506  FREE(&buf);
507  return SMTP_AUTH_SUCCESS;
508  }
509 
510 fail:
511  sasl_dispose(&saslconn);
512  FREE(&buf);
513  return SMTP_AUTH_FAIL;
514 }
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
#define mutt_message(...)
Definition: logging.h:83
#define _(a)
Definition: message.h:28
int mutt_sasl_client_new(struct Connection *conn, sasl_conn_t **saslconn)
Wrapper for sasl_client_new()
Definition: sasl.c:534
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:46
#define SMTP_AUTH_SUCCESS
Definition: smtp.c:76
void mutt_sasl_setup_conn(struct Connection *conn, sasl_conn_t *saslconn)
Set up an SASL connection.
Definition: sasl.c:678
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
#define MUTT_SOCK_LOG_FULL
Definition: mutt_socket.h:32
#define MAX(a, b)
Definition: memory.h:30
Log at debug level 2.
Definition: logging.h:41
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
size_t mutt_str_strfcpy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:750
#define SMTP_READY
Definition: smtp.c:66
#define smtp_success(x)
Definition: smtp.c:65
char * mutt_str_strcat(char *buf, size_t buflen, const char *s)
Concatenate two strings.
Definition: string.c:395
#define SMTP_AUTH_UNAVAIL
Definition: smtp.c:77
Log at debug level 1.
Definition: logging.h:40
static bool valid_smtp_code(char *buf, size_t buflen, int *n)
Is the is a valid SMTP return code?
Definition: smtp.c:105
#define FREE(x)
Definition: memory.h:40
int mutt_socket_readln_d(char *buf, size_t buflen, struct Connection *conn, int dbg)
Read a line from a socket.
Definition: socket.c:244
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
#define SMTP_AUTH_FAIL
Definition: smtp.c:78
int mutt_sasl_interact(sasl_interact_t *interaction)
Perform an SASL interaction with the user.
Definition: sasl.c:645
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_auth_oauth()

static int smtp_auth_oauth ( struct Connection conn)
static

Authenticate an SMTP connection using OAUTHBEARER.

Parameters
connConnection info
Return values
numResult, e.g. SMTP_AUTH_SUCCESS

Definition at line 522 of file smtp.c.

523 {
524  mutt_message(_("Authenticating (OAUTHBEARER)..."));
525 
526  /* We get the access token from the smtp_oauth_refresh_command */
527  char *oauthbearer = mutt_account_getoauthbearer(&conn->account);
528  if (!oauthbearer)
529  return SMTP_AUTH_FAIL;
530 
531  size_t ilen = strlen(oauthbearer) + 30;
532  char *ibuf = mutt_mem_malloc(ilen);
533  snprintf(ibuf, ilen, "AUTH OAUTHBEARER %s\r\n", oauthbearer);
534 
535  int rc = mutt_socket_send(conn, ibuf);
536  FREE(&oauthbearer);
537  FREE(&ibuf);
538 
539  if (rc == -1)
540  return SMTP_AUTH_FAIL;
541  if (smtp_get_resp(conn) != 0)
542  return SMTP_AUTH_FAIL;
543 
544  return SMTP_AUTH_SUCCESS;
545 }
struct ConnAccount account
Definition: connection.h:36
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
#define mutt_message(...)
Definition: logging.h:83
#define _(a)
Definition: message.h:28
#define SMTP_AUTH_SUCCESS
Definition: smtp.c:76
char * mutt_account_getoauthbearer(struct ConnAccount *account)
Get an OAUTHBEARER token.
Definition: mutt_account.c:348
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:126
#define FREE(x)
Definition: memory.h:40
#define SMTP_AUTH_FAIL
Definition: smtp.c:78
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_auth_plain()

static int smtp_auth_plain ( struct Connection conn)
static

Authenticate using plain text.

Parameters
connSMTP Connection
Return values
0Success
<0Error, e.g. SMTP_AUTH_FAIL

Definition at line 553 of file smtp.c.

554 {
555  char buf[1024];
556 
557  /* Get username and password. Bail out of any can't be retrieved. */
558  if ((mutt_account_getuser(&conn->account) < 0) ||
559  (mutt_account_getpass(&conn->account) < 0))
560  {
561  goto error;
562  }
563 
564  /* Build the initial client response. */
565  size_t len = mutt_sasl_plain_msg(buf, sizeof(buf), "AUTH PLAIN", conn->account.user,
566  conn->account.user, conn->account.pass);
567 
568  /* Terminate as per SMTP protocol. Bail out if there's no room left. */
569  if (snprintf(buf + len, sizeof(buf) - len, "\r\n") != 2)
570  {
571  goto error;
572  }
573 
574  /* Send request, receive response (with a check for OK code). */
575  if ((mutt_socket_send(conn, buf) < 0) || smtp_get_resp(conn))
576  {
577  goto error;
578  }
579 
580  /* If we got here, auth was successful. */
581  return 0;
582 
583 error:
584  mutt_error(_("SASL authentication failed"));
585  return -1;
586 }
struct ConnAccount account
Definition: connection.h:36
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
char user[128]
Definition: connaccount.h:33
#define _(a)
Definition: message.h:28
size_t mutt_sasl_plain_msg(char *buf, size_t buflen, const char *cmd, const char *authz, const char *user, const char *pass)
Construct a base64 encoded SASL PLAIN message.
Definition: sasl_plain.c:55
char pass[256]
Definition: connaccount.h:35
int mutt_account_getpass(struct ConnAccount *account)
Fetch password into ConnAccount, if necessary.
Definition: mutt_account.c:289
int mutt_account_getuser(struct ConnAccount *account)
Retrieve username into ConnAccount, if necessary.
Definition: mutt_account.c:208
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:126
#define mutt_error(...)
Definition: logging.h:84
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_auth()

static int smtp_auth ( struct Connection conn)
static

Authenticate to an SMTP server.

Parameters
connSMTP Connection
Return values
0Success
<0Error, e.g. SMTP_AUTH_FAIL

Definition at line 594 of file smtp.c.

595 {
596  int r = SMTP_AUTH_UNAVAIL;
597 
599  {
600  struct ListNode *np = NULL;
602  {
603  mutt_debug(LL_DEBUG2, "Trying method %s\n", np->data);
604 
605  if (strcmp(np->data, "oauthbearer") == 0)
606  {
607  r = smtp_auth_oauth(conn);
608  }
609  else if (strcmp(np->data, "plain") == 0)
610  {
611  r = smtp_auth_plain(conn);
612  }
613  else
614  {
615 #ifdef USE_SASL
616  r = smtp_auth_sasl(conn, np->data);
617 #else
618  mutt_error(_("SMTP authentication method %s requires SASL"), np->data);
619  continue;
620 #endif
621  }
622 
623  if ((r == SMTP_AUTH_FAIL) && (C_SmtpAuthenticators->count > 1))
624  {
625  mutt_error(_("%s authentication failed, trying next method"), np->data);
626  }
627  else if (r != SMTP_AUTH_UNAVAIL)
628  break;
629  }
630  }
631  else
632  {
633 #ifdef USE_SASL
634  r = smtp_auth_sasl(conn, AuthMechs);
635 #else
636  mutt_error(_("SMTP authentication requires SASL"));
637  r = SMTP_AUTH_UNAVAIL;
638 #endif
639  }
640 
641  if (r != SMTP_AUTH_SUCCESS)
643 
644  if (r == SMTP_AUTH_FAIL)
645  {
646  mutt_error(_("SASL authentication failed"));
647  }
648  else if (r == SMTP_AUTH_UNAVAIL)
649  {
650  mutt_error(_("No authenticators available"));
651  }
652 
653  return (r == SMTP_AUTH_SUCCESS) ? 0 : -1;
654 }
static int smtp_auth_plain(struct Connection *conn)
Authenticate using plain text.
Definition: smtp.c:553
struct ConnAccount account
Definition: connection.h:36
#define _(a)
Definition: message.h:28
#define SMTP_AUTH_SUCCESS
Definition: smtp.c:76
struct Slist * C_SmtpAuthenticators
Config: (smtp) List of allowed authentication methods.
Definition: smtp.c:63
Log at debug level 2.
Definition: logging.h:41
struct ListHead head
Definition: slist.h:45
size_t count
Definition: slist.h:46
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
#define SMTP_AUTH_UNAVAIL
Definition: smtp.c:77
char * data
String.
Definition: list.h:35
static int smtp_auth_sasl(struct Connection *conn, const char *mechlist)
Authenticate using SASL.
Definition: smtp.c:414
#define mutt_error(...)
Definition: logging.h:84
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
A List node for strings.
Definition: list.h:33
static char * AuthMechs
Definition: smtp.c:95
#define SMTP_AUTH_FAIL
Definition: smtp.c:78
static int smtp_auth_oauth(struct Connection *conn)
Authenticate an SMTP connection using OAUTHBEARER.
Definition: smtp.c:522
void mutt_account_unsetpass(struct ConnAccount *account)
Unset ConnAccount&#39;s password.
Definition: mutt_account.c:332
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_open()

static int smtp_open ( struct Connection conn,
bool  esmtp 
)
static

Open an SMTP Connection.

Parameters
connSMTP Connection
esmtpIf true, use ESMTP
Return values
0Success
-1Error

Definition at line 663 of file smtp.c.

664 {
665  int rc;
666 
667  if (mutt_socket_open(conn))
668  return -1;
669 
670  /* get greeting string */
671  rc = smtp_get_resp(conn);
672  if (rc != 0)
673  return rc;
674 
675  rc = smtp_helo(conn, esmtp);
676  if (rc != 0)
677  return rc;
678 
679 #ifdef USE_SSL
680  enum QuadOption ans = MUTT_NO;
681  if (conn->ssf)
682  ans = MUTT_NO;
683  else if (C_SslForceTls)
684  ans = MUTT_YES;
685  else if ((Capabilities & SMTP_CAP_STARTTLS) &&
687  _("Secure connection with TLS?"))) == MUTT_ABORT))
688  {
689  return -1;
690  }
691 
692  if (ans == MUTT_YES)
693  {
694  if (mutt_socket_send(conn, "STARTTLS\r\n") < 0)
695  return SMTP_ERR_WRITE;
696  rc = smtp_get_resp(conn);
697  if (rc != 0)
698  return rc;
699 
700  if (mutt_ssl_starttls(conn))
701  {
702  mutt_error(_("Could not negotiate TLS connection"));
703  return -1;
704  }
705 
706  /* re-EHLO to get authentication mechanisms */
707  rc = smtp_helo(conn, esmtp);
708  if (rc != 0)
709  return rc;
710  }
711 #endif
712 
713  if (conn->account.flags & MUTT_ACCT_USER)
714  {
715  if (!(Capabilities & SMTP_CAP_AUTH))
716  {
717  mutt_error(_("SMTP server does not support authentication"));
718  return -1;
719  }
720 
721  return smtp_auth(conn);
722  }
723 
724  return 0;
725 }
static int smtp_helo(struct Connection *conn, bool esmtp)
Say hello to an SMTP Server.
Definition: smtp.c:377
struct ConnAccount account
Definition: connection.h:36
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
unsigned int ssf
security strength factor, in bits
Definition: connection.h:37
#define SMTP_CAP_STARTTLS
Server supports STARTTLS command.
Definition: smtp.c:86
User aborted the question (with Ctrl-G)
Definition: quad.h:38
enum QuadOption query_quadoption(enum QuadOption opt, const char *prompt)
Ask the user a quad-question.
Definition: init.c:3360
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:40
#define _(a)
Definition: message.h:28
WHERE bool C_SslForceTls
Config: (ssl) Require TLS encryption for all connections.
Definition: globals.h:234
int mutt_socket_open(struct Connection *conn)
Simple wrapper.
Definition: socket.c:74
static int smtp_auth(struct Connection *conn)
Authenticate to an SMTP server.
Definition: smtp.c:594
static SmtpCapFlags Capabilities
Definition: smtp.c:96
#define SMTP_ERR_WRITE
Definition: smtp.c:70
int mutt_ssl_starttls(struct Connection *conn)
Negotiate TLS over an already opened connection.
Definition: ssl.c:1477
User answered &#39;No&#39;, or assume &#39;No&#39;.
Definition: quad.h:39
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:126
#define mutt_error(...)
Definition: logging.h:84
#define SMTP_CAP_AUTH
Server supports AUTH command.
Definition: smtp.c:87
QuadOption
Possible values for a quad-option.
Definition: quad.h:36
WHERE unsigned char C_SslStarttls
Config: (ssl) Use STARTTLS on servers advertising the capability.
Definition: globals.h:186
MuttAccountFlags flags
Which fields are initialised, e.g. MUTT_ACCT_USER.
Definition: connaccount.h:39
#define MUTT_ACCT_USER
User field has been set.
Definition: mutt_account.h:61
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_smtp_send()

int mutt_smtp_send ( const struct AddressList *  from,
const struct AddressList *  to,
const struct AddressList *  cc,
const struct AddressList *  bcc,
const char *  msgfile,
bool  eightbit 
)

Send a message using SMTP.

Parameters
fromFrom Address
toTo Address
ccCc Address
bccBcc Address
msgfileMessage to send to the server
eightbitIf true, try for an 8-bit friendly connection
Return values
0Success
-1Error

Definition at line 738 of file smtp.c.

741 {
742  struct Connection *conn = NULL;
743  struct ConnAccount account;
744  const char *envfrom = NULL;
745  char buf[1024];
746  int rc = -1;
747 
748  /* it might be better to synthesize an envelope from from user and host
749  * but this condition is most likely arrived at accidentally */
751  envfrom = C_EnvelopeFromAddress->mailbox;
752  else if (from && !TAILQ_EMPTY(from))
753  envfrom = TAILQ_FIRST(from)->mailbox;
754  else
755  {
756  mutt_error(_("No from address given"));
757  return -1;
758  }
759 
760  if (smtp_fill_account(&account) < 0)
761  return rc;
762 
763  conn = mutt_conn_find(NULL, &account);
764  if (!conn)
765  return -1;
766 
767  do
768  {
769  /* send our greeting */
770  rc = smtp_open(conn, eightbit);
771  if (rc != 0)
772  break;
773  FREE(&AuthMechs);
774 
775  /* send the sender's address */
776  int len = snprintf(buf, sizeof(buf), "MAIL FROM:<%s>", envfrom);
777  if (eightbit && (Capabilities & SMTP_CAP_EIGHTBITMIME))
778  {
779  mutt_str_strncat(buf, sizeof(buf), " BODY=8BITMIME", 15);
780  len += 14;
781  }
783  len += snprintf(buf + len, sizeof(buf) - len, " RET=%s", C_DsnReturn);
785  (address_uses_unicode(envfrom) || addresses_use_unicode(to) ||
787  {
788  snprintf(buf + len, sizeof(buf) - len, " SMTPUTF8");
789  }
790  mutt_str_strncat(buf, sizeof(buf), "\r\n", 3);
791  if (mutt_socket_send(conn, buf) == -1)
792  {
793  rc = SMTP_ERR_WRITE;
794  break;
795  }
796  rc = smtp_get_resp(conn);
797  if (rc != 0)
798  break;
799 
800  /* send the recipient list */
801  if ((rc = smtp_rcpt_to(conn, to)) || (rc = smtp_rcpt_to(conn, cc)) ||
802  (rc = smtp_rcpt_to(conn, bcc)))
803  {
804  break;
805  }
806 
807  /* send the message data */
808  rc = smtp_data(conn, msgfile);
809  if (rc != 0)
810  break;
811 
812  mutt_socket_send(conn, "QUIT\r\n");
813 
814  rc = 0;
815  } while (false);
816 
817  mutt_socket_close(conn);
818  FREE(&conn);
819 
820  if (rc == SMTP_ERR_READ)
821  mutt_error(_("SMTP session failed: read error"));
822  else if (rc == SMTP_ERR_WRITE)
823  mutt_error(_("SMTP session failed: write error"));
824  else if (rc == SMTP_ERR_CODE)
825  mutt_error(_("Invalid server response"));
826 
827  return rc;
828 }
#define TAILQ_FIRST(head)
Definition: queue.h:716
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
An open network connection (socket)
Definition: connection.h:34
#define SMTP_CAP_SMTPUTF8
Server accepts UTF-8 strings.
Definition: smtp.c:90
#define _(a)
Definition: message.h:28
char * mailbox
Mailbox and host address.
Definition: address.h:37
char * mutt_str_strncat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:424
static int smtp_open(struct Connection *conn, bool esmtp)
Open an SMTP Connection.
Definition: smtp.c:663
static SmtpCapFlags Capabilities
Definition: smtp.c:96
#define SMTP_ERR_WRITE
Definition: smtp.c:70
static bool address_uses_unicode(const char *a)
Do any addresses use Unicode.
Definition: smtp.c:286
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:126
#define SMTP_ERR_READ
Definition: smtp.c:69
Login details for a remote server.
Definition: connaccount.h:31
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition: socket.c:95
WHERE char * C_DsnReturn
Config: What to send as a notification of message delivery or delay.
Definition: globals.h:108
#define SMTP_ERR_CODE
Definition: smtp.c:71
static int smtp_fill_account(struct ConnAccount *account)
Create ConnAccount object from SMTP Url.
Definition: smtp.c:328
#define mutt_error(...)
Definition: logging.h:84
WHERE struct Address * C_EnvelopeFromAddress
Config: Manually set the sender for outgoing messages.
Definition: globals.h:93
#define SMTP_CAP_EIGHTBITMIME
Server supports 8-bit MIME content.
Definition: smtp.c:89
static bool addresses_use_unicode(const struct AddressList *al)
Do any of a list of addresses use Unicode.
Definition: smtp.c:306
#define FREE(x)
Definition: memory.h:40
static int smtp_rcpt_to(struct Connection *conn, const struct AddressList *al)
Set the recipient to an Address.
Definition: smtp.c:176
#define TAILQ_EMPTY(head)
Definition: queue.h:714
static char * AuthMechs
Definition: smtp.c:95
static int smtp_data(struct Connection *conn, const char *msgfile)
Send data to an SMTP server.
Definition: smtp.c:211
struct Connection * mutt_conn_find(const struct Connection *start, const struct ConnAccount *account)
Find a connection from a list.
Definition: mutt_socket.c:87
#define SMTP_CAP_DSN
Server supports Delivery Status Notification.
Definition: smtp.c:88
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ C_SmtpAuthenticators

struct Slist* C_SmtpAuthenticators

Config: (smtp) List of allowed authentication methods.

Definition at line 63 of file smtp.c.

◆ AuthMechs

char* AuthMechs = NULL
static

Definition at line 95 of file smtp.c.

◆ Capabilities

SmtpCapFlags Capabilities
static

Definition at line 96 of file smtp.c.