49#include <sys/socket.h>
66 const sasl_ssf_t *
ssf;
106 "ANONYMOUS",
"CRAM-MD5",
"DIGEST-MD5",
"EXTERNAL",
107 "GS2-IAKERB",
"GS2-KRB5",
"GSS-SPNEGO",
"GSSAPI",
108 "LOGIN",
"NTLM",
"OTP-MD4",
"OTP-MD5",
109 "OTP-SHA1",
"PASSDSS-3DES-1",
"PLAIN",
"SCRAM-SHA-1",
110 "SCRAM-SHA-224",
"SCRAM-SHA-256",
"SCRAM-SHA-384",
"SCRAM-SHA-512",
117#define MUTT_SASL_MAXBUF 65536
122#define IP_PORT_BUFLEN (NI_MAXHOST + NI_MAXSERV)
159 mutt_debug(
LL_DEBUG1,
"The name could not be resolved at this time. Future attempts may succeed\n");
171 mutt_debug(
LL_DEBUG1,
"The address family was not recognized or the address length was invalid for the specified family\n");
180 "NI_NAMEREQD is set and the host's name can't be located, or both nodename and servname were null.\n");
184 mutt_debug(
LL_DEBUG1,
"A system error occurred. The error code can be found in errno(%d,%s))\n",
185 errno, strerror(errno));
206static int iptostring(
const struct sockaddr *addr, socklen_t addrlen,
char *out,
209 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
213 return SASL_BADPARAM;
215 rc = getnameinfo(addr, addrlen, hbuf,
sizeof(hbuf), pbuf,
sizeof(pbuf),
224 if (outlen < strlen(hbuf) + strlen(pbuf) + 2)
227 snprintf(out, outlen,
"%s;%s", hbuf, pbuf);
241 if (priority == SASL_LOG_NONE)
244 int mutt_priority = 0;
266 mutt_debug(mutt_priority,
"SASL: %s\n", message);
278 static bool sasl_init =
false;
280 static sasl_callback_t callbacks[2];
287 callbacks[0].id = SASL_CB_LOG;
289 callbacks[0].context = NULL;
291 callbacks[1].id = SASL_CB_LIST_END;
292 callbacks[1].proc = NULL;
293 callbacks[1].context = NULL;
295 rc = sasl_client_init(callbacks);
328 return SASL_BADPARAM;
331 (
id == SASL_CB_AUTHNAME) ?
"authname" :
"user", cac->
host, cac->
port);
333 if (
id == SASL_CB_AUTHNAME)
337 *result = cac->
login;
347 *len = strlen(*result);
365 if (!cac || !psecret)
366 return SASL_BADPARAM;
373 len = strlen(cac->
pass);
392 callback->id = SASL_CB_USER;
394 callback->context = cac;
397 callback->id = SASL_CB_AUTHNAME;
399 callback->context = cac;
402 callback->id = SASL_CB_PASS;
404 callback->context = cac;
407 callback->id = SASL_CB_GETREALM;
408 callback->proc = NULL;
409 callback->context = NULL;
412 callback->id = SASL_CB_LIST_END;
413 callback->proc = NULL;
414 callback->context = NULL;
430 int rc = sasldata->
open(conn);
459 int rc = conn->
close(conn);
475 if (sasldata->
blen > sasldata->
bpos)
477 olen = ((sasldata->
blen - sasldata->
bpos) > count) ?
481 memcpy(
buf, sasldata->
buf + sasldata->
bpos, olen);
482 sasldata->
bpos += olen;
493 if (*sasldata->
ssf != 0)
498 rc = sasldata->
read(conn,
buf, count);
508 }
while (sasldata->
blen == 0);
510 olen = ((sasldata->
blen - sasldata->
bpos) > count) ?
514 memcpy(
buf, sasldata->
buf, olen);
515 sasldata->
bpos += olen;
521 rc = sasldata->
read(conn,
buf, count);
536 const char *pbuf = NULL;
537 unsigned int olen, plen;
543 if (*sasldata->
ssf != 0)
550 rc = sasl_encode(sasldata->
saslconn,
buf, olen, &pbuf, &plen);
557 rc = sasldata->
write(conn, pbuf, plen);
563 }
while (count > *sasldata->
pbufsize);
568 rc = sasldata->
write(conn,
buf, count);
589 rc = sasldata->
poll(conn, wait_secs);
618 struct sockaddr_storage local = { 0 };
621 size =
sizeof(local);
622 if (getsockname(conn->
fd, (
struct sockaddr *) &local, &size) == 0)
634 struct sockaddr_storage remote = { 0 };
637 size =
sizeof(remote);
638 if (getpeername(conn->
fd, (
struct sockaddr *) &remote, &size) == 0)
661 sasl_security_properties_t secprops = { 0 };
662 secprops.max_ssf = 0x7fff;
664 if (sasl_setprop(*saslconn, SASL_SEC_PROPS, &secprops) != SASL_OK)
666 mutt_error(
_(
"Error setting SASL security properties"));
667 sasl_dispose(saslconn);
675 if (sasl_setprop(*saslconn, SASL_SSF_EXTERNAL, &conn->
ssf) != SASL_OK)
677 mutt_error(
_(
"Error setting SASL external security strength"));
678 sasl_dispose(saslconn);
685 if (sasl_setprop(*saslconn, SASL_AUTH_EXTERNAL, conn->
account.
user) != SASL_OK)
687 mutt_error(
_(
"Error setting SASL external user name"));
688 sasl_dispose(saslconn);
706 char prompt[128] = { 0 };
709 while (interaction->id != SASL_CB_LIST_END)
713 snprintf(prompt,
sizeof(prompt),
"%s: ", interaction->prompt);
723 interaction->len =
buf_len(resp) + 1;
744 const void *tmp = NULL;
748 sasl_getprop(
saslconn, SASL_SSF, &tmp);
752 conn->
ssf += *sasldata->
ssf;
753 sasl_getprop(
saslconn, SASL_MAXOUTBUF, &tmp);
758 sasldata->
buf = NULL;
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
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.
int mutt_account_getlogin(struct ConnAccount *cac)
Retrieve login info into ConnAccount, if necessary.
An open network connection (socket)
bool OptNoCurses
(pseudo) when sending in batch mode
static int mutt_sasl_conn_close(struct Connection *conn)
Close SASL connection - Implements Connection::close() -.
int(* close)(struct Connection *conn)
Close a socket Connection - Implements Connection::close() -.
int(* open)(struct Connection *conn)
Open a socket Connection - Implements Connection::open() -.
static int mutt_sasl_conn_open(struct Connection *conn)
Empty wrapper for underlying open function - Implements Connection::open() -.
int(* poll)(struct Connection *conn, time_t wait_secs)
Check if any data is waiting on a socket - Implements Connection::poll() -.
static int mutt_sasl_conn_poll(struct Connection *conn, time_t wait_secs)
Check if any data is waiting on a socket - Implements Connection::poll() -.
static int mutt_sasl_conn_read(struct Connection *conn, char *buf, size_t count)
Read data from an SASL connection - Implements Connection::read() -.
int(* read)(struct Connection *conn, char *buf, size_t count)
Read from a socket Connection - Implements Connection::read() -.
static int mutt_sasl_conn_write(struct Connection *conn, const char *buf, size_t count)
Write to an SASL connection - Implements Connection::write() -.
int(* write)(struct Connection *conn, const char *buf, size_t count)
Write to a socket Connection - Implements Connection::write() -.
int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
Ask the user for a string -.
#define mutt_debug(LEVEL,...)
Read/write command history from/to a file.
@ HC_OTHER
Miscellaneous strings.
@ LL_DEBUG3
Log at debug level 3.
@ LL_DEBUG2
Log at debug level 2.
@ LL_DEBUG1
Log at debug level 1.
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
#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.
Many unsorted constants and some structs.
#define MUTT_COMP_NO_FLAGS
No flags are set.
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.
static int mutt_sasl_cb_pass(sasl_conn_t *conn, void *context, int id, sasl_secret_t **psecret)
SASL callback function to get password.
static int mutt_sasl_cb_authname(void *context, int id, const char **result, unsigned int *len)
Callback to retrieve authname or user from ConnAccount.
void mutt_sasl_cleanup(void)
Invoke when processing is complete.
static int getnameinfo_err(int rc)
Convert a getaddrinfo() error code into an SASL error code.
int mutt_sasl_interact(sasl_interact_t *interaction)
Perform an SASL interaction with the user.
static int mutt_sasl_start(void)
Initialise SASL library.
static int mutt_sasl_cb_log(void *context, int priority, const char *message)
Callback to log SASL messages.
static sasl_callback_t * mutt_sasl_get_callbacks(struct ConnAccount *cac)
Get the SASL callback functions.
static const char *const SaslAuthenticators[]
Authenticaion methods supported by Cyrus SASL.
static sasl_callback_t MuttSaslCallbacks[5]
SASL callback functions, e.g. mutt_sasl_cb_authname(), mutt_sasl_cb_pass()
static int iptostring(const struct sockaddr *addr, socklen_t addrlen, char *out, unsigned int outlen)
Convert IP Address to string.
bool sasl_auth_validator(const char *authenticator)
Validate an auth method against Cyrus SASL methods.
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.
static sasl_secret_t * SecretPtr
SASL secret, to store the password.
SASL authentication support.
String manipulation buffer.
Login details for a remote server.
char login[128]
Login name.
const char * service
Name of the service, e.g. "imap".
char host[128]
Server to login to.
unsigned short port
Port to connect to.
void * sockdata
Backend-specific socket data.
int(* poll)(struct Connection *conn, time_t wait_secs)
int(* write)(struct Connection *conn, const char *buf, size_t count)
unsigned int ssf
Security strength factor, in bits (see notes)
int(* close)(struct Connection *conn)
struct ConnAccount account
Account details: username, password, etc.
int(* open)(struct Connection *conn)
int fd
Socket file descriptor.
int(* read)(struct Connection *conn, char *buf, size_t count)
SASL authentication API -.
void * sockdata
Underlying socket data.
unsigned int blen
Size of the read buffer.
unsigned int bpos
Current read position.
const unsigned int * pbufsize
const char * buf
Buffer for data read from the connection.