NeoMutt  2018-07-16 +1783-b00bd9
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 <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 Address *a)
 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 Address *a)
 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 Address *from, const struct Address *to, const struct Address *cc, const struct Address *bcc, const char *msgfile, bool eightbit)
 Send a message using SMTP. More...
 

Variables

char * C_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

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

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

Definition at line 60 of file smtp.c.

#define SMTP_READY   334

Definition at line 61 of file smtp.c.

#define SMTP_CONTINUE   354

Definition at line 62 of file smtp.c.

#define SMTP_ERR_READ   -2

Definition at line 64 of file smtp.c.

#define SMTP_ERR_WRITE   -3

Definition at line 65 of file smtp.c.

#define SMTP_ERR_CODE   -4

Definition at line 66 of file smtp.c.

#define SMTP_PORT   25

Definition at line 68 of file smtp.c.

#define SMTPS_PORT   465

Definition at line 69 of file smtp.c.

#define SMTP_AUTH_SUCCESS   0

Definition at line 71 of file smtp.c.

#define SMTP_AUTH_UNAVAIL   1

Definition at line 72 of file smtp.c.

#define SMTP_AUTH_FAIL   -1

Definition at line 73 of file smtp.c.

#define SMTP_CAP_NO_FLAGS   0

No flags are set.

Definition at line 80 of file smtp.c.

#define SMTP_CAP_STARTTLS   (1 << 0)

Server supports STARTTLS command.

Definition at line 81 of file smtp.c.

#define SMTP_CAP_AUTH   (1 << 1)

Server supports AUTH command.

Definition at line 82 of file smtp.c.

#define SMTP_CAP_DSN   (1 << 2)

Server supports Delivery Status Notification.

Definition at line 83 of file smtp.c.

#define SMTP_CAP_EIGHTBITMIME   (1 << 3)

Server supports 8-bit MIME content.

Definition at line 84 of file smtp.c.

#define SMTP_CAP_SMTPUTF8   (1 << 4)

Server accepts UTF-8 strings.

Definition at line 85 of file smtp.c.

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

Definition at line 87 of file smtp.c.

Typedef Documentation

typedef uint8_t SmtpCapFlags

typedef SmtpCapFlags - SMTP server capabilities

Flags, e.g. SMTP_CAP_STARTTLS

Definition at line 79 of file smtp.c.

Function Documentation

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 100 of file smtp.c.

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

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 121 of file smtp.c.

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int smtp_rcpt_to ( struct Connection conn,
const struct Address a 
)
static

Set the recipient to an Address.

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

Definition at line 171 of file smtp.c.

172 {
173  char buf[1024];
174  int rc;
175 
176  while (a)
177  {
178  /* weed out group mailboxes, since those are for display only */
179  if (!a->mailbox || a->group)
180  {
181  a = a->next;
182  continue;
183  }
185  snprintf(buf, sizeof(buf), "RCPT TO:<%s> NOTIFY=%s\r\n", a->mailbox, C_DsnNotify);
186  else
187  snprintf(buf, sizeof(buf), "RCPT TO:<%s>\r\n", a->mailbox);
188  if (mutt_socket_send(conn, buf) == -1)
189  return SMTP_ERR_WRITE;
190  rc = smtp_get_resp(conn);
191  if (rc != 0)
192  return rc;
193  a = a->next;
194  }
195 
196  return 0;
197 }
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
char * mailbox
mailbox and host address
Definition: address.h:35
static SmtpCapFlags Capabilities
Definition: smtp.c:91
#define SMTP_ERR_WRITE
Definition: smtp.c:65
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:121
bool group
group mailbox?
Definition: address.h:36
WHERE char * C_DsnNotify
Config: Request notification for message delivery or delay.
Definition: globals.h:102
#define SMTP_CAP_DSN
Server supports Delivery Status Notification.
Definition: smtp.c:83
struct Address * next
Definition: address.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 206 of file smtp.c.

