merge many things in auth.c
[apps/madmutt.git] / imap / auth.c
index 301c27d..c51c969 100644 (file)
 #include "mutt_sasl.h"
 #include "imap_private.h"
 
-typedef enum {
-  IMAP_AUTH_SUCCESS = 0,
-  IMAP_AUTH_FAILURE,
-  IMAP_AUTH_UNAVAIL
-} imap_auth_res_t;
-
-typedef struct {
-  /* do authentication, using named method or any available if method is NULL */
-  imap_auth_res_t (*authenticate) (IMAP_DATA * idata, const char *method);
-  /* name of authentication method supported, NULL means variable. If this
-   * is not null, authenticate may ignore the second parameter. */
-  const char *method;
-} imap_auth_t;
+enum {
+    IMAP_AUTH_SUCCESS = 0,
+    IMAP_AUTH_FAILURE,
+    IMAP_AUTH_UNAVAIL
+};
 
 /* imap_auth_sasl: Default authenticator if available. */
-static imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
+static int imap_auth_sasl(IMAP_DATA * idata, const char *method)
 {
   sasl_conn_t *saslconn;
   sasl_interact_t *interaction = NULL;
@@ -168,100 +160,35 @@ bail:
 
   return IMAP_AUTH_FAILURE;
 }
-/* imap_auth_login: Plain LOGIN support */
-static imap_auth_res_t imap_auth_login(IMAP_DATA *idata, const char *method)
-{
-    char q_user[STRING], q_pass[STRING];
-    char buf[STRING];
-    int rc;
-
-    if (mutt_bit_isset (idata->capabilities, LOGINDISABLED)) {
-        mutt_message _("LOGIN disabled on this server.");
-
-        return IMAP_AUTH_UNAVAIL;
-    }
-
-    if (mutt_account_getlogin (&idata->conn->account))
-        return IMAP_AUTH_FAILURE;
-    if (mutt_account_getpass (&idata->conn->account))
-        return IMAP_AUTH_FAILURE;
 
-    mutt_message _("Logging in...");
-
-    imap_quote_string(q_user, sizeof(q_user), idata->conn->account.login);
-    imap_quote_string(q_pass, sizeof(q_pass), idata->conn->account.pass);
-
-    snprintf(buf, sizeof(buf), "LOGIN %s %s", q_user, q_pass);
-    rc = imap_exec(idata, buf, IMAP_CMD_FAIL_OK | IMAP_CMD_PASS);
-
-    if (!rc)
-        return IMAP_AUTH_SUCCESS;
-
-    mutt_error _("Login failed.");
-
-    mutt_sleep (2);
-    return IMAP_AUTH_FAILURE;
-}
-static imap_auth_t imap_authenticators[] = {
-  {imap_auth_sasl, NULL},
-  {imap_auth_login, "login"},
-  {NULL, NULL}
-};
-
-/* imap_authenticate: Attempt to authenticate using either user-specified
- *   authentication method if specified, or any. */
 int imap_authenticate (IMAP_DATA * idata)
 {
-  imap_auth_t *authenticator;
-  char *methods;
-  char *method;
-  char *delim;
-  int r = -1;
-
-  if (ImapAuthenticators && *ImapAuthenticators) {
-    /* Try user-specified list of authentication methods */
-    methods = m_strdup(ImapAuthenticators);
-
-    for (method = methods; method; method = delim) {
-      delim = strchr (method, ':');
-      if (delim)
-        *delim++ = '\0';
-      if (!method[0])
-        continue;
-
-      authenticator = imap_authenticators;
-
-      while (authenticator->authenticate) {
-        if (!authenticator->method ||
-            !ascii_strcasecmp (authenticator->method, method))
-          if ((r = authenticator->authenticate (idata, method)) !=
-              IMAP_AUTH_UNAVAIL) {
-            p_delete(&methods);
+    int r = -1;
+
+    if (!m_strisempty(ImapAuthenticators)) {
+        const char *p, *q;
+        char buf[STRING];
+
+        for (p = ImapAuthenticators;; p = q) {
+            while (*p == ':')
+                p++;
+            if (!*p)
+                break;
+
+            q = strchrnul(p, ':');
+            m_strncpy(buf, sizeof(buf), p, q - p);
+
+            if ((r = imap_auth_sasl(idata, buf)) != IMAP_AUTH_UNAVAIL) {
+                return r;
+            }
+        }
+    } else {
+        if ((r = imap_auth_sasl(idata, NULL)) != IMAP_AUTH_UNAVAIL) {
             return r;
-          }
-
-        authenticator++;
-      }
+        }
     }
 
-    p_delete(&methods);
-  }
-  else {
-    /* Fall back to default: any authenticator */
-    authenticator = imap_authenticators;
-
-    while (authenticator->authenticate) {
-      if ((r =
-           authenticator->authenticate (idata, NULL)) != IMAP_AUTH_UNAVAIL)
-        return r;
-      authenticator++;
-    }
-  }
-
-  if (r == IMAP_AUTH_UNAVAIL) {
     mutt_error (_("No authenticators available"));
     mutt_sleep (1);
-  }
-
-  return r;
+    return r;
 }