NeoMutt  2019-11-11
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 "config/lib.h"
#include "email/lib.h"
#include "conn/conn.h"
#include "mutt.h"
#include "globals.h"
#include "mutt_account.h"
#include "mutt_socket.h"
#include "options.h"
#include "progress.h"
#include "sendlib.h"
#include <sasl/sasl.h>
#include <sasl/saslutil.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 62 of file smtp.c.

◆ SMTP_READY

#define SMTP_READY   334

Definition at line 63 of file smtp.c.

◆ SMTP_CONTINUE

#define SMTP_CONTINUE   354

Definition at line 64 of file smtp.c.

◆ SMTP_ERR_READ

#define SMTP_ERR_READ   -2

Definition at line 66 of file smtp.c.

◆ SMTP_ERR_WRITE

#define SMTP_ERR_WRITE   -3

Definition at line 67 of file smtp.c.

◆ SMTP_ERR_CODE

#define SMTP_ERR_CODE   -4

Definition at line 68 of file smtp.c.

◆ SMTP_PORT

#define SMTP_PORT   25

Definition at line 70 of file smtp.c.

◆ SMTPS_PORT

#define SMTPS_PORT   465

Definition at line 71 of file smtp.c.

◆ SMTP_AUTH_SUCCESS

#define SMTP_AUTH_SUCCESS   0

Definition at line 73 of file smtp.c.

◆ SMTP_AUTH_UNAVAIL

#define SMTP_AUTH_UNAVAIL   1

Definition at line 74 of file smtp.c.

◆ SMTP_AUTH_FAIL

#define SMTP_AUTH_FAIL   -1

Definition at line 75 of file smtp.c.

◆ SMTP_CAP_NO_FLAGS

#define SMTP_CAP_NO_FLAGS   0

No flags are set.

Definition at line 82 of file smtp.c.

◆ SMTP_CAP_STARTTLS

#define SMTP_CAP_STARTTLS   (1 << 0)

Server supports STARTTLS command.

Definition at line 83 of file smtp.c.

◆ SMTP_CAP_AUTH

#define SMTP_CAP_AUTH   (1 << 1)

Server supports AUTH command.

Definition at line 84 of file smtp.c.

◆ SMTP_CAP_DSN

#define SMTP_CAP_DSN   (1 << 2)

Server supports Delivery Status Notification.

Definition at line 85 of file smtp.c.

◆ SMTP_CAP_EIGHTBITMIME

#define SMTP_CAP_EIGHTBITMIME   (1 << 3)

Server supports 8-bit MIME content.

Definition at line 86 of file smtp.c.

◆ SMTP_CAP_SMTPUTF8

#define SMTP_CAP_SMTPUTF8   (1 << 4)

Server accepts UTF-8 strings.

Definition at line 87 of file smtp.c.

◆ SMTP_CAP_ALL

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

Definition at line 89 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 81 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 102 of file smtp.c.

103 {
104  char code[4];
105 
106  if (buflen < 4)
107  return false;
108  code[0] = buf[0];
109  code[1] = buf[1];
110  code[2] = buf[2];
111  code[3] = '\0';
112  if (mutt_str_atoi(code, n) < 0)
113  return false;
114  return true;
115 }
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 123 of file smtp.c.

