forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsasl.c
223 lines (195 loc) · 5.48 KB
/
gsasl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* @file
* GNU SASL authentication support
*
* @authors
* Copyright (C) 2022 Richard Russon <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
/**
* @page conn_gsasl GNU SASL authentication support
*
* GNU SASL authentication support
*/
#include "config.h"
#include <gsasl.h>
#include <stdbool.h>
#include "mutt/lib.h"
#include "connaccount.h"
#include "connection.h"
#include "gsasl2.h"
#include "mutt_account.h"
/// Global GNU SASL handle
static Gsasl *MuttGsaslCtx = NULL;
/**
* mutt_gsasl_callback - Callback to retrieve authname or user from ConnAccount
* @param ctx GNU SASL context
* @param sctx GNU SASL session
* @param prop Property to get, e.g. GSASL_PASSWORD
* @retval num GNU SASL error code, e.g. GSASL_OK
*/
static int mutt_gsasl_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
{
int rc = GSASL_NO_CALLBACK;
struct Connection *conn = gsasl_session_hook_get(sctx);
if (!conn)
{
mutt_debug(LL_DEBUG1, "missing session hook data!\n");
return rc;
}
switch (prop)
{
case GSASL_PASSWORD:
if (mutt_account_getpass(&conn->account))
return rc;
gsasl_property_set(sctx, GSASL_PASSWORD, conn->account.pass);
rc = GSASL_OK;
break;
case GSASL_AUTHID:
/* whom the provided password belongs to: login */
if (mutt_account_getlogin(&conn->account))
return rc;
gsasl_property_set(sctx, GSASL_AUTHID, conn->account.login);
rc = GSASL_OK;
break;
case GSASL_AUTHZID:
/* name of the user whose mail/resources you intend to access: user */
if (mutt_account_getuser(&conn->account))
return rc;
gsasl_property_set(sctx, GSASL_AUTHZID, conn->account.user);
rc = GSASL_OK;
break;
case GSASL_ANONYMOUS_TOKEN:
gsasl_property_set(sctx, GSASL_ANONYMOUS_TOKEN, "dummy");
rc = GSASL_OK;
break;
case GSASL_SERVICE:
{
const char *service = NULL;
switch (conn->account.type)
{
case MUTT_ACCT_TYPE_IMAP:
service = "imap";
break;
case MUTT_ACCT_TYPE_POP:
service = "pop";
break;
case MUTT_ACCT_TYPE_SMTP:
service = "smtp";
break;
default:
return rc;
}
gsasl_property_set(sctx, GSASL_SERVICE, service);
rc = GSASL_OK;
break;
}
case GSASL_HOSTNAME:
gsasl_property_set(sctx, GSASL_HOSTNAME, conn->account.host);
rc = GSASL_OK;
break;
default:
break;
}
return rc;
}
/**
* mutt_gsasl_init - Initialise GNU SASL library
* @retval true Success
*/
static bool mutt_gsasl_init(void)
{
if (MuttGsaslCtx)
return true;
int rc = gsasl_init(&MuttGsaslCtx);
if (rc != GSASL_OK)
{
MuttGsaslCtx = NULL;
mutt_debug(LL_DEBUG1, "libgsasl initialisation failed (%d): %s.\n", rc,
gsasl_strerror(rc));
return false;
}
gsasl_callback_set(MuttGsaslCtx, mutt_gsasl_callback);
return true;
}
/**
* mutt_gsasl_done - Shutdown GNU SASL library
*/
void mutt_gsasl_done(void)
{
if (!MuttGsaslCtx)
return;
gsasl_done(MuttGsaslCtx);
MuttGsaslCtx = NULL;
}
/**
* mutt_gsasl_get_mech - Pick a connection mechanism
* @param requested_mech Requested mechanism
* @param server_mechlist Server's list of mechanisms
* @retval ptr Selected mechanism string
*/
const char *mutt_gsasl_get_mech(const char *requested_mech, const char *server_mechlist)
{
if (!mutt_gsasl_init())
return NULL;
/* libgsasl does not do case-independent string comparisons,
* and stores its methods internally in uppercase. */
char *uc_server_mechlist = mutt_str_dup(server_mechlist);
if (uc_server_mechlist)
mutt_str_upper(uc_server_mechlist);
char *uc_requested_mech = mutt_str_dup(requested_mech);
if (uc_requested_mech)
mutt_str_upper(uc_requested_mech);
const char *sel_mech = NULL;
if (uc_requested_mech)
sel_mech = gsasl_client_suggest_mechanism(MuttGsaslCtx, uc_requested_mech);
else
sel_mech = gsasl_client_suggest_mechanism(MuttGsaslCtx, uc_server_mechlist);
FREE(&uc_requested_mech);
FREE(&uc_server_mechlist);
return sel_mech;
}
/**
* mutt_gsasl_client_new - Create a new GNU SASL client
* @param conn Connection to a server
* @param mech Mechanisms to use
* @param sctx GNU SASL Session
* @retval 0 Success
* @retval -1 Error
*/
int mutt_gsasl_client_new(struct Connection *conn, const char *mech, Gsasl_session **sctx)
{
if (!mutt_gsasl_init())
return -1;
int rc = gsasl_client_start(MuttGsaslCtx, mech, sctx);
if (rc != GSASL_OK)
{
*sctx = NULL;
mutt_debug(LL_DEBUG1, "gsasl_client_start failed (%d): %s.\n", rc, gsasl_strerror(rc));
return -1;
}
gsasl_session_hook_set(*sctx, conn);
return 0;
}
/**
* mutt_gsasl_client_finish - Free a GNU SASL client
* @param sctx GNU SASL Session
*/
void mutt_gsasl_client_finish(Gsasl_session **sctx)
{
gsasl_finish(*sctx);
*sctx = NULL;
}