NeoMutt  2023-11-03-107-g582dc1
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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 "config/lib.h"
36#include "core/lib.h"
37#include "auth.h"
38
43{
52 enum ImapAuthRes (*authenticate)(struct ImapAccountData *adata, const char *method);
53
54 const char *method;
56};
57
61static const struct ImapAuth ImapAuthenticators[] = {
62 // clang-format off
63 { imap_auth_oauth, "oauthbearer" },
64 { imap_auth_xoauth2, "xoauth2" },
65 { imap_auth_plain, "plain" },
66#if defined(USE_SASL_CYRUS)
67 { imap_auth_sasl, NULL },
68#elif defined(USE_SASL_GNU)
69 { imap_auth_gsasl, NULL },
70#else
71 { imap_auth_anon, "anonymous" },
72#endif
73#ifdef USE_GSS
74 { imap_auth_gss, "gssapi" },
75#endif
76/* SASL includes CRAM-MD5 (and GSSAPI, but that's not enabled by default) */
77#ifndef HAVE_SASL
78 { imap_auth_cram_md5, "cram-md5" },
79#endif
80 { imap_auth_login, "login" },
81 // clang-format on
82};
83
92bool imap_auth_is_valid(const char *authenticator)
93{
94 for (size_t i = 0; i < mutt_array_size(ImapAuthenticators); i++)
95 {
96 const struct ImapAuth *auth = &ImapAuthenticators[i];
97 if (auth->method && mutt_istr_equal(auth->method, authenticator))
98 return true;
99 }
100
101 return false;
102}
103
113{
114 int rc = IMAP_AUTH_FAILURE;
115
116 const struct Slist *c_imap_authenticators = cs_subset_slist(NeoMutt->sub, "imap_authenticators");
117 if (c_imap_authenticators && (c_imap_authenticators->count > 0))
118 {
119 mutt_debug(LL_DEBUG2, "Trying user-defined imap_authenticators\n");
120
121 /* Try user-specified list of authentication methods */
122 struct ListNode *np = NULL;
123 STAILQ_FOREACH(np, &c_imap_authenticators->head, entries)
124 {
125 mutt_debug(LL_DEBUG2, "Trying method %s\n", np->data);
126
127 for (size_t i = 0; i < mutt_array_size(ImapAuthenticators); i++)
128 {
129 const struct ImapAuth *auth = &ImapAuthenticators[i];
130 if (!auth->method || mutt_istr_equal(auth->method, np->data))
131 {
132 rc = auth->authenticate(adata, np->data);
133 if (rc == IMAP_AUTH_SUCCESS)
134 {
135 return rc;
136 }
137 }
138 }
139 }
140 }
141 else
142 {
143 /* Fall back to default: any authenticator */
144 mutt_debug(LL_DEBUG2, "Trying pre-defined imap_authenticators\n");
145
146 for (size_t i = 0; i < mutt_array_size(ImapAuthenticators); i++)
147 {
148 rc = ImapAuthenticators[i].authenticate(adata, NULL);
149 if (rc == IMAP_AUTH_SUCCESS)
150 return rc;
151 }
152 }
153
154 mutt_error(_("No authenticators available or wrong credentials"));
155 return rc;
156}
IMAP authenticator multiplexor.
ImapAuthRes
Results of IMAP Authentication.
Definition: auth.h:38
@ IMAP_AUTH_FAILURE
Authentication failed.
Definition: auth.h:40
@ IMAP_AUTH_SUCCESS
Authentication successful.
Definition: auth.h:39
const struct Slist * cs_subset_slist(const struct ConfigSubset *sub, const char *name)
Get a string-list config item by name.
Definition: helpers.c:243
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
enum ImapAuthRes imap_auth_gss(struct ImapAccountData *adata, const char *method)
GSS Authentication support - Implements ImapAuth::authenticate() -.
Definition: auth_gss.c:104
enum ImapAuthRes imap_auth_login(struct ImapAccountData *adata, const char *method)
Plain LOGIN support - Implements ImapAuth::authenticate() -.
Definition: auth_login.c:44
enum ImapAuthRes imap_auth_gsasl(struct ImapAccountData *adata, const char *method)
GNU SASL authenticator - Implements ImapAuth::authenticate() -.
Definition: auth_gsasl.c:41
enum ImapAuthRes imap_auth_cram_md5(struct ImapAccountData *adata, const char *method)
Authenticate using CRAM-MD5 - Implements ImapAuth::authenticate() -.
Definition: auth_cram.c:95
enum ImapAuthRes imap_auth_xoauth2(struct ImapAccountData *adata, const char *method)
Authenticate an IMAP connection using XOAUTH2 - Implements ImapAuth::authenticate() -.
Definition: auth_oauth.c:117
enum ImapAuthRes imap_auth_plain(struct ImapAccountData *adata, const char *method)
SASL PLAIN support - Implements ImapAuth::authenticate() -.
Definition: auth_plain.c:41
enum ImapAuthRes imap_auth_anon(struct ImapAccountData *adata, const char *method)
Authenticate anonymously - Implements ImapAuth::authenticate() -.
Definition: auth_anon.c:41
enum ImapAuthRes imap_auth_oauth(struct ImapAccountData *adata, const char *method)
Authenticate an IMAP connection using OAUTHBEARER - Implements ImapAuth::authenticate() -.
Definition: auth_oauth.c:109
enum ImapAuthRes imap_auth_sasl(struct ImapAccountData *adata, const char *method)
SASL authenticator - Implements ImapAuth::authenticate() -.
Definition: auth_sasl.c:45
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static const struct ImapAuth ImapAuthenticators[]
Accepted authentication methods.
Definition: auth.c:61
int imap_authenticate(struct ImapAccountData *adata)
Authenticate to an IMAP server.
Definition: auth.c:112
bool imap_auth_is_valid(const char *authenticator)
Check if string is a valid imap authentication method.
Definition: auth.c:92
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
#define mutt_array_size(x)
Definition: memory.h:38
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:775
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
GUI display the mailboxes in a side panel.
IMAP-specific Account data -.
Definition: adata.h:40
IMAP authentication multiplexor.
Definition: auth.c:43
enum ImapAuthRes(* authenticate)(struct ImapAccountData *adata, const char *method)
Definition: auth.c:52
const char * method
Name of authentication method supported, NULL means variable.
Definition: auth.c:54
A List node for strings.
Definition: list.h:35
char * data
String.
Definition: list.h:36
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
String list.
Definition: slist.h:47
struct ListHead head
List containing values.
Definition: slist.h:48
size_t count
Number of values in list.
Definition: slist.h:49