NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
smtp.c File Reference

Send email to an SMTP server. More...

#include "config.h"
#include <arpa/inet.h>
#include <netdb.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "conn/lib.h"
#include "smtp.h"
#include "progress/lib.h"
#include "question/lib.h"
#include "globals.h"
#include "mutt_account.h"
#include "mutt_socket.h"
#include "sendlib.h"
+ Include dependency graph for smtp.c:

Go to the source code of this file.

Data Structures

struct  SmtpAccountData
 Server connection data. More...
 
struct  SmtpAuth
 SMTP authentication multiplexor. More...
 

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.
 
#define SMTP_CAP_STARTTLS   (1 << 0)
 Server supports STARTTLS command.
 
#define SMTP_CAP_AUTH   (1 << 1)
 Server supports AUTH command.
 
#define SMTP_CAP_DSN   (1 << 2)
 Server supports Delivery Status Notification.
 
#define SMTP_CAP_EIGHTBITMIME   (1 << 3)
 Server supports 8-bit MIME content.
 
#define SMTP_CAP_SMTPUTF8   (1 << 4)
 Server accepts UTF-8 strings.
 
#define SMTP_CAP_ALL   ((1 << 5) - 1)
 

Typedefs

typedef uint8_t SmtpCapFlags
 SMTP server capabilities.
 

Functions

static bool valid_smtp_code (char *buf, int *n)
 Is the is a valid SMTP return code?
 
static int smtp_get_resp (struct SmtpAccountData *adata)
 Read a command response from the SMTP server.
 
static int smtp_rcpt_to (struct SmtpAccountData *adata, const struct AddressList *al)
 Set the recipient to an Address.
 
static int smtp_data (struct SmtpAccountData *adata, const char *msgfile)
 Send data to an SMTP server.
 
static const char * smtp_get_field (enum ConnAccountField field, void *gf_data)
 Get connection login credentials - Implements ConnAccount::get_field() -.
 
static int smtp_fill_account (struct SmtpAccountData *adata, struct ConnAccount *cac)
 Create ConnAccount object from SMTP Url.
 
static int smtp_helo (struct SmtpAccountData *adata, bool esmtp)
 Say hello to an SMTP Server.
 
static int smtp_auth_oauth_xoauth2 (struct SmtpAccountData *adata, const char *method, bool xoauth2)
 Authenticate an SMTP connection using OAUTHBEARER/XOAUTH2.
 
static int smtp_auth_oauth (struct SmtpAccountData *adata, const char *method)
 Authenticate an SMTP connection using OAUTHBEARER - Implements SmtpAuth::authenticate() -.
 
static int smtp_auth_xoauth2 (struct SmtpAccountData *adata, const char *method)
 Authenticate an SMTP connection using XOAUTH2 - Implements SmtpAuth::authenticate() -.
 
static int smtp_auth_plain (struct SmtpAccountData *adata, const char *method)
 Authenticate using plain text - Implements SmtpAuth::authenticate() -.
 
static int smtp_auth_login (struct SmtpAccountData *adata, const char *method)
 Authenticate using plain text - Implements SmtpAuth::authenticate() -.
 
bool smtp_auth_is_valid (const char *authenticator)
 Check if string is a valid smtp authentication method.
 
static int smtp_authenticate (struct SmtpAccountData *adata)
 Authenticate to an SMTP server.
 
static int smtp_open (struct SmtpAccountData *adata, bool esmtp)
 Open an SMTP Connection.
 
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, struct ConfigSubset *sub)
 Send a message using SMTP.
 

Variables

static const struct SmtpAuth SmtpAuthenticators []
 Accepted authentication methods.
 

Detailed Description

Send email to an SMTP server.

Authors
  • Richard Russon
  • Pietro Cerutti
  • Yousef Akbar
  • Ryan Kavanagh
  • Alejandro Colomar
  • Anna Figueiredo Gomes
  • Rayford Shireman

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

◆ SMTP_READY

#define SMTP_READY   334

Definition at line 65 of file smtp.c.

◆ SMTP_CONTINUE

#define SMTP_CONTINUE   354

Definition at line 66 of file smtp.c.

◆ SMTP_ERR_READ

#define SMTP_ERR_READ   -2

Definition at line 68 of file smtp.c.

◆ SMTP_ERR_WRITE

#define SMTP_ERR_WRITE   -3

Definition at line 69 of file smtp.c.

◆ SMTP_ERR_CODE

#define SMTP_ERR_CODE   -4

Definition at line 70 of file smtp.c.

◆ SMTP_PORT

#define SMTP_PORT   25

Definition at line 72 of file smtp.c.

◆ SMTPS_PORT

#define SMTPS_PORT   465

Definition at line 73 of file smtp.c.

◆ SMTP_AUTH_SUCCESS

#define SMTP_AUTH_SUCCESS   0