124 {
125  int n;
126  char buf[1024];
127 
128  do
129  {
130  n = mutt_socket_readln(buf, sizeof(buf), conn);
131  if (n < 4)
132  {
133  /* read error, or no response code */
134  return SMTP_ERR_READ;
135  }
136  const char *s = buf + 4; /* Skip the response code and the space/dash */
137  size_t plen;
138 
139  if (mutt_str_startswith(s, "8BITMIME", CASE_IGNORE))
141  else if ((plen = mutt_str_startswith(s, "AUTH ", CASE_IGNORE)))
142  {
144  FREE(&AuthMechs);
145  AuthMechs = mutt_str_strdup(s + plen);
146  }
147  else if (mutt_str_startswith(s, "DSN", CASE_IGNORE))
149  else if (mutt_str_startswith(s, "STARTTLS", CASE_IGNORE))
151  else if (mutt_str_startswith(s, "SMTPUTF8", CASE_IGNORE))
153 
154  if (!valid_smtp_code(buf, n, &n))
155  return SMTP_ERR_CODE;
156 
157  } while (buf[3] == '-');
158 
159  if (smtp_success(n) || (n == SMTP_CONTINUE))
160  return 0;
161 
162  mutt_error(_("SMTP session failed: %s"), buf);
163  return -1;
164 }
#define SMTP_CAP_STARTTLS
Server supports STARTTLS command.
Definition: smtp.c:83
static size_t plen
Length of cached packet.
Definition: pgppacket.c:38
#define SMTP_CAP_SMTPUTF8
Server accepts UTF-8 strings.
Definition: smtp.c:87
#define _(a)
Definition: message.h:28
static SmtpCapFlags Capabilities
Definition: smtp.c:93
Ignore case when comparing strings.
Definition: string2.h:68
#define SMTP_ERR_READ
Definition: smtp.c:66
#define smtp_success(x)
Definition: smtp.c:62
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:68
static bool valid_smtp_code(char *buf, size_t buflen, int *n)
Is the is a valid SMTP return code?
Definition: smtp.c:102
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:64
#define SMTP_CAP_EIGHTBITMIME
Server supports 8-bit MIME content.
Definition: smtp.c:86
#define FREE(x)
Definition: memory.h:40
#define SMTP_CAP_AUTH
Server supports AUTH command.
Definition: smtp.c:84
static char * AuthMechs
Definition: smtp.c:92
#define SMTP_CAP_DSN
Server supports Delivery Status Notification.
Definition: smtp.c:85
+ 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 173 of file smtp.c.

174 {
175  if (!al)
176  return 0;
177 
178  struct Address *a = NULL;
179  TAILQ_FOREACH(a, al, entries)
180  {
181  /* weed out group mailboxes, since those are for display only */
182  if (!a->mailbox || a->group)
183  {
184  continue;
185  }
186  char buf[1024];
188  snprintf(buf, sizeof(buf), "RCPT TO:<%s> NOTIFY=%s\r\n", a->mailbox, C_DsnNotify);
189  else
190  snprintf(buf, sizeof(buf), "RCPT TO:<%s>\r\n", a->mailbox);
191  if (mutt_socket_send(conn, buf) == -1)
192  return SMTP_ERR_WRITE;
193  int rc = smtp_get_resp(conn);
194  if (rc != 0)
195  return rc;
196  }
197 
198  return 0;
199 }
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:719
#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:93
#define SMTP_ERR_WRITE
Definition: smtp.c:67
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:123
bool group
Group mailbox?
Definition: address.h:38
WHERE char * C_DsnNotify
Config: Request notification for message delivery or delay.
Definition: globals.h:109
#define SMTP_CAP_DSN
Server supports Delivery Status Notification.
Definition: smtp.c:85
+ 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 208 of file smtp.c.

209 {
210  char buf[1024];
211  struct Progress progress;
212  struct stat st;
213  int rc, term = 0;
214  size_t buflen = 0;
215 
216  FILE *fp = fopen(msgfile, "r");
217  if (!fp)
218  {
219  mutt_error(_("SMTP session failed: unable to open %s"), msgfile);
220  return -1;
221  }
222  stat(msgfile, &st);
223  unlink(msgfile);
224  mutt_progress_init(&progress, _("Sending message..."), MUTT_PROGRESS_NET, st.st_size);
225 
226  snprintf(buf, sizeof(buf), "DATA\r\n");
227  if (mutt_socket_send(conn, buf) == -1)
228  {
229  mutt_file_fclose(&fp);
230  return SMTP_ERR_WRITE;
231  }
232  rc = smtp_get_resp(conn);
233  if (rc != 0)
234  {
235  mutt_file_fclose(&fp);
236  return rc;
237  }
238 
239  while (fgets(buf, sizeof(buf) - 1, fp))
240  {
241  buflen = mutt_str_strlen(buf);
242  term = buflen && buf[buflen - 1] == '\n';
243  if (term && ((buflen == 1) || (buf[buflen - 2] != '\r')))
244  snprintf(buf + buflen - 1, sizeof(buf) - buflen + 1, "\r\n");
245  if (buf[0] == '.')
246  {
247  if (mutt_socket_send_d(conn, ".", MUTT_SOCK_LOG_FULL) == -1)
248  {
249  mutt_file_fclose(&fp);
250  return SMTP_ERR_WRITE;
251  }
252  }
253  if (mutt_socket_send_d(conn, buf, MUTT_SOCK_LOG_FULL) == -1)
254  {
255  mutt_file_fclose(&fp);
256  return SMTP_ERR_WRITE;
257  }
258  mutt_progress_update(&progress, ftell(fp), -1);
259  }
260  if (!term && buflen && (mutt_socket_send_d(conn, "\r\n", MUTT_SOCK_LOG_FULL) == -1))
261  {
262  mutt_file_fclose(&fp);
263  return SMTP_ERR_WRITE;
264  }
265  mutt_file_fclose(&fp);
266 
267  /* terminate the message body */
268  if (mutt_socket_send(conn, ".\r\n") == -1)
269  return SMTP_ERR_WRITE;
270 
271  rc = smtp_get_resp(conn);
272  if (rc != 0)
273  return rc;
274 
275  return 0;
276 }
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:215
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:67
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:123
void mutt_progress_init(struct Progress *progress, const char *msg, enum ProgressType type, size_t size)
Set up a progress bar.
Definition: progress.c:156
#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 283 of file smtp.c.

