NeoMutt  2018-07-16 +2388-bcedc8
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

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

Definition at line 62 of file smtp.c.

#define SMTP_READY   334

Definition at line 63 of file smtp.c.

#define SMTP_CONTINUE   354

Definition at line 64 of file smtp.c.

#define SMTP_ERR_READ   -2

Definition at line 66 of file smtp.c.

#define SMTP_ERR_WRITE   -3

Definition at line 67 of file smtp.c.

#define SMTP_ERR_CODE   -4

Definition at line 68 of file smtp.c.

#define SMTP_PORT   25

Definition at line 70 of file smtp.c.

#define SMTPS_PORT   465

Definition at line 71 of file smtp.c.

#define SMTP_AUTH_SUCCESS   0

Definition at line 73 of file smtp.c.

#define SMTP_AUTH_UNAVAIL   1

Definition at line 74 of file smtp.c.

#define SMTP_AUTH_FAIL   -1

Definition at line 75 of file smtp.c.

#define SMTP_CAP_NO_FLAGS   0

No flags are set.

Definition at line 82 of file smtp.c.

#define SMTP_CAP_STARTTLS   (1 << 0)

Server supports STARTTLS command.

Definition at line 83 of file smtp.c.

#define SMTP_CAP_AUTH   (1 << 1)

Server supports AUTH command.

Definition at line 84 of file smtp.c.

#define SMTP_CAP_DSN   (1 << 2)

Server supports Delivery Status Notification.

Definition at line 85 of file smtp.c.

#define SMTP_CAP_EIGHTBITMIME   (1 << 3)

Server supports 8-bit MIME content.

Definition at line 86 of file smtp.c.

#define SMTP_CAP_SMTPUTF8   (1 << 4)

Server accepts UTF-8 strings.

Definition at line 87 of file smtp.c.

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

Definition at line 89 of file smtp.c.

Typedef Documentation

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

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:

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:

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:111
#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:

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_SIZE,
225  C_NetInc, st.st_size);
226 
227  snprintf(buf, sizeof(buf), "DATA\r\n");
228  if (mutt_socket_send(conn, buf) == -1)
229  {
230  mutt_file_fclose(&fp);
231  return SMTP_ERR_WRITE;
232  }
233  rc = smtp_get_resp(conn);
234  if (rc != 0)
235  {
236  mutt_file_fclose(&fp);
237  return rc;
238  }
239 
240  while (fgets(buf, sizeof(buf) - 1, fp))
241  {
242  buflen = mutt_str_strlen(buf);
243  term = buflen && buf[buflen - 1] == '\n';
244  if (term && ((buflen == 1) || (buf[buflen - 2] != '\r')))
245  snprintf(buf + buflen - 1, sizeof(buf) - buflen + 1, "\r\n");
246  if (buf[0] == '.')
247  {
248  if (mutt_socket_send_d(conn, ".", MUTT_SOCK_LOG_FULL) == -1)
249  {
250  mutt_file_fclose(&fp);
251  return SMTP_ERR_WRITE;
252  }
253  }
254  if (mutt_socket_send_d(conn, buf, MUTT_SOCK_LOG_FULL) == -1)
255  {
256  mutt_file_fclose(&fp);
257  return SMTP_ERR_WRITE;
258  }
259  mutt_progress_update(&progress, ftell(fp), -1);
260  }
261  if (!term && buflen && (mutt_socket_send_d(conn, "\r\n", MUTT_SOCK_LOG_FULL) == -1))
262  {
263  mutt_file_fclose(&fp);
264  return SMTP_ERR_WRITE;
265  }
266  mutt_file_fclose(&fp);
267 
268  /* terminate the message body */
269  if (mutt_socket_send(conn, ".\r\n") == -1)
270  return SMTP_ERR_WRITE;
271 
272  rc = smtp_get_resp(conn);
273  if (rc != 0)
274  return rc;
275 
276  return 0;
277 }
#define MUTT_PROGRESS_SIZE
traffic-based progress
Definition: progress.h:31
#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:172
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:115
#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:37
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:67
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
WHERE short C_NetInc
Config: (socket) Update the progress bar after this many KB sent/received (0 to disable) ...
Definition: globals.h:132

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

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

+ Here is the caller graph for this function:

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

305 {
306  struct Address *a = NULL;
307  TAILQ_FOREACH(a, al, entries)
308  {
309  if (a->mailbox && !a->group && address_uses_unicode(a->mailbox))
310  return true;
311  }
312  return false;
313 }
#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:284
bool group
Group mailbox?
Definition: address.h:38

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

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

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