Definition at line 75 of file smtp.c.

◆ SMTP_AUTH_UNAVAIL

#define SMTP_AUTH_UNAVAIL   1

Definition at line 76 of file smtp.c.

◆ SMTP_AUTH_FAIL

#define SMTP_AUTH_FAIL   -1

Definition at line 77 of file smtp.c.

◆ SMTP_CAP_NO_FLAGS

#define SMTP_CAP_NO_FLAGS   0

No flags are set.

Definition at line 84 of file smtp.c.

◆ SMTP_CAP_STARTTLS

#define SMTP_CAP_STARTTLS   (1 << 0)

Server supports STARTTLS command.

Definition at line 85 of file smtp.c.

◆ SMTP_CAP_AUTH

#define SMTP_CAP_AUTH   (1 << 1)

Server supports AUTH command.

Definition at line 86 of file smtp.c.

◆ SMTP_CAP_DSN

#define SMTP_CAP_DSN   (1 << 2)

Server supports Delivery Status Notification.

Definition at line 87 of file smtp.c.

◆ SMTP_CAP_EIGHTBITMIME

#define SMTP_CAP_EIGHTBITMIME   (1 << 3)

Server supports 8-bit MIME content.

Definition at line 88 of file smtp.c.

◆ SMTP_CAP_SMTPUTF8

#define SMTP_CAP_SMTPUTF8   (1 << 4)

Server accepts UTF-8 strings.

Definition at line 89 of file smtp.c.

◆ SMTP_CAP_ALL

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

Definition at line 90 of file smtp.c.

Typedef Documentation

◆ SmtpCapFlags

typedef uint8_t SmtpCapFlags

SMTP server capabilities.

Flags, e.g. SMTP_CAP_STARTTLS

Definition at line 83 of file smtp.c.

Function Documentation

◆ valid_smtp_code()

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

Is the is a valid SMTP return code?

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

Definition at line 130 of file smtp.c.

131{
132 return (mutt_str_atoi(buf, n) - buf) <= 3;
133}
const char * mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: atoi.c:192
+ 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 SmtpAccountData adata)
static

Read a command response from the SMTP server.

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

Definition at line 141 of file smtp.c.

142{
143 int n;
144 char buf[1024] = { 0 };
145
146 do
147 {
148 n = mutt_socket_readln(buf, sizeof(buf), adata->conn);
149 if (n < 4)
150 {
151 /* read error, or no response code */
152 return SMTP_ERR_READ;
153 }
154 const char *s = buf + 4; /* Skip the response code and the space/dash */
155 size_t plen;
156
157 if (mutt_istr_startswith(s, "8BITMIME"))
158 {
160 }
161 else if ((plen = mutt_istr_startswith(s, "AUTH ")))
162 {
163 adata->capabilities |= SMTP_CAP_AUTH;
164 FREE(&adata->auth_mechs);
165 adata->auth_mechs = mutt_str_dup(s + plen);
166 }
167 else if (mutt_istr_startswith(s, "DSN"))
168 {
169 adata->capabilities |= SMTP_CAP_DSN;
170 }
171 else if (mutt_istr_startswith(s, "STARTTLS"))
172 {
174 }
175 else if (mutt_istr_startswith(s, "SMTPUTF8"))
176 {
178 }
179
180 if (!valid_smtp_code(buf, &n))
181 return SMTP_ERR_CODE;
182
183 } while (buf[3] == '-');
184
185 if (smtp_success(n) || (n == SMTP_CONTINUE))
186 return 0;
187
188 mutt_error(_("SMTP session failed: %s"), buf);
189 return -1;
190}
#define mutt_error(...)
Definition: logging2.h:92
#define FREE(x)
Definition: memory.h:55
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:242
#define SMTP_CAP_STARTTLS
Server supports STARTTLS command.
Definition: smtp.c:85
#define SMTP_ERR_READ
Definition: smtp.c:68
static bool valid_smtp_code(char *buf, int *n)
Is the is a valid SMTP return code?
Definition: smtp.c:130
#define SMTP_ERR_CODE
Definition: smtp.c:70
#define SMTP_CAP_EIGHTBITMIME
Server supports 8-bit MIME content.
Definition: smtp.c:88
#define smtp_success(x)
Definition: smtp.c:64
#define SMTP_CAP_AUTH
Server supports AUTH command.
Definition: smtp.c:86
#define SMTP_CAP_SMTPUTF8
Server accepts UTF-8 strings.
Definition: smtp.c:89
#define SMTP_CONTINUE
Definition: smtp.c:66
#define SMTP_CAP_DSN
Server supports Delivery Status Notification.
Definition: smtp.c:87
#define mutt_socket_readln(buf, buflen, conn)
Definition: socket.h:56
struct Connection * conn
Server Connection.
Definition: smtp.c:100
const char * auth_mechs
Allowed authorisation mechanisms.
Definition: smtp.c:98
SmtpCapFlags capabilities
Server capabilities.
Definition: smtp.c:99
+ 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 SmtpAccountData adata,
const struct AddressList *  al 
)
static

