50#include <sys/socket.h>
67 const sasl_ssf_t *
ssf;
107 "ANONYMOUS",
"CRAM-MD5",
"DIGEST-MD5",
"EXTERNAL",
108 "GS2-IAKERB",
"GS2-KRB5",
"GSS-SPNEGO",
"GSSAPI",
109 "LOGIN",
"NTLM",
"OTP-MD4",
"OTP-MD5",
110 "OTP-SHA1",
"PASSDSS-3DES-1",
"PLAIN",
"SCRAM-SHA-1",
111 "SCRAM-SHA-224",
"SCRAM-SHA-256",
"SCRAM-SHA-384",
"SCRAM-SHA-512",
118#define MUTT_SASL_MAXBUF 65536
123#define IP_PORT_BUFLEN (NI_MAXHOST + NI_MAXSERV)
160 mutt_debug(
LL_DEBUG1,
"The name could not be resolved at this time. Future attempts may succeed\n");
172 mutt_debug(
LL_DEBUG1,
"The address family was not recognized or the address length was invalid for the specified family\n");
181 "NI_NAMEREQD is set and the host's name can't be located, or both nodename and servname were null.\n");
185 mutt_debug(
LL_DEBUG1,
"A system error occurred. The error code can be found in errno(%d,%s))\n",
186 errno, strerror(errno));
207static int iptostring(
const struct sockaddr *addr, socklen_t addrlen,
char *out,
210 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
214 return SASL_BADPARAM;
216 rc = getnameinfo(addr, addrlen, hbuf,
sizeof(hbuf), pbuf,
sizeof(pbuf),
225 if (outlen < strlen(hbuf) + strlen(pbuf) + 2)
228 snprintf(out, outlen,
"%s;%s", hbuf, pbuf);
242 if (priority == SASL_LOG_NONE)
245 int mutt_priority = 0;
267 mutt_debug(mutt_priority,
"SASL: %s\n", message);
279 static bool sasl_init =
false;
281 static sasl_callback_t callbacks[2];
288 callbacks[0].id = SASL_CB_LOG;
290 callbacks[0].context = NULL;
292 callbacks[1].id = SASL_CB_LIST_END;
293 callbacks[1].proc = NULL;
294 callbacks[1].context = NULL;
296 rc = sasl_client_init(callbacks);
329 return SASL_BADPARAM;
332 (
id == SASL_CB_AUTHNAME) ?
"authname" :
"user", cac->
host, cac->
port);
334 if (
id == SASL_CB_AUTHNAME)
338 *result = cac->
login;
348 *len = strlen(*result);
366 if (!cac || !psecret)
367 return SASL_BADPARAM;
374 len = strlen(cac->
pass);
393 callback->id = SASL_CB_USER;
395 callback->context = cac;
398 callback->id = SASL_CB_AUTHNAME;
400 callback->context = cac;
403 callback->id = SASL_CB_PASS;
405 callback->context = cac;
408 callback->id = SASL_CB_GETREALM;
409 callback->proc = NULL;
410 callback->context = NULL;
413 callback->id = SASL_CB_LIST_END;
414 callback->proc = NULL;
415 callback->context = NULL;
431 int rc = sasldata->
open(conn);
460 int rc = conn->
close(conn);
476 if (sasldata->
blen > sasldata->
bpos)
478 olen = ((sasldata->
blen - sasldata->
bpos) > count) ?
482 memcpy(
buf, sasldata->
buf + sasldata->
bpos, olen);
483 sasldata->
bpos += olen;
494 if (*sasldata->
ssf != 0)
499 rc = sasldata->
read(conn,
buf, count);
509 }
while (sasldata->
blen == 0);
511 olen = ((sasldata->
blen - sasldata->
bpos) > count) ?
515 memcpy(
buf, sasldata->
buf, olen);
516 sasldata->
bpos += olen;
522 rc = sasldata->
read(conn,
buf, count);
537 const char *pbuf = NULL;
538 unsigned int olen, plen;
544 if (*sasldata->
ssf != 0)
551 rc = sasl_encode(sasldata->
saslconn,
buf, olen, &pbuf, &plen);
558 rc = sasldata->
write(conn, pbuf, plen);
564 }
while (count > *sasldata->
pbufsize);
569 rc = sasldata->
write(conn,
buf, count);
590 rc = sasldata->
poll(conn, wait_secs);
619 struct sockaddr_storage local = { 0 };
622 size =
sizeof(local);
623 if (getsockname(conn->
fd, (
struct sockaddr *) &local, &size) == 0)
635 struct sockaddr_storage remote = { 0 };
638 size =
sizeof(remote);
639 if (getpeername(conn->
fd, (
struct sockaddr *) &remote, &size) == 0)
662 sasl_security_properties_t secprops = { 0 };
663 secprops.max_ssf = 0x7fff;
665 if (sasl_setprop(*saslconn, SASL_SEC_PROPS, &secprops) != SASL_OK)
667 mutt_error(
_(
"Error setting SASL security properties"));
668 sasl_dispose(saslconn);
676 if (sasl_setprop(*saslconn, SASL_SSF_EXTERNAL, &conn->
ssf) != SASL_OK)
678 mutt_error(
_(
"Error setting SASL external security strength"));
679 sasl_dispose(saslconn);
686 if (sasl_setprop(*saslconn, SASL_AUTH_EXTERNAL, conn->
account.
user) != SASL_OK)
688 mutt_error(
_(
"Error setting SASL external user name"));
689 sasl_dispose(saslconn);
707 char prompt[128] = { 0 };
710 while (interaction->id != SASL_CB_LIST_END)
714 snprintf(prompt,
sizeof(prompt),
"%s: ", interaction->prompt);
724 interaction->len =
buf_len(resp) + 1;
745 const void *tmp = NULL;
749 sasl_getprop(
saslconn, SASL_SSF, &tmp);
753 conn->
ssf += *sasldata->
ssf;
754 sasl_getprop(
saslconn, SASL_MAXOUTBUF, &tmp);
759 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.
int mutt_sasl_start(void)
Initialise SASL library.
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_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[]
Authentication 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.