284 {
285  if (!a)
286  return false;
287 
288  while (*a)
289  {
290  if ((unsigned char) *a & (1 << 7))
291  return true;
292  a++;
293  }
294 
295  return false;
296 }
+ 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 303 of file smtp.c.

304 {
305  if (!al)
306  {
307  return false;
308  }
309 
310  struct Address *a = NULL;
311  TAILQ_FOREACH(a, al, entries)
312  {
313  if (a->mailbox && !a->group && address_uses_unicode(a->mailbox))
314  return true;
315  }
316  return false;
317 }
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:719
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:283
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 325 of file smtp.c.

326 {
327  account->flags = 0;
328  account->port = 0;
329  account->type = MUTT_ACCT_TYPE_SMTP;
330 
331  struct Url *url = url_parse(C_SmtpUrl);
332  if (!url || ((url->scheme != U_SMTP) && (url->scheme != U_SMTPS)) ||
333  !url->host || (mutt_account_fromurl(account, url) < 0))
334  {
335  url_free(&url);
336  mutt_error(_("Invalid SMTP URL: %s"), C_SmtpUrl);
337  return -1;
338  }
339 
340  if (url->scheme == U_SMTPS)
341  account->flags |= MUTT_ACCT_SSL;
342 
343  if (account->port == 0)
344  {
345  if (account->flags & MUTT_ACCT_SSL)
346  account->port = SMTPS_PORT;
347  else
348  {
349  static unsigned short SmtpPort = 0;
350  if (SmtpPort == 0)
351  {
352  struct servent *service = getservbyname("smtp", "tcp");
353  if (service)
354  SmtpPort = ntohs(service->s_port);
355  else
356  SmtpPort = SMTP_PORT;
357  mutt_debug(LL_DEBUG3, "Using default SMTP port %d\n", SmtpPort);
358  }
359  account->port = SmtpPort;
360  }
361  }
362 
363  url_free(&url);
364  return 0;
365 }
WHERE char * C_SmtpUrl
Config: (smtp) Url of the SMTP server.
Definition: globals.h:144
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:70
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:71
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:58
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 374 of file smtp.c.

375 {
376  Capabilities = 0;
377 
378  if (!esmtp)
379  {
380  /* if TLS or AUTH are requested, use EHLO */
381  if (conn->account.flags & MUTT_ACCT_USER)
382  esmtp = true;
383 #ifdef USE_SSL
384  if (C_SslForceTls || (C_SslStarttls != MUTT_NO))
385  esmtp = true;
386 #endif
387  }
388 
389  const char *fqdn = mutt_fqdn(false);
390  if (!fqdn)
391  fqdn = NONULL(ShortHostname);
392 
393  char buf[1024];
394  snprintf(buf, sizeof(buf), "%s %s\r\n", esmtp ? "EHLO" : "HELO", fqdn);
395  /* XXX there should probably be a wrapper in mutt_socket.c that
396  * repeatedly calls conn->write until all data is sent. This
397  * currently doesn't check for a short write. */
398  if (mutt_socket_send(conn, buf) == -1)
399  return SMTP_ERR_WRITE;
400  return smtp_get_resp(conn);
401 }
#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:239
static SmtpCapFlags Capabilities
Definition: smtp.c:93
#define SMTP_ERR_WRITE
Definition: smtp.c:67
User answered &#39;No&#39;, or assume &#39;No&#39;.
Definition: quad.h:38
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:123
const char * mutt_fqdn(bool may_hide_host)
Get the Fully-Qualified Domain Name.
Definition: sendlib.c:2537
WHERE char * ShortHostname
Short version of the hostname.
Definition: globals.h:52
WHERE unsigned char C_SslStarttls
Config: (ssl) Use STARTTLS on servers advertising the capability.
Definition: globals.h:191
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 411 of file smtp.c.

