8ee355b7cbf24bdec736e36fdc5adc2b943d83ea
[apps/madmutt.git] / imap / auth.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-8 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
5  * Copyright (C) 1999-2001 Brendan Cully <brendan@kublai.com>
6  *
7  * This file is part of mutt-ng, see http://www.muttng.org/.
8  * It's licensed under the GNU General Public License,
9  * please see the file GPL in the top level source directory.
10  */
11
12 /* IMAP login/authentication code */
13
14 #include <lib-lib/lib-lib.h>
15
16 #include "mutt.h"
17 #include "imap_private.h"
18 #include "auth.h"
19
20 static imap_auth_t imap_authenticators[] = {
21   {imap_auth_sasl, NULL},
22 #ifdef USE_GSS
23   {imap_auth_gss, "gssapi"},
24 #endif
25   /* SASL includes CRAM-MD5 (and GSSAPI, but that's not enabled by default) */
26   {imap_auth_login, "login"},
27   {NULL, NULL}
28 };
29
30 /* imap_authenticate: Attempt to authenticate using either user-specified
31  *   authentication method if specified, or any. */
32 int imap_authenticate (IMAP_DATA * idata)
33 {
34   imap_auth_t *authenticator;
35   char *methods;
36   char *method;
37   char *delim;
38   int r = -1;
39
40   if (ImapAuthenticators && *ImapAuthenticators) {
41     /* Try user-specified list of authentication methods */
42     methods = m_strdup(ImapAuthenticators);
43
44     for (method = methods; method; method = delim) {
45       delim = strchr (method, ':');
46       if (delim)
47         *delim++ = '\0';
48       if (!method[0])
49         continue;
50
51       authenticator = imap_authenticators;
52
53       while (authenticator->authenticate) {
54         if (!authenticator->method ||
55             !ascii_strcasecmp (authenticator->method, method))
56           if ((r = authenticator->authenticate (idata, method)) !=
57               IMAP_AUTH_UNAVAIL) {
58             p_delete(&methods);
59             return r;
60           }
61
62         authenticator++;
63       }
64     }
65
66     p_delete(&methods);
67   }
68   else {
69     /* Fall back to default: any authenticator */
70     authenticator = imap_authenticators;
71
72     while (authenticator->authenticate) {
73       if ((r =
74            authenticator->authenticate (idata, NULL)) != IMAP_AUTH_UNAVAIL)
75         return r;
76       authenticator++;
77     }
78   }
79
80   if (r == IMAP_AUTH_UNAVAIL) {
81     mutt_error (_("No authenticators available"));
82     mutt_sleep (1);
83   }
84
85   return r;
86 }