Set the recipient to an Address.

Parameters
adataSMTP Account data
alAddressList to use
Return values
0Success
<0Error, e.g. SMTP_ERR_WRITE

Definition at line 199 of file smtp.c.

200{
201 if (!al)
202 return 0;
203
204 const char *const c_dsn_notify = cs_subset_string(adata->sub, "dsn_notify");
205
206 struct Address *a = NULL;
207 TAILQ_FOREACH(a, al, entries)
208 {
209 /* weed out group mailboxes, since those are for display only */
210 if (!a->mailbox || a->group)
211 {
212 continue;
213 }
214 char buf[1024] = { 0 };
215 if ((adata->capabilities & SMTP_CAP_DSN) && c_dsn_notify)
216 {
217 snprintf(buf, sizeof(buf), "RCPT TO:<%s> NOTIFY=%s\r\n",
218 buf_string(a->mailbox), c_dsn_notify);
219 }
220 else
221 {
222 snprintf(buf, sizeof(buf), "RCPT TO:<%s>\r\n", buf_string(a->mailbox));
223 }
224 if (mutt_socket_send(adata->conn, buf) == -1)
225 return SMTP_ERR_WRITE;
226 int rc = smtp_get_resp(adata);
227 if (rc != 0)
228 return rc;
229 }
230
231 return 0;
232}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:291
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
static int smtp_get_resp(struct SmtpAccountData *adata)
Read a command response from the SMTP server.
Definition: smtp.c:141
#define SMTP_ERR_WRITE
Definition: smtp.c:69
#define mutt_socket_send(conn, buf)
Definition: socket.h:57
An email address.
Definition: address.h:36
bool group
Group mailbox?
Definition: address.h:39
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
struct ConfigSubset * sub
Config scope.
Definition: smtp.c:101
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_data()

static int smtp_data ( struct SmtpAccountData adata,
const char *  msgfile 
)
static

Send data to an SMTP server.

Parameters
adataSMTP Account data
msgfileFilename containing data
Return values
0Success
<0Error, e.g. SMTP_ERR_WRITE

Definition at line 241 of file smtp.c.

242{
243 char buf[1024] = { 0 };
244 struct Progress *progress = NULL;
245 int rc = SMTP_ERR_WRITE;
246 int term = 0;
247 size_t buflen = 0;
248
249 FILE *fp = mutt_file_fopen(msgfile, "r");
250 if (!fp)
251 {
252 mutt_error(_("SMTP session failed: unable to open %s"), msgfile);
253 return -1;
254 }
255 const long size = mutt_file_get_size_fp(fp);
256 if (size == 0)
257 {
258 mutt_file_fclose(&fp);
259 return -1;
260 }
261 unlink(msgfile);
262 progress = progress_new(MUTT_PROGRESS_NET, size);
263 progress_set_message(progress, _("Sending message..."));
264
265 snprintf(buf, sizeof(buf), "DATA\r\n");
266 if (mutt_socket_send(adata->conn, buf) == -1)
267 {
268 mutt_file_fclose(&fp);
269 goto done;
270 }
271 rc = smtp_get_resp(adata);
272 if (rc != 0)
273 {
274 mutt_file_fclose(&fp);
275 goto done;
276 }
277
278 rc = SMTP_ERR_WRITE;
279 while (fgets(buf, sizeof(buf) - 1, fp))
280 {
281 buflen = mutt_str_len(buf);
282 term = buflen && buf[buflen - 1] == '\n';
283 if (term && ((buflen == 1) || (buf[buflen - 2] != '\r')))
284 snprintf(buf + buflen - 1, sizeof(buf) - buflen + 1, "\r\n");
285 if (buf[0] == '.')
286 {
287 if (mutt_socket_send_d(adata->conn, ".", MUTT_SOCK_LOG_FULL) == -1)
288 {
289 mutt_file_fclose(&fp);
290 goto done;
291 }
292 }
293 if (mutt_socket_send_d(adata->conn, buf, MUTT_SOCK_LOG_FULL) == -1)
294 {
295 mutt_file_fclose(&fp);
296 goto done;
297 }
298 progress_update(progress, MAX(0, ftell(fp)), -1);
299 }
300 if (!term && buflen &&
301 (mutt_socket_send_d(adata->conn, "\r\n", MUTT_SOCK_LOG_FULL) == -1))
302 {
303 mutt_file_fclose(&fp);
304 goto done;
305 }
306 mutt_file_fclose(&fp);
307
308 /* terminate the message body */
309 if (mutt_socket_send(adata->conn, ".\r\n") == -1)
310 goto done;
311
312 rc = smtp_get_resp(adata);
313
314done:
315 progress_free(&progress);
316 return rc;
317}
long mutt_file_get_size_fp(FILE *fp)
Get the size of a file.
Definition: file.c:1537
#define mutt_file_fclose(FP)
Definition: file.h:138
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:137
#define MAX(a, b)
Definition: memory.h:31
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
@ MUTT_PROGRESS_NET
Progress tracks bytes, according to $net_inc
Definition: lib.h:81
struct Progress * progress_new(enum ProgressType type, size_t size)
Create a new Progress Bar.
Definition: progress.c:139
void progress_free(struct Progress **ptr)
Free a Progress Bar.
Definition: progress.c:110
void progress_set_message(struct Progress *progress, const char *fmt,...) __attribute__((__format__(__printf__
bool progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition: progress.c:80
#define MUTT_SOCK_LOG_FULL
Definition: socket.h:54
#define mutt_socket_send_d(conn, buf, dbg)
Definition: socket.h:58
+ 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 SmtpAccountData adata,
struct ConnAccount cac 
)
static

Create ConnAccount object from SMTP Url.

Parameters
adataSMTP Account data
cacConnAccount to populate
Return values
0Success
-1Error

Definition at line 359 of file smtp.c.

360{
361 cac->flags = 0;
362 cac->port = 0;
364 cac->service = "smtp";
366 cac->gf_data = adata;
367
368 const char *const c_smtp_url = cs_subset_string(adata->sub, "smtp_url");
369
370 struct Url *url = url_parse(c_smtp_url);
371 if (!url || ((url->scheme != U_SMTP) && (url->scheme != U_SMTPS)) ||
372 !url->host || (mutt_account_fromurl(cac, url) < 0))
373 {
374 url_free(&url);
375 mutt_error(_("Invalid SMTP URL: %s"), c_smtp_url);
376 return -1;
377 }
378
379 if (url->scheme == U_SMTPS)
380 cac->flags |= MUTT_ACCT_SSL;
381
382 if (cac->port == 0)
383 {
384 if (cac->flags & MUTT_ACCT_SSL)
385 {
386 cac->port = SMTPS_PORT;
387 }
388 else
389 {
390 static unsigned short SmtpPort = 0;
391 if (SmtpPort == 0)
392 {
393 struct servent *service = getservbyname("smtp", "tcp");
394 if (service)
395 SmtpPort = ntohs(service->s_port);
396 else
397 SmtpPort = SMTP_PORT;
398 mutt_debug(LL_DEBUG3, "Using default SMTP port %d\n", SmtpPort);
399 }
400 cac->port = SmtpPort;
401 }
402 }
403
404 url_free(&url);
405 return 0;
406}
#define MUTT_ACCT_SSL
Account uses SSL/TLS.
Definition: connaccount.h:47
static const char * smtp_get_field(enum ConnAccountField field, void *gf_data)
Get connection login credentials - Implements ConnAccount::get_field() -.
Definition: smtp.c:322
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG3
Log at debug level 3.
Definition: logging2.h:45
int mutt_account_fromurl(struct ConnAccount *cac, const struct Url *url)
Fill ConnAccount with information from url.
Definition: mutt_account.c:44
@ MUTT_ACCT_TYPE_SMTP
Smtp Account.
Definition: mutt_account.h:40
#define SMTPS_PORT
Definition: smtp.c:73
#define SMTP_PORT
Definition: smtp.c:72
const char * service
Name of the service, e.g. "imap".
Definition: connaccount.h:61
const char *(* get_field)(enum ConnAccountField field, void *gf_data)
Definition: connaccount.h:70
unsigned char type
Connection type, e.g. MUTT_ACCT_TYPE_IMAP.
Definition: connaccount.h:59
MuttAccountFlags flags
Which fields are initialised, e.g. MUTT_ACCT_USER.
Definition: connaccount.h:60
void * gf_data
Private data to pass to get_field()
Definition: connaccount.h:72
unsigned short port
Port to connect to.
Definition: connaccount.h:58
A parsed URL proto://user:password@host:port/path?a=1&b=2
Definition: url.h:69
char * host
Host.
Definition: url.h:73
enum UrlScheme scheme
Scheme, e.g. U_SMTPS.
Definition: url.h:70
struct Url * url_parse(const char *src)
Fill in Url.
Definition: url.c:239
void url_free(struct Url **ptr)
Free the contents of a URL.
Definition: url.c:124
@ U_SMTPS
Url is smtps://.
Definition: url.h:44
@ U_SMTP
Url is smtp://.
Definition: url.h:43
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_helo()

static int smtp_helo ( struct SmtpAccountData adata,
bool  esmtp 
)
static

Say hello to an SMTP Server.

Parameters
adataSMTP Account data
esmtpIf true, use ESMTP
Return values
0Success
<0Error, e.g. SMTP_ERR_WRITE

Definition at line 415 of file smtp.c.

416{
418
419 if (!esmtp)
420 {
421 /* if TLS or AUTH are requested, use EHLO */
422 if (adata->conn->account.flags & MUTT_ACCT_USER)
423 esmtp = true;
424#ifdef USE_SSL
425 const bool c_ssl_force_tls = cs_subset_bool(adata->sub, "ssl_force_tls");
426 const enum QuadOption c_ssl_starttls = cs_subset_quad(adata->sub, "ssl_starttls");
427
428 if (c_ssl_force_tls || (c_ssl_starttls != MUTT_NO))
429 esmtp = true;
430#endif
431 }
432
433 char buf[1024] = { 0 };
434 snprintf(buf, sizeof(buf), "%s %s\r\n", esmtp ? "EHLO" : "HELO", adata->fqdn);
435 /* XXX there should probably be a wrapper in mutt_socket.c that
436 * repeatedly calls adata->conn->write until all data is sent. This
437 * currently doesn't check for a short write. */
438 if (mutt_socket_send(adata->conn, buf) == -1)
439 return SMTP_ERR_WRITE;
440 return smtp_get_resp(adata);
441}
enum QuadOption cs_subset_quad(const struct ConfigSubset *sub, const char *name)
Get a quad-value config item by name.
Definition: helpers.c:192
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
#define MUTT_ACCT_USER
User field has been set.
Definition: connaccount.h:44
QuadOption
Possible values for a quad-option.
Definition: quad.h:36
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition: quad.h:38
#define SMTP_CAP_NO_FLAGS
No flags are set.
Definition: smtp.c:84
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:49
const char * fqdn
Fully-qualified domain name.
Definition: smtp.c:102
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_auth_oauth_xoauth2()

static int smtp_auth_oauth_xoauth2 ( struct SmtpAccountData adata,
const char *  method,
bool  xoauth2 
)
static

Authenticate an SMTP connection using OAUTHBEARER/XOAUTH2.

Parameters
adataSMTP Account data
methodAuthentication method
xoauth2Use XOAUTH2 token (if true), OAUTHBEARER token otherwise
Return values
numResult, e.g. SMTP_AUTH_SUCCESS

Definition at line 733 of file smtp.c.

734{
735 /* If they did not explicitly request or configure oauth then fail quietly */
736 const char *const c_smtp_oauth_refresh_command = cs_subset_string(NeoMutt->sub, "smtp_oauth_refresh_command");
737 if (!method && !c_smtp_oauth_refresh_command)
738 return SMTP_AUTH_UNAVAIL;
739
740 const char *authtype = xoauth2 ? "XOAUTH2" : "OAUTHBEARER";
741
742 // L10N: (%s) is the method name, e.g. Anonymous, CRAM-MD5, GSSAPI, SASL
743 mutt_message(_("Authenticating (%s)..."), authtype);
744
745 /* We get the access token from the smtp_oauth_refresh_command */
746 char *oauthbearer = mutt_account_getoauthbearer(&adata->conn->account, xoauth2);
747 if (!oauthbearer)
748 return SMTP_AUTH_FAIL;
749
750 char *ibuf = NULL;
751 mutt_str_asprintf(&ibuf, "AUTH %s %s\r\n", authtype, oauthbearer);
752
753 int rc = mutt_socket_send(adata->conn, ibuf);
754 FREE(&oauthbearer);
755 FREE(&ibuf);
756
757 if (rc == -1)
758 return SMTP_AUTH_FAIL;
759 if (smtp_get_resp(adata) != 0)
760 return SMTP_AUTH_FAIL;
761
762 return SMTP_AUTH_SUCCESS;
763}
char * mutt_account_getoauthbearer(struct ConnAccount *cac, bool xoauth2)
Get an OAUTHBEARER/XOAUTH2 token.
Definition: connaccount.c:195
#define mutt_message(...)
Definition: logging2.h:91
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:803
#define SMTP_AUTH_UNAVAIL
Definition: smtp.c:76
#define SMTP_AUTH_FAIL
Definition: smtp.c:77
#define SMTP_AUTH_SUCCESS
Definition: smtp.c:75
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_auth_is_valid()

bool smtp_auth_is_valid ( const char *  authenticator)

Check if string is a valid smtp authentication method.

Parameters
authenticatorAuthenticator string to check
Return values
trueArgument is a valid auth method

Validate whether an input string is an accepted smtp authentication method as defined by SmtpAuthenticators.

Definition at line 927 of file smtp.c.

928{
929 for (size_t i = 0; i < mutt_array_size(SmtpAuthenticators); i++)
930 {
931 const struct SmtpAuth *auth = &SmtpAuthenticators[i];
932 if (auth->method && mutt_istr_equal(auth->method, authenticator))
933 return true;
934 }
935
936 return false;
937}
#define mutt_array_size(x)
Definition: memory.h:38
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:672
static const struct SmtpAuth SmtpAuthenticators[]
Accepted authentication methods.
Definition: smtp.c:904
SMTP authentication multiplexor.
Definition: smtp.c:109
const char * method
Name of authentication method supported, NULL means variable.
Definition: smtp.c:120
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_authenticate()

static int smtp_authenticate ( struct SmtpAccountData adata)
static

Authenticate to an SMTP server.

Parameters
adataSMTP Account data
Return values
0Success
<0Error, e.g. SMTP_AUTH_FAIL

Definition at line 945 of file smtp.c.

946{
947 int r = SMTP_AUTH_UNAVAIL;
948
949 const struct Slist *c_smtp_authenticators = cs_subset_slist(adata->sub, "smtp_authenticators");
950 if (c_smtp_authenticators && (c_smtp_authenticators->count > 0))
951 {
952 mutt_debug(LL_DEBUG2, "Trying user-defined smtp_authenticators\n");
953
954 /* Try user-specified list of authentication methods */
955 struct ListNode *np = NULL;
956 STAILQ_FOREACH(np, &c_smtp_authenticators->head, entries)
957 {
958 mutt_debug(LL_DEBUG2, "Trying method %s\n", np->data);
959
960 for (size_t i = 0; i < mutt_array_size(SmtpAuthenticators); i++)
961 {
962 const struct SmtpAuth *auth = &SmtpAuthenticators[i];
963 if (!auth->method || mutt_istr_equal(auth->method, np->data))
964 {
965 r = auth->authenticate(adata, np->data);
966 if (r == SMTP_AUTH_SUCCESS)
967 return r;
968 }
969 }
970 }
971 }
972 else
973 {
974 /* Fall back to default: any authenticator */
975#if defined(USE_SASL_CYRUS)
976 mutt_debug(LL_DEBUG2, "Falling back to smtp_auth_sasl, if using sasl\n");
977 r = smtp_auth_sasl(adata, adata->auth_mechs);
978#elif defined(USE_SASL_GNU)
979 mutt_debug(LL_DEBUG2, "Falling back to smtp_auth_gsasl, if using gsasl\n");
980 r = smtp_auth_gsasl(adata, adata->auth_mechs);
981#else
982 mutt_debug(LL_DEBUG2, "Falling back to using any authenticator available\n");
983 /* Try all available authentication methods */
984 for (size_t i = 0; i < mutt_array_size(SmtpAuthenticators); i++)
985 {
986 const struct SmtpAuth *auth = &SmtpAuthenticators[i];
987 mutt_debug(LL_DEBUG2, "Trying method %s\n", auth->method ? auth->method : "<variable>");
988 r = auth->authenticate(adata, auth->method);
989 if (r == SMTP_AUTH_SUCCESS)
990 return r;
991 }
992#endif
993 }
994
995 if (r != SMTP_AUTH_SUCCESS)
997
998 if (r == SMTP_AUTH_FAIL)
999 {
1000 // L10N: %s is the method name, e.g. Anonymous, CRAM-MD5, GSSAPI, SASL
1001 mutt_error(_("%s authentication failed"), "SASL");
1002 }
1003 else if (r == SMTP_AUTH_UNAVAIL)
1004 {
1005 mutt_error(_("No authenticators available"));
1006 }
1007
1008 return (r == SMTP_AUTH_SUCCESS) ? 0 : -1;
1009}
const struct Slist * cs_subset_slist(const struct ConfigSubset *sub, const char *name)
Get a string-list config item by name.
Definition: helpers.c:242
void mutt_account_unsetpass(struct ConnAccount *cac)
Unset ConnAccount's password.
Definition: connaccount.c:177
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
A List node for strings.
Definition: list.h:37
char * data
String.
Definition: list.h:38
String list.
Definition: slist.h:37
struct ListHead head
List containing values.
Definition: slist.h:38
size_t count
Number of values in list.
Definition: slist.h:39
int(* authenticate)(struct SmtpAccountData *adata, const char *method)
Definition: smtp.c:118
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_open()

static int smtp_open ( struct SmtpAccountData adata,
bool  esmtp 
)
static

Open an SMTP Connection.

Parameters
adataSMTP Account data
esmtpIf true, use ESMTP
Return values
0Success
-1Error

Definition at line 1018 of file smtp.c.

1019{
1020 int rc;
1021
1022 if (mutt_socket_open(adata->conn))
1023 return -1;
1024
1025 const bool force_auth = cs_subset_string(adata->sub, "smtp_user");
1026 esmtp |= force_auth;
1027
1028 /* get greeting string */
1029 rc = smtp_get_resp(adata);
1030 if (rc != 0)
1031 return rc;
1032
1033 rc = smtp_helo(adata, esmtp);
1034 if (rc != 0)
1035 return rc;
1036
1037#ifdef USE_SSL
1038 const bool c_ssl_force_tls = cs_subset_bool(adata->sub, "ssl_force_tls");
1039 enum QuadOption ans = MUTT_NO;
1040 if (adata->conn->ssf != 0)
1041 ans = MUTT_NO;
1042 else if (c_ssl_force_tls)
1043 ans = MUTT_YES;
1044 else if ((adata->capabilities & SMTP_CAP_STARTTLS) &&
1045 ((ans = query_quadoption(_("Secure connection with TLS?"),
1046 adata->sub, "ssl_starttls")) == MUTT_ABORT))
1047 {
1048 return -1;
1049 }
1050
1051 if (ans == MUTT_YES)
1052 {
1053 if (mutt_socket_send(adata->conn, "STARTTLS\r\n") < 0)
1054 return SMTP_ERR_WRITE;
1055 rc = smtp_get_resp(adata);
1056 // Clear any data after the STARTTLS acknowledgement
1057 mutt_socket_empty(adata->conn);
1058 if (rc != 0)
1059 return rc;
1060
1061 if (mutt_ssl_starttls(adata->conn))
1062 {
1063 mutt_error(_("Could not negotiate TLS connection"));
1064 return -1;
1065 }
1066
1067 /* re-EHLO to get authentication mechanisms */
1068 rc = smtp_helo(adata, esmtp);
1069 if (rc != 0)
1070 return rc;
1071 }
1072#endif
1073
1074 if (force_auth || adata->conn->account.flags & MUTT_ACCT_USER)
1075 {
1076 if (!(adata->capabilities & SMTP_CAP_AUTH))
1077 {
1078 mutt_error(_("SMTP server does not support authentication"));
1079 return -1;
1080 }
1081
1082 return smtp_authenticate(adata);
1083 }
1084
1085 return 0;
1086}
int mutt_ssl_starttls(struct Connection *conn)
Negotiate TLS over an already opened connection.
Definition: gnutls.c:1145
@ MUTT_ABORT
User aborted the question (with Ctrl-G)
Definition: quad.h:37
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:39
enum QuadOption query_quadoption(const char *prompt, struct ConfigSubset *sub, const char *name)
Ask the user a quad-question.
Definition: question.c:379
static int smtp_authenticate(struct SmtpAccountData *adata)
Authenticate to an SMTP server.
Definition: smtp.c:945
static int smtp_helo(struct SmtpAccountData *adata, bool esmtp)
Say hello to an SMTP Server.
Definition: smtp.c:415
void mutt_socket_empty(struct Connection *conn)
Clear out any queued data.
Definition: socket.c:306
int mutt_socket_open(struct Connection *conn)
Simple wrapper.
Definition: socket.c:76
unsigned int ssf
Security strength factor, in bits (see notes)
Definition: connection.h:50
+ 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,
struct ConfigSubset sub 
)

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
subConfig Subset
Return values
0Success
-1Error

Definition at line 1100 of file smtp.c.

1103{
1104 struct SmtpAccountData adata = { 0 };
1105 struct ConnAccount cac = { { 0 } };
1106 const char *envfrom = NULL;
1107 int rc = -1;
1108
1109 adata.sub = sub;
1110 adata.fqdn = mutt_fqdn(false, adata.sub);
1111 if (!adata.fqdn)
1112 adata.fqdn = NONULL(ShortHostname);
1113
1114 const struct Address *c_envelope_from_address = cs_subset_address(adata.sub, "envelope_from_address");
1115
1116 if (smtp_fill_account(&adata, &cac) < 0)
1117 return rc;
1118
1119 adata.conn = mutt_conn_find(&cac);
1120 if (!adata.conn)
1121 return -1;
1122
1123 /* it might be better to synthesize an envelope from from user and host
1124 * but this condition is most likely arrived at accidentally */
1125 if (c_envelope_from_address)
1126 {
1127 envfrom = buf_string(c_envelope_from_address->mailbox);
1128 }
1129 else if (from && !TAILQ_EMPTY(from))
1130 {
1131 envfrom = buf_string(TAILQ_FIRST(from)->mailbox);
1132 }
1133 else
1134 {
1135 mutt_error(_("No from address given"));
1136 mutt_socket_close(adata.conn);
1137 return -1;
1138 }
1139
1140 const char *const c_dsn_return = cs_subset_string(adata.sub, "dsn_return");
1141
1142 struct Buffer *buf = buf_pool_get();
1143 do
1144 {
1145 /* send our greeting */
1146 rc = smtp_open(&adata, eightbit);
1147 if (rc != 0)
1148 break;
1149 FREE(&adata.auth_mechs);
1150
1151 /* send the sender's address */
1152 buf_printf(buf, "MAIL FROM:<%s>", envfrom);
1153 if (eightbit && (adata.capabilities & SMTP_CAP_EIGHTBITMIME))
1154 buf_addstr(buf, " BODY=8BITMIME");
1155
1156 if (c_dsn_return && (adata.capabilities & SMTP_CAP_DSN))
1157 buf_add_printf(buf, " RET=%s", c_dsn_return);
1158
1159 if ((adata.capabilities & SMTP_CAP_SMTPUTF8) &&
1162 {
1163 buf_addstr(buf, " SMTPUTF8");
1164 }
1165 buf_addstr(buf, "\r\n");
1166 if (mutt_socket_send(adata.conn, buf_string(buf)) == -1)
1167 {
1168 rc = SMTP_ERR_WRITE;
1169 break;
1170 }
1171 rc = smtp_get_resp(&adata);
1172 if (rc != 0)
1173 break;
1174
1175 /* send the recipient list */
1176 if ((rc = smtp_rcpt_to(&adata, to)) || (rc = smtp_rcpt_to(&adata, cc)) ||
1177 (rc = smtp_rcpt_to(&adata, bcc)))
1178 {
1179 break;
1180 }
1181
1182 /* send the message data */
1183 rc = smtp_data(&adata, msgfile);
1184 if (rc != 0)
1185 break;
1186
1187 mutt_socket_send(adata.conn, "QUIT\r\n");
1188
1189 rc = 0;
1190 } while (false);
1191
1192 mutt_socket_close(adata.conn);
1193 FREE(&adata.conn);
1194
1195 if (rc == SMTP_ERR_READ)
1196 mutt_error(_("SMTP session failed: read error"));
1197 else if (rc == SMTP_ERR_WRITE)
1198 mutt_error(_("SMTP session failed: write error"));
1199 else if (rc == SMTP_ERR_CODE)
1200 mutt_error(_("Invalid server response"));
1201
1202 buf_pool_release(&buf);
1203 return rc;
1204}
bool mutt_addrlist_uses_unicode(const struct AddressList *al)
Do any of a list of addresses use Unicode characters.
Definition: address.c:1522
bool mutt_addr_uses_unicode(const char *str)
Does this address use Unicode character.
Definition: address.c:1502
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
Definition: config_type.c:272
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:204
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
char * ShortHostname
Short version of the hostname.
Definition: globals.c:38
struct Connection * mutt_conn_find(const struct ConnAccount *cac)
Find a connection from a list.
Definition: mutt_socket.c:90
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
#define TAILQ_FIRST(head)
Definition: queue.h:723
#define TAILQ_EMPTY(head)
Definition: queue.h:721
const char * mutt_fqdn(bool may_hide_host, const struct ConfigSubset *sub)
Get the Fully-Qualified Domain Name.
Definition: sendlib.c:707
static int smtp_data(struct SmtpAccountData *adata, const char *msgfile)
Send data to an SMTP server.
Definition: smtp.c:241
static int smtp_fill_account(struct SmtpAccountData *adata, struct ConnAccount *cac)
Create ConnAccount object from SMTP Url.
Definition: smtp.c:359
static int smtp_rcpt_to(struct SmtpAccountData *adata, const struct AddressList *al)
Set the recipient to an Address.
Definition: smtp.c:199
static int smtp_open(struct SmtpAccountData *adata, bool esmtp)
Open an SMTP Connection.
Definition: smtp.c:1018
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition: socket.c:100
#define NONULL(x)
Definition: string2.h:37
String manipulation buffer.
Definition: buffer.h:36
Login details for a remote server.
Definition: connaccount.h:53
Server connection data.
Definition: smtp.c:97
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ SmtpAuthenticators

const struct SmtpAuth SmtpAuthenticators[]
static
Initial value:
= {
{ smtp_auth_oauth, "oauthbearer" },
{ smtp_auth_xoauth2, "xoauth2" },
{ smtp_auth_plain, "plain" },
{ smtp_auth_login, "login" },
}
static int smtp_auth_xoauth2(struct SmtpAccountData *adata, const char *method)
Authenticate an SMTP connection using XOAUTH2 - Implements SmtpAuth::authenticate() -.
Definition: smtp.c:782
static int smtp_auth_login(struct SmtpAccountData *adata, const char *method)
Authenticate using plain text - Implements SmtpAuth::authenticate() -.
Definition: smtp.c:836
static int smtp_auth_plain(struct SmtpAccountData *adata, const char *method)
Authenticate using plain text - Implements SmtpAuth::authenticate() -.
Definition: smtp.c:796
static int smtp_auth_oauth(struct SmtpAccountData *adata, const char *method)
Authenticate an SMTP connection using OAUTHBEARER - Implements SmtpAuth::authenticate() -.
Definition: smtp.c:771

Accepted authentication methods.

Definition at line 904 of file smtp.c.