207 {
208  char buf[1024];
209  struct Progress progress;
210  struct stat st;
211  int rc, term = 0;
212  size_t buflen = 0;
213 
214  FILE *fp = fopen(msgfile, "r");
215  if (!fp)
216  {
217  mutt_error(_("SMTP session failed: unable to open %s"), msgfile);
218  return -1;
219  }
220  stat(msgfile, &st);
221  unlink(msgfile);
222  mutt_progress_init(&progress, _("Sending message..."), MUTT_PROGRESS_SIZE,
223  C_NetInc, st.st_size);
224 
225  snprintf(buf, sizeof(buf), "DATA\r\n");
226  if (mutt_socket_send(conn, buf) == -1)
227  {
228  mutt_file_fclose(&fp);
229  return SMTP_ERR_WRITE;
230  }
231  rc = smtp_get_resp(conn);
232  if (rc != 0)
233  {
234  mutt_file_fclose(&fp);
235  return rc;
236  }
237 
238  while (fgets(buf, sizeof(buf) - 1, fp))
239  {
240  buflen = mutt_str_strlen(buf);
241  term = buflen && buf[buflen - 1] == '\n';
242  if (term && ((buflen == 1) || (buf[buflen - 2] != '\r')))
243  snprintf(buf + buflen - 1, sizeof(buf) - buflen + 1, "\r\n");
244  if (buf[0] == '.')
245  {
246  if (mutt_socket_send_d(conn, ".", MUTT_SOCK_LOG_FULL) == -1)
247  {
248  mutt_file_fclose(&fp);
249  return SMTP_ERR_WRITE;
250  }
251  }
252  if (mutt_socket_send_d(conn, buf, MUTT_SOCK_LOG_FULL) == -1)
253  {
254  mutt_file_fclose(&fp);
255  return SMTP_ERR_WRITE;
256  }
257  mutt_progress_update(&progress, ftell(fp), -1);
258  }
259  if (!term && buflen && (mutt_socket_send_d(conn, "\r\n", MUTT_SOCK_LOG_FULL) == -1))
260  {
261  mutt_file_fclose(&fp);
262  return SMTP_ERR_WRITE;
263  }
264  mutt_file_fclose(&fp);
265 
266  /* terminate the message body */
267  if (mutt_socket_send(conn, ".\r\n") == -1)
268  return SMTP_ERR_WRITE;
269 
270  rc = smtp_get_resp(conn);
271  if (rc != 0)
272  return rc;
273 
274  return 0;
275 }
#define MUTT_PROGRESS_SIZE
traffic-based progress
Definition: progress.h:32
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
void mutt_progress_update(struct Progress *progress, long pos, int percent)
Update the state of the progress bar.
Definition: progress.c:171
void mutt_progress_init(struct Progress *progress, const char *msg, unsigned short flags, unsigned short inc, size_t size)
Set up a progress bar.
Definition: progress.c:114
#define _(a)
Definition: message.h:28
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:669
#define MUTT_SOCK_LOG_FULL
Definition: mutt_socket.h:32
A progress bar.
Definition: progress.h:38
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:149
#define mutt_socket_send_d(conn, buf, dbg)
Definition: mutt_socket.h:39
#define SMTP_ERR_WRITE
Definition: smtp.c:65
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:121
#define mutt_error(...)
Definition: logging.h:83
WHERE short C_NetInc
Config: (socket) Update the progress bar after this many KB sent/received (0 to disable) ...
Definition: globals.h:123

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 282 of file smtp.c.

283 {
284  if (!a)
285  return false;
286 
287  while (*a)
288  {
289  if ((unsigned char) *a & (1 << 7))
290  return true;
291  a++;
292  }
293 
294  return false;
295 }

+ Here is the caller graph for this function:

static bool addresses_use_unicode ( const struct Address a)
static

Do any of a list of addresses use Unicode.

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

Definition at line 302 of file smtp.c.

303 {
304  while (a)
305  {
306  if (a->mailbox && !a->group && address_uses_unicode(a->mailbox))
307  return true;
308  a = a->next;
309  }
310  return false;
311 }
char * mailbox
mailbox and host address
Definition: address.h:35
static bool address_uses_unicode(const char *a)
Do any addresses use Unicode.
Definition: smtp.c:282
bool group
group mailbox?
Definition: address.h:36
struct Address * next
Definition: address.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 319 of file smtp.c.

