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
21 #include "mutt.h"
22 #include "imap_private.h"
23 #include "auth.h"
24
25 static imap_auth_t imap_authenticators[] = {
26 #ifdef USE_SASL
27   {imap_auth_sasl, NULL},
28 #else
29   {imap_auth_anon, "anonymous"},
30 #endif
31 #ifdef USE_GSS
32   {imap_auth_gss, "gssapi"},
33 #endif
34   /* SASL includes CRAM-MD5 (and GSSAPI, but that's not enabled by default) */
35 #ifndef USE_SASL
36   {imap_auth_cram_md5, "cram-md5"},
37 #endif
38   {imap_auth_login, "login"},
39
40   {NULL}
41 };
42
43 /* imap_authenticate: Attempt to authenticate using either user-specified
44  *   authentication method if specified, or any. */
45 int imap_authenticate (IMAP_DATA * idata)
46 {
47   imap_auth_t *authenticator;
48   char *methods;
49   char *method;
50   char *delim;
51   int r = -1;
52
53   if (ImapAuthenticators && *ImapAuthenticators) {
54     /* Try user-specified list of authentication methods */
55     methods = safe_strdup (ImapAuthenticators);
56
57     for (method = methods; method; method = delim) {
58       delim = strchr (method, ':');
59       if (delim)
60         *delim++ = '\0';
61       if (!method[0])
62         continue;
63
64       dprint (2,
65               (debugfile, "imap_authenticate: Trying method %s\n", method));
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             FREE (&methods);
74             return r;
75           }
76
77         authenticator++;
78       }
79     }
80
81     FREE (&methods);
82   }
83   else {
84     /* Fall back to default: any authenticator */
85     dprint (2,
86             (debugfile, "imap_authenticate: 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 }