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