412 {
413  sasl_conn_t *saslconn = NULL;
414  sasl_interact_t *interaction = NULL;
415  const char *mech = NULL;
416  const char *data = NULL;
417  unsigned int len;
418  char *buf = NULL;
419  size_t bufsize = 0;
420  int rc, saslrc;
421 
422  if (mutt_sasl_client_new(conn, &saslconn) < 0)
423  return SMTP_AUTH_FAIL;
424 
425  do
426  {
427  rc = sasl_client_start(saslconn, mechlist, &interaction, &data, &len, &mech);
428  if (rc == SASL_INTERACT)
429  mutt_sasl_interact(interaction);
430  } while (rc == SASL_INTERACT);
431 
432  if ((rc != SASL_OK) && (rc != SASL_CONTINUE))
433  {
434  mutt_debug(LL_DEBUG2, "%s unavailable\n", mech);
435  sasl_dispose(&saslconn);
436  return SMTP_AUTH_UNAVAIL;
437  }
438 
439  if (!OptNoCurses)
440  mutt_message(_("Authenticating (%s)..."), mech);
441 
442  bufsize = MAX((len * 2), 1024);
443  buf = mutt_mem_malloc(bufsize);
444 
445  snprintf(buf, bufsize, "AUTH %s", mech);
446  if (len)
447  {
448  mutt_str_strcat(buf, bufsize, " ");
449  if (sasl_encode64(data, len, buf + mutt_str_strlen(buf),
450  bufsize - mutt_str_strlen(buf), &len) != SASL_OK)
451  {
452  mutt_debug(LL_DEBUG1, "#1 error base64-encoding client response\n");
453  goto fail;
454  }
455  }
456  mutt_str_strcat(buf, bufsize, "\r\n");
457 
458  do
459  {
460  if (mutt_socket_send(conn, buf) < 0)
461  goto fail;
462  rc = mutt_socket_readln_d(buf, bufsize, conn, MUTT_SOCK_LOG_FULL);
463  if (rc < 0)
464  goto fail;
465  if (!valid_smtp_code(buf, rc, &rc))
466  goto fail;
467 
468  if (rc != SMTP_READY)
469  break;
470 
471  if (sasl_decode64(buf + 4, strlen(buf + 4), buf, bufsize - 1, &len) != SASL_OK)
472  {
473  mutt_debug(LL_DEBUG1, "error base64-decoding server response\n");
474  goto fail;
475  }
476 
477  do
478  {
479  saslrc = sasl_client_step(saslconn, buf, len, &interaction, &data, &len);
480  if (saslrc == SASL_INTERACT)
481  mutt_sasl_interact(interaction);
482  } while (saslrc == SASL_INTERACT);
483 
484  if (len)
485  {
486  if ((len * 2) > bufsize)
487  {
488  bufsize = len * 2;
489  mutt_mem_realloc(&buf, bufsize);
490  }
491  if (sasl_encode64(data, len, buf, bufsize, &len) != SASL_OK)
492  {
493  mutt_debug(LL_DEBUG1, "#2 error base64-encoding client response\n");
494  goto fail;
495  }
496  }
497  mutt_str_strfcpy(buf + len, "\r\n", bufsize - len);
498  } while (rc == SMTP_READY && saslrc != SASL_FAIL);
499 
500  if (smtp_success(rc))
501  {
502  mutt_sasl_setup_conn(conn, saslconn);
503  FREE(&buf);
504  return SMTP_AUTH_SUCCESS;
505  }
506 
507 fail:
508  sasl_dispose(&saslconn);
509  FREE(&buf);
510  return SMTP_AUTH_FAIL;
511 }
#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:73
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:57
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:63
#define smtp_success(x)
Definition: smtp.c:62
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:74
Log at debug level 1.
Definition: logging.h:56
static bool valid_smtp_code(char *buf, size_t buflen, int *n)
Is the is a valid SMTP return code?
Definition: smtp.c:102
#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:75
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 519 of file smtp.c.

