Debian Codegraph @

lib/gssapi/ntlm/init_sec_context.c

raw
1 /*
2 * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "ntlm.h"
35
36 static int
37 from_file(const char *fn, const char *target_domain,
38 char **domainp, char **usernamep, struct ntlm_buf *key)
39 {
40 char *str, buf[1024];
41 FILE *f;
42
43 *domainp = NULL;
44
45 f = fopen(fn, "r");
46 if (f == NULL)
47 return ENOENT;
48 rk_cloexec_file(f);
49
50 while (fgets(buf, sizeof(buf), f) != NULL) {
51 char *d, *u, *p;
52 buf[strcspn(buf, "\r\n")] = '\0';
53 if (buf[0] == '#')
54 continue;
55 str = NULL;
56 d = strtok_r(buf, ":", &str);
57 free(*domainp);
58 *domainp = NULL;
59 if (!d)
60 continue;
61 if (d && target_domain != NULL && strcasecmp(target_domain, d) != 0)
62 continue;
63 *domainp = strdup(d);
64 if (*domainp == NULL)
65 return ENOMEM;
66 u = strtok_r(NULL, ":", &str);
67 p = strtok_r(NULL, ":", &str);
68 if (u == NULL || p == NULL)
69 continue;
70
71 *usernamep = strdup(u);
72 if (*usernamep == NULL)
73 return ENOMEM;
74
75 heim_ntlm_nt_key(p, key);
76
77 memset_s(buf, sizeof(buf), 0, sizeof(buf));
78 fclose(f);
79 return 0;
80 }
81 memset_s(buf, sizeof(buf), 0, sizeof(buf));
82 fclose(f);
83 return ENOENT;
84 }
85
86 static int
87 get_user_file(const ntlm_name target_name,
88 char **domainp, char **usernamep, struct ntlm_buf *key)
89 {
90 const char *domain;
91 const char *fn;
92
93 *domainp = NULL;
94
95 if (issuid())
96 return ENOENT;
97
98 domain = target_name != NULL ? target_name->domain : NULL;
99
100 fn = getenv("NTLM_USER_FILE");
101 if (fn == NULL)
102 return ENOENT;
103 if (from_file(fn, domain, domainp, usernamep, key) == 0)
104 return 0;
105
106 return ENOENT;
107 }
108
109 /*
110 * Pick up the ntlm cred from the default krb5 credential cache.
111 */
112
113 static int
114 get_user_ccache(const ntlm_name name, char **domainp, char **usernamep, struct ntlm_buf *key)
115 {
116 krb5_context context = NULL;
117 krb5_principal client;
118 krb5_ccache id = NULL;
119 krb5_error_code ret;
120 char *confname;
121 krb5_data data;
122 int aret;
123
124 *domainp = NULL;
125 *usernamep = NULL;
126 krb5_data_zero(&data);
127 key->length = 0;
128 key->data = NULL;
129
130 ret = krb5_init_context(&context);
131 if (ret)
132 return ret;
133
134 ret = krb5_cc_default(context, &id);
135 if (ret)
136 goto out;
137
138 ret = krb5_cc_get_principal(context, id, &client);
139 if (ret)
140 goto out;
141
142 ret = krb5_unparse_name_flags(context, client,
143 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
144 usernamep);
145 krb5_free_principal(context, client);
146 if (ret)
147 goto out;
148
149 if (name != NULL) {
150 *domainp = strdup(name->domain);
151 } else {
152 krb5_data data_domain;
153
154 krb5_data_zero(&data_domain);
155 ret = krb5_cc_get_config(context, id, NULL, "default-ntlm-domain",
156 &data_domain);
157 if (ret)
158 goto out;
159
160 *domainp = strndup(data_domain.data, data_domain.length);
161 krb5_data_free(&data_domain);
162 }
163
164 if (*domainp == NULL) {
165 ret = krb5_enomem(context);
166 goto out;
167 }
168
169 aret = asprintf(&confname, "ntlm-key-%s", *domainp);
170 if (aret == -1) {
171 ret = krb5_enomem(context);
172 goto out;
173 }
174
175 ret = krb5_cc_get_config(context, id, NULL, confname, &data);
176 if (ret)
177 goto out;
178
179 key->data = malloc(data.length);
180 if (key->data == NULL) {
181 ret = ENOMEM;
182 goto out;
183 }
184 key->length = data.length;
185 memcpy(key->data, data.data, data.length);
186
187 out:
188 krb5_data_free(&data);
189 if (id)
190 krb5_cc_close(context, id);
191
192 krb5_free_context(context);
193
194 return ret;
195 }
196
197 int
198 _gss_ntlm_get_user_cred(const ntlm_name target_name,
199 ntlm_cred *rcred)
200 {
201 ntlm_cred cred;
202 int ret;
203
204 cred = calloc(1, sizeof(*cred));
205 if (cred == NULL)
206 return ENOMEM;
207
208 ret = get_user_file(target_name,
209 &cred->domain, &cred->username, &cred->key);
210 if (ret)
211 ret = get_user_ccache(target_name,
212 &cred->domain, &cred->username, &cred->key);
213 if (ret) {
214 free(cred);
215 return ret;
216 }
217
218 *rcred = cred;
219
220 return ret;
221 }
222
223 static int
224 _gss_copy_cred(ntlm_cred from, ntlm_cred *to)
225 {
226 *to = calloc(1, sizeof(**to));
227 if (*to == NULL)
228 return ENOMEM;
229 (*to)->username = strdup(from->username);
230 if ((*to)->username == NULL) {
231 free(*to);
232 return ENOMEM;
233 }
234 (*to)->domain = strdup(from->domain);
235 if ((*to)->domain == NULL) {
236 free((*to)->username);
237 free(*to);
238 return ENOMEM;
239 }
240 (*to)->key.data = malloc(from->key.length);
241 if ((*to)->key.data == NULL) {
242 free((*to)->domain);
243 free((*to)->username);
244 free(*to);
245 return ENOMEM;
246 }
247 memcpy((*to)->key.data, from->key.data, from->key.length);
248 (*to)->key.length = from->key.length;
249
250 return 0;
251 }
252
253 OM_uint32 GSSAPI_CALLCONV
254 _gss_ntlm_init_sec_context
255 (OM_uint32 * minor_status,
256 gss_const_cred_id_t initiator_cred_handle,
257 gss_ctx_id_t * context_handle,
258 gss_const_name_t target_name,
259 const gss_OID mech_type,
260 OM_uint32 req_flags,
261 OM_uint32 time_req,
262 const gss_channel_bindings_t input_chan_bindings,
263 const gss_buffer_t input_token,
264 gss_OID * actual_mech_type,
265 gss_buffer_t output_token,
266 OM_uint32 * ret_flags,
267 OM_uint32 * time_rec
268 )
269 {
270 ntlm_ctx ctx;
271 ntlm_name name = (ntlm_name)target_name;
272
273 *minor_status = 0;
274
275 if (ret_flags)
276 *ret_flags = 0;
277 if (time_rec)
278 *time_rec = 0;
279 if (actual_mech_type)
280 *actual_mech_type = GSS_C_NO_OID;
281
282 if (*context_handle == GSS_C_NO_CONTEXT) {
283 struct ntlm_type1 type1;
284 struct ntlm_buf data;
285 uint32_t flags = 0;
286 int ret;
287
288 ctx = calloc(1, sizeof(*ctx));
289 if (ctx == NULL) {
290 *minor_status = EINVAL;
291 return GSS_S_FAILURE;
292 }
293 *context_handle = (gss_ctx_id_t)ctx;
294
295 if (initiator_cred_handle != GSS_C_NO_CREDENTIAL) {
296 ntlm_cred cred = (ntlm_cred)initiator_cred_handle;
297 ret = _gss_copy_cred(cred, &ctx->client);
298 } else
299 ret = _gss_ntlm_get_user_cred(name, &ctx->client);
300
301 if (ret) {
302 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
303 *minor_status = ret;
304 return GSS_S_FAILURE;
305 }
306
307 if (req_flags & GSS_C_CONF_FLAG)
308 flags |= NTLM_NEG_SEAL;
309 if (req_flags & GSS_C_INTEG_FLAG)
310 flags |= NTLM_NEG_SIGN;
311 else
312 flags |= NTLM_NEG_ALWAYS_SIGN;
313
314 flags |= NTLM_NEG_UNICODE;
315 flags |= NTLM_NEG_NTLM;
316 flags |= NTLM_NEG_NTLM2_SESSION;
317 flags |= NTLM_NEG_KEYEX;
318
319 memset(&type1, 0, sizeof(type1));
320
321 type1.flags = flags;
322 type1.domain = name->domain;
323 type1.hostname = NULL;
324 type1.os[0] = 0;
325 type1.os[1] = 0;
326
327 ret = heim_ntlm_encode_type1(&type1, &data);
328 if (ret) {
329 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
330 *minor_status = ret;
331 return GSS_S_FAILURE;
332 }
333
334 output_token->value = data.data;
335 output_token->length = data.length;
336
337 return GSS_S_CONTINUE_NEEDED;
338 } else {
339 krb5_error_code ret;
340 struct ntlm_type2 type2;
341 struct ntlm_type3 type3;
342 struct ntlm_buf data;
343
344 ctx = (ntlm_ctx)*context_handle;
345
346 data.data = input_token->value;
347 data.length = input_token->length;
348
349 ret = heim_ntlm_decode_type2(&data, &type2);
350 if (ret) {
351 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
352 *minor_status = ret;
353 return GSS_S_FAILURE;
354 }
355
356 ctx->flags = type2.flags;
357
358 /* XXX check that type2.targetinfo matches `target_name´ */
359 /* XXX check verify targetinfo buffer */
360
361 memset(&type3, 0, sizeof(type3));
362
363 type3.username = ctx->client->username;
364 type3.flags = type2.flags;
365 type3.targetname = type2.targetname;
366 type3.ws = rk_UNCONST("workstation");
367
368 /*
369 * NTLM Version 1 if no targetinfo buffer.
370 */
371
372 if (1 || type2.targetinfo.length == 0) {
373 struct ntlm_buf sessionkey;
374
375 if (type2.flags & NTLM_NEG_NTLM2_SESSION) {
376 unsigned char nonce[8];
377
378 if (RAND_bytes(nonce, sizeof(nonce)) != 1) {
379 _gss_ntlm_delete_sec_context(minor_status,
380 context_handle, NULL);
381 *minor_status = EINVAL;
382 return GSS_S_FAILURE;
383 }
384
385 ret = heim_ntlm_calculate_ntlm2_sess(nonce,
386 type2.challenge,
387 ctx->client->key.data,
388 &type3.lm,
389 &type3.ntlm);
390 } else {
391 ret = heim_ntlm_calculate_ntlm1(ctx->client->key.data,
392 ctx->client->key.length,
393 type2.challenge,
394 &type3.ntlm);
395
396 }
397 if (ret) {
398 _gss_ntlm_delete_sec_context(minor_status,context_handle,NULL);
399 *minor_status = ret;
400 return GSS_S_FAILURE;
401 }
402
403 ret = heim_ntlm_build_ntlm1_master(ctx->client->key.data,
404 ctx->client->key.length,
405 &sessionkey,
406 &type3.sessionkey);
407 if (ret) {
408 if (type3.lm.data)
409 free(type3.lm.data);
410 if (type3.ntlm.data)
411 free(type3.ntlm.data);
412 _gss_ntlm_delete_sec_context(minor_status,context_handle,NULL);
413 *minor_status = ret;
414 return GSS_S_FAILURE;
415 }
416
417 ret = krb5_data_copy(&ctx->sessionkey,
418 sessionkey.data, sessionkey.length);
419 free(sessionkey.data);
420 if (ret) {
421 if (type3.lm.data)
422 free(type3.lm.data);
423 if (type3.ntlm.data)
424 free(type3.ntlm.data);
425 _gss_ntlm_delete_sec_context(minor_status,context_handle,NULL);
426 *minor_status = ret;
427 return GSS_S_FAILURE;
428 }
429 ctx->status |= STATUS_SESSIONKEY;
430
431 } else {
432 struct ntlm_buf sessionkey;
433 unsigned char ntlmv2[16];
434 struct ntlm_targetinfo ti;
435
436 /* verify infotarget */
437
438 ret = heim_ntlm_decode_targetinfo(&type2.targetinfo, 1, &ti);
439 if(ret) {
440 _gss_ntlm_delete_sec_context(minor_status,
441 context_handle, NULL);
442 *minor_status = ret;
443 return GSS_S_FAILURE;
444 }
445
446 if (ti.domainname && strcmp(ti.domainname, name->domain) != 0) {
447 _gss_ntlm_delete_sec_context(minor_status,
448 context_handle, NULL);
449 *minor_status = EINVAL;
450 return GSS_S_FAILURE;
451 }
452
453 ret = heim_ntlm_calculate_ntlm2(ctx->client->key.data,
454 ctx->client->key.length,
455 ctx->client->username,
456 name->domain,
457 type2.challenge,
458 &type2.targetinfo,
459 ntlmv2,
460 &type3.ntlm);
461 if (ret) {
462 _gss_ntlm_delete_sec_context(minor_status,
463 context_handle, NULL);
464 *minor_status = ret;
465 return GSS_S_FAILURE;
466 }
467
468 ret = heim_ntlm_build_ntlm1_master(ntlmv2, sizeof(ntlmv2),
469 &sessionkey,
470 &type3.sessionkey);
471 memset_s(ntlmv2, sizeof(ntlmv2), 0, sizeof(ntlmv2));
472 if (ret) {
473 _gss_ntlm_delete_sec_context(minor_status,
474 context_handle, NULL);
475 *minor_status = ret;
476 return GSS_S_FAILURE;
477 }
478
479 ctx->flags |= NTLM_NEG_NTLM2_SESSION;
480
481 ret = krb5_data_copy(&ctx->sessionkey,
482 sessionkey.data, sessionkey.length);
483 free(sessionkey.data);
484 if (ret) {
485 _gss_ntlm_delete_sec_context(minor_status,
486 context_handle, NULL);
487 *minor_status = ret;
488 return GSS_S_FAILURE;
489 }
490 }
491
492 if (ctx->flags & NTLM_NEG_NTLM2_SESSION) {
493 ctx->status |= STATUS_SESSIONKEY;
494 _gss_ntlm_set_key(&ctx->u.v2.send, 0, (ctx->flags & NTLM_NEG_KEYEX),
495 ctx->sessionkey.data,
496 ctx->sessionkey.length);
497 _gss_ntlm_set_key(&ctx->u.v2.recv, 1, (ctx->flags & NTLM_NEG_KEYEX),
498 ctx->sessionkey.data,
499 ctx->sessionkey.length);
500 } else {
501 ctx->status |= STATUS_SESSIONKEY;
502 RC4_set_key(&ctx->u.v1.crypto_recv.key,
503 ctx->sessionkey.length,
504 ctx->sessionkey.data);
505 RC4_set_key(&ctx->u.v1.crypto_send.key,
506 ctx->sessionkey.length,
507 ctx->sessionkey.data);
508 }
509
510
511
512 ret = heim_ntlm_encode_type3(&type3, &data, NULL);
513 free(type3.sessionkey.data);
514 if (type3.lm.data)
515 free(type3.lm.data);
516 if (type3.ntlm.data)
517 free(type3.ntlm.data);
518 if (ret) {
519 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
520 *minor_status = ret;
521 return GSS_S_FAILURE;
522 }
523
524 output_token->length = data.length;
525 output_token->value = data.data;
526
527 if (actual_mech_type)
528 *actual_mech_type = GSS_NTLM_MECHANISM;
529 if (ret_flags)
530 *ret_flags = 0;
531 if (time_rec)
532 *time_rec = GSS_C_INDEFINITE;
533
534 ctx->status |= STATUS_OPEN;
535
536 return GSS_S_COMPLETE;
537 }
538 }