auth_gss.c
7.95 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2001, 2002, 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 */
/*
GSSAPI authentication for imap (rfc 1731).
*/
#include "imap4d.h"
#include <netinet/in.h>
#include <krb5.h>
#include <gssapi/gssapi.h>
#include <gssapi/gssapi_generic.h>
#define GSS_AUTH_P_NONE 1
#define GSS_AUTH_P_INTEGRITY 2
#define GSS_AUTH_P_PRIVACY 4
#define SUPPORTED_P_MECH GSS_AUTH_P_NONE
static int protection_mech;
size_t server_buffer_size = 8192;
size_t client_buffer_size;
static void
display_status_1 (char *m, OM_uint32 code, int type)
{
OM_uint32 maj_stat, min_stat;
gss_buffer_desc msg;
OM_uint32 msg_ctx;
msg_ctx = 0;
do
{
maj_stat = gss_display_status (&min_stat, code,
type, GSS_C_NULL_OID, &msg_ctx, &msg);
syslog (LOG_ERR, _("GSS-API error %s: %s"), m, (char *) msg.value);
gss_release_buffer (&min_stat, &msg);
}
while (msg_ctx);
}
static void
display_status (char *msg, OM_uint32 maj_stat, OM_uint32 min_stat)
{
display_status_1 (msg, maj_stat, GSS_C_GSS_CODE);
display_status_1 (msg, min_stat, GSS_C_MECH_CODE);
}
static int
imap4d_gss_userok (gss_buffer_t client_name, char *name)
{
int rc = -1;
krb5_principal p;
krb5_context kcontext;
krb5_init_context (&kcontext);
if (krb5_parse_name (kcontext, client_name->value, &p) != 0)
return -1;
if (krb5_kuserok (kcontext, p, name))
rc = 0;
else
rc = 1;
krb5_free_principal (kcontext, p);
return rc;
}
static int
auth_gssapi (struct imap4d_command *command,
char *auth_type_unused, char *arg_unused, char **username)
{
gss_buffer_desc tokbuf, outbuf;
OM_uint32 maj_stat, min_stat, min_stat2;
OM_uint32 cflags;
OM_uint32 sec_level, mech;
gss_ctx_id_t context;
gss_cred_id_t cred_handle, server_creds;
gss_OID mech_type;
char *token_str;
unsigned char *tmp = NULL;
size_t size;
gss_name_t server_name;
gss_qop_t quality;
gss_name_t client;
gss_buffer_desc client_name;
/* Obtain server credentials. RFC 1732 states, that
"The server must issue a ready response with no data and pass the
resulting client supplied token to GSS_Accept_sec_context as
input_token, setting acceptor_cred_handle to NULL (for "use default
credentials"), and 0 for input_context_handle (initially)."
In MIT implementation, passing NULL as acceptor_cred_handle won't
work (possibly due to a bug in krb5_gss_accept_sec_context()), so
we acquire server credentials explicitly. */
asprintf ((char **) &tmp, "imap@%s", util_localname ());
tokbuf.value = tmp;
tokbuf.length = strlen (tokbuf.value) + 1;
maj_stat = gss_import_name (&min_stat, &tokbuf,
gss_nt_service_name, &server_name);
if (maj_stat != GSS_S_COMPLETE)
{
display_status ("import name", maj_stat, min_stat);
return RESP_NO;
}
maj_stat = gss_acquire_cred (&min_stat, server_name, 0,
GSS_C_NULL_OID_SET, GSS_C_ACCEPT,
&server_creds, NULL, NULL);
gss_release_name (&min_stat2, &server_name);
if (maj_stat != GSS_S_COMPLETE)
{
display_status ("acquire credentials", maj_stat, min_stat);
return RESP_NO;
}
/* Start the dialogue */
util_send ("+ \r\n");
util_flush_output ();
context = GSS_C_NO_CONTEXT;
for (;;)
{
token_str = imap4d_readline_ex ();
util_base64_decode (token_str, strlen (token_str), &tmp, &size);
tokbuf.value = tmp;
tokbuf.length = size;
free (token_str);
maj_stat = gss_accept_sec_context (&min_stat,
&context,
server_creds,
&tokbuf,
GSS_C_NO_CHANNEL_BINDINGS,
&client,
&mech_type,
&outbuf,
&cflags, NULL, &cred_handle);
free (tmp);
if (maj_stat == GSS_S_CONTINUE_NEEDED)
{
if (outbuf.length)
{
util_base64_encode (outbuf.value, outbuf.length, &tmp, &size);
util_send ("+ %*.*s\r\n", size, size, tmp);
free (tmp);
gss_release_buffer (&min_stat, &outbuf);
}
continue;
}
else if (maj_stat == GSS_S_COMPLETE)
break;
/* Bail out otherwise */
display_status ("accept context", maj_stat, min_stat);
maj_stat = gss_delete_sec_context (&min_stat, &context, &outbuf);
gss_release_buffer (&min_stat, &outbuf);
return RESP_NO;
}
if (outbuf.length)
{
util_base64_encode (outbuf.value, outbuf.length, &tmp, &size);
util_send ("+ %*.*s\r\n", size, size, tmp);
free (tmp);
gss_release_buffer (&min_stat, &outbuf);
token_str = imap4d_readline_ex ();
free (token_str);
}
/* Construct security-level data */
sec_level = htonl ((SUPPORTED_P_MECH << 24) | server_buffer_size);
tokbuf.length = 4;
tokbuf.value = &sec_level;
gss_wrap (&min_stat, context, 0, GSS_C_QOP_DEFAULT,
&tokbuf, &cflags, &outbuf);
util_base64_encode (outbuf.value, outbuf.length, &tmp, &size);
util_send ("+ %*.*s\r\n", size, size, tmp);
free (tmp);
token_str = imap4d_readline_ex ();
util_base64_decode (token_str, strlen (token_str),
(unsigned char **) &tokbuf.value, &tokbuf.length);
free (token_str);
gss_unwrap (&min_stat, context, &tokbuf, &outbuf, &cflags, &quality);
free (tokbuf.value);
sec_level = ntohl (*(OM_uint32 *) outbuf.value);
/* FIXME: parse sec_level and act accordingly to its settings */
mech = sec_level >> 24;
if ((mech & SUPPORTED_P_MECH) == 0)
{
syslog (LOG_NOTICE,
_("client requested unsupported protection mechanism (%d)"),
mech);
gss_release_buffer (&min_stat, &outbuf);
maj_stat = gss_delete_sec_context (&min_stat, &context, &outbuf);
gss_release_buffer (&min_stat, &outbuf);
return RESP_NO;
}
protection_mech = mech;
client_buffer_size = sec_level & 0x00ffffffff;
*username = malloc (outbuf.length - 4 + 1);
if (!*username)
{
syslog (LOG_NOTICE, _("not enough memory"));
gss_release_buffer (&min_stat, &outbuf);
maj_stat = gss_delete_sec_context (&min_stat, &context, &outbuf);
gss_release_buffer (&min_stat, &outbuf);
return RESP_NO;
}
memcpy (*username, (char *) outbuf.value + 4, outbuf.length - 4);
(*username)[outbuf.length - 4] = '\0';
gss_release_buffer (&min_stat, &outbuf);
maj_stat = gss_display_name (&min_stat, client, &client_name, &mech_type);
if (maj_stat != GSS_S_COMPLETE)
{
display_status ("get client name", maj_stat, min_stat);
maj_stat = gss_delete_sec_context (&min_stat, &context, &outbuf);
gss_release_buffer (&min_stat, &outbuf);
free (*username);
return RESP_NO;
}
if (imap4d_gss_userok (&client_name, *username))
{
syslog (LOG_NOTICE, _("GSSAPI user %s is NOT authorized as %s"),
(char *) client_name.value, *username);
maj_stat = gss_delete_sec_context (&min_stat, &context, &outbuf);
gss_release_buffer (&min_stat, &outbuf);
gss_release_buffer (&min_stat, &client_name);
free (*username);
return RESP_NO;
}
else
{
syslog (LOG_NOTICE, _("GSSAPI user %s is authorized as %s"),
(char *) client_name.value, *username);
}
gss_release_buffer (&min_stat, &client_name);
maj_stat = gss_delete_sec_context (&min_stat, &context, &outbuf);
gss_release_buffer (&min_stat, &outbuf);
return RESP_OK;
}
void
auth_gssapi_init ()
{
auth_add ("GSSAPI", auth_gssapi);
}