520 {
521  mutt_message(_("Authenticating (OAUTHBEARER)..."));
522 
523  /* We get the access token from the smtp_oauth_refresh_command */
524  char *oauthbearer = mutt_account_getoauthbearer(&conn->account);
525  if (!oauthbearer)
526  return SMTP_AUTH_FAIL;
527 
528  size_t ilen = strlen(oauthbearer) + 30;
529  char *ibuf = mutt_mem_malloc(ilen);
530  snprintf(ibuf, ilen, "AUTH OAUTHBEARER %s\r\n", oauthbearer);
531 
532  int rc = mutt_socket_send(conn, ibuf);
533  FREE(&oauthbearer);
534  FREE(&ibuf);
535 
536  if (rc == -1)
537  return SMTP_AUTH_FAIL;
538  if (smtp_get_resp(conn) != 0)
539  return SMTP_AUTH_FAIL;
540 
541  return SMTP_AUTH_SUCCESS;
542 }
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:73
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:123
#define FREE(x)
Definition: memory.h:40
#define SMTP_AUTH_FAIL
Definition: smtp.c:75
+ 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 550 of file smtp.c.

551 {
552  char buf[1024];
553 
554  /* Get username and password. Bail out of any can't be retrieved. */
555  if ((mutt_account_getuser(&conn->account) < 0) ||
556  (mutt_account_getpass(&conn->account) < 0))
557  {
558  goto error;
559  }
560 
561  /* Build the initial client response. */
562  size_t len = mutt_sasl_plain_msg(buf, sizeof(buf), "AUTH PLAIN", conn->account.user,
563  conn->account.user, conn->account.pass);
564 
565  /* Terminate as per SMTP protocol. Bail out if there's no room left. */
566  if (snprintf(buf + len, sizeof(buf) - len, "\r\n") != 2)
567  {
568  goto error;
569  }
570 
571  /* Send request, receive response (with a check for OK code). */
572  if ((mutt_socket_send(conn, buf) < 0) || smtp_get_resp(conn))
573  {
574  goto error;
575  }
576 
577  /* If we got here, auth was successful. */
578  return 0;
579 
580 error:
581  mutt_error(_("SASL authentication failed"));
582  return -1;
583 }
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:54
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:123
#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 591 of file smtp.c.

592 {
593  int r = SMTP_AUTH_UNAVAIL;
594 
596  {
597  struct ListNode *np = NULL;
599  {
600  mutt_debug(LL_DEBUG2, "Trying method %s\n", np->data);
601 
602  if (strcmp(np->data, "oauthbearer") == 0)
603  {
604  r = smtp_auth_oauth(conn);
605  }
606  else if (strcmp(np->data, "plain") == 0)
607  {
608  r = smtp_auth_plain(conn);
609  }
610  else
611  {
612 #ifdef USE_SASL
613  r = smtp_auth_sasl(conn, np->data);
614 #else
615  mutt_error(_("SMTP authentication method %s requires SASL"), np->data);
616  continue;
617 #endif
618  }
619 
620  if ((r == SMTP_AUTH_FAIL) && (C_SmtpAuthenticators->count > 1))
621  {
622  mutt_error(_("%s authentication failed, trying next method"), np->data);
623  }
624  else if (r != SMTP_AUTH_UNAVAIL)
625  break;
626  }
627  }
628  else
629  {
630 #ifdef USE_SASL
631  r = smtp_auth_sasl(conn, AuthMechs);
632 #else
633  mutt_error(_("SMTP authentication requires SASL"));
634  r = SMTP_AUTH_UNAVAIL;
635 #endif
636  }
637 
638  if (r != SMTP_AUTH_SUCCESS)
640 
641  if (r == SMTP_AUTH_FAIL)
642  {
643  mutt_error(_("SASL authentication failed"));
644  }
645  else if (r == SMTP_AUTH_UNAVAIL)
646  {
647  mutt_error(_("No authenticators available"));
648  }
649 
650  return (r == SMTP_AUTH_SUCCESS) ? 0 : -1;
651 }
static int smtp_auth_plain(struct Connection *conn)
Authenticate using plain text.
Definition: smtp.c:550
struct ConnAccount account
Definition: connection.h:36
#define _(a)
Definition: message.h:28
#define SMTP_AUTH_SUCCESS
Definition: smtp.c:73
struct Slist * C_SmtpAuthenticators
Config: (smtp) List of allowed authentication methods.
Definition: smtp.c:60
Log at debug level 2.
Definition: logging.h:57
struct ListHead head
Definition: slist.h:45
size_t count
Definition: slist.h:46
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
#define SMTP_AUTH_UNAVAIL
Definition: smtp.c:74
char * data
String.
Definition: list.h:35
static int smtp_auth_sasl(struct Connection *conn, const char *mechlist)
Authenticate using SASL.
Definition: smtp.c:411
#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:92
#define SMTP_AUTH_FAIL
Definition: smtp.c:75
static int smtp_auth_oauth(struct Connection *conn)
Authenticate an SMTP connection using OAUTHBEARER.
Definition: smtp.c:519
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 660 of file smtp.c.