320 {
321  account->flags = 0;
322  account->port = 0;
323  account->type = MUTT_ACCT_TYPE_SMTP;
324 
325  struct Url *url = url_parse(C_SmtpUrl);
326  if (!url || ((url->scheme != U_SMTP) && (url->scheme != U_SMTPS)) ||
327  !url->host || (mutt_account_fromurl(account, url) < 0))
328  {
329  url_free(&url);
330  mutt_error(_("Invalid SMTP URL: %s"), C_SmtpUrl);
331  return -1;
332  }
333 
334  if (url->scheme == U_SMTPS)
335  account->flags |= MUTT_ACCT_SSL;
336 
337  if (account->port == 0)
338  {
339  if (account->flags & MUTT_ACCT_SSL)
340  account->port = SMTPS_PORT;
341  else
342  {
343  static unsigned short SmtpPort = 0;
344  if (SmtpPort == 0)
345  {
346  struct servent *service = getservbyname("smtp", "tcp");
347  if (service)
348  SmtpPort = ntohs(service->s_port);
349  else
350  SmtpPort = SMTP_PORT;
351  mutt_debug(LL_DEBUG3, "Using default SMTP port %d\n", SmtpPort);
352  }
353  account->port = SmtpPort;
354  }
355  }
356 
357  url_free(&url);
358  return 0;
359 }
WHERE char * C_SmtpUrl
Config: (smtp) Url of the SMTP server.
Definition: globals.h:140
A parsed URL proto://user:password@host:port/path?a=1&b=2
Definition: url.h:66
enum UrlScheme scheme
Definition: url.h:68
#define _(a)
Definition: message.h:28
#define SMTP_PORT
Definition: smtp.c:68
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
Definition: url.h:71
#define mutt_error(...)
Definition: logging.h:83
void url_free(struct Url **u)
Free the contents of a URL.
Definition: url.c:289
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:80
#define SMTPS_PORT
Definition: smtp.c:69
int mutt_account_fromurl(struct ConnAccount *account, const struct Url *url)
Fill ConnAccount with information from url.
Definition: mutt_account.c:108
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:162

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 368 of file smtp.c.

369 {
370  Capabilities = 0;
371 
372  if (!esmtp)
373  {
374  /* if TLS or AUTH are requested, use EHLO */
375  if (conn->account.flags & MUTT_ACCT_USER)
376  esmtp = true;
377 #ifdef USE_SSL
378  if (C_SslForceTls || (C_SslStarttls != MUTT_NO))
379  esmtp = true;
380 #endif
381  }
382 
383  const char *fqdn = mutt_fqdn(false);
384  if (!fqdn)
385  fqdn = NONULL(ShortHostname);
386 
387  char buf[1024];
388  snprintf(buf, sizeof(buf), "%s %s\r\n", esmtp ? "EHLO" : "HELO", fqdn);
389  /* XXX there should probably be a wrapper in mutt_socket.c that
390  * repeatedly calls conn->write until all data is sent. This
391  * currently doesn't check for a short write. */
392  if (mutt_socket_send(conn, buf) == -1)
393  return SMTP_ERR_WRITE;
394  return smtp_get_resp(conn);
395 }
#define NONULL(x)
Definition: string2.h:36
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:232
static SmtpCapFlags Capabilities
Definition: smtp.c:91
#define SMTP_ERR_WRITE
Definition: smtp.c:65
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:121
const char * mutt_fqdn(bool may_hide_host)
Get the Fully-Qualified Domain Name.
Definition: sendlib.c:2453
WHERE char * ShortHostname
Short version of the hostname.
Definition: globals.h:49
WHERE unsigned char C_SslStarttls
Config: (ssl) Use STARTTLS on servers advertising the capability.
Definition: globals.h:188
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:

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 405 of file smtp.c.

