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