371 {
372  Capabilities = 0;
373 
374  if (!esmtp)
375  {
376  /* if TLS or AUTH are requested, use EHLO */
377  if (conn->account.flags & MUTT_ACCT_USER)
378  esmtp = true;
379 #ifdef USE_SSL
380  if (C_SslForceTls || (C_SslStarttls != MUTT_NO))
381  esmtp = true;
382 #endif
383  }
384 
385  const char *fqdn = mutt_fqdn(false);
386  if (!fqdn)
387  fqdn = NONULL(ShortHostname);
388 
389  char buf[1024];
390  snprintf(buf, sizeof(buf), "%s %s\r\n", esmtp ? "EHLO" : "HELO", fqdn);
391  /* XXX there should probably be a wrapper in mutt_socket.c that
392  * repeatedly calls conn->write until all data is sent. This
393  * currently doesn't check for a short write. */
394  if (mutt_socket_send(conn, buf) == -1)
395  return SMTP_ERR_WRITE;
396  return smtp_get_resp(conn);
397 }
#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:245
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:2468
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:197
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 407 of file smtp.c.

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

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

516 {
517  mutt_message(_("Authenticating (OAUTHBEARER)..."));
518 
519  /* We get the access token from the smtp_oauth_refresh_command */
520  char *oauthbearer = mutt_account_getoauthbearer(&conn->account);
521  if (!oauthbearer)
522  return SMTP_AUTH_FAIL;
523 
524  size_t ilen = strlen(oauthbearer) + 30;
525  char *ibuf = mutt_mem_malloc(ilen);
526  snprintf(ibuf, ilen, "AUTH OAUTHBEARER %s\r\n", oauthbearer);
527 
528  int rc = mutt_socket_send(conn, ibuf);
529  FREE(&oauthbearer);
530  FREE(&ibuf);
531 
532  if (rc == -1)
533  return SMTP_AUTH_FAIL;
534  if (smtp_get_resp(conn) != 0)
535  return SMTP_AUTH_FAIL;
536 
537  return SMTP_AUTH_SUCCESS;
538 }
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:

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

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

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

588 {
589  int r = SMTP_AUTH_UNAVAIL;
590 
592  {
593  struct ListNode *np = NULL;
595  {
596  mutt_debug(LL_DEBUG2, "Trying method %s\n", np->data);
597 
598  if (strcmp(np->data, "oauthbearer") == 0)
599  {
600  r = smtp_auth_oauth(conn);
601  }
602  else if (strcmp(np->data, "plain") == 0)
603  {
604  r = smtp_auth_plain(conn);
605  }
606  else
607  {
608 #ifdef USE_SASL
609  r = smtp_auth_sasl(conn, np->data);
610 #else
611  mutt_error(_("SMTP authentication method %s requires SASL"), np->data);
612  continue;
613 #endif
614  }
615 
616  if ((r == SMTP_AUTH_FAIL) && (C_SmtpAuthenticators->count > 1))
617  {
618  mutt_error(_("%s authentication failed, trying next method"), np->data);
619  }
620  else if (r != SMTP_AUTH_UNAVAIL)
621  break;
622  }
623  }
624  else
625  {
626 #ifdef USE_SASL
627  r = smtp_auth_sasl(conn, AuthMechs);
628 #else
629  mutt_error(_("SMTP authentication requires SASL"));
630  r = SMTP_AUTH_UNAVAIL;
631 #endif
632  }
633 
634  if (r != SMTP_AUTH_SUCCESS)
636 
637  if (r == SMTP_AUTH_FAIL)
638  {
639  mutt_error(_("SASL authentication failed"));
640  }
641  else if (r == SMTP_AUTH_UNAVAIL)
642  {
643  mutt_error(_("No authenticators available"));
644  }
645 
646  return (r == SMTP_AUTH_SUCCESS) ? 0 : -1;
647 }
static int smtp_auth_plain(struct Connection *conn)
Authenticate using plain text.
Definition: smtp.c:546
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
Definition: list.h:35
static int smtp_auth_sasl(struct Connection *conn, const char *mechlist)
Authenticate using SASL.
Definition: smtp.c:407
#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:515
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:

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