406 {
407  sasl_conn_t *saslconn = NULL;
408  sasl_interact_t *interaction = NULL;
409  const char *mech = NULL;
410  const char *data = NULL;
411  unsigned int len;
412  char *buf = NULL;
413  size_t bufsize = 0;
414  int rc, saslrc;
415 
416  if (mutt_sasl_client_new(conn, &saslconn) < 0)
417  return SMTP_AUTH_FAIL;
418 
419  do
420  {
421  rc = sasl_client_start(saslconn, mechlist, &interaction, &data, &len, &mech);
422  if (rc == SASL_INTERACT)
423  mutt_sasl_interact(interaction);
424  } while (rc == SASL_INTERACT);
425 
426  if ((rc != SASL_OK) && (rc != SASL_CONTINUE))
427  {
428  mutt_debug(LL_DEBUG2, "%s unavailable\n", mech);
429  sasl_dispose(&saslconn);
430  return SMTP_AUTH_UNAVAIL;
431  }
432 
433  if (!OptNoCurses)
434  mutt_message(_("Authenticating (%s)..."), mech);
435 
436  bufsize = MAX((len * 2), 1024);
437  buf = mutt_mem_malloc(bufsize);
438 
439  snprintf(buf, bufsize, "AUTH %s", mech);
440  if (len)
441  {
442  mutt_str_strcat(buf, bufsize, " ");
443  if (sasl_encode64(data, len, buf + mutt_str_strlen(buf),
444  bufsize - mutt_str_strlen(buf), &len) != SASL_OK)
445  {
446  mutt_debug(LL_DEBUG1, "#1 error base64-encoding client response\n");
447  goto fail;
448  }
449  }
450  mutt_str_strcat(buf, bufsize, "\r\n");
451 
452  do
453  {
454  if (mutt_socket_send(conn, buf) < 0)
455  goto fail;
456  rc = mutt_socket_readln_d(buf, bufsize, conn, MUTT_SOCK_LOG_FULL);
457  if (rc < 0)
458  goto fail;
459  if (!valid_smtp_code(buf, rc, &rc))
460  goto fail;
461 
462  if (rc != SMTP_READY)
463  break;
464 
465  if (sasl_decode64(buf + 4, strlen(buf + 4), buf, bufsize - 1, &len) != SASL_OK)
466  {
467  mutt_debug(LL_DEBUG1, "error base64-decoding server response\n");
468  goto fail;
469  }
470 
471  do
472  {
473  saslrc = sasl_client_step(saslconn, buf, len, &interaction, &data, &len);
474  if (saslrc == SASL_INTERACT)
475  mutt_sasl_interact(interaction);
476  } while (saslrc == SASL_INTERACT);
477 
478  if (len)
479  {
480  if ((len * 2) > bufsize)
481  {
482  bufsize = len * 2;
483  mutt_mem_realloc(&buf, bufsize);
484  }
485  if (sasl_encode64(data, len, buf, bufsize, &len) != SASL_OK)
486  {
487  mutt_debug(LL_DEBUG1, "#2 error base64-encoding client response\n");
488  goto fail;
489  }
490  }
491  mutt_str_strfcpy(buf + len, "\r\n", bufsize - len);
492  } while (rc == SMTP_READY && saslrc != SASL_FAIL);
493 
494  if (smtp_success(rc))
495  {
496  mutt_sasl_setup_conn(conn, saslconn);
497  FREE(&buf);
498  return SMTP_AUTH_SUCCESS;
499  }
500 
501 fail:
502  sasl_dispose(&saslconn);
503  FREE(&buf);
504  return SMTP_AUTH_FAIL;
505 }
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
#define mutt_message(...)
Definition: logging.h:82
#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:532
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:45
#define SMTP_AUTH_SUCCESS
Definition: smtp.c:71
void mutt_sasl_setup_conn(struct Connection *conn, sasl_conn_t *saslconn)
Set up an SASL connection.
Definition: sasl.c:675
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:669
#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:121
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:97
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:753
#define SMTP_READY
Definition: smtp.c:61
#define smtp_success(x)
Definition: smtp.c:60
char * mutt_str_strcat(char *buf, size_t buflen, const char *s)
Concatenate two strings.
Definition: string.c:398
#define SMTP_AUTH_UNAVAIL
Definition: smtp.c:72
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:100
#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:242
#define mutt_debug(LEVEL,...)
Definition: logging.h:80
#define SMTP_AUTH_FAIL
Definition: smtp.c:73
int mutt_sasl_interact(sasl_interact_t *interaction)
Perform an SASL interaction with the user.
Definition: sasl.c:643

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 513 of file smtp.c.

