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