661 {
662  int rc;
663 
664  if (mutt_socket_open(conn))
665  return -1;
666 
667  /* get greeting string */
668  rc = smtp_get_resp(conn);
669  if (rc != 0)
670  return rc;
671 
672  rc = smtp_helo(conn, esmtp);
673  if (rc != 0)
674  return rc;
675 
676 #ifdef USE_SSL
677  enum QuadOption ans = MUTT_NO;
678  if (conn->ssf)
679  ans = MUTT_NO;
680  else if (C_SslForceTls)
681  ans = MUTT_YES;
682  else if ((Capabilities & SMTP_CAP_STARTTLS) &&
684  _("Secure connection with TLS?"))) == MUTT_ABORT))
685  {
686  return -1;
687  }
688 
689  if (ans == MUTT_YES)
690  {
691  if (mutt_socket_send(conn, "STARTTLS\r\n") < 0)
692  return SMTP_ERR_WRITE;
693  rc = smtp_get_resp(conn);
694  if (rc != 0)
695  return rc;
696 
697  if (mutt_ssl_starttls(conn))
698  {
699  mutt_error(_("Could not negotiate TLS connection"));
700  return -1;
701  }
702 
703  /* re-EHLO to get authentication mechanisms */
704  rc = smtp_helo(conn, esmtp);
705  if (rc != 0)
706  return rc;
707  }
708 #endif
709 
710  if (conn->account.flags & MUTT_ACCT_USER)
711  {
712  if (!(Capabilities & SMTP_CAP_AUTH))
713  {
714  mutt_error(_("SMTP server does not support authentication"));
715  return -1;
716  }
717 
718  return smtp_auth(conn);
719  }
720 
721  return 0;
722 }
static int smtp_helo(struct Connection *conn, bool esmtp)
Say hello to an SMTP Server.
Definition: smtp.c:374
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:83
User aborted the question (with Ctrl-G)
Definition: quad.h:37
enum QuadOption query_quadoption(enum QuadOption opt, const char *prompt)
Ask the user a quad-question.
Definition: init.c:3340
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:39
#define _(a)
Definition: message.h:28
WHERE bool C_SslForceTls
Config: (ssl) Require TLS encryption for all connections.
Definition: globals.h:239
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:591
static SmtpCapFlags Capabilities
Definition: smtp.c:93
#define SMTP_ERR_WRITE
Definition: smtp.c:67
int mutt_ssl_starttls(struct Connection *conn)
Negotiate TLS over an already opened connection.
Definition: ssl.c:1447
User answered &#39;No&#39;, or assume &#39;No&#39;.
Definition: quad.h:38
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:123
#define mutt_error(...)
Definition: logging.h:84
#define SMTP_CAP_AUTH
Server supports AUTH command.
Definition: smtp.c:84
QuadOption
Possible values for a quad-option.
Definition: quad.h:35
WHERE unsigned char C_SslStarttls
Config: (ssl) Use STARTTLS on servers advertising the capability.
Definition: globals.h:191
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 735 of file smtp.c.