514 {
515  mutt_message(_("Authenticating (OAUTHBEARER)..."));
516 
517  /* We get the access token from the smtp_oauth_refresh_command */
518  char *oauthbearer = mutt_account_getoauthbearer(&conn->account);
519  if (!oauthbearer)
520  return SMTP_AUTH_FAIL;
521 
522  size_t ilen = strlen(oauthbearer) + 30;
523  char *ibuf = mutt_mem_malloc(ilen);
524  snprintf(ibuf, ilen, "AUTH OAUTHBEARER %s\r\n", oauthbearer);
525 
526  int rc = mutt_socket_send(conn, ibuf);
527  FREE(&oauthbearer);
528  FREE(&ibuf);
529 
530  if (rc == -1)
531  return SMTP_AUTH_FAIL;
532  if (smtp_get_resp(conn) != 0)
533  return SMTP_AUTH_FAIL;
534 
535  return SMTP_AUTH_SUCCESS;
536 }
struct ConnAccount account
Definition: connection.h:36
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:38
#define mutt_message(...)
Definition: logging.h:82
#define _(a)
Definition: message.h:28
#define SMTP_AUTH_SUCCESS
Definition: smtp.c:71
char * mutt_account_getoauthbearer(struct ConnAccount *account)
Get an OAUTHBEARER token.
Definition: mutt_account.c:347
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:97
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:121
#define FREE(x)
Definition: memory.h:40
#define SMTP_AUTH_FAIL
Definition: smtp.c:73

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 544 of file smtp.c.

545 {
546  char buf[1024];
547 
548  /* Get username and password. Bail out of any can't be retrieved. */
549  if ((mutt_account_getuser(&conn->account) < 0) ||
550  (mutt_account_getpass(&conn->account) < 0))
551  {
552  goto error;
553  }
554 
555  /* Build the initial client response. */
556  size_t len = mutt_sasl_plain_msg(buf, sizeof(buf), "AUTH PLAIN", conn->account.user,
557  conn->account.user, conn->account.pass);
558 
559  /* Terminate as per SMTP protocol. Bail out if there's no room left. */
560  if (snprintf(buf + len, sizeof(buf) - len, "\r\n") != 2)
561  {
562  goto error;
563  }
564 
565  /* Send request, receive response (with a check for OK code). */
566  if ((mutt_socket_send(conn, buf) < 0) || smtp_get_resp(conn))
567  {
568  goto error;
569  }
570 
571  /* If we got here, auth was successful. */
572  return 0;
573 
574 error:
575  mutt_error(_("SASL authentication failed"));
576  return -1;
577 }
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:288
int mutt_account_getuser(struct ConnAccount *account)
Retrieve username into ConnAccount, if necessary.
Definition: mutt_account.c:207
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:121
#define mutt_error(...)
Definition: logging.h:83

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 585 of file smtp.c.

586 {
587  int r = SMTP_AUTH_UNAVAIL;
588 
590  {
591  char *methods = mutt_str_strdup(C_SmtpAuthenticators);
592  char *method = NULL;
593  char *delim = NULL;
594 
595  for (method = methods; method; method = delim)
596  {
597  delim = strchr(method, ':');
598  if (delim)
599  *delim++ = '\0';
600  if (method[0] == '\0')
601  continue;
602 
603  mutt_debug(LL_DEBUG2, "Trying method %s\n", method);
604 
605  if (strcmp(method, "oauthbearer") == 0)
606  {
607  r = smtp_auth_oauth(conn);
608  }
609  else if (strcmp(method, "plain") == 0)
610  {
611  r = smtp_auth_plain(conn);
612  }
613  else
614  {
615 #ifdef USE_SASL
616  r = smtp_auth_sasl(conn, method);
617 #else
618  mutt_error(_("SMTP authentication method %s requires SASL"), method);
619  continue;
620 #endif
621  }
622 
623  if ((r == SMTP_AUTH_FAIL) && delim)
624  {
625  mutt_error(_("%s authentication failed, trying next method"), method);
626  }
627  else if (r != SMTP_AUTH_UNAVAIL)
628  break;
629  }
630 
631  FREE(&methods);
632  }
633  else
634  {
635 #ifdef USE_SASL
636  r = smtp_auth_sasl(conn, AuthMechs);
637 #else
638  mutt_error(_("SMTP authentication requires SASL"));
639  r = SMTP_AUTH_UNAVAIL;
640 #endif
641  }
642 
643  if (r != SMTP_AUTH_SUCCESS)
645 
646  if (r == SMTP_AUTH_FAIL)
647  {
648  mutt_error(_("SASL authentication failed"));
649  }
650  else if (r == SMTP_AUTH_UNAVAIL)
651  {
652  mutt_error(_("No authenticators available"));
653  }
654 
655  return (r == SMTP_AUTH_SUCCESS) ? 0 : -1;
656 }
static int smtp_auth_plain(struct Connection *conn)
Authenticate using plain text.
Definition: smtp.c:544
struct ConnAccount account
Definition: connection.h:36
#define _(a)
Definition: message.h:28
#define SMTP_AUTH_SUCCESS
Definition: smtp.c:71
Log at debug level 2.
Definition: logging.h:57
#define SMTP_AUTH_UNAVAIL
Definition: smtp.c:72
static int smtp_auth_sasl(struct Connection *conn, const char *mechlist)
Authenticate using SASL.
Definition: smtp.c:405
char * mutt_str_strdup(const char *str)
Copy a string, safely.
Definition: string.c:380
#define mutt_error(...)
Definition: logging.h:83
char * C_SmtpAuthenticators
Config: (smtp) List of allowed authentication methods.
Definition: smtp.c:58
#define FREE(x)
Definition: memory.h:40
#define mutt_debug(LEVEL,...)
Definition: logging.h:80
static char * AuthMechs
Definition: smtp.c:90
#define SMTP_AUTH_FAIL
Definition: smtp.c:73
static int smtp_auth_oauth(struct Connection *conn)
Authenticate an SMTP connection using OAUTHBEARER.
Definition: smtp.c:513
void mutt_account_unsetpass(struct ConnAccount *account)
Unset ConnAccount&#39;s password.
Definition: mutt_account.c:331

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 665 of file smtp.c.

