NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
auth.c
Go to the documentation of this file.
1
34#include "config.h"
35#include <string.h>
36#include "private.h"
37#include "mutt/lib.h"
38#include "config/lib.h"
39#include "core/lib.h"
40#include "auth.h"
41
46{
55 enum ImapAuthRes (*authenticate)(struct ImapAccountData *adata, const char *method);
56
57 const char *method;
59};
60
64static const struct ImapAuth ImapAuthenticators[] = {
65 // clang-format off
66 { imap_auth_oauth, "oauthbearer" },
67 { imap_auth_xoauth2, "xoauth2" },
68 { imap_auth_plain, "plain" },
69#if defined(USE_SASL_CYRUS)
70 { imap_auth_sasl, NULL },
71#elif defined(USE_SASL_GNU)
72 { imap_auth_gsasl, NULL },
73#else
74 { imap_auth_anon, "anonymous" },
75#endif
76#ifdef USE_GSS
77 { imap_auth_gss, "gssapi" },
78#endif
79/* SASL includes CRAM-MD5 (and GSSAPI, but that's not enabled by default) */
80#ifndef HAVE_SASL
81 { imap_auth_cram_md5, "cram-md5" },
82#endif
83 { imap_auth_login, "login" },
84 // clang-format on
85};
86
95bool imap_auth_is_valid(const char *authenticator)
96{
97 for (size_t i = 0; i < mutt_array_size(ImapAuthenticators); i++)
98 {
99 const struct ImapAuth *auth = &ImapAuthenticators[i];
100 if (auth->method && mutt_istr_equal(auth->method, authenticator))
101 return true;
102 }
103
104 return false;
105}
106
116{
117 int rc = IMAP_AUTH_FAILURE;
118
119 const struct Slist *c_imap_authenticators = cs_subset_slist(NeoMutt->sub, "imap_authenticators");
120 if (c_imap_authenticators && (c_imap_authenticators->count > 0))
121 {
122 mutt_debug(LL_DEBUG2, "Trying user-defined imap_authenticators\n");
123
124 /* Try user-specified list of authentication methods */
125 struct ListNode *np = NULL;
126 STAILQ_FOREACH(np, &c_imap_authenticators->head, entries)
127 {
128 mutt_debug(LL_DEBUG2, "Trying method %s\n", np->data);
129
130 for (size_t i = 0; i < mutt_array_size(ImapAuthenticators); i++)
131 {
132 const struct ImapAuth *auth = &ImapAuthenticators[i];
133 if (!auth->method || mutt_istr_equal(auth->method, np->data))
134 {
135 rc = auth->authenticate(adata, np->data);
136 if (rc == IMAP_AUTH_SUCCESS)
137 {
138 return rc;
139 }
140 }
141 }
142 }
143 }
144 else
145 {
146 /* Fall back to default: any authenticator */
147 mutt_debug(LL_DEBUG2, "Trying pre-defined imap_authenticators\n");
148
149 for (size_t i = 0; i < mutt_array_size(ImapAuthenticators); i++)
150 {
151 rc = ImapAuthenticators[i].authenticate(adata, NULL);
152 if (rc == IMAP_AUTH_SUCCESS)
153 return rc;
154 }
155 }
156
157 mutt_error(_("No authenticators available or wrong credentials"));
158 return rc;
159}
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:106
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:97
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:47
#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:64
int imap_authenticate(struct ImapAccountData *adata)
Authenticate to an IMAP server.
Definition: auth.c:115
bool imap_auth_is_valid(const char *authenticator)
Check if string is a valid imap authentication method.
Definition: auth.c:95
@ 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:672
#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:46
enum ImapAuthRes(* authenticate)(struct ImapAccountData *adata, const char *method)
Definition: auth.c:55
const char * method
Name of authentication method supported, NULL means variable.
Definition: auth.c:57
A List node for strings.
Definition: list.h:37
char * data
String.
Definition: list.h:38
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
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