Initial import of mutt-ng.
[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 #include "mutt.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   {
57     /* Try user-specified list of authentication methods */
58     methods = safe_strdup (ImapAuthenticators);
59
60     for (method = methods; method; method = delim)
61     {
62       delim = strchr (method, ':');
63       if (delim)
64         *delim++ = '\0';
65       if (! method[0])
66         continue;
67       
68       dprint (2, (debugfile, "imap_authenticate: Trying method %s\n", method));
69       authenticator = imap_authenticators;
70
71       while (authenticator->authenticate)
72       {
73         if (!authenticator->method ||
74             !ascii_strcasecmp (authenticator->method, method))
75           if ((r = authenticator->authenticate (idata, method)) !=
76               IMAP_AUTH_UNAVAIL)
77           {
78             FREE (&methods);
79             return r;
80           }
81         
82         authenticator++;
83       }
84     }
85
86     FREE (&methods);
87   }
88   else
89   {
90     /* Fall back to default: any authenticator */
91     dprint (2, (debugfile, "imap_authenticate: Using any available method.\n"));
92     authenticator = imap_authenticators;
93
94     while (authenticator->authenticate)
95     {
96       if ((r = authenticator->authenticate (idata, NULL)) != IMAP_AUTH_UNAVAIL)
97         return r;
98       authenticator++;
99     }
100   }
101
102   if (r == IMAP_AUTH_UNAVAIL)
103   {
104     mutt_error (_("No authenticators available"));
105     mutt_sleep (1);
106   }
107   
108   return r;
109 }