-
Notifications
You must be signed in to change notification settings - Fork 0
/
gssapi.c
635 lines (541 loc) · 20.9 KB
/
gssapi.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include "emacs-module.h"
#include "gssapi/gssapi.h"
#include "gssapi/gssapi_krb5.h"
int plugin_is_GPL_compatible;
#if 0
static void message(emacs_env *env, char *fmt, ...)
{
int size = 0;
char *p = NULL;
va_list ap;
va_start(ap, fmt);
size = vsnprintf(p, size, fmt, ap);
va_end(ap);
size++;
p = malloc(size);
if(p == NULL) {
// Out of memory, simply don't print anything
return;
}
va_start(ap, fmt);
size = vsnprintf(p, size, fmt, ap);
va_end(ap);
if(size < 0) {
free(p);
return;
}
emacs_value Qmessage = env->intern(env, "message");
emacs_value args[] = { env->make_string(env, p, size) };
env->funcall(env, Qmessage, 1, args);
free(p);
}
#endif
static void bind_function(emacs_env *env, char *name, emacs_value Sfun)
{
emacs_value Qfset = env->intern(env, "fset");
emacs_value Qsym = env->intern(env, name);
emacs_value args[] = { Qsym, Sfun };
env->funcall(env, Qfset, 2, args);
}
static void provide_module(emacs_env *env, const char *feature)
{
emacs_value Qfeat = env->intern (env, feature);
emacs_value Qprovide = env->intern (env, "provide");
emacs_value args[] = { Qfeat };
env->funcall(env, Qprovide, 1, args);
}
static void throw_error(emacs_env *env, char *message) {
emacs_value error_sym = env->intern(env, "error");
emacs_value message_value = env->make_string(env, message, strlen(message));
env->funcall(env, error_sym, 1, &message_value);
}
static emacs_value xcar(emacs_env *env, emacs_value obj) {
emacs_value Qcar = env->intern(env, "car");
emacs_value args[] = { obj };
return env->funcall(env, Qcar, 1, args);
}
static emacs_value xcdr(emacs_env *env, emacs_value obj) {
emacs_value Qcdr = env->intern(env, "cdr");
emacs_value args[] = { obj };
return env->funcall(env, Qcdr, 1, args);
}
static emacs_value make_array(emacs_env *env, void *ptr, size_t len)
{
unsigned char *array_as_char = ptr;
emacs_value Qmake_vector = env->intern(env, "make-vector");
emacs_value args[] = { env->make_integer(env, len), env->make_integer(env, 0) };
emacs_value array = env->funcall(env, Qmake_vector, 2, args);
for(size_t i = 0 ; i < len ; i++) {
env->vec_set(env, array, i, env->make_integer(env, array_as_char[i]));
}
return array;
}
static emacs_value xcons(emacs_env *env, emacs_value x, emacs_value y)
{
emacs_value Qcons = env->intern(env, "cons");
emacs_value args[] = { x, y };
return env->funcall(env, Qcons, 2, args);
}
static emacs_value extract_error_message(emacs_env *env, OM_uint32 status, int status_code_type, const gss_OID mech)
{
emacs_value Qnil = env->intern(env, "nil");
emacs_value Qreverse = env->intern(env, "reverse");
emacs_value messages = Qnil;
OM_uint32 message_context = 0;
do {
gss_buffer_desc status_output;
OM_uint32 minor;
OM_uint32 result = gss_display_status(&minor, status, status_code_type, mech, &message_context, &status_output);
if(GSS_ERROR(result)) {
abort();
}
messages = xcons(env, env->make_string(env, status_output.value, status_output.length), messages);
result = gss_release_buffer(&minor, &status_output);
if(GSS_ERROR(result)) {
abort();
}
} while(message_context != 0);
messages = env->funcall(env, Qreverse, 1, &messages);
return messages;
}
static int check_error(emacs_env *env, OM_uint32 major_status, OM_uint32 minor_status)
{
if(!GSS_ERROR(major_status)) {
return 0;
}
emacs_value major_messages = extract_error_message(env, major_status, GSS_C_GSS_CODE, GSS_C_NO_OID);
emacs_value minor_messages;
if(GSS_ERROR(minor_status)) {
minor_messages = extract_error_message(env, minor_status, GSS_C_MECH_CODE, (gss_OID)gss_mech_krb5);
}
else {
minor_messages = env->intern(env, "nil");
}
emacs_value Qlist = env->intern(env, "list");
emacs_value list_args[] = { major_messages, minor_messages };
emacs_value error_list = env->funcall(env, Qlist, 2, list_args);
env->non_local_exit_throw(env, env->intern(env, "gss-error"), error_list);
return 1;
}
char *crash_status = NULL;
char *crash_status_minor = NULL;
static void release_name(void *name_ptr)
{
gss_name_t name = name_ptr;
OM_uint32 minor;
OM_uint32 result = gss_release_name(&minor, &name);
if(GSS_ERROR(result)) {
gss_buffer_desc status_output;
OM_uint32 err_minor;
OM_uint32 msgctx;
OM_uint32 err_result = gss_display_status(&err_minor, result, GSS_C_GSS_CODE, GSS_C_NO_OID, &msgctx, &status_output);
if(!GSS_ERROR(err_result)) {
crash_status = malloc(status_output.length + 1);
strncpy(crash_status, status_output.value, status_output.length);
crash_status[status_output.length] = 0;
}
abort();
}
}
typedef struct {
gss_ctx_id_t context;
int is_released;
} ContextWrapper;
static void free_context(void *context_ptr)
{
ContextWrapper *context_wrapper = context_ptr;
if(!context_wrapper->is_released) {
gss_buffer_desc output;
OM_uint32 minor;
gss_ctx_id_t context_id = context_wrapper->context;
OM_uint32 result = gss_delete_sec_context(&minor, &context_id, &output);
if(GSS_ERROR(result)) {
gss_buffer_desc status_output;
OM_uint32 err_minor;
OM_uint32 msgctx;
OM_uint32 err_result = gss_display_status(&err_minor, result, GSS_C_GSS_CODE, GSS_C_NO_OID, &msgctx, &status_output);
if(!GSS_ERROR(err_result)) {
crash_status = malloc(status_output.length + 1);
strncpy(crash_status, status_output.value, status_output.length);
crash_status[status_output.length] = 0;
msgctx = 0;
err_result = gss_display_status(&err_minor, minor, GSS_C_MECH_CODE, (gss_OID)gss_mech_krb5, &msgctx, &status_output);
if(!GSS_ERROR(err_result)) {
crash_status_minor = malloc(status_output.length + 1);
strncpy(crash_status_minor, status_output.value, status_output.length);
crash_status_minor[status_output.length] = 0;
}
}
abort();
}
}
free(context_wrapper);
}
static emacs_value Fgssapi_internal_import_name(emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data)
{
// Deal with unused variable warning
(void)data;
(void)nargs;
emacs_value name = args[0];
emacs_value type = args[1];
gss_OID_desc *gss_type;
if(env->eq(env, type, env->intern(env, ":user-name"))) {
gss_type = GSS_C_NT_USER_NAME;
}
else if(env->eq(env, type, env->intern(env, ":machine-uid-name"))) {
gss_type = GSS_C_NT_MACHINE_UID_NAME;
}
else if(env->eq(env, type, env->intern(env, ":string-uid-name"))) {
gss_type = GSS_C_NT_STRING_UID_NAME;
}
else if(env->eq(env, type, env->intern(env, ":hostbased-service"))) {
gss_type = GSS_C_NT_HOSTBASED_SERVICE;
}
else {
throw_error(env, "illegal type");
return env->intern(env, "nil");
}
ptrdiff_t size;
env->copy_string_contents(env, name, NULL, &size);
char *buf = malloc(size);
if(!env->copy_string_contents(env, name, buf, &size)) {
throw_error(env, "unable to copy string");
return env->intern(env, "nil");
}
gss_name_t output_name;
OM_uint32 minor;
gss_buffer_desc name_buf;
name_buf.value = buf;
name_buf.length = strlen(buf);
OM_uint32 result = gss_import_name(&minor, &name_buf, gss_type, &output_name);
if(check_error(env, result, minor)) {
return env->intern(env, "nil");
}
free(buf);
return env->make_user_ptr(env, release_name, output_name);
}
static emacs_value Fgssapi_internal_name_to_string(emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data)
{
(void)data;
(void)nargs;
emacs_value name = args[0];
gss_buffer_desc buffer;
gss_OID type;
OM_uint32 minor;
OM_uint32 result = gss_display_name(&minor, env->get_user_ptr(env, name), &buffer, &type);
if(check_error(env, result, minor)) {
return env->intern(env, "nil");
}
emacs_value ret = env->make_string(env, buffer.value, buffer.length);
result = gss_release_buffer(&minor, &buffer);
int release_buffer_error = check_error(env, result, minor);
result = gss_release_oid(&minor, &type);
if(!release_buffer_error) {
if(check_error(env, result, minor)) {
return env->intern(env, "nil");
}
}
else {
return env->intern(env, "nil");
}
return ret;
}
static OM_uint32 make_flags(emacs_env *env, emacs_value flags)
{
emacs_value Qdeleg = env->intern(env, ":deleg");
emacs_value Qmutual = env->intern(env, ":mutual");
emacs_value Qreplay = env->intern(env, ":replay");
emacs_value Qsequence = env->intern(env, ":sequence");
emacs_value Qconf = env->intern(env, ":conf");
emacs_value Qinteg = env->intern(env, ":integ");
emacs_value Qanon = env->intern(env, ":anon");
OM_uint32 result = 0;
emacs_value curr = flags;
while(env->is_not_nil(env, curr)) {
emacs_value v = xcar(env, curr);
if(env->eq(env, v, Qdeleg)) {
result |= GSS_C_DELEG_FLAG;
}
else if(env->eq(env, v, Qmutual)) {
result |= GSS_C_MUTUAL_FLAG;
}
else if(env->eq(env, v, Qreplay)) {
result |= GSS_C_REPLAY_FLAG;
}
else if(env->eq(env, v, Qsequence)) {
result |= GSS_C_SEQUENCE_FLAG;
}
else if(env->eq(env, v, Qconf)) {
result |= GSS_C_CONF_FLAG;
}
else if(env->eq(env, v, Qinteg)) {
result |= GSS_C_INTEG_FLAG;
}
else if(env->eq(env, v, Qanon)) {
result |= GSS_C_ANON_FLAG;
}
curr = xcdr(env, curr);
}
return result;
}
static emacs_value parse_flags(emacs_env *env, OM_uint32 flags)
{
emacs_value res = env->intern(env, "nil");
if(flags & GSS_C_DELEG_FLAG) {
res = xcons(env, env->intern(env, ":deleg"), res);
}
if(flags & GSS_C_MUTUAL_FLAG) {
res = xcons(env, env->intern(env, ":mutual"), res);
}
if(flags & GSS_C_REPLAY_FLAG) {
res = xcons(env, env->intern(env, ":replay"), res);
}
if(flags & GSS_C_SEQUENCE_FLAG) {
res = xcons(env, env->intern(env, ":sequence"), res);
}
if(flags & GSS_C_CONF_FLAG) {
res = xcons(env, env->intern(env, ":conf"), res);
}
if(flags & GSS_C_INTEG_FLAG) {
res = xcons(env, env->intern(env, ":integ"), res);
}
if(flags & GSS_C_ANON_FLAG) {
res = xcons(env, env->intern(env, ":anon"), res);
}
if(flags & GSS_C_PROT_READY_FLAG) {
res = xcons(env, env->intern(env, ":prot-ready"), res);
}
if(flags & GSS_C_TRANS_FLAG) {
res = xcons(env, env->intern(env, ":trans"), res);
}
return res;
}
static gss_ctx_id_t make_context_ref(emacs_env *env, emacs_value context)
{
if(env->is_not_nil(env, context)) {
ContextWrapper *w = env->get_user_ptr(env, context);
return w->context;
}
else {
return GSS_C_NO_CONTEXT;
}
}
static gss_buffer_desc make_input_token(emacs_env *env, emacs_value content)
{
gss_buffer_desc input_token;
if(env->is_not_nil(env, content)) {
size_t input_token_length = env->vec_size(env, content);
unsigned char *input_token_buf = malloc(input_token_length);
for(size_t i = 0 ; i < input_token_length ; i++) {
input_token_buf[i] = env->extract_integer(env, env->vec_get(env, content, i));
}
input_token.value = input_token_buf;
input_token.length = input_token_length;
}
else {
input_token.value = NULL;
input_token.length = 0;
}
return input_token;
}
static void free_input_token(gss_buffer_desc *token)
{
if(token->value != NULL) {
free(token->value);
}
}
static emacs_value Fgssapi_internal_init_sec_context(emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data)
{
(void)data;
(void)nargs;
emacs_value target = args[0];
emacs_value flags = args[1];
emacs_value context = args[2];
emacs_value time_req = args[3];
emacs_value content = args[4];
gss_ctx_id_t context_handle = make_context_ref(env, context);
gss_buffer_desc input_token = make_input_token(env, content);
gss_OID actual_mech_type;
gss_buffer_desc output_token;
OM_uint32 ret_flags;
OM_uint32 time_rec;
OM_uint32 minor;
OM_uint32 result = gss_init_sec_context(&minor, NULL, &context_handle, env->get_user_ptr(env, target),
GSS_C_NO_OID, make_flags(env, flags),
env->extract_integer(env, time_req), GSS_C_NO_CHANNEL_BINDINGS,
&input_token, &actual_mech_type, &output_token, &ret_flags, &time_rec);
free_input_token(&input_token);
if(GSS_ERROR(result)) {
if(env->is_not_nil(env, context)) {
ContextWrapper *context_wrapper = env->get_user_ptr(env, context);
context_wrapper->is_released = 1;
}
check_error(env, result, minor);
return env->intern(env, "nil");
}
emacs_value Qnil = env->intern(env, "nil");
emacs_value Qt = env->intern(env, "t");
emacs_value context_ret;
if(context_handle == NULL) {
context_ret = Qnil;
}
else if(env->is_not_nil(env, context)) {
context_ret = context;
}
else {
ContextWrapper *context_wrapper = malloc(sizeof(ContextWrapper));
context_wrapper->context = context_handle;
context_wrapper->is_released = 0;
context_ret = env->make_user_ptr(env, free_context, context_wrapper);
}
emacs_value result_list[] = { result & GSS_S_CONTINUE_NEEDED ? Qt : Qnil,
context_ret,
output_token.length == 0 ? Qnil : make_array(env, output_token.value, output_token.length),
parse_flags(env, ret_flags) };
result = gss_release_buffer(&minor, &output_token);
if(check_error(env, result, minor)) {
return Qnil;
}
return env->funcall(env, env->intern(env, "list"), 4, result_list);
}
static emacs_value Fgssapi_internal_accept_sec_context(emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data)
{
(void)data;
(void)nargs;
emacs_value content = args[0];
emacs_value context = args[1];
gss_ctx_id_t context_handle = make_context_ref(env, context);
gss_buffer_desc input_token = make_input_token(env, content);
gss_name_t src_name;
gss_buffer_desc output_token;
OM_uint32 ret_flags;
OM_uint32 time_rec;
gss_cred_id_t output_cred_handle;
OM_uint32 minor;
OM_uint32 result = gss_accept_sec_context(&minor, &context_handle, NULL, &input_token, GSS_C_NO_CHANNEL_BINDINGS,
&src_name, NULL, &output_token, &ret_flags, &time_rec, &output_cred_handle);
free_input_token(&input_token);
if(check_error(env, result, minor)) {
return env->intern(env, "nil");
}
emacs_value Qnil = env->intern(env, "nil");
emacs_value Qt = env->intern(env, "t");
emacs_value result_list[] = { result & GSS_S_CONTINUE_NEEDED ? Qt : Qnil,
context_handle == NULL ? Qnil : env->make_user_ptr(env, free_context, context_handle),
env->make_user_ptr(env, release_name, src_name),
output_token.length == 0 ? Qnil : make_array(env, output_token.value, output_token.length),
parse_flags(env, ret_flags),
env->make_integer(env, time_rec),
Qnil };
result = gss_release_buffer(&minor, &output_token);
if(check_error(env, result, minor)) {
return env->intern(env, "nil");
}
return env->funcall(env, env->intern(env, "list"), 7, result_list);
}
static emacs_value Fregister_acceptor_identity(emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data)
{
(void)data;
(void)nargs;
emacs_value filename = args[0];
ptrdiff_t size;
env->copy_string_contents(env, filename, NULL, &size);
char *buf = malloc(size);
if(!env->copy_string_contents(env, filename, buf, &size)) {
throw_error(env, "unable to copy string");
return env->intern(env, "nil");
}
OM_uint32 result = gsskrb5_register_acceptor_identity(buf);
free(buf);
if(GSS_ERROR(result)) {
throw_error(env, "Error loading file");
}
return env->intern(env, "nil");
}
static emacs_value Fwrap(emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data)
{
(void)data;
(void)nargs;
emacs_value context = args[0];
emacs_value buffer = args[1];
emacs_value conf = args[2];
ContextWrapper *context_wrapper = env->get_user_ptr(env, context);
gss_buffer_desc buffer_desc = make_input_token(env, buffer);
int conf_state;
gss_buffer_desc output_desc;
OM_uint32 minor;
OM_uint32 result = gss_wrap(&minor,
context_wrapper->context,
env->is_not_nil(env, conf) ? 1 : 0,
GSS_C_QOP_DEFAULT,
&buffer_desc,
&conf_state,
&output_desc);
free_input_token(&buffer_desc);
if(check_error(env, result, minor)) {
return env->intern(env, "nil");
}
emacs_value ret = make_array(env, output_desc.value, output_desc.length);
result = gss_release_buffer(&minor, &output_desc);
if(check_error(env, result, minor)) {
return env->intern(env, "nil");
}
emacs_value Qlist = env->intern(env, "list");
emacs_value result_list[] = { ret, conf_state ? env->intern(env, "t") : env->intern(env, "nil") };
return env->funcall(env, Qlist, 2, result_list);
}
static emacs_value Funwrap(emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data)
{
(void)data;
(void)nargs;
emacs_value context = args[0];
emacs_value buffer = args[1];
ContextWrapper *context_wrapper = env->get_user_ptr(env, context);
gss_buffer_desc buffer_desc = make_input_token(env, buffer);
int conf_state;
gss_qop_t qop_state;
gss_buffer_desc output_desc;
OM_uint32 minor;
OM_uint32 result = gss_unwrap(&minor,
context_wrapper->context,
&buffer_desc,
&output_desc,
&conf_state,
&qop_state);
free_input_token(&buffer_desc);
if(check_error(env, result, minor)) {
return env->intern(env, "nil");
}
emacs_value ret = make_array(env, output_desc.value, output_desc.length);
result = gss_release_buffer(&minor, &output_desc);
if(check_error(env, result, minor)) {
return env->intern(env, "nil");
}
emacs_value Qlist = env->intern(env, "list");
emacs_value result_list[] = { ret, conf_state ? env->intern(env, "t") : env->intern(env, "nil") };
return env->funcall(env, Qlist, 2, result_list);
}
int emacs_module_init(struct emacs_runtime *ert)
{
emacs_env *env = ert->get_environment(ert);
emacs_value make_name_fn = env->make_function(env, 2, 2, Fgssapi_internal_import_name, "integrate gss_import_name", NULL);
bind_function(env, "gss--internal-import-name", make_name_fn);
emacs_value name_to_string_fn = env->make_function(env, 1, 1, Fgssapi_internal_name_to_string, "integrate gss_display_name", NULL);
bind_function(env, "gss--internal-name-to-string", name_to_string_fn);
emacs_value init_sec_context_fn = env->make_function(env, 5, 5, Fgssapi_internal_init_sec_context, "integrate gss_init_sec_context", NULL);
bind_function(env, "gss--internal-init-sec-context", init_sec_context_fn);
emacs_value accept_sec_context_fn = env->make_function(env, 2, 2, Fgssapi_internal_accept_sec_context, "integrate gss_accept_sec_context", NULL);
bind_function(env, "gss--internal-accept-sec-context", accept_sec_context_fn);
emacs_value register_acceptor_identity_fn = env->make_function(env, 1, 1, Fregister_acceptor_identity, "integrate krb5_gss_register_acceptor_identity", NULL);
bind_function(env, "gss--internal-krb5-register-acceptor-identity", register_acceptor_identity_fn);
emacs_value wrap_fn = env->make_function(env, 3, 3, Fwrap, "integrates gss_wrap", NULL);
bind_function(env, "gss--internal-wrap", wrap_fn);
emacs_value unwrap_fn = env->make_function(env, 2, 2, Funwrap, "integrates gss_unwrap", NULL);
bind_function(env, "gss--internal-unwrap", unwrap_fn);
provide_module(env, "emacs-gssapi");
return 0;
}