Rocco Rutte:
[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/mem.h"
19 #include "lib/intl.h"
20 #include "lib/debug.h"
21
22 #include "mutt.h"
23 #include "ascii.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}
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 = str_dup (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       debug_print (2, ("Trying method %s\n", method));
67       authenticator = imap_authenticators;
68
69       while (authenticator->authenticate) {
70         if (!authenticator->method ||
71             !ascii_strcasecmp (authenticator->method, method))
72           if ((r = authenticator->authenticate (idata, method)) !=
73               IMAP_AUTH_UNAVAIL) {
74             mem_free (&methods);
75             return r;
76           }
77
78         authenticator++;
79       }
80     }
81
82     mem_free (&methods);
83   }
84   else {
85     /* Fall back to default: any authenticator */
86     debug_print (2, ("Using any available method.\n"));
87     authenticator = imap_authenticators;
88
89     while (authenticator->authenticate) {
90       if ((r =
91            authenticator->authenticate (idata, NULL)) != IMAP_AUTH_UNAVAIL)
92         return r;
93       authenticator++;
94     }
95   }
96
97   if (r == IMAP_AUTH_UNAVAIL) {
98     mutt_error (_("No authenticators available"));
99     mutt_sleep (1);
100   }
101
102   return r;
103 }