NeoMutt  2025-12-11-596-g7cc1dd
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
auth.c
Go to the documentation of this file.
1
28
34
35#include "config.h"
36#include <string.h>
37#include "private.h"
38#include "mutt/lib.h"
39#include "config/lib.h"
40#include "core/lib.h"
41#include "auth.h"
42
47{
56 enum ImapAuthRes (*authenticate)(struct ImapAccountData *adata, const char *method);
57
58 const char *method;
60};
61
65static const struct ImapAuth ImapAuthenticators[] = {
66 // clang-format off
67 { imap_auth_oauth, "oauthbearer" },
68 { imap_auth_xoauth2, "xoauth2" },
69 { imap_auth_plain, "plain" },
70#if defined(USE_SASL_CYRUS)
71 { imap_auth_sasl, NULL },
72#elif defined(USE_SASL_GNU)
73 { imap_auth_gsasl, NULL },
74#else
75 { imap_auth_anon, "anonymous" },
76#endif
77#ifdef USE_GSS
78 { imap_auth_gss, "gssapi" },
79#endif
80/* SASL includes CRAM-MD5 (and GSSAPI, but that's not enabled by default) */
81#ifndef HAVE_SASL
82 { imap_auth_cram_md5, "cram-md5" },
83#endif
84 { imap_auth_login, "login" },
85 // clang-format on
86};
87
96bool imap_auth_is_valid(const char *authenticator)
97{
98 for (size_t i = 0; i < countof(ImapAuthenticators); i++)
99 {
100 const struct ImapAuth *auth = &ImapAuthenticators[i];
101 if (auth->method && mutt_istr_equal(auth->method, authenticator))
102 return true;
103 }
104
105 return false;
106}
107
117{
118 int rc = IMAP_AUTH_FAILURE;
119
120 const struct Slist *c_imap_authenticators = cs_subset_slist(NeoMutt->sub, "imap_authenticators");
121 if (c_imap_authenticators && (c_imap_authenticators->count > 0))
122 {
123 mutt_debug(LL_DEBUG2, "Trying user-defined imap_authenticators\n");
124
125 /* Try user-specified list of authentication methods */
126 struct ListNode *np = NULL;
127 STAILQ_FOREACH(np, &c_imap_authenticators->head, entries)
128 {
129 mutt_debug(LL_DEBUG2, "Trying method %s\n", np->data);
130
131 for (size_t i = 0; i < countof(ImapAuthenticators); i++)
132 {
133 const struct ImapAuth *auth = &ImapAuthenticators[i];
134 if (!auth->method || mutt_istr_equal(auth->method, np->data))
135 {
136 rc = auth->authenticate(adata, np->data);
137 if (rc == IMAP_AUTH_SUCCESS)
138 {
139 return rc;
140 }
141 }
142 }
143 }
144 }
145 else
146 {
147 /* Fall back to default: any authenticator */
148 mutt_debug(LL_DEBUG2, "Trying pre-defined imap_authenticators\n");
149
150 for (size_t i = 0; i < countof(ImapAuthenticators); i++)
151 {
152 rc = ImapAuthenticators[i].authenticate(adata, NULL);
153 if (rc == IMAP_AUTH_SUCCESS)
154 return rc;
155 }
156 }
157
158 mutt_error(_("No authenticators available or wrong credentials"));
159 return rc;
160}
IMAP authenticator multiplexor.
ImapAuthRes
Results of IMAP Authentication.
Definition auth.h:39
@ IMAP_AUTH_FAILURE
Authentication failed.
Definition auth.h:41
@ IMAP_AUTH_SUCCESS
Authentication successful.
Definition auth.h:40
const struct Slist * cs_subset_slist(const struct ConfigSubset *sub, const char *name)
Get a string-list config item by name.
Definition helpers.c:242
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:105
enum ImapAuthRes imap_auth_login(struct ImapAccountData *adata, const char *method)
Plain LOGIN support - Implements ImapAuth::authenticate() -.
Definition auth_login.c:45
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:96
enum ImapAuthRes imap_auth_xoauth2(struct ImapAccountData *adata, const char *method)
Authenticate an IMAP connection using XOAUTH2 - Implements ImapAuth::authenticate() -.
Definition auth_oauth.c:119
enum ImapAuthRes imap_auth_plain(struct ImapAccountData *adata, const char *method)
SASL PLAIN support - Implements ImapAuth::authenticate() -.
Definition auth_plain.c:42
enum ImapAuthRes imap_auth_anon(struct ImapAccountData *adata, const char *method)
Authenticate anonymously - Implements ImapAuth::authenticate() -.
Definition auth_anon.c:42
enum ImapAuthRes imap_auth_oauth(struct ImapAccountData *adata, const char *method)
Authenticate an IMAP connection using OAUTHBEARER - Implements ImapAuth::authenticate() -.
Definition auth_oauth.c:111
enum ImapAuthRes imap_auth_sasl(struct ImapAccountData *adata, const char *method)
SASL authenticator - Implements ImapAuth::authenticate() -.
Definition auth_sasl.c:46
#define mutt_error(...)
Definition logging2.h:94
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
static const struct ImapAuth ImapAuthenticators[]
Accepted authentication methods.
Definition auth.c:65
int imap_authenticate(struct ImapAccountData *adata)
Authenticate to an IMAP server.
Definition auth.c:116
bool imap_auth_is_valid(const char *authenticator)
Check if string is a valid imap authentication method.
Definition auth.c:96
Shared constants/structs that are private to IMAP.
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:46
#define countof(x)
Definition memory.h:49
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:677
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
IMAP-specific Account data -.
Definition adata.h:40
IMAP authentication multiplexor.
Definition auth.c:47
enum ImapAuthRes(* authenticate)(struct ImapAccountData *adata, const char *method)
Definition auth.c:56
const char * method
Name of authentication method supported, NULL means variable.
Definition auth.c:58
A List node for strings.
Definition list.h:37
char * data
String.
Definition list.h:38
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
String list.
Definition slist.h:37
struct ListHead head
List containing values.
Definition slist.h:38
size_t count
Number of values in list.
Definition slist.h:39