- imap_auth_t* authenticator;
- char* methods;
- char* method;
- char* delim;
- int r = -1;
-
- if (ImapAuthenticators && *ImapAuthenticators)
- {
- /* Try user-specified list of authentication methods */
- methods = safe_strdup (ImapAuthenticators);
-
- for (method = methods; method; method = delim)
- {
- delim = strchr (method, ':');
- if (delim)
- *delim++ = '\0';
- if (! method[0])
- continue;
-
- dprint (2, (debugfile, "imap_authenticate: Trying method %s\n", method));
- authenticator = imap_authenticators;
-
- while (authenticator->authenticate)
+ sasl_conn_t *saslconn;
+ sasl_interact_t *interaction = NULL;
+ int rc, irc;
+ char buf[HUGE_STRING];
+ const char *mech;
+
+ const char *pc = NULL;
+ unsigned int len, olen;
+ unsigned char client_start;
+
+ if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
+ return IMAP_AUTH_FAILURE;
+ }
+
+ rc = SASL_FAIL;
+
+ /* If the user hasn't specified a method, use any available */
+ if (!method) {
+ method = idata->capstr;
+
+ /* hack for SASL ANONYMOUS support:
+ * 1. Fetch username. If it's "" or "anonymous" then
+ * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
+ * 3. if sasl_client_start fails, fall through... */
+
+ if (mutt_account_getuser (&idata->conn->account))
+ return IMAP_AUTH_FAILURE;
+
+ if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
+ (!idata->conn->account.user[0] ||
+ !m_strncmp(idata->conn->account.user, "anonymous", 9)))
+ rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
+ &mech);
+ }
+
+ if (rc != SASL_OK && rc != SASL_CONTINUE)
+ do {
+ rc = sasl_client_start (saslconn, method, &interaction,
+ &pc, &olen, &mech);
+ if (rc == SASL_INTERACT)
+ mutt_sasl_interact (interaction);
+ }
+ while (rc == SASL_INTERACT);
+
+ client_start = (olen > 0);
+
+ if (rc != SASL_OK && rc != SASL_CONTINUE) {
+ /* SASL doesn't support LOGIN, so fall back */
+ return IMAP_AUTH_UNAVAIL;
+ }
+
+ mutt_message (_("Authenticating (%s)..."), mech);
+
+ snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
+ imap_cmd_start (idata, buf);
+ irc = IMAP_CMD_CONTINUE;
+
+ /* looping protocol */
+ while (rc == SASL_CONTINUE || olen > 0) {
+ do
+ irc = imap_cmd_step (idata);
+ while (irc == IMAP_CMD_CONTINUE);
+
+ if (method && irc == IMAP_CMD_NO) {
+ sasl_dispose (&saslconn);
+ return IMAP_AUTH_UNAVAIL;
+ }
+
+ if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
+ goto bail;
+
+ if (irc == IMAP_CMD_RESPOND) {
+ if (sasl_decode64(idata->cmd.buf.data + 2, idata->cmd.buf.len - 2, buf,
+ LONG_STRING - 1, &len) != SASL_OK)