666 {
667  int rc;
668 
669  if (mutt_socket_open(conn))
670  return -1;
671 
672  /* get greeting string */
673  rc = smtp_get_resp(conn);
674  if (rc != 0)
675  return rc;
676 
677  rc = smtp_helo(conn, esmtp);
678  if (rc != 0)
679  return rc;
680 
681 #ifdef USE_SSL
682  enum QuadOption ans = MUTT_NO;
683  if (conn->ssf)
684  ans = MUTT_NO;
685  else if (C_SslForceTls)
686  ans = MUTT_YES;
687  else if ((Capabilities & SMTP_CAP_STARTTLS) &&
689  _("Secure connection with TLS?"))) == MUTT_ABORT))
690  {
691  return -1;
692  }
693 
694  if (ans == MUTT_YES)
695  {
696  if (mutt_socket_send(conn, "STARTTLS\r\n") < 0)
697  return SMTP_ERR_WRITE;
698  rc = smtp_get_resp(conn);
699  if (rc != 0)
700  return rc;
701 
702  if (mutt_ssl_starttls(conn))
703  {
704  mutt_error(_("Could not negotiate TLS connection"));
705  return -1;
706  }
707 
708  /* re-EHLO to get authentication mechanisms */
709  rc = smtp_helo(conn, esmtp);
710  if (rc != 0)
711  return rc;
712  }
713 #endif
714 
715  if (conn->account.flags & MUTT_ACCT_USER)
716  {
717  if (!(Capabilities & SMTP_CAP_AUTH))
718  {
719  mutt_error(_("SMTP server does not support authentication"));
720  return -1;
721  }
722 
723  return smtp_auth(conn);
724  }
725 
726  return 0;
727 }
static int smtp_helo(struct Connection *conn, bool esmtp)
Say hello to an SMTP Server.
Definition: smtp.c:368
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:81
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:3367
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:232
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:585
static SmtpCapFlags Capabilities
Definition: smtp.c:91
#define SMTP_ERR_WRITE
Definition: smtp.c:65
int mutt_ssl_starttls(struct Connection *conn)
Negotiate TLS over an already opened connection.
Definition: ssl.c:1408
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:121
#define mutt_error(...)
Definition: logging.h:83
#define SMTP_CAP_AUTH
Server supports AUTH command.
Definition: smtp.c:82
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:188
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:

