mutt stable branch with some hacks
1/*
2 * Copyright (C) 1996-1998 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1996-1999 Brandon Long <blong@fiction.net>
4 * Copyright (C) 1999-2001 Brendan Cully <brendan@kublai.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21/* IMAP login/authentication code */
22
23#if HAVE_CONFIG_H
24# include "config.h"
25#endif
26
27#include "mutt.h"
28#include "imap_private.h"
29#include "auth.h"
30
31static const imap_auth_t imap_authenticators[] = {
32#ifdef USE_SASL
33 { imap_auth_sasl, NULL },
34#else
35 { imap_auth_anon, "anonymous" },
36#endif
37#ifdef USE_GSS
38 { imap_auth_gss, "gssapi" },
39#endif
40 /* SASL includes CRAM-MD5 (and GSSAPI, but that's not enabled by default) */
41#ifndef USE_SASL
42 { imap_auth_cram_md5, "cram-md5" },
43#endif
44 { imap_auth_login, "login" },
45
46 { NULL, NULL }
47};
48
49/* imap_authenticate: Attempt to authenticate using either user-specified
50 * authentication method if specified, or any. */
51int imap_authenticate (IMAP_DATA* idata)
52{
53 const imap_auth_t* authenticator;
54 char* methods;
55 char* method;
56 char* delim;
57 int r = -1;
58
59 if (ImapAuthenticators && *ImapAuthenticators)
60 {
61 /* Try user-specified list of authentication methods */
62 methods = safe_strdup (ImapAuthenticators);
63
64 for (method = methods; method; method = delim)
65 {
66 delim = strchr (method, ':');
67 if (delim)
68 *delim++ = '\0';
69 if (! method[0])
70 continue;
71
72 dprint (2, (debugfile, "imap_authenticate: Trying method %s\n", method));
73 authenticator = imap_authenticators;
74
75 while (authenticator->authenticate)
76 {
77 if (!authenticator->method ||
78 !ascii_strcasecmp (authenticator->method, method))
79 if ((r = authenticator->authenticate (idata, method)) !=
80 IMAP_AUTH_UNAVAIL)
81 {
82 FREE (&methods);
83 return r;
84 }
85
86 authenticator++;
87 }
88 }
89
90 FREE (&methods);
91 }
92 else
93 {
94 /* Fall back to default: any authenticator */
95 dprint (2, (debugfile, "imap_authenticate: Using any available method.\n"));
96 authenticator = imap_authenticators;
97
98 while (authenticator->authenticate)
99 {
100 if ((r = authenticator->authenticate (idata, NULL)) != IMAP_AUTH_UNAVAIL)
101 return r;
102 authenticator++;
103 }
104 }
105
106 if (r == IMAP_AUTH_UNAVAIL)
107 {
108 mutt_error (_("No authenticators available"));
109 mutt_sleep (1);
110 }
111
112 return r;
113}