NeoMutt  2020-11-20
Teaching an old dog new tricks
DOXYGEN
auth.c
Go to the documentation of this file.
1 
31 #include "config.h"
32 #include <string.h>
33 #include "private.h"
34 #include "mutt/lib.h"
35 #include "auth.h"
36 
40 struct ImapAuth
41 {
48  enum ImapAuthRes (*authenticate)(struct ImapAccountData *adata, const char *method);
49 
50  const char *method;
51 };
53 
57 static const struct ImapAuth imap_authenticators[] = {
58  // clang-format off
59  { imap_auth_oauth, "oauthbearer" },
60  { imap_auth_plain, "plain" },
61 #ifdef USE_SASL
62  { imap_auth_sasl, NULL },
63 #else
64  { imap_auth_anon, "anonymous" },
65 #endif
66 #ifdef USE_GSS
67  { imap_auth_gss, "gssapi" },
68 #endif
69 /* SASL includes CRAM-MD5 (and GSSAPI, but that's not enabled by default) */
70 #ifndef USE_SASL
71  { imap_auth_cram_md5, "cram-md5" },
72 #endif
73  { imap_auth_login, "login" },
74  // clang-format on
75 };
76 
85 bool imap_auth_is_valid(const char *authenticator)
86 {
87  for (size_t i = 0; i < mutt_array_size(imap_authenticators); i++)
88  {
89  const struct ImapAuth *auth = &imap_authenticators[i];
90  if (auth->method && mutt_istr_equal(auth->method, authenticator))
91  return true;
92  }
93 
94  return false;
95 }
96 
106 {
107  int rc = IMAP_AUTH_FAILURE;
108 
110  {
111  mutt_debug(LL_DEBUG2, "Trying user-defined imap_authenticators\n");
112 
113  /* Try user-specified list of authentication methods */
114  struct ListNode *np = NULL;
116  {
117  mutt_debug(LL_DEBUG2, "Trying method %s\n", np->data);
118 
119  for (size_t i = 0; i < mutt_array_size(imap_authenticators); i++)
120  {
121  const struct ImapAuth *auth = &imap_authenticators[i];
122  if (!auth->method || mutt_istr_equal(auth->method, np->data))
123  {
124  rc = auth->authenticate(adata, np->data);
125  if (rc == IMAP_AUTH_SUCCESS)
126  {
127  return rc;
128  }
129  }
130  }
131  }
132  }
133  else
134  {
135  /* Fall back to default: any authenticator */
136  mutt_debug(LL_DEBUG2, "Trying pre-defined imap_authenticators\n");
137 
138  for (size_t i = 0; i < mutt_array_size(imap_authenticators); i++)
139  {
140  rc = imap_authenticators[i].authenticate(adata, NULL);
141  if (rc == IMAP_AUTH_SUCCESS)
142  return rc;
143  }
144  }
145 
146  mutt_error(_("No authenticators available or wrong credentials"));
147  return rc;
148 }
const char * method
Name of authentication method supported, NULL means variable.
Definition: auth.c:50
struct Slist * C_ImapAuthenticators
Config: (imap) List of allowed IMAP authentication methods.
Definition: config.c:39
enum ImapAuthRes imap_auth_login(struct ImapAccountData *adata, const char *method)
Plain LOGIN support - Implements ImapAuth::authenticate()
Definition: auth_login.c:41
IMAP authenticator multiplexor.
#define _(a)
Definition: message.h:28
#define mutt_array_size(x)
Definition: memory.h:33
Log at debug level 2.
Definition: logging.h:41
enum ImapAuthRes imap_auth_plain(struct ImapAccountData *adata, const char *method)
SASL PLAIN support - Implements ImapAuth::authenticate()
Definition: auth_plain.c:41
struct ListHead head
Definition: slist.h:47
Authentication failed.
Definition: auth.h:39
Shared constants/structs that are private to IMAP.
enum ImapAuthRes imap_auth_anon(struct ImapAccountData *adata, const char *method)
Authenticate anonymously - Implements ImapAuth::authenticate()
Definition: auth_anon.c:42
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:883
size_t count
Definition: slist.h:48
enum ImapAuthRes imap_auth_sasl(struct ImapAccountData *adata, const char *method)
Default authenticator if available - Implements ImapAuth::authenticate()
Definition: auth_sasl.c:45
ImapAuthRes
Results of IMAP Authentication.
Definition: auth.h:36
bool imap_auth_is_valid(const char *authenticator)
Check if string is a valid imap authentication method.
Definition: auth.c:85
enum ImapAuthRes(* authenticate)(struct ImapAccountData *adata, const char *method)
Authenticate an IMAP connection.
Definition: auth.c:48
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
IMAP authentication multiplexor.
Definition: auth.c:40
IMAP-specific Account data -.
Definition: private.h:170
char * data
String.
Definition: list.h:36
#define mutt_error(...)
Definition: logging.h:84
int imap_authenticate(struct ImapAccountData *adata)
Authenticate to an IMAP server.
Definition: auth.c:105
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
enum ImapAuthRes imap_auth_oauth(struct ImapAccountData *adata, const char *method)
Authenticate an IMAP connection using OAUTHBEARER - Implements ImapAuth::authenticate() ...
Definition: auth_oauth.c:43
Convenience wrapper for the library headers.
A List node for strings.
Definition: list.h:34
enum ImapAuthRes imap_auth_cram_md5(struct ImapAccountData *adata, const char *method)
Authenticate using CRAM-MD5 - Implements ImapAuth::authenticate()
Definition: auth_cram.c:94
enum ImapAuthRes imap_auth_gss(struct ImapAccountData *adata, const char *method)
GSS Authentication support - Implements ImapAuth::authenticate()
Definition: auth_gss.c:103
Authentication successful.
Definition: auth.h:38