drop the horrible list2_t for good.
[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 #ifdef USE_SASL
22   {imap_auth_sasl, NULL},
23 #else
24   {imap_auth_anon, "anonymous"},
25 #endif
26 #ifdef USE_GSS
27   {imap_auth_gss, "gssapi"},
28 #endif
29   /* SASL includes CRAM-MD5 (and GSSAPI, but that's not enabled by default) */
30 #ifndef USE_SASL
31   {imap_auth_cram_md5, "cram-md5"},
32 #endif
33   {imap_auth_login, "login"},
34
35   {NULL, NULL}
36 };
37
38 /* imap_authenticate: Attempt to authenticate using either user-specified
39  *   authentication method if specified, or any. */
40 int imap_authenticate (IMAP_DATA * idata)
41 {
42   imap_auth_t *authenticator;
43   char *methods;
44   char *method;
45   char *delim;
46   int r = -1;
47
48   if (ImapAuthenticators && *ImapAuthenticators) {
49     /* Try user-specified list of authentication methods */
50     methods = m_strdup(ImapAuthenticators);
51
52     for (method = methods; method; method = delim) {
53       delim = strchr (method, ':');
54       if (delim)
55         *delim++ = '\0';
56       if (!method[0])
57         continue;
58
59       authenticator = imap_authenticators;
60
61       while (authenticator->authenticate) {
62         if (!authenticator->method ||
63             !ascii_strcasecmp (authenticator->method, method))
64           if ((r = authenticator->authenticate (idata, method)) !=
65               IMAP_AUTH_UNAVAIL) {
66             p_delete(&methods);
67             return r;
68           }
69
70         authenticator++;
71       }
72     }
73
74     p_delete(&methods);
75   }
76   else {
77     /* Fall back to default: any authenticator */
78     authenticator = imap_authenticators;
79
80     while (authenticator->authenticate) {
81       if ((r =
82            authenticator->authenticate (idata, NULL)) != IMAP_AUTH_UNAVAIL)
83         return r;
84       authenticator++;
85     }
86   }
87
88   if (r == IMAP_AUTH_UNAVAIL) {
89     mutt_error (_("No authenticators available"));
90     mutt_sleep (1);
91   }
92
93   return r;
94 }