NeoMutt  2025-12-11-911-gd8d604
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
gsasl.c File Reference

GNU SASL authentication support. More...

#include "config.h"
#include <gsasl.h>
#include <stdbool.h>
#include <stddef.h>
#include "mutt/lib.h"
#include "core/lib.h"
#include "connaccount.h"
#include "connection.h"
#include "gsasl2.h"
#include "module_data.h"
#include "mutt_account.h"
+ Include dependency graph for gsasl.c:

Go to the source code of this file.

Functions

static int mutt_gsasl_callback (Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
 Callback to retrieve authname or user from ConnAccount.
 
static bool mutt_gsasl_init (void)
 Initialise GNU SASL library.
 
void mutt_gsasl_cleanup (Gsasl **ctx)
 Shutdown GNU SASL library.
 
const char * mutt_gsasl_get_mech (const char *requested_mech, const char *server_mechlist)
 Pick a connection mechanism.
 
int mutt_gsasl_client_new (struct Connection *conn, const char *mech, Gsasl_session **sctx)
 Create a new GNU SASL client.
 
void mutt_gsasl_client_finish (Gsasl_session **sctx)
 Free a GNU SASL client.
 

Detailed Description

GNU SASL authentication support.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file gsasl.c.

Function Documentation

◆ mutt_gsasl_callback()

static int mutt_gsasl_callback ( Gsasl * ctx,
Gsasl_session * sctx,
Gsasl_property prop )
static

Callback to retrieve authname or user from ConnAccount.

Parameters
ctxGNU SASL context
sctxGNU SASL session
propProperty to get, e.g. GSASL_PASSWORD
Return values
numGNU SASL error code, e.g. GSASL_OK

Definition at line 48 of file gsasl.c.

49{
50 int rc = GSASL_NO_CALLBACK;
51
52 struct Connection *conn = gsasl_session_hook_get(sctx);
53 if (!conn)
54 {
55 mutt_debug(LL_DEBUG1, "missing session hook data!\n");
56 return rc;
57 }
58
59 switch (prop)
60 {
61 case GSASL_PASSWORD:
62 if (mutt_account_getpass(&conn->account) != 0)
63 return rc;
64 gsasl_property_set(sctx, GSASL_PASSWORD, conn->account.pass);
65 rc = GSASL_OK;
66 break;
67
68 case GSASL_AUTHID:
69 /* whom the provided password belongs to: login */
70 if (mutt_account_getlogin(&conn->account) != 0)
71 return rc;
72 gsasl_property_set(sctx, GSASL_AUTHID, conn->account.login);
73 rc = GSASL_OK;
74 break;
75
76 case GSASL_AUTHZID:
77 /* name of the user whose mail/resources you intend to access: user */
78 if (mutt_account_getuser(&conn->account) != 0)
79 return rc;
80 gsasl_property_set(sctx, GSASL_AUTHZID, conn->account.user);
81 rc = GSASL_OK;
82 break;
83
84 case GSASL_ANONYMOUS_TOKEN:
85 gsasl_property_set(sctx, GSASL_ANONYMOUS_TOKEN, "dummy");
86 rc = GSASL_OK;
87 break;
88
89 case GSASL_SERVICE:
90 {
91 const char *service = NULL;
92 switch (conn->account.type)
93 {
95 service = "imap";
96 break;
98 service = "pop";
99 break;
101 service = "smtp";
102 break;
103 default:
104 return rc;
105 }
106 gsasl_property_set(sctx, GSASL_SERVICE, service);
107 rc = GSASL_OK;
108 break;
109 }
110
111 case GSASL_HOSTNAME:
112 gsasl_property_set(sctx, GSASL_HOSTNAME, conn->account.host);
113 rc = GSASL_OK;
114 break;
115
116 default:
117 break;
118 }
119
120 return rc;
121}
int mutt_account_getpass(struct ConnAccount *cac)
Fetch password into ConnAccount, if necessary.
int mutt_account_getuser(struct ConnAccount *cac)
Retrieve username into ConnAccount, if necessary.
Definition connaccount.c:51
int mutt_account_getlogin(struct ConnAccount *cac)
Retrieve login info into ConnAccount, if necessary.
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
@ MUTT_ACCT_TYPE_SMTP
Smtp Account.
@ MUTT_ACCT_TYPE_POP
Pop Account.
@ MUTT_ACCT_TYPE_IMAP
Imap Account.
char login[128]
Login name.
Definition connaccount.h:61
char user[128]
Username.
Definition connaccount.h:62
char pass[256]
Password.
Definition connaccount.h:63
char host[128]
Server to login to.
Definition connaccount.h:60
unsigned char type
Connection type, e.g. MUTT_ACCT_TYPE_IMAP.
Definition connaccount.h:65
struct ConnAccount account
Account details: username, password, etc.
Definition connection.h:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_gsasl_init()

static bool mutt_gsasl_init ( void )
static

Initialise GNU SASL library.

Return values
trueSuccess

Definition at line 127 of file gsasl.c.

128{
130 if (mod_data->mutt_gsasl_ctx)
131 return true;
132
133 Gsasl *ctx = NULL;
134 int rc = gsasl_init(&ctx);
135 if (rc != GSASL_OK)
136 {
137 mutt_debug(LL_DEBUG1, "libgsasl initialisation failed (%d): %s\n", rc,
138 gsasl_strerror(rc));
139 return false;
140 }
141
142 gsasl_callback_set(ctx, mutt_gsasl_callback);
143 mod_data->mutt_gsasl_ctx = ctx;
144 return true;
145}
static int mutt_gsasl_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
Callback to retrieve authname or user from ConnAccount.
Definition gsasl.c:48
@ MODULE_ID_CONN
ModuleConn, Network connections
Definition module_api.h:60
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:663
Conn private Module data.
Definition module_data.h:38
Container for Accounts, Notifications.
Definition neomutt.h:41
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_gsasl_cleanup()

void mutt_gsasl_cleanup ( Gsasl ** ctx)

Shutdown GNU SASL library.

Parameters
ctxGNU SASL context to clean up

Definition at line 151 of file gsasl.c.

152{
153 if (!ctx || !*ctx)
154 return;
155
156 gsasl_done(*ctx);
157 *ctx = NULL;
158}
+ Here is the caller graph for this function:

◆ mutt_gsasl_get_mech()

const char * mutt_gsasl_get_mech ( const char * requested_mech,
const char * server_mechlist )

Pick a connection mechanism.

Parameters
requested_mechRequested mechanism
server_mechlistServer's list of mechanisms
Return values
ptrSelected mechanism string

Definition at line 166 of file gsasl.c.

167{
168 if (!mutt_gsasl_init())
169 return NULL;
170
172
173 /* libgsasl does not do case-independent string comparisons,
174 * and stores its methods internally in uppercase. */
175 char *uc_server_mechlist = mutt_str_dup(server_mechlist);
176 if (uc_server_mechlist)
177 mutt_str_upper(uc_server_mechlist);
178
179 char *uc_requested_mech = mutt_str_dup(requested_mech);
180 if (uc_requested_mech)
181 mutt_str_upper(uc_requested_mech);
182
183 const char *sel_mech = NULL;
184 if (uc_requested_mech)
185 sel_mech = gsasl_client_suggest_mechanism(mod_data->mutt_gsasl_ctx, uc_requested_mech);
186 else
187 sel_mech = gsasl_client_suggest_mechanism(mod_data->mutt_gsasl_ctx, uc_server_mechlist);
188
189 FREE(&uc_requested_mech);
190 FREE(&uc_server_mechlist);
191
192 return sel_mech;
193}
static bool mutt_gsasl_init(void)
Initialise GNU SASL library.
Definition gsasl.c:127
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
char * mutt_str_upper(char *str)
Convert all characters in the string to uppercase.
Definition string.c:340
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_gsasl_client_new()

int mutt_gsasl_client_new ( struct Connection * conn,
const char * mech,
Gsasl_session ** sctx )

Create a new GNU SASL client.

Parameters
connConnection to a server
mechMechanisms to use
sctxGNU SASL Session
Return values
0Success
-1Error

Definition at line 203 of file gsasl.c.

204{
205 if (!mutt_gsasl_init())
206 return -1;
207
209 int rc = gsasl_client_start(mod_data->mutt_gsasl_ctx, mech, sctx);
210 if (rc != GSASL_OK)
211 {
212 *sctx = NULL;
213 mutt_debug(LL_DEBUG1, "gsasl_client_start failed (%d): %s\n", rc, gsasl_strerror(rc));
214 return -1;
215 }
216
217 gsasl_session_hook_set(*sctx, conn);
218 return 0;
219}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_gsasl_client_finish()

void mutt_gsasl_client_finish ( Gsasl_session ** sctx)

Free a GNU SASL client.

Parameters
sctxGNU SASL Session

Definition at line 225 of file gsasl.c.

226{
227 gsasl_finish(*sctx);
228 *sctx = NULL;
229}
+ Here is the caller graph for this function: