64#define smtp_success(x) (((x) / 100) == 2)
66#define SMTP_CONTINUE 354
68#define SMTP_ERR_READ -2
69#define SMTP_ERR_WRITE -3
70#define SMTP_ERR_CODE -4
75#define SMTP_AUTH_SUCCESS 0
76#define SMTP_AUTH_UNAVAIL 1
77#define SMTP_AUTH_FAIL -1
84#define SMTP_CAP_NO_FLAGS 0
85#define SMTP_CAP_STARTTLS (1 << 0)
86#define SMTP_CAP_AUTH (1 << 1)
87#define SMTP_CAP_DSN (1 << 2)
88#define SMTP_CAP_EIGHTBITMIME (1 << 3)
89#define SMTP_CAP_SMTPUTF8 (1 << 4)
90#define SMTP_CAP_ALL ((1 << 5) - 1)
144 char buf[1024] = { 0 };
154 const char *s = buf + 4;
183 }
while (buf[3] ==
'-');
214 char buf[1024] = { 0 };
217 snprintf(buf,
sizeof(buf),
"RCPT TO:<%s> NOTIFY=%s\r\n",
243 char buf[1024] = { 0 };
244 struct Progress *progress = NULL;
252 mutt_error(
_(
"SMTP session failed: unable to open %s"), msgfile);
265 snprintf(buf,
sizeof(buf),
"DATA\r\n");
279 while (fgets(buf,
sizeof(buf) - 1, fp))
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");
300 if (!term && buflen &&
343 const char *
const c_smtp_oauth_refresh_command =
cs_subset_string(adata->
sub,
"smtp_oauth_refresh_command");
344 return c_smtp_oauth_refresh_command;
390 static unsigned short SmtpPort = 0;
393 struct servent *service = getservbyname(
"smtp",
"tcp");
395 SmtpPort = ntohs(service->s_port);
400 cac->
port = SmtpPort;
428 if (c_ssl_force_tls || (c_ssl_starttls !=
MUTT_NO))
433 char buf[1024] = { 0 };
434 snprintf(buf,
sizeof(buf),
"%s %s\r\n", esmtp ?
"EHLO" :
"HELO", adata->
fqdn);
443#if defined(USE_SASL_CYRUS) || defined(USE_SASL_GNU)
450static int smtp_code(
const struct Buffer *buf,
int *n)
455 char code[4] = { 0 };
464 if (!end || (*end !=
'\0'))
480static int smtp_get_auth_response(
struct Connection *conn,
struct Buffer *input_buf,
481 int *smtp_rc,
struct Buffer *response_buf)
488 if (!smtp_code(input_buf, smtp_rc))
496 const char *smtp_response = input_buf->
data + 3;
502 }
while (input_buf->
data[3] ==
'-');
516static int smtp_auth_gsasl(
struct SmtpAccountData *adata,
const char *mechlist)
518 Gsasl_session *gsasl_session = NULL;
519 struct Buffer *input_buf = NULL, *output_buf = NULL, *smtp_response_buf = NULL;
544 buf_printf(output_buf,
"AUTH %s", chosen_mech);
551 char *gsasl_step_output = NULL;
552 gsasl_rc = gsasl_step64(gsasl_session,
"", &gsasl_step_output);
553 if (gsasl_rc != GSASL_NEEDS_MORE && gsasl_rc != GSASL_OK)
556 gsasl_strerror(gsasl_rc));
562 gsasl_free(gsasl_step_output);
572 if (smtp_get_auth_response(adata->
conn, input_buf, &smtp_rc, smtp_response_buf) < 0)
578 char *gsasl_step_output = NULL;
579 gsasl_rc = gsasl_step64(gsasl_session,
buf_string(smtp_response_buf), &gsasl_step_output);
580 if ((gsasl_rc == GSASL_NEEDS_MORE) || (gsasl_rc == GSASL_OK))
584 gsasl_free(gsasl_step_output);
589 gsasl_strerror(gsasl_rc));
591 }
while ((gsasl_rc == GSASL_NEEDS_MORE) || (gsasl_rc == GSASL_OK));
623static int smtp_auth_sasl(
struct SmtpAccountData *adata,
const char *mechlist)
625 sasl_conn_t *saslconn = NULL;
626 sasl_interact_t *interaction = NULL;
627 const char *mech = NULL;
628 const char *
data = NULL;
629 unsigned int data_len = 0;
630 struct Buffer *temp_buf = NULL;
631 struct Buffer *output_buf = NULL;
632 struct Buffer *smtp_response_buf = NULL;
642 rc_sasl = sasl_client_start(saslconn, mechlist, &interaction, &
data, &data_len, &mech);
643 if (rc_sasl == SASL_INTERACT)
645 }
while (rc_sasl == SASL_INTERACT);
647 if ((rc_sasl != SASL_OK) && (rc_sasl != SASL_CONTINUE))
650 sasl_dispose(&saslconn);
678 if (smtp_get_auth_response(adata->
conn, temp_buf, &rc_smtp, smtp_response_buf) < 0)
693 &interaction, &
data, &data_len);
694 if (rc_sasl == SASL_INTERACT)
696 }
while (rc_sasl == SASL_INTERACT);
704 }
while (rc_sasl != SASL_FAIL);
715 sasl_dispose(&saslconn);
737 if (!method && !c_smtp_oauth_refresh_command)
740 const char *authtype = xoauth2 ?
"XOAUTH2" :
"OAUTHBEARER";
798 struct Buffer *buf = NULL;
838 char b64[1024] = { 0 };
839 char buf[1026] = { 0 };
862 size_t len = snprintf(buf,
sizeof(buf),
"%s", adata->
conn->
account.
user);
864 snprintf(buf,
sizeof(buf),
"%s\r\n", b64);
880 snprintf(buf,
sizeof(buf),
"%s\r\n", b64);
897 mutt_error(
_(
"%s authentication failed"),
"LOGIN");
911 { smtp_auth_sasl, NULL },
914 { smtp_auth_gsasl, NULL },
950 if (c_smtp_authenticators && (c_smtp_authenticators->
count > 0))
975#if defined(USE_SASL_CYRUS)
978#elif defined(USE_SASL_GNU)
980 r = smtp_auth_gsasl(adata, adata->
auth_mechs);
1001 mutt_error(
_(
"%s authentication failed"),
"SASL");
1026 esmtp |= force_auth;
1042 else if (c_ssl_force_tls)
1063 mutt_error(
_(
"Could not negotiate TLS connection"));
1078 mutt_error(
_(
"SMTP server does not support authentication"));
1101 const struct AddressList *cc,
const struct AddressList *bcc,
1102 const char *msgfile,
bool eightbit,
struct ConfigSubset *sub)
1106 const char *envfrom = NULL;
1125 if (c_envelope_from_address)
1198 mutt_error(
_(
"SMTP session failed: write error"));
bool mutt_addrlist_uses_unicode(const struct AddressList *al)
Do any of a list of addresses use Unicode characters.
bool mutt_addr_uses_unicode(const char *str)
Does this address use Unicode character.
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
const char * mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
size_t mutt_b64_encode(const char *in, size_t inlen, char *out, size_t outlen)
Convert raw bytes to null-terminated base64 string.
size_t mutt_b64_buffer_encode(struct Buffer *buf, const char *in, size_t len)
Convert raw bytes to null-terminated base64 string.
int mutt_b64_buffer_decode(struct Buffer *buf, const char *in)
Convert null-terminated base64 string to raw bytes.
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
const struct Slist * cs_subset_slist(const struct ConfigSubset *sub, const char *name)
Get a string-list config item by name.
enum QuadOption cs_subset_quad(const struct ConfigSubset *sub, const char *name)
Get a quad-value config item by name.
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Convenience wrapper for the config headers.
int mutt_account_getpass(struct ConnAccount *cac)
Fetch password into ConnAccount, if necessary.
int mutt_account_getuser(struct ConnAccount *cac)
Retrieve username into ConnAccount, if necessary.
void mutt_account_unsetpass(struct ConnAccount *cac)
Unset ConnAccount's password.
char * mutt_account_getoauthbearer(struct ConnAccount *cac, bool xoauth2)
Get an OAUTHBEARER/XOAUTH2 token.
ConnAccountField
Login credentials.
@ MUTT_CA_OAUTH_CMD
OAuth refresh command.
@ MUTT_CA_LOGIN
Login name.
@ MUTT_CA_HOST
Server name.
#define MUTT_ACCT_SSL
Account uses SSL/TLS.
#define MUTT_ACCT_USER
User field has been set.
Convenience wrapper for the core headers.
Structs that make up an email.
long mutt_file_get_size_fp(FILE *fp)
Get the size of a file.
#define mutt_file_fclose(FP)
#define mutt_file_fopen(PATH, MODE)
char * ShortHostname
Short version of the hostname.
bool OptNoCurses
(pseudo) when sending in batch mode
int mutt_ssl_starttls(struct Connection *conn)
Negotiate TLS over an already opened connection.
static const char * smtp_get_field(enum ConnAccountField field, void *gf_data)
Get connection login credentials - Implements ConnAccount::get_field() -.
#define mutt_message(...)
#define mutt_debug(LEVEL,...)
static int smtp_auth_xoauth2(struct SmtpAccountData *adata, const char *method)
Authenticate an SMTP connection using XOAUTH2 - Implements SmtpAuth::authenticate() -.
static int smtp_auth_login(struct SmtpAccountData *adata, const char *method)
Authenticate using plain text - 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_oauth(struct SmtpAccountData *adata, const char *method)
Authenticate an SMTP connection using OAUTHBEARER - Implements SmtpAuth::authenticate() -.
const char * mutt_gsasl_get_mech(const char *requested_mech, const char *server_mechlist)
Pick a connection mechanism.
int mutt_gsasl_client_new(struct Connection *conn, const char *mech, Gsasl_session **sctx)
Create a new GNU SASL client.
void mutt_gsasl_client_finish(Gsasl_session **sctx)
Free a GNU SASL client.
@ LL_DEBUG3
Log at debug level 3.
@ LL_DEBUG2
Log at debug level 2.
@ LL_DEBUG1
Log at debug level 1.
#define mutt_array_size(x)
Convenience wrapper for the library headers.
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
char * mutt_str_dup(const char *str)
Copy a string, safely.
int mutt_str_asprintf(char **strp, const char *fmt,...)
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
int mutt_account_fromurl(struct ConnAccount *cac, const struct Url *url)
Fill ConnAccount with information from url.
ConnAccount object used by POP and IMAP.
@ MUTT_ACCT_TYPE_SMTP
Smtp Account.
struct Connection * mutt_conn_find(const struct ConnAccount *cac)
Find a connection from a list.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
@ MUTT_PROGRESS_NET
Progress tracks bytes, according to $net_inc
struct Progress * progress_new(enum ProgressType type, size_t size)
Create a new Progress Bar.
void progress_free(struct Progress **ptr)
Free a Progress Bar.
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.
QuadOption
Possible values for a quad-option.
@ MUTT_ABORT
User aborted the question (with Ctrl-G)
@ MUTT_NO
User answered 'No', or assume 'No'.
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
enum QuadOption query_quadoption(const char *prompt, struct ConfigSubset *sub, const char *name)
Ask the user a quad-question.
#define TAILQ_FOREACH(var, head, field)
#define STAILQ_FOREACH(var, head, field)
#define TAILQ_FIRST(head)
#define TAILQ_EMPTY(head)
int mutt_sasl_interact(sasl_interact_t *interaction)
Perform an SASL interaction with the user.
int mutt_sasl_client_new(struct Connection *conn, sasl_conn_t **saslconn)
Wrapper for sasl_client_new()
void mutt_sasl_setup_conn(struct Connection *conn, sasl_conn_t *saslconn)
Set up an SASL connection.
size_t mutt_sasl_plain_msg(struct Buffer *buf, const char *cmd, const char *authz, const char *user, const char *pass)
Construct a base64 encoded SASL PLAIN message.
const char * mutt_fqdn(bool may_hide_host, const struct ConfigSubset *sub)
Get the Fully-Qualified Domain Name.
Miscellaneous functions for sending an email.
static int smtp_get_resp(struct SmtpAccountData *adata)
Read a command response from the SMTP server.
#define SMTP_CAP_NO_FLAGS
No flags are set.
#define SMTP_CAP_STARTTLS
Server supports STARTTLS command.
uint8_t SmtpCapFlags
SMTP server capabilities.
static int smtp_authenticate(struct SmtpAccountData *adata)
Authenticate to an SMTP server.
bool smtp_auth_is_valid(const char *authenticator)
Check if string is a valid smtp authentication method.
static int smtp_auth_oauth_xoauth2(struct SmtpAccountData *adata, const char *method, bool xoauth2)
Authenticate an SMTP connection using OAUTHBEARER/XOAUTH2.
static const struct SmtpAuth SmtpAuthenticators[]
Accepted authentication methods.
static bool valid_smtp_code(char *buf, int *n)
Is the is a valid SMTP return code?
#define SMTP_AUTH_UNAVAIL
static int smtp_helo(struct SmtpAccountData *adata, bool esmtp)
Say hello to an SMTP Server.
#define SMTP_CAP_EIGHTBITMIME
Server supports 8-bit MIME content.
#define SMTP_CAP_AUTH
Server supports AUTH command.
static int smtp_data(struct SmtpAccountData *adata, const char *msgfile)
Send data to an SMTP server.
static int smtp_fill_account(struct SmtpAccountData *adata, struct ConnAccount *cac)
Create ConnAccount object from SMTP Url.
#define SMTP_AUTH_SUCCESS
#define SMTP_CAP_SMTPUTF8
Server accepts UTF-8 strings.
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.
#define SMTP_CAP_DSN
Server supports Delivery Status Notification.
static int smtp_rcpt_to(struct SmtpAccountData *adata, const struct AddressList *al)
Set the recipient to an Address.
static int smtp_open(struct SmtpAccountData *adata, bool esmtp)
Open an SMTP Connection.
Send email to an SMTP server.
int mutt_socket_close(struct Connection *conn)
Close a socket.
void mutt_socket_empty(struct Connection *conn)
Clear out any queued data.
int mutt_socket_open(struct Connection *conn)
Simple wrapper.
int mutt_socket_readln_d(char *buf, size_t buflen, struct Connection *conn, int dbg)
Read a line from a socket.
#define MUTT_SOCK_LOG_FULL
#define mutt_socket_readln(buf, buflen, conn)
#define mutt_socket_send(conn, buf)
#define mutt_socket_buffer_readln(buf, conn)
#define mutt_socket_send_d(conn, buf, dbg)
struct Buffer * mailbox
Mailbox and host address.
String manipulation buffer.
char * data
Pointer to data.
A set of inherited config items.
Login details for a remote server.
const char * service
Name of the service, e.g. "imap".
const char *(* get_field)(enum ConnAccountField field, void *gf_data)
unsigned char type
Connection type, e.g. MUTT_ACCT_TYPE_IMAP.
MuttAccountFlags flags
Which fields are initialised, e.g. MUTT_ACCT_USER.
void * gf_data
Private data to pass to get_field()
unsigned short port
Port to connect to.
unsigned int ssf
Security strength factor, in bits (see notes)
struct ConnAccount account
Account details: username, password, etc.
Container for Accounts, Notifications.
struct ConfigSubset * sub
Inherited config items.
struct ListHead head
List containing values.
size_t count
Number of values in list.
const char * fqdn
Fully-qualified domain name.
struct ConfigSubset * sub
Config scope.
struct Connection * conn
Server Connection.
const char * auth_mechs
Allowed authorisation mechanisms.
SmtpCapFlags capabilities
Server capabilities.
SMTP authentication multiplexor.
int(* authenticate)(struct SmtpAccountData *adata, const char *method)
const char * method
Name of authentication method supported, NULL means variable.
A parsed URL proto://user:password@host:port/path?a=1&b=2
enum UrlScheme scheme
Scheme, e.g. U_SMTPS.
struct Url * url_parse(const char *src)
Fill in Url.
void url_free(struct Url **ptr)
Free the contents of a URL.
@ U_SMTPS
Url is smtps://.