738 {
739  struct Connection *conn = NULL;
740  struct ConnAccount account;
741  const char *envfrom = NULL;
742  char buf[1024];
743  int rc = -1;
744 
745  /* it might be better to synthesize an envelope from from user and host
746  * but this condition is most likely arrived at accidentally */
748  envfrom = C_EnvelopeFromAddress->mailbox;
749  else if (from && !TAILQ_EMPTY(from))
750  envfrom = TAILQ_FIRST(from)->mailbox;
751  else
752  {
753  mutt_error(_("No from address given"));
754  return -1;
755  }
756 
757  if (smtp_fill_account(&account) < 0)
758  return rc;
759 
760  conn = mutt_conn_find(NULL, &account);
761  if (!conn)
762  return -1;
763 
764  do
765  {
766  /* send our greeting */
767  rc = smtp_open(conn, eightbit);
768  if (rc != 0)
769  break;
770  FREE(&AuthMechs);
771 
772  /* send the sender's address */
773  int len = snprintf(buf, sizeof(buf), "MAIL FROM:<%s>", envfrom);
774  if (eightbit && (Capabilities & SMTP_CAP_EIGHTBITMIME))
775  {
776  mutt_str_strncat(buf, sizeof(buf), " BODY=8BITMIME", 15);
777  len += 14;
778  }
780  len += snprintf(buf + len, sizeof(buf) - len, " RET=%s", C_DsnReturn);
782  (address_uses_unicode(envfrom) || addresses_use_unicode(to) ||
784  {
785  snprintf(buf + len, sizeof(buf) - len, " SMTPUTF8");
786  }
787  mutt_str_strncat(buf, sizeof(buf), "\r\n", 3);
788  if (mutt_socket_send(conn, buf) == -1)
789  {
790  rc = SMTP_ERR_WRITE;
791  break;
792  }
793  rc = smtp_get_resp(conn);
794  if (rc != 0)
795  break;
796 
797  /* send the recipient list */
798  if ((rc = smtp_rcpt_to(conn, to)) || (rc = smtp_rcpt_to(conn, cc)) ||
799  (rc = smtp_rcpt_to(conn, bcc)))
800  {
801  break;
802  }
803 
804  /* send the message data */
805  rc = smtp_data(conn, msgfile);
806  if (rc != 0)
807  break;
808 
809  mutt_socket_send(conn, "QUIT\r\n");
810 
811  rc = 0;
812  } while (false);
813 
814  mutt_socket_close(conn);
815  FREE(&conn);
816 
817  if (rc == SMTP_ERR_READ)
818  mutt_error(_("SMTP session failed: read error"));
819  else if (rc == SMTP_ERR_WRITE)
820  mutt_error(_("SMTP session failed: write error"));
821  else if (rc == SMTP_ERR_CODE)
822  mutt_error(_("Invalid server response"));
823 
824  return rc;
825 }
#define TAILQ_FIRST(head)
Definition: queue.h:717
#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:87
#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:660
static SmtpCapFlags Capabilities
Definition: smtp.c:93
#define SMTP_ERR_WRITE
Definition: smtp.c:67
static bool address_uses_unicode(const char *a)
Do any addresses use Unicode.
Definition: smtp.c:283
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:123
#define SMTP_ERR_READ
Definition: smtp.c:66
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:110
#define SMTP_ERR_CODE
Definition: smtp.c:68
static int smtp_fill_account(struct ConnAccount *account)
Create ConnAccount object from SMTP Url.
Definition: smtp.c:325
#define mutt_error(...)
Definition: logging.h:84
WHERE struct Address * C_EnvelopeFromAddress
Config: Manually set the sender for outgoing messages.
Definition: globals.h:95
#define SMTP_CAP_EIGHTBITMIME
Server supports 8-bit MIME content.
Definition: smtp.c:86
static bool addresses_use_unicode(const struct AddressList *al)
Do any of a list of addresses use Unicode.
Definition: smtp.c:303
#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:173
#define TAILQ_EMPTY(head)
Definition: queue.h:715
static char * AuthMechs
Definition: smtp.c:92
static int smtp_data(struct Connection *conn, const char *msgfile)
Send data to an SMTP server.
Definition: smtp.c:208
struct Connection * mutt_conn_find(const struct Connection *start, const struct ConnAccount *account)
Find a connection from a list.
Definition: mutt_socket.c:84
#define SMTP_CAP_DSN
Server supports Delivery Status Notification.
Definition: smtp.c:85
+ 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 60 of file smtp.c.

◆ AuthMechs

char* AuthMechs = NULL
static

Definition at line 92 of file smtp.c.

◆ Capabilities

SmtpCapFlags Capabilities
static

Definition at line 93 of file smtp.c.