NeoMutt  2024-11-14-138-ge5ca67
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 "connaccount.h"
#include "connection.h"
#include "gsasl2.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 (void)
 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.
 

Variables

static Gsasl * MuttGsaslCtx = NULL
 Global GNU SASL handle.
 

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 49 of file gsasl.c.

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

129{
130 if (MuttGsaslCtx)
131 return true;
132
133 int rc = gsasl_init(&MuttGsaslCtx);
134 if (rc != GSASL_OK)
135 {
136 MuttGsaslCtx = NULL;
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(MuttGsaslCtx, mutt_gsasl_callback);
143 return true;
144}
static int mutt_gsasl_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
Callback to retrieve authname or user from ConnAccount.
Definition: gsasl.c:49
static Gsasl * MuttGsaslCtx
Global GNU SASL handle.
Definition: gsasl.c:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_gsasl_cleanup()

void mutt_gsasl_cleanup ( void  )

Shutdown GNU SASL library.

Definition at line 149 of file gsasl.c.

150{
151 if (!MuttGsaslCtx)
152 return;
153
154 gsasl_done(MuttGsaslCtx);
155 MuttGsaslCtx = NULL;
156}
+ 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 164 of file gsasl.c.

165{
166 if (!mutt_gsasl_init())
167 return NULL;
168
169 /* libgsasl does not do case-independent string comparisons,
170 * and stores its methods internally in uppercase. */
171 char *uc_server_mechlist = mutt_str_dup(server_mechlist);
172 if (uc_server_mechlist)
173 mutt_str_upper(uc_server_mechlist);
174
175 char *uc_requested_mech = mutt_str_dup(requested_mech);
176 if (uc_requested_mech)
177 mutt_str_upper(uc_requested_mech);
178
179 const char *sel_mech = NULL;
180 if (uc_requested_mech)
181 sel_mech = gsasl_client_suggest_mechanism(MuttGsaslCtx, uc_requested_mech);
182 else
183 sel_mech = gsasl_client_suggest_mechanism(MuttGsaslCtx, uc_server_mechlist);
184
185 FREE(&uc_requested_mech);
186 FREE(&uc_server_mechlist);
187
188 return sel_mech;
189}
static bool mutt_gsasl_init(void)
Initialise GNU SASL library.
Definition: gsasl.c:128
#define FREE(x)
Definition: memory.h:55
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
char * mutt_str_upper(char *str)
Convert all characters in the string to uppercase.
Definition: string.c:336
+ 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 199 of file gsasl.c.

200{
201 if (!mutt_gsasl_init())
202 return -1;
203
204 int rc = gsasl_client_start(MuttGsaslCtx, mech, sctx);
205 if (rc != GSASL_OK)
206 {
207 *sctx = NULL;
208 mutt_debug(LL_DEBUG1, "gsasl_client_start failed (%d): %s\n", rc, gsasl_strerror(rc));
209 return -1;
210 }
211
212 gsasl_session_hook_set(*sctx, conn);
213 return 0;
214}
+ 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 220 of file gsasl.c.

221{
222 gsasl_finish(*sctx);
223 *sctx = NULL;
224}
+ Here is the caller graph for this function:

Variable Documentation

◆ MuttGsaslCtx

Gsasl* MuttGsaslCtx = NULL
static

Global GNU SASL handle.

Definition at line 40 of file gsasl.c.