forked from cyrusimap/cyrus-sasl
/
gssapi.c
2134 lines (1823 loc) · 58.8 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GSSAPI SASL plugin
* Leif Johansson
* Rob Siemborski (SASL v2 Conversion)
* $Id: gssapi.c,v 1.115 2011/11/21 15:12:35 mel Exp $
*/
/*
* Copyright (c) 1998-2003 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any other legal
* details, please contact
* Office of Technology Transfer
* Carnegie Mellon University
* 5000 Forbes Avenue
* Pittsburgh, PA 15213-3890
* (412) 268-4387, fax: (412) 268-7395
* tech-transfer@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
#ifdef HAVE_GSSAPI_H
#include <gssapi.h>
#else
#include <gssapi/gssapi.h>
#endif
#ifdef WIN32
# include <winsock2.h>
# ifndef R_OK
# define R_OK 04
# endif
/* we also need io.h for access() prototype */
# include <io.h>
#else
# include <sys/param.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <netdb.h>
#endif /* WIN32 */
#include <fcntl.h>
#include <stdio.h>
#include <sasl.h>
#include <saslutil.h>
#include <saslplug.h>
#include "plugin_common.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <assert.h>
/***************************** Common Section *****************************/
static const char plugin_id[] = "$Id: gssapi.c,v 1.115 2011/11/21 15:12:35 mel Exp $";
static const char * GSSAPI_BLANK_STRING = "";
static gss_OID_desc gss_spnego_oid = { 6, (void *) "\x2b\x06\x01\x05\x05\x02" };
#if !defined(HAVE_GSS_C_NT_HOSTBASED_SERVICE) && !defined(GSS_C_NT_HOSTBASED_SERVICE)
extern gss_OID gss_nt_service_name;
#define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
#endif
#ifdef WANT_KERBEROS5_3DES
/* Check if CyberSafe flag is defined */
#ifdef CSF_GSS_C_DES3_FLAG
#define K5_MAX_SSF 112
#endif
/* Heimdal and MIT use the following */
#ifdef GSS_KRB5_CONF_C_QOP_DES3_KD
#define K5_MAX_SSF 112
#endif
#endif
#ifndef K5_MAX_SSF
/* All Kerberos implementations support DES */
#define K5_MAX_SSF 56
#endif
/* GSSAPI SASL Mechanism by Leif Johansson <leifj@matematik.su.se>
* inspired by the kerberos mechanism and the gssapi_server and
* gssapi_client from the heimdal distribution by Assar Westerlund
* <assar@sics.se> and Johan Danielsson <joda@pdc.kth.se>.
* See the configure.in file for details on dependencies.
*
* Important contributions from Sam Hartman <hartmans@fundsxpress.com>.
*
* This code was tested with the following distributions of Kerberos:
* Heimdal (http://www.pdc.kth.se/heimdal), MIT (http://web.mit.edu/kerberos/www/)
* CyberSafe (http://www.cybersafe.com/) and SEAM.
*/
#ifdef GSS_USE_MUTEXES
#define GSS_LOCK_MUTEX(utils) \
if(((sasl_utils_t *)(utils))->mutex_lock(gss_mutex) != 0) { \
return SASL_FAIL; \
}
#define GSS_UNLOCK_MUTEX(utils) \
if(((sasl_utils_t *)(utils))->mutex_unlock(gss_mutex) != 0) { \
return SASL_FAIL; \
}
static void *gss_mutex = NULL;
#else
#define GSS_LOCK_MUTEX(utils)
#define GSS_UNLOCK_MUTEX(utils)
#endif
typedef struct context {
int state;
gss_OID mech_type; /* GSS-SPNEGO or GSSAPI */
int http_mode; /* use RFC 4559 compatible protocol? */
gss_ctx_id_t gss_ctx;
gss_name_t client_name;
gss_name_t server_name;
gss_cred_id_t server_creds;
gss_cred_id_t client_creds;
sasl_ssf_t limitssf, requiressf; /* application defined bounds, for the
server */
unsigned char qop; /* as allowed by GSSAPI */
const sasl_utils_t *utils;
/* layers buffering */
decode_context_t decode_context;
char *encode_buf; /* For encoding/decoding mem management */
char *decode_buf;
char *decode_once_buf;
unsigned encode_buf_len;
unsigned decode_buf_len;
unsigned decode_once_buf_len;
buffer_info_t *enc_in_buf;
char *out_buf; /* per-step mem management */
unsigned out_buf_len;
char *authid; /* hold the authid between steps - server */
const char *user; /* hold the userid between steps - client */
} context_t;
enum {
SASL_GSSAPI_STATE_AUTHNEG = 1,
SASL_GSSAPI_STATE_SSFCAP = 2,
SASL_GSSAPI_STATE_SSFREQ = 3,
SASL_GSSAPI_STATE_AUTHENTICATED = 4
};
#define LAYER_CONFIDENTIALITY 4
#define LAYER_INTEGRITY 2
#define LAYER_NONE 1
/* sasl_gss_log: only logs status string returned from gss_display_status() */
#define sasl_gss_log(x,y,z) sasl_gss_seterror_(x,y,z,1)
#define sasl_gss_seterror(x,y,z) sasl_gss_seterror_(x,y,z,0)
static int
sasl_gss_seterror_(const sasl_utils_t *utils, OM_uint32 maj, OM_uint32 min,
int logonly)
{
OM_uint32 maj_stat, min_stat;
gss_buffer_desc msg;
OM_uint32 msg_ctx;
int ret;
char *out = NULL;
size_t len, curlen = 0;
const char prefix[] = "GSSAPI Error: ";
if (!utils) return SASL_OK;
len = sizeof(prefix);
ret = _plug_buf_alloc(utils, &out, &curlen, 256);
if (ret != SASL_OK) return SASL_NOMEM;
strcpy(out, prefix);
msg_ctx = 0;
while (1) {
GSS_LOCK_MUTEX(utils);
maj_stat = gss_display_status(&min_stat, maj,
GSS_C_GSS_CODE, GSS_C_NULL_OID,
&msg_ctx, &msg);
GSS_UNLOCK_MUTEX(utils);
if(GSS_ERROR(maj_stat)) {
if (logonly) {
utils->log(utils->conn, SASL_LOG_FAIL,
"GSSAPI Failure: (could not get major error message)");
} else {
utils->seterror(utils->conn, 0,
"GSSAPI Failure "
"(could not get major error message)");
}
utils->free(out);
return SASL_OK;
}
len += len + msg.length;
ret = _plug_buf_alloc(utils, &out, &curlen, len);
if(ret != SASL_OK) {
utils->free(out);
return SASL_NOMEM;
}
strcat(out, msg.value);
GSS_LOCK_MUTEX(utils);
gss_release_buffer(&min_stat, &msg);
GSS_UNLOCK_MUTEX(utils);
if (!msg_ctx)
break;
}
/* Now get the minor status */
len += 2;
ret = _plug_buf_alloc(utils, &out, &curlen, len);
if(ret != SASL_OK) {
utils->free(out);
return SASL_NOMEM;
}
strcat(out, " (");
msg_ctx = 0;
while (1) {
GSS_LOCK_MUTEX(utils);
maj_stat = gss_display_status(&min_stat, min,
GSS_C_MECH_CODE, GSS_C_NULL_OID,
&msg_ctx, &msg);
GSS_UNLOCK_MUTEX(utils);
if(GSS_ERROR(maj_stat)) {
if (logonly) {
utils->log(utils->conn, SASL_LOG_FAIL,
"GSSAPI Failure: (could not get minor error message)");
} else {
utils->seterror(utils->conn, 0,
"GSSAPI Failure "
"(could not get minor error message)");
}
utils->free(out);
return SASL_OK;
}
len += len + msg.length;
ret = _plug_buf_alloc(utils, &out, &curlen, len);
if(ret != SASL_OK) {
utils->free(out);
return SASL_NOMEM;
}
strcat(out, msg.value);
GSS_LOCK_MUTEX(utils);
gss_release_buffer(&min_stat, &msg);
GSS_UNLOCK_MUTEX(utils);
if (!msg_ctx)
break;
}
len += 1;
ret = _plug_buf_alloc(utils, &out, &curlen, len);
if(ret != SASL_OK) {
utils->free(out);
return SASL_NOMEM;
}
strcat(out, ")");
if (logonly) {
utils->log(utils->conn, SASL_LOG_FAIL, out);
} else {
utils->seterror(utils->conn, 0, out);
}
utils->free(out);
return SASL_OK;
}
static int
sasl_gss_encode(void *context, const struct iovec *invec, unsigned numiov,
const char **output, unsigned *outputlen, int privacy)
{
context_t *text = (context_t *)context;
OM_uint32 maj_stat, min_stat;
gss_buffer_t input_token, output_token;
gss_buffer_desc real_input_token, real_output_token;
int ret;
struct buffer_info *inblob, bufinfo;
if (!output) return SASL_BADPARAM;
if (numiov > 1) {
ret = _plug_iovec_to_buf(text->utils, invec, numiov, &text->enc_in_buf);
if (ret != SASL_OK) return ret;
inblob = text->enc_in_buf;
} else {
bufinfo.data = invec[0].iov_base;
bufinfo.curlen = invec[0].iov_len;
inblob = &bufinfo;
}
if (text->state != SASL_GSSAPI_STATE_AUTHENTICATED) return SASL_NOTDONE;
input_token = &real_input_token;
real_input_token.value = inblob->data;
real_input_token.length = inblob->curlen;
output_token = &real_output_token;
output_token->value = NULL;
output_token->length = 0;
GSS_LOCK_MUTEX(text->utils);
maj_stat = gss_wrap (&min_stat,
text->gss_ctx,
privacy,
GSS_C_QOP_DEFAULT,
input_token,
NULL,
output_token);
GSS_UNLOCK_MUTEX(text->utils);
if (GSS_ERROR(maj_stat)) {
sasl_gss_seterror(text->utils, maj_stat, min_stat);
if (output_token->value) {
GSS_LOCK_MUTEX(text->utils);
gss_release_buffer(&min_stat, output_token);
GSS_UNLOCK_MUTEX(text->utils);
}
return SASL_FAIL;
}
if (output_token->value && output) {
unsigned char * p;
ret = _plug_buf_alloc(text->utils,
&(text->encode_buf),
&(text->encode_buf_len),
output_token->length + 4);
if (ret != SASL_OK) {
GSS_LOCK_MUTEX(text->utils);
gss_release_buffer(&min_stat, output_token);
GSS_UNLOCK_MUTEX(text->utils);
return ret;
}
p = (unsigned char *) text->encode_buf;
p[0] = (output_token->length>>24) & 0xFF;
p[1] = (output_token->length>>16) & 0xFF;
p[2] = (output_token->length>>8) & 0xFF;
p[3] = output_token->length & 0xFF;
memcpy(text->encode_buf + 4, output_token->value, output_token->length);
}
if (outputlen) {
*outputlen = output_token->length + 4;
}
*output = text->encode_buf;
if (output_token->value) {
GSS_LOCK_MUTEX(text->utils);
gss_release_buffer(&min_stat, output_token);
GSS_UNLOCK_MUTEX(text->utils);
}
return SASL_OK;
}
static int gssapi_privacy_encode(void *context, const struct iovec *invec,
unsigned numiov, const char **output,
unsigned *outputlen)
{
return sasl_gss_encode(context,invec,numiov,output,outputlen,1);
}
static int gssapi_integrity_encode(void *context, const struct iovec *invec,
unsigned numiov, const char **output,
unsigned *outputlen)
{
return sasl_gss_encode(context,invec,numiov,output,outputlen,0);
}
static int
gssapi_decode_packet(void *context,
const char *input,
unsigned inputlen,
char **output,
unsigned *outputlen)
{
context_t *text = (context_t *) context;
OM_uint32 maj_stat, min_stat;
gss_buffer_t input_token, output_token;
gss_buffer_desc real_input_token, real_output_token;
int result;
if (text->state != SASL_GSSAPI_STATE_AUTHENTICATED) {
SETERROR(text->utils, "GSSAPI Failure");
return SASL_NOTDONE;
}
input_token = &real_input_token;
real_input_token.value = (char *) input;
real_input_token.length = inputlen;
output_token = &real_output_token;
output_token->value = NULL;
output_token->length = 0;
GSS_LOCK_MUTEX(text->utils);
maj_stat = gss_unwrap (&min_stat,
text->gss_ctx,
input_token,
output_token,
NULL,
NULL);
GSS_UNLOCK_MUTEX(text->utils);
if (GSS_ERROR(maj_stat)) {
sasl_gss_seterror(text->utils,maj_stat,min_stat);
if (output_token->value) {
GSS_LOCK_MUTEX(text->utils);
gss_release_buffer(&min_stat, output_token);
GSS_UNLOCK_MUTEX(text->utils);
}
return SASL_FAIL;
}
if (outputlen) {
*outputlen = output_token->length;
}
if (output_token->value) {
if (output) {
result = _plug_buf_alloc(text->utils, &text->decode_once_buf,
&text->decode_once_buf_len,
*outputlen);
if (result != SASL_OK) {
GSS_LOCK_MUTEX(text->utils);
gss_release_buffer(&min_stat, output_token);
GSS_UNLOCK_MUTEX(text->utils);
return result;
}
*output = text->decode_once_buf;
memcpy(*output, output_token->value, *outputlen);
}
GSS_LOCK_MUTEX(text->utils);
gss_release_buffer(&min_stat, output_token);
GSS_UNLOCK_MUTEX(text->utils);
}
return SASL_OK;
}
static int gssapi_decode(void *context,
const char *input, unsigned inputlen,
const char **output, unsigned *outputlen)
{
context_t *text = (context_t *) context;
int ret;
ret = _plug_decode(&text->decode_context, input, inputlen,
&text->decode_buf, &text->decode_buf_len, outputlen,
gssapi_decode_packet, text);
*output = text->decode_buf;
return ret;
}
static context_t *sasl_gss_new_context(const sasl_utils_t *utils)
{
context_t *ret;
ret = utils->malloc(sizeof(context_t));
if(!ret) return NULL;
memset(ret,0,sizeof(context_t));
ret->utils = utils;
return ret;
}
static int sasl_gss_free_context_contents(context_t *text)
{
OM_uint32 maj_stat, min_stat;
if (!text) return SASL_OK;
GSS_LOCK_MUTEX(text->utils);
if (text->gss_ctx != GSS_C_NO_CONTEXT) {
maj_stat = gss_delete_sec_context(&min_stat,&text->gss_ctx,
GSS_C_NO_BUFFER);
text->gss_ctx = GSS_C_NO_CONTEXT;
}
if (text->client_name != GSS_C_NO_NAME) {
maj_stat = gss_release_name(&min_stat,&text->client_name);
text->client_name = GSS_C_NO_NAME;
}
if (text->server_name != GSS_C_NO_NAME) {
maj_stat = gss_release_name(&min_stat,&text->server_name);
text->server_name = GSS_C_NO_NAME;
}
if ( text->server_creds != GSS_C_NO_CREDENTIAL) {
maj_stat = gss_release_cred(&min_stat, &text->server_creds);
text->server_creds = GSS_C_NO_CREDENTIAL;
}
if ( text->client_creds != GSS_C_NO_CREDENTIAL) {
maj_stat = gss_release_cred(&min_stat, &text->client_creds);
text->client_creds = GSS_C_NO_CREDENTIAL;
}
GSS_UNLOCK_MUTEX(text->utils);
if (text->out_buf) {
text->utils->free(text->out_buf);
text->out_buf = NULL;
}
if (text->encode_buf) {
text->utils->free(text->encode_buf);
text->encode_buf = NULL;
}
if (text->decode_buf) {
text->utils->free(text->decode_buf);
text->decode_buf = NULL;
}
if (text->decode_once_buf) {
text->utils->free(text->decode_once_buf);
text->decode_once_buf = NULL;
}
if (text->enc_in_buf) {
if(text->enc_in_buf->data) text->utils->free(text->enc_in_buf->data);
text->utils->free(text->enc_in_buf);
text->enc_in_buf = NULL;
}
_plug_decode_free(&text->decode_context);
if (text->authid) { /* works for both client and server */
text->utils->free(text->authid);
text->authid = NULL;
}
return SASL_OK;
}
static void gssapi_common_mech_dispose(void *conn_context,
const sasl_utils_t *utils)
{
sasl_gss_free_context_contents((context_t *)(conn_context));
utils->free(conn_context);
}
static void gssapi_common_mech_free(void *global_context __attribute__((unused)),
const sasl_utils_t *utils)
{
#ifdef GSS_USE_MUTEXES
if (gss_mutex) {
utils->mutex_free(gss_mutex);
gss_mutex=NULL;
}
#endif
}
/* The GSS-SPNEGO mechanism does not do SSF negotiation, instead it uses the
* flags negotiated by GSSAPI to determine If confidentiality or integrity are
* used. These flags are stored in text->qop transalated as layers by the
* caller */
static int gssapi_spnego_ssf(context_t *text, const sasl_utils_t *utils,
sasl_security_properties_t *props,
sasl_out_params_t *oparams)
{
OM_uint32 maj_stat = 0, min_stat = 0;
OM_uint32 max_input;
if (text->qop & LAYER_CONFIDENTIALITY) {
oparams->encode = &gssapi_privacy_encode;
oparams->decode = &gssapi_decode;
oparams->mech_ssf = K5_MAX_SSF;
} else if (text->qop & LAYER_INTEGRITY) {
oparams->encode = &gssapi_integrity_encode;
oparams->decode = &gssapi_decode;
oparams->mech_ssf = 1;
} else {
oparams->encode = NULL;
oparams->decode = NULL;
oparams->mech_ssf = 0;
}
if (oparams->mech_ssf) {
maj_stat = gss_wrap_size_limit(&min_stat,
text->gss_ctx,
1,
GSS_C_QOP_DEFAULT,
(OM_uint32)oparams->maxoutbuf,
&max_input);
if (max_input > oparams->maxoutbuf) {
/* Heimdal appears to get this wrong */
oparams->maxoutbuf -= (max_input - oparams->maxoutbuf);
} else {
/* This code is actually correct */
oparams->maxoutbuf = max_input;
}
}
text->state = SASL_GSSAPI_STATE_AUTHENTICATED;
/* used by layers */
_plug_decode_init(&text->decode_context, text->utils,
(props->maxbufsize > 0xFFFFFF) ? 0xFFFFFF :
props->maxbufsize);
return SASL_OK;
}
/***************************** Server Section *****************************/
static int
gssapi_server_mech_new(void *glob_context,
sasl_server_params_t *params,
const char *challenge __attribute__((unused)),
unsigned challen __attribute__((unused)),
void **conn_context)
{
context_t *text;
text = sasl_gss_new_context(params->utils);
if (text == NULL) {
MEMERROR(params->utils);
return SASL_NOMEM;
}
text->gss_ctx = GSS_C_NO_CONTEXT;
text->client_name = GSS_C_NO_NAME;
text->server_name = GSS_C_NO_NAME;
text->server_creds = GSS_C_NO_CREDENTIAL;
text->client_creds = GSS_C_NO_CREDENTIAL;
text->state = SASL_GSSAPI_STATE_AUTHNEG;
text->http_mode = (params->flags & SASL_NEED_HTTP);
text->mech_type = (gss_OID) glob_context;
*conn_context = text;
return SASL_OK;
}
static int
gssapi_server_mech_authneg(context_t *text,
sasl_server_params_t *params,
const char *clientin,
unsigned clientinlen,
const char **serverout,
unsigned *serveroutlen,
sasl_out_params_t *oparams)
{
gss_buffer_t input_token, output_token;
gss_buffer_desc real_input_token, real_output_token;
OM_uint32 maj_stat = 0, min_stat = 0;
gss_buffer_desc name_token;
int ret, equal = 0;
unsigned out_flags = 0;
gss_cred_id_t server_creds = (gss_cred_id_t) params->gss_creds;
gss_buffer_desc name_without_realm;
gss_name_t client_name_MN = NULL, without = NULL;
gss_OID mech_type;
input_token = &real_input_token;
output_token = &real_output_token;
output_token->value = NULL; output_token->length = 0;
input_token->value = NULL; input_token->length = 0;
if (text->server_name == GSS_C_NO_NAME) { /* only once */
if (params->serverFQDN == NULL
|| strlen(params->serverFQDN) == 0) {
SETERROR(text->utils, "GSSAPI Failure: no serverFQDN");
sasl_gss_free_context_contents(text);
return SASL_FAIL;
}
name_token.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
name_token.value = (char *)params->utils->malloc((name_token.length + 1) * sizeof(char));
if (name_token.value == NULL) {
MEMERROR(text->utils);
sasl_gss_free_context_contents(text);
return SASL_NOMEM;
}
sprintf(name_token.value,"%s@%s", params->service, params->serverFQDN);
GSS_LOCK_MUTEX(params->utils);
maj_stat = gss_import_name (&min_stat,
&name_token,
GSS_C_NT_HOSTBASED_SERVICE,
&text->server_name);
GSS_UNLOCK_MUTEX(params->utils);
params->utils->free(name_token.value);
name_token.value = NULL;
if (GSS_ERROR(maj_stat)) {
sasl_gss_seterror(text->utils, maj_stat, min_stat);
sasl_gss_free_context_contents(text);
return SASL_FAIL;
}
if ( text->server_creds != GSS_C_NO_CREDENTIAL) {
GSS_LOCK_MUTEX(params->utils);
maj_stat = gss_release_cred(&min_stat, &text->server_creds);
GSS_UNLOCK_MUTEX(params->utils);
text->server_creds = GSS_C_NO_CREDENTIAL;
}
/* If caller didn't provide creds already */
if ( server_creds == GSS_C_NO_CREDENTIAL) {
GSS_LOCK_MUTEX(params->utils);
maj_stat = gss_acquire_cred(&min_stat,
text->server_name,
GSS_C_INDEFINITE,
GSS_C_NO_OID_SET,
GSS_C_ACCEPT,
&text->server_creds,
NULL,
NULL);
GSS_UNLOCK_MUTEX(params->utils);
if (GSS_ERROR(maj_stat)) {
sasl_gss_seterror(text->utils, maj_stat, min_stat);
sasl_gss_free_context_contents(text);
return SASL_FAIL;
}
server_creds = text->server_creds;
}
}
if (clientinlen) {
real_input_token.value = (void *)clientin;
real_input_token.length = clientinlen;
}
GSS_LOCK_MUTEX(params->utils);
maj_stat =
gss_accept_sec_context(&min_stat,
&(text->gss_ctx),
server_creds,
input_token,
GSS_C_NO_CHANNEL_BINDINGS,
&text->client_name,
&mech_type,
output_token,
&out_flags,
NULL, /* context validity period */
&(text->client_creds));
GSS_UNLOCK_MUTEX(params->utils);
if (GSS_ERROR(maj_stat)) {
sasl_gss_log(text->utils, maj_stat, min_stat);
text->utils->seterror(text->utils->conn, SASL_NOLOG, "GSSAPI Failure: gss_accept_sec_context");
if (output_token->value) {
GSS_LOCK_MUTEX(params->utils);
gss_release_buffer(&min_stat, output_token);
GSS_UNLOCK_MUTEX(params->utils);
}
sasl_gss_free_context_contents(text);
return SASL_BADAUTH;
}
if (serveroutlen) {
*serveroutlen = output_token->length;
}
if (output_token->value) {
if (serverout) {
ret = _plug_buf_alloc(text->utils, &(text->out_buf),
&(text->out_buf_len), *serveroutlen);
if(ret != SASL_OK) {
GSS_LOCK_MUTEX(params->utils);
gss_release_buffer(&min_stat, output_token);
GSS_UNLOCK_MUTEX(params->utils);
return ret;
}
memcpy(text->out_buf, output_token->value, *serveroutlen);
*serverout = text->out_buf;
}
GSS_LOCK_MUTEX(params->utils);
gss_release_buffer(&min_stat, output_token);
GSS_UNLOCK_MUTEX(params->utils);
} else {
/* No output token, send an empty string */
*serverout = GSSAPI_BLANK_STRING;
*serveroutlen = 0;
}
if (maj_stat == GSS_S_CONTINUE_NEEDED) {
/* Context isn't complete */
return SASL_CONTINUE;
}
assert(maj_stat == GSS_S_COMPLETE);
/* When GSS_Accept_sec_context returns GSS_S_COMPLETE, the server
examines the context to ensure that it provides a level of protection
permitted by the server's security policy. In particular, if the
integ_avail flag is not set in the context, then no security layer
can be offered or accepted. If the conf_avail flag is not set in the
context, then no security layer with confidentiality can be offered
or accepted. */
if ((out_flags & GSS_C_INTEG_FLAG) == 0) {
/* if the integ_avail flag is not set in the context,
then no security layer can be offered or accepted. */
text->qop = LAYER_NONE;
} else if ((out_flags & GSS_C_CONF_FLAG) == 0) {
/* If the conf_avail flag is not set in the context,
then no security layer with confidentiality can be offered
or accepted. */
text->qop = LAYER_NONE | LAYER_INTEGRITY;
} else {
text->qop = LAYER_NONE | LAYER_INTEGRITY | LAYER_CONFIDENTIALITY;
}
if ((params->props.security_flags & SASL_SEC_PASS_CREDENTIALS) &&
(!(out_flags & GSS_C_DELEG_FLAG) ||
text->client_creds == GSS_C_NO_CREDENTIAL) )
{
text->utils->seterror(text->utils->conn, SASL_LOG_WARN,
"GSSAPI warning: no credentials were passed");
/* continue with authentication */
}
GSS_LOCK_MUTEX(params->utils);
maj_stat = gss_canonicalize_name(&min_stat,
text->client_name,
mech_type,
&client_name_MN);
GSS_UNLOCK_MUTEX(params->utils);
if (GSS_ERROR(maj_stat)) {
SETERROR(text->utils, "GSSAPI Failure: gss_canonicalize_name");
sasl_gss_free_context_contents(text);
return SASL_BADAUTH;
}
name_token.value = NULL;
name_without_realm.value = NULL;
GSS_LOCK_MUTEX(params->utils);
maj_stat = gss_display_name (&min_stat,
client_name_MN,
&name_token,
NULL);
GSS_UNLOCK_MUTEX(params->utils);
if (GSS_ERROR(maj_stat)) {
SETERROR(text->utils, "GSSAPI Failure: gss_display_name");
sasl_gss_free_context_contents(text);
ret = SASL_BADAUTH;
goto cleanup;
}
/* If the id contains a realm get the identifier for the user
without the realm and see if it's the same id (i.e.
tmartin == tmartin@ANDREW.CMU.EDU. If this is the case we just want
to return the id (i.e. just "tmartin" */
if (strchr((char *) name_token.value, (int) '@') != NULL) {
/* NOTE: libc malloc, as it is freed below by a gssapi internal
* function! */
name_without_realm.value = params->utils->malloc(strlen(name_token.value)+1);
if (name_without_realm.value == NULL) {
MEMERROR(text->utils);
ret = SASL_NOMEM;
goto cleanup;
}
strcpy(name_without_realm.value, name_token.value);
/* cut off string at '@' */
(strchr(name_without_realm.value,'@'))[0] = '\0';
name_without_realm.length = strlen( (char *) name_without_realm.value );
GSS_LOCK_MUTEX(params->utils);
maj_stat = gss_import_name (&min_stat,
&name_without_realm,
/* Solaris 8/9 gss_import_name doesn't accept GSS_C_NULL_OID here,
so use GSS_C_NT_USER_NAME instead if available. */
#ifdef HAVE_GSS_C_NT_USER_NAME
GSS_C_NT_USER_NAME,
#else
GSS_C_NULL_OID,
#endif
&without);
GSS_UNLOCK_MUTEX(params->utils);
if (GSS_ERROR(maj_stat)) {
SETERROR(text->utils, "GSSAPI Failure: gss_import_name");
sasl_gss_free_context_contents(text);
ret = SASL_BADAUTH;
goto cleanup;
}
GSS_LOCK_MUTEX(params->utils);
maj_stat = gss_compare_name(&min_stat,
client_name_MN,
without,
&equal);
GSS_UNLOCK_MUTEX(params->utils);
if (GSS_ERROR(maj_stat)) {
SETERROR(text->utils, "GSSAPI Failure: gss_compare_name");
sasl_gss_free_context_contents(text);
ret = SASL_BADAUTH;
goto cleanup;
}
} else {
equal = 0;
}
if (equal) {
text->authid = strdup(name_without_realm.value);
} else {
text->authid = strdup(name_token.value);
}
if (text->authid == NULL) {
MEMERROR(params->utils);
ret = SASL_NOMEM;
goto cleanup;
}
if (text->http_mode) {
/* HTTP doesn't do any ssf negotiation */
text->state = SASL_GSSAPI_STATE_AUTHENTICATED;
ret = SASL_OK;
} else if (text->mech_type && text->mech_type == &gss_spnego_oid) {
ret = gssapi_spnego_ssf(text, params->utils, ¶ms->props, oparams);
} else {
/* Switch to ssf negotiation */
text->state = SASL_GSSAPI_STATE_SSFCAP;
ret = SASL_CONTINUE;
}