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