657 {
658  int rc;
659 
660  if (mutt_socket_open(conn))
661  return -1;
662 
663  /* get greeting string */
664  rc = smtp_get_resp(conn);
665  if (rc != 0)
666  return rc;
667 
668  rc = smtp_helo(conn, esmtp);
669  if (rc != 0)
670  return rc;
671 
672 #ifdef USE_SSL
673  enum QuadOption ans = MUTT_NO;
674  if (conn->ssf)
675  ans = MUTT_NO;
676  else if (C_SslForceTls)
677  ans = MUTT_YES;
678  else if ((Capabilities & SMTP_CAP_STARTTLS) &&
680  _("Secure connection with TLS?"))) == MUTT_ABORT))
681  {
682  return -1;
683  }
684 
685  if (ans == MUTT_YES)
686  {
687  if (mutt_socket_send(conn, "STARTTLS\r\n") < 0)
688  return SMTP_ERR_WRITE;
689  rc = smtp_get_resp(conn);
690  if (rc != 0)
691  return rc;
692 
693  if (mutt_ssl_starttls(conn))
694  {
695  mutt_error(_("Could not negotiate TLS connection"));
696  return -1;
697  }
698 
699  /* re-EHLO to get authentication mechanisms */
700  rc = smtp_helo(conn, esmtp);
701  if (rc != 0)
702  return rc;
703  }
704 #endif
705 
706  if (conn->account.flags & MUTT_ACCT_USER)
707  {
708  if (!(Capabilities & SMTP_CAP_AUTH))
709  {
710  mutt_error(_("SMTP server does not support authentication"));
711  return -1;
712  }
713 
714  return smtp_auth(conn);
715  }
716 
717  return 0;
718 }
static int smtp_helo(struct Connection *conn, bool esmtp)
Say hello to an SMTP Server.
Definition: smtp.c:370
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:3322
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:245
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:587
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:1444
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:197
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 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 731 of file smtp.c.

734 {
735  struct Connection *conn = NULL;
736  struct ConnAccount account;
737  const char *envfrom = NULL;
738  char buf[1024];
739  int rc = -1;
740 
741  /* it might be better to synthesize an envelope from from user and host
742  * but this condition is most likely arrived at accidentally */
744  envfrom = C_EnvelopeFromAddress->mailbox;
745  else if (from && !TAILQ_EMPTY(from))
746  envfrom = TAILQ_FIRST(from)->mailbox;
747  else
748  {
749  mutt_error(_("No from address given"));
750  return -1;
751  }
752 
753  if (smtp_fill_account(&account) < 0)
754  return rc;
755 
756  conn = mutt_conn_find(NULL, &account);
757  if (!conn)
758  return -1;
759 
760  do
761  {
762  /* send our greeting */
763  rc = smtp_open(conn, eightbit);
764  if (rc != 0)
765  break;
766  FREE(&AuthMechs);
767 
768  /* send the sender's address */
769  int len = snprintf(buf, sizeof(buf), "MAIL FROM:<%s>", envfrom);
770  if (eightbit && (Capabilities & SMTP_CAP_EIGHTBITMIME))
771  {
772  mutt_str_strncat(buf, sizeof(buf), " BODY=8BITMIME", 15);
773  len += 14;
774  }
776  len += snprintf(buf + len, sizeof(buf) - len, " RET=%s", C_DsnReturn);
778  (address_uses_unicode(envfrom) || addresses_use_unicode(to) ||
780  {
781  snprintf(buf + len, sizeof(buf) - len, " SMTPUTF8");
782  }
783  mutt_str_strncat(buf, sizeof(buf), "\r\n", 3);
784  if (mutt_socket_send(conn, buf) == -1)
785  {
786  rc = SMTP_ERR_WRITE;
787  break;
788  }
789  rc = smtp_get_resp(conn);
790  if (rc != 0)
791  break;
792 
793  /* send the recipient list */
794  if ((rc = smtp_rcpt_to(conn, to)) || (rc = smtp_rcpt_to(conn, cc)) ||
795  (rc = smtp_rcpt_to(conn, bcc)))
796  {
797  break;
798  }
799 
800  /* send the message data */
801  rc = smtp_data(conn, msgfile);
802  if (rc != 0)
803  break;
804 
805  mutt_socket_send(conn, "QUIT\r\n");
806 
807  rc = 0;
808  } while (false);
809 
810  mutt_socket_close(conn);
811 
812  if (rc == SMTP_ERR_READ)
813  mutt_error(_("SMTP session failed: read error"));
814  else if (rc == SMTP_ERR_WRITE)
815  mutt_error(_("SMTP session failed: write error"));
816  else if (rc == SMTP_ERR_CODE)
817  mutt_error(_("Invalid server response"));
818 
819  return rc;
820 }
#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:656
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:284
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:112
#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:321
#define mutt_error(...)
Definition: logging.h:84
WHERE struct Address * C_EnvelopeFromAddress
Config: Manually set the sender for outgoing messages.
Definition: globals.h:97
#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:304
#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

struct Slist* C_SmtpAuthenticators

Config: (smtp) List of allowed authentication methods.

Definition at line 60 of file smtp.c.

char* AuthMechs = NULL
static

Definition at line 92 of file smtp.c.

SmtpCapFlags Capabilities
static

Definition at line 93 of file smtp.c.