auth_gsasl.c
5.46 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2003 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 2, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include "imap4d.h"
#include <gsasl.h>
#include <mailutils/gsasl.h>
static Gsasl_ctx *ctx;
static Gsasl_session_ctx *sess_ctx;
static int
create_gsasl_stream (stream_t *newstr, stream_t str, int flags)
{
int rc;
rc = gsasl_stream_create (newstr, str, sess_ctx, flags);
if (rc)
{
syslog (LOG_ERR, _("cannot create SASL stream: %s"),
mu_errstring (rc));
return RESP_NO;
}
if ((rc = stream_open (*newstr)) != 0)
{
char *p;
if (stream_strerror (*newstr, &p))
p = mu_errstring (rc);
syslog (LOG_ERR, _("cannot open SASL input stream: %s"), p);
return RESP_NO;
}
return RESP_OK;
}
static int
auth_gsasl (struct imap4d_command *command,
char *auth_type, char *arg, char **username)
{
char *input = NULL;
char output[512];
size_t output_len;
char *s, *client_init;
int rc;
client_init = util_getword (arg, &s);
if (!client_init)
return RESP_BAD;
util_unquote (&client_init);
rc = gsasl_server_start (ctx, auth_type, &sess_ctx);
if (rc != GSASL_OK)
{
syslog (LOG_NOTICE, _("SASL gsasl_server_start: %s"),
gsasl_strerror(rc));
return 0;
}
gsasl_server_application_data_set (sess_ctx, username);
input = client_init;
output[0] = '\0';
output_len = sizeof (output);
while ((rc = gsasl_server_step_base64 (sess_ctx, input, output, output_len))
== GSASL_NEEDS_MORE)
{
util_send ("+ %s\r\n", output);
input = imap4d_readline_ex ();
}
while (rc == GSASL_NEEDS_MORE);
if (rc != GSASL_OK)
{
syslog (LOG_NOTICE, _("GSASL error: %s"), gsasl_strerror (rc));
return RESP_NO;
}
if (*username == NULL)
{
syslog (LOG_NOTICE, _("GSASL %s: cannot get username"), auth_type);
return RESP_NO;
}
if (sess_ctx)
{
stream_t in, out, new_in, new_out;
util_get_input (&in);
util_get_output (&out);
if (create_gsasl_stream (&new_in, in, MU_STREAM_READ))
return RESP_NO;
if (create_gsasl_stream (&new_out, out, MU_STREAM_WRITE))
{
stream_destroy (&new_in, stream_get_owner (new_in));
return RESP_NO;
}
util_set_input (new_in);
util_set_output (new_out);
}
auth_gsasl_capa_init (1);
return RESP_OK;
}
static void
auth_gsasl_capa_init (int disable)
{
int rc;
char *listmech, *name, *s;
size_t size;
rc = gsasl_server_listmech (ctx, NULL, &size);
if (rc != GSASL_OK)
return;
listmech = malloc (size);
if (!listmech)
imap4d_bye (ERR_NO_MEM);
rc = gsasl_server_listmech (ctx, listmech, &size);
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);
}
/* This is for DIGEST-MD5 */
static int
cb_realm (Gsasl_session_ctx *ctx, char *out, size_t *outlen, size_t nth)
{
char *realm = util_localname ();
if (nth > 0)
return GSASL_NO_MORE_REALMS;
if (out)
{
if (*outlen < strlen (realm))
return GSASL_TOO_SMALL_BUFFER;
memcpy (out, realm, strlen (realm));
}
*outlen = strlen (realm);
return GSASL_OK;
}
static int
cb_validate (Gsasl_session_ctx *ctx,
const char *authorization_id,
const char *authentication_id,
const char *password)
{
char **username = gsasl_server_application_data_get (ctx);
*username = strdup (authentication_id ?
authentication_id : authorization_id);
return GSASL_OK;
}
#define GSSAPI_SERVICE "imap"
static int
cb_service (Gsasl_session_ctx *ctx, char *srv, size_t *srvlen,
char *host, size_t *hostlen)
{
char *hostname = util_localname ();
if (srv)
{
if (*srvlen < strlen (GSSAPI_SERVICE))
return GSASL_TOO_SMALL_BUFFER;
memcpy (srv, GSSAPI_SERVICE, strlen (GSSAPI_SERVICE));
}
if (srvlen)
*srvlen = strlen (GSSAPI_SERVICE);
if (host)
{
if (*hostlen < strlen (hostname))
return GSASL_TOO_SMALL_BUFFER;
memcpy (host, hostname, strlen (hostname));
}
if (hostlen)
*hostlen = strlen (hostname);
return GSASL_OK;
}
/* This gets called when SASL mechanism EXTERNAL is invoked */
static int
cb_external (Gsasl_session_ctx *ctx)
{
return GSASL_AUTHENTICATION_ERROR;
}
void
auth_gsasl_init ()
{
int rc;
rc = gsasl_init (&ctx);
if (rc != GSASL_OK)
{
syslog (LOG_NOTICE, _("cannot initialize libgsasl: %s"),
gsasl_strerror (rc));
}
gsasl_server_callback_realm_set (ctx, cb_realm);
gsasl_server_callback_external_set (ctx, cb_external);
gsasl_server_callback_validate_set (ctx, cb_validate);
gsasl_server_callback_service_set (ctx, cb_service);
auth_gsasl_capa_init (0);
}