auth_gsasl.c
7.8 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010 Free Software
Foundation, Inc.
GNU Mailutils 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 3, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#include "imap4d.h"
#include <gsasl.h>
#include <mailutils/gsasl.h>
#ifdef USE_SQL
# include <mailutils/sql.h>
#endif
static Gsasl *ctx;
static Gsasl_session *sess_ctx;
static void auth_gsasl_capa_init (int disable);
static int
create_gsasl_stream (mu_stream_t *newstr, mu_stream_t transport, int flags)
{
int rc;
rc = mu_gsasl_stream_create (newstr, transport, sess_ctx, flags);
if (rc)
{
mu_diag_output (MU_DIAG_ERROR, _("cannot create SASL stream: %s"),
mu_strerror (rc));
return RESP_NO;
}
if ((rc = mu_stream_open (*newstr)) != 0)
{
const char *p;
if (mu_stream_strerror (*newstr, &p))
p = mu_strerror (rc);
mu_diag_output (MU_DIAG_ERROR, _("cannot open SASL input stream: %s"), p);
return RESP_NO;
}
return RESP_OK;
}
int
gsasl_replace_streams (void *self, void *data)
{
mu_stream_t *s = data;
util_set_input (s[0]);
util_set_output (s[1]);
free (s);
util_event_remove (self);
free (self);
return 0;
}
static void
finish_session (void)
{
gsasl_finish (sess_ctx);
}
static int
auth_gsasl (struct imap4d_command *command, char *auth_type, char **username)
{
char *input_str = NULL;
size_t input_size = 0;
size_t input_len;
char *output;
int rc;
rc = gsasl_server_start (ctx, auth_type, &sess_ctx);
if (rc != GSASL_OK)
{
mu_diag_output (MU_DIAG_NOTICE, _("SASL gsasl_server_start: %s"),
gsasl_strerror (rc));
return 0;
}
gsasl_callback_hook_set (ctx, username);
output = NULL;
while ((rc = gsasl_step64 (sess_ctx, input_str, &output))
== GSASL_NEEDS_MORE)
{
util_send ("+ %s\r\n", output);
imap4d_getline (&input_str, &input_size, &input_len);
}
if (rc != GSASL_OK)
{
mu_diag_output (MU_DIAG_NOTICE, _("GSASL error: %s"),
gsasl_strerror (rc));
free (input_str);
free (output);
return RESP_NO;
}
/* Some SASL mechanisms output additional data when GSASL_OK is
returned, and clients must respond with an empty response. */
if (output[0])
{
util_send ("+ %s\r\n", output);
imap4d_getline (&input_str, &input_size, &input_len);
if (input_len != 0)
{
mu_diag_output (MU_DIAG_NOTICE, _("non-empty client response"));
free (input_str);
free (output);
return RESP_NO;
}
}
free (input_str);
free (output);
if (*username == NULL)
{
mu_diag_output (MU_DIAG_NOTICE, _("GSASL %s: cannot get username"), auth_type);
return RESP_NO;
}
if (sess_ctx)
{
mu_stream_t tmp, new_in, new_out;
mu_stream_t *s;
util_get_input (&tmp);
if (create_gsasl_stream (&new_in, tmp, MU_STREAM_READ))
return RESP_NO;
util_get_output (&tmp);
if (create_gsasl_stream (&new_out, tmp, MU_STREAM_WRITE))
{
mu_stream_destroy (&new_in, mu_stream_get_owner (new_in));
return RESP_NO;
}
s = calloc (2, sizeof (mu_stream_t));
s[0] = new_in;
s[1] = new_out;
util_register_event (STATE_NONAUTH, STATE_AUTH,
gsasl_replace_streams, s);
util_atexit (finish_session);
}
auth_gsasl_capa_init (1);
return RESP_OK;
}
static void
auth_gsasl_capa_init (int disable)
{
int rc;
char *listmech, *name, *s;
rc = gsasl_server_mechlist (ctx, &listmech);
if (rc != GSASL_OK)
return;
for (name = strtok_r (listmech, " ", &s); name;
name = strtok_r (NULL, " ", &s))
{
if (disable)
auth_remove (name);
else
auth_add (strdup (name), auth_gsasl);
}
free (listmech);
}
#define IMAP_GSSAPI_SERVICE "imap"
static int
retrieve_password (Gsasl *ctx, Gsasl_session *sctx)
{
char **username = gsasl_callback_hook_get (ctx);
const char *authid = gsasl_property_get (sctx, GSASL_AUTHID);
if (username && *username == 0)
*username = strdup (authid);
if (mu_gsasl_module_data.cram_md5_pwd
&& access (mu_gsasl_module_data.cram_md5_pwd, R_OK) == 0)
{
char *key;
int rc = gsasl_simple_getpass (mu_gsasl_module_data.cram_md5_pwd,
authid, &key);
if (rc == GSASL_OK)
{
gsasl_property_set (sctx, GSASL_PASSWORD, key);
free (key);
return rc;
}
}
#ifdef USE_SQL
if (mu_sql_module_config.password_type == password_plaintext)
{
char *passwd;
int status = mu_sql_getpass (*username, &passwd);
if (status == 0)
{
gsasl_property_set (sctx, GSASL_PASSWORD, passwd);
free (passwd);
return GSASL_OK;
}
}
#endif
return GSASL_AUTHENTICATION_ERROR;
}
static int
cb_validate (Gsasl *ctx, Gsasl_session *sctx)
{
int rc;
struct mu_auth_data *auth;
char **username = gsasl_callback_hook_get (ctx);
const char *authid = gsasl_property_get (sctx, GSASL_AUTHID);
const char *pass = gsasl_property_get (sctx, GSASL_PASSWORD);
if (!authid)
return GSASL_NO_AUTHID;
if (!pass)
return GSASL_NO_PASSWORD;
*username = strdup (authid);
auth = mu_get_auth_by_name (*username);
if (auth == NULL)
return GSASL_AUTHENTICATION_ERROR;
rc = mu_authenticate (auth, pass);
mu_auth_data_free (auth);
return rc == 0 ? GSASL_OK : GSASL_AUTHENTICATION_ERROR;
}
static int
callback (Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
{
int rc = GSASL_OK;
switch (prop) {
case GSASL_PASSWORD:
rc = retrieve_password (ctx, sctx);
break;
case GSASL_SERVICE:
gsasl_property_set (sctx, prop,
mu_gsasl_module_data.service ?
mu_gsasl_module_data.service :IMAP_GSSAPI_SERVICE);
break;
case GSASL_REALM:
gsasl_property_set (sctx, prop,
mu_gsasl_module_data.realm ?
mu_gsasl_module_data.realm : util_localname ());
break;
case GSASL_HOSTNAME:
gsasl_property_set (sctx, prop,
mu_gsasl_module_data.hostname ?
mu_gsasl_module_data.hostname : util_localname ());
break;
#if 0
FIXME:
case GSASL_VALIDATE_EXTERNAL:
case GSASL_VALIDATE_SECURID:
#endif
case GSASL_VALIDATE_SIMPLE:
rc = cb_validate (ctx, sctx);
break;
case GSASL_VALIDATE_ANONYMOUS:
if (mu_gsasl_module_data.anon_user)
{
char **username = gsasl_callback_hook_get (ctx);
mu_diag_output (MU_DIAG_INFO, _("anonymous user %s logged in"),
gsasl_property_get (sctx, GSASL_ANONYMOUS_TOKEN));
*username = strdup (mu_gsasl_module_data.anon_user);
}
else
{
mu_diag_output (MU_DIAG_ERR,
_("attempt to log in as anonymous user denied"));
}
break;
case GSASL_VALIDATE_GSSAPI:
{
char **username = gsasl_callback_hook_get (ctx);
*username = strdup (gsasl_property_get(sctx, GSASL_AUTHZID));
break;
}
default:
rc = GSASL_NO_CALLBACK;
mu_error (_("unsupported callback property %d"), prop);
break;
}
return rc;
}
void
auth_gsasl_init ()
{
int rc;
rc = gsasl_init (&ctx);
if (rc != GSASL_OK)
{
mu_diag_output (MU_DIAG_NOTICE, _("cannot initialize libgsasl: %s"),
gsasl_strerror (rc));
}
gsasl_callback_set (ctx, callback);
auth_gsasl_capa_init (0);
}