4e30c9c6a78a2fd67c9532f2eb188a88181c05ec
[apps/madmutt.git] / imap / auth.c
1 /*
2  * Copyright (C) 1996-8 Michael R. Elkins <me@mutt.org>
3  * Copyright (C) 1996-9 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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
31 static 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}
47 };
48
49 /* imap_authenticate: Attempt to authenticate using either user-specified
50  *   authentication method if specified, or any. */
51 int imap_authenticate (IMAP_DATA * idata)
52 {
53   imap_auth_t *authenticator;
54   char *methods;
55   char *method;
56   char *delim;
57   int r = -1;
58
59   if (ImapAuthenticators && *ImapAuthenticators) {
60     /* Try user-specified list of authentication methods */
61     methods = safe_strdup (ImapAuthenticators);
62
63     for (method = methods; method; method = delim) {
64       delim = strchr (method, ':');
65       if (delim)
66         *delim++ = '\0';
67       if (!method[0])
68         continue;
69
70       dprint (2,
71               (debugfile, "imap_authenticate: Trying method %s\n", method));
72       authenticator = imap_authenticators;
73
74       while (authenticator->authenticate) {
75         if (!authenticator->method ||
76             !ascii_strcasecmp (authenticator->method, method))
77           if ((r = authenticator->authenticate (idata, method)) !=
78               IMAP_AUTH_UNAVAIL) {
79             FREE (&methods);
80             return r;
81           }
82
83         authenticator++;
84       }
85     }
86
87     FREE (&methods);
88   }
89   else {
90     /* Fall back to default: any authenticator */
91     dprint (2,
92             (debugfile, "imap_authenticate: Using any available method.\n"));
93     authenticator = imap_authenticators;
94
95     while (authenticator->authenticate) {
96       if ((r =
97            authenticator->authenticate (idata, NULL)) != IMAP_AUTH_UNAVAIL)
98         return r;
99       authenticator++;
100     }
101   }
102
103   if (r == IMAP_AUTH_UNAVAIL) {
104     mutt_error (_("No authenticators available"));
105     mutt_sleep (1);
106   }
107
108   return r;
109 }