int mutt_smtp_send ( const struct Address from,
const struct Address to,
const struct Address cc,
const struct Address 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 740 of file smtp.c.

743 {
744  struct Connection *conn = NULL;
745  struct ConnAccount account;
746  const char *envfrom = NULL;
747  char buf[1024];
748  int rc = -1;
749 
750  /* it might be better to synthesize an envelope from from user and host
751  * but this condition is most likely arrived at accidentally */
753  envfrom = C_EnvelopeFromAddress->mailbox;
754  else if (from)
755  envfrom = from->mailbox;
756  else
757  {
758  mutt_error(_("No from address given"));
759  return -1;
760  }
761 
762  if (smtp_fill_account(&account) < 0)
763  return rc;
764 
765  conn = mutt_conn_find(NULL, &account);
766  if (!conn)
767  return -1;
768 
769  do
770  {
771  /* send our greeting */
772  rc = smtp_open(conn, eightbit);
773  if (rc != 0)
774  break;
775  FREE(&AuthMechs);
776 
777  /* send the sender's address */
778  int len = snprintf(buf, sizeof(buf), "MAIL FROM:<%s>", envfrom);
779  if (eightbit && (Capabilities & SMTP_CAP_EIGHTBITMIME))
780  {
781  mutt_str_strncat(buf, sizeof(buf), " BODY=8BITMIME", 15);
782  len += 14;
783  }
785  len += snprintf(buf + len, sizeof(buf) - len, " RET=%s", C_DsnReturn);
787  (address_uses_unicode(envfrom) || addresses_use_unicode(to) ||
789  {
790  snprintf(buf + len, sizeof(buf) - len, " SMTPUTF8");
791  }
792  mutt_str_strncat(buf, sizeof(buf), "\r\n", 3);
793  if (mutt_socket_send(conn, buf) == -1)
794  {
795  rc = SMTP_ERR_WRITE;
796  break;
797  }
798  rc = smtp_get_resp(conn);
799  if (rc != 0)
800  break;
801 
802  /* send the recipient list */
803  if ((rc = smtp_rcpt_to(conn, to)) || (rc = smtp_rcpt_to(conn, cc)) ||
804  (rc = smtp_rcpt_to(conn, bcc)))
805  {
806  break;
807  }
808 
809  /* send the message data */
810  rc = smtp_data(conn, msgfile);
811  if (rc != 0)
812  break;
813 
814  mutt_socket_send(conn, "QUIT\r\n");
815 
816  rc = 0;
817  } while (false);
818 
819  mutt_socket_close(conn);
820 
821  if (rc == SMTP_ERR_READ)
822  mutt_error(_("SMTP session failed: read error"));
823  else if (rc == SMTP_ERR_WRITE)
824  mutt_error(_("SMTP session failed: write error"));
825  else if (rc == SMTP_ERR_CODE)
826  mutt_error(_("Invalid server response"));
827 
828  return rc;
829 }
#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:85
#define _(a)
Definition: message.h:28
char * mailbox
mailbox and host address
Definition: address.h:35
char * mutt_str_strncat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:427
static int smtp_open(struct Connection *conn, bool esmtp)
Open an SMTP Connection.
Definition: smtp.c:665
static int smtp_rcpt_to(struct Connection *conn, const struct Address *a)
Set the recipient to an Address.
Definition: smtp.c:171
static SmtpCapFlags Capabilities
Definition: smtp.c:91
#define SMTP_ERR_WRITE
Definition: smtp.c:65
static bool address_uses_unicode(const char *a)
Do any addresses use Unicode.
Definition: smtp.c:282
static int smtp_get_resp(struct Connection *conn)
Read a command response from the SMTP server.
Definition: smtp.c:121
#define SMTP_ERR_READ
Definition: smtp.c:64
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:103
#define SMTP_ERR_CODE
Definition: smtp.c:66
static int smtp_fill_account(struct ConnAccount *account)
Create ConnAccount object from SMTP Url.
Definition: smtp.c:319
#define mutt_error(...)
Definition: logging.h:83
WHERE struct Address * C_EnvelopeFromAddress
Config: Manually set the sender for outgoing messages.
Definition: globals.h:92
#define SMTP_CAP_EIGHTBITMIME
Server supports 8-bit MIME content.
Definition: smtp.c:84
#define FREE(x)
Definition: memory.h:40
static char * AuthMechs
Definition: smtp.c:90
static int smtp_data(struct Connection *conn, const char *msgfile)
Send data to an SMTP server.
Definition: smtp.c:206
static bool addresses_use_unicode(const struct Address *a)
Do any of a list of addresses use Unicode.
Definition: smtp.c:302
struct Connection * mutt_conn_find(const struct Connection *start, const struct ConnAccount *account)
Find a connection from a list.
Definition: mutt_socket.c:85
#define SMTP_CAP_DSN
Server supports Delivery Status Notification.
Definition: smtp.c:83

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

char* C_SmtpAuthenticators

Config: (smtp) List of allowed authentication methods.

Definition at line 58 of file smtp.c.

char* AuthMechs = NULL
static

Definition at line 90 of file smtp.c.

SmtpCapFlags Capabilities
static

Definition at line 91 of file smtp.c.