drop mem_alloc and mem_free, use my own hand crafted optmized macros that
[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-lib/mem.h>
19
20 #include "lib/mem.h"
21 #include "lib/intl.h"
22 #include "lib/debug.h"
23
24 #include "mutt.h"
25 #include "ascii.h"
26 #include "imap_private.h"
27 #include "auth.h"
28
29 static imap_auth_t imap_authenticators[] = {
30 #ifdef USE_SASL
31   {imap_auth_sasl, NULL},
32 #else
33   {imap_auth_anon, "anonymous"},
34 #endif
35 #ifdef USE_GSS
36   {imap_auth_gss, "gssapi"},
37 #endif
38   /* SASL includes CRAM-MD5 (and GSSAPI, but that's not enabled by default) */
39 #ifndef USE_SASL
40   {imap_auth_cram_md5, "cram-md5"},
41 #endif
42   {imap_auth_login, "login"},
43
44   {NULL, NULL}
45 };
46
47 /* imap_authenticate: Attempt to authenticate using either user-specified
48  *   authentication method if specified, or any. */
49 int imap_authenticate (IMAP_DATA * idata)
50 {
51   imap_auth_t *authenticator;
52   char *methods;
53   char *method;
54   char *delim;
55   int r = -1;
56
57   if (ImapAuthenticators && *ImapAuthenticators) {
58     /* Try user-specified list of authentication methods */
59     methods = str_dup (ImapAuthenticators);
60
61     for (method = methods; method; method = delim) {
62       delim = strchr (method, ':');
63       if (delim)
64         *delim++ = '\0';
65       if (!method[0])
66         continue;
67
68       debug_print (2, ("Trying method %s\n", method));
69       authenticator = imap_authenticators;
70
71       while (authenticator->authenticate) {
72         if (!authenticator->method ||
73             !ascii_strcasecmp (authenticator->method, method))
74           if ((r = authenticator->authenticate (idata, method)) !=
75               IMAP_AUTH_UNAVAIL) {
76             p_delete(&methods);
77             return r;
78           }
79
80         authenticator++;
81       }
82     }
83
84     p_delete(&methods);
85   }
86   else {
87     /* Fall back to default: any authenticator */
88     debug_print (2, ("Using any available method.\n"));
89     authenticator = imap_authenticators;
90
91     while (authenticator->authenticate) {
92       if ((r =
93            authenticator->authenticate (idata, NULL)) != IMAP_AUTH_UNAVAIL)
94         return r;
95       authenticator++;
96     }
97   }
98
99   if (r == IMAP_AUTH_UNAVAIL) {
100     mutt_error (_("No authenticators available"));
101     mutt_sleep (1);
102   }
103
104   return r;
105 }